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 == 0
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);
295 shstrtab[shstrtabsize] = '\0';
296 i_shdrp[shindex]->contents = shstrtab;
298 return (char *) shstrtab;
302 bfd_elf_string_from_elf_section (bfd *abfd,
303 unsigned int shindex,
304 unsigned int strindex)
306 Elf_Internal_Shdr *hdr;
311 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
314 hdr = elf_elfsections (abfd)[shindex];
316 if (hdr->contents == NULL
317 && bfd_elf_get_str_section (abfd, shindex) == NULL)
320 if (strindex >= hdr->sh_size)
322 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
323 (*_bfd_error_handler)
324 (_("%B: invalid string offset %u >= %lu for section `%s'"),
325 abfd, strindex, (unsigned long) hdr->sh_size,
326 (shindex == shstrndx && strindex == hdr->sh_name
328 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
332 return ((char *) hdr->contents) + strindex;
335 /* Read and convert symbols to internal format.
336 SYMCOUNT specifies the number of symbols to read, starting from
337 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
338 are non-NULL, they are used to store the internal symbols, external
339 symbols, and symbol section index extensions, respectively.
340 Returns a pointer to the internal symbol buffer (malloced if necessary)
341 or NULL if there were no symbols or some kind of problem. */
344 bfd_elf_get_elf_syms (bfd *ibfd,
345 Elf_Internal_Shdr *symtab_hdr,
348 Elf_Internal_Sym *intsym_buf,
350 Elf_External_Sym_Shndx *extshndx_buf)
352 Elf_Internal_Shdr *shndx_hdr;
354 const bfd_byte *esym;
355 Elf_External_Sym_Shndx *alloc_extshndx;
356 Elf_External_Sym_Shndx *shndx;
357 Elf_Internal_Sym *isym;
358 Elf_Internal_Sym *isymend;
359 const struct elf_backend_data *bed;
364 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
370 /* Normal syms might have section extension entries. */
372 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
373 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
375 /* Read the symbols. */
377 alloc_extshndx = NULL;
378 bed = get_elf_backend_data (ibfd);
379 extsym_size = bed->s->sizeof_sym;
380 amt = symcount * extsym_size;
381 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
382 if (extsym_buf == NULL)
384 alloc_ext = bfd_malloc2 (symcount, extsym_size);
385 extsym_buf = alloc_ext;
387 if (extsym_buf == NULL
388 || bfd_seek (ibfd, pos, SEEK_SET) != 0
389 || bfd_bread (extsym_buf, amt, ibfd) != amt)
395 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
399 amt = symcount * sizeof (Elf_External_Sym_Shndx);
400 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
401 if (extshndx_buf == NULL)
403 alloc_extshndx = bfd_malloc2 (symcount,
404 sizeof (Elf_External_Sym_Shndx));
405 extshndx_buf = alloc_extshndx;
407 if (extshndx_buf == NULL
408 || bfd_seek (ibfd, pos, SEEK_SET) != 0
409 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
416 if (intsym_buf == NULL)
418 intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
419 if (intsym_buf == NULL)
423 /* Convert the symbols to internal form. */
424 isymend = intsym_buf + symcount;
425 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
427 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
428 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
430 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
431 (*_bfd_error_handler) (_("%B symbol number %lu references "
432 "nonexistent SHT_SYMTAB_SHNDX section"),
433 ibfd, (unsigned long) symoffset);
439 if (alloc_ext != NULL)
441 if (alloc_extshndx != NULL)
442 free (alloc_extshndx);
447 /* Look up a symbol name. */
449 bfd_elf_sym_name (bfd *abfd,
450 Elf_Internal_Shdr *symtab_hdr,
451 Elf_Internal_Sym *isym,
455 unsigned int iname = isym->st_name;
456 unsigned int shindex = symtab_hdr->sh_link;
458 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
459 /* Check for a bogus st_shndx to avoid crashing. */
460 && isym->st_shndx < elf_numsections (abfd)
461 && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE))
463 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
464 shindex = elf_elfheader (abfd)->e_shstrndx;
467 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
470 else if (sym_sec && *name == '\0')
471 name = bfd_section_name (abfd, sym_sec);
476 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
477 sections. The first element is the flags, the rest are section
480 typedef union elf_internal_group {
481 Elf_Internal_Shdr *shdr;
483 } Elf_Internal_Group;
485 /* Return the name of the group signature symbol. Why isn't the
486 signature just a string? */
489 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
491 Elf_Internal_Shdr *hdr;
492 unsigned char esym[sizeof (Elf64_External_Sym)];
493 Elf_External_Sym_Shndx eshndx;
494 Elf_Internal_Sym isym;
496 /* First we need to ensure the symbol table is available. Make sure
497 that it is a symbol table section. */
498 hdr = elf_elfsections (abfd) [ghdr->sh_link];
499 if (hdr->sh_type != SHT_SYMTAB
500 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
503 /* Go read the symbol. */
504 hdr = &elf_tdata (abfd)->symtab_hdr;
505 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
506 &isym, esym, &eshndx) == NULL)
509 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
512 /* Set next_in_group list pointer, and group name for NEWSECT. */
515 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
517 unsigned int num_group = elf_tdata (abfd)->num_group;
519 /* If num_group is zero, read in all SHT_GROUP sections. The count
520 is set to -1 if there are no SHT_GROUP sections. */
523 unsigned int i, shnum;
525 /* First count the number of groups. If we have a SHT_GROUP
526 section with just a flag word (ie. sh_size is 4), ignore it. */
527 shnum = elf_numsections (abfd);
530 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
531 ( (shdr)->sh_type == SHT_GROUP \
532 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
533 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
534 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
536 for (i = 0; i < shnum; i++)
538 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
540 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
546 num_group = (unsigned) -1;
547 elf_tdata (abfd)->num_group = num_group;
551 /* We keep a list of elf section headers for group sections,
552 so we can find them quickly. */
555 elf_tdata (abfd)->num_group = num_group;
556 elf_tdata (abfd)->group_sect_ptr
557 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
558 if (elf_tdata (abfd)->group_sect_ptr == NULL)
562 for (i = 0; i < shnum; i++)
564 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
566 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
569 Elf_Internal_Group *dest;
571 /* Add to list of sections. */
572 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
575 /* Read the raw contents. */
576 BFD_ASSERT (sizeof (*dest) >= 4);
577 amt = shdr->sh_size * sizeof (*dest) / 4;
578 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
580 /* PR binutils/4110: Handle corrupt group headers. */
581 if (shdr->contents == NULL)
584 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
585 bfd_set_error (bfd_error_bad_value);
589 memset (shdr->contents, 0, amt);
591 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
592 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
596 /* Translate raw contents, a flag word followed by an
597 array of elf section indices all in target byte order,
598 to the flag word followed by an array of elf section
600 src = shdr->contents + shdr->sh_size;
601 dest = (Elf_Internal_Group *) (shdr->contents + amt);
608 idx = H_GET_32 (abfd, src);
609 if (src == shdr->contents)
612 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
613 shdr->bfd_section->flags
614 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
619 ((*_bfd_error_handler)
620 (_("%B: invalid SHT_GROUP entry"), abfd));
623 dest->shdr = elf_elfsections (abfd)[idx];
630 if (num_group != (unsigned) -1)
634 for (i = 0; i < num_group; i++)
636 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
637 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
638 unsigned int n_elt = shdr->sh_size / 4;
640 /* Look through this group's sections to see if current
641 section is a member. */
643 if ((++idx)->shdr == hdr)
647 /* We are a member of this group. Go looking through
648 other members to see if any others are linked via
650 idx = (Elf_Internal_Group *) shdr->contents;
651 n_elt = shdr->sh_size / 4;
653 if ((s = (++idx)->shdr->bfd_section) != NULL
654 && elf_next_in_group (s) != NULL)
658 /* Snarf the group name from other member, and
659 insert current section in circular list. */
660 elf_group_name (newsect) = elf_group_name (s);
661 elf_next_in_group (newsect) = elf_next_in_group (s);
662 elf_next_in_group (s) = newsect;
668 gname = group_signature (abfd, shdr);
671 elf_group_name (newsect) = gname;
673 /* Start a circular list with one element. */
674 elf_next_in_group (newsect) = newsect;
677 /* If the group section has been created, point to the
679 if (shdr->bfd_section != NULL)
680 elf_next_in_group (shdr->bfd_section) = newsect;
688 if (elf_group_name (newsect) == NULL)
690 (*_bfd_error_handler) (_("%B: no group info for section %A"),
697 _bfd_elf_setup_sections (bfd *abfd)
700 unsigned int num_group = elf_tdata (abfd)->num_group;
701 bfd_boolean result = TRUE;
704 /* Process SHF_LINK_ORDER. */
705 for (s = abfd->sections; s != NULL; s = s->next)
707 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
708 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
710 unsigned int elfsec = this_hdr->sh_link;
711 /* FIXME: The old Intel compiler and old strip/objcopy may
712 not set the sh_link or sh_info fields. Hence we could
713 get the situation where elfsec is 0. */
716 const struct elf_backend_data *bed
717 = get_elf_backend_data (abfd);
718 if (bed->link_order_error_handler)
719 bed->link_order_error_handler
720 (_("%B: warning: sh_link not set for section `%A'"),
727 this_hdr = elf_elfsections (abfd)[elfsec];
730 Some strip/objcopy may leave an incorrect value in
731 sh_link. We don't want to proceed. */
732 link = this_hdr->bfd_section;
735 (*_bfd_error_handler)
736 (_("%B: sh_link [%d] in section `%A' is incorrect"),
737 s->owner, s, elfsec);
741 elf_linked_to_section (s) = link;
746 /* Process section groups. */
747 if (num_group == (unsigned) -1)
750 for (i = 0; i < num_group; i++)
752 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
753 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
754 unsigned int n_elt = shdr->sh_size / 4;
757 if ((++idx)->shdr->bfd_section)
758 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
759 else if (idx->shdr->sh_type == SHT_RELA
760 || idx->shdr->sh_type == SHT_REL)
761 /* We won't include relocation sections in section groups in
762 output object files. We adjust the group section size here
763 so that relocatable link will work correctly when
764 relocation sections are in section group in input object
766 shdr->bfd_section->size -= 4;
769 /* There are some unknown sections in the group. */
770 (*_bfd_error_handler)
771 (_("%B: unknown [%d] section `%s' in group [%s]"),
773 (unsigned int) idx->shdr->sh_type,
774 bfd_elf_string_from_elf_section (abfd,
775 (elf_elfheader (abfd)
778 shdr->bfd_section->name);
786 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
788 return elf_next_in_group (sec) != NULL;
791 /* Make a BFD section from an ELF section. We store a pointer to the
792 BFD section in the bfd_section field of the header. */
795 _bfd_elf_make_section_from_shdr (bfd *abfd,
796 Elf_Internal_Shdr *hdr,
802 const struct elf_backend_data *bed;
804 if (hdr->bfd_section != NULL)
806 BFD_ASSERT (strcmp (name,
807 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
811 newsect = bfd_make_section_anyway (abfd, name);
815 hdr->bfd_section = newsect;
816 elf_section_data (newsect)->this_hdr = *hdr;
817 elf_section_data (newsect)->this_idx = shindex;
819 /* Always use the real type/flags. */
820 elf_section_type (newsect) = hdr->sh_type;
821 elf_section_flags (newsect) = hdr->sh_flags;
823 newsect->filepos = hdr->sh_offset;
825 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
826 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
827 || ! bfd_set_section_alignment (abfd, newsect,
828 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
831 flags = SEC_NO_FLAGS;
832 if (hdr->sh_type != SHT_NOBITS)
833 flags |= SEC_HAS_CONTENTS;
834 if (hdr->sh_type == SHT_GROUP)
835 flags |= SEC_GROUP | SEC_EXCLUDE;
836 if ((hdr->sh_flags & SHF_ALLOC) != 0)
839 if (hdr->sh_type != SHT_NOBITS)
842 if ((hdr->sh_flags & SHF_WRITE) == 0)
843 flags |= SEC_READONLY;
844 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
846 else if ((flags & SEC_LOAD) != 0)
848 if ((hdr->sh_flags & SHF_MERGE) != 0)
851 newsect->entsize = hdr->sh_entsize;
852 if ((hdr->sh_flags & SHF_STRINGS) != 0)
853 flags |= SEC_STRINGS;
855 if (hdr->sh_flags & SHF_GROUP)
856 if (!setup_group (abfd, hdr, newsect))
858 if ((hdr->sh_flags & SHF_TLS) != 0)
859 flags |= SEC_THREAD_LOCAL;
861 if ((flags & SEC_ALLOC) == 0)
863 /* The debugging sections appear to be recognized only by name,
864 not any sort of flag. Their SEC_ALLOC bits are cleared. */
869 } debug_sections [] =
871 { STRING_COMMA_LEN ("debug") }, /* 'd' */
872 { NULL, 0 }, /* 'e' */
873 { NULL, 0 }, /* 'f' */
874 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
875 { NULL, 0 }, /* 'h' */
876 { NULL, 0 }, /* 'i' */
877 { NULL, 0 }, /* 'j' */
878 { NULL, 0 }, /* 'k' */
879 { STRING_COMMA_LEN ("line") }, /* 'l' */
880 { NULL, 0 }, /* 'm' */
881 { NULL, 0 }, /* 'n' */
882 { NULL, 0 }, /* 'o' */
883 { NULL, 0 }, /* 'p' */
884 { NULL, 0 }, /* 'q' */
885 { NULL, 0 }, /* 'r' */
886 { STRING_COMMA_LEN ("stab") } /* 's' */
891 int i = name [1] - 'd';
893 && i < (int) ARRAY_SIZE (debug_sections)
894 && debug_sections [i].name != NULL
895 && strncmp (&name [1], debug_sections [i].name,
896 debug_sections [i].len) == 0)
897 flags |= SEC_DEBUGGING;
901 /* As a GNU extension, if the name begins with .gnu.linkonce, we
902 only link a single copy of the section. This is used to support
903 g++. g++ will emit each template expansion in its own section.
904 The symbols will be defined as weak, so that multiple definitions
905 are permitted. The GNU linker extension is to actually discard
906 all but one of the sections. */
907 if (CONST_STRNEQ (name, ".gnu.linkonce")
908 && elf_next_in_group (newsect) == NULL)
909 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
911 bed = get_elf_backend_data (abfd);
912 if (bed->elf_backend_section_flags)
913 if (! bed->elf_backend_section_flags (&flags, hdr))
916 if (! bfd_set_section_flags (abfd, newsect, flags))
919 /* We do not parse the PT_NOTE segments as we are interested even in the
920 separate debug info files which may have the segments offsets corrupted.
921 PT_NOTEs from the core files are currently not parsed using BFD. */
922 if (hdr->sh_type == SHT_NOTE)
926 contents = bfd_malloc (hdr->sh_size);
930 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
932 || !elf_parse_notes (abfd, contents, hdr->sh_size, -1))
941 if ((flags & SEC_ALLOC) != 0)
943 Elf_Internal_Phdr *phdr;
946 /* Look through the phdrs to see if we need to adjust the lma.
947 If all the p_paddr fields are zero, we ignore them, since
948 some ELF linkers produce such output. */
949 phdr = elf_tdata (abfd)->phdr;
950 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
952 if (phdr->p_paddr != 0)
955 if (i < elf_elfheader (abfd)->e_phnum)
957 phdr = elf_tdata (abfd)->phdr;
958 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
960 /* This section is part of this segment if its file
961 offset plus size lies within the segment's memory
962 span and, if the section is loaded, the extent of the
963 loaded data lies within the extent of the segment.
965 Note - we used to check the p_paddr field as well, and
966 refuse to set the LMA if it was 0. This is wrong
967 though, as a perfectly valid initialised segment can
968 have a p_paddr of zero. Some architectures, eg ARM,
969 place special significance on the address 0 and
970 executables need to be able to have a segment which
971 covers this address. */
972 if (phdr->p_type == PT_LOAD
973 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
974 && (hdr->sh_offset + hdr->sh_size
975 <= phdr->p_offset + phdr->p_memsz)
976 && ((flags & SEC_LOAD) == 0
977 || (hdr->sh_offset + hdr->sh_size
978 <= phdr->p_offset + phdr->p_filesz)))
980 if ((flags & SEC_LOAD) == 0)
981 newsect->lma = (phdr->p_paddr
982 + hdr->sh_addr - phdr->p_vaddr);
984 /* We used to use the same adjustment for SEC_LOAD
985 sections, but that doesn't work if the segment
986 is packed with code from multiple VMAs.
987 Instead we calculate the section LMA based on
988 the segment LMA. It is assumed that the
989 segment will contain sections with contiguous
990 LMAs, even if the VMAs are not. */
991 newsect->lma = (phdr->p_paddr
992 + hdr->sh_offset - phdr->p_offset);
994 /* With contiguous segments, we can't tell from file
995 offsets whether a section with zero size should
996 be placed at the end of one segment or the
997 beginning of the next. Decide based on vaddr. */
998 if (hdr->sh_addr >= phdr->p_vaddr
999 && (hdr->sh_addr + hdr->sh_size
1000 <= phdr->p_vaddr + phdr->p_memsz))
1012 bfd_elf_find_section
1015 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1018 Helper functions for GDB to locate the string tables.
1019 Since BFD hides string tables from callers, GDB needs to use an
1020 internal hook to find them. Sun's .stabstr, in particular,
1021 isn't even pointed to by the .stab section, so ordinary
1022 mechanisms wouldn't work to find it, even if we had some.
1025 struct elf_internal_shdr *
1026 bfd_elf_find_section (bfd *abfd, char *name)
1028 Elf_Internal_Shdr **i_shdrp;
1033 i_shdrp = elf_elfsections (abfd);
1034 if (i_shdrp != NULL)
1036 shstrtab = bfd_elf_get_str_section (abfd,
1037 elf_elfheader (abfd)->e_shstrndx);
1038 if (shstrtab != NULL)
1040 max = elf_numsections (abfd);
1041 for (i = 1; i < max; i++)
1042 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1049 const char *const bfd_elf_section_type_names[] = {
1050 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1051 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1052 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1055 /* ELF relocs are against symbols. If we are producing relocatable
1056 output, and the reloc is against an external symbol, and nothing
1057 has given us any additional addend, the resulting reloc will also
1058 be against the same symbol. In such a case, we don't want to
1059 change anything about the way the reloc is handled, since it will
1060 all be done at final link time. Rather than put special case code
1061 into bfd_perform_relocation, all the reloc types use this howto
1062 function. It just short circuits the reloc if producing
1063 relocatable output against an external symbol. */
1065 bfd_reloc_status_type
1066 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1067 arelent *reloc_entry,
1069 void *data ATTRIBUTE_UNUSED,
1070 asection *input_section,
1072 char **error_message ATTRIBUTE_UNUSED)
1074 if (output_bfd != NULL
1075 && (symbol->flags & BSF_SECTION_SYM) == 0
1076 && (! reloc_entry->howto->partial_inplace
1077 || reloc_entry->addend == 0))
1079 reloc_entry->address += input_section->output_offset;
1080 return bfd_reloc_ok;
1083 return bfd_reloc_continue;
1086 /* Copy the program header and other data from one object module to
1090 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1092 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1093 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1096 BFD_ASSERT (!elf_flags_init (obfd)
1097 || (elf_elfheader (obfd)->e_flags
1098 == elf_elfheader (ibfd)->e_flags));
1100 elf_gp (obfd) = elf_gp (ibfd);
1101 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1102 elf_flags_init (obfd) = TRUE;
1104 /* Copy object attributes. */
1105 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1111 get_segment_type (unsigned int p_type)
1116 case PT_NULL: pt = "NULL"; break;
1117 case PT_LOAD: pt = "LOAD"; break;
1118 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1119 case PT_INTERP: pt = "INTERP"; break;
1120 case PT_NOTE: pt = "NOTE"; break;
1121 case PT_SHLIB: pt = "SHLIB"; break;
1122 case PT_PHDR: pt = "PHDR"; break;
1123 case PT_TLS: pt = "TLS"; break;
1124 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1125 case PT_GNU_STACK: pt = "STACK"; break;
1126 case PT_GNU_RELRO: pt = "RELRO"; break;
1127 default: pt = NULL; break;
1132 /* Print out the program headers. */
1135 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1138 Elf_Internal_Phdr *p;
1140 bfd_byte *dynbuf = NULL;
1142 p = elf_tdata (abfd)->phdr;
1147 fprintf (f, _("\nProgram Header:\n"));
1148 c = elf_elfheader (abfd)->e_phnum;
1149 for (i = 0; i < c; i++, p++)
1151 const char *pt = get_segment_type (p->p_type);
1156 sprintf (buf, "0x%lx", p->p_type);
1159 fprintf (f, "%8s off 0x", pt);
1160 bfd_fprintf_vma (abfd, f, p->p_offset);
1161 fprintf (f, " vaddr 0x");
1162 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1163 fprintf (f, " paddr 0x");
1164 bfd_fprintf_vma (abfd, f, p->p_paddr);
1165 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1166 fprintf (f, " filesz 0x");
1167 bfd_fprintf_vma (abfd, f, p->p_filesz);
1168 fprintf (f, " memsz 0x");
1169 bfd_fprintf_vma (abfd, f, p->p_memsz);
1170 fprintf (f, " flags %c%c%c",
1171 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1172 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1173 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1174 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1175 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1180 s = bfd_get_section_by_name (abfd, ".dynamic");
1184 unsigned long shlink;
1185 bfd_byte *extdyn, *extdynend;
1187 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1189 fprintf (f, _("\nDynamic Section:\n"));
1191 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1194 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1197 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1199 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1200 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1203 extdynend = extdyn + s->size;
1204 for (; extdyn < extdynend; extdyn += extdynsize)
1206 Elf_Internal_Dyn dyn;
1209 bfd_boolean stringp;
1211 (*swap_dyn_in) (abfd, extdyn, &dyn);
1213 if (dyn.d_tag == DT_NULL)
1220 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1224 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1225 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1226 case DT_PLTGOT: name = "PLTGOT"; break;
1227 case DT_HASH: name = "HASH"; break;
1228 case DT_STRTAB: name = "STRTAB"; break;
1229 case DT_SYMTAB: name = "SYMTAB"; break;
1230 case DT_RELA: name = "RELA"; break;
1231 case DT_RELASZ: name = "RELASZ"; break;
1232 case DT_RELAENT: name = "RELAENT"; break;
1233 case DT_STRSZ: name = "STRSZ"; break;
1234 case DT_SYMENT: name = "SYMENT"; break;
1235 case DT_INIT: name = "INIT"; break;
1236 case DT_FINI: name = "FINI"; break;
1237 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1238 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1239 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1240 case DT_REL: name = "REL"; break;
1241 case DT_RELSZ: name = "RELSZ"; break;
1242 case DT_RELENT: name = "RELENT"; break;
1243 case DT_PLTREL: name = "PLTREL"; break;
1244 case DT_DEBUG: name = "DEBUG"; break;
1245 case DT_TEXTREL: name = "TEXTREL"; break;
1246 case DT_JMPREL: name = "JMPREL"; break;
1247 case DT_BIND_NOW: name = "BIND_NOW"; break;
1248 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1249 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1250 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1251 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1252 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1253 case DT_FLAGS: name = "FLAGS"; break;
1254 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1255 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1256 case DT_CHECKSUM: name = "CHECKSUM"; break;
1257 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1258 case DT_MOVEENT: name = "MOVEENT"; break;
1259 case DT_MOVESZ: name = "MOVESZ"; break;
1260 case DT_FEATURE: name = "FEATURE"; break;
1261 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1262 case DT_SYMINSZ: name = "SYMINSZ"; break;
1263 case DT_SYMINENT: name = "SYMINENT"; break;
1264 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1265 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1266 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1267 case DT_PLTPAD: name = "PLTPAD"; break;
1268 case DT_MOVETAB: name = "MOVETAB"; break;
1269 case DT_SYMINFO: name = "SYMINFO"; break;
1270 case DT_RELACOUNT: name = "RELACOUNT"; break;
1271 case DT_RELCOUNT: name = "RELCOUNT"; break;
1272 case DT_FLAGS_1: name = "FLAGS_1"; break;
1273 case DT_VERSYM: name = "VERSYM"; break;
1274 case DT_VERDEF: name = "VERDEF"; break;
1275 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1276 case DT_VERNEED: name = "VERNEED"; break;
1277 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1278 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1279 case DT_USED: name = "USED"; break;
1280 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1281 case DT_GNU_HASH: name = "GNU_HASH"; break;
1284 fprintf (f, " %-11s ", name);
1286 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1290 unsigned int tagv = dyn.d_un.d_val;
1292 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1295 fprintf (f, "%s", string);
1304 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1305 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1307 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1311 if (elf_dynverdef (abfd) != 0)
1313 Elf_Internal_Verdef *t;
1315 fprintf (f, _("\nVersion definitions:\n"));
1316 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1318 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1319 t->vd_flags, t->vd_hash,
1320 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1321 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1323 Elf_Internal_Verdaux *a;
1326 for (a = t->vd_auxptr->vda_nextptr;
1330 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1336 if (elf_dynverref (abfd) != 0)
1338 Elf_Internal_Verneed *t;
1340 fprintf (f, _("\nVersion References:\n"));
1341 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1343 Elf_Internal_Vernaux *a;
1345 fprintf (f, _(" required from %s:\n"),
1346 t->vn_filename ? t->vn_filename : "<corrupt>");
1347 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1348 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1349 a->vna_flags, a->vna_other,
1350 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1362 /* Display ELF-specific fields of a symbol. */
1365 bfd_elf_print_symbol (bfd *abfd,
1368 bfd_print_symbol_type how)
1373 case bfd_print_symbol_name:
1374 fprintf (file, "%s", symbol->name);
1376 case bfd_print_symbol_more:
1377 fprintf (file, "elf ");
1378 bfd_fprintf_vma (abfd, file, symbol->value);
1379 fprintf (file, " %lx", (long) symbol->flags);
1381 case bfd_print_symbol_all:
1383 const char *section_name;
1384 const char *name = NULL;
1385 const struct elf_backend_data *bed;
1386 unsigned char st_other;
1389 section_name = symbol->section ? symbol->section->name : "(*none*)";
1391 bed = get_elf_backend_data (abfd);
1392 if (bed->elf_backend_print_symbol_all)
1393 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1397 name = symbol->name;
1398 bfd_print_symbol_vandf (abfd, file, symbol);
1401 fprintf (file, " %s\t", section_name);
1402 /* Print the "other" value for a symbol. For common symbols,
1403 we've already printed the size; now print the alignment.
1404 For other symbols, we have no specified alignment, and
1405 we've printed the address; now print the size. */
1406 if (symbol->section && bfd_is_com_section (symbol->section))
1407 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1409 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1410 bfd_fprintf_vma (abfd, file, val);
1412 /* If we have version information, print it. */
1413 if (elf_tdata (abfd)->dynversym_section != 0
1414 && (elf_tdata (abfd)->dynverdef_section != 0
1415 || elf_tdata (abfd)->dynverref_section != 0))
1417 unsigned int vernum;
1418 const char *version_string;
1420 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1423 version_string = "";
1424 else if (vernum == 1)
1425 version_string = "Base";
1426 else if (vernum <= elf_tdata (abfd)->cverdefs)
1428 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1431 Elf_Internal_Verneed *t;
1433 version_string = "";
1434 for (t = elf_tdata (abfd)->verref;
1438 Elf_Internal_Vernaux *a;
1440 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1442 if (a->vna_other == vernum)
1444 version_string = a->vna_nodename;
1451 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1452 fprintf (file, " %-11s", version_string);
1457 fprintf (file, " (%s)", version_string);
1458 for (i = 10 - strlen (version_string); i > 0; --i)
1463 /* If the st_other field is not zero, print it. */
1464 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1469 case STV_INTERNAL: fprintf (file, " .internal"); break;
1470 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1471 case STV_PROTECTED: fprintf (file, " .protected"); break;
1473 /* Some other non-defined flags are also present, so print
1475 fprintf (file, " 0x%02x", (unsigned int) st_other);
1478 fprintf (file, " %s", name);
1484 /* Allocate an ELF string table--force the first byte to be zero. */
1486 struct bfd_strtab_hash *
1487 _bfd_elf_stringtab_init (void)
1489 struct bfd_strtab_hash *ret;
1491 ret = _bfd_stringtab_init ();
1496 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1497 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1498 if (loc == (bfd_size_type) -1)
1500 _bfd_stringtab_free (ret);
1507 /* ELF .o/exec file reading */
1509 /* Create a new bfd section from an ELF section header. */
1512 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1514 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1515 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1516 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1519 name = bfd_elf_string_from_elf_section (abfd,
1520 elf_elfheader (abfd)->e_shstrndx,
1525 switch (hdr->sh_type)
1528 /* Inactive section. Throw it away. */
1531 case SHT_PROGBITS: /* Normal section with contents. */
1532 case SHT_NOBITS: /* .bss section. */
1533 case SHT_HASH: /* .hash section. */
1534 case SHT_NOTE: /* .note section. */
1535 case SHT_INIT_ARRAY: /* .init_array section. */
1536 case SHT_FINI_ARRAY: /* .fini_array section. */
1537 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1538 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1539 case SHT_GNU_HASH: /* .gnu.hash section. */
1540 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1542 case SHT_DYNAMIC: /* Dynamic linking information. */
1543 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1545 if (hdr->sh_link > elf_numsections (abfd)
1546 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1548 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1550 Elf_Internal_Shdr *dynsymhdr;
1552 /* The shared libraries distributed with hpux11 have a bogus
1553 sh_link field for the ".dynamic" section. Find the
1554 string table for the ".dynsym" section instead. */
1555 if (elf_dynsymtab (abfd) != 0)
1557 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1558 hdr->sh_link = dynsymhdr->sh_link;
1562 unsigned int i, num_sec;
1564 num_sec = elf_numsections (abfd);
1565 for (i = 1; i < num_sec; i++)
1567 dynsymhdr = elf_elfsections (abfd)[i];
1568 if (dynsymhdr->sh_type == SHT_DYNSYM)
1570 hdr->sh_link = dynsymhdr->sh_link;
1578 case SHT_SYMTAB: /* A symbol table */
1579 if (elf_onesymtab (abfd) == shindex)
1582 if (hdr->sh_entsize != bed->s->sizeof_sym)
1584 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1585 elf_onesymtab (abfd) = shindex;
1586 elf_tdata (abfd)->symtab_hdr = *hdr;
1587 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1588 abfd->flags |= HAS_SYMS;
1590 /* Sometimes a shared object will map in the symbol table. If
1591 SHF_ALLOC is set, and this is a shared object, then we also
1592 treat this section as a BFD section. We can not base the
1593 decision purely on SHF_ALLOC, because that flag is sometimes
1594 set in a relocatable object file, which would confuse the
1596 if ((hdr->sh_flags & SHF_ALLOC) != 0
1597 && (abfd->flags & DYNAMIC) != 0
1598 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1602 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1603 can't read symbols without that section loaded as well. It
1604 is most likely specified by the next section header. */
1605 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1607 unsigned int i, num_sec;
1609 num_sec = elf_numsections (abfd);
1610 for (i = shindex + 1; i < num_sec; i++)
1612 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1613 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1614 && hdr2->sh_link == shindex)
1618 for (i = 1; i < shindex; i++)
1620 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1621 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1622 && hdr2->sh_link == shindex)
1626 return bfd_section_from_shdr (abfd, i);
1630 case SHT_DYNSYM: /* A dynamic symbol table */
1631 if (elf_dynsymtab (abfd) == shindex)
1634 if (hdr->sh_entsize != bed->s->sizeof_sym)
1636 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1637 elf_dynsymtab (abfd) = shindex;
1638 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1639 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1640 abfd->flags |= HAS_SYMS;
1642 /* Besides being a symbol table, we also treat this as a regular
1643 section, so that objcopy can handle it. */
1644 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1646 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1647 if (elf_symtab_shndx (abfd) == shindex)
1650 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1651 elf_symtab_shndx (abfd) = shindex;
1652 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1653 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1656 case SHT_STRTAB: /* A string table */
1657 if (hdr->bfd_section != NULL)
1659 if (ehdr->e_shstrndx == shindex)
1661 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1662 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1665 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1668 elf_tdata (abfd)->strtab_hdr = *hdr;
1669 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1672 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1675 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1676 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1677 elf_elfsections (abfd)[shindex] = hdr;
1678 /* We also treat this as a regular section, so that objcopy
1680 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1684 /* If the string table isn't one of the above, then treat it as a
1685 regular section. We need to scan all the headers to be sure,
1686 just in case this strtab section appeared before the above. */
1687 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1689 unsigned int i, num_sec;
1691 num_sec = elf_numsections (abfd);
1692 for (i = 1; i < num_sec; i++)
1694 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1695 if (hdr2->sh_link == shindex)
1697 /* Prevent endless recursion on broken objects. */
1700 if (! bfd_section_from_shdr (abfd, i))
1702 if (elf_onesymtab (abfd) == i)
1704 if (elf_dynsymtab (abfd) == i)
1705 goto dynsymtab_strtab;
1709 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1713 /* *These* do a lot of work -- but build no sections! */
1715 asection *target_sect;
1716 Elf_Internal_Shdr *hdr2;
1717 unsigned int num_sec = elf_numsections (abfd);
1720 != (bfd_size_type) (hdr->sh_type == SHT_REL
1721 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1724 /* Check for a bogus link to avoid crashing. */
1725 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1726 || hdr->sh_link >= num_sec)
1728 ((*_bfd_error_handler)
1729 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1730 abfd, hdr->sh_link, name, shindex));
1731 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1735 /* For some incomprehensible reason Oracle distributes
1736 libraries for Solaris in which some of the objects have
1737 bogus sh_link fields. It would be nice if we could just
1738 reject them, but, unfortunately, some people need to use
1739 them. We scan through the section headers; if we find only
1740 one suitable symbol table, we clobber the sh_link to point
1741 to it. I hope this doesn't break anything. */
1742 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1743 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1749 for (scan = 1; scan < num_sec; scan++)
1751 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1752 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1763 hdr->sh_link = found;
1766 /* Get the symbol table. */
1767 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1768 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1769 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1772 /* If this reloc section does not use the main symbol table we
1773 don't treat it as a reloc section. BFD can't adequately
1774 represent such a section, so at least for now, we don't
1775 try. We just present it as a normal section. We also
1776 can't use it as a reloc section if it points to the null
1777 section, an invalid section, or another reloc section. */
1778 if (hdr->sh_link != elf_onesymtab (abfd)
1779 || hdr->sh_info == SHN_UNDEF
1780 || (hdr->sh_info >= SHN_LORESERVE && hdr->sh_info <= SHN_HIRESERVE)
1781 || hdr->sh_info >= num_sec
1782 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1783 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1784 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1787 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1789 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1790 if (target_sect == NULL)
1793 if ((target_sect->flags & SEC_RELOC) == 0
1794 || target_sect->reloc_count == 0)
1795 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1799 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1800 amt = sizeof (*hdr2);
1801 hdr2 = bfd_alloc (abfd, amt);
1804 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1807 elf_elfsections (abfd)[shindex] = hdr2;
1808 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1809 target_sect->flags |= SEC_RELOC;
1810 target_sect->relocation = NULL;
1811 target_sect->rel_filepos = hdr->sh_offset;
1812 /* In the section to which the relocations apply, mark whether
1813 its relocations are of the REL or RELA variety. */
1814 if (hdr->sh_size != 0)
1815 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1816 abfd->flags |= HAS_RELOC;
1820 case SHT_GNU_verdef:
1821 elf_dynverdef (abfd) = shindex;
1822 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1823 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1825 case SHT_GNU_versym:
1826 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1828 elf_dynversym (abfd) = shindex;
1829 elf_tdata (abfd)->dynversym_hdr = *hdr;
1830 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1832 case SHT_GNU_verneed:
1833 elf_dynverref (abfd) = shindex;
1834 elf_tdata (abfd)->dynverref_hdr = *hdr;
1835 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1841 /* We need a BFD section for objcopy and relocatable linking,
1842 and it's handy to have the signature available as the section
1844 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1846 name = group_signature (abfd, hdr);
1849 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1851 if (hdr->contents != NULL)
1853 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1854 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1857 if (idx->flags & GRP_COMDAT)
1858 hdr->bfd_section->flags
1859 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1861 /* We try to keep the same section order as it comes in. */
1863 while (--n_elt != 0)
1867 if (idx->shdr != NULL
1868 && (s = idx->shdr->bfd_section) != NULL
1869 && elf_next_in_group (s) != NULL)
1871 elf_next_in_group (hdr->bfd_section) = s;
1879 /* Possibly an attributes section. */
1880 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1881 || hdr->sh_type == bed->obj_attrs_section_type)
1883 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1885 _bfd_elf_parse_attributes (abfd, hdr);
1889 /* Check for any processor-specific section types. */
1890 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1893 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1895 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1896 /* FIXME: How to properly handle allocated section reserved
1897 for applications? */
1898 (*_bfd_error_handler)
1899 (_("%B: don't know how to handle allocated, application "
1900 "specific section `%s' [0x%8x]"),
1901 abfd, name, hdr->sh_type);
1903 /* Allow sections reserved for applications. */
1904 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1907 else if (hdr->sh_type >= SHT_LOPROC
1908 && hdr->sh_type <= SHT_HIPROC)
1909 /* FIXME: We should handle this section. */
1910 (*_bfd_error_handler)
1911 (_("%B: don't know how to handle processor specific section "
1913 abfd, name, hdr->sh_type);
1914 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1916 /* Unrecognised OS-specific sections. */
1917 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1918 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1919 required to correctly process the section and the file should
1920 be rejected with an error message. */
1921 (*_bfd_error_handler)
1922 (_("%B: don't know how to handle OS specific section "
1924 abfd, name, hdr->sh_type);
1926 /* Otherwise it should be processed. */
1927 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1930 /* FIXME: We should handle this section. */
1931 (*_bfd_error_handler)
1932 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1933 abfd, name, hdr->sh_type);
1941 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1942 Return SEC for sections that have no elf section, and NULL on error. */
1945 bfd_section_from_r_symndx (bfd *abfd,
1946 struct sym_sec_cache *cache,
1948 unsigned long r_symndx)
1950 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1953 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1955 Elf_Internal_Shdr *symtab_hdr;
1956 unsigned char esym[sizeof (Elf64_External_Sym)];
1957 Elf_External_Sym_Shndx eshndx;
1958 Elf_Internal_Sym isym;
1960 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1961 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1962 &isym, esym, &eshndx) == NULL)
1965 if (cache->abfd != abfd)
1967 memset (cache->indx, -1, sizeof (cache->indx));
1970 cache->indx[ent] = r_symndx;
1971 cache->shndx[ent] = isym.st_shndx;
1974 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
1981 /* Given an ELF section number, retrieve the corresponding BFD
1985 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
1987 if (index >= elf_numsections (abfd))
1989 return elf_elfsections (abfd)[index]->bfd_section;
1992 static const struct bfd_elf_special_section special_sections_b[] =
1994 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1995 { NULL, 0, 0, 0, 0 }
1998 static const struct bfd_elf_special_section special_sections_c[] =
2000 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2001 { NULL, 0, 0, 0, 0 }
2004 static const struct bfd_elf_special_section special_sections_d[] =
2006 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2007 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2008 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2009 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2010 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2011 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2012 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2013 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2014 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2015 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2016 { NULL, 0, 0, 0, 0 }
2019 static const struct bfd_elf_special_section special_sections_f[] =
2021 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2022 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2023 { NULL, 0, 0, 0, 0 }
2026 static const struct bfd_elf_special_section special_sections_g[] =
2028 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2029 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2030 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2031 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2032 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2033 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2034 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2035 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2036 { NULL, 0, 0, 0, 0 }
2039 static const struct bfd_elf_special_section special_sections_h[] =
2041 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2042 { NULL, 0, 0, 0, 0 }
2045 static const struct bfd_elf_special_section special_sections_i[] =
2047 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2048 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2049 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2050 { NULL, 0, 0, 0, 0 }
2053 static const struct bfd_elf_special_section special_sections_l[] =
2055 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2056 { NULL, 0, 0, 0, 0 }
2059 static const struct bfd_elf_special_section special_sections_n[] =
2061 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2062 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2063 { NULL, 0, 0, 0, 0 }
2066 static const struct bfd_elf_special_section special_sections_p[] =
2068 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2069 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2070 { NULL, 0, 0, 0, 0 }
2073 static const struct bfd_elf_special_section special_sections_r[] =
2075 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2076 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2077 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2078 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2079 { NULL, 0, 0, 0, 0 }
2082 static const struct bfd_elf_special_section special_sections_s[] =
2084 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2085 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2086 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2087 /* See struct bfd_elf_special_section declaration for the semantics of
2088 this special case where .prefix_length != strlen (.prefix). */
2089 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2090 { NULL, 0, 0, 0, 0 }
2093 static const struct bfd_elf_special_section special_sections_t[] =
2095 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2096 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2097 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2098 { NULL, 0, 0, 0, 0 }
2101 static const struct bfd_elf_special_section *special_sections[] =
2103 special_sections_b, /* 'b' */
2104 special_sections_c, /* 'c' */
2105 special_sections_d, /* 'd' */
2107 special_sections_f, /* 'f' */
2108 special_sections_g, /* 'g' */
2109 special_sections_h, /* 'h' */
2110 special_sections_i, /* 'i' */
2113 special_sections_l, /* 'l' */
2115 special_sections_n, /* 'n' */
2117 special_sections_p, /* 'p' */
2119 special_sections_r, /* 'r' */
2120 special_sections_s, /* 's' */
2121 special_sections_t, /* 't' */
2124 const struct bfd_elf_special_section *
2125 _bfd_elf_get_special_section (const char *name,
2126 const struct bfd_elf_special_section *spec,
2132 len = strlen (name);
2134 for (i = 0; spec[i].prefix != NULL; i++)
2137 int prefix_len = spec[i].prefix_length;
2139 if (len < prefix_len)
2141 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2144 suffix_len = spec[i].suffix_length;
2145 if (suffix_len <= 0)
2147 if (name[prefix_len] != 0)
2149 if (suffix_len == 0)
2151 if (name[prefix_len] != '.'
2152 && (suffix_len == -2
2153 || (rela && spec[i].type == SHT_REL)))
2159 if (len < prefix_len + suffix_len)
2161 if (memcmp (name + len - suffix_len,
2162 spec[i].prefix + prefix_len,
2172 const struct bfd_elf_special_section *
2173 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2176 const struct bfd_elf_special_section *spec;
2177 const struct elf_backend_data *bed;
2179 /* See if this is one of the special sections. */
2180 if (sec->name == NULL)
2183 bed = get_elf_backend_data (abfd);
2184 spec = bed->special_sections;
2187 spec = _bfd_elf_get_special_section (sec->name,
2188 bed->special_sections,
2194 if (sec->name[0] != '.')
2197 i = sec->name[1] - 'b';
2198 if (i < 0 || i > 't' - 'b')
2201 spec = special_sections[i];
2206 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2210 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2212 struct bfd_elf_section_data *sdata;
2213 const struct elf_backend_data *bed;
2214 const struct bfd_elf_special_section *ssect;
2216 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2219 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2222 sec->used_by_bfd = sdata;
2225 /* Indicate whether or not this section should use RELA relocations. */
2226 bed = get_elf_backend_data (abfd);
2227 sec->use_rela_p = bed->default_use_rela_p;
2229 /* When we read a file, we don't need to set ELF section type and
2230 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2231 anyway. We will set ELF section type and flags for all linker
2232 created sections. If user specifies BFD section flags, we will
2233 set ELF section type and flags based on BFD section flags in
2234 elf_fake_sections. */
2235 if ((!sec->flags && abfd->direction != read_direction)
2236 || (sec->flags & SEC_LINKER_CREATED) != 0)
2238 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2241 elf_section_type (sec) = ssect->type;
2242 elf_section_flags (sec) = ssect->attr;
2246 return _bfd_generic_new_section_hook (abfd, sec);
2249 /* Create a new bfd section from an ELF program header.
2251 Since program segments have no names, we generate a synthetic name
2252 of the form segment<NUM>, where NUM is generally the index in the
2253 program header table. For segments that are split (see below) we
2254 generate the names segment<NUM>a and segment<NUM>b.
2256 Note that some program segments may have a file size that is different than
2257 (less than) the memory size. All this means is that at execution the
2258 system must allocate the amount of memory specified by the memory size,
2259 but only initialize it with the first "file size" bytes read from the
2260 file. This would occur for example, with program segments consisting
2261 of combined data+bss.
2263 To handle the above situation, this routine generates TWO bfd sections
2264 for the single program segment. The first has the length specified by
2265 the file size of the segment, and the second has the length specified
2266 by the difference between the two sizes. In effect, the segment is split
2267 into its initialized and uninitialized parts.
2272 _bfd_elf_make_section_from_phdr (bfd *abfd,
2273 Elf_Internal_Phdr *hdr,
2275 const char *typename)
2283 split = ((hdr->p_memsz > 0)
2284 && (hdr->p_filesz > 0)
2285 && (hdr->p_memsz > hdr->p_filesz));
2287 if (hdr->p_filesz > 0)
2289 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2290 len = strlen (namebuf) + 1;
2291 name = bfd_alloc (abfd, len);
2294 memcpy (name, namebuf, len);
2295 newsect = bfd_make_section (abfd, name);
2296 if (newsect == NULL)
2298 newsect->vma = hdr->p_vaddr;
2299 newsect->lma = hdr->p_paddr;
2300 newsect->size = hdr->p_filesz;
2301 newsect->filepos = hdr->p_offset;
2302 newsect->flags |= SEC_HAS_CONTENTS;
2303 newsect->alignment_power = bfd_log2 (hdr->p_align);
2304 if (hdr->p_type == PT_LOAD)
2306 newsect->flags |= SEC_ALLOC;
2307 newsect->flags |= SEC_LOAD;
2308 if (hdr->p_flags & PF_X)
2310 /* FIXME: all we known is that it has execute PERMISSION,
2312 newsect->flags |= SEC_CODE;
2315 if (!(hdr->p_flags & PF_W))
2317 newsect->flags |= SEC_READONLY;
2321 if (hdr->p_memsz > hdr->p_filesz)
2325 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
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 + hdr->p_filesz;
2335 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2336 newsect->size = hdr->p_memsz - hdr->p_filesz;
2337 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2338 align = newsect->vma & -newsect->vma;
2339 if (align == 0 || align > hdr->p_align)
2340 align = hdr->p_align;
2341 newsect->alignment_power = bfd_log2 (align);
2342 if (hdr->p_type == PT_LOAD)
2344 /* Hack for gdb. Segments that have not been modified do
2345 not have their contents written to a core file, on the
2346 assumption that a debugger can find the contents in the
2347 executable. We flag this case by setting the fake
2348 section size to zero. Note that "real" bss sections will
2349 always have their contents dumped to the core file. */
2350 if (bfd_get_format (abfd) == bfd_core)
2352 newsect->flags |= SEC_ALLOC;
2353 if (hdr->p_flags & PF_X)
2354 newsect->flags |= SEC_CODE;
2356 if (!(hdr->p_flags & PF_W))
2357 newsect->flags |= SEC_READONLY;
2364 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2366 const struct elf_backend_data *bed;
2368 switch (hdr->p_type)
2371 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2374 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2377 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2380 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2383 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2385 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2390 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2393 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2395 case PT_GNU_EH_FRAME:
2396 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2400 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2403 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2406 /* Check for any processor-specific program segment types. */
2407 bed = get_elf_backend_data (abfd);
2408 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2412 /* Initialize REL_HDR, the section-header for new section, containing
2413 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2414 relocations; otherwise, we use REL relocations. */
2417 _bfd_elf_init_reloc_shdr (bfd *abfd,
2418 Elf_Internal_Shdr *rel_hdr,
2420 bfd_boolean use_rela_p)
2423 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2424 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2426 name = bfd_alloc (abfd, amt);
2429 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2431 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2433 if (rel_hdr->sh_name == (unsigned int) -1)
2435 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2436 rel_hdr->sh_entsize = (use_rela_p
2437 ? bed->s->sizeof_rela
2438 : bed->s->sizeof_rel);
2439 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2440 rel_hdr->sh_flags = 0;
2441 rel_hdr->sh_addr = 0;
2442 rel_hdr->sh_size = 0;
2443 rel_hdr->sh_offset = 0;
2448 /* Set up an ELF internal section header for a section. */
2451 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2453 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2454 bfd_boolean *failedptr = failedptrarg;
2455 Elf_Internal_Shdr *this_hdr;
2456 unsigned int sh_type;
2460 /* We already failed; just get out of the bfd_map_over_sections
2465 this_hdr = &elf_section_data (asect)->this_hdr;
2467 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2468 asect->name, FALSE);
2469 if (this_hdr->sh_name == (unsigned int) -1)
2475 /* Don't clear sh_flags. Assembler may set additional bits. */
2477 if ((asect->flags & SEC_ALLOC) != 0
2478 || asect->user_set_vma)
2479 this_hdr->sh_addr = asect->vma;
2481 this_hdr->sh_addr = 0;
2483 this_hdr->sh_offset = 0;
2484 this_hdr->sh_size = asect->size;
2485 this_hdr->sh_link = 0;
2486 this_hdr->sh_addralign = 1 << asect->alignment_power;
2487 /* The sh_entsize and sh_info fields may have been set already by
2488 copy_private_section_data. */
2490 this_hdr->bfd_section = asect;
2491 this_hdr->contents = NULL;
2493 /* If the section type is unspecified, we set it based on
2495 if ((asect->flags & SEC_GROUP) != 0)
2496 sh_type = SHT_GROUP;
2497 else if ((asect->flags & SEC_ALLOC) != 0
2498 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2499 || (asect->flags & SEC_NEVER_LOAD) != 0))
2500 sh_type = SHT_NOBITS;
2502 sh_type = SHT_PROGBITS;
2504 if (this_hdr->sh_type == SHT_NULL)
2505 this_hdr->sh_type = sh_type;
2506 else if (this_hdr->sh_type == SHT_NOBITS
2507 && sh_type == SHT_PROGBITS
2508 && (asect->flags & SEC_ALLOC) != 0)
2510 /* Warn if we are changing a NOBITS section to PROGBITS, but
2511 allow the link to proceed. This can happen when users link
2512 non-bss input sections to bss output sections, or emit data
2513 to a bss output section via a linker script. */
2514 (*_bfd_error_handler)
2515 (_("warning: section `%A' type changed to PROGBITS"), asect);
2516 this_hdr->sh_type = sh_type;
2519 switch (this_hdr->sh_type)
2525 case SHT_INIT_ARRAY:
2526 case SHT_FINI_ARRAY:
2527 case SHT_PREINIT_ARRAY:
2534 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2538 this_hdr->sh_entsize = bed->s->sizeof_sym;
2542 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2546 if (get_elf_backend_data (abfd)->may_use_rela_p)
2547 this_hdr->sh_entsize = bed->s->sizeof_rela;
2551 if (get_elf_backend_data (abfd)->may_use_rel_p)
2552 this_hdr->sh_entsize = bed->s->sizeof_rel;
2555 case SHT_GNU_versym:
2556 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2559 case SHT_GNU_verdef:
2560 this_hdr->sh_entsize = 0;
2561 /* objcopy or strip will copy over sh_info, but may not set
2562 cverdefs. The linker will set cverdefs, but sh_info will be
2564 if (this_hdr->sh_info == 0)
2565 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2567 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2568 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2571 case SHT_GNU_verneed:
2572 this_hdr->sh_entsize = 0;
2573 /* objcopy or strip will copy over sh_info, but may not set
2574 cverrefs. The linker will set cverrefs, but sh_info will be
2576 if (this_hdr->sh_info == 0)
2577 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2579 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2580 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2584 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2588 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2592 if ((asect->flags & SEC_ALLOC) != 0)
2593 this_hdr->sh_flags |= SHF_ALLOC;
2594 if ((asect->flags & SEC_READONLY) == 0)
2595 this_hdr->sh_flags |= SHF_WRITE;
2596 if ((asect->flags & SEC_CODE) != 0)
2597 this_hdr->sh_flags |= SHF_EXECINSTR;
2598 if ((asect->flags & SEC_MERGE) != 0)
2600 this_hdr->sh_flags |= SHF_MERGE;
2601 this_hdr->sh_entsize = asect->entsize;
2602 if ((asect->flags & SEC_STRINGS) != 0)
2603 this_hdr->sh_flags |= SHF_STRINGS;
2605 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2606 this_hdr->sh_flags |= SHF_GROUP;
2607 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2609 this_hdr->sh_flags |= SHF_TLS;
2610 if (asect->size == 0
2611 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2613 struct bfd_link_order *o = asect->map_tail.link_order;
2615 this_hdr->sh_size = 0;
2618 this_hdr->sh_size = o->offset + o->size;
2619 if (this_hdr->sh_size != 0)
2620 this_hdr->sh_type = SHT_NOBITS;
2625 /* Check for processor-specific section types. */
2626 sh_type = this_hdr->sh_type;
2627 if (bed->elf_backend_fake_sections
2628 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2631 if (sh_type == SHT_NOBITS && asect->size != 0)
2633 /* Don't change the header type from NOBITS if we are being
2634 called for objcopy --only-keep-debug. */
2635 this_hdr->sh_type = sh_type;
2638 /* If the section has relocs, set up a section header for the
2639 SHT_REL[A] section. If two relocation sections are required for
2640 this section, it is up to the processor-specific back-end to
2641 create the other. */
2642 if ((asect->flags & SEC_RELOC) != 0
2643 && !_bfd_elf_init_reloc_shdr (abfd,
2644 &elf_section_data (asect)->rel_hdr,
2650 /* Fill in the contents of a SHT_GROUP section. */
2653 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2655 bfd_boolean *failedptr = failedptrarg;
2656 unsigned long symindx;
2657 asection *elt, *first;
2661 /* Ignore linker created group section. See elfNN_ia64_object_p in
2663 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2668 if (elf_group_id (sec) != NULL)
2669 symindx = elf_group_id (sec)->udata.i;
2673 /* If called from the assembler, swap_out_syms will have set up
2674 elf_section_syms; If called for "ld -r", use target_index. */
2675 if (elf_section_syms (abfd) != NULL)
2676 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2678 symindx = sec->target_index;
2680 elf_section_data (sec)->this_hdr.sh_info = symindx;
2682 /* The contents won't be allocated for "ld -r" or objcopy. */
2684 if (sec->contents == NULL)
2687 sec->contents = bfd_alloc (abfd, sec->size);
2689 /* Arrange for the section to be written out. */
2690 elf_section_data (sec)->this_hdr.contents = sec->contents;
2691 if (sec->contents == NULL)
2698 loc = sec->contents + sec->size;
2700 /* Get the pointer to the first section in the group that gas
2701 squirreled away here. objcopy arranges for this to be set to the
2702 start of the input section group. */
2703 first = elt = elf_next_in_group (sec);
2705 /* First element is a flag word. Rest of section is elf section
2706 indices for all the sections of the group. Write them backwards
2707 just to keep the group in the same order as given in .section
2708 directives, not that it matters. */
2717 s = s->output_section;
2720 idx = elf_section_data (s)->this_idx;
2721 H_PUT_32 (abfd, idx, loc);
2722 elt = elf_next_in_group (elt);
2727 if ((loc -= 4) != sec->contents)
2730 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2733 /* Assign all ELF section numbers. The dummy first section is handled here
2734 too. The link/info pointers for the standard section types are filled
2735 in here too, while we're at it. */
2738 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2740 struct elf_obj_tdata *t = elf_tdata (abfd);
2742 unsigned int section_number, secn;
2743 Elf_Internal_Shdr **i_shdrp;
2744 struct bfd_elf_section_data *d;
2748 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2750 /* SHT_GROUP sections are in relocatable files only. */
2751 if (link_info == NULL || link_info->relocatable)
2753 /* Put SHT_GROUP sections first. */
2754 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2756 d = elf_section_data (sec);
2758 if (d->this_hdr.sh_type == SHT_GROUP)
2760 if (sec->flags & SEC_LINKER_CREATED)
2762 /* Remove the linker created SHT_GROUP sections. */
2763 bfd_section_list_remove (abfd, sec);
2764 abfd->section_count--;
2768 if (section_number == SHN_LORESERVE)
2769 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2770 d->this_idx = section_number++;
2776 for (sec = abfd->sections; sec; sec = sec->next)
2778 d = elf_section_data (sec);
2780 if (d->this_hdr.sh_type != SHT_GROUP)
2782 if (section_number == SHN_LORESERVE)
2783 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2784 d->this_idx = section_number++;
2786 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2787 if ((sec->flags & SEC_RELOC) == 0)
2791 if (section_number == SHN_LORESERVE)
2792 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2793 d->rel_idx = section_number++;
2794 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2799 if (section_number == SHN_LORESERVE)
2800 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2801 d->rel_idx2 = section_number++;
2802 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2808 if (section_number == SHN_LORESERVE)
2809 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2810 t->shstrtab_section = section_number++;
2811 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2812 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2814 if (bfd_get_symcount (abfd) > 0)
2816 if (section_number == SHN_LORESERVE)
2817 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2818 t->symtab_section = section_number++;
2819 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2820 if (section_number > SHN_LORESERVE - 2)
2822 if (section_number == SHN_LORESERVE)
2823 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2824 t->symtab_shndx_section = section_number++;
2825 t->symtab_shndx_hdr.sh_name
2826 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2827 ".symtab_shndx", FALSE);
2828 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2831 if (section_number == SHN_LORESERVE)
2832 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2833 t->strtab_section = section_number++;
2834 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2837 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2838 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2840 elf_numsections (abfd) = section_number;
2841 elf_elfheader (abfd)->e_shnum = section_number;
2842 if (section_number > SHN_LORESERVE)
2843 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2845 /* Set up the list of section header pointers, in agreement with the
2847 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2848 if (i_shdrp == NULL)
2851 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2852 if (i_shdrp[0] == NULL)
2854 bfd_release (abfd, i_shdrp);
2858 elf_elfsections (abfd) = i_shdrp;
2860 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2861 if (bfd_get_symcount (abfd) > 0)
2863 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2864 if (elf_numsections (abfd) > SHN_LORESERVE)
2866 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2867 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2869 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2870 t->symtab_hdr.sh_link = t->strtab_section;
2873 for (sec = abfd->sections; sec; sec = sec->next)
2875 struct bfd_elf_section_data *d = elf_section_data (sec);
2879 i_shdrp[d->this_idx] = &d->this_hdr;
2880 if (d->rel_idx != 0)
2881 i_shdrp[d->rel_idx] = &d->rel_hdr;
2882 if (d->rel_idx2 != 0)
2883 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2885 /* Fill in the sh_link and sh_info fields while we're at it. */
2887 /* sh_link of a reloc section is the section index of the symbol
2888 table. sh_info is the section index of the section to which
2889 the relocation entries apply. */
2890 if (d->rel_idx != 0)
2892 d->rel_hdr.sh_link = t->symtab_section;
2893 d->rel_hdr.sh_info = d->this_idx;
2895 if (d->rel_idx2 != 0)
2897 d->rel_hdr2->sh_link = t->symtab_section;
2898 d->rel_hdr2->sh_info = d->this_idx;
2901 /* We need to set up sh_link for SHF_LINK_ORDER. */
2902 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2904 s = elf_linked_to_section (sec);
2907 /* elf_linked_to_section points to the input section. */
2908 if (link_info != NULL)
2910 /* Check discarded linkonce section. */
2911 if (elf_discarded_section (s))
2914 (*_bfd_error_handler)
2915 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2916 abfd, d->this_hdr.bfd_section,
2918 /* Point to the kept section if it has the same
2919 size as the discarded one. */
2920 kept = _bfd_elf_check_kept_section (s, link_info);
2923 bfd_set_error (bfd_error_bad_value);
2929 s = s->output_section;
2930 BFD_ASSERT (s != NULL);
2934 /* Handle objcopy. */
2935 if (s->output_section == NULL)
2937 (*_bfd_error_handler)
2938 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2939 abfd, d->this_hdr.bfd_section, s, s->owner);
2940 bfd_set_error (bfd_error_bad_value);
2943 s = s->output_section;
2945 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2950 The Intel C compiler generates SHT_IA_64_UNWIND with
2951 SHF_LINK_ORDER. But it doesn't set the sh_link or
2952 sh_info fields. Hence we could get the situation
2954 const struct elf_backend_data *bed
2955 = get_elf_backend_data (abfd);
2956 if (bed->link_order_error_handler)
2957 bed->link_order_error_handler
2958 (_("%B: warning: sh_link not set for section `%A'"),
2963 switch (d->this_hdr.sh_type)
2967 /* A reloc section which we are treating as a normal BFD
2968 section. sh_link is the section index of the symbol
2969 table. sh_info is the section index of the section to
2970 which the relocation entries apply. We assume that an
2971 allocated reloc section uses the dynamic symbol table.
2972 FIXME: How can we be sure? */
2973 s = bfd_get_section_by_name (abfd, ".dynsym");
2975 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2977 /* We look up the section the relocs apply to by name. */
2979 if (d->this_hdr.sh_type == SHT_REL)
2983 s = bfd_get_section_by_name (abfd, name);
2985 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2989 /* We assume that a section named .stab*str is a stabs
2990 string section. We look for a section with the same name
2991 but without the trailing ``str'', and set its sh_link
2992 field to point to this section. */
2993 if (CONST_STRNEQ (sec->name, ".stab")
2994 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2999 len = strlen (sec->name);
3000 alc = bfd_malloc (len - 2);
3003 memcpy (alc, sec->name, len - 3);
3004 alc[len - 3] = '\0';
3005 s = bfd_get_section_by_name (abfd, alc);
3009 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3011 /* This is a .stab section. */
3012 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3013 elf_section_data (s)->this_hdr.sh_entsize
3014 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3021 case SHT_GNU_verneed:
3022 case SHT_GNU_verdef:
3023 /* sh_link is the section header index of the string table
3024 used for the dynamic entries, or the symbol table, or the
3026 s = bfd_get_section_by_name (abfd, ".dynstr");
3028 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3031 case SHT_GNU_LIBLIST:
3032 /* sh_link is the section header index of the prelink library
3033 list used for the dynamic entries, or the symbol table, or
3034 the version strings. */
3035 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3036 ? ".dynstr" : ".gnu.libstr");
3038 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3043 case SHT_GNU_versym:
3044 /* sh_link is the section header index of the symbol table
3045 this hash table or version table is for. */
3046 s = bfd_get_section_by_name (abfd, ".dynsym");
3048 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3052 d->this_hdr.sh_link = t->symtab_section;
3056 for (secn = 1; secn < section_number; ++secn)
3057 if (i_shdrp[secn] == NULL)
3058 i_shdrp[secn] = i_shdrp[0];
3060 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3061 i_shdrp[secn]->sh_name);
3065 /* Map symbol from it's internal number to the external number, moving
3066 all local symbols to be at the head of the list. */
3069 sym_is_global (bfd *abfd, asymbol *sym)
3071 /* If the backend has a special mapping, use it. */
3072 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3073 if (bed->elf_backend_sym_is_global)
3074 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3076 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3077 || bfd_is_und_section (bfd_get_section (sym))
3078 || bfd_is_com_section (bfd_get_section (sym)));
3081 /* Don't output section symbols for sections that are not going to be
3082 output. Also, don't output section symbols for reloc and other
3083 special sections. */
3086 ignore_section_sym (bfd *abfd, asymbol *sym)
3088 return ((sym->flags & BSF_SECTION_SYM) != 0
3090 || (sym->section->owner != abfd
3091 && (sym->section->output_section->owner != abfd
3092 || sym->section->output_offset != 0))));
3096 elf_map_symbols (bfd *abfd)
3098 unsigned int symcount = bfd_get_symcount (abfd);
3099 asymbol **syms = bfd_get_outsymbols (abfd);
3100 asymbol **sect_syms;
3101 unsigned int num_locals = 0;
3102 unsigned int num_globals = 0;
3103 unsigned int num_locals2 = 0;
3104 unsigned int num_globals2 = 0;
3111 fprintf (stderr, "elf_map_symbols\n");
3115 for (asect = abfd->sections; asect; asect = asect->next)
3117 if (max_index < asect->index)
3118 max_index = asect->index;
3122 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3123 if (sect_syms == NULL)
3125 elf_section_syms (abfd) = sect_syms;
3126 elf_num_section_syms (abfd) = max_index;
3128 /* Init sect_syms entries for any section symbols we have already
3129 decided to output. */
3130 for (idx = 0; idx < symcount; idx++)
3132 asymbol *sym = syms[idx];
3134 if ((sym->flags & BSF_SECTION_SYM) != 0
3135 && !ignore_section_sym (abfd, sym))
3137 asection *sec = sym->section;
3139 if (sec->owner != abfd)
3140 sec = sec->output_section;
3142 sect_syms[sec->index] = syms[idx];
3146 /* Classify all of the symbols. */
3147 for (idx = 0; idx < symcount; idx++)
3149 if (ignore_section_sym (abfd, syms[idx]))
3151 if (!sym_is_global (abfd, syms[idx]))
3157 /* We will be adding a section symbol for each normal BFD section. Most
3158 sections will already have a section symbol in outsymbols, but
3159 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3160 at least in that case. */
3161 for (asect = abfd->sections; asect; asect = asect->next)
3163 if (sect_syms[asect->index] == NULL)
3165 if (!sym_is_global (abfd, asect->symbol))
3172 /* Now sort the symbols so the local symbols are first. */
3173 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3175 if (new_syms == NULL)
3178 for (idx = 0; idx < symcount; idx++)
3180 asymbol *sym = syms[idx];
3183 if (ignore_section_sym (abfd, sym))
3185 if (!sym_is_global (abfd, sym))
3188 i = num_locals + num_globals2++;
3190 sym->udata.i = i + 1;
3192 for (asect = abfd->sections; asect; asect = asect->next)
3194 if (sect_syms[asect->index] == NULL)
3196 asymbol *sym = asect->symbol;
3199 sect_syms[asect->index] = sym;
3200 if (!sym_is_global (abfd, sym))
3203 i = num_locals + num_globals2++;
3205 sym->udata.i = i + 1;
3209 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3211 elf_num_locals (abfd) = num_locals;
3212 elf_num_globals (abfd) = num_globals;
3216 /* Align to the maximum file alignment that could be required for any
3217 ELF data structure. */
3219 static inline file_ptr
3220 align_file_position (file_ptr off, int align)
3222 return (off + align - 1) & ~(align - 1);
3225 /* Assign a file position to a section, optionally aligning to the
3226 required section alignment. */
3229 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3237 al = i_shdrp->sh_addralign;
3239 offset = BFD_ALIGN (offset, al);
3241 i_shdrp->sh_offset = offset;
3242 if (i_shdrp->bfd_section != NULL)
3243 i_shdrp->bfd_section->filepos = offset;
3244 if (i_shdrp->sh_type != SHT_NOBITS)
3245 offset += i_shdrp->sh_size;
3249 /* Compute the file positions we are going to put the sections at, and
3250 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3251 is not NULL, this is being called by the ELF backend linker. */
3254 _bfd_elf_compute_section_file_positions (bfd *abfd,
3255 struct bfd_link_info *link_info)
3257 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3259 struct bfd_strtab_hash *strtab = NULL;
3260 Elf_Internal_Shdr *shstrtab_hdr;
3262 if (abfd->output_has_begun)
3265 /* Do any elf backend specific processing first. */
3266 if (bed->elf_backend_begin_write_processing)
3267 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3269 if (! prep_headers (abfd))
3272 /* Post process the headers if necessary. */
3273 if (bed->elf_backend_post_process_headers)
3274 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3277 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3281 if (!assign_section_numbers (abfd, link_info))
3284 /* The backend linker builds symbol table information itself. */
3285 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3287 /* Non-zero if doing a relocatable link. */
3288 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3290 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3294 if (link_info == NULL)
3296 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3301 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3302 /* sh_name was set in prep_headers. */
3303 shstrtab_hdr->sh_type = SHT_STRTAB;
3304 shstrtab_hdr->sh_flags = 0;
3305 shstrtab_hdr->sh_addr = 0;
3306 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3307 shstrtab_hdr->sh_entsize = 0;
3308 shstrtab_hdr->sh_link = 0;
3309 shstrtab_hdr->sh_info = 0;
3310 /* sh_offset is set in assign_file_positions_except_relocs. */
3311 shstrtab_hdr->sh_addralign = 1;
3313 if (!assign_file_positions_except_relocs (abfd, link_info))
3316 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3319 Elf_Internal_Shdr *hdr;
3321 off = elf_tdata (abfd)->next_file_pos;
3323 hdr = &elf_tdata (abfd)->symtab_hdr;
3324 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3326 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3327 if (hdr->sh_size != 0)
3328 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3330 hdr = &elf_tdata (abfd)->strtab_hdr;
3331 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3333 elf_tdata (abfd)->next_file_pos = off;
3335 /* Now that we know where the .strtab section goes, write it
3337 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3338 || ! _bfd_stringtab_emit (abfd, strtab))
3340 _bfd_stringtab_free (strtab);
3343 abfd->output_has_begun = TRUE;
3348 /* Make an initial estimate of the size of the program header. If we
3349 get the number wrong here, we'll redo section placement. */
3351 static bfd_size_type
3352 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3356 const struct elf_backend_data *bed;
3358 /* Assume we will need exactly two PT_LOAD segments: one for text
3359 and one for data. */
3362 s = bfd_get_section_by_name (abfd, ".interp");
3363 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3365 /* If we have a loadable interpreter section, we need a
3366 PT_INTERP segment. In this case, assume we also need a
3367 PT_PHDR segment, although that may not be true for all
3372 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3374 /* We need a PT_DYNAMIC segment. */
3380 /* We need a PT_GNU_RELRO segment. */
3384 if (elf_tdata (abfd)->eh_frame_hdr)
3386 /* We need a PT_GNU_EH_FRAME segment. */
3390 if (elf_tdata (abfd)->stack_flags)
3392 /* We need a PT_GNU_STACK segment. */
3396 for (s = abfd->sections; s != NULL; s = s->next)
3398 if ((s->flags & SEC_LOAD) != 0
3399 && CONST_STRNEQ (s->name, ".note"))
3401 /* We need a PT_NOTE segment. */
3403 /* Try to create just one PT_NOTE segment
3404 for all adjacent loadable .note* sections.
3405 gABI requires that within a PT_NOTE segment
3406 (and also inside of each SHT_NOTE section)
3407 each note is padded to a multiple of 4 size,
3408 so we check whether the sections are correctly
3410 if (s->alignment_power == 2)
3411 while (s->next != NULL
3412 && s->next->alignment_power == 2
3413 && (s->next->flags & SEC_LOAD) != 0
3414 && CONST_STRNEQ (s->next->name, ".note"))
3419 for (s = abfd->sections; s != NULL; s = s->next)
3421 if (s->flags & SEC_THREAD_LOCAL)
3423 /* We need a PT_TLS segment. */
3429 /* Let the backend count up any program headers it might need. */
3430 bed = get_elf_backend_data (abfd);
3431 if (bed->elf_backend_additional_program_headers)
3435 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3441 return segs * bed->s->sizeof_phdr;
3444 /* Find the segment that contains the output_section of section. */
3447 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3449 struct elf_segment_map *m;
3450 Elf_Internal_Phdr *p;
3452 for (m = elf_tdata (abfd)->segment_map,
3453 p = elf_tdata (abfd)->phdr;
3459 for (i = m->count - 1; i >= 0; i--)
3460 if (m->sections[i] == section)
3467 /* Create a mapping from a set of sections to a program segment. */
3469 static struct elf_segment_map *
3470 make_mapping (bfd *abfd,
3471 asection **sections,
3476 struct elf_segment_map *m;
3481 amt = sizeof (struct elf_segment_map);
3482 amt += (to - from - 1) * sizeof (asection *);
3483 m = bfd_zalloc (abfd, amt);
3487 m->p_type = PT_LOAD;
3488 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3489 m->sections[i - from] = *hdrpp;
3490 m->count = to - from;
3492 if (from == 0 && phdr)
3494 /* Include the headers in the first PT_LOAD segment. */
3495 m->includes_filehdr = 1;
3496 m->includes_phdrs = 1;
3502 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3505 struct elf_segment_map *
3506 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3508 struct elf_segment_map *m;
3510 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3514 m->p_type = PT_DYNAMIC;
3516 m->sections[0] = dynsec;
3521 /* Possibly add or remove segments from the segment map. */
3524 elf_modify_segment_map (bfd *abfd,
3525 struct bfd_link_info *info,
3526 bfd_boolean remove_empty_load)
3528 struct elf_segment_map **m;
3529 const struct elf_backend_data *bed;
3531 /* The placement algorithm assumes that non allocated sections are
3532 not in PT_LOAD segments. We ensure this here by removing such
3533 sections from the segment map. We also remove excluded
3534 sections. Finally, any PT_LOAD segment without sections is
3536 m = &elf_tdata (abfd)->segment_map;
3539 unsigned int i, new_count;
3541 for (new_count = 0, i = 0; i < (*m)->count; i++)
3543 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3544 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3545 || (*m)->p_type != PT_LOAD))
3547 (*m)->sections[new_count] = (*m)->sections[i];
3551 (*m)->count = new_count;
3553 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3559 bed = get_elf_backend_data (abfd);
3560 if (bed->elf_backend_modify_segment_map != NULL)
3562 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3569 /* Set up a mapping from BFD sections to program segments. */
3572 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3575 struct elf_segment_map *m;
3576 asection **sections = NULL;
3577 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3578 bfd_boolean no_user_phdrs;
3580 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3581 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3585 struct elf_segment_map *mfirst;
3586 struct elf_segment_map **pm;
3589 unsigned int phdr_index;
3590 bfd_vma maxpagesize;
3592 bfd_boolean phdr_in_segment = TRUE;
3593 bfd_boolean writable;
3595 asection *first_tls = NULL;
3596 asection *dynsec, *eh_frame_hdr;
3599 /* Select the allocated sections, and sort them. */
3601 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3602 if (sections == NULL)
3606 for (s = abfd->sections; s != NULL; s = s->next)
3608 if ((s->flags & SEC_ALLOC) != 0)
3614 BFD_ASSERT (i <= bfd_count_sections (abfd));
3617 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3619 /* Build the mapping. */
3624 /* If we have a .interp section, then create a PT_PHDR segment for
3625 the program headers and a PT_INTERP segment for the .interp
3627 s = bfd_get_section_by_name (abfd, ".interp");
3628 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3630 amt = sizeof (struct elf_segment_map);
3631 m = bfd_zalloc (abfd, amt);
3635 m->p_type = PT_PHDR;
3636 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3637 m->p_flags = PF_R | PF_X;
3638 m->p_flags_valid = 1;
3639 m->includes_phdrs = 1;
3644 amt = sizeof (struct elf_segment_map);
3645 m = bfd_zalloc (abfd, amt);
3649 m->p_type = PT_INTERP;
3657 /* Look through the sections. We put sections in the same program
3658 segment when the start of the second section can be placed within
3659 a few bytes of the end of the first section. */
3663 maxpagesize = bed->maxpagesize;
3665 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3667 && (dynsec->flags & SEC_LOAD) == 0)
3670 /* Deal with -Ttext or something similar such that the first section
3671 is not adjacent to the program headers. This is an
3672 approximation, since at this point we don't know exactly how many
3673 program headers we will need. */
3676 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3678 if (phdr_size == (bfd_size_type) -1)
3679 phdr_size = get_program_header_size (abfd, info);
3680 if ((abfd->flags & D_PAGED) == 0
3681 || sections[0]->lma < phdr_size
3682 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3683 phdr_in_segment = FALSE;
3686 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3689 bfd_boolean new_segment;
3693 /* See if this section and the last one will fit in the same
3696 if (last_hdr == NULL)
3698 /* If we don't have a segment yet, then we don't need a new
3699 one (we build the last one after this loop). */
3700 new_segment = FALSE;
3702 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3704 /* If this section has a different relation between the
3705 virtual address and the load address, then we need a new
3709 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3710 < BFD_ALIGN (hdr->lma, maxpagesize))
3712 /* If putting this section in this segment would force us to
3713 skip a page in the segment, then we need a new segment. */
3716 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3717 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3719 /* We don't want to put a loadable section after a
3720 nonloadable section in the same segment.
3721 Consider .tbss sections as loadable for this purpose. */
3724 else if ((abfd->flags & D_PAGED) == 0)
3726 /* If the file is not demand paged, which means that we
3727 don't require the sections to be correctly aligned in the
3728 file, then there is no other reason for a new segment. */
3729 new_segment = FALSE;
3732 && (hdr->flags & SEC_READONLY) == 0
3733 && (((last_hdr->lma + last_size - 1)
3734 & ~(maxpagesize - 1))
3735 != (hdr->lma & ~(maxpagesize - 1))))
3737 /* We don't want to put a writable section in a read only
3738 segment, unless they are on the same page in memory
3739 anyhow. We already know that the last section does not
3740 bring us past the current section on the page, so the
3741 only case in which the new section is not on the same
3742 page as the previous section is when the previous section
3743 ends precisely on a page boundary. */
3748 /* Otherwise, we can use the same segment. */
3749 new_segment = FALSE;
3752 /* Allow interested parties a chance to override our decision. */
3753 if (last_hdr && info->callbacks->override_segment_assignment)
3754 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3758 if ((hdr->flags & SEC_READONLY) == 0)
3761 /* .tbss sections effectively have zero size. */
3762 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3763 != SEC_THREAD_LOCAL)
3764 last_size = hdr->size;
3770 /* We need a new program segment. We must create a new program
3771 header holding all the sections from phdr_index until hdr. */
3773 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3780 if ((hdr->flags & SEC_READONLY) == 0)
3786 /* .tbss sections effectively have zero size. */
3787 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3788 last_size = hdr->size;
3792 phdr_in_segment = FALSE;
3795 /* Create a final PT_LOAD program segment. */
3796 if (last_hdr != NULL)
3798 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3806 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3809 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3816 /* For each batch of consecutive loadable .note sections,
3817 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3818 because if we link together nonloadable .note sections and
3819 loadable .note sections, we will generate two .note sections
3820 in the output file. FIXME: Using names for section types is
3822 for (s = abfd->sections; s != NULL; s = s->next)
3824 if ((s->flags & SEC_LOAD) != 0
3825 && CONST_STRNEQ (s->name, ".note"))
3829 amt = sizeof (struct elf_segment_map);
3830 if (s->alignment_power == 2)
3831 for (s2 = s; s2->next != NULL; s2 = s2->next)
3833 if (s2->next->alignment_power == 2
3834 && (s2->next->flags & SEC_LOAD) != 0
3835 && CONST_STRNEQ (s2->next->name, ".note")
3836 && align_power (s2->vma + s2->size, 2)
3842 amt += (count - 1) * sizeof (asection *);
3843 m = bfd_zalloc (abfd, amt);
3847 m->p_type = PT_NOTE;
3851 m->sections[m->count - count--] = s;
3852 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3855 m->sections[m->count - 1] = s;
3856 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3860 if (s->flags & SEC_THREAD_LOCAL)
3868 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3873 amt = sizeof (struct elf_segment_map);
3874 amt += (tls_count - 1) * sizeof (asection *);
3875 m = bfd_zalloc (abfd, amt);
3880 m->count = tls_count;
3881 /* Mandated PF_R. */
3883 m->p_flags_valid = 1;
3884 for (i = 0; i < tls_count; ++i)
3886 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3887 m->sections[i] = first_tls;
3888 first_tls = first_tls->next;
3895 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3897 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3898 if (eh_frame_hdr != NULL
3899 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3901 amt = sizeof (struct elf_segment_map);
3902 m = bfd_zalloc (abfd, amt);
3906 m->p_type = PT_GNU_EH_FRAME;
3908 m->sections[0] = eh_frame_hdr->output_section;
3914 if (elf_tdata (abfd)->stack_flags)
3916 amt = sizeof (struct elf_segment_map);
3917 m = bfd_zalloc (abfd, amt);
3921 m->p_type = PT_GNU_STACK;
3922 m->p_flags = elf_tdata (abfd)->stack_flags;
3923 m->p_flags_valid = 1;
3931 for (m = mfirst; m != NULL; m = m->next)
3933 if (m->p_type == PT_LOAD)
3935 asection *last = m->sections[m->count - 1];
3936 bfd_vma vaddr = m->sections[0]->vma;
3937 bfd_vma filesz = last->vma - vaddr + last->size;
3939 if (vaddr < info->relro_end
3940 && vaddr >= info->relro_start
3941 && (vaddr + filesz) >= info->relro_end)
3946 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3949 amt = sizeof (struct elf_segment_map);
3950 m = bfd_zalloc (abfd, amt);
3954 m->p_type = PT_GNU_RELRO;
3956 m->p_flags_valid = 1;
3964 elf_tdata (abfd)->segment_map = mfirst;
3967 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
3970 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3972 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
3977 if (sections != NULL)
3982 /* Sort sections by address. */
3985 elf_sort_sections (const void *arg1, const void *arg2)
3987 const asection *sec1 = *(const asection **) arg1;
3988 const asection *sec2 = *(const asection **) arg2;
3989 bfd_size_type size1, size2;
3991 /* Sort by LMA first, since this is the address used to
3992 place the section into a segment. */
3993 if (sec1->lma < sec2->lma)
3995 else if (sec1->lma > sec2->lma)
3998 /* Then sort by VMA. Normally the LMA and the VMA will be
3999 the same, and this will do nothing. */
4000 if (sec1->vma < sec2->vma)
4002 else if (sec1->vma > sec2->vma)
4005 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4007 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4013 /* If the indicies are the same, do not return 0
4014 here, but continue to try the next comparison. */
4015 if (sec1->target_index - sec2->target_index != 0)
4016 return sec1->target_index - sec2->target_index;
4021 else if (TOEND (sec2))
4026 /* Sort by size, to put zero sized sections
4027 before others at the same address. */
4029 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4030 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4037 return sec1->target_index - sec2->target_index;
4040 /* Ian Lance Taylor writes:
4042 We shouldn't be using % with a negative signed number. That's just
4043 not good. We have to make sure either that the number is not
4044 negative, or that the number has an unsigned type. When the types
4045 are all the same size they wind up as unsigned. When file_ptr is a
4046 larger signed type, the arithmetic winds up as signed long long,
4049 What we're trying to say here is something like ``increase OFF by
4050 the least amount that will cause it to be equal to the VMA modulo
4052 /* In other words, something like:
4054 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4055 off_offset = off % bed->maxpagesize;
4056 if (vma_offset < off_offset)
4057 adjustment = vma_offset + bed->maxpagesize - off_offset;
4059 adjustment = vma_offset - off_offset;
4061 which can can be collapsed into the expression below. */
4064 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4066 return ((vma - off) % maxpagesize);
4070 print_segment_map (const struct elf_segment_map *m)
4073 const char *pt = get_segment_type (m->p_type);
4078 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4079 sprintf (buf, "LOPROC+%7.7x",
4080 (unsigned int) (m->p_type - PT_LOPROC));
4081 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4082 sprintf (buf, "LOOS+%7.7x",
4083 (unsigned int) (m->p_type - PT_LOOS));
4085 snprintf (buf, sizeof (buf), "%8.8x",
4086 (unsigned int) m->p_type);
4089 fprintf (stderr, "%s:", pt);
4090 for (j = 0; j < m->count; j++)
4091 fprintf (stderr, " %s", m->sections [j]->name);
4095 /* Assign file positions to the sections based on the mapping from
4096 sections to segments. This function also sets up some fields in
4100 assign_file_positions_for_load_sections (bfd *abfd,
4101 struct bfd_link_info *link_info)
4103 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4104 struct elf_segment_map *m;
4105 Elf_Internal_Phdr *phdrs;
4106 Elf_Internal_Phdr *p;
4108 bfd_size_type maxpagesize;
4112 if (link_info == NULL
4113 && !elf_modify_segment_map (abfd, link_info, FALSE))
4117 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4120 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4121 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4122 elf_elfheader (abfd)->e_phnum = alloc;
4124 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4125 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4127 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4128 >= alloc * bed->s->sizeof_phdr);
4132 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4136 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4137 elf_tdata (abfd)->phdr = phdrs;
4142 if ((abfd->flags & D_PAGED) != 0)
4143 maxpagesize = bed->maxpagesize;
4145 off = bed->s->sizeof_ehdr;
4146 off += alloc * bed->s->sizeof_phdr;
4148 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4150 m = m->next, p++, j++)
4154 bfd_boolean no_contents;
4156 /* If elf_segment_map is not from map_sections_to_segments, the
4157 sections may not be correctly ordered. NOTE: sorting should
4158 not be done to the PT_NOTE section of a corefile, which may
4159 contain several pseudo-sections artificially created by bfd.
4160 Sorting these pseudo-sections breaks things badly. */
4162 && !(elf_elfheader (abfd)->e_type == ET_CORE
4163 && m->p_type == PT_NOTE))
4164 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4167 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4168 number of sections with contents contributing to both p_filesz
4169 and p_memsz, followed by a number of sections with no contents
4170 that just contribute to p_memsz. In this loop, OFF tracks next
4171 available file offset for PT_LOAD and PT_NOTE segments. */
4172 p->p_type = m->p_type;
4173 p->p_flags = m->p_flags;
4178 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4180 if (m->p_paddr_valid)
4181 p->p_paddr = m->p_paddr;
4182 else if (m->count == 0)
4185 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4187 if (p->p_type == PT_LOAD
4188 && (abfd->flags & D_PAGED) != 0)
4190 /* p_align in demand paged PT_LOAD segments effectively stores
4191 the maximum page size. When copying an executable with
4192 objcopy, we set m->p_align from the input file. Use this
4193 value for maxpagesize rather than bed->maxpagesize, which
4194 may be different. Note that we use maxpagesize for PT_TLS
4195 segment alignment later in this function, so we are relying
4196 on at least one PT_LOAD segment appearing before a PT_TLS
4198 if (m->p_align_valid)
4199 maxpagesize = m->p_align;
4201 p->p_align = maxpagesize;
4203 else if (m->p_align_valid)
4204 p->p_align = m->p_align;
4205 else if (m->count == 0)
4206 p->p_align = 1 << bed->s->log_file_align;
4210 no_contents = FALSE;
4212 if (p->p_type == PT_LOAD
4215 bfd_size_type align;
4216 unsigned int align_power = 0;
4218 if (m->p_align_valid)
4222 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4224 unsigned int secalign;
4226 secalign = bfd_get_section_alignment (abfd, *secpp);
4227 if (secalign > align_power)
4228 align_power = secalign;
4230 align = (bfd_size_type) 1 << align_power;
4231 if (align < maxpagesize)
4232 align = maxpagesize;
4235 for (i = 0; i < m->count; i++)
4236 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4237 /* If we aren't making room for this section, then
4238 it must be SHT_NOBITS regardless of what we've
4239 set via struct bfd_elf_special_section. */
4240 elf_section_type (m->sections[i]) = SHT_NOBITS;
4242 /* Find out whether this segment contains any loadable
4243 sections. If the first section isn't loadable, the same
4244 holds for any other sections. */
4246 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4248 /* If a segment starts with .tbss, we need to look
4249 at the next section to decide whether the segment
4250 has any loadable sections. */
4251 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4259 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4263 /* We shouldn't need to align the segment on disk since
4264 the segment doesn't need file space, but the gABI
4265 arguably requires the alignment and glibc ld.so
4266 checks it. So to comply with the alignment
4267 requirement but not waste file space, we adjust
4268 p_offset for just this segment. (OFF_ADJUST is
4269 subtracted from OFF later.) This may put p_offset
4270 past the end of file, but that shouldn't matter. */
4275 /* Make sure the .dynamic section is the first section in the
4276 PT_DYNAMIC segment. */
4277 else if (p->p_type == PT_DYNAMIC
4279 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4282 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4284 bfd_set_error (bfd_error_bad_value);
4287 /* Set the note section type to SHT_NOTE. */
4288 else if (p->p_type == PT_NOTE)
4289 for (i = 0; i < m->count; i++)
4290 elf_section_type (m->sections[i]) = SHT_NOTE;
4296 if (m->includes_filehdr)
4298 if (!m->p_flags_valid)
4300 p->p_filesz = bed->s->sizeof_ehdr;
4301 p->p_memsz = bed->s->sizeof_ehdr;
4304 BFD_ASSERT (p->p_type == PT_LOAD);
4306 if (p->p_vaddr < (bfd_vma) off)
4308 (*_bfd_error_handler)
4309 (_("%B: Not enough room for program headers, try linking with -N"),
4311 bfd_set_error (bfd_error_bad_value);
4316 if (!m->p_paddr_valid)
4321 if (m->includes_phdrs)
4323 if (!m->p_flags_valid)
4326 if (!m->includes_filehdr)
4328 p->p_offset = bed->s->sizeof_ehdr;
4332 BFD_ASSERT (p->p_type == PT_LOAD);
4333 p->p_vaddr -= off - p->p_offset;
4334 if (!m->p_paddr_valid)
4335 p->p_paddr -= off - p->p_offset;
4339 p->p_filesz += alloc * bed->s->sizeof_phdr;
4340 p->p_memsz += alloc * bed->s->sizeof_phdr;
4343 if (p->p_type == PT_LOAD
4344 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4346 if (!m->includes_filehdr && !m->includes_phdrs)
4352 adjust = off - (p->p_offset + p->p_filesz);
4354 p->p_filesz += adjust;
4355 p->p_memsz += adjust;
4359 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4360 maps. Set filepos for sections in PT_LOAD segments, and in
4361 core files, for sections in PT_NOTE segments.
4362 assign_file_positions_for_non_load_sections will set filepos
4363 for other sections and update p_filesz for other segments. */
4364 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4367 bfd_size_type align;
4368 Elf_Internal_Shdr *this_hdr;
4371 this_hdr = &elf_section_data (sec)->this_hdr;
4372 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4374 if (p->p_type == PT_LOAD
4375 || p->p_type == PT_TLS)
4377 bfd_signed_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
4379 if (this_hdr->sh_type != SHT_NOBITS
4380 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4381 && ((this_hdr->sh_flags & SHF_TLS) == 0
4382 || p->p_type == PT_TLS)))
4386 (*_bfd_error_handler)
4387 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4388 abfd, sec, (unsigned long) sec->lma);
4391 p->p_memsz += adjust;
4393 if (this_hdr->sh_type != SHT_NOBITS)
4396 p->p_filesz += adjust;
4401 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4403 /* The section at i == 0 is the one that actually contains
4407 this_hdr->sh_offset = sec->filepos = off;
4408 off += this_hdr->sh_size;
4409 p->p_filesz = this_hdr->sh_size;
4415 /* The rest are fake sections that shouldn't be written. */
4424 if (p->p_type == PT_LOAD)
4426 this_hdr->sh_offset = sec->filepos = off;
4427 if (this_hdr->sh_type != SHT_NOBITS)
4428 off += this_hdr->sh_size;
4431 if (this_hdr->sh_type != SHT_NOBITS)
4433 p->p_filesz += this_hdr->sh_size;
4434 /* A load section without SHF_ALLOC is something like
4435 a note section in a PT_NOTE segment. These take
4436 file space but are not loaded into memory. */
4437 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4438 p->p_memsz += this_hdr->sh_size;
4440 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4442 if (p->p_type == PT_TLS)
4443 p->p_memsz += this_hdr->sh_size;
4445 /* .tbss is special. It doesn't contribute to p_memsz of
4447 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4448 p->p_memsz += this_hdr->sh_size;
4451 if (align > p->p_align
4452 && !m->p_align_valid
4453 && (p->p_type != PT_LOAD
4454 || (abfd->flags & D_PAGED) == 0))
4458 if (!m->p_flags_valid)
4461 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4463 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4469 /* Check that all sections are in a PT_LOAD segment.
4470 Don't check funky gdb generated core files. */
4471 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4472 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4474 Elf_Internal_Shdr *this_hdr;
4478 this_hdr = &(elf_section_data(sec)->this_hdr);
4479 if (this_hdr->sh_size != 0
4480 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4482 (*_bfd_error_handler)
4483 (_("%B: section `%A' can't be allocated in segment %d"),
4485 print_segment_map (m);
4486 bfd_set_error (bfd_error_bad_value);
4492 elf_tdata (abfd)->next_file_pos = off;
4496 /* Assign file positions for the other sections. */
4499 assign_file_positions_for_non_load_sections (bfd *abfd,
4500 struct bfd_link_info *link_info)
4502 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4503 Elf_Internal_Shdr **i_shdrpp;
4504 Elf_Internal_Shdr **hdrpp;
4505 Elf_Internal_Phdr *phdrs;
4506 Elf_Internal_Phdr *p;
4507 struct elf_segment_map *m;
4508 bfd_vma filehdr_vaddr, filehdr_paddr;
4509 bfd_vma phdrs_vaddr, phdrs_paddr;
4511 unsigned int num_sec;
4515 i_shdrpp = elf_elfsections (abfd);
4516 num_sec = elf_numsections (abfd);
4517 off = elf_tdata (abfd)->next_file_pos;
4518 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4520 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4521 Elf_Internal_Shdr *hdr;
4524 if (hdr->bfd_section != NULL
4525 && (hdr->bfd_section->filepos != 0
4526 || (hdr->sh_type == SHT_NOBITS
4527 && hdr->contents == NULL)))
4528 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4529 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4531 if (hdr->sh_size != 0)
4532 ((*_bfd_error_handler)
4533 (_("%B: warning: allocated section `%s' not in segment"),
4535 (hdr->bfd_section == NULL
4537 : hdr->bfd_section->name)));
4538 /* We don't need to page align empty sections. */
4539 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4540 off += vma_page_aligned_bias (hdr->sh_addr, off,
4543 off += vma_page_aligned_bias (hdr->sh_addr, off,
4545 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4548 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4549 && hdr->bfd_section == NULL)
4550 || hdr == i_shdrpp[tdata->symtab_section]
4551 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4552 || hdr == i_shdrpp[tdata->strtab_section])
4553 hdr->sh_offset = -1;
4555 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4557 if (i == SHN_LORESERVE - 1)
4559 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4560 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4564 /* Now that we have set the section file positions, we can set up
4565 the file positions for the non PT_LOAD segments. */
4569 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4571 phdrs = elf_tdata (abfd)->phdr;
4572 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4577 if (p->p_type != PT_LOAD)
4580 if (m->includes_filehdr)
4582 filehdr_vaddr = p->p_vaddr;
4583 filehdr_paddr = p->p_paddr;
4585 if (m->includes_phdrs)
4587 phdrs_vaddr = p->p_vaddr;
4588 phdrs_paddr = p->p_paddr;
4589 if (m->includes_filehdr)
4591 phdrs_vaddr += bed->s->sizeof_ehdr;
4592 phdrs_paddr += bed->s->sizeof_ehdr;
4597 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4603 if (p->p_type != PT_LOAD
4604 && (p->p_type != PT_NOTE
4605 || bfd_get_format (abfd) != bfd_core))
4607 Elf_Internal_Shdr *hdr;
4610 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4612 sect = m->sections[m->count - 1];
4613 hdr = &elf_section_data (sect)->this_hdr;
4614 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4615 if (hdr->sh_type != SHT_NOBITS)
4616 p->p_filesz += hdr->sh_size;
4618 if (p->p_type == PT_GNU_RELRO)
4620 /* When we get here, we are copying executable
4621 or shared library. But we need to use the same
4623 Elf_Internal_Phdr *lp;
4625 for (lp = phdrs; lp < phdrs + count; ++lp)
4627 if (lp->p_type == PT_LOAD
4628 && lp->p_paddr == p->p_paddr)
4632 if (lp < phdrs + count)
4634 /* We should use p_size if it is valid since it
4635 may contain the first few bytes of the next
4636 SEC_ALLOC section. */
4637 if (m->p_size_valid)
4638 p->p_filesz = m->p_size;
4641 p->p_vaddr = lp->p_vaddr;
4642 p->p_offset = lp->p_offset;
4643 p->p_memsz = p->p_filesz;
4650 p->p_offset = m->sections[0]->filepos;
4655 if (m->includes_filehdr)
4657 p->p_vaddr = filehdr_vaddr;
4658 if (! m->p_paddr_valid)
4659 p->p_paddr = filehdr_paddr;
4661 else if (m->includes_phdrs)
4663 p->p_vaddr = phdrs_vaddr;
4664 if (! m->p_paddr_valid)
4665 p->p_paddr = phdrs_paddr;
4667 else if (p->p_type == PT_GNU_RELRO)
4669 Elf_Internal_Phdr *lp;
4671 for (lp = phdrs; lp < phdrs + count; ++lp)
4673 if (lp->p_type == PT_LOAD
4674 && lp->p_vaddr <= link_info->relro_end
4675 && lp->p_vaddr >= link_info->relro_start
4676 && (lp->p_vaddr + lp->p_filesz
4677 >= link_info->relro_end))
4681 if (lp < phdrs + count
4682 && link_info->relro_end > lp->p_vaddr)
4684 p->p_vaddr = lp->p_vaddr;
4685 p->p_paddr = lp->p_paddr;
4686 p->p_offset = lp->p_offset;
4687 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4688 p->p_memsz = p->p_filesz;
4690 p->p_flags = (lp->p_flags & ~PF_W);
4694 memset (p, 0, sizeof *p);
4695 p->p_type = PT_NULL;
4701 elf_tdata (abfd)->next_file_pos = off;
4706 /* Work out the file positions of all the sections. This is called by
4707 _bfd_elf_compute_section_file_positions. All the section sizes and
4708 VMAs must be known before this is called.
4710 Reloc sections come in two flavours: Those processed specially as
4711 "side-channel" data attached to a section to which they apply, and
4712 those that bfd doesn't process as relocations. The latter sort are
4713 stored in a normal bfd section by bfd_section_from_shdr. We don't
4714 consider the former sort here, unless they form part of the loadable
4715 image. Reloc sections not assigned here will be handled later by
4716 assign_file_positions_for_relocs.
4718 We also don't set the positions of the .symtab and .strtab here. */
4721 assign_file_positions_except_relocs (bfd *abfd,
4722 struct bfd_link_info *link_info)
4724 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4725 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4727 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4729 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4730 && bfd_get_format (abfd) != bfd_core)
4732 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4733 unsigned int num_sec = elf_numsections (abfd);
4734 Elf_Internal_Shdr **hdrpp;
4737 /* Start after the ELF header. */
4738 off = i_ehdrp->e_ehsize;
4740 /* We are not creating an executable, which means that we are
4741 not creating a program header, and that the actual order of
4742 the sections in the file is unimportant. */
4743 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4745 Elf_Internal_Shdr *hdr;
4748 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4749 && hdr->bfd_section == NULL)
4750 || i == tdata->symtab_section
4751 || i == tdata->symtab_shndx_section
4752 || i == tdata->strtab_section)
4754 hdr->sh_offset = -1;
4757 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4759 if (i == SHN_LORESERVE - 1)
4761 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4762 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4770 /* Assign file positions for the loaded sections based on the
4771 assignment of sections to segments. */
4772 if (!assign_file_positions_for_load_sections (abfd, link_info))
4775 /* And for non-load sections. */
4776 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4779 if (bed->elf_backend_modify_program_headers != NULL)
4781 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4785 /* Write out the program headers. */
4786 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4787 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4788 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4791 off = tdata->next_file_pos;
4794 /* Place the section headers. */
4795 off = align_file_position (off, 1 << bed->s->log_file_align);
4796 i_ehdrp->e_shoff = off;
4797 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4799 tdata->next_file_pos = off;
4805 prep_headers (bfd *abfd)
4807 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4808 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4809 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4810 struct elf_strtab_hash *shstrtab;
4811 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4813 i_ehdrp = elf_elfheader (abfd);
4814 i_shdrp = elf_elfsections (abfd);
4816 shstrtab = _bfd_elf_strtab_init ();
4817 if (shstrtab == NULL)
4820 elf_shstrtab (abfd) = shstrtab;
4822 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4823 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4824 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4825 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4827 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4828 i_ehdrp->e_ident[EI_DATA] =
4829 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4830 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4832 if ((abfd->flags & DYNAMIC) != 0)
4833 i_ehdrp->e_type = ET_DYN;
4834 else if ((abfd->flags & EXEC_P) != 0)
4835 i_ehdrp->e_type = ET_EXEC;
4836 else if (bfd_get_format (abfd) == bfd_core)
4837 i_ehdrp->e_type = ET_CORE;
4839 i_ehdrp->e_type = ET_REL;
4841 switch (bfd_get_arch (abfd))
4843 case bfd_arch_unknown:
4844 i_ehdrp->e_machine = EM_NONE;
4847 /* There used to be a long list of cases here, each one setting
4848 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4849 in the corresponding bfd definition. To avoid duplication,
4850 the switch was removed. Machines that need special handling
4851 can generally do it in elf_backend_final_write_processing(),
4852 unless they need the information earlier than the final write.
4853 Such need can generally be supplied by replacing the tests for
4854 e_machine with the conditions used to determine it. */
4856 i_ehdrp->e_machine = bed->elf_machine_code;
4859 i_ehdrp->e_version = bed->s->ev_current;
4860 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4862 /* No program header, for now. */
4863 i_ehdrp->e_phoff = 0;
4864 i_ehdrp->e_phentsize = 0;
4865 i_ehdrp->e_phnum = 0;
4867 /* Each bfd section is section header entry. */
4868 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4869 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4871 /* If we're building an executable, we'll need a program header table. */
4872 if (abfd->flags & EXEC_P)
4873 /* It all happens later. */
4877 i_ehdrp->e_phentsize = 0;
4879 i_ehdrp->e_phoff = 0;
4882 elf_tdata (abfd)->symtab_hdr.sh_name =
4883 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4884 elf_tdata (abfd)->strtab_hdr.sh_name =
4885 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4886 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4887 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4888 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4889 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4890 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4896 /* Assign file positions for all the reloc sections which are not part
4897 of the loadable file image. */
4900 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4903 unsigned int i, num_sec;
4904 Elf_Internal_Shdr **shdrpp;
4906 off = elf_tdata (abfd)->next_file_pos;
4908 num_sec = elf_numsections (abfd);
4909 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4911 Elf_Internal_Shdr *shdrp;
4914 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4915 && shdrp->sh_offset == -1)
4916 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4919 elf_tdata (abfd)->next_file_pos = off;
4923 _bfd_elf_write_object_contents (bfd *abfd)
4925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4926 Elf_Internal_Ehdr *i_ehdrp;
4927 Elf_Internal_Shdr **i_shdrp;
4929 unsigned int count, num_sec;
4931 if (! abfd->output_has_begun
4932 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4935 i_shdrp = elf_elfsections (abfd);
4936 i_ehdrp = elf_elfheader (abfd);
4939 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4943 _bfd_elf_assign_file_positions_for_relocs (abfd);
4945 /* After writing the headers, we need to write the sections too... */
4946 num_sec = elf_numsections (abfd);
4947 for (count = 1; count < num_sec; count++)
4949 if (bed->elf_backend_section_processing)
4950 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4951 if (i_shdrp[count]->contents)
4953 bfd_size_type amt = i_shdrp[count]->sh_size;
4955 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4956 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4959 if (count == SHN_LORESERVE - 1)
4960 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4963 /* Write out the section header names. */
4964 if (elf_shstrtab (abfd) != NULL
4965 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4966 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4969 if (bed->elf_backend_final_write_processing)
4970 (*bed->elf_backend_final_write_processing) (abfd,
4971 elf_tdata (abfd)->linker);
4973 if (!bed->s->write_shdrs_and_ehdr (abfd))
4976 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4977 if (elf_tdata (abfd)->after_write_object_contents)
4978 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4984 _bfd_elf_write_corefile_contents (bfd *abfd)
4986 /* Hopefully this can be done just like an object file. */
4987 return _bfd_elf_write_object_contents (abfd);
4990 /* Given a section, search the header to find them. */
4993 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4995 const struct elf_backend_data *bed;
4998 if (elf_section_data (asect) != NULL
4999 && elf_section_data (asect)->this_idx != 0)
5000 return elf_section_data (asect)->this_idx;
5002 if (bfd_is_abs_section (asect))
5004 else if (bfd_is_com_section (asect))
5006 else if (bfd_is_und_section (asect))
5011 bed = get_elf_backend_data (abfd);
5012 if (bed->elf_backend_section_from_bfd_section)
5016 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5021 bfd_set_error (bfd_error_nonrepresentable_section);
5026 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5030 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5032 asymbol *asym_ptr = *asym_ptr_ptr;
5034 flagword flags = asym_ptr->flags;
5036 /* When gas creates relocations against local labels, it creates its
5037 own symbol for the section, but does put the symbol into the
5038 symbol chain, so udata is 0. When the linker is generating
5039 relocatable output, this section symbol may be for one of the
5040 input sections rather than the output section. */
5041 if (asym_ptr->udata.i == 0
5042 && (flags & BSF_SECTION_SYM)
5043 && asym_ptr->section)
5048 sec = asym_ptr->section;
5049 if (sec->owner != abfd && sec->output_section != NULL)
5050 sec = sec->output_section;
5051 if (sec->owner == abfd
5052 && (indx = sec->index) < elf_num_section_syms (abfd)
5053 && elf_section_syms (abfd)[indx] != NULL)
5054 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5057 idx = asym_ptr->udata.i;
5061 /* This case can occur when using --strip-symbol on a symbol
5062 which is used in a relocation entry. */
5063 (*_bfd_error_handler)
5064 (_("%B: symbol `%s' required but not present"),
5065 abfd, bfd_asymbol_name (asym_ptr));
5066 bfd_set_error (bfd_error_no_symbols);
5073 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5074 (long) asym_ptr, asym_ptr->name, idx, flags,
5075 elf_symbol_flags (flags));
5083 /* Rewrite program header information. */
5086 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5088 Elf_Internal_Ehdr *iehdr;
5089 struct elf_segment_map *map;
5090 struct elf_segment_map *map_first;
5091 struct elf_segment_map **pointer_to_map;
5092 Elf_Internal_Phdr *segment;
5095 unsigned int num_segments;
5096 bfd_boolean phdr_included = FALSE;
5097 bfd_vma maxpagesize;
5098 struct elf_segment_map *phdr_adjust_seg = NULL;
5099 unsigned int phdr_adjust_num = 0;
5100 const struct elf_backend_data *bed;
5102 bed = get_elf_backend_data (ibfd);
5103 iehdr = elf_elfheader (ibfd);
5106 pointer_to_map = &map_first;
5108 num_segments = elf_elfheader (ibfd)->e_phnum;
5109 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5111 /* Returns the end address of the segment + 1. */
5112 #define SEGMENT_END(segment, start) \
5113 (start + (segment->p_memsz > segment->p_filesz \
5114 ? segment->p_memsz : segment->p_filesz))
5116 #define SECTION_SIZE(section, segment) \
5117 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5118 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5119 ? section->size : 0)
5121 /* Returns TRUE if the given section is contained within
5122 the given segment. VMA addresses are compared. */
5123 #define IS_CONTAINED_BY_VMA(section, segment) \
5124 (section->vma >= segment->p_vaddr \
5125 && (section->vma + SECTION_SIZE (section, segment) \
5126 <= (SEGMENT_END (segment, segment->p_vaddr))))
5128 /* Returns TRUE if the given section is contained within
5129 the given segment. LMA addresses are compared. */
5130 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5131 (section->lma >= base \
5132 && (section->lma + SECTION_SIZE (section, segment) \
5133 <= SEGMENT_END (segment, base)))
5135 /* Handle PT_NOTE segment. */
5136 #define IS_NOTE(p, s) \
5137 (p->p_type == PT_NOTE \
5138 && elf_section_type (s) == SHT_NOTE \
5139 && (bfd_vma) s->filepos >= p->p_offset \
5140 && ((bfd_vma) s->filepos + s->size \
5141 <= p->p_offset + p->p_filesz))
5143 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5145 #define IS_COREFILE_NOTE(p, s) \
5147 && bfd_get_format (ibfd) == bfd_core \
5151 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5152 linker, which generates a PT_INTERP section with p_vaddr and
5153 p_memsz set to 0. */
5154 #define IS_SOLARIS_PT_INTERP(p, s) \
5156 && p->p_paddr == 0 \
5157 && p->p_memsz == 0 \
5158 && p->p_filesz > 0 \
5159 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5161 && (bfd_vma) s->filepos >= p->p_offset \
5162 && ((bfd_vma) s->filepos + s->size \
5163 <= p->p_offset + p->p_filesz))
5165 /* Decide if the given section should be included in the given segment.
5166 A section will be included if:
5167 1. It is within the address space of the segment -- we use the LMA
5168 if that is set for the segment and the VMA otherwise,
5169 2. It is an allocated section or a NOTE section in a PT_NOTE
5171 3. There is an output section associated with it,
5172 4. The section has not already been allocated to a previous segment.
5173 5. PT_GNU_STACK segments do not include any sections.
5174 6. PT_TLS segment includes only SHF_TLS sections.
5175 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5176 8. PT_DYNAMIC should not contain empty sections at the beginning
5177 (with the possible exception of .dynamic). */
5178 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5179 ((((segment->p_paddr \
5180 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5181 : IS_CONTAINED_BY_VMA (section, segment)) \
5182 && (section->flags & SEC_ALLOC) != 0) \
5183 || IS_NOTE (segment, section)) \
5184 && segment->p_type != PT_GNU_STACK \
5185 && (segment->p_type != PT_TLS \
5186 || (section->flags & SEC_THREAD_LOCAL)) \
5187 && (segment->p_type == PT_LOAD \
5188 || segment->p_type == PT_TLS \
5189 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5190 && (segment->p_type != PT_DYNAMIC \
5191 || SECTION_SIZE (section, segment) > 0 \
5192 || (segment->p_paddr \
5193 ? segment->p_paddr != section->lma \
5194 : segment->p_vaddr != section->vma) \
5195 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5197 && !section->segment_mark)
5199 /* If the output section of a section in the input segment is NULL,
5200 it is removed from the corresponding output segment. */
5201 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5202 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5203 && section->output_section != NULL)
5205 /* Returns TRUE iff seg1 starts after the end of seg2. */
5206 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5207 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5209 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5210 their VMA address ranges and their LMA address ranges overlap.
5211 It is possible to have overlapping VMA ranges without overlapping LMA
5212 ranges. RedBoot images for example can have both .data and .bss mapped
5213 to the same VMA range, but with the .data section mapped to a different
5215 #define SEGMENT_OVERLAPS(seg1, seg2) \
5216 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5217 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5218 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5219 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5221 /* Initialise the segment mark field. */
5222 for (section = ibfd->sections; section != NULL; section = section->next)
5223 section->segment_mark = FALSE;
5225 /* Scan through the segments specified in the program header
5226 of the input BFD. For this first scan we look for overlaps
5227 in the loadable segments. These can be created by weird
5228 parameters to objcopy. Also, fix some solaris weirdness. */
5229 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5234 Elf_Internal_Phdr *segment2;
5236 if (segment->p_type == PT_INTERP)
5237 for (section = ibfd->sections; section; section = section->next)
5238 if (IS_SOLARIS_PT_INTERP (segment, section))
5240 /* Mininal change so that the normal section to segment
5241 assignment code will work. */
5242 segment->p_vaddr = section->vma;
5246 if (segment->p_type != PT_LOAD)
5248 /* Remove PT_GNU_RELRO segment. */
5249 if (segment->p_type == PT_GNU_RELRO)
5250 segment->p_type = PT_NULL;
5254 /* Determine if this segment overlaps any previous segments. */
5255 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5257 bfd_signed_vma extra_length;
5259 if (segment2->p_type != PT_LOAD
5260 || !SEGMENT_OVERLAPS (segment, segment2))
5263 /* Merge the two segments together. */
5264 if (segment2->p_vaddr < segment->p_vaddr)
5266 /* Extend SEGMENT2 to include SEGMENT and then delete
5268 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5269 - SEGMENT_END (segment2, segment2->p_vaddr));
5271 if (extra_length > 0)
5273 segment2->p_memsz += extra_length;
5274 segment2->p_filesz += extra_length;
5277 segment->p_type = PT_NULL;
5279 /* Since we have deleted P we must restart the outer loop. */
5281 segment = elf_tdata (ibfd)->phdr;
5286 /* Extend SEGMENT to include SEGMENT2 and then delete
5288 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5289 - SEGMENT_END (segment, segment->p_vaddr));
5291 if (extra_length > 0)
5293 segment->p_memsz += extra_length;
5294 segment->p_filesz += extra_length;
5297 segment2->p_type = PT_NULL;
5302 /* The second scan attempts to assign sections to segments. */
5303 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5307 unsigned int section_count;
5308 asection **sections;
5309 asection *output_section;
5311 bfd_vma matching_lma;
5312 bfd_vma suggested_lma;
5315 asection *first_section;
5316 bfd_boolean first_matching_lma;
5317 bfd_boolean first_suggested_lma;
5319 if (segment->p_type == PT_NULL)
5322 first_section = NULL;
5323 /* Compute how many sections might be placed into this segment. */
5324 for (section = ibfd->sections, section_count = 0;
5326 section = section->next)
5328 /* Find the first section in the input segment, which may be
5329 removed from the corresponding output segment. */
5330 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5332 if (first_section == NULL)
5333 first_section = section;
5334 if (section->output_section != NULL)
5339 /* Allocate a segment map big enough to contain
5340 all of the sections we have selected. */
5341 amt = sizeof (struct elf_segment_map);
5342 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5343 map = bfd_zalloc (obfd, amt);
5347 /* Initialise the fields of the segment map. Default to
5348 using the physical address of the segment in the input BFD. */
5350 map->p_type = segment->p_type;
5351 map->p_flags = segment->p_flags;
5352 map->p_flags_valid = 1;
5354 /* If the first section in the input segment is removed, there is
5355 no need to preserve segment physical address in the corresponding
5357 if (!first_section || first_section->output_section != NULL)
5359 map->p_paddr = segment->p_paddr;
5360 map->p_paddr_valid = 1;
5363 /* Determine if this segment contains the ELF file header
5364 and if it contains the program headers themselves. */
5365 map->includes_filehdr = (segment->p_offset == 0
5366 && segment->p_filesz >= iehdr->e_ehsize);
5367 map->includes_phdrs = 0;
5369 if (!phdr_included || segment->p_type != PT_LOAD)
5371 map->includes_phdrs =
5372 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5373 && (segment->p_offset + segment->p_filesz
5374 >= ((bfd_vma) iehdr->e_phoff
5375 + iehdr->e_phnum * iehdr->e_phentsize)));
5377 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5378 phdr_included = TRUE;
5381 if (section_count == 0)
5383 /* Special segments, such as the PT_PHDR segment, may contain
5384 no sections, but ordinary, loadable segments should contain
5385 something. They are allowed by the ELF spec however, so only
5386 a warning is produced. */
5387 if (segment->p_type == PT_LOAD)
5388 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5389 " detected, is this intentional ?\n"),
5393 *pointer_to_map = map;
5394 pointer_to_map = &map->next;
5399 /* Now scan the sections in the input BFD again and attempt
5400 to add their corresponding output sections to the segment map.
5401 The problem here is how to handle an output section which has
5402 been moved (ie had its LMA changed). There are four possibilities:
5404 1. None of the sections have been moved.
5405 In this case we can continue to use the segment LMA from the
5408 2. All of the sections have been moved by the same amount.
5409 In this case we can change the segment's LMA to match the LMA
5410 of the first section.
5412 3. Some of the sections have been moved, others have not.
5413 In this case those sections which have not been moved can be
5414 placed in the current segment which will have to have its size,
5415 and possibly its LMA changed, and a new segment or segments will
5416 have to be created to contain the other sections.
5418 4. The sections have been moved, but not by the same amount.
5419 In this case we can change the segment's LMA to match the LMA
5420 of the first section and we will have to create a new segment
5421 or segments to contain the other sections.
5423 In order to save time, we allocate an array to hold the section
5424 pointers that we are interested in. As these sections get assigned
5425 to a segment, they are removed from this array. */
5427 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5428 to work around this long long bug. */
5429 sections = bfd_malloc2 (section_count, sizeof (asection *));
5430 if (sections == NULL)
5433 /* Step One: Scan for segment vs section LMA conflicts.
5434 Also add the sections to the section array allocated above.
5435 Also add the sections to the current segment. In the common
5436 case, where the sections have not been moved, this means that
5437 we have completely filled the segment, and there is nothing
5442 first_matching_lma = TRUE;
5443 first_suggested_lma = TRUE;
5445 for (section = ibfd->sections;
5447 section = section->next)
5448 if (section == first_section)
5451 for (j = 0; section != NULL; section = section->next)
5453 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5455 output_section = section->output_section;
5457 sections[j++] = section;
5459 /* The Solaris native linker always sets p_paddr to 0.
5460 We try to catch that case here, and set it to the
5461 correct value. Note - some backends require that
5462 p_paddr be left as zero. */
5463 if (segment->p_paddr == 0
5464 && segment->p_vaddr != 0
5465 && !bed->want_p_paddr_set_to_zero
5467 && output_section->lma != 0
5468 && output_section->vma == (segment->p_vaddr
5469 + (map->includes_filehdr
5472 + (map->includes_phdrs
5474 * iehdr->e_phentsize)
5476 map->p_paddr = segment->p_vaddr;
5478 /* Match up the physical address of the segment with the
5479 LMA address of the output section. */
5480 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5481 || IS_COREFILE_NOTE (segment, section)
5482 || (bed->want_p_paddr_set_to_zero
5483 && IS_CONTAINED_BY_VMA (output_section, segment)))
5485 if (first_matching_lma || output_section->lma < matching_lma)
5487 matching_lma = output_section->lma;
5488 first_matching_lma = FALSE;
5491 /* We assume that if the section fits within the segment
5492 then it does not overlap any other section within that
5494 map->sections[isec++] = output_section;
5496 else if (first_suggested_lma)
5498 suggested_lma = output_section->lma;
5499 first_suggested_lma = FALSE;
5502 if (j == section_count)
5507 BFD_ASSERT (j == section_count);
5509 /* Step Two: Adjust the physical address of the current segment,
5511 if (isec == section_count)
5513 /* All of the sections fitted within the segment as currently
5514 specified. This is the default case. Add the segment to
5515 the list of built segments and carry on to process the next
5516 program header in the input BFD. */
5517 map->count = section_count;
5518 *pointer_to_map = map;
5519 pointer_to_map = &map->next;
5521 if (!bed->want_p_paddr_set_to_zero
5522 && matching_lma != map->p_paddr
5523 && !map->includes_filehdr && !map->includes_phdrs)
5524 /* There is some padding before the first section in the
5525 segment. So, we must account for that in the output
5527 map->p_vaddr_offset = matching_lma - map->p_paddr;
5534 if (!first_matching_lma)
5536 /* At least one section fits inside the current segment.
5537 Keep it, but modify its physical address to match the
5538 LMA of the first section that fitted. */
5539 map->p_paddr = matching_lma;
5543 /* None of the sections fitted inside the current segment.
5544 Change the current segment's physical address to match
5545 the LMA of the first section. */
5546 map->p_paddr = suggested_lma;
5549 /* Offset the segment physical address from the lma
5550 to allow for space taken up by elf headers. */
5551 if (map->includes_filehdr)
5552 map->p_paddr -= iehdr->e_ehsize;
5554 if (map->includes_phdrs)
5556 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5558 /* iehdr->e_phnum is just an estimate of the number
5559 of program headers that we will need. Make a note
5560 here of the number we used and the segment we chose
5561 to hold these headers, so that we can adjust the
5562 offset when we know the correct value. */
5563 phdr_adjust_num = iehdr->e_phnum;
5564 phdr_adjust_seg = map;
5568 /* Step Three: Loop over the sections again, this time assigning
5569 those that fit to the current segment and removing them from the
5570 sections array; but making sure not to leave large gaps. Once all
5571 possible sections have been assigned to the current segment it is
5572 added to the list of built segments and if sections still remain
5573 to be assigned, a new segment is constructed before repeating
5580 first_suggested_lma = TRUE;
5582 /* Fill the current segment with sections that fit. */
5583 for (j = 0; j < section_count; j++)
5585 section = sections[j];
5587 if (section == NULL)
5590 output_section = section->output_section;
5592 BFD_ASSERT (output_section != NULL);
5594 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5595 || IS_COREFILE_NOTE (segment, section))
5597 if (map->count == 0)
5599 /* If the first section in a segment does not start at
5600 the beginning of the segment, then something is
5602 if (output_section->lma
5604 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5605 + (map->includes_phdrs
5606 ? iehdr->e_phnum * iehdr->e_phentsize
5614 prev_sec = map->sections[map->count - 1];
5616 /* If the gap between the end of the previous section
5617 and the start of this section is more than
5618 maxpagesize then we need to start a new segment. */
5619 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5621 < BFD_ALIGN (output_section->lma, maxpagesize))
5622 || (prev_sec->lma + prev_sec->size
5623 > output_section->lma))
5625 if (first_suggested_lma)
5627 suggested_lma = output_section->lma;
5628 first_suggested_lma = FALSE;
5635 map->sections[map->count++] = output_section;
5638 section->segment_mark = TRUE;
5640 else if (first_suggested_lma)
5642 suggested_lma = output_section->lma;
5643 first_suggested_lma = FALSE;
5647 BFD_ASSERT (map->count > 0);
5649 /* Add the current segment to the list of built segments. */
5650 *pointer_to_map = map;
5651 pointer_to_map = &map->next;
5653 if (isec < section_count)
5655 /* We still have not allocated all of the sections to
5656 segments. Create a new segment here, initialise it
5657 and carry on looping. */
5658 amt = sizeof (struct elf_segment_map);
5659 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5660 map = bfd_alloc (obfd, amt);
5667 /* Initialise the fields of the segment map. Set the physical
5668 physical address to the LMA of the first section that has
5669 not yet been assigned. */
5671 map->p_type = segment->p_type;
5672 map->p_flags = segment->p_flags;
5673 map->p_flags_valid = 1;
5674 map->p_paddr = suggested_lma;
5675 map->p_paddr_valid = 1;
5676 map->includes_filehdr = 0;
5677 map->includes_phdrs = 0;
5680 while (isec < section_count);
5685 /* The Solaris linker creates program headers in which all the
5686 p_paddr fields are zero. When we try to objcopy or strip such a
5687 file, we get confused. Check for this case, and if we find it
5688 reset the p_paddr_valid fields. */
5689 for (map = map_first; map != NULL; map = map->next)
5690 if (map->p_paddr != 0)
5693 for (map = map_first; map != NULL; map = map->next)
5694 map->p_paddr_valid = 0;
5696 elf_tdata (obfd)->segment_map = map_first;
5698 /* If we had to estimate the number of program headers that were
5699 going to be needed, then check our estimate now and adjust
5700 the offset if necessary. */
5701 if (phdr_adjust_seg != NULL)
5705 for (count = 0, map = map_first; map != NULL; map = map->next)
5708 if (count > phdr_adjust_num)
5709 phdr_adjust_seg->p_paddr
5710 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5715 #undef IS_CONTAINED_BY_VMA
5716 #undef IS_CONTAINED_BY_LMA
5718 #undef IS_COREFILE_NOTE
5719 #undef IS_SOLARIS_PT_INTERP
5720 #undef IS_SECTION_IN_INPUT_SEGMENT
5721 #undef INCLUDE_SECTION_IN_SEGMENT
5722 #undef SEGMENT_AFTER_SEGMENT
5723 #undef SEGMENT_OVERLAPS
5727 /* Copy ELF program header information. */
5730 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5732 Elf_Internal_Ehdr *iehdr;
5733 struct elf_segment_map *map;
5734 struct elf_segment_map *map_first;
5735 struct elf_segment_map **pointer_to_map;
5736 Elf_Internal_Phdr *segment;
5738 unsigned int num_segments;
5739 bfd_boolean phdr_included = FALSE;
5741 iehdr = elf_elfheader (ibfd);
5744 pointer_to_map = &map_first;
5746 num_segments = elf_elfheader (ibfd)->e_phnum;
5747 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5752 unsigned int section_count;
5754 Elf_Internal_Shdr *this_hdr;
5755 asection *first_section = NULL;
5756 asection *lowest_section = NULL;
5758 /* Compute how many sections are in this segment. */
5759 for (section = ibfd->sections, section_count = 0;
5761 section = section->next)
5763 this_hdr = &(elf_section_data(section)->this_hdr);
5764 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5767 first_section = lowest_section = section;
5768 if (section->lma < lowest_section->lma)
5769 lowest_section = section;
5774 /* Allocate a segment map big enough to contain
5775 all of the sections we have selected. */
5776 amt = sizeof (struct elf_segment_map);
5777 if (section_count != 0)
5778 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5779 map = bfd_zalloc (obfd, amt);
5783 /* Initialize the fields of the output segment map with the
5786 map->p_type = segment->p_type;
5787 map->p_flags = segment->p_flags;
5788 map->p_flags_valid = 1;
5789 map->p_paddr = segment->p_paddr;
5790 map->p_paddr_valid = 1;
5791 map->p_align = segment->p_align;
5792 map->p_align_valid = 1;
5793 map->p_vaddr_offset = 0;
5795 if (map->p_type == PT_GNU_RELRO
5796 && segment->p_filesz == segment->p_memsz)
5798 /* The PT_GNU_RELRO segment may contain the first a few
5799 bytes in the .got.plt section even if the whole .got.plt
5800 section isn't in the PT_GNU_RELRO segment. We won't
5801 change the size of the PT_GNU_RELRO segment. */
5802 map->p_size = segment->p_filesz;
5803 map->p_size_valid = 1;
5806 /* Determine if this segment contains the ELF file header
5807 and if it contains the program headers themselves. */
5808 map->includes_filehdr = (segment->p_offset == 0
5809 && segment->p_filesz >= iehdr->e_ehsize);
5811 map->includes_phdrs = 0;
5812 if (! phdr_included || segment->p_type != PT_LOAD)
5814 map->includes_phdrs =
5815 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5816 && (segment->p_offset + segment->p_filesz
5817 >= ((bfd_vma) iehdr->e_phoff
5818 + iehdr->e_phnum * iehdr->e_phentsize)));
5820 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5821 phdr_included = TRUE;
5824 if (!map->includes_phdrs && !map->includes_filehdr)
5825 /* There is some other padding before the first section. */
5826 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5827 - segment->p_paddr);
5829 if (section_count != 0)
5831 unsigned int isec = 0;
5833 for (section = first_section;
5835 section = section->next)
5837 this_hdr = &(elf_section_data(section)->this_hdr);
5838 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5840 map->sections[isec++] = section->output_section;
5841 if (isec == section_count)
5847 map->count = section_count;
5848 *pointer_to_map = map;
5849 pointer_to_map = &map->next;
5852 elf_tdata (obfd)->segment_map = map_first;
5856 /* Copy private BFD data. This copies or rewrites ELF program header
5860 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5862 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5863 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5866 if (elf_tdata (ibfd)->phdr == NULL)
5869 if (ibfd->xvec == obfd->xvec)
5871 /* Check to see if any sections in the input BFD
5872 covered by ELF program header have changed. */
5873 Elf_Internal_Phdr *segment;
5874 asection *section, *osec;
5875 unsigned int i, num_segments;
5876 Elf_Internal_Shdr *this_hdr;
5877 const struct elf_backend_data *bed;
5879 bed = get_elf_backend_data (ibfd);
5881 /* Regenerate the segment map if p_paddr is set to 0. */
5882 if (bed->want_p_paddr_set_to_zero)
5885 /* Initialize the segment mark field. */
5886 for (section = obfd->sections; section != NULL;
5887 section = section->next)
5888 section->segment_mark = FALSE;
5890 num_segments = elf_elfheader (ibfd)->e_phnum;
5891 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5895 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5896 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5897 which severly confuses things, so always regenerate the segment
5898 map in this case. */
5899 if (segment->p_paddr == 0
5900 && segment->p_memsz == 0
5901 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5904 for (section = ibfd->sections;
5905 section != NULL; section = section->next)
5907 /* We mark the output section so that we know it comes
5908 from the input BFD. */
5909 osec = section->output_section;
5911 osec->segment_mark = TRUE;
5913 /* Check if this section is covered by the segment. */
5914 this_hdr = &(elf_section_data(section)->this_hdr);
5915 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5917 /* FIXME: Check if its output section is changed or
5918 removed. What else do we need to check? */
5920 || section->flags != osec->flags
5921 || section->lma != osec->lma
5922 || section->vma != osec->vma
5923 || section->size != osec->size
5924 || section->rawsize != osec->rawsize
5925 || section->alignment_power != osec->alignment_power)
5931 /* Check to see if any output section do not come from the
5933 for (section = obfd->sections; section != NULL;
5934 section = section->next)
5936 if (section->segment_mark == FALSE)
5939 section->segment_mark = FALSE;
5942 return copy_elf_program_header (ibfd, obfd);
5946 return rewrite_elf_program_header (ibfd, obfd);
5949 /* Initialize private output section information from input section. */
5952 _bfd_elf_init_private_section_data (bfd *ibfd,
5956 struct bfd_link_info *link_info)
5959 Elf_Internal_Shdr *ihdr, *ohdr;
5960 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5962 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5963 || obfd->xvec->flavour != bfd_target_elf_flavour)
5966 /* Don't copy the output ELF section type from input if the
5967 output BFD section flags have been set to something different.
5968 elf_fake_sections will set ELF section type based on BFD
5970 if (elf_section_type (osec) == SHT_NULL
5971 && (osec->flags == isec->flags || !osec->flags))
5972 elf_section_type (osec) = elf_section_type (isec);
5974 /* FIXME: Is this correct for all OS/PROC specific flags? */
5975 elf_section_flags (osec) |= (elf_section_flags (isec)
5976 & (SHF_MASKOS | SHF_MASKPROC));
5978 /* Set things up for objcopy and relocatable link. The output
5979 SHT_GROUP section will have its elf_next_in_group pointing back
5980 to the input group members. Ignore linker created group section.
5981 See elfNN_ia64_object_p in elfxx-ia64.c. */
5984 if (elf_sec_group (isec) == NULL
5985 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
5987 if (elf_section_flags (isec) & SHF_GROUP)
5988 elf_section_flags (osec) |= SHF_GROUP;
5989 elf_next_in_group (osec) = elf_next_in_group (isec);
5990 elf_group_name (osec) = elf_group_name (isec);
5994 ihdr = &elf_section_data (isec)->this_hdr;
5996 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
5997 don't use the output section of the linked-to section since it
5998 may be NULL at this point. */
5999 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6001 ohdr = &elf_section_data (osec)->this_hdr;
6002 ohdr->sh_flags |= SHF_LINK_ORDER;
6003 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6006 osec->use_rela_p = isec->use_rela_p;
6011 /* Copy private section information. This copies over the entsize
6012 field, and sometimes the info field. */
6015 _bfd_elf_copy_private_section_data (bfd *ibfd,
6020 Elf_Internal_Shdr *ihdr, *ohdr;
6022 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6023 || obfd->xvec->flavour != bfd_target_elf_flavour)
6026 ihdr = &elf_section_data (isec)->this_hdr;
6027 ohdr = &elf_section_data (osec)->this_hdr;
6029 ohdr->sh_entsize = ihdr->sh_entsize;
6031 if (ihdr->sh_type == SHT_SYMTAB
6032 || ihdr->sh_type == SHT_DYNSYM
6033 || ihdr->sh_type == SHT_GNU_verneed
6034 || ihdr->sh_type == SHT_GNU_verdef)
6035 ohdr->sh_info = ihdr->sh_info;
6037 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6041 /* Copy private header information. */
6044 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6048 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6049 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6052 /* Copy over private BFD data if it has not already been copied.
6053 This must be done here, rather than in the copy_private_bfd_data
6054 entry point, because the latter is called after the section
6055 contents have been set, which means that the program headers have
6056 already been worked out. */
6057 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6059 if (! copy_private_bfd_data (ibfd, obfd))
6063 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6064 but this might be wrong if we deleted the group section. */
6065 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6066 if (elf_section_type (isec) == SHT_GROUP
6067 && isec->output_section == NULL)
6069 asection *first = elf_next_in_group (isec);
6070 asection *s = first;
6073 if (s->output_section != NULL)
6075 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6076 elf_group_name (s->output_section) = NULL;
6078 s = elf_next_in_group (s);
6087 /* Copy private symbol information. If this symbol is in a section
6088 which we did not map into a BFD section, try to map the section
6089 index correctly. We use special macro definitions for the mapped
6090 section indices; these definitions are interpreted by the
6091 swap_out_syms function. */
6093 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6094 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6095 #define MAP_STRTAB (SHN_HIOS + 3)
6096 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6097 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6100 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6105 elf_symbol_type *isym, *osym;
6107 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6108 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6111 isym = elf_symbol_from (ibfd, isymarg);
6112 osym = elf_symbol_from (obfd, osymarg);
6115 && isym->internal_elf_sym.st_shndx != 0
6117 && bfd_is_abs_section (isym->symbol.section))
6121 shndx = isym->internal_elf_sym.st_shndx;
6122 if (shndx == elf_onesymtab (ibfd))
6123 shndx = MAP_ONESYMTAB;
6124 else if (shndx == elf_dynsymtab (ibfd))
6125 shndx = MAP_DYNSYMTAB;
6126 else if (shndx == elf_tdata (ibfd)->strtab_section)
6128 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6129 shndx = MAP_SHSTRTAB;
6130 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6131 shndx = MAP_SYM_SHNDX;
6132 osym->internal_elf_sym.st_shndx = shndx;
6138 /* Swap out the symbols. */
6141 swap_out_syms (bfd *abfd,
6142 struct bfd_strtab_hash **sttp,
6145 const struct elf_backend_data *bed;
6148 struct bfd_strtab_hash *stt;
6149 Elf_Internal_Shdr *symtab_hdr;
6150 Elf_Internal_Shdr *symtab_shndx_hdr;
6151 Elf_Internal_Shdr *symstrtab_hdr;
6152 bfd_byte *outbound_syms;
6153 bfd_byte *outbound_shndx;
6156 bfd_boolean name_local_sections;
6158 if (!elf_map_symbols (abfd))
6161 /* Dump out the symtabs. */
6162 stt = _bfd_elf_stringtab_init ();
6166 bed = get_elf_backend_data (abfd);
6167 symcount = bfd_get_symcount (abfd);
6168 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6169 symtab_hdr->sh_type = SHT_SYMTAB;
6170 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6171 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6172 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6173 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
6175 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6176 symstrtab_hdr->sh_type = SHT_STRTAB;
6178 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6179 if (outbound_syms == NULL)
6181 _bfd_stringtab_free (stt);
6184 symtab_hdr->contents = outbound_syms;
6186 outbound_shndx = NULL;
6187 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6188 if (symtab_shndx_hdr->sh_name != 0)
6190 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6191 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6192 sizeof (Elf_External_Sym_Shndx));
6193 if (outbound_shndx == NULL)
6195 _bfd_stringtab_free (stt);
6199 symtab_shndx_hdr->contents = outbound_shndx;
6200 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6201 symtab_shndx_hdr->sh_size = amt;
6202 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6203 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6206 /* Now generate the data (for "contents"). */
6208 /* Fill in zeroth symbol and swap it out. */
6209 Elf_Internal_Sym sym;
6215 sym.st_shndx = SHN_UNDEF;
6216 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6217 outbound_syms += bed->s->sizeof_sym;
6218 if (outbound_shndx != NULL)
6219 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6223 = (bed->elf_backend_name_local_section_symbols
6224 && bed->elf_backend_name_local_section_symbols (abfd));
6226 syms = bfd_get_outsymbols (abfd);
6227 for (idx = 0; idx < symcount; idx++)
6229 Elf_Internal_Sym sym;
6230 bfd_vma value = syms[idx]->value;
6231 elf_symbol_type *type_ptr;
6232 flagword flags = syms[idx]->flags;
6235 if (!name_local_sections
6236 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6238 /* Local section symbols have no name. */
6243 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6246 if (sym.st_name == (unsigned long) -1)
6248 _bfd_stringtab_free (stt);
6253 type_ptr = elf_symbol_from (abfd, syms[idx]);
6255 if ((flags & BSF_SECTION_SYM) == 0
6256 && bfd_is_com_section (syms[idx]->section))
6258 /* ELF common symbols put the alignment into the `value' field,
6259 and the size into the `size' field. This is backwards from
6260 how BFD handles it, so reverse it here. */
6261 sym.st_size = value;
6262 if (type_ptr == NULL
6263 || type_ptr->internal_elf_sym.st_value == 0)
6264 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6266 sym.st_value = type_ptr->internal_elf_sym.st_value;
6267 sym.st_shndx = _bfd_elf_section_from_bfd_section
6268 (abfd, syms[idx]->section);
6272 asection *sec = syms[idx]->section;
6275 if (sec->output_section)
6277 value += sec->output_offset;
6278 sec = sec->output_section;
6281 /* Don't add in the section vma for relocatable output. */
6282 if (! relocatable_p)
6284 sym.st_value = value;
6285 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6287 if (bfd_is_abs_section (sec)
6289 && type_ptr->internal_elf_sym.st_shndx != 0)
6291 /* This symbol is in a real ELF section which we did
6292 not create as a BFD section. Undo the mapping done
6293 by copy_private_symbol_data. */
6294 shndx = type_ptr->internal_elf_sym.st_shndx;
6298 shndx = elf_onesymtab (abfd);
6301 shndx = elf_dynsymtab (abfd);
6304 shndx = elf_tdata (abfd)->strtab_section;
6307 shndx = elf_tdata (abfd)->shstrtab_section;
6310 shndx = elf_tdata (abfd)->symtab_shndx_section;
6318 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6324 /* Writing this would be a hell of a lot easier if
6325 we had some decent documentation on bfd, and
6326 knew what to expect of the library, and what to
6327 demand of applications. For example, it
6328 appears that `objcopy' might not set the
6329 section of a symbol to be a section that is
6330 actually in the output file. */
6331 sec2 = bfd_get_section_by_name (abfd, sec->name);
6334 _bfd_error_handler (_("\
6335 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6336 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6338 bfd_set_error (bfd_error_invalid_operation);
6339 _bfd_stringtab_free (stt);
6343 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6344 BFD_ASSERT (shndx != -1);
6348 sym.st_shndx = shndx;
6351 if ((flags & BSF_THREAD_LOCAL) != 0)
6353 else if ((flags & BSF_FUNCTION) != 0)
6355 else if ((flags & BSF_OBJECT) != 0)
6357 else if ((flags & BSF_RELC) != 0)
6359 else if ((flags & BSF_SRELC) != 0)
6364 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6367 /* Processor-specific types. */
6368 if (type_ptr != NULL
6369 && bed->elf_backend_get_symbol_type)
6370 type = ((*bed->elf_backend_get_symbol_type)
6371 (&type_ptr->internal_elf_sym, type));
6373 if (flags & BSF_SECTION_SYM)
6375 if (flags & BSF_GLOBAL)
6376 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6378 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6380 else if (bfd_is_com_section (syms[idx]->section))
6381 sym.st_info = ELF_ST_INFO (STB_GLOBAL,
6382 #ifdef USE_STT_COMMON
6383 type == STT_OBJECT ? STT_COMMON :
6386 else if (bfd_is_und_section (syms[idx]->section))
6387 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6391 else if (flags & BSF_FILE)
6392 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6395 int bind = STB_LOCAL;
6397 if (flags & BSF_LOCAL)
6399 else if (flags & BSF_WEAK)
6401 else if (flags & BSF_GLOBAL)
6404 sym.st_info = ELF_ST_INFO (bind, type);
6407 if (type_ptr != NULL)
6408 sym.st_other = type_ptr->internal_elf_sym.st_other;
6412 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6413 outbound_syms += bed->s->sizeof_sym;
6414 if (outbound_shndx != NULL)
6415 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6419 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6420 symstrtab_hdr->sh_type = SHT_STRTAB;
6422 symstrtab_hdr->sh_flags = 0;
6423 symstrtab_hdr->sh_addr = 0;
6424 symstrtab_hdr->sh_entsize = 0;
6425 symstrtab_hdr->sh_link = 0;
6426 symstrtab_hdr->sh_info = 0;
6427 symstrtab_hdr->sh_addralign = 1;
6432 /* Return the number of bytes required to hold the symtab vector.
6434 Note that we base it on the count plus 1, since we will null terminate
6435 the vector allocated based on this size. However, the ELF symbol table
6436 always has a dummy entry as symbol #0, so it ends up even. */
6439 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6443 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6445 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6446 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6448 symtab_size -= sizeof (asymbol *);
6454 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6458 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6460 if (elf_dynsymtab (abfd) == 0)
6462 bfd_set_error (bfd_error_invalid_operation);
6466 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6467 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6469 symtab_size -= sizeof (asymbol *);
6475 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6478 return (asect->reloc_count + 1) * sizeof (arelent *);
6481 /* Canonicalize the relocs. */
6484 _bfd_elf_canonicalize_reloc (bfd *abfd,
6491 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6493 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6496 tblptr = section->relocation;
6497 for (i = 0; i < section->reloc_count; i++)
6498 *relptr++ = tblptr++;
6502 return section->reloc_count;
6506 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6508 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6509 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6512 bfd_get_symcount (abfd) = symcount;
6517 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6518 asymbol **allocation)
6520 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6521 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6524 bfd_get_dynamic_symcount (abfd) = symcount;
6528 /* Return the size required for the dynamic reloc entries. Any loadable
6529 section that was actually installed in the BFD, and has type SHT_REL
6530 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6531 dynamic reloc section. */
6534 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6539 if (elf_dynsymtab (abfd) == 0)
6541 bfd_set_error (bfd_error_invalid_operation);
6545 ret = sizeof (arelent *);
6546 for (s = abfd->sections; s != NULL; s = s->next)
6547 if ((s->flags & SEC_LOAD) != 0
6548 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6549 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6550 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6551 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6552 * sizeof (arelent *));
6557 /* Canonicalize the dynamic relocation entries. Note that we return the
6558 dynamic relocations as a single block, although they are actually
6559 associated with particular sections; the interface, which was
6560 designed for SunOS style shared libraries, expects that there is only
6561 one set of dynamic relocs. Any loadable section that was actually
6562 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6563 dynamic symbol table, is considered to be a dynamic reloc section. */
6566 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6570 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6574 if (elf_dynsymtab (abfd) == 0)
6576 bfd_set_error (bfd_error_invalid_operation);
6580 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6582 for (s = abfd->sections; s != NULL; s = s->next)
6584 if ((s->flags & SEC_LOAD) != 0
6585 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6586 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6587 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6592 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6594 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6596 for (i = 0; i < count; i++)
6607 /* Read in the version information. */
6610 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6612 bfd_byte *contents = NULL;
6613 unsigned int freeidx = 0;
6615 if (elf_dynverref (abfd) != 0)
6617 Elf_Internal_Shdr *hdr;
6618 Elf_External_Verneed *everneed;
6619 Elf_Internal_Verneed *iverneed;
6621 bfd_byte *contents_end;
6623 hdr = &elf_tdata (abfd)->dynverref_hdr;
6625 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6626 sizeof (Elf_Internal_Verneed));
6627 if (elf_tdata (abfd)->verref == NULL)
6630 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6632 contents = bfd_malloc (hdr->sh_size);
6633 if (contents == NULL)
6635 error_return_verref:
6636 elf_tdata (abfd)->verref = NULL;
6637 elf_tdata (abfd)->cverrefs = 0;
6640 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6641 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6642 goto error_return_verref;
6644 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6645 goto error_return_verref;
6647 BFD_ASSERT (sizeof (Elf_External_Verneed)
6648 == sizeof (Elf_External_Vernaux));
6649 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6650 everneed = (Elf_External_Verneed *) contents;
6651 iverneed = elf_tdata (abfd)->verref;
6652 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6654 Elf_External_Vernaux *evernaux;
6655 Elf_Internal_Vernaux *ivernaux;
6658 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6660 iverneed->vn_bfd = abfd;
6662 iverneed->vn_filename =
6663 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6665 if (iverneed->vn_filename == NULL)
6666 goto error_return_verref;
6668 if (iverneed->vn_cnt == 0)
6669 iverneed->vn_auxptr = NULL;
6672 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6673 sizeof (Elf_Internal_Vernaux));
6674 if (iverneed->vn_auxptr == NULL)
6675 goto error_return_verref;
6678 if (iverneed->vn_aux
6679 > (size_t) (contents_end - (bfd_byte *) everneed))
6680 goto error_return_verref;
6682 evernaux = ((Elf_External_Vernaux *)
6683 ((bfd_byte *) everneed + iverneed->vn_aux));
6684 ivernaux = iverneed->vn_auxptr;
6685 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6687 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6689 ivernaux->vna_nodename =
6690 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6691 ivernaux->vna_name);
6692 if (ivernaux->vna_nodename == NULL)
6693 goto error_return_verref;
6695 if (j + 1 < iverneed->vn_cnt)
6696 ivernaux->vna_nextptr = ivernaux + 1;
6698 ivernaux->vna_nextptr = NULL;
6700 if (ivernaux->vna_next
6701 > (size_t) (contents_end - (bfd_byte *) evernaux))
6702 goto error_return_verref;
6704 evernaux = ((Elf_External_Vernaux *)
6705 ((bfd_byte *) evernaux + ivernaux->vna_next));
6707 if (ivernaux->vna_other > freeidx)
6708 freeidx = ivernaux->vna_other;
6711 if (i + 1 < hdr->sh_info)
6712 iverneed->vn_nextref = iverneed + 1;
6714 iverneed->vn_nextref = NULL;
6716 if (iverneed->vn_next
6717 > (size_t) (contents_end - (bfd_byte *) everneed))
6718 goto error_return_verref;
6720 everneed = ((Elf_External_Verneed *)
6721 ((bfd_byte *) everneed + iverneed->vn_next));
6728 if (elf_dynverdef (abfd) != 0)
6730 Elf_Internal_Shdr *hdr;
6731 Elf_External_Verdef *everdef;
6732 Elf_Internal_Verdef *iverdef;
6733 Elf_Internal_Verdef *iverdefarr;
6734 Elf_Internal_Verdef iverdefmem;
6736 unsigned int maxidx;
6737 bfd_byte *contents_end_def, *contents_end_aux;
6739 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6741 contents = bfd_malloc (hdr->sh_size);
6742 if (contents == NULL)
6744 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6745 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6748 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6751 BFD_ASSERT (sizeof (Elf_External_Verdef)
6752 >= sizeof (Elf_External_Verdaux));
6753 contents_end_def = contents + hdr->sh_size
6754 - sizeof (Elf_External_Verdef);
6755 contents_end_aux = contents + hdr->sh_size
6756 - sizeof (Elf_External_Verdaux);
6758 /* We know the number of entries in the section but not the maximum
6759 index. Therefore we have to run through all entries and find
6761 everdef = (Elf_External_Verdef *) contents;
6763 for (i = 0; i < hdr->sh_info; ++i)
6765 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6767 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6768 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6770 if (iverdefmem.vd_next
6771 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6774 everdef = ((Elf_External_Verdef *)
6775 ((bfd_byte *) everdef + iverdefmem.vd_next));
6778 if (default_imported_symver)
6780 if (freeidx > maxidx)
6785 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6786 sizeof (Elf_Internal_Verdef));
6787 if (elf_tdata (abfd)->verdef == NULL)
6790 elf_tdata (abfd)->cverdefs = maxidx;
6792 everdef = (Elf_External_Verdef *) contents;
6793 iverdefarr = elf_tdata (abfd)->verdef;
6794 for (i = 0; i < hdr->sh_info; i++)
6796 Elf_External_Verdaux *everdaux;
6797 Elf_Internal_Verdaux *iverdaux;
6800 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6802 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6804 error_return_verdef:
6805 elf_tdata (abfd)->verdef = NULL;
6806 elf_tdata (abfd)->cverdefs = 0;
6810 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6811 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6813 iverdef->vd_bfd = abfd;
6815 if (iverdef->vd_cnt == 0)
6816 iverdef->vd_auxptr = NULL;
6819 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6820 sizeof (Elf_Internal_Verdaux));
6821 if (iverdef->vd_auxptr == NULL)
6822 goto error_return_verdef;
6826 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6827 goto error_return_verdef;
6829 everdaux = ((Elf_External_Verdaux *)
6830 ((bfd_byte *) everdef + iverdef->vd_aux));
6831 iverdaux = iverdef->vd_auxptr;
6832 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6834 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6836 iverdaux->vda_nodename =
6837 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6838 iverdaux->vda_name);
6839 if (iverdaux->vda_nodename == NULL)
6840 goto error_return_verdef;
6842 if (j + 1 < iverdef->vd_cnt)
6843 iverdaux->vda_nextptr = iverdaux + 1;
6845 iverdaux->vda_nextptr = NULL;
6847 if (iverdaux->vda_next
6848 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6849 goto error_return_verdef;
6851 everdaux = ((Elf_External_Verdaux *)
6852 ((bfd_byte *) everdaux + iverdaux->vda_next));
6855 if (iverdef->vd_cnt)
6856 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6858 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6859 iverdef->vd_nextdef = iverdef + 1;
6861 iverdef->vd_nextdef = NULL;
6863 everdef = ((Elf_External_Verdef *)
6864 ((bfd_byte *) everdef + iverdef->vd_next));
6870 else if (default_imported_symver)
6877 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6878 sizeof (Elf_Internal_Verdef));
6879 if (elf_tdata (abfd)->verdef == NULL)
6882 elf_tdata (abfd)->cverdefs = freeidx;
6885 /* Create a default version based on the soname. */
6886 if (default_imported_symver)
6888 Elf_Internal_Verdef *iverdef;
6889 Elf_Internal_Verdaux *iverdaux;
6891 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6893 iverdef->vd_version = VER_DEF_CURRENT;
6894 iverdef->vd_flags = 0;
6895 iverdef->vd_ndx = freeidx;
6896 iverdef->vd_cnt = 1;
6898 iverdef->vd_bfd = abfd;
6900 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6901 if (iverdef->vd_nodename == NULL)
6902 goto error_return_verdef;
6903 iverdef->vd_nextdef = NULL;
6904 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6905 if (iverdef->vd_auxptr == NULL)
6906 goto error_return_verdef;
6908 iverdaux = iverdef->vd_auxptr;
6909 iverdaux->vda_nodename = iverdef->vd_nodename;
6910 iverdaux->vda_nextptr = NULL;
6916 if (contents != NULL)
6922 _bfd_elf_make_empty_symbol (bfd *abfd)
6924 elf_symbol_type *newsym;
6925 bfd_size_type amt = sizeof (elf_symbol_type);
6927 newsym = bfd_zalloc (abfd, amt);
6932 newsym->symbol.the_bfd = abfd;
6933 return &newsym->symbol;
6938 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6942 bfd_symbol_info (symbol, ret);
6945 /* Return whether a symbol name implies a local symbol. Most targets
6946 use this function for the is_local_label_name entry point, but some
6950 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6953 /* Normal local symbols start with ``.L''. */
6954 if (name[0] == '.' && name[1] == 'L')
6957 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6958 DWARF debugging symbols starting with ``..''. */
6959 if (name[0] == '.' && name[1] == '.')
6962 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6963 emitting DWARF debugging output. I suspect this is actually a
6964 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6965 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6966 underscore to be emitted on some ELF targets). For ease of use,
6967 we treat such symbols as local. */
6968 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6975 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6976 asymbol *symbol ATTRIBUTE_UNUSED)
6983 _bfd_elf_set_arch_mach (bfd *abfd,
6984 enum bfd_architecture arch,
6985 unsigned long machine)
6987 /* If this isn't the right architecture for this backend, and this
6988 isn't the generic backend, fail. */
6989 if (arch != get_elf_backend_data (abfd)->arch
6990 && arch != bfd_arch_unknown
6991 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6994 return bfd_default_set_arch_mach (abfd, arch, machine);
6997 /* Find the function to a particular section and offset,
6998 for error reporting. */
7001 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7005 const char **filename_ptr,
7006 const char **functionname_ptr)
7008 const char *filename;
7009 asymbol *func, *file;
7012 /* ??? Given multiple file symbols, it is impossible to reliably
7013 choose the right file name for global symbols. File symbols are
7014 local symbols, and thus all file symbols must sort before any
7015 global symbols. The ELF spec may be interpreted to say that a
7016 file symbol must sort before other local symbols, but currently
7017 ld -r doesn't do this. So, for ld -r output, it is possible to
7018 make a better choice of file name for local symbols by ignoring
7019 file symbols appearing after a given local symbol. */
7020 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7026 state = nothing_seen;
7028 for (p = symbols; *p != NULL; p++)
7032 q = (elf_symbol_type *) *p;
7034 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7040 if (state == symbol_seen)
7041 state = file_after_symbol_seen;
7045 if (bfd_get_section (&q->symbol) == section
7046 && q->symbol.value >= low_func
7047 && q->symbol.value <= offset)
7049 func = (asymbol *) q;
7050 low_func = q->symbol.value;
7053 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7054 || state != file_after_symbol_seen))
7055 filename = bfd_asymbol_name (file);
7059 if (state == nothing_seen)
7060 state = symbol_seen;
7067 *filename_ptr = filename;
7068 if (functionname_ptr)
7069 *functionname_ptr = bfd_asymbol_name (func);
7074 /* Find the nearest line to a particular section and offset,
7075 for error reporting. */
7078 _bfd_elf_find_nearest_line (bfd *abfd,
7082 const char **filename_ptr,
7083 const char **functionname_ptr,
7084 unsigned int *line_ptr)
7088 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7089 filename_ptr, functionname_ptr,
7092 if (!*functionname_ptr)
7093 elf_find_function (abfd, section, symbols, offset,
7094 *filename_ptr ? NULL : filename_ptr,
7100 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7101 filename_ptr, functionname_ptr,
7103 &elf_tdata (abfd)->dwarf2_find_line_info))
7105 if (!*functionname_ptr)
7106 elf_find_function (abfd, section, symbols, offset,
7107 *filename_ptr ? NULL : filename_ptr,
7113 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7114 &found, filename_ptr,
7115 functionname_ptr, line_ptr,
7116 &elf_tdata (abfd)->line_info))
7118 if (found && (*functionname_ptr || *line_ptr))
7121 if (symbols == NULL)
7124 if (! elf_find_function (abfd, section, symbols, offset,
7125 filename_ptr, functionname_ptr))
7132 /* Find the line for a symbol. */
7135 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7136 const char **filename_ptr, unsigned int *line_ptr)
7138 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7139 filename_ptr, line_ptr, 0,
7140 &elf_tdata (abfd)->dwarf2_find_line_info);
7143 /* After a call to bfd_find_nearest_line, successive calls to
7144 bfd_find_inliner_info can be used to get source information about
7145 each level of function inlining that terminated at the address
7146 passed to bfd_find_nearest_line. Currently this is only supported
7147 for DWARF2 with appropriate DWARF3 extensions. */
7150 _bfd_elf_find_inliner_info (bfd *abfd,
7151 const char **filename_ptr,
7152 const char **functionname_ptr,
7153 unsigned int *line_ptr)
7156 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7157 functionname_ptr, line_ptr,
7158 & elf_tdata (abfd)->dwarf2_find_line_info);
7163 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7165 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7166 int ret = bed->s->sizeof_ehdr;
7168 if (!info->relocatable)
7170 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7172 if (phdr_size == (bfd_size_type) -1)
7174 struct elf_segment_map *m;
7177 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7178 phdr_size += bed->s->sizeof_phdr;
7181 phdr_size = get_program_header_size (abfd, info);
7184 elf_tdata (abfd)->program_header_size = phdr_size;
7192 _bfd_elf_set_section_contents (bfd *abfd,
7194 const void *location,
7196 bfd_size_type count)
7198 Elf_Internal_Shdr *hdr;
7201 if (! abfd->output_has_begun
7202 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7205 hdr = &elf_section_data (section)->this_hdr;
7206 pos = hdr->sh_offset + offset;
7207 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7208 || bfd_bwrite (location, count, abfd) != count)
7215 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7216 arelent *cache_ptr ATTRIBUTE_UNUSED,
7217 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7222 /* Try to convert a non-ELF reloc into an ELF one. */
7225 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7227 /* Check whether we really have an ELF howto. */
7229 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7231 bfd_reloc_code_real_type code;
7232 reloc_howto_type *howto;
7234 /* Alien reloc: Try to determine its type to replace it with an
7235 equivalent ELF reloc. */
7237 if (areloc->howto->pc_relative)
7239 switch (areloc->howto->bitsize)
7242 code = BFD_RELOC_8_PCREL;
7245 code = BFD_RELOC_12_PCREL;
7248 code = BFD_RELOC_16_PCREL;
7251 code = BFD_RELOC_24_PCREL;
7254 code = BFD_RELOC_32_PCREL;
7257 code = BFD_RELOC_64_PCREL;
7263 howto = bfd_reloc_type_lookup (abfd, code);
7265 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7267 if (howto->pcrel_offset)
7268 areloc->addend += areloc->address;
7270 areloc->addend -= areloc->address; /* addend is unsigned!! */
7275 switch (areloc->howto->bitsize)
7281 code = BFD_RELOC_14;
7284 code = BFD_RELOC_16;
7287 code = BFD_RELOC_26;
7290 code = BFD_RELOC_32;
7293 code = BFD_RELOC_64;
7299 howto = bfd_reloc_type_lookup (abfd, code);
7303 areloc->howto = howto;
7311 (*_bfd_error_handler)
7312 (_("%B: unsupported relocation type %s"),
7313 abfd, areloc->howto->name);
7314 bfd_set_error (bfd_error_bad_value);
7319 _bfd_elf_close_and_cleanup (bfd *abfd)
7321 if (bfd_get_format (abfd) == bfd_object)
7323 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7324 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7325 _bfd_dwarf2_cleanup_debug_info (abfd);
7328 return _bfd_generic_close_and_cleanup (abfd);
7331 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7332 in the relocation's offset. Thus we cannot allow any sort of sanity
7333 range-checking to interfere. There is nothing else to do in processing
7336 bfd_reloc_status_type
7337 _bfd_elf_rel_vtable_reloc_fn
7338 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7339 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7340 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7341 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7343 return bfd_reloc_ok;
7346 /* Elf core file support. Much of this only works on native
7347 toolchains, since we rely on knowing the
7348 machine-dependent procfs structure in order to pick
7349 out details about the corefile. */
7351 #ifdef HAVE_SYS_PROCFS_H
7352 # include <sys/procfs.h>
7355 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7358 elfcore_make_pid (bfd *abfd)
7360 return ((elf_tdata (abfd)->core_lwpid << 16)
7361 + (elf_tdata (abfd)->core_pid));
7364 /* If there isn't a section called NAME, make one, using
7365 data from SECT. Note, this function will generate a
7366 reference to NAME, so you shouldn't deallocate or
7370 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7374 if (bfd_get_section_by_name (abfd, name) != NULL)
7377 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7381 sect2->size = sect->size;
7382 sect2->filepos = sect->filepos;
7383 sect2->alignment_power = sect->alignment_power;
7387 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7388 actually creates up to two pseudosections:
7389 - For the single-threaded case, a section named NAME, unless
7390 such a section already exists.
7391 - For the multi-threaded case, a section named "NAME/PID", where
7392 PID is elfcore_make_pid (abfd).
7393 Both pseudosections have identical contents. */
7395 _bfd_elfcore_make_pseudosection (bfd *abfd,
7401 char *threaded_name;
7405 /* Build the section name. */
7407 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7408 len = strlen (buf) + 1;
7409 threaded_name = bfd_alloc (abfd, len);
7410 if (threaded_name == NULL)
7412 memcpy (threaded_name, buf, len);
7414 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7419 sect->filepos = filepos;
7420 sect->alignment_power = 2;
7422 return elfcore_maybe_make_sect (abfd, name, sect);
7425 /* prstatus_t exists on:
7427 linux 2.[01] + glibc
7431 #if defined (HAVE_PRSTATUS_T)
7434 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7439 if (note->descsz == sizeof (prstatus_t))
7443 size = sizeof (prstat.pr_reg);
7444 offset = offsetof (prstatus_t, pr_reg);
7445 memcpy (&prstat, note->descdata, sizeof (prstat));
7447 /* Do not overwrite the core signal if it
7448 has already been set by another thread. */
7449 if (elf_tdata (abfd)->core_signal == 0)
7450 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7451 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7453 /* pr_who exists on:
7456 pr_who doesn't exist on:
7459 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7460 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7463 #if defined (HAVE_PRSTATUS32_T)
7464 else if (note->descsz == sizeof (prstatus32_t))
7466 /* 64-bit host, 32-bit corefile */
7467 prstatus32_t prstat;
7469 size = sizeof (prstat.pr_reg);
7470 offset = offsetof (prstatus32_t, pr_reg);
7471 memcpy (&prstat, note->descdata, sizeof (prstat));
7473 /* Do not overwrite the core signal if it
7474 has already been set by another thread. */
7475 if (elf_tdata (abfd)->core_signal == 0)
7476 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7477 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7479 /* pr_who exists on:
7482 pr_who doesn't exist on:
7485 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7486 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7489 #endif /* HAVE_PRSTATUS32_T */
7492 /* Fail - we don't know how to handle any other
7493 note size (ie. data object type). */
7497 /* Make a ".reg/999" section and a ".reg" section. */
7498 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7499 size, note->descpos + offset);
7501 #endif /* defined (HAVE_PRSTATUS_T) */
7503 /* Create a pseudosection containing the exact contents of NOTE. */
7505 elfcore_make_note_pseudosection (bfd *abfd,
7507 Elf_Internal_Note *note)
7509 return _bfd_elfcore_make_pseudosection (abfd, name,
7510 note->descsz, note->descpos);
7513 /* There isn't a consistent prfpregset_t across platforms,
7514 but it doesn't matter, because we don't have to pick this
7515 data structure apart. */
7518 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7520 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7523 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7524 type of NT_PRXFPREG. Just include the whole note's contents
7528 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7530 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7534 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7536 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7540 #if defined (HAVE_PRPSINFO_T)
7541 typedef prpsinfo_t elfcore_psinfo_t;
7542 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7543 typedef prpsinfo32_t elfcore_psinfo32_t;
7547 #if defined (HAVE_PSINFO_T)
7548 typedef psinfo_t elfcore_psinfo_t;
7549 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7550 typedef psinfo32_t elfcore_psinfo32_t;
7554 /* return a malloc'ed copy of a string at START which is at
7555 most MAX bytes long, possibly without a terminating '\0'.
7556 the copy will always have a terminating '\0'. */
7559 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7562 char *end = memchr (start, '\0', max);
7570 dups = bfd_alloc (abfd, len + 1);
7574 memcpy (dups, start, len);
7580 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7582 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7584 if (note->descsz == sizeof (elfcore_psinfo_t))
7586 elfcore_psinfo_t psinfo;
7588 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7590 elf_tdata (abfd)->core_program
7591 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7592 sizeof (psinfo.pr_fname));
7594 elf_tdata (abfd)->core_command
7595 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7596 sizeof (psinfo.pr_psargs));
7598 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7599 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7601 /* 64-bit host, 32-bit corefile */
7602 elfcore_psinfo32_t psinfo;
7604 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7606 elf_tdata (abfd)->core_program
7607 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7608 sizeof (psinfo.pr_fname));
7610 elf_tdata (abfd)->core_command
7611 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7612 sizeof (psinfo.pr_psargs));
7618 /* Fail - we don't know how to handle any other
7619 note size (ie. data object type). */
7623 /* Note that for some reason, a spurious space is tacked
7624 onto the end of the args in some (at least one anyway)
7625 implementations, so strip it off if it exists. */
7628 char *command = elf_tdata (abfd)->core_command;
7629 int n = strlen (command);
7631 if (0 < n && command[n - 1] == ' ')
7632 command[n - 1] = '\0';
7637 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7639 #if defined (HAVE_PSTATUS_T)
7641 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7643 if (note->descsz == sizeof (pstatus_t)
7644 #if defined (HAVE_PXSTATUS_T)
7645 || note->descsz == sizeof (pxstatus_t)
7651 memcpy (&pstat, note->descdata, sizeof (pstat));
7653 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7655 #if defined (HAVE_PSTATUS32_T)
7656 else if (note->descsz == sizeof (pstatus32_t))
7658 /* 64-bit host, 32-bit corefile */
7661 memcpy (&pstat, note->descdata, sizeof (pstat));
7663 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7666 /* Could grab some more details from the "representative"
7667 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7668 NT_LWPSTATUS note, presumably. */
7672 #endif /* defined (HAVE_PSTATUS_T) */
7674 #if defined (HAVE_LWPSTATUS_T)
7676 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7678 lwpstatus_t lwpstat;
7684 if (note->descsz != sizeof (lwpstat)
7685 #if defined (HAVE_LWPXSTATUS_T)
7686 && note->descsz != sizeof (lwpxstatus_t)
7691 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7693 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7694 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7696 /* Make a ".reg/999" section. */
7698 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7699 len = strlen (buf) + 1;
7700 name = bfd_alloc (abfd, len);
7703 memcpy (name, buf, len);
7705 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7709 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7710 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7711 sect->filepos = note->descpos
7712 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7715 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7716 sect->size = sizeof (lwpstat.pr_reg);
7717 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7720 sect->alignment_power = 2;
7722 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7725 /* Make a ".reg2/999" section */
7727 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7728 len = strlen (buf) + 1;
7729 name = bfd_alloc (abfd, len);
7732 memcpy (name, buf, len);
7734 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7738 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7739 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7740 sect->filepos = note->descpos
7741 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7744 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7745 sect->size = sizeof (lwpstat.pr_fpreg);
7746 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7749 sect->alignment_power = 2;
7751 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7753 #endif /* defined (HAVE_LWPSTATUS_T) */
7756 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7763 int is_active_thread;
7766 if (note->descsz < 728)
7769 if (! CONST_STRNEQ (note->namedata, "win32"))
7772 type = bfd_get_32 (abfd, note->descdata);
7776 case 1 /* NOTE_INFO_PROCESS */:
7777 /* FIXME: need to add ->core_command. */
7778 /* process_info.pid */
7779 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7780 /* process_info.signal */
7781 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7784 case 2 /* NOTE_INFO_THREAD */:
7785 /* Make a ".reg/999" section. */
7786 /* thread_info.tid */
7787 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7789 len = strlen (buf) + 1;
7790 name = bfd_alloc (abfd, len);
7794 memcpy (name, buf, len);
7796 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7800 /* sizeof (thread_info.thread_context) */
7802 /* offsetof (thread_info.thread_context) */
7803 sect->filepos = note->descpos + 12;
7804 sect->alignment_power = 2;
7806 /* thread_info.is_active_thread */
7807 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7809 if (is_active_thread)
7810 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7814 case 3 /* NOTE_INFO_MODULE */:
7815 /* Make a ".module/xxxxxxxx" section. */
7816 /* module_info.base_address */
7817 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7818 sprintf (buf, ".module/%08lx", (long) base_addr);
7820 len = strlen (buf) + 1;
7821 name = bfd_alloc (abfd, len);
7825 memcpy (name, buf, len);
7827 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7832 sect->size = note->descsz;
7833 sect->filepos = note->descpos;
7834 sect->alignment_power = 2;
7845 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7847 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7855 if (bed->elf_backend_grok_prstatus)
7856 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7858 #if defined (HAVE_PRSTATUS_T)
7859 return elfcore_grok_prstatus (abfd, note);
7864 #if defined (HAVE_PSTATUS_T)
7866 return elfcore_grok_pstatus (abfd, note);
7869 #if defined (HAVE_LWPSTATUS_T)
7871 return elfcore_grok_lwpstatus (abfd, note);
7874 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7875 return elfcore_grok_prfpreg (abfd, note);
7877 case NT_WIN32PSTATUS:
7878 return elfcore_grok_win32pstatus (abfd, note);
7880 case NT_PRXFPREG: /* Linux SSE extension */
7881 if (note->namesz == 6
7882 && strcmp (note->namedata, "LINUX") == 0)
7883 return elfcore_grok_prxfpreg (abfd, note);
7888 if (note->namesz == 6
7889 && strcmp (note->namedata, "LINUX") == 0)
7890 return elfcore_grok_ppc_vmx (abfd, note);
7896 if (bed->elf_backend_grok_psinfo)
7897 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7899 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7900 return elfcore_grok_psinfo (abfd, note);
7907 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7912 sect->size = note->descsz;
7913 sect->filepos = note->descpos;
7914 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7922 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7924 elf_tdata (abfd)->build_id_size = note->descsz;
7925 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7926 if (elf_tdata (abfd)->build_id == NULL)
7929 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7935 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
7942 case NT_GNU_BUILD_ID:
7943 return elfobj_grok_gnu_build_id (abfd, note);
7948 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7952 cp = strchr (note->namedata, '@');
7955 *lwpidp = atoi(cp + 1);
7962 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7964 /* Signal number at offset 0x08. */
7965 elf_tdata (abfd)->core_signal
7966 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7968 /* Process ID at offset 0x50. */
7969 elf_tdata (abfd)->core_pid
7970 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7972 /* Command name at 0x7c (max 32 bytes, including nul). */
7973 elf_tdata (abfd)->core_command
7974 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7976 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7981 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7985 if (elfcore_netbsd_get_lwpid (note, &lwp))
7986 elf_tdata (abfd)->core_lwpid = lwp;
7988 if (note->type == NT_NETBSDCORE_PROCINFO)
7990 /* NetBSD-specific core "procinfo". Note that we expect to
7991 find this note before any of the others, which is fine,
7992 since the kernel writes this note out first when it
7993 creates a core file. */
7995 return elfcore_grok_netbsd_procinfo (abfd, note);
7998 /* As of Jan 2002 there are no other machine-independent notes
7999 defined for NetBSD core files. If the note type is less
8000 than the start of the machine-dependent note types, we don't
8003 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8007 switch (bfd_get_arch (abfd))
8009 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8010 PT_GETFPREGS == mach+2. */
8012 case bfd_arch_alpha:
8013 case bfd_arch_sparc:
8016 case NT_NETBSDCORE_FIRSTMACH+0:
8017 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8019 case NT_NETBSDCORE_FIRSTMACH+2:
8020 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8026 /* On all other arch's, PT_GETREGS == mach+1 and
8027 PT_GETFPREGS == mach+3. */
8032 case NT_NETBSDCORE_FIRSTMACH+1:
8033 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8035 case NT_NETBSDCORE_FIRSTMACH+3:
8036 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8046 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8048 void *ddata = note->descdata;
8055 /* nto_procfs_status 'pid' field is at offset 0. */
8056 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8058 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8059 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8061 /* nto_procfs_status 'flags' field is at offset 8. */
8062 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8064 /* nto_procfs_status 'what' field is at offset 14. */
8065 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8067 elf_tdata (abfd)->core_signal = sig;
8068 elf_tdata (abfd)->core_lwpid = *tid;
8071 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8072 do not come from signals so we make sure we set the current
8073 thread just in case. */
8074 if (flags & 0x00000080)
8075 elf_tdata (abfd)->core_lwpid = *tid;
8077 /* Make a ".qnx_core_status/%d" section. */
8078 sprintf (buf, ".qnx_core_status/%ld", *tid);
8080 name = bfd_alloc (abfd, strlen (buf) + 1);
8085 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8089 sect->size = note->descsz;
8090 sect->filepos = note->descpos;
8091 sect->alignment_power = 2;
8093 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8097 elfcore_grok_nto_regs (bfd *abfd,
8098 Elf_Internal_Note *note,
8106 /* Make a "(base)/%d" section. */
8107 sprintf (buf, "%s/%ld", base, tid);
8109 name = bfd_alloc (abfd, strlen (buf) + 1);
8114 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8118 sect->size = note->descsz;
8119 sect->filepos = note->descpos;
8120 sect->alignment_power = 2;
8122 /* This is the current thread. */
8123 if (elf_tdata (abfd)->core_lwpid == tid)
8124 return elfcore_maybe_make_sect (abfd, base, sect);
8129 #define BFD_QNT_CORE_INFO 7
8130 #define BFD_QNT_CORE_STATUS 8
8131 #define BFD_QNT_CORE_GREG 9
8132 #define BFD_QNT_CORE_FPREG 10
8135 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8137 /* Every GREG section has a STATUS section before it. Store the
8138 tid from the previous call to pass down to the next gregs
8140 static long tid = 1;
8144 case BFD_QNT_CORE_INFO:
8145 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8146 case BFD_QNT_CORE_STATUS:
8147 return elfcore_grok_nto_status (abfd, note, &tid);
8148 case BFD_QNT_CORE_GREG:
8149 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8150 case BFD_QNT_CORE_FPREG:
8151 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8158 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8164 /* Use note name as section name. */
8166 name = bfd_alloc (abfd, len);
8169 memcpy (name, note->namedata, len);
8170 name[len - 1] = '\0';
8172 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8176 sect->size = note->descsz;
8177 sect->filepos = note->descpos;
8178 sect->alignment_power = 1;
8183 /* Function: elfcore_write_note
8186 buffer to hold note, and current size of buffer
8190 size of data for note
8192 Writes note to end of buffer. ELF64 notes are written exactly as
8193 for ELF32, despite the current (as of 2006) ELF gabi specifying
8194 that they ought to have 8-byte namesz and descsz field, and have
8195 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8198 Pointer to realloc'd buffer, *BUFSIZ updated. */
8201 elfcore_write_note (bfd *abfd,
8209 Elf_External_Note *xnp;
8216 namesz = strlen (name) + 1;
8218 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8220 buf = realloc (buf, *bufsiz + newspace);
8223 dest = buf + *bufsiz;
8224 *bufsiz += newspace;
8225 xnp = (Elf_External_Note *) dest;
8226 H_PUT_32 (abfd, namesz, xnp->namesz);
8227 H_PUT_32 (abfd, size, xnp->descsz);
8228 H_PUT_32 (abfd, type, xnp->type);
8232 memcpy (dest, name, namesz);
8240 memcpy (dest, input, size);
8250 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8252 elfcore_write_prpsinfo (bfd *abfd,
8258 const char *note_name = "CORE";
8259 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8261 if (bed->elf_backend_write_core_note != NULL)
8264 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8265 NT_PRPSINFO, fname, psargs);
8270 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8271 if (bed->s->elfclass == ELFCLASS32)
8273 #if defined (HAVE_PSINFO32_T)
8275 int note_type = NT_PSINFO;
8278 int note_type = NT_PRPSINFO;
8281 memset (&data, 0, sizeof (data));
8282 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8283 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8284 return elfcore_write_note (abfd, buf, bufsiz,
8285 note_name, note_type, &data, sizeof (data));
8290 #if defined (HAVE_PSINFO_T)
8292 int note_type = NT_PSINFO;
8295 int note_type = NT_PRPSINFO;
8298 memset (&data, 0, sizeof (data));
8299 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8300 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8301 return elfcore_write_note (abfd, buf, bufsiz,
8302 note_name, note_type, &data, sizeof (data));
8305 #endif /* PSINFO_T or PRPSINFO_T */
8307 #if defined (HAVE_PRSTATUS_T)
8309 elfcore_write_prstatus (bfd *abfd,
8316 const char *note_name = "CORE";
8317 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8319 if (bed->elf_backend_write_core_note != NULL)
8322 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8324 pid, cursig, gregs);
8329 #if defined (HAVE_PRSTATUS32_T)
8330 if (bed->s->elfclass == ELFCLASS32)
8332 prstatus32_t prstat;
8334 memset (&prstat, 0, sizeof (prstat));
8335 prstat.pr_pid = pid;
8336 prstat.pr_cursig = cursig;
8337 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8338 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8339 NT_PRSTATUS, &prstat, sizeof (prstat));
8346 memset (&prstat, 0, sizeof (prstat));
8347 prstat.pr_pid = pid;
8348 prstat.pr_cursig = cursig;
8349 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8350 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8351 NT_PRSTATUS, &prstat, sizeof (prstat));
8354 #endif /* HAVE_PRSTATUS_T */
8356 #if defined (HAVE_LWPSTATUS_T)
8358 elfcore_write_lwpstatus (bfd *abfd,
8365 lwpstatus_t lwpstat;
8366 const char *note_name = "CORE";
8368 memset (&lwpstat, 0, sizeof (lwpstat));
8369 lwpstat.pr_lwpid = pid >> 16;
8370 lwpstat.pr_cursig = cursig;
8371 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8372 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8373 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8375 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8376 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8378 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8379 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8382 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8383 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8385 #endif /* HAVE_LWPSTATUS_T */
8387 #if defined (HAVE_PSTATUS_T)
8389 elfcore_write_pstatus (bfd *abfd,
8393 int cursig ATTRIBUTE_UNUSED,
8394 const void *gregs ATTRIBUTE_UNUSED)
8396 const char *note_name = "CORE";
8397 #if defined (HAVE_PSTATUS32_T)
8398 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8400 if (bed->s->elfclass == ELFCLASS32)
8404 memset (&pstat, 0, sizeof (pstat));
8405 pstat.pr_pid = pid & 0xffff;
8406 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8407 NT_PSTATUS, &pstat, sizeof (pstat));
8415 memset (&pstat, 0, sizeof (pstat));
8416 pstat.pr_pid = pid & 0xffff;
8417 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8418 NT_PSTATUS, &pstat, sizeof (pstat));
8422 #endif /* HAVE_PSTATUS_T */
8425 elfcore_write_prfpreg (bfd *abfd,
8431 const char *note_name = "CORE";
8432 return elfcore_write_note (abfd, buf, bufsiz,
8433 note_name, NT_FPREGSET, fpregs, size);
8437 elfcore_write_prxfpreg (bfd *abfd,
8440 const void *xfpregs,
8443 char *note_name = "LINUX";
8444 return elfcore_write_note (abfd, buf, bufsiz,
8445 note_name, NT_PRXFPREG, xfpregs, size);
8449 elfcore_write_ppc_vmx (bfd *abfd,
8452 const void *ppc_vmx,
8455 char *note_name = "LINUX";
8456 return elfcore_write_note (abfd, buf, bufsiz,
8457 note_name, NT_PPC_VMX, ppc_vmx, size);
8461 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8466 while (p < buf + size)
8468 /* FIXME: bad alignment assumption. */
8469 Elf_External_Note *xnp = (Elf_External_Note *) p;
8470 Elf_Internal_Note in;
8472 in.type = H_GET_32 (abfd, xnp->type);
8474 in.namesz = H_GET_32 (abfd, xnp->namesz);
8475 in.namedata = xnp->name;
8477 in.descsz = H_GET_32 (abfd, xnp->descsz);
8478 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8479 in.descpos = offset + (in.descdata - buf);
8481 switch (bfd_get_format (abfd))
8487 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8489 if (! elfcore_grok_netbsd_note (abfd, &in))
8492 else if (CONST_STRNEQ (in.namedata, "QNX"))
8494 if (! elfcore_grok_nto_note (abfd, &in))
8497 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8499 if (! elfcore_grok_spu_note (abfd, &in))
8504 if (! elfcore_grok_note (abfd, &in))
8510 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8512 if (! elfobj_grok_gnu_note (abfd, &in))
8518 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8525 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8532 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8535 buf = bfd_malloc (size);
8539 if (bfd_bread (buf, size, abfd) != size
8540 || !elf_parse_notes (abfd, buf, size, offset))
8550 /* Providing external access to the ELF program header table. */
8552 /* Return an upper bound on the number of bytes required to store a
8553 copy of ABFD's program header table entries. Return -1 if an error
8554 occurs; bfd_get_error will return an appropriate code. */
8557 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8559 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8561 bfd_set_error (bfd_error_wrong_format);
8565 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8568 /* Copy ABFD's program header table entries to *PHDRS. The entries
8569 will be stored as an array of Elf_Internal_Phdr structures, as
8570 defined in include/elf/internal.h. To find out how large the
8571 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8573 Return the number of program header table entries read, or -1 if an
8574 error occurs; bfd_get_error will return an appropriate code. */
8577 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8581 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8583 bfd_set_error (bfd_error_wrong_format);
8587 num_phdrs = elf_elfheader (abfd)->e_phnum;
8588 memcpy (phdrs, elf_tdata (abfd)->phdr,
8589 num_phdrs * sizeof (Elf_Internal_Phdr));
8594 enum elf_reloc_type_class
8595 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8597 return reloc_class_normal;
8600 /* For RELA architectures, return the relocation value for a
8601 relocation against a local symbol. */
8604 _bfd_elf_rela_local_sym (bfd *abfd,
8605 Elf_Internal_Sym *sym,
8607 Elf_Internal_Rela *rel)
8609 asection *sec = *psec;
8612 relocation = (sec->output_section->vma
8613 + sec->output_offset
8615 if ((sec->flags & SEC_MERGE)
8616 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8617 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8620 _bfd_merged_section_offset (abfd, psec,
8621 elf_section_data (sec)->sec_info,
8622 sym->st_value + rel->r_addend);
8625 /* If we have changed the section, and our original section is
8626 marked with SEC_EXCLUDE, it means that the original
8627 SEC_MERGE section has been completely subsumed in some
8628 other SEC_MERGE section. In this case, we need to leave
8629 some info around for --emit-relocs. */
8630 if ((sec->flags & SEC_EXCLUDE) != 0)
8631 sec->kept_section = *psec;
8634 rel->r_addend -= relocation;
8635 rel->r_addend += sec->output_section->vma + sec->output_offset;
8641 _bfd_elf_rel_local_sym (bfd *abfd,
8642 Elf_Internal_Sym *sym,
8646 asection *sec = *psec;
8648 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8649 return sym->st_value + addend;
8651 return _bfd_merged_section_offset (abfd, psec,
8652 elf_section_data (sec)->sec_info,
8653 sym->st_value + addend);
8657 _bfd_elf_section_offset (bfd *abfd,
8658 struct bfd_link_info *info,
8662 switch (sec->sec_info_type)
8664 case ELF_INFO_TYPE_STABS:
8665 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8667 case ELF_INFO_TYPE_EH_FRAME:
8668 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8674 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8675 reconstruct an ELF file by reading the segments out of remote memory
8676 based on the ELF file header at EHDR_VMA and the ELF program headers it
8677 points to. If not null, *LOADBASEP is filled in with the difference
8678 between the VMAs from which the segments were read, and the VMAs the
8679 file headers (and hence BFD's idea of each section's VMA) put them at.
8681 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8682 remote memory at target address VMA into the local buffer at MYADDR; it
8683 should return zero on success or an `errno' code on failure. TEMPL must
8684 be a BFD for an ELF target with the word size and byte order found in
8685 the remote memory. */
8688 bfd_elf_bfd_from_remote_memory
8692 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8694 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8695 (templ, ehdr_vma, loadbasep, target_read_memory);
8699 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8700 long symcount ATTRIBUTE_UNUSED,
8701 asymbol **syms ATTRIBUTE_UNUSED,
8706 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8709 const char *relplt_name;
8710 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8714 Elf_Internal_Shdr *hdr;
8720 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8723 if (dynsymcount <= 0)
8726 if (!bed->plt_sym_val)
8729 relplt_name = bed->relplt_name;
8730 if (relplt_name == NULL)
8731 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8732 relplt = bfd_get_section_by_name (abfd, relplt_name);
8736 hdr = &elf_section_data (relplt)->this_hdr;
8737 if (hdr->sh_link != elf_dynsymtab (abfd)
8738 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8741 plt = bfd_get_section_by_name (abfd, ".plt");
8745 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8746 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8749 count = relplt->size / hdr->sh_entsize;
8750 size = count * sizeof (asymbol);
8751 p = relplt->relocation;
8752 for (i = 0; i < count; i++, p++)
8753 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8755 s = *ret = bfd_malloc (size);
8759 names = (char *) (s + count);
8760 p = relplt->relocation;
8762 for (i = 0; i < count; i++, p++)
8767 addr = bed->plt_sym_val (i, plt, p);
8768 if (addr == (bfd_vma) -1)
8771 *s = **p->sym_ptr_ptr;
8772 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8773 we are defining a symbol, ensure one of them is set. */
8774 if ((s->flags & BSF_LOCAL) == 0)
8775 s->flags |= BSF_GLOBAL;
8777 s->value = addr - plt->vma;
8780 len = strlen ((*p->sym_ptr_ptr)->name);
8781 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8783 memcpy (names, "@plt", sizeof ("@plt"));
8784 names += sizeof ("@plt");
8791 /* It is only used by x86-64 so far. */
8792 asection _bfd_elf_large_com_section
8793 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8794 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8797 _bfd_elf_set_osabi (bfd * abfd,
8798 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8800 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8802 i_ehdrp = elf_elfheader (abfd);
8804 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8808 /* Return TRUE for ELF symbol types that represent functions.
8809 This is the default version of this function, which is sufficient for
8810 most targets. It returns true if TYPE is STT_FUNC. */
8813 _bfd_elf_is_function_type (unsigned int type)
8815 return (type == STT_FUNC);