1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
47 static int elf_sort_sections (const void *, const void *);
48 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
49 static bfd_boolean prep_headers (bfd *);
50 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
51 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
52 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
55 /* Swap version information in and out. The version information is
56 currently size independent. If that ever changes, this code will
57 need to move into elfcode.h. */
59 /* Swap in a Verdef structure. */
62 _bfd_elf_swap_verdef_in (bfd *abfd,
63 const Elf_External_Verdef *src,
64 Elf_Internal_Verdef *dst)
66 dst->vd_version = H_GET_16 (abfd, src->vd_version);
67 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
68 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
69 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
70 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
71 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
72 dst->vd_next = H_GET_32 (abfd, src->vd_next);
75 /* Swap out a Verdef structure. */
78 _bfd_elf_swap_verdef_out (bfd *abfd,
79 const Elf_Internal_Verdef *src,
80 Elf_External_Verdef *dst)
82 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
83 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
84 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
85 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
86 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
87 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
88 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
91 /* Swap in a Verdaux structure. */
94 _bfd_elf_swap_verdaux_in (bfd *abfd,
95 const Elf_External_Verdaux *src,
96 Elf_Internal_Verdaux *dst)
98 dst->vda_name = H_GET_32 (abfd, src->vda_name);
99 dst->vda_next = H_GET_32 (abfd, src->vda_next);
102 /* Swap out a Verdaux structure. */
105 _bfd_elf_swap_verdaux_out (bfd *abfd,
106 const Elf_Internal_Verdaux *src,
107 Elf_External_Verdaux *dst)
109 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
110 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
113 /* Swap in a Verneed structure. */
116 _bfd_elf_swap_verneed_in (bfd *abfd,
117 const Elf_External_Verneed *src,
118 Elf_Internal_Verneed *dst)
120 dst->vn_version = H_GET_16 (abfd, src->vn_version);
121 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
122 dst->vn_file = H_GET_32 (abfd, src->vn_file);
123 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
124 dst->vn_next = H_GET_32 (abfd, src->vn_next);
127 /* Swap out a Verneed structure. */
130 _bfd_elf_swap_verneed_out (bfd *abfd,
131 const Elf_Internal_Verneed *src,
132 Elf_External_Verneed *dst)
134 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
135 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
136 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
137 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
138 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
141 /* Swap in a Vernaux structure. */
144 _bfd_elf_swap_vernaux_in (bfd *abfd,
145 const Elf_External_Vernaux *src,
146 Elf_Internal_Vernaux *dst)
148 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
149 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
150 dst->vna_other = H_GET_16 (abfd, src->vna_other);
151 dst->vna_name = H_GET_32 (abfd, src->vna_name);
152 dst->vna_next = H_GET_32 (abfd, src->vna_next);
155 /* Swap out a Vernaux structure. */
158 _bfd_elf_swap_vernaux_out (bfd *abfd,
159 const Elf_Internal_Vernaux *src,
160 Elf_External_Vernaux *dst)
162 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
163 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
164 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
165 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
166 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
169 /* Swap in a Versym structure. */
172 _bfd_elf_swap_versym_in (bfd *abfd,
173 const Elf_External_Versym *src,
174 Elf_Internal_Versym *dst)
176 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
179 /* Swap out a Versym structure. */
182 _bfd_elf_swap_versym_out (bfd *abfd,
183 const Elf_Internal_Versym *src,
184 Elf_External_Versym *dst)
186 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
189 /* Standard ELF hash function. Do not change this function; you will
190 cause invalid hash tables to be generated. */
193 bfd_elf_hash (const char *namearg)
195 const unsigned char *name = (const unsigned char *) namearg;
200 while ((ch = *name++) != '\0')
203 if ((g = (h & 0xf0000000)) != 0)
206 /* The ELF ABI says `h &= ~g', but this is equivalent in
207 this case and on some machines one insn instead of two. */
211 return h & 0xffffffff;
214 /* DT_GNU_HASH hash function. Do not change this function; you will
215 cause invalid hash tables to be generated. */
218 bfd_elf_gnu_hash (const char *namearg)
220 const unsigned char *name = (const unsigned char *) namearg;
221 unsigned long h = 5381;
224 while ((ch = *name++) != '\0')
225 h = (h << 5) + h + ch;
226 return h & 0xffffffff;
229 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
230 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
232 bfd_elf_allocate_object (bfd *abfd,
234 enum elf_object_id object_id)
236 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
237 abfd->tdata.any = bfd_zalloc (abfd, object_size);
238 if (abfd->tdata.any == NULL)
241 elf_object_id (abfd) = object_id;
242 elf_program_header_size (abfd) = (bfd_size_type) -1;
248 bfd_elf_make_generic_object (bfd *abfd)
250 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
255 bfd_elf_mkcorefile (bfd *abfd)
257 /* I think this can be done just like an object file. */
258 return bfd_elf_make_generic_object (abfd);
262 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
264 Elf_Internal_Shdr **i_shdrp;
265 bfd_byte *shstrtab = NULL;
267 bfd_size_type shstrtabsize;
269 i_shdrp = elf_elfsections (abfd);
271 || shindex >= elf_numsections (abfd)
272 || i_shdrp[shindex] == 0)
275 shstrtab = i_shdrp[shindex]->contents;
276 if (shstrtab == NULL)
278 /* No cached one, attempt to read, and cache what we read. */
279 offset = i_shdrp[shindex]->sh_offset;
280 shstrtabsize = i_shdrp[shindex]->sh_size;
282 /* Allocate and clear an extra byte at the end, to prevent crashes
283 in case the string table is not terminated. */
284 if (shstrtabsize + 1 <= 1
285 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
286 || bfd_seek (abfd, offset, SEEK_SET) != 0)
288 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
290 if (bfd_get_error () != bfd_error_system_call)
291 bfd_set_error (bfd_error_file_truncated);
293 /* Once we've failed to read it, make sure we don't keep
294 trying. Otherwise, we'll keep allocating space for
295 the string table over and over. */
296 i_shdrp[shindex]->sh_size = 0;
299 shstrtab[shstrtabsize] = '\0';
300 i_shdrp[shindex]->contents = shstrtab;
302 return (char *) shstrtab;
306 bfd_elf_string_from_elf_section (bfd *abfd,
307 unsigned int shindex,
308 unsigned int strindex)
310 Elf_Internal_Shdr *hdr;
315 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318 hdr = elf_elfsections (abfd)[shindex];
320 if (hdr->contents == NULL
321 && bfd_elf_get_str_section (abfd, shindex) == NULL)
324 if (strindex >= hdr->sh_size)
326 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
327 (*_bfd_error_handler)
328 (_("%B: invalid string offset %u >= %lu for section `%s'"),
329 abfd, strindex, (unsigned long) hdr->sh_size,
330 (shindex == shstrndx && strindex == hdr->sh_name
332 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
336 return ((char *) hdr->contents) + strindex;
339 /* Read and convert symbols to internal format.
340 SYMCOUNT specifies the number of symbols to read, starting from
341 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
342 are non-NULL, they are used to store the internal symbols, external
343 symbols, and symbol section index extensions, respectively.
344 Returns a pointer to the internal symbol buffer (malloced if necessary)
345 or NULL if there were no symbols or some kind of problem. */
348 bfd_elf_get_elf_syms (bfd *ibfd,
349 Elf_Internal_Shdr *symtab_hdr,
352 Elf_Internal_Sym *intsym_buf,
354 Elf_External_Sym_Shndx *extshndx_buf)
356 Elf_Internal_Shdr *shndx_hdr;
358 const bfd_byte *esym;
359 Elf_External_Sym_Shndx *alloc_extshndx;
360 Elf_External_Sym_Shndx *shndx;
361 Elf_Internal_Sym *alloc_intsym;
362 Elf_Internal_Sym *isym;
363 Elf_Internal_Sym *isymend;
364 const struct elf_backend_data *bed;
369 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
375 /* Normal syms might have section extension entries. */
377 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
378 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
380 /* Read the symbols. */
382 alloc_extshndx = NULL;
384 bed = get_elf_backend_data (ibfd);
385 extsym_size = bed->s->sizeof_sym;
386 amt = symcount * extsym_size;
387 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
388 if (extsym_buf == NULL)
390 alloc_ext = bfd_malloc2 (symcount, extsym_size);
391 extsym_buf = alloc_ext;
393 if (extsym_buf == NULL
394 || bfd_seek (ibfd, pos, SEEK_SET) != 0
395 || bfd_bread (extsym_buf, amt, ibfd) != amt)
401 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
405 amt = symcount * sizeof (Elf_External_Sym_Shndx);
406 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
407 if (extshndx_buf == NULL)
409 alloc_extshndx = bfd_malloc2 (symcount,
410 sizeof (Elf_External_Sym_Shndx));
411 extshndx_buf = alloc_extshndx;
413 if (extshndx_buf == NULL
414 || bfd_seek (ibfd, pos, SEEK_SET) != 0
415 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
422 if (intsym_buf == NULL)
424 alloc_intsym = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
425 intsym_buf = alloc_intsym;
426 if (intsym_buf == NULL)
430 /* Convert the symbols to internal form. */
431 isymend = intsym_buf + symcount;
432 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
434 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
435 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
437 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
438 (*_bfd_error_handler) (_("%B symbol number %lu references "
439 "nonexistent SHT_SYMTAB_SHNDX section"),
440 ibfd, (unsigned long) symoffset);
441 if (alloc_intsym != NULL)
448 if (alloc_ext != NULL)
450 if (alloc_extshndx != NULL)
451 free (alloc_extshndx);
456 /* Look up a symbol name. */
458 bfd_elf_sym_name (bfd *abfd,
459 Elf_Internal_Shdr *symtab_hdr,
460 Elf_Internal_Sym *isym,
464 unsigned int iname = isym->st_name;
465 unsigned int shindex = symtab_hdr->sh_link;
467 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
468 /* Check for a bogus st_shndx to avoid crashing. */
469 && isym->st_shndx < elf_numsections (abfd))
471 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
472 shindex = elf_elfheader (abfd)->e_shstrndx;
475 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
478 else if (sym_sec && *name == '\0')
479 name = bfd_section_name (abfd, sym_sec);
484 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
485 sections. The first element is the flags, the rest are section
488 typedef union elf_internal_group {
489 Elf_Internal_Shdr *shdr;
491 } Elf_Internal_Group;
493 /* Return the name of the group signature symbol. Why isn't the
494 signature just a string? */
497 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
499 Elf_Internal_Shdr *hdr;
500 unsigned char esym[sizeof (Elf64_External_Sym)];
501 Elf_External_Sym_Shndx eshndx;
502 Elf_Internal_Sym isym;
504 /* First we need to ensure the symbol table is available. Make sure
505 that it is a symbol table section. */
506 if (ghdr->sh_link >= elf_numsections (abfd))
508 hdr = elf_elfsections (abfd) [ghdr->sh_link];
509 if (hdr->sh_type != SHT_SYMTAB
510 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
513 /* Go read the symbol. */
514 hdr = &elf_tdata (abfd)->symtab_hdr;
515 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
516 &isym, esym, &eshndx) == NULL)
519 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
522 /* Set next_in_group list pointer, and group name for NEWSECT. */
525 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
527 unsigned int num_group = elf_tdata (abfd)->num_group;
529 /* If num_group is zero, read in all SHT_GROUP sections. The count
530 is set to -1 if there are no SHT_GROUP sections. */
533 unsigned int i, shnum;
535 /* First count the number of groups. If we have a SHT_GROUP
536 section with just a flag word (ie. sh_size is 4), ignore it. */
537 shnum = elf_numsections (abfd);
540 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
541 ( (shdr)->sh_type == SHT_GROUP \
542 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
543 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
544 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
546 for (i = 0; i < shnum; i++)
548 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
550 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
556 num_group = (unsigned) -1;
557 elf_tdata (abfd)->num_group = num_group;
561 /* We keep a list of elf section headers for group sections,
562 so we can find them quickly. */
565 elf_tdata (abfd)->num_group = num_group;
566 elf_tdata (abfd)->group_sect_ptr
567 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
568 if (elf_tdata (abfd)->group_sect_ptr == NULL)
572 for (i = 0; i < shnum; i++)
574 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
576 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
579 Elf_Internal_Group *dest;
581 /* Add to list of sections. */
582 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
585 /* Read the raw contents. */
586 BFD_ASSERT (sizeof (*dest) >= 4);
587 amt = shdr->sh_size * sizeof (*dest) / 4;
588 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
590 /* PR binutils/4110: Handle corrupt group headers. */
591 if (shdr->contents == NULL)
594 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
595 bfd_set_error (bfd_error_bad_value);
599 memset (shdr->contents, 0, amt);
601 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
602 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
606 /* Translate raw contents, a flag word followed by an
607 array of elf section indices all in target byte order,
608 to the flag word followed by an array of elf section
610 src = shdr->contents + shdr->sh_size;
611 dest = (Elf_Internal_Group *) (shdr->contents + amt);
618 idx = H_GET_32 (abfd, src);
619 if (src == shdr->contents)
622 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
623 shdr->bfd_section->flags
624 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
629 ((*_bfd_error_handler)
630 (_("%B: invalid SHT_GROUP entry"), abfd));
633 dest->shdr = elf_elfsections (abfd)[idx];
640 if (num_group != (unsigned) -1)
644 for (i = 0; i < num_group; i++)
646 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
647 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
648 unsigned int n_elt = shdr->sh_size / 4;
650 /* Look through this group's sections to see if current
651 section is a member. */
653 if ((++idx)->shdr == hdr)
657 /* We are a member of this group. Go looking through
658 other members to see if any others are linked via
660 idx = (Elf_Internal_Group *) shdr->contents;
661 n_elt = shdr->sh_size / 4;
663 if ((s = (++idx)->shdr->bfd_section) != NULL
664 && elf_next_in_group (s) != NULL)
668 /* Snarf the group name from other member, and
669 insert current section in circular list. */
670 elf_group_name (newsect) = elf_group_name (s);
671 elf_next_in_group (newsect) = elf_next_in_group (s);
672 elf_next_in_group (s) = newsect;
678 gname = group_signature (abfd, shdr);
681 elf_group_name (newsect) = gname;
683 /* Start a circular list with one element. */
684 elf_next_in_group (newsect) = newsect;
687 /* If the group section has been created, point to the
689 if (shdr->bfd_section != NULL)
690 elf_next_in_group (shdr->bfd_section) = newsect;
698 if (elf_group_name (newsect) == NULL)
700 (*_bfd_error_handler) (_("%B: no group info for section %A"),
707 _bfd_elf_setup_sections (bfd *abfd)
710 unsigned int num_group = elf_tdata (abfd)->num_group;
711 bfd_boolean result = TRUE;
714 /* Process SHF_LINK_ORDER. */
715 for (s = abfd->sections; s != NULL; s = s->next)
717 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
718 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
720 unsigned int elfsec = this_hdr->sh_link;
721 /* FIXME: The old Intel compiler and old strip/objcopy may
722 not set the sh_link or sh_info fields. Hence we could
723 get the situation where elfsec is 0. */
726 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
727 if (bed->link_order_error_handler)
728 bed->link_order_error_handler
729 (_("%B: warning: sh_link not set for section `%A'"),
734 asection *link = NULL;
736 if (elfsec < elf_numsections (abfd))
738 this_hdr = elf_elfsections (abfd)[elfsec];
739 link = this_hdr->bfd_section;
743 Some strip/objcopy may leave an incorrect value in
744 sh_link. We don't want to proceed. */
747 (*_bfd_error_handler)
748 (_("%B: sh_link [%d] in section `%A' is incorrect"),
749 s->owner, s, elfsec);
753 elf_linked_to_section (s) = link;
758 /* Process section groups. */
759 if (num_group == (unsigned) -1)
762 for (i = 0; i < num_group; i++)
764 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
765 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
766 unsigned int n_elt = shdr->sh_size / 4;
769 if ((++idx)->shdr->bfd_section)
770 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
771 else if (idx->shdr->sh_type == SHT_RELA
772 || idx->shdr->sh_type == SHT_REL)
773 /* We won't include relocation sections in section groups in
774 output object files. We adjust the group section size here
775 so that relocatable link will work correctly when
776 relocation sections are in section group in input object
778 shdr->bfd_section->size -= 4;
781 /* There are some unknown sections in the group. */
782 (*_bfd_error_handler)
783 (_("%B: unknown [%d] section `%s' in group [%s]"),
785 (unsigned int) idx->shdr->sh_type,
786 bfd_elf_string_from_elf_section (abfd,
787 (elf_elfheader (abfd)
790 shdr->bfd_section->name);
798 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
800 return elf_next_in_group (sec) != NULL;
803 /* Make a BFD section from an ELF section. We store a pointer to the
804 BFD section in the bfd_section field of the header. */
807 _bfd_elf_make_section_from_shdr (bfd *abfd,
808 Elf_Internal_Shdr *hdr,
814 const struct elf_backend_data *bed;
816 if (hdr->bfd_section != NULL)
818 BFD_ASSERT (strcmp (name,
819 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
823 newsect = bfd_make_section_anyway (abfd, name);
827 hdr->bfd_section = newsect;
828 elf_section_data (newsect)->this_hdr = *hdr;
829 elf_section_data (newsect)->this_idx = shindex;
831 /* Always use the real type/flags. */
832 elf_section_type (newsect) = hdr->sh_type;
833 elf_section_flags (newsect) = hdr->sh_flags;
835 newsect->filepos = hdr->sh_offset;
837 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
838 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
839 || ! bfd_set_section_alignment (abfd, newsect,
840 bfd_log2 (hdr->sh_addralign)))
843 flags = SEC_NO_FLAGS;
844 if (hdr->sh_type != SHT_NOBITS)
845 flags |= SEC_HAS_CONTENTS;
846 if (hdr->sh_type == SHT_GROUP)
847 flags |= SEC_GROUP | SEC_EXCLUDE;
848 if ((hdr->sh_flags & SHF_ALLOC) != 0)
851 if (hdr->sh_type != SHT_NOBITS)
854 if ((hdr->sh_flags & SHF_WRITE) == 0)
855 flags |= SEC_READONLY;
856 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
858 else if ((flags & SEC_LOAD) != 0)
860 if ((hdr->sh_flags & SHF_MERGE) != 0)
863 newsect->entsize = hdr->sh_entsize;
864 if ((hdr->sh_flags & SHF_STRINGS) != 0)
865 flags |= SEC_STRINGS;
867 if (hdr->sh_flags & SHF_GROUP)
868 if (!setup_group (abfd, hdr, newsect))
870 if ((hdr->sh_flags & SHF_TLS) != 0)
871 flags |= SEC_THREAD_LOCAL;
873 if ((flags & SEC_ALLOC) == 0)
875 /* The debugging sections appear to be recognized only by name,
876 not any sort of flag. Their SEC_ALLOC bits are cleared. */
881 } debug_sections [] =
883 { STRING_COMMA_LEN ("debug") }, /* 'd' */
884 { NULL, 0 }, /* 'e' */
885 { NULL, 0 }, /* 'f' */
886 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
887 { NULL, 0 }, /* 'h' */
888 { NULL, 0 }, /* 'i' */
889 { NULL, 0 }, /* 'j' */
890 { NULL, 0 }, /* 'k' */
891 { STRING_COMMA_LEN ("line") }, /* 'l' */
892 { NULL, 0 }, /* 'm' */
893 { NULL, 0 }, /* 'n' */
894 { NULL, 0 }, /* 'o' */
895 { NULL, 0 }, /* 'p' */
896 { NULL, 0 }, /* 'q' */
897 { NULL, 0 }, /* 'r' */
898 { STRING_COMMA_LEN ("stab") }, /* 's' */
899 { NULL, 0 }, /* 't' */
900 { NULL, 0 }, /* 'u' */
901 { NULL, 0 }, /* 'v' */
902 { NULL, 0 }, /* 'w' */
903 { NULL, 0 }, /* 'x' */
904 { NULL, 0 }, /* 'y' */
905 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
910 int i = name [1] - 'd';
912 && i < (int) ARRAY_SIZE (debug_sections)
913 && debug_sections [i].name != NULL
914 && strncmp (&name [1], debug_sections [i].name,
915 debug_sections [i].len) == 0)
916 flags |= SEC_DEBUGGING;
920 /* As a GNU extension, if the name begins with .gnu.linkonce, we
921 only link a single copy of the section. This is used to support
922 g++. g++ will emit each template expansion in its own section.
923 The symbols will be defined as weak, so that multiple definitions
924 are permitted. The GNU linker extension is to actually discard
925 all but one of the sections. */
926 if (CONST_STRNEQ (name, ".gnu.linkonce")
927 && elf_next_in_group (newsect) == NULL)
928 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
930 bed = get_elf_backend_data (abfd);
931 if (bed->elf_backend_section_flags)
932 if (! bed->elf_backend_section_flags (&flags, hdr))
935 if (! bfd_set_section_flags (abfd, newsect, flags))
938 /* We do not parse the PT_NOTE segments as we are interested even in the
939 separate debug info files which may have the segments offsets corrupted.
940 PT_NOTEs from the core files are currently not parsed using BFD. */
941 if (hdr->sh_type == SHT_NOTE)
945 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
948 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
952 if ((flags & SEC_ALLOC) != 0)
954 Elf_Internal_Phdr *phdr;
955 unsigned int i, nload;
957 /* Some ELF linkers produce binaries with all the program header
958 p_paddr fields zero. If we have such a binary with more than
959 one PT_LOAD header, then leave the section lma equal to vma
960 so that we don't create sections with overlapping lma. */
961 phdr = elf_tdata (abfd)->phdr;
962 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
963 if (phdr->p_paddr != 0)
965 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
967 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
970 phdr = elf_tdata (abfd)->phdr;
971 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
973 /* This section is part of this segment if its file
974 offset plus size lies within the segment's memory
975 span and, if the section is loaded, the extent of the
976 loaded data lies within the extent of the segment.
978 Note - we used to check the p_paddr field as well, and
979 refuse to set the LMA if it was 0. This is wrong
980 though, as a perfectly valid initialised segment can
981 have a p_paddr of zero. Some architectures, eg ARM,
982 place special significance on the address 0 and
983 executables need to be able to have a segment which
984 covers this address. */
985 if (phdr->p_type == PT_LOAD
986 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
987 && (hdr->sh_offset + hdr->sh_size
988 <= phdr->p_offset + phdr->p_memsz)
989 && ((flags & SEC_LOAD) == 0
990 || (hdr->sh_offset + hdr->sh_size
991 <= phdr->p_offset + phdr->p_filesz)))
993 if ((flags & SEC_LOAD) == 0)
994 newsect->lma = (phdr->p_paddr
995 + hdr->sh_addr - phdr->p_vaddr);
997 /* We used to use the same adjustment for SEC_LOAD
998 sections, but that doesn't work if the segment
999 is packed with code from multiple VMAs.
1000 Instead we calculate the section LMA based on
1001 the segment LMA. It is assumed that the
1002 segment will contain sections with contiguous
1003 LMAs, even if the VMAs are not. */
1004 newsect->lma = (phdr->p_paddr
1005 + hdr->sh_offset - phdr->p_offset);
1007 /* With contiguous segments, we can't tell from file
1008 offsets whether a section with zero size should
1009 be placed at the end of one segment or the
1010 beginning of the next. Decide based on vaddr. */
1011 if (hdr->sh_addr >= phdr->p_vaddr
1012 && (hdr->sh_addr + hdr->sh_size
1013 <= phdr->p_vaddr + phdr->p_memsz))
1024 bfd_elf_find_section
1027 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1030 Helper functions for GDB to locate the string tables.
1031 Since BFD hides string tables from callers, GDB needs to use an
1032 internal hook to find them. Sun's .stabstr, in particular,
1033 isn't even pointed to by the .stab section, so ordinary
1034 mechanisms wouldn't work to find it, even if we had some.
1037 struct elf_internal_shdr *
1038 bfd_elf_find_section (bfd *abfd, char *name)
1040 Elf_Internal_Shdr **i_shdrp;
1045 i_shdrp = elf_elfsections (abfd);
1046 if (i_shdrp != NULL)
1048 shstrtab = bfd_elf_get_str_section (abfd,
1049 elf_elfheader (abfd)->e_shstrndx);
1050 if (shstrtab != NULL)
1052 max = elf_numsections (abfd);
1053 for (i = 1; i < max; i++)
1054 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1061 const char *const bfd_elf_section_type_names[] = {
1062 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1063 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1064 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1067 /* ELF relocs are against symbols. If we are producing relocatable
1068 output, and the reloc is against an external symbol, and nothing
1069 has given us any additional addend, the resulting reloc will also
1070 be against the same symbol. In such a case, we don't want to
1071 change anything about the way the reloc is handled, since it will
1072 all be done at final link time. Rather than put special case code
1073 into bfd_perform_relocation, all the reloc types use this howto
1074 function. It just short circuits the reloc if producing
1075 relocatable output against an external symbol. */
1077 bfd_reloc_status_type
1078 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1079 arelent *reloc_entry,
1081 void *data ATTRIBUTE_UNUSED,
1082 asection *input_section,
1084 char **error_message ATTRIBUTE_UNUSED)
1086 if (output_bfd != NULL
1087 && (symbol->flags & BSF_SECTION_SYM) == 0
1088 && (! reloc_entry->howto->partial_inplace
1089 || reloc_entry->addend == 0))
1091 reloc_entry->address += input_section->output_offset;
1092 return bfd_reloc_ok;
1095 return bfd_reloc_continue;
1098 /* Copy the program header and other data from one object module to
1102 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1104 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1105 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1108 BFD_ASSERT (!elf_flags_init (obfd)
1109 || (elf_elfheader (obfd)->e_flags
1110 == elf_elfheader (ibfd)->e_flags));
1112 elf_gp (obfd) = elf_gp (ibfd);
1113 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1114 elf_flags_init (obfd) = TRUE;
1116 /* Copy object attributes. */
1117 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1123 get_segment_type (unsigned int p_type)
1128 case PT_NULL: pt = "NULL"; break;
1129 case PT_LOAD: pt = "LOAD"; break;
1130 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1131 case PT_INTERP: pt = "INTERP"; break;
1132 case PT_NOTE: pt = "NOTE"; break;
1133 case PT_SHLIB: pt = "SHLIB"; break;
1134 case PT_PHDR: pt = "PHDR"; break;
1135 case PT_TLS: pt = "TLS"; break;
1136 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1137 case PT_GNU_STACK: pt = "STACK"; break;
1138 case PT_GNU_RELRO: pt = "RELRO"; break;
1139 default: pt = NULL; break;
1144 /* Print out the program headers. */
1147 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1150 Elf_Internal_Phdr *p;
1152 bfd_byte *dynbuf = NULL;
1154 p = elf_tdata (abfd)->phdr;
1159 fprintf (f, _("\nProgram Header:\n"));
1160 c = elf_elfheader (abfd)->e_phnum;
1161 for (i = 0; i < c; i++, p++)
1163 const char *pt = get_segment_type (p->p_type);
1168 sprintf (buf, "0x%lx", p->p_type);
1171 fprintf (f, "%8s off 0x", pt);
1172 bfd_fprintf_vma (abfd, f, p->p_offset);
1173 fprintf (f, " vaddr 0x");
1174 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1175 fprintf (f, " paddr 0x");
1176 bfd_fprintf_vma (abfd, f, p->p_paddr);
1177 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1178 fprintf (f, " filesz 0x");
1179 bfd_fprintf_vma (abfd, f, p->p_filesz);
1180 fprintf (f, " memsz 0x");
1181 bfd_fprintf_vma (abfd, f, p->p_memsz);
1182 fprintf (f, " flags %c%c%c",
1183 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1184 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1185 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1186 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1187 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1192 s = bfd_get_section_by_name (abfd, ".dynamic");
1195 unsigned int elfsec;
1196 unsigned long shlink;
1197 bfd_byte *extdyn, *extdynend;
1199 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1201 fprintf (f, _("\nDynamic Section:\n"));
1203 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1206 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1207 if (elfsec == SHN_BAD)
1209 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1211 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1212 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1215 extdynend = extdyn + s->size;
1216 for (; extdyn < extdynend; extdyn += extdynsize)
1218 Elf_Internal_Dyn dyn;
1219 const char *name = "";
1221 bfd_boolean stringp;
1222 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1224 (*swap_dyn_in) (abfd, extdyn, &dyn);
1226 if (dyn.d_tag == DT_NULL)
1233 if (bed->elf_backend_get_target_dtag)
1234 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1236 if (!strcmp (name, ""))
1238 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1243 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1244 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1245 case DT_PLTGOT: name = "PLTGOT"; break;
1246 case DT_HASH: name = "HASH"; break;
1247 case DT_STRTAB: name = "STRTAB"; break;
1248 case DT_SYMTAB: name = "SYMTAB"; break;
1249 case DT_RELA: name = "RELA"; break;
1250 case DT_RELASZ: name = "RELASZ"; break;
1251 case DT_RELAENT: name = "RELAENT"; break;
1252 case DT_STRSZ: name = "STRSZ"; break;
1253 case DT_SYMENT: name = "SYMENT"; break;
1254 case DT_INIT: name = "INIT"; break;
1255 case DT_FINI: name = "FINI"; break;
1256 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1257 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1258 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1259 case DT_REL: name = "REL"; break;
1260 case DT_RELSZ: name = "RELSZ"; break;
1261 case DT_RELENT: name = "RELENT"; break;
1262 case DT_PLTREL: name = "PLTREL"; break;
1263 case DT_DEBUG: name = "DEBUG"; break;
1264 case DT_TEXTREL: name = "TEXTREL"; break;
1265 case DT_JMPREL: name = "JMPREL"; break;
1266 case DT_BIND_NOW: name = "BIND_NOW"; break;
1267 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1268 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1269 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1270 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1271 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1272 case DT_FLAGS: name = "FLAGS"; break;
1273 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1274 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1275 case DT_CHECKSUM: name = "CHECKSUM"; break;
1276 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1277 case DT_MOVEENT: name = "MOVEENT"; break;
1278 case DT_MOVESZ: name = "MOVESZ"; break;
1279 case DT_FEATURE: name = "FEATURE"; break;
1280 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1281 case DT_SYMINSZ: name = "SYMINSZ"; break;
1282 case DT_SYMINENT: name = "SYMINENT"; break;
1283 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1284 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1285 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1286 case DT_PLTPAD: name = "PLTPAD"; break;
1287 case DT_MOVETAB: name = "MOVETAB"; break;
1288 case DT_SYMINFO: name = "SYMINFO"; break;
1289 case DT_RELACOUNT: name = "RELACOUNT"; break;
1290 case DT_RELCOUNT: name = "RELCOUNT"; break;
1291 case DT_FLAGS_1: name = "FLAGS_1"; break;
1292 case DT_VERSYM: name = "VERSYM"; break;
1293 case DT_VERDEF: name = "VERDEF"; break;
1294 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1295 case DT_VERNEED: name = "VERNEED"; break;
1296 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1297 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1298 case DT_USED: name = "USED"; break;
1299 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1300 case DT_GNU_HASH: name = "GNU_HASH"; break;
1303 fprintf (f, " %-20s ", name);
1307 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1312 unsigned int tagv = dyn.d_un.d_val;
1314 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1317 fprintf (f, "%s", string);
1326 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1327 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1329 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1333 if (elf_dynverdef (abfd) != 0)
1335 Elf_Internal_Verdef *t;
1337 fprintf (f, _("\nVersion definitions:\n"));
1338 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1340 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1341 t->vd_flags, t->vd_hash,
1342 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1343 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1345 Elf_Internal_Verdaux *a;
1348 for (a = t->vd_auxptr->vda_nextptr;
1352 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1358 if (elf_dynverref (abfd) != 0)
1360 Elf_Internal_Verneed *t;
1362 fprintf (f, _("\nVersion References:\n"));
1363 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1365 Elf_Internal_Vernaux *a;
1367 fprintf (f, _(" required from %s:\n"),
1368 t->vn_filename ? t->vn_filename : "<corrupt>");
1369 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1370 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1371 a->vna_flags, a->vna_other,
1372 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1384 /* Display ELF-specific fields of a symbol. */
1387 bfd_elf_print_symbol (bfd *abfd,
1390 bfd_print_symbol_type how)
1395 case bfd_print_symbol_name:
1396 fprintf (file, "%s", symbol->name);
1398 case bfd_print_symbol_more:
1399 fprintf (file, "elf ");
1400 bfd_fprintf_vma (abfd, file, symbol->value);
1401 fprintf (file, " %lx", (unsigned long) symbol->flags);
1403 case bfd_print_symbol_all:
1405 const char *section_name;
1406 const char *name = NULL;
1407 const struct elf_backend_data *bed;
1408 unsigned char st_other;
1411 section_name = symbol->section ? symbol->section->name : "(*none*)";
1413 bed = get_elf_backend_data (abfd);
1414 if (bed->elf_backend_print_symbol_all)
1415 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1419 name = symbol->name;
1420 bfd_print_symbol_vandf (abfd, file, symbol);
1423 fprintf (file, " %s\t", section_name);
1424 /* Print the "other" value for a symbol. For common symbols,
1425 we've already printed the size; now print the alignment.
1426 For other symbols, we have no specified alignment, and
1427 we've printed the address; now print the size. */
1428 if (symbol->section && bfd_is_com_section (symbol->section))
1429 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1431 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1432 bfd_fprintf_vma (abfd, file, val);
1434 /* If we have version information, print it. */
1435 if (elf_tdata (abfd)->dynversym_section != 0
1436 && (elf_tdata (abfd)->dynverdef_section != 0
1437 || elf_tdata (abfd)->dynverref_section != 0))
1439 unsigned int vernum;
1440 const char *version_string;
1442 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1445 version_string = "";
1446 else if (vernum == 1)
1447 version_string = "Base";
1448 else if (vernum <= elf_tdata (abfd)->cverdefs)
1450 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1453 Elf_Internal_Verneed *t;
1455 version_string = "";
1456 for (t = elf_tdata (abfd)->verref;
1460 Elf_Internal_Vernaux *a;
1462 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1464 if (a->vna_other == vernum)
1466 version_string = a->vna_nodename;
1473 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1474 fprintf (file, " %-11s", version_string);
1479 fprintf (file, " (%s)", version_string);
1480 for (i = 10 - strlen (version_string); i > 0; --i)
1485 /* If the st_other field is not zero, print it. */
1486 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1491 case STV_INTERNAL: fprintf (file, " .internal"); break;
1492 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1493 case STV_PROTECTED: fprintf (file, " .protected"); break;
1495 /* Some other non-defined flags are also present, so print
1497 fprintf (file, " 0x%02x", (unsigned int) st_other);
1500 fprintf (file, " %s", name);
1506 /* Allocate an ELF string table--force the first byte to be zero. */
1508 struct bfd_strtab_hash *
1509 _bfd_elf_stringtab_init (void)
1511 struct bfd_strtab_hash *ret;
1513 ret = _bfd_stringtab_init ();
1518 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1519 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1520 if (loc == (bfd_size_type) -1)
1522 _bfd_stringtab_free (ret);
1529 /* ELF .o/exec file reading */
1531 /* Create a new bfd section from an ELF section header. */
1534 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1536 Elf_Internal_Shdr *hdr;
1537 Elf_Internal_Ehdr *ehdr;
1538 const struct elf_backend_data *bed;
1541 if (shindex >= elf_numsections (abfd))
1544 hdr = elf_elfsections (abfd)[shindex];
1545 ehdr = elf_elfheader (abfd);
1546 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1551 bed = get_elf_backend_data (abfd);
1552 switch (hdr->sh_type)
1555 /* Inactive section. Throw it away. */
1558 case SHT_PROGBITS: /* Normal section with contents. */
1559 case SHT_NOBITS: /* .bss section. */
1560 case SHT_HASH: /* .hash section. */
1561 case SHT_NOTE: /* .note section. */
1562 case SHT_INIT_ARRAY: /* .init_array section. */
1563 case SHT_FINI_ARRAY: /* .fini_array section. */
1564 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1565 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1566 case SHT_GNU_HASH: /* .gnu.hash section. */
1567 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1569 case SHT_DYNAMIC: /* Dynamic linking information. */
1570 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1572 if (hdr->sh_link > elf_numsections (abfd)
1573 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1575 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1577 Elf_Internal_Shdr *dynsymhdr;
1579 /* The shared libraries distributed with hpux11 have a bogus
1580 sh_link field for the ".dynamic" section. Find the
1581 string table for the ".dynsym" section instead. */
1582 if (elf_dynsymtab (abfd) != 0)
1584 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1585 hdr->sh_link = dynsymhdr->sh_link;
1589 unsigned int i, num_sec;
1591 num_sec = elf_numsections (abfd);
1592 for (i = 1; i < num_sec; i++)
1594 dynsymhdr = elf_elfsections (abfd)[i];
1595 if (dynsymhdr->sh_type == SHT_DYNSYM)
1597 hdr->sh_link = dynsymhdr->sh_link;
1605 case SHT_SYMTAB: /* A symbol table */
1606 if (elf_onesymtab (abfd) == shindex)
1609 if (hdr->sh_entsize != bed->s->sizeof_sym)
1611 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1613 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1614 elf_onesymtab (abfd) = shindex;
1615 elf_tdata (abfd)->symtab_hdr = *hdr;
1616 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1617 abfd->flags |= HAS_SYMS;
1619 /* Sometimes a shared object will map in the symbol table. If
1620 SHF_ALLOC is set, and this is a shared object, then we also
1621 treat this section as a BFD section. We can not base the
1622 decision purely on SHF_ALLOC, because that flag is sometimes
1623 set in a relocatable object file, which would confuse the
1625 if ((hdr->sh_flags & SHF_ALLOC) != 0
1626 && (abfd->flags & DYNAMIC) != 0
1627 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1631 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1632 can't read symbols without that section loaded as well. It
1633 is most likely specified by the next section header. */
1634 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1636 unsigned int i, num_sec;
1638 num_sec = elf_numsections (abfd);
1639 for (i = shindex + 1; i < num_sec; i++)
1641 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1642 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1643 && hdr2->sh_link == shindex)
1647 for (i = 1; i < shindex; i++)
1649 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1650 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1651 && hdr2->sh_link == shindex)
1655 return bfd_section_from_shdr (abfd, i);
1659 case SHT_DYNSYM: /* A dynamic symbol table */
1660 if (elf_dynsymtab (abfd) == shindex)
1663 if (hdr->sh_entsize != bed->s->sizeof_sym)
1665 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1666 elf_dynsymtab (abfd) = shindex;
1667 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1668 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1669 abfd->flags |= HAS_SYMS;
1671 /* Besides being a symbol table, we also treat this as a regular
1672 section, so that objcopy can handle it. */
1673 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1675 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1676 if (elf_symtab_shndx (abfd) == shindex)
1679 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1680 elf_symtab_shndx (abfd) = shindex;
1681 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1682 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1685 case SHT_STRTAB: /* A string table */
1686 if (hdr->bfd_section != NULL)
1688 if (ehdr->e_shstrndx == shindex)
1690 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1691 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1694 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1697 elf_tdata (abfd)->strtab_hdr = *hdr;
1698 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1701 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1704 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1705 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1706 elf_elfsections (abfd)[shindex] = hdr;
1707 /* We also treat this as a regular section, so that objcopy
1709 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1713 /* If the string table isn't one of the above, then treat it as a
1714 regular section. We need to scan all the headers to be sure,
1715 just in case this strtab section appeared before the above. */
1716 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1718 unsigned int i, num_sec;
1720 num_sec = elf_numsections (abfd);
1721 for (i = 1; i < num_sec; i++)
1723 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1724 if (hdr2->sh_link == shindex)
1726 /* Prevent endless recursion on broken objects. */
1729 if (! bfd_section_from_shdr (abfd, i))
1731 if (elf_onesymtab (abfd) == i)
1733 if (elf_dynsymtab (abfd) == i)
1734 goto dynsymtab_strtab;
1738 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1742 /* *These* do a lot of work -- but build no sections! */
1744 asection *target_sect;
1745 Elf_Internal_Shdr *hdr2;
1746 unsigned int num_sec = elf_numsections (abfd);
1749 != (bfd_size_type) (hdr->sh_type == SHT_REL
1750 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1753 /* Check for a bogus link to avoid crashing. */
1754 if (hdr->sh_link >= num_sec)
1756 ((*_bfd_error_handler)
1757 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1758 abfd, hdr->sh_link, name, shindex));
1759 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1763 /* For some incomprehensible reason Oracle distributes
1764 libraries for Solaris in which some of the objects have
1765 bogus sh_link fields. It would be nice if we could just
1766 reject them, but, unfortunately, some people need to use
1767 them. We scan through the section headers; if we find only
1768 one suitable symbol table, we clobber the sh_link to point
1769 to it. I hope this doesn't break anything. */
1770 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1771 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1777 for (scan = 1; scan < num_sec; scan++)
1779 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1780 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1791 hdr->sh_link = found;
1794 /* Get the symbol table. */
1795 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1796 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1797 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1800 /* If this reloc section does not use the main symbol table we
1801 don't treat it as a reloc section. BFD can't adequately
1802 represent such a section, so at least for now, we don't
1803 try. We just present it as a normal section. We also
1804 can't use it as a reloc section if it points to the null
1805 section, an invalid section, or another reloc section. */
1806 if (hdr->sh_link != elf_onesymtab (abfd)
1807 || hdr->sh_info == SHN_UNDEF
1808 || hdr->sh_info >= num_sec
1809 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1810 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1811 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1814 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1816 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1817 if (target_sect == NULL)
1820 if ((target_sect->flags & SEC_RELOC) == 0
1821 || target_sect->reloc_count == 0)
1822 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1826 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1827 amt = sizeof (*hdr2);
1828 hdr2 = bfd_alloc (abfd, amt);
1831 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1834 elf_elfsections (abfd)[shindex] = hdr2;
1835 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1836 target_sect->flags |= SEC_RELOC;
1837 target_sect->relocation = NULL;
1838 target_sect->rel_filepos = hdr->sh_offset;
1839 /* In the section to which the relocations apply, mark whether
1840 its relocations are of the REL or RELA variety. */
1841 if (hdr->sh_size != 0)
1842 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1843 abfd->flags |= HAS_RELOC;
1847 case SHT_GNU_verdef:
1848 elf_dynverdef (abfd) = shindex;
1849 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1850 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1852 case SHT_GNU_versym:
1853 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1855 elf_dynversym (abfd) = shindex;
1856 elf_tdata (abfd)->dynversym_hdr = *hdr;
1857 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1859 case SHT_GNU_verneed:
1860 elf_dynverref (abfd) = shindex;
1861 elf_tdata (abfd)->dynverref_hdr = *hdr;
1862 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1868 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1870 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1872 if (hdr->contents != NULL)
1874 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1875 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1878 if (idx->flags & GRP_COMDAT)
1879 hdr->bfd_section->flags
1880 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1882 /* We try to keep the same section order as it comes in. */
1884 while (--n_elt != 0)
1888 if (idx->shdr != NULL
1889 && (s = idx->shdr->bfd_section) != NULL
1890 && elf_next_in_group (s) != NULL)
1892 elf_next_in_group (hdr->bfd_section) = s;
1900 /* Possibly an attributes section. */
1901 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1902 || hdr->sh_type == bed->obj_attrs_section_type)
1904 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1906 _bfd_elf_parse_attributes (abfd, hdr);
1910 /* Check for any processor-specific section types. */
1911 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1914 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1916 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1917 /* FIXME: How to properly handle allocated section reserved
1918 for applications? */
1919 (*_bfd_error_handler)
1920 (_("%B: don't know how to handle allocated, application "
1921 "specific section `%s' [0x%8x]"),
1922 abfd, name, hdr->sh_type);
1924 /* Allow sections reserved for applications. */
1925 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1928 else if (hdr->sh_type >= SHT_LOPROC
1929 && hdr->sh_type <= SHT_HIPROC)
1930 /* FIXME: We should handle this section. */
1931 (*_bfd_error_handler)
1932 (_("%B: don't know how to handle processor specific section "
1934 abfd, name, hdr->sh_type);
1935 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1937 /* Unrecognised OS-specific sections. */
1938 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1939 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1940 required to correctly process the section and the file should
1941 be rejected with an error message. */
1942 (*_bfd_error_handler)
1943 (_("%B: don't know how to handle OS specific section "
1945 abfd, name, hdr->sh_type);
1947 /* Otherwise it should be processed. */
1948 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1951 /* FIXME: We should handle this section. */
1952 (*_bfd_error_handler)
1953 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1954 abfd, name, hdr->sh_type);
1962 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1963 Return SEC for sections that have no elf section, and NULL on error. */
1966 bfd_section_from_r_symndx (bfd *abfd,
1967 struct sym_sec_cache *cache,
1969 unsigned long r_symndx)
1971 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1974 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1976 Elf_Internal_Shdr *symtab_hdr;
1977 unsigned char esym[sizeof (Elf64_External_Sym)];
1978 Elf_External_Sym_Shndx eshndx;
1979 Elf_Internal_Sym isym;
1981 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1982 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1983 &isym, esym, &eshndx) == NULL)
1986 if (cache->abfd != abfd)
1988 memset (cache->indx, -1, sizeof (cache->indx));
1991 cache->indx[ent] = r_symndx;
1992 cache->shndx[ent] = isym.st_shndx;
1995 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
2002 /* Given an ELF section number, retrieve the corresponding BFD
2006 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2008 if (index >= elf_numsections (abfd))
2010 return elf_elfsections (abfd)[index]->bfd_section;
2013 static const struct bfd_elf_special_section special_sections_b[] =
2015 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2016 { NULL, 0, 0, 0, 0 }
2019 static const struct bfd_elf_special_section special_sections_c[] =
2021 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2022 { NULL, 0, 0, 0, 0 }
2025 static const struct bfd_elf_special_section special_sections_d[] =
2027 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2028 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2029 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2030 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2031 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2032 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2033 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2034 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2035 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2036 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2037 { NULL, 0, 0, 0, 0 }
2040 static const struct bfd_elf_special_section special_sections_f[] =
2042 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2043 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2044 { NULL, 0, 0, 0, 0 }
2047 static const struct bfd_elf_special_section special_sections_g[] =
2049 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2050 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2051 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2052 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2053 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2054 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2055 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2056 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2057 { NULL, 0, 0, 0, 0 }
2060 static const struct bfd_elf_special_section special_sections_h[] =
2062 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2063 { NULL, 0, 0, 0, 0 }
2066 static const struct bfd_elf_special_section special_sections_i[] =
2068 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2069 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2070 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2071 { NULL, 0, 0, 0, 0 }
2074 static const struct bfd_elf_special_section special_sections_l[] =
2076 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2077 { NULL, 0, 0, 0, 0 }
2080 static const struct bfd_elf_special_section special_sections_n[] =
2082 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2083 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2084 { NULL, 0, 0, 0, 0 }
2087 static const struct bfd_elf_special_section special_sections_p[] =
2089 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2090 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2091 { NULL, 0, 0, 0, 0 }
2094 static const struct bfd_elf_special_section special_sections_r[] =
2096 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2097 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2098 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2099 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2100 { NULL, 0, 0, 0, 0 }
2103 static const struct bfd_elf_special_section special_sections_s[] =
2105 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2106 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2107 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2108 /* See struct bfd_elf_special_section declaration for the semantics of
2109 this special case where .prefix_length != strlen (.prefix). */
2110 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2111 { NULL, 0, 0, 0, 0 }
2114 static const struct bfd_elf_special_section special_sections_t[] =
2116 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2117 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2118 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2119 { NULL, 0, 0, 0, 0 }
2122 static const struct bfd_elf_special_section special_sections_z[] =
2124 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2125 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2126 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2127 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2128 { NULL, 0, 0, 0, 0 }
2131 static const struct bfd_elf_special_section *special_sections[] =
2133 special_sections_b, /* 'b' */
2134 special_sections_c, /* 'c' */
2135 special_sections_d, /* 'd' */
2137 special_sections_f, /* 'f' */
2138 special_sections_g, /* 'g' */
2139 special_sections_h, /* 'h' */
2140 special_sections_i, /* 'i' */
2143 special_sections_l, /* 'l' */
2145 special_sections_n, /* 'n' */
2147 special_sections_p, /* 'p' */
2149 special_sections_r, /* 'r' */
2150 special_sections_s, /* 's' */
2151 special_sections_t, /* 't' */
2157 special_sections_z /* 'z' */
2160 const struct bfd_elf_special_section *
2161 _bfd_elf_get_special_section (const char *name,
2162 const struct bfd_elf_special_section *spec,
2168 len = strlen (name);
2170 for (i = 0; spec[i].prefix != NULL; i++)
2173 int prefix_len = spec[i].prefix_length;
2175 if (len < prefix_len)
2177 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2180 suffix_len = spec[i].suffix_length;
2181 if (suffix_len <= 0)
2183 if (name[prefix_len] != 0)
2185 if (suffix_len == 0)
2187 if (name[prefix_len] != '.'
2188 && (suffix_len == -2
2189 || (rela && spec[i].type == SHT_REL)))
2195 if (len < prefix_len + suffix_len)
2197 if (memcmp (name + len - suffix_len,
2198 spec[i].prefix + prefix_len,
2208 const struct bfd_elf_special_section *
2209 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2212 const struct bfd_elf_special_section *spec;
2213 const struct elf_backend_data *bed;
2215 /* See if this is one of the special sections. */
2216 if (sec->name == NULL)
2219 bed = get_elf_backend_data (abfd);
2220 spec = bed->special_sections;
2223 spec = _bfd_elf_get_special_section (sec->name,
2224 bed->special_sections,
2230 if (sec->name[0] != '.')
2233 i = sec->name[1] - 'b';
2234 if (i < 0 || i > 'z' - 'b')
2237 spec = special_sections[i];
2242 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2246 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2248 struct bfd_elf_section_data *sdata;
2249 const struct elf_backend_data *bed;
2250 const struct bfd_elf_special_section *ssect;
2252 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2255 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2258 sec->used_by_bfd = sdata;
2261 /* Indicate whether or not this section should use RELA relocations. */
2262 bed = get_elf_backend_data (abfd);
2263 sec->use_rela_p = bed->default_use_rela_p;
2265 /* When we read a file, we don't need to set ELF section type and
2266 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2267 anyway. We will set ELF section type and flags for all linker
2268 created sections. If user specifies BFD section flags, we will
2269 set ELF section type and flags based on BFD section flags in
2270 elf_fake_sections. */
2271 if ((!sec->flags && abfd->direction != read_direction)
2272 || (sec->flags & SEC_LINKER_CREATED) != 0)
2274 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2277 elf_section_type (sec) = ssect->type;
2278 elf_section_flags (sec) = ssect->attr;
2282 return _bfd_generic_new_section_hook (abfd, sec);
2285 /* Create a new bfd section from an ELF program header.
2287 Since program segments have no names, we generate a synthetic name
2288 of the form segment<NUM>, where NUM is generally the index in the
2289 program header table. For segments that are split (see below) we
2290 generate the names segment<NUM>a and segment<NUM>b.
2292 Note that some program segments may have a file size that is different than
2293 (less than) the memory size. All this means is that at execution the
2294 system must allocate the amount of memory specified by the memory size,
2295 but only initialize it with the first "file size" bytes read from the
2296 file. This would occur for example, with program segments consisting
2297 of combined data+bss.
2299 To handle the above situation, this routine generates TWO bfd sections
2300 for the single program segment. The first has the length specified by
2301 the file size of the segment, and the second has the length specified
2302 by the difference between the two sizes. In effect, the segment is split
2303 into its initialized and uninitialized parts.
2308 _bfd_elf_make_section_from_phdr (bfd *abfd,
2309 Elf_Internal_Phdr *hdr,
2311 const char *typename)
2319 split = ((hdr->p_memsz > 0)
2320 && (hdr->p_filesz > 0)
2321 && (hdr->p_memsz > hdr->p_filesz));
2323 if (hdr->p_filesz > 0)
2325 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2326 len = strlen (namebuf) + 1;
2327 name = bfd_alloc (abfd, len);
2330 memcpy (name, namebuf, len);
2331 newsect = bfd_make_section (abfd, name);
2332 if (newsect == NULL)
2334 newsect->vma = hdr->p_vaddr;
2335 newsect->lma = hdr->p_paddr;
2336 newsect->size = hdr->p_filesz;
2337 newsect->filepos = hdr->p_offset;
2338 newsect->flags |= SEC_HAS_CONTENTS;
2339 newsect->alignment_power = bfd_log2 (hdr->p_align);
2340 if (hdr->p_type == PT_LOAD)
2342 newsect->flags |= SEC_ALLOC;
2343 newsect->flags |= SEC_LOAD;
2344 if (hdr->p_flags & PF_X)
2346 /* FIXME: all we known is that it has execute PERMISSION,
2348 newsect->flags |= SEC_CODE;
2351 if (!(hdr->p_flags & PF_W))
2353 newsect->flags |= SEC_READONLY;
2357 if (hdr->p_memsz > hdr->p_filesz)
2361 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
2362 len = strlen (namebuf) + 1;
2363 name = bfd_alloc (abfd, len);
2366 memcpy (name, namebuf, len);
2367 newsect = bfd_make_section (abfd, name);
2368 if (newsect == NULL)
2370 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2371 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2372 newsect->size = hdr->p_memsz - hdr->p_filesz;
2373 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2374 align = newsect->vma & -newsect->vma;
2375 if (align == 0 || align > hdr->p_align)
2376 align = hdr->p_align;
2377 newsect->alignment_power = bfd_log2 (align);
2378 if (hdr->p_type == PT_LOAD)
2380 /* Hack for gdb. Segments that have not been modified do
2381 not have their contents written to a core file, on the
2382 assumption that a debugger can find the contents in the
2383 executable. We flag this case by setting the fake
2384 section size to zero. Note that "real" bss sections will
2385 always have their contents dumped to the core file. */
2386 if (bfd_get_format (abfd) == bfd_core)
2388 newsect->flags |= SEC_ALLOC;
2389 if (hdr->p_flags & PF_X)
2390 newsect->flags |= SEC_CODE;
2392 if (!(hdr->p_flags & PF_W))
2393 newsect->flags |= SEC_READONLY;
2400 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2402 const struct elf_backend_data *bed;
2404 switch (hdr->p_type)
2407 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2410 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2413 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2416 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2419 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2421 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2426 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2429 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2431 case PT_GNU_EH_FRAME:
2432 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2436 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2439 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2442 /* Check for any processor-specific program segment types. */
2443 bed = get_elf_backend_data (abfd);
2444 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2448 /* Initialize REL_HDR, the section-header for new section, containing
2449 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2450 relocations; otherwise, we use REL relocations. */
2453 _bfd_elf_init_reloc_shdr (bfd *abfd,
2454 Elf_Internal_Shdr *rel_hdr,
2456 bfd_boolean use_rela_p)
2459 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2460 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2462 name = bfd_alloc (abfd, amt);
2465 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2467 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2469 if (rel_hdr->sh_name == (unsigned int) -1)
2471 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2472 rel_hdr->sh_entsize = (use_rela_p
2473 ? bed->s->sizeof_rela
2474 : bed->s->sizeof_rel);
2475 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2476 rel_hdr->sh_flags = 0;
2477 rel_hdr->sh_addr = 0;
2478 rel_hdr->sh_size = 0;
2479 rel_hdr->sh_offset = 0;
2484 /* Set up an ELF internal section header for a section. */
2487 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2489 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2490 bfd_boolean *failedptr = failedptrarg;
2491 Elf_Internal_Shdr *this_hdr;
2492 unsigned int sh_type;
2496 /* We already failed; just get out of the bfd_map_over_sections
2501 this_hdr = &elf_section_data (asect)->this_hdr;
2503 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2504 asect->name, FALSE);
2505 if (this_hdr->sh_name == (unsigned int) -1)
2511 /* Don't clear sh_flags. Assembler may set additional bits. */
2513 if ((asect->flags & SEC_ALLOC) != 0
2514 || asect->user_set_vma)
2515 this_hdr->sh_addr = asect->vma;
2517 this_hdr->sh_addr = 0;
2519 this_hdr->sh_offset = 0;
2520 this_hdr->sh_size = asect->size;
2521 this_hdr->sh_link = 0;
2522 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2523 /* The sh_entsize and sh_info fields may have been set already by
2524 copy_private_section_data. */
2526 this_hdr->bfd_section = asect;
2527 this_hdr->contents = NULL;
2529 /* If the section type is unspecified, we set it based on
2531 if ((asect->flags & SEC_GROUP) != 0)
2532 sh_type = SHT_GROUP;
2533 else if ((asect->flags & SEC_ALLOC) != 0
2534 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2535 || (asect->flags & SEC_NEVER_LOAD) != 0))
2536 sh_type = SHT_NOBITS;
2538 sh_type = SHT_PROGBITS;
2540 if (this_hdr->sh_type == SHT_NULL)
2541 this_hdr->sh_type = sh_type;
2542 else if (this_hdr->sh_type == SHT_NOBITS
2543 && sh_type == SHT_PROGBITS
2544 && (asect->flags & SEC_ALLOC) != 0)
2546 /* Warn if we are changing a NOBITS section to PROGBITS, but
2547 allow the link to proceed. This can happen when users link
2548 non-bss input sections to bss output sections, or emit data
2549 to a bss output section via a linker script. */
2550 (*_bfd_error_handler)
2551 (_("warning: section `%A' type changed to PROGBITS"), asect);
2552 this_hdr->sh_type = sh_type;
2555 switch (this_hdr->sh_type)
2561 case SHT_INIT_ARRAY:
2562 case SHT_FINI_ARRAY:
2563 case SHT_PREINIT_ARRAY:
2570 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2574 this_hdr->sh_entsize = bed->s->sizeof_sym;
2578 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2582 if (get_elf_backend_data (abfd)->may_use_rela_p)
2583 this_hdr->sh_entsize = bed->s->sizeof_rela;
2587 if (get_elf_backend_data (abfd)->may_use_rel_p)
2588 this_hdr->sh_entsize = bed->s->sizeof_rel;
2591 case SHT_GNU_versym:
2592 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2595 case SHT_GNU_verdef:
2596 this_hdr->sh_entsize = 0;
2597 /* objcopy or strip will copy over sh_info, but may not set
2598 cverdefs. The linker will set cverdefs, but sh_info will be
2600 if (this_hdr->sh_info == 0)
2601 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2603 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2604 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2607 case SHT_GNU_verneed:
2608 this_hdr->sh_entsize = 0;
2609 /* objcopy or strip will copy over sh_info, but may not set
2610 cverrefs. The linker will set cverrefs, but sh_info will be
2612 if (this_hdr->sh_info == 0)
2613 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2615 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2616 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2620 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2624 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2628 if ((asect->flags & SEC_ALLOC) != 0)
2629 this_hdr->sh_flags |= SHF_ALLOC;
2630 if ((asect->flags & SEC_READONLY) == 0)
2631 this_hdr->sh_flags |= SHF_WRITE;
2632 if ((asect->flags & SEC_CODE) != 0)
2633 this_hdr->sh_flags |= SHF_EXECINSTR;
2634 if ((asect->flags & SEC_MERGE) != 0)
2636 this_hdr->sh_flags |= SHF_MERGE;
2637 this_hdr->sh_entsize = asect->entsize;
2638 if ((asect->flags & SEC_STRINGS) != 0)
2639 this_hdr->sh_flags |= SHF_STRINGS;
2641 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2642 this_hdr->sh_flags |= SHF_GROUP;
2643 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2645 this_hdr->sh_flags |= SHF_TLS;
2646 if (asect->size == 0
2647 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2649 struct bfd_link_order *o = asect->map_tail.link_order;
2651 this_hdr->sh_size = 0;
2654 this_hdr->sh_size = o->offset + o->size;
2655 if (this_hdr->sh_size != 0)
2656 this_hdr->sh_type = SHT_NOBITS;
2661 /* Check for processor-specific section types. */
2662 sh_type = this_hdr->sh_type;
2663 if (bed->elf_backend_fake_sections
2664 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2667 if (sh_type == SHT_NOBITS && asect->size != 0)
2669 /* Don't change the header type from NOBITS if we are being
2670 called for objcopy --only-keep-debug. */
2671 this_hdr->sh_type = sh_type;
2674 /* If the section has relocs, set up a section header for the
2675 SHT_REL[A] section. If two relocation sections are required for
2676 this section, it is up to the processor-specific back-end to
2677 create the other. */
2678 if ((asect->flags & SEC_RELOC) != 0
2679 && !_bfd_elf_init_reloc_shdr (abfd,
2680 &elf_section_data (asect)->rel_hdr,
2686 /* Fill in the contents of a SHT_GROUP section. Called from
2687 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2688 when ELF targets use the generic linker, ld. Called for ld -r
2689 from bfd_elf_final_link. */
2692 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2694 bfd_boolean *failedptr = failedptrarg;
2695 asection *elt, *first;
2699 /* Ignore linker created group section. See elfNN_ia64_object_p in
2701 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2705 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2707 unsigned long symindx = 0;
2709 /* elf_group_id will have been set up by objcopy and the
2711 if (elf_group_id (sec) != NULL)
2712 symindx = elf_group_id (sec)->udata.i;
2716 /* If called from the assembler, swap_out_syms will have set up
2717 elf_section_syms. */
2718 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2719 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2721 elf_section_data (sec)->this_hdr.sh_info = symindx;
2723 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2725 /* The ELF backend linker sets sh_info to -2 when the group
2726 signature symbol is global, and thus the index can't be
2727 set until all local symbols are output. */
2728 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2729 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2730 unsigned long symndx = sec_data->this_hdr.sh_info;
2731 unsigned long extsymoff = 0;
2732 struct elf_link_hash_entry *h;
2734 if (!elf_bad_symtab (igroup->owner))
2736 Elf_Internal_Shdr *symtab_hdr;
2738 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2739 extsymoff = symtab_hdr->sh_info;
2741 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2742 while (h->root.type == bfd_link_hash_indirect
2743 || h->root.type == bfd_link_hash_warning)
2744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2746 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2749 /* The contents won't be allocated for "ld -r" or objcopy. */
2751 if (sec->contents == NULL)
2754 sec->contents = bfd_alloc (abfd, sec->size);
2756 /* Arrange for the section to be written out. */
2757 elf_section_data (sec)->this_hdr.contents = sec->contents;
2758 if (sec->contents == NULL)
2765 loc = sec->contents + sec->size;
2767 /* Get the pointer to the first section in the group that gas
2768 squirreled away here. objcopy arranges for this to be set to the
2769 start of the input section group. */
2770 first = elt = elf_next_in_group (sec);
2772 /* First element is a flag word. Rest of section is elf section
2773 indices for all the sections of the group. Write them backwards
2774 just to keep the group in the same order as given in .section
2775 directives, not that it matters. */
2782 if (! elf_discarded_section (s))
2786 s = s->output_section;
2789 idx = elf_section_data (s)->this_idx;
2790 H_PUT_32 (abfd, idx, loc);
2792 elt = elf_next_in_group (elt);
2797 if ((loc -= 4) != sec->contents)
2800 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2803 /* Assign all ELF section numbers. The dummy first section is handled here
2804 too. The link/info pointers for the standard section types are filled
2805 in here too, while we're at it. */
2808 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2810 struct elf_obj_tdata *t = elf_tdata (abfd);
2812 unsigned int section_number, secn;
2813 Elf_Internal_Shdr **i_shdrp;
2814 struct bfd_elf_section_data *d;
2818 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2820 /* SHT_GROUP sections are in relocatable files only. */
2821 if (link_info == NULL || link_info->relocatable)
2823 /* Put SHT_GROUP sections first. */
2824 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2826 d = elf_section_data (sec);
2828 if (d->this_hdr.sh_type == SHT_GROUP)
2830 if (sec->flags & SEC_LINKER_CREATED)
2832 /* Remove the linker created SHT_GROUP sections. */
2833 bfd_section_list_remove (abfd, sec);
2834 abfd->section_count--;
2837 d->this_idx = section_number++;
2842 for (sec = abfd->sections; sec; sec = sec->next)
2844 d = elf_section_data (sec);
2846 if (d->this_hdr.sh_type != SHT_GROUP)
2847 d->this_idx = section_number++;
2848 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2849 if ((sec->flags & SEC_RELOC) == 0)
2853 d->rel_idx = section_number++;
2854 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2859 d->rel_idx2 = section_number++;
2860 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2866 t->shstrtab_section = section_number++;
2867 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2868 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2870 if (bfd_get_symcount (abfd) > 0)
2872 t->symtab_section = section_number++;
2873 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2874 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2876 t->symtab_shndx_section = section_number++;
2877 t->symtab_shndx_hdr.sh_name
2878 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2879 ".symtab_shndx", FALSE);
2880 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2883 t->strtab_section = section_number++;
2884 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2887 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2888 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2890 elf_numsections (abfd) = section_number;
2891 elf_elfheader (abfd)->e_shnum = section_number;
2893 /* Set up the list of section header pointers, in agreement with the
2895 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2896 if (i_shdrp == NULL)
2899 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2900 if (i_shdrp[0] == NULL)
2902 bfd_release (abfd, i_shdrp);
2906 elf_elfsections (abfd) = i_shdrp;
2908 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2909 if (bfd_get_symcount (abfd) > 0)
2911 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2912 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2914 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2915 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2917 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2918 t->symtab_hdr.sh_link = t->strtab_section;
2921 for (sec = abfd->sections; sec; sec = sec->next)
2923 struct bfd_elf_section_data *d = elf_section_data (sec);
2927 i_shdrp[d->this_idx] = &d->this_hdr;
2928 if (d->rel_idx != 0)
2929 i_shdrp[d->rel_idx] = &d->rel_hdr;
2930 if (d->rel_idx2 != 0)
2931 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2933 /* Fill in the sh_link and sh_info fields while we're at it. */
2935 /* sh_link of a reloc section is the section index of the symbol
2936 table. sh_info is the section index of the section to which
2937 the relocation entries apply. */
2938 if (d->rel_idx != 0)
2940 d->rel_hdr.sh_link = t->symtab_section;
2941 d->rel_hdr.sh_info = d->this_idx;
2943 if (d->rel_idx2 != 0)
2945 d->rel_hdr2->sh_link = t->symtab_section;
2946 d->rel_hdr2->sh_info = d->this_idx;
2949 /* We need to set up sh_link for SHF_LINK_ORDER. */
2950 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2952 s = elf_linked_to_section (sec);
2955 /* elf_linked_to_section points to the input section. */
2956 if (link_info != NULL)
2958 /* Check discarded linkonce section. */
2959 if (elf_discarded_section (s))
2962 (*_bfd_error_handler)
2963 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2964 abfd, d->this_hdr.bfd_section,
2966 /* Point to the kept section if it has the same
2967 size as the discarded one. */
2968 kept = _bfd_elf_check_kept_section (s, link_info);
2971 bfd_set_error (bfd_error_bad_value);
2977 s = s->output_section;
2978 BFD_ASSERT (s != NULL);
2982 /* Handle objcopy. */
2983 if (s->output_section == NULL)
2985 (*_bfd_error_handler)
2986 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2987 abfd, d->this_hdr.bfd_section, s, s->owner);
2988 bfd_set_error (bfd_error_bad_value);
2991 s = s->output_section;
2993 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2998 The Intel C compiler generates SHT_IA_64_UNWIND with
2999 SHF_LINK_ORDER. But it doesn't set the sh_link or
3000 sh_info fields. Hence we could get the situation
3002 const struct elf_backend_data *bed
3003 = get_elf_backend_data (abfd);
3004 if (bed->link_order_error_handler)
3005 bed->link_order_error_handler
3006 (_("%B: warning: sh_link not set for section `%A'"),
3011 switch (d->this_hdr.sh_type)
3015 /* A reloc section which we are treating as a normal BFD
3016 section. sh_link is the section index of the symbol
3017 table. sh_info is the section index of the section to
3018 which the relocation entries apply. We assume that an
3019 allocated reloc section uses the dynamic symbol table.
3020 FIXME: How can we be sure? */
3021 s = bfd_get_section_by_name (abfd, ".dynsym");
3023 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3025 /* We look up the section the relocs apply to by name. */
3027 if (d->this_hdr.sh_type == SHT_REL)
3031 s = bfd_get_section_by_name (abfd, name);
3033 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3037 /* We assume that a section named .stab*str is a stabs
3038 string section. We look for a section with the same name
3039 but without the trailing ``str'', and set its sh_link
3040 field to point to this section. */
3041 if (CONST_STRNEQ (sec->name, ".stab")
3042 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3047 len = strlen (sec->name);
3048 alc = bfd_malloc (len - 2);
3051 memcpy (alc, sec->name, len - 3);
3052 alc[len - 3] = '\0';
3053 s = bfd_get_section_by_name (abfd, alc);
3057 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3059 /* This is a .stab section. */
3060 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3061 elf_section_data (s)->this_hdr.sh_entsize
3062 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3069 case SHT_GNU_verneed:
3070 case SHT_GNU_verdef:
3071 /* sh_link is the section header index of the string table
3072 used for the dynamic entries, or the symbol table, or the
3074 s = bfd_get_section_by_name (abfd, ".dynstr");
3076 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3079 case SHT_GNU_LIBLIST:
3080 /* sh_link is the section header index of the prelink library
3081 list used for the dynamic entries, or the symbol table, or
3082 the version strings. */
3083 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3084 ? ".dynstr" : ".gnu.libstr");
3086 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3091 case SHT_GNU_versym:
3092 /* sh_link is the section header index of the symbol table
3093 this hash table or version table is for. */
3094 s = bfd_get_section_by_name (abfd, ".dynsym");
3096 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3100 d->this_hdr.sh_link = t->symtab_section;
3104 for (secn = 1; secn < section_number; ++secn)
3105 if (i_shdrp[secn] == NULL)
3106 i_shdrp[secn] = i_shdrp[0];
3108 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3109 i_shdrp[secn]->sh_name);
3113 /* Map symbol from it's internal number to the external number, moving
3114 all local symbols to be at the head of the list. */
3117 sym_is_global (bfd *abfd, asymbol *sym)
3119 /* If the backend has a special mapping, use it. */
3120 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3121 if (bed->elf_backend_sym_is_global)
3122 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3124 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3125 || bfd_is_und_section (bfd_get_section (sym))
3126 || bfd_is_com_section (bfd_get_section (sym)));
3129 /* Don't output section symbols for sections that are not going to be
3133 ignore_section_sym (bfd *abfd, asymbol *sym)
3135 return ((sym->flags & BSF_SECTION_SYM) != 0
3136 && !(sym->section->owner == abfd
3137 || (sym->section->output_section->owner == abfd
3138 && sym->section->output_offset == 0)));
3142 elf_map_symbols (bfd *abfd)
3144 unsigned int symcount = bfd_get_symcount (abfd);
3145 asymbol **syms = bfd_get_outsymbols (abfd);
3146 asymbol **sect_syms;
3147 unsigned int num_locals = 0;
3148 unsigned int num_globals = 0;
3149 unsigned int num_locals2 = 0;
3150 unsigned int num_globals2 = 0;
3157 fprintf (stderr, "elf_map_symbols\n");
3161 for (asect = abfd->sections; asect; asect = asect->next)
3163 if (max_index < asect->index)
3164 max_index = asect->index;
3168 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3169 if (sect_syms == NULL)
3171 elf_section_syms (abfd) = sect_syms;
3172 elf_num_section_syms (abfd) = max_index;
3174 /* Init sect_syms entries for any section symbols we have already
3175 decided to output. */
3176 for (idx = 0; idx < symcount; idx++)
3178 asymbol *sym = syms[idx];
3180 if ((sym->flags & BSF_SECTION_SYM) != 0
3182 && !ignore_section_sym (abfd, sym))
3184 asection *sec = sym->section;
3186 if (sec->owner != abfd)
3187 sec = sec->output_section;
3189 sect_syms[sec->index] = syms[idx];
3193 /* Classify all of the symbols. */
3194 for (idx = 0; idx < symcount; idx++)
3196 if (ignore_section_sym (abfd, syms[idx]))
3198 if (!sym_is_global (abfd, syms[idx]))
3204 /* We will be adding a section symbol for each normal BFD section. Most
3205 sections will already have a section symbol in outsymbols, but
3206 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3207 at least in that case. */
3208 for (asect = abfd->sections; asect; asect = asect->next)
3210 if (sect_syms[asect->index] == NULL)
3212 if (!sym_is_global (abfd, asect->symbol))
3219 /* Now sort the symbols so the local symbols are first. */
3220 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3222 if (new_syms == NULL)
3225 for (idx = 0; idx < symcount; idx++)
3227 asymbol *sym = syms[idx];
3230 if (ignore_section_sym (abfd, sym))
3232 if (!sym_is_global (abfd, sym))
3235 i = num_locals + num_globals2++;
3237 sym->udata.i = i + 1;
3239 for (asect = abfd->sections; asect; asect = asect->next)
3241 if (sect_syms[asect->index] == NULL)
3243 asymbol *sym = asect->symbol;
3246 sect_syms[asect->index] = sym;
3247 if (!sym_is_global (abfd, sym))
3250 i = num_locals + num_globals2++;
3252 sym->udata.i = i + 1;
3256 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3258 elf_num_locals (abfd) = num_locals;
3259 elf_num_globals (abfd) = num_globals;
3263 /* Align to the maximum file alignment that could be required for any
3264 ELF data structure. */
3266 static inline file_ptr
3267 align_file_position (file_ptr off, int align)
3269 return (off + align - 1) & ~(align - 1);
3272 /* Assign a file position to a section, optionally aligning to the
3273 required section alignment. */
3276 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3280 if (align && i_shdrp->sh_addralign > 1)
3281 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3282 i_shdrp->sh_offset = offset;
3283 if (i_shdrp->bfd_section != NULL)
3284 i_shdrp->bfd_section->filepos = offset;
3285 if (i_shdrp->sh_type != SHT_NOBITS)
3286 offset += i_shdrp->sh_size;
3290 /* Compute the file positions we are going to put the sections at, and
3291 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3292 is not NULL, this is being called by the ELF backend linker. */
3295 _bfd_elf_compute_section_file_positions (bfd *abfd,
3296 struct bfd_link_info *link_info)
3298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3300 struct bfd_strtab_hash *strtab = NULL;
3301 Elf_Internal_Shdr *shstrtab_hdr;
3303 if (abfd->output_has_begun)
3306 /* Do any elf backend specific processing first. */
3307 if (bed->elf_backend_begin_write_processing)
3308 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3310 if (! prep_headers (abfd))
3313 /* Post process the headers if necessary. */
3314 if (bed->elf_backend_post_process_headers)
3315 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3318 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3322 if (!assign_section_numbers (abfd, link_info))
3325 /* The backend linker builds symbol table information itself. */
3326 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3328 /* Non-zero if doing a relocatable link. */
3329 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3331 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3335 if (link_info == NULL)
3337 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3342 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3343 /* sh_name was set in prep_headers. */
3344 shstrtab_hdr->sh_type = SHT_STRTAB;
3345 shstrtab_hdr->sh_flags = 0;
3346 shstrtab_hdr->sh_addr = 0;
3347 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3348 shstrtab_hdr->sh_entsize = 0;
3349 shstrtab_hdr->sh_link = 0;
3350 shstrtab_hdr->sh_info = 0;
3351 /* sh_offset is set in assign_file_positions_except_relocs. */
3352 shstrtab_hdr->sh_addralign = 1;
3354 if (!assign_file_positions_except_relocs (abfd, link_info))
3357 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3360 Elf_Internal_Shdr *hdr;
3362 off = elf_tdata (abfd)->next_file_pos;
3364 hdr = &elf_tdata (abfd)->symtab_hdr;
3365 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3367 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3368 if (hdr->sh_size != 0)
3369 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3371 hdr = &elf_tdata (abfd)->strtab_hdr;
3372 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3374 elf_tdata (abfd)->next_file_pos = off;
3376 /* Now that we know where the .strtab section goes, write it
3378 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3379 || ! _bfd_stringtab_emit (abfd, strtab))
3381 _bfd_stringtab_free (strtab);
3384 abfd->output_has_begun = TRUE;
3389 /* Make an initial estimate of the size of the program header. If we
3390 get the number wrong here, we'll redo section placement. */
3392 static bfd_size_type
3393 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3397 const struct elf_backend_data *bed;
3399 /* Assume we will need exactly two PT_LOAD segments: one for text
3400 and one for data. */
3403 s = bfd_get_section_by_name (abfd, ".interp");
3404 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3406 /* If we have a loadable interpreter section, we need a
3407 PT_INTERP segment. In this case, assume we also need a
3408 PT_PHDR segment, although that may not be true for all
3413 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3415 /* We need a PT_DYNAMIC segment. */
3419 if (info != NULL && info->relro)
3421 /* We need a PT_GNU_RELRO segment. */
3425 if (elf_tdata (abfd)->eh_frame_hdr)
3427 /* We need a PT_GNU_EH_FRAME segment. */
3431 if (elf_tdata (abfd)->stack_flags)
3433 /* We need a PT_GNU_STACK segment. */
3437 for (s = abfd->sections; s != NULL; s = s->next)
3439 if ((s->flags & SEC_LOAD) != 0
3440 && CONST_STRNEQ (s->name, ".note"))
3442 /* We need a PT_NOTE segment. */
3444 /* Try to create just one PT_NOTE segment
3445 for all adjacent loadable .note* sections.
3446 gABI requires that within a PT_NOTE segment
3447 (and also inside of each SHT_NOTE section)
3448 each note is padded to a multiple of 4 size,
3449 so we check whether the sections are correctly
3451 if (s->alignment_power == 2)
3452 while (s->next != NULL
3453 && s->next->alignment_power == 2
3454 && (s->next->flags & SEC_LOAD) != 0
3455 && CONST_STRNEQ (s->next->name, ".note"))
3460 for (s = abfd->sections; s != NULL; s = s->next)
3462 if (s->flags & SEC_THREAD_LOCAL)
3464 /* We need a PT_TLS segment. */
3470 /* Let the backend count up any program headers it might need. */
3471 bed = get_elf_backend_data (abfd);
3472 if (bed->elf_backend_additional_program_headers)
3476 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3482 return segs * bed->s->sizeof_phdr;
3485 /* Find the segment that contains the output_section of section. */
3488 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3490 struct elf_segment_map *m;
3491 Elf_Internal_Phdr *p;
3493 for (m = elf_tdata (abfd)->segment_map,
3494 p = elf_tdata (abfd)->phdr;
3500 for (i = m->count - 1; i >= 0; i--)
3501 if (m->sections[i] == section)
3508 /* Create a mapping from a set of sections to a program segment. */
3510 static struct elf_segment_map *
3511 make_mapping (bfd *abfd,
3512 asection **sections,
3517 struct elf_segment_map *m;
3522 amt = sizeof (struct elf_segment_map);
3523 amt += (to - from - 1) * sizeof (asection *);
3524 m = bfd_zalloc (abfd, amt);
3528 m->p_type = PT_LOAD;
3529 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3530 m->sections[i - from] = *hdrpp;
3531 m->count = to - from;
3533 if (from == 0 && phdr)
3535 /* Include the headers in the first PT_LOAD segment. */
3536 m->includes_filehdr = 1;
3537 m->includes_phdrs = 1;
3543 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3546 struct elf_segment_map *
3547 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3549 struct elf_segment_map *m;
3551 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3555 m->p_type = PT_DYNAMIC;
3557 m->sections[0] = dynsec;
3562 /* Possibly add or remove segments from the segment map. */
3565 elf_modify_segment_map (bfd *abfd,
3566 struct bfd_link_info *info,
3567 bfd_boolean remove_empty_load)
3569 struct elf_segment_map **m;
3570 const struct elf_backend_data *bed;
3572 /* The placement algorithm assumes that non allocated sections are
3573 not in PT_LOAD segments. We ensure this here by removing such
3574 sections from the segment map. We also remove excluded
3575 sections. Finally, any PT_LOAD segment without sections is
3577 m = &elf_tdata (abfd)->segment_map;
3580 unsigned int i, new_count;
3582 for (new_count = 0, i = 0; i < (*m)->count; i++)
3584 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3585 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3586 || (*m)->p_type != PT_LOAD))
3588 (*m)->sections[new_count] = (*m)->sections[i];
3592 (*m)->count = new_count;
3594 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3600 bed = get_elf_backend_data (abfd);
3601 if (bed->elf_backend_modify_segment_map != NULL)
3603 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3610 /* Set up a mapping from BFD sections to program segments. */
3613 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3616 struct elf_segment_map *m;
3617 asection **sections = NULL;
3618 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3619 bfd_boolean no_user_phdrs;
3621 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3622 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3626 struct elf_segment_map *mfirst;
3627 struct elf_segment_map **pm;
3630 unsigned int phdr_index;
3631 bfd_vma maxpagesize;
3633 bfd_boolean phdr_in_segment = TRUE;
3634 bfd_boolean writable;
3636 asection *first_tls = NULL;
3637 asection *dynsec, *eh_frame_hdr;
3640 /* Select the allocated sections, and sort them. */
3642 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3643 if (sections == NULL)
3647 for (s = abfd->sections; s != NULL; s = s->next)
3649 if ((s->flags & SEC_ALLOC) != 0)
3655 BFD_ASSERT (i <= bfd_count_sections (abfd));
3658 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3660 /* Build the mapping. */
3665 /* If we have a .interp section, then create a PT_PHDR segment for
3666 the program headers and a PT_INTERP segment for the .interp
3668 s = bfd_get_section_by_name (abfd, ".interp");
3669 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3671 amt = sizeof (struct elf_segment_map);
3672 m = bfd_zalloc (abfd, amt);
3676 m->p_type = PT_PHDR;
3677 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3678 m->p_flags = PF_R | PF_X;
3679 m->p_flags_valid = 1;
3680 m->includes_phdrs = 1;
3685 amt = sizeof (struct elf_segment_map);
3686 m = bfd_zalloc (abfd, amt);
3690 m->p_type = PT_INTERP;
3698 /* Look through the sections. We put sections in the same program
3699 segment when the start of the second section can be placed within
3700 a few bytes of the end of the first section. */
3704 maxpagesize = bed->maxpagesize;
3706 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3708 && (dynsec->flags & SEC_LOAD) == 0)
3711 /* Deal with -Ttext or something similar such that the first section
3712 is not adjacent to the program headers. This is an
3713 approximation, since at this point we don't know exactly how many
3714 program headers we will need. */
3717 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3719 if (phdr_size == (bfd_size_type) -1)
3720 phdr_size = get_program_header_size (abfd, info);
3721 if ((abfd->flags & D_PAGED) == 0
3722 || sections[0]->lma < phdr_size
3723 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3724 phdr_in_segment = FALSE;
3727 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3730 bfd_boolean new_segment;
3734 /* See if this section and the last one will fit in the same
3737 if (last_hdr == NULL)
3739 /* If we don't have a segment yet, then we don't need a new
3740 one (we build the last one after this loop). */
3741 new_segment = FALSE;
3743 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3745 /* If this section has a different relation between the
3746 virtual address and the load address, then we need a new
3750 /* In the next test we have to be careful when last_hdr->lma is close
3751 to the end of the address space. If the aligned address wraps
3752 around to the start of the address space, then there are no more
3753 pages left in memory and it is OK to assume that the current
3754 section can be included in the current segment. */
3755 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3757 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3760 /* If putting this section in this segment would force us to
3761 skip a page in the segment, then we need a new segment. */
3764 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3765 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3767 /* We don't want to put a loadable section after a
3768 nonloadable section in the same segment.
3769 Consider .tbss sections as loadable for this purpose. */
3772 else if ((abfd->flags & D_PAGED) == 0)
3774 /* If the file is not demand paged, which means that we
3775 don't require the sections to be correctly aligned in the
3776 file, then there is no other reason for a new segment. */
3777 new_segment = FALSE;
3780 && (hdr->flags & SEC_READONLY) == 0
3781 && (((last_hdr->lma + last_size - 1)
3782 & ~(maxpagesize - 1))
3783 != (hdr->lma & ~(maxpagesize - 1))))
3785 /* We don't want to put a writable section in a read only
3786 segment, unless they are on the same page in memory
3787 anyhow. We already know that the last section does not
3788 bring us past the current section on the page, so the
3789 only case in which the new section is not on the same
3790 page as the previous section is when the previous section
3791 ends precisely on a page boundary. */
3796 /* Otherwise, we can use the same segment. */
3797 new_segment = FALSE;
3800 /* Allow interested parties a chance to override our decision. */
3801 if (last_hdr != NULL
3803 && info->callbacks->override_segment_assignment != NULL)
3805 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3811 if ((hdr->flags & SEC_READONLY) == 0)
3814 /* .tbss sections effectively have zero size. */
3815 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3816 != SEC_THREAD_LOCAL)
3817 last_size = hdr->size;
3823 /* We need a new program segment. We must create a new program
3824 header holding all the sections from phdr_index until hdr. */
3826 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3833 if ((hdr->flags & SEC_READONLY) == 0)
3839 /* .tbss sections effectively have zero size. */
3840 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3841 last_size = hdr->size;
3845 phdr_in_segment = FALSE;
3848 /* Create a final PT_LOAD program segment. */
3849 if (last_hdr != NULL)
3851 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3859 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3862 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3869 /* For each batch of consecutive loadable .note sections,
3870 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3871 because if we link together nonloadable .note sections and
3872 loadable .note sections, we will generate two .note sections
3873 in the output file. FIXME: Using names for section types is
3875 for (s = abfd->sections; s != NULL; s = s->next)
3877 if ((s->flags & SEC_LOAD) != 0
3878 && CONST_STRNEQ (s->name, ".note"))
3882 amt = sizeof (struct elf_segment_map);
3883 if (s->alignment_power == 2)
3884 for (s2 = s; s2->next != NULL; s2 = s2->next)
3886 if (s2->next->alignment_power == 2
3887 && (s2->next->flags & SEC_LOAD) != 0
3888 && CONST_STRNEQ (s2->next->name, ".note")
3889 && align_power (s2->vma + s2->size, 2)
3895 amt += (count - 1) * sizeof (asection *);
3896 m = bfd_zalloc (abfd, amt);
3900 m->p_type = PT_NOTE;
3904 m->sections[m->count - count--] = s;
3905 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3908 m->sections[m->count - 1] = s;
3909 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3913 if (s->flags & SEC_THREAD_LOCAL)
3921 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3926 amt = sizeof (struct elf_segment_map);
3927 amt += (tls_count - 1) * sizeof (asection *);
3928 m = bfd_zalloc (abfd, amt);
3933 m->count = tls_count;
3934 /* Mandated PF_R. */
3936 m->p_flags_valid = 1;
3937 for (i = 0; i < tls_count; ++i)
3939 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3940 m->sections[i] = first_tls;
3941 first_tls = first_tls->next;
3948 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3950 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3951 if (eh_frame_hdr != NULL
3952 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3954 amt = sizeof (struct elf_segment_map);
3955 m = bfd_zalloc (abfd, amt);
3959 m->p_type = PT_GNU_EH_FRAME;
3961 m->sections[0] = eh_frame_hdr->output_section;
3967 if (elf_tdata (abfd)->stack_flags)
3969 amt = sizeof (struct elf_segment_map);
3970 m = bfd_zalloc (abfd, amt);
3974 m->p_type = PT_GNU_STACK;
3975 m->p_flags = elf_tdata (abfd)->stack_flags;
3976 m->p_flags_valid = 1;
3982 if (info != NULL && info->relro)
3984 for (m = mfirst; m != NULL; m = m->next)
3986 if (m->p_type == PT_LOAD)
3988 asection *last = m->sections[m->count - 1];
3989 bfd_vma vaddr = m->sections[0]->vma;
3990 bfd_vma filesz = last->vma - vaddr + last->size;
3992 if (vaddr < info->relro_end
3993 && vaddr >= info->relro_start
3994 && (vaddr + filesz) >= info->relro_end)
3999 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4002 amt = sizeof (struct elf_segment_map);
4003 m = bfd_zalloc (abfd, amt);
4007 m->p_type = PT_GNU_RELRO;
4009 m->p_flags_valid = 1;
4017 elf_tdata (abfd)->segment_map = mfirst;
4020 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4023 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4025 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4030 if (sections != NULL)
4035 /* Sort sections by address. */
4038 elf_sort_sections (const void *arg1, const void *arg2)
4040 const asection *sec1 = *(const asection **) arg1;
4041 const asection *sec2 = *(const asection **) arg2;
4042 bfd_size_type size1, size2;
4044 /* Sort by LMA first, since this is the address used to
4045 place the section into a segment. */
4046 if (sec1->lma < sec2->lma)
4048 else if (sec1->lma > sec2->lma)
4051 /* Then sort by VMA. Normally the LMA and the VMA will be
4052 the same, and this will do nothing. */
4053 if (sec1->vma < sec2->vma)
4055 else if (sec1->vma > sec2->vma)
4058 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4060 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4066 /* If the indicies are the same, do not return 0
4067 here, but continue to try the next comparison. */
4068 if (sec1->target_index - sec2->target_index != 0)
4069 return sec1->target_index - sec2->target_index;
4074 else if (TOEND (sec2))
4079 /* Sort by size, to put zero sized sections
4080 before others at the same address. */
4082 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4083 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4090 return sec1->target_index - sec2->target_index;
4093 /* Ian Lance Taylor writes:
4095 We shouldn't be using % with a negative signed number. That's just
4096 not good. We have to make sure either that the number is not
4097 negative, or that the number has an unsigned type. When the types
4098 are all the same size they wind up as unsigned. When file_ptr is a
4099 larger signed type, the arithmetic winds up as signed long long,
4102 What we're trying to say here is something like ``increase OFF by
4103 the least amount that will cause it to be equal to the VMA modulo
4105 /* In other words, something like:
4107 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4108 off_offset = off % bed->maxpagesize;
4109 if (vma_offset < off_offset)
4110 adjustment = vma_offset + bed->maxpagesize - off_offset;
4112 adjustment = vma_offset - off_offset;
4114 which can can be collapsed into the expression below. */
4117 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4119 return ((vma - off) % maxpagesize);
4123 print_segment_map (const struct elf_segment_map *m)
4126 const char *pt = get_segment_type (m->p_type);
4131 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4132 sprintf (buf, "LOPROC+%7.7x",
4133 (unsigned int) (m->p_type - PT_LOPROC));
4134 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4135 sprintf (buf, "LOOS+%7.7x",
4136 (unsigned int) (m->p_type - PT_LOOS));
4138 snprintf (buf, sizeof (buf), "%8.8x",
4139 (unsigned int) m->p_type);
4142 fprintf (stderr, "%s:", pt);
4143 for (j = 0; j < m->count; j++)
4144 fprintf (stderr, " %s", m->sections [j]->name);
4148 /* Assign file positions to the sections based on the mapping from
4149 sections to segments. This function also sets up some fields in
4153 assign_file_positions_for_load_sections (bfd *abfd,
4154 struct bfd_link_info *link_info)
4156 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4157 struct elf_segment_map *m;
4158 Elf_Internal_Phdr *phdrs;
4159 Elf_Internal_Phdr *p;
4161 bfd_size_type maxpagesize;
4164 bfd_vma header_pad = 0;
4166 if (link_info == NULL
4167 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4171 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4175 header_pad = m->header_size;
4178 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4179 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4180 elf_elfheader (abfd)->e_phnum = alloc;
4182 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4183 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4185 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4186 >= alloc * bed->s->sizeof_phdr);
4190 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4194 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4195 see assign_file_positions_except_relocs, so make sure we have
4196 that amount allocated, with trailing space cleared.
4197 The variable alloc contains the computed need, while elf_tdata
4198 (abfd)->program_header_size contains the size used for the
4200 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4201 where the layout is forced to according to a larger size in the
4202 last iterations for the testcase ld-elf/header. */
4203 BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4205 phdrs = bfd_zalloc2 (abfd,
4206 (elf_tdata (abfd)->program_header_size
4207 / bed->s->sizeof_phdr),
4208 sizeof (Elf_Internal_Phdr));
4209 elf_tdata (abfd)->phdr = phdrs;
4214 if ((abfd->flags & D_PAGED) != 0)
4215 maxpagesize = bed->maxpagesize;
4217 off = bed->s->sizeof_ehdr;
4218 off += alloc * bed->s->sizeof_phdr;
4219 if (header_pad < (bfd_vma) off)
4225 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4227 m = m->next, p++, j++)
4231 bfd_boolean no_contents;
4233 /* If elf_segment_map is not from map_sections_to_segments, the
4234 sections may not be correctly ordered. NOTE: sorting should
4235 not be done to the PT_NOTE section of a corefile, which may
4236 contain several pseudo-sections artificially created by bfd.
4237 Sorting these pseudo-sections breaks things badly. */
4239 && !(elf_elfheader (abfd)->e_type == ET_CORE
4240 && m->p_type == PT_NOTE))
4241 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4244 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4245 number of sections with contents contributing to both p_filesz
4246 and p_memsz, followed by a number of sections with no contents
4247 that just contribute to p_memsz. In this loop, OFF tracks next
4248 available file offset for PT_LOAD and PT_NOTE segments. */
4249 p->p_type = m->p_type;
4250 p->p_flags = m->p_flags;
4255 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4257 if (m->p_paddr_valid)
4258 p->p_paddr = m->p_paddr;
4259 else if (m->count == 0)
4262 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4264 if (p->p_type == PT_LOAD
4265 && (abfd->flags & D_PAGED) != 0)
4267 /* p_align in demand paged PT_LOAD segments effectively stores
4268 the maximum page size. When copying an executable with
4269 objcopy, we set m->p_align from the input file. Use this
4270 value for maxpagesize rather than bed->maxpagesize, which
4271 may be different. Note that we use maxpagesize for PT_TLS
4272 segment alignment later in this function, so we are relying
4273 on at least one PT_LOAD segment appearing before a PT_TLS
4275 if (m->p_align_valid)
4276 maxpagesize = m->p_align;
4278 p->p_align = maxpagesize;
4280 else if (m->p_align_valid)
4281 p->p_align = m->p_align;
4282 else if (m->count == 0)
4283 p->p_align = 1 << bed->s->log_file_align;
4287 no_contents = FALSE;
4289 if (p->p_type == PT_LOAD
4292 bfd_size_type align;
4293 unsigned int align_power = 0;
4295 if (m->p_align_valid)
4299 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4301 unsigned int secalign;
4303 secalign = bfd_get_section_alignment (abfd, *secpp);
4304 if (secalign > align_power)
4305 align_power = secalign;
4307 align = (bfd_size_type) 1 << align_power;
4308 if (align < maxpagesize)
4309 align = maxpagesize;
4312 for (i = 0; i < m->count; i++)
4313 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4314 /* If we aren't making room for this section, then
4315 it must be SHT_NOBITS regardless of what we've
4316 set via struct bfd_elf_special_section. */
4317 elf_section_type (m->sections[i]) = SHT_NOBITS;
4319 /* Find out whether this segment contains any loadable
4322 for (i = 0; i < m->count; i++)
4323 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4325 no_contents = FALSE;
4329 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4333 /* We shouldn't need to align the segment on disk since
4334 the segment doesn't need file space, but the gABI
4335 arguably requires the alignment and glibc ld.so
4336 checks it. So to comply with the alignment
4337 requirement but not waste file space, we adjust
4338 p_offset for just this segment. (OFF_ADJUST is
4339 subtracted from OFF later.) This may put p_offset
4340 past the end of file, but that shouldn't matter. */
4345 /* Make sure the .dynamic section is the first section in the
4346 PT_DYNAMIC segment. */
4347 else if (p->p_type == PT_DYNAMIC
4349 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4352 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4354 bfd_set_error (bfd_error_bad_value);
4357 /* Set the note section type to SHT_NOTE. */
4358 else if (p->p_type == PT_NOTE)
4359 for (i = 0; i < m->count; i++)
4360 elf_section_type (m->sections[i]) = SHT_NOTE;
4366 if (m->includes_filehdr)
4368 if (!m->p_flags_valid)
4370 p->p_filesz = bed->s->sizeof_ehdr;
4371 p->p_memsz = bed->s->sizeof_ehdr;
4374 BFD_ASSERT (p->p_type == PT_LOAD);
4376 if (p->p_vaddr < (bfd_vma) off)
4378 (*_bfd_error_handler)
4379 (_("%B: Not enough room for program headers, try linking with -N"),
4381 bfd_set_error (bfd_error_bad_value);
4386 if (!m->p_paddr_valid)
4391 if (m->includes_phdrs)
4393 if (!m->p_flags_valid)
4396 if (!m->includes_filehdr)
4398 p->p_offset = bed->s->sizeof_ehdr;
4402 BFD_ASSERT (p->p_type == PT_LOAD);
4403 p->p_vaddr -= off - p->p_offset;
4404 if (!m->p_paddr_valid)
4405 p->p_paddr -= off - p->p_offset;
4409 p->p_filesz += alloc * bed->s->sizeof_phdr;
4410 p->p_memsz += alloc * bed->s->sizeof_phdr;
4413 p->p_filesz += header_pad;
4414 p->p_memsz += header_pad;
4418 if (p->p_type == PT_LOAD
4419 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4421 if (!m->includes_filehdr && !m->includes_phdrs)
4427 adjust = off - (p->p_offset + p->p_filesz);
4429 p->p_filesz += adjust;
4430 p->p_memsz += adjust;
4434 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4435 maps. Set filepos for sections in PT_LOAD segments, and in
4436 core files, for sections in PT_NOTE segments.
4437 assign_file_positions_for_non_load_sections will set filepos
4438 for other sections and update p_filesz for other segments. */
4439 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4442 bfd_size_type align;
4443 Elf_Internal_Shdr *this_hdr;
4446 this_hdr = &elf_section_data (sec)->this_hdr;
4447 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4449 if ((p->p_type == PT_LOAD
4450 || p->p_type == PT_TLS)
4451 && (this_hdr->sh_type != SHT_NOBITS
4452 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4453 && ((this_hdr->sh_flags & SHF_TLS) == 0
4454 || p->p_type == PT_TLS))))
4456 bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
4460 (*_bfd_error_handler)
4461 (_("%B: section %A vma 0x%lx overlaps previous sections"),
4462 abfd, sec, (unsigned long) sec->vma);
4465 p->p_memsz += adjust;
4467 if (this_hdr->sh_type != SHT_NOBITS)
4470 p->p_filesz += adjust;
4474 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4476 /* The section at i == 0 is the one that actually contains
4480 this_hdr->sh_offset = sec->filepos = off;
4481 off += this_hdr->sh_size;
4482 p->p_filesz = this_hdr->sh_size;
4488 /* The rest are fake sections that shouldn't be written. */
4497 if (p->p_type == PT_LOAD)
4499 this_hdr->sh_offset = sec->filepos = off;
4500 if (this_hdr->sh_type != SHT_NOBITS)
4501 off += this_hdr->sh_size;
4504 if (this_hdr->sh_type != SHT_NOBITS)
4506 p->p_filesz += this_hdr->sh_size;
4507 /* A load section without SHF_ALLOC is something like
4508 a note section in a PT_NOTE segment. These take
4509 file space but are not loaded into memory. */
4510 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4511 p->p_memsz += this_hdr->sh_size;
4513 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4515 if (p->p_type == PT_TLS)
4516 p->p_memsz += this_hdr->sh_size;
4518 /* .tbss is special. It doesn't contribute to p_memsz of
4520 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4521 p->p_memsz += this_hdr->sh_size;
4524 if (align > p->p_align
4525 && !m->p_align_valid
4526 && (p->p_type != PT_LOAD
4527 || (abfd->flags & D_PAGED) == 0))
4531 if (!m->p_flags_valid)
4534 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4536 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4542 /* Check that all sections are in a PT_LOAD segment.
4543 Don't check funky gdb generated core files. */
4544 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4545 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4547 Elf_Internal_Shdr *this_hdr;
4551 this_hdr = &(elf_section_data(sec)->this_hdr);
4552 if (this_hdr->sh_size != 0
4553 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4555 (*_bfd_error_handler)
4556 (_("%B: section `%A' can't be allocated in segment %d"),
4558 print_segment_map (m);
4559 bfd_set_error (bfd_error_bad_value);
4565 elf_tdata (abfd)->next_file_pos = off;
4569 /* Assign file positions for the other sections. */
4572 assign_file_positions_for_non_load_sections (bfd *abfd,
4573 struct bfd_link_info *link_info)
4575 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4576 Elf_Internal_Shdr **i_shdrpp;
4577 Elf_Internal_Shdr **hdrpp;
4578 Elf_Internal_Phdr *phdrs;
4579 Elf_Internal_Phdr *p;
4580 struct elf_segment_map *m;
4581 bfd_vma filehdr_vaddr, filehdr_paddr;
4582 bfd_vma phdrs_vaddr, phdrs_paddr;
4584 unsigned int num_sec;
4588 i_shdrpp = elf_elfsections (abfd);
4589 num_sec = elf_numsections (abfd);
4590 off = elf_tdata (abfd)->next_file_pos;
4591 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4593 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4594 Elf_Internal_Shdr *hdr;
4597 if (hdr->bfd_section != NULL
4598 && (hdr->bfd_section->filepos != 0
4599 || (hdr->sh_type == SHT_NOBITS
4600 && hdr->contents == NULL)))
4601 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4602 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4604 if (hdr->sh_size != 0)
4605 ((*_bfd_error_handler)
4606 (_("%B: warning: allocated section `%s' not in segment"),
4608 (hdr->bfd_section == NULL
4610 : hdr->bfd_section->name)));
4611 /* We don't need to page align empty sections. */
4612 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4613 off += vma_page_aligned_bias (hdr->sh_addr, off,
4616 off += vma_page_aligned_bias (hdr->sh_addr, off,
4618 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4621 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4622 && hdr->bfd_section == NULL)
4623 || hdr == i_shdrpp[tdata->symtab_section]
4624 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4625 || hdr == i_shdrpp[tdata->strtab_section])
4626 hdr->sh_offset = -1;
4628 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4631 /* Now that we have set the section file positions, we can set up
4632 the file positions for the non PT_LOAD segments. */
4636 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4638 phdrs = elf_tdata (abfd)->phdr;
4639 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4644 if (p->p_type != PT_LOAD)
4647 if (m->includes_filehdr)
4649 filehdr_vaddr = p->p_vaddr;
4650 filehdr_paddr = p->p_paddr;
4652 if (m->includes_phdrs)
4654 phdrs_vaddr = p->p_vaddr;
4655 phdrs_paddr = p->p_paddr;
4656 if (m->includes_filehdr)
4658 phdrs_vaddr += bed->s->sizeof_ehdr;
4659 phdrs_paddr += bed->s->sizeof_ehdr;
4664 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4668 if (p->p_type == PT_GNU_RELRO)
4670 const Elf_Internal_Phdr *lp;
4672 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4674 if (link_info != NULL)
4676 /* During linking the range of the RELRO segment is passed
4678 for (lp = phdrs; lp < phdrs + count; ++lp)
4680 if (lp->p_type == PT_LOAD
4681 && lp->p_vaddr >= link_info->relro_start
4682 && lp->p_vaddr < link_info->relro_end
4683 && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4689 /* Otherwise we are copying an executable or shared
4690 library, but we need to use the same linker logic. */
4691 for (lp = phdrs; lp < phdrs + count; ++lp)
4693 if (lp->p_type == PT_LOAD
4694 && lp->p_paddr == p->p_paddr)
4699 if (lp < phdrs + count)
4701 p->p_vaddr = lp->p_vaddr;
4702 p->p_paddr = lp->p_paddr;
4703 p->p_offset = lp->p_offset;
4704 if (link_info != NULL)
4705 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4706 else if (m->p_size_valid)
4707 p->p_filesz = m->p_size;
4710 p->p_memsz = p->p_filesz;
4712 p->p_flags = (lp->p_flags & ~PF_W);
4714 else if (link_info != NULL)
4716 memset (p, 0, sizeof *p);
4717 p->p_type = PT_NULL;
4722 else if (m->count != 0)
4724 if (p->p_type != PT_LOAD
4725 && (p->p_type != PT_NOTE
4726 || bfd_get_format (abfd) != bfd_core))
4728 Elf_Internal_Shdr *hdr;
4731 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4733 sect = m->sections[m->count - 1];
4734 hdr = &elf_section_data (sect)->this_hdr;
4735 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4736 if (hdr->sh_type != SHT_NOBITS)
4737 p->p_filesz += hdr->sh_size;
4738 p->p_offset = m->sections[0]->filepos;
4741 else if (m->includes_filehdr)
4743 p->p_vaddr = filehdr_vaddr;
4744 if (! m->p_paddr_valid)
4745 p->p_paddr = filehdr_paddr;
4747 else if (m->includes_phdrs)
4749 p->p_vaddr = phdrs_vaddr;
4750 if (! m->p_paddr_valid)
4751 p->p_paddr = phdrs_paddr;
4755 elf_tdata (abfd)->next_file_pos = off;
4760 /* Work out the file positions of all the sections. This is called by
4761 _bfd_elf_compute_section_file_positions. All the section sizes and
4762 VMAs must be known before this is called.
4764 Reloc sections come in two flavours: Those processed specially as
4765 "side-channel" data attached to a section to which they apply, and
4766 those that bfd doesn't process as relocations. The latter sort are
4767 stored in a normal bfd section by bfd_section_from_shdr. We don't
4768 consider the former sort here, unless they form part of the loadable
4769 image. Reloc sections not assigned here will be handled later by
4770 assign_file_positions_for_relocs.
4772 We also don't set the positions of the .symtab and .strtab here. */
4775 assign_file_positions_except_relocs (bfd *abfd,
4776 struct bfd_link_info *link_info)
4778 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4779 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4781 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4783 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4784 && bfd_get_format (abfd) != bfd_core)
4786 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4787 unsigned int num_sec = elf_numsections (abfd);
4788 Elf_Internal_Shdr **hdrpp;
4791 /* Start after the ELF header. */
4792 off = i_ehdrp->e_ehsize;
4794 /* We are not creating an executable, which means that we are
4795 not creating a program header, and that the actual order of
4796 the sections in the file is unimportant. */
4797 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4799 Elf_Internal_Shdr *hdr;
4802 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4803 && hdr->bfd_section == NULL)
4804 || i == tdata->symtab_section
4805 || i == tdata->symtab_shndx_section
4806 || i == tdata->strtab_section)
4808 hdr->sh_offset = -1;
4811 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4818 /* Assign file positions for the loaded sections based on the
4819 assignment of sections to segments. */
4820 if (!assign_file_positions_for_load_sections (abfd, link_info))
4823 /* And for non-load sections. */
4824 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4827 if (bed->elf_backend_modify_program_headers != NULL)
4829 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4833 /* Write out the program headers. */
4834 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4835 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4836 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4839 off = tdata->next_file_pos;
4842 /* Place the section headers. */
4843 off = align_file_position (off, 1 << bed->s->log_file_align);
4844 i_ehdrp->e_shoff = off;
4845 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4847 tdata->next_file_pos = off;
4853 prep_headers (bfd *abfd)
4855 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4856 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4857 struct elf_strtab_hash *shstrtab;
4858 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4860 i_ehdrp = elf_elfheader (abfd);
4862 shstrtab = _bfd_elf_strtab_init ();
4863 if (shstrtab == NULL)
4866 elf_shstrtab (abfd) = shstrtab;
4868 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4869 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4870 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4871 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4873 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4874 i_ehdrp->e_ident[EI_DATA] =
4875 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4876 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4878 if ((abfd->flags & DYNAMIC) != 0)
4879 i_ehdrp->e_type = ET_DYN;
4880 else if ((abfd->flags & EXEC_P) != 0)
4881 i_ehdrp->e_type = ET_EXEC;
4882 else if (bfd_get_format (abfd) == bfd_core)
4883 i_ehdrp->e_type = ET_CORE;
4885 i_ehdrp->e_type = ET_REL;
4887 switch (bfd_get_arch (abfd))
4889 case bfd_arch_unknown:
4890 i_ehdrp->e_machine = EM_NONE;
4893 /* There used to be a long list of cases here, each one setting
4894 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4895 in the corresponding bfd definition. To avoid duplication,
4896 the switch was removed. Machines that need special handling
4897 can generally do it in elf_backend_final_write_processing(),
4898 unless they need the information earlier than the final write.
4899 Such need can generally be supplied by replacing the tests for
4900 e_machine with the conditions used to determine it. */
4902 i_ehdrp->e_machine = bed->elf_machine_code;
4905 i_ehdrp->e_version = bed->s->ev_current;
4906 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4908 /* No program header, for now. */
4909 i_ehdrp->e_phoff = 0;
4910 i_ehdrp->e_phentsize = 0;
4911 i_ehdrp->e_phnum = 0;
4913 /* Each bfd section is section header entry. */
4914 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4915 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4917 /* If we're building an executable, we'll need a program header table. */
4918 if (abfd->flags & EXEC_P)
4919 /* It all happens later. */
4923 i_ehdrp->e_phentsize = 0;
4925 i_ehdrp->e_phoff = 0;
4928 elf_tdata (abfd)->symtab_hdr.sh_name =
4929 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4930 elf_tdata (abfd)->strtab_hdr.sh_name =
4931 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4932 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4933 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4934 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4935 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4936 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4942 /* Assign file positions for all the reloc sections which are not part
4943 of the loadable file image. */
4946 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4949 unsigned int i, num_sec;
4950 Elf_Internal_Shdr **shdrpp;
4952 off = elf_tdata (abfd)->next_file_pos;
4954 num_sec = elf_numsections (abfd);
4955 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4957 Elf_Internal_Shdr *shdrp;
4960 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4961 && shdrp->sh_offset == -1)
4962 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4965 elf_tdata (abfd)->next_file_pos = off;
4969 _bfd_elf_write_object_contents (bfd *abfd)
4971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4972 Elf_Internal_Ehdr *i_ehdrp;
4973 Elf_Internal_Shdr **i_shdrp;
4975 unsigned int count, num_sec;
4977 if (! abfd->output_has_begun
4978 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4981 i_shdrp = elf_elfsections (abfd);
4982 i_ehdrp = elf_elfheader (abfd);
4985 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4989 _bfd_elf_assign_file_positions_for_relocs (abfd);
4991 /* After writing the headers, we need to write the sections too... */
4992 num_sec = elf_numsections (abfd);
4993 for (count = 1; count < num_sec; count++)
4995 if (bed->elf_backend_section_processing)
4996 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4997 if (i_shdrp[count]->contents)
4999 bfd_size_type amt = i_shdrp[count]->sh_size;
5001 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5002 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5007 /* Write out the section header names. */
5008 if (elf_shstrtab (abfd) != NULL
5009 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5010 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5013 if (bed->elf_backend_final_write_processing)
5014 (*bed->elf_backend_final_write_processing) (abfd,
5015 elf_tdata (abfd)->linker);
5017 if (!bed->s->write_shdrs_and_ehdr (abfd))
5020 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5021 if (elf_tdata (abfd)->after_write_object_contents)
5022 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
5028 _bfd_elf_write_corefile_contents (bfd *abfd)
5030 /* Hopefully this can be done just like an object file. */
5031 return _bfd_elf_write_object_contents (abfd);
5034 /* Given a section, search the header to find them. */
5037 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5039 const struct elf_backend_data *bed;
5042 if (elf_section_data (asect) != NULL
5043 && elf_section_data (asect)->this_idx != 0)
5044 return elf_section_data (asect)->this_idx;
5046 if (bfd_is_abs_section (asect))
5048 else if (bfd_is_com_section (asect))
5050 else if (bfd_is_und_section (asect))
5055 bed = get_elf_backend_data (abfd);
5056 if (bed->elf_backend_section_from_bfd_section)
5060 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5064 if (index == SHN_BAD)
5065 bfd_set_error (bfd_error_nonrepresentable_section);
5070 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5074 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5076 asymbol *asym_ptr = *asym_ptr_ptr;
5078 flagword flags = asym_ptr->flags;
5080 /* When gas creates relocations against local labels, it creates its
5081 own symbol for the section, but does put the symbol into the
5082 symbol chain, so udata is 0. When the linker is generating
5083 relocatable output, this section symbol may be for one of the
5084 input sections rather than the output section. */
5085 if (asym_ptr->udata.i == 0
5086 && (flags & BSF_SECTION_SYM)
5087 && asym_ptr->section)
5092 sec = asym_ptr->section;
5093 if (sec->owner != abfd && sec->output_section != NULL)
5094 sec = sec->output_section;
5095 if (sec->owner == abfd
5096 && (indx = sec->index) < elf_num_section_syms (abfd)
5097 && elf_section_syms (abfd)[indx] != NULL)
5098 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5101 idx = asym_ptr->udata.i;
5105 /* This case can occur when using --strip-symbol on a symbol
5106 which is used in a relocation entry. */
5107 (*_bfd_error_handler)
5108 (_("%B: symbol `%s' required but not present"),
5109 abfd, bfd_asymbol_name (asym_ptr));
5110 bfd_set_error (bfd_error_no_symbols);
5117 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5118 (long) asym_ptr, asym_ptr->name, idx, flags,
5119 elf_symbol_flags (flags));
5127 /* Rewrite program header information. */
5130 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5132 Elf_Internal_Ehdr *iehdr;
5133 struct elf_segment_map *map;
5134 struct elf_segment_map *map_first;
5135 struct elf_segment_map **pointer_to_map;
5136 Elf_Internal_Phdr *segment;
5139 unsigned int num_segments;
5140 bfd_boolean phdr_included = FALSE;
5141 bfd_boolean p_paddr_valid;
5142 bfd_vma maxpagesize;
5143 struct elf_segment_map *phdr_adjust_seg = NULL;
5144 unsigned int phdr_adjust_num = 0;
5145 const struct elf_backend_data *bed;
5147 bed = get_elf_backend_data (ibfd);
5148 iehdr = elf_elfheader (ibfd);
5151 pointer_to_map = &map_first;
5153 num_segments = elf_elfheader (ibfd)->e_phnum;
5154 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5156 /* Returns the end address of the segment + 1. */
5157 #define SEGMENT_END(segment, start) \
5158 (start + (segment->p_memsz > segment->p_filesz \
5159 ? segment->p_memsz : segment->p_filesz))
5161 #define SECTION_SIZE(section, segment) \
5162 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5163 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5164 ? section->size : 0)
5166 /* Returns TRUE if the given section is contained within
5167 the given segment. VMA addresses are compared. */
5168 #define IS_CONTAINED_BY_VMA(section, segment) \
5169 (section->vma >= segment->p_vaddr \
5170 && (section->vma + SECTION_SIZE (section, segment) \
5171 <= (SEGMENT_END (segment, segment->p_vaddr))))
5173 /* Returns TRUE if the given section is contained within
5174 the given segment. LMA addresses are compared. */
5175 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5176 (section->lma >= base \
5177 && (section->lma + SECTION_SIZE (section, segment) \
5178 <= SEGMENT_END (segment, base)))
5180 /* Handle PT_NOTE segment. */
5181 #define IS_NOTE(p, s) \
5182 (p->p_type == PT_NOTE \
5183 && elf_section_type (s) == SHT_NOTE \
5184 && (bfd_vma) s->filepos >= p->p_offset \
5185 && ((bfd_vma) s->filepos + s->size \
5186 <= p->p_offset + p->p_filesz))
5188 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5190 #define IS_COREFILE_NOTE(p, s) \
5192 && bfd_get_format (ibfd) == bfd_core \
5196 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5197 linker, which generates a PT_INTERP section with p_vaddr and
5198 p_memsz set to 0. */
5199 #define IS_SOLARIS_PT_INTERP(p, s) \
5201 && p->p_paddr == 0 \
5202 && p->p_memsz == 0 \
5203 && p->p_filesz > 0 \
5204 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5206 && (bfd_vma) s->filepos >= p->p_offset \
5207 && ((bfd_vma) s->filepos + s->size \
5208 <= p->p_offset + p->p_filesz))
5210 /* Decide if the given section should be included in the given segment.
5211 A section will be included if:
5212 1. It is within the address space of the segment -- we use the LMA
5213 if that is set for the segment and the VMA otherwise,
5214 2. It is an allocated section or a NOTE section in a PT_NOTE
5216 3. There is an output section associated with it,
5217 4. The section has not already been allocated to a previous segment.
5218 5. PT_GNU_STACK segments do not include any sections.
5219 6. PT_TLS segment includes only SHF_TLS sections.
5220 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5221 8. PT_DYNAMIC should not contain empty sections at the beginning
5222 (with the possible exception of .dynamic). */
5223 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5224 ((((segment->p_paddr \
5225 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5226 : IS_CONTAINED_BY_VMA (section, segment)) \
5227 && (section->flags & SEC_ALLOC) != 0) \
5228 || IS_NOTE (segment, section)) \
5229 && segment->p_type != PT_GNU_STACK \
5230 && (segment->p_type != PT_TLS \
5231 || (section->flags & SEC_THREAD_LOCAL)) \
5232 && (segment->p_type == PT_LOAD \
5233 || segment->p_type == PT_TLS \
5234 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5235 && (segment->p_type != PT_DYNAMIC \
5236 || SECTION_SIZE (section, segment) > 0 \
5237 || (segment->p_paddr \
5238 ? segment->p_paddr != section->lma \
5239 : segment->p_vaddr != section->vma) \
5240 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5242 && !section->segment_mark)
5244 /* If the output section of a section in the input segment is NULL,
5245 it is removed from the corresponding output segment. */
5246 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5247 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5248 && section->output_section != NULL)
5250 /* Returns TRUE iff seg1 starts after the end of seg2. */
5251 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5252 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5254 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5255 their VMA address ranges and their LMA address ranges overlap.
5256 It is possible to have overlapping VMA ranges without overlapping LMA
5257 ranges. RedBoot images for example can have both .data and .bss mapped
5258 to the same VMA range, but with the .data section mapped to a different
5260 #define SEGMENT_OVERLAPS(seg1, seg2) \
5261 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5262 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5263 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5264 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5266 /* Initialise the segment mark field. */
5267 for (section = ibfd->sections; section != NULL; section = section->next)
5268 section->segment_mark = FALSE;
5270 /* The Solaris linker creates program headers in which all the
5271 p_paddr fields are zero. When we try to objcopy or strip such a
5272 file, we get confused. Check for this case, and if we find it
5273 don't set the p_paddr_valid fields. */
5274 p_paddr_valid = FALSE;
5275 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5278 if (segment->p_paddr != 0)
5280 p_paddr_valid = TRUE;
5284 /* Scan through the segments specified in the program header
5285 of the input BFD. For this first scan we look for overlaps
5286 in the loadable segments. These can be created by weird
5287 parameters to objcopy. Also, fix some solaris weirdness. */
5288 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5293 Elf_Internal_Phdr *segment2;
5295 if (segment->p_type == PT_INTERP)
5296 for (section = ibfd->sections; section; section = section->next)
5297 if (IS_SOLARIS_PT_INTERP (segment, section))
5299 /* Mininal change so that the normal section to segment
5300 assignment code will work. */
5301 segment->p_vaddr = section->vma;
5305 if (segment->p_type != PT_LOAD)
5307 /* Remove PT_GNU_RELRO segment. */
5308 if (segment->p_type == PT_GNU_RELRO)
5309 segment->p_type = PT_NULL;
5313 /* Determine if this segment overlaps any previous segments. */
5314 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5316 bfd_signed_vma extra_length;
5318 if (segment2->p_type != PT_LOAD
5319 || !SEGMENT_OVERLAPS (segment, segment2))
5322 /* Merge the two segments together. */
5323 if (segment2->p_vaddr < segment->p_vaddr)
5325 /* Extend SEGMENT2 to include SEGMENT and then delete
5327 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5328 - SEGMENT_END (segment2, segment2->p_vaddr));
5330 if (extra_length > 0)
5332 segment2->p_memsz += extra_length;
5333 segment2->p_filesz += extra_length;
5336 segment->p_type = PT_NULL;
5338 /* Since we have deleted P we must restart the outer loop. */
5340 segment = elf_tdata (ibfd)->phdr;
5345 /* Extend SEGMENT to include SEGMENT2 and then delete
5347 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5348 - SEGMENT_END (segment, segment->p_vaddr));
5350 if (extra_length > 0)
5352 segment->p_memsz += extra_length;
5353 segment->p_filesz += extra_length;
5356 segment2->p_type = PT_NULL;
5361 /* The second scan attempts to assign sections to segments. */
5362 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5366 unsigned int section_count;
5367 asection **sections;
5368 asection *output_section;
5370 bfd_vma matching_lma;
5371 bfd_vma suggested_lma;
5374 asection *first_section;
5375 bfd_boolean first_matching_lma;
5376 bfd_boolean first_suggested_lma;
5378 if (segment->p_type == PT_NULL)
5381 first_section = NULL;
5382 /* Compute how many sections might be placed into this segment. */
5383 for (section = ibfd->sections, section_count = 0;
5385 section = section->next)
5387 /* Find the first section in the input segment, which may be
5388 removed from the corresponding output segment. */
5389 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5391 if (first_section == NULL)
5392 first_section = section;
5393 if (section->output_section != NULL)
5398 /* Allocate a segment map big enough to contain
5399 all of the sections we have selected. */
5400 amt = sizeof (struct elf_segment_map);
5401 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5402 map = bfd_zalloc (obfd, amt);
5406 /* Initialise the fields of the segment map. Default to
5407 using the physical address of the segment in the input BFD. */
5409 map->p_type = segment->p_type;
5410 map->p_flags = segment->p_flags;
5411 map->p_flags_valid = 1;
5413 /* If the first section in the input segment is removed, there is
5414 no need to preserve segment physical address in the corresponding
5416 if (!first_section || first_section->output_section != NULL)
5418 map->p_paddr = segment->p_paddr;
5419 map->p_paddr_valid = p_paddr_valid;
5422 /* Determine if this segment contains the ELF file header
5423 and if it contains the program headers themselves. */
5424 map->includes_filehdr = (segment->p_offset == 0
5425 && segment->p_filesz >= iehdr->e_ehsize);
5426 map->includes_phdrs = 0;
5428 if (!phdr_included || segment->p_type != PT_LOAD)
5430 map->includes_phdrs =
5431 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5432 && (segment->p_offset + segment->p_filesz
5433 >= ((bfd_vma) iehdr->e_phoff
5434 + iehdr->e_phnum * iehdr->e_phentsize)));
5436 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5437 phdr_included = TRUE;
5440 if (section_count == 0)
5442 /* Special segments, such as the PT_PHDR segment, may contain
5443 no sections, but ordinary, loadable segments should contain
5444 something. They are allowed by the ELF spec however, so only
5445 a warning is produced. */
5446 if (segment->p_type == PT_LOAD)
5447 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5448 " detected, is this intentional ?\n"),
5452 *pointer_to_map = map;
5453 pointer_to_map = &map->next;
5458 /* Now scan the sections in the input BFD again and attempt
5459 to add their corresponding output sections to the segment map.
5460 The problem here is how to handle an output section which has
5461 been moved (ie had its LMA changed). There are four possibilities:
5463 1. None of the sections have been moved.
5464 In this case we can continue to use the segment LMA from the
5467 2. All of the sections have been moved by the same amount.
5468 In this case we can change the segment's LMA to match the LMA
5469 of the first section.
5471 3. Some of the sections have been moved, others have not.
5472 In this case those sections which have not been moved can be
5473 placed in the current segment which will have to have its size,
5474 and possibly its LMA changed, and a new segment or segments will
5475 have to be created to contain the other sections.
5477 4. The sections have been moved, but not by the same amount.
5478 In this case we can change the segment's LMA to match the LMA
5479 of the first section and we will have to create a new segment
5480 or segments to contain the other sections.
5482 In order to save time, we allocate an array to hold the section
5483 pointers that we are interested in. As these sections get assigned
5484 to a segment, they are removed from this array. */
5486 sections = bfd_malloc2 (section_count, sizeof (asection *));
5487 if (sections == NULL)
5490 /* Step One: Scan for segment vs section LMA conflicts.
5491 Also add the sections to the section array allocated above.
5492 Also add the sections to the current segment. In the common
5493 case, where the sections have not been moved, this means that
5494 we have completely filled the segment, and there is nothing
5499 first_matching_lma = TRUE;
5500 first_suggested_lma = TRUE;
5502 for (section = ibfd->sections;
5504 section = section->next)
5505 if (section == first_section)
5508 for (j = 0; section != NULL; section = section->next)
5510 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5512 output_section = section->output_section;
5514 sections[j++] = section;
5516 /* The Solaris native linker always sets p_paddr to 0.
5517 We try to catch that case here, and set it to the
5518 correct value. Note - some backends require that
5519 p_paddr be left as zero. */
5521 && segment->p_vaddr != 0
5522 && !bed->want_p_paddr_set_to_zero
5524 && output_section->lma != 0
5525 && output_section->vma == (segment->p_vaddr
5526 + (map->includes_filehdr
5529 + (map->includes_phdrs
5531 * iehdr->e_phentsize)
5533 map->p_paddr = segment->p_vaddr;
5535 /* Match up the physical address of the segment with the
5536 LMA address of the output section. */
5537 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5538 || IS_COREFILE_NOTE (segment, section)
5539 || (bed->want_p_paddr_set_to_zero
5540 && IS_CONTAINED_BY_VMA (output_section, segment)))
5542 if (first_matching_lma || output_section->lma < matching_lma)
5544 matching_lma = output_section->lma;
5545 first_matching_lma = FALSE;
5548 /* We assume that if the section fits within the segment
5549 then it does not overlap any other section within that
5551 map->sections[isec++] = output_section;
5553 else if (first_suggested_lma)
5555 suggested_lma = output_section->lma;
5556 first_suggested_lma = FALSE;
5559 if (j == section_count)
5564 BFD_ASSERT (j == section_count);
5566 /* Step Two: Adjust the physical address of the current segment,
5568 if (isec == section_count)
5570 /* All of the sections fitted within the segment as currently
5571 specified. This is the default case. Add the segment to
5572 the list of built segments and carry on to process the next
5573 program header in the input BFD. */
5574 map->count = section_count;
5575 *pointer_to_map = map;
5576 pointer_to_map = &map->next;
5579 && !bed->want_p_paddr_set_to_zero
5580 && matching_lma != map->p_paddr
5581 && !map->includes_filehdr
5582 && !map->includes_phdrs)
5583 /* There is some padding before the first section in the
5584 segment. So, we must account for that in the output
5586 map->p_vaddr_offset = matching_lma - map->p_paddr;
5593 if (!first_matching_lma)
5595 /* At least one section fits inside the current segment.
5596 Keep it, but modify its physical address to match the
5597 LMA of the first section that fitted. */
5598 map->p_paddr = matching_lma;
5602 /* None of the sections fitted inside the current segment.
5603 Change the current segment's physical address to match
5604 the LMA of the first section. */
5605 map->p_paddr = suggested_lma;
5608 /* Offset the segment physical address from the lma
5609 to allow for space taken up by elf headers. */
5610 if (map->includes_filehdr)
5612 if (map->p_paddr >= iehdr->e_ehsize)
5613 map->p_paddr -= iehdr->e_ehsize;
5616 map->includes_filehdr = FALSE;
5617 map->includes_phdrs = FALSE;
5621 if (map->includes_phdrs)
5623 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5625 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5627 /* iehdr->e_phnum is just an estimate of the number
5628 of program headers that we will need. Make a note
5629 here of the number we used and the segment we chose
5630 to hold these headers, so that we can adjust the
5631 offset when we know the correct value. */
5632 phdr_adjust_num = iehdr->e_phnum;
5633 phdr_adjust_seg = map;
5636 map->includes_phdrs = FALSE;
5640 /* Step Three: Loop over the sections again, this time assigning
5641 those that fit to the current segment and removing them from the
5642 sections array; but making sure not to leave large gaps. Once all
5643 possible sections have been assigned to the current segment it is
5644 added to the list of built segments and if sections still remain
5645 to be assigned, a new segment is constructed before repeating
5652 first_suggested_lma = TRUE;
5654 /* Fill the current segment with sections that fit. */
5655 for (j = 0; j < section_count; j++)
5657 section = sections[j];
5659 if (section == NULL)
5662 output_section = section->output_section;
5664 BFD_ASSERT (output_section != NULL);
5666 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5667 || IS_COREFILE_NOTE (segment, section))
5669 if (map->count == 0)
5671 /* If the first section in a segment does not start at
5672 the beginning of the segment, then something is
5674 if (output_section->lma
5676 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5677 + (map->includes_phdrs
5678 ? iehdr->e_phnum * iehdr->e_phentsize
5686 prev_sec = map->sections[map->count - 1];
5688 /* If the gap between the end of the previous section
5689 and the start of this section is more than
5690 maxpagesize then we need to start a new segment. */
5691 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5693 < BFD_ALIGN (output_section->lma, maxpagesize))
5694 || (prev_sec->lma + prev_sec->size
5695 > output_section->lma))
5697 if (first_suggested_lma)
5699 suggested_lma = output_section->lma;
5700 first_suggested_lma = FALSE;
5707 map->sections[map->count++] = output_section;
5710 section->segment_mark = TRUE;
5712 else if (first_suggested_lma)
5714 suggested_lma = output_section->lma;
5715 first_suggested_lma = FALSE;
5719 BFD_ASSERT (map->count > 0);
5721 /* Add the current segment to the list of built segments. */
5722 *pointer_to_map = map;
5723 pointer_to_map = &map->next;
5725 if (isec < section_count)
5727 /* We still have not allocated all of the sections to
5728 segments. Create a new segment here, initialise it
5729 and carry on looping. */
5730 amt = sizeof (struct elf_segment_map);
5731 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5732 map = bfd_alloc (obfd, amt);
5739 /* Initialise the fields of the segment map. Set the physical
5740 physical address to the LMA of the first section that has
5741 not yet been assigned. */
5743 map->p_type = segment->p_type;
5744 map->p_flags = segment->p_flags;
5745 map->p_flags_valid = 1;
5746 map->p_paddr = suggested_lma;
5747 map->p_paddr_valid = p_paddr_valid;
5748 map->includes_filehdr = 0;
5749 map->includes_phdrs = 0;
5752 while (isec < section_count);
5757 elf_tdata (obfd)->segment_map = map_first;
5759 /* If we had to estimate the number of program headers that were
5760 going to be needed, then check our estimate now and adjust
5761 the offset if necessary. */
5762 if (phdr_adjust_seg != NULL)
5766 for (count = 0, map = map_first; map != NULL; map = map->next)
5769 if (count > phdr_adjust_num)
5770 phdr_adjust_seg->p_paddr
5771 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5776 #undef IS_CONTAINED_BY_VMA
5777 #undef IS_CONTAINED_BY_LMA
5779 #undef IS_COREFILE_NOTE
5780 #undef IS_SOLARIS_PT_INTERP
5781 #undef IS_SECTION_IN_INPUT_SEGMENT
5782 #undef INCLUDE_SECTION_IN_SEGMENT
5783 #undef SEGMENT_AFTER_SEGMENT
5784 #undef SEGMENT_OVERLAPS
5788 /* Copy ELF program header information. */
5791 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5793 Elf_Internal_Ehdr *iehdr;
5794 struct elf_segment_map *map;
5795 struct elf_segment_map *map_first;
5796 struct elf_segment_map **pointer_to_map;
5797 Elf_Internal_Phdr *segment;
5799 unsigned int num_segments;
5800 bfd_boolean phdr_included = FALSE;
5801 bfd_boolean p_paddr_valid;
5803 iehdr = elf_elfheader (ibfd);
5806 pointer_to_map = &map_first;
5808 /* If all the segment p_paddr fields are zero, don't set
5809 map->p_paddr_valid. */
5810 p_paddr_valid = FALSE;
5811 num_segments = elf_elfheader (ibfd)->e_phnum;
5812 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5815 if (segment->p_paddr != 0)
5817 p_paddr_valid = TRUE;
5821 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5826 unsigned int section_count;
5828 Elf_Internal_Shdr *this_hdr;
5829 asection *first_section = NULL;
5830 asection *lowest_section = NULL;
5832 /* Compute how many sections are in this segment. */
5833 for (section = ibfd->sections, section_count = 0;
5835 section = section->next)
5837 this_hdr = &(elf_section_data(section)->this_hdr);
5838 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5841 first_section = lowest_section = section;
5842 if (section->lma < lowest_section->lma)
5843 lowest_section = section;
5848 /* Allocate a segment map big enough to contain
5849 all of the sections we have selected. */
5850 amt = sizeof (struct elf_segment_map);
5851 if (section_count != 0)
5852 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5853 map = bfd_zalloc (obfd, amt);
5857 /* Initialize the fields of the output segment map with the
5860 map->p_type = segment->p_type;
5861 map->p_flags = segment->p_flags;
5862 map->p_flags_valid = 1;
5863 map->p_paddr = segment->p_paddr;
5864 map->p_paddr_valid = p_paddr_valid;
5865 map->p_align = segment->p_align;
5866 map->p_align_valid = 1;
5867 map->p_vaddr_offset = 0;
5869 if (map->p_type == PT_GNU_RELRO
5870 && segment->p_filesz == segment->p_memsz)
5872 /* The PT_GNU_RELRO segment may contain the first a few
5873 bytes in the .got.plt section even if the whole .got.plt
5874 section isn't in the PT_GNU_RELRO segment. We won't
5875 change the size of the PT_GNU_RELRO segment. */
5876 map->p_size = segment->p_filesz;
5877 map->p_size_valid = 1;
5880 /* Determine if this segment contains the ELF file header
5881 and if it contains the program headers themselves. */
5882 map->includes_filehdr = (segment->p_offset == 0
5883 && segment->p_filesz >= iehdr->e_ehsize);
5885 map->includes_phdrs = 0;
5886 if (! phdr_included || segment->p_type != PT_LOAD)
5888 map->includes_phdrs =
5889 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5890 && (segment->p_offset + segment->p_filesz
5891 >= ((bfd_vma) iehdr->e_phoff
5892 + iehdr->e_phnum * iehdr->e_phentsize)));
5894 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5895 phdr_included = TRUE;
5898 if (map->includes_filehdr && first_section)
5899 /* We need to keep the space used by the headers fixed. */
5900 map->header_size = first_section->vma - segment->p_vaddr;
5902 if (!map->includes_phdrs
5903 && !map->includes_filehdr
5904 && map->p_paddr_valid)
5905 /* There is some other padding before the first section. */
5906 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5907 - segment->p_paddr);
5909 if (section_count != 0)
5911 unsigned int isec = 0;
5913 for (section = first_section;
5915 section = section->next)
5917 this_hdr = &(elf_section_data(section)->this_hdr);
5918 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5920 map->sections[isec++] = section->output_section;
5921 if (isec == section_count)
5927 map->count = section_count;
5928 *pointer_to_map = map;
5929 pointer_to_map = &map->next;
5932 elf_tdata (obfd)->segment_map = map_first;
5936 /* Copy private BFD data. This copies or rewrites ELF program header
5940 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5942 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5943 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5946 if (elf_tdata (ibfd)->phdr == NULL)
5949 if (ibfd->xvec == obfd->xvec)
5951 /* Check to see if any sections in the input BFD
5952 covered by ELF program header have changed. */
5953 Elf_Internal_Phdr *segment;
5954 asection *section, *osec;
5955 unsigned int i, num_segments;
5956 Elf_Internal_Shdr *this_hdr;
5957 const struct elf_backend_data *bed;
5959 bed = get_elf_backend_data (ibfd);
5961 /* Regenerate the segment map if p_paddr is set to 0. */
5962 if (bed->want_p_paddr_set_to_zero)
5965 /* Initialize the segment mark field. */
5966 for (section = obfd->sections; section != NULL;
5967 section = section->next)
5968 section->segment_mark = FALSE;
5970 num_segments = elf_elfheader (ibfd)->e_phnum;
5971 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5975 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5976 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5977 which severly confuses things, so always regenerate the segment
5978 map in this case. */
5979 if (segment->p_paddr == 0
5980 && segment->p_memsz == 0
5981 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5984 for (section = ibfd->sections;
5985 section != NULL; section = section->next)
5987 /* We mark the output section so that we know it comes
5988 from the input BFD. */
5989 osec = section->output_section;
5991 osec->segment_mark = TRUE;
5993 /* Check if this section is covered by the segment. */
5994 this_hdr = &(elf_section_data(section)->this_hdr);
5995 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5997 /* FIXME: Check if its output section is changed or
5998 removed. What else do we need to check? */
6000 || section->flags != osec->flags
6001 || section->lma != osec->lma
6002 || section->vma != osec->vma
6003 || section->size != osec->size
6004 || section->rawsize != osec->rawsize
6005 || section->alignment_power != osec->alignment_power)
6011 /* Check to see if any output section do not come from the
6013 for (section = obfd->sections; section != NULL;
6014 section = section->next)
6016 if (section->segment_mark == FALSE)
6019 section->segment_mark = FALSE;
6022 return copy_elf_program_header (ibfd, obfd);
6026 return rewrite_elf_program_header (ibfd, obfd);
6029 /* Initialize private output section information from input section. */
6032 _bfd_elf_init_private_section_data (bfd *ibfd,
6036 struct bfd_link_info *link_info)
6039 Elf_Internal_Shdr *ihdr, *ohdr;
6040 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
6042 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6043 || obfd->xvec->flavour != bfd_target_elf_flavour)
6046 /* Don't copy the output ELF section type from input if the
6047 output BFD section flags have been set to something different.
6048 elf_fake_sections will set ELF section type based on BFD
6050 if (elf_section_type (osec) == SHT_NULL
6051 && (osec->flags == isec->flags || !osec->flags))
6052 elf_section_type (osec) = elf_section_type (isec);
6054 /* FIXME: Is this correct for all OS/PROC specific flags? */
6055 elf_section_flags (osec) |= (elf_section_flags (isec)
6056 & (SHF_MASKOS | SHF_MASKPROC));
6058 /* Set things up for objcopy and relocatable link. The output
6059 SHT_GROUP section will have its elf_next_in_group pointing back
6060 to the input group members. Ignore linker created group section.
6061 See elfNN_ia64_object_p in elfxx-ia64.c. */
6064 if (elf_sec_group (isec) == NULL
6065 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6067 if (elf_section_flags (isec) & SHF_GROUP)
6068 elf_section_flags (osec) |= SHF_GROUP;
6069 elf_next_in_group (osec) = elf_next_in_group (isec);
6070 elf_section_data (osec)->group = elf_section_data (isec)->group;
6074 ihdr = &elf_section_data (isec)->this_hdr;
6076 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6077 don't use the output section of the linked-to section since it
6078 may be NULL at this point. */
6079 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6081 ohdr = &elf_section_data (osec)->this_hdr;
6082 ohdr->sh_flags |= SHF_LINK_ORDER;
6083 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6086 osec->use_rela_p = isec->use_rela_p;
6091 /* Copy private section information. This copies over the entsize
6092 field, and sometimes the info field. */
6095 _bfd_elf_copy_private_section_data (bfd *ibfd,
6100 Elf_Internal_Shdr *ihdr, *ohdr;
6102 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6103 || obfd->xvec->flavour != bfd_target_elf_flavour)
6106 ihdr = &elf_section_data (isec)->this_hdr;
6107 ohdr = &elf_section_data (osec)->this_hdr;
6109 ohdr->sh_entsize = ihdr->sh_entsize;
6111 if (ihdr->sh_type == SHT_SYMTAB
6112 || ihdr->sh_type == SHT_DYNSYM
6113 || ihdr->sh_type == SHT_GNU_verneed
6114 || ihdr->sh_type == SHT_GNU_verdef)
6115 ohdr->sh_info = ihdr->sh_info;
6117 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6121 /* Copy private header information. */
6124 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6128 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6129 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6132 /* Copy over private BFD data if it has not already been copied.
6133 This must be done here, rather than in the copy_private_bfd_data
6134 entry point, because the latter is called after the section
6135 contents have been set, which means that the program headers have
6136 already been worked out. */
6137 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6139 if (! copy_private_bfd_data (ibfd, obfd))
6143 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6144 but this might be wrong if we deleted the group section. */
6145 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6146 if (elf_section_type (isec) == SHT_GROUP
6147 && isec->output_section == NULL)
6149 asection *first = elf_next_in_group (isec);
6150 asection *s = first;
6153 if (s->output_section != NULL)
6155 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6156 elf_group_name (s->output_section) = NULL;
6158 s = elf_next_in_group (s);
6167 /* Copy private symbol information. If this symbol is in a section
6168 which we did not map into a BFD section, try to map the section
6169 index correctly. We use special macro definitions for the mapped
6170 section indices; these definitions are interpreted by the
6171 swap_out_syms function. */
6173 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6174 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6175 #define MAP_STRTAB (SHN_HIOS + 3)
6176 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6177 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6180 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6185 elf_symbol_type *isym, *osym;
6187 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6188 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6191 isym = elf_symbol_from (ibfd, isymarg);
6192 osym = elf_symbol_from (obfd, osymarg);
6195 && isym->internal_elf_sym.st_shndx != 0
6197 && bfd_is_abs_section (isym->symbol.section))
6201 shndx = isym->internal_elf_sym.st_shndx;
6202 if (shndx == elf_onesymtab (ibfd))
6203 shndx = MAP_ONESYMTAB;
6204 else if (shndx == elf_dynsymtab (ibfd))
6205 shndx = MAP_DYNSYMTAB;
6206 else if (shndx == elf_tdata (ibfd)->strtab_section)
6208 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6209 shndx = MAP_SHSTRTAB;
6210 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6211 shndx = MAP_SYM_SHNDX;
6212 osym->internal_elf_sym.st_shndx = shndx;
6218 /* Swap out the symbols. */
6221 swap_out_syms (bfd *abfd,
6222 struct bfd_strtab_hash **sttp,
6225 const struct elf_backend_data *bed;
6228 struct bfd_strtab_hash *stt;
6229 Elf_Internal_Shdr *symtab_hdr;
6230 Elf_Internal_Shdr *symtab_shndx_hdr;
6231 Elf_Internal_Shdr *symstrtab_hdr;
6232 bfd_byte *outbound_syms;
6233 bfd_byte *outbound_shndx;
6236 bfd_boolean name_local_sections;
6238 if (!elf_map_symbols (abfd))
6241 /* Dump out the symtabs. */
6242 stt = _bfd_elf_stringtab_init ();
6246 bed = get_elf_backend_data (abfd);
6247 symcount = bfd_get_symcount (abfd);
6248 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6249 symtab_hdr->sh_type = SHT_SYMTAB;
6250 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6251 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6252 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6253 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6255 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6256 symstrtab_hdr->sh_type = SHT_STRTAB;
6258 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6259 if (outbound_syms == NULL)
6261 _bfd_stringtab_free (stt);
6264 symtab_hdr->contents = outbound_syms;
6266 outbound_shndx = NULL;
6267 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6268 if (symtab_shndx_hdr->sh_name != 0)
6270 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6271 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6272 sizeof (Elf_External_Sym_Shndx));
6273 if (outbound_shndx == NULL)
6275 _bfd_stringtab_free (stt);
6279 symtab_shndx_hdr->contents = outbound_shndx;
6280 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6281 symtab_shndx_hdr->sh_size = amt;
6282 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6283 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6286 /* Now generate the data (for "contents"). */
6288 /* Fill in zeroth symbol and swap it out. */
6289 Elf_Internal_Sym sym;
6295 sym.st_shndx = SHN_UNDEF;
6296 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6297 outbound_syms += bed->s->sizeof_sym;
6298 if (outbound_shndx != NULL)
6299 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6303 = (bed->elf_backend_name_local_section_symbols
6304 && bed->elf_backend_name_local_section_symbols (abfd));
6306 syms = bfd_get_outsymbols (abfd);
6307 for (idx = 0; idx < symcount; idx++)
6309 Elf_Internal_Sym sym;
6310 bfd_vma value = syms[idx]->value;
6311 elf_symbol_type *type_ptr;
6312 flagword flags = syms[idx]->flags;
6315 if (!name_local_sections
6316 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6318 /* Local section symbols have no name. */
6323 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6326 if (sym.st_name == (unsigned long) -1)
6328 _bfd_stringtab_free (stt);
6333 type_ptr = elf_symbol_from (abfd, syms[idx]);
6335 if ((flags & BSF_SECTION_SYM) == 0
6336 && bfd_is_com_section (syms[idx]->section))
6338 /* ELF common symbols put the alignment into the `value' field,
6339 and the size into the `size' field. This is backwards from
6340 how BFD handles it, so reverse it here. */
6341 sym.st_size = value;
6342 if (type_ptr == NULL
6343 || type_ptr->internal_elf_sym.st_value == 0)
6344 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6346 sym.st_value = type_ptr->internal_elf_sym.st_value;
6347 sym.st_shndx = _bfd_elf_section_from_bfd_section
6348 (abfd, syms[idx]->section);
6352 asection *sec = syms[idx]->section;
6355 if (sec->output_section)
6357 value += sec->output_offset;
6358 sec = sec->output_section;
6361 /* Don't add in the section vma for relocatable output. */
6362 if (! relocatable_p)
6364 sym.st_value = value;
6365 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6367 if (bfd_is_abs_section (sec)
6369 && type_ptr->internal_elf_sym.st_shndx != 0)
6371 /* This symbol is in a real ELF section which we did
6372 not create as a BFD section. Undo the mapping done
6373 by copy_private_symbol_data. */
6374 shndx = type_ptr->internal_elf_sym.st_shndx;
6378 shndx = elf_onesymtab (abfd);
6381 shndx = elf_dynsymtab (abfd);
6384 shndx = elf_tdata (abfd)->strtab_section;
6387 shndx = elf_tdata (abfd)->shstrtab_section;
6390 shndx = elf_tdata (abfd)->symtab_shndx_section;
6398 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6400 if (shndx == SHN_BAD)
6404 /* Writing this would be a hell of a lot easier if
6405 we had some decent documentation on bfd, and
6406 knew what to expect of the library, and what to
6407 demand of applications. For example, it
6408 appears that `objcopy' might not set the
6409 section of a symbol to be a section that is
6410 actually in the output file. */
6411 sec2 = bfd_get_section_by_name (abfd, sec->name);
6414 _bfd_error_handler (_("\
6415 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6416 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6418 bfd_set_error (bfd_error_invalid_operation);
6419 _bfd_stringtab_free (stt);
6423 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6424 BFD_ASSERT (shndx != SHN_BAD);
6428 sym.st_shndx = shndx;
6431 if ((flags & BSF_THREAD_LOCAL) != 0)
6433 else if ((flags & BSF_FUNCTION) != 0)
6435 else if ((flags & BSF_OBJECT) != 0)
6437 else if ((flags & BSF_RELC) != 0)
6439 else if ((flags & BSF_SRELC) != 0)
6444 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6447 /* Processor-specific types. */
6448 if (type_ptr != NULL
6449 && bed->elf_backend_get_symbol_type)
6450 type = ((*bed->elf_backend_get_symbol_type)
6451 (&type_ptr->internal_elf_sym, type));
6453 if (flags & BSF_SECTION_SYM)
6455 if (flags & BSF_GLOBAL)
6456 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6458 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6460 else if (bfd_is_com_section (syms[idx]->section))
6462 #ifdef USE_STT_COMMON
6463 if (type == STT_OBJECT)
6464 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6467 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6469 else if (bfd_is_und_section (syms[idx]->section))
6470 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6474 else if (flags & BSF_FILE)
6475 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6478 int bind = STB_LOCAL;
6480 if (flags & BSF_LOCAL)
6482 else if (flags & BSF_WEAK)
6484 else if (flags & BSF_GLOBAL)
6487 sym.st_info = ELF_ST_INFO (bind, type);
6490 if (type_ptr != NULL)
6491 sym.st_other = type_ptr->internal_elf_sym.st_other;
6495 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6496 outbound_syms += bed->s->sizeof_sym;
6497 if (outbound_shndx != NULL)
6498 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6502 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6503 symstrtab_hdr->sh_type = SHT_STRTAB;
6505 symstrtab_hdr->sh_flags = 0;
6506 symstrtab_hdr->sh_addr = 0;
6507 symstrtab_hdr->sh_entsize = 0;
6508 symstrtab_hdr->sh_link = 0;
6509 symstrtab_hdr->sh_info = 0;
6510 symstrtab_hdr->sh_addralign = 1;
6515 /* Return the number of bytes required to hold the symtab vector.
6517 Note that we base it on the count plus 1, since we will null terminate
6518 the vector allocated based on this size. However, the ELF symbol table
6519 always has a dummy entry as symbol #0, so it ends up even. */
6522 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6526 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6528 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6529 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6531 symtab_size -= sizeof (asymbol *);
6537 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6541 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6543 if (elf_dynsymtab (abfd) == 0)
6545 bfd_set_error (bfd_error_invalid_operation);
6549 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6550 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6552 symtab_size -= sizeof (asymbol *);
6558 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6561 return (asect->reloc_count + 1) * sizeof (arelent *);
6564 /* Canonicalize the relocs. */
6567 _bfd_elf_canonicalize_reloc (bfd *abfd,
6574 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6576 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6579 tblptr = section->relocation;
6580 for (i = 0; i < section->reloc_count; i++)
6581 *relptr++ = tblptr++;
6585 return section->reloc_count;
6589 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6591 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6592 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6595 bfd_get_symcount (abfd) = symcount;
6600 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6601 asymbol **allocation)
6603 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6604 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6607 bfd_get_dynamic_symcount (abfd) = symcount;
6611 /* Return the size required for the dynamic reloc entries. Any loadable
6612 section that was actually installed in the BFD, and has type SHT_REL
6613 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6614 dynamic reloc section. */
6617 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6622 if (elf_dynsymtab (abfd) == 0)
6624 bfd_set_error (bfd_error_invalid_operation);
6628 ret = sizeof (arelent *);
6629 for (s = abfd->sections; s != NULL; s = s->next)
6630 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6631 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6632 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6633 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6634 * sizeof (arelent *));
6639 /* Canonicalize the dynamic relocation entries. Note that we return the
6640 dynamic relocations as a single block, although they are actually
6641 associated with particular sections; the interface, which was
6642 designed for SunOS style shared libraries, expects that there is only
6643 one set of dynamic relocs. Any loadable section that was actually
6644 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6645 dynamic symbol table, is considered to be a dynamic reloc section. */
6648 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6652 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6656 if (elf_dynsymtab (abfd) == 0)
6658 bfd_set_error (bfd_error_invalid_operation);
6662 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6664 for (s = abfd->sections; s != NULL; s = s->next)
6666 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6667 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6668 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6673 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6675 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6677 for (i = 0; i < count; i++)
6688 /* Read in the version information. */
6691 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6693 bfd_byte *contents = NULL;
6694 unsigned int freeidx = 0;
6696 if (elf_dynverref (abfd) != 0)
6698 Elf_Internal_Shdr *hdr;
6699 Elf_External_Verneed *everneed;
6700 Elf_Internal_Verneed *iverneed;
6702 bfd_byte *contents_end;
6704 hdr = &elf_tdata (abfd)->dynverref_hdr;
6706 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6707 sizeof (Elf_Internal_Verneed));
6708 if (elf_tdata (abfd)->verref == NULL)
6711 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6713 contents = bfd_malloc (hdr->sh_size);
6714 if (contents == NULL)
6716 error_return_verref:
6717 elf_tdata (abfd)->verref = NULL;
6718 elf_tdata (abfd)->cverrefs = 0;
6721 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6722 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6723 goto error_return_verref;
6725 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6726 goto error_return_verref;
6728 BFD_ASSERT (sizeof (Elf_External_Verneed)
6729 == sizeof (Elf_External_Vernaux));
6730 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6731 everneed = (Elf_External_Verneed *) contents;
6732 iverneed = elf_tdata (abfd)->verref;
6733 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6735 Elf_External_Vernaux *evernaux;
6736 Elf_Internal_Vernaux *ivernaux;
6739 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6741 iverneed->vn_bfd = abfd;
6743 iverneed->vn_filename =
6744 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6746 if (iverneed->vn_filename == NULL)
6747 goto error_return_verref;
6749 if (iverneed->vn_cnt == 0)
6750 iverneed->vn_auxptr = NULL;
6753 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6754 sizeof (Elf_Internal_Vernaux));
6755 if (iverneed->vn_auxptr == NULL)
6756 goto error_return_verref;
6759 if (iverneed->vn_aux
6760 > (size_t) (contents_end - (bfd_byte *) everneed))
6761 goto error_return_verref;
6763 evernaux = ((Elf_External_Vernaux *)
6764 ((bfd_byte *) everneed + iverneed->vn_aux));
6765 ivernaux = iverneed->vn_auxptr;
6766 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6768 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6770 ivernaux->vna_nodename =
6771 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6772 ivernaux->vna_name);
6773 if (ivernaux->vna_nodename == NULL)
6774 goto error_return_verref;
6776 if (j + 1 < iverneed->vn_cnt)
6777 ivernaux->vna_nextptr = ivernaux + 1;
6779 ivernaux->vna_nextptr = NULL;
6781 if (ivernaux->vna_next
6782 > (size_t) (contents_end - (bfd_byte *) evernaux))
6783 goto error_return_verref;
6785 evernaux = ((Elf_External_Vernaux *)
6786 ((bfd_byte *) evernaux + ivernaux->vna_next));
6788 if (ivernaux->vna_other > freeidx)
6789 freeidx = ivernaux->vna_other;
6792 if (i + 1 < hdr->sh_info)
6793 iverneed->vn_nextref = iverneed + 1;
6795 iverneed->vn_nextref = NULL;
6797 if (iverneed->vn_next
6798 > (size_t) (contents_end - (bfd_byte *) everneed))
6799 goto error_return_verref;
6801 everneed = ((Elf_External_Verneed *)
6802 ((bfd_byte *) everneed + iverneed->vn_next));
6809 if (elf_dynverdef (abfd) != 0)
6811 Elf_Internal_Shdr *hdr;
6812 Elf_External_Verdef *everdef;
6813 Elf_Internal_Verdef *iverdef;
6814 Elf_Internal_Verdef *iverdefarr;
6815 Elf_Internal_Verdef iverdefmem;
6817 unsigned int maxidx;
6818 bfd_byte *contents_end_def, *contents_end_aux;
6820 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6822 contents = bfd_malloc (hdr->sh_size);
6823 if (contents == NULL)
6825 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6826 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6829 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6832 BFD_ASSERT (sizeof (Elf_External_Verdef)
6833 >= sizeof (Elf_External_Verdaux));
6834 contents_end_def = contents + hdr->sh_size
6835 - sizeof (Elf_External_Verdef);
6836 contents_end_aux = contents + hdr->sh_size
6837 - sizeof (Elf_External_Verdaux);
6839 /* We know the number of entries in the section but not the maximum
6840 index. Therefore we have to run through all entries and find
6842 everdef = (Elf_External_Verdef *) contents;
6844 for (i = 0; i < hdr->sh_info; ++i)
6846 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6848 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6849 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6851 if (iverdefmem.vd_next
6852 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6855 everdef = ((Elf_External_Verdef *)
6856 ((bfd_byte *) everdef + iverdefmem.vd_next));
6859 if (default_imported_symver)
6861 if (freeidx > maxidx)
6866 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6867 sizeof (Elf_Internal_Verdef));
6868 if (elf_tdata (abfd)->verdef == NULL)
6871 elf_tdata (abfd)->cverdefs = maxidx;
6873 everdef = (Elf_External_Verdef *) contents;
6874 iverdefarr = elf_tdata (abfd)->verdef;
6875 for (i = 0; i < hdr->sh_info; i++)
6877 Elf_External_Verdaux *everdaux;
6878 Elf_Internal_Verdaux *iverdaux;
6881 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6883 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6885 error_return_verdef:
6886 elf_tdata (abfd)->verdef = NULL;
6887 elf_tdata (abfd)->cverdefs = 0;
6891 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6892 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6894 iverdef->vd_bfd = abfd;
6896 if (iverdef->vd_cnt == 0)
6897 iverdef->vd_auxptr = NULL;
6900 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6901 sizeof (Elf_Internal_Verdaux));
6902 if (iverdef->vd_auxptr == NULL)
6903 goto error_return_verdef;
6907 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6908 goto error_return_verdef;
6910 everdaux = ((Elf_External_Verdaux *)
6911 ((bfd_byte *) everdef + iverdef->vd_aux));
6912 iverdaux = iverdef->vd_auxptr;
6913 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6915 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6917 iverdaux->vda_nodename =
6918 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6919 iverdaux->vda_name);
6920 if (iverdaux->vda_nodename == NULL)
6921 goto error_return_verdef;
6923 if (j + 1 < iverdef->vd_cnt)
6924 iverdaux->vda_nextptr = iverdaux + 1;
6926 iverdaux->vda_nextptr = NULL;
6928 if (iverdaux->vda_next
6929 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6930 goto error_return_verdef;
6932 everdaux = ((Elf_External_Verdaux *)
6933 ((bfd_byte *) everdaux + iverdaux->vda_next));
6936 if (iverdef->vd_cnt)
6937 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6939 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6940 iverdef->vd_nextdef = iverdef + 1;
6942 iverdef->vd_nextdef = NULL;
6944 everdef = ((Elf_External_Verdef *)
6945 ((bfd_byte *) everdef + iverdef->vd_next));
6951 else if (default_imported_symver)
6958 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6959 sizeof (Elf_Internal_Verdef));
6960 if (elf_tdata (abfd)->verdef == NULL)
6963 elf_tdata (abfd)->cverdefs = freeidx;
6966 /* Create a default version based on the soname. */
6967 if (default_imported_symver)
6969 Elf_Internal_Verdef *iverdef;
6970 Elf_Internal_Verdaux *iverdaux;
6972 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6974 iverdef->vd_version = VER_DEF_CURRENT;
6975 iverdef->vd_flags = 0;
6976 iverdef->vd_ndx = freeidx;
6977 iverdef->vd_cnt = 1;
6979 iverdef->vd_bfd = abfd;
6981 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6982 if (iverdef->vd_nodename == NULL)
6983 goto error_return_verdef;
6984 iverdef->vd_nextdef = NULL;
6985 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6986 if (iverdef->vd_auxptr == NULL)
6987 goto error_return_verdef;
6989 iverdaux = iverdef->vd_auxptr;
6990 iverdaux->vda_nodename = iverdef->vd_nodename;
6991 iverdaux->vda_nextptr = NULL;
6997 if (contents != NULL)
7003 _bfd_elf_make_empty_symbol (bfd *abfd)
7005 elf_symbol_type *newsym;
7006 bfd_size_type amt = sizeof (elf_symbol_type);
7008 newsym = bfd_zalloc (abfd, amt);
7013 newsym->symbol.the_bfd = abfd;
7014 return &newsym->symbol;
7019 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7023 bfd_symbol_info (symbol, ret);
7026 /* Return whether a symbol name implies a local symbol. Most targets
7027 use this function for the is_local_label_name entry point, but some
7031 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7034 /* Normal local symbols start with ``.L''. */
7035 if (name[0] == '.' && name[1] == 'L')
7038 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7039 DWARF debugging symbols starting with ``..''. */
7040 if (name[0] == '.' && name[1] == '.')
7043 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7044 emitting DWARF debugging output. I suspect this is actually a
7045 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7046 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7047 underscore to be emitted on some ELF targets). For ease of use,
7048 we treat such symbols as local. */
7049 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7056 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7057 asymbol *symbol ATTRIBUTE_UNUSED)
7064 _bfd_elf_set_arch_mach (bfd *abfd,
7065 enum bfd_architecture arch,
7066 unsigned long machine)
7068 /* If this isn't the right architecture for this backend, and this
7069 isn't the generic backend, fail. */
7070 if (arch != get_elf_backend_data (abfd)->arch
7071 && arch != bfd_arch_unknown
7072 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7075 return bfd_default_set_arch_mach (abfd, arch, machine);
7078 /* Find the function to a particular section and offset,
7079 for error reporting. */
7082 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7086 const char **filename_ptr,
7087 const char **functionname_ptr)
7089 const char *filename;
7090 asymbol *func, *file;
7093 /* ??? Given multiple file symbols, it is impossible to reliably
7094 choose the right file name for global symbols. File symbols are
7095 local symbols, and thus all file symbols must sort before any
7096 global symbols. The ELF spec may be interpreted to say that a
7097 file symbol must sort before other local symbols, but currently
7098 ld -r doesn't do this. So, for ld -r output, it is possible to
7099 make a better choice of file name for local symbols by ignoring
7100 file symbols appearing after a given local symbol. */
7101 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7107 state = nothing_seen;
7109 for (p = symbols; *p != NULL; p++)
7113 q = (elf_symbol_type *) *p;
7115 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7121 if (state == symbol_seen)
7122 state = file_after_symbol_seen;
7126 if (bfd_get_section (&q->symbol) == section
7127 && q->symbol.value >= low_func
7128 && q->symbol.value <= offset)
7130 func = (asymbol *) q;
7131 low_func = q->symbol.value;
7134 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7135 || state != file_after_symbol_seen))
7136 filename = bfd_asymbol_name (file);
7140 if (state == nothing_seen)
7141 state = symbol_seen;
7148 *filename_ptr = filename;
7149 if (functionname_ptr)
7150 *functionname_ptr = bfd_asymbol_name (func);
7155 /* Find the nearest line to a particular section and offset,
7156 for error reporting. */
7159 _bfd_elf_find_nearest_line (bfd *abfd,
7163 const char **filename_ptr,
7164 const char **functionname_ptr,
7165 unsigned int *line_ptr)
7169 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7170 filename_ptr, functionname_ptr,
7173 if (!*functionname_ptr)
7174 elf_find_function (abfd, section, symbols, offset,
7175 *filename_ptr ? NULL : filename_ptr,
7181 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7182 filename_ptr, functionname_ptr,
7184 &elf_tdata (abfd)->dwarf2_find_line_info))
7186 if (!*functionname_ptr)
7187 elf_find_function (abfd, section, symbols, offset,
7188 *filename_ptr ? NULL : filename_ptr,
7194 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7195 &found, filename_ptr,
7196 functionname_ptr, line_ptr,
7197 &elf_tdata (abfd)->line_info))
7199 if (found && (*functionname_ptr || *line_ptr))
7202 if (symbols == NULL)
7205 if (! elf_find_function (abfd, section, symbols, offset,
7206 filename_ptr, functionname_ptr))
7213 /* Find the line for a symbol. */
7216 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7217 const char **filename_ptr, unsigned int *line_ptr)
7219 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7220 filename_ptr, line_ptr, 0,
7221 &elf_tdata (abfd)->dwarf2_find_line_info);
7224 /* After a call to bfd_find_nearest_line, successive calls to
7225 bfd_find_inliner_info can be used to get source information about
7226 each level of function inlining that terminated at the address
7227 passed to bfd_find_nearest_line. Currently this is only supported
7228 for DWARF2 with appropriate DWARF3 extensions. */
7231 _bfd_elf_find_inliner_info (bfd *abfd,
7232 const char **filename_ptr,
7233 const char **functionname_ptr,
7234 unsigned int *line_ptr)
7237 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7238 functionname_ptr, line_ptr,
7239 & elf_tdata (abfd)->dwarf2_find_line_info);
7244 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7246 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7247 int ret = bed->s->sizeof_ehdr;
7249 if (!info->relocatable)
7251 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7253 if (phdr_size == (bfd_size_type) -1)
7255 struct elf_segment_map *m;
7258 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7259 phdr_size += bed->s->sizeof_phdr;
7262 phdr_size = get_program_header_size (abfd, info);
7265 elf_tdata (abfd)->program_header_size = phdr_size;
7273 _bfd_elf_set_section_contents (bfd *abfd,
7275 const void *location,
7277 bfd_size_type count)
7279 Elf_Internal_Shdr *hdr;
7282 if (! abfd->output_has_begun
7283 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7286 hdr = &elf_section_data (section)->this_hdr;
7287 pos = hdr->sh_offset + offset;
7288 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7289 || bfd_bwrite (location, count, abfd) != count)
7296 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7297 arelent *cache_ptr ATTRIBUTE_UNUSED,
7298 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7303 /* Try to convert a non-ELF reloc into an ELF one. */
7306 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7308 /* Check whether we really have an ELF howto. */
7310 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7312 bfd_reloc_code_real_type code;
7313 reloc_howto_type *howto;
7315 /* Alien reloc: Try to determine its type to replace it with an
7316 equivalent ELF reloc. */
7318 if (areloc->howto->pc_relative)
7320 switch (areloc->howto->bitsize)
7323 code = BFD_RELOC_8_PCREL;
7326 code = BFD_RELOC_12_PCREL;
7329 code = BFD_RELOC_16_PCREL;
7332 code = BFD_RELOC_24_PCREL;
7335 code = BFD_RELOC_32_PCREL;
7338 code = BFD_RELOC_64_PCREL;
7344 howto = bfd_reloc_type_lookup (abfd, code);
7346 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7348 if (howto->pcrel_offset)
7349 areloc->addend += areloc->address;
7351 areloc->addend -= areloc->address; /* addend is unsigned!! */
7356 switch (areloc->howto->bitsize)
7362 code = BFD_RELOC_14;
7365 code = BFD_RELOC_16;
7368 code = BFD_RELOC_26;
7371 code = BFD_RELOC_32;
7374 code = BFD_RELOC_64;
7380 howto = bfd_reloc_type_lookup (abfd, code);
7384 areloc->howto = howto;
7392 (*_bfd_error_handler)
7393 (_("%B: unsupported relocation type %s"),
7394 abfd, areloc->howto->name);
7395 bfd_set_error (bfd_error_bad_value);
7400 _bfd_elf_close_and_cleanup (bfd *abfd)
7402 if (bfd_get_format (abfd) == bfd_object)
7404 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7405 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7406 _bfd_dwarf2_cleanup_debug_info (abfd);
7409 return _bfd_generic_close_and_cleanup (abfd);
7412 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7413 in the relocation's offset. Thus we cannot allow any sort of sanity
7414 range-checking to interfere. There is nothing else to do in processing
7417 bfd_reloc_status_type
7418 _bfd_elf_rel_vtable_reloc_fn
7419 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7420 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7421 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7422 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7424 return bfd_reloc_ok;
7427 /* Elf core file support. Much of this only works on native
7428 toolchains, since we rely on knowing the
7429 machine-dependent procfs structure in order to pick
7430 out details about the corefile. */
7432 #ifdef HAVE_SYS_PROCFS_H
7433 # include <sys/procfs.h>
7436 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7439 elfcore_make_pid (bfd *abfd)
7441 return ((elf_tdata (abfd)->core_lwpid << 16)
7442 + (elf_tdata (abfd)->core_pid));
7445 /* If there isn't a section called NAME, make one, using
7446 data from SECT. Note, this function will generate a
7447 reference to NAME, so you shouldn't deallocate or
7451 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7455 if (bfd_get_section_by_name (abfd, name) != NULL)
7458 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7462 sect2->size = sect->size;
7463 sect2->filepos = sect->filepos;
7464 sect2->alignment_power = sect->alignment_power;
7468 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7469 actually creates up to two pseudosections:
7470 - For the single-threaded case, a section named NAME, unless
7471 such a section already exists.
7472 - For the multi-threaded case, a section named "NAME/PID", where
7473 PID is elfcore_make_pid (abfd).
7474 Both pseudosections have identical contents. */
7476 _bfd_elfcore_make_pseudosection (bfd *abfd,
7482 char *threaded_name;
7486 /* Build the section name. */
7488 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7489 len = strlen (buf) + 1;
7490 threaded_name = bfd_alloc (abfd, len);
7491 if (threaded_name == NULL)
7493 memcpy (threaded_name, buf, len);
7495 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7500 sect->filepos = filepos;
7501 sect->alignment_power = 2;
7503 return elfcore_maybe_make_sect (abfd, name, sect);
7506 /* prstatus_t exists on:
7508 linux 2.[01] + glibc
7512 #if defined (HAVE_PRSTATUS_T)
7515 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7520 if (note->descsz == sizeof (prstatus_t))
7524 size = sizeof (prstat.pr_reg);
7525 offset = offsetof (prstatus_t, pr_reg);
7526 memcpy (&prstat, note->descdata, sizeof (prstat));
7528 /* Do not overwrite the core signal if it
7529 has already been set by another thread. */
7530 if (elf_tdata (abfd)->core_signal == 0)
7531 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7532 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7534 /* pr_who exists on:
7537 pr_who doesn't exist on:
7540 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7541 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7544 #if defined (HAVE_PRSTATUS32_T)
7545 else if (note->descsz == sizeof (prstatus32_t))
7547 /* 64-bit host, 32-bit corefile */
7548 prstatus32_t prstat;
7550 size = sizeof (prstat.pr_reg);
7551 offset = offsetof (prstatus32_t, pr_reg);
7552 memcpy (&prstat, note->descdata, sizeof (prstat));
7554 /* Do not overwrite the core signal if it
7555 has already been set by another thread. */
7556 if (elf_tdata (abfd)->core_signal == 0)
7557 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7558 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7560 /* pr_who exists on:
7563 pr_who doesn't exist on:
7566 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7567 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7570 #endif /* HAVE_PRSTATUS32_T */
7573 /* Fail - we don't know how to handle any other
7574 note size (ie. data object type). */
7578 /* Make a ".reg/999" section and a ".reg" section. */
7579 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7580 size, note->descpos + offset);
7582 #endif /* defined (HAVE_PRSTATUS_T) */
7584 /* Create a pseudosection containing the exact contents of NOTE. */
7586 elfcore_make_note_pseudosection (bfd *abfd,
7588 Elf_Internal_Note *note)
7590 return _bfd_elfcore_make_pseudosection (abfd, name,
7591 note->descsz, note->descpos);
7594 /* There isn't a consistent prfpregset_t across platforms,
7595 but it doesn't matter, because we don't have to pick this
7596 data structure apart. */
7599 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7601 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7604 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7605 type of NT_PRXFPREG. Just include the whole note's contents
7609 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7611 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7615 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7617 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7621 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7623 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7626 #if defined (HAVE_PRPSINFO_T)
7627 typedef prpsinfo_t elfcore_psinfo_t;
7628 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7629 typedef prpsinfo32_t elfcore_psinfo32_t;
7633 #if defined (HAVE_PSINFO_T)
7634 typedef psinfo_t elfcore_psinfo_t;
7635 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7636 typedef psinfo32_t elfcore_psinfo32_t;
7640 /* return a malloc'ed copy of a string at START which is at
7641 most MAX bytes long, possibly without a terminating '\0'.
7642 the copy will always have a terminating '\0'. */
7645 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7648 char *end = memchr (start, '\0', max);
7656 dups = bfd_alloc (abfd, len + 1);
7660 memcpy (dups, start, len);
7666 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7668 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7670 if (note->descsz == sizeof (elfcore_psinfo_t))
7672 elfcore_psinfo_t psinfo;
7674 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7676 elf_tdata (abfd)->core_program
7677 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7678 sizeof (psinfo.pr_fname));
7680 elf_tdata (abfd)->core_command
7681 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7682 sizeof (psinfo.pr_psargs));
7684 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7685 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7687 /* 64-bit host, 32-bit corefile */
7688 elfcore_psinfo32_t psinfo;
7690 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7692 elf_tdata (abfd)->core_program
7693 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7694 sizeof (psinfo.pr_fname));
7696 elf_tdata (abfd)->core_command
7697 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7698 sizeof (psinfo.pr_psargs));
7704 /* Fail - we don't know how to handle any other
7705 note size (ie. data object type). */
7709 /* Note that for some reason, a spurious space is tacked
7710 onto the end of the args in some (at least one anyway)
7711 implementations, so strip it off if it exists. */
7714 char *command = elf_tdata (abfd)->core_command;
7715 int n = strlen (command);
7717 if (0 < n && command[n - 1] == ' ')
7718 command[n - 1] = '\0';
7723 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7725 #if defined (HAVE_PSTATUS_T)
7727 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7729 if (note->descsz == sizeof (pstatus_t)
7730 #if defined (HAVE_PXSTATUS_T)
7731 || note->descsz == sizeof (pxstatus_t)
7737 memcpy (&pstat, note->descdata, sizeof (pstat));
7739 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7741 #if defined (HAVE_PSTATUS32_T)
7742 else if (note->descsz == sizeof (pstatus32_t))
7744 /* 64-bit host, 32-bit corefile */
7747 memcpy (&pstat, note->descdata, sizeof (pstat));
7749 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7752 /* Could grab some more details from the "representative"
7753 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7754 NT_LWPSTATUS note, presumably. */
7758 #endif /* defined (HAVE_PSTATUS_T) */
7760 #if defined (HAVE_LWPSTATUS_T)
7762 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7764 lwpstatus_t lwpstat;
7770 if (note->descsz != sizeof (lwpstat)
7771 #if defined (HAVE_LWPXSTATUS_T)
7772 && note->descsz != sizeof (lwpxstatus_t)
7777 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7779 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7780 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7782 /* Make a ".reg/999" section. */
7784 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7785 len = strlen (buf) + 1;
7786 name = bfd_alloc (abfd, len);
7789 memcpy (name, buf, len);
7791 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7795 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7796 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7797 sect->filepos = note->descpos
7798 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7801 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7802 sect->size = sizeof (lwpstat.pr_reg);
7803 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7806 sect->alignment_power = 2;
7808 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7811 /* Make a ".reg2/999" section */
7813 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7814 len = strlen (buf) + 1;
7815 name = bfd_alloc (abfd, len);
7818 memcpy (name, buf, len);
7820 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7824 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7825 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7826 sect->filepos = note->descpos
7827 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7830 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7831 sect->size = sizeof (lwpstat.pr_fpreg);
7832 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7835 sect->alignment_power = 2;
7837 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7839 #endif /* defined (HAVE_LWPSTATUS_T) */
7842 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7849 int is_active_thread;
7852 if (note->descsz < 728)
7855 if (! CONST_STRNEQ (note->namedata, "win32"))
7858 type = bfd_get_32 (abfd, note->descdata);
7862 case 1 /* NOTE_INFO_PROCESS */:
7863 /* FIXME: need to add ->core_command. */
7864 /* process_info.pid */
7865 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7866 /* process_info.signal */
7867 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7870 case 2 /* NOTE_INFO_THREAD */:
7871 /* Make a ".reg/999" section. */
7872 /* thread_info.tid */
7873 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7875 len = strlen (buf) + 1;
7876 name = bfd_alloc (abfd, len);
7880 memcpy (name, buf, len);
7882 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7886 /* sizeof (thread_info.thread_context) */
7888 /* offsetof (thread_info.thread_context) */
7889 sect->filepos = note->descpos + 12;
7890 sect->alignment_power = 2;
7892 /* thread_info.is_active_thread */
7893 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7895 if (is_active_thread)
7896 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7900 case 3 /* NOTE_INFO_MODULE */:
7901 /* Make a ".module/xxxxxxxx" section. */
7902 /* module_info.base_address */
7903 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7904 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
7906 len = strlen (buf) + 1;
7907 name = bfd_alloc (abfd, len);
7911 memcpy (name, buf, len);
7913 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7918 sect->size = note->descsz;
7919 sect->filepos = note->descpos;
7920 sect->alignment_power = 2;
7931 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7933 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7941 if (bed->elf_backend_grok_prstatus)
7942 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7944 #if defined (HAVE_PRSTATUS_T)
7945 return elfcore_grok_prstatus (abfd, note);
7950 #if defined (HAVE_PSTATUS_T)
7952 return elfcore_grok_pstatus (abfd, note);
7955 #if defined (HAVE_LWPSTATUS_T)
7957 return elfcore_grok_lwpstatus (abfd, note);
7960 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7961 return elfcore_grok_prfpreg (abfd, note);
7963 case NT_WIN32PSTATUS:
7964 return elfcore_grok_win32pstatus (abfd, note);
7966 case NT_PRXFPREG: /* Linux SSE extension */
7967 if (note->namesz == 6
7968 && strcmp (note->namedata, "LINUX") == 0)
7969 return elfcore_grok_prxfpreg (abfd, note);
7974 if (note->namesz == 6
7975 && strcmp (note->namedata, "LINUX") == 0)
7976 return elfcore_grok_ppc_vmx (abfd, note);
7981 if (note->namesz == 6
7982 && strcmp (note->namedata, "LINUX") == 0)
7983 return elfcore_grok_ppc_vsx (abfd, note);
7989 if (bed->elf_backend_grok_psinfo)
7990 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7992 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7993 return elfcore_grok_psinfo (abfd, note);
8000 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8005 sect->size = note->descsz;
8006 sect->filepos = note->descpos;
8007 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8015 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8017 elf_tdata (abfd)->build_id_size = note->descsz;
8018 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
8019 if (elf_tdata (abfd)->build_id == NULL)
8022 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
8028 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8035 case NT_GNU_BUILD_ID:
8036 return elfobj_grok_gnu_build_id (abfd, note);
8041 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8045 cp = strchr (note->namedata, '@');
8048 *lwpidp = atoi(cp + 1);
8055 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8057 /* Signal number at offset 0x08. */
8058 elf_tdata (abfd)->core_signal
8059 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8061 /* Process ID at offset 0x50. */
8062 elf_tdata (abfd)->core_pid
8063 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8065 /* Command name at 0x7c (max 32 bytes, including nul). */
8066 elf_tdata (abfd)->core_command
8067 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8069 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8074 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8078 if (elfcore_netbsd_get_lwpid (note, &lwp))
8079 elf_tdata (abfd)->core_lwpid = lwp;
8081 if (note->type == NT_NETBSDCORE_PROCINFO)
8083 /* NetBSD-specific core "procinfo". Note that we expect to
8084 find this note before any of the others, which is fine,
8085 since the kernel writes this note out first when it
8086 creates a core file. */
8088 return elfcore_grok_netbsd_procinfo (abfd, note);
8091 /* As of Jan 2002 there are no other machine-independent notes
8092 defined for NetBSD core files. If the note type is less
8093 than the start of the machine-dependent note types, we don't
8096 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8100 switch (bfd_get_arch (abfd))
8102 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8103 PT_GETFPREGS == mach+2. */
8105 case bfd_arch_alpha:
8106 case bfd_arch_sparc:
8109 case NT_NETBSDCORE_FIRSTMACH+0:
8110 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8112 case NT_NETBSDCORE_FIRSTMACH+2:
8113 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8119 /* On all other arch's, PT_GETREGS == mach+1 and
8120 PT_GETFPREGS == mach+3. */
8125 case NT_NETBSDCORE_FIRSTMACH+1:
8126 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8128 case NT_NETBSDCORE_FIRSTMACH+3:
8129 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8139 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8141 void *ddata = note->descdata;
8148 /* nto_procfs_status 'pid' field is at offset 0. */
8149 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8151 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8152 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8154 /* nto_procfs_status 'flags' field is at offset 8. */
8155 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8157 /* nto_procfs_status 'what' field is at offset 14. */
8158 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8160 elf_tdata (abfd)->core_signal = sig;
8161 elf_tdata (abfd)->core_lwpid = *tid;
8164 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8165 do not come from signals so we make sure we set the current
8166 thread just in case. */
8167 if (flags & 0x00000080)
8168 elf_tdata (abfd)->core_lwpid = *tid;
8170 /* Make a ".qnx_core_status/%d" section. */
8171 sprintf (buf, ".qnx_core_status/%ld", *tid);
8173 name = bfd_alloc (abfd, strlen (buf) + 1);
8178 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8182 sect->size = note->descsz;
8183 sect->filepos = note->descpos;
8184 sect->alignment_power = 2;
8186 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8190 elfcore_grok_nto_regs (bfd *abfd,
8191 Elf_Internal_Note *note,
8199 /* Make a "(base)/%d" section. */
8200 sprintf (buf, "%s/%ld", base, tid);
8202 name = bfd_alloc (abfd, strlen (buf) + 1);
8207 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8211 sect->size = note->descsz;
8212 sect->filepos = note->descpos;
8213 sect->alignment_power = 2;
8215 /* This is the current thread. */
8216 if (elf_tdata (abfd)->core_lwpid == tid)
8217 return elfcore_maybe_make_sect (abfd, base, sect);
8222 #define BFD_QNT_CORE_INFO 7
8223 #define BFD_QNT_CORE_STATUS 8
8224 #define BFD_QNT_CORE_GREG 9
8225 #define BFD_QNT_CORE_FPREG 10
8228 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8230 /* Every GREG section has a STATUS section before it. Store the
8231 tid from the previous call to pass down to the next gregs
8233 static long tid = 1;
8237 case BFD_QNT_CORE_INFO:
8238 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8239 case BFD_QNT_CORE_STATUS:
8240 return elfcore_grok_nto_status (abfd, note, &tid);
8241 case BFD_QNT_CORE_GREG:
8242 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8243 case BFD_QNT_CORE_FPREG:
8244 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8251 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8257 /* Use note name as section name. */
8259 name = bfd_alloc (abfd, len);
8262 memcpy (name, note->namedata, len);
8263 name[len - 1] = '\0';
8265 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8269 sect->size = note->descsz;
8270 sect->filepos = note->descpos;
8271 sect->alignment_power = 1;
8276 /* Function: elfcore_write_note
8279 buffer to hold note, and current size of buffer
8283 size of data for note
8285 Writes note to end of buffer. ELF64 notes are written exactly as
8286 for ELF32, despite the current (as of 2006) ELF gabi specifying
8287 that they ought to have 8-byte namesz and descsz field, and have
8288 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8291 Pointer to realloc'd buffer, *BUFSIZ updated. */
8294 elfcore_write_note (bfd *abfd,
8302 Elf_External_Note *xnp;
8309 namesz = strlen (name) + 1;
8311 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8313 buf = realloc (buf, *bufsiz + newspace);
8316 dest = buf + *bufsiz;
8317 *bufsiz += newspace;
8318 xnp = (Elf_External_Note *) dest;
8319 H_PUT_32 (abfd, namesz, xnp->namesz);
8320 H_PUT_32 (abfd, size, xnp->descsz);
8321 H_PUT_32 (abfd, type, xnp->type);
8325 memcpy (dest, name, namesz);
8333 memcpy (dest, input, size);
8343 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8345 elfcore_write_prpsinfo (bfd *abfd,
8351 const char *note_name = "CORE";
8352 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8354 if (bed->elf_backend_write_core_note != NULL)
8357 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8358 NT_PRPSINFO, fname, psargs);
8363 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8364 if (bed->s->elfclass == ELFCLASS32)
8366 #if defined (HAVE_PSINFO32_T)
8368 int note_type = NT_PSINFO;
8371 int note_type = NT_PRPSINFO;
8374 memset (&data, 0, sizeof (data));
8375 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8376 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8377 return elfcore_write_note (abfd, buf, bufsiz,
8378 note_name, note_type, &data, sizeof (data));
8383 #if defined (HAVE_PSINFO_T)
8385 int note_type = NT_PSINFO;
8388 int note_type = NT_PRPSINFO;
8391 memset (&data, 0, sizeof (data));
8392 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8393 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8394 return elfcore_write_note (abfd, buf, bufsiz,
8395 note_name, note_type, &data, sizeof (data));
8398 #endif /* PSINFO_T or PRPSINFO_T */
8400 #if defined (HAVE_PRSTATUS_T)
8402 elfcore_write_prstatus (bfd *abfd,
8409 const char *note_name = "CORE";
8410 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8412 if (bed->elf_backend_write_core_note != NULL)
8415 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8417 pid, cursig, gregs);
8422 #if defined (HAVE_PRSTATUS32_T)
8423 if (bed->s->elfclass == ELFCLASS32)
8425 prstatus32_t prstat;
8427 memset (&prstat, 0, sizeof (prstat));
8428 prstat.pr_pid = pid;
8429 prstat.pr_cursig = cursig;
8430 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8431 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8432 NT_PRSTATUS, &prstat, sizeof (prstat));
8439 memset (&prstat, 0, sizeof (prstat));
8440 prstat.pr_pid = pid;
8441 prstat.pr_cursig = cursig;
8442 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8443 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8444 NT_PRSTATUS, &prstat, sizeof (prstat));
8447 #endif /* HAVE_PRSTATUS_T */
8449 #if defined (HAVE_LWPSTATUS_T)
8451 elfcore_write_lwpstatus (bfd *abfd,
8458 lwpstatus_t lwpstat;
8459 const char *note_name = "CORE";
8461 memset (&lwpstat, 0, sizeof (lwpstat));
8462 lwpstat.pr_lwpid = pid >> 16;
8463 lwpstat.pr_cursig = cursig;
8464 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8465 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8466 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8468 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8469 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8471 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8472 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8475 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8476 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8478 #endif /* HAVE_LWPSTATUS_T */
8480 #if defined (HAVE_PSTATUS_T)
8482 elfcore_write_pstatus (bfd *abfd,
8486 int cursig ATTRIBUTE_UNUSED,
8487 const void *gregs ATTRIBUTE_UNUSED)
8489 const char *note_name = "CORE";
8490 #if defined (HAVE_PSTATUS32_T)
8491 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8493 if (bed->s->elfclass == ELFCLASS32)
8497 memset (&pstat, 0, sizeof (pstat));
8498 pstat.pr_pid = pid & 0xffff;
8499 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8500 NT_PSTATUS, &pstat, sizeof (pstat));
8508 memset (&pstat, 0, sizeof (pstat));
8509 pstat.pr_pid = pid & 0xffff;
8510 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8511 NT_PSTATUS, &pstat, sizeof (pstat));
8515 #endif /* HAVE_PSTATUS_T */
8518 elfcore_write_prfpreg (bfd *abfd,
8524 const char *note_name = "CORE";
8525 return elfcore_write_note (abfd, buf, bufsiz,
8526 note_name, NT_FPREGSET, fpregs, size);
8530 elfcore_write_prxfpreg (bfd *abfd,
8533 const void *xfpregs,
8536 char *note_name = "LINUX";
8537 return elfcore_write_note (abfd, buf, bufsiz,
8538 note_name, NT_PRXFPREG, xfpregs, size);
8542 elfcore_write_ppc_vmx (bfd *abfd,
8545 const void *ppc_vmx,
8548 char *note_name = "LINUX";
8549 return elfcore_write_note (abfd, buf, bufsiz,
8550 note_name, NT_PPC_VMX, ppc_vmx, size);
8554 elfcore_write_ppc_vsx (bfd *abfd,
8557 const void *ppc_vsx,
8560 char *note_name = "LINUX";
8561 return elfcore_write_note (abfd, buf, bufsiz,
8562 note_name, NT_PPC_VSX, ppc_vsx, size);
8566 elfcore_write_register_note (bfd *abfd,
8569 const char *section,
8573 if (strcmp (section, ".reg2") == 0)
8574 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8575 if (strcmp (section, ".reg-xfp") == 0)
8576 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8577 if (strcmp (section, ".reg-ppc-vmx") == 0)
8578 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8579 if (strcmp (section, ".reg-ppc-vsx") == 0)
8580 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8585 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8590 while (p < buf + size)
8592 /* FIXME: bad alignment assumption. */
8593 Elf_External_Note *xnp = (Elf_External_Note *) p;
8594 Elf_Internal_Note in;
8596 if (offsetof (Elf_External_Note, name) > buf - p + size)
8599 in.type = H_GET_32 (abfd, xnp->type);
8601 in.namesz = H_GET_32 (abfd, xnp->namesz);
8602 in.namedata = xnp->name;
8603 if (in.namesz > buf - in.namedata + size)
8606 in.descsz = H_GET_32 (abfd, xnp->descsz);
8607 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8608 in.descpos = offset + (in.descdata - buf);
8610 && (in.descdata >= buf + size
8611 || in.descsz > buf - in.descdata + size))
8614 switch (bfd_get_format (abfd))
8620 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8622 if (! elfcore_grok_netbsd_note (abfd, &in))
8625 else if (CONST_STRNEQ (in.namedata, "QNX"))
8627 if (! elfcore_grok_nto_note (abfd, &in))
8630 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8632 if (! elfcore_grok_spu_note (abfd, &in))
8637 if (! elfcore_grok_note (abfd, &in))
8643 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8645 if (! elfobj_grok_gnu_note (abfd, &in))
8651 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8658 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8665 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8668 buf = bfd_malloc (size);
8672 if (bfd_bread (buf, size, abfd) != size
8673 || !elf_parse_notes (abfd, buf, size, offset))
8683 /* Providing external access to the ELF program header table. */
8685 /* Return an upper bound on the number of bytes required to store a
8686 copy of ABFD's program header table entries. Return -1 if an error
8687 occurs; bfd_get_error will return an appropriate code. */
8690 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8692 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8694 bfd_set_error (bfd_error_wrong_format);
8698 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8701 /* Copy ABFD's program header table entries to *PHDRS. The entries
8702 will be stored as an array of Elf_Internal_Phdr structures, as
8703 defined in include/elf/internal.h. To find out how large the
8704 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8706 Return the number of program header table entries read, or -1 if an
8707 error occurs; bfd_get_error will return an appropriate code. */
8710 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8714 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8716 bfd_set_error (bfd_error_wrong_format);
8720 num_phdrs = elf_elfheader (abfd)->e_phnum;
8721 memcpy (phdrs, elf_tdata (abfd)->phdr,
8722 num_phdrs * sizeof (Elf_Internal_Phdr));
8727 enum elf_reloc_type_class
8728 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8730 return reloc_class_normal;
8733 /* For RELA architectures, return the relocation value for a
8734 relocation against a local symbol. */
8737 _bfd_elf_rela_local_sym (bfd *abfd,
8738 Elf_Internal_Sym *sym,
8740 Elf_Internal_Rela *rel)
8742 asection *sec = *psec;
8745 relocation = (sec->output_section->vma
8746 + sec->output_offset
8748 if ((sec->flags & SEC_MERGE)
8749 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8750 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8753 _bfd_merged_section_offset (abfd, psec,
8754 elf_section_data (sec)->sec_info,
8755 sym->st_value + rel->r_addend);
8758 /* If we have changed the section, and our original section is
8759 marked with SEC_EXCLUDE, it means that the original
8760 SEC_MERGE section has been completely subsumed in some
8761 other SEC_MERGE section. In this case, we need to leave
8762 some info around for --emit-relocs. */
8763 if ((sec->flags & SEC_EXCLUDE) != 0)
8764 sec->kept_section = *psec;
8767 rel->r_addend -= relocation;
8768 rel->r_addend += sec->output_section->vma + sec->output_offset;
8774 _bfd_elf_rel_local_sym (bfd *abfd,
8775 Elf_Internal_Sym *sym,
8779 asection *sec = *psec;
8781 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8782 return sym->st_value + addend;
8784 return _bfd_merged_section_offset (abfd, psec,
8785 elf_section_data (sec)->sec_info,
8786 sym->st_value + addend);
8790 _bfd_elf_section_offset (bfd *abfd,
8791 struct bfd_link_info *info,
8795 switch (sec->sec_info_type)
8797 case ELF_INFO_TYPE_STABS:
8798 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8800 case ELF_INFO_TYPE_EH_FRAME:
8801 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8807 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8808 reconstruct an ELF file by reading the segments out of remote memory
8809 based on the ELF file header at EHDR_VMA and the ELF program headers it
8810 points to. If not null, *LOADBASEP is filled in with the difference
8811 between the VMAs from which the segments were read, and the VMAs the
8812 file headers (and hence BFD's idea of each section's VMA) put them at.
8814 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8815 remote memory at target address VMA into the local buffer at MYADDR; it
8816 should return zero on success or an `errno' code on failure. TEMPL must
8817 be a BFD for an ELF target with the word size and byte order found in
8818 the remote memory. */
8821 bfd_elf_bfd_from_remote_memory
8825 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8827 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8828 (templ, ehdr_vma, loadbasep, target_read_memory);
8832 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8833 long symcount ATTRIBUTE_UNUSED,
8834 asymbol **syms ATTRIBUTE_UNUSED,
8839 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8842 const char *relplt_name;
8843 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8847 Elf_Internal_Shdr *hdr;
8853 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8856 if (dynsymcount <= 0)
8859 if (!bed->plt_sym_val)
8862 relplt_name = bed->relplt_name;
8863 if (relplt_name == NULL)
8864 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
8865 relplt = bfd_get_section_by_name (abfd, relplt_name);
8869 hdr = &elf_section_data (relplt)->this_hdr;
8870 if (hdr->sh_link != elf_dynsymtab (abfd)
8871 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8874 plt = bfd_get_section_by_name (abfd, ".plt");
8878 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8879 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8882 count = relplt->size / hdr->sh_entsize;
8883 size = count * sizeof (asymbol);
8884 p = relplt->relocation;
8885 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8886 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8888 s = *ret = bfd_malloc (size);
8892 names = (char *) (s + count);
8893 p = relplt->relocation;
8895 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8900 addr = bed->plt_sym_val (i, plt, p);
8901 if (addr == (bfd_vma) -1)
8904 *s = **p->sym_ptr_ptr;
8905 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8906 we are defining a symbol, ensure one of them is set. */
8907 if ((s->flags & BSF_LOCAL) == 0)
8908 s->flags |= BSF_GLOBAL;
8909 s->flags |= BSF_SYNTHETIC;
8911 s->value = addr - plt->vma;
8914 len = strlen ((*p->sym_ptr_ptr)->name);
8915 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8917 memcpy (names, "@plt", sizeof ("@plt"));
8918 names += sizeof ("@plt");
8925 /* It is only used by x86-64 so far. */
8926 asection _bfd_elf_large_com_section
8927 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8928 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8931 _bfd_elf_set_osabi (bfd * abfd,
8932 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8934 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8936 i_ehdrp = elf_elfheader (abfd);
8938 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8942 /* Return TRUE for ELF symbol types that represent functions.
8943 This is the default version of this function, which is sufficient for
8944 most targets. It returns true if TYPE is STT_FUNC. */
8947 _bfd_elf_is_function_type (unsigned int type)
8949 return (type == STT_FUNC);