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;
1207 const char *name = "";
1209 bfd_boolean stringp;
1210 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1212 (*swap_dyn_in) (abfd, extdyn, &dyn);
1214 if (dyn.d_tag == DT_NULL)
1221 if (bed->elf_backend_get_target_dtag)
1222 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1224 if (!strcmp (name, ""))
1226 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1231 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1232 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1233 case DT_PLTGOT: name = "PLTGOT"; break;
1234 case DT_HASH: name = "HASH"; break;
1235 case DT_STRTAB: name = "STRTAB"; break;
1236 case DT_SYMTAB: name = "SYMTAB"; break;
1237 case DT_RELA: name = "RELA"; break;
1238 case DT_RELASZ: name = "RELASZ"; break;
1239 case DT_RELAENT: name = "RELAENT"; break;
1240 case DT_STRSZ: name = "STRSZ"; break;
1241 case DT_SYMENT: name = "SYMENT"; break;
1242 case DT_INIT: name = "INIT"; break;
1243 case DT_FINI: name = "FINI"; break;
1244 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1245 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1246 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1247 case DT_REL: name = "REL"; break;
1248 case DT_RELSZ: name = "RELSZ"; break;
1249 case DT_RELENT: name = "RELENT"; break;
1250 case DT_PLTREL: name = "PLTREL"; break;
1251 case DT_DEBUG: name = "DEBUG"; break;
1252 case DT_TEXTREL: name = "TEXTREL"; break;
1253 case DT_JMPREL: name = "JMPREL"; break;
1254 case DT_BIND_NOW: name = "BIND_NOW"; break;
1255 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1256 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1257 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1258 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1259 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1260 case DT_FLAGS: name = "FLAGS"; break;
1261 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1262 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1263 case DT_CHECKSUM: name = "CHECKSUM"; break;
1264 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1265 case DT_MOVEENT: name = "MOVEENT"; break;
1266 case DT_MOVESZ: name = "MOVESZ"; break;
1267 case DT_FEATURE: name = "FEATURE"; break;
1268 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1269 case DT_SYMINSZ: name = "SYMINSZ"; break;
1270 case DT_SYMINENT: name = "SYMINENT"; break;
1271 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1272 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1273 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1274 case DT_PLTPAD: name = "PLTPAD"; break;
1275 case DT_MOVETAB: name = "MOVETAB"; break;
1276 case DT_SYMINFO: name = "SYMINFO"; break;
1277 case DT_RELACOUNT: name = "RELACOUNT"; break;
1278 case DT_RELCOUNT: name = "RELCOUNT"; break;
1279 case DT_FLAGS_1: name = "FLAGS_1"; break;
1280 case DT_VERSYM: name = "VERSYM"; break;
1281 case DT_VERDEF: name = "VERDEF"; break;
1282 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1283 case DT_VERNEED: name = "VERNEED"; break;
1284 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1285 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1286 case DT_USED: name = "USED"; break;
1287 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1288 case DT_GNU_HASH: name = "GNU_HASH"; break;
1291 fprintf (f, " %-20s ", name);
1293 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1297 unsigned int tagv = dyn.d_un.d_val;
1299 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1302 fprintf (f, "%s", string);
1311 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1312 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1314 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1318 if (elf_dynverdef (abfd) != 0)
1320 Elf_Internal_Verdef *t;
1322 fprintf (f, _("\nVersion definitions:\n"));
1323 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1325 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1326 t->vd_flags, t->vd_hash,
1327 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1328 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1330 Elf_Internal_Verdaux *a;
1333 for (a = t->vd_auxptr->vda_nextptr;
1337 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1343 if (elf_dynverref (abfd) != 0)
1345 Elf_Internal_Verneed *t;
1347 fprintf (f, _("\nVersion References:\n"));
1348 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1350 Elf_Internal_Vernaux *a;
1352 fprintf (f, _(" required from %s:\n"),
1353 t->vn_filename ? t->vn_filename : "<corrupt>");
1354 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1355 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1356 a->vna_flags, a->vna_other,
1357 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1369 /* Display ELF-specific fields of a symbol. */
1372 bfd_elf_print_symbol (bfd *abfd,
1375 bfd_print_symbol_type how)
1380 case bfd_print_symbol_name:
1381 fprintf (file, "%s", symbol->name);
1383 case bfd_print_symbol_more:
1384 fprintf (file, "elf ");
1385 bfd_fprintf_vma (abfd, file, symbol->value);
1386 fprintf (file, " %lx", (long) symbol->flags);
1388 case bfd_print_symbol_all:
1390 const char *section_name;
1391 const char *name = NULL;
1392 const struct elf_backend_data *bed;
1393 unsigned char st_other;
1396 section_name = symbol->section ? symbol->section->name : "(*none*)";
1398 bed = get_elf_backend_data (abfd);
1399 if (bed->elf_backend_print_symbol_all)
1400 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1404 name = symbol->name;
1405 bfd_print_symbol_vandf (abfd, file, symbol);
1408 fprintf (file, " %s\t", section_name);
1409 /* Print the "other" value for a symbol. For common symbols,
1410 we've already printed the size; now print the alignment.
1411 For other symbols, we have no specified alignment, and
1412 we've printed the address; now print the size. */
1413 if (symbol->section && bfd_is_com_section (symbol->section))
1414 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1416 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1417 bfd_fprintf_vma (abfd, file, val);
1419 /* If we have version information, print it. */
1420 if (elf_tdata (abfd)->dynversym_section != 0
1421 && (elf_tdata (abfd)->dynverdef_section != 0
1422 || elf_tdata (abfd)->dynverref_section != 0))
1424 unsigned int vernum;
1425 const char *version_string;
1427 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1430 version_string = "";
1431 else if (vernum == 1)
1432 version_string = "Base";
1433 else if (vernum <= elf_tdata (abfd)->cverdefs)
1435 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1438 Elf_Internal_Verneed *t;
1440 version_string = "";
1441 for (t = elf_tdata (abfd)->verref;
1445 Elf_Internal_Vernaux *a;
1447 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1449 if (a->vna_other == vernum)
1451 version_string = a->vna_nodename;
1458 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1459 fprintf (file, " %-11s", version_string);
1464 fprintf (file, " (%s)", version_string);
1465 for (i = 10 - strlen (version_string); i > 0; --i)
1470 /* If the st_other field is not zero, print it. */
1471 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1476 case STV_INTERNAL: fprintf (file, " .internal"); break;
1477 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1478 case STV_PROTECTED: fprintf (file, " .protected"); break;
1480 /* Some other non-defined flags are also present, so print
1482 fprintf (file, " 0x%02x", (unsigned int) st_other);
1485 fprintf (file, " %s", name);
1491 /* Allocate an ELF string table--force the first byte to be zero. */
1493 struct bfd_strtab_hash *
1494 _bfd_elf_stringtab_init (void)
1496 struct bfd_strtab_hash *ret;
1498 ret = _bfd_stringtab_init ();
1503 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1504 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1505 if (loc == (bfd_size_type) -1)
1507 _bfd_stringtab_free (ret);
1514 /* ELF .o/exec file reading */
1516 /* Create a new bfd section from an ELF section header. */
1519 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1521 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1522 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1523 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1526 name = bfd_elf_string_from_elf_section (abfd,
1527 elf_elfheader (abfd)->e_shstrndx,
1532 switch (hdr->sh_type)
1535 /* Inactive section. Throw it away. */
1538 case SHT_PROGBITS: /* Normal section with contents. */
1539 case SHT_NOBITS: /* .bss section. */
1540 case SHT_HASH: /* .hash section. */
1541 case SHT_NOTE: /* .note section. */
1542 case SHT_INIT_ARRAY: /* .init_array section. */
1543 case SHT_FINI_ARRAY: /* .fini_array section. */
1544 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1545 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1546 case SHT_GNU_HASH: /* .gnu.hash section. */
1547 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1549 case SHT_DYNAMIC: /* Dynamic linking information. */
1550 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1552 if (hdr->sh_link > elf_numsections (abfd)
1553 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1555 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1557 Elf_Internal_Shdr *dynsymhdr;
1559 /* The shared libraries distributed with hpux11 have a bogus
1560 sh_link field for the ".dynamic" section. Find the
1561 string table for the ".dynsym" section instead. */
1562 if (elf_dynsymtab (abfd) != 0)
1564 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1565 hdr->sh_link = dynsymhdr->sh_link;
1569 unsigned int i, num_sec;
1571 num_sec = elf_numsections (abfd);
1572 for (i = 1; i < num_sec; i++)
1574 dynsymhdr = elf_elfsections (abfd)[i];
1575 if (dynsymhdr->sh_type == SHT_DYNSYM)
1577 hdr->sh_link = dynsymhdr->sh_link;
1585 case SHT_SYMTAB: /* A symbol table */
1586 if (elf_onesymtab (abfd) == shindex)
1589 if (hdr->sh_entsize != bed->s->sizeof_sym)
1591 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1592 elf_onesymtab (abfd) = shindex;
1593 elf_tdata (abfd)->symtab_hdr = *hdr;
1594 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1595 abfd->flags |= HAS_SYMS;
1597 /* Sometimes a shared object will map in the symbol table. If
1598 SHF_ALLOC is set, and this is a shared object, then we also
1599 treat this section as a BFD section. We can not base the
1600 decision purely on SHF_ALLOC, because that flag is sometimes
1601 set in a relocatable object file, which would confuse the
1603 if ((hdr->sh_flags & SHF_ALLOC) != 0
1604 && (abfd->flags & DYNAMIC) != 0
1605 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1609 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1610 can't read symbols without that section loaded as well. It
1611 is most likely specified by the next section header. */
1612 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1614 unsigned int i, num_sec;
1616 num_sec = elf_numsections (abfd);
1617 for (i = shindex + 1; i < num_sec; i++)
1619 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1620 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1621 && hdr2->sh_link == shindex)
1625 for (i = 1; i < shindex; i++)
1627 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1628 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1629 && hdr2->sh_link == shindex)
1633 return bfd_section_from_shdr (abfd, i);
1637 case SHT_DYNSYM: /* A dynamic symbol table */
1638 if (elf_dynsymtab (abfd) == shindex)
1641 if (hdr->sh_entsize != bed->s->sizeof_sym)
1643 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1644 elf_dynsymtab (abfd) = shindex;
1645 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1646 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1647 abfd->flags |= HAS_SYMS;
1649 /* Besides being a symbol table, we also treat this as a regular
1650 section, so that objcopy can handle it. */
1651 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1653 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1654 if (elf_symtab_shndx (abfd) == shindex)
1657 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1658 elf_symtab_shndx (abfd) = shindex;
1659 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1660 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1663 case SHT_STRTAB: /* A string table */
1664 if (hdr->bfd_section != NULL)
1666 if (ehdr->e_shstrndx == shindex)
1668 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1669 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1672 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1675 elf_tdata (abfd)->strtab_hdr = *hdr;
1676 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1679 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1682 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1683 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1684 elf_elfsections (abfd)[shindex] = hdr;
1685 /* We also treat this as a regular section, so that objcopy
1687 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1691 /* If the string table isn't one of the above, then treat it as a
1692 regular section. We need to scan all the headers to be sure,
1693 just in case this strtab section appeared before the above. */
1694 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1696 unsigned int i, num_sec;
1698 num_sec = elf_numsections (abfd);
1699 for (i = 1; i < num_sec; i++)
1701 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1702 if (hdr2->sh_link == shindex)
1704 /* Prevent endless recursion on broken objects. */
1707 if (! bfd_section_from_shdr (abfd, i))
1709 if (elf_onesymtab (abfd) == i)
1711 if (elf_dynsymtab (abfd) == i)
1712 goto dynsymtab_strtab;
1716 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1720 /* *These* do a lot of work -- but build no sections! */
1722 asection *target_sect;
1723 Elf_Internal_Shdr *hdr2;
1724 unsigned int num_sec = elf_numsections (abfd);
1727 != (bfd_size_type) (hdr->sh_type == SHT_REL
1728 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1731 /* Check for a bogus link to avoid crashing. */
1732 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1733 || hdr->sh_link >= num_sec)
1735 ((*_bfd_error_handler)
1736 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1737 abfd, hdr->sh_link, name, shindex));
1738 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1742 /* For some incomprehensible reason Oracle distributes
1743 libraries for Solaris in which some of the objects have
1744 bogus sh_link fields. It would be nice if we could just
1745 reject them, but, unfortunately, some people need to use
1746 them. We scan through the section headers; if we find only
1747 one suitable symbol table, we clobber the sh_link to point
1748 to it. I hope this doesn't break anything. */
1749 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1750 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1756 for (scan = 1; scan < num_sec; scan++)
1758 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1759 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1770 hdr->sh_link = found;
1773 /* Get the symbol table. */
1774 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1775 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1776 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1779 /* If this reloc section does not use the main symbol table we
1780 don't treat it as a reloc section. BFD can't adequately
1781 represent such a section, so at least for now, we don't
1782 try. We just present it as a normal section. We also
1783 can't use it as a reloc section if it points to the null
1784 section, an invalid section, or another reloc section. */
1785 if (hdr->sh_link != elf_onesymtab (abfd)
1786 || hdr->sh_info == SHN_UNDEF
1787 || (hdr->sh_info >= SHN_LORESERVE && hdr->sh_info <= SHN_HIRESERVE)
1788 || hdr->sh_info >= num_sec
1789 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1790 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1791 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1794 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1796 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1797 if (target_sect == NULL)
1800 if ((target_sect->flags & SEC_RELOC) == 0
1801 || target_sect->reloc_count == 0)
1802 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1806 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1807 amt = sizeof (*hdr2);
1808 hdr2 = bfd_alloc (abfd, amt);
1811 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1814 elf_elfsections (abfd)[shindex] = hdr2;
1815 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1816 target_sect->flags |= SEC_RELOC;
1817 target_sect->relocation = NULL;
1818 target_sect->rel_filepos = hdr->sh_offset;
1819 /* In the section to which the relocations apply, mark whether
1820 its relocations are of the REL or RELA variety. */
1821 if (hdr->sh_size != 0)
1822 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1823 abfd->flags |= HAS_RELOC;
1827 case SHT_GNU_verdef:
1828 elf_dynverdef (abfd) = shindex;
1829 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1830 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1832 case SHT_GNU_versym:
1833 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1835 elf_dynversym (abfd) = shindex;
1836 elf_tdata (abfd)->dynversym_hdr = *hdr;
1837 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1839 case SHT_GNU_verneed:
1840 elf_dynverref (abfd) = shindex;
1841 elf_tdata (abfd)->dynverref_hdr = *hdr;
1842 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1848 /* We need a BFD section for objcopy and relocatable linking,
1849 and it's handy to have the signature available as the section
1851 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1853 name = group_signature (abfd, hdr);
1856 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1858 if (hdr->contents != NULL)
1860 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1861 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1864 if (idx->flags & GRP_COMDAT)
1865 hdr->bfd_section->flags
1866 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1868 /* We try to keep the same section order as it comes in. */
1870 while (--n_elt != 0)
1874 if (idx->shdr != NULL
1875 && (s = idx->shdr->bfd_section) != NULL
1876 && elf_next_in_group (s) != NULL)
1878 elf_next_in_group (hdr->bfd_section) = s;
1886 /* Possibly an attributes section. */
1887 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1888 || hdr->sh_type == bed->obj_attrs_section_type)
1890 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1892 _bfd_elf_parse_attributes (abfd, hdr);
1896 /* Check for any processor-specific section types. */
1897 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1900 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1902 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1903 /* FIXME: How to properly handle allocated section reserved
1904 for applications? */
1905 (*_bfd_error_handler)
1906 (_("%B: don't know how to handle allocated, application "
1907 "specific section `%s' [0x%8x]"),
1908 abfd, name, hdr->sh_type);
1910 /* Allow sections reserved for applications. */
1911 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1914 else if (hdr->sh_type >= SHT_LOPROC
1915 && hdr->sh_type <= SHT_HIPROC)
1916 /* FIXME: We should handle this section. */
1917 (*_bfd_error_handler)
1918 (_("%B: don't know how to handle processor specific section "
1920 abfd, name, hdr->sh_type);
1921 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1923 /* Unrecognised OS-specific sections. */
1924 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1925 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1926 required to correctly process the section and the file should
1927 be rejected with an error message. */
1928 (*_bfd_error_handler)
1929 (_("%B: don't know how to handle OS specific section "
1931 abfd, name, hdr->sh_type);
1933 /* Otherwise it should be processed. */
1934 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1937 /* FIXME: We should handle this section. */
1938 (*_bfd_error_handler)
1939 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1940 abfd, name, hdr->sh_type);
1948 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1949 Return SEC for sections that have no elf section, and NULL on error. */
1952 bfd_section_from_r_symndx (bfd *abfd,
1953 struct sym_sec_cache *cache,
1955 unsigned long r_symndx)
1957 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1960 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1962 Elf_Internal_Shdr *symtab_hdr;
1963 unsigned char esym[sizeof (Elf64_External_Sym)];
1964 Elf_External_Sym_Shndx eshndx;
1965 Elf_Internal_Sym isym;
1967 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1968 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1969 &isym, esym, &eshndx) == NULL)
1972 if (cache->abfd != abfd)
1974 memset (cache->indx, -1, sizeof (cache->indx));
1977 cache->indx[ent] = r_symndx;
1978 cache->shndx[ent] = isym.st_shndx;
1981 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
1988 /* Given an ELF section number, retrieve the corresponding BFD
1992 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
1994 if (index >= elf_numsections (abfd))
1996 return elf_elfsections (abfd)[index]->bfd_section;
1999 static const struct bfd_elf_special_section special_sections_b[] =
2001 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2002 { NULL, 0, 0, 0, 0 }
2005 static const struct bfd_elf_special_section special_sections_c[] =
2007 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2008 { NULL, 0, 0, 0, 0 }
2011 static const struct bfd_elf_special_section special_sections_d[] =
2013 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2014 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2015 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2016 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2017 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2018 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2019 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2020 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2021 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2022 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2023 { NULL, 0, 0, 0, 0 }
2026 static const struct bfd_elf_special_section special_sections_f[] =
2028 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2029 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2030 { NULL, 0, 0, 0, 0 }
2033 static const struct bfd_elf_special_section special_sections_g[] =
2035 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2036 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2037 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2038 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2039 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2040 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2041 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2042 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2043 { NULL, 0, 0, 0, 0 }
2046 static const struct bfd_elf_special_section special_sections_h[] =
2048 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2049 { NULL, 0, 0, 0, 0 }
2052 static const struct bfd_elf_special_section special_sections_i[] =
2054 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2055 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2056 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2057 { NULL, 0, 0, 0, 0 }
2060 static const struct bfd_elf_special_section special_sections_l[] =
2062 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2063 { NULL, 0, 0, 0, 0 }
2066 static const struct bfd_elf_special_section special_sections_n[] =
2068 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2069 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2070 { NULL, 0, 0, 0, 0 }
2073 static const struct bfd_elf_special_section special_sections_p[] =
2075 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2076 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2077 { NULL, 0, 0, 0, 0 }
2080 static const struct bfd_elf_special_section special_sections_r[] =
2082 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2083 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2084 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2085 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2086 { NULL, 0, 0, 0, 0 }
2089 static const struct bfd_elf_special_section special_sections_s[] =
2091 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2092 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2093 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2094 /* See struct bfd_elf_special_section declaration for the semantics of
2095 this special case where .prefix_length != strlen (.prefix). */
2096 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2097 { NULL, 0, 0, 0, 0 }
2100 static const struct bfd_elf_special_section special_sections_t[] =
2102 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2103 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2104 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2105 { NULL, 0, 0, 0, 0 }
2108 static const struct bfd_elf_special_section *special_sections[] =
2110 special_sections_b, /* 'b' */
2111 special_sections_c, /* 'c' */
2112 special_sections_d, /* 'd' */
2114 special_sections_f, /* 'f' */
2115 special_sections_g, /* 'g' */
2116 special_sections_h, /* 'h' */
2117 special_sections_i, /* 'i' */
2120 special_sections_l, /* 'l' */
2122 special_sections_n, /* 'n' */
2124 special_sections_p, /* 'p' */
2126 special_sections_r, /* 'r' */
2127 special_sections_s, /* 's' */
2128 special_sections_t, /* 't' */
2131 const struct bfd_elf_special_section *
2132 _bfd_elf_get_special_section (const char *name,
2133 const struct bfd_elf_special_section *spec,
2139 len = strlen (name);
2141 for (i = 0; spec[i].prefix != NULL; i++)
2144 int prefix_len = spec[i].prefix_length;
2146 if (len < prefix_len)
2148 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2151 suffix_len = spec[i].suffix_length;
2152 if (suffix_len <= 0)
2154 if (name[prefix_len] != 0)
2156 if (suffix_len == 0)
2158 if (name[prefix_len] != '.'
2159 && (suffix_len == -2
2160 || (rela && spec[i].type == SHT_REL)))
2166 if (len < prefix_len + suffix_len)
2168 if (memcmp (name + len - suffix_len,
2169 spec[i].prefix + prefix_len,
2179 const struct bfd_elf_special_section *
2180 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2183 const struct bfd_elf_special_section *spec;
2184 const struct elf_backend_data *bed;
2186 /* See if this is one of the special sections. */
2187 if (sec->name == NULL)
2190 bed = get_elf_backend_data (abfd);
2191 spec = bed->special_sections;
2194 spec = _bfd_elf_get_special_section (sec->name,
2195 bed->special_sections,
2201 if (sec->name[0] != '.')
2204 i = sec->name[1] - 'b';
2205 if (i < 0 || i > 't' - 'b')
2208 spec = special_sections[i];
2213 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2217 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2219 struct bfd_elf_section_data *sdata;
2220 const struct elf_backend_data *bed;
2221 const struct bfd_elf_special_section *ssect;
2223 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2226 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2229 sec->used_by_bfd = sdata;
2232 /* Indicate whether or not this section should use RELA relocations. */
2233 bed = get_elf_backend_data (abfd);
2234 sec->use_rela_p = bed->default_use_rela_p;
2236 /* When we read a file, we don't need to set ELF section type and
2237 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2238 anyway. We will set ELF section type and flags for all linker
2239 created sections. If user specifies BFD section flags, we will
2240 set ELF section type and flags based on BFD section flags in
2241 elf_fake_sections. */
2242 if ((!sec->flags && abfd->direction != read_direction)
2243 || (sec->flags & SEC_LINKER_CREATED) != 0)
2245 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2248 elf_section_type (sec) = ssect->type;
2249 elf_section_flags (sec) = ssect->attr;
2253 return _bfd_generic_new_section_hook (abfd, sec);
2256 /* Create a new bfd section from an ELF program header.
2258 Since program segments have no names, we generate a synthetic name
2259 of the form segment<NUM>, where NUM is generally the index in the
2260 program header table. For segments that are split (see below) we
2261 generate the names segment<NUM>a and segment<NUM>b.
2263 Note that some program segments may have a file size that is different than
2264 (less than) the memory size. All this means is that at execution the
2265 system must allocate the amount of memory specified by the memory size,
2266 but only initialize it with the first "file size" bytes read from the
2267 file. This would occur for example, with program segments consisting
2268 of combined data+bss.
2270 To handle the above situation, this routine generates TWO bfd sections
2271 for the single program segment. The first has the length specified by
2272 the file size of the segment, and the second has the length specified
2273 by the difference between the two sizes. In effect, the segment is split
2274 into its initialized and uninitialized parts.
2279 _bfd_elf_make_section_from_phdr (bfd *abfd,
2280 Elf_Internal_Phdr *hdr,
2282 const char *typename)
2290 split = ((hdr->p_memsz > 0)
2291 && (hdr->p_filesz > 0)
2292 && (hdr->p_memsz > hdr->p_filesz));
2294 if (hdr->p_filesz > 0)
2296 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2297 len = strlen (namebuf) + 1;
2298 name = bfd_alloc (abfd, len);
2301 memcpy (name, namebuf, len);
2302 newsect = bfd_make_section (abfd, name);
2303 if (newsect == NULL)
2305 newsect->vma = hdr->p_vaddr;
2306 newsect->lma = hdr->p_paddr;
2307 newsect->size = hdr->p_filesz;
2308 newsect->filepos = hdr->p_offset;
2309 newsect->flags |= SEC_HAS_CONTENTS;
2310 newsect->alignment_power = bfd_log2 (hdr->p_align);
2311 if (hdr->p_type == PT_LOAD)
2313 newsect->flags |= SEC_ALLOC;
2314 newsect->flags |= SEC_LOAD;
2315 if (hdr->p_flags & PF_X)
2317 /* FIXME: all we known is that it has execute PERMISSION,
2319 newsect->flags |= SEC_CODE;
2322 if (!(hdr->p_flags & PF_W))
2324 newsect->flags |= SEC_READONLY;
2328 if (hdr->p_memsz > hdr->p_filesz)
2332 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
2333 len = strlen (namebuf) + 1;
2334 name = bfd_alloc (abfd, len);
2337 memcpy (name, namebuf, len);
2338 newsect = bfd_make_section (abfd, name);
2339 if (newsect == NULL)
2341 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2342 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2343 newsect->size = hdr->p_memsz - hdr->p_filesz;
2344 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2345 align = newsect->vma & -newsect->vma;
2346 if (align == 0 || align > hdr->p_align)
2347 align = hdr->p_align;
2348 newsect->alignment_power = bfd_log2 (align);
2349 if (hdr->p_type == PT_LOAD)
2351 /* Hack for gdb. Segments that have not been modified do
2352 not have their contents written to a core file, on the
2353 assumption that a debugger can find the contents in the
2354 executable. We flag this case by setting the fake
2355 section size to zero. Note that "real" bss sections will
2356 always have their contents dumped to the core file. */
2357 if (bfd_get_format (abfd) == bfd_core)
2359 newsect->flags |= SEC_ALLOC;
2360 if (hdr->p_flags & PF_X)
2361 newsect->flags |= SEC_CODE;
2363 if (!(hdr->p_flags & PF_W))
2364 newsect->flags |= SEC_READONLY;
2371 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2373 const struct elf_backend_data *bed;
2375 switch (hdr->p_type)
2378 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2381 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2384 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2387 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2390 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2392 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2397 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2400 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2402 case PT_GNU_EH_FRAME:
2403 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2407 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2410 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2413 /* Check for any processor-specific program segment types. */
2414 bed = get_elf_backend_data (abfd);
2415 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2419 /* Initialize REL_HDR, the section-header for new section, containing
2420 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2421 relocations; otherwise, we use REL relocations. */
2424 _bfd_elf_init_reloc_shdr (bfd *abfd,
2425 Elf_Internal_Shdr *rel_hdr,
2427 bfd_boolean use_rela_p)
2430 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2431 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2433 name = bfd_alloc (abfd, amt);
2436 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2438 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2440 if (rel_hdr->sh_name == (unsigned int) -1)
2442 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2443 rel_hdr->sh_entsize = (use_rela_p
2444 ? bed->s->sizeof_rela
2445 : bed->s->sizeof_rel);
2446 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2447 rel_hdr->sh_flags = 0;
2448 rel_hdr->sh_addr = 0;
2449 rel_hdr->sh_size = 0;
2450 rel_hdr->sh_offset = 0;
2455 /* Set up an ELF internal section header for a section. */
2458 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2460 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2461 bfd_boolean *failedptr = failedptrarg;
2462 Elf_Internal_Shdr *this_hdr;
2463 unsigned int sh_type;
2467 /* We already failed; just get out of the bfd_map_over_sections
2472 this_hdr = &elf_section_data (asect)->this_hdr;
2474 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2475 asect->name, FALSE);
2476 if (this_hdr->sh_name == (unsigned int) -1)
2482 /* Don't clear sh_flags. Assembler may set additional bits. */
2484 if ((asect->flags & SEC_ALLOC) != 0
2485 || asect->user_set_vma)
2486 this_hdr->sh_addr = asect->vma;
2488 this_hdr->sh_addr = 0;
2490 this_hdr->sh_offset = 0;
2491 this_hdr->sh_size = asect->size;
2492 this_hdr->sh_link = 0;
2493 this_hdr->sh_addralign = 1 << asect->alignment_power;
2494 /* The sh_entsize and sh_info fields may have been set already by
2495 copy_private_section_data. */
2497 this_hdr->bfd_section = asect;
2498 this_hdr->contents = NULL;
2500 /* If the section type is unspecified, we set it based on
2502 if ((asect->flags & SEC_GROUP) != 0)
2503 sh_type = SHT_GROUP;
2504 else if ((asect->flags & SEC_ALLOC) != 0
2505 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2506 || (asect->flags & SEC_NEVER_LOAD) != 0))
2507 sh_type = SHT_NOBITS;
2509 sh_type = SHT_PROGBITS;
2511 if (this_hdr->sh_type == SHT_NULL)
2512 this_hdr->sh_type = sh_type;
2513 else if (this_hdr->sh_type == SHT_NOBITS
2514 && sh_type == SHT_PROGBITS
2515 && (asect->flags & SEC_ALLOC) != 0)
2517 /* Warn if we are changing a NOBITS section to PROGBITS, but
2518 allow the link to proceed. This can happen when users link
2519 non-bss input sections to bss output sections, or emit data
2520 to a bss output section via a linker script. */
2521 (*_bfd_error_handler)
2522 (_("warning: section `%A' type changed to PROGBITS"), asect);
2523 this_hdr->sh_type = sh_type;
2526 switch (this_hdr->sh_type)
2532 case SHT_INIT_ARRAY:
2533 case SHT_FINI_ARRAY:
2534 case SHT_PREINIT_ARRAY:
2541 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2545 this_hdr->sh_entsize = bed->s->sizeof_sym;
2549 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2553 if (get_elf_backend_data (abfd)->may_use_rela_p)
2554 this_hdr->sh_entsize = bed->s->sizeof_rela;
2558 if (get_elf_backend_data (abfd)->may_use_rel_p)
2559 this_hdr->sh_entsize = bed->s->sizeof_rel;
2562 case SHT_GNU_versym:
2563 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2566 case SHT_GNU_verdef:
2567 this_hdr->sh_entsize = 0;
2568 /* objcopy or strip will copy over sh_info, but may not set
2569 cverdefs. The linker will set cverdefs, but sh_info will be
2571 if (this_hdr->sh_info == 0)
2572 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2574 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2575 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2578 case SHT_GNU_verneed:
2579 this_hdr->sh_entsize = 0;
2580 /* objcopy or strip will copy over sh_info, but may not set
2581 cverrefs. The linker will set cverrefs, but sh_info will be
2583 if (this_hdr->sh_info == 0)
2584 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2586 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2587 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2591 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2595 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2599 if ((asect->flags & SEC_ALLOC) != 0)
2600 this_hdr->sh_flags |= SHF_ALLOC;
2601 if ((asect->flags & SEC_READONLY) == 0)
2602 this_hdr->sh_flags |= SHF_WRITE;
2603 if ((asect->flags & SEC_CODE) != 0)
2604 this_hdr->sh_flags |= SHF_EXECINSTR;
2605 if ((asect->flags & SEC_MERGE) != 0)
2607 this_hdr->sh_flags |= SHF_MERGE;
2608 this_hdr->sh_entsize = asect->entsize;
2609 if ((asect->flags & SEC_STRINGS) != 0)
2610 this_hdr->sh_flags |= SHF_STRINGS;
2612 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2613 this_hdr->sh_flags |= SHF_GROUP;
2614 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2616 this_hdr->sh_flags |= SHF_TLS;
2617 if (asect->size == 0
2618 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2620 struct bfd_link_order *o = asect->map_tail.link_order;
2622 this_hdr->sh_size = 0;
2625 this_hdr->sh_size = o->offset + o->size;
2626 if (this_hdr->sh_size != 0)
2627 this_hdr->sh_type = SHT_NOBITS;
2632 /* Check for processor-specific section types. */
2633 sh_type = this_hdr->sh_type;
2634 if (bed->elf_backend_fake_sections
2635 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2638 if (sh_type == SHT_NOBITS && asect->size != 0)
2640 /* Don't change the header type from NOBITS if we are being
2641 called for objcopy --only-keep-debug. */
2642 this_hdr->sh_type = sh_type;
2645 /* If the section has relocs, set up a section header for the
2646 SHT_REL[A] section. If two relocation sections are required for
2647 this section, it is up to the processor-specific back-end to
2648 create the other. */
2649 if ((asect->flags & SEC_RELOC) != 0
2650 && !_bfd_elf_init_reloc_shdr (abfd,
2651 &elf_section_data (asect)->rel_hdr,
2657 /* Fill in the contents of a SHT_GROUP section. */
2660 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2662 bfd_boolean *failedptr = failedptrarg;
2663 unsigned long symindx;
2664 asection *elt, *first;
2668 /* Ignore linker created group section. See elfNN_ia64_object_p in
2670 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2675 if (elf_group_id (sec) != NULL)
2676 symindx = elf_group_id (sec)->udata.i;
2680 /* If called from the assembler, swap_out_syms will have set up
2681 elf_section_syms; If called for "ld -r", use target_index. */
2682 if (elf_section_syms (abfd) != NULL)
2683 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2685 symindx = sec->target_index;
2687 elf_section_data (sec)->this_hdr.sh_info = symindx;
2689 /* The contents won't be allocated for "ld -r" or objcopy. */
2691 if (sec->contents == NULL)
2694 sec->contents = bfd_alloc (abfd, sec->size);
2696 /* Arrange for the section to be written out. */
2697 elf_section_data (sec)->this_hdr.contents = sec->contents;
2698 if (sec->contents == NULL)
2705 loc = sec->contents + sec->size;
2707 /* Get the pointer to the first section in the group that gas
2708 squirreled away here. objcopy arranges for this to be set to the
2709 start of the input section group. */
2710 first = elt = elf_next_in_group (sec);
2712 /* First element is a flag word. Rest of section is elf section
2713 indices for all the sections of the group. Write them backwards
2714 just to keep the group in the same order as given in .section
2715 directives, not that it matters. */
2724 s = s->output_section;
2727 idx = elf_section_data (s)->this_idx;
2728 H_PUT_32 (abfd, idx, loc);
2729 elt = elf_next_in_group (elt);
2734 if ((loc -= 4) != sec->contents)
2737 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2740 /* Assign all ELF section numbers. The dummy first section is handled here
2741 too. The link/info pointers for the standard section types are filled
2742 in here too, while we're at it. */
2745 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2747 struct elf_obj_tdata *t = elf_tdata (abfd);
2749 unsigned int section_number, secn;
2750 Elf_Internal_Shdr **i_shdrp;
2751 struct bfd_elf_section_data *d;
2755 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2757 /* SHT_GROUP sections are in relocatable files only. */
2758 if (link_info == NULL || link_info->relocatable)
2760 /* Put SHT_GROUP sections first. */
2761 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2763 d = elf_section_data (sec);
2765 if (d->this_hdr.sh_type == SHT_GROUP)
2767 if (sec->flags & SEC_LINKER_CREATED)
2769 /* Remove the linker created SHT_GROUP sections. */
2770 bfd_section_list_remove (abfd, sec);
2771 abfd->section_count--;
2775 if (section_number == SHN_LORESERVE)
2776 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2777 d->this_idx = section_number++;
2783 for (sec = abfd->sections; sec; sec = sec->next)
2785 d = elf_section_data (sec);
2787 if (d->this_hdr.sh_type != SHT_GROUP)
2789 if (section_number == SHN_LORESERVE)
2790 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2791 d->this_idx = section_number++;
2793 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2794 if ((sec->flags & SEC_RELOC) == 0)
2798 if (section_number == SHN_LORESERVE)
2799 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2800 d->rel_idx = section_number++;
2801 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2806 if (section_number == SHN_LORESERVE)
2807 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2808 d->rel_idx2 = section_number++;
2809 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2815 if (section_number == SHN_LORESERVE)
2816 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2817 t->shstrtab_section = section_number++;
2818 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2819 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2821 if (bfd_get_symcount (abfd) > 0)
2823 if (section_number == SHN_LORESERVE)
2824 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2825 t->symtab_section = section_number++;
2826 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2827 if (section_number > SHN_LORESERVE - 2)
2829 if (section_number == SHN_LORESERVE)
2830 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2831 t->symtab_shndx_section = section_number++;
2832 t->symtab_shndx_hdr.sh_name
2833 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2834 ".symtab_shndx", FALSE);
2835 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2838 if (section_number == SHN_LORESERVE)
2839 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2840 t->strtab_section = section_number++;
2841 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2844 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2845 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2847 elf_numsections (abfd) = section_number;
2848 elf_elfheader (abfd)->e_shnum = section_number;
2849 if (section_number > SHN_LORESERVE)
2850 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2852 /* Set up the list of section header pointers, in agreement with the
2854 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2855 if (i_shdrp == NULL)
2858 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2859 if (i_shdrp[0] == NULL)
2861 bfd_release (abfd, i_shdrp);
2865 elf_elfsections (abfd) = i_shdrp;
2867 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2868 if (bfd_get_symcount (abfd) > 0)
2870 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2871 if (elf_numsections (abfd) > SHN_LORESERVE)
2873 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2874 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2876 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2877 t->symtab_hdr.sh_link = t->strtab_section;
2880 for (sec = abfd->sections; sec; sec = sec->next)
2882 struct bfd_elf_section_data *d = elf_section_data (sec);
2886 i_shdrp[d->this_idx] = &d->this_hdr;
2887 if (d->rel_idx != 0)
2888 i_shdrp[d->rel_idx] = &d->rel_hdr;
2889 if (d->rel_idx2 != 0)
2890 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2892 /* Fill in the sh_link and sh_info fields while we're at it. */
2894 /* sh_link of a reloc section is the section index of the symbol
2895 table. sh_info is the section index of the section to which
2896 the relocation entries apply. */
2897 if (d->rel_idx != 0)
2899 d->rel_hdr.sh_link = t->symtab_section;
2900 d->rel_hdr.sh_info = d->this_idx;
2902 if (d->rel_idx2 != 0)
2904 d->rel_hdr2->sh_link = t->symtab_section;
2905 d->rel_hdr2->sh_info = d->this_idx;
2908 /* We need to set up sh_link for SHF_LINK_ORDER. */
2909 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2911 s = elf_linked_to_section (sec);
2914 /* elf_linked_to_section points to the input section. */
2915 if (link_info != NULL)
2917 /* Check discarded linkonce section. */
2918 if (elf_discarded_section (s))
2921 (*_bfd_error_handler)
2922 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2923 abfd, d->this_hdr.bfd_section,
2925 /* Point to the kept section if it has the same
2926 size as the discarded one. */
2927 kept = _bfd_elf_check_kept_section (s, link_info);
2930 bfd_set_error (bfd_error_bad_value);
2936 s = s->output_section;
2937 BFD_ASSERT (s != NULL);
2941 /* Handle objcopy. */
2942 if (s->output_section == NULL)
2944 (*_bfd_error_handler)
2945 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2946 abfd, d->this_hdr.bfd_section, s, s->owner);
2947 bfd_set_error (bfd_error_bad_value);
2950 s = s->output_section;
2952 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2957 The Intel C compiler generates SHT_IA_64_UNWIND with
2958 SHF_LINK_ORDER. But it doesn't set the sh_link or
2959 sh_info fields. Hence we could get the situation
2961 const struct elf_backend_data *bed
2962 = get_elf_backend_data (abfd);
2963 if (bed->link_order_error_handler)
2964 bed->link_order_error_handler
2965 (_("%B: warning: sh_link not set for section `%A'"),
2970 switch (d->this_hdr.sh_type)
2974 /* A reloc section which we are treating as a normal BFD
2975 section. sh_link is the section index of the symbol
2976 table. sh_info is the section index of the section to
2977 which the relocation entries apply. We assume that an
2978 allocated reloc section uses the dynamic symbol table.
2979 FIXME: How can we be sure? */
2980 s = bfd_get_section_by_name (abfd, ".dynsym");
2982 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2984 /* We look up the section the relocs apply to by name. */
2986 if (d->this_hdr.sh_type == SHT_REL)
2990 s = bfd_get_section_by_name (abfd, name);
2992 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2996 /* We assume that a section named .stab*str is a stabs
2997 string section. We look for a section with the same name
2998 but without the trailing ``str'', and set its sh_link
2999 field to point to this section. */
3000 if (CONST_STRNEQ (sec->name, ".stab")
3001 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3006 len = strlen (sec->name);
3007 alc = bfd_malloc (len - 2);
3010 memcpy (alc, sec->name, len - 3);
3011 alc[len - 3] = '\0';
3012 s = bfd_get_section_by_name (abfd, alc);
3016 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3018 /* This is a .stab section. */
3019 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3020 elf_section_data (s)->this_hdr.sh_entsize
3021 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3028 case SHT_GNU_verneed:
3029 case SHT_GNU_verdef:
3030 /* sh_link is the section header index of the string table
3031 used for the dynamic entries, or the symbol table, or the
3033 s = bfd_get_section_by_name (abfd, ".dynstr");
3035 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3038 case SHT_GNU_LIBLIST:
3039 /* sh_link is the section header index of the prelink library
3040 list used for the dynamic entries, or the symbol table, or
3041 the version strings. */
3042 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3043 ? ".dynstr" : ".gnu.libstr");
3045 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3050 case SHT_GNU_versym:
3051 /* sh_link is the section header index of the symbol table
3052 this hash table or version table is for. */
3053 s = bfd_get_section_by_name (abfd, ".dynsym");
3055 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3059 d->this_hdr.sh_link = t->symtab_section;
3063 for (secn = 1; secn < section_number; ++secn)
3064 if (i_shdrp[secn] == NULL)
3065 i_shdrp[secn] = i_shdrp[0];
3067 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3068 i_shdrp[secn]->sh_name);
3072 /* Map symbol from it's internal number to the external number, moving
3073 all local symbols to be at the head of the list. */
3076 sym_is_global (bfd *abfd, asymbol *sym)
3078 /* If the backend has a special mapping, use it. */
3079 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3080 if (bed->elf_backend_sym_is_global)
3081 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3083 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3084 || bfd_is_und_section (bfd_get_section (sym))
3085 || bfd_is_com_section (bfd_get_section (sym)));
3088 /* Don't output section symbols for sections that are not going to be
3089 output. Also, don't output section symbols for reloc and other
3090 special sections. */
3093 ignore_section_sym (bfd *abfd, asymbol *sym)
3095 return ((sym->flags & BSF_SECTION_SYM) != 0
3097 || (sym->section->owner != abfd
3098 && (sym->section->output_section->owner != abfd
3099 || sym->section->output_offset != 0))));
3103 elf_map_symbols (bfd *abfd)
3105 unsigned int symcount = bfd_get_symcount (abfd);
3106 asymbol **syms = bfd_get_outsymbols (abfd);
3107 asymbol **sect_syms;
3108 unsigned int num_locals = 0;
3109 unsigned int num_globals = 0;
3110 unsigned int num_locals2 = 0;
3111 unsigned int num_globals2 = 0;
3118 fprintf (stderr, "elf_map_symbols\n");
3122 for (asect = abfd->sections; asect; asect = asect->next)
3124 if (max_index < asect->index)
3125 max_index = asect->index;
3129 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3130 if (sect_syms == NULL)
3132 elf_section_syms (abfd) = sect_syms;
3133 elf_num_section_syms (abfd) = max_index;
3135 /* Init sect_syms entries for any section symbols we have already
3136 decided to output. */
3137 for (idx = 0; idx < symcount; idx++)
3139 asymbol *sym = syms[idx];
3141 if ((sym->flags & BSF_SECTION_SYM) != 0
3142 && !ignore_section_sym (abfd, sym))
3144 asection *sec = sym->section;
3146 if (sec->owner != abfd)
3147 sec = sec->output_section;
3149 sect_syms[sec->index] = syms[idx];
3153 /* Classify all of the symbols. */
3154 for (idx = 0; idx < symcount; idx++)
3156 if (ignore_section_sym (abfd, syms[idx]))
3158 if (!sym_is_global (abfd, syms[idx]))
3164 /* We will be adding a section symbol for each normal BFD section. Most
3165 sections will already have a section symbol in outsymbols, but
3166 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3167 at least in that case. */
3168 for (asect = abfd->sections; asect; asect = asect->next)
3170 if (sect_syms[asect->index] == NULL)
3172 if (!sym_is_global (abfd, asect->symbol))
3179 /* Now sort the symbols so the local symbols are first. */
3180 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3182 if (new_syms == NULL)
3185 for (idx = 0; idx < symcount; idx++)
3187 asymbol *sym = syms[idx];
3190 if (ignore_section_sym (abfd, sym))
3192 if (!sym_is_global (abfd, sym))
3195 i = num_locals + num_globals2++;
3197 sym->udata.i = i + 1;
3199 for (asect = abfd->sections; asect; asect = asect->next)
3201 if (sect_syms[asect->index] == NULL)
3203 asymbol *sym = asect->symbol;
3206 sect_syms[asect->index] = sym;
3207 if (!sym_is_global (abfd, sym))
3210 i = num_locals + num_globals2++;
3212 sym->udata.i = i + 1;
3216 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3218 elf_num_locals (abfd) = num_locals;
3219 elf_num_globals (abfd) = num_globals;
3223 /* Align to the maximum file alignment that could be required for any
3224 ELF data structure. */
3226 static inline file_ptr
3227 align_file_position (file_ptr off, int align)
3229 return (off + align - 1) & ~(align - 1);
3232 /* Assign a file position to a section, optionally aligning to the
3233 required section alignment. */
3236 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3244 al = i_shdrp->sh_addralign;
3246 offset = BFD_ALIGN (offset, al);
3248 i_shdrp->sh_offset = offset;
3249 if (i_shdrp->bfd_section != NULL)
3250 i_shdrp->bfd_section->filepos = offset;
3251 if (i_shdrp->sh_type != SHT_NOBITS)
3252 offset += i_shdrp->sh_size;
3256 /* Compute the file positions we are going to put the sections at, and
3257 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3258 is not NULL, this is being called by the ELF backend linker. */
3261 _bfd_elf_compute_section_file_positions (bfd *abfd,
3262 struct bfd_link_info *link_info)
3264 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3266 struct bfd_strtab_hash *strtab = NULL;
3267 Elf_Internal_Shdr *shstrtab_hdr;
3269 if (abfd->output_has_begun)
3272 /* Do any elf backend specific processing first. */
3273 if (bed->elf_backend_begin_write_processing)
3274 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3276 if (! prep_headers (abfd))
3279 /* Post process the headers if necessary. */
3280 if (bed->elf_backend_post_process_headers)
3281 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3284 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3288 if (!assign_section_numbers (abfd, link_info))
3291 /* The backend linker builds symbol table information itself. */
3292 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3294 /* Non-zero if doing a relocatable link. */
3295 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3297 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3301 if (link_info == NULL)
3303 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3308 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3309 /* sh_name was set in prep_headers. */
3310 shstrtab_hdr->sh_type = SHT_STRTAB;
3311 shstrtab_hdr->sh_flags = 0;
3312 shstrtab_hdr->sh_addr = 0;
3313 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3314 shstrtab_hdr->sh_entsize = 0;
3315 shstrtab_hdr->sh_link = 0;
3316 shstrtab_hdr->sh_info = 0;
3317 /* sh_offset is set in assign_file_positions_except_relocs. */
3318 shstrtab_hdr->sh_addralign = 1;
3320 if (!assign_file_positions_except_relocs (abfd, link_info))
3323 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3326 Elf_Internal_Shdr *hdr;
3328 off = elf_tdata (abfd)->next_file_pos;
3330 hdr = &elf_tdata (abfd)->symtab_hdr;
3331 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3333 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3334 if (hdr->sh_size != 0)
3335 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3337 hdr = &elf_tdata (abfd)->strtab_hdr;
3338 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3340 elf_tdata (abfd)->next_file_pos = off;
3342 /* Now that we know where the .strtab section goes, write it
3344 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3345 || ! _bfd_stringtab_emit (abfd, strtab))
3347 _bfd_stringtab_free (strtab);
3350 abfd->output_has_begun = TRUE;
3355 /* Make an initial estimate of the size of the program header. If we
3356 get the number wrong here, we'll redo section placement. */
3358 static bfd_size_type
3359 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3363 const struct elf_backend_data *bed;
3365 /* Assume we will need exactly two PT_LOAD segments: one for text
3366 and one for data. */
3369 s = bfd_get_section_by_name (abfd, ".interp");
3370 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3372 /* If we have a loadable interpreter section, we need a
3373 PT_INTERP segment. In this case, assume we also need a
3374 PT_PHDR segment, although that may not be true for all
3379 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3381 /* We need a PT_DYNAMIC segment. */
3387 /* We need a PT_GNU_RELRO segment. */
3391 if (elf_tdata (abfd)->eh_frame_hdr)
3393 /* We need a PT_GNU_EH_FRAME segment. */
3397 if (elf_tdata (abfd)->stack_flags)
3399 /* We need a PT_GNU_STACK segment. */
3403 for (s = abfd->sections; s != NULL; s = s->next)
3405 if ((s->flags & SEC_LOAD) != 0
3406 && CONST_STRNEQ (s->name, ".note"))
3408 /* We need a PT_NOTE segment. */
3410 /* Try to create just one PT_NOTE segment
3411 for all adjacent loadable .note* sections.
3412 gABI requires that within a PT_NOTE segment
3413 (and also inside of each SHT_NOTE section)
3414 each note is padded to a multiple of 4 size,
3415 so we check whether the sections are correctly
3417 if (s->alignment_power == 2)
3418 while (s->next != NULL
3419 && s->next->alignment_power == 2
3420 && (s->next->flags & SEC_LOAD) != 0
3421 && CONST_STRNEQ (s->next->name, ".note"))
3426 for (s = abfd->sections; s != NULL; s = s->next)
3428 if (s->flags & SEC_THREAD_LOCAL)
3430 /* We need a PT_TLS segment. */
3436 /* Let the backend count up any program headers it might need. */
3437 bed = get_elf_backend_data (abfd);
3438 if (bed->elf_backend_additional_program_headers)
3442 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3448 return segs * bed->s->sizeof_phdr;
3451 /* Find the segment that contains the output_section of section. */
3454 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3456 struct elf_segment_map *m;
3457 Elf_Internal_Phdr *p;
3459 for (m = elf_tdata (abfd)->segment_map,
3460 p = elf_tdata (abfd)->phdr;
3466 for (i = m->count - 1; i >= 0; i--)
3467 if (m->sections[i] == section)
3474 /* Create a mapping from a set of sections to a program segment. */
3476 static struct elf_segment_map *
3477 make_mapping (bfd *abfd,
3478 asection **sections,
3483 struct elf_segment_map *m;
3488 amt = sizeof (struct elf_segment_map);
3489 amt += (to - from - 1) * sizeof (asection *);
3490 m = bfd_zalloc (abfd, amt);
3494 m->p_type = PT_LOAD;
3495 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3496 m->sections[i - from] = *hdrpp;
3497 m->count = to - from;
3499 if (from == 0 && phdr)
3501 /* Include the headers in the first PT_LOAD segment. */
3502 m->includes_filehdr = 1;
3503 m->includes_phdrs = 1;
3509 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3512 struct elf_segment_map *
3513 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3515 struct elf_segment_map *m;
3517 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3521 m->p_type = PT_DYNAMIC;
3523 m->sections[0] = dynsec;
3528 /* Possibly add or remove segments from the segment map. */
3531 elf_modify_segment_map (bfd *abfd,
3532 struct bfd_link_info *info,
3533 bfd_boolean remove_empty_load)
3535 struct elf_segment_map **m;
3536 const struct elf_backend_data *bed;
3538 /* The placement algorithm assumes that non allocated sections are
3539 not in PT_LOAD segments. We ensure this here by removing such
3540 sections from the segment map. We also remove excluded
3541 sections. Finally, any PT_LOAD segment without sections is
3543 m = &elf_tdata (abfd)->segment_map;
3546 unsigned int i, new_count;
3548 for (new_count = 0, i = 0; i < (*m)->count; i++)
3550 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3551 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3552 || (*m)->p_type != PT_LOAD))
3554 (*m)->sections[new_count] = (*m)->sections[i];
3558 (*m)->count = new_count;
3560 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3566 bed = get_elf_backend_data (abfd);
3567 if (bed->elf_backend_modify_segment_map != NULL)
3569 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3576 /* Set up a mapping from BFD sections to program segments. */
3579 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3582 struct elf_segment_map *m;
3583 asection **sections = NULL;
3584 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3585 bfd_boolean no_user_phdrs;
3587 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3588 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3592 struct elf_segment_map *mfirst;
3593 struct elf_segment_map **pm;
3596 unsigned int phdr_index;
3597 bfd_vma maxpagesize;
3599 bfd_boolean phdr_in_segment = TRUE;
3600 bfd_boolean writable;
3602 asection *first_tls = NULL;
3603 asection *dynsec, *eh_frame_hdr;
3606 /* Select the allocated sections, and sort them. */
3608 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3609 if (sections == NULL)
3613 for (s = abfd->sections; s != NULL; s = s->next)
3615 if ((s->flags & SEC_ALLOC) != 0)
3621 BFD_ASSERT (i <= bfd_count_sections (abfd));
3624 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3626 /* Build the mapping. */
3631 /* If we have a .interp section, then create a PT_PHDR segment for
3632 the program headers and a PT_INTERP segment for the .interp
3634 s = bfd_get_section_by_name (abfd, ".interp");
3635 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3637 amt = sizeof (struct elf_segment_map);
3638 m = bfd_zalloc (abfd, amt);
3642 m->p_type = PT_PHDR;
3643 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3644 m->p_flags = PF_R | PF_X;
3645 m->p_flags_valid = 1;
3646 m->includes_phdrs = 1;
3651 amt = sizeof (struct elf_segment_map);
3652 m = bfd_zalloc (abfd, amt);
3656 m->p_type = PT_INTERP;
3664 /* Look through the sections. We put sections in the same program
3665 segment when the start of the second section can be placed within
3666 a few bytes of the end of the first section. */
3670 maxpagesize = bed->maxpagesize;
3672 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3674 && (dynsec->flags & SEC_LOAD) == 0)
3677 /* Deal with -Ttext or something similar such that the first section
3678 is not adjacent to the program headers. This is an
3679 approximation, since at this point we don't know exactly how many
3680 program headers we will need. */
3683 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3685 if (phdr_size == (bfd_size_type) -1)
3686 phdr_size = get_program_header_size (abfd, info);
3687 if ((abfd->flags & D_PAGED) == 0
3688 || sections[0]->lma < phdr_size
3689 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3690 phdr_in_segment = FALSE;
3693 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3696 bfd_boolean new_segment;
3700 /* See if this section and the last one will fit in the same
3703 if (last_hdr == NULL)
3705 /* If we don't have a segment yet, then we don't need a new
3706 one (we build the last one after this loop). */
3707 new_segment = FALSE;
3709 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3711 /* If this section has a different relation between the
3712 virtual address and the load address, then we need a new
3716 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3717 < BFD_ALIGN (hdr->lma, maxpagesize))
3719 /* If putting this section in this segment would force us to
3720 skip a page in the segment, then we need a new segment. */
3723 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3724 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3726 /* We don't want to put a loadable section after a
3727 nonloadable section in the same segment.
3728 Consider .tbss sections as loadable for this purpose. */
3731 else if ((abfd->flags & D_PAGED) == 0)
3733 /* If the file is not demand paged, which means that we
3734 don't require the sections to be correctly aligned in the
3735 file, then there is no other reason for a new segment. */
3736 new_segment = FALSE;
3739 && (hdr->flags & SEC_READONLY) == 0
3740 && (((last_hdr->lma + last_size - 1)
3741 & ~(maxpagesize - 1))
3742 != (hdr->lma & ~(maxpagesize - 1))))
3744 /* We don't want to put a writable section in a read only
3745 segment, unless they are on the same page in memory
3746 anyhow. We already know that the last section does not
3747 bring us past the current section on the page, so the
3748 only case in which the new section is not on the same
3749 page as the previous section is when the previous section
3750 ends precisely on a page boundary. */
3755 /* Otherwise, we can use the same segment. */
3756 new_segment = FALSE;
3759 /* Allow interested parties a chance to override our decision. */
3760 if (last_hdr && info->callbacks->override_segment_assignment)
3761 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3765 if ((hdr->flags & SEC_READONLY) == 0)
3768 /* .tbss sections effectively have zero size. */
3769 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3770 != SEC_THREAD_LOCAL)
3771 last_size = hdr->size;
3777 /* We need a new program segment. We must create a new program
3778 header holding all the sections from phdr_index until hdr. */
3780 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3787 if ((hdr->flags & SEC_READONLY) == 0)
3793 /* .tbss sections effectively have zero size. */
3794 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3795 last_size = hdr->size;
3799 phdr_in_segment = FALSE;
3802 /* Create a final PT_LOAD program segment. */
3803 if (last_hdr != NULL)
3805 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3813 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3816 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3823 /* For each batch of consecutive loadable .note sections,
3824 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3825 because if we link together nonloadable .note sections and
3826 loadable .note sections, we will generate two .note sections
3827 in the output file. FIXME: Using names for section types is
3829 for (s = abfd->sections; s != NULL; s = s->next)
3831 if ((s->flags & SEC_LOAD) != 0
3832 && CONST_STRNEQ (s->name, ".note"))
3836 amt = sizeof (struct elf_segment_map);
3837 if (s->alignment_power == 2)
3838 for (s2 = s; s2->next != NULL; s2 = s2->next)
3840 if (s2->next->alignment_power == 2
3841 && (s2->next->flags & SEC_LOAD) != 0
3842 && CONST_STRNEQ (s2->next->name, ".note")
3843 && align_power (s2->vma + s2->size, 2)
3849 amt += (count - 1) * sizeof (asection *);
3850 m = bfd_zalloc (abfd, amt);
3854 m->p_type = PT_NOTE;
3858 m->sections[m->count - count--] = s;
3859 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3862 m->sections[m->count - 1] = s;
3863 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3867 if (s->flags & SEC_THREAD_LOCAL)
3875 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3880 amt = sizeof (struct elf_segment_map);
3881 amt += (tls_count - 1) * sizeof (asection *);
3882 m = bfd_zalloc (abfd, amt);
3887 m->count = tls_count;
3888 /* Mandated PF_R. */
3890 m->p_flags_valid = 1;
3891 for (i = 0; i < tls_count; ++i)
3893 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3894 m->sections[i] = first_tls;
3895 first_tls = first_tls->next;
3902 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3904 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3905 if (eh_frame_hdr != NULL
3906 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3908 amt = sizeof (struct elf_segment_map);
3909 m = bfd_zalloc (abfd, amt);
3913 m->p_type = PT_GNU_EH_FRAME;
3915 m->sections[0] = eh_frame_hdr->output_section;
3921 if (elf_tdata (abfd)->stack_flags)
3923 amt = sizeof (struct elf_segment_map);
3924 m = bfd_zalloc (abfd, amt);
3928 m->p_type = PT_GNU_STACK;
3929 m->p_flags = elf_tdata (abfd)->stack_flags;
3930 m->p_flags_valid = 1;
3938 for (m = mfirst; m != NULL; m = m->next)
3940 if (m->p_type == PT_LOAD)
3942 asection *last = m->sections[m->count - 1];
3943 bfd_vma vaddr = m->sections[0]->vma;
3944 bfd_vma filesz = last->vma - vaddr + last->size;
3946 if (vaddr < info->relro_end
3947 && vaddr >= info->relro_start
3948 && (vaddr + filesz) >= info->relro_end)
3953 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3956 amt = sizeof (struct elf_segment_map);
3957 m = bfd_zalloc (abfd, amt);
3961 m->p_type = PT_GNU_RELRO;
3963 m->p_flags_valid = 1;
3971 elf_tdata (abfd)->segment_map = mfirst;
3974 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
3977 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3979 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
3984 if (sections != NULL)
3989 /* Sort sections by address. */
3992 elf_sort_sections (const void *arg1, const void *arg2)
3994 const asection *sec1 = *(const asection **) arg1;
3995 const asection *sec2 = *(const asection **) arg2;
3996 bfd_size_type size1, size2;
3998 /* Sort by LMA first, since this is the address used to
3999 place the section into a segment. */
4000 if (sec1->lma < sec2->lma)
4002 else if (sec1->lma > sec2->lma)
4005 /* Then sort by VMA. Normally the LMA and the VMA will be
4006 the same, and this will do nothing. */
4007 if (sec1->vma < sec2->vma)
4009 else if (sec1->vma > sec2->vma)
4012 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4014 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4020 /* If the indicies are the same, do not return 0
4021 here, but continue to try the next comparison. */
4022 if (sec1->target_index - sec2->target_index != 0)
4023 return sec1->target_index - sec2->target_index;
4028 else if (TOEND (sec2))
4033 /* Sort by size, to put zero sized sections
4034 before others at the same address. */
4036 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4037 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4044 return sec1->target_index - sec2->target_index;
4047 /* Ian Lance Taylor writes:
4049 We shouldn't be using % with a negative signed number. That's just
4050 not good. We have to make sure either that the number is not
4051 negative, or that the number has an unsigned type. When the types
4052 are all the same size they wind up as unsigned. When file_ptr is a
4053 larger signed type, the arithmetic winds up as signed long long,
4056 What we're trying to say here is something like ``increase OFF by
4057 the least amount that will cause it to be equal to the VMA modulo
4059 /* In other words, something like:
4061 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4062 off_offset = off % bed->maxpagesize;
4063 if (vma_offset < off_offset)
4064 adjustment = vma_offset + bed->maxpagesize - off_offset;
4066 adjustment = vma_offset - off_offset;
4068 which can can be collapsed into the expression below. */
4071 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4073 return ((vma - off) % maxpagesize);
4077 print_segment_map (const struct elf_segment_map *m)
4080 const char *pt = get_segment_type (m->p_type);
4085 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4086 sprintf (buf, "LOPROC+%7.7x",
4087 (unsigned int) (m->p_type - PT_LOPROC));
4088 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4089 sprintf (buf, "LOOS+%7.7x",
4090 (unsigned int) (m->p_type - PT_LOOS));
4092 snprintf (buf, sizeof (buf), "%8.8x",
4093 (unsigned int) m->p_type);
4096 fprintf (stderr, "%s:", pt);
4097 for (j = 0; j < m->count; j++)
4098 fprintf (stderr, " %s", m->sections [j]->name);
4102 /* Assign file positions to the sections based on the mapping from
4103 sections to segments. This function also sets up some fields in
4107 assign_file_positions_for_load_sections (bfd *abfd,
4108 struct bfd_link_info *link_info)
4110 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4111 struct elf_segment_map *m;
4112 Elf_Internal_Phdr *phdrs;
4113 Elf_Internal_Phdr *p;
4115 bfd_size_type maxpagesize;
4119 if (link_info == NULL
4120 && !elf_modify_segment_map (abfd, link_info, FALSE))
4124 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4127 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4128 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4129 elf_elfheader (abfd)->e_phnum = alloc;
4131 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4132 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4134 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4135 >= alloc * bed->s->sizeof_phdr);
4139 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4143 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4144 elf_tdata (abfd)->phdr = phdrs;
4149 if ((abfd->flags & D_PAGED) != 0)
4150 maxpagesize = bed->maxpagesize;
4152 off = bed->s->sizeof_ehdr;
4153 off += alloc * bed->s->sizeof_phdr;
4155 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4157 m = m->next, p++, j++)
4161 bfd_boolean no_contents;
4163 /* If elf_segment_map is not from map_sections_to_segments, the
4164 sections may not be correctly ordered. NOTE: sorting should
4165 not be done to the PT_NOTE section of a corefile, which may
4166 contain several pseudo-sections artificially created by bfd.
4167 Sorting these pseudo-sections breaks things badly. */
4169 && !(elf_elfheader (abfd)->e_type == ET_CORE
4170 && m->p_type == PT_NOTE))
4171 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4174 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4175 number of sections with contents contributing to both p_filesz
4176 and p_memsz, followed by a number of sections with no contents
4177 that just contribute to p_memsz. In this loop, OFF tracks next
4178 available file offset for PT_LOAD and PT_NOTE segments. */
4179 p->p_type = m->p_type;
4180 p->p_flags = m->p_flags;
4185 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4187 if (m->p_paddr_valid)
4188 p->p_paddr = m->p_paddr;
4189 else if (m->count == 0)
4192 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4194 if (p->p_type == PT_LOAD
4195 && (abfd->flags & D_PAGED) != 0)
4197 /* p_align in demand paged PT_LOAD segments effectively stores
4198 the maximum page size. When copying an executable with
4199 objcopy, we set m->p_align from the input file. Use this
4200 value for maxpagesize rather than bed->maxpagesize, which
4201 may be different. Note that we use maxpagesize for PT_TLS
4202 segment alignment later in this function, so we are relying
4203 on at least one PT_LOAD segment appearing before a PT_TLS
4205 if (m->p_align_valid)
4206 maxpagesize = m->p_align;
4208 p->p_align = maxpagesize;
4210 else if (m->p_align_valid)
4211 p->p_align = m->p_align;
4212 else if (m->count == 0)
4213 p->p_align = 1 << bed->s->log_file_align;
4217 no_contents = FALSE;
4219 if (p->p_type == PT_LOAD
4222 bfd_size_type align;
4223 unsigned int align_power = 0;
4225 if (m->p_align_valid)
4229 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4231 unsigned int secalign;
4233 secalign = bfd_get_section_alignment (abfd, *secpp);
4234 if (secalign > align_power)
4235 align_power = secalign;
4237 align = (bfd_size_type) 1 << align_power;
4238 if (align < maxpagesize)
4239 align = maxpagesize;
4242 for (i = 0; i < m->count; i++)
4243 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4244 /* If we aren't making room for this section, then
4245 it must be SHT_NOBITS regardless of what we've
4246 set via struct bfd_elf_special_section. */
4247 elf_section_type (m->sections[i]) = SHT_NOBITS;
4249 /* Find out whether this segment contains any loadable
4250 sections. If the first section isn't loadable, the same
4251 holds for any other sections. */
4253 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4255 /* If a segment starts with .tbss, we need to look
4256 at the next section to decide whether the segment
4257 has any loadable sections. */
4258 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4266 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4270 /* We shouldn't need to align the segment on disk since
4271 the segment doesn't need file space, but the gABI
4272 arguably requires the alignment and glibc ld.so
4273 checks it. So to comply with the alignment
4274 requirement but not waste file space, we adjust
4275 p_offset for just this segment. (OFF_ADJUST is
4276 subtracted from OFF later.) This may put p_offset
4277 past the end of file, but that shouldn't matter. */
4282 /* Make sure the .dynamic section is the first section in the
4283 PT_DYNAMIC segment. */
4284 else if (p->p_type == PT_DYNAMIC
4286 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4289 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4291 bfd_set_error (bfd_error_bad_value);
4294 /* Set the note section type to SHT_NOTE. */
4295 else if (p->p_type == PT_NOTE)
4296 for (i = 0; i < m->count; i++)
4297 elf_section_type (m->sections[i]) = SHT_NOTE;
4303 if (m->includes_filehdr)
4305 if (!m->p_flags_valid)
4307 p->p_filesz = bed->s->sizeof_ehdr;
4308 p->p_memsz = bed->s->sizeof_ehdr;
4311 BFD_ASSERT (p->p_type == PT_LOAD);
4313 if (p->p_vaddr < (bfd_vma) off)
4315 (*_bfd_error_handler)
4316 (_("%B: Not enough room for program headers, try linking with -N"),
4318 bfd_set_error (bfd_error_bad_value);
4323 if (!m->p_paddr_valid)
4328 if (m->includes_phdrs)
4330 if (!m->p_flags_valid)
4333 if (!m->includes_filehdr)
4335 p->p_offset = bed->s->sizeof_ehdr;
4339 BFD_ASSERT (p->p_type == PT_LOAD);
4340 p->p_vaddr -= off - p->p_offset;
4341 if (!m->p_paddr_valid)
4342 p->p_paddr -= off - p->p_offset;
4346 p->p_filesz += alloc * bed->s->sizeof_phdr;
4347 p->p_memsz += alloc * bed->s->sizeof_phdr;
4350 if (p->p_type == PT_LOAD
4351 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4353 if (!m->includes_filehdr && !m->includes_phdrs)
4359 adjust = off - (p->p_offset + p->p_filesz);
4361 p->p_filesz += adjust;
4362 p->p_memsz += adjust;
4366 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4367 maps. Set filepos for sections in PT_LOAD segments, and in
4368 core files, for sections in PT_NOTE segments.
4369 assign_file_positions_for_non_load_sections will set filepos
4370 for other sections and update p_filesz for other segments. */
4371 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4374 bfd_size_type align;
4375 Elf_Internal_Shdr *this_hdr;
4378 this_hdr = &elf_section_data (sec)->this_hdr;
4379 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4381 if (p->p_type == PT_LOAD
4382 || p->p_type == PT_TLS)
4384 bfd_signed_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
4386 if (this_hdr->sh_type != SHT_NOBITS
4387 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4388 && ((this_hdr->sh_flags & SHF_TLS) == 0
4389 || p->p_type == PT_TLS)))
4393 (*_bfd_error_handler)
4394 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4395 abfd, sec, (unsigned long) sec->lma);
4398 p->p_memsz += adjust;
4400 if (this_hdr->sh_type != SHT_NOBITS)
4403 p->p_filesz += adjust;
4408 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4410 /* The section at i == 0 is the one that actually contains
4414 this_hdr->sh_offset = sec->filepos = off;
4415 off += this_hdr->sh_size;
4416 p->p_filesz = this_hdr->sh_size;
4422 /* The rest are fake sections that shouldn't be written. */
4431 if (p->p_type == PT_LOAD)
4433 this_hdr->sh_offset = sec->filepos = off;
4434 if (this_hdr->sh_type != SHT_NOBITS)
4435 off += this_hdr->sh_size;
4438 if (this_hdr->sh_type != SHT_NOBITS)
4440 p->p_filesz += this_hdr->sh_size;
4441 /* A load section without SHF_ALLOC is something like
4442 a note section in a PT_NOTE segment. These take
4443 file space but are not loaded into memory. */
4444 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4445 p->p_memsz += this_hdr->sh_size;
4447 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4449 if (p->p_type == PT_TLS)
4450 p->p_memsz += this_hdr->sh_size;
4452 /* .tbss is special. It doesn't contribute to p_memsz of
4454 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4455 p->p_memsz += this_hdr->sh_size;
4458 if (align > p->p_align
4459 && !m->p_align_valid
4460 && (p->p_type != PT_LOAD
4461 || (abfd->flags & D_PAGED) == 0))
4465 if (!m->p_flags_valid)
4468 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4470 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4476 /* Check that all sections are in a PT_LOAD segment.
4477 Don't check funky gdb generated core files. */
4478 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4479 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4481 Elf_Internal_Shdr *this_hdr;
4485 this_hdr = &(elf_section_data(sec)->this_hdr);
4486 if (this_hdr->sh_size != 0
4487 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4489 (*_bfd_error_handler)
4490 (_("%B: section `%A' can't be allocated in segment %d"),
4492 print_segment_map (m);
4493 bfd_set_error (bfd_error_bad_value);
4499 elf_tdata (abfd)->next_file_pos = off;
4503 /* Assign file positions for the other sections. */
4506 assign_file_positions_for_non_load_sections (bfd *abfd,
4507 struct bfd_link_info *link_info)
4509 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4510 Elf_Internal_Shdr **i_shdrpp;
4511 Elf_Internal_Shdr **hdrpp;
4512 Elf_Internal_Phdr *phdrs;
4513 Elf_Internal_Phdr *p;
4514 struct elf_segment_map *m;
4515 bfd_vma filehdr_vaddr, filehdr_paddr;
4516 bfd_vma phdrs_vaddr, phdrs_paddr;
4518 unsigned int num_sec;
4522 i_shdrpp = elf_elfsections (abfd);
4523 num_sec = elf_numsections (abfd);
4524 off = elf_tdata (abfd)->next_file_pos;
4525 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4527 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4528 Elf_Internal_Shdr *hdr;
4531 if (hdr->bfd_section != NULL
4532 && (hdr->bfd_section->filepos != 0
4533 || (hdr->sh_type == SHT_NOBITS
4534 && hdr->contents == NULL)))
4535 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4536 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4538 if (hdr->sh_size != 0)
4539 ((*_bfd_error_handler)
4540 (_("%B: warning: allocated section `%s' not in segment"),
4542 (hdr->bfd_section == NULL
4544 : hdr->bfd_section->name)));
4545 /* We don't need to page align empty sections. */
4546 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4547 off += vma_page_aligned_bias (hdr->sh_addr, off,
4550 off += vma_page_aligned_bias (hdr->sh_addr, off,
4552 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4555 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4556 && hdr->bfd_section == NULL)
4557 || hdr == i_shdrpp[tdata->symtab_section]
4558 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4559 || hdr == i_shdrpp[tdata->strtab_section])
4560 hdr->sh_offset = -1;
4562 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4564 if (i == SHN_LORESERVE - 1)
4566 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4567 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4571 /* Now that we have set the section file positions, we can set up
4572 the file positions for the non PT_LOAD segments. */
4576 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4578 phdrs = elf_tdata (abfd)->phdr;
4579 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4584 if (p->p_type != PT_LOAD)
4587 if (m->includes_filehdr)
4589 filehdr_vaddr = p->p_vaddr;
4590 filehdr_paddr = p->p_paddr;
4592 if (m->includes_phdrs)
4594 phdrs_vaddr = p->p_vaddr;
4595 phdrs_paddr = p->p_paddr;
4596 if (m->includes_filehdr)
4598 phdrs_vaddr += bed->s->sizeof_ehdr;
4599 phdrs_paddr += bed->s->sizeof_ehdr;
4604 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4610 if (p->p_type != PT_LOAD
4611 && (p->p_type != PT_NOTE
4612 || bfd_get_format (abfd) != bfd_core))
4614 Elf_Internal_Shdr *hdr;
4617 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4619 sect = m->sections[m->count - 1];
4620 hdr = &elf_section_data (sect)->this_hdr;
4621 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4622 if (hdr->sh_type != SHT_NOBITS)
4623 p->p_filesz += hdr->sh_size;
4625 if (p->p_type == PT_GNU_RELRO)
4627 /* When we get here, we are copying executable
4628 or shared library. But we need to use the same
4630 Elf_Internal_Phdr *lp;
4632 for (lp = phdrs; lp < phdrs + count; ++lp)
4634 if (lp->p_type == PT_LOAD
4635 && lp->p_paddr == p->p_paddr)
4639 if (lp < phdrs + count)
4641 /* We should use p_size if it is valid since it
4642 may contain the first few bytes of the next
4643 SEC_ALLOC section. */
4644 if (m->p_size_valid)
4645 p->p_filesz = m->p_size;
4648 p->p_vaddr = lp->p_vaddr;
4649 p->p_offset = lp->p_offset;
4650 p->p_memsz = p->p_filesz;
4657 p->p_offset = m->sections[0]->filepos;
4662 if (m->includes_filehdr)
4664 p->p_vaddr = filehdr_vaddr;
4665 if (! m->p_paddr_valid)
4666 p->p_paddr = filehdr_paddr;
4668 else if (m->includes_phdrs)
4670 p->p_vaddr = phdrs_vaddr;
4671 if (! m->p_paddr_valid)
4672 p->p_paddr = phdrs_paddr;
4674 else if (p->p_type == PT_GNU_RELRO)
4676 Elf_Internal_Phdr *lp;
4678 for (lp = phdrs; lp < phdrs + count; ++lp)
4680 if (lp->p_type == PT_LOAD
4681 && lp->p_vaddr <= link_info->relro_end
4682 && lp->p_vaddr >= link_info->relro_start
4683 && (lp->p_vaddr + lp->p_filesz
4684 >= link_info->relro_end))
4688 if (lp < phdrs + count
4689 && link_info->relro_end > lp->p_vaddr)
4691 p->p_vaddr = lp->p_vaddr;
4692 p->p_paddr = lp->p_paddr;
4693 p->p_offset = lp->p_offset;
4694 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4695 p->p_memsz = p->p_filesz;
4697 p->p_flags = (lp->p_flags & ~PF_W);
4701 memset (p, 0, sizeof *p);
4702 p->p_type = PT_NULL;
4708 elf_tdata (abfd)->next_file_pos = off;
4713 /* Work out the file positions of all the sections. This is called by
4714 _bfd_elf_compute_section_file_positions. All the section sizes and
4715 VMAs must be known before this is called.
4717 Reloc sections come in two flavours: Those processed specially as
4718 "side-channel" data attached to a section to which they apply, and
4719 those that bfd doesn't process as relocations. The latter sort are
4720 stored in a normal bfd section by bfd_section_from_shdr. We don't
4721 consider the former sort here, unless they form part of the loadable
4722 image. Reloc sections not assigned here will be handled later by
4723 assign_file_positions_for_relocs.
4725 We also don't set the positions of the .symtab and .strtab here. */
4728 assign_file_positions_except_relocs (bfd *abfd,
4729 struct bfd_link_info *link_info)
4731 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4732 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4734 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4736 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4737 && bfd_get_format (abfd) != bfd_core)
4739 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4740 unsigned int num_sec = elf_numsections (abfd);
4741 Elf_Internal_Shdr **hdrpp;
4744 /* Start after the ELF header. */
4745 off = i_ehdrp->e_ehsize;
4747 /* We are not creating an executable, which means that we are
4748 not creating a program header, and that the actual order of
4749 the sections in the file is unimportant. */
4750 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4752 Elf_Internal_Shdr *hdr;
4755 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4756 && hdr->bfd_section == NULL)
4757 || i == tdata->symtab_section
4758 || i == tdata->symtab_shndx_section
4759 || i == tdata->strtab_section)
4761 hdr->sh_offset = -1;
4764 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4766 if (i == SHN_LORESERVE - 1)
4768 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4769 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4777 /* Assign file positions for the loaded sections based on the
4778 assignment of sections to segments. */
4779 if (!assign_file_positions_for_load_sections (abfd, link_info))
4782 /* And for non-load sections. */
4783 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4786 if (bed->elf_backend_modify_program_headers != NULL)
4788 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4792 /* Write out the program headers. */
4793 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4794 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4795 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4798 off = tdata->next_file_pos;
4801 /* Place the section headers. */
4802 off = align_file_position (off, 1 << bed->s->log_file_align);
4803 i_ehdrp->e_shoff = off;
4804 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4806 tdata->next_file_pos = off;
4812 prep_headers (bfd *abfd)
4814 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4815 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4816 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4817 struct elf_strtab_hash *shstrtab;
4818 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4820 i_ehdrp = elf_elfheader (abfd);
4821 i_shdrp = elf_elfsections (abfd);
4823 shstrtab = _bfd_elf_strtab_init ();
4824 if (shstrtab == NULL)
4827 elf_shstrtab (abfd) = shstrtab;
4829 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4830 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4831 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4832 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4834 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4835 i_ehdrp->e_ident[EI_DATA] =
4836 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4837 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4839 if ((abfd->flags & DYNAMIC) != 0)
4840 i_ehdrp->e_type = ET_DYN;
4841 else if ((abfd->flags & EXEC_P) != 0)
4842 i_ehdrp->e_type = ET_EXEC;
4843 else if (bfd_get_format (abfd) == bfd_core)
4844 i_ehdrp->e_type = ET_CORE;
4846 i_ehdrp->e_type = ET_REL;
4848 switch (bfd_get_arch (abfd))
4850 case bfd_arch_unknown:
4851 i_ehdrp->e_machine = EM_NONE;
4854 /* There used to be a long list of cases here, each one setting
4855 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4856 in the corresponding bfd definition. To avoid duplication,
4857 the switch was removed. Machines that need special handling
4858 can generally do it in elf_backend_final_write_processing(),
4859 unless they need the information earlier than the final write.
4860 Such need can generally be supplied by replacing the tests for
4861 e_machine with the conditions used to determine it. */
4863 i_ehdrp->e_machine = bed->elf_machine_code;
4866 i_ehdrp->e_version = bed->s->ev_current;
4867 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4869 /* No program header, for now. */
4870 i_ehdrp->e_phoff = 0;
4871 i_ehdrp->e_phentsize = 0;
4872 i_ehdrp->e_phnum = 0;
4874 /* Each bfd section is section header entry. */
4875 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4876 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4878 /* If we're building an executable, we'll need a program header table. */
4879 if (abfd->flags & EXEC_P)
4880 /* It all happens later. */
4884 i_ehdrp->e_phentsize = 0;
4886 i_ehdrp->e_phoff = 0;
4889 elf_tdata (abfd)->symtab_hdr.sh_name =
4890 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4891 elf_tdata (abfd)->strtab_hdr.sh_name =
4892 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4893 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4894 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4895 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4896 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4897 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4903 /* Assign file positions for all the reloc sections which are not part
4904 of the loadable file image. */
4907 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4910 unsigned int i, num_sec;
4911 Elf_Internal_Shdr **shdrpp;
4913 off = elf_tdata (abfd)->next_file_pos;
4915 num_sec = elf_numsections (abfd);
4916 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4918 Elf_Internal_Shdr *shdrp;
4921 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4922 && shdrp->sh_offset == -1)
4923 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4926 elf_tdata (abfd)->next_file_pos = off;
4930 _bfd_elf_write_object_contents (bfd *abfd)
4932 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4933 Elf_Internal_Ehdr *i_ehdrp;
4934 Elf_Internal_Shdr **i_shdrp;
4936 unsigned int count, num_sec;
4938 if (! abfd->output_has_begun
4939 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4942 i_shdrp = elf_elfsections (abfd);
4943 i_ehdrp = elf_elfheader (abfd);
4946 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4950 _bfd_elf_assign_file_positions_for_relocs (abfd);
4952 /* After writing the headers, we need to write the sections too... */
4953 num_sec = elf_numsections (abfd);
4954 for (count = 1; count < num_sec; count++)
4956 if (bed->elf_backend_section_processing)
4957 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4958 if (i_shdrp[count]->contents)
4960 bfd_size_type amt = i_shdrp[count]->sh_size;
4962 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4963 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4966 if (count == SHN_LORESERVE - 1)
4967 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4970 /* Write out the section header names. */
4971 if (elf_shstrtab (abfd) != NULL
4972 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4973 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4976 if (bed->elf_backend_final_write_processing)
4977 (*bed->elf_backend_final_write_processing) (abfd,
4978 elf_tdata (abfd)->linker);
4980 if (!bed->s->write_shdrs_and_ehdr (abfd))
4983 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4984 if (elf_tdata (abfd)->after_write_object_contents)
4985 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4991 _bfd_elf_write_corefile_contents (bfd *abfd)
4993 /* Hopefully this can be done just like an object file. */
4994 return _bfd_elf_write_object_contents (abfd);
4997 /* Given a section, search the header to find them. */
5000 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5002 const struct elf_backend_data *bed;
5005 if (elf_section_data (asect) != NULL
5006 && elf_section_data (asect)->this_idx != 0)
5007 return elf_section_data (asect)->this_idx;
5009 if (bfd_is_abs_section (asect))
5011 else if (bfd_is_com_section (asect))
5013 else if (bfd_is_und_section (asect))
5018 bed = get_elf_backend_data (abfd);
5019 if (bed->elf_backend_section_from_bfd_section)
5023 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5028 bfd_set_error (bfd_error_nonrepresentable_section);
5033 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5037 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5039 asymbol *asym_ptr = *asym_ptr_ptr;
5041 flagword flags = asym_ptr->flags;
5043 /* When gas creates relocations against local labels, it creates its
5044 own symbol for the section, but does put the symbol into the
5045 symbol chain, so udata is 0. When the linker is generating
5046 relocatable output, this section symbol may be for one of the
5047 input sections rather than the output section. */
5048 if (asym_ptr->udata.i == 0
5049 && (flags & BSF_SECTION_SYM)
5050 && asym_ptr->section)
5055 sec = asym_ptr->section;
5056 if (sec->owner != abfd && sec->output_section != NULL)
5057 sec = sec->output_section;
5058 if (sec->owner == abfd
5059 && (indx = sec->index) < elf_num_section_syms (abfd)
5060 && elf_section_syms (abfd)[indx] != NULL)
5061 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5064 idx = asym_ptr->udata.i;
5068 /* This case can occur when using --strip-symbol on a symbol
5069 which is used in a relocation entry. */
5070 (*_bfd_error_handler)
5071 (_("%B: symbol `%s' required but not present"),
5072 abfd, bfd_asymbol_name (asym_ptr));
5073 bfd_set_error (bfd_error_no_symbols);
5080 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5081 (long) asym_ptr, asym_ptr->name, idx, flags,
5082 elf_symbol_flags (flags));
5090 /* Rewrite program header information. */
5093 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5095 Elf_Internal_Ehdr *iehdr;
5096 struct elf_segment_map *map;
5097 struct elf_segment_map *map_first;
5098 struct elf_segment_map **pointer_to_map;
5099 Elf_Internal_Phdr *segment;
5102 unsigned int num_segments;
5103 bfd_boolean phdr_included = FALSE;
5104 bfd_vma maxpagesize;
5105 struct elf_segment_map *phdr_adjust_seg = NULL;
5106 unsigned int phdr_adjust_num = 0;
5107 const struct elf_backend_data *bed;
5109 bed = get_elf_backend_data (ibfd);
5110 iehdr = elf_elfheader (ibfd);
5113 pointer_to_map = &map_first;
5115 num_segments = elf_elfheader (ibfd)->e_phnum;
5116 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5118 /* Returns the end address of the segment + 1. */
5119 #define SEGMENT_END(segment, start) \
5120 (start + (segment->p_memsz > segment->p_filesz \
5121 ? segment->p_memsz : segment->p_filesz))
5123 #define SECTION_SIZE(section, segment) \
5124 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5125 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5126 ? section->size : 0)
5128 /* Returns TRUE if the given section is contained within
5129 the given segment. VMA addresses are compared. */
5130 #define IS_CONTAINED_BY_VMA(section, segment) \
5131 (section->vma >= segment->p_vaddr \
5132 && (section->vma + SECTION_SIZE (section, segment) \
5133 <= (SEGMENT_END (segment, segment->p_vaddr))))
5135 /* Returns TRUE if the given section is contained within
5136 the given segment. LMA addresses are compared. */
5137 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5138 (section->lma >= base \
5139 && (section->lma + SECTION_SIZE (section, segment) \
5140 <= SEGMENT_END (segment, base)))
5142 /* Handle PT_NOTE segment. */
5143 #define IS_NOTE(p, s) \
5144 (p->p_type == PT_NOTE \
5145 && elf_section_type (s) == SHT_NOTE \
5146 && (bfd_vma) s->filepos >= p->p_offset \
5147 && ((bfd_vma) s->filepos + s->size \
5148 <= p->p_offset + p->p_filesz))
5150 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5152 #define IS_COREFILE_NOTE(p, s) \
5154 && bfd_get_format (ibfd) == bfd_core \
5158 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5159 linker, which generates a PT_INTERP section with p_vaddr and
5160 p_memsz set to 0. */
5161 #define IS_SOLARIS_PT_INTERP(p, s) \
5163 && p->p_paddr == 0 \
5164 && p->p_memsz == 0 \
5165 && p->p_filesz > 0 \
5166 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5168 && (bfd_vma) s->filepos >= p->p_offset \
5169 && ((bfd_vma) s->filepos + s->size \
5170 <= p->p_offset + p->p_filesz))
5172 /* Decide if the given section should be included in the given segment.
5173 A section will be included if:
5174 1. It is within the address space of the segment -- we use the LMA
5175 if that is set for the segment and the VMA otherwise,
5176 2. It is an allocated section or a NOTE section in a PT_NOTE
5178 3. There is an output section associated with it,
5179 4. The section has not already been allocated to a previous segment.
5180 5. PT_GNU_STACK segments do not include any sections.
5181 6. PT_TLS segment includes only SHF_TLS sections.
5182 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5183 8. PT_DYNAMIC should not contain empty sections at the beginning
5184 (with the possible exception of .dynamic). */
5185 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5186 ((((segment->p_paddr \
5187 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5188 : IS_CONTAINED_BY_VMA (section, segment)) \
5189 && (section->flags & SEC_ALLOC) != 0) \
5190 || IS_NOTE (segment, section)) \
5191 && segment->p_type != PT_GNU_STACK \
5192 && (segment->p_type != PT_TLS \
5193 || (section->flags & SEC_THREAD_LOCAL)) \
5194 && (segment->p_type == PT_LOAD \
5195 || segment->p_type == PT_TLS \
5196 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5197 && (segment->p_type != PT_DYNAMIC \
5198 || SECTION_SIZE (section, segment) > 0 \
5199 || (segment->p_paddr \
5200 ? segment->p_paddr != section->lma \
5201 : segment->p_vaddr != section->vma) \
5202 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5204 && !section->segment_mark)
5206 /* If the output section of a section in the input segment is NULL,
5207 it is removed from the corresponding output segment. */
5208 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5209 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5210 && section->output_section != NULL)
5212 /* Returns TRUE iff seg1 starts after the end of seg2. */
5213 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5214 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5216 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5217 their VMA address ranges and their LMA address ranges overlap.
5218 It is possible to have overlapping VMA ranges without overlapping LMA
5219 ranges. RedBoot images for example can have both .data and .bss mapped
5220 to the same VMA range, but with the .data section mapped to a different
5222 #define SEGMENT_OVERLAPS(seg1, seg2) \
5223 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5224 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5225 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5226 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5228 /* Initialise the segment mark field. */
5229 for (section = ibfd->sections; section != NULL; section = section->next)
5230 section->segment_mark = FALSE;
5232 /* Scan through the segments specified in the program header
5233 of the input BFD. For this first scan we look for overlaps
5234 in the loadable segments. These can be created by weird
5235 parameters to objcopy. Also, fix some solaris weirdness. */
5236 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5241 Elf_Internal_Phdr *segment2;
5243 if (segment->p_type == PT_INTERP)
5244 for (section = ibfd->sections; section; section = section->next)
5245 if (IS_SOLARIS_PT_INTERP (segment, section))
5247 /* Mininal change so that the normal section to segment
5248 assignment code will work. */
5249 segment->p_vaddr = section->vma;
5253 if (segment->p_type != PT_LOAD)
5255 /* Remove PT_GNU_RELRO segment. */
5256 if (segment->p_type == PT_GNU_RELRO)
5257 segment->p_type = PT_NULL;
5261 /* Determine if this segment overlaps any previous segments. */
5262 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5264 bfd_signed_vma extra_length;
5266 if (segment2->p_type != PT_LOAD
5267 || !SEGMENT_OVERLAPS (segment, segment2))
5270 /* Merge the two segments together. */
5271 if (segment2->p_vaddr < segment->p_vaddr)
5273 /* Extend SEGMENT2 to include SEGMENT and then delete
5275 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5276 - SEGMENT_END (segment2, segment2->p_vaddr));
5278 if (extra_length > 0)
5280 segment2->p_memsz += extra_length;
5281 segment2->p_filesz += extra_length;
5284 segment->p_type = PT_NULL;
5286 /* Since we have deleted P we must restart the outer loop. */
5288 segment = elf_tdata (ibfd)->phdr;
5293 /* Extend SEGMENT to include SEGMENT2 and then delete
5295 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5296 - SEGMENT_END (segment, segment->p_vaddr));
5298 if (extra_length > 0)
5300 segment->p_memsz += extra_length;
5301 segment->p_filesz += extra_length;
5304 segment2->p_type = PT_NULL;
5309 /* The second scan attempts to assign sections to segments. */
5310 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5314 unsigned int section_count;
5315 asection **sections;
5316 asection *output_section;
5318 bfd_vma matching_lma;
5319 bfd_vma suggested_lma;
5322 asection *first_section;
5323 bfd_boolean first_matching_lma;
5324 bfd_boolean first_suggested_lma;
5326 if (segment->p_type == PT_NULL)
5329 first_section = NULL;
5330 /* Compute how many sections might be placed into this segment. */
5331 for (section = ibfd->sections, section_count = 0;
5333 section = section->next)
5335 /* Find the first section in the input segment, which may be
5336 removed from the corresponding output segment. */
5337 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5339 if (first_section == NULL)
5340 first_section = section;
5341 if (section->output_section != NULL)
5346 /* Allocate a segment map big enough to contain
5347 all of the sections we have selected. */
5348 amt = sizeof (struct elf_segment_map);
5349 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5350 map = bfd_zalloc (obfd, amt);
5354 /* Initialise the fields of the segment map. Default to
5355 using the physical address of the segment in the input BFD. */
5357 map->p_type = segment->p_type;
5358 map->p_flags = segment->p_flags;
5359 map->p_flags_valid = 1;
5361 /* If the first section in the input segment is removed, there is
5362 no need to preserve segment physical address in the corresponding
5364 if (!first_section || first_section->output_section != NULL)
5366 map->p_paddr = segment->p_paddr;
5367 map->p_paddr_valid = 1;
5370 /* Determine if this segment contains the ELF file header
5371 and if it contains the program headers themselves. */
5372 map->includes_filehdr = (segment->p_offset == 0
5373 && segment->p_filesz >= iehdr->e_ehsize);
5374 map->includes_phdrs = 0;
5376 if (!phdr_included || segment->p_type != PT_LOAD)
5378 map->includes_phdrs =
5379 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5380 && (segment->p_offset + segment->p_filesz
5381 >= ((bfd_vma) iehdr->e_phoff
5382 + iehdr->e_phnum * iehdr->e_phentsize)));
5384 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5385 phdr_included = TRUE;
5388 if (section_count == 0)
5390 /* Special segments, such as the PT_PHDR segment, may contain
5391 no sections, but ordinary, loadable segments should contain
5392 something. They are allowed by the ELF spec however, so only
5393 a warning is produced. */
5394 if (segment->p_type == PT_LOAD)
5395 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5396 " detected, is this intentional ?\n"),
5400 *pointer_to_map = map;
5401 pointer_to_map = &map->next;
5406 /* Now scan the sections in the input BFD again and attempt
5407 to add their corresponding output sections to the segment map.
5408 The problem here is how to handle an output section which has
5409 been moved (ie had its LMA changed). There are four possibilities:
5411 1. None of the sections have been moved.
5412 In this case we can continue to use the segment LMA from the
5415 2. All of the sections have been moved by the same amount.
5416 In this case we can change the segment's LMA to match the LMA
5417 of the first section.
5419 3. Some of the sections have been moved, others have not.
5420 In this case those sections which have not been moved can be
5421 placed in the current segment which will have to have its size,
5422 and possibly its LMA changed, and a new segment or segments will
5423 have to be created to contain the other sections.
5425 4. The sections have been moved, but not by the same amount.
5426 In this case we can change the segment's LMA to match the LMA
5427 of the first section and we will have to create a new segment
5428 or segments to contain the other sections.
5430 In order to save time, we allocate an array to hold the section
5431 pointers that we are interested in. As these sections get assigned
5432 to a segment, they are removed from this array. */
5434 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5435 to work around this long long bug. */
5436 sections = bfd_malloc2 (section_count, sizeof (asection *));
5437 if (sections == NULL)
5440 /* Step One: Scan for segment vs section LMA conflicts.
5441 Also add the sections to the section array allocated above.
5442 Also add the sections to the current segment. In the common
5443 case, where the sections have not been moved, this means that
5444 we have completely filled the segment, and there is nothing
5449 first_matching_lma = TRUE;
5450 first_suggested_lma = TRUE;
5452 for (section = ibfd->sections;
5454 section = section->next)
5455 if (section == first_section)
5458 for (j = 0; section != NULL; section = section->next)
5460 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5462 output_section = section->output_section;
5464 sections[j++] = section;
5466 /* The Solaris native linker always sets p_paddr to 0.
5467 We try to catch that case here, and set it to the
5468 correct value. Note - some backends require that
5469 p_paddr be left as zero. */
5470 if (segment->p_paddr == 0
5471 && segment->p_vaddr != 0
5472 && !bed->want_p_paddr_set_to_zero
5474 && output_section->lma != 0
5475 && output_section->vma == (segment->p_vaddr
5476 + (map->includes_filehdr
5479 + (map->includes_phdrs
5481 * iehdr->e_phentsize)
5483 map->p_paddr = segment->p_vaddr;
5485 /* Match up the physical address of the segment with the
5486 LMA address of the output section. */
5487 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5488 || IS_COREFILE_NOTE (segment, section)
5489 || (bed->want_p_paddr_set_to_zero
5490 && IS_CONTAINED_BY_VMA (output_section, segment)))
5492 if (first_matching_lma || output_section->lma < matching_lma)
5494 matching_lma = output_section->lma;
5495 first_matching_lma = FALSE;
5498 /* We assume that if the section fits within the segment
5499 then it does not overlap any other section within that
5501 map->sections[isec++] = output_section;
5503 else if (first_suggested_lma)
5505 suggested_lma = output_section->lma;
5506 first_suggested_lma = FALSE;
5509 if (j == section_count)
5514 BFD_ASSERT (j == section_count);
5516 /* Step Two: Adjust the physical address of the current segment,
5518 if (isec == section_count)
5520 /* All of the sections fitted within the segment as currently
5521 specified. This is the default case. Add the segment to
5522 the list of built segments and carry on to process the next
5523 program header in the input BFD. */
5524 map->count = section_count;
5525 *pointer_to_map = map;
5526 pointer_to_map = &map->next;
5528 if (!bed->want_p_paddr_set_to_zero
5529 && matching_lma != map->p_paddr
5530 && !map->includes_filehdr && !map->includes_phdrs)
5531 /* There is some padding before the first section in the
5532 segment. So, we must account for that in the output
5534 map->p_vaddr_offset = matching_lma - map->p_paddr;
5541 if (!first_matching_lma)
5543 /* At least one section fits inside the current segment.
5544 Keep it, but modify its physical address to match the
5545 LMA of the first section that fitted. */
5546 map->p_paddr = matching_lma;
5550 /* None of the sections fitted inside the current segment.
5551 Change the current segment's physical address to match
5552 the LMA of the first section. */
5553 map->p_paddr = suggested_lma;
5556 /* Offset the segment physical address from the lma
5557 to allow for space taken up by elf headers. */
5558 if (map->includes_filehdr)
5559 map->p_paddr -= iehdr->e_ehsize;
5561 if (map->includes_phdrs)
5563 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5565 /* iehdr->e_phnum is just an estimate of the number
5566 of program headers that we will need. Make a note
5567 here of the number we used and the segment we chose
5568 to hold these headers, so that we can adjust the
5569 offset when we know the correct value. */
5570 phdr_adjust_num = iehdr->e_phnum;
5571 phdr_adjust_seg = map;
5575 /* Step Three: Loop over the sections again, this time assigning
5576 those that fit to the current segment and removing them from the
5577 sections array; but making sure not to leave large gaps. Once all
5578 possible sections have been assigned to the current segment it is
5579 added to the list of built segments and if sections still remain
5580 to be assigned, a new segment is constructed before repeating
5587 first_suggested_lma = TRUE;
5589 /* Fill the current segment with sections that fit. */
5590 for (j = 0; j < section_count; j++)
5592 section = sections[j];
5594 if (section == NULL)
5597 output_section = section->output_section;
5599 BFD_ASSERT (output_section != NULL);
5601 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5602 || IS_COREFILE_NOTE (segment, section))
5604 if (map->count == 0)
5606 /* If the first section in a segment does not start at
5607 the beginning of the segment, then something is
5609 if (output_section->lma
5611 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5612 + (map->includes_phdrs
5613 ? iehdr->e_phnum * iehdr->e_phentsize
5621 prev_sec = map->sections[map->count - 1];
5623 /* If the gap between the end of the previous section
5624 and the start of this section is more than
5625 maxpagesize then we need to start a new segment. */
5626 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5628 < BFD_ALIGN (output_section->lma, maxpagesize))
5629 || (prev_sec->lma + prev_sec->size
5630 > output_section->lma))
5632 if (first_suggested_lma)
5634 suggested_lma = output_section->lma;
5635 first_suggested_lma = FALSE;
5642 map->sections[map->count++] = output_section;
5645 section->segment_mark = TRUE;
5647 else if (first_suggested_lma)
5649 suggested_lma = output_section->lma;
5650 first_suggested_lma = FALSE;
5654 BFD_ASSERT (map->count > 0);
5656 /* Add the current segment to the list of built segments. */
5657 *pointer_to_map = map;
5658 pointer_to_map = &map->next;
5660 if (isec < section_count)
5662 /* We still have not allocated all of the sections to
5663 segments. Create a new segment here, initialise it
5664 and carry on looping. */
5665 amt = sizeof (struct elf_segment_map);
5666 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5667 map = bfd_alloc (obfd, amt);
5674 /* Initialise the fields of the segment map. Set the physical
5675 physical address to the LMA of the first section that has
5676 not yet been assigned. */
5678 map->p_type = segment->p_type;
5679 map->p_flags = segment->p_flags;
5680 map->p_flags_valid = 1;
5681 map->p_paddr = suggested_lma;
5682 map->p_paddr_valid = 1;
5683 map->includes_filehdr = 0;
5684 map->includes_phdrs = 0;
5687 while (isec < section_count);
5692 /* The Solaris linker creates program headers in which all the
5693 p_paddr fields are zero. When we try to objcopy or strip such a
5694 file, we get confused. Check for this case, and if we find it
5695 reset the p_paddr_valid fields. */
5696 for (map = map_first; map != NULL; map = map->next)
5697 if (map->p_paddr != 0)
5700 for (map = map_first; map != NULL; map = map->next)
5701 map->p_paddr_valid = 0;
5703 elf_tdata (obfd)->segment_map = map_first;
5705 /* If we had to estimate the number of program headers that were
5706 going to be needed, then check our estimate now and adjust
5707 the offset if necessary. */
5708 if (phdr_adjust_seg != NULL)
5712 for (count = 0, map = map_first; map != NULL; map = map->next)
5715 if (count > phdr_adjust_num)
5716 phdr_adjust_seg->p_paddr
5717 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5722 #undef IS_CONTAINED_BY_VMA
5723 #undef IS_CONTAINED_BY_LMA
5725 #undef IS_COREFILE_NOTE
5726 #undef IS_SOLARIS_PT_INTERP
5727 #undef IS_SECTION_IN_INPUT_SEGMENT
5728 #undef INCLUDE_SECTION_IN_SEGMENT
5729 #undef SEGMENT_AFTER_SEGMENT
5730 #undef SEGMENT_OVERLAPS
5734 /* Copy ELF program header information. */
5737 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5739 Elf_Internal_Ehdr *iehdr;
5740 struct elf_segment_map *map;
5741 struct elf_segment_map *map_first;
5742 struct elf_segment_map **pointer_to_map;
5743 Elf_Internal_Phdr *segment;
5745 unsigned int num_segments;
5746 bfd_boolean phdr_included = FALSE;
5748 iehdr = elf_elfheader (ibfd);
5751 pointer_to_map = &map_first;
5753 num_segments = elf_elfheader (ibfd)->e_phnum;
5754 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5759 unsigned int section_count;
5761 Elf_Internal_Shdr *this_hdr;
5762 asection *first_section = NULL;
5763 asection *lowest_section = NULL;
5765 /* Compute how many sections are in this segment. */
5766 for (section = ibfd->sections, section_count = 0;
5768 section = section->next)
5770 this_hdr = &(elf_section_data(section)->this_hdr);
5771 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5774 first_section = lowest_section = section;
5775 if (section->lma < lowest_section->lma)
5776 lowest_section = section;
5781 /* Allocate a segment map big enough to contain
5782 all of the sections we have selected. */
5783 amt = sizeof (struct elf_segment_map);
5784 if (section_count != 0)
5785 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5786 map = bfd_zalloc (obfd, amt);
5790 /* Initialize the fields of the output segment map with the
5793 map->p_type = segment->p_type;
5794 map->p_flags = segment->p_flags;
5795 map->p_flags_valid = 1;
5796 map->p_paddr = segment->p_paddr;
5797 map->p_paddr_valid = 1;
5798 map->p_align = segment->p_align;
5799 map->p_align_valid = 1;
5800 map->p_vaddr_offset = 0;
5802 if (map->p_type == PT_GNU_RELRO
5803 && segment->p_filesz == segment->p_memsz)
5805 /* The PT_GNU_RELRO segment may contain the first a few
5806 bytes in the .got.plt section even if the whole .got.plt
5807 section isn't in the PT_GNU_RELRO segment. We won't
5808 change the size of the PT_GNU_RELRO segment. */
5809 map->p_size = segment->p_filesz;
5810 map->p_size_valid = 1;
5813 /* Determine if this segment contains the ELF file header
5814 and if it contains the program headers themselves. */
5815 map->includes_filehdr = (segment->p_offset == 0
5816 && segment->p_filesz >= iehdr->e_ehsize);
5818 map->includes_phdrs = 0;
5819 if (! phdr_included || segment->p_type != PT_LOAD)
5821 map->includes_phdrs =
5822 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5823 && (segment->p_offset + segment->p_filesz
5824 >= ((bfd_vma) iehdr->e_phoff
5825 + iehdr->e_phnum * iehdr->e_phentsize)));
5827 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5828 phdr_included = TRUE;
5831 if (!map->includes_phdrs && !map->includes_filehdr)
5832 /* There is some other padding before the first section. */
5833 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5834 - segment->p_paddr);
5836 if (section_count != 0)
5838 unsigned int isec = 0;
5840 for (section = first_section;
5842 section = section->next)
5844 this_hdr = &(elf_section_data(section)->this_hdr);
5845 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5847 map->sections[isec++] = section->output_section;
5848 if (isec == section_count)
5854 map->count = section_count;
5855 *pointer_to_map = map;
5856 pointer_to_map = &map->next;
5859 elf_tdata (obfd)->segment_map = map_first;
5863 /* Copy private BFD data. This copies or rewrites ELF program header
5867 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5869 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5870 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5873 if (elf_tdata (ibfd)->phdr == NULL)
5876 if (ibfd->xvec == obfd->xvec)
5878 /* Check to see if any sections in the input BFD
5879 covered by ELF program header have changed. */
5880 Elf_Internal_Phdr *segment;
5881 asection *section, *osec;
5882 unsigned int i, num_segments;
5883 Elf_Internal_Shdr *this_hdr;
5884 const struct elf_backend_data *bed;
5886 bed = get_elf_backend_data (ibfd);
5888 /* Regenerate the segment map if p_paddr is set to 0. */
5889 if (bed->want_p_paddr_set_to_zero)
5892 /* Initialize the segment mark field. */
5893 for (section = obfd->sections; section != NULL;
5894 section = section->next)
5895 section->segment_mark = FALSE;
5897 num_segments = elf_elfheader (ibfd)->e_phnum;
5898 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5902 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5903 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5904 which severly confuses things, so always regenerate the segment
5905 map in this case. */
5906 if (segment->p_paddr == 0
5907 && segment->p_memsz == 0
5908 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5911 for (section = ibfd->sections;
5912 section != NULL; section = section->next)
5914 /* We mark the output section so that we know it comes
5915 from the input BFD. */
5916 osec = section->output_section;
5918 osec->segment_mark = TRUE;
5920 /* Check if this section is covered by the segment. */
5921 this_hdr = &(elf_section_data(section)->this_hdr);
5922 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5924 /* FIXME: Check if its output section is changed or
5925 removed. What else do we need to check? */
5927 || section->flags != osec->flags
5928 || section->lma != osec->lma
5929 || section->vma != osec->vma
5930 || section->size != osec->size
5931 || section->rawsize != osec->rawsize
5932 || section->alignment_power != osec->alignment_power)
5938 /* Check to see if any output section do not come from the
5940 for (section = obfd->sections; section != NULL;
5941 section = section->next)
5943 if (section->segment_mark == FALSE)
5946 section->segment_mark = FALSE;
5949 return copy_elf_program_header (ibfd, obfd);
5953 return rewrite_elf_program_header (ibfd, obfd);
5956 /* Initialize private output section information from input section. */
5959 _bfd_elf_init_private_section_data (bfd *ibfd,
5963 struct bfd_link_info *link_info)
5966 Elf_Internal_Shdr *ihdr, *ohdr;
5967 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5969 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5970 || obfd->xvec->flavour != bfd_target_elf_flavour)
5973 /* Don't copy the output ELF section type from input if the
5974 output BFD section flags have been set to something different.
5975 elf_fake_sections will set ELF section type based on BFD
5977 if (elf_section_type (osec) == SHT_NULL
5978 && (osec->flags == isec->flags || !osec->flags))
5979 elf_section_type (osec) = elf_section_type (isec);
5981 /* FIXME: Is this correct for all OS/PROC specific flags? */
5982 elf_section_flags (osec) |= (elf_section_flags (isec)
5983 & (SHF_MASKOS | SHF_MASKPROC));
5985 /* Set things up for objcopy and relocatable link. The output
5986 SHT_GROUP section will have its elf_next_in_group pointing back
5987 to the input group members. Ignore linker created group section.
5988 See elfNN_ia64_object_p in elfxx-ia64.c. */
5991 if (elf_sec_group (isec) == NULL
5992 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
5994 if (elf_section_flags (isec) & SHF_GROUP)
5995 elf_section_flags (osec) |= SHF_GROUP;
5996 elf_next_in_group (osec) = elf_next_in_group (isec);
5997 elf_group_name (osec) = elf_group_name (isec);
6001 ihdr = &elf_section_data (isec)->this_hdr;
6003 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6004 don't use the output section of the linked-to section since it
6005 may be NULL at this point. */
6006 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6008 ohdr = &elf_section_data (osec)->this_hdr;
6009 ohdr->sh_flags |= SHF_LINK_ORDER;
6010 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6013 osec->use_rela_p = isec->use_rela_p;
6018 /* Copy private section information. This copies over the entsize
6019 field, and sometimes the info field. */
6022 _bfd_elf_copy_private_section_data (bfd *ibfd,
6027 Elf_Internal_Shdr *ihdr, *ohdr;
6029 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6030 || obfd->xvec->flavour != bfd_target_elf_flavour)
6033 ihdr = &elf_section_data (isec)->this_hdr;
6034 ohdr = &elf_section_data (osec)->this_hdr;
6036 ohdr->sh_entsize = ihdr->sh_entsize;
6038 if (ihdr->sh_type == SHT_SYMTAB
6039 || ihdr->sh_type == SHT_DYNSYM
6040 || ihdr->sh_type == SHT_GNU_verneed
6041 || ihdr->sh_type == SHT_GNU_verdef)
6042 ohdr->sh_info = ihdr->sh_info;
6044 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6048 /* Copy private header information. */
6051 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6055 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6056 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6059 /* Copy over private BFD data if it has not already been copied.
6060 This must be done here, rather than in the copy_private_bfd_data
6061 entry point, because the latter is called after the section
6062 contents have been set, which means that the program headers have
6063 already been worked out. */
6064 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6066 if (! copy_private_bfd_data (ibfd, obfd))
6070 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6071 but this might be wrong if we deleted the group section. */
6072 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6073 if (elf_section_type (isec) == SHT_GROUP
6074 && isec->output_section == NULL)
6076 asection *first = elf_next_in_group (isec);
6077 asection *s = first;
6080 if (s->output_section != NULL)
6082 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6083 elf_group_name (s->output_section) = NULL;
6085 s = elf_next_in_group (s);
6094 /* Copy private symbol information. If this symbol is in a section
6095 which we did not map into a BFD section, try to map the section
6096 index correctly. We use special macro definitions for the mapped
6097 section indices; these definitions are interpreted by the
6098 swap_out_syms function. */
6100 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6101 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6102 #define MAP_STRTAB (SHN_HIOS + 3)
6103 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6104 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6107 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6112 elf_symbol_type *isym, *osym;
6114 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6115 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6118 isym = elf_symbol_from (ibfd, isymarg);
6119 osym = elf_symbol_from (obfd, osymarg);
6122 && isym->internal_elf_sym.st_shndx != 0
6124 && bfd_is_abs_section (isym->symbol.section))
6128 shndx = isym->internal_elf_sym.st_shndx;
6129 if (shndx == elf_onesymtab (ibfd))
6130 shndx = MAP_ONESYMTAB;
6131 else if (shndx == elf_dynsymtab (ibfd))
6132 shndx = MAP_DYNSYMTAB;
6133 else if (shndx == elf_tdata (ibfd)->strtab_section)
6135 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6136 shndx = MAP_SHSTRTAB;
6137 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6138 shndx = MAP_SYM_SHNDX;
6139 osym->internal_elf_sym.st_shndx = shndx;
6145 /* Swap out the symbols. */
6148 swap_out_syms (bfd *abfd,
6149 struct bfd_strtab_hash **sttp,
6152 const struct elf_backend_data *bed;
6155 struct bfd_strtab_hash *stt;
6156 Elf_Internal_Shdr *symtab_hdr;
6157 Elf_Internal_Shdr *symtab_shndx_hdr;
6158 Elf_Internal_Shdr *symstrtab_hdr;
6159 bfd_byte *outbound_syms;
6160 bfd_byte *outbound_shndx;
6163 bfd_boolean name_local_sections;
6165 if (!elf_map_symbols (abfd))
6168 /* Dump out the symtabs. */
6169 stt = _bfd_elf_stringtab_init ();
6173 bed = get_elf_backend_data (abfd);
6174 symcount = bfd_get_symcount (abfd);
6175 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6176 symtab_hdr->sh_type = SHT_SYMTAB;
6177 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6178 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6179 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6180 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
6182 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6183 symstrtab_hdr->sh_type = SHT_STRTAB;
6185 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6186 if (outbound_syms == NULL)
6188 _bfd_stringtab_free (stt);
6191 symtab_hdr->contents = outbound_syms;
6193 outbound_shndx = NULL;
6194 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6195 if (symtab_shndx_hdr->sh_name != 0)
6197 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6198 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6199 sizeof (Elf_External_Sym_Shndx));
6200 if (outbound_shndx == NULL)
6202 _bfd_stringtab_free (stt);
6206 symtab_shndx_hdr->contents = outbound_shndx;
6207 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6208 symtab_shndx_hdr->sh_size = amt;
6209 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6210 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6213 /* Now generate the data (for "contents"). */
6215 /* Fill in zeroth symbol and swap it out. */
6216 Elf_Internal_Sym sym;
6222 sym.st_shndx = SHN_UNDEF;
6223 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6224 outbound_syms += bed->s->sizeof_sym;
6225 if (outbound_shndx != NULL)
6226 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6230 = (bed->elf_backend_name_local_section_symbols
6231 && bed->elf_backend_name_local_section_symbols (abfd));
6233 syms = bfd_get_outsymbols (abfd);
6234 for (idx = 0; idx < symcount; idx++)
6236 Elf_Internal_Sym sym;
6237 bfd_vma value = syms[idx]->value;
6238 elf_symbol_type *type_ptr;
6239 flagword flags = syms[idx]->flags;
6242 if (!name_local_sections
6243 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6245 /* Local section symbols have no name. */
6250 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6253 if (sym.st_name == (unsigned long) -1)
6255 _bfd_stringtab_free (stt);
6260 type_ptr = elf_symbol_from (abfd, syms[idx]);
6262 if ((flags & BSF_SECTION_SYM) == 0
6263 && bfd_is_com_section (syms[idx]->section))
6265 /* ELF common symbols put the alignment into the `value' field,
6266 and the size into the `size' field. This is backwards from
6267 how BFD handles it, so reverse it here. */
6268 sym.st_size = value;
6269 if (type_ptr == NULL
6270 || type_ptr->internal_elf_sym.st_value == 0)
6271 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6273 sym.st_value = type_ptr->internal_elf_sym.st_value;
6274 sym.st_shndx = _bfd_elf_section_from_bfd_section
6275 (abfd, syms[idx]->section);
6279 asection *sec = syms[idx]->section;
6282 if (sec->output_section)
6284 value += sec->output_offset;
6285 sec = sec->output_section;
6288 /* Don't add in the section vma for relocatable output. */
6289 if (! relocatable_p)
6291 sym.st_value = value;
6292 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6294 if (bfd_is_abs_section (sec)
6296 && type_ptr->internal_elf_sym.st_shndx != 0)
6298 /* This symbol is in a real ELF section which we did
6299 not create as a BFD section. Undo the mapping done
6300 by copy_private_symbol_data. */
6301 shndx = type_ptr->internal_elf_sym.st_shndx;
6305 shndx = elf_onesymtab (abfd);
6308 shndx = elf_dynsymtab (abfd);
6311 shndx = elf_tdata (abfd)->strtab_section;
6314 shndx = elf_tdata (abfd)->shstrtab_section;
6317 shndx = elf_tdata (abfd)->symtab_shndx_section;
6325 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6331 /* Writing this would be a hell of a lot easier if
6332 we had some decent documentation on bfd, and
6333 knew what to expect of the library, and what to
6334 demand of applications. For example, it
6335 appears that `objcopy' might not set the
6336 section of a symbol to be a section that is
6337 actually in the output file. */
6338 sec2 = bfd_get_section_by_name (abfd, sec->name);
6341 _bfd_error_handler (_("\
6342 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6343 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6345 bfd_set_error (bfd_error_invalid_operation);
6346 _bfd_stringtab_free (stt);
6350 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6351 BFD_ASSERT (shndx != -1);
6355 sym.st_shndx = shndx;
6358 if ((flags & BSF_THREAD_LOCAL) != 0)
6360 else if ((flags & BSF_FUNCTION) != 0)
6362 else if ((flags & BSF_OBJECT) != 0)
6364 else if ((flags & BSF_RELC) != 0)
6366 else if ((flags & BSF_SRELC) != 0)
6371 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6374 /* Processor-specific types. */
6375 if (type_ptr != NULL
6376 && bed->elf_backend_get_symbol_type)
6377 type = ((*bed->elf_backend_get_symbol_type)
6378 (&type_ptr->internal_elf_sym, type));
6380 if (flags & BSF_SECTION_SYM)
6382 if (flags & BSF_GLOBAL)
6383 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6385 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6387 else if (bfd_is_com_section (syms[idx]->section))
6389 #ifdef USE_STT_COMMON
6390 if (type == STT_OBJECT)
6391 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6394 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6397 else if (bfd_is_und_section (syms[idx]->section))
6398 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6402 else if (flags & BSF_FILE)
6403 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6406 int bind = STB_LOCAL;
6408 if (flags & BSF_LOCAL)
6410 else if (flags & BSF_WEAK)
6412 else if (flags & BSF_GLOBAL)
6415 sym.st_info = ELF_ST_INFO (bind, type);
6418 if (type_ptr != NULL)
6419 sym.st_other = type_ptr->internal_elf_sym.st_other;
6423 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6424 outbound_syms += bed->s->sizeof_sym;
6425 if (outbound_shndx != NULL)
6426 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6430 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6431 symstrtab_hdr->sh_type = SHT_STRTAB;
6433 symstrtab_hdr->sh_flags = 0;
6434 symstrtab_hdr->sh_addr = 0;
6435 symstrtab_hdr->sh_entsize = 0;
6436 symstrtab_hdr->sh_link = 0;
6437 symstrtab_hdr->sh_info = 0;
6438 symstrtab_hdr->sh_addralign = 1;
6443 /* Return the number of bytes required to hold the symtab vector.
6445 Note that we base it on the count plus 1, since we will null terminate
6446 the vector allocated based on this size. However, the ELF symbol table
6447 always has a dummy entry as symbol #0, so it ends up even. */
6450 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6454 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6456 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6457 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6459 symtab_size -= sizeof (asymbol *);
6465 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6469 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6471 if (elf_dynsymtab (abfd) == 0)
6473 bfd_set_error (bfd_error_invalid_operation);
6477 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6478 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6480 symtab_size -= sizeof (asymbol *);
6486 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6489 return (asect->reloc_count + 1) * sizeof (arelent *);
6492 /* Canonicalize the relocs. */
6495 _bfd_elf_canonicalize_reloc (bfd *abfd,
6502 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6504 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6507 tblptr = section->relocation;
6508 for (i = 0; i < section->reloc_count; i++)
6509 *relptr++ = tblptr++;
6513 return section->reloc_count;
6517 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6520 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6523 bfd_get_symcount (abfd) = symcount;
6528 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6529 asymbol **allocation)
6531 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6532 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6535 bfd_get_dynamic_symcount (abfd) = symcount;
6539 /* Return the size required for the dynamic reloc entries. Any loadable
6540 section that was actually installed in the BFD, and has type SHT_REL
6541 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6542 dynamic reloc section. */
6545 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6550 if (elf_dynsymtab (abfd) == 0)
6552 bfd_set_error (bfd_error_invalid_operation);
6556 ret = sizeof (arelent *);
6557 for (s = abfd->sections; s != NULL; s = s->next)
6558 if ((s->flags & SEC_LOAD) != 0
6559 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6560 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6561 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6562 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6563 * sizeof (arelent *));
6568 /* Canonicalize the dynamic relocation entries. Note that we return the
6569 dynamic relocations as a single block, although they are actually
6570 associated with particular sections; the interface, which was
6571 designed for SunOS style shared libraries, expects that there is only
6572 one set of dynamic relocs. Any loadable section that was actually
6573 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6574 dynamic symbol table, is considered to be a dynamic reloc section. */
6577 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6581 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6585 if (elf_dynsymtab (abfd) == 0)
6587 bfd_set_error (bfd_error_invalid_operation);
6591 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6593 for (s = abfd->sections; s != NULL; s = s->next)
6595 if ((s->flags & SEC_LOAD) != 0
6596 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6597 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6598 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6603 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6605 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6607 for (i = 0; i < count; i++)
6618 /* Read in the version information. */
6621 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6623 bfd_byte *contents = NULL;
6624 unsigned int freeidx = 0;
6626 if (elf_dynverref (abfd) != 0)
6628 Elf_Internal_Shdr *hdr;
6629 Elf_External_Verneed *everneed;
6630 Elf_Internal_Verneed *iverneed;
6632 bfd_byte *contents_end;
6634 hdr = &elf_tdata (abfd)->dynverref_hdr;
6636 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6637 sizeof (Elf_Internal_Verneed));
6638 if (elf_tdata (abfd)->verref == NULL)
6641 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6643 contents = bfd_malloc (hdr->sh_size);
6644 if (contents == NULL)
6646 error_return_verref:
6647 elf_tdata (abfd)->verref = NULL;
6648 elf_tdata (abfd)->cverrefs = 0;
6651 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6652 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6653 goto error_return_verref;
6655 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6656 goto error_return_verref;
6658 BFD_ASSERT (sizeof (Elf_External_Verneed)
6659 == sizeof (Elf_External_Vernaux));
6660 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6661 everneed = (Elf_External_Verneed *) contents;
6662 iverneed = elf_tdata (abfd)->verref;
6663 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6665 Elf_External_Vernaux *evernaux;
6666 Elf_Internal_Vernaux *ivernaux;
6669 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6671 iverneed->vn_bfd = abfd;
6673 iverneed->vn_filename =
6674 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6676 if (iverneed->vn_filename == NULL)
6677 goto error_return_verref;
6679 if (iverneed->vn_cnt == 0)
6680 iverneed->vn_auxptr = NULL;
6683 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6684 sizeof (Elf_Internal_Vernaux));
6685 if (iverneed->vn_auxptr == NULL)
6686 goto error_return_verref;
6689 if (iverneed->vn_aux
6690 > (size_t) (contents_end - (bfd_byte *) everneed))
6691 goto error_return_verref;
6693 evernaux = ((Elf_External_Vernaux *)
6694 ((bfd_byte *) everneed + iverneed->vn_aux));
6695 ivernaux = iverneed->vn_auxptr;
6696 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6698 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6700 ivernaux->vna_nodename =
6701 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6702 ivernaux->vna_name);
6703 if (ivernaux->vna_nodename == NULL)
6704 goto error_return_verref;
6706 if (j + 1 < iverneed->vn_cnt)
6707 ivernaux->vna_nextptr = ivernaux + 1;
6709 ivernaux->vna_nextptr = NULL;
6711 if (ivernaux->vna_next
6712 > (size_t) (contents_end - (bfd_byte *) evernaux))
6713 goto error_return_verref;
6715 evernaux = ((Elf_External_Vernaux *)
6716 ((bfd_byte *) evernaux + ivernaux->vna_next));
6718 if (ivernaux->vna_other > freeidx)
6719 freeidx = ivernaux->vna_other;
6722 if (i + 1 < hdr->sh_info)
6723 iverneed->vn_nextref = iverneed + 1;
6725 iverneed->vn_nextref = NULL;
6727 if (iverneed->vn_next
6728 > (size_t) (contents_end - (bfd_byte *) everneed))
6729 goto error_return_verref;
6731 everneed = ((Elf_External_Verneed *)
6732 ((bfd_byte *) everneed + iverneed->vn_next));
6739 if (elf_dynverdef (abfd) != 0)
6741 Elf_Internal_Shdr *hdr;
6742 Elf_External_Verdef *everdef;
6743 Elf_Internal_Verdef *iverdef;
6744 Elf_Internal_Verdef *iverdefarr;
6745 Elf_Internal_Verdef iverdefmem;
6747 unsigned int maxidx;
6748 bfd_byte *contents_end_def, *contents_end_aux;
6750 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6752 contents = bfd_malloc (hdr->sh_size);
6753 if (contents == NULL)
6755 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6756 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6759 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6762 BFD_ASSERT (sizeof (Elf_External_Verdef)
6763 >= sizeof (Elf_External_Verdaux));
6764 contents_end_def = contents + hdr->sh_size
6765 - sizeof (Elf_External_Verdef);
6766 contents_end_aux = contents + hdr->sh_size
6767 - sizeof (Elf_External_Verdaux);
6769 /* We know the number of entries in the section but not the maximum
6770 index. Therefore we have to run through all entries and find
6772 everdef = (Elf_External_Verdef *) contents;
6774 for (i = 0; i < hdr->sh_info; ++i)
6776 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6778 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6779 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6781 if (iverdefmem.vd_next
6782 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6785 everdef = ((Elf_External_Verdef *)
6786 ((bfd_byte *) everdef + iverdefmem.vd_next));
6789 if (default_imported_symver)
6791 if (freeidx > maxidx)
6796 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6797 sizeof (Elf_Internal_Verdef));
6798 if (elf_tdata (abfd)->verdef == NULL)
6801 elf_tdata (abfd)->cverdefs = maxidx;
6803 everdef = (Elf_External_Verdef *) contents;
6804 iverdefarr = elf_tdata (abfd)->verdef;
6805 for (i = 0; i < hdr->sh_info; i++)
6807 Elf_External_Verdaux *everdaux;
6808 Elf_Internal_Verdaux *iverdaux;
6811 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6813 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6815 error_return_verdef:
6816 elf_tdata (abfd)->verdef = NULL;
6817 elf_tdata (abfd)->cverdefs = 0;
6821 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6822 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6824 iverdef->vd_bfd = abfd;
6826 if (iverdef->vd_cnt == 0)
6827 iverdef->vd_auxptr = NULL;
6830 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6831 sizeof (Elf_Internal_Verdaux));
6832 if (iverdef->vd_auxptr == NULL)
6833 goto error_return_verdef;
6837 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6838 goto error_return_verdef;
6840 everdaux = ((Elf_External_Verdaux *)
6841 ((bfd_byte *) everdef + iverdef->vd_aux));
6842 iverdaux = iverdef->vd_auxptr;
6843 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6845 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6847 iverdaux->vda_nodename =
6848 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6849 iverdaux->vda_name);
6850 if (iverdaux->vda_nodename == NULL)
6851 goto error_return_verdef;
6853 if (j + 1 < iverdef->vd_cnt)
6854 iverdaux->vda_nextptr = iverdaux + 1;
6856 iverdaux->vda_nextptr = NULL;
6858 if (iverdaux->vda_next
6859 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6860 goto error_return_verdef;
6862 everdaux = ((Elf_External_Verdaux *)
6863 ((bfd_byte *) everdaux + iverdaux->vda_next));
6866 if (iverdef->vd_cnt)
6867 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6869 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6870 iverdef->vd_nextdef = iverdef + 1;
6872 iverdef->vd_nextdef = NULL;
6874 everdef = ((Elf_External_Verdef *)
6875 ((bfd_byte *) everdef + iverdef->vd_next));
6881 else if (default_imported_symver)
6888 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6889 sizeof (Elf_Internal_Verdef));
6890 if (elf_tdata (abfd)->verdef == NULL)
6893 elf_tdata (abfd)->cverdefs = freeidx;
6896 /* Create a default version based on the soname. */
6897 if (default_imported_symver)
6899 Elf_Internal_Verdef *iverdef;
6900 Elf_Internal_Verdaux *iverdaux;
6902 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6904 iverdef->vd_version = VER_DEF_CURRENT;
6905 iverdef->vd_flags = 0;
6906 iverdef->vd_ndx = freeidx;
6907 iverdef->vd_cnt = 1;
6909 iverdef->vd_bfd = abfd;
6911 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6912 if (iverdef->vd_nodename == NULL)
6913 goto error_return_verdef;
6914 iverdef->vd_nextdef = NULL;
6915 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6916 if (iverdef->vd_auxptr == NULL)
6917 goto error_return_verdef;
6919 iverdaux = iverdef->vd_auxptr;
6920 iverdaux->vda_nodename = iverdef->vd_nodename;
6921 iverdaux->vda_nextptr = NULL;
6927 if (contents != NULL)
6933 _bfd_elf_make_empty_symbol (bfd *abfd)
6935 elf_symbol_type *newsym;
6936 bfd_size_type amt = sizeof (elf_symbol_type);
6938 newsym = bfd_zalloc (abfd, amt);
6943 newsym->symbol.the_bfd = abfd;
6944 return &newsym->symbol;
6949 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6953 bfd_symbol_info (symbol, ret);
6956 /* Return whether a symbol name implies a local symbol. Most targets
6957 use this function for the is_local_label_name entry point, but some
6961 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6964 /* Normal local symbols start with ``.L''. */
6965 if (name[0] == '.' && name[1] == 'L')
6968 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6969 DWARF debugging symbols starting with ``..''. */
6970 if (name[0] == '.' && name[1] == '.')
6973 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6974 emitting DWARF debugging output. I suspect this is actually a
6975 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6976 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6977 underscore to be emitted on some ELF targets). For ease of use,
6978 we treat such symbols as local. */
6979 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6986 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6987 asymbol *symbol ATTRIBUTE_UNUSED)
6994 _bfd_elf_set_arch_mach (bfd *abfd,
6995 enum bfd_architecture arch,
6996 unsigned long machine)
6998 /* If this isn't the right architecture for this backend, and this
6999 isn't the generic backend, fail. */
7000 if (arch != get_elf_backend_data (abfd)->arch
7001 && arch != bfd_arch_unknown
7002 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7005 return bfd_default_set_arch_mach (abfd, arch, machine);
7008 /* Find the function to a particular section and offset,
7009 for error reporting. */
7012 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7016 const char **filename_ptr,
7017 const char **functionname_ptr)
7019 const char *filename;
7020 asymbol *func, *file;
7023 /* ??? Given multiple file symbols, it is impossible to reliably
7024 choose the right file name for global symbols. File symbols are
7025 local symbols, and thus all file symbols must sort before any
7026 global symbols. The ELF spec may be interpreted to say that a
7027 file symbol must sort before other local symbols, but currently
7028 ld -r doesn't do this. So, for ld -r output, it is possible to
7029 make a better choice of file name for local symbols by ignoring
7030 file symbols appearing after a given local symbol. */
7031 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7037 state = nothing_seen;
7039 for (p = symbols; *p != NULL; p++)
7043 q = (elf_symbol_type *) *p;
7045 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7051 if (state == symbol_seen)
7052 state = file_after_symbol_seen;
7056 if (bfd_get_section (&q->symbol) == section
7057 && q->symbol.value >= low_func
7058 && q->symbol.value <= offset)
7060 func = (asymbol *) q;
7061 low_func = q->symbol.value;
7064 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7065 || state != file_after_symbol_seen))
7066 filename = bfd_asymbol_name (file);
7070 if (state == nothing_seen)
7071 state = symbol_seen;
7078 *filename_ptr = filename;
7079 if (functionname_ptr)
7080 *functionname_ptr = bfd_asymbol_name (func);
7085 /* Find the nearest line to a particular section and offset,
7086 for error reporting. */
7089 _bfd_elf_find_nearest_line (bfd *abfd,
7093 const char **filename_ptr,
7094 const char **functionname_ptr,
7095 unsigned int *line_ptr)
7099 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7100 filename_ptr, functionname_ptr,
7103 if (!*functionname_ptr)
7104 elf_find_function (abfd, section, symbols, offset,
7105 *filename_ptr ? NULL : filename_ptr,
7111 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7112 filename_ptr, functionname_ptr,
7114 &elf_tdata (abfd)->dwarf2_find_line_info))
7116 if (!*functionname_ptr)
7117 elf_find_function (abfd, section, symbols, offset,
7118 *filename_ptr ? NULL : filename_ptr,
7124 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7125 &found, filename_ptr,
7126 functionname_ptr, line_ptr,
7127 &elf_tdata (abfd)->line_info))
7129 if (found && (*functionname_ptr || *line_ptr))
7132 if (symbols == NULL)
7135 if (! elf_find_function (abfd, section, symbols, offset,
7136 filename_ptr, functionname_ptr))
7143 /* Find the line for a symbol. */
7146 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7147 const char **filename_ptr, unsigned int *line_ptr)
7149 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7150 filename_ptr, line_ptr, 0,
7151 &elf_tdata (abfd)->dwarf2_find_line_info);
7154 /* After a call to bfd_find_nearest_line, successive calls to
7155 bfd_find_inliner_info can be used to get source information about
7156 each level of function inlining that terminated at the address
7157 passed to bfd_find_nearest_line. Currently this is only supported
7158 for DWARF2 with appropriate DWARF3 extensions. */
7161 _bfd_elf_find_inliner_info (bfd *abfd,
7162 const char **filename_ptr,
7163 const char **functionname_ptr,
7164 unsigned int *line_ptr)
7167 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7168 functionname_ptr, line_ptr,
7169 & elf_tdata (abfd)->dwarf2_find_line_info);
7174 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7176 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7177 int ret = bed->s->sizeof_ehdr;
7179 if (!info->relocatable)
7181 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7183 if (phdr_size == (bfd_size_type) -1)
7185 struct elf_segment_map *m;
7188 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7189 phdr_size += bed->s->sizeof_phdr;
7192 phdr_size = get_program_header_size (abfd, info);
7195 elf_tdata (abfd)->program_header_size = phdr_size;
7203 _bfd_elf_set_section_contents (bfd *abfd,
7205 const void *location,
7207 bfd_size_type count)
7209 Elf_Internal_Shdr *hdr;
7212 if (! abfd->output_has_begun
7213 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7216 hdr = &elf_section_data (section)->this_hdr;
7217 pos = hdr->sh_offset + offset;
7218 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7219 || bfd_bwrite (location, count, abfd) != count)
7226 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7227 arelent *cache_ptr ATTRIBUTE_UNUSED,
7228 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7233 /* Try to convert a non-ELF reloc into an ELF one. */
7236 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7238 /* Check whether we really have an ELF howto. */
7240 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7242 bfd_reloc_code_real_type code;
7243 reloc_howto_type *howto;
7245 /* Alien reloc: Try to determine its type to replace it with an
7246 equivalent ELF reloc. */
7248 if (areloc->howto->pc_relative)
7250 switch (areloc->howto->bitsize)
7253 code = BFD_RELOC_8_PCREL;
7256 code = BFD_RELOC_12_PCREL;
7259 code = BFD_RELOC_16_PCREL;
7262 code = BFD_RELOC_24_PCREL;
7265 code = BFD_RELOC_32_PCREL;
7268 code = BFD_RELOC_64_PCREL;
7274 howto = bfd_reloc_type_lookup (abfd, code);
7276 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7278 if (howto->pcrel_offset)
7279 areloc->addend += areloc->address;
7281 areloc->addend -= areloc->address; /* addend is unsigned!! */
7286 switch (areloc->howto->bitsize)
7292 code = BFD_RELOC_14;
7295 code = BFD_RELOC_16;
7298 code = BFD_RELOC_26;
7301 code = BFD_RELOC_32;
7304 code = BFD_RELOC_64;
7310 howto = bfd_reloc_type_lookup (abfd, code);
7314 areloc->howto = howto;
7322 (*_bfd_error_handler)
7323 (_("%B: unsupported relocation type %s"),
7324 abfd, areloc->howto->name);
7325 bfd_set_error (bfd_error_bad_value);
7330 _bfd_elf_close_and_cleanup (bfd *abfd)
7332 if (bfd_get_format (abfd) == bfd_object)
7334 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7335 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7336 _bfd_dwarf2_cleanup_debug_info (abfd);
7339 return _bfd_generic_close_and_cleanup (abfd);
7342 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7343 in the relocation's offset. Thus we cannot allow any sort of sanity
7344 range-checking to interfere. There is nothing else to do in processing
7347 bfd_reloc_status_type
7348 _bfd_elf_rel_vtable_reloc_fn
7349 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7350 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7351 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7352 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7354 return bfd_reloc_ok;
7357 /* Elf core file support. Much of this only works on native
7358 toolchains, since we rely on knowing the
7359 machine-dependent procfs structure in order to pick
7360 out details about the corefile. */
7362 #ifdef HAVE_SYS_PROCFS_H
7363 # include <sys/procfs.h>
7366 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7369 elfcore_make_pid (bfd *abfd)
7371 return ((elf_tdata (abfd)->core_lwpid << 16)
7372 + (elf_tdata (abfd)->core_pid));
7375 /* If there isn't a section called NAME, make one, using
7376 data from SECT. Note, this function will generate a
7377 reference to NAME, so you shouldn't deallocate or
7381 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7385 if (bfd_get_section_by_name (abfd, name) != NULL)
7388 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7392 sect2->size = sect->size;
7393 sect2->filepos = sect->filepos;
7394 sect2->alignment_power = sect->alignment_power;
7398 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7399 actually creates up to two pseudosections:
7400 - For the single-threaded case, a section named NAME, unless
7401 such a section already exists.
7402 - For the multi-threaded case, a section named "NAME/PID", where
7403 PID is elfcore_make_pid (abfd).
7404 Both pseudosections have identical contents. */
7406 _bfd_elfcore_make_pseudosection (bfd *abfd,
7412 char *threaded_name;
7416 /* Build the section name. */
7418 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7419 len = strlen (buf) + 1;
7420 threaded_name = bfd_alloc (abfd, len);
7421 if (threaded_name == NULL)
7423 memcpy (threaded_name, buf, len);
7425 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7430 sect->filepos = filepos;
7431 sect->alignment_power = 2;
7433 return elfcore_maybe_make_sect (abfd, name, sect);
7436 /* prstatus_t exists on:
7438 linux 2.[01] + glibc
7442 #if defined (HAVE_PRSTATUS_T)
7445 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7450 if (note->descsz == sizeof (prstatus_t))
7454 size = sizeof (prstat.pr_reg);
7455 offset = offsetof (prstatus_t, pr_reg);
7456 memcpy (&prstat, note->descdata, sizeof (prstat));
7458 /* Do not overwrite the core signal if it
7459 has already been set by another thread. */
7460 if (elf_tdata (abfd)->core_signal == 0)
7461 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7462 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7464 /* pr_who exists on:
7467 pr_who doesn't exist on:
7470 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7471 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7474 #if defined (HAVE_PRSTATUS32_T)
7475 else if (note->descsz == sizeof (prstatus32_t))
7477 /* 64-bit host, 32-bit corefile */
7478 prstatus32_t prstat;
7480 size = sizeof (prstat.pr_reg);
7481 offset = offsetof (prstatus32_t, pr_reg);
7482 memcpy (&prstat, note->descdata, sizeof (prstat));
7484 /* Do not overwrite the core signal if it
7485 has already been set by another thread. */
7486 if (elf_tdata (abfd)->core_signal == 0)
7487 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7488 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7490 /* pr_who exists on:
7493 pr_who doesn't exist on:
7496 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7497 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7500 #endif /* HAVE_PRSTATUS32_T */
7503 /* Fail - we don't know how to handle any other
7504 note size (ie. data object type). */
7508 /* Make a ".reg/999" section and a ".reg" section. */
7509 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7510 size, note->descpos + offset);
7512 #endif /* defined (HAVE_PRSTATUS_T) */
7514 /* Create a pseudosection containing the exact contents of NOTE. */
7516 elfcore_make_note_pseudosection (bfd *abfd,
7518 Elf_Internal_Note *note)
7520 return _bfd_elfcore_make_pseudosection (abfd, name,
7521 note->descsz, note->descpos);
7524 /* There isn't a consistent prfpregset_t across platforms,
7525 but it doesn't matter, because we don't have to pick this
7526 data structure apart. */
7529 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7531 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7534 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7535 type of NT_PRXFPREG. Just include the whole note's contents
7539 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7541 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7545 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7547 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7551 #if defined (HAVE_PRPSINFO_T)
7552 typedef prpsinfo_t elfcore_psinfo_t;
7553 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7554 typedef prpsinfo32_t elfcore_psinfo32_t;
7558 #if defined (HAVE_PSINFO_T)
7559 typedef psinfo_t elfcore_psinfo_t;
7560 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7561 typedef psinfo32_t elfcore_psinfo32_t;
7565 /* return a malloc'ed copy of a string at START which is at
7566 most MAX bytes long, possibly without a terminating '\0'.
7567 the copy will always have a terminating '\0'. */
7570 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7573 char *end = memchr (start, '\0', max);
7581 dups = bfd_alloc (abfd, len + 1);
7585 memcpy (dups, start, len);
7591 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7593 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7595 if (note->descsz == sizeof (elfcore_psinfo_t))
7597 elfcore_psinfo_t psinfo;
7599 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7601 elf_tdata (abfd)->core_program
7602 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7603 sizeof (psinfo.pr_fname));
7605 elf_tdata (abfd)->core_command
7606 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7607 sizeof (psinfo.pr_psargs));
7609 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7610 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7612 /* 64-bit host, 32-bit corefile */
7613 elfcore_psinfo32_t psinfo;
7615 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7617 elf_tdata (abfd)->core_program
7618 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7619 sizeof (psinfo.pr_fname));
7621 elf_tdata (abfd)->core_command
7622 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7623 sizeof (psinfo.pr_psargs));
7629 /* Fail - we don't know how to handle any other
7630 note size (ie. data object type). */
7634 /* Note that for some reason, a spurious space is tacked
7635 onto the end of the args in some (at least one anyway)
7636 implementations, so strip it off if it exists. */
7639 char *command = elf_tdata (abfd)->core_command;
7640 int n = strlen (command);
7642 if (0 < n && command[n - 1] == ' ')
7643 command[n - 1] = '\0';
7648 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7650 #if defined (HAVE_PSTATUS_T)
7652 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7654 if (note->descsz == sizeof (pstatus_t)
7655 #if defined (HAVE_PXSTATUS_T)
7656 || note->descsz == sizeof (pxstatus_t)
7662 memcpy (&pstat, note->descdata, sizeof (pstat));
7664 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7666 #if defined (HAVE_PSTATUS32_T)
7667 else if (note->descsz == sizeof (pstatus32_t))
7669 /* 64-bit host, 32-bit corefile */
7672 memcpy (&pstat, note->descdata, sizeof (pstat));
7674 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7677 /* Could grab some more details from the "representative"
7678 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7679 NT_LWPSTATUS note, presumably. */
7683 #endif /* defined (HAVE_PSTATUS_T) */
7685 #if defined (HAVE_LWPSTATUS_T)
7687 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7689 lwpstatus_t lwpstat;
7695 if (note->descsz != sizeof (lwpstat)
7696 #if defined (HAVE_LWPXSTATUS_T)
7697 && note->descsz != sizeof (lwpxstatus_t)
7702 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7704 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7705 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7707 /* Make a ".reg/999" section. */
7709 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7710 len = strlen (buf) + 1;
7711 name = bfd_alloc (abfd, len);
7714 memcpy (name, buf, len);
7716 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7720 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7721 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7722 sect->filepos = note->descpos
7723 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7726 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7727 sect->size = sizeof (lwpstat.pr_reg);
7728 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7731 sect->alignment_power = 2;
7733 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7736 /* Make a ".reg2/999" section */
7738 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7739 len = strlen (buf) + 1;
7740 name = bfd_alloc (abfd, len);
7743 memcpy (name, buf, len);
7745 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7749 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7750 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7751 sect->filepos = note->descpos
7752 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7755 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7756 sect->size = sizeof (lwpstat.pr_fpreg);
7757 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7760 sect->alignment_power = 2;
7762 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7764 #endif /* defined (HAVE_LWPSTATUS_T) */
7767 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7774 int is_active_thread;
7777 if (note->descsz < 728)
7780 if (! CONST_STRNEQ (note->namedata, "win32"))
7783 type = bfd_get_32 (abfd, note->descdata);
7787 case 1 /* NOTE_INFO_PROCESS */:
7788 /* FIXME: need to add ->core_command. */
7789 /* process_info.pid */
7790 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7791 /* process_info.signal */
7792 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7795 case 2 /* NOTE_INFO_THREAD */:
7796 /* Make a ".reg/999" section. */
7797 /* thread_info.tid */
7798 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7800 len = strlen (buf) + 1;
7801 name = bfd_alloc (abfd, len);
7805 memcpy (name, buf, len);
7807 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7811 /* sizeof (thread_info.thread_context) */
7813 /* offsetof (thread_info.thread_context) */
7814 sect->filepos = note->descpos + 12;
7815 sect->alignment_power = 2;
7817 /* thread_info.is_active_thread */
7818 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7820 if (is_active_thread)
7821 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7825 case 3 /* NOTE_INFO_MODULE */:
7826 /* Make a ".module/xxxxxxxx" section. */
7827 /* module_info.base_address */
7828 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7829 sprintf (buf, ".module/%08lx", (long) base_addr);
7831 len = strlen (buf) + 1;
7832 name = bfd_alloc (abfd, len);
7836 memcpy (name, buf, len);
7838 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7843 sect->size = note->descsz;
7844 sect->filepos = note->descpos;
7845 sect->alignment_power = 2;
7856 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7858 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7866 if (bed->elf_backend_grok_prstatus)
7867 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7869 #if defined (HAVE_PRSTATUS_T)
7870 return elfcore_grok_prstatus (abfd, note);
7875 #if defined (HAVE_PSTATUS_T)
7877 return elfcore_grok_pstatus (abfd, note);
7880 #if defined (HAVE_LWPSTATUS_T)
7882 return elfcore_grok_lwpstatus (abfd, note);
7885 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7886 return elfcore_grok_prfpreg (abfd, note);
7888 case NT_WIN32PSTATUS:
7889 return elfcore_grok_win32pstatus (abfd, note);
7891 case NT_PRXFPREG: /* Linux SSE extension */
7892 if (note->namesz == 6
7893 && strcmp (note->namedata, "LINUX") == 0)
7894 return elfcore_grok_prxfpreg (abfd, note);
7899 if (note->namesz == 6
7900 && strcmp (note->namedata, "LINUX") == 0)
7901 return elfcore_grok_ppc_vmx (abfd, note);
7907 if (bed->elf_backend_grok_psinfo)
7908 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7910 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7911 return elfcore_grok_psinfo (abfd, note);
7918 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7923 sect->size = note->descsz;
7924 sect->filepos = note->descpos;
7925 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7933 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7935 elf_tdata (abfd)->build_id_size = note->descsz;
7936 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7937 if (elf_tdata (abfd)->build_id == NULL)
7940 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7946 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
7953 case NT_GNU_BUILD_ID:
7954 return elfobj_grok_gnu_build_id (abfd, note);
7959 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7963 cp = strchr (note->namedata, '@');
7966 *lwpidp = atoi(cp + 1);
7973 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7975 /* Signal number at offset 0x08. */
7976 elf_tdata (abfd)->core_signal
7977 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7979 /* Process ID at offset 0x50. */
7980 elf_tdata (abfd)->core_pid
7981 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7983 /* Command name at 0x7c (max 32 bytes, including nul). */
7984 elf_tdata (abfd)->core_command
7985 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7987 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7992 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7996 if (elfcore_netbsd_get_lwpid (note, &lwp))
7997 elf_tdata (abfd)->core_lwpid = lwp;
7999 if (note->type == NT_NETBSDCORE_PROCINFO)
8001 /* NetBSD-specific core "procinfo". Note that we expect to
8002 find this note before any of the others, which is fine,
8003 since the kernel writes this note out first when it
8004 creates a core file. */
8006 return elfcore_grok_netbsd_procinfo (abfd, note);
8009 /* As of Jan 2002 there are no other machine-independent notes
8010 defined for NetBSD core files. If the note type is less
8011 than the start of the machine-dependent note types, we don't
8014 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8018 switch (bfd_get_arch (abfd))
8020 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8021 PT_GETFPREGS == mach+2. */
8023 case bfd_arch_alpha:
8024 case bfd_arch_sparc:
8027 case NT_NETBSDCORE_FIRSTMACH+0:
8028 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8030 case NT_NETBSDCORE_FIRSTMACH+2:
8031 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8037 /* On all other arch's, PT_GETREGS == mach+1 and
8038 PT_GETFPREGS == mach+3. */
8043 case NT_NETBSDCORE_FIRSTMACH+1:
8044 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8046 case NT_NETBSDCORE_FIRSTMACH+3:
8047 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8057 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8059 void *ddata = note->descdata;
8066 /* nto_procfs_status 'pid' field is at offset 0. */
8067 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8069 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8070 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8072 /* nto_procfs_status 'flags' field is at offset 8. */
8073 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8075 /* nto_procfs_status 'what' field is at offset 14. */
8076 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8078 elf_tdata (abfd)->core_signal = sig;
8079 elf_tdata (abfd)->core_lwpid = *tid;
8082 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8083 do not come from signals so we make sure we set the current
8084 thread just in case. */
8085 if (flags & 0x00000080)
8086 elf_tdata (abfd)->core_lwpid = *tid;
8088 /* Make a ".qnx_core_status/%d" section. */
8089 sprintf (buf, ".qnx_core_status/%ld", *tid);
8091 name = bfd_alloc (abfd, strlen (buf) + 1);
8096 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8100 sect->size = note->descsz;
8101 sect->filepos = note->descpos;
8102 sect->alignment_power = 2;
8104 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8108 elfcore_grok_nto_regs (bfd *abfd,
8109 Elf_Internal_Note *note,
8117 /* Make a "(base)/%d" section. */
8118 sprintf (buf, "%s/%ld", base, tid);
8120 name = bfd_alloc (abfd, strlen (buf) + 1);
8125 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8129 sect->size = note->descsz;
8130 sect->filepos = note->descpos;
8131 sect->alignment_power = 2;
8133 /* This is the current thread. */
8134 if (elf_tdata (abfd)->core_lwpid == tid)
8135 return elfcore_maybe_make_sect (abfd, base, sect);
8140 #define BFD_QNT_CORE_INFO 7
8141 #define BFD_QNT_CORE_STATUS 8
8142 #define BFD_QNT_CORE_GREG 9
8143 #define BFD_QNT_CORE_FPREG 10
8146 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8148 /* Every GREG section has a STATUS section before it. Store the
8149 tid from the previous call to pass down to the next gregs
8151 static long tid = 1;
8155 case BFD_QNT_CORE_INFO:
8156 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8157 case BFD_QNT_CORE_STATUS:
8158 return elfcore_grok_nto_status (abfd, note, &tid);
8159 case BFD_QNT_CORE_GREG:
8160 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8161 case BFD_QNT_CORE_FPREG:
8162 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8169 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8175 /* Use note name as section name. */
8177 name = bfd_alloc (abfd, len);
8180 memcpy (name, note->namedata, len);
8181 name[len - 1] = '\0';
8183 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8187 sect->size = note->descsz;
8188 sect->filepos = note->descpos;
8189 sect->alignment_power = 1;
8194 /* Function: elfcore_write_note
8197 buffer to hold note, and current size of buffer
8201 size of data for note
8203 Writes note to end of buffer. ELF64 notes are written exactly as
8204 for ELF32, despite the current (as of 2006) ELF gabi specifying
8205 that they ought to have 8-byte namesz and descsz field, and have
8206 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8209 Pointer to realloc'd buffer, *BUFSIZ updated. */
8212 elfcore_write_note (bfd *abfd,
8220 Elf_External_Note *xnp;
8227 namesz = strlen (name) + 1;
8229 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8231 buf = realloc (buf, *bufsiz + newspace);
8234 dest = buf + *bufsiz;
8235 *bufsiz += newspace;
8236 xnp = (Elf_External_Note *) dest;
8237 H_PUT_32 (abfd, namesz, xnp->namesz);
8238 H_PUT_32 (abfd, size, xnp->descsz);
8239 H_PUT_32 (abfd, type, xnp->type);
8243 memcpy (dest, name, namesz);
8251 memcpy (dest, input, size);
8261 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8263 elfcore_write_prpsinfo (bfd *abfd,
8269 const char *note_name = "CORE";
8270 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8272 if (bed->elf_backend_write_core_note != NULL)
8275 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8276 NT_PRPSINFO, fname, psargs);
8281 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8282 if (bed->s->elfclass == ELFCLASS32)
8284 #if defined (HAVE_PSINFO32_T)
8286 int note_type = NT_PSINFO;
8289 int note_type = NT_PRPSINFO;
8292 memset (&data, 0, sizeof (data));
8293 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8294 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8295 return elfcore_write_note (abfd, buf, bufsiz,
8296 note_name, note_type, &data, sizeof (data));
8301 #if defined (HAVE_PSINFO_T)
8303 int note_type = NT_PSINFO;
8306 int note_type = NT_PRPSINFO;
8309 memset (&data, 0, sizeof (data));
8310 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8311 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8312 return elfcore_write_note (abfd, buf, bufsiz,
8313 note_name, note_type, &data, sizeof (data));
8316 #endif /* PSINFO_T or PRPSINFO_T */
8318 #if defined (HAVE_PRSTATUS_T)
8320 elfcore_write_prstatus (bfd *abfd,
8327 const char *note_name = "CORE";
8328 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8330 if (bed->elf_backend_write_core_note != NULL)
8333 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8335 pid, cursig, gregs);
8340 #if defined (HAVE_PRSTATUS32_T)
8341 if (bed->s->elfclass == ELFCLASS32)
8343 prstatus32_t prstat;
8345 memset (&prstat, 0, sizeof (prstat));
8346 prstat.pr_pid = pid;
8347 prstat.pr_cursig = cursig;
8348 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8349 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8350 NT_PRSTATUS, &prstat, sizeof (prstat));
8357 memset (&prstat, 0, sizeof (prstat));
8358 prstat.pr_pid = pid;
8359 prstat.pr_cursig = cursig;
8360 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8361 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8362 NT_PRSTATUS, &prstat, sizeof (prstat));
8365 #endif /* HAVE_PRSTATUS_T */
8367 #if defined (HAVE_LWPSTATUS_T)
8369 elfcore_write_lwpstatus (bfd *abfd,
8376 lwpstatus_t lwpstat;
8377 const char *note_name = "CORE";
8379 memset (&lwpstat, 0, sizeof (lwpstat));
8380 lwpstat.pr_lwpid = pid >> 16;
8381 lwpstat.pr_cursig = cursig;
8382 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8383 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8384 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8386 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8387 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8389 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8390 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8393 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8394 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8396 #endif /* HAVE_LWPSTATUS_T */
8398 #if defined (HAVE_PSTATUS_T)
8400 elfcore_write_pstatus (bfd *abfd,
8404 int cursig ATTRIBUTE_UNUSED,
8405 const void *gregs ATTRIBUTE_UNUSED)
8407 const char *note_name = "CORE";
8408 #if defined (HAVE_PSTATUS32_T)
8409 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8411 if (bed->s->elfclass == ELFCLASS32)
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));
8426 memset (&pstat, 0, sizeof (pstat));
8427 pstat.pr_pid = pid & 0xffff;
8428 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8429 NT_PSTATUS, &pstat, sizeof (pstat));
8433 #endif /* HAVE_PSTATUS_T */
8436 elfcore_write_prfpreg (bfd *abfd,
8442 const char *note_name = "CORE";
8443 return elfcore_write_note (abfd, buf, bufsiz,
8444 note_name, NT_FPREGSET, fpregs, size);
8448 elfcore_write_prxfpreg (bfd *abfd,
8451 const void *xfpregs,
8454 char *note_name = "LINUX";
8455 return elfcore_write_note (abfd, buf, bufsiz,
8456 note_name, NT_PRXFPREG, xfpregs, size);
8460 elfcore_write_ppc_vmx (bfd *abfd,
8463 const void *ppc_vmx,
8466 char *note_name = "LINUX";
8467 return elfcore_write_note (abfd, buf, bufsiz,
8468 note_name, NT_PPC_VMX, ppc_vmx, size);
8472 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8477 while (p < buf + size)
8479 /* FIXME: bad alignment assumption. */
8480 Elf_External_Note *xnp = (Elf_External_Note *) p;
8481 Elf_Internal_Note in;
8483 in.type = H_GET_32 (abfd, xnp->type);
8485 in.namesz = H_GET_32 (abfd, xnp->namesz);
8486 in.namedata = xnp->name;
8488 in.descsz = H_GET_32 (abfd, xnp->descsz);
8489 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8490 in.descpos = offset + (in.descdata - buf);
8492 switch (bfd_get_format (abfd))
8498 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8500 if (! elfcore_grok_netbsd_note (abfd, &in))
8503 else if (CONST_STRNEQ (in.namedata, "QNX"))
8505 if (! elfcore_grok_nto_note (abfd, &in))
8508 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8510 if (! elfcore_grok_spu_note (abfd, &in))
8515 if (! elfcore_grok_note (abfd, &in))
8521 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8523 if (! elfobj_grok_gnu_note (abfd, &in))
8529 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8536 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8543 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8546 buf = bfd_malloc (size);
8550 if (bfd_bread (buf, size, abfd) != size
8551 || !elf_parse_notes (abfd, buf, size, offset))
8561 /* Providing external access to the ELF program header table. */
8563 /* Return an upper bound on the number of bytes required to store a
8564 copy of ABFD's program header table entries. Return -1 if an error
8565 occurs; bfd_get_error will return an appropriate code. */
8568 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8570 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8572 bfd_set_error (bfd_error_wrong_format);
8576 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8579 /* Copy ABFD's program header table entries to *PHDRS. The entries
8580 will be stored as an array of Elf_Internal_Phdr structures, as
8581 defined in include/elf/internal.h. To find out how large the
8582 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8584 Return the number of program header table entries read, or -1 if an
8585 error occurs; bfd_get_error will return an appropriate code. */
8588 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8592 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8594 bfd_set_error (bfd_error_wrong_format);
8598 num_phdrs = elf_elfheader (abfd)->e_phnum;
8599 memcpy (phdrs, elf_tdata (abfd)->phdr,
8600 num_phdrs * sizeof (Elf_Internal_Phdr));
8605 enum elf_reloc_type_class
8606 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8608 return reloc_class_normal;
8611 /* For RELA architectures, return the relocation value for a
8612 relocation against a local symbol. */
8615 _bfd_elf_rela_local_sym (bfd *abfd,
8616 Elf_Internal_Sym *sym,
8618 Elf_Internal_Rela *rel)
8620 asection *sec = *psec;
8623 relocation = (sec->output_section->vma
8624 + sec->output_offset
8626 if ((sec->flags & SEC_MERGE)
8627 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8628 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8631 _bfd_merged_section_offset (abfd, psec,
8632 elf_section_data (sec)->sec_info,
8633 sym->st_value + rel->r_addend);
8636 /* If we have changed the section, and our original section is
8637 marked with SEC_EXCLUDE, it means that the original
8638 SEC_MERGE section has been completely subsumed in some
8639 other SEC_MERGE section. In this case, we need to leave
8640 some info around for --emit-relocs. */
8641 if ((sec->flags & SEC_EXCLUDE) != 0)
8642 sec->kept_section = *psec;
8645 rel->r_addend -= relocation;
8646 rel->r_addend += sec->output_section->vma + sec->output_offset;
8652 _bfd_elf_rel_local_sym (bfd *abfd,
8653 Elf_Internal_Sym *sym,
8657 asection *sec = *psec;
8659 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8660 return sym->st_value + addend;
8662 return _bfd_merged_section_offset (abfd, psec,
8663 elf_section_data (sec)->sec_info,
8664 sym->st_value + addend);
8668 _bfd_elf_section_offset (bfd *abfd,
8669 struct bfd_link_info *info,
8673 switch (sec->sec_info_type)
8675 case ELF_INFO_TYPE_STABS:
8676 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8678 case ELF_INFO_TYPE_EH_FRAME:
8679 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8685 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8686 reconstruct an ELF file by reading the segments out of remote memory
8687 based on the ELF file header at EHDR_VMA and the ELF program headers it
8688 points to. If not null, *LOADBASEP is filled in with the difference
8689 between the VMAs from which the segments were read, and the VMAs the
8690 file headers (and hence BFD's idea of each section's VMA) put them at.
8692 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8693 remote memory at target address VMA into the local buffer at MYADDR; it
8694 should return zero on success or an `errno' code on failure. TEMPL must
8695 be a BFD for an ELF target with the word size and byte order found in
8696 the remote memory. */
8699 bfd_elf_bfd_from_remote_memory
8703 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8705 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8706 (templ, ehdr_vma, loadbasep, target_read_memory);
8710 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8711 long symcount ATTRIBUTE_UNUSED,
8712 asymbol **syms ATTRIBUTE_UNUSED,
8717 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8720 const char *relplt_name;
8721 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8725 Elf_Internal_Shdr *hdr;
8731 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8734 if (dynsymcount <= 0)
8737 if (!bed->plt_sym_val)
8740 relplt_name = bed->relplt_name;
8741 if (relplt_name == NULL)
8742 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8743 relplt = bfd_get_section_by_name (abfd, relplt_name);
8747 hdr = &elf_section_data (relplt)->this_hdr;
8748 if (hdr->sh_link != elf_dynsymtab (abfd)
8749 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8752 plt = bfd_get_section_by_name (abfd, ".plt");
8756 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8757 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8760 count = relplt->size / hdr->sh_entsize;
8761 size = count * sizeof (asymbol);
8762 p = relplt->relocation;
8763 for (i = 0; i < count; i++, p++)
8764 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8766 s = *ret = bfd_malloc (size);
8770 names = (char *) (s + count);
8771 p = relplt->relocation;
8773 for (i = 0; i < count; i++, p++)
8778 addr = bed->plt_sym_val (i, plt, p);
8779 if (addr == (bfd_vma) -1)
8782 *s = **p->sym_ptr_ptr;
8783 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8784 we are defining a symbol, ensure one of them is set. */
8785 if ((s->flags & BSF_LOCAL) == 0)
8786 s->flags |= BSF_GLOBAL;
8788 s->value = addr - plt->vma;
8791 len = strlen ((*p->sym_ptr_ptr)->name);
8792 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8794 memcpy (names, "@plt", sizeof ("@plt"));
8795 names += sizeof ("@plt");
8802 /* It is only used by x86-64 so far. */
8803 asection _bfd_elf_large_com_section
8804 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8805 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8808 _bfd_elf_set_osabi (bfd * abfd,
8809 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8811 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8813 i_ehdrp = elf_elfheader (abfd);
8815 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8819 /* Return TRUE for ELF symbol types that represent functions.
8820 This is the default version of this function, which is sufficient for
8821 most targets. It returns true if TYPE is STT_FUNC. */
8824 _bfd_elf_is_function_type (unsigned int type)
8826 return (type == STT_FUNC);