]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
* config/tc-mips.c (md_begin): Add $fcc registers to the symbol
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
e1fddb6b 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
7898deda 3 Free Software Foundation, Inc.
252b5132 4
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
252b5132 11
5e8d7549
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
5e8d7549
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132 20
661a3fd4 21/* SECTION
47d9a591 22
252b5132
RH
23 ELF backends
24
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
28
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
661a3fd4 31 haven't bothered yet. */
252b5132 32
7ee38065
MS
33/* For sparc64-cross-sparc32. */
34#define _SYSCALL32
252b5132
RH
35#include "bfd.h"
36#include "sysdep.h"
37#include "bfdlink.h"
38#include "libbfd.h"
39#define ARCH_SIZE 0
40#include "elf-bfd.h"
e0e8c97f 41#include "libiberty.h"
252b5132
RH
42
43static INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
45static boolean map_sections_to_segments PARAMS ((bfd *));
46static int elf_sort_sections PARAMS ((const PTR, const PTR));
47static boolean assign_file_positions_for_segments PARAMS ((bfd *));
48static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
49static boolean prep_headers PARAMS ((bfd *));
50static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
51static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
dc810e39 52static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
b885599b 53static const char *group_signature PARAMS ((bfd *, Elf_Internal_Shdr *));
dbb410c3 54static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
d3c456e9 55static void merge_sections_remove_hook PARAMS ((bfd *, asection *));
252b5132
RH
56static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
57static boolean assign_section_numbers PARAMS ((bfd *));
58static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
59static boolean elf_map_symbols PARAMS ((bfd *));
60static bfd_size_type get_program_header_size PARAMS ((bfd *));
dc810e39 61static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
a7b97311
AM
62static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
63 bfd_vma, const char **,
64 const char **));
65static int elfcore_make_pid PARAMS ((bfd *));
66static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
67static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
68 Elf_Internal_Note *));
69static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
70static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
71static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
252b5132 72
50b2bdb7
AM
73static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *));
74static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *,
75 Elf_Internal_Note *));
76static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *));
77
252b5132
RH
78/* Swap version information in and out. The version information is
79 currently size independent. If that ever changes, this code will
80 need to move into elfcode.h. */
81
82/* Swap in a Verdef structure. */
83
84void
85_bfd_elf_swap_verdef_in (abfd, src, dst)
86 bfd *abfd;
87 const Elf_External_Verdef *src;
88 Elf_Internal_Verdef *dst;
89{
dc810e39
AM
90 dst->vd_version = H_GET_16 (abfd, src->vd_version);
91 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
92 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
93 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
94 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
95 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
96 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
97}
98
99/* Swap out a Verdef structure. */
100
101void
102_bfd_elf_swap_verdef_out (abfd, src, dst)
103 bfd *abfd;
104 const Elf_Internal_Verdef *src;
105 Elf_External_Verdef *dst;
106{
dc810e39
AM
107 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
108 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
109 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
110 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
111 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
112 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
113 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
114}
115
116/* Swap in a Verdaux structure. */
117
118void
119_bfd_elf_swap_verdaux_in (abfd, src, dst)
120 bfd *abfd;
121 const Elf_External_Verdaux *src;
122 Elf_Internal_Verdaux *dst;
123{
dc810e39
AM
124 dst->vda_name = H_GET_32 (abfd, src->vda_name);
125 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
126}
127
128/* Swap out a Verdaux structure. */
129
130void
131_bfd_elf_swap_verdaux_out (abfd, src, dst)
132 bfd *abfd;
133 const Elf_Internal_Verdaux *src;
134 Elf_External_Verdaux *dst;
135{
dc810e39
AM
136 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
137 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
138}
139
140/* Swap in a Verneed structure. */
141
142void
143_bfd_elf_swap_verneed_in (abfd, src, dst)
144 bfd *abfd;
145 const Elf_External_Verneed *src;
146 Elf_Internal_Verneed *dst;
147{
dc810e39
AM
148 dst->vn_version = H_GET_16 (abfd, src->vn_version);
149 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
150 dst->vn_file = H_GET_32 (abfd, src->vn_file);
151 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
152 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
153}
154
155/* Swap out a Verneed structure. */
156
157void
158_bfd_elf_swap_verneed_out (abfd, src, dst)
159 bfd *abfd;
160 const Elf_Internal_Verneed *src;
161 Elf_External_Verneed *dst;
162{
dc810e39
AM
163 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
164 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
165 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
166 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
167 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
168}
169
170/* Swap in a Vernaux structure. */
171
172void
173_bfd_elf_swap_vernaux_in (abfd, src, dst)
174 bfd *abfd;
175 const Elf_External_Vernaux *src;
176 Elf_Internal_Vernaux *dst;
177{
dc810e39
AM
178 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
179 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
180 dst->vna_other = H_GET_16 (abfd, src->vna_other);
181 dst->vna_name = H_GET_32 (abfd, src->vna_name);
182 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
183}
184
185/* Swap out a Vernaux structure. */
186
187void
188_bfd_elf_swap_vernaux_out (abfd, src, dst)
189 bfd *abfd;
190 const Elf_Internal_Vernaux *src;
191 Elf_External_Vernaux *dst;
192{
dc810e39
AM
193 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
194 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
195 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
196 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
197 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
198}
199
200/* Swap in a Versym structure. */
201
202void
203_bfd_elf_swap_versym_in (abfd, src, dst)
204 bfd *abfd;
205 const Elf_External_Versym *src;
206 Elf_Internal_Versym *dst;
207{
dc810e39 208 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
209}
210
211/* Swap out a Versym structure. */
212
213void
214_bfd_elf_swap_versym_out (abfd, src, dst)
215 bfd *abfd;
216 const Elf_Internal_Versym *src;
217 Elf_External_Versym *dst;
218{
dc810e39 219 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
220}
221
222/* Standard ELF hash function. Do not change this function; you will
223 cause invalid hash tables to be generated. */
3a99b017 224
252b5132 225unsigned long
3a99b017
ILT
226bfd_elf_hash (namearg)
227 const char *namearg;
252b5132 228{
3a99b017 229 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
230 unsigned long h = 0;
231 unsigned long g;
232 int ch;
233
234 while ((ch = *name++) != '\0')
235 {
236 h = (h << 4) + ch;
237 if ((g = (h & 0xf0000000)) != 0)
238 {
239 h ^= g >> 24;
240 /* The ELF ABI says `h &= ~g', but this is equivalent in
241 this case and on some machines one insn instead of two. */
242 h ^= g;
243 }
244 }
245 return h;
246}
247
248/* Read a specified number of bytes at a specified offset in an ELF
249 file, into a newly allocated buffer, and return a pointer to the
c044fabd 250 buffer. */
252b5132
RH
251
252static char *
253elf_read (abfd, offset, size)
c044fabd 254 bfd *abfd;
dc810e39
AM
255 file_ptr offset;
256 bfd_size_type size;
252b5132
RH
257{
258 char *buf;
259
260 if ((buf = bfd_alloc (abfd, size)) == NULL)
261 return NULL;
dc810e39 262 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
252b5132 263 return NULL;
dc810e39 264 if (bfd_bread ((PTR) buf, size, abfd) != size)
252b5132
RH
265 {
266 if (bfd_get_error () != bfd_error_system_call)
267 bfd_set_error (bfd_error_file_truncated);
268 return NULL;
269 }
270 return buf;
271}
272
273boolean
274bfd_elf_mkobject (abfd)
c044fabd 275 bfd *abfd;
252b5132 276{
c044fabd
KH
277 /* This just does initialization. */
278 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
dc810e39
AM
279 bfd_size_type amt = sizeof (struct elf_obj_tdata);
280 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
252b5132
RH
281 if (elf_tdata (abfd) == 0)
282 return false;
c044fabd
KH
283 /* Since everything is done at close time, do we need any
284 initialization? */
252b5132
RH
285
286 return true;
287}
288
289boolean
290bfd_elf_mkcorefile (abfd)
c044fabd 291 bfd *abfd;
252b5132 292{
c044fabd 293 /* I think this can be done just like an object file. */
252b5132
RH
294 return bfd_elf_mkobject (abfd);
295}
296
297char *
298bfd_elf_get_str_section (abfd, shindex)
c044fabd 299 bfd *abfd;
252b5132
RH
300 unsigned int shindex;
301{
302 Elf_Internal_Shdr **i_shdrp;
303 char *shstrtab = NULL;
dc810e39
AM
304 file_ptr offset;
305 bfd_size_type shstrtabsize;
252b5132
RH
306
307 i_shdrp = elf_elfsections (abfd);
308 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
309 return 0;
310
311 shstrtab = (char *) i_shdrp[shindex]->contents;
312 if (shstrtab == NULL)
313 {
c044fabd 314 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
315 offset = i_shdrp[shindex]->sh_offset;
316 shstrtabsize = i_shdrp[shindex]->sh_size;
317 shstrtab = elf_read (abfd, offset, shstrtabsize);
318 i_shdrp[shindex]->contents = (PTR) shstrtab;
319 }
320 return shstrtab;
321}
322
323char *
324bfd_elf_string_from_elf_section (abfd, shindex, strindex)
c044fabd 325 bfd *abfd;
252b5132
RH
326 unsigned int shindex;
327 unsigned int strindex;
328{
329 Elf_Internal_Shdr *hdr;
330
331 if (strindex == 0)
332 return "";
333
334 hdr = elf_elfsections (abfd)[shindex];
335
336 if (hdr->contents == NULL
337 && bfd_elf_get_str_section (abfd, shindex) == NULL)
338 return NULL;
339
340 if (strindex >= hdr->sh_size)
341 {
342 (*_bfd_error_handler)
343 (_("%s: invalid string offset %u >= %lu for section `%s'"),
8f615d07 344 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
252b5132
RH
345 ((shindex == elf_elfheader(abfd)->e_shstrndx
346 && strindex == hdr->sh_name)
347 ? ".shstrtab"
348 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
349 return "";
350 }
351
352 return ((char *) hdr->contents) + strindex;
353}
354
6cdc0ccc
AM
355/* Read and convert symbols to internal format.
356 SYMCOUNT specifies the number of symbols to read, starting from
357 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
358 are non-NULL, they are used to store the internal symbols, external
359 symbols, and symbol section index extensions, respectively. */
360
361Elf_Internal_Sym *
362bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset,
363 intsym_buf, extsym_buf, extshndx_buf)
364 bfd *ibfd;
365 Elf_Internal_Shdr *symtab_hdr;
366 size_t symcount;
367 size_t symoffset;
368 Elf_Internal_Sym *intsym_buf;
369 PTR extsym_buf;
370 Elf_External_Sym_Shndx *extshndx_buf;
371{
372 Elf_Internal_Shdr *shndx_hdr;
373 PTR alloc_ext;
df622259 374 const bfd_byte *esym;
6cdc0ccc
AM
375 Elf_External_Sym_Shndx *alloc_extshndx;
376 Elf_External_Sym_Shndx *shndx;
377 Elf_Internal_Sym *isym;
378 Elf_Internal_Sym *isymend;
379 struct elf_backend_data *bed;
380 size_t extsym_size;
381 bfd_size_type amt;
382 file_ptr pos;
383
384 if (symcount == 0)
385 return intsym_buf;
386
387 /* Normal syms might have section extension entries. */
388 shndx_hdr = NULL;
389 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
390 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
391
392 /* Read the symbols. */
393 alloc_ext = NULL;
394 alloc_extshndx = NULL;
395 bed = get_elf_backend_data (ibfd);
396 extsym_size = bed->s->sizeof_sym;
397 amt = symcount * extsym_size;
398 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
399 if (extsym_buf == NULL)
400 {
401 alloc_ext = bfd_malloc (amt);
402 extsym_buf = alloc_ext;
403 }
404 if (extsym_buf == NULL
405 || bfd_seek (ibfd, pos, SEEK_SET) != 0
406 || bfd_bread (extsym_buf, amt, ibfd) != amt)
407 {
408 intsym_buf = NULL;
409 goto out;
410 }
411
412 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
413 extshndx_buf = NULL;
414 else
415 {
416 amt = symcount * sizeof (Elf_External_Sym_Shndx);
417 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
418 if (extshndx_buf == NULL)
419 {
420 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
421 extshndx_buf = alloc_extshndx;
422 }
423 if (extshndx_buf == NULL
424 || bfd_seek (ibfd, pos, SEEK_SET) != 0
425 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
426 {
427 intsym_buf = NULL;
428 goto out;
429 }
430 }
431
432 if (intsym_buf == NULL)
433 {
434 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
435 intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt);
436 if (intsym_buf == NULL)
437 goto out;
438 }
439
440 /* Convert the symbols to internal form. */
441 isymend = intsym_buf + symcount;
442 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
443 isym < isymend;
444 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
445 (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym);
446
447 out:
448 if (alloc_ext != NULL)
449 free (alloc_ext);
450 if (alloc_extshndx != NULL)
451 free (alloc_extshndx);
452
453 return intsym_buf;
454}
455
dbb410c3
AM
456/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
457 sections. The first element is the flags, the rest are section
458 pointers. */
459
460typedef union elf_internal_group {
461 Elf_Internal_Shdr *shdr;
462 unsigned int flags;
463} Elf_Internal_Group;
464
b885599b
AM
465/* Return the name of the group signature symbol. Why isn't the
466 signature just a string? */
467
468static const char *
469group_signature (abfd, ghdr)
470 bfd *abfd;
471 Elf_Internal_Shdr *ghdr;
472{
9dce4196 473 Elf_Internal_Shdr *hdr;
9dce4196
AM
474 unsigned char esym[sizeof (Elf64_External_Sym)];
475 Elf_External_Sym_Shndx eshndx;
476 Elf_Internal_Sym isym;
477 unsigned int iname;
478 unsigned int shindex;
b885599b
AM
479
480 /* First we need to ensure the symbol table is available. */
481 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
482 return NULL;
483
9dce4196
AM
484 /* Go read the symbol. */
485 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
486 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
487 &isym, esym, &eshndx) == NULL)
b885599b 488 return NULL;
9dce4196 489
9dce4196
AM
490 /* Look up the symbol name. */
491 iname = isym.st_name;
492 shindex = hdr->sh_link;
493 if (iname == 0 && ELF_ST_TYPE (isym.st_info) == STT_SECTION)
494 {
495 iname = elf_elfsections (abfd)[isym.st_shndx]->sh_name;
496 shindex = elf_elfheader (abfd)->e_shstrndx;
497 }
498
499 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
b885599b
AM
500}
501
dbb410c3
AM
502/* Set next_in_group list pointer, and group name for NEWSECT. */
503
504static boolean
505setup_group (abfd, hdr, newsect)
506 bfd *abfd;
507 Elf_Internal_Shdr *hdr;
508 asection *newsect;
509{
510 unsigned int num_group = elf_tdata (abfd)->num_group;
511
512 /* If num_group is zero, read in all SHT_GROUP sections. The count
513 is set to -1 if there are no SHT_GROUP sections. */
514 if (num_group == 0)
515 {
516 unsigned int i, shnum;
517
518 /* First count the number of groups. If we have a SHT_GROUP
519 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 520 shnum = elf_numsections (abfd);
dbb410c3
AM
521 num_group = 0;
522 for (i = 0; i < shnum; i++)
523 {
524 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
525 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
526 num_group += 1;
527 }
528
529 if (num_group == 0)
973ffd63 530 num_group = (unsigned) -1;
dbb410c3
AM
531 elf_tdata (abfd)->num_group = num_group;
532
533 if (num_group > 0)
534 {
535 /* We keep a list of elf section headers for group sections,
536 so we can find them quickly. */
537 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
538 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
539 if (elf_tdata (abfd)->group_sect_ptr == NULL)
540 return false;
541
542 num_group = 0;
543 for (i = 0; i < shnum; i++)
544 {
545 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
546 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
547 {
973ffd63 548 unsigned char *src;
dbb410c3
AM
549 Elf_Internal_Group *dest;
550
551 /* Add to list of sections. */
552 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
553 num_group += 1;
554
555 /* Read the raw contents. */
556 BFD_ASSERT (sizeof (*dest) >= 4);
557 amt = shdr->sh_size * sizeof (*dest) / 4;
558 shdr->contents = bfd_alloc (abfd, amt);
559 if (shdr->contents == NULL
560 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
561 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
562 != shdr->sh_size))
563 return false;
564
565 /* Translate raw contents, a flag word followed by an
566 array of elf section indices all in target byte order,
567 to the flag word followed by an array of elf section
568 pointers. */
569 src = shdr->contents + shdr->sh_size;
570 dest = (Elf_Internal_Group *) (shdr->contents + amt);
571 while (1)
572 {
573 unsigned int idx;
574
575 src -= 4;
576 --dest;
577 idx = H_GET_32 (abfd, src);
578 if (src == shdr->contents)
579 {
580 dest->flags = idx;
b885599b
AM
581 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
582 shdr->bfd_section->flags
583 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
584 break;
585 }
586 if (idx >= shnum)
587 {
588 ((*_bfd_error_handler)
589 (_("%s: invalid SHT_GROUP entry"),
590 bfd_archive_filename (abfd)));
591 idx = 0;
592 }
593 dest->shdr = elf_elfsections (abfd)[idx];
594 }
595 }
596 }
597 }
598 }
599
600 if (num_group != (unsigned) -1)
601 {
602 unsigned int i;
603
604 for (i = 0; i < num_group; i++)
605 {
606 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
607 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
608 unsigned int n_elt = shdr->sh_size / 4;
609
610 /* Look through this group's sections to see if current
611 section is a member. */
612 while (--n_elt != 0)
613 if ((++idx)->shdr == hdr)
614 {
e0e8c97f 615 asection *s = NULL;
dbb410c3
AM
616
617 /* We are a member of this group. Go looking through
618 other members to see if any others are linked via
619 next_in_group. */
620 idx = (Elf_Internal_Group *) shdr->contents;
621 n_elt = shdr->sh_size / 4;
622 while (--n_elt != 0)
623 if ((s = (++idx)->shdr->bfd_section) != NULL
945906ff 624 && elf_next_in_group (s) != NULL)
dbb410c3
AM
625 break;
626 if (n_elt != 0)
627 {
dbb410c3
AM
628 /* Snarf the group name from other member, and
629 insert current section in circular list. */
945906ff
AM
630 elf_group_name (newsect) = elf_group_name (s);
631 elf_next_in_group (newsect) = elf_next_in_group (s);
632 elf_next_in_group (s) = newsect;
dbb410c3
AM
633 }
634 else
635 {
dbb410c3
AM
636 const char *gname;
637
b885599b
AM
638 gname = group_signature (abfd, shdr);
639 if (gname == NULL)
dbb410c3 640 return false;
945906ff 641 elf_group_name (newsect) = gname;
dbb410c3
AM
642
643 /* Start a circular list with one element. */
945906ff 644 elf_next_in_group (newsect) = newsect;
dbb410c3 645 }
b885599b 646
9dce4196
AM
647 /* If the group section has been created, point to the
648 new member. */
dbb410c3 649 if (shdr->bfd_section != NULL)
945906ff 650 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 651
dbb410c3
AM
652 i = num_group - 1;
653 break;
654 }
655 }
656 }
657
945906ff 658 if (elf_group_name (newsect) == NULL)
dbb410c3
AM
659 {
660 (*_bfd_error_handler) (_("%s: no group info for section %s"),
661 bfd_archive_filename (abfd), newsect->name);
662 }
663 return true;
664}
665
e61463e1 666boolean
b885599b
AM
667bfd_elf_discard_group (abfd, group)
668 bfd *abfd ATTRIBUTE_UNUSED;
669 asection *group;
670{
671 asection *first = elf_next_in_group (group);
672 asection *s = first;
673
674 while (s != NULL)
675 {
676 s->output_section = bfd_abs_section_ptr;
677 s = elf_next_in_group (s);
678 /* These lists are circular. */
679 if (s == first)
680 break;
681 }
e61463e1 682 return true;
b885599b
AM
683}
684
252b5132
RH
685/* Make a BFD section from an ELF section. We store a pointer to the
686 BFD section in the bfd_section field of the header. */
687
688boolean
689_bfd_elf_make_section_from_shdr (abfd, hdr, name)
690 bfd *abfd;
691 Elf_Internal_Shdr *hdr;
692 const char *name;
693{
694 asection *newsect;
695 flagword flags;
fa152c49 696 struct elf_backend_data *bed;
252b5132
RH
697
698 if (hdr->bfd_section != NULL)
699 {
700 BFD_ASSERT (strcmp (name,
701 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
702 return true;
703 }
704
705 newsect = bfd_make_section_anyway (abfd, name);
706 if (newsect == NULL)
707 return false;
708
709 newsect->filepos = hdr->sh_offset;
710
711 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
712 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
713 || ! bfd_set_section_alignment (abfd, newsect,
dc810e39 714 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
252b5132
RH
715 return false;
716
717 flags = SEC_NO_FLAGS;
718 if (hdr->sh_type != SHT_NOBITS)
719 flags |= SEC_HAS_CONTENTS;
dbb410c3
AM
720 if (hdr->sh_type == SHT_GROUP)
721 flags |= SEC_GROUP | SEC_EXCLUDE;
252b5132
RH
722 if ((hdr->sh_flags & SHF_ALLOC) != 0)
723 {
724 flags |= SEC_ALLOC;
725 if (hdr->sh_type != SHT_NOBITS)
726 flags |= SEC_LOAD;
727 }
728 if ((hdr->sh_flags & SHF_WRITE) == 0)
729 flags |= SEC_READONLY;
730 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
731 flags |= SEC_CODE;
732 else if ((flags & SEC_LOAD) != 0)
733 flags |= SEC_DATA;
f5fa8ca2
JJ
734 if ((hdr->sh_flags & SHF_MERGE) != 0)
735 {
736 flags |= SEC_MERGE;
737 newsect->entsize = hdr->sh_entsize;
738 if ((hdr->sh_flags & SHF_STRINGS) != 0)
739 flags |= SEC_STRINGS;
740 }
dbb410c3
AM
741 if (hdr->sh_flags & SHF_GROUP)
742 if (!setup_group (abfd, hdr, newsect))
743 return false;
13ae64f3
JJ
744 if ((hdr->sh_flags & SHF_TLS) != 0)
745 flags |= SEC_THREAD_LOCAL;
252b5132
RH
746
747 /* The debugging sections appear to be recognized only by name, not
748 any sort of flag. */
7a6cc5fb 749 {
dbf48117 750 static const char *debug_sec_names [] =
7a6cc5fb
NC
751 {
752 ".debug",
753 ".gnu.linkonce.wi.",
754 ".line",
755 ".stab"
756 };
757 int i;
758
e0e8c97f 759 for (i = ARRAY_SIZE (debug_sec_names); i--;)
7a6cc5fb
NC
760 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
761 break;
762
763 if (i >= 0)
764 flags |= SEC_DEBUGGING;
765 }
252b5132
RH
766
767 /* As a GNU extension, if the name begins with .gnu.linkonce, we
768 only link a single copy of the section. This is used to support
769 g++. g++ will emit each template expansion in its own section.
770 The symbols will be defined as weak, so that multiple definitions
771 are permitted. The GNU linker extension is to actually discard
772 all but one of the sections. */
b885599b
AM
773 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
774 && elf_next_in_group (newsect) == NULL)
252b5132
RH
775 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
776
fa152c49
JW
777 bed = get_elf_backend_data (abfd);
778 if (bed->elf_backend_section_flags)
779 if (! bed->elf_backend_section_flags (&flags, hdr))
780 return false;
781
252b5132
RH
782 if (! bfd_set_section_flags (abfd, newsect, flags))
783 return false;
784
785 if ((flags & SEC_ALLOC) != 0)
786 {
787 Elf_Internal_Phdr *phdr;
788 unsigned int i;
789
790 /* Look through the phdrs to see if we need to adjust the lma.
791 If all the p_paddr fields are zero, we ignore them, since
792 some ELF linkers produce such output. */
793 phdr = elf_tdata (abfd)->phdr;
794 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
795 {
796 if (phdr->p_paddr != 0)
797 break;
798 }
799 if (i < elf_elfheader (abfd)->e_phnum)
800 {
801 phdr = elf_tdata (abfd)->phdr;
802 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
803 {
e0e8c97f
NC
804 /* This section is part of this segment if its file
805 offset plus size lies within the segment's memory
806 span and, if the section is loaded, the extent of the
47d9a591 807 loaded data lies within the extent of the segment.
bf36db18
NC
808
809 Note - we used to check the p_paddr field as well, and
810 refuse to set the LMA if it was 0. This is wrong
dba143ef 811 though, as a perfectly valid initialised segment can
bf36db18 812 have a p_paddr of zero. Some architectures, eg ARM,
dba143ef 813 place special significance on the address 0 and
bf36db18
NC
814 executables need to be able to have a segment which
815 covers this address. */
252b5132 816 if (phdr->p_type == PT_LOAD
e0e8c97f
NC
817 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
818 && (hdr->sh_offset + hdr->sh_size
819 <= phdr->p_offset + phdr->p_memsz)
252b5132 820 && ((flags & SEC_LOAD) == 0
d7866f04
AM
821 || (hdr->sh_offset + hdr->sh_size
822 <= phdr->p_offset + phdr->p_filesz)))
252b5132 823 {
dba143ef 824 if ((flags & SEC_LOAD) == 0)
d7866f04
AM
825 newsect->lma = (phdr->p_paddr
826 + hdr->sh_addr - phdr->p_vaddr);
dba143ef
AM
827 else
828 /* We used to use the same adjustment for SEC_LOAD
829 sections, but that doesn't work if the segment
830 is packed with code from multiple VMAs.
831 Instead we calculate the section LMA based on
832 the segment LMA. It is assumed that the
833 segment will contain sections with contiguous
834 LMAs, even if the VMAs are not. */
835 newsect->lma = (phdr->p_paddr
836 + hdr->sh_offset - phdr->p_offset);
d7866f04
AM
837
838 /* With contiguous segments, we can't tell from file
839 offsets whether a section with zero size should
840 be placed at the end of one segment or the
841 beginning of the next. Decide based on vaddr. */
842 if (hdr->sh_addr >= phdr->p_vaddr
843 && (hdr->sh_addr + hdr->sh_size
844 <= phdr->p_vaddr + phdr->p_memsz))
845 break;
252b5132
RH
846 }
847 }
848 }
849 }
850
851 hdr->bfd_section = newsect;
852 elf_section_data (newsect)->this_hdr = *hdr;
853
854 return true;
855}
856
857/*
858INTERNAL_FUNCTION
859 bfd_elf_find_section
860
861SYNOPSIS
862 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
863
864DESCRIPTION
865 Helper functions for GDB to locate the string tables.
866 Since BFD hides string tables from callers, GDB needs to use an
867 internal hook to find them. Sun's .stabstr, in particular,
868 isn't even pointed to by the .stab section, so ordinary
869 mechanisms wouldn't work to find it, even if we had some.
870*/
871
872struct elf_internal_shdr *
873bfd_elf_find_section (abfd, name)
c044fabd 874 bfd *abfd;
252b5132
RH
875 char *name;
876{
877 Elf_Internal_Shdr **i_shdrp;
878 char *shstrtab;
879 unsigned int max;
880 unsigned int i;
881
882 i_shdrp = elf_elfsections (abfd);
883 if (i_shdrp != NULL)
884 {
9ad5cbcf
AM
885 shstrtab = bfd_elf_get_str_section (abfd,
886 elf_elfheader (abfd)->e_shstrndx);
252b5132
RH
887 if (shstrtab != NULL)
888 {
9ad5cbcf 889 max = elf_numsections (abfd);
252b5132
RH
890 for (i = 1; i < max; i++)
891 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
892 return i_shdrp[i];
893 }
894 }
895 return 0;
896}
897
898const char *const bfd_elf_section_type_names[] = {
899 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
900 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
901 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
902};
903
904/* ELF relocs are against symbols. If we are producing relocateable
905 output, and the reloc is against an external symbol, and nothing
906 has given us any additional addend, the resulting reloc will also
907 be against the same symbol. In such a case, we don't want to
908 change anything about the way the reloc is handled, since it will
909 all be done at final link time. Rather than put special case code
910 into bfd_perform_relocation, all the reloc types use this howto
911 function. It just short circuits the reloc if producing
912 relocateable output against an external symbol. */
913
252b5132
RH
914bfd_reloc_status_type
915bfd_elf_generic_reloc (abfd,
916 reloc_entry,
917 symbol,
918 data,
919 input_section,
920 output_bfd,
921 error_message)
7442e600 922 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
923 arelent *reloc_entry;
924 asymbol *symbol;
7442e600 925 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
926 asection *input_section;
927 bfd *output_bfd;
7442e600 928 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
929{
930 if (output_bfd != (bfd *) NULL
931 && (symbol->flags & BSF_SECTION_SYM) == 0
932 && (! reloc_entry->howto->partial_inplace
933 || reloc_entry->addend == 0))
934 {
935 reloc_entry->address += input_section->output_offset;
936 return bfd_reloc_ok;
937 }
938
939 return bfd_reloc_continue;
940}
941\f
d3c456e9
JJ
942/* Make sure sec_info_type is cleared if sec_info is cleared too. */
943
944static void
945merge_sections_remove_hook (abfd, sec)
946 bfd *abfd ATTRIBUTE_UNUSED;
947 asection *sec;
948{
949 struct bfd_elf_section_data *sec_data;
47d9a591 950
d3c456e9
JJ
951 sec_data = elf_section_data (sec);
952 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);
953 sec_data->sec_info_type = ELF_INFO_TYPE_NONE;
954}
955
8550eb6e
JJ
956/* Finish SHF_MERGE section merging. */
957
958boolean
959_bfd_elf_merge_sections (abfd, info)
960 bfd *abfd;
961 struct bfd_link_info *info;
962{
b0f35f36 963 if (!is_elf_hash_table (info))
8ea2e4bd 964 return false;
b0f35f36 965 if (elf_hash_table (info)->merge_info)
d3c456e9
JJ
966 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
967 merge_sections_remove_hook);
8550eb6e
JJ
968 return true;
969}
2d653fc7
AM
970
971void
972_bfd_elf_link_just_syms (sec, info)
973 asection *sec;
974 struct bfd_link_info *info;
975{
976 sec->output_section = bfd_abs_section_ptr;
977 sec->output_offset = sec->vma;
978 if (!is_elf_hash_table (info))
979 return;
980
981 elf_section_data (sec)->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
982}
8550eb6e 983\f
0ac4564e
L
984/* Copy the program header and other data from one object module to
985 another. */
252b5132 986
2d502050
L
987boolean
988_bfd_elf_copy_private_bfd_data (ibfd, obfd)
989 bfd *ibfd;
990 bfd *obfd;
991{
992 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
993 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
994 return true;
995
996 BFD_ASSERT (!elf_flags_init (obfd)
997 || (elf_elfheader (obfd)->e_flags
998 == elf_elfheader (ibfd)->e_flags));
999
0ac4564e 1000 elf_gp (obfd) = elf_gp (ibfd);
2d502050
L
1001 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1002 elf_flags_init (obfd) = true;
1003 return true;
1004}
1005
f0b79d91
L
1006/* Print out the program headers. */
1007
252b5132
RH
1008boolean
1009_bfd_elf_print_private_bfd_data (abfd, farg)
1010 bfd *abfd;
1011 PTR farg;
1012{
1013 FILE *f = (FILE *) farg;
1014 Elf_Internal_Phdr *p;
1015 asection *s;
1016 bfd_byte *dynbuf = NULL;
1017
1018 p = elf_tdata (abfd)->phdr;
1019 if (p != NULL)
1020 {
1021 unsigned int i, c;
1022
1023 fprintf (f, _("\nProgram Header:\n"));
1024 c = elf_elfheader (abfd)->e_phnum;
1025 for (i = 0; i < c; i++, p++)
1026 {
dc810e39 1027 const char *pt;
252b5132
RH
1028 char buf[20];
1029
1030 switch (p->p_type)
1031 {
dc810e39
AM
1032 case PT_NULL: pt = "NULL"; break;
1033 case PT_LOAD: pt = "LOAD"; break;
1034 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1035 case PT_INTERP: pt = "INTERP"; break;
1036 case PT_NOTE: pt = "NOTE"; break;
1037 case PT_SHLIB: pt = "SHLIB"; break;
1038 case PT_PHDR: pt = "PHDR"; break;
13ae64f3 1039 case PT_TLS: pt = "TLS"; break;
65765700 1040 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
dc810e39 1041 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
252b5132 1042 }
dc810e39 1043 fprintf (f, "%8s off 0x", pt);
60b89a18 1044 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1045 fprintf (f, " vaddr 0x");
60b89a18 1046 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1047 fprintf (f, " paddr 0x");
60b89a18 1048 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1049 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1050 fprintf (f, " filesz 0x");
60b89a18 1051 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1052 fprintf (f, " memsz 0x");
60b89a18 1053 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1054 fprintf (f, " flags %c%c%c",
1055 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1056 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1057 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1058 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1059 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1060 fprintf (f, "\n");
1061 }
1062 }
1063
1064 s = bfd_get_section_by_name (abfd, ".dynamic");
1065 if (s != NULL)
1066 {
1067 int elfsec;
dc810e39 1068 unsigned long shlink;
252b5132
RH
1069 bfd_byte *extdyn, *extdynend;
1070 size_t extdynsize;
1071 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1072
1073 fprintf (f, _("\nDynamic Section:\n"));
1074
1075 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1076 if (dynbuf == NULL)
1077 goto error_return;
1078 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1079 s->_raw_size))
1080 goto error_return;
1081
1082 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1083 if (elfsec == -1)
1084 goto error_return;
dc810e39 1085 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1086
1087 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1088 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1089
1090 extdyn = dynbuf;
1091 extdynend = extdyn + s->_raw_size;
1092 for (; extdyn < extdynend; extdyn += extdynsize)
1093 {
1094 Elf_Internal_Dyn dyn;
1095 const char *name;
1096 char ab[20];
1097 boolean stringp;
1098
1099 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1100
1101 if (dyn.d_tag == DT_NULL)
1102 break;
1103
1104 stringp = false;
1105 switch (dyn.d_tag)
1106 {
1107 default:
1108 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1109 name = ab;
1110 break;
1111
1112 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1113 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1114 case DT_PLTGOT: name = "PLTGOT"; break;
1115 case DT_HASH: name = "HASH"; break;
1116 case DT_STRTAB: name = "STRTAB"; break;
1117 case DT_SYMTAB: name = "SYMTAB"; break;
1118 case DT_RELA: name = "RELA"; break;
1119 case DT_RELASZ: name = "RELASZ"; break;
1120 case DT_RELAENT: name = "RELAENT"; break;
1121 case DT_STRSZ: name = "STRSZ"; break;
1122 case DT_SYMENT: name = "SYMENT"; break;
1123 case DT_INIT: name = "INIT"; break;
1124 case DT_FINI: name = "FINI"; break;
1125 case DT_SONAME: name = "SONAME"; stringp = true; break;
1126 case DT_RPATH: name = "RPATH"; stringp = true; break;
1127 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1128 case DT_REL: name = "REL"; break;
1129 case DT_RELSZ: name = "RELSZ"; break;
1130 case DT_RELENT: name = "RELENT"; break;
1131 case DT_PLTREL: name = "PLTREL"; break;
1132 case DT_DEBUG: name = "DEBUG"; break;
1133 case DT_TEXTREL: name = "TEXTREL"; break;
1134 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1135 case DT_BIND_NOW: name = "BIND_NOW"; break;
1136 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1137 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1138 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1139 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1140 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1141 case DT_FLAGS: name = "FLAGS"; break;
1142 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1143 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1144 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1145 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1146 case DT_MOVEENT: name = "MOVEENT"; break;
1147 case DT_MOVESZ: name = "MOVESZ"; break;
1148 case DT_FEATURE: name = "FEATURE"; break;
1149 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1150 case DT_SYMINSZ: name = "SYMINSZ"; break;
1151 case DT_SYMINENT: name = "SYMINENT"; break;
36a30e65
L
1152 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1153 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1154 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
94558834
L
1155 case DT_PLTPAD: name = "PLTPAD"; break;
1156 case DT_MOVETAB: name = "MOVETAB"; break;
1157 case DT_SYMINFO: name = "SYMINFO"; break;
1158 case DT_RELACOUNT: name = "RELACOUNT"; break;
1159 case DT_RELCOUNT: name = "RELCOUNT"; break;
1160 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1161 case DT_VERSYM: name = "VERSYM"; break;
1162 case DT_VERDEF: name = "VERDEF"; break;
1163 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1164 case DT_VERNEED: name = "VERNEED"; break;
1165 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
94558834
L
1166 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1167 case DT_USED: name = "USED"; break;
1168 case DT_FILTER: name = "FILTER"; stringp = true; break;
252b5132
RH
1169 }
1170
1171 fprintf (f, " %-11s ", name);
1172 if (! stringp)
1173 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1174 else
1175 {
1176 const char *string;
dc810e39 1177 unsigned int tagv = dyn.d_un.d_val;
252b5132 1178
dc810e39 1179 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1180 if (string == NULL)
1181 goto error_return;
1182 fprintf (f, "%s", string);
1183 }
1184 fprintf (f, "\n");
1185 }
1186
1187 free (dynbuf);
1188 dynbuf = NULL;
1189 }
1190
1191 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1192 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1193 {
1194 if (! _bfd_elf_slurp_version_tables (abfd))
1195 return false;
1196 }
1197
1198 if (elf_dynverdef (abfd) != 0)
1199 {
1200 Elf_Internal_Verdef *t;
1201
1202 fprintf (f, _("\nVersion definitions:\n"));
1203 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1204 {
1205 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1206 t->vd_flags, t->vd_hash, t->vd_nodename);
1207 if (t->vd_auxptr->vda_nextptr != NULL)
1208 {
1209 Elf_Internal_Verdaux *a;
1210
1211 fprintf (f, "\t");
1212 for (a = t->vd_auxptr->vda_nextptr;
1213 a != NULL;
1214 a = a->vda_nextptr)
1215 fprintf (f, "%s ", a->vda_nodename);
1216 fprintf (f, "\n");
1217 }
1218 }
1219 }
1220
1221 if (elf_dynverref (abfd) != 0)
1222 {
1223 Elf_Internal_Verneed *t;
1224
1225 fprintf (f, _("\nVersion References:\n"));
1226 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1227 {
1228 Elf_Internal_Vernaux *a;
1229
1230 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1231 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1232 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1233 a->vna_flags, a->vna_other, a->vna_nodename);
1234 }
1235 }
1236
1237 return true;
1238
1239 error_return:
1240 if (dynbuf != NULL)
1241 free (dynbuf);
1242 return false;
1243}
1244
1245/* Display ELF-specific fields of a symbol. */
1246
1247void
1248bfd_elf_print_symbol (abfd, filep, symbol, how)
1249 bfd *abfd;
1250 PTR filep;
1251 asymbol *symbol;
1252 bfd_print_symbol_type how;
1253{
1254 FILE *file = (FILE *) filep;
1255 switch (how)
1256 {
1257 case bfd_print_symbol_name:
1258 fprintf (file, "%s", symbol->name);
1259 break;
1260 case bfd_print_symbol_more:
1261 fprintf (file, "elf ");
60b89a18 1262 bfd_fprintf_vma (abfd, file, symbol->value);
252b5132
RH
1263 fprintf (file, " %lx", (long) symbol->flags);
1264 break;
1265 case bfd_print_symbol_all:
1266 {
4e8a9624
AM
1267 const char *section_name;
1268 const char *name = NULL;
587ff49e 1269 struct elf_backend_data *bed;
7a13edea 1270 unsigned char st_other;
dbb410c3 1271 bfd_vma val;
c044fabd 1272
252b5132 1273 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1274
1275 bed = get_elf_backend_data (abfd);
1276 if (bed->elf_backend_print_symbol_all)
c044fabd 1277 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1278
1279 if (name == NULL)
1280 {
7ee38065 1281 name = symbol->name;
60b89a18 1282 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
587ff49e
RH
1283 }
1284
252b5132
RH
1285 fprintf (file, " %s\t", section_name);
1286 /* Print the "other" value for a symbol. For common symbols,
1287 we've already printed the size; now print the alignment.
1288 For other symbols, we have no specified alignment, and
1289 we've printed the address; now print the size. */
dbb410c3
AM
1290 if (bfd_is_com_section (symbol->section))
1291 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1292 else
1293 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1294 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1295
1296 /* If we have version information, print it. */
1297 if (elf_tdata (abfd)->dynversym_section != 0
1298 && (elf_tdata (abfd)->dynverdef_section != 0
1299 || elf_tdata (abfd)->dynverref_section != 0))
1300 {
1301 unsigned int vernum;
1302 const char *version_string;
1303
1304 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1305
1306 if (vernum == 0)
1307 version_string = "";
1308 else if (vernum == 1)
1309 version_string = "Base";
1310 else if (vernum <= elf_tdata (abfd)->cverdefs)
1311 version_string =
1312 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1313 else
1314 {
1315 Elf_Internal_Verneed *t;
1316
1317 version_string = "";
1318 for (t = elf_tdata (abfd)->verref;
1319 t != NULL;
1320 t = t->vn_nextref)
1321 {
1322 Elf_Internal_Vernaux *a;
1323
1324 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1325 {
1326 if (a->vna_other == vernum)
1327 {
1328 version_string = a->vna_nodename;
1329 break;
1330 }
1331 }
1332 }
1333 }
1334
1335 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1336 fprintf (file, " %-11s", version_string);
1337 else
1338 {
1339 int i;
1340
1341 fprintf (file, " (%s)", version_string);
1342 for (i = 10 - strlen (version_string); i > 0; --i)
1343 putc (' ', file);
1344 }
1345 }
1346
1347 /* If the st_other field is not zero, print it. */
7a13edea 1348 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 1349
7a13edea
NC
1350 switch (st_other)
1351 {
1352 case 0: break;
1353 case STV_INTERNAL: fprintf (file, " .internal"); break;
1354 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1355 case STV_PROTECTED: fprintf (file, " .protected"); break;
1356 default:
1357 /* Some other non-defined flags are also present, so print
1358 everything hex. */
1359 fprintf (file, " 0x%02x", (unsigned int) st_other);
1360 }
252b5132 1361
587ff49e 1362 fprintf (file, " %s", name);
252b5132
RH
1363 }
1364 break;
1365 }
1366}
1367\f
1368/* Create an entry in an ELF linker hash table. */
1369
1370struct bfd_hash_entry *
1371_bfd_elf_link_hash_newfunc (entry, table, string)
1372 struct bfd_hash_entry *entry;
1373 struct bfd_hash_table *table;
1374 const char *string;
1375{
252b5132
RH
1376 /* Allocate the structure if it has not already been allocated by a
1377 subclass. */
51b64d56
AM
1378 if (entry == NULL)
1379 {
1380 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1381 if (entry == NULL)
1382 return entry;
1383 }
252b5132
RH
1384
1385 /* Call the allocation method of the superclass. */
51b64d56
AM
1386 entry = _bfd_link_hash_newfunc (entry, table, string);
1387 if (entry != NULL)
252b5132 1388 {
51b64d56
AM
1389 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1390 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1391
252b5132
RH
1392 /* Set local fields. */
1393 ret->indx = -1;
1394 ret->size = 0;
1395 ret->dynindx = -1;
1396 ret->dynstr_index = 0;
1397 ret->weakdef = NULL;
51b64d56
AM
1398 ret->got.refcount = htab->init_refcount;
1399 ret->plt.refcount = htab->init_refcount;
1400 ret->linker_section_pointer = NULL;
252b5132
RH
1401 ret->verinfo.verdef = NULL;
1402 ret->vtable_entries_used = NULL;
1403 ret->vtable_entries_size = 0;
1404 ret->vtable_parent = NULL;
1405 ret->type = STT_NOTYPE;
1406 ret->other = 0;
1407 /* Assume that we have been called by a non-ELF symbol reader.
1408 This flag is then reset by the code which reads an ELF input
1409 file. This ensures that a symbol created by a non-ELF symbol
1410 reader will have the flag set correctly. */
1411 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1412 }
1413
51b64d56 1414 return entry;
252b5132
RH
1415}
1416
2920b85c 1417/* Copy data from an indirect symbol to its direct symbol, hiding the
0a991dfe 1418 old indirect symbol. Also used for copying flags to a weakdef. */
2920b85c 1419
c61b8717 1420void
b48fa14c
AM
1421_bfd_elf_link_hash_copy_indirect (bed, dir, ind)
1422 struct elf_backend_data *bed;
2920b85c
RH
1423 struct elf_link_hash_entry *dir, *ind;
1424{
3c3e9281 1425 bfd_signed_vma tmp;
b48fa14c 1426 bfd_signed_vma lowest_valid = bed->can_refcount;
3c3e9281 1427
2920b85c
RH
1428 /* Copy down any references that we may have already seen to the
1429 symbol which just became indirect. */
1430
1431 dir->elf_link_hash_flags |=
1432 (ind->elf_link_hash_flags
1433 & (ELF_LINK_HASH_REF_DYNAMIC
1434 | ELF_LINK_HASH_REF_REGULAR
1435 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1436 | ELF_LINK_NON_GOT_REF));
1437
1e370bd2 1438 if (ind->root.type != bfd_link_hash_indirect)
0a991dfe
AM
1439 return;
1440
51b64d56 1441 /* Copy over the global and procedure linkage table refcount entries.
2920b85c 1442 These may have been already set up by a check_relocs routine. */
3c3e9281 1443 tmp = dir->got.refcount;
b48fa14c 1444 if (tmp < lowest_valid)
2920b85c 1445 {
51b64d56 1446 dir->got.refcount = ind->got.refcount;
3c3e9281 1447 ind->got.refcount = tmp;
2920b85c 1448 }
3c3e9281 1449 else
b48fa14c 1450 BFD_ASSERT (ind->got.refcount < lowest_valid);
2920b85c 1451
3c3e9281 1452 tmp = dir->plt.refcount;
b48fa14c 1453 if (tmp < lowest_valid)
2920b85c 1454 {
51b64d56 1455 dir->plt.refcount = ind->plt.refcount;
3c3e9281 1456 ind->plt.refcount = tmp;
2920b85c 1457 }
3c3e9281 1458 else
b48fa14c 1459 BFD_ASSERT (ind->plt.refcount < lowest_valid);
2920b85c
RH
1460
1461 if (dir->dynindx == -1)
1462 {
1463 dir->dynindx = ind->dynindx;
1464 dir->dynstr_index = ind->dynstr_index;
1465 ind->dynindx = -1;
1466 ind->dynstr_index = 0;
1467 }
3c3e9281
AM
1468 else
1469 BFD_ASSERT (ind->dynindx == -1);
2920b85c
RH
1470}
1471
c61b8717 1472void
e5094212
AM
1473_bfd_elf_link_hash_hide_symbol (info, h, force_local)
1474 struct bfd_link_info *info;
2920b85c 1475 struct elf_link_hash_entry *h;
e5094212 1476 boolean force_local;
2920b85c 1477{
2920b85c 1478 h->plt.offset = (bfd_vma) -1;
e5094212
AM
1479 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1480 if (force_local)
1481 {
1482 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1483 if (h->dynindx != -1)
1484 {
1485 h->dynindx = -1;
1486 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1487 h->dynstr_index);
1488 }
1489 }
2920b85c
RH
1490}
1491
252b5132
RH
1492/* Initialize an ELF linker hash table. */
1493
1494boolean
1495_bfd_elf_link_hash_table_init (table, abfd, newfunc)
1496 struct elf_link_hash_table *table;
1497 bfd *abfd;
1498 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1499 struct bfd_hash_table *,
1500 const char *));
1501{
8ea2e4bd
NC
1502 boolean ret;
1503
252b5132
RH
1504 table->dynamic_sections_created = false;
1505 table->dynobj = NULL;
51b64d56 1506 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
252b5132
RH
1507 /* The first dynamic symbol is a dummy. */
1508 table->dynsymcount = 1;
1509 table->dynstr = NULL;
1510 table->bucketcount = 0;
1511 table->needed = NULL;
a963dc6a 1512 table->runpath = NULL;
f5d44ba0 1513 table->loaded = NULL;
252b5132
RH
1514 table->hgot = NULL;
1515 table->stab_info = NULL;
f5fa8ca2 1516 table->merge_info = NULL;
1ae00f9d 1517 table->dynlocal = NULL;
8ea2e4bd
NC
1518 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1519 table->root.type = bfd_link_elf_hash_table;
1520
1521 return ret;
252b5132
RH
1522}
1523
1524/* Create an ELF linker hash table. */
1525
1526struct bfd_link_hash_table *
1527_bfd_elf_link_hash_table_create (abfd)
1528 bfd *abfd;
1529{
1530 struct elf_link_hash_table *ret;
dc810e39 1531 bfd_size_type amt = sizeof (struct elf_link_hash_table);
252b5132 1532
e2d34d7d 1533 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
252b5132
RH
1534 if (ret == (struct elf_link_hash_table *) NULL)
1535 return NULL;
1536
1537 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1538 {
e2d34d7d 1539 free (ret);
252b5132
RH
1540 return NULL;
1541 }
1542
1543 return &ret->root;
1544}
1545
1546/* This is a hook for the ELF emulation code in the generic linker to
1547 tell the backend linker what file name to use for the DT_NEEDED
1548 entry for a dynamic object. The generic linker passes name as an
1549 empty string to indicate that no DT_NEEDED entry should be made. */
1550
1551void
1552bfd_elf_set_dt_needed_name (abfd, name)
1553 bfd *abfd;
1554 const char *name;
1555{
1556 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1557 && bfd_get_format (abfd) == bfd_object)
1558 elf_dt_name (abfd) = name;
1559}
1560
74816898
L
1561void
1562bfd_elf_set_dt_needed_soname (abfd, name)
1563 bfd *abfd;
1564 const char *name;
1565{
1566 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1567 && bfd_get_format (abfd) == bfd_object)
1568 elf_dt_soname (abfd) = name;
1569}
1570
252b5132
RH
1571/* Get the list of DT_NEEDED entries for a link. This is a hook for
1572 the linker ELF emulation code. */
1573
1574struct bfd_link_needed_list *
1575bfd_elf_get_needed_list (abfd, info)
7442e600 1576 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1577 struct bfd_link_info *info;
1578{
1579 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1580 return NULL;
1581 return elf_hash_table (info)->needed;
1582}
1583
a963dc6a
L
1584/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1585 hook for the linker ELF emulation code. */
1586
1587struct bfd_link_needed_list *
1588bfd_elf_get_runpath_list (abfd, info)
1589 bfd *abfd ATTRIBUTE_UNUSED;
1590 struct bfd_link_info *info;
1591{
1592 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1593 return NULL;
1594 return elf_hash_table (info)->runpath;
1595}
1596
252b5132
RH
1597/* Get the name actually used for a dynamic object for a link. This
1598 is the SONAME entry if there is one. Otherwise, it is the string
1599 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1600
1601const char *
1602bfd_elf_get_dt_soname (abfd)
1603 bfd *abfd;
1604{
1605 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1606 && bfd_get_format (abfd) == bfd_object)
1607 return elf_dt_name (abfd);
1608 return NULL;
1609}
1610
1611/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1612 the ELF linker emulation code. */
1613
1614boolean
1615bfd_elf_get_bfd_needed_list (abfd, pneeded)
1616 bfd *abfd;
1617 struct bfd_link_needed_list **pneeded;
1618{
1619 asection *s;
1620 bfd_byte *dynbuf = NULL;
1621 int elfsec;
dc810e39 1622 unsigned long shlink;
252b5132
RH
1623 bfd_byte *extdyn, *extdynend;
1624 size_t extdynsize;
1625 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1626
1627 *pneeded = NULL;
1628
1629 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1630 || bfd_get_format (abfd) != bfd_object)
1631 return true;
1632
1633 s = bfd_get_section_by_name (abfd, ".dynamic");
1634 if (s == NULL || s->_raw_size == 0)
1635 return true;
1636
1637 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1638 if (dynbuf == NULL)
1639 goto error_return;
1640
1641 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1642 s->_raw_size))
1643 goto error_return;
1644
1645 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1646 if (elfsec == -1)
1647 goto error_return;
1648
dc810e39 1649 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1650
1651 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1652 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1653
1654 extdyn = dynbuf;
1655 extdynend = extdyn + s->_raw_size;
1656 for (; extdyn < extdynend; extdyn += extdynsize)
1657 {
1658 Elf_Internal_Dyn dyn;
1659
1660 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1661
1662 if (dyn.d_tag == DT_NULL)
1663 break;
1664
1665 if (dyn.d_tag == DT_NEEDED)
1666 {
1667 const char *string;
1668 struct bfd_link_needed_list *l;
dc810e39
AM
1669 unsigned int tagv = dyn.d_un.d_val;
1670 bfd_size_type amt;
252b5132 1671
dc810e39 1672 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1673 if (string == NULL)
1674 goto error_return;
1675
dc810e39
AM
1676 amt = sizeof *l;
1677 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
252b5132
RH
1678 if (l == NULL)
1679 goto error_return;
1680
1681 l->by = abfd;
1682 l->name = string;
1683 l->next = *pneeded;
1684 *pneeded = l;
1685 }
1686 }
1687
1688 free (dynbuf);
1689
1690 return true;
1691
1692 error_return:
1693 if (dynbuf != NULL)
1694 free (dynbuf);
1695 return false;
1696}
1697\f
1698/* Allocate an ELF string table--force the first byte to be zero. */
1699
1700struct bfd_strtab_hash *
1701_bfd_elf_stringtab_init ()
1702{
1703 struct bfd_strtab_hash *ret;
1704
1705 ret = _bfd_stringtab_init ();
1706 if (ret != NULL)
1707 {
1708 bfd_size_type loc;
1709
1710 loc = _bfd_stringtab_add (ret, "", true, false);
1711 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1712 if (loc == (bfd_size_type) -1)
1713 {
1714 _bfd_stringtab_free (ret);
1715 ret = NULL;
1716 }
1717 }
1718 return ret;
1719}
1720\f
1721/* ELF .o/exec file reading */
1722
c044fabd 1723/* Create a new bfd section from an ELF section header. */
252b5132
RH
1724
1725boolean
1726bfd_section_from_shdr (abfd, shindex)
1727 bfd *abfd;
1728 unsigned int shindex;
1729{
1730 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1731 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1732 struct elf_backend_data *bed = get_elf_backend_data (abfd);
90937f86 1733 const char *name;
252b5132
RH
1734
1735 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1736
1737 switch (hdr->sh_type)
1738 {
1739 case SHT_NULL:
1740 /* Inactive section. Throw it away. */
1741 return true;
1742
1743 case SHT_PROGBITS: /* Normal section with contents. */
252b5132
RH
1744 case SHT_NOBITS: /* .bss section. */
1745 case SHT_HASH: /* .hash section. */
1746 case SHT_NOTE: /* .note section. */
25e27870
L
1747 case SHT_INIT_ARRAY: /* .init_array section. */
1748 case SHT_FINI_ARRAY: /* .fini_array section. */
1749 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
252b5132
RH
1750 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1751
797fc050
AM
1752 case SHT_DYNAMIC: /* Dynamic linking information. */
1753 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1754 return false;
1755 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1756 {
1757 Elf_Internal_Shdr *dynsymhdr;
1758
1759 /* The shared libraries distributed with hpux11 have a bogus
1760 sh_link field for the ".dynamic" section. Find the
1761 string table for the ".dynsym" section instead. */
1762 if (elf_dynsymtab (abfd) != 0)
1763 {
1764 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1765 hdr->sh_link = dynsymhdr->sh_link;
1766 }
1767 else
1768 {
1769 unsigned int i, num_sec;
1770
1771 num_sec = elf_numsections (abfd);
1772 for (i = 1; i < num_sec; i++)
1773 {
1774 dynsymhdr = elf_elfsections (abfd)[i];
1775 if (dynsymhdr->sh_type == SHT_DYNSYM)
1776 {
1777 hdr->sh_link = dynsymhdr->sh_link;
1778 break;
1779 }
1780 }
1781 }
1782 }
1783 break;
1784
252b5132
RH
1785 case SHT_SYMTAB: /* A symbol table */
1786 if (elf_onesymtab (abfd) == shindex)
1787 return true;
1788
1789 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1790 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1791 elf_onesymtab (abfd) = shindex;
1792 elf_tdata (abfd)->symtab_hdr = *hdr;
1793 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1794 abfd->flags |= HAS_SYMS;
1795
1796 /* Sometimes a shared object will map in the symbol table. If
1797 SHF_ALLOC is set, and this is a shared object, then we also
1798 treat this section as a BFD section. We can not base the
1799 decision purely on SHF_ALLOC, because that flag is sometimes
1800 set in a relocateable object file, which would confuse the
1801 linker. */
1802 if ((hdr->sh_flags & SHF_ALLOC) != 0
1803 && (abfd->flags & DYNAMIC) != 0
1804 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1805 return false;
1806
1807 return true;
1808
1809 case SHT_DYNSYM: /* A dynamic symbol table */
1810 if (elf_dynsymtab (abfd) == shindex)
1811 return true;
1812
1813 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1814 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1815 elf_dynsymtab (abfd) = shindex;
1816 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1817 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1818 abfd->flags |= HAS_SYMS;
1819
1820 /* Besides being a symbol table, we also treat this as a regular
1821 section, so that objcopy can handle it. */
1822 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1823
9ad5cbcf
AM
1824 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1825 if (elf_symtab_shndx (abfd) == shindex)
1826 return true;
1827
1828 /* Get the associated symbol table. */
1829 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1830 || hdr->sh_link != elf_onesymtab (abfd))
1831 return false;
1832
1833 elf_symtab_shndx (abfd) = shindex;
1834 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1835 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1836 return true;
1837
252b5132
RH
1838 case SHT_STRTAB: /* A string table */
1839 if (hdr->bfd_section != NULL)
1840 return true;
1841 if (ehdr->e_shstrndx == shindex)
1842 {
1843 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1844 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1845 return true;
1846 }
1847 {
9ad5cbcf 1848 unsigned int i, num_sec;
252b5132 1849
9ad5cbcf
AM
1850 num_sec = elf_numsections (abfd);
1851 for (i = 1; i < num_sec; i++)
252b5132
RH
1852 {
1853 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1854 if (hdr2->sh_link == shindex)
1855 {
1856 if (! bfd_section_from_shdr (abfd, i))
1857 return false;
1858 if (elf_onesymtab (abfd) == i)
1859 {
1860 elf_tdata (abfd)->strtab_hdr = *hdr;
1861 elf_elfsections (abfd)[shindex] =
1862 &elf_tdata (abfd)->strtab_hdr;
1863 return true;
1864 }
1865 if (elf_dynsymtab (abfd) == i)
1866 {
1867 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1868 elf_elfsections (abfd)[shindex] = hdr =
1869 &elf_tdata (abfd)->dynstrtab_hdr;
1870 /* We also treat this as a regular section, so
1871 that objcopy can handle it. */
1872 break;
1873 }
1874#if 0 /* Not handling other string tables specially right now. */
1875 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1876 /* We have a strtab for some random other section. */
1877 newsect = (asection *) hdr2->bfd_section;
1878 if (!newsect)
1879 break;
1880 hdr->bfd_section = newsect;
1881 hdr2 = &elf_section_data (newsect)->str_hdr;
1882 *hdr2 = *hdr;
1883 elf_elfsections (abfd)[shindex] = hdr2;
1884#endif
1885 }
1886 }
1887 }
1888
1889 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1890
1891 case SHT_REL:
1892 case SHT_RELA:
1893 /* *These* do a lot of work -- but build no sections! */
1894 {
1895 asection *target_sect;
1896 Elf_Internal_Shdr *hdr2;
9ad5cbcf 1897 unsigned int num_sec = elf_numsections (abfd);
252b5132 1898
03ae5f59 1899 /* Check for a bogus link to avoid crashing. */
9ad5cbcf
AM
1900 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1901 || hdr->sh_link >= num_sec)
03ae5f59
ILT
1902 {
1903 ((*_bfd_error_handler)
1904 (_("%s: invalid link %lu for reloc section %s (index %u)"),
8f615d07 1905 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
03ae5f59
ILT
1906 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1907 }
1908
252b5132
RH
1909 /* For some incomprehensible reason Oracle distributes
1910 libraries for Solaris in which some of the objects have
1911 bogus sh_link fields. It would be nice if we could just
1912 reject them, but, unfortunately, some people need to use
1913 them. We scan through the section headers; if we find only
1914 one suitable symbol table, we clobber the sh_link to point
1915 to it. I hope this doesn't break anything. */
1916 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1917 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1918 {
9ad5cbcf 1919 unsigned int scan;
252b5132
RH
1920 int found;
1921
1922 found = 0;
9ad5cbcf 1923 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
1924 {
1925 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1926 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1927 {
1928 if (found != 0)
1929 {
1930 found = 0;
1931 break;
1932 }
1933 found = scan;
1934 }
1935 }
1936 if (found != 0)
1937 hdr->sh_link = found;
1938 }
1939
1940 /* Get the symbol table. */
1941 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1942 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1943 return false;
1944
1945 /* If this reloc section does not use the main symbol table we
1946 don't treat it as a reloc section. BFD can't adequately
1947 represent such a section, so at least for now, we don't
c044fabd 1948 try. We just present it as a normal section. We also
60bcf0fa 1949 can't use it as a reloc section if it points to the null
c044fabd 1950 section. */
60bcf0fa 1951 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
252b5132
RH
1952 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1953
1954 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1955 return false;
1956 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1957 if (target_sect == NULL)
1958 return false;
1959
1960 if ((target_sect->flags & SEC_RELOC) == 0
1961 || target_sect->reloc_count == 0)
1962 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1963 else
1964 {
dc810e39 1965 bfd_size_type amt;
252b5132 1966 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
dc810e39
AM
1967 amt = sizeof (*hdr2);
1968 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
252b5132
RH
1969 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1970 }
1971 *hdr2 = *hdr;
1972 elf_elfsections (abfd)[shindex] = hdr2;
d9bc7a44 1973 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
252b5132
RH
1974 target_sect->flags |= SEC_RELOC;
1975 target_sect->relocation = NULL;
1976 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
1977 /* In the section to which the relocations apply, mark whether
1978 its relocations are of the REL or RELA variety. */
72730e0c
AM
1979 if (hdr->sh_size != 0)
1980 elf_section_data (target_sect)->use_rela_p
1981 = (hdr->sh_type == SHT_RELA);
252b5132
RH
1982 abfd->flags |= HAS_RELOC;
1983 return true;
1984 }
1985 break;
1986
1987 case SHT_GNU_verdef:
1988 elf_dynverdef (abfd) = shindex;
1989 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1990 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1991 break;
1992
1993 case SHT_GNU_versym:
1994 elf_dynversym (abfd) = shindex;
1995 elf_tdata (abfd)->dynversym_hdr = *hdr;
1996 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1997 break;
1998
1999 case SHT_GNU_verneed:
2000 elf_dynverref (abfd) = shindex;
2001 elf_tdata (abfd)->dynverref_hdr = *hdr;
2002 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2003 break;
2004
2005 case SHT_SHLIB:
2006 return true;
2007
dbb410c3 2008 case SHT_GROUP:
b885599b
AM
2009 /* We need a BFD section for objcopy and relocatable linking,
2010 and it's handy to have the signature available as the section
2011 name. */
2012 name = group_signature (abfd, hdr);
2013 if (name == NULL)
2014 return false;
dbb410c3
AM
2015 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
2016 return false;
2017 if (hdr->contents != NULL)
2018 {
2019 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2020 unsigned int n_elt = hdr->sh_size / 4;
2021 asection *s;
2022
b885599b
AM
2023 if (idx->flags & GRP_COMDAT)
2024 hdr->bfd_section->flags
2025 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2026
dbb410c3
AM
2027 while (--n_elt != 0)
2028 if ((s = (++idx)->shdr->bfd_section) != NULL
945906ff 2029 && elf_next_in_group (s) != NULL)
dbb410c3 2030 {
945906ff 2031 elf_next_in_group (hdr->bfd_section) = s;
dbb410c3
AM
2032 break;
2033 }
2034 }
2035 break;
2036
252b5132
RH
2037 default:
2038 /* Check for any processor-specific section types. */
2039 {
2040 if (bed->elf_backend_section_from_shdr)
2041 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
2042 }
2043 break;
2044 }
2045
2046 return true;
2047}
2048
ec338859
AM
2049/* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2050 Return SEC for sections that have no elf section, and NULL on error. */
2051
2052asection *
2053bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
2054 bfd *abfd;
2055 struct sym_sec_cache *cache;
2056 asection *sec;
2057 unsigned long r_symndx;
2058{
ec338859 2059 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc
AM
2060 unsigned char esym[sizeof (Elf64_External_Sym)];
2061 Elf_External_Sym_Shndx eshndx;
2062 Elf_Internal_Sym isym;
ec338859
AM
2063 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2064
2065 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2066 return cache->sec[ent];
2067
2068 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
2069 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2070 &isym, esym, &eshndx) == NULL)
ec338859 2071 return NULL;
9ad5cbcf 2072
ec338859
AM
2073 if (cache->abfd != abfd)
2074 {
2075 memset (cache->indx, -1, sizeof (cache->indx));
2076 cache->abfd = abfd;
2077 }
2078 cache->indx[ent] = r_symndx;
2079 cache->sec[ent] = sec;
6cdc0ccc 2080 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
ec338859
AM
2081 {
2082 asection *s;
6cdc0ccc 2083 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
ec338859
AM
2084 if (s != NULL)
2085 cache->sec[ent] = s;
2086 }
2087 return cache->sec[ent];
2088}
2089
252b5132
RH
2090/* Given an ELF section number, retrieve the corresponding BFD
2091 section. */
2092
2093asection *
2094bfd_section_from_elf_index (abfd, index)
2095 bfd *abfd;
2096 unsigned int index;
2097{
9ad5cbcf 2098 if (index >= elf_numsections (abfd))
252b5132
RH
2099 return NULL;
2100 return elf_elfsections (abfd)[index]->bfd_section;
2101}
2102
2103boolean
2104_bfd_elf_new_section_hook (abfd, sec)
2105 bfd *abfd;
2106 asection *sec;
2107{
2108 struct bfd_elf_section_data *sdata;
dc810e39 2109 bfd_size_type amt = sizeof (*sdata);
252b5132 2110
dc810e39 2111 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
252b5132
RH
2112 if (!sdata)
2113 return false;
2114 sec->used_by_bfd = (PTR) sdata;
bf572ba0
MM
2115
2116 /* Indicate whether or not this section should use RELA relocations. */
c044fabd 2117 sdata->use_rela_p
bf572ba0
MM
2118 = get_elf_backend_data (abfd)->default_use_rela_p;
2119
252b5132
RH
2120 return true;
2121}
2122
2123/* Create a new bfd section from an ELF program header.
2124
2125 Since program segments have no names, we generate a synthetic name
2126 of the form segment<NUM>, where NUM is generally the index in the
2127 program header table. For segments that are split (see below) we
2128 generate the names segment<NUM>a and segment<NUM>b.
2129
2130 Note that some program segments may have a file size that is different than
2131 (less than) the memory size. All this means is that at execution the
2132 system must allocate the amount of memory specified by the memory size,
2133 but only initialize it with the first "file size" bytes read from the
2134 file. This would occur for example, with program segments consisting
2135 of combined data+bss.
2136
2137 To handle the above situation, this routine generates TWO bfd sections
2138 for the single program segment. The first has the length specified by
2139 the file size of the segment, and the second has the length specified
2140 by the difference between the two sizes. In effect, the segment is split
2141 into it's initialized and uninitialized parts.
2142
2143 */
2144
2145boolean
20cfcaae 2146_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
252b5132
RH
2147 bfd *abfd;
2148 Elf_Internal_Phdr *hdr;
2149 int index;
20cfcaae 2150 const char *typename;
252b5132
RH
2151{
2152 asection *newsect;
2153 char *name;
2154 char namebuf[64];
d4c88bbb 2155 size_t len;
252b5132
RH
2156 int split;
2157
2158 split = ((hdr->p_memsz > 0)
2159 && (hdr->p_filesz > 0)
2160 && (hdr->p_memsz > hdr->p_filesz));
27ac83bf 2161 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
d4c88bbb
AM
2162 len = strlen (namebuf) + 1;
2163 name = bfd_alloc (abfd, (bfd_size_type) len);
252b5132
RH
2164 if (!name)
2165 return false;
d4c88bbb 2166 memcpy (name, namebuf, len);
252b5132
RH
2167 newsect = bfd_make_section (abfd, name);
2168 if (newsect == NULL)
2169 return false;
2170 newsect->vma = hdr->p_vaddr;
2171 newsect->lma = hdr->p_paddr;
2172 newsect->_raw_size = hdr->p_filesz;
2173 newsect->filepos = hdr->p_offset;
2174 newsect->flags |= SEC_HAS_CONTENTS;
2175 if (hdr->p_type == PT_LOAD)
2176 {
2177 newsect->flags |= SEC_ALLOC;
2178 newsect->flags |= SEC_LOAD;
2179 if (hdr->p_flags & PF_X)
2180 {
2181 /* FIXME: all we known is that it has execute PERMISSION,
c044fabd 2182 may be data. */
252b5132
RH
2183 newsect->flags |= SEC_CODE;
2184 }
2185 }
2186 if (!(hdr->p_flags & PF_W))
2187 {
2188 newsect->flags |= SEC_READONLY;
2189 }
2190
2191 if (split)
2192 {
27ac83bf 2193 sprintf (namebuf, "%s%db", typename, index);
d4c88bbb
AM
2194 len = strlen (namebuf) + 1;
2195 name = bfd_alloc (abfd, (bfd_size_type) len);
252b5132
RH
2196 if (!name)
2197 return false;
d4c88bbb 2198 memcpy (name, namebuf, len);
252b5132
RH
2199 newsect = bfd_make_section (abfd, name);
2200 if (newsect == NULL)
2201 return false;
2202 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2203 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2204 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2205 if (hdr->p_type == PT_LOAD)
2206 {
2207 newsect->flags |= SEC_ALLOC;
2208 if (hdr->p_flags & PF_X)
2209 newsect->flags |= SEC_CODE;
2210 }
2211 if (!(hdr->p_flags & PF_W))
2212 newsect->flags |= SEC_READONLY;
2213 }
2214
2215 return true;
2216}
2217
20cfcaae
NC
2218boolean
2219bfd_section_from_phdr (abfd, hdr, index)
2220 bfd *abfd;
2221 Elf_Internal_Phdr *hdr;
2222 int index;
2223{
2224 struct elf_backend_data *bed;
2225
2226 switch (hdr->p_type)
2227 {
2228 case PT_NULL:
2229 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2230
2231 case PT_LOAD:
2232 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2233
2234 case PT_DYNAMIC:
2235 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2236
2237 case PT_INTERP:
2238 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2239
2240 case PT_NOTE:
2241 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2242 return false;
dc810e39 2243 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
20cfcaae
NC
2244 return false;
2245 return true;
2246
2247 case PT_SHLIB:
2248 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2249
2250 case PT_PHDR:
2251 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2252
2253 default:
2254 /* Check for any processor-specific program segment types.
c044fabd 2255 If no handler for them, default to making "segment" sections. */
20cfcaae
NC
2256 bed = get_elf_backend_data (abfd);
2257 if (bed->elf_backend_section_from_phdr)
2258 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2259 else
2260 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2261 }
2262}
2263
23bc299b
MM
2264/* Initialize REL_HDR, the section-header for new section, containing
2265 relocations against ASECT. If USE_RELA_P is true, we use RELA
2266 relocations; otherwise, we use REL relocations. */
2267
2268boolean
2269_bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2270 bfd *abfd;
2271 Elf_Internal_Shdr *rel_hdr;
2272 asection *asect;
2273 boolean use_rela_p;
2274{
2275 char *name;
dc810e39
AM
2276 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2277 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
23bc299b 2278
dc810e39 2279 name = bfd_alloc (abfd, amt);
23bc299b
MM
2280 if (name == NULL)
2281 return false;
2282 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2283 rel_hdr->sh_name =
2b0f7ef9
JJ
2284 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2285 false);
23bc299b
MM
2286 if (rel_hdr->sh_name == (unsigned int) -1)
2287 return false;
2288 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2289 rel_hdr->sh_entsize = (use_rela_p
2290 ? bed->s->sizeof_rela
2291 : bed->s->sizeof_rel);
2292 rel_hdr->sh_addralign = bed->s->file_align;
2293 rel_hdr->sh_flags = 0;
2294 rel_hdr->sh_addr = 0;
2295 rel_hdr->sh_size = 0;
2296 rel_hdr->sh_offset = 0;
2297
2298 return true;
2299}
2300
252b5132
RH
2301/* Set up an ELF internal section header for a section. */
2302
252b5132
RH
2303static void
2304elf_fake_sections (abfd, asect, failedptrarg)
2305 bfd *abfd;
2306 asection *asect;
2307 PTR failedptrarg;
2308{
2309 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2310 boolean *failedptr = (boolean *) failedptrarg;
2311 Elf_Internal_Shdr *this_hdr;
2312
2313 if (*failedptr)
2314 {
2315 /* We already failed; just get out of the bfd_map_over_sections
2316 loop. */
2317 return;
2318 }
2319
2320 this_hdr = &elf_section_data (asect)->this_hdr;
2321
2b0f7ef9
JJ
2322 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2323 asect->name, false);
252b5132
RH
2324 if (this_hdr->sh_name == (unsigned long) -1)
2325 {
2326 *failedptr = true;
2327 return;
2328 }
2329
2330 this_hdr->sh_flags = 0;
2331
2332 if ((asect->flags & SEC_ALLOC) != 0
2333 || asect->user_set_vma)
2334 this_hdr->sh_addr = asect->vma;
2335 else
2336 this_hdr->sh_addr = 0;
2337
2338 this_hdr->sh_offset = 0;
2339 this_hdr->sh_size = asect->_raw_size;
2340 this_hdr->sh_link = 0;
2341 this_hdr->sh_addralign = 1 << asect->alignment_power;
2342 /* The sh_entsize and sh_info fields may have been set already by
2343 copy_private_section_data. */
2344
2345 this_hdr->bfd_section = asect;
2346 this_hdr->contents = NULL;
2347
2348 /* FIXME: This should not be based on section names. */
2349 if (strcmp (asect->name, ".dynstr") == 0)
2350 this_hdr->sh_type = SHT_STRTAB;
2351 else if (strcmp (asect->name, ".hash") == 0)
2352 {
2353 this_hdr->sh_type = SHT_HASH;
c7ac6ff8 2354 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
252b5132
RH
2355 }
2356 else if (strcmp (asect->name, ".dynsym") == 0)
2357 {
2358 this_hdr->sh_type = SHT_DYNSYM;
2359 this_hdr->sh_entsize = bed->s->sizeof_sym;
2360 }
2361 else if (strcmp (asect->name, ".dynamic") == 0)
2362 {
2363 this_hdr->sh_type = SHT_DYNAMIC;
2364 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2365 }
a9d024b8 2366 else if (strncmp (asect->name, ".rela", 5) == 0
bf572ba0 2367 && get_elf_backend_data (abfd)->may_use_rela_p)
252b5132
RH
2368 {
2369 this_hdr->sh_type = SHT_RELA;
2370 this_hdr->sh_entsize = bed->s->sizeof_rela;
2371 }
a9d024b8 2372 else if (strncmp (asect->name, ".rel", 4) == 0
bf572ba0 2373 && get_elf_backend_data (abfd)->may_use_rel_p)
252b5132
RH
2374 {
2375 this_hdr->sh_type = SHT_REL;
2376 this_hdr->sh_entsize = bed->s->sizeof_rel;
2377 }
25e27870
L
2378 else if (strcmp (asect->name, ".init_array") == 0)
2379 this_hdr->sh_type = SHT_INIT_ARRAY;
2380 else if (strcmp (asect->name, ".fini_array") == 0)
2381 this_hdr->sh_type = SHT_FINI_ARRAY;
2382 else if (strcmp (asect->name, ".preinit_array") == 0)
2383 this_hdr->sh_type = SHT_PREINIT_ARRAY;
252b5132
RH
2384 else if (strncmp (asect->name, ".note", 5) == 0)
2385 this_hdr->sh_type = SHT_NOTE;
2386 else if (strncmp (asect->name, ".stab", 5) == 0
2387 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2388 this_hdr->sh_type = SHT_STRTAB;
2389 else if (strcmp (asect->name, ".gnu.version") == 0)
2390 {
2391 this_hdr->sh_type = SHT_GNU_versym;
2392 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2393 }
2394 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2395 {
2396 this_hdr->sh_type = SHT_GNU_verdef;
2397 this_hdr->sh_entsize = 0;
2398 /* objcopy or strip will copy over sh_info, but may not set
2399 cverdefs. The linker will set cverdefs, but sh_info will be
2400 zero. */
2401 if (this_hdr->sh_info == 0)
2402 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2403 else
2404 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2405 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2406 }
2407 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2408 {
2409 this_hdr->sh_type = SHT_GNU_verneed;
2410 this_hdr->sh_entsize = 0;
2411 /* objcopy or strip will copy over sh_info, but may not set
2412 cverrefs. The linker will set cverrefs, but sh_info will be
2413 zero. */
2414 if (this_hdr->sh_info == 0)
2415 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2416 else
2417 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2418 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2419 }
dbb410c3
AM
2420 else if ((asect->flags & SEC_GROUP) != 0)
2421 {
2422 this_hdr->sh_type = SHT_GROUP;
2423 this_hdr->sh_entsize = 4;
2424 }
252b5132 2425 else if ((asect->flags & SEC_ALLOC) != 0
edd29cf9
AM
2426 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2427 || (asect->flags & SEC_NEVER_LOAD) != 0))
252b5132
RH
2428 this_hdr->sh_type = SHT_NOBITS;
2429 else
6c99a5c3 2430 this_hdr->sh_type = SHT_PROGBITS;
252b5132
RH
2431
2432 if ((asect->flags & SEC_ALLOC) != 0)
2433 this_hdr->sh_flags |= SHF_ALLOC;
2434 if ((asect->flags & SEC_READONLY) == 0)
2435 this_hdr->sh_flags |= SHF_WRITE;
2436 if ((asect->flags & SEC_CODE) != 0)
2437 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
2438 if ((asect->flags & SEC_MERGE) != 0)
2439 {
2440 this_hdr->sh_flags |= SHF_MERGE;
2441 this_hdr->sh_entsize = asect->entsize;
2442 if ((asect->flags & SEC_STRINGS) != 0)
2443 this_hdr->sh_flags |= SHF_STRINGS;
2444 }
1126897b 2445 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 2446 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 2447 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
2448 {
2449 this_hdr->sh_flags |= SHF_TLS;
2450 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2451 {
2452 struct bfd_link_order *o;
2453
2454 this_hdr->sh_size = 0;
2455 for (o = asect->link_order_head; o != NULL; o = o->next)
2456 if (this_hdr->sh_size < o->offset + o->size)
2457 this_hdr->sh_size = o->offset + o->size;
2458 if (this_hdr->sh_size)
2459 this_hdr->sh_type = SHT_NOBITS;
2460 }
2461 }
252b5132
RH
2462
2463 /* Check for processor-specific section types. */
e1fddb6b
AO
2464 if (bed->elf_backend_fake_sections
2465 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2466 *failedptr = true;
252b5132
RH
2467
2468 /* If the section has relocs, set up a section header for the
23bc299b
MM
2469 SHT_REL[A] section. If two relocation sections are required for
2470 this section, it is up to the processor-specific back-end to
c044fabd 2471 create the other. */
23bc299b 2472 if ((asect->flags & SEC_RELOC) != 0
c044fabd 2473 && !_bfd_elf_init_reloc_shdr (abfd,
23bc299b 2474 &elf_section_data (asect)->rel_hdr,
c044fabd 2475 asect,
23bc299b
MM
2476 elf_section_data (asect)->use_rela_p))
2477 *failedptr = true;
252b5132
RH
2478}
2479
dbb410c3
AM
2480/* Fill in the contents of a SHT_GROUP section. */
2481
1126897b
AM
2482void
2483bfd_elf_set_group_contents (abfd, sec, failedptrarg)
dbb410c3
AM
2484 bfd *abfd;
2485 asection *sec;
1126897b 2486 PTR failedptrarg;
dbb410c3
AM
2487{
2488 boolean *failedptr = (boolean *) failedptrarg;
2489 unsigned long symindx;
9dce4196 2490 asection *elt, *first;
dbb410c3
AM
2491 unsigned char *loc;
2492 struct bfd_link_order *l;
9dce4196 2493 boolean gas;
dbb410c3
AM
2494
2495 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2496 || *failedptr)
2497 return;
2498
1126897b
AM
2499 symindx = 0;
2500 if (elf_group_id (sec) != NULL)
2501 symindx = elf_group_id (sec)->udata.i;
2502
2503 if (symindx == 0)
2504 {
2505 /* If called from the assembler, swap_out_syms will have set up
2506 elf_section_syms; If called for "ld -r", use target_index. */
2507 if (elf_section_syms (abfd) != NULL)
2508 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2509 else
2510 symindx = sec->target_index;
2511 }
dbb410c3
AM
2512 elf_section_data (sec)->this_hdr.sh_info = symindx;
2513
1126897b 2514 /* The contents won't be allocated for "ld -r" or objcopy. */
9dce4196 2515 gas = true;
dbb410c3
AM
2516 if (sec->contents == NULL)
2517 {
9dce4196 2518 gas = false;
dbb410c3 2519 sec->contents = bfd_alloc (abfd, sec->_raw_size);
9dce4196
AM
2520
2521 /* Arrange for the section to be written out. */
2522 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
2523 if (sec->contents == NULL)
2524 {
2525 *failedptr = true;
2526 return;
2527 }
2528 }
2529
2530 loc = sec->contents + sec->_raw_size;
2531
9dce4196
AM
2532 /* Get the pointer to the first section in the group that gas
2533 squirreled away here. objcopy arranges for this to be set to the
2534 start of the input section group. */
2535 first = elt = elf_next_in_group (sec);
dbb410c3
AM
2536
2537 /* First element is a flag word. Rest of section is elf section
2538 indices for all the sections of the group. Write them backwards
2539 just to keep the group in the same order as given in .section
2540 directives, not that it matters. */
2541 while (elt != NULL)
2542 {
9dce4196
AM
2543 asection *s;
2544 unsigned int idx;
2545
dbb410c3 2546 loc -= 4;
9dce4196
AM
2547 s = elt;
2548 if (!gas)
2549 s = s->output_section;
2550 idx = 0;
2551 if (s != NULL)
2552 idx = elf_section_data (s)->this_idx;
2553 H_PUT_32 (abfd, idx, loc);
945906ff 2554 elt = elf_next_in_group (elt);
9dce4196
AM
2555 if (elt == first)
2556 break;
dbb410c3
AM
2557 }
2558
2559 /* If this is a relocatable link, then the above did nothing because
2560 SEC is the output section. Look through the input sections
2561 instead. */
2562 for (l = sec->link_order_head; l != NULL; l = l->next)
2563 if (l->type == bfd_indirect_link_order
945906ff 2564 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
dbb410c3
AM
2565 do
2566 {
2567 loc -= 4;
2568 H_PUT_32 (abfd,
2569 elf_section_data (elt->output_section)->this_idx, loc);
945906ff 2570 elt = elf_next_in_group (elt);
dbb410c3
AM
2571 /* During a relocatable link, the lists are circular. */
2572 }
945906ff 2573 while (elt != elf_next_in_group (l->u.indirect.section));
dbb410c3 2574
9dce4196
AM
2575 /* With ld -r, merging SHT_GROUP sections results in wasted space
2576 due to allowing for the flag word on each input. We may well
2577 duplicate entries too. */
2578 while ((loc -= 4) > sec->contents)
2579 H_PUT_32 (abfd, 0, loc);
2580
2581 if (loc != sec->contents)
2582 abort ();
dbb410c3 2583
9dce4196 2584 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
2585}
2586
252b5132
RH
2587/* Assign all ELF section numbers. The dummy first section is handled here
2588 too. The link/info pointers for the standard section types are filled
2589 in here too, while we're at it. */
2590
2591static boolean
2592assign_section_numbers (abfd)
2593 bfd *abfd;
2594{
2595 struct elf_obj_tdata *t = elf_tdata (abfd);
2596 asection *sec;
2b0f7ef9 2597 unsigned int section_number, secn;
252b5132 2598 Elf_Internal_Shdr **i_shdrp;
dc810e39 2599 bfd_size_type amt;
252b5132
RH
2600
2601 section_number = 1;
2602
2b0f7ef9
JJ
2603 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2604
252b5132
RH
2605 for (sec = abfd->sections; sec; sec = sec->next)
2606 {
2607 struct bfd_elf_section_data *d = elf_section_data (sec);
2608
9ad5cbcf
AM
2609 if (section_number == SHN_LORESERVE)
2610 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2611 d->this_idx = section_number++;
2b0f7ef9 2612 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
252b5132
RH
2613 if ((sec->flags & SEC_RELOC) == 0)
2614 d->rel_idx = 0;
2615 else
2b0f7ef9 2616 {
9ad5cbcf
AM
2617 if (section_number == SHN_LORESERVE)
2618 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2b0f7ef9
JJ
2619 d->rel_idx = section_number++;
2620 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2621 }
23bc299b
MM
2622
2623 if (d->rel_hdr2)
2b0f7ef9 2624 {
9ad5cbcf
AM
2625 if (section_number == SHN_LORESERVE)
2626 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2b0f7ef9
JJ
2627 d->rel_idx2 = section_number++;
2628 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2629 }
23bc299b
MM
2630 else
2631 d->rel_idx2 = 0;
252b5132
RH
2632 }
2633
9ad5cbcf
AM
2634 if (section_number == SHN_LORESERVE)
2635 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2636 t->shstrtab_section = section_number++;
2b0f7ef9 2637 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
252b5132 2638 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
252b5132
RH
2639
2640 if (bfd_get_symcount (abfd) > 0)
2641 {
9ad5cbcf
AM
2642 if (section_number == SHN_LORESERVE)
2643 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2644 t->symtab_section = section_number++;
2b0f7ef9 2645 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
9ad5cbcf
AM
2646 if (section_number > SHN_LORESERVE - 2)
2647 {
2648 if (section_number == SHN_LORESERVE)
2649 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2650 t->symtab_shndx_section = section_number++;
2651 t->symtab_shndx_hdr.sh_name
2652 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2653 ".symtab_shndx", false);
2654 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2655 return false;
2656 }
2657 if (section_number == SHN_LORESERVE)
2658 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2659 t->strtab_section = section_number++;
2b0f7ef9 2660 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
2661 }
2662
2b0f7ef9
JJ
2663 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2664 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
9ad5cbcf
AM
2665
2666 elf_numsections (abfd) = section_number;
252b5132 2667 elf_elfheader (abfd)->e_shnum = section_number;
9ad5cbcf
AM
2668 if (section_number > SHN_LORESERVE)
2669 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
2670
2671 /* Set up the list of section header pointers, in agreement with the
2672 indices. */
dc810e39
AM
2673 amt = section_number * sizeof (Elf_Internal_Shdr *);
2674 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
252b5132
RH
2675 if (i_shdrp == NULL)
2676 return false;
2677
dc810e39
AM
2678 amt = sizeof (Elf_Internal_Shdr);
2679 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
252b5132
RH
2680 if (i_shdrp[0] == NULL)
2681 {
2682 bfd_release (abfd, i_shdrp);
2683 return false;
2684 }
2685 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2686
2687 elf_elfsections (abfd) = i_shdrp;
2688
2689 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2690 if (bfd_get_symcount (abfd) > 0)
2691 {
2692 i_shdrp[t->symtab_section] = &t->symtab_hdr;
9ad5cbcf
AM
2693 if (elf_numsections (abfd) > SHN_LORESERVE)
2694 {
2695 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2696 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2697 }
252b5132
RH
2698 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2699 t->symtab_hdr.sh_link = t->strtab_section;
2700 }
2701 for (sec = abfd->sections; sec; sec = sec->next)
2702 {
2703 struct bfd_elf_section_data *d = elf_section_data (sec);
2704 asection *s;
2705 const char *name;
2706
2707 i_shdrp[d->this_idx] = &d->this_hdr;
2708 if (d->rel_idx != 0)
2709 i_shdrp[d->rel_idx] = &d->rel_hdr;
23bc299b
MM
2710 if (d->rel_idx2 != 0)
2711 i_shdrp[d->rel_idx2] = d->rel_hdr2;
252b5132
RH
2712
2713 /* Fill in the sh_link and sh_info fields while we're at it. */
2714
2715 /* sh_link of a reloc section is the section index of the symbol
2716 table. sh_info is the section index of the section to which
2717 the relocation entries apply. */
2718 if (d->rel_idx != 0)
2719 {
2720 d->rel_hdr.sh_link = t->symtab_section;
2721 d->rel_hdr.sh_info = d->this_idx;
2722 }
23bc299b
MM
2723 if (d->rel_idx2 != 0)
2724 {
2725 d->rel_hdr2->sh_link = t->symtab_section;
2726 d->rel_hdr2->sh_info = d->this_idx;
2727 }
252b5132
RH
2728
2729 switch (d->this_hdr.sh_type)
2730 {
2731 case SHT_REL:
2732 case SHT_RELA:
2733 /* A reloc section which we are treating as a normal BFD
2734 section. sh_link is the section index of the symbol
2735 table. sh_info is the section index of the section to
2736 which the relocation entries apply. We assume that an
2737 allocated reloc section uses the dynamic symbol table.
2738 FIXME: How can we be sure? */
2739 s = bfd_get_section_by_name (abfd, ".dynsym");
2740 if (s != NULL)
2741 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2742
2743 /* We look up the section the relocs apply to by name. */
2744 name = sec->name;
2745 if (d->this_hdr.sh_type == SHT_REL)
2746 name += 4;
2747 else
2748 name += 5;
2749 s = bfd_get_section_by_name (abfd, name);
2750 if (s != NULL)
2751 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2752 break;
2753
2754 case SHT_STRTAB:
2755 /* We assume that a section named .stab*str is a stabs
2756 string section. We look for a section with the same name
2757 but without the trailing ``str'', and set its sh_link
2758 field to point to this section. */
2759 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2760 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2761 {
2762 size_t len;
2763 char *alc;
2764
2765 len = strlen (sec->name);
d4c88bbb 2766 alc = (char *) bfd_malloc ((bfd_size_type) (len - 2));
252b5132
RH
2767 if (alc == NULL)
2768 return false;
d4c88bbb 2769 memcpy (alc, sec->name, len - 3);
252b5132
RH
2770 alc[len - 3] = '\0';
2771 s = bfd_get_section_by_name (abfd, alc);
2772 free (alc);
2773 if (s != NULL)
2774 {
2775 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2776
2777 /* This is a .stab section. */
0594c12d
AM
2778 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2779 elf_section_data (s)->this_hdr.sh_entsize
2780 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
2781 }
2782 }
2783 break;
2784
2785 case SHT_DYNAMIC:
2786 case SHT_DYNSYM:
2787 case SHT_GNU_verneed:
2788 case SHT_GNU_verdef:
2789 /* sh_link is the section header index of the string table
2790 used for the dynamic entries, or the symbol table, or the
2791 version strings. */
2792 s = bfd_get_section_by_name (abfd, ".dynstr");
2793 if (s != NULL)
2794 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2795 break;
2796
2797 case SHT_HASH:
2798 case SHT_GNU_versym:
2799 /* sh_link is the section header index of the symbol table
2800 this hash table or version table is for. */
2801 s = bfd_get_section_by_name (abfd, ".dynsym");
2802 if (s != NULL)
2803 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2804 break;
dbb410c3
AM
2805
2806 case SHT_GROUP:
2807 d->this_hdr.sh_link = t->symtab_section;
252b5132
RH
2808 }
2809 }
2810
2b0f7ef9 2811 for (secn = 1; secn < section_number; ++secn)
9ad5cbcf
AM
2812 if (i_shdrp[secn] == NULL)
2813 i_shdrp[secn] = i_shdrp[0];
2814 else
2815 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2816 i_shdrp[secn]->sh_name);
252b5132
RH
2817 return true;
2818}
2819
2820/* Map symbol from it's internal number to the external number, moving
2821 all local symbols to be at the head of the list. */
2822
2823static INLINE int
2824sym_is_global (abfd, sym)
2825 bfd *abfd;
2826 asymbol *sym;
2827{
2828 /* If the backend has a special mapping, use it. */
2829 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2830 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2831 (abfd, sym));
2832
2833 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2834 || bfd_is_und_section (bfd_get_section (sym))
2835 || bfd_is_com_section (bfd_get_section (sym)));
2836}
2837
2838static boolean
2839elf_map_symbols (abfd)
2840 bfd *abfd;
2841{
dc810e39 2842 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
2843 asymbol **syms = bfd_get_outsymbols (abfd);
2844 asymbol **sect_syms;
dc810e39
AM
2845 unsigned int num_locals = 0;
2846 unsigned int num_globals = 0;
2847 unsigned int num_locals2 = 0;
2848 unsigned int num_globals2 = 0;
252b5132 2849 int max_index = 0;
dc810e39 2850 unsigned int idx;
252b5132
RH
2851 asection *asect;
2852 asymbol **new_syms;
dc810e39 2853 bfd_size_type amt;
252b5132
RH
2854
2855#ifdef DEBUG
2856 fprintf (stderr, "elf_map_symbols\n");
2857 fflush (stderr);
2858#endif
2859
252b5132
RH
2860 for (asect = abfd->sections; asect; asect = asect->next)
2861 {
2862 if (max_index < asect->index)
2863 max_index = asect->index;
2864 }
2865
2866 max_index++;
dc810e39
AM
2867 amt = max_index * sizeof (asymbol *);
2868 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132
RH
2869 if (sect_syms == NULL)
2870 return false;
2871 elf_section_syms (abfd) = sect_syms;
4e89ac30 2872 elf_num_section_syms (abfd) = max_index;
252b5132 2873
079e9a2f
AM
2874 /* Init sect_syms entries for any section symbols we have already
2875 decided to output. */
252b5132
RH
2876 for (idx = 0; idx < symcount; idx++)
2877 {
dc810e39 2878 asymbol *sym = syms[idx];
c044fabd 2879
252b5132
RH
2880 if ((sym->flags & BSF_SECTION_SYM) != 0
2881 && sym->value == 0)
2882 {
2883 asection *sec;
2884
2885 sec = sym->section;
2886
2887 if (sec->owner != NULL)
2888 {
2889 if (sec->owner != abfd)
2890 {
2891 if (sec->output_offset != 0)
2892 continue;
c044fabd 2893
252b5132
RH
2894 sec = sec->output_section;
2895
079e9a2f
AM
2896 /* Empty sections in the input files may have had a
2897 section symbol created for them. (See the comment
2898 near the end of _bfd_generic_link_output_symbols in
2899 linker.c). If the linker script discards such
2900 sections then we will reach this point. Since we know
2901 that we cannot avoid this case, we detect it and skip
2902 the abort and the assignment to the sect_syms array.
2903 To reproduce this particular case try running the
2904 linker testsuite test ld-scripts/weak.exp for an ELF
2905 port that uses the generic linker. */
252b5132
RH
2906 if (sec->owner == NULL)
2907 continue;
2908
2909 BFD_ASSERT (sec->owner == abfd);
2910 }
2911 sect_syms[sec->index] = syms[idx];
2912 }
2913 }
2914 }
2915
252b5132
RH
2916 /* Classify all of the symbols. */
2917 for (idx = 0; idx < symcount; idx++)
2918 {
2919 if (!sym_is_global (abfd, syms[idx]))
2920 num_locals++;
2921 else
2922 num_globals++;
2923 }
079e9a2f
AM
2924
2925 /* We will be adding a section symbol for each BFD section. Most normal
2926 sections will already have a section symbol in outsymbols, but
2927 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2928 at least in that case. */
252b5132
RH
2929 for (asect = abfd->sections; asect; asect = asect->next)
2930 {
079e9a2f 2931 if (sect_syms[asect->index] == NULL)
252b5132 2932 {
079e9a2f 2933 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
2934 num_locals++;
2935 else
2936 num_globals++;
252b5132
RH
2937 }
2938 }
2939
2940 /* Now sort the symbols so the local symbols are first. */
dc810e39
AM
2941 amt = (num_locals + num_globals) * sizeof (asymbol *);
2942 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2943
252b5132
RH
2944 if (new_syms == NULL)
2945 return false;
2946
2947 for (idx = 0; idx < symcount; idx++)
2948 {
2949 asymbol *sym = syms[idx];
dc810e39 2950 unsigned int i;
252b5132
RH
2951
2952 if (!sym_is_global (abfd, sym))
2953 i = num_locals2++;
2954 else
2955 i = num_locals + num_globals2++;
2956 new_syms[i] = sym;
2957 sym->udata.i = i + 1;
2958 }
2959 for (asect = abfd->sections; asect; asect = asect->next)
2960 {
079e9a2f 2961 if (sect_syms[asect->index] == NULL)
252b5132 2962 {
079e9a2f 2963 asymbol *sym = asect->symbol;
dc810e39 2964 unsigned int i;
252b5132 2965
079e9a2f 2966 sect_syms[asect->index] = sym;
252b5132
RH
2967 if (!sym_is_global (abfd, sym))
2968 i = num_locals2++;
2969 else
2970 i = num_locals + num_globals2++;
2971 new_syms[i] = sym;
2972 sym->udata.i = i + 1;
2973 }
2974 }
2975
2976 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2977
2978 elf_num_locals (abfd) = num_locals;
2979 elf_num_globals (abfd) = num_globals;
2980 return true;
2981}
2982
2983/* Align to the maximum file alignment that could be required for any
2984 ELF data structure. */
2985
2986static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2987static INLINE file_ptr
2988align_file_position (off, align)
2989 file_ptr off;
2990 int align;
2991{
2992 return (off + align - 1) & ~(align - 1);
2993}
2994
2995/* Assign a file position to a section, optionally aligning to the
2996 required section alignment. */
2997
2998INLINE file_ptr
2999_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
3000 Elf_Internal_Shdr *i_shdrp;
3001 file_ptr offset;
3002 boolean align;
3003{
3004 if (align)
3005 {
3006 unsigned int al;
3007
3008 al = i_shdrp->sh_addralign;
3009 if (al > 1)
3010 offset = BFD_ALIGN (offset, al);
3011 }
3012 i_shdrp->sh_offset = offset;
3013 if (i_shdrp->bfd_section != NULL)
3014 i_shdrp->bfd_section->filepos = offset;
3015 if (i_shdrp->sh_type != SHT_NOBITS)
3016 offset += i_shdrp->sh_size;
3017 return offset;
3018}
3019
3020/* Compute the file positions we are going to put the sections at, and
3021 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3022 is not NULL, this is being called by the ELF backend linker. */
3023
3024boolean
3025_bfd_elf_compute_section_file_positions (abfd, link_info)
3026 bfd *abfd;
3027 struct bfd_link_info *link_info;
3028{
3029 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3030 boolean failed;
3031 struct bfd_strtab_hash *strtab;
3032 Elf_Internal_Shdr *shstrtab_hdr;
3033
3034 if (abfd->output_has_begun)
3035 return true;
3036
3037 /* Do any elf backend specific processing first. */
3038 if (bed->elf_backend_begin_write_processing)
3039 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3040
3041 if (! prep_headers (abfd))
3042 return false;
3043
e6c51ed4
NC
3044 /* Post process the headers if necessary. */
3045 if (bed->elf_backend_post_process_headers)
3046 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3047
252b5132
RH
3048 failed = false;
3049 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3050 if (failed)
3051 return false;
3052
3053 if (!assign_section_numbers (abfd))
3054 return false;
3055
3056 /* The backend linker builds symbol table information itself. */
3057 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3058 {
3059 /* Non-zero if doing a relocatable link. */
3060 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3061
3062 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3063 return false;
3064 }
3065
1126897b 3066 if (link_info == NULL)
dbb410c3 3067 {
1126897b 3068 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3
AM
3069 if (failed)
3070 return false;
3071 }
3072
252b5132
RH
3073 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3074 /* sh_name was set in prep_headers. */
3075 shstrtab_hdr->sh_type = SHT_STRTAB;
3076 shstrtab_hdr->sh_flags = 0;
3077 shstrtab_hdr->sh_addr = 0;
2b0f7ef9 3078 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
252b5132
RH
3079 shstrtab_hdr->sh_entsize = 0;
3080 shstrtab_hdr->sh_link = 0;
3081 shstrtab_hdr->sh_info = 0;
3082 /* sh_offset is set in assign_file_positions_except_relocs. */
3083 shstrtab_hdr->sh_addralign = 1;
3084
3085 if (!assign_file_positions_except_relocs (abfd))
3086 return false;
3087
3088 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3089 {
3090 file_ptr off;
3091 Elf_Internal_Shdr *hdr;
3092
3093 off = elf_tdata (abfd)->next_file_pos;
3094
3095 hdr = &elf_tdata (abfd)->symtab_hdr;
3096 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3097
9ad5cbcf
AM
3098 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3099 if (hdr->sh_size != 0)
3100 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3101
252b5132
RH
3102 hdr = &elf_tdata (abfd)->strtab_hdr;
3103 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3104
3105 elf_tdata (abfd)->next_file_pos = off;
3106
3107 /* Now that we know where the .strtab section goes, write it
3108 out. */
3109 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3110 || ! _bfd_stringtab_emit (abfd, strtab))
3111 return false;
3112 _bfd_stringtab_free (strtab);
3113 }
3114
3115 abfd->output_has_begun = true;
3116
3117 return true;
3118}
3119
3120/* Create a mapping from a set of sections to a program segment. */
3121
3122static INLINE struct elf_segment_map *
3123make_mapping (abfd, sections, from, to, phdr)
3124 bfd *abfd;
3125 asection **sections;
3126 unsigned int from;
3127 unsigned int to;
3128 boolean phdr;
3129{
3130 struct elf_segment_map *m;
3131 unsigned int i;
3132 asection **hdrpp;
dc810e39 3133 bfd_size_type amt;
252b5132 3134
dc810e39
AM
3135 amt = sizeof (struct elf_segment_map);
3136 amt += (to - from - 1) * sizeof (asection *);
3137 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3138 if (m == NULL)
3139 return NULL;
3140 m->next = NULL;
3141 m->p_type = PT_LOAD;
3142 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3143 m->sections[i - from] = *hdrpp;
3144 m->count = to - from;
3145
3146 if (from == 0 && phdr)
3147 {
3148 /* Include the headers in the first PT_LOAD segment. */
3149 m->includes_filehdr = 1;
3150 m->includes_phdrs = 1;
3151 }
3152
3153 return m;
3154}
3155
3156/* Set up a mapping from BFD sections to program segments. */
3157
3158static boolean
3159map_sections_to_segments (abfd)
3160 bfd *abfd;
3161{
3162 asection **sections = NULL;
3163 asection *s;
3164 unsigned int i;
3165 unsigned int count;
3166 struct elf_segment_map *mfirst;
3167 struct elf_segment_map **pm;
3168 struct elf_segment_map *m;
3169 asection *last_hdr;
3170 unsigned int phdr_index;
3171 bfd_vma maxpagesize;
3172 asection **hdrpp;
3173 boolean phdr_in_segment = true;
3174 boolean writable;
13ae64f3
JJ
3175 int tls_count = 0;
3176 asection *first_tls = NULL;
65765700 3177 asection *dynsec, *eh_frame_hdr;
dc810e39 3178 bfd_size_type amt;
252b5132
RH
3179
3180 if (elf_tdata (abfd)->segment_map != NULL)
3181 return true;
3182
3183 if (bfd_count_sections (abfd) == 0)
3184 return true;
3185
3186 /* Select the allocated sections, and sort them. */
3187
dc810e39
AM
3188 amt = bfd_count_sections (abfd) * sizeof (asection *);
3189 sections = (asection **) bfd_malloc (amt);
252b5132
RH
3190 if (sections == NULL)
3191 goto error_return;
3192
3193 i = 0;
3194 for (s = abfd->sections; s != NULL; s = s->next)
3195 {
3196 if ((s->flags & SEC_ALLOC) != 0)
3197 {
3198 sections[i] = s;
3199 ++i;
3200 }
3201 }
3202 BFD_ASSERT (i <= bfd_count_sections (abfd));
3203 count = i;
3204
3205 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3206
3207 /* Build the mapping. */
3208
3209 mfirst = NULL;
3210 pm = &mfirst;
3211
3212 /* If we have a .interp section, then create a PT_PHDR segment for
3213 the program headers and a PT_INTERP segment for the .interp
3214 section. */
3215 s = bfd_get_section_by_name (abfd, ".interp");
3216 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3217 {
dc810e39
AM
3218 amt = sizeof (struct elf_segment_map);
3219 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3220 if (m == NULL)
3221 goto error_return;
3222 m->next = NULL;
3223 m->p_type = PT_PHDR;
3224 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3225 m->p_flags = PF_R | PF_X;
3226 m->p_flags_valid = 1;
3227 m->includes_phdrs = 1;
3228
3229 *pm = m;
3230 pm = &m->next;
3231
dc810e39
AM
3232 amt = sizeof (struct elf_segment_map);
3233 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3234 if (m == NULL)
3235 goto error_return;
3236 m->next = NULL;
3237 m->p_type = PT_INTERP;
3238 m->count = 1;
3239 m->sections[0] = s;
3240
3241 *pm = m;
3242 pm = &m->next;
3243 }
3244
3245 /* Look through the sections. We put sections in the same program
3246 segment when the start of the second section can be placed within
3247 a few bytes of the end of the first section. */
3248 last_hdr = NULL;
3249 phdr_index = 0;
3250 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3251 writable = false;
3252 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3253 if (dynsec != NULL
3254 && (dynsec->flags & SEC_LOAD) == 0)
3255 dynsec = NULL;
3256
3257 /* Deal with -Ttext or something similar such that the first section
3258 is not adjacent to the program headers. This is an
3259 approximation, since at this point we don't know exactly how many
3260 program headers we will need. */
3261 if (count > 0)
3262 {
3263 bfd_size_type phdr_size;
3264
3265 phdr_size = elf_tdata (abfd)->program_header_size;
3266 if (phdr_size == 0)
3267 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3268 if ((abfd->flags & D_PAGED) == 0
3269 || sections[0]->lma < phdr_size
3270 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3271 phdr_in_segment = false;
3272 }
3273
3274 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3275 {
3276 asection *hdr;
3277 boolean new_segment;
3278
3279 hdr = *hdrpp;
3280
3281 /* See if this section and the last one will fit in the same
3282 segment. */
3283
3284 if (last_hdr == NULL)
3285 {
3286 /* If we don't have a segment yet, then we don't need a new
3287 one (we build the last one after this loop). */
3288 new_segment = false;
3289 }
3290 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3291 {
3292 /* If this section has a different relation between the
3293 virtual address and the load address, then we need a new
3294 segment. */
3295 new_segment = true;
3296 }
3297 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3298 < BFD_ALIGN (hdr->lma, maxpagesize))
3299 {
3300 /* If putting this section in this segment would force us to
3301 skip a page in the segment, then we need a new segment. */
3302 new_segment = true;
3303 }
3304 else if ((last_hdr->flags & SEC_LOAD) == 0
3305 && (hdr->flags & SEC_LOAD) != 0)
3306 {
3307 /* We don't want to put a loadable section after a
3308 nonloadable section in the same segment. */
3309 new_segment = true;
3310 }
3311 else if ((abfd->flags & D_PAGED) == 0)
3312 {
3313 /* If the file is not demand paged, which means that we
3314 don't require the sections to be correctly aligned in the
3315 file, then there is no other reason for a new segment. */
3316 new_segment = false;
3317 }
3318 else if (! writable
3319 && (hdr->flags & SEC_READONLY) == 0
b89fe0ee
AM
3320 && (((last_hdr->lma + last_hdr->_raw_size - 1)
3321 & ~(maxpagesize - 1))
3322 != (hdr->lma & ~(maxpagesize - 1))))
252b5132
RH
3323 {
3324 /* We don't want to put a writable section in a read only
3325 segment, unless they are on the same page in memory
3326 anyhow. We already know that the last section does not
3327 bring us past the current section on the page, so the
3328 only case in which the new section is not on the same
3329 page as the previous section is when the previous section
3330 ends precisely on a page boundary. */
3331 new_segment = true;
3332 }
3333 else
3334 {
3335 /* Otherwise, we can use the same segment. */
3336 new_segment = false;
3337 }
3338
3339 if (! new_segment)
3340 {
3341 if ((hdr->flags & SEC_READONLY) == 0)
3342 writable = true;
3343 last_hdr = hdr;
3344 continue;
3345 }
3346
3347 /* We need a new program segment. We must create a new program
3348 header holding all the sections from phdr_index until hdr. */
3349
3350 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3351 if (m == NULL)
3352 goto error_return;
3353
3354 *pm = m;
3355 pm = &m->next;
3356
3357 if ((hdr->flags & SEC_READONLY) == 0)
3358 writable = true;
3359 else
3360 writable = false;
3361
3362 last_hdr = hdr;
3363 phdr_index = i;
3364 phdr_in_segment = false;
3365 }
3366
3367 /* Create a final PT_LOAD program segment. */
3368 if (last_hdr != NULL)
3369 {
3370 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3371 if (m == NULL)
3372 goto error_return;
3373
3374 *pm = m;
3375 pm = &m->next;
3376 }
3377
3378 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3379 if (dynsec != NULL)
3380 {
dc810e39
AM
3381 amt = sizeof (struct elf_segment_map);
3382 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3383 if (m == NULL)
3384 goto error_return;
3385 m->next = NULL;
3386 m->p_type = PT_DYNAMIC;
3387 m->count = 1;
3388 m->sections[0] = dynsec;
3389
3390 *pm = m;
3391 pm = &m->next;
3392 }
3393
3394 /* For each loadable .note section, add a PT_NOTE segment. We don't
3395 use bfd_get_section_by_name, because if we link together
3396 nonloadable .note sections and loadable .note sections, we will
3397 generate two .note sections in the output file. FIXME: Using
3398 names for section types is bogus anyhow. */
3399 for (s = abfd->sections; s != NULL; s = s->next)
3400 {
3401 if ((s->flags & SEC_LOAD) != 0
3402 && strncmp (s->name, ".note", 5) == 0)
3403 {
dc810e39
AM
3404 amt = sizeof (struct elf_segment_map);
3405 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3406 if (m == NULL)
3407 goto error_return;
3408 m->next = NULL;
3409 m->p_type = PT_NOTE;
3410 m->count = 1;
3411 m->sections[0] = s;
3412
3413 *pm = m;
3414 pm = &m->next;
3415 }
13ae64f3
JJ
3416 if (s->flags & SEC_THREAD_LOCAL)
3417 {
3418 if (! tls_count)
3419 first_tls = s;
3420 tls_count++;
3421 }
3422 }
3423
3424 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3425 if (tls_count > 0)
3426 {
3427 int i;
3428
3429 amt = sizeof (struct elf_segment_map);
3430 amt += (tls_count - 1) * sizeof (asection *);
3431 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3432 if (m == NULL)
3433 goto error_return;
3434 m->next = NULL;
3435 m->p_type = PT_TLS;
3436 m->count = tls_count;
3437 /* Mandated PF_R. */
3438 m->p_flags = PF_R;
3439 m->p_flags_valid = 1;
3440 for (i = 0; i < tls_count; ++i)
3441 {
3442 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3443 m->sections[i] = first_tls;
3444 first_tls = first_tls->next;
3445 }
3446
3447 *pm = m;
3448 pm = &m->next;
252b5132
RH
3449 }
3450
65765700
JJ
3451 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3452 segment. */
9ad5cbcf
AM
3453 eh_frame_hdr = NULL;
3454 if (elf_tdata (abfd)->eh_frame_hdr)
3455 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
65765700
JJ
3456 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3457 {
3458 amt = sizeof (struct elf_segment_map);
3459 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3460 if (m == NULL)
3461 goto error_return;
3462 m->next = NULL;
3463 m->p_type = PT_GNU_EH_FRAME;
3464 m->count = 1;
3465 m->sections[0] = eh_frame_hdr;
3466
3467 *pm = m;
3468 pm = &m->next;
3469 }
3470
252b5132
RH
3471 free (sections);
3472 sections = NULL;
3473
3474 elf_tdata (abfd)->segment_map = mfirst;
3475 return true;
3476
3477 error_return:
3478 if (sections != NULL)
3479 free (sections);
3480 return false;
3481}
3482
3483/* Sort sections by address. */
3484
3485static int
3486elf_sort_sections (arg1, arg2)
3487 const PTR arg1;
3488 const PTR arg2;
3489{
3490 const asection *sec1 = *(const asection **) arg1;
3491 const asection *sec2 = *(const asection **) arg2;
3492
3493 /* Sort by LMA first, since this is the address used to
3494 place the section into a segment. */
3495 if (sec1->lma < sec2->lma)
3496 return -1;
3497 else if (sec1->lma > sec2->lma)
3498 return 1;
3499
3500 /* Then sort by VMA. Normally the LMA and the VMA will be
3501 the same, and this will do nothing. */
3502 if (sec1->vma < sec2->vma)
3503 return -1;
3504 else if (sec1->vma > sec2->vma)
3505 return 1;
3506
3507 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3508
3509#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3510
3511 if (TOEND (sec1))
3512 {
3513 if (TOEND (sec2))
00a7cdc5
NC
3514 {
3515 /* If the indicies are the same, do not return 0
3516 here, but continue to try the next comparison. */
3517 if (sec1->target_index - sec2->target_index != 0)
3518 return sec1->target_index - sec2->target_index;
3519 }
252b5132
RH
3520 else
3521 return 1;
3522 }
00a7cdc5 3523 else if (TOEND (sec2))
252b5132
RH
3524 return -1;
3525
3526#undef TOEND
3527
00a7cdc5
NC
3528 /* Sort by size, to put zero sized sections
3529 before others at the same address. */
252b5132
RH
3530
3531 if (sec1->_raw_size < sec2->_raw_size)
3532 return -1;
3533 if (sec1->_raw_size > sec2->_raw_size)
3534 return 1;
3535
3536 return sec1->target_index - sec2->target_index;
3537}
3538
3539/* Assign file positions to the sections based on the mapping from
3540 sections to segments. This function also sets up some fields in
3541 the file header, and writes out the program headers. */
3542
3543static boolean
3544assign_file_positions_for_segments (abfd)
3545 bfd *abfd;
3546{
3547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3548 unsigned int count;
3549 struct elf_segment_map *m;
3550 unsigned int alloc;
3551 Elf_Internal_Phdr *phdrs;
3552 file_ptr off, voff;
3553 bfd_vma filehdr_vaddr, filehdr_paddr;
3554 bfd_vma phdrs_vaddr, phdrs_paddr;
3555 Elf_Internal_Phdr *p;
dc810e39 3556 bfd_size_type amt;
252b5132
RH
3557
3558 if (elf_tdata (abfd)->segment_map == NULL)
3559 {
3560 if (! map_sections_to_segments (abfd))
3561 return false;
3562 }
1ed89aa9
NC
3563 else
3564 {
3565 /* The placement algorithm assumes that non allocated sections are
3566 not in PT_LOAD segments. We ensure this here by removing such
3567 sections from the segment map. */
3568 for (m = elf_tdata (abfd)->segment_map;
3569 m != NULL;
3570 m = m->next)
3571 {
3572 unsigned int new_count;
3573 unsigned int i;
3574
3575 if (m->p_type != PT_LOAD)
3576 continue;
3577
3578 new_count = 0;
3579 for (i = 0; i < m->count; i ++)
3580 {
3581 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3582 {
47d9a591 3583 if (i != new_count)
1ed89aa9
NC
3584 m->sections[new_count] = m->sections[i];
3585
3586 new_count ++;
3587 }
3588 }
3589
3590 if (new_count != m->count)
3591 m->count = new_count;
3592 }
3593 }
252b5132
RH
3594
3595 if (bed->elf_backend_modify_segment_map)
3596 {
3597 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3598 return false;
3599 }
3600
3601 count = 0;
3602 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3603 ++count;
3604
3605 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3606 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3607 elf_elfheader (abfd)->e_phnum = count;
3608
3609 if (count == 0)
3610 return true;
3611
3612 /* If we already counted the number of program segments, make sure
3613 that we allocated enough space. This happens when SIZEOF_HEADERS
3614 is used in a linker script. */
3615 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3616 if (alloc != 0 && count > alloc)
3617 {
3618 ((*_bfd_error_handler)
3619 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3620 bfd_get_filename (abfd), alloc, count));
3621 bfd_set_error (bfd_error_bad_value);
3622 return false;
3623 }
3624
3625 if (alloc == 0)
3626 alloc = count;
3627
dc810e39
AM
3628 amt = alloc * sizeof (Elf_Internal_Phdr);
3629 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
252b5132
RH
3630 if (phdrs == NULL)
3631 return false;
3632
3633 off = bed->s->sizeof_ehdr;
3634 off += alloc * bed->s->sizeof_phdr;
3635
3636 filehdr_vaddr = 0;
3637 filehdr_paddr = 0;
3638 phdrs_vaddr = 0;
3639 phdrs_paddr = 0;
3640
3641 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3642 m != NULL;
3643 m = m->next, p++)
3644 {
3645 unsigned int i;
3646 asection **secpp;
3647
3648 /* If elf_segment_map is not from map_sections_to_segments, the
47d9a591 3649 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
3650 not be done to the PT_NOTE section of a corefile, which may
3651 contain several pseudo-sections artificially created by bfd.
3652 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
3653 if (m->count > 1
3654 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 3655 && m->p_type == PT_NOTE))
252b5132
RH
3656 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3657 elf_sort_sections);
3658
3659 p->p_type = m->p_type;
28a7f3e7 3660 p->p_flags = m->p_flags;
252b5132
RH
3661
3662 if (p->p_type == PT_LOAD
3663 && m->count > 0
3664 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3665 {
3666 if ((abfd->flags & D_PAGED) != 0)
3667 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3668 else
3669 {
3670 bfd_size_type align;
3671
3672 align = 0;
3673 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3674 {
3675 bfd_size_type secalign;
3676
3677 secalign = bfd_get_section_alignment (abfd, *secpp);
3678 if (secalign > align)
3679 align = secalign;
3680 }
3681
3682 off += (m->sections[0]->vma - off) % (1 << align);
3683 }
3684 }
3685
3686 if (m->count == 0)
3687 p->p_vaddr = 0;
3688 else
3689 p->p_vaddr = m->sections[0]->vma;
3690
3691 if (m->p_paddr_valid)
3692 p->p_paddr = m->p_paddr;
3693 else if (m->count == 0)
3694 p->p_paddr = 0;
3695 else
3696 p->p_paddr = m->sections[0]->lma;
3697
3698 if (p->p_type == PT_LOAD
3699 && (abfd->flags & D_PAGED) != 0)
3700 p->p_align = bed->maxpagesize;
3701 else if (m->count == 0)
3702 p->p_align = bed->s->file_align;
3703 else
3704 p->p_align = 0;
3705
3706 p->p_offset = 0;
3707 p->p_filesz = 0;
3708 p->p_memsz = 0;
3709
3710 if (m->includes_filehdr)
3711 {
3712 if (! m->p_flags_valid)
3713 p->p_flags |= PF_R;
3714 p->p_offset = 0;
3715 p->p_filesz = bed->s->sizeof_ehdr;
3716 p->p_memsz = bed->s->sizeof_ehdr;
3717 if (m->count > 0)
3718 {
3719 BFD_ASSERT (p->p_type == PT_LOAD);
3720
3721 if (p->p_vaddr < (bfd_vma) off)
3722 {
caf47ea6
AM
3723 (*_bfd_error_handler)
3724 (_("%s: Not enough room for program headers, try linking with -N"),
3725 bfd_get_filename (abfd));
252b5132
RH
3726 bfd_set_error (bfd_error_bad_value);
3727 return false;
3728 }
3729
3730 p->p_vaddr -= off;
3731 if (! m->p_paddr_valid)
3732 p->p_paddr -= off;
3733 }
3734 if (p->p_type == PT_LOAD)
3735 {
3736 filehdr_vaddr = p->p_vaddr;
3737 filehdr_paddr = p->p_paddr;
3738 }
3739 }
3740
3741 if (m->includes_phdrs)
3742 {
3743 if (! m->p_flags_valid)
3744 p->p_flags |= PF_R;
3745
3746 if (m->includes_filehdr)
3747 {
3748 if (p->p_type == PT_LOAD)
3749 {
3750 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3751 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3752 }
3753 }
3754 else
3755 {
3756 p->p_offset = bed->s->sizeof_ehdr;
3757
3758 if (m->count > 0)
3759 {
3760 BFD_ASSERT (p->p_type == PT_LOAD);
3761 p->p_vaddr -= off - p->p_offset;
3762 if (! m->p_paddr_valid)
3763 p->p_paddr -= off - p->p_offset;
3764 }
3765
3766 if (p->p_type == PT_LOAD)
3767 {
3768 phdrs_vaddr = p->p_vaddr;
3769 phdrs_paddr = p->p_paddr;
3770 }
3771 else
3772 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3773 }
3774
3775 p->p_filesz += alloc * bed->s->sizeof_phdr;
3776 p->p_memsz += alloc * bed->s->sizeof_phdr;
3777 }
3778
3779 if (p->p_type == PT_LOAD
3780 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3781 {
3782 if (! m->includes_filehdr && ! m->includes_phdrs)
3783 p->p_offset = off;
3784 else
3785 {
3786 file_ptr adjust;
3787
3788 adjust = off - (p->p_offset + p->p_filesz);
3789 p->p_filesz += adjust;
3790 p->p_memsz += adjust;
3791 }
3792 }
3793
3794 voff = off;
3795
3796 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3797 {
3798 asection *sec;
3799 flagword flags;
3800 bfd_size_type align;
3801
3802 sec = *secpp;
3803 flags = sec->flags;
3804 align = 1 << bfd_get_section_alignment (abfd, sec);
3805
3806 /* The section may have artificial alignment forced by a
3807 link script. Notice this case by the gap between the
f5ffc919
NC
3808 cumulative phdr lma and the section's lma. */
3809 if (p->p_paddr + p->p_memsz < sec->lma)
252b5132 3810 {
f5ffc919 3811 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
252b5132
RH
3812
3813 p->p_memsz += adjust;
3814 off += adjust;
3815 voff += adjust;
3816 if ((flags & SEC_LOAD) != 0)
3817 p->p_filesz += adjust;
3818 }
3819
3820 if (p->p_type == PT_LOAD)
3821 {
3822 bfd_signed_vma adjust;
3823
3824 if ((flags & SEC_LOAD) != 0)
3825 {
3826 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3827 if (adjust < 0)
3828 adjust = 0;
3829 }
3830 else if ((flags & SEC_ALLOC) != 0)
3831 {
3832 /* The section VMA must equal the file position
3833 modulo the page size. FIXME: I'm not sure if
3834 this adjustment is really necessary. We used to
3835 not have the SEC_LOAD case just above, and then
3836 this was necessary, but now I'm not sure. */
3837 if ((abfd->flags & D_PAGED) != 0)
3838 adjust = (sec->vma - voff) % bed->maxpagesize;
3839 else
3840 adjust = (sec->vma - voff) % align;
3841 }
3842 else
3843 adjust = 0;
3844
3845 if (adjust != 0)
3846 {
3847 if (i == 0)
3848 {
cdc7c09f
NC
3849 (* _bfd_error_handler) (_("\
3850Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3851 bfd_section_name (abfd, sec),
3852 sec->lma,
3853 p->p_paddr);
252b5132
RH
3854 return false;
3855 }
3856 p->p_memsz += adjust;
3857 off += adjust;
3858 voff += adjust;
3859 if ((flags & SEC_LOAD) != 0)
3860 p->p_filesz += adjust;
3861 }
3862
3863 sec->filepos = off;
3864
3865 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3866 used in a linker script we may have a section with
3867 SEC_LOAD clear but which is supposed to have
3868 contents. */
3869 if ((flags & SEC_LOAD) != 0
3870 || (flags & SEC_HAS_CONTENTS) != 0)
3871 off += sec->_raw_size;
3872
3873 if ((flags & SEC_ALLOC) != 0)
3874 voff += sec->_raw_size;
3875 }
3876
3877 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3878 {
4a938328
MS
3879 /* The actual "note" segment has i == 0.
3880 This is the one that actually contains everything. */
3881 if (i == 0)
3882 {
252b5132
RH
3883 sec->filepos = off;
3884 p->p_filesz = sec->_raw_size;
3885 off += sec->_raw_size;
3886 voff = off;
3887 }
4a938328 3888 else
252b5132 3889 {
4a938328 3890 /* Fake sections -- don't need to be written. */
252b5132
RH
3891 sec->filepos = 0;
3892 sec->_raw_size = 0;
4a938328 3893 flags = sec->flags = 0;
252b5132
RH
3894 }
3895 p->p_memsz = 0;
3896 p->p_align = 1;
3897 }
3898 else
3899 {
3900 p->p_memsz += sec->_raw_size;
3901
3902 if ((flags & SEC_LOAD) != 0)
3903 p->p_filesz += sec->_raw_size;
3904
13ae64f3
JJ
3905 if (p->p_type == PT_TLS
3906 && sec->_raw_size == 0
3907 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3908 {
3909 struct bfd_link_order *o;
3910 bfd_vma tbss_size = 0;
3911
3912 for (o = sec->link_order_head; o != NULL; o = o->next)
3913 if (tbss_size < o->offset + o->size)
3914 tbss_size = o->offset + o->size;
3915
3916 p->p_memsz += tbss_size;
3917 }
3918
252b5132
RH
3919 if (align > p->p_align
3920 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3921 p->p_align = align;
3922 }
3923
3924 if (! m->p_flags_valid)
3925 {
3926 p->p_flags |= PF_R;
3927 if ((flags & SEC_CODE) != 0)
3928 p->p_flags |= PF_X;
3929 if ((flags & SEC_READONLY) == 0)
3930 p->p_flags |= PF_W;
3931 }
3932 }
3933 }
3934
3935 /* Now that we have set the section file positions, we can set up
3936 the file positions for the non PT_LOAD segments. */
3937 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3938 m != NULL;
3939 m = m->next, p++)
3940 {
3941 if (p->p_type != PT_LOAD && m->count > 0)
3942 {
3943 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3944 p->p_offset = m->sections[0]->filepos;
3945 }
3946 if (m->count == 0)
3947 {
3948 if (m->includes_filehdr)
3949 {
3950 p->p_vaddr = filehdr_vaddr;
3951 if (! m->p_paddr_valid)
3952 p->p_paddr = filehdr_paddr;
3953 }
3954 else if (m->includes_phdrs)
3955 {
3956 p->p_vaddr = phdrs_vaddr;
3957 if (! m->p_paddr_valid)
3958 p->p_paddr = phdrs_paddr;
3959 }
3960 }
3961 }
3962
caf47ea6
AM
3963 /* If additional nonloadable filepos adjustments are required,
3964 do them now. */
3965 if (bed->set_nonloadable_filepos)
3966 (*bed->set_nonloadable_filepos) (abfd, phdrs);
3967
252b5132
RH
3968 /* Clear out any program headers we allocated but did not use. */
3969 for (; count < alloc; count++, p++)
3970 {
3971 memset (p, 0, sizeof *p);
3972 p->p_type = PT_NULL;
3973 }
3974
3975 elf_tdata (abfd)->phdr = phdrs;
3976
3977 elf_tdata (abfd)->next_file_pos = off;
3978
3979 /* Write out the program headers. */
dc810e39 3980 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
252b5132
RH
3981 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3982 return false;
3983
3984 return true;
3985}
3986
3987/* Get the size of the program header.
3988
3989 If this is called by the linker before any of the section VMA's are set, it
3990 can't calculate the correct value for a strange memory layout. This only
3991 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3992 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3993 data segment (exclusive of .interp and .dynamic).
3994
3995 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3996 will be two segments. */
3997
3998static bfd_size_type
3999get_program_header_size (abfd)
4000 bfd *abfd;
4001{
4002 size_t segs;
4003 asection *s;
4004 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4005
4006 /* We can't return a different result each time we're called. */
4007 if (elf_tdata (abfd)->program_header_size != 0)
4008 return elf_tdata (abfd)->program_header_size;
4009
4010 if (elf_tdata (abfd)->segment_map != NULL)
4011 {
4012 struct elf_segment_map *m;
4013
4014 segs = 0;
4015 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4016 ++segs;
4017 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4018 return elf_tdata (abfd)->program_header_size;
4019 }
4020
4021 /* Assume we will need exactly two PT_LOAD segments: one for text
4022 and one for data. */
4023 segs = 2;
4024
4025 s = bfd_get_section_by_name (abfd, ".interp");
4026 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4027 {
4028 /* If we have a loadable interpreter section, we need a
4029 PT_INTERP segment. In this case, assume we also need a
4030 PT_PHDR segment, although that may not be true for all
4031 targets. */
4032 segs += 2;
4033 }
4034
4035 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4036 {
4037 /* We need a PT_DYNAMIC segment. */
4038 ++segs;
4039 }
4040
65765700
JJ
4041 if (elf_tdata (abfd)->eh_frame_hdr
4042 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
4043 {
4044 /* We need a PT_GNU_EH_FRAME segment. */
4045 ++segs;
4046 }
4047
252b5132
RH
4048 for (s = abfd->sections; s != NULL; s = s->next)
4049 {
4050 if ((s->flags & SEC_LOAD) != 0
4051 && strncmp (s->name, ".note", 5) == 0)
4052 {
4053 /* We need a PT_NOTE segment. */
4054 ++segs;
4055 }
4056 }
4057
13ae64f3
JJ
4058 for (s = abfd->sections; s != NULL; s = s->next)
4059 {
4060 if (s->flags & SEC_THREAD_LOCAL)
4061 {
4062 /* We need a PT_TLS segment. */
4063 ++segs;
4064 break;
4065 }
4066 }
4067
252b5132
RH
4068 /* Let the backend count up any program headers it might need. */
4069 if (bed->elf_backend_additional_program_headers)
4070 {
4071 int a;
4072
4073 a = (*bed->elf_backend_additional_program_headers) (abfd);
4074 if (a == -1)
4075 abort ();
4076 segs += a;
4077 }
4078
4079 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4080 return elf_tdata (abfd)->program_header_size;
4081}
4082
4083/* Work out the file positions of all the sections. This is called by
4084 _bfd_elf_compute_section_file_positions. All the section sizes and
4085 VMAs must be known before this is called.
4086
4087 We do not consider reloc sections at this point, unless they form
4088 part of the loadable image. Reloc sections are assigned file
4089 positions in assign_file_positions_for_relocs, which is called by
4090 write_object_contents and final_link.
4091
4092 We also don't set the positions of the .symtab and .strtab here. */
4093
4094static boolean
4095assign_file_positions_except_relocs (abfd)
4096 bfd *abfd;
4097{
4098 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4099 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4100 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
9ad5cbcf 4101 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
4102 file_ptr off;
4103 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4104
4105 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4106 && bfd_get_format (abfd) != bfd_core)
4107 {
4108 Elf_Internal_Shdr **hdrpp;
4109 unsigned int i;
4110
4111 /* Start after the ELF header. */
4112 off = i_ehdrp->e_ehsize;
4113
4114 /* We are not creating an executable, which means that we are
4115 not creating a program header, and that the actual order of
4116 the sections in the file is unimportant. */
9ad5cbcf 4117 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
4118 {
4119 Elf_Internal_Shdr *hdr;
4120
4121 hdr = *hdrpp;
9ad5cbcf
AM
4122 if (hdr->sh_type == SHT_REL
4123 || hdr->sh_type == SHT_RELA
4124 || i == tdata->symtab_section
4125 || i == tdata->symtab_shndx_section
252b5132
RH
4126 || i == tdata->strtab_section)
4127 {
4128 hdr->sh_offset = -1;
252b5132 4129 }
9ad5cbcf
AM
4130 else
4131 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4132
9ad5cbcf
AM
4133 if (i == SHN_LORESERVE - 1)
4134 {
4135 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4136 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4137 }
252b5132
RH
4138 }
4139 }
4140 else
4141 {
4142 unsigned int i;
4143 Elf_Internal_Shdr **hdrpp;
4144
4145 /* Assign file positions for the loaded sections based on the
4146 assignment of sections to segments. */
4147 if (! assign_file_positions_for_segments (abfd))
4148 return false;
4149
4150 /* Assign file positions for the other sections. */
4151
4152 off = elf_tdata (abfd)->next_file_pos;
9ad5cbcf 4153 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
4154 {
4155 Elf_Internal_Shdr *hdr;
4156
4157 hdr = *hdrpp;
4158 if (hdr->bfd_section != NULL
4159 && hdr->bfd_section->filepos != 0)
4160 hdr->sh_offset = hdr->bfd_section->filepos;
4161 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4162 {
4163 ((*_bfd_error_handler)
4164 (_("%s: warning: allocated section `%s' not in segment"),
4165 bfd_get_filename (abfd),
4166 (hdr->bfd_section == NULL
4167 ? "*unknown*"
4168 : hdr->bfd_section->name)));
4169 if ((abfd->flags & D_PAGED) != 0)
4170 off += (hdr->sh_addr - off) % bed->maxpagesize;
4171 else
4172 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4173 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4174 false);
4175 }
4176 else if (hdr->sh_type == SHT_REL
4177 || hdr->sh_type == SHT_RELA
4178 || hdr == i_shdrpp[tdata->symtab_section]
9ad5cbcf 4179 || hdr == i_shdrpp[tdata->symtab_shndx_section]
252b5132
RH
4180 || hdr == i_shdrpp[tdata->strtab_section])
4181 hdr->sh_offset = -1;
4182 else
4183 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
9ad5cbcf
AM
4184
4185 if (i == SHN_LORESERVE - 1)
4186 {
4187 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4188 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4189 }
252b5132
RH
4190 }
4191 }
4192
4193 /* Place the section headers. */
4194 off = align_file_position (off, bed->s->file_align);
4195 i_ehdrp->e_shoff = off;
4196 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4197
4198 elf_tdata (abfd)->next_file_pos = off;
4199
4200 return true;
4201}
4202
4203static boolean
4204prep_headers (abfd)
4205 bfd *abfd;
4206{
4207 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4208 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4209 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2b0f7ef9 4210 struct elf_strtab_hash *shstrtab;
252b5132
RH
4211 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4212
4213 i_ehdrp = elf_elfheader (abfd);
4214 i_shdrp = elf_elfsections (abfd);
4215
2b0f7ef9 4216 shstrtab = _bfd_elf_strtab_init ();
252b5132
RH
4217 if (shstrtab == NULL)
4218 return false;
4219
4220 elf_shstrtab (abfd) = shstrtab;
4221
4222 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4223 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4224 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4225 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4226
4227 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4228 i_ehdrp->e_ident[EI_DATA] =
4229 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4230 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4231
252b5132
RH
4232 if ((abfd->flags & DYNAMIC) != 0)
4233 i_ehdrp->e_type = ET_DYN;
4234 else if ((abfd->flags & EXEC_P) != 0)
4235 i_ehdrp->e_type = ET_EXEC;
4236 else if (bfd_get_format (abfd) == bfd_core)
4237 i_ehdrp->e_type = ET_CORE;
4238 else
4239 i_ehdrp->e_type = ET_REL;
4240
4241 switch (bfd_get_arch (abfd))
4242 {
4243 case bfd_arch_unknown:
4244 i_ehdrp->e_machine = EM_NONE;
4245 break;
aa4f99bb
AO
4246
4247 /* There used to be a long list of cases here, each one setting
4248 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4249 in the corresponding bfd definition. To avoid duplication,
4250 the switch was removed. Machines that need special handling
4251 can generally do it in elf_backend_final_write_processing(),
4252 unless they need the information earlier than the final write.
4253 Such need can generally be supplied by replacing the tests for
4254 e_machine with the conditions used to determine it. */
252b5132 4255 default:
aa4f99bb
AO
4256 if (get_elf_backend_data (abfd) != NULL)
4257 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4258 else
4259 i_ehdrp->e_machine = EM_NONE;
4260 }
4261
252b5132
RH
4262 i_ehdrp->e_version = bed->s->ev_current;
4263 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4264
c044fabd 4265 /* No program header, for now. */
252b5132
RH
4266 i_ehdrp->e_phoff = 0;
4267 i_ehdrp->e_phentsize = 0;
4268 i_ehdrp->e_phnum = 0;
4269
c044fabd 4270 /* Each bfd section is section header entry. */
252b5132
RH
4271 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4272 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4273
c044fabd 4274 /* If we're building an executable, we'll need a program header table. */
252b5132
RH
4275 if (abfd->flags & EXEC_P)
4276 {
c044fabd 4277 /* It all happens later. */
252b5132
RH
4278#if 0
4279 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4280
4281 /* elf_build_phdrs() returns a (NULL-terminated) array of
c044fabd 4282 Elf_Internal_Phdrs. */
252b5132
RH
4283 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4284 i_ehdrp->e_phoff = outbase;
4285 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4286#endif
4287 }
4288 else
4289 {
4290 i_ehdrp->e_phentsize = 0;
4291 i_phdrp = 0;
4292 i_ehdrp->e_phoff = 0;
4293 }
4294
4295 elf_tdata (abfd)->symtab_hdr.sh_name =
2b0f7ef9 4296 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
252b5132 4297 elf_tdata (abfd)->strtab_hdr.sh_name =
2b0f7ef9 4298 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
252b5132 4299 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2b0f7ef9 4300 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
252b5132
RH
4301 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4302 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4303 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4304 return false;
4305
4306 return true;
4307}
4308
4309/* Assign file positions for all the reloc sections which are not part
4310 of the loadable file image. */
4311
4312void
4313_bfd_elf_assign_file_positions_for_relocs (abfd)
4314 bfd *abfd;
4315{
4316 file_ptr off;
9ad5cbcf 4317 unsigned int i, num_sec;
252b5132
RH
4318 Elf_Internal_Shdr **shdrpp;
4319
4320 off = elf_tdata (abfd)->next_file_pos;
4321
9ad5cbcf
AM
4322 num_sec = elf_numsections (abfd);
4323 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
252b5132
RH
4324 {
4325 Elf_Internal_Shdr *shdrp;
4326
4327 shdrp = *shdrpp;
4328 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4329 && shdrp->sh_offset == -1)
4330 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
4331 }
4332
4333 elf_tdata (abfd)->next_file_pos = off;
4334}
4335
4336boolean
4337_bfd_elf_write_object_contents (abfd)
4338 bfd *abfd;
4339{
4340 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4341 Elf_Internal_Ehdr *i_ehdrp;
4342 Elf_Internal_Shdr **i_shdrp;
4343 boolean failed;
9ad5cbcf 4344 unsigned int count, num_sec;
252b5132
RH
4345
4346 if (! abfd->output_has_begun
4347 && ! _bfd_elf_compute_section_file_positions
4348 (abfd, (struct bfd_link_info *) NULL))
4349 return false;
4350
4351 i_shdrp = elf_elfsections (abfd);
4352 i_ehdrp = elf_elfheader (abfd);
4353
4354 failed = false;
4355 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4356 if (failed)
4357 return false;
4358
4359 _bfd_elf_assign_file_positions_for_relocs (abfd);
4360
c044fabd 4361 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
4362 num_sec = elf_numsections (abfd);
4363 for (count = 1; count < num_sec; count++)
252b5132
RH
4364 {
4365 if (bed->elf_backend_section_processing)
4366 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4367 if (i_shdrp[count]->contents)
4368 {
dc810e39
AM
4369 bfd_size_type amt = i_shdrp[count]->sh_size;
4370
252b5132 4371 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 4372 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
252b5132
RH
4373 return false;
4374 }
9ad5cbcf
AM
4375 if (count == SHN_LORESERVE - 1)
4376 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
4377 }
4378
4379 /* Write out the section header names. */
4380 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2b0f7ef9 4381 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
252b5132
RH
4382 return false;
4383
4384 if (bed->elf_backend_final_write_processing)
4385 (*bed->elf_backend_final_write_processing) (abfd,
4386 elf_tdata (abfd)->linker);
4387
4388 return bed->s->write_shdrs_and_ehdr (abfd);
4389}
4390
4391boolean
4392_bfd_elf_write_corefile_contents (abfd)
4393 bfd *abfd;
4394{
c044fabd 4395 /* Hopefully this can be done just like an object file. */
252b5132
RH
4396 return _bfd_elf_write_object_contents (abfd);
4397}
c044fabd
KH
4398
4399/* Given a section, search the header to find them. */
4400
252b5132
RH
4401int
4402_bfd_elf_section_from_bfd_section (abfd, asect)
4403 bfd *abfd;
4404 struct sec *asect;
4405{
af746e92 4406 struct elf_backend_data *bed;
252b5132 4407 int index;
252b5132 4408
9ad5cbcf
AM
4409 if (elf_section_data (asect) != NULL
4410 && elf_section_data (asect)->this_idx != 0)
4411 return elf_section_data (asect)->this_idx;
4412
4413 if (bfd_is_abs_section (asect))
af746e92
AM
4414 index = SHN_ABS;
4415 else if (bfd_is_com_section (asect))
4416 index = SHN_COMMON;
4417 else if (bfd_is_und_section (asect))
4418 index = SHN_UNDEF;
4419 else
252b5132 4420 {
af746e92
AM
4421 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4422 int maxindex = elf_numsections (abfd);
4423
4424 for (index = 1; index < maxindex; index++)
4425 {
4426 Elf_Internal_Shdr *hdr = i_shdrp[index];
4427
4428 if (hdr != NULL && hdr->bfd_section == asect)
4429 return index;
4430 }
4431 index = -1;
252b5132
RH
4432 }
4433
af746e92 4434 bed = get_elf_backend_data (abfd);
252b5132
RH
4435 if (bed->elf_backend_section_from_bfd_section)
4436 {
af746e92 4437 int retval = index;
9ad5cbcf 4438
af746e92
AM
4439 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4440 return retval;
252b5132
RH
4441 }
4442
af746e92
AM
4443 if (index == -1)
4444 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 4445
af746e92 4446 return index;
252b5132
RH
4447}
4448
4449/* Given a BFD symbol, return the index in the ELF symbol table, or -1
4450 on error. */
4451
4452int
4453_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4454 bfd *abfd;
4455 asymbol **asym_ptr_ptr;
4456{
4457 asymbol *asym_ptr = *asym_ptr_ptr;
4458 int idx;
4459 flagword flags = asym_ptr->flags;
4460
4461 /* When gas creates relocations against local labels, it creates its
4462 own symbol for the section, but does put the symbol into the
4463 symbol chain, so udata is 0. When the linker is generating
4464 relocatable output, this section symbol may be for one of the
4465 input sections rather than the output section. */
4466 if (asym_ptr->udata.i == 0
4467 && (flags & BSF_SECTION_SYM)
4468 && asym_ptr->section)
4469 {
4470 int indx;
4471
4472 if (asym_ptr->section->output_section != NULL)
4473 indx = asym_ptr->section->output_section->index;
4474 else
4475 indx = asym_ptr->section->index;
4e89ac30
L
4476 if (indx < elf_num_section_syms (abfd)
4477 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
4478 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4479 }
4480
4481 idx = asym_ptr->udata.i;
4482
4483 if (idx == 0)
4484 {
4485 /* This case can occur when using --strip-symbol on a symbol
4486 which is used in a relocation entry. */
4487 (*_bfd_error_handler)
4488 (_("%s: symbol `%s' required but not present"),
8f615d07 4489 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
252b5132
RH
4490 bfd_set_error (bfd_error_no_symbols);
4491 return -1;
4492 }
4493
4494#if DEBUG & 4
4495 {
4496 fprintf (stderr,
661a3fd4 4497 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
252b5132
RH
4498 (long) asym_ptr, asym_ptr->name, idx, flags,
4499 elf_symbol_flags (flags));
4500 fflush (stderr);
4501 }
4502#endif
4503
4504 return idx;
4505}
4506
4507/* Copy private BFD data. This copies any program header information. */
4508
4509static boolean
4510copy_private_bfd_data (ibfd, obfd)
4511 bfd *ibfd;
4512 bfd *obfd;
4513{
bc67d8a6
NC
4514 Elf_Internal_Ehdr * iehdr;
4515 struct elf_segment_map * map;
4516 struct elf_segment_map * map_first;
4517 struct elf_segment_map ** pointer_to_map;
4518 Elf_Internal_Phdr * segment;
4519 asection * section;
4520 unsigned int i;
4521 unsigned int num_segments;
4522 boolean phdr_included = false;
4523 bfd_vma maxpagesize;
4524 struct elf_segment_map * phdr_adjust_seg = NULL;
4525 unsigned int phdr_adjust_num = 0;
caf47ea6 4526 struct elf_backend_data * bed;
bc67d8a6 4527
c044fabd 4528 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
252b5132
RH
4529 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4530 return true;
4531
4532 if (elf_tdata (ibfd)->phdr == NULL)
4533 return true;
4534
caf47ea6 4535 bed = get_elf_backend_data (ibfd);
252b5132
RH
4536 iehdr = elf_elfheader (ibfd);
4537
bc67d8a6 4538 map_first = NULL;
c044fabd 4539 pointer_to_map = &map_first;
252b5132
RH
4540
4541 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
4542 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4543
4544 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
4545#define SEGMENT_END(segment, start) \
4546 (start + (segment->p_memsz > segment->p_filesz \
4547 ? segment->p_memsz : segment->p_filesz))
bc67d8a6
NC
4548
4549 /* Returns true if the given section is contained within
4550 the given segment. VMA addresses are compared. */
aecc8f8a
AM
4551#define IS_CONTAINED_BY_VMA(section, segment) \
4552 (section->vma >= segment->p_vaddr \
4553 && (section->vma + section->_raw_size \
4554 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 4555
bc67d8a6
NC
4556 /* Returns true if the given section is contained within
4557 the given segment. LMA addresses are compared. */
aecc8f8a
AM
4558#define IS_CONTAINED_BY_LMA(section, segment, base) \
4559 (section->lma >= base \
4560 && (section->lma + section->_raw_size \
4561 <= SEGMENT_END (segment, base)))
252b5132 4562
caf47ea6
AM
4563 /* Returns true if the given section is contained within the
4564 given segment. Filepos addresses are compared in an elf
4565 backend function. */
aecc8f8a
AM
4566#define IS_CONTAINED_BY_FILEPOS(sec, seg, bed) \
4567 (bed->is_contained_by_filepos \
caf47ea6
AM
4568 && (*bed->is_contained_by_filepos) (sec, seg))
4569
c044fabd 4570 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
aecc8f8a
AM
4571#define IS_COREFILE_NOTE(p, s) \
4572 (p->p_type == PT_NOTE \
4573 && bfd_get_format (ibfd) == bfd_core \
4574 && s->vma == 0 && s->lma == 0 \
4575 && (bfd_vma) s->filepos >= p->p_offset \
4576 && ((bfd_vma) s->filepos + s->_raw_size \
4577 <= p->p_offset + p->p_filesz))
252b5132
RH
4578
4579 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4580 linker, which generates a PT_INTERP section with p_vaddr and
4581 p_memsz set to 0. */
aecc8f8a
AM
4582#define IS_SOLARIS_PT_INTERP(p, s) \
4583 (p->p_vaddr == 0 \
4584 && p->p_paddr == 0 \
4585 && p->p_memsz == 0 \
4586 && p->p_filesz > 0 \
4587 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4588 && s->_raw_size > 0 \
4589 && (bfd_vma) s->filepos >= p->p_offset \
4590 && ((bfd_vma) s->filepos + s->_raw_size \
4591 <= p->p_offset + p->p_filesz))
5c440b1e 4592
bc67d8a6
NC
4593 /* Decide if the given section should be included in the given segment.
4594 A section will be included if:
f5ffc919
NC
4595 1. It is within the address space of the segment -- we use the LMA
4596 if that is set for the segment and the VMA otherwise,
bc67d8a6
NC
4597 2. It is an allocated segment,
4598 3. There is an output section associated with it,
4599 4. The section has not already been allocated to a previous segment. */
caf47ea6 4600#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
aecc8f8a
AM
4601 ((((segment->p_paddr \
4602 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4603 : IS_CONTAINED_BY_VMA (section, segment)) \
f5ffc919 4604 && (section->flags & SEC_ALLOC) != 0) \
caf47ea6
AM
4605 || IS_COREFILE_NOTE (segment, section) \
4606 || (IS_CONTAINED_BY_FILEPOS (section, segment, bed) \
4607 && (section->flags & SEC_ALLOC) == 0)) \
f5ffc919 4608 && section->output_section != NULL \
82e51918 4609 && ! section->segment_mark)
bc67d8a6
NC
4610
4611 /* Returns true iff seg1 starts after the end of seg2. */
aecc8f8a
AM
4612#define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4613 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
bc67d8a6
NC
4614
4615 /* Returns true iff seg1 and seg2 overlap. */
aecc8f8a
AM
4616#define SEGMENT_OVERLAPS(seg1, seg2) \
4617 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) \
4618 || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
bc67d8a6
NC
4619
4620 /* Initialise the segment mark field. */
4621 for (section = ibfd->sections; section != NULL; section = section->next)
4622 section->segment_mark = false;
4623
252b5132 4624 /* Scan through the segments specified in the program header
bc67d8a6 4625 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 4626 in the loadable segments. These can be created by weird
aecc8f8a 4627 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
4628 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4629 i < num_segments;
c044fabd 4630 i++, segment++)
252b5132 4631 {
252b5132 4632 unsigned int j;
c044fabd 4633 Elf_Internal_Phdr *segment2;
252b5132 4634
aecc8f8a
AM
4635 if (segment->p_type == PT_INTERP)
4636 for (section = ibfd->sections; section; section = section->next)
4637 if (IS_SOLARIS_PT_INTERP (segment, section))
4638 {
4639 /* Mininal change so that the normal section to segment
4640 assigment code will work. */
4641 segment->p_vaddr = section->vma;
4642 break;
4643 }
4644
bc67d8a6
NC
4645 if (segment->p_type != PT_LOAD)
4646 continue;
c044fabd 4647
bc67d8a6 4648 /* Determine if this segment overlaps any previous segments. */
c044fabd 4649 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
bc67d8a6
NC
4650 {
4651 bfd_signed_vma extra_length;
c044fabd 4652
bc67d8a6
NC
4653 if (segment2->p_type != PT_LOAD
4654 || ! SEGMENT_OVERLAPS (segment, segment2))
4655 continue;
c044fabd 4656
bc67d8a6
NC
4657 /* Merge the two segments together. */
4658 if (segment2->p_vaddr < segment->p_vaddr)
4659 {
c044fabd
KH
4660 /* Extend SEGMENT2 to include SEGMENT and then delete
4661 SEGMENT. */
bc67d8a6
NC
4662 extra_length =
4663 SEGMENT_END (segment, segment->p_vaddr)
4664 - SEGMENT_END (segment2, segment2->p_vaddr);
c044fabd 4665
bc67d8a6
NC
4666 if (extra_length > 0)
4667 {
4668 segment2->p_memsz += extra_length;
4669 segment2->p_filesz += extra_length;
4670 }
c044fabd 4671
bc67d8a6 4672 segment->p_type = PT_NULL;
c044fabd 4673
bc67d8a6
NC
4674 /* Since we have deleted P we must restart the outer loop. */
4675 i = 0;
4676 segment = elf_tdata (ibfd)->phdr;
4677 break;
4678 }
4679 else
4680 {
c044fabd
KH
4681 /* Extend SEGMENT to include SEGMENT2 and then delete
4682 SEGMENT2. */
bc67d8a6
NC
4683 extra_length =
4684 SEGMENT_END (segment2, segment2->p_vaddr)
4685 - SEGMENT_END (segment, segment->p_vaddr);
c044fabd 4686
bc67d8a6
NC
4687 if (extra_length > 0)
4688 {
4689 segment->p_memsz += extra_length;
4690 segment->p_filesz += extra_length;
4691 }
c044fabd 4692
bc67d8a6
NC
4693 segment2->p_type = PT_NULL;
4694 }
4695 }
4696 }
c044fabd 4697
bc67d8a6
NC
4698 /* The second scan attempts to assign sections to segments. */
4699 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4700 i < num_segments;
4701 i ++, segment ++)
4702 {
4703 unsigned int section_count;
4704 asection ** sections;
4705 asection * output_section;
4706 unsigned int isec;
4707 bfd_vma matching_lma;
4708 bfd_vma suggested_lma;
4709 unsigned int j;
dc810e39 4710 bfd_size_type amt;
bc67d8a6
NC
4711
4712 if (segment->p_type == PT_NULL)
4713 continue;
c044fabd 4714
bc67d8a6
NC
4715 /* Compute how many sections might be placed into this segment. */
4716 section_count = 0;
4717 for (section = ibfd->sections; section != NULL; section = section->next)
caf47ea6 4718 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c044fabd 4719 ++section_count;
252b5132
RH
4720
4721 /* Allocate a segment map big enough to contain all of the
4722 sections we have selected. */
dc810e39
AM
4723 amt = sizeof (struct elf_segment_map);
4724 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4725 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 4726 if (map == NULL)
252b5132
RH
4727 return false;
4728
4729 /* Initialise the fields of the segment map. Default to
4730 using the physical address of the segment in the input BFD. */
bc67d8a6
NC
4731 map->next = NULL;
4732 map->p_type = segment->p_type;
4733 map->p_flags = segment->p_flags;
4734 map->p_flags_valid = 1;
4735 map->p_paddr = segment->p_paddr;
4736 map->p_paddr_valid = 1;
252b5132
RH
4737
4738 /* Determine if this segment contains the ELF file header
4739 and if it contains the program headers themselves. */
bc67d8a6
NC
4740 map->includes_filehdr = (segment->p_offset == 0
4741 && segment->p_filesz >= iehdr->e_ehsize);
252b5132 4742
bc67d8a6 4743 map->includes_phdrs = 0;
252b5132 4744
bc67d8a6 4745 if (! phdr_included || segment->p_type != PT_LOAD)
252b5132 4746 {
bc67d8a6
NC
4747 map->includes_phdrs =
4748 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4749 && (segment->p_offset + segment->p_filesz
252b5132
RH
4750 >= ((bfd_vma) iehdr->e_phoff
4751 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 4752
bc67d8a6 4753 if (segment->p_type == PT_LOAD && map->includes_phdrs)
252b5132
RH
4754 phdr_included = true;
4755 }
4756
bc67d8a6 4757 if (section_count == 0)
252b5132
RH
4758 {
4759 /* Special segments, such as the PT_PHDR segment, may contain
4760 no sections, but ordinary, loadable segments should contain
1ed89aa9
NC
4761 something. They are allowed by the ELF spec however, so only
4762 a warning is produced. */
bc67d8a6 4763 if (segment->p_type == PT_LOAD)
caf47ea6 4764 (*_bfd_error_handler)
1ed89aa9 4765 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
caf47ea6 4766 bfd_archive_filename (ibfd));
252b5132 4767
bc67d8a6 4768 map->count = 0;
c044fabd
KH
4769 *pointer_to_map = map;
4770 pointer_to_map = &map->next;
252b5132
RH
4771
4772 continue;
4773 }
4774
4775 /* Now scan the sections in the input BFD again and attempt
4776 to add their corresponding output sections to the segment map.
4777 The problem here is how to handle an output section which has
4778 been moved (ie had its LMA changed). There are four possibilities:
4779
4780 1. None of the sections have been moved.
4781 In this case we can continue to use the segment LMA from the
4782 input BFD.
4783
4784 2. All of the sections have been moved by the same amount.
4785 In this case we can change the segment's LMA to match the LMA
4786 of the first section.
4787
4788 3. Some of the sections have been moved, others have not.
4789 In this case those sections which have not been moved can be
4790 placed in the current segment which will have to have its size,
4791 and possibly its LMA changed, and a new segment or segments will
4792 have to be created to contain the other sections.
4793
4794 4. The sections have been moved, but not be the same amount.
4795 In this case we can change the segment's LMA to match the LMA
4796 of the first section and we will have to create a new segment
4797 or segments to contain the other sections.
4798
4799 In order to save time, we allocate an array to hold the section
4800 pointers that we are interested in. As these sections get assigned
4801 to a segment, they are removed from this array. */
4802
0b14c2aa
L
4803 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4804 to work around this long long bug. */
4805 amt = section_count * sizeof (asection *);
dc810e39 4806 sections = (asection **) bfd_malloc (amt);
252b5132
RH
4807 if (sections == NULL)
4808 return false;
4809
4810 /* Step One: Scan for segment vs section LMA conflicts.
4811 Also add the sections to the section array allocated above.
4812 Also add the sections to the current segment. In the common
4813 case, where the sections have not been moved, this means that
4814 we have completely filled the segment, and there is nothing
4815 more to do. */
252b5132 4816 isec = 0;
72730e0c 4817 matching_lma = 0;
252b5132
RH
4818 suggested_lma = 0;
4819
bc67d8a6
NC
4820 for (j = 0, section = ibfd->sections;
4821 section != NULL;
4822 section = section->next)
252b5132 4823 {
caf47ea6 4824 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 4825 {
bc67d8a6
NC
4826 output_section = section->output_section;
4827
4828 sections[j ++] = section;
252b5132
RH
4829
4830 /* The Solaris native linker always sets p_paddr to 0.
4831 We try to catch that case here, and set it to the
5e8d7549
NC
4832 correct value. Note - some backends require that
4833 p_paddr be left as zero. */
bc67d8a6 4834 if (segment->p_paddr == 0
4455705d 4835 && segment->p_vaddr != 0
5e8d7549 4836 && (! bed->want_p_paddr_set_to_zero)
252b5132 4837 && isec == 0
bc67d8a6
NC
4838 && output_section->lma != 0
4839 && (output_section->vma == (segment->p_vaddr
4840 + (map->includes_filehdr
4841 ? iehdr->e_ehsize
4842 : 0)
4843 + (map->includes_phdrs
079e9a2f
AM
4844 ? (iehdr->e_phnum
4845 * iehdr->e_phentsize)
bc67d8a6
NC
4846 : 0))))
4847 map->p_paddr = segment->p_vaddr;
252b5132
RH
4848
4849 /* Match up the physical address of the segment with the
4850 LMA address of the output section. */
bc67d8a6 4851 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
caf47ea6 4852 || IS_CONTAINED_BY_FILEPOS (section, segment, bed)
5e8d7549
NC
4853 || IS_COREFILE_NOTE (segment, section)
4854 || (bed->want_p_paddr_set_to_zero &&
4855 IS_CONTAINED_BY_VMA (output_section, segment))
4856 )
252b5132
RH
4857 {
4858 if (matching_lma == 0)
bc67d8a6 4859 matching_lma = output_section->lma;
252b5132
RH
4860
4861 /* We assume that if the section fits within the segment
bc67d8a6 4862 then it does not overlap any other section within that
252b5132 4863 segment. */
bc67d8a6 4864 map->sections[isec ++] = output_section;
252b5132
RH
4865 }
4866 else if (suggested_lma == 0)
bc67d8a6 4867 suggested_lma = output_section->lma;
252b5132
RH
4868 }
4869 }
4870
bc67d8a6 4871 BFD_ASSERT (j == section_count);
252b5132
RH
4872
4873 /* Step Two: Adjust the physical address of the current segment,
4874 if necessary. */
bc67d8a6 4875 if (isec == section_count)
252b5132
RH
4876 {
4877 /* All of the sections fitted within the segment as currently
4878 specified. This is the default case. Add the segment to
4879 the list of built segments and carry on to process the next
4880 program header in the input BFD. */
bc67d8a6 4881 map->count = section_count;
c044fabd
KH
4882 *pointer_to_map = map;
4883 pointer_to_map = &map->next;
252b5132
RH
4884
4885 free (sections);
4886 continue;
4887 }
252b5132
RH
4888 else
4889 {
72730e0c
AM
4890 if (matching_lma != 0)
4891 {
4892 /* At least one section fits inside the current segment.
4893 Keep it, but modify its physical address to match the
4894 LMA of the first section that fitted. */
bc67d8a6 4895 map->p_paddr = matching_lma;
72730e0c
AM
4896 }
4897 else
4898 {
4899 /* None of the sections fitted inside the current segment.
4900 Change the current segment's physical address to match
4901 the LMA of the first section. */
bc67d8a6 4902 map->p_paddr = suggested_lma;
72730e0c
AM
4903 }
4904
bc67d8a6
NC
4905 /* Offset the segment physical address from the lma
4906 to allow for space taken up by elf headers. */
4907 if (map->includes_filehdr)
4908 map->p_paddr -= iehdr->e_ehsize;
252b5132 4909
bc67d8a6
NC
4910 if (map->includes_phdrs)
4911 {
4912 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4913
4914 /* iehdr->e_phnum is just an estimate of the number
4915 of program headers that we will need. Make a note
4916 here of the number we used and the segment we chose
4917 to hold these headers, so that we can adjust the
4918 offset when we know the correct value. */
4919 phdr_adjust_num = iehdr->e_phnum;
4920 phdr_adjust_seg = map;
4921 }
252b5132
RH
4922 }
4923
4924 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 4925 those that fit to the current segment and removing them from the
252b5132
RH
4926 sections array; but making sure not to leave large gaps. Once all
4927 possible sections have been assigned to the current segment it is
4928 added to the list of built segments and if sections still remain
4929 to be assigned, a new segment is constructed before repeating
4930 the loop. */
4931 isec = 0;
4932 do
4933 {
bc67d8a6 4934 map->count = 0;
252b5132
RH
4935 suggested_lma = 0;
4936
4937 /* Fill the current segment with sections that fit. */
bc67d8a6 4938 for (j = 0; j < section_count; j++)
252b5132 4939 {
bc67d8a6 4940 section = sections[j];
252b5132 4941
bc67d8a6 4942 if (section == NULL)
252b5132
RH
4943 continue;
4944
bc67d8a6 4945 output_section = section->output_section;
252b5132 4946
bc67d8a6 4947 BFD_ASSERT (output_section != NULL);
c044fabd 4948
bc67d8a6
NC
4949 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4950 || IS_COREFILE_NOTE (segment, section))
252b5132 4951 {
bc67d8a6 4952 if (map->count == 0)
252b5132
RH
4953 {
4954 /* If the first section in a segment does not start at
bc67d8a6
NC
4955 the beginning of the segment, then something is
4956 wrong. */
4957 if (output_section->lma !=
4958 (map->p_paddr
4959 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4960 + (map->includes_phdrs
4961 ? iehdr->e_phnum * iehdr->e_phentsize
4962 : 0)))
252b5132
RH
4963 abort ();
4964 }
4965 else
4966 {
4967 asection * prev_sec;
252b5132 4968
bc67d8a6 4969 prev_sec = map->sections[map->count - 1];
252b5132
RH
4970
4971 /* If the gap between the end of the previous section
bc67d8a6
NC
4972 and the start of this section is more than
4973 maxpagesize then we need to start a new segment. */
079e9a2f
AM
4974 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4975 maxpagesize)
caf47ea6 4976 < BFD_ALIGN (output_section->lma, maxpagesize))
079e9a2f
AM
4977 || ((prev_sec->lma + prev_sec->_raw_size)
4978 > output_section->lma))
252b5132
RH
4979 {
4980 if (suggested_lma == 0)
bc67d8a6 4981 suggested_lma = output_section->lma;
252b5132
RH
4982
4983 continue;
4984 }
4985 }
4986
bc67d8a6 4987 map->sections[map->count++] = output_section;
252b5132
RH
4988 ++isec;
4989 sections[j] = NULL;
bc67d8a6 4990 section->segment_mark = true;
252b5132
RH
4991 }
4992 else if (suggested_lma == 0)
bc67d8a6 4993 suggested_lma = output_section->lma;
252b5132
RH
4994 }
4995
bc67d8a6 4996 BFD_ASSERT (map->count > 0);
252b5132
RH
4997
4998 /* Add the current segment to the list of built segments. */
c044fabd
KH
4999 *pointer_to_map = map;
5000 pointer_to_map = &map->next;
252b5132 5001
bc67d8a6 5002 if (isec < section_count)
252b5132
RH
5003 {
5004 /* We still have not allocated all of the sections to
5005 segments. Create a new segment here, initialise it
5006 and carry on looping. */
dc810e39
AM
5007 amt = sizeof (struct elf_segment_map);
5008 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5009 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 5010 if (map == NULL)
252b5132
RH
5011 return false;
5012
5013 /* Initialise the fields of the segment map. Set the physical
5014 physical address to the LMA of the first section that has
5015 not yet been assigned. */
bc67d8a6
NC
5016 map->next = NULL;
5017 map->p_type = segment->p_type;
5018 map->p_flags = segment->p_flags;
5019 map->p_flags_valid = 1;
5020 map->p_paddr = suggested_lma;
5021 map->p_paddr_valid = 1;
5022 map->includes_filehdr = 0;
5023 map->includes_phdrs = 0;
252b5132
RH
5024 }
5025 }
bc67d8a6 5026 while (isec < section_count);
252b5132
RH
5027
5028 free (sections);
5029 }
5030
5031 /* The Solaris linker creates program headers in which all the
5032 p_paddr fields are zero. When we try to objcopy or strip such a
5033 file, we get confused. Check for this case, and if we find it
5034 reset the p_paddr_valid fields. */
bc67d8a6
NC
5035 for (map = map_first; map != NULL; map = map->next)
5036 if (map->p_paddr != 0)
252b5132 5037 break;
bc67d8a6 5038 if (map == NULL)
252b5132 5039 {
bc67d8a6
NC
5040 for (map = map_first; map != NULL; map = map->next)
5041 map->p_paddr_valid = 0;
252b5132
RH
5042 }
5043
bc67d8a6
NC
5044 elf_tdata (obfd)->segment_map = map_first;
5045
5046 /* If we had to estimate the number of program headers that were
9ad5cbcf 5047 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
5048 the offset if necessary. */
5049 if (phdr_adjust_seg != NULL)
5050 {
5051 unsigned int count;
c044fabd 5052
bc67d8a6 5053 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 5054 count++;
252b5132 5055
bc67d8a6
NC
5056 if (count > phdr_adjust_num)
5057 phdr_adjust_seg->p_paddr
5058 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5059 }
c044fabd 5060
252b5132 5061#if 0
c044fabd
KH
5062 /* Final Step: Sort the segments into ascending order of physical
5063 address. */
bc67d8a6 5064 if (map_first != NULL)
252b5132 5065 {
c044fabd 5066 struct elf_segment_map *prev;
252b5132 5067
bc67d8a6
NC
5068 prev = map_first;
5069 for (map = map_first->next; map != NULL; prev = map, map = map->next)
252b5132 5070 {
bc67d8a6
NC
5071 /* Yes I know - its a bubble sort.... */
5072 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
252b5132 5073 {
bc67d8a6
NC
5074 /* Swap map and map->next. */
5075 prev->next = map->next;
5076 map->next = map->next->next;
5077 prev->next->next = map;
252b5132 5078
bc67d8a6
NC
5079 /* Restart loop. */
5080 map = map_first;
252b5132
RH
5081 }
5082 }
5083 }
5084#endif
5085
bc67d8a6
NC
5086#undef SEGMENT_END
5087#undef IS_CONTAINED_BY_VMA
5088#undef IS_CONTAINED_BY_LMA
caf47ea6 5089#undef IS_CONTAINED_BY_FILEPOS
252b5132 5090#undef IS_COREFILE_NOTE
bc67d8a6
NC
5091#undef IS_SOLARIS_PT_INTERP
5092#undef INCLUDE_SECTION_IN_SEGMENT
5093#undef SEGMENT_AFTER_SEGMENT
5094#undef SEGMENT_OVERLAPS
252b5132
RH
5095 return true;
5096}
5097
5098/* Copy private section information. This copies over the entsize
5099 field, and sometimes the info field. */
5100
5101boolean
5102_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
5103 bfd *ibfd;
5104 asection *isec;
5105 bfd *obfd;
5106 asection *osec;
5107{
5108 Elf_Internal_Shdr *ihdr, *ohdr;
caf47ea6 5109 const struct elf_backend_data *bed = get_elf_backend_data (ibfd);
252b5132
RH
5110
5111 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5112 || obfd->xvec->flavour != bfd_target_elf_flavour)
5113 return true;
5114
5115 /* Copy over private BFD data if it has not already been copied.
5116 This must be done here, rather than in the copy_private_bfd_data
5117 entry point, because the latter is called after the section
5118 contents have been set, which means that the program headers have
47d9a591
AM
5119 already been worked out. The backend function provides a way to
5120 override the test conditions and code path for the call to
caf47ea6
AM
5121 copy_private_bfd_data. */
5122 if (bed->copy_private_bfd_data_p)
252b5132 5123 {
caf47ea6
AM
5124 if ((*bed->copy_private_bfd_data_p) (ibfd, isec, obfd, osec))
5125 if (! copy_private_bfd_data (ibfd, obfd))
5126 return false;
47d9a591 5127 }
caf47ea6
AM
5128 else if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5129 {
5130 asection *s;
5131
5132 /* Only set up the segments if there are no more SEC_ALLOC
5133 sections. FIXME: This won't do the right thing if objcopy is
5134 used to remove the last SEC_ALLOC section, since objcopy
5135 won't call this routine in that case. */
5136 for (s = isec->next; s != NULL; s = s->next)
5137 if ((s->flags & SEC_ALLOC) != 0)
5138 break;
5139 if (s == NULL)
5140 {
5141 if (! copy_private_bfd_data (ibfd, obfd))
5142 return false;
5143 }
252b5132
RH
5144 }
5145
5146 ihdr = &elf_section_data (isec)->this_hdr;
5147 ohdr = &elf_section_data (osec)->this_hdr;
5148
5149 ohdr->sh_entsize = ihdr->sh_entsize;
5150
5151 if (ihdr->sh_type == SHT_SYMTAB
5152 || ihdr->sh_type == SHT_DYNSYM
5153 || ihdr->sh_type == SHT_GNU_verneed
5154 || ihdr->sh_type == SHT_GNU_verdef)
5155 ohdr->sh_info = ihdr->sh_info;
5156
9dce4196
AM
5157 /* Set things up for objcopy. The output SHT_GROUP section will
5158 have its elf_next_in_group pointing back to the input group
5159 members. */
5160 elf_next_in_group (osec) = elf_next_in_group (isec);
5161 elf_group_name (osec) = elf_group_name (isec);
5162
bf572ba0
MM
5163 elf_section_data (osec)->use_rela_p
5164 = elf_section_data (isec)->use_rela_p;
5165
252b5132
RH
5166 return true;
5167}
5168
5169/* Copy private symbol information. If this symbol is in a section
5170 which we did not map into a BFD section, try to map the section
5171 index correctly. We use special macro definitions for the mapped
5172 section indices; these definitions are interpreted by the
5173 swap_out_syms function. */
5174
9ad5cbcf
AM
5175#define MAP_ONESYMTAB (SHN_HIOS + 1)
5176#define MAP_DYNSYMTAB (SHN_HIOS + 2)
5177#define MAP_STRTAB (SHN_HIOS + 3)
5178#define MAP_SHSTRTAB (SHN_HIOS + 4)
5179#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132
RH
5180
5181boolean
5182_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5183 bfd *ibfd;
5184 asymbol *isymarg;
5185 bfd *obfd;
5186 asymbol *osymarg;
5187{
5188 elf_symbol_type *isym, *osym;
5189
5190 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5191 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5192 return true;
5193
5194 isym = elf_symbol_from (ibfd, isymarg);
5195 osym = elf_symbol_from (obfd, osymarg);
5196
5197 if (isym != NULL
5198 && osym != NULL
5199 && bfd_is_abs_section (isym->symbol.section))
5200 {
5201 unsigned int shndx;
5202
5203 shndx = isym->internal_elf_sym.st_shndx;
5204 if (shndx == elf_onesymtab (ibfd))
5205 shndx = MAP_ONESYMTAB;
5206 else if (shndx == elf_dynsymtab (ibfd))
5207 shndx = MAP_DYNSYMTAB;
5208 else if (shndx == elf_tdata (ibfd)->strtab_section)
5209 shndx = MAP_STRTAB;
5210 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5211 shndx = MAP_SHSTRTAB;
9ad5cbcf
AM
5212 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5213 shndx = MAP_SYM_SHNDX;
252b5132
RH
5214 osym->internal_elf_sym.st_shndx = shndx;
5215 }
5216
5217 return true;
5218}
5219
5220/* Swap out the symbols. */
5221
5222static boolean
5223swap_out_syms (abfd, sttp, relocatable_p)
5224 bfd *abfd;
5225 struct bfd_strtab_hash **sttp;
5226 int relocatable_p;
5227{
079e9a2f
AM
5228 struct elf_backend_data *bed;
5229 int symcount;
5230 asymbol **syms;
5231 struct bfd_strtab_hash *stt;
5232 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 5233 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f
AM
5234 Elf_Internal_Shdr *symstrtab_hdr;
5235 char *outbound_syms;
9ad5cbcf 5236 char *outbound_shndx;
079e9a2f
AM
5237 int idx;
5238 bfd_size_type amt;
252b5132
RH
5239
5240 if (!elf_map_symbols (abfd))
5241 return false;
5242
c044fabd 5243 /* Dump out the symtabs. */
079e9a2f
AM
5244 stt = _bfd_elf_stringtab_init ();
5245 if (stt == NULL)
5246 return false;
252b5132 5247
079e9a2f
AM
5248 bed = get_elf_backend_data (abfd);
5249 symcount = bfd_get_symcount (abfd);
5250 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5251 symtab_hdr->sh_type = SHT_SYMTAB;
5252 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5253 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5254 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5255 symtab_hdr->sh_addralign = bed->s->file_align;
5256
5257 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5258 symstrtab_hdr->sh_type = SHT_STRTAB;
5259
5260 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5261 outbound_syms = bfd_alloc (abfd, amt);
5262 if (outbound_syms == NULL)
5263 return false;
5264 symtab_hdr->contents = (PTR) outbound_syms;
252b5132 5265
9ad5cbcf
AM
5266 outbound_shndx = NULL;
5267 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5268 if (symtab_shndx_hdr->sh_name != 0)
5269 {
5270 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
1126897b 5271 outbound_shndx = bfd_zalloc (abfd, amt);
9ad5cbcf
AM
5272 if (outbound_shndx == NULL)
5273 return false;
9ad5cbcf
AM
5274 symtab_shndx_hdr->contents = outbound_shndx;
5275 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5276 symtab_shndx_hdr->sh_size = amt;
5277 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5278 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5279 }
5280
079e9a2f
AM
5281 /* now generate the data (for "contents") */
5282 {
5283 /* Fill in zeroth symbol and swap it out. */
5284 Elf_Internal_Sym sym;
5285 sym.st_name = 0;
5286 sym.st_value = 0;
5287 sym.st_size = 0;
5288 sym.st_info = 0;
5289 sym.st_other = 0;
5290 sym.st_shndx = SHN_UNDEF;
9ad5cbcf 5291 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5292 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5293 if (outbound_shndx != NULL)
5294 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5295 }
252b5132 5296
079e9a2f
AM
5297 syms = bfd_get_outsymbols (abfd);
5298 for (idx = 0; idx < symcount; idx++)
252b5132 5299 {
252b5132 5300 Elf_Internal_Sym sym;
079e9a2f
AM
5301 bfd_vma value = syms[idx]->value;
5302 elf_symbol_type *type_ptr;
5303 flagword flags = syms[idx]->flags;
5304 int type;
252b5132 5305
079e9a2f
AM
5306 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5307 {
5308 /* Local section symbols have no name. */
5309 sym.st_name = 0;
5310 }
5311 else
5312 {
5313 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5314 syms[idx]->name,
5315 true, false);
5316 if (sym.st_name == (unsigned long) -1)
5317 return false;
5318 }
252b5132 5319
079e9a2f 5320 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 5321
079e9a2f
AM
5322 if ((flags & BSF_SECTION_SYM) == 0
5323 && bfd_is_com_section (syms[idx]->section))
5324 {
5325 /* ELF common symbols put the alignment into the `value' field,
5326 and the size into the `size' field. This is backwards from
5327 how BFD handles it, so reverse it here. */
5328 sym.st_size = value;
5329 if (type_ptr == NULL
5330 || type_ptr->internal_elf_sym.st_value == 0)
5331 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5332 else
5333 sym.st_value = type_ptr->internal_elf_sym.st_value;
5334 sym.st_shndx = _bfd_elf_section_from_bfd_section
5335 (abfd, syms[idx]->section);
5336 }
5337 else
5338 {
5339 asection *sec = syms[idx]->section;
5340 int shndx;
252b5132 5341
079e9a2f
AM
5342 if (sec->output_section)
5343 {
5344 value += sec->output_offset;
5345 sec = sec->output_section;
5346 }
5347 /* Don't add in the section vma for relocatable output. */
5348 if (! relocatable_p)
5349 value += sec->vma;
5350 sym.st_value = value;
5351 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5352
5353 if (bfd_is_abs_section (sec)
5354 && type_ptr != NULL
5355 && type_ptr->internal_elf_sym.st_shndx != 0)
5356 {
5357 /* This symbol is in a real ELF section which we did
5358 not create as a BFD section. Undo the mapping done
5359 by copy_private_symbol_data. */
5360 shndx = type_ptr->internal_elf_sym.st_shndx;
5361 switch (shndx)
5362 {
5363 case MAP_ONESYMTAB:
5364 shndx = elf_onesymtab (abfd);
5365 break;
5366 case MAP_DYNSYMTAB:
5367 shndx = elf_dynsymtab (abfd);
5368 break;
5369 case MAP_STRTAB:
5370 shndx = elf_tdata (abfd)->strtab_section;
5371 break;
5372 case MAP_SHSTRTAB:
5373 shndx = elf_tdata (abfd)->shstrtab_section;
5374 break;
9ad5cbcf
AM
5375 case MAP_SYM_SHNDX:
5376 shndx = elf_tdata (abfd)->symtab_shndx_section;
5377 break;
079e9a2f
AM
5378 default:
5379 break;
5380 }
5381 }
5382 else
5383 {
5384 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 5385
079e9a2f
AM
5386 if (shndx == -1)
5387 {
5388 asection *sec2;
5389
5390 /* Writing this would be a hell of a lot easier if
5391 we had some decent documentation on bfd, and
5392 knew what to expect of the library, and what to
5393 demand of applications. For example, it
5394 appears that `objcopy' might not set the
5395 section of a symbol to be a section that is
5396 actually in the output file. */
5397 sec2 = bfd_get_section_by_name (abfd, sec->name);
5398 BFD_ASSERT (sec2 != 0);
5399 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5400 BFD_ASSERT (shndx != -1);
5401 }
5402 }
252b5132 5403
079e9a2f
AM
5404 sym.st_shndx = shndx;
5405 }
252b5132 5406
13ae64f3
JJ
5407 if ((flags & BSF_THREAD_LOCAL) != 0)
5408 type = STT_TLS;
5409 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
5410 type = STT_FUNC;
5411 else if ((flags & BSF_OBJECT) != 0)
5412 type = STT_OBJECT;
5413 else
5414 type = STT_NOTYPE;
252b5132 5415
13ae64f3
JJ
5416 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5417 type = STT_TLS;
5418
079e9a2f
AM
5419 /* Processor-specific types */
5420 if (type_ptr != NULL
5421 && bed->elf_backend_get_symbol_type)
5422 type = ((*bed->elf_backend_get_symbol_type)
5423 (&type_ptr->internal_elf_sym, type));
252b5132 5424
079e9a2f
AM
5425 if (flags & BSF_SECTION_SYM)
5426 {
5427 if (flags & BSF_GLOBAL)
5428 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5429 else
5430 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5431 }
5432 else if (bfd_is_com_section (syms[idx]->section))
5433 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5434 else if (bfd_is_und_section (syms[idx]->section))
5435 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5436 ? STB_WEAK
5437 : STB_GLOBAL),
5438 type);
5439 else if (flags & BSF_FILE)
5440 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5441 else
5442 {
5443 int bind = STB_LOCAL;
252b5132 5444
079e9a2f
AM
5445 if (flags & BSF_LOCAL)
5446 bind = STB_LOCAL;
5447 else if (flags & BSF_WEAK)
5448 bind = STB_WEAK;
5449 else if (flags & BSF_GLOBAL)
5450 bind = STB_GLOBAL;
252b5132 5451
079e9a2f
AM
5452 sym.st_info = ELF_ST_INFO (bind, type);
5453 }
252b5132 5454
079e9a2f
AM
5455 if (type_ptr != NULL)
5456 sym.st_other = type_ptr->internal_elf_sym.st_other;
5457 else
5458 sym.st_other = 0;
252b5132 5459
9ad5cbcf 5460 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5461 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5462 if (outbound_shndx != NULL)
5463 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5464 }
252b5132 5465
079e9a2f
AM
5466 *sttp = stt;
5467 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5468 symstrtab_hdr->sh_type = SHT_STRTAB;
252b5132 5469
079e9a2f
AM
5470 symstrtab_hdr->sh_flags = 0;
5471 symstrtab_hdr->sh_addr = 0;
5472 symstrtab_hdr->sh_entsize = 0;
5473 symstrtab_hdr->sh_link = 0;
5474 symstrtab_hdr->sh_info = 0;
5475 symstrtab_hdr->sh_addralign = 1;
252b5132
RH
5476
5477 return true;
5478}
5479
5480/* Return the number of bytes required to hold the symtab vector.
5481
5482 Note that we base it on the count plus 1, since we will null terminate
5483 the vector allocated based on this size. However, the ELF symbol table
5484 always has a dummy entry as symbol #0, so it ends up even. */
5485
5486long
5487_bfd_elf_get_symtab_upper_bound (abfd)
5488 bfd *abfd;
5489{
5490 long symcount;
5491 long symtab_size;
5492 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5493
5494 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5495 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5496 if (symcount > 0)
5497 symtab_size -= sizeof (asymbol *);
252b5132
RH
5498
5499 return symtab_size;
5500}
5501
5502long
5503_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5504 bfd *abfd;
5505{
5506 long symcount;
5507 long symtab_size;
5508 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5509
5510 if (elf_dynsymtab (abfd) == 0)
5511 {
5512 bfd_set_error (bfd_error_invalid_operation);
5513 return -1;
5514 }
5515
5516 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5517 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5518 if (symcount > 0)
5519 symtab_size -= sizeof (asymbol *);
252b5132
RH
5520
5521 return symtab_size;
5522}
5523
5524long
5525_bfd_elf_get_reloc_upper_bound (abfd, asect)
7442e600 5526 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5527 sec_ptr asect;
5528{
5529 return (asect->reloc_count + 1) * sizeof (arelent *);
5530}
5531
5532/* Canonicalize the relocs. */
5533
5534long
5535_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5536 bfd *abfd;
5537 sec_ptr section;
5538 arelent **relptr;
5539 asymbol **symbols;
5540{
5541 arelent *tblptr;
5542 unsigned int i;
dbb410c3 5543 struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5544
dbb410c3 5545 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
5546 return -1;
5547
5548 tblptr = section->relocation;
5549 for (i = 0; i < section->reloc_count; i++)
5550 *relptr++ = tblptr++;
5551
5552 *relptr = NULL;
5553
5554 return section->reloc_count;
5555}
5556
5557long
5558_bfd_elf_get_symtab (abfd, alocation)
5559 bfd *abfd;
5560 asymbol **alocation;
5561{
dbb410c3
AM
5562 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5563 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
252b5132
RH
5564
5565 if (symcount >= 0)
5566 bfd_get_symcount (abfd) = symcount;
5567 return symcount;
5568}
5569
5570long
5571_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5572 bfd *abfd;
5573 asymbol **alocation;
5574{
dbb410c3 5575 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1f70368c
DJ
5576 long symcount = bed->s->slurp_symbol_table (abfd, alocation, true);
5577
5578 if (symcount >= 0)
5579 bfd_get_dynamic_symcount (abfd) = symcount;
5580 return symcount;
252b5132
RH
5581}
5582
5583/* Return the size required for the dynamic reloc entries. Any
5584 section that was actually installed in the BFD, and has type
5585 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5586 considered to be a dynamic reloc section. */
5587
5588long
5589_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5590 bfd *abfd;
5591{
5592 long ret;
5593 asection *s;
5594
5595 if (elf_dynsymtab (abfd) == 0)
5596 {
5597 bfd_set_error (bfd_error_invalid_operation);
5598 return -1;
5599 }
5600
5601 ret = sizeof (arelent *);
5602 for (s = abfd->sections; s != NULL; s = s->next)
5603 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5604 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5605 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5606 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5607 * sizeof (arelent *));
5608
5609 return ret;
5610}
5611
5612/* Canonicalize the dynamic relocation entries. Note that we return
5613 the dynamic relocations as a single block, although they are
5614 actually associated with particular sections; the interface, which
5615 was designed for SunOS style shared libraries, expects that there
5616 is only one set of dynamic relocs. Any section that was actually
5617 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5618 the dynamic symbol table, is considered to be a dynamic reloc
5619 section. */
5620
5621long
5622_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5623 bfd *abfd;
5624 arelent **storage;
5625 asymbol **syms;
5626{
5627 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5628 asection *s;
5629 long ret;
5630
5631 if (elf_dynsymtab (abfd) == 0)
5632 {
5633 bfd_set_error (bfd_error_invalid_operation);
5634 return -1;
5635 }
5636
5637 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5638 ret = 0;
5639 for (s = abfd->sections; s != NULL; s = s->next)
5640 {
5641 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5642 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5643 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5644 {
5645 arelent *p;
5646 long count, i;
5647
5648 if (! (*slurp_relocs) (abfd, s, syms, true))
5649 return -1;
5650 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5651 p = s->relocation;
5652 for (i = 0; i < count; i++)
5653 *storage++ = p++;
5654 ret += count;
5655 }
5656 }
5657
5658 *storage = NULL;
5659
5660 return ret;
5661}
5662\f
5663/* Read in the version information. */
5664
5665boolean
5666_bfd_elf_slurp_version_tables (abfd)
5667 bfd *abfd;
5668{
5669 bfd_byte *contents = NULL;
dc810e39 5670 bfd_size_type amt;
252b5132
RH
5671
5672 if (elf_dynverdef (abfd) != 0)
5673 {
5674 Elf_Internal_Shdr *hdr;
5675 Elf_External_Verdef *everdef;
5676 Elf_Internal_Verdef *iverdef;
f631889e
UD
5677 Elf_Internal_Verdef *iverdefarr;
5678 Elf_Internal_Verdef iverdefmem;
252b5132 5679 unsigned int i;
062e2358 5680 unsigned int maxidx;
252b5132
RH
5681
5682 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5683
252b5132
RH
5684 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5685 if (contents == NULL)
5686 goto error_return;
5687 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5688 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5689 goto error_return;
5690
f631889e
UD
5691 /* We know the number of entries in the section but not the maximum
5692 index. Therefore we have to run through all entries and find
5693 the maximum. */
252b5132 5694 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
5695 maxidx = 0;
5696 for (i = 0; i < hdr->sh_info; ++i)
5697 {
5698 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5699
062e2358
AM
5700 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5701 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e
UD
5702
5703 everdef = ((Elf_External_Verdef *)
5704 ((bfd_byte *) everdef + iverdefmem.vd_next));
5705 }
5706
dc810e39
AM
5707 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5708 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e
UD
5709 if (elf_tdata (abfd)->verdef == NULL)
5710 goto error_return;
5711
5712 elf_tdata (abfd)->cverdefs = maxidx;
5713
5714 everdef = (Elf_External_Verdef *) contents;
5715 iverdefarr = elf_tdata (abfd)->verdef;
5716 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
5717 {
5718 Elf_External_Verdaux *everdaux;
5719 Elf_Internal_Verdaux *iverdaux;
5720 unsigned int j;
5721
f631889e
UD
5722 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5723
5724 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5725 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
252b5132
RH
5726
5727 iverdef->vd_bfd = abfd;
5728
dc810e39
AM
5729 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5730 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
252b5132
RH
5731 if (iverdef->vd_auxptr == NULL)
5732 goto error_return;
5733
5734 everdaux = ((Elf_External_Verdaux *)
5735 ((bfd_byte *) everdef + iverdef->vd_aux));
5736 iverdaux = iverdef->vd_auxptr;
5737 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5738 {
5739 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5740
5741 iverdaux->vda_nodename =
5742 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5743 iverdaux->vda_name);
5744 if (iverdaux->vda_nodename == NULL)
5745 goto error_return;
5746
5747 if (j + 1 < iverdef->vd_cnt)
5748 iverdaux->vda_nextptr = iverdaux + 1;
5749 else
5750 iverdaux->vda_nextptr = NULL;
5751
5752 everdaux = ((Elf_External_Verdaux *)
5753 ((bfd_byte *) everdaux + iverdaux->vda_next));
5754 }
5755
5756 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5757
5758 if (i + 1 < hdr->sh_info)
5759 iverdef->vd_nextdef = iverdef + 1;
5760 else
5761 iverdef->vd_nextdef = NULL;
5762
5763 everdef = ((Elf_External_Verdef *)
5764 ((bfd_byte *) everdef + iverdef->vd_next));
5765 }
5766
5767 free (contents);
5768 contents = NULL;
5769 }
5770
5771 if (elf_dynverref (abfd) != 0)
5772 {
5773 Elf_Internal_Shdr *hdr;
5774 Elf_External_Verneed *everneed;
5775 Elf_Internal_Verneed *iverneed;
5776 unsigned int i;
5777
5778 hdr = &elf_tdata (abfd)->dynverref_hdr;
5779
dc810e39 5780 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
252b5132 5781 elf_tdata (abfd)->verref =
dc810e39 5782 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
252b5132
RH
5783 if (elf_tdata (abfd)->verref == NULL)
5784 goto error_return;
5785
5786 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5787
5788 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5789 if (contents == NULL)
5790 goto error_return;
5791 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5792 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5793 goto error_return;
5794
5795 everneed = (Elf_External_Verneed *) contents;
5796 iverneed = elf_tdata (abfd)->verref;
5797 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5798 {
5799 Elf_External_Vernaux *evernaux;
5800 Elf_Internal_Vernaux *ivernaux;
5801 unsigned int j;
5802
5803 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5804
5805 iverneed->vn_bfd = abfd;
5806
5807 iverneed->vn_filename =
5808 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5809 iverneed->vn_file);
5810 if (iverneed->vn_filename == NULL)
5811 goto error_return;
5812
dc810e39
AM
5813 amt = iverneed->vn_cnt;
5814 amt *= sizeof (Elf_Internal_Vernaux);
5815 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
252b5132
RH
5816
5817 evernaux = ((Elf_External_Vernaux *)
5818 ((bfd_byte *) everneed + iverneed->vn_aux));
5819 ivernaux = iverneed->vn_auxptr;
5820 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5821 {
5822 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5823
5824 ivernaux->vna_nodename =
5825 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5826 ivernaux->vna_name);
5827 if (ivernaux->vna_nodename == NULL)
5828 goto error_return;
5829
5830 if (j + 1 < iverneed->vn_cnt)
5831 ivernaux->vna_nextptr = ivernaux + 1;
5832 else
5833 ivernaux->vna_nextptr = NULL;
5834
5835 evernaux = ((Elf_External_Vernaux *)
5836 ((bfd_byte *) evernaux + ivernaux->vna_next));
5837 }
5838
5839 if (i + 1 < hdr->sh_info)
5840 iverneed->vn_nextref = iverneed + 1;
5841 else
5842 iverneed->vn_nextref = NULL;
5843
5844 everneed = ((Elf_External_Verneed *)
5845 ((bfd_byte *) everneed + iverneed->vn_next));
5846 }
5847
5848 free (contents);
5849 contents = NULL;
5850 }
5851
5852 return true;
5853
5854 error_return:
5855 if (contents == NULL)
5856 free (contents);
5857 return false;
5858}
5859\f
5860asymbol *
5861_bfd_elf_make_empty_symbol (abfd)
5862 bfd *abfd;
5863{
5864 elf_symbol_type *newsym;
dc810e39 5865 bfd_size_type amt = sizeof (elf_symbol_type);
252b5132 5866
dc810e39 5867 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
252b5132
RH
5868 if (!newsym)
5869 return NULL;
5870 else
5871 {
5872 newsym->symbol.the_bfd = abfd;
5873 return &newsym->symbol;
5874 }
5875}
5876
5877void
5878_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
7442e600 5879 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
5880 asymbol *symbol;
5881 symbol_info *ret;
5882{
5883 bfd_symbol_info (symbol, ret);
5884}
5885
5886/* Return whether a symbol name implies a local symbol. Most targets
5887 use this function for the is_local_label_name entry point, but some
5888 override it. */
5889
5890boolean
5891_bfd_elf_is_local_label_name (abfd, name)
7442e600 5892 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5893 const char *name;
5894{
5895 /* Normal local symbols start with ``.L''. */
5896 if (name[0] == '.' && name[1] == 'L')
5897 return true;
5898
5899 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5900 DWARF debugging symbols starting with ``..''. */
5901 if (name[0] == '.' && name[1] == '.')
5902 return true;
5903
5904 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5905 emitting DWARF debugging output. I suspect this is actually a
5906 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5907 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5908 underscore to be emitted on some ELF targets). For ease of use,
5909 we treat such symbols as local. */
5910 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5911 return true;
5912
5913 return false;
5914}
5915
5916alent *
5917_bfd_elf_get_lineno (ignore_abfd, symbol)
7442e600
ILT
5918 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5919 asymbol *symbol ATTRIBUTE_UNUSED;
252b5132
RH
5920{
5921 abort ();
5922 return NULL;
5923}
5924
5925boolean
5926_bfd_elf_set_arch_mach (abfd, arch, machine)
5927 bfd *abfd;
5928 enum bfd_architecture arch;
5929 unsigned long machine;
5930{
5931 /* If this isn't the right architecture for this backend, and this
5932 isn't the generic backend, fail. */
5933 if (arch != get_elf_backend_data (abfd)->arch
5934 && arch != bfd_arch_unknown
5935 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5936 return false;
5937
5938 return bfd_default_set_arch_mach (abfd, arch, machine);
5939}
5940
d1fad7c6
NC
5941/* Find the function to a particular section and offset,
5942 for error reporting. */
252b5132 5943
d1fad7c6
NC
5944static boolean
5945elf_find_function (abfd, section, symbols, offset,
4e8a9624 5946 filename_ptr, functionname_ptr)
d1fad7c6 5947 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5948 asection *section;
5949 asymbol **symbols;
5950 bfd_vma offset;
4e8a9624
AM
5951 const char **filename_ptr;
5952 const char **functionname_ptr;
252b5132 5953{
252b5132
RH
5954 const char *filename;
5955 asymbol *func;
5956 bfd_vma low_func;
5957 asymbol **p;
5958
252b5132
RH
5959 filename = NULL;
5960 func = NULL;
5961 low_func = 0;
5962
5963 for (p = symbols; *p != NULL; p++)
5964 {
5965 elf_symbol_type *q;
5966
5967 q = (elf_symbol_type *) *p;
5968
5969 if (bfd_get_section (&q->symbol) != section)
5970 continue;
5971
5972 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5973 {
5974 default:
5975 break;
5976 case STT_FILE:
5977 filename = bfd_asymbol_name (&q->symbol);
5978 break;
5979 case STT_NOTYPE:
5980 case STT_FUNC:
5981 if (q->symbol.section == section
5982 && q->symbol.value >= low_func
5983 && q->symbol.value <= offset)
5984 {
5985 func = (asymbol *) q;
5986 low_func = q->symbol.value;
5987 }
5988 break;
5989 }
5990 }
5991
5992 if (func == NULL)
5993 return false;
5994
d1fad7c6
NC
5995 if (filename_ptr)
5996 *filename_ptr = filename;
5997 if (functionname_ptr)
5998 *functionname_ptr = bfd_asymbol_name (func);
5999
6000 return true;
6001}
6002
6003/* Find the nearest line to a particular section and offset,
6004 for error reporting. */
6005
6006boolean
6007_bfd_elf_find_nearest_line (abfd, section, symbols, offset,
4e8a9624 6008 filename_ptr, functionname_ptr, line_ptr)
d1fad7c6
NC
6009 bfd *abfd;
6010 asection *section;
6011 asymbol **symbols;
6012 bfd_vma offset;
4e8a9624
AM
6013 const char **filename_ptr;
6014 const char **functionname_ptr;
d1fad7c6
NC
6015 unsigned int *line_ptr;
6016{
6017 boolean found;
6018
6019 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
6020 filename_ptr, functionname_ptr,
6021 line_ptr))
d1fad7c6
NC
6022 {
6023 if (!*functionname_ptr)
4e8a9624
AM
6024 elf_find_function (abfd, section, symbols, offset,
6025 *filename_ptr ? NULL : filename_ptr,
6026 functionname_ptr);
6027
d1fad7c6
NC
6028 return true;
6029 }
6030
6031 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
6032 filename_ptr, functionname_ptr,
6033 line_ptr, 0,
6034 &elf_tdata (abfd)->dwarf2_find_line_info))
d1fad7c6
NC
6035 {
6036 if (!*functionname_ptr)
4e8a9624
AM
6037 elf_find_function (abfd, section, symbols, offset,
6038 *filename_ptr ? NULL : filename_ptr,
6039 functionname_ptr);
6040
d1fad7c6
NC
6041 return true;
6042 }
6043
6044 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
6045 &found, filename_ptr,
6046 functionname_ptr, line_ptr,
6047 &elf_tdata (abfd)->line_info))
d1fad7c6 6048 return false;
dc43ada5 6049 if (found && (*functionname_ptr || *line_ptr))
d1fad7c6
NC
6050 return true;
6051
6052 if (symbols == NULL)
6053 return false;
6054
6055 if (! elf_find_function (abfd, section, symbols, offset,
4e8a9624 6056 filename_ptr, functionname_ptr))
d1fad7c6
NC
6057 return false;
6058
252b5132
RH
6059 *line_ptr = 0;
6060 return true;
6061}
6062
6063int
6064_bfd_elf_sizeof_headers (abfd, reloc)
6065 bfd *abfd;
6066 boolean reloc;
6067{
6068 int ret;
6069
6070 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6071 if (! reloc)
6072 ret += get_program_header_size (abfd);
6073 return ret;
6074}
6075
6076boolean
6077_bfd_elf_set_section_contents (abfd, section, location, offset, count)
6078 bfd *abfd;
6079 sec_ptr section;
6080 PTR location;
6081 file_ptr offset;
6082 bfd_size_type count;
6083{
6084 Elf_Internal_Shdr *hdr;
dc810e39 6085 bfd_signed_vma pos;
252b5132
RH
6086
6087 if (! abfd->output_has_begun
82e51918
AM
6088 && ! (_bfd_elf_compute_section_file_positions
6089 (abfd, (struct bfd_link_info *) NULL)))
252b5132
RH
6090 return false;
6091
6092 hdr = &elf_section_data (section)->this_hdr;
dc810e39
AM
6093 pos = hdr->sh_offset + offset;
6094 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6095 || bfd_bwrite (location, count, abfd) != count)
252b5132
RH
6096 return false;
6097
6098 return true;
6099}
6100
6101void
6102_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
7442e600
ILT
6103 bfd *abfd ATTRIBUTE_UNUSED;
6104 arelent *cache_ptr ATTRIBUTE_UNUSED;
6105 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
252b5132
RH
6106{
6107 abort ();
6108}
6109
6110#if 0
6111void
6112_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
6113 bfd *abfd;
6114 arelent *cache_ptr;
6115 Elf_Internal_Rel *dst;
6116{
6117 abort ();
6118}
6119#endif
6120
6121/* Try to convert a non-ELF reloc into an ELF one. */
6122
6123boolean
6124_bfd_elf_validate_reloc (abfd, areloc)
6125 bfd *abfd;
6126 arelent *areloc;
6127{
c044fabd 6128 /* Check whether we really have an ELF howto. */
252b5132
RH
6129
6130 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6131 {
6132 bfd_reloc_code_real_type code;
6133 reloc_howto_type *howto;
6134
6135 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 6136 equivalent ELF reloc. */
252b5132
RH
6137
6138 if (areloc->howto->pc_relative)
6139 {
6140 switch (areloc->howto->bitsize)
6141 {
6142 case 8:
6143 code = BFD_RELOC_8_PCREL;
6144 break;
6145 case 12:
6146 code = BFD_RELOC_12_PCREL;
6147 break;
6148 case 16:
6149 code = BFD_RELOC_16_PCREL;
6150 break;
6151 case 24:
6152 code = BFD_RELOC_24_PCREL;
6153 break;
6154 case 32:
6155 code = BFD_RELOC_32_PCREL;
6156 break;
6157 case 64:
6158 code = BFD_RELOC_64_PCREL;
6159 break;
6160 default:
6161 goto fail;
6162 }
6163
6164 howto = bfd_reloc_type_lookup (abfd, code);
6165
6166 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6167 {
6168 if (howto->pcrel_offset)
6169 areloc->addend += areloc->address;
6170 else
6171 areloc->addend -= areloc->address; /* addend is unsigned!! */
6172 }
6173 }
6174 else
6175 {
6176 switch (areloc->howto->bitsize)
6177 {
6178 case 8:
6179 code = BFD_RELOC_8;
6180 break;
6181 case 14:
6182 code = BFD_RELOC_14;
6183 break;
6184 case 16:
6185 code = BFD_RELOC_16;
6186 break;
6187 case 26:
6188 code = BFD_RELOC_26;
6189 break;
6190 case 32:
6191 code = BFD_RELOC_32;
6192 break;
6193 case 64:
6194 code = BFD_RELOC_64;
6195 break;
6196 default:
6197 goto fail;
6198 }
6199
6200 howto = bfd_reloc_type_lookup (abfd, code);
6201 }
6202
6203 if (howto)
6204 areloc->howto = howto;
6205 else
6206 goto fail;
6207 }
6208
6209 return true;
6210
6211 fail:
6212 (*_bfd_error_handler)
6213 (_("%s: unsupported relocation type %s"),
8f615d07 6214 bfd_archive_filename (abfd), areloc->howto->name);
252b5132
RH
6215 bfd_set_error (bfd_error_bad_value);
6216 return false;
6217}
6218
6219boolean
6220_bfd_elf_close_and_cleanup (abfd)
6221 bfd *abfd;
6222{
6223 if (bfd_get_format (abfd) == bfd_object)
6224 {
6225 if (elf_shstrtab (abfd) != NULL)
2b0f7ef9 6226 _bfd_elf_strtab_free (elf_shstrtab (abfd));
252b5132
RH
6227 }
6228
6229 return _bfd_generic_close_and_cleanup (abfd);
6230}
6231
6232/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6233 in the relocation's offset. Thus we cannot allow any sort of sanity
6234 range-checking to interfere. There is nothing else to do in processing
6235 this reloc. */
6236
6237bfd_reloc_status_type
6238_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
7442e600
ILT
6239 bfd *abfd ATTRIBUTE_UNUSED;
6240 arelent *re ATTRIBUTE_UNUSED;
6241 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6242 PTR data ATTRIBUTE_UNUSED;
6243 asection *is ATTRIBUTE_UNUSED;
6244 bfd *obfd ATTRIBUTE_UNUSED;
6245 char **errmsg ATTRIBUTE_UNUSED;
252b5132
RH
6246{
6247 return bfd_reloc_ok;
6248}
252b5132
RH
6249\f
6250/* Elf core file support. Much of this only works on native
6251 toolchains, since we rely on knowing the
6252 machine-dependent procfs structure in order to pick
c044fabd 6253 out details about the corefile. */
252b5132
RH
6254
6255#ifdef HAVE_SYS_PROCFS_H
6256# include <sys/procfs.h>
6257#endif
6258
c044fabd 6259/* FIXME: this is kinda wrong, but it's what gdb wants. */
252b5132
RH
6260
6261static int
6262elfcore_make_pid (abfd)
c044fabd 6263 bfd *abfd;
252b5132
RH
6264{
6265 return ((elf_tdata (abfd)->core_lwpid << 16)
6266 + (elf_tdata (abfd)->core_pid));
6267}
6268
252b5132
RH
6269/* If there isn't a section called NAME, make one, using
6270 data from SECT. Note, this function will generate a
6271 reference to NAME, so you shouldn't deallocate or
c044fabd 6272 overwrite it. */
252b5132
RH
6273
6274static boolean
6275elfcore_maybe_make_sect (abfd, name, sect)
c044fabd
KH
6276 bfd *abfd;
6277 char *name;
6278 asection *sect;
252b5132 6279{
c044fabd 6280 asection *sect2;
252b5132
RH
6281
6282 if (bfd_get_section_by_name (abfd, name) != NULL)
6283 return true;
6284
6285 sect2 = bfd_make_section (abfd, name);
6286 if (sect2 == NULL)
6287 return false;
6288
6289 sect2->_raw_size = sect->_raw_size;
6290 sect2->filepos = sect->filepos;
6291 sect2->flags = sect->flags;
6292 sect2->alignment_power = sect->alignment_power;
6293 return true;
6294}
6295
bb0082d6
AM
6296/* Create a pseudosection containing SIZE bytes at FILEPOS. This
6297 actually creates up to two pseudosections:
6298 - For the single-threaded case, a section named NAME, unless
6299 such a section already exists.
6300 - For the multi-threaded case, a section named "NAME/PID", where
6301 PID is elfcore_make_pid (abfd).
6302 Both pseudosections have identical contents. */
6303boolean
6304_bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6305 bfd *abfd;
6306 char *name;
dc810e39
AM
6307 size_t size;
6308 ufile_ptr filepos;
bb0082d6
AM
6309{
6310 char buf[100];
6311 char *threaded_name;
d4c88bbb 6312 size_t len;
bb0082d6
AM
6313 asection *sect;
6314
6315 /* Build the section name. */
6316
6317 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb
AM
6318 len = strlen (buf) + 1;
6319 threaded_name = bfd_alloc (abfd, (bfd_size_type) len);
bb0082d6
AM
6320 if (threaded_name == NULL)
6321 return false;
d4c88bbb 6322 memcpy (threaded_name, buf, len);
bb0082d6
AM
6323
6324 sect = bfd_make_section (abfd, threaded_name);
6325 if (sect == NULL)
6326 return false;
6327 sect->_raw_size = size;
6328 sect->filepos = filepos;
6329 sect->flags = SEC_HAS_CONTENTS;
6330 sect->alignment_power = 2;
6331
936e320b 6332 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
6333}
6334
252b5132 6335/* prstatus_t exists on:
4a938328 6336 solaris 2.5+
252b5132
RH
6337 linux 2.[01] + glibc
6338 unixware 4.2
6339*/
6340
6341#if defined (HAVE_PRSTATUS_T)
a7b97311
AM
6342static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
6343
252b5132
RH
6344static boolean
6345elfcore_grok_prstatus (abfd, note)
c044fabd
KH
6346 bfd *abfd;
6347 Elf_Internal_Note *note;
252b5132 6348{
dc810e39 6349 size_t raw_size;
7ee38065 6350 int offset;
252b5132 6351
4a938328
MS
6352 if (note->descsz == sizeof (prstatus_t))
6353 {
6354 prstatus_t prstat;
252b5132 6355
e0ebfc61 6356 raw_size = sizeof (prstat.pr_reg);
7ee38065 6357 offset = offsetof (prstatus_t, pr_reg);
4a938328 6358 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 6359
fa49d224
NC
6360 /* Do not overwrite the core signal if it
6361 has already been set by another thread. */
6362 if (elf_tdata (abfd)->core_signal == 0)
6363 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328 6364 elf_tdata (abfd)->core_pid = prstat.pr_pid;
252b5132 6365
4a938328
MS
6366 /* pr_who exists on:
6367 solaris 2.5+
6368 unixware 4.2
6369 pr_who doesn't exist on:
6370 linux 2.[01]
6371 */
252b5132 6372#if defined (HAVE_PRSTATUS_T_PR_WHO)
4a938328 6373 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
252b5132 6374#endif
4a938328 6375 }
7ee38065 6376#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
6377 else if (note->descsz == sizeof (prstatus32_t))
6378 {
6379 /* 64-bit host, 32-bit corefile */
6380 prstatus32_t prstat;
6381
e0ebfc61 6382 raw_size = sizeof (prstat.pr_reg);
7ee38065 6383 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
6384 memcpy (&prstat, note->descdata, sizeof (prstat));
6385
fa49d224
NC
6386 /* Do not overwrite the core signal if it
6387 has already been set by another thread. */
6388 if (elf_tdata (abfd)->core_signal == 0)
6389 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328
MS
6390 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6391
6392 /* pr_who exists on:
6393 solaris 2.5+
6394 unixware 4.2
6395 pr_who doesn't exist on:
6396 linux 2.[01]
6397 */
7ee38065 6398#if defined (HAVE_PRSTATUS32_T_PR_WHO)
4a938328
MS
6399 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6400#endif
6401 }
7ee38065 6402#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
6403 else
6404 {
6405 /* Fail - we don't know how to handle any other
6406 note size (ie. data object type). */
6407 return true;
6408 }
252b5132 6409
bb0082d6 6410 /* Make a ".reg/999" section and a ".reg" section. */
936e320b
AM
6411 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6412 raw_size, note->descpos + offset);
252b5132
RH
6413}
6414#endif /* defined (HAVE_PRSTATUS_T) */
6415
bb0082d6 6416/* Create a pseudosection containing the exact contents of NOTE. */
252b5132 6417static boolean
ff08c6bb 6418elfcore_make_note_pseudosection (abfd, name, note)
c044fabd 6419 bfd *abfd;
ff08c6bb 6420 char *name;
c044fabd 6421 Elf_Internal_Note *note;
252b5132 6422{
936e320b
AM
6423 return _bfd_elfcore_make_pseudosection (abfd, name,
6424 note->descsz, note->descpos);
252b5132
RH
6425}
6426
ff08c6bb
JB
6427/* There isn't a consistent prfpregset_t across platforms,
6428 but it doesn't matter, because we don't have to pick this
c044fabd
KH
6429 data structure apart. */
6430
ff08c6bb
JB
6431static boolean
6432elfcore_grok_prfpreg (abfd, note)
c044fabd
KH
6433 bfd *abfd;
6434 Elf_Internal_Note *note;
ff08c6bb
JB
6435{
6436 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6437}
6438
ff08c6bb
JB
6439/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6440 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6441 literally. */
c044fabd 6442
ff08c6bb
JB
6443static boolean
6444elfcore_grok_prxfpreg (abfd, note)
c044fabd
KH
6445 bfd *abfd;
6446 Elf_Internal_Note *note;
ff08c6bb
JB
6447{
6448 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6449}
6450
252b5132 6451#if defined (HAVE_PRPSINFO_T)
4a938328 6452typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 6453#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6454typedef prpsinfo32_t elfcore_psinfo32_t;
6455#endif
252b5132
RH
6456#endif
6457
6458#if defined (HAVE_PSINFO_T)
4a938328 6459typedef psinfo_t elfcore_psinfo_t;
7ee38065 6460#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6461typedef psinfo32_t elfcore_psinfo32_t;
6462#endif
252b5132
RH
6463#endif
6464
252b5132
RH
6465/* return a malloc'ed copy of a string at START which is at
6466 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 6467 the copy will always have a terminating '\0'. */
252b5132 6468
936e320b 6469char *
bb0082d6 6470_bfd_elfcore_strndup (abfd, start, max)
c044fabd
KH
6471 bfd *abfd;
6472 char *start;
dc810e39 6473 size_t max;
252b5132 6474{
dc810e39 6475 char *dups;
c044fabd 6476 char *end = memchr (start, '\0', max);
dc810e39 6477 size_t len;
252b5132
RH
6478
6479 if (end == NULL)
6480 len = max;
6481 else
6482 len = end - start;
6483
dc810e39
AM
6484 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6485 if (dups == NULL)
252b5132
RH
6486 return NULL;
6487
dc810e39
AM
6488 memcpy (dups, start, len);
6489 dups[len] = '\0';
252b5132 6490
dc810e39 6491 return dups;
252b5132
RH
6492}
6493
bb0082d6 6494#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
a7b97311 6495static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
bb0082d6 6496
252b5132
RH
6497static boolean
6498elfcore_grok_psinfo (abfd, note)
c044fabd
KH
6499 bfd *abfd;
6500 Elf_Internal_Note *note;
252b5132 6501{
4a938328
MS
6502 if (note->descsz == sizeof (elfcore_psinfo_t))
6503 {
6504 elfcore_psinfo_t psinfo;
252b5132 6505
7ee38065 6506 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6507
4a938328 6508 elf_tdata (abfd)->core_program
936e320b
AM
6509 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6510 sizeof (psinfo.pr_fname));
252b5132 6511
4a938328 6512 elf_tdata (abfd)->core_command
936e320b
AM
6513 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6514 sizeof (psinfo.pr_psargs));
4a938328 6515 }
7ee38065 6516#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
6517 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6518 {
6519 /* 64-bit host, 32-bit corefile */
6520 elfcore_psinfo32_t psinfo;
6521
7ee38065 6522 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6523
4a938328 6524 elf_tdata (abfd)->core_program
936e320b
AM
6525 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6526 sizeof (psinfo.pr_fname));
4a938328
MS
6527
6528 elf_tdata (abfd)->core_command
936e320b
AM
6529 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6530 sizeof (psinfo.pr_psargs));
4a938328
MS
6531 }
6532#endif
6533
6534 else
6535 {
6536 /* Fail - we don't know how to handle any other
6537 note size (ie. data object type). */
6538 return true;
6539 }
252b5132
RH
6540
6541 /* Note that for some reason, a spurious space is tacked
6542 onto the end of the args in some (at least one anyway)
c044fabd 6543 implementations, so strip it off if it exists. */
252b5132
RH
6544
6545 {
c044fabd 6546 char *command = elf_tdata (abfd)->core_command;
252b5132
RH
6547 int n = strlen (command);
6548
6549 if (0 < n && command[n - 1] == ' ')
6550 command[n - 1] = '\0';
6551 }
6552
6553 return true;
6554}
6555#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6556
252b5132 6557#if defined (HAVE_PSTATUS_T)
51316059
MS
6558static boolean elfcore_grok_pstatus PARAMS ((bfd *, Elf_Internal_Note *));
6559
252b5132
RH
6560static boolean
6561elfcore_grok_pstatus (abfd, note)
c044fabd
KH
6562 bfd *abfd;
6563 Elf_Internal_Note *note;
252b5132 6564{
f572a39d
AM
6565 if (note->descsz == sizeof (pstatus_t)
6566#if defined (HAVE_PXSTATUS_T)
6567 || note->descsz == sizeof (pxstatus_t)
6568#endif
6569 )
4a938328
MS
6570 {
6571 pstatus_t pstat;
252b5132 6572
4a938328 6573 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6574
4a938328
MS
6575 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6576 }
7ee38065 6577#if defined (HAVE_PSTATUS32_T)
4a938328
MS
6578 else if (note->descsz == sizeof (pstatus32_t))
6579 {
6580 /* 64-bit host, 32-bit corefile */
6581 pstatus32_t pstat;
252b5132 6582
4a938328 6583 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6584
4a938328
MS
6585 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6586 }
6587#endif
252b5132
RH
6588 /* Could grab some more details from the "representative"
6589 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 6590 NT_LWPSTATUS note, presumably. */
252b5132
RH
6591
6592 return true;
6593}
6594#endif /* defined (HAVE_PSTATUS_T) */
6595
252b5132 6596#if defined (HAVE_LWPSTATUS_T)
51316059
MS
6597static boolean elfcore_grok_lwpstatus PARAMS ((bfd *, Elf_Internal_Note *));
6598
252b5132
RH
6599static boolean
6600elfcore_grok_lwpstatus (abfd, note)
c044fabd
KH
6601 bfd *abfd;
6602 Elf_Internal_Note *note;
252b5132
RH
6603{
6604 lwpstatus_t lwpstat;
6605 char buf[100];
c044fabd 6606 char *name;
d4c88bbb 6607 size_t len;
c044fabd 6608 asection *sect;
252b5132 6609
f572a39d
AM
6610 if (note->descsz != sizeof (lwpstat)
6611#if defined (HAVE_LWPXSTATUS_T)
6612 && note->descsz != sizeof (lwpxstatus_t)
6613#endif
6614 )
252b5132
RH
6615 return true;
6616
6617 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6618
6619 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6620 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6621
c044fabd 6622 /* Make a ".reg/999" section. */
252b5132
RH
6623
6624 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb
AM
6625 len = strlen (buf) + 1;
6626 name = bfd_alloc (abfd, (bfd_size_type) len);
252b5132
RH
6627 if (name == NULL)
6628 return false;
d4c88bbb 6629 memcpy (name, buf, len);
252b5132
RH
6630
6631 sect = bfd_make_section (abfd, name);
6632 if (sect == NULL)
6633 return false;
6634
6635#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6636 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6637 sect->filepos = note->descpos
6638 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6639#endif
6640
6641#if defined (HAVE_LWPSTATUS_T_PR_REG)
6642 sect->_raw_size = sizeof (lwpstat.pr_reg);
6643 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6644#endif
6645
6646 sect->flags = SEC_HAS_CONTENTS;
6647 sect->alignment_power = 2;
6648
6649 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6650 return false;
6651
6652 /* Make a ".reg2/999" section */
6653
6654 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb
AM
6655 len = strlen (buf) + 1;
6656 name = bfd_alloc (abfd, (bfd_size_type) len);
252b5132
RH
6657 if (name == NULL)
6658 return false;
d4c88bbb 6659 memcpy (name, buf, len);
252b5132
RH
6660
6661 sect = bfd_make_section (abfd, name);
6662 if (sect == NULL)
6663 return false;
6664
6665#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6666 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6667 sect->filepos = note->descpos
6668 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6669#endif
6670
6671#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6672 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6673 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6674#endif
6675
6676 sect->flags = SEC_HAS_CONTENTS;
6677 sect->alignment_power = 2;
6678
936e320b 6679 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
6680}
6681#endif /* defined (HAVE_LWPSTATUS_T) */
6682
16e9c715
NC
6683#if defined (HAVE_WIN32_PSTATUS_T)
6684static boolean
6685elfcore_grok_win32pstatus (abfd, note)
c044fabd
KH
6686 bfd *abfd;
6687 Elf_Internal_Note *note;
16e9c715
NC
6688{
6689 char buf[30];
c044fabd 6690 char *name;
d4c88bbb 6691 size_t len;
c044fabd 6692 asection *sect;
16e9c715
NC
6693 win32_pstatus_t pstatus;
6694
6695 if (note->descsz < sizeof (pstatus))
6696 return true;
6697
e8eab623 6698 memcpy (&pstatus, note->descdata, sizeof (pstatus));
c044fabd
KH
6699
6700 switch (pstatus.data_type)
16e9c715
NC
6701 {
6702 case NOTE_INFO_PROCESS:
6703 /* FIXME: need to add ->core_command. */
6704 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6705 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
c044fabd 6706 break;
16e9c715
NC
6707
6708 case NOTE_INFO_THREAD:
6709 /* Make a ".reg/999" section. */
6710 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
c044fabd 6711
d4c88bbb
AM
6712 len = strlen (buf) + 1;
6713 name = bfd_alloc (abfd, (bfd_size_type) len);
16e9c715 6714 if (name == NULL)
c044fabd
KH
6715 return false;
6716
d4c88bbb 6717 memcpy (name, buf, len);
16e9c715
NC
6718
6719 sect = bfd_make_section (abfd, name);
6720 if (sect == NULL)
c044fabd
KH
6721 return false;
6722
16e9c715 6723 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
079e9a2f
AM
6724 sect->filepos = (note->descpos
6725 + offsetof (struct win32_pstatus,
6726 data.thread_info.thread_context));
16e9c715
NC
6727 sect->flags = SEC_HAS_CONTENTS;
6728 sect->alignment_power = 2;
6729
6730 if (pstatus.data.thread_info.is_active_thread)
6731 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6732 return false;
6733 break;
6734
6735 case NOTE_INFO_MODULE:
6736 /* Make a ".module/xxxxxxxx" section. */
c044fabd
KH
6737 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6738
d4c88bbb
AM
6739 len = strlen (buf) + 1;
6740 name = bfd_alloc (abfd, (bfd_size_type) len);
16e9c715
NC
6741 if (name == NULL)
6742 return false;
c044fabd 6743
d4c88bbb 6744 memcpy (name, buf, len);
252b5132 6745
16e9c715 6746 sect = bfd_make_section (abfd, name);
c044fabd 6747
16e9c715
NC
6748 if (sect == NULL)
6749 return false;
c044fabd 6750
16e9c715
NC
6751 sect->_raw_size = note->descsz;
6752 sect->filepos = note->descpos;
6753 sect->flags = SEC_HAS_CONTENTS;
6754 sect->alignment_power = 2;
6755 break;
6756
6757 default:
6758 return true;
6759 }
6760
6761 return true;
6762}
6763#endif /* HAVE_WIN32_PSTATUS_T */
252b5132
RH
6764
6765static boolean
6766elfcore_grok_note (abfd, note)
c044fabd
KH
6767 bfd *abfd;
6768 Elf_Internal_Note *note;
252b5132 6769{
bb0082d6
AM
6770 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6771
252b5132
RH
6772 switch (note->type)
6773 {
6774 default:
6775 return true;
6776
252b5132 6777 case NT_PRSTATUS:
bb0082d6
AM
6778 if (bed->elf_backend_grok_prstatus)
6779 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6780 return true;
6781#if defined (HAVE_PRSTATUS_T)
252b5132 6782 return elfcore_grok_prstatus (abfd, note);
bb0082d6
AM
6783#else
6784 return true;
252b5132
RH
6785#endif
6786
6787#if defined (HAVE_PSTATUS_T)
6788 case NT_PSTATUS:
6789 return elfcore_grok_pstatus (abfd, note);
6790#endif
6791
6792#if defined (HAVE_LWPSTATUS_T)
6793 case NT_LWPSTATUS:
6794 return elfcore_grok_lwpstatus (abfd, note);
6795#endif
6796
6797 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6798 return elfcore_grok_prfpreg (abfd, note);
6799
16e9c715 6800#if defined (HAVE_WIN32_PSTATUS_T)
c044fabd 6801 case NT_WIN32PSTATUS:
16e9c715
NC
6802 return elfcore_grok_win32pstatus (abfd, note);
6803#endif
6804
c044fabd 6805 case NT_PRXFPREG: /* Linux SSE extension */
ff08c6bb
JB
6806 if (note->namesz == 5
6807 && ! strcmp (note->namedata, "LINUX"))
6808 return elfcore_grok_prxfpreg (abfd, note);
6809 else
6810 return true;
6811
252b5132
RH
6812 case NT_PRPSINFO:
6813 case NT_PSINFO:
bb0082d6
AM
6814 if (bed->elf_backend_grok_psinfo)
6815 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6816 return true;
6817#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 6818 return elfcore_grok_psinfo (abfd, note);
bb0082d6
AM
6819#else
6820 return true;
252b5132
RH
6821#endif
6822 }
6823}
6824
50b2bdb7
AM
6825static boolean
6826elfcore_netbsd_get_lwpid (note, lwpidp)
6827 Elf_Internal_Note *note;
6828 int *lwpidp;
6829{
6830 char *cp;
6831
6832 cp = strchr (note->namedata, '@');
6833 if (cp != NULL)
6834 {
d2b64500 6835 *lwpidp = atoi(cp + 1);
50b2bdb7
AM
6836 return true;
6837 }
6838 return false;
6839}
6840
6841static boolean
6842elfcore_grok_netbsd_procinfo (abfd, note)
6843 bfd *abfd;
6844 Elf_Internal_Note *note;
6845{
6846
6847 /* Signal number at offset 0x08. */
6848 elf_tdata (abfd)->core_signal
6849 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6850
6851 /* Process ID at offset 0x50. */
6852 elf_tdata (abfd)->core_pid
6853 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6854
6855 /* Command name at 0x7c (max 32 bytes, including nul). */
6856 elf_tdata (abfd)->core_command
6857 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6858
6859 return true;
6860}
6861
6862static boolean
6863elfcore_grok_netbsd_note (abfd, note)
6864 bfd *abfd;
6865 Elf_Internal_Note *note;
6866{
6867 int lwp;
6868
6869 if (elfcore_netbsd_get_lwpid (note, &lwp))
6870 elf_tdata (abfd)->core_lwpid = lwp;
6871
b4db1224 6872 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
6873 {
6874 /* NetBSD-specific core "procinfo". Note that we expect to
6875 find this note before any of the others, which is fine,
6876 since the kernel writes this note out first when it
6877 creates a core file. */
47d9a591 6878
50b2bdb7
AM
6879 return elfcore_grok_netbsd_procinfo (abfd, note);
6880 }
6881
b4db1224
JT
6882 /* As of Jan 2002 there are no other machine-independent notes
6883 defined for NetBSD core files. If the note type is less
6884 than the start of the machine-dependent note types, we don't
6885 understand it. */
47d9a591 6886
b4db1224 6887 if (note->type < NT_NETBSDCORE_FIRSTMACH)
50b2bdb7
AM
6888 return true;
6889
6890
6891 switch (bfd_get_arch (abfd))
6892 {
6893 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6894 PT_GETFPREGS == mach+2. */
6895
6896 case bfd_arch_alpha:
6897 case bfd_arch_sparc:
6898 switch (note->type)
6899 {
b4db1224 6900 case NT_NETBSDCORE_FIRSTMACH+0:
50b2bdb7
AM
6901 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6902
b4db1224 6903 case NT_NETBSDCORE_FIRSTMACH+2:
50b2bdb7
AM
6904 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6905
6906 default:
6907 return true;
6908 }
6909
6910 /* On all other arch's, PT_GETREGS == mach+1 and
6911 PT_GETFPREGS == mach+3. */
6912
6913 default:
6914 switch (note->type)
6915 {
b4db1224 6916 case NT_NETBSDCORE_FIRSTMACH+1:
50b2bdb7
AM
6917 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6918
b4db1224 6919 case NT_NETBSDCORE_FIRSTMACH+3:
50b2bdb7
AM
6920 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6921
6922 default:
6923 return true;
6924 }
6925 }
6926 /* NOTREACHED */
6927}
6928
7c76fa91
MS
6929/* Function: elfcore_write_note
6930
47d9a591 6931 Inputs:
7c76fa91
MS
6932 buffer to hold note
6933 name of note
6934 type of note
6935 data for note
6936 size of data for note
6937
6938 Return:
6939 End of buffer containing note. */
6940
6941char *
6942elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6943 bfd *abfd;
6944 char *buf;
6945 int *bufsiz;
d4c88bbb 6946 const char *name;
7c76fa91 6947 int type;
d4c88bbb 6948 const PTR input;
7c76fa91
MS
6949 int size;
6950{
6951 Elf_External_Note *xnp;
d4c88bbb
AM
6952 size_t namesz;
6953 size_t pad;
6954 size_t newspace;
7c76fa91
MS
6955 char *p, *dest;
6956
d4c88bbb
AM
6957 namesz = 0;
6958 pad = 0;
6959 if (name != NULL)
6960 {
6961 struct elf_backend_data *bed;
6962
6963 namesz = strlen (name) + 1;
6964 bed = get_elf_backend_data (abfd);
6965 pad = -namesz & (bed->s->file_align - 1);
6966 }
6967
6968 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;
6969
7c76fa91
MS
6970 p = realloc (buf, *bufsiz + newspace);
6971 dest = p + *bufsiz;
6972 *bufsiz += newspace;
6973 xnp = (Elf_External_Note *) dest;
6974 H_PUT_32 (abfd, namesz, xnp->namesz);
6975 H_PUT_32 (abfd, size, xnp->descsz);
6976 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
6977 dest = xnp->name;
6978 if (name != NULL)
6979 {
6980 memcpy (dest, name, namesz);
6981 dest += namesz;
6982 while (pad != 0)
6983 {
6984 *dest++ = '\0';
6985 --pad;
6986 }
6987 }
6988 memcpy (dest, input, size);
7c76fa91
MS
6989 return p;
6990}
6991
6992#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6993char *
6994elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6995 bfd *abfd;
6996 char *buf;
6997 int *bufsiz;
47d9a591 6998 const char *fname;
d4c88bbb 6999 const char *psargs;
7c76fa91
MS
7000{
7001 int note_type;
7002 char *note_name = "CORE";
7003
7004#if defined (HAVE_PSINFO_T)
7005 psinfo_t data;
7006 note_type = NT_PSINFO;
7007#else
7008 prpsinfo_t data;
7009 note_type = NT_PRPSINFO;
7010#endif
7011
7012 memset (&data, 0, sizeof (data));
7013 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7014 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
47d9a591 7015 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7016 note_name, note_type, &data, sizeof (data));
7017}
7018#endif /* PSINFO_T or PRPSINFO_T */
7019
7020#if defined (HAVE_PRSTATUS_T)
7021char *
7022elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7023 bfd *abfd;
7024 char *buf;
7025 int *bufsiz;
b87011e9 7026 long pid;
7c76fa91 7027 int cursig;
d4c88bbb 7028 const PTR gregs;
7c76fa91
MS
7029{
7030 prstatus_t prstat;
7031 char *note_name = "CORE";
7032
7033 memset (&prstat, 0, sizeof (prstat));
7034 prstat.pr_pid = pid;
7035 prstat.pr_cursig = cursig;
c106e334 7036 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
47d9a591 7037 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7038 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7039}
7040#endif /* HAVE_PRSTATUS_T */
7041
51316059
MS
7042#if defined (HAVE_LWPSTATUS_T)
7043char *
7044elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7045 bfd *abfd;
7046 char *buf;
7047 int *bufsiz;
7048 long pid;
7049 int cursig;
d4c88bbb 7050 const PTR gregs;
51316059
MS
7051{
7052 lwpstatus_t lwpstat;
7053 char *note_name = "CORE";
7054
7055 memset (&lwpstat, 0, sizeof (lwpstat));
7056 lwpstat.pr_lwpid = pid >> 16;
7057 lwpstat.pr_cursig = cursig;
7058#if defined (HAVE_LWPSTATUS_T_PR_REG)
7059 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7060#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7061#if !defined(gregs)
7062 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7063 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7064#else
7065 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7066 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7067#endif
7068#endif
47d9a591 7069 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
7070 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7071}
7072#endif /* HAVE_LWPSTATUS_T */
7073
7c76fa91
MS
7074#if defined (HAVE_PSTATUS_T)
7075char *
7076elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7077 bfd *abfd;
7078 char *buf;
7079 int *bufsiz;
b87011e9 7080 long pid;
7c76fa91 7081 int cursig;
d4c88bbb 7082 const PTR gregs;
7c76fa91
MS
7083{
7084 pstatus_t pstat;
7085 char *note_name = "CORE";
7086
51316059
MS
7087 memset (&pstat, 0, sizeof (pstat));
7088 pstat.pr_pid = pid & 0xffff;
47d9a591 7089 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
7090 NT_PSTATUS, &pstat, sizeof (pstat));
7091 return buf;
7c76fa91
MS
7092}
7093#endif /* HAVE_PSTATUS_T */
7094
7095char *
7096elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
7097 bfd *abfd;
7098 char *buf;
7099 int *bufsiz;
d4c88bbb 7100 const PTR fpregs;
7c76fa91
MS
7101 int size;
7102{
7103 char *note_name = "CORE";
47d9a591 7104 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7105 note_name, NT_FPREGSET, fpregs, size);
7106}
7107
7108char *
7109elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
7110 bfd *abfd;
7111 char *buf;
7112 int *bufsiz;
d4c88bbb 7113 const PTR xfpregs;
7c76fa91
MS
7114 int size;
7115{
7116 char *note_name = "LINUX";
47d9a591 7117 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7118 note_name, NT_PRXFPREG, xfpregs, size);
7119}
7120
252b5132
RH
7121static boolean
7122elfcore_read_notes (abfd, offset, size)
c044fabd 7123 bfd *abfd;
dc810e39
AM
7124 file_ptr offset;
7125 bfd_size_type size;
252b5132 7126{
c044fabd
KH
7127 char *buf;
7128 char *p;
252b5132
RH
7129
7130 if (size <= 0)
7131 return true;
7132
dc810e39 7133 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
252b5132
RH
7134 return false;
7135
dc810e39 7136 buf = bfd_malloc (size);
252b5132
RH
7137 if (buf == NULL)
7138 return false;
7139
dc810e39 7140 if (bfd_bread (buf, size, abfd) != size)
252b5132
RH
7141 {
7142 error:
7143 free (buf);
7144 return false;
7145 }
7146
7147 p = buf;
7148 while (p < buf + size)
7149 {
c044fabd
KH
7150 /* FIXME: bad alignment assumption. */
7151 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
7152 Elf_Internal_Note in;
7153
dc810e39 7154 in.type = H_GET_32 (abfd, xnp->type);
252b5132 7155
dc810e39 7156 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132
RH
7157 in.namedata = xnp->name;
7158
dc810e39 7159 in.descsz = H_GET_32 (abfd, xnp->descsz);
252b5132
RH
7160 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7161 in.descpos = offset + (in.descdata - buf);
7162
50b2bdb7
AM
7163 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7164 {
7165 if (! elfcore_grok_netbsd_note (abfd, &in))
7166 goto error;
7167 }
7168 else
7169 {
7170 if (! elfcore_grok_note (abfd, &in))
7171 goto error;
7172 }
252b5132
RH
7173
7174 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7175 }
7176
7177 free (buf);
7178 return true;
7179}
98d8431c
JB
7180\f
7181/* Providing external access to the ELF program header table. */
7182
7183/* Return an upper bound on the number of bytes required to store a
7184 copy of ABFD's program header table entries. Return -1 if an error
7185 occurs; bfd_get_error will return an appropriate code. */
c044fabd 7186
98d8431c
JB
7187long
7188bfd_get_elf_phdr_upper_bound (abfd)
7189 bfd *abfd;
7190{
7191 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7192 {
7193 bfd_set_error (bfd_error_wrong_format);
7194 return -1;
7195 }
7196
936e320b 7197 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
7198}
7199
98d8431c
JB
7200/* Copy ABFD's program header table entries to *PHDRS. The entries
7201 will be stored as an array of Elf_Internal_Phdr structures, as
7202 defined in include/elf/internal.h. To find out how large the
7203 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7204
7205 Return the number of program header table entries read, or -1 if an
7206 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 7207
98d8431c
JB
7208int
7209bfd_get_elf_phdrs (abfd, phdrs)
7210 bfd *abfd;
7211 void *phdrs;
7212{
7213 int num_phdrs;
7214
7215 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7216 {
7217 bfd_set_error (bfd_error_wrong_format);
7218 return -1;
7219 }
7220
7221 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 7222 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
7223 num_phdrs * sizeof (Elf_Internal_Phdr));
7224
7225 return num_phdrs;
7226}
ae4221d7
L
7227
7228void
4e771d61 7229_bfd_elf_sprintf_vma (abfd, buf, value)
cc55aec9 7230 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
7231 char *buf;
7232 bfd_vma value;
7233{
d3b05f8d 7234#ifdef BFD64
ae4221d7
L
7235 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7236
7237 i_ehdrp = elf_elfheader (abfd);
7238 if (i_ehdrp == NULL)
7239 sprintf_vma (buf, value);
7240 else
7241 {
7242 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7243 {
ae4221d7 7244#if BFD_HOST_64BIT_LONG
cc55aec9 7245 sprintf (buf, "%016lx", value);
ae4221d7 7246#else
cc55aec9
AM
7247 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7248 _bfd_int64_low (value));
ae4221d7 7249#endif
cc55aec9 7250 }
ae4221d7
L
7251 else
7252 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7253 }
d3b05f8d
L
7254#else
7255 sprintf_vma (buf, value);
7256#endif
ae4221d7
L
7257}
7258
7259void
4e771d61 7260_bfd_elf_fprintf_vma (abfd, stream, value)
cc55aec9 7261 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
7262 PTR stream;
7263 bfd_vma value;
7264{
d3b05f8d 7265#ifdef BFD64
ae4221d7
L
7266 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7267
7268 i_ehdrp = elf_elfheader (abfd);
7269 if (i_ehdrp == NULL)
7270 fprintf_vma ((FILE *) stream, value);
7271 else
7272 {
7273 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7274 {
ae4221d7 7275#if BFD_HOST_64BIT_LONG
cc55aec9 7276 fprintf ((FILE *) stream, "%016lx", value);
ae4221d7 7277#else
cc55aec9
AM
7278 fprintf ((FILE *) stream, "%08lx%08lx",
7279 _bfd_int64_high (value), _bfd_int64_low (value));
ae4221d7 7280#endif
cc55aec9 7281 }
ae4221d7
L
7282 else
7283 fprintf ((FILE *) stream, "%08lx",
7284 (unsigned long) (value & 0xffffffff));
7285 }
d3b05f8d
L
7286#else
7287 fprintf_vma ((FILE *) stream, value);
7288#endif
ae4221d7 7289}
db6751f2
JJ
7290
7291enum elf_reloc_type_class
f51e552e
AM
7292_bfd_elf_reloc_type_class (rela)
7293 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
db6751f2
JJ
7294{
7295 return reloc_class_normal;
7296}
f8df10f4 7297
47d9a591 7298/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
7299 relocation against a local symbol. */
7300
7301bfd_vma
7302_bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7303 bfd *abfd;
7304 Elf_Internal_Sym *sym;
7305 asection *sec;
7306 Elf_Internal_Rela *rel;
7307{
7308 bfd_vma relocation;
7309
7310 relocation = (sec->output_section->vma
7311 + sec->output_offset
7312 + sym->st_value);
7313 if ((sec->flags & SEC_MERGE)
c629eae0 7314 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
65765700 7315 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
f8df10f4
JJ
7316 {
7317 asection *msec;
7318
7319 msec = sec;
7320 rel->r_addend =
7321 _bfd_merged_section_offset (abfd, &msec,
65765700 7322 elf_section_data (sec)->sec_info,
f8df10f4
JJ
7323 sym->st_value + rel->r_addend,
7324 (bfd_vma) 0)
7325 - relocation;
7326 rel->r_addend += msec->output_section->vma + msec->output_offset;
7327 }
7328 return relocation;
7329}
c629eae0
JJ
7330
7331bfd_vma
7332_bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7333 bfd *abfd;
7334 Elf_Internal_Sym *sym;
7335 asection **psec;
7336 bfd_vma addend;
47d9a591 7337{
c629eae0
JJ
7338 asection *sec = *psec;
7339
65765700 7340 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
c629eae0
JJ
7341 return sym->st_value + addend;
7342
7343 return _bfd_merged_section_offset (abfd, psec,
65765700 7344 elf_section_data (sec)->sec_info,
c629eae0
JJ
7345 sym->st_value + addend, (bfd_vma) 0);
7346}
7347
7348bfd_vma
7349_bfd_elf_section_offset (abfd, info, sec, offset)
7350 bfd *abfd;
7351 struct bfd_link_info *info;
7352 asection *sec;
7353 bfd_vma offset;
7354{
7355 struct bfd_elf_section_data *sec_data;
7356
7357 sec_data = elf_section_data (sec);
65765700
JJ
7358 switch (sec_data->sec_info_type)
7359 {
7360 case ELF_INFO_TYPE_STABS:
7361 return _bfd_stab_section_offset
7362 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
7363 offset);
7364 case ELF_INFO_TYPE_EH_FRAME:
7365 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7366 default:
7367 return offset;
7368 }
c629eae0 7369}