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))
462 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
463 shindex = elf_elfheader (abfd)->e_shstrndx;
466 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
469 else if (sym_sec && *name == '\0')
470 name = bfd_section_name (abfd, sym_sec);
475 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
476 sections. The first element is the flags, the rest are section
479 typedef union elf_internal_group {
480 Elf_Internal_Shdr *shdr;
482 } Elf_Internal_Group;
484 /* Return the name of the group signature symbol. Why isn't the
485 signature just a string? */
488 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
490 Elf_Internal_Shdr *hdr;
491 unsigned char esym[sizeof (Elf64_External_Sym)];
492 Elf_External_Sym_Shndx eshndx;
493 Elf_Internal_Sym isym;
495 /* First we need to ensure the symbol table is available. Make sure
496 that it is a symbol table section. */
497 if (ghdr->sh_link >= elf_numsections (abfd))
499 hdr = elf_elfsections (abfd) [ghdr->sh_link];
500 if (hdr->sh_type != SHT_SYMTAB
501 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
504 /* Go read the symbol. */
505 hdr = &elf_tdata (abfd)->symtab_hdr;
506 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
507 &isym, esym, &eshndx) == NULL)
510 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
513 /* Set next_in_group list pointer, and group name for NEWSECT. */
516 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
518 unsigned int num_group = elf_tdata (abfd)->num_group;
520 /* If num_group is zero, read in all SHT_GROUP sections. The count
521 is set to -1 if there are no SHT_GROUP sections. */
524 unsigned int i, shnum;
526 /* First count the number of groups. If we have a SHT_GROUP
527 section with just a flag word (ie. sh_size is 4), ignore it. */
528 shnum = elf_numsections (abfd);
531 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
532 ( (shdr)->sh_type == SHT_GROUP \
533 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
534 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
535 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
537 for (i = 0; i < shnum; i++)
539 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
541 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
547 num_group = (unsigned) -1;
548 elf_tdata (abfd)->num_group = num_group;
552 /* We keep a list of elf section headers for group sections,
553 so we can find them quickly. */
556 elf_tdata (abfd)->num_group = num_group;
557 elf_tdata (abfd)->group_sect_ptr
558 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
559 if (elf_tdata (abfd)->group_sect_ptr == NULL)
563 for (i = 0; i < shnum; i++)
565 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
567 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
570 Elf_Internal_Group *dest;
572 /* Add to list of sections. */
573 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
576 /* Read the raw contents. */
577 BFD_ASSERT (sizeof (*dest) >= 4);
578 amt = shdr->sh_size * sizeof (*dest) / 4;
579 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
581 /* PR binutils/4110: Handle corrupt group headers. */
582 if (shdr->contents == NULL)
585 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
586 bfd_set_error (bfd_error_bad_value);
590 memset (shdr->contents, 0, amt);
592 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
593 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
597 /* Translate raw contents, a flag word followed by an
598 array of elf section indices all in target byte order,
599 to the flag word followed by an array of elf section
601 src = shdr->contents + shdr->sh_size;
602 dest = (Elf_Internal_Group *) (shdr->contents + amt);
609 idx = H_GET_32 (abfd, src);
610 if (src == shdr->contents)
613 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
614 shdr->bfd_section->flags
615 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
620 ((*_bfd_error_handler)
621 (_("%B: invalid SHT_GROUP entry"), abfd));
624 dest->shdr = elf_elfsections (abfd)[idx];
631 if (num_group != (unsigned) -1)
635 for (i = 0; i < num_group; i++)
637 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
638 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
639 unsigned int n_elt = shdr->sh_size / 4;
641 /* Look through this group's sections to see if current
642 section is a member. */
644 if ((++idx)->shdr == hdr)
648 /* We are a member of this group. Go looking through
649 other members to see if any others are linked via
651 idx = (Elf_Internal_Group *) shdr->contents;
652 n_elt = shdr->sh_size / 4;
654 if ((s = (++idx)->shdr->bfd_section) != NULL
655 && elf_next_in_group (s) != NULL)
659 /* Snarf the group name from other member, and
660 insert current section in circular list. */
661 elf_group_name (newsect) = elf_group_name (s);
662 elf_next_in_group (newsect) = elf_next_in_group (s);
663 elf_next_in_group (s) = newsect;
669 gname = group_signature (abfd, shdr);
672 elf_group_name (newsect) = gname;
674 /* Start a circular list with one element. */
675 elf_next_in_group (newsect) = newsect;
678 /* If the group section has been created, point to the
680 if (shdr->bfd_section != NULL)
681 elf_next_in_group (shdr->bfd_section) = newsect;
689 if (elf_group_name (newsect) == NULL)
691 (*_bfd_error_handler) (_("%B: no group info for section %A"),
698 _bfd_elf_setup_sections (bfd *abfd)
701 unsigned int num_group = elf_tdata (abfd)->num_group;
702 bfd_boolean result = TRUE;
705 /* Process SHF_LINK_ORDER. */
706 for (s = abfd->sections; s != NULL; s = s->next)
708 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
709 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
711 unsigned int elfsec = this_hdr->sh_link;
712 /* FIXME: The old Intel compiler and old strip/objcopy may
713 not set the sh_link or sh_info fields. Hence we could
714 get the situation where elfsec is 0. */
717 const struct elf_backend_data *bed = 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'"),
725 asection *link = NULL;
727 if (elfsec < elf_numsections (abfd))
729 this_hdr = elf_elfsections (abfd)[elfsec];
730 link = this_hdr->bfd_section;
734 Some strip/objcopy may leave an incorrect value in
735 sh_link. We don't want to proceed. */
738 (*_bfd_error_handler)
739 (_("%B: sh_link [%d] in section `%A' is incorrect"),
740 s->owner, s, elfsec);
744 elf_linked_to_section (s) = link;
749 /* Process section groups. */
750 if (num_group == (unsigned) -1)
753 for (i = 0; i < num_group; i++)
755 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
756 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
757 unsigned int n_elt = shdr->sh_size / 4;
760 if ((++idx)->shdr->bfd_section)
761 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
762 else if (idx->shdr->sh_type == SHT_RELA
763 || idx->shdr->sh_type == SHT_REL)
764 /* We won't include relocation sections in section groups in
765 output object files. We adjust the group section size here
766 so that relocatable link will work correctly when
767 relocation sections are in section group in input object
769 shdr->bfd_section->size -= 4;
772 /* There are some unknown sections in the group. */
773 (*_bfd_error_handler)
774 (_("%B: unknown [%d] section `%s' in group [%s]"),
776 (unsigned int) idx->shdr->sh_type,
777 bfd_elf_string_from_elf_section (abfd,
778 (elf_elfheader (abfd)
781 shdr->bfd_section->name);
789 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
791 return elf_next_in_group (sec) != NULL;
794 /* Make a BFD section from an ELF section. We store a pointer to the
795 BFD section in the bfd_section field of the header. */
798 _bfd_elf_make_section_from_shdr (bfd *abfd,
799 Elf_Internal_Shdr *hdr,
805 const struct elf_backend_data *bed;
807 if (hdr->bfd_section != NULL)
809 BFD_ASSERT (strcmp (name,
810 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
814 newsect = bfd_make_section_anyway (abfd, name);
818 hdr->bfd_section = newsect;
819 elf_section_data (newsect)->this_hdr = *hdr;
820 elf_section_data (newsect)->this_idx = shindex;
822 /* Always use the real type/flags. */
823 elf_section_type (newsect) = hdr->sh_type;
824 elf_section_flags (newsect) = hdr->sh_flags;
826 newsect->filepos = hdr->sh_offset;
828 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
829 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
830 || ! bfd_set_section_alignment (abfd, newsect,
831 bfd_log2 (hdr->sh_addralign)))
834 flags = SEC_NO_FLAGS;
835 if (hdr->sh_type != SHT_NOBITS)
836 flags |= SEC_HAS_CONTENTS;
837 if (hdr->sh_type == SHT_GROUP)
838 flags |= SEC_GROUP | SEC_EXCLUDE;
839 if ((hdr->sh_flags & SHF_ALLOC) != 0)
842 if (hdr->sh_type != SHT_NOBITS)
845 if ((hdr->sh_flags & SHF_WRITE) == 0)
846 flags |= SEC_READONLY;
847 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
849 else if ((flags & SEC_LOAD) != 0)
851 if ((hdr->sh_flags & SHF_MERGE) != 0)
854 newsect->entsize = hdr->sh_entsize;
855 if ((hdr->sh_flags & SHF_STRINGS) != 0)
856 flags |= SEC_STRINGS;
858 if (hdr->sh_flags & SHF_GROUP)
859 if (!setup_group (abfd, hdr, newsect))
861 if ((hdr->sh_flags & SHF_TLS) != 0)
862 flags |= SEC_THREAD_LOCAL;
864 if ((flags & SEC_ALLOC) == 0)
866 /* The debugging sections appear to be recognized only by name,
867 not any sort of flag. Their SEC_ALLOC bits are cleared. */
872 } debug_sections [] =
874 { STRING_COMMA_LEN ("debug") }, /* 'd' */
875 { NULL, 0 }, /* 'e' */
876 { NULL, 0 }, /* 'f' */
877 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
878 { NULL, 0 }, /* 'h' */
879 { NULL, 0 }, /* 'i' */
880 { NULL, 0 }, /* 'j' */
881 { NULL, 0 }, /* 'k' */
882 { STRING_COMMA_LEN ("line") }, /* 'l' */
883 { NULL, 0 }, /* 'm' */
884 { NULL, 0 }, /* 'n' */
885 { NULL, 0 }, /* 'o' */
886 { NULL, 0 }, /* 'p' */
887 { NULL, 0 }, /* 'q' */
888 { NULL, 0 }, /* 'r' */
889 { STRING_COMMA_LEN ("stab") } /* 's' */
894 int i = name [1] - 'd';
896 && i < (int) ARRAY_SIZE (debug_sections)
897 && debug_sections [i].name != NULL
898 && strncmp (&name [1], debug_sections [i].name,
899 debug_sections [i].len) == 0)
900 flags |= SEC_DEBUGGING;
904 /* As a GNU extension, if the name begins with .gnu.linkonce, we
905 only link a single copy of the section. This is used to support
906 g++. g++ will emit each template expansion in its own section.
907 The symbols will be defined as weak, so that multiple definitions
908 are permitted. The GNU linker extension is to actually discard
909 all but one of the sections. */
910 if (CONST_STRNEQ (name, ".gnu.linkonce")
911 && elf_next_in_group (newsect) == NULL)
912 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
914 bed = get_elf_backend_data (abfd);
915 if (bed->elf_backend_section_flags)
916 if (! bed->elf_backend_section_flags (&flags, hdr))
919 if (! bfd_set_section_flags (abfd, newsect, flags))
922 /* We do not parse the PT_NOTE segments as we are interested even in the
923 separate debug info files which may have the segments offsets corrupted.
924 PT_NOTEs from the core files are currently not parsed using BFD. */
925 if (hdr->sh_type == SHT_NOTE)
929 contents = bfd_malloc (hdr->sh_size);
933 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
935 || !elf_parse_notes (abfd, contents, hdr->sh_size, -1))
944 if ((flags & SEC_ALLOC) != 0)
946 Elf_Internal_Phdr *phdr;
949 phdr = elf_tdata (abfd)->phdr;
950 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
952 /* This section is part of this segment if its file
953 offset plus size lies within the segment's memory
954 span and, if the section is loaded, the extent of the
955 loaded data lies within the extent of the segment.
957 Note - we used to check the p_paddr field as well, and
958 refuse to set the LMA if it was 0. This is wrong
959 though, as a perfectly valid initialised segment can
960 have a p_paddr of zero. Some architectures, eg ARM,
961 place special significance on the address 0 and
962 executables need to be able to have a segment which
963 covers this address. */
964 if (phdr->p_type == PT_LOAD
965 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
966 && (hdr->sh_offset + hdr->sh_size
967 <= phdr->p_offset + phdr->p_memsz)
968 && ((flags & SEC_LOAD) == 0
969 || (hdr->sh_offset + hdr->sh_size
970 <= phdr->p_offset + phdr->p_filesz)))
972 if ((flags & SEC_LOAD) == 0)
973 newsect->lma = (phdr->p_paddr
974 + hdr->sh_addr - phdr->p_vaddr);
976 /* We used to use the same adjustment for SEC_LOAD
977 sections, but that doesn't work if the segment
978 is packed with code from multiple VMAs.
979 Instead we calculate the section LMA based on
980 the segment LMA. It is assumed that the
981 segment will contain sections with contiguous
982 LMAs, even if the VMAs are not. */
983 newsect->lma = (phdr->p_paddr
984 + hdr->sh_offset - phdr->p_offset);
986 /* With contiguous segments, we can't tell from file
987 offsets whether a section with zero size should
988 be placed at the end of one segment or the
989 beginning of the next. Decide based on vaddr. */
990 if (hdr->sh_addr >= phdr->p_vaddr
991 && (hdr->sh_addr + hdr->sh_size
992 <= phdr->p_vaddr + phdr->p_memsz))
1003 bfd_elf_find_section
1006 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1009 Helper functions for GDB to locate the string tables.
1010 Since BFD hides string tables from callers, GDB needs to use an
1011 internal hook to find them. Sun's .stabstr, in particular,
1012 isn't even pointed to by the .stab section, so ordinary
1013 mechanisms wouldn't work to find it, even if we had some.
1016 struct elf_internal_shdr *
1017 bfd_elf_find_section (bfd *abfd, char *name)
1019 Elf_Internal_Shdr **i_shdrp;
1024 i_shdrp = elf_elfsections (abfd);
1025 if (i_shdrp != NULL)
1027 shstrtab = bfd_elf_get_str_section (abfd,
1028 elf_elfheader (abfd)->e_shstrndx);
1029 if (shstrtab != NULL)
1031 max = elf_numsections (abfd);
1032 for (i = 1; i < max; i++)
1033 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1040 const char *const bfd_elf_section_type_names[] = {
1041 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1042 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1043 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1046 /* ELF relocs are against symbols. If we are producing relocatable
1047 output, and the reloc is against an external symbol, and nothing
1048 has given us any additional addend, the resulting reloc will also
1049 be against the same symbol. In such a case, we don't want to
1050 change anything about the way the reloc is handled, since it will
1051 all be done at final link time. Rather than put special case code
1052 into bfd_perform_relocation, all the reloc types use this howto
1053 function. It just short circuits the reloc if producing
1054 relocatable output against an external symbol. */
1056 bfd_reloc_status_type
1057 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1058 arelent *reloc_entry,
1060 void *data ATTRIBUTE_UNUSED,
1061 asection *input_section,
1063 char **error_message ATTRIBUTE_UNUSED)
1065 if (output_bfd != NULL
1066 && (symbol->flags & BSF_SECTION_SYM) == 0
1067 && (! reloc_entry->howto->partial_inplace
1068 || reloc_entry->addend == 0))
1070 reloc_entry->address += input_section->output_offset;
1071 return bfd_reloc_ok;
1074 return bfd_reloc_continue;
1077 /* Copy the program header and other data from one object module to
1081 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1083 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1084 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1087 BFD_ASSERT (!elf_flags_init (obfd)
1088 || (elf_elfheader (obfd)->e_flags
1089 == elf_elfheader (ibfd)->e_flags));
1091 elf_gp (obfd) = elf_gp (ibfd);
1092 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1093 elf_flags_init (obfd) = TRUE;
1095 /* Copy object attributes. */
1096 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1102 get_segment_type (unsigned int p_type)
1107 case PT_NULL: pt = "NULL"; break;
1108 case PT_LOAD: pt = "LOAD"; break;
1109 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1110 case PT_INTERP: pt = "INTERP"; break;
1111 case PT_NOTE: pt = "NOTE"; break;
1112 case PT_SHLIB: pt = "SHLIB"; break;
1113 case PT_PHDR: pt = "PHDR"; break;
1114 case PT_TLS: pt = "TLS"; break;
1115 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1116 case PT_GNU_STACK: pt = "STACK"; break;
1117 case PT_GNU_RELRO: pt = "RELRO"; break;
1118 default: pt = NULL; break;
1123 /* Print out the program headers. */
1126 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1129 Elf_Internal_Phdr *p;
1131 bfd_byte *dynbuf = NULL;
1133 p = elf_tdata (abfd)->phdr;
1138 fprintf (f, _("\nProgram Header:\n"));
1139 c = elf_elfheader (abfd)->e_phnum;
1140 for (i = 0; i < c; i++, p++)
1142 const char *pt = get_segment_type (p->p_type);
1147 sprintf (buf, "0x%lx", p->p_type);
1150 fprintf (f, "%8s off 0x", pt);
1151 bfd_fprintf_vma (abfd, f, p->p_offset);
1152 fprintf (f, " vaddr 0x");
1153 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1154 fprintf (f, " paddr 0x");
1155 bfd_fprintf_vma (abfd, f, p->p_paddr);
1156 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1157 fprintf (f, " filesz 0x");
1158 bfd_fprintf_vma (abfd, f, p->p_filesz);
1159 fprintf (f, " memsz 0x");
1160 bfd_fprintf_vma (abfd, f, p->p_memsz);
1161 fprintf (f, " flags %c%c%c",
1162 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1163 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1164 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1165 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1166 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1171 s = bfd_get_section_by_name (abfd, ".dynamic");
1174 unsigned int elfsec;
1175 unsigned long shlink;
1176 bfd_byte *extdyn, *extdynend;
1178 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1180 fprintf (f, _("\nDynamic Section:\n"));
1182 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1185 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1186 if (elfsec == SHN_BAD)
1188 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1190 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1191 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1194 extdynend = extdyn + s->size;
1195 for (; extdyn < extdynend; extdyn += extdynsize)
1197 Elf_Internal_Dyn dyn;
1198 const char *name = "";
1200 bfd_boolean stringp;
1201 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1203 (*swap_dyn_in) (abfd, extdyn, &dyn);
1205 if (dyn.d_tag == DT_NULL)
1212 if (bed->elf_backend_get_target_dtag)
1213 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1215 if (!strcmp (name, ""))
1217 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1222 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1223 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1224 case DT_PLTGOT: name = "PLTGOT"; break;
1225 case DT_HASH: name = "HASH"; break;
1226 case DT_STRTAB: name = "STRTAB"; break;
1227 case DT_SYMTAB: name = "SYMTAB"; break;
1228 case DT_RELA: name = "RELA"; break;
1229 case DT_RELASZ: name = "RELASZ"; break;
1230 case DT_RELAENT: name = "RELAENT"; break;
1231 case DT_STRSZ: name = "STRSZ"; break;
1232 case DT_SYMENT: name = "SYMENT"; break;
1233 case DT_INIT: name = "INIT"; break;
1234 case DT_FINI: name = "FINI"; break;
1235 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1236 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1237 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1238 case DT_REL: name = "REL"; break;
1239 case DT_RELSZ: name = "RELSZ"; break;
1240 case DT_RELENT: name = "RELENT"; break;
1241 case DT_PLTREL: name = "PLTREL"; break;
1242 case DT_DEBUG: name = "DEBUG"; break;
1243 case DT_TEXTREL: name = "TEXTREL"; break;
1244 case DT_JMPREL: name = "JMPREL"; break;
1245 case DT_BIND_NOW: name = "BIND_NOW"; break;
1246 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1247 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1248 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1249 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1250 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1251 case DT_FLAGS: name = "FLAGS"; break;
1252 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1253 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1254 case DT_CHECKSUM: name = "CHECKSUM"; break;
1255 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1256 case DT_MOVEENT: name = "MOVEENT"; break;
1257 case DT_MOVESZ: name = "MOVESZ"; break;
1258 case DT_FEATURE: name = "FEATURE"; break;
1259 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1260 case DT_SYMINSZ: name = "SYMINSZ"; break;
1261 case DT_SYMINENT: name = "SYMINENT"; break;
1262 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1263 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1264 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1265 case DT_PLTPAD: name = "PLTPAD"; break;
1266 case DT_MOVETAB: name = "MOVETAB"; break;
1267 case DT_SYMINFO: name = "SYMINFO"; break;
1268 case DT_RELACOUNT: name = "RELACOUNT"; break;
1269 case DT_RELCOUNT: name = "RELCOUNT"; break;
1270 case DT_FLAGS_1: name = "FLAGS_1"; break;
1271 case DT_VERSYM: name = "VERSYM"; break;
1272 case DT_VERDEF: name = "VERDEF"; break;
1273 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1274 case DT_VERNEED: name = "VERNEED"; break;
1275 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1276 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1277 case DT_USED: name = "USED"; break;
1278 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1279 case DT_GNU_HASH: name = "GNU_HASH"; break;
1282 fprintf (f, " %-20s ", name);
1286 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1291 unsigned int tagv = dyn.d_un.d_val;
1293 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1296 fprintf (f, "%s", string);
1305 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1306 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1308 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1312 if (elf_dynverdef (abfd) != 0)
1314 Elf_Internal_Verdef *t;
1316 fprintf (f, _("\nVersion definitions:\n"));
1317 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1319 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1320 t->vd_flags, t->vd_hash,
1321 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1322 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1324 Elf_Internal_Verdaux *a;
1327 for (a = t->vd_auxptr->vda_nextptr;
1331 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1337 if (elf_dynverref (abfd) != 0)
1339 Elf_Internal_Verneed *t;
1341 fprintf (f, _("\nVersion References:\n"));
1342 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1344 Elf_Internal_Vernaux *a;
1346 fprintf (f, _(" required from %s:\n"),
1347 t->vn_filename ? t->vn_filename : "<corrupt>");
1348 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1349 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1350 a->vna_flags, a->vna_other,
1351 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1363 /* Display ELF-specific fields of a symbol. */
1366 bfd_elf_print_symbol (bfd *abfd,
1369 bfd_print_symbol_type how)
1374 case bfd_print_symbol_name:
1375 fprintf (file, "%s", symbol->name);
1377 case bfd_print_symbol_more:
1378 fprintf (file, "elf ");
1379 bfd_fprintf_vma (abfd, file, symbol->value);
1380 fprintf (file, " %lx", (long) symbol->flags);
1382 case bfd_print_symbol_all:
1384 const char *section_name;
1385 const char *name = NULL;
1386 const struct elf_backend_data *bed;
1387 unsigned char st_other;
1390 section_name = symbol->section ? symbol->section->name : "(*none*)";
1392 bed = get_elf_backend_data (abfd);
1393 if (bed->elf_backend_print_symbol_all)
1394 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1398 name = symbol->name;
1399 bfd_print_symbol_vandf (abfd, file, symbol);
1402 fprintf (file, " %s\t", section_name);
1403 /* Print the "other" value for a symbol. For common symbols,
1404 we've already printed the size; now print the alignment.
1405 For other symbols, we have no specified alignment, and
1406 we've printed the address; now print the size. */
1407 if (symbol->section && bfd_is_com_section (symbol->section))
1408 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1410 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1411 bfd_fprintf_vma (abfd, file, val);
1413 /* If we have version information, print it. */
1414 if (elf_tdata (abfd)->dynversym_section != 0
1415 && (elf_tdata (abfd)->dynverdef_section != 0
1416 || elf_tdata (abfd)->dynverref_section != 0))
1418 unsigned int vernum;
1419 const char *version_string;
1421 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1424 version_string = "";
1425 else if (vernum == 1)
1426 version_string = "Base";
1427 else if (vernum <= elf_tdata (abfd)->cverdefs)
1429 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1432 Elf_Internal_Verneed *t;
1434 version_string = "";
1435 for (t = elf_tdata (abfd)->verref;
1439 Elf_Internal_Vernaux *a;
1441 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1443 if (a->vna_other == vernum)
1445 version_string = a->vna_nodename;
1452 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1453 fprintf (file, " %-11s", version_string);
1458 fprintf (file, " (%s)", version_string);
1459 for (i = 10 - strlen (version_string); i > 0; --i)
1464 /* If the st_other field is not zero, print it. */
1465 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1470 case STV_INTERNAL: fprintf (file, " .internal"); break;
1471 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1472 case STV_PROTECTED: fprintf (file, " .protected"); break;
1474 /* Some other non-defined flags are also present, so print
1476 fprintf (file, " 0x%02x", (unsigned int) st_other);
1479 fprintf (file, " %s", name);
1485 /* Allocate an ELF string table--force the first byte to be zero. */
1487 struct bfd_strtab_hash *
1488 _bfd_elf_stringtab_init (void)
1490 struct bfd_strtab_hash *ret;
1492 ret = _bfd_stringtab_init ();
1497 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1498 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1499 if (loc == (bfd_size_type) -1)
1501 _bfd_stringtab_free (ret);
1508 /* ELF .o/exec file reading */
1510 /* Create a new bfd section from an ELF section header. */
1513 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1515 Elf_Internal_Shdr *hdr;
1516 Elf_Internal_Ehdr *ehdr;
1517 const struct elf_backend_data *bed;
1520 if (shindex >= elf_numsections (abfd))
1523 hdr = elf_elfsections (abfd)[shindex];
1524 ehdr = elf_elfheader (abfd);
1525 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1530 bed = get_elf_backend_data (abfd);
1531 switch (hdr->sh_type)
1534 /* Inactive section. Throw it away. */
1537 case SHT_PROGBITS: /* Normal section with contents. */
1538 case SHT_NOBITS: /* .bss section. */
1539 case SHT_HASH: /* .hash section. */
1540 case SHT_NOTE: /* .note section. */
1541 case SHT_INIT_ARRAY: /* .init_array section. */
1542 case SHT_FINI_ARRAY: /* .fini_array section. */
1543 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1544 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1545 case SHT_GNU_HASH: /* .gnu.hash section. */
1546 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1548 case SHT_DYNAMIC: /* Dynamic linking information. */
1549 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1551 if (hdr->sh_link > elf_numsections (abfd)
1552 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1554 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1556 Elf_Internal_Shdr *dynsymhdr;
1558 /* The shared libraries distributed with hpux11 have a bogus
1559 sh_link field for the ".dynamic" section. Find the
1560 string table for the ".dynsym" section instead. */
1561 if (elf_dynsymtab (abfd) != 0)
1563 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1564 hdr->sh_link = dynsymhdr->sh_link;
1568 unsigned int i, num_sec;
1570 num_sec = elf_numsections (abfd);
1571 for (i = 1; i < num_sec; i++)
1573 dynsymhdr = elf_elfsections (abfd)[i];
1574 if (dynsymhdr->sh_type == SHT_DYNSYM)
1576 hdr->sh_link = dynsymhdr->sh_link;
1584 case SHT_SYMTAB: /* A symbol table */
1585 if (elf_onesymtab (abfd) == shindex)
1588 if (hdr->sh_entsize != bed->s->sizeof_sym)
1590 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1591 elf_onesymtab (abfd) = shindex;
1592 elf_tdata (abfd)->symtab_hdr = *hdr;
1593 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1594 abfd->flags |= HAS_SYMS;
1596 /* Sometimes a shared object will map in the symbol table. If
1597 SHF_ALLOC is set, and this is a shared object, then we also
1598 treat this section as a BFD section. We can not base the
1599 decision purely on SHF_ALLOC, because that flag is sometimes
1600 set in a relocatable object file, which would confuse the
1602 if ((hdr->sh_flags & SHF_ALLOC) != 0
1603 && (abfd->flags & DYNAMIC) != 0
1604 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1608 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1609 can't read symbols without that section loaded as well. It
1610 is most likely specified by the next section header. */
1611 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1613 unsigned int i, num_sec;
1615 num_sec = elf_numsections (abfd);
1616 for (i = shindex + 1; i < num_sec; i++)
1618 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1619 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1620 && hdr2->sh_link == shindex)
1624 for (i = 1; i < shindex; i++)
1626 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1627 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1628 && hdr2->sh_link == shindex)
1632 return bfd_section_from_shdr (abfd, i);
1636 case SHT_DYNSYM: /* A dynamic symbol table */
1637 if (elf_dynsymtab (abfd) == shindex)
1640 if (hdr->sh_entsize != bed->s->sizeof_sym)
1642 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1643 elf_dynsymtab (abfd) = shindex;
1644 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1645 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1646 abfd->flags |= HAS_SYMS;
1648 /* Besides being a symbol table, we also treat this as a regular
1649 section, so that objcopy can handle it. */
1650 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1652 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1653 if (elf_symtab_shndx (abfd) == shindex)
1656 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1657 elf_symtab_shndx (abfd) = shindex;
1658 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1659 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1662 case SHT_STRTAB: /* A string table */
1663 if (hdr->bfd_section != NULL)
1665 if (ehdr->e_shstrndx == shindex)
1667 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1668 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1671 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1674 elf_tdata (abfd)->strtab_hdr = *hdr;
1675 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1678 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1681 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1682 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1683 elf_elfsections (abfd)[shindex] = hdr;
1684 /* We also treat this as a regular section, so that objcopy
1686 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1690 /* If the string table isn't one of the above, then treat it as a
1691 regular section. We need to scan all the headers to be sure,
1692 just in case this strtab section appeared before the above. */
1693 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1695 unsigned int i, num_sec;
1697 num_sec = elf_numsections (abfd);
1698 for (i = 1; i < num_sec; i++)
1700 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1701 if (hdr2->sh_link == shindex)
1703 /* Prevent endless recursion on broken objects. */
1706 if (! bfd_section_from_shdr (abfd, i))
1708 if (elf_onesymtab (abfd) == i)
1710 if (elf_dynsymtab (abfd) == i)
1711 goto dynsymtab_strtab;
1715 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1719 /* *These* do a lot of work -- but build no sections! */
1721 asection *target_sect;
1722 Elf_Internal_Shdr *hdr2;
1723 unsigned int num_sec = elf_numsections (abfd);
1726 != (bfd_size_type) (hdr->sh_type == SHT_REL
1727 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1730 /* Check for a bogus link to avoid crashing. */
1731 if (hdr->sh_link >= num_sec)
1733 ((*_bfd_error_handler)
1734 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1735 abfd, hdr->sh_link, name, shindex));
1736 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1740 /* For some incomprehensible reason Oracle distributes
1741 libraries for Solaris in which some of the objects have
1742 bogus sh_link fields. It would be nice if we could just
1743 reject them, but, unfortunately, some people need to use
1744 them. We scan through the section headers; if we find only
1745 one suitable symbol table, we clobber the sh_link to point
1746 to it. I hope this doesn't break anything. */
1747 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1748 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1754 for (scan = 1; scan < num_sec; scan++)
1756 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1757 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1768 hdr->sh_link = found;
1771 /* Get the symbol table. */
1772 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1773 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1774 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1777 /* If this reloc section does not use the main symbol table we
1778 don't treat it as a reloc section. BFD can't adequately
1779 represent such a section, so at least for now, we don't
1780 try. We just present it as a normal section. We also
1781 can't use it as a reloc section if it points to the null
1782 section, an invalid section, or another reloc section. */
1783 if (hdr->sh_link != elf_onesymtab (abfd)
1784 || hdr->sh_info == SHN_UNDEF
1785 || hdr->sh_info >= num_sec
1786 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1787 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1788 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1791 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1793 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1794 if (target_sect == NULL)
1797 if ((target_sect->flags & SEC_RELOC) == 0
1798 || target_sect->reloc_count == 0)
1799 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1803 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1804 amt = sizeof (*hdr2);
1805 hdr2 = bfd_alloc (abfd, amt);
1808 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1811 elf_elfsections (abfd)[shindex] = hdr2;
1812 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1813 target_sect->flags |= SEC_RELOC;
1814 target_sect->relocation = NULL;
1815 target_sect->rel_filepos = hdr->sh_offset;
1816 /* In the section to which the relocations apply, mark whether
1817 its relocations are of the REL or RELA variety. */
1818 if (hdr->sh_size != 0)
1819 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1820 abfd->flags |= HAS_RELOC;
1824 case SHT_GNU_verdef:
1825 elf_dynverdef (abfd) = shindex;
1826 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1827 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1829 case SHT_GNU_versym:
1830 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1832 elf_dynversym (abfd) = shindex;
1833 elf_tdata (abfd)->dynversym_hdr = *hdr;
1834 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1836 case SHT_GNU_verneed:
1837 elf_dynverref (abfd) = shindex;
1838 elf_tdata (abfd)->dynverref_hdr = *hdr;
1839 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1845 /* We need a BFD section for objcopy and relocatable linking,
1846 and it's handy to have the signature available as the section
1848 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1850 name = group_signature (abfd, hdr);
1853 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1855 if (hdr->contents != NULL)
1857 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1858 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1861 if (idx->flags & GRP_COMDAT)
1862 hdr->bfd_section->flags
1863 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1865 /* We try to keep the same section order as it comes in. */
1867 while (--n_elt != 0)
1871 if (idx->shdr != NULL
1872 && (s = idx->shdr->bfd_section) != NULL
1873 && elf_next_in_group (s) != NULL)
1875 elf_next_in_group (hdr->bfd_section) = s;
1883 /* Possibly an attributes section. */
1884 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1885 || hdr->sh_type == bed->obj_attrs_section_type)
1887 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1889 _bfd_elf_parse_attributes (abfd, hdr);
1893 /* Check for any processor-specific section types. */
1894 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1897 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1899 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1900 /* FIXME: How to properly handle allocated section reserved
1901 for applications? */
1902 (*_bfd_error_handler)
1903 (_("%B: don't know how to handle allocated, application "
1904 "specific section `%s' [0x%8x]"),
1905 abfd, name, hdr->sh_type);
1907 /* Allow sections reserved for applications. */
1908 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1911 else if (hdr->sh_type >= SHT_LOPROC
1912 && hdr->sh_type <= SHT_HIPROC)
1913 /* FIXME: We should handle this section. */
1914 (*_bfd_error_handler)
1915 (_("%B: don't know how to handle processor specific section "
1917 abfd, name, hdr->sh_type);
1918 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1920 /* Unrecognised OS-specific sections. */
1921 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1922 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1923 required to correctly process the section and the file should
1924 be rejected with an error message. */
1925 (*_bfd_error_handler)
1926 (_("%B: don't know how to handle OS specific section "
1928 abfd, name, hdr->sh_type);
1930 /* Otherwise it should be processed. */
1931 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1934 /* FIXME: We should handle this section. */
1935 (*_bfd_error_handler)
1936 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1937 abfd, name, hdr->sh_type);
1945 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1946 Return SEC for sections that have no elf section, and NULL on error. */
1949 bfd_section_from_r_symndx (bfd *abfd,
1950 struct sym_sec_cache *cache,
1952 unsigned long r_symndx)
1954 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1957 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1959 Elf_Internal_Shdr *symtab_hdr;
1960 unsigned char esym[sizeof (Elf64_External_Sym)];
1961 Elf_External_Sym_Shndx eshndx;
1962 Elf_Internal_Sym isym;
1964 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1965 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1966 &isym, esym, &eshndx) == NULL)
1969 if (cache->abfd != abfd)
1971 memset (cache->indx, -1, sizeof (cache->indx));
1974 cache->indx[ent] = r_symndx;
1975 cache->shndx[ent] = isym.st_shndx;
1978 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
1985 /* Given an ELF section number, retrieve the corresponding BFD
1989 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
1991 if (index >= elf_numsections (abfd))
1993 return elf_elfsections (abfd)[index]->bfd_section;
1996 static const struct bfd_elf_special_section special_sections_b[] =
1998 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1999 { NULL, 0, 0, 0, 0 }
2002 static const struct bfd_elf_special_section special_sections_c[] =
2004 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2005 { NULL, 0, 0, 0, 0 }
2008 static const struct bfd_elf_special_section special_sections_d[] =
2010 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2011 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2012 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2013 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2014 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2015 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2016 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2017 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2018 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2019 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2020 { NULL, 0, 0, 0, 0 }
2023 static const struct bfd_elf_special_section special_sections_f[] =
2025 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2026 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2027 { NULL, 0, 0, 0, 0 }
2030 static const struct bfd_elf_special_section special_sections_g[] =
2032 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2033 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2034 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2035 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2036 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2037 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2038 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2039 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2040 { NULL, 0, 0, 0, 0 }
2043 static const struct bfd_elf_special_section special_sections_h[] =
2045 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2046 { NULL, 0, 0, 0, 0 }
2049 static const struct bfd_elf_special_section special_sections_i[] =
2051 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2052 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2053 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2054 { NULL, 0, 0, 0, 0 }
2057 static const struct bfd_elf_special_section special_sections_l[] =
2059 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2060 { NULL, 0, 0, 0, 0 }
2063 static const struct bfd_elf_special_section special_sections_n[] =
2065 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2066 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2067 { NULL, 0, 0, 0, 0 }
2070 static const struct bfd_elf_special_section special_sections_p[] =
2072 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2073 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2074 { NULL, 0, 0, 0, 0 }
2077 static const struct bfd_elf_special_section special_sections_r[] =
2079 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2080 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2081 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2082 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2083 { NULL, 0, 0, 0, 0 }
2086 static const struct bfd_elf_special_section special_sections_s[] =
2088 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2089 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2090 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2091 /* See struct bfd_elf_special_section declaration for the semantics of
2092 this special case where .prefix_length != strlen (.prefix). */
2093 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2094 { NULL, 0, 0, 0, 0 }
2097 static const struct bfd_elf_special_section special_sections_t[] =
2099 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2100 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2101 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2102 { NULL, 0, 0, 0, 0 }
2105 static const struct bfd_elf_special_section *special_sections[] =
2107 special_sections_b, /* 'b' */
2108 special_sections_c, /* 'c' */
2109 special_sections_d, /* 'd' */
2111 special_sections_f, /* 'f' */
2112 special_sections_g, /* 'g' */
2113 special_sections_h, /* 'h' */
2114 special_sections_i, /* 'i' */
2117 special_sections_l, /* 'l' */
2119 special_sections_n, /* 'n' */
2121 special_sections_p, /* 'p' */
2123 special_sections_r, /* 'r' */
2124 special_sections_s, /* 's' */
2125 special_sections_t, /* 't' */
2128 const struct bfd_elf_special_section *
2129 _bfd_elf_get_special_section (const char *name,
2130 const struct bfd_elf_special_section *spec,
2136 len = strlen (name);
2138 for (i = 0; spec[i].prefix != NULL; i++)
2141 int prefix_len = spec[i].prefix_length;
2143 if (len < prefix_len)
2145 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2148 suffix_len = spec[i].suffix_length;
2149 if (suffix_len <= 0)
2151 if (name[prefix_len] != 0)
2153 if (suffix_len == 0)
2155 if (name[prefix_len] != '.'
2156 && (suffix_len == -2
2157 || (rela && spec[i].type == SHT_REL)))
2163 if (len < prefix_len + suffix_len)
2165 if (memcmp (name + len - suffix_len,
2166 spec[i].prefix + prefix_len,
2176 const struct bfd_elf_special_section *
2177 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2180 const struct bfd_elf_special_section *spec;
2181 const struct elf_backend_data *bed;
2183 /* See if this is one of the special sections. */
2184 if (sec->name == NULL)
2187 bed = get_elf_backend_data (abfd);
2188 spec = bed->special_sections;
2191 spec = _bfd_elf_get_special_section (sec->name,
2192 bed->special_sections,
2198 if (sec->name[0] != '.')
2201 i = sec->name[1] - 'b';
2202 if (i < 0 || i > 't' - 'b')
2205 spec = special_sections[i];
2210 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2214 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2216 struct bfd_elf_section_data *sdata;
2217 const struct elf_backend_data *bed;
2218 const struct bfd_elf_special_section *ssect;
2220 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2223 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2226 sec->used_by_bfd = sdata;
2229 /* Indicate whether or not this section should use RELA relocations. */
2230 bed = get_elf_backend_data (abfd);
2231 sec->use_rela_p = bed->default_use_rela_p;
2233 /* When we read a file, we don't need to set ELF section type and
2234 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2235 anyway. We will set ELF section type and flags for all linker
2236 created sections. If user specifies BFD section flags, we will
2237 set ELF section type and flags based on BFD section flags in
2238 elf_fake_sections. */
2239 if ((!sec->flags && abfd->direction != read_direction)
2240 || (sec->flags & SEC_LINKER_CREATED) != 0)
2242 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2245 elf_section_type (sec) = ssect->type;
2246 elf_section_flags (sec) = ssect->attr;
2250 return _bfd_generic_new_section_hook (abfd, sec);
2253 /* Create a new bfd section from an ELF program header.
2255 Since program segments have no names, we generate a synthetic name
2256 of the form segment<NUM>, where NUM is generally the index in the
2257 program header table. For segments that are split (see below) we
2258 generate the names segment<NUM>a and segment<NUM>b.
2260 Note that some program segments may have a file size that is different than
2261 (less than) the memory size. All this means is that at execution the
2262 system must allocate the amount of memory specified by the memory size,
2263 but only initialize it with the first "file size" bytes read from the
2264 file. This would occur for example, with program segments consisting
2265 of combined data+bss.
2267 To handle the above situation, this routine generates TWO bfd sections
2268 for the single program segment. The first has the length specified by
2269 the file size of the segment, and the second has the length specified
2270 by the difference between the two sizes. In effect, the segment is split
2271 into its initialized and uninitialized parts.
2276 _bfd_elf_make_section_from_phdr (bfd *abfd,
2277 Elf_Internal_Phdr *hdr,
2279 const char *typename)
2287 split = ((hdr->p_memsz > 0)
2288 && (hdr->p_filesz > 0)
2289 && (hdr->p_memsz > hdr->p_filesz));
2291 if (hdr->p_filesz > 0)
2293 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2294 len = strlen (namebuf) + 1;
2295 name = bfd_alloc (abfd, len);
2298 memcpy (name, namebuf, len);
2299 newsect = bfd_make_section (abfd, name);
2300 if (newsect == NULL)
2302 newsect->vma = hdr->p_vaddr;
2303 newsect->lma = hdr->p_paddr;
2304 newsect->size = hdr->p_filesz;
2305 newsect->filepos = hdr->p_offset;
2306 newsect->flags |= SEC_HAS_CONTENTS;
2307 newsect->alignment_power = bfd_log2 (hdr->p_align);
2308 if (hdr->p_type == PT_LOAD)
2310 newsect->flags |= SEC_ALLOC;
2311 newsect->flags |= SEC_LOAD;
2312 if (hdr->p_flags & PF_X)
2314 /* FIXME: all we known is that it has execute PERMISSION,
2316 newsect->flags |= SEC_CODE;
2319 if (!(hdr->p_flags & PF_W))
2321 newsect->flags |= SEC_READONLY;
2325 if (hdr->p_memsz > hdr->p_filesz)
2329 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
2330 len = strlen (namebuf) + 1;
2331 name = bfd_alloc (abfd, len);
2334 memcpy (name, namebuf, len);
2335 newsect = bfd_make_section (abfd, name);
2336 if (newsect == NULL)
2338 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2339 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2340 newsect->size = hdr->p_memsz - hdr->p_filesz;
2341 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2342 align = newsect->vma & -newsect->vma;
2343 if (align == 0 || align > hdr->p_align)
2344 align = hdr->p_align;
2345 newsect->alignment_power = bfd_log2 (align);
2346 if (hdr->p_type == PT_LOAD)
2348 /* Hack for gdb. Segments that have not been modified do
2349 not have their contents written to a core file, on the
2350 assumption that a debugger can find the contents in the
2351 executable. We flag this case by setting the fake
2352 section size to zero. Note that "real" bss sections will
2353 always have their contents dumped to the core file. */
2354 if (bfd_get_format (abfd) == bfd_core)
2356 newsect->flags |= SEC_ALLOC;
2357 if (hdr->p_flags & PF_X)
2358 newsect->flags |= SEC_CODE;
2360 if (!(hdr->p_flags & PF_W))
2361 newsect->flags |= SEC_READONLY;
2368 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2370 const struct elf_backend_data *bed;
2372 switch (hdr->p_type)
2375 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2378 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2381 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2384 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2387 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2389 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2394 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2397 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2399 case PT_GNU_EH_FRAME:
2400 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2404 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2407 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2410 /* Check for any processor-specific program segment types. */
2411 bed = get_elf_backend_data (abfd);
2412 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2416 /* Initialize REL_HDR, the section-header for new section, containing
2417 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2418 relocations; otherwise, we use REL relocations. */
2421 _bfd_elf_init_reloc_shdr (bfd *abfd,
2422 Elf_Internal_Shdr *rel_hdr,
2424 bfd_boolean use_rela_p)
2427 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2428 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2430 name = bfd_alloc (abfd, amt);
2433 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2435 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2437 if (rel_hdr->sh_name == (unsigned int) -1)
2439 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2440 rel_hdr->sh_entsize = (use_rela_p
2441 ? bed->s->sizeof_rela
2442 : bed->s->sizeof_rel);
2443 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2444 rel_hdr->sh_flags = 0;
2445 rel_hdr->sh_addr = 0;
2446 rel_hdr->sh_size = 0;
2447 rel_hdr->sh_offset = 0;
2452 /* Set up an ELF internal section header for a section. */
2455 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2457 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2458 bfd_boolean *failedptr = failedptrarg;
2459 Elf_Internal_Shdr *this_hdr;
2460 unsigned int sh_type;
2464 /* We already failed; just get out of the bfd_map_over_sections
2469 this_hdr = &elf_section_data (asect)->this_hdr;
2471 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2472 asect->name, FALSE);
2473 if (this_hdr->sh_name == (unsigned int) -1)
2479 /* Don't clear sh_flags. Assembler may set additional bits. */
2481 if ((asect->flags & SEC_ALLOC) != 0
2482 || asect->user_set_vma)
2483 this_hdr->sh_addr = asect->vma;
2485 this_hdr->sh_addr = 0;
2487 this_hdr->sh_offset = 0;
2488 this_hdr->sh_size = asect->size;
2489 this_hdr->sh_link = 0;
2490 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2491 /* The sh_entsize and sh_info fields may have been set already by
2492 copy_private_section_data. */
2494 this_hdr->bfd_section = asect;
2495 this_hdr->contents = NULL;
2497 /* If the section type is unspecified, we set it based on
2499 if ((asect->flags & SEC_GROUP) != 0)
2500 sh_type = SHT_GROUP;
2501 else if ((asect->flags & SEC_ALLOC) != 0
2502 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2503 || (asect->flags & SEC_NEVER_LOAD) != 0))
2504 sh_type = SHT_NOBITS;
2506 sh_type = SHT_PROGBITS;
2508 if (this_hdr->sh_type == SHT_NULL)
2509 this_hdr->sh_type = sh_type;
2510 else if (this_hdr->sh_type == SHT_NOBITS
2511 && sh_type == SHT_PROGBITS
2512 && (asect->flags & SEC_ALLOC) != 0)
2514 /* Warn if we are changing a NOBITS section to PROGBITS, but
2515 allow the link to proceed. This can happen when users link
2516 non-bss input sections to bss output sections, or emit data
2517 to a bss output section via a linker script. */
2518 (*_bfd_error_handler)
2519 (_("warning: section `%A' type changed to PROGBITS"), asect);
2520 this_hdr->sh_type = sh_type;
2523 switch (this_hdr->sh_type)
2529 case SHT_INIT_ARRAY:
2530 case SHT_FINI_ARRAY:
2531 case SHT_PREINIT_ARRAY:
2538 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2542 this_hdr->sh_entsize = bed->s->sizeof_sym;
2546 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2550 if (get_elf_backend_data (abfd)->may_use_rela_p)
2551 this_hdr->sh_entsize = bed->s->sizeof_rela;
2555 if (get_elf_backend_data (abfd)->may_use_rel_p)
2556 this_hdr->sh_entsize = bed->s->sizeof_rel;
2559 case SHT_GNU_versym:
2560 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2563 case SHT_GNU_verdef:
2564 this_hdr->sh_entsize = 0;
2565 /* objcopy or strip will copy over sh_info, but may not set
2566 cverdefs. The linker will set cverdefs, but sh_info will be
2568 if (this_hdr->sh_info == 0)
2569 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2571 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2572 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2575 case SHT_GNU_verneed:
2576 this_hdr->sh_entsize = 0;
2577 /* objcopy or strip will copy over sh_info, but may not set
2578 cverrefs. The linker will set cverrefs, but sh_info will be
2580 if (this_hdr->sh_info == 0)
2581 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2583 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2584 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2588 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2592 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2596 if ((asect->flags & SEC_ALLOC) != 0)
2597 this_hdr->sh_flags |= SHF_ALLOC;
2598 if ((asect->flags & SEC_READONLY) == 0)
2599 this_hdr->sh_flags |= SHF_WRITE;
2600 if ((asect->flags & SEC_CODE) != 0)
2601 this_hdr->sh_flags |= SHF_EXECINSTR;
2602 if ((asect->flags & SEC_MERGE) != 0)
2604 this_hdr->sh_flags |= SHF_MERGE;
2605 this_hdr->sh_entsize = asect->entsize;
2606 if ((asect->flags & SEC_STRINGS) != 0)
2607 this_hdr->sh_flags |= SHF_STRINGS;
2609 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2610 this_hdr->sh_flags |= SHF_GROUP;
2611 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2613 this_hdr->sh_flags |= SHF_TLS;
2614 if (asect->size == 0
2615 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2617 struct bfd_link_order *o = asect->map_tail.link_order;
2619 this_hdr->sh_size = 0;
2622 this_hdr->sh_size = o->offset + o->size;
2623 if (this_hdr->sh_size != 0)
2624 this_hdr->sh_type = SHT_NOBITS;
2629 /* Check for processor-specific section types. */
2630 sh_type = this_hdr->sh_type;
2631 if (bed->elf_backend_fake_sections
2632 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2635 if (sh_type == SHT_NOBITS && asect->size != 0)
2637 /* Don't change the header type from NOBITS if we are being
2638 called for objcopy --only-keep-debug. */
2639 this_hdr->sh_type = sh_type;
2642 /* If the section has relocs, set up a section header for the
2643 SHT_REL[A] section. If two relocation sections are required for
2644 this section, it is up to the processor-specific back-end to
2645 create the other. */
2646 if ((asect->flags & SEC_RELOC) != 0
2647 && !_bfd_elf_init_reloc_shdr (abfd,
2648 &elf_section_data (asect)->rel_hdr,
2654 /* Fill in the contents of a SHT_GROUP section. */
2657 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2659 bfd_boolean *failedptr = failedptrarg;
2660 unsigned long symindx;
2661 asection *elt, *first;
2665 /* Ignore linker created group section. See elfNN_ia64_object_p in
2667 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2672 if (elf_group_id (sec) != NULL)
2673 symindx = elf_group_id (sec)->udata.i;
2677 /* If called from the assembler, swap_out_syms will have set up
2678 elf_section_syms; If called for "ld -r", use target_index. */
2679 if (elf_section_syms (abfd) != NULL)
2680 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2682 symindx = sec->target_index;
2684 elf_section_data (sec)->this_hdr.sh_info = symindx;
2686 /* The contents won't be allocated for "ld -r" or objcopy. */
2688 if (sec->contents == NULL)
2691 sec->contents = bfd_alloc (abfd, sec->size);
2693 /* Arrange for the section to be written out. */
2694 elf_section_data (sec)->this_hdr.contents = sec->contents;
2695 if (sec->contents == NULL)
2702 loc = sec->contents + sec->size;
2704 /* Get the pointer to the first section in the group that gas
2705 squirreled away here. objcopy arranges for this to be set to the
2706 start of the input section group. */
2707 first = elt = elf_next_in_group (sec);
2709 /* First element is a flag word. Rest of section is elf section
2710 indices for all the sections of the group. Write them backwards
2711 just to keep the group in the same order as given in .section
2712 directives, not that it matters. */
2721 s = s->output_section;
2724 idx = elf_section_data (s)->this_idx;
2725 H_PUT_32 (abfd, idx, loc);
2726 elt = elf_next_in_group (elt);
2731 if ((loc -= 4) != sec->contents)
2734 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2737 /* Assign all ELF section numbers. The dummy first section is handled here
2738 too. The link/info pointers for the standard section types are filled
2739 in here too, while we're at it. */
2742 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2744 struct elf_obj_tdata *t = elf_tdata (abfd);
2746 unsigned int section_number, secn;
2747 Elf_Internal_Shdr **i_shdrp;
2748 struct bfd_elf_section_data *d;
2752 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2754 /* SHT_GROUP sections are in relocatable files only. */
2755 if (link_info == NULL || link_info->relocatable)
2757 /* Put SHT_GROUP sections first. */
2758 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2760 d = elf_section_data (sec);
2762 if (d->this_hdr.sh_type == SHT_GROUP)
2764 if (sec->flags & SEC_LINKER_CREATED)
2766 /* Remove the linker created SHT_GROUP sections. */
2767 bfd_section_list_remove (abfd, sec);
2768 abfd->section_count--;
2771 d->this_idx = section_number++;
2776 for (sec = abfd->sections; sec; sec = sec->next)
2778 d = elf_section_data (sec);
2780 if (d->this_hdr.sh_type != SHT_GROUP)
2781 d->this_idx = section_number++;
2782 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2783 if ((sec->flags & SEC_RELOC) == 0)
2787 d->rel_idx = section_number++;
2788 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2793 d->rel_idx2 = section_number++;
2794 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2800 t->shstrtab_section = section_number++;
2801 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2802 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2804 if (bfd_get_symcount (abfd) > 0)
2806 t->symtab_section = section_number++;
2807 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2808 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2810 t->symtab_shndx_section = section_number++;
2811 t->symtab_shndx_hdr.sh_name
2812 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2813 ".symtab_shndx", FALSE);
2814 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2817 t->strtab_section = section_number++;
2818 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2821 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2822 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2824 elf_numsections (abfd) = section_number;
2825 elf_elfheader (abfd)->e_shnum = section_number;
2827 /* Set up the list of section header pointers, in agreement with the
2829 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2830 if (i_shdrp == NULL)
2833 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2834 if (i_shdrp[0] == NULL)
2836 bfd_release (abfd, i_shdrp);
2840 elf_elfsections (abfd) = i_shdrp;
2842 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2843 if (bfd_get_symcount (abfd) > 0)
2845 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2846 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2848 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2849 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2851 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2852 t->symtab_hdr.sh_link = t->strtab_section;
2855 for (sec = abfd->sections; sec; sec = sec->next)
2857 struct bfd_elf_section_data *d = elf_section_data (sec);
2861 i_shdrp[d->this_idx] = &d->this_hdr;
2862 if (d->rel_idx != 0)
2863 i_shdrp[d->rel_idx] = &d->rel_hdr;
2864 if (d->rel_idx2 != 0)
2865 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2867 /* Fill in the sh_link and sh_info fields while we're at it. */
2869 /* sh_link of a reloc section is the section index of the symbol
2870 table. sh_info is the section index of the section to which
2871 the relocation entries apply. */
2872 if (d->rel_idx != 0)
2874 d->rel_hdr.sh_link = t->symtab_section;
2875 d->rel_hdr.sh_info = d->this_idx;
2877 if (d->rel_idx2 != 0)
2879 d->rel_hdr2->sh_link = t->symtab_section;
2880 d->rel_hdr2->sh_info = d->this_idx;
2883 /* We need to set up sh_link for SHF_LINK_ORDER. */
2884 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2886 s = elf_linked_to_section (sec);
2889 /* elf_linked_to_section points to the input section. */
2890 if (link_info != NULL)
2892 /* Check discarded linkonce section. */
2893 if (elf_discarded_section (s))
2896 (*_bfd_error_handler)
2897 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2898 abfd, d->this_hdr.bfd_section,
2900 /* Point to the kept section if it has the same
2901 size as the discarded one. */
2902 kept = _bfd_elf_check_kept_section (s, link_info);
2905 bfd_set_error (bfd_error_bad_value);
2911 s = s->output_section;
2912 BFD_ASSERT (s != NULL);
2916 /* Handle objcopy. */
2917 if (s->output_section == NULL)
2919 (*_bfd_error_handler)
2920 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2921 abfd, d->this_hdr.bfd_section, s, s->owner);
2922 bfd_set_error (bfd_error_bad_value);
2925 s = s->output_section;
2927 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2932 The Intel C compiler generates SHT_IA_64_UNWIND with
2933 SHF_LINK_ORDER. But it doesn't set the sh_link or
2934 sh_info fields. Hence we could get the situation
2936 const struct elf_backend_data *bed
2937 = get_elf_backend_data (abfd);
2938 if (bed->link_order_error_handler)
2939 bed->link_order_error_handler
2940 (_("%B: warning: sh_link not set for section `%A'"),
2945 switch (d->this_hdr.sh_type)
2949 /* A reloc section which we are treating as a normal BFD
2950 section. sh_link is the section index of the symbol
2951 table. sh_info is the section index of the section to
2952 which the relocation entries apply. We assume that an
2953 allocated reloc section uses the dynamic symbol table.
2954 FIXME: How can we be sure? */
2955 s = bfd_get_section_by_name (abfd, ".dynsym");
2957 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2959 /* We look up the section the relocs apply to by name. */
2961 if (d->this_hdr.sh_type == SHT_REL)
2965 s = bfd_get_section_by_name (abfd, name);
2967 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2971 /* We assume that a section named .stab*str is a stabs
2972 string section. We look for a section with the same name
2973 but without the trailing ``str'', and set its sh_link
2974 field to point to this section. */
2975 if (CONST_STRNEQ (sec->name, ".stab")
2976 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2981 len = strlen (sec->name);
2982 alc = bfd_malloc (len - 2);
2985 memcpy (alc, sec->name, len - 3);
2986 alc[len - 3] = '\0';
2987 s = bfd_get_section_by_name (abfd, alc);
2991 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2993 /* This is a .stab section. */
2994 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2995 elf_section_data (s)->this_hdr.sh_entsize
2996 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3003 case SHT_GNU_verneed:
3004 case SHT_GNU_verdef:
3005 /* sh_link is the section header index of the string table
3006 used for the dynamic entries, or the symbol table, or the
3008 s = bfd_get_section_by_name (abfd, ".dynstr");
3010 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3013 case SHT_GNU_LIBLIST:
3014 /* sh_link is the section header index of the prelink library
3015 list used for the dynamic entries, or the symbol table, or
3016 the version strings. */
3017 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3018 ? ".dynstr" : ".gnu.libstr");
3020 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3025 case SHT_GNU_versym:
3026 /* sh_link is the section header index of the symbol table
3027 this hash table or version table is for. */
3028 s = bfd_get_section_by_name (abfd, ".dynsym");
3030 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3034 d->this_hdr.sh_link = t->symtab_section;
3038 for (secn = 1; secn < section_number; ++secn)
3039 if (i_shdrp[secn] == NULL)
3040 i_shdrp[secn] = i_shdrp[0];
3042 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3043 i_shdrp[secn]->sh_name);
3047 /* Map symbol from it's internal number to the external number, moving
3048 all local symbols to be at the head of the list. */
3051 sym_is_global (bfd *abfd, asymbol *sym)
3053 /* If the backend has a special mapping, use it. */
3054 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3055 if (bed->elf_backend_sym_is_global)
3056 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3058 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3059 || bfd_is_und_section (bfd_get_section (sym))
3060 || bfd_is_com_section (bfd_get_section (sym)));
3063 /* Don't output section symbols for sections that are not going to be
3064 output. Also, don't output section symbols for reloc and other
3065 special sections. */
3068 ignore_section_sym (bfd *abfd, asymbol *sym)
3070 return ((sym->flags & BSF_SECTION_SYM) != 0
3072 || (sym->section->owner != abfd
3073 && (sym->section->output_section->owner != abfd
3074 || sym->section->output_offset != 0))));
3078 elf_map_symbols (bfd *abfd)
3080 unsigned int symcount = bfd_get_symcount (abfd);
3081 asymbol **syms = bfd_get_outsymbols (abfd);
3082 asymbol **sect_syms;
3083 unsigned int num_locals = 0;
3084 unsigned int num_globals = 0;
3085 unsigned int num_locals2 = 0;
3086 unsigned int num_globals2 = 0;
3093 fprintf (stderr, "elf_map_symbols\n");
3097 for (asect = abfd->sections; asect; asect = asect->next)
3099 if (max_index < asect->index)
3100 max_index = asect->index;
3104 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3105 if (sect_syms == NULL)
3107 elf_section_syms (abfd) = sect_syms;
3108 elf_num_section_syms (abfd) = max_index;
3110 /* Init sect_syms entries for any section symbols we have already
3111 decided to output. */
3112 for (idx = 0; idx < symcount; idx++)
3114 asymbol *sym = syms[idx];
3116 if ((sym->flags & BSF_SECTION_SYM) != 0
3117 && !ignore_section_sym (abfd, sym))
3119 asection *sec = sym->section;
3121 if (sec->owner != abfd)
3122 sec = sec->output_section;
3124 sect_syms[sec->index] = syms[idx];
3128 /* Classify all of the symbols. */
3129 for (idx = 0; idx < symcount; idx++)
3131 if (ignore_section_sym (abfd, syms[idx]))
3133 if (!sym_is_global (abfd, syms[idx]))
3139 /* We will be adding a section symbol for each normal BFD section. Most
3140 sections will already have a section symbol in outsymbols, but
3141 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3142 at least in that case. */
3143 for (asect = abfd->sections; asect; asect = asect->next)
3145 if (sect_syms[asect->index] == NULL)
3147 if (!sym_is_global (abfd, asect->symbol))
3154 /* Now sort the symbols so the local symbols are first. */
3155 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3157 if (new_syms == NULL)
3160 for (idx = 0; idx < symcount; idx++)
3162 asymbol *sym = syms[idx];
3165 if (ignore_section_sym (abfd, sym))
3167 if (!sym_is_global (abfd, sym))
3170 i = num_locals + num_globals2++;
3172 sym->udata.i = i + 1;
3174 for (asect = abfd->sections; asect; asect = asect->next)
3176 if (sect_syms[asect->index] == NULL)
3178 asymbol *sym = asect->symbol;
3181 sect_syms[asect->index] = sym;
3182 if (!sym_is_global (abfd, sym))
3185 i = num_locals + num_globals2++;
3187 sym->udata.i = i + 1;
3191 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3193 elf_num_locals (abfd) = num_locals;
3194 elf_num_globals (abfd) = num_globals;
3198 /* Align to the maximum file alignment that could be required for any
3199 ELF data structure. */
3201 static inline file_ptr
3202 align_file_position (file_ptr off, int align)
3204 return (off + align - 1) & ~(align - 1);
3207 /* Assign a file position to a section, optionally aligning to the
3208 required section alignment. */
3211 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3215 if (align && i_shdrp->sh_addralign > 1)
3216 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3217 i_shdrp->sh_offset = offset;
3218 if (i_shdrp->bfd_section != NULL)
3219 i_shdrp->bfd_section->filepos = offset;
3220 if (i_shdrp->sh_type != SHT_NOBITS)
3221 offset += i_shdrp->sh_size;
3225 /* Compute the file positions we are going to put the sections at, and
3226 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3227 is not NULL, this is being called by the ELF backend linker. */
3230 _bfd_elf_compute_section_file_positions (bfd *abfd,
3231 struct bfd_link_info *link_info)
3233 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3235 struct bfd_strtab_hash *strtab = NULL;
3236 Elf_Internal_Shdr *shstrtab_hdr;
3238 if (abfd->output_has_begun)
3241 /* Do any elf backend specific processing first. */
3242 if (bed->elf_backend_begin_write_processing)
3243 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3245 if (! prep_headers (abfd))
3248 /* Post process the headers if necessary. */
3249 if (bed->elf_backend_post_process_headers)
3250 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3253 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3257 if (!assign_section_numbers (abfd, link_info))
3260 /* The backend linker builds symbol table information itself. */
3261 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3263 /* Non-zero if doing a relocatable link. */
3264 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3266 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3270 if (link_info == NULL)
3272 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3277 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3278 /* sh_name was set in prep_headers. */
3279 shstrtab_hdr->sh_type = SHT_STRTAB;
3280 shstrtab_hdr->sh_flags = 0;
3281 shstrtab_hdr->sh_addr = 0;
3282 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3283 shstrtab_hdr->sh_entsize = 0;
3284 shstrtab_hdr->sh_link = 0;
3285 shstrtab_hdr->sh_info = 0;
3286 /* sh_offset is set in assign_file_positions_except_relocs. */
3287 shstrtab_hdr->sh_addralign = 1;
3289 if (!assign_file_positions_except_relocs (abfd, link_info))
3292 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3295 Elf_Internal_Shdr *hdr;
3297 off = elf_tdata (abfd)->next_file_pos;
3299 hdr = &elf_tdata (abfd)->symtab_hdr;
3300 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3302 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3303 if (hdr->sh_size != 0)
3304 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3306 hdr = &elf_tdata (abfd)->strtab_hdr;
3307 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3309 elf_tdata (abfd)->next_file_pos = off;
3311 /* Now that we know where the .strtab section goes, write it
3313 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3314 || ! _bfd_stringtab_emit (abfd, strtab))
3316 _bfd_stringtab_free (strtab);
3319 abfd->output_has_begun = TRUE;
3324 /* Make an initial estimate of the size of the program header. If we
3325 get the number wrong here, we'll redo section placement. */
3327 static bfd_size_type
3328 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3332 const struct elf_backend_data *bed;
3334 /* Assume we will need exactly two PT_LOAD segments: one for text
3335 and one for data. */
3338 s = bfd_get_section_by_name (abfd, ".interp");
3339 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3341 /* If we have a loadable interpreter section, we need a
3342 PT_INTERP segment. In this case, assume we also need a
3343 PT_PHDR segment, although that may not be true for all
3348 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3350 /* We need a PT_DYNAMIC segment. */
3356 /* We need a PT_GNU_RELRO segment. */
3360 if (elf_tdata (abfd)->eh_frame_hdr)
3362 /* We need a PT_GNU_EH_FRAME segment. */
3366 if (elf_tdata (abfd)->stack_flags)
3368 /* We need a PT_GNU_STACK segment. */
3372 for (s = abfd->sections; s != NULL; s = s->next)
3374 if ((s->flags & SEC_LOAD) != 0
3375 && CONST_STRNEQ (s->name, ".note"))
3377 /* We need a PT_NOTE segment. */
3379 /* Try to create just one PT_NOTE segment
3380 for all adjacent loadable .note* sections.
3381 gABI requires that within a PT_NOTE segment
3382 (and also inside of each SHT_NOTE section)
3383 each note is padded to a multiple of 4 size,
3384 so we check whether the sections are correctly
3386 if (s->alignment_power == 2)
3387 while (s->next != NULL
3388 && s->next->alignment_power == 2
3389 && (s->next->flags & SEC_LOAD) != 0
3390 && CONST_STRNEQ (s->next->name, ".note"))
3395 for (s = abfd->sections; s != NULL; s = s->next)
3397 if (s->flags & SEC_THREAD_LOCAL)
3399 /* We need a PT_TLS segment. */
3405 /* Let the backend count up any program headers it might need. */
3406 bed = get_elf_backend_data (abfd);
3407 if (bed->elf_backend_additional_program_headers)
3411 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3417 return segs * bed->s->sizeof_phdr;
3420 /* Find the segment that contains the output_section of section. */
3423 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3425 struct elf_segment_map *m;
3426 Elf_Internal_Phdr *p;
3428 for (m = elf_tdata (abfd)->segment_map,
3429 p = elf_tdata (abfd)->phdr;
3435 for (i = m->count - 1; i >= 0; i--)
3436 if (m->sections[i] == section)
3443 /* Create a mapping from a set of sections to a program segment. */
3445 static struct elf_segment_map *
3446 make_mapping (bfd *abfd,
3447 asection **sections,
3452 struct elf_segment_map *m;
3457 amt = sizeof (struct elf_segment_map);
3458 amt += (to - from - 1) * sizeof (asection *);
3459 m = bfd_zalloc (abfd, amt);
3463 m->p_type = PT_LOAD;
3464 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3465 m->sections[i - from] = *hdrpp;
3466 m->count = to - from;
3468 if (from == 0 && phdr)
3470 /* Include the headers in the first PT_LOAD segment. */
3471 m->includes_filehdr = 1;
3472 m->includes_phdrs = 1;
3478 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3481 struct elf_segment_map *
3482 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3484 struct elf_segment_map *m;
3486 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3490 m->p_type = PT_DYNAMIC;
3492 m->sections[0] = dynsec;
3497 /* Possibly add or remove segments from the segment map. */
3500 elf_modify_segment_map (bfd *abfd,
3501 struct bfd_link_info *info,
3502 bfd_boolean remove_empty_load)
3504 struct elf_segment_map **m;
3505 const struct elf_backend_data *bed;
3507 /* The placement algorithm assumes that non allocated sections are
3508 not in PT_LOAD segments. We ensure this here by removing such
3509 sections from the segment map. We also remove excluded
3510 sections. Finally, any PT_LOAD segment without sections is
3512 m = &elf_tdata (abfd)->segment_map;
3515 unsigned int i, new_count;
3517 for (new_count = 0, i = 0; i < (*m)->count; i++)
3519 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3520 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3521 || (*m)->p_type != PT_LOAD))
3523 (*m)->sections[new_count] = (*m)->sections[i];
3527 (*m)->count = new_count;
3529 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3535 bed = get_elf_backend_data (abfd);
3536 if (bed->elf_backend_modify_segment_map != NULL)
3538 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3545 /* Set up a mapping from BFD sections to program segments. */
3548 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3551 struct elf_segment_map *m;
3552 asection **sections = NULL;
3553 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3554 bfd_boolean no_user_phdrs;
3556 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3557 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3561 struct elf_segment_map *mfirst;
3562 struct elf_segment_map **pm;
3565 unsigned int phdr_index;
3566 bfd_vma maxpagesize;
3568 bfd_boolean phdr_in_segment = TRUE;
3569 bfd_boolean writable;
3571 asection *first_tls = NULL;
3572 asection *dynsec, *eh_frame_hdr;
3575 /* Select the allocated sections, and sort them. */
3577 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3578 if (sections == NULL)
3582 for (s = abfd->sections; s != NULL; s = s->next)
3584 if ((s->flags & SEC_ALLOC) != 0)
3590 BFD_ASSERT (i <= bfd_count_sections (abfd));
3593 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3595 /* Build the mapping. */
3600 /* If we have a .interp section, then create a PT_PHDR segment for
3601 the program headers and a PT_INTERP segment for the .interp
3603 s = bfd_get_section_by_name (abfd, ".interp");
3604 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3606 amt = sizeof (struct elf_segment_map);
3607 m = bfd_zalloc (abfd, amt);
3611 m->p_type = PT_PHDR;
3612 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3613 m->p_flags = PF_R | PF_X;
3614 m->p_flags_valid = 1;
3615 m->includes_phdrs = 1;
3620 amt = sizeof (struct elf_segment_map);
3621 m = bfd_zalloc (abfd, amt);
3625 m->p_type = PT_INTERP;
3633 /* Look through the sections. We put sections in the same program
3634 segment when the start of the second section can be placed within
3635 a few bytes of the end of the first section. */
3639 maxpagesize = bed->maxpagesize;
3641 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3643 && (dynsec->flags & SEC_LOAD) == 0)
3646 /* Deal with -Ttext or something similar such that the first section
3647 is not adjacent to the program headers. This is an
3648 approximation, since at this point we don't know exactly how many
3649 program headers we will need. */
3652 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3654 if (phdr_size == (bfd_size_type) -1)
3655 phdr_size = get_program_header_size (abfd, info);
3656 if ((abfd->flags & D_PAGED) == 0
3657 || sections[0]->lma < phdr_size
3658 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3659 phdr_in_segment = FALSE;
3662 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3665 bfd_boolean new_segment;
3669 /* See if this section and the last one will fit in the same
3672 if (last_hdr == NULL)
3674 /* If we don't have a segment yet, then we don't need a new
3675 one (we build the last one after this loop). */
3676 new_segment = FALSE;
3678 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3680 /* If this section has a different relation between the
3681 virtual address and the load address, then we need a new
3685 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3686 < BFD_ALIGN (hdr->lma, maxpagesize))
3688 /* If putting this section in this segment would force us to
3689 skip a page in the segment, then we need a new segment. */
3692 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3693 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3695 /* We don't want to put a loadable section after a
3696 nonloadable section in the same segment.
3697 Consider .tbss sections as loadable for this purpose. */
3700 else if ((abfd->flags & D_PAGED) == 0)
3702 /* If the file is not demand paged, which means that we
3703 don't require the sections to be correctly aligned in the
3704 file, then there is no other reason for a new segment. */
3705 new_segment = FALSE;
3708 && (hdr->flags & SEC_READONLY) == 0
3709 && (((last_hdr->lma + last_size - 1)
3710 & ~(maxpagesize - 1))
3711 != (hdr->lma & ~(maxpagesize - 1))))
3713 /* We don't want to put a writable section in a read only
3714 segment, unless they are on the same page in memory
3715 anyhow. We already know that the last section does not
3716 bring us past the current section on the page, so the
3717 only case in which the new section is not on the same
3718 page as the previous section is when the previous section
3719 ends precisely on a page boundary. */
3724 /* Otherwise, we can use the same segment. */
3725 new_segment = FALSE;
3728 /* Allow interested parties a chance to override our decision. */
3729 if (last_hdr && info->callbacks->override_segment_assignment)
3730 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3734 if ((hdr->flags & SEC_READONLY) == 0)
3737 /* .tbss sections effectively have zero size. */
3738 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3739 != SEC_THREAD_LOCAL)
3740 last_size = hdr->size;
3746 /* We need a new program segment. We must create a new program
3747 header holding all the sections from phdr_index until hdr. */
3749 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3756 if ((hdr->flags & SEC_READONLY) == 0)
3762 /* .tbss sections effectively have zero size. */
3763 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3764 last_size = hdr->size;
3768 phdr_in_segment = FALSE;
3771 /* Create a final PT_LOAD program segment. */
3772 if (last_hdr != NULL)
3774 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3782 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3785 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3792 /* For each batch of consecutive loadable .note sections,
3793 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3794 because if we link together nonloadable .note sections and
3795 loadable .note sections, we will generate two .note sections
3796 in the output file. FIXME: Using names for section types is
3798 for (s = abfd->sections; s != NULL; s = s->next)
3800 if ((s->flags & SEC_LOAD) != 0
3801 && CONST_STRNEQ (s->name, ".note"))
3805 amt = sizeof (struct elf_segment_map);
3806 if (s->alignment_power == 2)
3807 for (s2 = s; s2->next != NULL; s2 = s2->next)
3809 if (s2->next->alignment_power == 2
3810 && (s2->next->flags & SEC_LOAD) != 0
3811 && CONST_STRNEQ (s2->next->name, ".note")
3812 && align_power (s2->vma + s2->size, 2)
3818 amt += (count - 1) * sizeof (asection *);
3819 m = bfd_zalloc (abfd, amt);
3823 m->p_type = PT_NOTE;
3827 m->sections[m->count - count--] = s;
3828 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3831 m->sections[m->count - 1] = s;
3832 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3836 if (s->flags & SEC_THREAD_LOCAL)
3844 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3849 amt = sizeof (struct elf_segment_map);
3850 amt += (tls_count - 1) * sizeof (asection *);
3851 m = bfd_zalloc (abfd, amt);
3856 m->count = tls_count;
3857 /* Mandated PF_R. */
3859 m->p_flags_valid = 1;
3860 for (i = 0; i < tls_count; ++i)
3862 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3863 m->sections[i] = first_tls;
3864 first_tls = first_tls->next;
3871 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3873 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3874 if (eh_frame_hdr != NULL
3875 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3877 amt = sizeof (struct elf_segment_map);
3878 m = bfd_zalloc (abfd, amt);
3882 m->p_type = PT_GNU_EH_FRAME;
3884 m->sections[0] = eh_frame_hdr->output_section;
3890 if (elf_tdata (abfd)->stack_flags)
3892 amt = sizeof (struct elf_segment_map);
3893 m = bfd_zalloc (abfd, amt);
3897 m->p_type = PT_GNU_STACK;
3898 m->p_flags = elf_tdata (abfd)->stack_flags;
3899 m->p_flags_valid = 1;
3907 for (m = mfirst; m != NULL; m = m->next)
3909 if (m->p_type == PT_LOAD)
3911 asection *last = m->sections[m->count - 1];
3912 bfd_vma vaddr = m->sections[0]->vma;
3913 bfd_vma filesz = last->vma - vaddr + last->size;
3915 if (vaddr < info->relro_end
3916 && vaddr >= info->relro_start
3917 && (vaddr + filesz) >= info->relro_end)
3922 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3925 amt = sizeof (struct elf_segment_map);
3926 m = bfd_zalloc (abfd, amt);
3930 m->p_type = PT_GNU_RELRO;
3932 m->p_flags_valid = 1;
3940 elf_tdata (abfd)->segment_map = mfirst;
3943 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
3946 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3948 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
3953 if (sections != NULL)
3958 /* Sort sections by address. */
3961 elf_sort_sections (const void *arg1, const void *arg2)
3963 const asection *sec1 = *(const asection **) arg1;
3964 const asection *sec2 = *(const asection **) arg2;
3965 bfd_size_type size1, size2;
3967 /* Sort by LMA first, since this is the address used to
3968 place the section into a segment. */
3969 if (sec1->lma < sec2->lma)
3971 else if (sec1->lma > sec2->lma)
3974 /* Then sort by VMA. Normally the LMA and the VMA will be
3975 the same, and this will do nothing. */
3976 if (sec1->vma < sec2->vma)
3978 else if (sec1->vma > sec2->vma)
3981 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3983 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3989 /* If the indicies are the same, do not return 0
3990 here, but continue to try the next comparison. */
3991 if (sec1->target_index - sec2->target_index != 0)
3992 return sec1->target_index - sec2->target_index;
3997 else if (TOEND (sec2))
4002 /* Sort by size, to put zero sized sections
4003 before others at the same address. */
4005 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4006 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4013 return sec1->target_index - sec2->target_index;
4016 /* Ian Lance Taylor writes:
4018 We shouldn't be using % with a negative signed number. That's just
4019 not good. We have to make sure either that the number is not
4020 negative, or that the number has an unsigned type. When the types
4021 are all the same size they wind up as unsigned. When file_ptr is a
4022 larger signed type, the arithmetic winds up as signed long long,
4025 What we're trying to say here is something like ``increase OFF by
4026 the least amount that will cause it to be equal to the VMA modulo
4028 /* In other words, something like:
4030 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4031 off_offset = off % bed->maxpagesize;
4032 if (vma_offset < off_offset)
4033 adjustment = vma_offset + bed->maxpagesize - off_offset;
4035 adjustment = vma_offset - off_offset;
4037 which can can be collapsed into the expression below. */
4040 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4042 return ((vma - off) % maxpagesize);
4046 print_segment_map (const struct elf_segment_map *m)
4049 const char *pt = get_segment_type (m->p_type);
4054 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4055 sprintf (buf, "LOPROC+%7.7x",
4056 (unsigned int) (m->p_type - PT_LOPROC));
4057 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4058 sprintf (buf, "LOOS+%7.7x",
4059 (unsigned int) (m->p_type - PT_LOOS));
4061 snprintf (buf, sizeof (buf), "%8.8x",
4062 (unsigned int) m->p_type);
4065 fprintf (stderr, "%s:", pt);
4066 for (j = 0; j < m->count; j++)
4067 fprintf (stderr, " %s", m->sections [j]->name);
4071 /* Assign file positions to the sections based on the mapping from
4072 sections to segments. This function also sets up some fields in
4076 assign_file_positions_for_load_sections (bfd *abfd,
4077 struct bfd_link_info *link_info)
4079 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4080 struct elf_segment_map *m;
4081 Elf_Internal_Phdr *phdrs;
4082 Elf_Internal_Phdr *p;
4084 bfd_size_type maxpagesize;
4088 if (link_info == NULL
4089 && !elf_modify_segment_map (abfd, link_info, FALSE))
4093 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4096 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4097 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4098 elf_elfheader (abfd)->e_phnum = alloc;
4100 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4101 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4103 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4104 >= alloc * bed->s->sizeof_phdr);
4108 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4112 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4113 elf_tdata (abfd)->phdr = phdrs;
4118 if ((abfd->flags & D_PAGED) != 0)
4119 maxpagesize = bed->maxpagesize;
4121 off = bed->s->sizeof_ehdr;
4122 off += alloc * bed->s->sizeof_phdr;
4124 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4126 m = m->next, p++, j++)
4130 bfd_boolean no_contents;
4132 /* If elf_segment_map is not from map_sections_to_segments, the
4133 sections may not be correctly ordered. NOTE: sorting should
4134 not be done to the PT_NOTE section of a corefile, which may
4135 contain several pseudo-sections artificially created by bfd.
4136 Sorting these pseudo-sections breaks things badly. */
4138 && !(elf_elfheader (abfd)->e_type == ET_CORE
4139 && m->p_type == PT_NOTE))
4140 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4143 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4144 number of sections with contents contributing to both p_filesz
4145 and p_memsz, followed by a number of sections with no contents
4146 that just contribute to p_memsz. In this loop, OFF tracks next
4147 available file offset for PT_LOAD and PT_NOTE segments. */
4148 p->p_type = m->p_type;
4149 p->p_flags = m->p_flags;
4154 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4156 if (m->p_paddr_valid)
4157 p->p_paddr = m->p_paddr;
4158 else if (m->count == 0)
4161 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4163 if (p->p_type == PT_LOAD
4164 && (abfd->flags & D_PAGED) != 0)
4166 /* p_align in demand paged PT_LOAD segments effectively stores
4167 the maximum page size. When copying an executable with
4168 objcopy, we set m->p_align from the input file. Use this
4169 value for maxpagesize rather than bed->maxpagesize, which
4170 may be different. Note that we use maxpagesize for PT_TLS
4171 segment alignment later in this function, so we are relying
4172 on at least one PT_LOAD segment appearing before a PT_TLS
4174 if (m->p_align_valid)
4175 maxpagesize = m->p_align;
4177 p->p_align = maxpagesize;
4179 else if (m->p_align_valid)
4180 p->p_align = m->p_align;
4181 else if (m->count == 0)
4182 p->p_align = 1 << bed->s->log_file_align;
4186 no_contents = FALSE;
4188 if (p->p_type == PT_LOAD
4191 bfd_size_type align;
4192 unsigned int align_power = 0;
4194 if (m->p_align_valid)
4198 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4200 unsigned int secalign;
4202 secalign = bfd_get_section_alignment (abfd, *secpp);
4203 if (secalign > align_power)
4204 align_power = secalign;
4206 align = (bfd_size_type) 1 << align_power;
4207 if (align < maxpagesize)
4208 align = maxpagesize;
4211 for (i = 0; i < m->count; i++)
4212 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4213 /* If we aren't making room for this section, then
4214 it must be SHT_NOBITS regardless of what we've
4215 set via struct bfd_elf_special_section. */
4216 elf_section_type (m->sections[i]) = SHT_NOBITS;
4218 /* Find out whether this segment contains any loadable
4219 sections. If the first section isn't loadable, the same
4220 holds for any other sections. */
4222 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4224 /* If a segment starts with .tbss, we need to look
4225 at the next section to decide whether the segment
4226 has any loadable sections. */
4227 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4235 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4239 /* We shouldn't need to align the segment on disk since
4240 the segment doesn't need file space, but the gABI
4241 arguably requires the alignment and glibc ld.so
4242 checks it. So to comply with the alignment
4243 requirement but not waste file space, we adjust
4244 p_offset for just this segment. (OFF_ADJUST is
4245 subtracted from OFF later.) This may put p_offset
4246 past the end of file, but that shouldn't matter. */
4251 /* Make sure the .dynamic section is the first section in the
4252 PT_DYNAMIC segment. */
4253 else if (p->p_type == PT_DYNAMIC
4255 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4258 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4260 bfd_set_error (bfd_error_bad_value);
4263 /* Set the note section type to SHT_NOTE. */
4264 else if (p->p_type == PT_NOTE)
4265 for (i = 0; i < m->count; i++)
4266 elf_section_type (m->sections[i]) = SHT_NOTE;
4272 if (m->includes_filehdr)
4274 if (!m->p_flags_valid)
4276 p->p_filesz = bed->s->sizeof_ehdr;
4277 p->p_memsz = bed->s->sizeof_ehdr;
4280 BFD_ASSERT (p->p_type == PT_LOAD);
4282 if (p->p_vaddr < (bfd_vma) off)
4284 (*_bfd_error_handler)
4285 (_("%B: Not enough room for program headers, try linking with -N"),
4287 bfd_set_error (bfd_error_bad_value);
4292 if (!m->p_paddr_valid)
4297 if (m->includes_phdrs)
4299 if (!m->p_flags_valid)
4302 if (!m->includes_filehdr)
4304 p->p_offset = bed->s->sizeof_ehdr;
4308 BFD_ASSERT (p->p_type == PT_LOAD);
4309 p->p_vaddr -= off - p->p_offset;
4310 if (!m->p_paddr_valid)
4311 p->p_paddr -= off - p->p_offset;
4315 p->p_filesz += alloc * bed->s->sizeof_phdr;
4316 p->p_memsz += alloc * bed->s->sizeof_phdr;
4319 if (p->p_type == PT_LOAD
4320 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4322 if (!m->includes_filehdr && !m->includes_phdrs)
4328 adjust = off - (p->p_offset + p->p_filesz);
4330 p->p_filesz += adjust;
4331 p->p_memsz += adjust;
4335 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4336 maps. Set filepos for sections in PT_LOAD segments, and in
4337 core files, for sections in PT_NOTE segments.
4338 assign_file_positions_for_non_load_sections will set filepos
4339 for other sections and update p_filesz for other segments. */
4340 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4343 bfd_size_type align;
4344 Elf_Internal_Shdr *this_hdr;
4347 this_hdr = &elf_section_data (sec)->this_hdr;
4348 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4350 if ((p->p_type == PT_LOAD
4351 || p->p_type == PT_TLS)
4352 && (this_hdr->sh_type != SHT_NOBITS
4353 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4354 && ((this_hdr->sh_flags & SHF_TLS) == 0
4355 || p->p_type == PT_TLS))))
4357 bfd_signed_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
4361 (*_bfd_error_handler)
4362 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4363 abfd, sec, (unsigned long) sec->lma);
4366 p->p_memsz += adjust;
4368 if (this_hdr->sh_type != SHT_NOBITS)
4371 p->p_filesz += adjust;
4375 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4377 /* The section at i == 0 is the one that actually contains
4381 this_hdr->sh_offset = sec->filepos = off;
4382 off += this_hdr->sh_size;
4383 p->p_filesz = this_hdr->sh_size;
4389 /* The rest are fake sections that shouldn't be written. */
4398 if (p->p_type == PT_LOAD)
4400 this_hdr->sh_offset = sec->filepos = off;
4401 if (this_hdr->sh_type != SHT_NOBITS)
4402 off += this_hdr->sh_size;
4405 if (this_hdr->sh_type != SHT_NOBITS)
4407 p->p_filesz += this_hdr->sh_size;
4408 /* A load section without SHF_ALLOC is something like
4409 a note section in a PT_NOTE segment. These take
4410 file space but are not loaded into memory. */
4411 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4412 p->p_memsz += this_hdr->sh_size;
4414 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4416 if (p->p_type == PT_TLS)
4417 p->p_memsz += this_hdr->sh_size;
4419 /* .tbss is special. It doesn't contribute to p_memsz of
4421 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4422 p->p_memsz += this_hdr->sh_size;
4425 if (align > p->p_align
4426 && !m->p_align_valid
4427 && (p->p_type != PT_LOAD
4428 || (abfd->flags & D_PAGED) == 0))
4432 if (!m->p_flags_valid)
4435 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4437 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4443 /* Check that all sections are in a PT_LOAD segment.
4444 Don't check funky gdb generated core files. */
4445 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4446 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4448 Elf_Internal_Shdr *this_hdr;
4452 this_hdr = &(elf_section_data(sec)->this_hdr);
4453 if (this_hdr->sh_size != 0
4454 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4456 (*_bfd_error_handler)
4457 (_("%B: section `%A' can't be allocated in segment %d"),
4459 print_segment_map (m);
4460 bfd_set_error (bfd_error_bad_value);
4466 elf_tdata (abfd)->next_file_pos = off;
4470 /* Assign file positions for the other sections. */
4473 assign_file_positions_for_non_load_sections (bfd *abfd,
4474 struct bfd_link_info *link_info)
4476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4477 Elf_Internal_Shdr **i_shdrpp;
4478 Elf_Internal_Shdr **hdrpp;
4479 Elf_Internal_Phdr *phdrs;
4480 Elf_Internal_Phdr *p;
4481 struct elf_segment_map *m;
4482 bfd_vma filehdr_vaddr, filehdr_paddr;
4483 bfd_vma phdrs_vaddr, phdrs_paddr;
4485 unsigned int num_sec;
4489 i_shdrpp = elf_elfsections (abfd);
4490 num_sec = elf_numsections (abfd);
4491 off = elf_tdata (abfd)->next_file_pos;
4492 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4494 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4495 Elf_Internal_Shdr *hdr;
4498 if (hdr->bfd_section != NULL
4499 && (hdr->bfd_section->filepos != 0
4500 || (hdr->sh_type == SHT_NOBITS
4501 && hdr->contents == NULL)))
4502 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4503 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4505 if (hdr->sh_size != 0)
4506 ((*_bfd_error_handler)
4507 (_("%B: warning: allocated section `%s' not in segment"),
4509 (hdr->bfd_section == NULL
4511 : hdr->bfd_section->name)));
4512 /* We don't need to page align empty sections. */
4513 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4514 off += vma_page_aligned_bias (hdr->sh_addr, off,
4517 off += vma_page_aligned_bias (hdr->sh_addr, off,
4519 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4522 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4523 && hdr->bfd_section == NULL)
4524 || hdr == i_shdrpp[tdata->symtab_section]
4525 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4526 || hdr == i_shdrpp[tdata->strtab_section])
4527 hdr->sh_offset = -1;
4529 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4532 /* Now that we have set the section file positions, we can set up
4533 the file positions for the non PT_LOAD segments. */
4537 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4539 phdrs = elf_tdata (abfd)->phdr;
4540 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4545 if (p->p_type != PT_LOAD)
4548 if (m->includes_filehdr)
4550 filehdr_vaddr = p->p_vaddr;
4551 filehdr_paddr = p->p_paddr;
4553 if (m->includes_phdrs)
4555 phdrs_vaddr = p->p_vaddr;
4556 phdrs_paddr = p->p_paddr;
4557 if (m->includes_filehdr)
4559 phdrs_vaddr += bed->s->sizeof_ehdr;
4560 phdrs_paddr += bed->s->sizeof_ehdr;
4565 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4571 if (p->p_type != PT_LOAD
4572 && (p->p_type != PT_NOTE
4573 || bfd_get_format (abfd) != bfd_core))
4575 Elf_Internal_Shdr *hdr;
4578 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4580 sect = m->sections[m->count - 1];
4581 hdr = &elf_section_data (sect)->this_hdr;
4582 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4583 if (hdr->sh_type != SHT_NOBITS)
4584 p->p_filesz += hdr->sh_size;
4586 if (p->p_type == PT_GNU_RELRO)
4588 /* When we get here, we are copying executable
4589 or shared library. But we need to use the same
4591 Elf_Internal_Phdr *lp;
4593 for (lp = phdrs; lp < phdrs + count; ++lp)
4595 if (lp->p_type == PT_LOAD
4596 && lp->p_paddr == p->p_paddr)
4600 if (lp < phdrs + count)
4602 /* We should use p_size if it is valid since it
4603 may contain the first few bytes of the next
4604 SEC_ALLOC section. */
4605 if (m->p_size_valid)
4606 p->p_filesz = m->p_size;
4609 p->p_vaddr = lp->p_vaddr;
4610 p->p_offset = lp->p_offset;
4611 p->p_memsz = p->p_filesz;
4618 p->p_offset = m->sections[0]->filepos;
4623 if (m->includes_filehdr)
4625 p->p_vaddr = filehdr_vaddr;
4626 if (! m->p_paddr_valid)
4627 p->p_paddr = filehdr_paddr;
4629 else if (m->includes_phdrs)
4631 p->p_vaddr = phdrs_vaddr;
4632 if (! m->p_paddr_valid)
4633 p->p_paddr = phdrs_paddr;
4635 else if (p->p_type == PT_GNU_RELRO)
4637 Elf_Internal_Phdr *lp;
4639 for (lp = phdrs; lp < phdrs + count; ++lp)
4641 if (lp->p_type == PT_LOAD
4642 && lp->p_vaddr <= link_info->relro_end
4643 && lp->p_vaddr >= link_info->relro_start
4644 && (lp->p_vaddr + lp->p_filesz
4645 >= link_info->relro_end))
4649 if (lp < phdrs + count
4650 && link_info->relro_end > lp->p_vaddr)
4652 p->p_vaddr = lp->p_vaddr;
4653 p->p_paddr = lp->p_paddr;
4654 p->p_offset = lp->p_offset;
4655 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4656 p->p_memsz = p->p_filesz;
4658 p->p_flags = (lp->p_flags & ~PF_W);
4662 memset (p, 0, sizeof *p);
4663 p->p_type = PT_NULL;
4669 elf_tdata (abfd)->next_file_pos = off;
4674 /* Work out the file positions of all the sections. This is called by
4675 _bfd_elf_compute_section_file_positions. All the section sizes and
4676 VMAs must be known before this is called.
4678 Reloc sections come in two flavours: Those processed specially as
4679 "side-channel" data attached to a section to which they apply, and
4680 those that bfd doesn't process as relocations. The latter sort are
4681 stored in a normal bfd section by bfd_section_from_shdr. We don't
4682 consider the former sort here, unless they form part of the loadable
4683 image. Reloc sections not assigned here will be handled later by
4684 assign_file_positions_for_relocs.
4686 We also don't set the positions of the .symtab and .strtab here. */
4689 assign_file_positions_except_relocs (bfd *abfd,
4690 struct bfd_link_info *link_info)
4692 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4693 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4695 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4697 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4698 && bfd_get_format (abfd) != bfd_core)
4700 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4701 unsigned int num_sec = elf_numsections (abfd);
4702 Elf_Internal_Shdr **hdrpp;
4705 /* Start after the ELF header. */
4706 off = i_ehdrp->e_ehsize;
4708 /* We are not creating an executable, which means that we are
4709 not creating a program header, and that the actual order of
4710 the sections in the file is unimportant. */
4711 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4713 Elf_Internal_Shdr *hdr;
4716 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4717 && hdr->bfd_section == NULL)
4718 || i == tdata->symtab_section
4719 || i == tdata->symtab_shndx_section
4720 || i == tdata->strtab_section)
4722 hdr->sh_offset = -1;
4725 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4732 /* Assign file positions for the loaded sections based on the
4733 assignment of sections to segments. */
4734 if (!assign_file_positions_for_load_sections (abfd, link_info))
4737 /* And for non-load sections. */
4738 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4741 if (bed->elf_backend_modify_program_headers != NULL)
4743 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4747 /* Write out the program headers. */
4748 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4749 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4750 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4753 off = tdata->next_file_pos;
4756 /* Place the section headers. */
4757 off = align_file_position (off, 1 << bed->s->log_file_align);
4758 i_ehdrp->e_shoff = off;
4759 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4761 tdata->next_file_pos = off;
4767 prep_headers (bfd *abfd)
4769 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4770 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4771 struct elf_strtab_hash *shstrtab;
4772 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4774 i_ehdrp = elf_elfheader (abfd);
4776 shstrtab = _bfd_elf_strtab_init ();
4777 if (shstrtab == NULL)
4780 elf_shstrtab (abfd) = shstrtab;
4782 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4783 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4784 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4785 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4787 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4788 i_ehdrp->e_ident[EI_DATA] =
4789 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4790 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4792 if ((abfd->flags & DYNAMIC) != 0)
4793 i_ehdrp->e_type = ET_DYN;
4794 else if ((abfd->flags & EXEC_P) != 0)
4795 i_ehdrp->e_type = ET_EXEC;
4796 else if (bfd_get_format (abfd) == bfd_core)
4797 i_ehdrp->e_type = ET_CORE;
4799 i_ehdrp->e_type = ET_REL;
4801 switch (bfd_get_arch (abfd))
4803 case bfd_arch_unknown:
4804 i_ehdrp->e_machine = EM_NONE;
4807 /* There used to be a long list of cases here, each one setting
4808 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4809 in the corresponding bfd definition. To avoid duplication,
4810 the switch was removed. Machines that need special handling
4811 can generally do it in elf_backend_final_write_processing(),
4812 unless they need the information earlier than the final write.
4813 Such need can generally be supplied by replacing the tests for
4814 e_machine with the conditions used to determine it. */
4816 i_ehdrp->e_machine = bed->elf_machine_code;
4819 i_ehdrp->e_version = bed->s->ev_current;
4820 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4822 /* No program header, for now. */
4823 i_ehdrp->e_phoff = 0;
4824 i_ehdrp->e_phentsize = 0;
4825 i_ehdrp->e_phnum = 0;
4827 /* Each bfd section is section header entry. */
4828 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4829 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4831 /* If we're building an executable, we'll need a program header table. */
4832 if (abfd->flags & EXEC_P)
4833 /* It all happens later. */
4837 i_ehdrp->e_phentsize = 0;
4839 i_ehdrp->e_phoff = 0;
4842 elf_tdata (abfd)->symtab_hdr.sh_name =
4843 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4844 elf_tdata (abfd)->strtab_hdr.sh_name =
4845 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4846 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4847 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4848 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4849 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4850 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4856 /* Assign file positions for all the reloc sections which are not part
4857 of the loadable file image. */
4860 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4863 unsigned int i, num_sec;
4864 Elf_Internal_Shdr **shdrpp;
4866 off = elf_tdata (abfd)->next_file_pos;
4868 num_sec = elf_numsections (abfd);
4869 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4871 Elf_Internal_Shdr *shdrp;
4874 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4875 && shdrp->sh_offset == -1)
4876 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4879 elf_tdata (abfd)->next_file_pos = off;
4883 _bfd_elf_write_object_contents (bfd *abfd)
4885 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4886 Elf_Internal_Ehdr *i_ehdrp;
4887 Elf_Internal_Shdr **i_shdrp;
4889 unsigned int count, num_sec;
4891 if (! abfd->output_has_begun
4892 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4895 i_shdrp = elf_elfsections (abfd);
4896 i_ehdrp = elf_elfheader (abfd);
4899 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4903 _bfd_elf_assign_file_positions_for_relocs (abfd);
4905 /* After writing the headers, we need to write the sections too... */
4906 num_sec = elf_numsections (abfd);
4907 for (count = 1; count < num_sec; count++)
4909 if (bed->elf_backend_section_processing)
4910 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4911 if (i_shdrp[count]->contents)
4913 bfd_size_type amt = i_shdrp[count]->sh_size;
4915 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4916 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4921 /* Write out the section header names. */
4922 if (elf_shstrtab (abfd) != NULL
4923 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4924 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4927 if (bed->elf_backend_final_write_processing)
4928 (*bed->elf_backend_final_write_processing) (abfd,
4929 elf_tdata (abfd)->linker);
4931 if (!bed->s->write_shdrs_and_ehdr (abfd))
4934 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4935 if (elf_tdata (abfd)->after_write_object_contents)
4936 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4942 _bfd_elf_write_corefile_contents (bfd *abfd)
4944 /* Hopefully this can be done just like an object file. */
4945 return _bfd_elf_write_object_contents (abfd);
4948 /* Given a section, search the header to find them. */
4951 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4953 const struct elf_backend_data *bed;
4956 if (elf_section_data (asect) != NULL
4957 && elf_section_data (asect)->this_idx != 0)
4958 return elf_section_data (asect)->this_idx;
4960 if (bfd_is_abs_section (asect))
4962 else if (bfd_is_com_section (asect))
4964 else if (bfd_is_und_section (asect))
4969 bed = get_elf_backend_data (abfd);
4970 if (bed->elf_backend_section_from_bfd_section)
4974 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4978 if (index == SHN_BAD)
4979 bfd_set_error (bfd_error_nonrepresentable_section);
4984 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4988 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4990 asymbol *asym_ptr = *asym_ptr_ptr;
4992 flagword flags = asym_ptr->flags;
4994 /* When gas creates relocations against local labels, it creates its
4995 own symbol for the section, but does put the symbol into the
4996 symbol chain, so udata is 0. When the linker is generating
4997 relocatable output, this section symbol may be for one of the
4998 input sections rather than the output section. */
4999 if (asym_ptr->udata.i == 0
5000 && (flags & BSF_SECTION_SYM)
5001 && asym_ptr->section)
5006 sec = asym_ptr->section;
5007 if (sec->owner != abfd && sec->output_section != NULL)
5008 sec = sec->output_section;
5009 if (sec->owner == abfd
5010 && (indx = sec->index) < elf_num_section_syms (abfd)
5011 && elf_section_syms (abfd)[indx] != NULL)
5012 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5015 idx = asym_ptr->udata.i;
5019 /* This case can occur when using --strip-symbol on a symbol
5020 which is used in a relocation entry. */
5021 (*_bfd_error_handler)
5022 (_("%B: symbol `%s' required but not present"),
5023 abfd, bfd_asymbol_name (asym_ptr));
5024 bfd_set_error (bfd_error_no_symbols);
5031 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5032 (long) asym_ptr, asym_ptr->name, idx, flags,
5033 elf_symbol_flags (flags));
5041 /* Rewrite program header information. */
5044 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5046 Elf_Internal_Ehdr *iehdr;
5047 struct elf_segment_map *map;
5048 struct elf_segment_map *map_first;
5049 struct elf_segment_map **pointer_to_map;
5050 Elf_Internal_Phdr *segment;
5053 unsigned int num_segments;
5054 bfd_boolean phdr_included = FALSE;
5055 bfd_vma maxpagesize;
5056 struct elf_segment_map *phdr_adjust_seg = NULL;
5057 unsigned int phdr_adjust_num = 0;
5058 const struct elf_backend_data *bed;
5060 bed = get_elf_backend_data (ibfd);
5061 iehdr = elf_elfheader (ibfd);
5064 pointer_to_map = &map_first;
5066 num_segments = elf_elfheader (ibfd)->e_phnum;
5067 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5069 /* Returns the end address of the segment + 1. */
5070 #define SEGMENT_END(segment, start) \
5071 (start + (segment->p_memsz > segment->p_filesz \
5072 ? segment->p_memsz : segment->p_filesz))
5074 #define SECTION_SIZE(section, segment) \
5075 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5076 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5077 ? section->size : 0)
5079 /* Returns TRUE if the given section is contained within
5080 the given segment. VMA addresses are compared. */
5081 #define IS_CONTAINED_BY_VMA(section, segment) \
5082 (section->vma >= segment->p_vaddr \
5083 && (section->vma + SECTION_SIZE (section, segment) \
5084 <= (SEGMENT_END (segment, segment->p_vaddr))))
5086 /* Returns TRUE if the given section is contained within
5087 the given segment. LMA addresses are compared. */
5088 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5089 (section->lma >= base \
5090 && (section->lma + SECTION_SIZE (section, segment) \
5091 <= SEGMENT_END (segment, base)))
5093 /* Handle PT_NOTE segment. */
5094 #define IS_NOTE(p, s) \
5095 (p->p_type == PT_NOTE \
5096 && elf_section_type (s) == SHT_NOTE \
5097 && (bfd_vma) s->filepos >= p->p_offset \
5098 && ((bfd_vma) s->filepos + s->size \
5099 <= p->p_offset + p->p_filesz))
5101 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5103 #define IS_COREFILE_NOTE(p, s) \
5105 && bfd_get_format (ibfd) == bfd_core \
5109 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5110 linker, which generates a PT_INTERP section with p_vaddr and
5111 p_memsz set to 0. */
5112 #define IS_SOLARIS_PT_INTERP(p, s) \
5114 && p->p_paddr == 0 \
5115 && p->p_memsz == 0 \
5116 && p->p_filesz > 0 \
5117 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5119 && (bfd_vma) s->filepos >= p->p_offset \
5120 && ((bfd_vma) s->filepos + s->size \
5121 <= p->p_offset + p->p_filesz))
5123 /* Decide if the given section should be included in the given segment.
5124 A section will be included if:
5125 1. It is within the address space of the segment -- we use the LMA
5126 if that is set for the segment and the VMA otherwise,
5127 2. It is an allocated section or a NOTE section in a PT_NOTE
5129 3. There is an output section associated with it,
5130 4. The section has not already been allocated to a previous segment.
5131 5. PT_GNU_STACK segments do not include any sections.
5132 6. PT_TLS segment includes only SHF_TLS sections.
5133 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5134 8. PT_DYNAMIC should not contain empty sections at the beginning
5135 (with the possible exception of .dynamic). */
5136 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5137 ((((segment->p_paddr \
5138 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5139 : IS_CONTAINED_BY_VMA (section, segment)) \
5140 && (section->flags & SEC_ALLOC) != 0) \
5141 || IS_NOTE (segment, section)) \
5142 && segment->p_type != PT_GNU_STACK \
5143 && (segment->p_type != PT_TLS \
5144 || (section->flags & SEC_THREAD_LOCAL)) \
5145 && (segment->p_type == PT_LOAD \
5146 || segment->p_type == PT_TLS \
5147 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5148 && (segment->p_type != PT_DYNAMIC \
5149 || SECTION_SIZE (section, segment) > 0 \
5150 || (segment->p_paddr \
5151 ? segment->p_paddr != section->lma \
5152 : segment->p_vaddr != section->vma) \
5153 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5155 && !section->segment_mark)
5157 /* If the output section of a section in the input segment is NULL,
5158 it is removed from the corresponding output segment. */
5159 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5160 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5161 && section->output_section != NULL)
5163 /* Returns TRUE iff seg1 starts after the end of seg2. */
5164 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5165 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5167 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5168 their VMA address ranges and their LMA address ranges overlap.
5169 It is possible to have overlapping VMA ranges without overlapping LMA
5170 ranges. RedBoot images for example can have both .data and .bss mapped
5171 to the same VMA range, but with the .data section mapped to a different
5173 #define SEGMENT_OVERLAPS(seg1, seg2) \
5174 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5175 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5176 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5177 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5179 /* Initialise the segment mark field. */
5180 for (section = ibfd->sections; section != NULL; section = section->next)
5181 section->segment_mark = FALSE;
5183 /* Scan through the segments specified in the program header
5184 of the input BFD. For this first scan we look for overlaps
5185 in the loadable segments. These can be created by weird
5186 parameters to objcopy. Also, fix some solaris weirdness. */
5187 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5192 Elf_Internal_Phdr *segment2;
5194 if (segment->p_type == PT_INTERP)
5195 for (section = ibfd->sections; section; section = section->next)
5196 if (IS_SOLARIS_PT_INTERP (segment, section))
5198 /* Mininal change so that the normal section to segment
5199 assignment code will work. */
5200 segment->p_vaddr = section->vma;
5204 if (segment->p_type != PT_LOAD)
5206 /* Remove PT_GNU_RELRO segment. */
5207 if (segment->p_type == PT_GNU_RELRO)
5208 segment->p_type = PT_NULL;
5212 /* Determine if this segment overlaps any previous segments. */
5213 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5215 bfd_signed_vma extra_length;
5217 if (segment2->p_type != PT_LOAD
5218 || !SEGMENT_OVERLAPS (segment, segment2))
5221 /* Merge the two segments together. */
5222 if (segment2->p_vaddr < segment->p_vaddr)
5224 /* Extend SEGMENT2 to include SEGMENT and then delete
5226 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5227 - SEGMENT_END (segment2, segment2->p_vaddr));
5229 if (extra_length > 0)
5231 segment2->p_memsz += extra_length;
5232 segment2->p_filesz += extra_length;
5235 segment->p_type = PT_NULL;
5237 /* Since we have deleted P we must restart the outer loop. */
5239 segment = elf_tdata (ibfd)->phdr;
5244 /* Extend SEGMENT to include SEGMENT2 and then delete
5246 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5247 - SEGMENT_END (segment, segment->p_vaddr));
5249 if (extra_length > 0)
5251 segment->p_memsz += extra_length;
5252 segment->p_filesz += extra_length;
5255 segment2->p_type = PT_NULL;
5260 /* The second scan attempts to assign sections to segments. */
5261 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5265 unsigned int section_count;
5266 asection **sections;
5267 asection *output_section;
5269 bfd_vma matching_lma;
5270 bfd_vma suggested_lma;
5273 asection *first_section;
5274 bfd_boolean first_matching_lma;
5275 bfd_boolean first_suggested_lma;
5277 if (segment->p_type == PT_NULL)
5280 first_section = NULL;
5281 /* Compute how many sections might be placed into this segment. */
5282 for (section = ibfd->sections, section_count = 0;
5284 section = section->next)
5286 /* Find the first section in the input segment, which may be
5287 removed from the corresponding output segment. */
5288 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5290 if (first_section == NULL)
5291 first_section = section;
5292 if (section->output_section != NULL)
5297 /* Allocate a segment map big enough to contain
5298 all of the sections we have selected. */
5299 amt = sizeof (struct elf_segment_map);
5300 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5301 map = bfd_zalloc (obfd, amt);
5305 /* Initialise the fields of the segment map. Default to
5306 using the physical address of the segment in the input BFD. */
5308 map->p_type = segment->p_type;
5309 map->p_flags = segment->p_flags;
5310 map->p_flags_valid = 1;
5312 /* If the first section in the input segment is removed, there is
5313 no need to preserve segment physical address in the corresponding
5315 if (!first_section || first_section->output_section != NULL)
5317 map->p_paddr = segment->p_paddr;
5318 map->p_paddr_valid = 1;
5321 /* Determine if this segment contains the ELF file header
5322 and if it contains the program headers themselves. */
5323 map->includes_filehdr = (segment->p_offset == 0
5324 && segment->p_filesz >= iehdr->e_ehsize);
5325 map->includes_phdrs = 0;
5327 if (!phdr_included || segment->p_type != PT_LOAD)
5329 map->includes_phdrs =
5330 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5331 && (segment->p_offset + segment->p_filesz
5332 >= ((bfd_vma) iehdr->e_phoff
5333 + iehdr->e_phnum * iehdr->e_phentsize)));
5335 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5336 phdr_included = TRUE;
5339 if (section_count == 0)
5341 /* Special segments, such as the PT_PHDR segment, may contain
5342 no sections, but ordinary, loadable segments should contain
5343 something. They are allowed by the ELF spec however, so only
5344 a warning is produced. */
5345 if (segment->p_type == PT_LOAD)
5346 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5347 " detected, is this intentional ?\n"),
5351 *pointer_to_map = map;
5352 pointer_to_map = &map->next;
5357 /* Now scan the sections in the input BFD again and attempt
5358 to add their corresponding output sections to the segment map.
5359 The problem here is how to handle an output section which has
5360 been moved (ie had its LMA changed). There are four possibilities:
5362 1. None of the sections have been moved.
5363 In this case we can continue to use the segment LMA from the
5366 2. All of the sections have been moved by the same amount.
5367 In this case we can change the segment's LMA to match the LMA
5368 of the first section.
5370 3. Some of the sections have been moved, others have not.
5371 In this case those sections which have not been moved can be
5372 placed in the current segment which will have to have its size,
5373 and possibly its LMA changed, and a new segment or segments will
5374 have to be created to contain the other sections.
5376 4. The sections have been moved, but not by the same amount.
5377 In this case we can change the segment's LMA to match the LMA
5378 of the first section and we will have to create a new segment
5379 or segments to contain the other sections.
5381 In order to save time, we allocate an array to hold the section
5382 pointers that we are interested in. As these sections get assigned
5383 to a segment, they are removed from this array. */
5385 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5386 to work around this long long bug. */
5387 sections = bfd_malloc2 (section_count, sizeof (asection *));
5388 if (sections == NULL)
5391 /* Step One: Scan for segment vs section LMA conflicts.
5392 Also add the sections to the section array allocated above.
5393 Also add the sections to the current segment. In the common
5394 case, where the sections have not been moved, this means that
5395 we have completely filled the segment, and there is nothing
5400 first_matching_lma = TRUE;
5401 first_suggested_lma = TRUE;
5403 for (section = ibfd->sections;
5405 section = section->next)
5406 if (section == first_section)
5409 for (j = 0; section != NULL; section = section->next)
5411 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5413 output_section = section->output_section;
5415 sections[j++] = section;
5417 /* The Solaris native linker always sets p_paddr to 0.
5418 We try to catch that case here, and set it to the
5419 correct value. Note - some backends require that
5420 p_paddr be left as zero. */
5421 if (segment->p_paddr == 0
5422 && segment->p_vaddr != 0
5423 && !bed->want_p_paddr_set_to_zero
5425 && output_section->lma != 0
5426 && output_section->vma == (segment->p_vaddr
5427 + (map->includes_filehdr
5430 + (map->includes_phdrs
5432 * iehdr->e_phentsize)
5434 map->p_paddr = segment->p_vaddr;
5436 /* Match up the physical address of the segment with the
5437 LMA address of the output section. */
5438 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5439 || IS_COREFILE_NOTE (segment, section)
5440 || (bed->want_p_paddr_set_to_zero
5441 && IS_CONTAINED_BY_VMA (output_section, segment)))
5443 if (first_matching_lma || output_section->lma < matching_lma)
5445 matching_lma = output_section->lma;
5446 first_matching_lma = FALSE;
5449 /* We assume that if the section fits within the segment
5450 then it does not overlap any other section within that
5452 map->sections[isec++] = output_section;
5454 else if (first_suggested_lma)
5456 suggested_lma = output_section->lma;
5457 first_suggested_lma = FALSE;
5460 if (j == section_count)
5465 BFD_ASSERT (j == section_count);
5467 /* Step Two: Adjust the physical address of the current segment,
5469 if (isec == section_count)
5471 /* All of the sections fitted within the segment as currently
5472 specified. This is the default case. Add the segment to
5473 the list of built segments and carry on to process the next
5474 program header in the input BFD. */
5475 map->count = section_count;
5476 *pointer_to_map = map;
5477 pointer_to_map = &map->next;
5479 if (!bed->want_p_paddr_set_to_zero
5480 && matching_lma != map->p_paddr
5481 && !map->includes_filehdr && !map->includes_phdrs)
5482 /* There is some padding before the first section in the
5483 segment. So, we must account for that in the output
5485 map->p_vaddr_offset = matching_lma - map->p_paddr;
5492 if (!first_matching_lma)
5494 /* At least one section fits inside the current segment.
5495 Keep it, but modify its physical address to match the
5496 LMA of the first section that fitted. */
5497 map->p_paddr = matching_lma;
5501 /* None of the sections fitted inside the current segment.
5502 Change the current segment's physical address to match
5503 the LMA of the first section. */
5504 map->p_paddr = suggested_lma;
5507 /* Offset the segment physical address from the lma
5508 to allow for space taken up by elf headers. */
5509 if (map->includes_filehdr)
5510 map->p_paddr -= iehdr->e_ehsize;
5512 if (map->includes_phdrs)
5514 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5516 /* iehdr->e_phnum is just an estimate of the number
5517 of program headers that we will need. Make a note
5518 here of the number we used and the segment we chose
5519 to hold these headers, so that we can adjust the
5520 offset when we know the correct value. */
5521 phdr_adjust_num = iehdr->e_phnum;
5522 phdr_adjust_seg = map;
5526 /* Step Three: Loop over the sections again, this time assigning
5527 those that fit to the current segment and removing them from the
5528 sections array; but making sure not to leave large gaps. Once all
5529 possible sections have been assigned to the current segment it is
5530 added to the list of built segments and if sections still remain
5531 to be assigned, a new segment is constructed before repeating
5538 first_suggested_lma = TRUE;
5540 /* Fill the current segment with sections that fit. */
5541 for (j = 0; j < section_count; j++)
5543 section = sections[j];
5545 if (section == NULL)
5548 output_section = section->output_section;
5550 BFD_ASSERT (output_section != NULL);
5552 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5553 || IS_COREFILE_NOTE (segment, section))
5555 if (map->count == 0)
5557 /* If the first section in a segment does not start at
5558 the beginning of the segment, then something is
5560 if (output_section->lma
5562 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5563 + (map->includes_phdrs
5564 ? iehdr->e_phnum * iehdr->e_phentsize
5572 prev_sec = map->sections[map->count - 1];
5574 /* If the gap between the end of the previous section
5575 and the start of this section is more than
5576 maxpagesize then we need to start a new segment. */
5577 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5579 < BFD_ALIGN (output_section->lma, maxpagesize))
5580 || (prev_sec->lma + prev_sec->size
5581 > output_section->lma))
5583 if (first_suggested_lma)
5585 suggested_lma = output_section->lma;
5586 first_suggested_lma = FALSE;
5593 map->sections[map->count++] = output_section;
5596 section->segment_mark = TRUE;
5598 else if (first_suggested_lma)
5600 suggested_lma = output_section->lma;
5601 first_suggested_lma = FALSE;
5605 BFD_ASSERT (map->count > 0);
5607 /* Add the current segment to the list of built segments. */
5608 *pointer_to_map = map;
5609 pointer_to_map = &map->next;
5611 if (isec < section_count)
5613 /* We still have not allocated all of the sections to
5614 segments. Create a new segment here, initialise it
5615 and carry on looping. */
5616 amt = sizeof (struct elf_segment_map);
5617 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5618 map = bfd_alloc (obfd, amt);
5625 /* Initialise the fields of the segment map. Set the physical
5626 physical address to the LMA of the first section that has
5627 not yet been assigned. */
5629 map->p_type = segment->p_type;
5630 map->p_flags = segment->p_flags;
5631 map->p_flags_valid = 1;
5632 map->p_paddr = suggested_lma;
5633 map->p_paddr_valid = 1;
5634 map->includes_filehdr = 0;
5635 map->includes_phdrs = 0;
5638 while (isec < section_count);
5643 /* The Solaris linker creates program headers in which all the
5644 p_paddr fields are zero. When we try to objcopy or strip such a
5645 file, we get confused. Check for this case, and if we find it
5646 reset the p_paddr_valid fields. */
5647 for (map = map_first; map != NULL; map = map->next)
5648 if (map->p_paddr != 0)
5651 for (map = map_first; map != NULL; map = map->next)
5652 map->p_paddr_valid = 0;
5654 elf_tdata (obfd)->segment_map = map_first;
5656 /* If we had to estimate the number of program headers that were
5657 going to be needed, then check our estimate now and adjust
5658 the offset if necessary. */
5659 if (phdr_adjust_seg != NULL)
5663 for (count = 0, map = map_first; map != NULL; map = map->next)
5666 if (count > phdr_adjust_num)
5667 phdr_adjust_seg->p_paddr
5668 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5673 #undef IS_CONTAINED_BY_VMA
5674 #undef IS_CONTAINED_BY_LMA
5676 #undef IS_COREFILE_NOTE
5677 #undef IS_SOLARIS_PT_INTERP
5678 #undef IS_SECTION_IN_INPUT_SEGMENT
5679 #undef INCLUDE_SECTION_IN_SEGMENT
5680 #undef SEGMENT_AFTER_SEGMENT
5681 #undef SEGMENT_OVERLAPS
5685 /* Copy ELF program header information. */
5688 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5690 Elf_Internal_Ehdr *iehdr;
5691 struct elf_segment_map *map;
5692 struct elf_segment_map *map_first;
5693 struct elf_segment_map **pointer_to_map;
5694 Elf_Internal_Phdr *segment;
5696 unsigned int num_segments;
5697 bfd_boolean phdr_included = FALSE;
5698 bfd_boolean p_paddr_valid;
5700 iehdr = elf_elfheader (ibfd);
5703 pointer_to_map = &map_first;
5705 /* If all the segment p_paddr fields are zero, don't set
5706 map->p_paddr_valid. */
5707 p_paddr_valid = FALSE;
5708 num_segments = elf_elfheader (ibfd)->e_phnum;
5709 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5712 if (segment->p_paddr != 0)
5714 p_paddr_valid = TRUE;
5718 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5723 unsigned int section_count;
5725 Elf_Internal_Shdr *this_hdr;
5726 asection *first_section = NULL;
5727 asection *lowest_section = NULL;
5729 /* Compute how many sections are in this segment. */
5730 for (section = ibfd->sections, section_count = 0;
5732 section = section->next)
5734 this_hdr = &(elf_section_data(section)->this_hdr);
5735 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5738 first_section = lowest_section = section;
5739 if (section->lma < lowest_section->lma)
5740 lowest_section = section;
5745 /* Allocate a segment map big enough to contain
5746 all of the sections we have selected. */
5747 amt = sizeof (struct elf_segment_map);
5748 if (section_count != 0)
5749 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5750 map = bfd_zalloc (obfd, amt);
5754 /* Initialize the fields of the output segment map with the
5757 map->p_type = segment->p_type;
5758 map->p_flags = segment->p_flags;
5759 map->p_flags_valid = 1;
5760 map->p_paddr = segment->p_paddr;
5761 map->p_paddr_valid = p_paddr_valid;
5762 map->p_align = segment->p_align;
5763 map->p_align_valid = 1;
5764 map->p_vaddr_offset = 0;
5766 if (map->p_type == PT_GNU_RELRO
5767 && segment->p_filesz == segment->p_memsz)
5769 /* The PT_GNU_RELRO segment may contain the first a few
5770 bytes in the .got.plt section even if the whole .got.plt
5771 section isn't in the PT_GNU_RELRO segment. We won't
5772 change the size of the PT_GNU_RELRO segment. */
5773 map->p_size = segment->p_filesz;
5774 map->p_size_valid = 1;
5777 /* Determine if this segment contains the ELF file header
5778 and if it contains the program headers themselves. */
5779 map->includes_filehdr = (segment->p_offset == 0
5780 && segment->p_filesz >= iehdr->e_ehsize);
5782 map->includes_phdrs = 0;
5783 if (! phdr_included || segment->p_type != PT_LOAD)
5785 map->includes_phdrs =
5786 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5787 && (segment->p_offset + segment->p_filesz
5788 >= ((bfd_vma) iehdr->e_phoff
5789 + iehdr->e_phnum * iehdr->e_phentsize)));
5791 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5792 phdr_included = TRUE;
5795 if (!map->includes_phdrs
5796 && !map->includes_filehdr
5797 && map->p_paddr_valid)
5798 /* There is some other padding before the first section. */
5799 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5800 - segment->p_paddr);
5802 if (section_count != 0)
5804 unsigned int isec = 0;
5806 for (section = first_section;
5808 section = section->next)
5810 this_hdr = &(elf_section_data(section)->this_hdr);
5811 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5813 map->sections[isec++] = section->output_section;
5814 if (isec == section_count)
5820 map->count = section_count;
5821 *pointer_to_map = map;
5822 pointer_to_map = &map->next;
5825 elf_tdata (obfd)->segment_map = map_first;
5829 /* Copy private BFD data. This copies or rewrites ELF program header
5833 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5835 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5836 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5839 if (elf_tdata (ibfd)->phdr == NULL)
5842 if (ibfd->xvec == obfd->xvec)
5844 /* Check to see if any sections in the input BFD
5845 covered by ELF program header have changed. */
5846 Elf_Internal_Phdr *segment;
5847 asection *section, *osec;
5848 unsigned int i, num_segments;
5849 Elf_Internal_Shdr *this_hdr;
5850 const struct elf_backend_data *bed;
5852 bed = get_elf_backend_data (ibfd);
5854 /* Regenerate the segment map if p_paddr is set to 0. */
5855 if (bed->want_p_paddr_set_to_zero)
5858 /* Initialize the segment mark field. */
5859 for (section = obfd->sections; section != NULL;
5860 section = section->next)
5861 section->segment_mark = FALSE;
5863 num_segments = elf_elfheader (ibfd)->e_phnum;
5864 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5868 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5869 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5870 which severly confuses things, so always regenerate the segment
5871 map in this case. */
5872 if (segment->p_paddr == 0
5873 && segment->p_memsz == 0
5874 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5877 for (section = ibfd->sections;
5878 section != NULL; section = section->next)
5880 /* We mark the output section so that we know it comes
5881 from the input BFD. */
5882 osec = section->output_section;
5884 osec->segment_mark = TRUE;
5886 /* Check if this section is covered by the segment. */
5887 this_hdr = &(elf_section_data(section)->this_hdr);
5888 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5890 /* FIXME: Check if its output section is changed or
5891 removed. What else do we need to check? */
5893 || section->flags != osec->flags
5894 || section->lma != osec->lma
5895 || section->vma != osec->vma
5896 || section->size != osec->size
5897 || section->rawsize != osec->rawsize
5898 || section->alignment_power != osec->alignment_power)
5904 /* Check to see if any output section do not come from the
5906 for (section = obfd->sections; section != NULL;
5907 section = section->next)
5909 if (section->segment_mark == FALSE)
5912 section->segment_mark = FALSE;
5915 return copy_elf_program_header (ibfd, obfd);
5919 return rewrite_elf_program_header (ibfd, obfd);
5922 /* Initialize private output section information from input section. */
5925 _bfd_elf_init_private_section_data (bfd *ibfd,
5929 struct bfd_link_info *link_info)
5932 Elf_Internal_Shdr *ihdr, *ohdr;
5933 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5935 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5936 || obfd->xvec->flavour != bfd_target_elf_flavour)
5939 /* Don't copy the output ELF section type from input if the
5940 output BFD section flags have been set to something different.
5941 elf_fake_sections will set ELF section type based on BFD
5943 if (elf_section_type (osec) == SHT_NULL
5944 && (osec->flags == isec->flags || !osec->flags))
5945 elf_section_type (osec) = elf_section_type (isec);
5947 /* FIXME: Is this correct for all OS/PROC specific flags? */
5948 elf_section_flags (osec) |= (elf_section_flags (isec)
5949 & (SHF_MASKOS | SHF_MASKPROC));
5951 /* Set things up for objcopy and relocatable link. The output
5952 SHT_GROUP section will have its elf_next_in_group pointing back
5953 to the input group members. Ignore linker created group section.
5954 See elfNN_ia64_object_p in elfxx-ia64.c. */
5957 if (elf_sec_group (isec) == NULL
5958 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
5960 if (elf_section_flags (isec) & SHF_GROUP)
5961 elf_section_flags (osec) |= SHF_GROUP;
5962 elf_next_in_group (osec) = elf_next_in_group (isec);
5963 elf_group_name (osec) = elf_group_name (isec);
5967 ihdr = &elf_section_data (isec)->this_hdr;
5969 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
5970 don't use the output section of the linked-to section since it
5971 may be NULL at this point. */
5972 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
5974 ohdr = &elf_section_data (osec)->this_hdr;
5975 ohdr->sh_flags |= SHF_LINK_ORDER;
5976 elf_linked_to_section (osec) = elf_linked_to_section (isec);
5979 osec->use_rela_p = isec->use_rela_p;
5984 /* Copy private section information. This copies over the entsize
5985 field, and sometimes the info field. */
5988 _bfd_elf_copy_private_section_data (bfd *ibfd,
5993 Elf_Internal_Shdr *ihdr, *ohdr;
5995 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5996 || obfd->xvec->flavour != bfd_target_elf_flavour)
5999 ihdr = &elf_section_data (isec)->this_hdr;
6000 ohdr = &elf_section_data (osec)->this_hdr;
6002 ohdr->sh_entsize = ihdr->sh_entsize;
6004 if (ihdr->sh_type == SHT_SYMTAB
6005 || ihdr->sh_type == SHT_DYNSYM
6006 || ihdr->sh_type == SHT_GNU_verneed
6007 || ihdr->sh_type == SHT_GNU_verdef)
6008 ohdr->sh_info = ihdr->sh_info;
6010 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6014 /* Copy private header information. */
6017 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6021 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6022 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6025 /* Copy over private BFD data if it has not already been copied.
6026 This must be done here, rather than in the copy_private_bfd_data
6027 entry point, because the latter is called after the section
6028 contents have been set, which means that the program headers have
6029 already been worked out. */
6030 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6032 if (! copy_private_bfd_data (ibfd, obfd))
6036 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6037 but this might be wrong if we deleted the group section. */
6038 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6039 if (elf_section_type (isec) == SHT_GROUP
6040 && isec->output_section == NULL)
6042 asection *first = elf_next_in_group (isec);
6043 asection *s = first;
6046 if (s->output_section != NULL)
6048 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6049 elf_group_name (s->output_section) = NULL;
6051 s = elf_next_in_group (s);
6060 /* Copy private symbol information. If this symbol is in a section
6061 which we did not map into a BFD section, try to map the section
6062 index correctly. We use special macro definitions for the mapped
6063 section indices; these definitions are interpreted by the
6064 swap_out_syms function. */
6066 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6067 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6068 #define MAP_STRTAB (SHN_HIOS + 3)
6069 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6070 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6073 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6078 elf_symbol_type *isym, *osym;
6080 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6081 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6084 isym = elf_symbol_from (ibfd, isymarg);
6085 osym = elf_symbol_from (obfd, osymarg);
6088 && isym->internal_elf_sym.st_shndx != 0
6090 && bfd_is_abs_section (isym->symbol.section))
6094 shndx = isym->internal_elf_sym.st_shndx;
6095 if (shndx == elf_onesymtab (ibfd))
6096 shndx = MAP_ONESYMTAB;
6097 else if (shndx == elf_dynsymtab (ibfd))
6098 shndx = MAP_DYNSYMTAB;
6099 else if (shndx == elf_tdata (ibfd)->strtab_section)
6101 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6102 shndx = MAP_SHSTRTAB;
6103 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6104 shndx = MAP_SYM_SHNDX;
6105 osym->internal_elf_sym.st_shndx = shndx;
6111 /* Swap out the symbols. */
6114 swap_out_syms (bfd *abfd,
6115 struct bfd_strtab_hash **sttp,
6118 const struct elf_backend_data *bed;
6121 struct bfd_strtab_hash *stt;
6122 Elf_Internal_Shdr *symtab_hdr;
6123 Elf_Internal_Shdr *symtab_shndx_hdr;
6124 Elf_Internal_Shdr *symstrtab_hdr;
6125 bfd_byte *outbound_syms;
6126 bfd_byte *outbound_shndx;
6129 bfd_boolean name_local_sections;
6131 if (!elf_map_symbols (abfd))
6134 /* Dump out the symtabs. */
6135 stt = _bfd_elf_stringtab_init ();
6139 bed = get_elf_backend_data (abfd);
6140 symcount = bfd_get_symcount (abfd);
6141 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6142 symtab_hdr->sh_type = SHT_SYMTAB;
6143 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6144 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6145 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6146 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6148 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6149 symstrtab_hdr->sh_type = SHT_STRTAB;
6151 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6152 if (outbound_syms == NULL)
6154 _bfd_stringtab_free (stt);
6157 symtab_hdr->contents = outbound_syms;
6159 outbound_shndx = NULL;
6160 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6161 if (symtab_shndx_hdr->sh_name != 0)
6163 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6164 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6165 sizeof (Elf_External_Sym_Shndx));
6166 if (outbound_shndx == NULL)
6168 _bfd_stringtab_free (stt);
6172 symtab_shndx_hdr->contents = outbound_shndx;
6173 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6174 symtab_shndx_hdr->sh_size = amt;
6175 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6176 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6179 /* Now generate the data (for "contents"). */
6181 /* Fill in zeroth symbol and swap it out. */
6182 Elf_Internal_Sym sym;
6188 sym.st_shndx = SHN_UNDEF;
6189 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6190 outbound_syms += bed->s->sizeof_sym;
6191 if (outbound_shndx != NULL)
6192 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6196 = (bed->elf_backend_name_local_section_symbols
6197 && bed->elf_backend_name_local_section_symbols (abfd));
6199 syms = bfd_get_outsymbols (abfd);
6200 for (idx = 0; idx < symcount; idx++)
6202 Elf_Internal_Sym sym;
6203 bfd_vma value = syms[idx]->value;
6204 elf_symbol_type *type_ptr;
6205 flagword flags = syms[idx]->flags;
6208 if (!name_local_sections
6209 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6211 /* Local section symbols have no name. */
6216 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6219 if (sym.st_name == (unsigned long) -1)
6221 _bfd_stringtab_free (stt);
6226 type_ptr = elf_symbol_from (abfd, syms[idx]);
6228 if ((flags & BSF_SECTION_SYM) == 0
6229 && bfd_is_com_section (syms[idx]->section))
6231 /* ELF common symbols put the alignment into the `value' field,
6232 and the size into the `size' field. This is backwards from
6233 how BFD handles it, so reverse it here. */
6234 sym.st_size = value;
6235 if (type_ptr == NULL
6236 || type_ptr->internal_elf_sym.st_value == 0)
6237 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6239 sym.st_value = type_ptr->internal_elf_sym.st_value;
6240 sym.st_shndx = _bfd_elf_section_from_bfd_section
6241 (abfd, syms[idx]->section);
6245 asection *sec = syms[idx]->section;
6248 if (sec->output_section)
6250 value += sec->output_offset;
6251 sec = sec->output_section;
6254 /* Don't add in the section vma for relocatable output. */
6255 if (! relocatable_p)
6257 sym.st_value = value;
6258 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6260 if (bfd_is_abs_section (sec)
6262 && type_ptr->internal_elf_sym.st_shndx != 0)
6264 /* This symbol is in a real ELF section which we did
6265 not create as a BFD section. Undo the mapping done
6266 by copy_private_symbol_data. */
6267 shndx = type_ptr->internal_elf_sym.st_shndx;
6271 shndx = elf_onesymtab (abfd);
6274 shndx = elf_dynsymtab (abfd);
6277 shndx = elf_tdata (abfd)->strtab_section;
6280 shndx = elf_tdata (abfd)->shstrtab_section;
6283 shndx = elf_tdata (abfd)->symtab_shndx_section;
6291 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6293 if (shndx == SHN_BAD)
6297 /* Writing this would be a hell of a lot easier if
6298 we had some decent documentation on bfd, and
6299 knew what to expect of the library, and what to
6300 demand of applications. For example, it
6301 appears that `objcopy' might not set the
6302 section of a symbol to be a section that is
6303 actually in the output file. */
6304 sec2 = bfd_get_section_by_name (abfd, sec->name);
6307 _bfd_error_handler (_("\
6308 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6309 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6311 bfd_set_error (bfd_error_invalid_operation);
6312 _bfd_stringtab_free (stt);
6316 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6317 BFD_ASSERT (shndx != SHN_BAD);
6321 sym.st_shndx = shndx;
6324 if ((flags & BSF_THREAD_LOCAL) != 0)
6326 else if ((flags & BSF_FUNCTION) != 0)
6328 else if ((flags & BSF_OBJECT) != 0)
6330 else if ((flags & BSF_RELC) != 0)
6332 else if ((flags & BSF_SRELC) != 0)
6337 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6340 /* Processor-specific types. */
6341 if (type_ptr != NULL
6342 && bed->elf_backend_get_symbol_type)
6343 type = ((*bed->elf_backend_get_symbol_type)
6344 (&type_ptr->internal_elf_sym, type));
6346 if (flags & BSF_SECTION_SYM)
6348 if (flags & BSF_GLOBAL)
6349 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6351 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6353 else if (bfd_is_com_section (syms[idx]->section))
6355 #ifdef USE_STT_COMMON
6356 if (type == STT_OBJECT)
6357 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6360 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6363 else if (bfd_is_und_section (syms[idx]->section))
6364 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6368 else if (flags & BSF_FILE)
6369 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6372 int bind = STB_LOCAL;
6374 if (flags & BSF_LOCAL)
6376 else if (flags & BSF_WEAK)
6378 else if (flags & BSF_GLOBAL)
6381 sym.st_info = ELF_ST_INFO (bind, type);
6384 if (type_ptr != NULL)
6385 sym.st_other = type_ptr->internal_elf_sym.st_other;
6389 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6390 outbound_syms += bed->s->sizeof_sym;
6391 if (outbound_shndx != NULL)
6392 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6396 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6397 symstrtab_hdr->sh_type = SHT_STRTAB;
6399 symstrtab_hdr->sh_flags = 0;
6400 symstrtab_hdr->sh_addr = 0;
6401 symstrtab_hdr->sh_entsize = 0;
6402 symstrtab_hdr->sh_link = 0;
6403 symstrtab_hdr->sh_info = 0;
6404 symstrtab_hdr->sh_addralign = 1;
6409 /* Return the number of bytes required to hold the symtab vector.
6411 Note that we base it on the count plus 1, since we will null terminate
6412 the vector allocated based on this size. However, the ELF symbol table
6413 always has a dummy entry as symbol #0, so it ends up even. */
6416 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6420 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6422 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6423 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6425 symtab_size -= sizeof (asymbol *);
6431 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6435 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6437 if (elf_dynsymtab (abfd) == 0)
6439 bfd_set_error (bfd_error_invalid_operation);
6443 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6444 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6446 symtab_size -= sizeof (asymbol *);
6452 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6455 return (asect->reloc_count + 1) * sizeof (arelent *);
6458 /* Canonicalize the relocs. */
6461 _bfd_elf_canonicalize_reloc (bfd *abfd,
6468 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6470 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6473 tblptr = section->relocation;
6474 for (i = 0; i < section->reloc_count; i++)
6475 *relptr++ = tblptr++;
6479 return section->reloc_count;
6483 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6485 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6486 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6489 bfd_get_symcount (abfd) = symcount;
6494 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6495 asymbol **allocation)
6497 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6498 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6501 bfd_get_dynamic_symcount (abfd) = symcount;
6505 /* Return the size required for the dynamic reloc entries. Any loadable
6506 section that was actually installed in the BFD, and has type SHT_REL
6507 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6508 dynamic reloc section. */
6511 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6516 if (elf_dynsymtab (abfd) == 0)
6518 bfd_set_error (bfd_error_invalid_operation);
6522 ret = sizeof (arelent *);
6523 for (s = abfd->sections; s != NULL; s = s->next)
6524 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6525 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6526 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6527 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6528 * sizeof (arelent *));
6533 /* Canonicalize the dynamic relocation entries. Note that we return the
6534 dynamic relocations as a single block, although they are actually
6535 associated with particular sections; the interface, which was
6536 designed for SunOS style shared libraries, expects that there is only
6537 one set of dynamic relocs. Any loadable section that was actually
6538 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6539 dynamic symbol table, is considered to be a dynamic reloc section. */
6542 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6546 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6550 if (elf_dynsymtab (abfd) == 0)
6552 bfd_set_error (bfd_error_invalid_operation);
6556 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6558 for (s = abfd->sections; s != NULL; s = s->next)
6560 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6561 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6562 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6567 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6569 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6571 for (i = 0; i < count; i++)
6582 /* Read in the version information. */
6585 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6587 bfd_byte *contents = NULL;
6588 unsigned int freeidx = 0;
6590 if (elf_dynverref (abfd) != 0)
6592 Elf_Internal_Shdr *hdr;
6593 Elf_External_Verneed *everneed;
6594 Elf_Internal_Verneed *iverneed;
6596 bfd_byte *contents_end;
6598 hdr = &elf_tdata (abfd)->dynverref_hdr;
6600 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6601 sizeof (Elf_Internal_Verneed));
6602 if (elf_tdata (abfd)->verref == NULL)
6605 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6607 contents = bfd_malloc (hdr->sh_size);
6608 if (contents == NULL)
6610 error_return_verref:
6611 elf_tdata (abfd)->verref = NULL;
6612 elf_tdata (abfd)->cverrefs = 0;
6615 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6616 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6617 goto error_return_verref;
6619 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6620 goto error_return_verref;
6622 BFD_ASSERT (sizeof (Elf_External_Verneed)
6623 == sizeof (Elf_External_Vernaux));
6624 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6625 everneed = (Elf_External_Verneed *) contents;
6626 iverneed = elf_tdata (abfd)->verref;
6627 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6629 Elf_External_Vernaux *evernaux;
6630 Elf_Internal_Vernaux *ivernaux;
6633 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6635 iverneed->vn_bfd = abfd;
6637 iverneed->vn_filename =
6638 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6640 if (iverneed->vn_filename == NULL)
6641 goto error_return_verref;
6643 if (iverneed->vn_cnt == 0)
6644 iverneed->vn_auxptr = NULL;
6647 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6648 sizeof (Elf_Internal_Vernaux));
6649 if (iverneed->vn_auxptr == NULL)
6650 goto error_return_verref;
6653 if (iverneed->vn_aux
6654 > (size_t) (contents_end - (bfd_byte *) everneed))
6655 goto error_return_verref;
6657 evernaux = ((Elf_External_Vernaux *)
6658 ((bfd_byte *) everneed + iverneed->vn_aux));
6659 ivernaux = iverneed->vn_auxptr;
6660 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6662 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6664 ivernaux->vna_nodename =
6665 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6666 ivernaux->vna_name);
6667 if (ivernaux->vna_nodename == NULL)
6668 goto error_return_verref;
6670 if (j + 1 < iverneed->vn_cnt)
6671 ivernaux->vna_nextptr = ivernaux + 1;
6673 ivernaux->vna_nextptr = NULL;
6675 if (ivernaux->vna_next
6676 > (size_t) (contents_end - (bfd_byte *) evernaux))
6677 goto error_return_verref;
6679 evernaux = ((Elf_External_Vernaux *)
6680 ((bfd_byte *) evernaux + ivernaux->vna_next));
6682 if (ivernaux->vna_other > freeidx)
6683 freeidx = ivernaux->vna_other;
6686 if (i + 1 < hdr->sh_info)
6687 iverneed->vn_nextref = iverneed + 1;
6689 iverneed->vn_nextref = NULL;
6691 if (iverneed->vn_next
6692 > (size_t) (contents_end - (bfd_byte *) everneed))
6693 goto error_return_verref;
6695 everneed = ((Elf_External_Verneed *)
6696 ((bfd_byte *) everneed + iverneed->vn_next));
6703 if (elf_dynverdef (abfd) != 0)
6705 Elf_Internal_Shdr *hdr;
6706 Elf_External_Verdef *everdef;
6707 Elf_Internal_Verdef *iverdef;
6708 Elf_Internal_Verdef *iverdefarr;
6709 Elf_Internal_Verdef iverdefmem;
6711 unsigned int maxidx;
6712 bfd_byte *contents_end_def, *contents_end_aux;
6714 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6716 contents = bfd_malloc (hdr->sh_size);
6717 if (contents == NULL)
6719 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6720 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6723 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6726 BFD_ASSERT (sizeof (Elf_External_Verdef)
6727 >= sizeof (Elf_External_Verdaux));
6728 contents_end_def = contents + hdr->sh_size
6729 - sizeof (Elf_External_Verdef);
6730 contents_end_aux = contents + hdr->sh_size
6731 - sizeof (Elf_External_Verdaux);
6733 /* We know the number of entries in the section but not the maximum
6734 index. Therefore we have to run through all entries and find
6736 everdef = (Elf_External_Verdef *) contents;
6738 for (i = 0; i < hdr->sh_info; ++i)
6740 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6742 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6743 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6745 if (iverdefmem.vd_next
6746 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6749 everdef = ((Elf_External_Verdef *)
6750 ((bfd_byte *) everdef + iverdefmem.vd_next));
6753 if (default_imported_symver)
6755 if (freeidx > maxidx)
6760 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6761 sizeof (Elf_Internal_Verdef));
6762 if (elf_tdata (abfd)->verdef == NULL)
6765 elf_tdata (abfd)->cverdefs = maxidx;
6767 everdef = (Elf_External_Verdef *) contents;
6768 iverdefarr = elf_tdata (abfd)->verdef;
6769 for (i = 0; i < hdr->sh_info; i++)
6771 Elf_External_Verdaux *everdaux;
6772 Elf_Internal_Verdaux *iverdaux;
6775 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6777 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6779 error_return_verdef:
6780 elf_tdata (abfd)->verdef = NULL;
6781 elf_tdata (abfd)->cverdefs = 0;
6785 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6786 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6788 iverdef->vd_bfd = abfd;
6790 if (iverdef->vd_cnt == 0)
6791 iverdef->vd_auxptr = NULL;
6794 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6795 sizeof (Elf_Internal_Verdaux));
6796 if (iverdef->vd_auxptr == NULL)
6797 goto error_return_verdef;
6801 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6802 goto error_return_verdef;
6804 everdaux = ((Elf_External_Verdaux *)
6805 ((bfd_byte *) everdef + iverdef->vd_aux));
6806 iverdaux = iverdef->vd_auxptr;
6807 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6809 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6811 iverdaux->vda_nodename =
6812 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6813 iverdaux->vda_name);
6814 if (iverdaux->vda_nodename == NULL)
6815 goto error_return_verdef;
6817 if (j + 1 < iverdef->vd_cnt)
6818 iverdaux->vda_nextptr = iverdaux + 1;
6820 iverdaux->vda_nextptr = NULL;
6822 if (iverdaux->vda_next
6823 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6824 goto error_return_verdef;
6826 everdaux = ((Elf_External_Verdaux *)
6827 ((bfd_byte *) everdaux + iverdaux->vda_next));
6830 if (iverdef->vd_cnt)
6831 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6833 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6834 iverdef->vd_nextdef = iverdef + 1;
6836 iverdef->vd_nextdef = NULL;
6838 everdef = ((Elf_External_Verdef *)
6839 ((bfd_byte *) everdef + iverdef->vd_next));
6845 else if (default_imported_symver)
6852 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6853 sizeof (Elf_Internal_Verdef));
6854 if (elf_tdata (abfd)->verdef == NULL)
6857 elf_tdata (abfd)->cverdefs = freeidx;
6860 /* Create a default version based on the soname. */
6861 if (default_imported_symver)
6863 Elf_Internal_Verdef *iverdef;
6864 Elf_Internal_Verdaux *iverdaux;
6866 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6868 iverdef->vd_version = VER_DEF_CURRENT;
6869 iverdef->vd_flags = 0;
6870 iverdef->vd_ndx = freeidx;
6871 iverdef->vd_cnt = 1;
6873 iverdef->vd_bfd = abfd;
6875 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6876 if (iverdef->vd_nodename == NULL)
6877 goto error_return_verdef;
6878 iverdef->vd_nextdef = NULL;
6879 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6880 if (iverdef->vd_auxptr == NULL)
6881 goto error_return_verdef;
6883 iverdaux = iverdef->vd_auxptr;
6884 iverdaux->vda_nodename = iverdef->vd_nodename;
6885 iverdaux->vda_nextptr = NULL;
6891 if (contents != NULL)
6897 _bfd_elf_make_empty_symbol (bfd *abfd)
6899 elf_symbol_type *newsym;
6900 bfd_size_type amt = sizeof (elf_symbol_type);
6902 newsym = bfd_zalloc (abfd, amt);
6907 newsym->symbol.the_bfd = abfd;
6908 return &newsym->symbol;
6913 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6917 bfd_symbol_info (symbol, ret);
6920 /* Return whether a symbol name implies a local symbol. Most targets
6921 use this function for the is_local_label_name entry point, but some
6925 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6928 /* Normal local symbols start with ``.L''. */
6929 if (name[0] == '.' && name[1] == 'L')
6932 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6933 DWARF debugging symbols starting with ``..''. */
6934 if (name[0] == '.' && name[1] == '.')
6937 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6938 emitting DWARF debugging output. I suspect this is actually a
6939 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6940 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6941 underscore to be emitted on some ELF targets). For ease of use,
6942 we treat such symbols as local. */
6943 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6950 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6951 asymbol *symbol ATTRIBUTE_UNUSED)
6958 _bfd_elf_set_arch_mach (bfd *abfd,
6959 enum bfd_architecture arch,
6960 unsigned long machine)
6962 /* If this isn't the right architecture for this backend, and this
6963 isn't the generic backend, fail. */
6964 if (arch != get_elf_backend_data (abfd)->arch
6965 && arch != bfd_arch_unknown
6966 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6969 return bfd_default_set_arch_mach (abfd, arch, machine);
6972 /* Find the function to a particular section and offset,
6973 for error reporting. */
6976 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6980 const char **filename_ptr,
6981 const char **functionname_ptr)
6983 const char *filename;
6984 asymbol *func, *file;
6987 /* ??? Given multiple file symbols, it is impossible to reliably
6988 choose the right file name for global symbols. File symbols are
6989 local symbols, and thus all file symbols must sort before any
6990 global symbols. The ELF spec may be interpreted to say that a
6991 file symbol must sort before other local symbols, but currently
6992 ld -r doesn't do this. So, for ld -r output, it is possible to
6993 make a better choice of file name for local symbols by ignoring
6994 file symbols appearing after a given local symbol. */
6995 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7001 state = nothing_seen;
7003 for (p = symbols; *p != NULL; p++)
7007 q = (elf_symbol_type *) *p;
7009 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7015 if (state == symbol_seen)
7016 state = file_after_symbol_seen;
7020 if (bfd_get_section (&q->symbol) == section
7021 && q->symbol.value >= low_func
7022 && q->symbol.value <= offset)
7024 func = (asymbol *) q;
7025 low_func = q->symbol.value;
7028 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7029 || state != file_after_symbol_seen))
7030 filename = bfd_asymbol_name (file);
7034 if (state == nothing_seen)
7035 state = symbol_seen;
7042 *filename_ptr = filename;
7043 if (functionname_ptr)
7044 *functionname_ptr = bfd_asymbol_name (func);
7049 /* Find the nearest line to a particular section and offset,
7050 for error reporting. */
7053 _bfd_elf_find_nearest_line (bfd *abfd,
7057 const char **filename_ptr,
7058 const char **functionname_ptr,
7059 unsigned int *line_ptr)
7063 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7064 filename_ptr, functionname_ptr,
7067 if (!*functionname_ptr)
7068 elf_find_function (abfd, section, symbols, offset,
7069 *filename_ptr ? NULL : filename_ptr,
7075 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7076 filename_ptr, functionname_ptr,
7078 &elf_tdata (abfd)->dwarf2_find_line_info))
7080 if (!*functionname_ptr)
7081 elf_find_function (abfd, section, symbols, offset,
7082 *filename_ptr ? NULL : filename_ptr,
7088 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7089 &found, filename_ptr,
7090 functionname_ptr, line_ptr,
7091 &elf_tdata (abfd)->line_info))
7093 if (found && (*functionname_ptr || *line_ptr))
7096 if (symbols == NULL)
7099 if (! elf_find_function (abfd, section, symbols, offset,
7100 filename_ptr, functionname_ptr))
7107 /* Find the line for a symbol. */
7110 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7111 const char **filename_ptr, unsigned int *line_ptr)
7113 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7114 filename_ptr, line_ptr, 0,
7115 &elf_tdata (abfd)->dwarf2_find_line_info);
7118 /* After a call to bfd_find_nearest_line, successive calls to
7119 bfd_find_inliner_info can be used to get source information about
7120 each level of function inlining that terminated at the address
7121 passed to bfd_find_nearest_line. Currently this is only supported
7122 for DWARF2 with appropriate DWARF3 extensions. */
7125 _bfd_elf_find_inliner_info (bfd *abfd,
7126 const char **filename_ptr,
7127 const char **functionname_ptr,
7128 unsigned int *line_ptr)
7131 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7132 functionname_ptr, line_ptr,
7133 & elf_tdata (abfd)->dwarf2_find_line_info);
7138 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7140 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7141 int ret = bed->s->sizeof_ehdr;
7143 if (!info->relocatable)
7145 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7147 if (phdr_size == (bfd_size_type) -1)
7149 struct elf_segment_map *m;
7152 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7153 phdr_size += bed->s->sizeof_phdr;
7156 phdr_size = get_program_header_size (abfd, info);
7159 elf_tdata (abfd)->program_header_size = phdr_size;
7167 _bfd_elf_set_section_contents (bfd *abfd,
7169 const void *location,
7171 bfd_size_type count)
7173 Elf_Internal_Shdr *hdr;
7176 if (! abfd->output_has_begun
7177 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7180 hdr = &elf_section_data (section)->this_hdr;
7181 pos = hdr->sh_offset + offset;
7182 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7183 || bfd_bwrite (location, count, abfd) != count)
7190 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7191 arelent *cache_ptr ATTRIBUTE_UNUSED,
7192 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7197 /* Try to convert a non-ELF reloc into an ELF one. */
7200 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7202 /* Check whether we really have an ELF howto. */
7204 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7206 bfd_reloc_code_real_type code;
7207 reloc_howto_type *howto;
7209 /* Alien reloc: Try to determine its type to replace it with an
7210 equivalent ELF reloc. */
7212 if (areloc->howto->pc_relative)
7214 switch (areloc->howto->bitsize)
7217 code = BFD_RELOC_8_PCREL;
7220 code = BFD_RELOC_12_PCREL;
7223 code = BFD_RELOC_16_PCREL;
7226 code = BFD_RELOC_24_PCREL;
7229 code = BFD_RELOC_32_PCREL;
7232 code = BFD_RELOC_64_PCREL;
7238 howto = bfd_reloc_type_lookup (abfd, code);
7240 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7242 if (howto->pcrel_offset)
7243 areloc->addend += areloc->address;
7245 areloc->addend -= areloc->address; /* addend is unsigned!! */
7250 switch (areloc->howto->bitsize)
7256 code = BFD_RELOC_14;
7259 code = BFD_RELOC_16;
7262 code = BFD_RELOC_26;
7265 code = BFD_RELOC_32;
7268 code = BFD_RELOC_64;
7274 howto = bfd_reloc_type_lookup (abfd, code);
7278 areloc->howto = howto;
7286 (*_bfd_error_handler)
7287 (_("%B: unsupported relocation type %s"),
7288 abfd, areloc->howto->name);
7289 bfd_set_error (bfd_error_bad_value);
7294 _bfd_elf_close_and_cleanup (bfd *abfd)
7296 if (bfd_get_format (abfd) == bfd_object)
7298 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7299 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7300 _bfd_dwarf2_cleanup_debug_info (abfd);
7303 return _bfd_generic_close_and_cleanup (abfd);
7306 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7307 in the relocation's offset. Thus we cannot allow any sort of sanity
7308 range-checking to interfere. There is nothing else to do in processing
7311 bfd_reloc_status_type
7312 _bfd_elf_rel_vtable_reloc_fn
7313 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7314 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7315 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7316 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7318 return bfd_reloc_ok;
7321 /* Elf core file support. Much of this only works on native
7322 toolchains, since we rely on knowing the
7323 machine-dependent procfs structure in order to pick
7324 out details about the corefile. */
7326 #ifdef HAVE_SYS_PROCFS_H
7327 # include <sys/procfs.h>
7330 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7333 elfcore_make_pid (bfd *abfd)
7335 return ((elf_tdata (abfd)->core_lwpid << 16)
7336 + (elf_tdata (abfd)->core_pid));
7339 /* If there isn't a section called NAME, make one, using
7340 data from SECT. Note, this function will generate a
7341 reference to NAME, so you shouldn't deallocate or
7345 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7349 if (bfd_get_section_by_name (abfd, name) != NULL)
7352 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7356 sect2->size = sect->size;
7357 sect2->filepos = sect->filepos;
7358 sect2->alignment_power = sect->alignment_power;
7362 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7363 actually creates up to two pseudosections:
7364 - For the single-threaded case, a section named NAME, unless
7365 such a section already exists.
7366 - For the multi-threaded case, a section named "NAME/PID", where
7367 PID is elfcore_make_pid (abfd).
7368 Both pseudosections have identical contents. */
7370 _bfd_elfcore_make_pseudosection (bfd *abfd,
7376 char *threaded_name;
7380 /* Build the section name. */
7382 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7383 len = strlen (buf) + 1;
7384 threaded_name = bfd_alloc (abfd, len);
7385 if (threaded_name == NULL)
7387 memcpy (threaded_name, buf, len);
7389 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7394 sect->filepos = filepos;
7395 sect->alignment_power = 2;
7397 return elfcore_maybe_make_sect (abfd, name, sect);
7400 /* prstatus_t exists on:
7402 linux 2.[01] + glibc
7406 #if defined (HAVE_PRSTATUS_T)
7409 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7414 if (note->descsz == sizeof (prstatus_t))
7418 size = sizeof (prstat.pr_reg);
7419 offset = offsetof (prstatus_t, pr_reg);
7420 memcpy (&prstat, note->descdata, sizeof (prstat));
7422 /* Do not overwrite the core signal if it
7423 has already been set by another thread. */
7424 if (elf_tdata (abfd)->core_signal == 0)
7425 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7426 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7428 /* pr_who exists on:
7431 pr_who doesn't exist on:
7434 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7435 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7438 #if defined (HAVE_PRSTATUS32_T)
7439 else if (note->descsz == sizeof (prstatus32_t))
7441 /* 64-bit host, 32-bit corefile */
7442 prstatus32_t prstat;
7444 size = sizeof (prstat.pr_reg);
7445 offset = offsetof (prstatus32_t, pr_reg);
7446 memcpy (&prstat, note->descdata, sizeof (prstat));
7448 /* Do not overwrite the core signal if it
7449 has already been set by another thread. */
7450 if (elf_tdata (abfd)->core_signal == 0)
7451 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7452 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7454 /* pr_who exists on:
7457 pr_who doesn't exist on:
7460 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7461 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7464 #endif /* HAVE_PRSTATUS32_T */
7467 /* Fail - we don't know how to handle any other
7468 note size (ie. data object type). */
7472 /* Make a ".reg/999" section and a ".reg" section. */
7473 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7474 size, note->descpos + offset);
7476 #endif /* defined (HAVE_PRSTATUS_T) */
7478 /* Create a pseudosection containing the exact contents of NOTE. */
7480 elfcore_make_note_pseudosection (bfd *abfd,
7482 Elf_Internal_Note *note)
7484 return _bfd_elfcore_make_pseudosection (abfd, name,
7485 note->descsz, note->descpos);
7488 /* There isn't a consistent prfpregset_t across platforms,
7489 but it doesn't matter, because we don't have to pick this
7490 data structure apart. */
7493 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7495 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7498 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7499 type of NT_PRXFPREG. Just include the whole note's contents
7503 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7505 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7509 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7511 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7515 #if defined (HAVE_PRPSINFO_T)
7516 typedef prpsinfo_t elfcore_psinfo_t;
7517 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7518 typedef prpsinfo32_t elfcore_psinfo32_t;
7522 #if defined (HAVE_PSINFO_T)
7523 typedef psinfo_t elfcore_psinfo_t;
7524 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7525 typedef psinfo32_t elfcore_psinfo32_t;
7529 /* return a malloc'ed copy of a string at START which is at
7530 most MAX bytes long, possibly without a terminating '\0'.
7531 the copy will always have a terminating '\0'. */
7534 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7537 char *end = memchr (start, '\0', max);
7545 dups = bfd_alloc (abfd, len + 1);
7549 memcpy (dups, start, len);
7555 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7557 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7559 if (note->descsz == sizeof (elfcore_psinfo_t))
7561 elfcore_psinfo_t psinfo;
7563 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7565 elf_tdata (abfd)->core_program
7566 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7567 sizeof (psinfo.pr_fname));
7569 elf_tdata (abfd)->core_command
7570 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7571 sizeof (psinfo.pr_psargs));
7573 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7574 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7576 /* 64-bit host, 32-bit corefile */
7577 elfcore_psinfo32_t psinfo;
7579 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7581 elf_tdata (abfd)->core_program
7582 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7583 sizeof (psinfo.pr_fname));
7585 elf_tdata (abfd)->core_command
7586 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7587 sizeof (psinfo.pr_psargs));
7593 /* Fail - we don't know how to handle any other
7594 note size (ie. data object type). */
7598 /* Note that for some reason, a spurious space is tacked
7599 onto the end of the args in some (at least one anyway)
7600 implementations, so strip it off if it exists. */
7603 char *command = elf_tdata (abfd)->core_command;
7604 int n = strlen (command);
7606 if (0 < n && command[n - 1] == ' ')
7607 command[n - 1] = '\0';
7612 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7614 #if defined (HAVE_PSTATUS_T)
7616 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7618 if (note->descsz == sizeof (pstatus_t)
7619 #if defined (HAVE_PXSTATUS_T)
7620 || note->descsz == sizeof (pxstatus_t)
7626 memcpy (&pstat, note->descdata, sizeof (pstat));
7628 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7630 #if defined (HAVE_PSTATUS32_T)
7631 else if (note->descsz == sizeof (pstatus32_t))
7633 /* 64-bit host, 32-bit corefile */
7636 memcpy (&pstat, note->descdata, sizeof (pstat));
7638 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7641 /* Could grab some more details from the "representative"
7642 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7643 NT_LWPSTATUS note, presumably. */
7647 #endif /* defined (HAVE_PSTATUS_T) */
7649 #if defined (HAVE_LWPSTATUS_T)
7651 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7653 lwpstatus_t lwpstat;
7659 if (note->descsz != sizeof (lwpstat)
7660 #if defined (HAVE_LWPXSTATUS_T)
7661 && note->descsz != sizeof (lwpxstatus_t)
7666 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7668 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7669 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7671 /* Make a ".reg/999" section. */
7673 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7674 len = strlen (buf) + 1;
7675 name = bfd_alloc (abfd, len);
7678 memcpy (name, buf, len);
7680 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7684 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7685 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7686 sect->filepos = note->descpos
7687 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7690 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7691 sect->size = sizeof (lwpstat.pr_reg);
7692 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7695 sect->alignment_power = 2;
7697 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7700 /* Make a ".reg2/999" section */
7702 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7703 len = strlen (buf) + 1;
7704 name = bfd_alloc (abfd, len);
7707 memcpy (name, buf, len);
7709 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7713 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7714 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7715 sect->filepos = note->descpos
7716 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7719 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7720 sect->size = sizeof (lwpstat.pr_fpreg);
7721 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7724 sect->alignment_power = 2;
7726 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7728 #endif /* defined (HAVE_LWPSTATUS_T) */
7731 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7738 int is_active_thread;
7741 if (note->descsz < 728)
7744 if (! CONST_STRNEQ (note->namedata, "win32"))
7747 type = bfd_get_32 (abfd, note->descdata);
7751 case 1 /* NOTE_INFO_PROCESS */:
7752 /* FIXME: need to add ->core_command. */
7753 /* process_info.pid */
7754 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7755 /* process_info.signal */
7756 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7759 case 2 /* NOTE_INFO_THREAD */:
7760 /* Make a ".reg/999" section. */
7761 /* thread_info.tid */
7762 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7764 len = strlen (buf) + 1;
7765 name = bfd_alloc (abfd, len);
7769 memcpy (name, buf, len);
7771 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7775 /* sizeof (thread_info.thread_context) */
7777 /* offsetof (thread_info.thread_context) */
7778 sect->filepos = note->descpos + 12;
7779 sect->alignment_power = 2;
7781 /* thread_info.is_active_thread */
7782 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7784 if (is_active_thread)
7785 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7789 case 3 /* NOTE_INFO_MODULE */:
7790 /* Make a ".module/xxxxxxxx" section. */
7791 /* module_info.base_address */
7792 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7793 sprintf (buf, ".module/%08lx", (long) base_addr);
7795 len = strlen (buf) + 1;
7796 name = bfd_alloc (abfd, len);
7800 memcpy (name, buf, len);
7802 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7807 sect->size = note->descsz;
7808 sect->filepos = note->descpos;
7809 sect->alignment_power = 2;
7820 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7822 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7830 if (bed->elf_backend_grok_prstatus)
7831 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7833 #if defined (HAVE_PRSTATUS_T)
7834 return elfcore_grok_prstatus (abfd, note);
7839 #if defined (HAVE_PSTATUS_T)
7841 return elfcore_grok_pstatus (abfd, note);
7844 #if defined (HAVE_LWPSTATUS_T)
7846 return elfcore_grok_lwpstatus (abfd, note);
7849 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7850 return elfcore_grok_prfpreg (abfd, note);
7852 case NT_WIN32PSTATUS:
7853 return elfcore_grok_win32pstatus (abfd, note);
7855 case NT_PRXFPREG: /* Linux SSE extension */
7856 if (note->namesz == 6
7857 && strcmp (note->namedata, "LINUX") == 0)
7858 return elfcore_grok_prxfpreg (abfd, note);
7863 if (note->namesz == 6
7864 && strcmp (note->namedata, "LINUX") == 0)
7865 return elfcore_grok_ppc_vmx (abfd, note);
7871 if (bed->elf_backend_grok_psinfo)
7872 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7874 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7875 return elfcore_grok_psinfo (abfd, note);
7882 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7887 sect->size = note->descsz;
7888 sect->filepos = note->descpos;
7889 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7897 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7899 elf_tdata (abfd)->build_id_size = note->descsz;
7900 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7901 if (elf_tdata (abfd)->build_id == NULL)
7904 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7910 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
7917 case NT_GNU_BUILD_ID:
7918 return elfobj_grok_gnu_build_id (abfd, note);
7923 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7927 cp = strchr (note->namedata, '@');
7930 *lwpidp = atoi(cp + 1);
7937 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7939 /* Signal number at offset 0x08. */
7940 elf_tdata (abfd)->core_signal
7941 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7943 /* Process ID at offset 0x50. */
7944 elf_tdata (abfd)->core_pid
7945 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7947 /* Command name at 0x7c (max 32 bytes, including nul). */
7948 elf_tdata (abfd)->core_command
7949 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7951 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7956 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7960 if (elfcore_netbsd_get_lwpid (note, &lwp))
7961 elf_tdata (abfd)->core_lwpid = lwp;
7963 if (note->type == NT_NETBSDCORE_PROCINFO)
7965 /* NetBSD-specific core "procinfo". Note that we expect to
7966 find this note before any of the others, which is fine,
7967 since the kernel writes this note out first when it
7968 creates a core file. */
7970 return elfcore_grok_netbsd_procinfo (abfd, note);
7973 /* As of Jan 2002 there are no other machine-independent notes
7974 defined for NetBSD core files. If the note type is less
7975 than the start of the machine-dependent note types, we don't
7978 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7982 switch (bfd_get_arch (abfd))
7984 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7985 PT_GETFPREGS == mach+2. */
7987 case bfd_arch_alpha:
7988 case bfd_arch_sparc:
7991 case NT_NETBSDCORE_FIRSTMACH+0:
7992 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7994 case NT_NETBSDCORE_FIRSTMACH+2:
7995 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8001 /* On all other arch's, PT_GETREGS == mach+1 and
8002 PT_GETFPREGS == mach+3. */
8007 case NT_NETBSDCORE_FIRSTMACH+1:
8008 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8010 case NT_NETBSDCORE_FIRSTMACH+3:
8011 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8021 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8023 void *ddata = note->descdata;
8030 /* nto_procfs_status 'pid' field is at offset 0. */
8031 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8033 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8034 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8036 /* nto_procfs_status 'flags' field is at offset 8. */
8037 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8039 /* nto_procfs_status 'what' field is at offset 14. */
8040 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8042 elf_tdata (abfd)->core_signal = sig;
8043 elf_tdata (abfd)->core_lwpid = *tid;
8046 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8047 do not come from signals so we make sure we set the current
8048 thread just in case. */
8049 if (flags & 0x00000080)
8050 elf_tdata (abfd)->core_lwpid = *tid;
8052 /* Make a ".qnx_core_status/%d" section. */
8053 sprintf (buf, ".qnx_core_status/%ld", *tid);
8055 name = bfd_alloc (abfd, strlen (buf) + 1);
8060 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8064 sect->size = note->descsz;
8065 sect->filepos = note->descpos;
8066 sect->alignment_power = 2;
8068 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8072 elfcore_grok_nto_regs (bfd *abfd,
8073 Elf_Internal_Note *note,
8081 /* Make a "(base)/%d" section. */
8082 sprintf (buf, "%s/%ld", base, tid);
8084 name = bfd_alloc (abfd, strlen (buf) + 1);
8089 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8093 sect->size = note->descsz;
8094 sect->filepos = note->descpos;
8095 sect->alignment_power = 2;
8097 /* This is the current thread. */
8098 if (elf_tdata (abfd)->core_lwpid == tid)
8099 return elfcore_maybe_make_sect (abfd, base, sect);
8104 #define BFD_QNT_CORE_INFO 7
8105 #define BFD_QNT_CORE_STATUS 8
8106 #define BFD_QNT_CORE_GREG 9
8107 #define BFD_QNT_CORE_FPREG 10
8110 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8112 /* Every GREG section has a STATUS section before it. Store the
8113 tid from the previous call to pass down to the next gregs
8115 static long tid = 1;
8119 case BFD_QNT_CORE_INFO:
8120 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8121 case BFD_QNT_CORE_STATUS:
8122 return elfcore_grok_nto_status (abfd, note, &tid);
8123 case BFD_QNT_CORE_GREG:
8124 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8125 case BFD_QNT_CORE_FPREG:
8126 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8133 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8139 /* Use note name as section name. */
8141 name = bfd_alloc (abfd, len);
8144 memcpy (name, note->namedata, len);
8145 name[len - 1] = '\0';
8147 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8151 sect->size = note->descsz;
8152 sect->filepos = note->descpos;
8153 sect->alignment_power = 1;
8158 /* Function: elfcore_write_note
8161 buffer to hold note, and current size of buffer
8165 size of data for note
8167 Writes note to end of buffer. ELF64 notes are written exactly as
8168 for ELF32, despite the current (as of 2006) ELF gabi specifying
8169 that they ought to have 8-byte namesz and descsz field, and have
8170 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8173 Pointer to realloc'd buffer, *BUFSIZ updated. */
8176 elfcore_write_note (bfd *abfd,
8184 Elf_External_Note *xnp;
8191 namesz = strlen (name) + 1;
8193 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8195 buf = realloc (buf, *bufsiz + newspace);
8198 dest = buf + *bufsiz;
8199 *bufsiz += newspace;
8200 xnp = (Elf_External_Note *) dest;
8201 H_PUT_32 (abfd, namesz, xnp->namesz);
8202 H_PUT_32 (abfd, size, xnp->descsz);
8203 H_PUT_32 (abfd, type, xnp->type);
8207 memcpy (dest, name, namesz);
8215 memcpy (dest, input, size);
8225 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8227 elfcore_write_prpsinfo (bfd *abfd,
8233 const char *note_name = "CORE";
8234 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8236 if (bed->elf_backend_write_core_note != NULL)
8239 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8240 NT_PRPSINFO, fname, psargs);
8245 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8246 if (bed->s->elfclass == ELFCLASS32)
8248 #if defined (HAVE_PSINFO32_T)
8250 int note_type = NT_PSINFO;
8253 int note_type = NT_PRPSINFO;
8256 memset (&data, 0, sizeof (data));
8257 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8258 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8259 return elfcore_write_note (abfd, buf, bufsiz,
8260 note_name, note_type, &data, sizeof (data));
8265 #if defined (HAVE_PSINFO_T)
8267 int note_type = NT_PSINFO;
8270 int note_type = NT_PRPSINFO;
8273 memset (&data, 0, sizeof (data));
8274 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8275 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8276 return elfcore_write_note (abfd, buf, bufsiz,
8277 note_name, note_type, &data, sizeof (data));
8280 #endif /* PSINFO_T or PRPSINFO_T */
8282 #if defined (HAVE_PRSTATUS_T)
8284 elfcore_write_prstatus (bfd *abfd,
8291 const char *note_name = "CORE";
8292 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8294 if (bed->elf_backend_write_core_note != NULL)
8297 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8299 pid, cursig, gregs);
8304 #if defined (HAVE_PRSTATUS32_T)
8305 if (bed->s->elfclass == ELFCLASS32)
8307 prstatus32_t prstat;
8309 memset (&prstat, 0, sizeof (prstat));
8310 prstat.pr_pid = pid;
8311 prstat.pr_cursig = cursig;
8312 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8313 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8314 NT_PRSTATUS, &prstat, sizeof (prstat));
8321 memset (&prstat, 0, sizeof (prstat));
8322 prstat.pr_pid = pid;
8323 prstat.pr_cursig = cursig;
8324 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8325 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8326 NT_PRSTATUS, &prstat, sizeof (prstat));
8329 #endif /* HAVE_PRSTATUS_T */
8331 #if defined (HAVE_LWPSTATUS_T)
8333 elfcore_write_lwpstatus (bfd *abfd,
8340 lwpstatus_t lwpstat;
8341 const char *note_name = "CORE";
8343 memset (&lwpstat, 0, sizeof (lwpstat));
8344 lwpstat.pr_lwpid = pid >> 16;
8345 lwpstat.pr_cursig = cursig;
8346 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8347 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8348 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8350 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8351 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8353 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8354 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8357 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8358 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8360 #endif /* HAVE_LWPSTATUS_T */
8362 #if defined (HAVE_PSTATUS_T)
8364 elfcore_write_pstatus (bfd *abfd,
8368 int cursig ATTRIBUTE_UNUSED,
8369 const void *gregs ATTRIBUTE_UNUSED)
8371 const char *note_name = "CORE";
8372 #if defined (HAVE_PSTATUS32_T)
8373 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8375 if (bed->s->elfclass == ELFCLASS32)
8379 memset (&pstat, 0, sizeof (pstat));
8380 pstat.pr_pid = pid & 0xffff;
8381 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8382 NT_PSTATUS, &pstat, sizeof (pstat));
8390 memset (&pstat, 0, sizeof (pstat));
8391 pstat.pr_pid = pid & 0xffff;
8392 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8393 NT_PSTATUS, &pstat, sizeof (pstat));
8397 #endif /* HAVE_PSTATUS_T */
8400 elfcore_write_prfpreg (bfd *abfd,
8406 const char *note_name = "CORE";
8407 return elfcore_write_note (abfd, buf, bufsiz,
8408 note_name, NT_FPREGSET, fpregs, size);
8412 elfcore_write_prxfpreg (bfd *abfd,
8415 const void *xfpregs,
8418 char *note_name = "LINUX";
8419 return elfcore_write_note (abfd, buf, bufsiz,
8420 note_name, NT_PRXFPREG, xfpregs, size);
8424 elfcore_write_ppc_vmx (bfd *abfd,
8427 const void *ppc_vmx,
8430 char *note_name = "LINUX";
8431 return elfcore_write_note (abfd, buf, bufsiz,
8432 note_name, NT_PPC_VMX, ppc_vmx, size);
8436 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8441 while (p < buf + size)
8443 /* FIXME: bad alignment assumption. */
8444 Elf_External_Note *xnp = (Elf_External_Note *) p;
8445 Elf_Internal_Note in;
8447 in.type = H_GET_32 (abfd, xnp->type);
8449 in.namesz = H_GET_32 (abfd, xnp->namesz);
8450 in.namedata = xnp->name;
8452 in.descsz = H_GET_32 (abfd, xnp->descsz);
8453 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8454 in.descpos = offset + (in.descdata - buf);
8456 switch (bfd_get_format (abfd))
8462 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8464 if (! elfcore_grok_netbsd_note (abfd, &in))
8467 else if (CONST_STRNEQ (in.namedata, "QNX"))
8469 if (! elfcore_grok_nto_note (abfd, &in))
8472 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8474 if (! elfcore_grok_spu_note (abfd, &in))
8479 if (! elfcore_grok_note (abfd, &in))
8485 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8487 if (! elfobj_grok_gnu_note (abfd, &in))
8493 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8500 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8507 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8510 buf = bfd_malloc (size);
8514 if (bfd_bread (buf, size, abfd) != size
8515 || !elf_parse_notes (abfd, buf, size, offset))
8525 /* Providing external access to the ELF program header table. */
8527 /* Return an upper bound on the number of bytes required to store a
8528 copy of ABFD's program header table entries. Return -1 if an error
8529 occurs; bfd_get_error will return an appropriate code. */
8532 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8534 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8536 bfd_set_error (bfd_error_wrong_format);
8540 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8543 /* Copy ABFD's program header table entries to *PHDRS. The entries
8544 will be stored as an array of Elf_Internal_Phdr structures, as
8545 defined in include/elf/internal.h. To find out how large the
8546 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8548 Return the number of program header table entries read, or -1 if an
8549 error occurs; bfd_get_error will return an appropriate code. */
8552 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8556 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8558 bfd_set_error (bfd_error_wrong_format);
8562 num_phdrs = elf_elfheader (abfd)->e_phnum;
8563 memcpy (phdrs, elf_tdata (abfd)->phdr,
8564 num_phdrs * sizeof (Elf_Internal_Phdr));
8569 enum elf_reloc_type_class
8570 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8572 return reloc_class_normal;
8575 /* For RELA architectures, return the relocation value for a
8576 relocation against a local symbol. */
8579 _bfd_elf_rela_local_sym (bfd *abfd,
8580 Elf_Internal_Sym *sym,
8582 Elf_Internal_Rela *rel)
8584 asection *sec = *psec;
8587 relocation = (sec->output_section->vma
8588 + sec->output_offset
8590 if ((sec->flags & SEC_MERGE)
8591 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8592 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8595 _bfd_merged_section_offset (abfd, psec,
8596 elf_section_data (sec)->sec_info,
8597 sym->st_value + rel->r_addend);
8600 /* If we have changed the section, and our original section is
8601 marked with SEC_EXCLUDE, it means that the original
8602 SEC_MERGE section has been completely subsumed in some
8603 other SEC_MERGE section. In this case, we need to leave
8604 some info around for --emit-relocs. */
8605 if ((sec->flags & SEC_EXCLUDE) != 0)
8606 sec->kept_section = *psec;
8609 rel->r_addend -= relocation;
8610 rel->r_addend += sec->output_section->vma + sec->output_offset;
8616 _bfd_elf_rel_local_sym (bfd *abfd,
8617 Elf_Internal_Sym *sym,
8621 asection *sec = *psec;
8623 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8624 return sym->st_value + addend;
8626 return _bfd_merged_section_offset (abfd, psec,
8627 elf_section_data (sec)->sec_info,
8628 sym->st_value + addend);
8632 _bfd_elf_section_offset (bfd *abfd,
8633 struct bfd_link_info *info,
8637 switch (sec->sec_info_type)
8639 case ELF_INFO_TYPE_STABS:
8640 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8642 case ELF_INFO_TYPE_EH_FRAME:
8643 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8649 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8650 reconstruct an ELF file by reading the segments out of remote memory
8651 based on the ELF file header at EHDR_VMA and the ELF program headers it
8652 points to. If not null, *LOADBASEP is filled in with the difference
8653 between the VMAs from which the segments were read, and the VMAs the
8654 file headers (and hence BFD's idea of each section's VMA) put them at.
8656 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8657 remote memory at target address VMA into the local buffer at MYADDR; it
8658 should return zero on success or an `errno' code on failure. TEMPL must
8659 be a BFD for an ELF target with the word size and byte order found in
8660 the remote memory. */
8663 bfd_elf_bfd_from_remote_memory
8667 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8669 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8670 (templ, ehdr_vma, loadbasep, target_read_memory);
8674 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8675 long symcount ATTRIBUTE_UNUSED,
8676 asymbol **syms ATTRIBUTE_UNUSED,
8681 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8684 const char *relplt_name;
8685 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8689 Elf_Internal_Shdr *hdr;
8695 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8698 if (dynsymcount <= 0)
8701 if (!bed->plt_sym_val)
8704 relplt_name = bed->relplt_name;
8705 if (relplt_name == NULL)
8706 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8707 relplt = bfd_get_section_by_name (abfd, relplt_name);
8711 hdr = &elf_section_data (relplt)->this_hdr;
8712 if (hdr->sh_link != elf_dynsymtab (abfd)
8713 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8716 plt = bfd_get_section_by_name (abfd, ".plt");
8720 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8721 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8724 count = relplt->size / hdr->sh_entsize;
8725 size = count * sizeof (asymbol);
8726 p = relplt->relocation;
8727 for (i = 0; i < count; i++, p++)
8728 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8730 s = *ret = bfd_malloc (size);
8734 names = (char *) (s + count);
8735 p = relplt->relocation;
8737 for (i = 0; i < count; i++, p++)
8742 addr = bed->plt_sym_val (i, plt, p);
8743 if (addr == (bfd_vma) -1)
8746 *s = **p->sym_ptr_ptr;
8747 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8748 we are defining a symbol, ensure one of them is set. */
8749 if ((s->flags & BSF_LOCAL) == 0)
8750 s->flags |= BSF_GLOBAL;
8752 s->value = addr - plt->vma;
8755 len = strlen ((*p->sym_ptr_ptr)->name);
8756 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8758 memcpy (names, "@plt", sizeof ("@plt"));
8759 names += sizeof ("@plt");
8766 /* It is only used by x86-64 so far. */
8767 asection _bfd_elf_large_com_section
8768 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8769 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8772 _bfd_elf_set_osabi (bfd * abfd,
8773 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8775 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8777 i_ehdrp = elf_elfheader (abfd);
8779 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8783 /* Return TRUE for ELF symbol types that represent functions.
8784 This is the default version of this function, which is sufficient for
8785 most targets. It returns true if TYPE is STT_FUNC. */
8788 _bfd_elf_is_function_type (unsigned int type)
8790 return (type == STT_FUNC);