1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
47 static int elf_sort_sections (const void *, const void *);
48 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
49 static bfd_boolean prep_headers (bfd *);
50 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
51 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
52 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
55 /* Swap version information in and out. The version information is
56 currently size independent. If that ever changes, this code will
57 need to move into elfcode.h. */
59 /* Swap in a Verdef structure. */
62 _bfd_elf_swap_verdef_in (bfd *abfd,
63 const Elf_External_Verdef *src,
64 Elf_Internal_Verdef *dst)
66 dst->vd_version = H_GET_16 (abfd, src->vd_version);
67 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
68 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
69 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
70 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
71 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
72 dst->vd_next = H_GET_32 (abfd, src->vd_next);
75 /* Swap out a Verdef structure. */
78 _bfd_elf_swap_verdef_out (bfd *abfd,
79 const Elf_Internal_Verdef *src,
80 Elf_External_Verdef *dst)
82 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
83 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
84 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
85 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
86 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
87 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
88 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
91 /* Swap in a Verdaux structure. */
94 _bfd_elf_swap_verdaux_in (bfd *abfd,
95 const Elf_External_Verdaux *src,
96 Elf_Internal_Verdaux *dst)
98 dst->vda_name = H_GET_32 (abfd, src->vda_name);
99 dst->vda_next = H_GET_32 (abfd, src->vda_next);
102 /* Swap out a Verdaux structure. */
105 _bfd_elf_swap_verdaux_out (bfd *abfd,
106 const Elf_Internal_Verdaux *src,
107 Elf_External_Verdaux *dst)
109 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
110 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
113 /* Swap in a Verneed structure. */
116 _bfd_elf_swap_verneed_in (bfd *abfd,
117 const Elf_External_Verneed *src,
118 Elf_Internal_Verneed *dst)
120 dst->vn_version = H_GET_16 (abfd, src->vn_version);
121 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
122 dst->vn_file = H_GET_32 (abfd, src->vn_file);
123 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
124 dst->vn_next = H_GET_32 (abfd, src->vn_next);
127 /* Swap out a Verneed structure. */
130 _bfd_elf_swap_verneed_out (bfd *abfd,
131 const Elf_Internal_Verneed *src,
132 Elf_External_Verneed *dst)
134 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
135 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
136 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
137 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
138 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
141 /* Swap in a Vernaux structure. */
144 _bfd_elf_swap_vernaux_in (bfd *abfd,
145 const Elf_External_Vernaux *src,
146 Elf_Internal_Vernaux *dst)
148 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
149 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
150 dst->vna_other = H_GET_16 (abfd, src->vna_other);
151 dst->vna_name = H_GET_32 (abfd, src->vna_name);
152 dst->vna_next = H_GET_32 (abfd, src->vna_next);
155 /* Swap out a Vernaux structure. */
158 _bfd_elf_swap_vernaux_out (bfd *abfd,
159 const Elf_Internal_Vernaux *src,
160 Elf_External_Vernaux *dst)
162 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
163 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
164 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
165 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
166 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
169 /* Swap in a Versym structure. */
172 _bfd_elf_swap_versym_in (bfd *abfd,
173 const Elf_External_Versym *src,
174 Elf_Internal_Versym *dst)
176 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
179 /* Swap out a Versym structure. */
182 _bfd_elf_swap_versym_out (bfd *abfd,
183 const Elf_Internal_Versym *src,
184 Elf_External_Versym *dst)
186 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
189 /* Standard ELF hash function. Do not change this function; you will
190 cause invalid hash tables to be generated. */
193 bfd_elf_hash (const char *namearg)
195 const unsigned char *name = (const unsigned char *) namearg;
200 while ((ch = *name++) != '\0')
203 if ((g = (h & 0xf0000000)) != 0)
206 /* The ELF ABI says `h &= ~g', but this is equivalent in
207 this case and on some machines one insn instead of two. */
211 return h & 0xffffffff;
214 /* DT_GNU_HASH hash function. Do not change this function; you will
215 cause invalid hash tables to be generated. */
218 bfd_elf_gnu_hash (const char *namearg)
220 const unsigned char *name = (const unsigned char *) namearg;
221 unsigned long h = 5381;
224 while ((ch = *name++) != '\0')
225 h = (h << 5) + h + ch;
226 return h & 0xffffffff;
229 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
230 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
232 bfd_elf_allocate_object (bfd *abfd,
234 enum elf_object_id object_id)
236 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
237 abfd->tdata.any = bfd_zalloc (abfd, object_size);
238 if (abfd->tdata.any == NULL)
241 elf_object_id (abfd) = object_id;
242 elf_program_header_size (abfd) = (bfd_size_type) -1;
248 bfd_elf_make_generic_object (bfd *abfd)
250 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
255 bfd_elf_mkcorefile (bfd *abfd)
257 /* I think this can be done just like an object file. */
258 return bfd_elf_make_generic_object (abfd);
262 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
264 Elf_Internal_Shdr **i_shdrp;
265 bfd_byte *shstrtab = NULL;
267 bfd_size_type shstrtabsize;
269 i_shdrp = elf_elfsections (abfd);
271 || shindex >= elf_numsections (abfd)
272 || i_shdrp[shindex] == 0)
275 shstrtab = i_shdrp[shindex]->contents;
276 if (shstrtab == NULL)
278 /* No cached one, attempt to read, and cache what we read. */
279 offset = i_shdrp[shindex]->sh_offset;
280 shstrtabsize = i_shdrp[shindex]->sh_size;
282 /* Allocate and clear an extra byte at the end, to prevent crashes
283 in case the string table is not terminated. */
284 if (shstrtabsize + 1 <= 1
285 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
286 || bfd_seek (abfd, offset, SEEK_SET) != 0)
288 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
290 if (bfd_get_error () != bfd_error_system_call)
291 bfd_set_error (bfd_error_file_truncated);
293 /* Once we've failed to read it, make sure we don't keep
294 trying. Otherwise, we'll keep allocating space for
295 the string table over and over. */
296 i_shdrp[shindex]->sh_size = 0;
299 shstrtab[shstrtabsize] = '\0';
300 i_shdrp[shindex]->contents = shstrtab;
302 return (char *) shstrtab;
306 bfd_elf_string_from_elf_section (bfd *abfd,
307 unsigned int shindex,
308 unsigned int strindex)
310 Elf_Internal_Shdr *hdr;
315 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318 hdr = elf_elfsections (abfd)[shindex];
320 if (hdr->contents == NULL
321 && bfd_elf_get_str_section (abfd, shindex) == NULL)
324 if (strindex >= hdr->sh_size)
326 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
327 (*_bfd_error_handler)
328 (_("%B: invalid string offset %u >= %lu for section `%s'"),
329 abfd, strindex, (unsigned long) hdr->sh_size,
330 (shindex == shstrndx && strindex == hdr->sh_name
332 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
336 return ((char *) hdr->contents) + strindex;
339 /* Read and convert symbols to internal format.
340 SYMCOUNT specifies the number of symbols to read, starting from
341 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
342 are non-NULL, they are used to store the internal symbols, external
343 symbols, and symbol section index extensions, respectively.
344 Returns a pointer to the internal symbol buffer (malloced if necessary)
345 or NULL if there were no symbols or some kind of problem. */
348 bfd_elf_get_elf_syms (bfd *ibfd,
349 Elf_Internal_Shdr *symtab_hdr,
352 Elf_Internal_Sym *intsym_buf,
354 Elf_External_Sym_Shndx *extshndx_buf)
356 Elf_Internal_Shdr *shndx_hdr;
358 const bfd_byte *esym;
359 Elf_External_Sym_Shndx *alloc_extshndx;
360 Elf_External_Sym_Shndx *shndx;
361 Elf_Internal_Sym *isym;
362 Elf_Internal_Sym *isymend;
363 const struct elf_backend_data *bed;
368 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
374 /* Normal syms might have section extension entries. */
376 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
377 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
379 /* Read the symbols. */
381 alloc_extshndx = NULL;
382 bed = get_elf_backend_data (ibfd);
383 extsym_size = bed->s->sizeof_sym;
384 amt = symcount * extsym_size;
385 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
386 if (extsym_buf == NULL)
388 alloc_ext = bfd_malloc2 (symcount, extsym_size);
389 extsym_buf = alloc_ext;
391 if (extsym_buf == NULL
392 || bfd_seek (ibfd, pos, SEEK_SET) != 0
393 || bfd_bread (extsym_buf, amt, ibfd) != amt)
399 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
403 amt = symcount * sizeof (Elf_External_Sym_Shndx);
404 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
405 if (extshndx_buf == NULL)
407 alloc_extshndx = bfd_malloc2 (symcount,
408 sizeof (Elf_External_Sym_Shndx));
409 extshndx_buf = alloc_extshndx;
411 if (extshndx_buf == NULL
412 || bfd_seek (ibfd, pos, SEEK_SET) != 0
413 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
420 if (intsym_buf == NULL)
422 intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
423 if (intsym_buf == NULL)
427 /* Convert the symbols to internal form. */
428 isymend = intsym_buf + symcount;
429 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
431 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
432 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
434 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
435 (*_bfd_error_handler) (_("%B symbol number %lu references "
436 "nonexistent SHT_SYMTAB_SHNDX section"),
437 ibfd, (unsigned long) symoffset);
443 if (alloc_ext != NULL)
445 if (alloc_extshndx != NULL)
446 free (alloc_extshndx);
451 /* Look up a symbol name. */
453 bfd_elf_sym_name (bfd *abfd,
454 Elf_Internal_Shdr *symtab_hdr,
455 Elf_Internal_Sym *isym,
459 unsigned int iname = isym->st_name;
460 unsigned int shindex = symtab_hdr->sh_link;
462 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
463 /* Check for a bogus st_shndx to avoid crashing. */
464 && isym->st_shndx < elf_numsections (abfd))
466 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
467 shindex = elf_elfheader (abfd)->e_shstrndx;
470 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
473 else if (sym_sec && *name == '\0')
474 name = bfd_section_name (abfd, sym_sec);
479 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
480 sections. The first element is the flags, the rest are section
483 typedef union elf_internal_group {
484 Elf_Internal_Shdr *shdr;
486 } Elf_Internal_Group;
488 /* Return the name of the group signature symbol. Why isn't the
489 signature just a string? */
492 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
494 Elf_Internal_Shdr *hdr;
495 unsigned char esym[sizeof (Elf64_External_Sym)];
496 Elf_External_Sym_Shndx eshndx;
497 Elf_Internal_Sym isym;
499 /* First we need to ensure the symbol table is available. Make sure
500 that it is a symbol table section. */
501 if (ghdr->sh_link >= elf_numsections (abfd))
503 hdr = elf_elfsections (abfd) [ghdr->sh_link];
504 if (hdr->sh_type != SHT_SYMTAB
505 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
508 /* Go read the symbol. */
509 hdr = &elf_tdata (abfd)->symtab_hdr;
510 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
511 &isym, esym, &eshndx) == NULL)
514 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
517 /* Set next_in_group list pointer, and group name for NEWSECT. */
520 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
522 unsigned int num_group = elf_tdata (abfd)->num_group;
524 /* If num_group is zero, read in all SHT_GROUP sections. The count
525 is set to -1 if there are no SHT_GROUP sections. */
528 unsigned int i, shnum;
530 /* First count the number of groups. If we have a SHT_GROUP
531 section with just a flag word (ie. sh_size is 4), ignore it. */
532 shnum = elf_numsections (abfd);
535 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
536 ( (shdr)->sh_type == SHT_GROUP \
537 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
538 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
539 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
541 for (i = 0; i < shnum; i++)
543 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
545 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
551 num_group = (unsigned) -1;
552 elf_tdata (abfd)->num_group = num_group;
556 /* We keep a list of elf section headers for group sections,
557 so we can find them quickly. */
560 elf_tdata (abfd)->num_group = num_group;
561 elf_tdata (abfd)->group_sect_ptr
562 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
563 if (elf_tdata (abfd)->group_sect_ptr == NULL)
567 for (i = 0; i < shnum; i++)
569 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
571 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
574 Elf_Internal_Group *dest;
576 /* Add to list of sections. */
577 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
580 /* Read the raw contents. */
581 BFD_ASSERT (sizeof (*dest) >= 4);
582 amt = shdr->sh_size * sizeof (*dest) / 4;
583 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
585 /* PR binutils/4110: Handle corrupt group headers. */
586 if (shdr->contents == NULL)
589 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
590 bfd_set_error (bfd_error_bad_value);
594 memset (shdr->contents, 0, amt);
596 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
597 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
601 /* Translate raw contents, a flag word followed by an
602 array of elf section indices all in target byte order,
603 to the flag word followed by an array of elf section
605 src = shdr->contents + shdr->sh_size;
606 dest = (Elf_Internal_Group *) (shdr->contents + amt);
613 idx = H_GET_32 (abfd, src);
614 if (src == shdr->contents)
617 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
618 shdr->bfd_section->flags
619 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
624 ((*_bfd_error_handler)
625 (_("%B: invalid SHT_GROUP entry"), abfd));
628 dest->shdr = elf_elfsections (abfd)[idx];
635 if (num_group != (unsigned) -1)
639 for (i = 0; i < num_group; i++)
641 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
642 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
643 unsigned int n_elt = shdr->sh_size / 4;
645 /* Look through this group's sections to see if current
646 section is a member. */
648 if ((++idx)->shdr == hdr)
652 /* We are a member of this group. Go looking through
653 other members to see if any others are linked via
655 idx = (Elf_Internal_Group *) shdr->contents;
656 n_elt = shdr->sh_size / 4;
658 if ((s = (++idx)->shdr->bfd_section) != NULL
659 && elf_next_in_group (s) != NULL)
663 /* Snarf the group name from other member, and
664 insert current section in circular list. */
665 elf_group_name (newsect) = elf_group_name (s);
666 elf_next_in_group (newsect) = elf_next_in_group (s);
667 elf_next_in_group (s) = newsect;
673 gname = group_signature (abfd, shdr);
676 elf_group_name (newsect) = gname;
678 /* Start a circular list with one element. */
679 elf_next_in_group (newsect) = newsect;
682 /* If the group section has been created, point to the
684 if (shdr->bfd_section != NULL)
685 elf_next_in_group (shdr->bfd_section) = newsect;
693 if (elf_group_name (newsect) == NULL)
695 (*_bfd_error_handler) (_("%B: no group info for section %A"),
702 _bfd_elf_setup_sections (bfd *abfd)
705 unsigned int num_group = elf_tdata (abfd)->num_group;
706 bfd_boolean result = TRUE;
709 /* Process SHF_LINK_ORDER. */
710 for (s = abfd->sections; s != NULL; s = s->next)
712 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
713 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
715 unsigned int elfsec = this_hdr->sh_link;
716 /* FIXME: The old Intel compiler and old strip/objcopy may
717 not set the sh_link or sh_info fields. Hence we could
718 get the situation where elfsec is 0. */
721 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
722 if (bed->link_order_error_handler)
723 bed->link_order_error_handler
724 (_("%B: warning: sh_link not set for section `%A'"),
729 asection *link = NULL;
731 if (elfsec < elf_numsections (abfd))
733 this_hdr = elf_elfsections (abfd)[elfsec];
734 link = this_hdr->bfd_section;
738 Some strip/objcopy may leave an incorrect value in
739 sh_link. We don't want to proceed. */
742 (*_bfd_error_handler)
743 (_("%B: sh_link [%d] in section `%A' is incorrect"),
744 s->owner, s, elfsec);
748 elf_linked_to_section (s) = link;
753 /* Process section groups. */
754 if (num_group == (unsigned) -1)
757 for (i = 0; i < num_group; i++)
759 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
760 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
761 unsigned int n_elt = shdr->sh_size / 4;
764 if ((++idx)->shdr->bfd_section)
765 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
766 else if (idx->shdr->sh_type == SHT_RELA
767 || idx->shdr->sh_type == SHT_REL)
768 /* We won't include relocation sections in section groups in
769 output object files. We adjust the group section size here
770 so that relocatable link will work correctly when
771 relocation sections are in section group in input object
773 shdr->bfd_section->size -= 4;
776 /* There are some unknown sections in the group. */
777 (*_bfd_error_handler)
778 (_("%B: unknown [%d] section `%s' in group [%s]"),
780 (unsigned int) idx->shdr->sh_type,
781 bfd_elf_string_from_elf_section (abfd,
782 (elf_elfheader (abfd)
785 shdr->bfd_section->name);
793 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
795 return elf_next_in_group (sec) != NULL;
798 /* Make a BFD section from an ELF section. We store a pointer to the
799 BFD section in the bfd_section field of the header. */
802 _bfd_elf_make_section_from_shdr (bfd *abfd,
803 Elf_Internal_Shdr *hdr,
809 const struct elf_backend_data *bed;
811 if (hdr->bfd_section != NULL)
813 BFD_ASSERT (strcmp (name,
814 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
818 newsect = bfd_make_section_anyway (abfd, name);
822 hdr->bfd_section = newsect;
823 elf_section_data (newsect)->this_hdr = *hdr;
824 elf_section_data (newsect)->this_idx = shindex;
826 /* Always use the real type/flags. */
827 elf_section_type (newsect) = hdr->sh_type;
828 elf_section_flags (newsect) = hdr->sh_flags;
830 newsect->filepos = hdr->sh_offset;
832 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
833 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
834 || ! bfd_set_section_alignment (abfd, newsect,
835 bfd_log2 (hdr->sh_addralign)))
838 flags = SEC_NO_FLAGS;
839 if (hdr->sh_type != SHT_NOBITS)
840 flags |= SEC_HAS_CONTENTS;
841 if (hdr->sh_type == SHT_GROUP)
842 flags |= SEC_GROUP | SEC_EXCLUDE;
843 if ((hdr->sh_flags & SHF_ALLOC) != 0)
846 if (hdr->sh_type != SHT_NOBITS)
849 if ((hdr->sh_flags & SHF_WRITE) == 0)
850 flags |= SEC_READONLY;
851 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
853 else if ((flags & SEC_LOAD) != 0)
855 if ((hdr->sh_flags & SHF_MERGE) != 0)
858 newsect->entsize = hdr->sh_entsize;
859 if ((hdr->sh_flags & SHF_STRINGS) != 0)
860 flags |= SEC_STRINGS;
862 if (hdr->sh_flags & SHF_GROUP)
863 if (!setup_group (abfd, hdr, newsect))
865 if ((hdr->sh_flags & SHF_TLS) != 0)
866 flags |= SEC_THREAD_LOCAL;
868 if ((flags & SEC_ALLOC) == 0)
870 /* The debugging sections appear to be recognized only by name,
871 not any sort of flag. Their SEC_ALLOC bits are cleared. */
876 } debug_sections [] =
878 { STRING_COMMA_LEN ("debug") }, /* 'd' */
879 { NULL, 0 }, /* 'e' */
880 { NULL, 0 }, /* 'f' */
881 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
882 { NULL, 0 }, /* 'h' */
883 { NULL, 0 }, /* 'i' */
884 { NULL, 0 }, /* 'j' */
885 { NULL, 0 }, /* 'k' */
886 { STRING_COMMA_LEN ("line") }, /* 'l' */
887 { NULL, 0 }, /* 'm' */
888 { NULL, 0 }, /* 'n' */
889 { NULL, 0 }, /* 'o' */
890 { NULL, 0 }, /* 'p' */
891 { NULL, 0 }, /* 'q' */
892 { NULL, 0 }, /* 'r' */
893 { STRING_COMMA_LEN ("stab") }, /* 's' */
894 { NULL, 0 }, /* 't' */
895 { NULL, 0 }, /* 'u' */
896 { NULL, 0 }, /* 'v' */
897 { NULL, 0 }, /* 'w' */
898 { NULL, 0 }, /* 'x' */
899 { NULL, 0 }, /* 'y' */
900 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
905 int i = name [1] - 'd';
907 && i < (int) ARRAY_SIZE (debug_sections)
908 && debug_sections [i].name != NULL
909 && strncmp (&name [1], debug_sections [i].name,
910 debug_sections [i].len) == 0)
911 flags |= SEC_DEBUGGING;
915 /* As a GNU extension, if the name begins with .gnu.linkonce, we
916 only link a single copy of the section. This is used to support
917 g++. g++ will emit each template expansion in its own section.
918 The symbols will be defined as weak, so that multiple definitions
919 are permitted. The GNU linker extension is to actually discard
920 all but one of the sections. */
921 if (CONST_STRNEQ (name, ".gnu.linkonce")
922 && elf_next_in_group (newsect) == NULL)
923 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
925 bed = get_elf_backend_data (abfd);
926 if (bed->elf_backend_section_flags)
927 if (! bed->elf_backend_section_flags (&flags, hdr))
930 if (! bfd_set_section_flags (abfd, newsect, flags))
933 /* We do not parse the PT_NOTE segments as we are interested even in the
934 separate debug info files which may have the segments offsets corrupted.
935 PT_NOTEs from the core files are currently not parsed using BFD. */
936 if (hdr->sh_type == SHT_NOTE)
940 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
943 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
947 if ((flags & SEC_ALLOC) != 0)
949 Elf_Internal_Phdr *phdr;
950 unsigned int i, nload;
952 /* Some ELF linkers produce binaries with all the program header
953 p_paddr fields zero. If we have such a binary with more than
954 one PT_LOAD header, then leave the section lma equal to vma
955 so that we don't create sections with overlapping lma. */
956 phdr = elf_tdata (abfd)->phdr;
957 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
958 if (phdr->p_paddr != 0)
960 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
962 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
965 phdr = elf_tdata (abfd)->phdr;
966 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
968 /* This section is part of this segment if its file
969 offset plus size lies within the segment's memory
970 span and, if the section is loaded, the extent of the
971 loaded data lies within the extent of the segment.
973 Note - we used to check the p_paddr field as well, and
974 refuse to set the LMA if it was 0. This is wrong
975 though, as a perfectly valid initialised segment can
976 have a p_paddr of zero. Some architectures, eg ARM,
977 place special significance on the address 0 and
978 executables need to be able to have a segment which
979 covers this address. */
980 if (phdr->p_type == PT_LOAD
981 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
982 && (hdr->sh_offset + hdr->sh_size
983 <= phdr->p_offset + phdr->p_memsz)
984 && ((flags & SEC_LOAD) == 0
985 || (hdr->sh_offset + hdr->sh_size
986 <= phdr->p_offset + phdr->p_filesz)))
988 if ((flags & SEC_LOAD) == 0)
989 newsect->lma = (phdr->p_paddr
990 + hdr->sh_addr - phdr->p_vaddr);
992 /* We used to use the same adjustment for SEC_LOAD
993 sections, but that doesn't work if the segment
994 is packed with code from multiple VMAs.
995 Instead we calculate the section LMA based on
996 the segment LMA. It is assumed that the
997 segment will contain sections with contiguous
998 LMAs, even if the VMAs are not. */
999 newsect->lma = (phdr->p_paddr
1000 + hdr->sh_offset - phdr->p_offset);
1002 /* With contiguous segments, we can't tell from file
1003 offsets whether a section with zero size should
1004 be placed at the end of one segment or the
1005 beginning of the next. Decide based on vaddr. */
1006 if (hdr->sh_addr >= phdr->p_vaddr
1007 && (hdr->sh_addr + hdr->sh_size
1008 <= phdr->p_vaddr + phdr->p_memsz))
1019 bfd_elf_find_section
1022 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1025 Helper functions for GDB to locate the string tables.
1026 Since BFD hides string tables from callers, GDB needs to use an
1027 internal hook to find them. Sun's .stabstr, in particular,
1028 isn't even pointed to by the .stab section, so ordinary
1029 mechanisms wouldn't work to find it, even if we had some.
1032 struct elf_internal_shdr *
1033 bfd_elf_find_section (bfd *abfd, char *name)
1035 Elf_Internal_Shdr **i_shdrp;
1040 i_shdrp = elf_elfsections (abfd);
1041 if (i_shdrp != NULL)
1043 shstrtab = bfd_elf_get_str_section (abfd,
1044 elf_elfheader (abfd)->e_shstrndx);
1045 if (shstrtab != NULL)
1047 max = elf_numsections (abfd);
1048 for (i = 1; i < max; i++)
1049 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1056 const char *const bfd_elf_section_type_names[] = {
1057 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1058 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1059 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1062 /* ELF relocs are against symbols. If we are producing relocatable
1063 output, and the reloc is against an external symbol, and nothing
1064 has given us any additional addend, the resulting reloc will also
1065 be against the same symbol. In such a case, we don't want to
1066 change anything about the way the reloc is handled, since it will
1067 all be done at final link time. Rather than put special case code
1068 into bfd_perform_relocation, all the reloc types use this howto
1069 function. It just short circuits the reloc if producing
1070 relocatable output against an external symbol. */
1072 bfd_reloc_status_type
1073 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1074 arelent *reloc_entry,
1076 void *data ATTRIBUTE_UNUSED,
1077 asection *input_section,
1079 char **error_message ATTRIBUTE_UNUSED)
1081 if (output_bfd != NULL
1082 && (symbol->flags & BSF_SECTION_SYM) == 0
1083 && (! reloc_entry->howto->partial_inplace
1084 || reloc_entry->addend == 0))
1086 reloc_entry->address += input_section->output_offset;
1087 return bfd_reloc_ok;
1090 return bfd_reloc_continue;
1093 /* Copy the program header and other data from one object module to
1097 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1099 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1100 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1103 BFD_ASSERT (!elf_flags_init (obfd)
1104 || (elf_elfheader (obfd)->e_flags
1105 == elf_elfheader (ibfd)->e_flags));
1107 elf_gp (obfd) = elf_gp (ibfd);
1108 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1109 elf_flags_init (obfd) = TRUE;
1111 /* Copy object attributes. */
1112 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1118 get_segment_type (unsigned int p_type)
1123 case PT_NULL: pt = "NULL"; break;
1124 case PT_LOAD: pt = "LOAD"; break;
1125 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1126 case PT_INTERP: pt = "INTERP"; break;
1127 case PT_NOTE: pt = "NOTE"; break;
1128 case PT_SHLIB: pt = "SHLIB"; break;
1129 case PT_PHDR: pt = "PHDR"; break;
1130 case PT_TLS: pt = "TLS"; break;
1131 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1132 case PT_GNU_STACK: pt = "STACK"; break;
1133 case PT_GNU_RELRO: pt = "RELRO"; break;
1134 default: pt = NULL; break;
1139 /* Print out the program headers. */
1142 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1145 Elf_Internal_Phdr *p;
1147 bfd_byte *dynbuf = NULL;
1149 p = elf_tdata (abfd)->phdr;
1154 fprintf (f, _("\nProgram Header:\n"));
1155 c = elf_elfheader (abfd)->e_phnum;
1156 for (i = 0; i < c; i++, p++)
1158 const char *pt = get_segment_type (p->p_type);
1163 sprintf (buf, "0x%lx", p->p_type);
1166 fprintf (f, "%8s off 0x", pt);
1167 bfd_fprintf_vma (abfd, f, p->p_offset);
1168 fprintf (f, " vaddr 0x");
1169 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1170 fprintf (f, " paddr 0x");
1171 bfd_fprintf_vma (abfd, f, p->p_paddr);
1172 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1173 fprintf (f, " filesz 0x");
1174 bfd_fprintf_vma (abfd, f, p->p_filesz);
1175 fprintf (f, " memsz 0x");
1176 bfd_fprintf_vma (abfd, f, p->p_memsz);
1177 fprintf (f, " flags %c%c%c",
1178 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1179 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1180 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1181 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1182 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1187 s = bfd_get_section_by_name (abfd, ".dynamic");
1190 unsigned int elfsec;
1191 unsigned long shlink;
1192 bfd_byte *extdyn, *extdynend;
1194 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1196 fprintf (f, _("\nDynamic Section:\n"));
1198 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1201 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1202 if (elfsec == SHN_BAD)
1204 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1206 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1207 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1210 extdynend = extdyn + s->size;
1211 for (; extdyn < extdynend; extdyn += extdynsize)
1213 Elf_Internal_Dyn dyn;
1214 const char *name = "";
1216 bfd_boolean stringp;
1217 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1219 (*swap_dyn_in) (abfd, extdyn, &dyn);
1221 if (dyn.d_tag == DT_NULL)
1228 if (bed->elf_backend_get_target_dtag)
1229 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1231 if (!strcmp (name, ""))
1233 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1238 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1239 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1240 case DT_PLTGOT: name = "PLTGOT"; break;
1241 case DT_HASH: name = "HASH"; break;
1242 case DT_STRTAB: name = "STRTAB"; break;
1243 case DT_SYMTAB: name = "SYMTAB"; break;
1244 case DT_RELA: name = "RELA"; break;
1245 case DT_RELASZ: name = "RELASZ"; break;
1246 case DT_RELAENT: name = "RELAENT"; break;
1247 case DT_STRSZ: name = "STRSZ"; break;
1248 case DT_SYMENT: name = "SYMENT"; break;
1249 case DT_INIT: name = "INIT"; break;
1250 case DT_FINI: name = "FINI"; break;
1251 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1252 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1253 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1254 case DT_REL: name = "REL"; break;
1255 case DT_RELSZ: name = "RELSZ"; break;
1256 case DT_RELENT: name = "RELENT"; break;
1257 case DT_PLTREL: name = "PLTREL"; break;
1258 case DT_DEBUG: name = "DEBUG"; break;
1259 case DT_TEXTREL: name = "TEXTREL"; break;
1260 case DT_JMPREL: name = "JMPREL"; break;
1261 case DT_BIND_NOW: name = "BIND_NOW"; break;
1262 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1263 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1264 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1265 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1266 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1267 case DT_FLAGS: name = "FLAGS"; break;
1268 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1269 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1270 case DT_CHECKSUM: name = "CHECKSUM"; break;
1271 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1272 case DT_MOVEENT: name = "MOVEENT"; break;
1273 case DT_MOVESZ: name = "MOVESZ"; break;
1274 case DT_FEATURE: name = "FEATURE"; break;
1275 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1276 case DT_SYMINSZ: name = "SYMINSZ"; break;
1277 case DT_SYMINENT: name = "SYMINENT"; break;
1278 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1279 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1280 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1281 case DT_PLTPAD: name = "PLTPAD"; break;
1282 case DT_MOVETAB: name = "MOVETAB"; break;
1283 case DT_SYMINFO: name = "SYMINFO"; break;
1284 case DT_RELACOUNT: name = "RELACOUNT"; break;
1285 case DT_RELCOUNT: name = "RELCOUNT"; break;
1286 case DT_FLAGS_1: name = "FLAGS_1"; break;
1287 case DT_VERSYM: name = "VERSYM"; break;
1288 case DT_VERDEF: name = "VERDEF"; break;
1289 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1290 case DT_VERNEED: name = "VERNEED"; break;
1291 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1292 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1293 case DT_USED: name = "USED"; break;
1294 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1295 case DT_GNU_HASH: name = "GNU_HASH"; break;
1298 fprintf (f, " %-20s ", name);
1302 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1307 unsigned int tagv = dyn.d_un.d_val;
1309 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1312 fprintf (f, "%s", string);
1321 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1322 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1324 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1328 if (elf_dynverdef (abfd) != 0)
1330 Elf_Internal_Verdef *t;
1332 fprintf (f, _("\nVersion definitions:\n"));
1333 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1335 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1336 t->vd_flags, t->vd_hash,
1337 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1338 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1340 Elf_Internal_Verdaux *a;
1343 for (a = t->vd_auxptr->vda_nextptr;
1347 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1353 if (elf_dynverref (abfd) != 0)
1355 Elf_Internal_Verneed *t;
1357 fprintf (f, _("\nVersion References:\n"));
1358 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1360 Elf_Internal_Vernaux *a;
1362 fprintf (f, _(" required from %s:\n"),
1363 t->vn_filename ? t->vn_filename : "<corrupt>");
1364 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1365 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1366 a->vna_flags, a->vna_other,
1367 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1379 /* Display ELF-specific fields of a symbol. */
1382 bfd_elf_print_symbol (bfd *abfd,
1385 bfd_print_symbol_type how)
1390 case bfd_print_symbol_name:
1391 fprintf (file, "%s", symbol->name);
1393 case bfd_print_symbol_more:
1394 fprintf (file, "elf ");
1395 bfd_fprintf_vma (abfd, file, symbol->value);
1396 fprintf (file, " %lx", (unsigned long) symbol->flags);
1398 case bfd_print_symbol_all:
1400 const char *section_name;
1401 const char *name = NULL;
1402 const struct elf_backend_data *bed;
1403 unsigned char st_other;
1406 section_name = symbol->section ? symbol->section->name : "(*none*)";
1408 bed = get_elf_backend_data (abfd);
1409 if (bed->elf_backend_print_symbol_all)
1410 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1414 name = symbol->name;
1415 bfd_print_symbol_vandf (abfd, file, symbol);
1418 fprintf (file, " %s\t", section_name);
1419 /* Print the "other" value for a symbol. For common symbols,
1420 we've already printed the size; now print the alignment.
1421 For other symbols, we have no specified alignment, and
1422 we've printed the address; now print the size. */
1423 if (symbol->section && bfd_is_com_section (symbol->section))
1424 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1426 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1427 bfd_fprintf_vma (abfd, file, val);
1429 /* If we have version information, print it. */
1430 if (elf_tdata (abfd)->dynversym_section != 0
1431 && (elf_tdata (abfd)->dynverdef_section != 0
1432 || elf_tdata (abfd)->dynverref_section != 0))
1434 unsigned int vernum;
1435 const char *version_string;
1437 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1440 version_string = "";
1441 else if (vernum == 1)
1442 version_string = "Base";
1443 else if (vernum <= elf_tdata (abfd)->cverdefs)
1445 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1448 Elf_Internal_Verneed *t;
1450 version_string = "";
1451 for (t = elf_tdata (abfd)->verref;
1455 Elf_Internal_Vernaux *a;
1457 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1459 if (a->vna_other == vernum)
1461 version_string = a->vna_nodename;
1468 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1469 fprintf (file, " %-11s", version_string);
1474 fprintf (file, " (%s)", version_string);
1475 for (i = 10 - strlen (version_string); i > 0; --i)
1480 /* If the st_other field is not zero, print it. */
1481 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1486 case STV_INTERNAL: fprintf (file, " .internal"); break;
1487 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1488 case STV_PROTECTED: fprintf (file, " .protected"); break;
1490 /* Some other non-defined flags are also present, so print
1492 fprintf (file, " 0x%02x", (unsigned int) st_other);
1495 fprintf (file, " %s", name);
1501 /* Allocate an ELF string table--force the first byte to be zero. */
1503 struct bfd_strtab_hash *
1504 _bfd_elf_stringtab_init (void)
1506 struct bfd_strtab_hash *ret;
1508 ret = _bfd_stringtab_init ();
1513 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1514 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1515 if (loc == (bfd_size_type) -1)
1517 _bfd_stringtab_free (ret);
1524 /* ELF .o/exec file reading */
1526 /* Create a new bfd section from an ELF section header. */
1529 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1531 Elf_Internal_Shdr *hdr;
1532 Elf_Internal_Ehdr *ehdr;
1533 const struct elf_backend_data *bed;
1536 if (shindex >= elf_numsections (abfd))
1539 hdr = elf_elfsections (abfd)[shindex];
1540 ehdr = elf_elfheader (abfd);
1541 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1546 bed = get_elf_backend_data (abfd);
1547 switch (hdr->sh_type)
1550 /* Inactive section. Throw it away. */
1553 case SHT_PROGBITS: /* Normal section with contents. */
1554 case SHT_NOBITS: /* .bss section. */
1555 case SHT_HASH: /* .hash section. */
1556 case SHT_NOTE: /* .note section. */
1557 case SHT_INIT_ARRAY: /* .init_array section. */
1558 case SHT_FINI_ARRAY: /* .fini_array section. */
1559 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1560 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1561 case SHT_GNU_HASH: /* .gnu.hash section. */
1562 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1564 case SHT_DYNAMIC: /* Dynamic linking information. */
1565 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1567 if (hdr->sh_link > elf_numsections (abfd)
1568 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1570 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1572 Elf_Internal_Shdr *dynsymhdr;
1574 /* The shared libraries distributed with hpux11 have a bogus
1575 sh_link field for the ".dynamic" section. Find the
1576 string table for the ".dynsym" section instead. */
1577 if (elf_dynsymtab (abfd) != 0)
1579 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1580 hdr->sh_link = dynsymhdr->sh_link;
1584 unsigned int i, num_sec;
1586 num_sec = elf_numsections (abfd);
1587 for (i = 1; i < num_sec; i++)
1589 dynsymhdr = elf_elfsections (abfd)[i];
1590 if (dynsymhdr->sh_type == SHT_DYNSYM)
1592 hdr->sh_link = dynsymhdr->sh_link;
1600 case SHT_SYMTAB: /* A symbol table */
1601 if (elf_onesymtab (abfd) == shindex)
1604 if (hdr->sh_entsize != bed->s->sizeof_sym)
1606 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1607 elf_onesymtab (abfd) = shindex;
1608 elf_tdata (abfd)->symtab_hdr = *hdr;
1609 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1610 abfd->flags |= HAS_SYMS;
1612 /* Sometimes a shared object will map in the symbol table. If
1613 SHF_ALLOC is set, and this is a shared object, then we also
1614 treat this section as a BFD section. We can not base the
1615 decision purely on SHF_ALLOC, because that flag is sometimes
1616 set in a relocatable object file, which would confuse the
1618 if ((hdr->sh_flags & SHF_ALLOC) != 0
1619 && (abfd->flags & DYNAMIC) != 0
1620 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1624 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1625 can't read symbols without that section loaded as well. It
1626 is most likely specified by the next section header. */
1627 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1629 unsigned int i, num_sec;
1631 num_sec = elf_numsections (abfd);
1632 for (i = shindex + 1; i < num_sec; i++)
1634 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1635 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1636 && hdr2->sh_link == shindex)
1640 for (i = 1; i < shindex; i++)
1642 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1643 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1644 && hdr2->sh_link == shindex)
1648 return bfd_section_from_shdr (abfd, i);
1652 case SHT_DYNSYM: /* A dynamic symbol table */
1653 if (elf_dynsymtab (abfd) == shindex)
1656 if (hdr->sh_entsize != bed->s->sizeof_sym)
1658 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1659 elf_dynsymtab (abfd) = shindex;
1660 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1661 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1662 abfd->flags |= HAS_SYMS;
1664 /* Besides being a symbol table, we also treat this as a regular
1665 section, so that objcopy can handle it. */
1666 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1668 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1669 if (elf_symtab_shndx (abfd) == shindex)
1672 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1673 elf_symtab_shndx (abfd) = shindex;
1674 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1675 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1678 case SHT_STRTAB: /* A string table */
1679 if (hdr->bfd_section != NULL)
1681 if (ehdr->e_shstrndx == shindex)
1683 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1684 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1687 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1690 elf_tdata (abfd)->strtab_hdr = *hdr;
1691 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1694 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1697 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1698 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1699 elf_elfsections (abfd)[shindex] = hdr;
1700 /* We also treat this as a regular section, so that objcopy
1702 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1706 /* If the string table isn't one of the above, then treat it as a
1707 regular section. We need to scan all the headers to be sure,
1708 just in case this strtab section appeared before the above. */
1709 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1711 unsigned int i, num_sec;
1713 num_sec = elf_numsections (abfd);
1714 for (i = 1; i < num_sec; i++)
1716 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1717 if (hdr2->sh_link == shindex)
1719 /* Prevent endless recursion on broken objects. */
1722 if (! bfd_section_from_shdr (abfd, i))
1724 if (elf_onesymtab (abfd) == i)
1726 if (elf_dynsymtab (abfd) == i)
1727 goto dynsymtab_strtab;
1731 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1735 /* *These* do a lot of work -- but build no sections! */
1737 asection *target_sect;
1738 Elf_Internal_Shdr *hdr2;
1739 unsigned int num_sec = elf_numsections (abfd);
1742 != (bfd_size_type) (hdr->sh_type == SHT_REL
1743 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1746 /* Check for a bogus link to avoid crashing. */
1747 if (hdr->sh_link >= num_sec)
1749 ((*_bfd_error_handler)
1750 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1751 abfd, hdr->sh_link, name, shindex));
1752 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1756 /* For some incomprehensible reason Oracle distributes
1757 libraries for Solaris in which some of the objects have
1758 bogus sh_link fields. It would be nice if we could just
1759 reject them, but, unfortunately, some people need to use
1760 them. We scan through the section headers; if we find only
1761 one suitable symbol table, we clobber the sh_link to point
1762 to it. I hope this doesn't break anything. */
1763 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1764 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1770 for (scan = 1; scan < num_sec; scan++)
1772 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1773 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1784 hdr->sh_link = found;
1787 /* Get the symbol table. */
1788 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1789 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1790 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1793 /* If this reloc section does not use the main symbol table we
1794 don't treat it as a reloc section. BFD can't adequately
1795 represent such a section, so at least for now, we don't
1796 try. We just present it as a normal section. We also
1797 can't use it as a reloc section if it points to the null
1798 section, an invalid section, or another reloc section. */
1799 if (hdr->sh_link != elf_onesymtab (abfd)
1800 || hdr->sh_info == SHN_UNDEF
1801 || hdr->sh_info >= num_sec
1802 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1803 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1804 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1807 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1809 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1810 if (target_sect == NULL)
1813 if ((target_sect->flags & SEC_RELOC) == 0
1814 || target_sect->reloc_count == 0)
1815 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1819 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1820 amt = sizeof (*hdr2);
1821 hdr2 = bfd_alloc (abfd, amt);
1824 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1827 elf_elfsections (abfd)[shindex] = hdr2;
1828 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1829 target_sect->flags |= SEC_RELOC;
1830 target_sect->relocation = NULL;
1831 target_sect->rel_filepos = hdr->sh_offset;
1832 /* In the section to which the relocations apply, mark whether
1833 its relocations are of the REL or RELA variety. */
1834 if (hdr->sh_size != 0)
1835 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1836 abfd->flags |= HAS_RELOC;
1840 case SHT_GNU_verdef:
1841 elf_dynverdef (abfd) = shindex;
1842 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1843 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1845 case SHT_GNU_versym:
1846 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1848 elf_dynversym (abfd) = shindex;
1849 elf_tdata (abfd)->dynversym_hdr = *hdr;
1850 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1852 case SHT_GNU_verneed:
1853 elf_dynverref (abfd) = shindex;
1854 elf_tdata (abfd)->dynverref_hdr = *hdr;
1855 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1861 /* We need a BFD section for objcopy and relocatable linking,
1862 and it's handy to have the signature available as the section
1864 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1866 name = group_signature (abfd, hdr);
1869 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1871 if (hdr->contents != NULL)
1873 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1874 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1877 if (idx->flags & GRP_COMDAT)
1878 hdr->bfd_section->flags
1879 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1881 /* We try to keep the same section order as it comes in. */
1883 while (--n_elt != 0)
1887 if (idx->shdr != NULL
1888 && (s = idx->shdr->bfd_section) != NULL
1889 && elf_next_in_group (s) != NULL)
1891 elf_next_in_group (hdr->bfd_section) = s;
1899 /* Possibly an attributes section. */
1900 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1901 || hdr->sh_type == bed->obj_attrs_section_type)
1903 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1905 _bfd_elf_parse_attributes (abfd, hdr);
1909 /* Check for any processor-specific section types. */
1910 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1913 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1915 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1916 /* FIXME: How to properly handle allocated section reserved
1917 for applications? */
1918 (*_bfd_error_handler)
1919 (_("%B: don't know how to handle allocated, application "
1920 "specific section `%s' [0x%8x]"),
1921 abfd, name, hdr->sh_type);
1923 /* Allow sections reserved for applications. */
1924 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1927 else if (hdr->sh_type >= SHT_LOPROC
1928 && hdr->sh_type <= SHT_HIPROC)
1929 /* FIXME: We should handle this section. */
1930 (*_bfd_error_handler)
1931 (_("%B: don't know how to handle processor specific section "
1933 abfd, name, hdr->sh_type);
1934 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1936 /* Unrecognised OS-specific sections. */
1937 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1938 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1939 required to correctly process the section and the file should
1940 be rejected with an error message. */
1941 (*_bfd_error_handler)
1942 (_("%B: don't know how to handle OS specific section "
1944 abfd, name, hdr->sh_type);
1946 /* Otherwise it should be processed. */
1947 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1950 /* FIXME: We should handle this section. */
1951 (*_bfd_error_handler)
1952 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1953 abfd, name, hdr->sh_type);
1961 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1962 Return SEC for sections that have no elf section, and NULL on error. */
1965 bfd_section_from_r_symndx (bfd *abfd,
1966 struct sym_sec_cache *cache,
1968 unsigned long r_symndx)
1970 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1973 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1975 Elf_Internal_Shdr *symtab_hdr;
1976 unsigned char esym[sizeof (Elf64_External_Sym)];
1977 Elf_External_Sym_Shndx eshndx;
1978 Elf_Internal_Sym isym;
1980 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1981 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1982 &isym, esym, &eshndx) == NULL)
1985 if (cache->abfd != abfd)
1987 memset (cache->indx, -1, sizeof (cache->indx));
1990 cache->indx[ent] = r_symndx;
1991 cache->shndx[ent] = isym.st_shndx;
1994 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
2001 /* Given an ELF section number, retrieve the corresponding BFD
2005 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2007 if (index >= elf_numsections (abfd))
2009 return elf_elfsections (abfd)[index]->bfd_section;
2012 static const struct bfd_elf_special_section special_sections_b[] =
2014 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2015 { NULL, 0, 0, 0, 0 }
2018 static const struct bfd_elf_special_section special_sections_c[] =
2020 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2021 { NULL, 0, 0, 0, 0 }
2024 static const struct bfd_elf_special_section special_sections_d[] =
2026 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2027 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2028 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2029 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2030 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2031 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2032 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2033 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2034 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2035 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2036 { NULL, 0, 0, 0, 0 }
2039 static const struct bfd_elf_special_section special_sections_f[] =
2041 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2042 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2043 { NULL, 0, 0, 0, 0 }
2046 static const struct bfd_elf_special_section special_sections_g[] =
2048 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2049 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2050 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2051 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2052 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2053 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2054 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2055 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2056 { NULL, 0, 0, 0, 0 }
2059 static const struct bfd_elf_special_section special_sections_h[] =
2061 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2062 { NULL, 0, 0, 0, 0 }
2065 static const struct bfd_elf_special_section special_sections_i[] =
2067 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2068 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2069 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2070 { NULL, 0, 0, 0, 0 }
2073 static const struct bfd_elf_special_section special_sections_l[] =
2075 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2076 { NULL, 0, 0, 0, 0 }
2079 static const struct bfd_elf_special_section special_sections_n[] =
2081 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2082 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2083 { NULL, 0, 0, 0, 0 }
2086 static const struct bfd_elf_special_section special_sections_p[] =
2088 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2089 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2090 { NULL, 0, 0, 0, 0 }
2093 static const struct bfd_elf_special_section special_sections_r[] =
2095 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2096 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2097 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2098 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2099 { NULL, 0, 0, 0, 0 }
2102 static const struct bfd_elf_special_section special_sections_s[] =
2104 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2105 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2106 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2107 /* See struct bfd_elf_special_section declaration for the semantics of
2108 this special case where .prefix_length != strlen (.prefix). */
2109 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2110 { NULL, 0, 0, 0, 0 }
2113 static const struct bfd_elf_special_section special_sections_t[] =
2115 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2116 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2117 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2118 { NULL, 0, 0, 0, 0 }
2121 static const struct bfd_elf_special_section special_sections_z[] =
2123 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2124 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2125 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2126 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2127 { NULL, 0, 0, 0, 0 }
2130 static const struct bfd_elf_special_section *special_sections[] =
2132 special_sections_b, /* 'b' */
2133 special_sections_c, /* 'c' */
2134 special_sections_d, /* 'd' */
2136 special_sections_f, /* 'f' */
2137 special_sections_g, /* 'g' */
2138 special_sections_h, /* 'h' */
2139 special_sections_i, /* 'i' */
2142 special_sections_l, /* 'l' */
2144 special_sections_n, /* 'n' */
2146 special_sections_p, /* 'p' */
2148 special_sections_r, /* 'r' */
2149 special_sections_s, /* 's' */
2150 special_sections_t, /* 't' */
2156 special_sections_z /* 'z' */
2159 const struct bfd_elf_special_section *
2160 _bfd_elf_get_special_section (const char *name,
2161 const struct bfd_elf_special_section *spec,
2167 len = strlen (name);
2169 for (i = 0; spec[i].prefix != NULL; i++)
2172 int prefix_len = spec[i].prefix_length;
2174 if (len < prefix_len)
2176 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2179 suffix_len = spec[i].suffix_length;
2180 if (suffix_len <= 0)
2182 if (name[prefix_len] != 0)
2184 if (suffix_len == 0)
2186 if (name[prefix_len] != '.'
2187 && (suffix_len == -2
2188 || (rela && spec[i].type == SHT_REL)))
2194 if (len < prefix_len + suffix_len)
2196 if (memcmp (name + len - suffix_len,
2197 spec[i].prefix + prefix_len,
2207 const struct bfd_elf_special_section *
2208 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2211 const struct bfd_elf_special_section *spec;
2212 const struct elf_backend_data *bed;
2214 /* See if this is one of the special sections. */
2215 if (sec->name == NULL)
2218 bed = get_elf_backend_data (abfd);
2219 spec = bed->special_sections;
2222 spec = _bfd_elf_get_special_section (sec->name,
2223 bed->special_sections,
2229 if (sec->name[0] != '.')
2232 i = sec->name[1] - 'b';
2233 if (i < 0 || i > 'z' - 'b')
2236 spec = special_sections[i];
2241 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2245 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2247 struct bfd_elf_section_data *sdata;
2248 const struct elf_backend_data *bed;
2249 const struct bfd_elf_special_section *ssect;
2251 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2254 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2257 sec->used_by_bfd = sdata;
2260 /* Indicate whether or not this section should use RELA relocations. */
2261 bed = get_elf_backend_data (abfd);
2262 sec->use_rela_p = bed->default_use_rela_p;
2264 /* When we read a file, we don't need to set ELF section type and
2265 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2266 anyway. We will set ELF section type and flags for all linker
2267 created sections. If user specifies BFD section flags, we will
2268 set ELF section type and flags based on BFD section flags in
2269 elf_fake_sections. */
2270 if ((!sec->flags && abfd->direction != read_direction)
2271 || (sec->flags & SEC_LINKER_CREATED) != 0)
2273 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2276 elf_section_type (sec) = ssect->type;
2277 elf_section_flags (sec) = ssect->attr;
2281 return _bfd_generic_new_section_hook (abfd, sec);
2284 /* Create a new bfd section from an ELF program header.
2286 Since program segments have no names, we generate a synthetic name
2287 of the form segment<NUM>, where NUM is generally the index in the
2288 program header table. For segments that are split (see below) we
2289 generate the names segment<NUM>a and segment<NUM>b.
2291 Note that some program segments may have a file size that is different than
2292 (less than) the memory size. All this means is that at execution the
2293 system must allocate the amount of memory specified by the memory size,
2294 but only initialize it with the first "file size" bytes read from the
2295 file. This would occur for example, with program segments consisting
2296 of combined data+bss.
2298 To handle the above situation, this routine generates TWO bfd sections
2299 for the single program segment. The first has the length specified by
2300 the file size of the segment, and the second has the length specified
2301 by the difference between the two sizes. In effect, the segment is split
2302 into its initialized and uninitialized parts.
2307 _bfd_elf_make_section_from_phdr (bfd *abfd,
2308 Elf_Internal_Phdr *hdr,
2310 const char *typename)
2318 split = ((hdr->p_memsz > 0)
2319 && (hdr->p_filesz > 0)
2320 && (hdr->p_memsz > hdr->p_filesz));
2322 if (hdr->p_filesz > 0)
2324 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2325 len = strlen (namebuf) + 1;
2326 name = bfd_alloc (abfd, len);
2329 memcpy (name, namebuf, len);
2330 newsect = bfd_make_section (abfd, name);
2331 if (newsect == NULL)
2333 newsect->vma = hdr->p_vaddr;
2334 newsect->lma = hdr->p_paddr;
2335 newsect->size = hdr->p_filesz;
2336 newsect->filepos = hdr->p_offset;
2337 newsect->flags |= SEC_HAS_CONTENTS;
2338 newsect->alignment_power = bfd_log2 (hdr->p_align);
2339 if (hdr->p_type == PT_LOAD)
2341 newsect->flags |= SEC_ALLOC;
2342 newsect->flags |= SEC_LOAD;
2343 if (hdr->p_flags & PF_X)
2345 /* FIXME: all we known is that it has execute PERMISSION,
2347 newsect->flags |= SEC_CODE;
2350 if (!(hdr->p_flags & PF_W))
2352 newsect->flags |= SEC_READONLY;
2356 if (hdr->p_memsz > hdr->p_filesz)
2360 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
2361 len = strlen (namebuf) + 1;
2362 name = bfd_alloc (abfd, len);
2365 memcpy (name, namebuf, len);
2366 newsect = bfd_make_section (abfd, name);
2367 if (newsect == NULL)
2369 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2370 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2371 newsect->size = hdr->p_memsz - hdr->p_filesz;
2372 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2373 align = newsect->vma & -newsect->vma;
2374 if (align == 0 || align > hdr->p_align)
2375 align = hdr->p_align;
2376 newsect->alignment_power = bfd_log2 (align);
2377 if (hdr->p_type == PT_LOAD)
2379 /* Hack for gdb. Segments that have not been modified do
2380 not have their contents written to a core file, on the
2381 assumption that a debugger can find the contents in the
2382 executable. We flag this case by setting the fake
2383 section size to zero. Note that "real" bss sections will
2384 always have their contents dumped to the core file. */
2385 if (bfd_get_format (abfd) == bfd_core)
2387 newsect->flags |= SEC_ALLOC;
2388 if (hdr->p_flags & PF_X)
2389 newsect->flags |= SEC_CODE;
2391 if (!(hdr->p_flags & PF_W))
2392 newsect->flags |= SEC_READONLY;
2399 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2401 const struct elf_backend_data *bed;
2403 switch (hdr->p_type)
2406 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2409 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2412 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2415 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2418 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2420 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2425 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2428 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2430 case PT_GNU_EH_FRAME:
2431 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2435 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2438 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2441 /* Check for any processor-specific program segment types. */
2442 bed = get_elf_backend_data (abfd);
2443 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2447 /* Initialize REL_HDR, the section-header for new section, containing
2448 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2449 relocations; otherwise, we use REL relocations. */
2452 _bfd_elf_init_reloc_shdr (bfd *abfd,
2453 Elf_Internal_Shdr *rel_hdr,
2455 bfd_boolean use_rela_p)
2458 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2459 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2461 name = bfd_alloc (abfd, amt);
2464 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2466 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2468 if (rel_hdr->sh_name == (unsigned int) -1)
2470 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2471 rel_hdr->sh_entsize = (use_rela_p
2472 ? bed->s->sizeof_rela
2473 : bed->s->sizeof_rel);
2474 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2475 rel_hdr->sh_flags = 0;
2476 rel_hdr->sh_addr = 0;
2477 rel_hdr->sh_size = 0;
2478 rel_hdr->sh_offset = 0;
2483 /* Set up an ELF internal section header for a section. */
2486 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2488 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2489 bfd_boolean *failedptr = failedptrarg;
2490 Elf_Internal_Shdr *this_hdr;
2491 unsigned int sh_type;
2495 /* We already failed; just get out of the bfd_map_over_sections
2500 this_hdr = &elf_section_data (asect)->this_hdr;
2502 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2503 asect->name, FALSE);
2504 if (this_hdr->sh_name == (unsigned int) -1)
2510 /* Don't clear sh_flags. Assembler may set additional bits. */
2512 if ((asect->flags & SEC_ALLOC) != 0
2513 || asect->user_set_vma)
2514 this_hdr->sh_addr = asect->vma;
2516 this_hdr->sh_addr = 0;
2518 this_hdr->sh_offset = 0;
2519 this_hdr->sh_size = asect->size;
2520 this_hdr->sh_link = 0;
2521 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2522 /* The sh_entsize and sh_info fields may have been set already by
2523 copy_private_section_data. */
2525 this_hdr->bfd_section = asect;
2526 this_hdr->contents = NULL;
2528 /* If the section type is unspecified, we set it based on
2530 if ((asect->flags & SEC_GROUP) != 0)
2531 sh_type = SHT_GROUP;
2532 else if ((asect->flags & SEC_ALLOC) != 0
2533 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2534 || (asect->flags & SEC_NEVER_LOAD) != 0))
2535 sh_type = SHT_NOBITS;
2537 sh_type = SHT_PROGBITS;
2539 if (this_hdr->sh_type == SHT_NULL)
2540 this_hdr->sh_type = sh_type;
2541 else if (this_hdr->sh_type == SHT_NOBITS
2542 && sh_type == SHT_PROGBITS
2543 && (asect->flags & SEC_ALLOC) != 0)
2545 /* Warn if we are changing a NOBITS section to PROGBITS, but
2546 allow the link to proceed. This can happen when users link
2547 non-bss input sections to bss output sections, or emit data
2548 to a bss output section via a linker script. */
2549 (*_bfd_error_handler)
2550 (_("warning: section `%A' type changed to PROGBITS"), asect);
2551 this_hdr->sh_type = sh_type;
2554 switch (this_hdr->sh_type)
2560 case SHT_INIT_ARRAY:
2561 case SHT_FINI_ARRAY:
2562 case SHT_PREINIT_ARRAY:
2569 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2573 this_hdr->sh_entsize = bed->s->sizeof_sym;
2577 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2581 if (get_elf_backend_data (abfd)->may_use_rela_p)
2582 this_hdr->sh_entsize = bed->s->sizeof_rela;
2586 if (get_elf_backend_data (abfd)->may_use_rel_p)
2587 this_hdr->sh_entsize = bed->s->sizeof_rel;
2590 case SHT_GNU_versym:
2591 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2594 case SHT_GNU_verdef:
2595 this_hdr->sh_entsize = 0;
2596 /* objcopy or strip will copy over sh_info, but may not set
2597 cverdefs. The linker will set cverdefs, but sh_info will be
2599 if (this_hdr->sh_info == 0)
2600 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2602 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2603 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2606 case SHT_GNU_verneed:
2607 this_hdr->sh_entsize = 0;
2608 /* objcopy or strip will copy over sh_info, but may not set
2609 cverrefs. The linker will set cverrefs, but sh_info will be
2611 if (this_hdr->sh_info == 0)
2612 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2614 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2615 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2619 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2623 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2627 if ((asect->flags & SEC_ALLOC) != 0)
2628 this_hdr->sh_flags |= SHF_ALLOC;
2629 if ((asect->flags & SEC_READONLY) == 0)
2630 this_hdr->sh_flags |= SHF_WRITE;
2631 if ((asect->flags & SEC_CODE) != 0)
2632 this_hdr->sh_flags |= SHF_EXECINSTR;
2633 if ((asect->flags & SEC_MERGE) != 0)
2635 this_hdr->sh_flags |= SHF_MERGE;
2636 this_hdr->sh_entsize = asect->entsize;
2637 if ((asect->flags & SEC_STRINGS) != 0)
2638 this_hdr->sh_flags |= SHF_STRINGS;
2640 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2641 this_hdr->sh_flags |= SHF_GROUP;
2642 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2644 this_hdr->sh_flags |= SHF_TLS;
2645 if (asect->size == 0
2646 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2648 struct bfd_link_order *o = asect->map_tail.link_order;
2650 this_hdr->sh_size = 0;
2653 this_hdr->sh_size = o->offset + o->size;
2654 if (this_hdr->sh_size != 0)
2655 this_hdr->sh_type = SHT_NOBITS;
2660 /* Check for processor-specific section types. */
2661 sh_type = this_hdr->sh_type;
2662 if (bed->elf_backend_fake_sections
2663 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2666 if (sh_type == SHT_NOBITS && asect->size != 0)
2668 /* Don't change the header type from NOBITS if we are being
2669 called for objcopy --only-keep-debug. */
2670 this_hdr->sh_type = sh_type;
2673 /* If the section has relocs, set up a section header for the
2674 SHT_REL[A] section. If two relocation sections are required for
2675 this section, it is up to the processor-specific back-end to
2676 create the other. */
2677 if ((asect->flags & SEC_RELOC) != 0
2678 && !_bfd_elf_init_reloc_shdr (abfd,
2679 &elf_section_data (asect)->rel_hdr,
2685 /* Fill in the contents of a SHT_GROUP section. */
2688 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2690 bfd_boolean *failedptr = failedptrarg;
2691 unsigned long symindx;
2692 asection *elt, *first;
2696 /* Ignore linker created group section. See elfNN_ia64_object_p in
2698 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2703 if (elf_group_id (sec) != NULL)
2704 symindx = elf_group_id (sec)->udata.i;
2708 /* If called from the assembler, swap_out_syms will have set up
2709 elf_section_syms; If called for "ld -r", use target_index. */
2710 if (elf_section_syms (abfd) != NULL)
2711 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2713 symindx = sec->target_index;
2715 elf_section_data (sec)->this_hdr.sh_info = symindx;
2717 /* The contents won't be allocated for "ld -r" or objcopy. */
2719 if (sec->contents == NULL)
2722 sec->contents = bfd_alloc (abfd, sec->size);
2724 /* Arrange for the section to be written out. */
2725 elf_section_data (sec)->this_hdr.contents = sec->contents;
2726 if (sec->contents == NULL)
2733 loc = sec->contents + sec->size;
2735 /* Get the pointer to the first section in the group that gas
2736 squirreled away here. objcopy arranges for this to be set to the
2737 start of the input section group. */
2738 first = elt = elf_next_in_group (sec);
2740 /* First element is a flag word. Rest of section is elf section
2741 indices for all the sections of the group. Write them backwards
2742 just to keep the group in the same order as given in .section
2743 directives, not that it matters. */
2752 s = s->output_section;
2755 idx = elf_section_data (s)->this_idx;
2756 H_PUT_32 (abfd, idx, loc);
2757 elt = elf_next_in_group (elt);
2762 if ((loc -= 4) != sec->contents)
2765 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2768 /* Assign all ELF section numbers. The dummy first section is handled here
2769 too. The link/info pointers for the standard section types are filled
2770 in here too, while we're at it. */
2773 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2775 struct elf_obj_tdata *t = elf_tdata (abfd);
2777 unsigned int section_number, secn;
2778 Elf_Internal_Shdr **i_shdrp;
2779 struct bfd_elf_section_data *d;
2783 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2785 /* SHT_GROUP sections are in relocatable files only. */
2786 if (link_info == NULL || link_info->relocatable)
2788 /* Put SHT_GROUP sections first. */
2789 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2791 d = elf_section_data (sec);
2793 if (d->this_hdr.sh_type == SHT_GROUP)
2795 if (sec->flags & SEC_LINKER_CREATED)
2797 /* Remove the linker created SHT_GROUP sections. */
2798 bfd_section_list_remove (abfd, sec);
2799 abfd->section_count--;
2802 d->this_idx = section_number++;
2807 for (sec = abfd->sections; sec; sec = sec->next)
2809 d = elf_section_data (sec);
2811 if (d->this_hdr.sh_type != SHT_GROUP)
2812 d->this_idx = section_number++;
2813 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2814 if ((sec->flags & SEC_RELOC) == 0)
2818 d->rel_idx = section_number++;
2819 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2824 d->rel_idx2 = section_number++;
2825 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2831 t->shstrtab_section = section_number++;
2832 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2833 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2835 if (bfd_get_symcount (abfd) > 0)
2837 t->symtab_section = section_number++;
2838 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2839 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2841 t->symtab_shndx_section = section_number++;
2842 t->symtab_shndx_hdr.sh_name
2843 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2844 ".symtab_shndx", FALSE);
2845 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2848 t->strtab_section = section_number++;
2849 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2852 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2853 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2855 elf_numsections (abfd) = section_number;
2856 elf_elfheader (abfd)->e_shnum = section_number;
2858 /* Set up the list of section header pointers, in agreement with the
2860 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2861 if (i_shdrp == NULL)
2864 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2865 if (i_shdrp[0] == NULL)
2867 bfd_release (abfd, i_shdrp);
2871 elf_elfsections (abfd) = i_shdrp;
2873 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2874 if (bfd_get_symcount (abfd) > 0)
2876 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2877 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2879 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2880 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2882 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2883 t->symtab_hdr.sh_link = t->strtab_section;
2886 for (sec = abfd->sections; sec; sec = sec->next)
2888 struct bfd_elf_section_data *d = elf_section_data (sec);
2892 i_shdrp[d->this_idx] = &d->this_hdr;
2893 if (d->rel_idx != 0)
2894 i_shdrp[d->rel_idx] = &d->rel_hdr;
2895 if (d->rel_idx2 != 0)
2896 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2898 /* Fill in the sh_link and sh_info fields while we're at it. */
2900 /* sh_link of a reloc section is the section index of the symbol
2901 table. sh_info is the section index of the section to which
2902 the relocation entries apply. */
2903 if (d->rel_idx != 0)
2905 d->rel_hdr.sh_link = t->symtab_section;
2906 d->rel_hdr.sh_info = d->this_idx;
2908 if (d->rel_idx2 != 0)
2910 d->rel_hdr2->sh_link = t->symtab_section;
2911 d->rel_hdr2->sh_info = d->this_idx;
2914 /* We need to set up sh_link for SHF_LINK_ORDER. */
2915 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2917 s = elf_linked_to_section (sec);
2920 /* elf_linked_to_section points to the input section. */
2921 if (link_info != NULL)
2923 /* Check discarded linkonce section. */
2924 if (elf_discarded_section (s))
2927 (*_bfd_error_handler)
2928 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2929 abfd, d->this_hdr.bfd_section,
2931 /* Point to the kept section if it has the same
2932 size as the discarded one. */
2933 kept = _bfd_elf_check_kept_section (s, link_info);
2936 bfd_set_error (bfd_error_bad_value);
2942 s = s->output_section;
2943 BFD_ASSERT (s != NULL);
2947 /* Handle objcopy. */
2948 if (s->output_section == NULL)
2950 (*_bfd_error_handler)
2951 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2952 abfd, d->this_hdr.bfd_section, s, s->owner);
2953 bfd_set_error (bfd_error_bad_value);
2956 s = s->output_section;
2958 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2963 The Intel C compiler generates SHT_IA_64_UNWIND with
2964 SHF_LINK_ORDER. But it doesn't set the sh_link or
2965 sh_info fields. Hence we could get the situation
2967 const struct elf_backend_data *bed
2968 = get_elf_backend_data (abfd);
2969 if (bed->link_order_error_handler)
2970 bed->link_order_error_handler
2971 (_("%B: warning: sh_link not set for section `%A'"),
2976 switch (d->this_hdr.sh_type)
2980 /* A reloc section which we are treating as a normal BFD
2981 section. sh_link is the section index of the symbol
2982 table. sh_info is the section index of the section to
2983 which the relocation entries apply. We assume that an
2984 allocated reloc section uses the dynamic symbol table.
2985 FIXME: How can we be sure? */
2986 s = bfd_get_section_by_name (abfd, ".dynsym");
2988 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2990 /* We look up the section the relocs apply to by name. */
2992 if (d->this_hdr.sh_type == SHT_REL)
2996 s = bfd_get_section_by_name (abfd, name);
2998 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3002 /* We assume that a section named .stab*str is a stabs
3003 string section. We look for a section with the same name
3004 but without the trailing ``str'', and set its sh_link
3005 field to point to this section. */
3006 if (CONST_STRNEQ (sec->name, ".stab")
3007 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3012 len = strlen (sec->name);
3013 alc = bfd_malloc (len - 2);
3016 memcpy (alc, sec->name, len - 3);
3017 alc[len - 3] = '\0';
3018 s = bfd_get_section_by_name (abfd, alc);
3022 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3024 /* This is a .stab section. */
3025 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3026 elf_section_data (s)->this_hdr.sh_entsize
3027 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3034 case SHT_GNU_verneed:
3035 case SHT_GNU_verdef:
3036 /* sh_link is the section header index of the string table
3037 used for the dynamic entries, or the symbol table, or the
3039 s = bfd_get_section_by_name (abfd, ".dynstr");
3041 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3044 case SHT_GNU_LIBLIST:
3045 /* sh_link is the section header index of the prelink library
3046 list used for the dynamic entries, or the symbol table, or
3047 the version strings. */
3048 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3049 ? ".dynstr" : ".gnu.libstr");
3051 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3056 case SHT_GNU_versym:
3057 /* sh_link is the section header index of the symbol table
3058 this hash table or version table is for. */
3059 s = bfd_get_section_by_name (abfd, ".dynsym");
3061 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3065 d->this_hdr.sh_link = t->symtab_section;
3069 for (secn = 1; secn < section_number; ++secn)
3070 if (i_shdrp[secn] == NULL)
3071 i_shdrp[secn] = i_shdrp[0];
3073 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3074 i_shdrp[secn]->sh_name);
3078 /* Map symbol from it's internal number to the external number, moving
3079 all local symbols to be at the head of the list. */
3082 sym_is_global (bfd *abfd, asymbol *sym)
3084 /* If the backend has a special mapping, use it. */
3085 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3086 if (bed->elf_backend_sym_is_global)
3087 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3089 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3090 || bfd_is_und_section (bfd_get_section (sym))
3091 || bfd_is_com_section (bfd_get_section (sym)));
3094 /* Don't output section symbols for sections that are not going to be
3098 ignore_section_sym (bfd *abfd, asymbol *sym)
3100 return ((sym->flags & BSF_SECTION_SYM) != 0
3101 && !(sym->section->owner == abfd
3102 || (sym->section->output_section->owner == abfd
3103 && sym->section->output_offset == 0)));
3107 elf_map_symbols (bfd *abfd)
3109 unsigned int symcount = bfd_get_symcount (abfd);
3110 asymbol **syms = bfd_get_outsymbols (abfd);
3111 asymbol **sect_syms;
3112 unsigned int num_locals = 0;
3113 unsigned int num_globals = 0;
3114 unsigned int num_locals2 = 0;
3115 unsigned int num_globals2 = 0;
3122 fprintf (stderr, "elf_map_symbols\n");
3126 for (asect = abfd->sections; asect; asect = asect->next)
3128 if (max_index < asect->index)
3129 max_index = asect->index;
3133 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3134 if (sect_syms == NULL)
3136 elf_section_syms (abfd) = sect_syms;
3137 elf_num_section_syms (abfd) = max_index;
3139 /* Init sect_syms entries for any section symbols we have already
3140 decided to output. */
3141 for (idx = 0; idx < symcount; idx++)
3143 asymbol *sym = syms[idx];
3145 if ((sym->flags & BSF_SECTION_SYM) != 0
3147 && !ignore_section_sym (abfd, sym))
3149 asection *sec = sym->section;
3151 if (sec->owner != abfd)
3152 sec = sec->output_section;
3154 sect_syms[sec->index] = syms[idx];
3158 /* Classify all of the symbols. */
3159 for (idx = 0; idx < symcount; idx++)
3161 if (ignore_section_sym (abfd, syms[idx]))
3163 if (!sym_is_global (abfd, syms[idx]))
3169 /* We will be adding a section symbol for each normal BFD section. Most
3170 sections will already have a section symbol in outsymbols, but
3171 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3172 at least in that case. */
3173 for (asect = abfd->sections; asect; asect = asect->next)
3175 if (sect_syms[asect->index] == NULL)
3177 if (!sym_is_global (abfd, asect->symbol))
3184 /* Now sort the symbols so the local symbols are first. */
3185 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3187 if (new_syms == NULL)
3190 for (idx = 0; idx < symcount; idx++)
3192 asymbol *sym = syms[idx];
3195 if (ignore_section_sym (abfd, sym))
3197 if (!sym_is_global (abfd, sym))
3200 i = num_locals + num_globals2++;
3202 sym->udata.i = i + 1;
3204 for (asect = abfd->sections; asect; asect = asect->next)
3206 if (sect_syms[asect->index] == NULL)
3208 asymbol *sym = asect->symbol;
3211 sect_syms[asect->index] = sym;
3212 if (!sym_is_global (abfd, sym))
3215 i = num_locals + num_globals2++;
3217 sym->udata.i = i + 1;
3221 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3223 elf_num_locals (abfd) = num_locals;
3224 elf_num_globals (abfd) = num_globals;
3228 /* Align to the maximum file alignment that could be required for any
3229 ELF data structure. */
3231 static inline file_ptr
3232 align_file_position (file_ptr off, int align)
3234 return (off + align - 1) & ~(align - 1);
3237 /* Assign a file position to a section, optionally aligning to the
3238 required section alignment. */
3241 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3245 if (align && i_shdrp->sh_addralign > 1)
3246 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3247 i_shdrp->sh_offset = offset;
3248 if (i_shdrp->bfd_section != NULL)
3249 i_shdrp->bfd_section->filepos = offset;
3250 if (i_shdrp->sh_type != SHT_NOBITS)
3251 offset += i_shdrp->sh_size;
3255 /* Compute the file positions we are going to put the sections at, and
3256 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3257 is not NULL, this is being called by the ELF backend linker. */
3260 _bfd_elf_compute_section_file_positions (bfd *abfd,
3261 struct bfd_link_info *link_info)
3263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3265 struct bfd_strtab_hash *strtab = NULL;
3266 Elf_Internal_Shdr *shstrtab_hdr;
3268 if (abfd->output_has_begun)
3271 /* Do any elf backend specific processing first. */
3272 if (bed->elf_backend_begin_write_processing)
3273 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3275 if (! prep_headers (abfd))
3278 /* Post process the headers if necessary. */
3279 if (bed->elf_backend_post_process_headers)
3280 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3283 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3287 if (!assign_section_numbers (abfd, link_info))
3290 /* The backend linker builds symbol table information itself. */
3291 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3293 /* Non-zero if doing a relocatable link. */
3294 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3296 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3300 if (link_info == NULL)
3302 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3307 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3308 /* sh_name was set in prep_headers. */
3309 shstrtab_hdr->sh_type = SHT_STRTAB;
3310 shstrtab_hdr->sh_flags = 0;
3311 shstrtab_hdr->sh_addr = 0;
3312 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3313 shstrtab_hdr->sh_entsize = 0;
3314 shstrtab_hdr->sh_link = 0;
3315 shstrtab_hdr->sh_info = 0;
3316 /* sh_offset is set in assign_file_positions_except_relocs. */
3317 shstrtab_hdr->sh_addralign = 1;
3319 if (!assign_file_positions_except_relocs (abfd, link_info))
3322 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3325 Elf_Internal_Shdr *hdr;
3327 off = elf_tdata (abfd)->next_file_pos;
3329 hdr = &elf_tdata (abfd)->symtab_hdr;
3330 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3332 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3333 if (hdr->sh_size != 0)
3334 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3336 hdr = &elf_tdata (abfd)->strtab_hdr;
3337 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3339 elf_tdata (abfd)->next_file_pos = off;
3341 /* Now that we know where the .strtab section goes, write it
3343 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3344 || ! _bfd_stringtab_emit (abfd, strtab))
3346 _bfd_stringtab_free (strtab);
3349 abfd->output_has_begun = TRUE;
3354 /* Make an initial estimate of the size of the program header. If we
3355 get the number wrong here, we'll redo section placement. */
3357 static bfd_size_type
3358 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3362 const struct elf_backend_data *bed;
3364 /* Assume we will need exactly two PT_LOAD segments: one for text
3365 and one for data. */
3368 s = bfd_get_section_by_name (abfd, ".interp");
3369 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3371 /* If we have a loadable interpreter section, we need a
3372 PT_INTERP segment. In this case, assume we also need a
3373 PT_PHDR segment, although that may not be true for all
3378 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3380 /* We need a PT_DYNAMIC segment. */
3386 /* We need a PT_GNU_RELRO segment. */
3390 if (elf_tdata (abfd)->eh_frame_hdr)
3392 /* We need a PT_GNU_EH_FRAME segment. */
3396 if (elf_tdata (abfd)->stack_flags)
3398 /* We need a PT_GNU_STACK segment. */
3402 for (s = abfd->sections; s != NULL; s = s->next)
3404 if ((s->flags & SEC_LOAD) != 0
3405 && CONST_STRNEQ (s->name, ".note"))
3407 /* We need a PT_NOTE segment. */
3409 /* Try to create just one PT_NOTE segment
3410 for all adjacent loadable .note* sections.
3411 gABI requires that within a PT_NOTE segment
3412 (and also inside of each SHT_NOTE section)
3413 each note is padded to a multiple of 4 size,
3414 so we check whether the sections are correctly
3416 if (s->alignment_power == 2)
3417 while (s->next != NULL
3418 && s->next->alignment_power == 2
3419 && (s->next->flags & SEC_LOAD) != 0
3420 && CONST_STRNEQ (s->next->name, ".note"))
3425 for (s = abfd->sections; s != NULL; s = s->next)
3427 if (s->flags & SEC_THREAD_LOCAL)
3429 /* We need a PT_TLS segment. */
3435 /* Let the backend count up any program headers it might need. */
3436 bed = get_elf_backend_data (abfd);
3437 if (bed->elf_backend_additional_program_headers)
3441 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3447 return segs * bed->s->sizeof_phdr;
3450 /* Find the segment that contains the output_section of section. */
3453 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3455 struct elf_segment_map *m;
3456 Elf_Internal_Phdr *p;
3458 for (m = elf_tdata (abfd)->segment_map,
3459 p = elf_tdata (abfd)->phdr;
3465 for (i = m->count - 1; i >= 0; i--)
3466 if (m->sections[i] == section)
3473 /* Create a mapping from a set of sections to a program segment. */
3475 static struct elf_segment_map *
3476 make_mapping (bfd *abfd,
3477 asection **sections,
3482 struct elf_segment_map *m;
3487 amt = sizeof (struct elf_segment_map);
3488 amt += (to - from - 1) * sizeof (asection *);
3489 m = bfd_zalloc (abfd, amt);
3493 m->p_type = PT_LOAD;
3494 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3495 m->sections[i - from] = *hdrpp;
3496 m->count = to - from;
3498 if (from == 0 && phdr)
3500 /* Include the headers in the first PT_LOAD segment. */
3501 m->includes_filehdr = 1;
3502 m->includes_phdrs = 1;
3508 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3511 struct elf_segment_map *
3512 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3514 struct elf_segment_map *m;
3516 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3520 m->p_type = PT_DYNAMIC;
3522 m->sections[0] = dynsec;
3527 /* Possibly add or remove segments from the segment map. */
3530 elf_modify_segment_map (bfd *abfd,
3531 struct bfd_link_info *info,
3532 bfd_boolean remove_empty_load)
3534 struct elf_segment_map **m;
3535 const struct elf_backend_data *bed;
3537 /* The placement algorithm assumes that non allocated sections are
3538 not in PT_LOAD segments. We ensure this here by removing such
3539 sections from the segment map. We also remove excluded
3540 sections. Finally, any PT_LOAD segment without sections is
3542 m = &elf_tdata (abfd)->segment_map;
3545 unsigned int i, new_count;
3547 for (new_count = 0, i = 0; i < (*m)->count; i++)
3549 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3550 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3551 || (*m)->p_type != PT_LOAD))
3553 (*m)->sections[new_count] = (*m)->sections[i];
3557 (*m)->count = new_count;
3559 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3565 bed = get_elf_backend_data (abfd);
3566 if (bed->elf_backend_modify_segment_map != NULL)
3568 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3575 /* Set up a mapping from BFD sections to program segments. */
3578 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3581 struct elf_segment_map *m;
3582 asection **sections = NULL;
3583 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3584 bfd_boolean no_user_phdrs;
3586 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3587 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3591 struct elf_segment_map *mfirst;
3592 struct elf_segment_map **pm;
3595 unsigned int phdr_index;
3596 bfd_vma maxpagesize;
3598 bfd_boolean phdr_in_segment = TRUE;
3599 bfd_boolean writable;
3601 asection *first_tls = NULL;
3602 asection *dynsec, *eh_frame_hdr;
3605 /* Select the allocated sections, and sort them. */
3607 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3608 if (sections == NULL)
3612 for (s = abfd->sections; s != NULL; s = s->next)
3614 if ((s->flags & SEC_ALLOC) != 0)
3620 BFD_ASSERT (i <= bfd_count_sections (abfd));
3623 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3625 /* Build the mapping. */
3630 /* If we have a .interp section, then create a PT_PHDR segment for
3631 the program headers and a PT_INTERP segment for the .interp
3633 s = bfd_get_section_by_name (abfd, ".interp");
3634 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3636 amt = sizeof (struct elf_segment_map);
3637 m = bfd_zalloc (abfd, amt);
3641 m->p_type = PT_PHDR;
3642 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3643 m->p_flags = PF_R | PF_X;
3644 m->p_flags_valid = 1;
3645 m->includes_phdrs = 1;
3650 amt = sizeof (struct elf_segment_map);
3651 m = bfd_zalloc (abfd, amt);
3655 m->p_type = PT_INTERP;
3663 /* Look through the sections. We put sections in the same program
3664 segment when the start of the second section can be placed within
3665 a few bytes of the end of the first section. */
3669 maxpagesize = bed->maxpagesize;
3671 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3673 && (dynsec->flags & SEC_LOAD) == 0)
3676 /* Deal with -Ttext or something similar such that the first section
3677 is not adjacent to the program headers. This is an
3678 approximation, since at this point we don't know exactly how many
3679 program headers we will need. */
3682 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3684 if (phdr_size == (bfd_size_type) -1)
3685 phdr_size = get_program_header_size (abfd, info);
3686 if ((abfd->flags & D_PAGED) == 0
3687 || sections[0]->lma < phdr_size
3688 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3689 phdr_in_segment = FALSE;
3692 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3695 bfd_boolean new_segment;
3699 /* See if this section and the last one will fit in the same
3702 if (last_hdr == NULL)
3704 /* If we don't have a segment yet, then we don't need a new
3705 one (we build the last one after this loop). */
3706 new_segment = FALSE;
3708 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3710 /* If this section has a different relation between the
3711 virtual address and the load address, then we need a new
3715 /* In the next test we have to be careful when last_hdr->lma is close
3716 to the end of the address space. If the aligned address wraps
3717 around to the start of the address space, then there are no more
3718 pages left in memory and it is OK to assume that the current
3719 section can be included in the current segment. */
3720 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3722 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3725 /* If putting this section in this segment would force us to
3726 skip a page in the segment, then we need a new segment. */
3729 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3730 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3732 /* We don't want to put a loadable section after a
3733 nonloadable section in the same segment.
3734 Consider .tbss sections as loadable for this purpose. */
3737 else if ((abfd->flags & D_PAGED) == 0)
3739 /* If the file is not demand paged, which means that we
3740 don't require the sections to be correctly aligned in the
3741 file, then there is no other reason for a new segment. */
3742 new_segment = FALSE;
3745 && (hdr->flags & SEC_READONLY) == 0
3746 && (((last_hdr->lma + last_size - 1)
3747 & ~(maxpagesize - 1))
3748 != (hdr->lma & ~(maxpagesize - 1))))
3750 /* We don't want to put a writable section in a read only
3751 segment, unless they are on the same page in memory
3752 anyhow. We already know that the last section does not
3753 bring us past the current section on the page, so the
3754 only case in which the new section is not on the same
3755 page as the previous section is when the previous section
3756 ends precisely on a page boundary. */
3761 /* Otherwise, we can use the same segment. */
3762 new_segment = FALSE;
3765 /* Allow interested parties a chance to override our decision. */
3766 if (last_hdr && info->callbacks->override_segment_assignment)
3767 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3771 if ((hdr->flags & SEC_READONLY) == 0)
3774 /* .tbss sections effectively have zero size. */
3775 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3776 != SEC_THREAD_LOCAL)
3777 last_size = hdr->size;
3783 /* We need a new program segment. We must create a new program
3784 header holding all the sections from phdr_index until hdr. */
3786 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3793 if ((hdr->flags & SEC_READONLY) == 0)
3799 /* .tbss sections effectively have zero size. */
3800 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3801 last_size = hdr->size;
3805 phdr_in_segment = FALSE;
3808 /* Create a final PT_LOAD program segment. */
3809 if (last_hdr != NULL)
3811 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3819 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3822 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3829 /* For each batch of consecutive loadable .note sections,
3830 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3831 because if we link together nonloadable .note sections and
3832 loadable .note sections, we will generate two .note sections
3833 in the output file. FIXME: Using names for section types is
3835 for (s = abfd->sections; s != NULL; s = s->next)
3837 if ((s->flags & SEC_LOAD) != 0
3838 && CONST_STRNEQ (s->name, ".note"))
3842 amt = sizeof (struct elf_segment_map);
3843 if (s->alignment_power == 2)
3844 for (s2 = s; s2->next != NULL; s2 = s2->next)
3846 if (s2->next->alignment_power == 2
3847 && (s2->next->flags & SEC_LOAD) != 0
3848 && CONST_STRNEQ (s2->next->name, ".note")
3849 && align_power (s2->vma + s2->size, 2)
3855 amt += (count - 1) * sizeof (asection *);
3856 m = bfd_zalloc (abfd, amt);
3860 m->p_type = PT_NOTE;
3864 m->sections[m->count - count--] = s;
3865 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3868 m->sections[m->count - 1] = s;
3869 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3873 if (s->flags & SEC_THREAD_LOCAL)
3881 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3886 amt = sizeof (struct elf_segment_map);
3887 amt += (tls_count - 1) * sizeof (asection *);
3888 m = bfd_zalloc (abfd, amt);
3893 m->count = tls_count;
3894 /* Mandated PF_R. */
3896 m->p_flags_valid = 1;
3897 for (i = 0; i < tls_count; ++i)
3899 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3900 m->sections[i] = first_tls;
3901 first_tls = first_tls->next;
3908 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3910 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3911 if (eh_frame_hdr != NULL
3912 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3914 amt = sizeof (struct elf_segment_map);
3915 m = bfd_zalloc (abfd, amt);
3919 m->p_type = PT_GNU_EH_FRAME;
3921 m->sections[0] = eh_frame_hdr->output_section;
3927 if (elf_tdata (abfd)->stack_flags)
3929 amt = sizeof (struct elf_segment_map);
3930 m = bfd_zalloc (abfd, amt);
3934 m->p_type = PT_GNU_STACK;
3935 m->p_flags = elf_tdata (abfd)->stack_flags;
3936 m->p_flags_valid = 1;
3944 for (m = mfirst; m != NULL; m = m->next)
3946 if (m->p_type == PT_LOAD)
3948 asection *last = m->sections[m->count - 1];
3949 bfd_vma vaddr = m->sections[0]->vma;
3950 bfd_vma filesz = last->vma - vaddr + last->size;
3952 if (vaddr < info->relro_end
3953 && vaddr >= info->relro_start
3954 && (vaddr + filesz) >= info->relro_end)
3959 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3962 amt = sizeof (struct elf_segment_map);
3963 m = bfd_zalloc (abfd, amt);
3967 m->p_type = PT_GNU_RELRO;
3969 m->p_flags_valid = 1;
3977 elf_tdata (abfd)->segment_map = mfirst;
3980 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
3983 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3985 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
3990 if (sections != NULL)
3995 /* Sort sections by address. */
3998 elf_sort_sections (const void *arg1, const void *arg2)
4000 const asection *sec1 = *(const asection **) arg1;
4001 const asection *sec2 = *(const asection **) arg2;
4002 bfd_size_type size1, size2;
4004 /* Sort by LMA first, since this is the address used to
4005 place the section into a segment. */
4006 if (sec1->lma < sec2->lma)
4008 else if (sec1->lma > sec2->lma)
4011 /* Then sort by VMA. Normally the LMA and the VMA will be
4012 the same, and this will do nothing. */
4013 if (sec1->vma < sec2->vma)
4015 else if (sec1->vma > sec2->vma)
4018 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4020 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4026 /* If the indicies are the same, do not return 0
4027 here, but continue to try the next comparison. */
4028 if (sec1->target_index - sec2->target_index != 0)
4029 return sec1->target_index - sec2->target_index;
4034 else if (TOEND (sec2))
4039 /* Sort by size, to put zero sized sections
4040 before others at the same address. */
4042 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4043 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4050 return sec1->target_index - sec2->target_index;
4053 /* Ian Lance Taylor writes:
4055 We shouldn't be using % with a negative signed number. That's just
4056 not good. We have to make sure either that the number is not
4057 negative, or that the number has an unsigned type. When the types
4058 are all the same size they wind up as unsigned. When file_ptr is a
4059 larger signed type, the arithmetic winds up as signed long long,
4062 What we're trying to say here is something like ``increase OFF by
4063 the least amount that will cause it to be equal to the VMA modulo
4065 /* In other words, something like:
4067 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4068 off_offset = off % bed->maxpagesize;
4069 if (vma_offset < off_offset)
4070 adjustment = vma_offset + bed->maxpagesize - off_offset;
4072 adjustment = vma_offset - off_offset;
4074 which can can be collapsed into the expression below. */
4077 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4079 return ((vma - off) % maxpagesize);
4083 print_segment_map (const struct elf_segment_map *m)
4086 const char *pt = get_segment_type (m->p_type);
4091 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4092 sprintf (buf, "LOPROC+%7.7x",
4093 (unsigned int) (m->p_type - PT_LOPROC));
4094 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4095 sprintf (buf, "LOOS+%7.7x",
4096 (unsigned int) (m->p_type - PT_LOOS));
4098 snprintf (buf, sizeof (buf), "%8.8x",
4099 (unsigned int) m->p_type);
4102 fprintf (stderr, "%s:", pt);
4103 for (j = 0; j < m->count; j++)
4104 fprintf (stderr, " %s", m->sections [j]->name);
4108 /* Assign file positions to the sections based on the mapping from
4109 sections to segments. This function also sets up some fields in
4113 assign_file_positions_for_load_sections (bfd *abfd,
4114 struct bfd_link_info *link_info)
4116 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4117 struct elf_segment_map *m;
4118 Elf_Internal_Phdr *phdrs;
4119 Elf_Internal_Phdr *p;
4121 bfd_size_type maxpagesize;
4125 if (link_info == NULL
4126 && !elf_modify_segment_map (abfd, link_info, FALSE))
4130 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4133 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4134 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4135 elf_elfheader (abfd)->e_phnum = alloc;
4137 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4138 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4140 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4141 >= alloc * bed->s->sizeof_phdr);
4145 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4149 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4150 elf_tdata (abfd)->phdr = phdrs;
4155 if ((abfd->flags & D_PAGED) != 0)
4156 maxpagesize = bed->maxpagesize;
4158 off = bed->s->sizeof_ehdr;
4159 off += alloc * bed->s->sizeof_phdr;
4161 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4163 m = m->next, p++, j++)
4167 bfd_boolean no_contents;
4169 /* If elf_segment_map is not from map_sections_to_segments, the
4170 sections may not be correctly ordered. NOTE: sorting should
4171 not be done to the PT_NOTE section of a corefile, which may
4172 contain several pseudo-sections artificially created by bfd.
4173 Sorting these pseudo-sections breaks things badly. */
4175 && !(elf_elfheader (abfd)->e_type == ET_CORE
4176 && m->p_type == PT_NOTE))
4177 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4180 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4181 number of sections with contents contributing to both p_filesz
4182 and p_memsz, followed by a number of sections with no contents
4183 that just contribute to p_memsz. In this loop, OFF tracks next
4184 available file offset for PT_LOAD and PT_NOTE segments. */
4185 p->p_type = m->p_type;
4186 p->p_flags = m->p_flags;
4191 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4193 if (m->p_paddr_valid)
4194 p->p_paddr = m->p_paddr;
4195 else if (m->count == 0)
4198 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4200 if (p->p_type == PT_LOAD
4201 && (abfd->flags & D_PAGED) != 0)
4203 /* p_align in demand paged PT_LOAD segments effectively stores
4204 the maximum page size. When copying an executable with
4205 objcopy, we set m->p_align from the input file. Use this
4206 value for maxpagesize rather than bed->maxpagesize, which
4207 may be different. Note that we use maxpagesize for PT_TLS
4208 segment alignment later in this function, so we are relying
4209 on at least one PT_LOAD segment appearing before a PT_TLS
4211 if (m->p_align_valid)
4212 maxpagesize = m->p_align;
4214 p->p_align = maxpagesize;
4216 else if (m->p_align_valid)
4217 p->p_align = m->p_align;
4218 else if (m->count == 0)
4219 p->p_align = 1 << bed->s->log_file_align;
4223 no_contents = FALSE;
4225 if (p->p_type == PT_LOAD
4228 bfd_size_type align;
4229 unsigned int align_power = 0;
4231 if (m->p_align_valid)
4235 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4237 unsigned int secalign;
4239 secalign = bfd_get_section_alignment (abfd, *secpp);
4240 if (secalign > align_power)
4241 align_power = secalign;
4243 align = (bfd_size_type) 1 << align_power;
4244 if (align < maxpagesize)
4245 align = maxpagesize;
4248 for (i = 0; i < m->count; i++)
4249 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4250 /* If we aren't making room for this section, then
4251 it must be SHT_NOBITS regardless of what we've
4252 set via struct bfd_elf_special_section. */
4253 elf_section_type (m->sections[i]) = SHT_NOBITS;
4255 /* Find out whether this segment contains any loadable
4256 sections. If the first section isn't loadable, the same
4257 holds for any other sections. */
4259 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4261 /* If a segment starts with .tbss, we need to look
4262 at the next section to decide whether the segment
4263 has any loadable sections. */
4264 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4272 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4276 /* We shouldn't need to align the segment on disk since
4277 the segment doesn't need file space, but the gABI
4278 arguably requires the alignment and glibc ld.so
4279 checks it. So to comply with the alignment
4280 requirement but not waste file space, we adjust
4281 p_offset for just this segment. (OFF_ADJUST is
4282 subtracted from OFF later.) This may put p_offset
4283 past the end of file, but that shouldn't matter. */
4288 /* Make sure the .dynamic section is the first section in the
4289 PT_DYNAMIC segment. */
4290 else if (p->p_type == PT_DYNAMIC
4292 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4295 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4297 bfd_set_error (bfd_error_bad_value);
4300 /* Set the note section type to SHT_NOTE. */
4301 else if (p->p_type == PT_NOTE)
4302 for (i = 0; i < m->count; i++)
4303 elf_section_type (m->sections[i]) = SHT_NOTE;
4309 if (m->includes_filehdr)
4311 if (!m->p_flags_valid)
4313 p->p_filesz = bed->s->sizeof_ehdr;
4314 p->p_memsz = bed->s->sizeof_ehdr;
4317 BFD_ASSERT (p->p_type == PT_LOAD);
4319 if (p->p_vaddr < (bfd_vma) off)
4321 (*_bfd_error_handler)
4322 (_("%B: Not enough room for program headers, try linking with -N"),
4324 bfd_set_error (bfd_error_bad_value);
4329 if (!m->p_paddr_valid)
4334 if (m->includes_phdrs)
4336 if (!m->p_flags_valid)
4339 if (!m->includes_filehdr)
4341 p->p_offset = bed->s->sizeof_ehdr;
4345 BFD_ASSERT (p->p_type == PT_LOAD);
4346 p->p_vaddr -= off - p->p_offset;
4347 if (!m->p_paddr_valid)
4348 p->p_paddr -= off - p->p_offset;
4352 p->p_filesz += alloc * bed->s->sizeof_phdr;
4353 p->p_memsz += alloc * bed->s->sizeof_phdr;
4356 if (p->p_type == PT_LOAD
4357 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4359 if (!m->includes_filehdr && !m->includes_phdrs)
4365 adjust = off - (p->p_offset + p->p_filesz);
4367 p->p_filesz += adjust;
4368 p->p_memsz += adjust;
4372 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4373 maps. Set filepos for sections in PT_LOAD segments, and in
4374 core files, for sections in PT_NOTE segments.
4375 assign_file_positions_for_non_load_sections will set filepos
4376 for other sections and update p_filesz for other segments. */
4377 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4380 bfd_size_type align;
4381 Elf_Internal_Shdr *this_hdr;
4384 this_hdr = &elf_section_data (sec)->this_hdr;
4385 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4387 if ((p->p_type == PT_LOAD
4388 || p->p_type == PT_TLS)
4389 && (this_hdr->sh_type != SHT_NOBITS
4390 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4391 && ((this_hdr->sh_flags & SHF_TLS) == 0
4392 || p->p_type == PT_TLS))))
4394 bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
4398 (*_bfd_error_handler)
4399 (_("%B: section %A vma 0x%lx overlaps previous sections"),
4400 abfd, sec, (unsigned long) sec->vma);
4403 p->p_memsz += adjust;
4405 if (this_hdr->sh_type != SHT_NOBITS)
4408 p->p_filesz += adjust;
4412 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4414 /* The section at i == 0 is the one that actually contains
4418 this_hdr->sh_offset = sec->filepos = off;
4419 off += this_hdr->sh_size;
4420 p->p_filesz = this_hdr->sh_size;
4426 /* The rest are fake sections that shouldn't be written. */
4435 if (p->p_type == PT_LOAD)
4437 this_hdr->sh_offset = sec->filepos = off;
4438 if (this_hdr->sh_type != SHT_NOBITS)
4439 off += this_hdr->sh_size;
4442 if (this_hdr->sh_type != SHT_NOBITS)
4444 p->p_filesz += this_hdr->sh_size;
4445 /* A load section without SHF_ALLOC is something like
4446 a note section in a PT_NOTE segment. These take
4447 file space but are not loaded into memory. */
4448 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4449 p->p_memsz += this_hdr->sh_size;
4451 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4453 if (p->p_type == PT_TLS)
4454 p->p_memsz += this_hdr->sh_size;
4456 /* .tbss is special. It doesn't contribute to p_memsz of
4458 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4459 p->p_memsz += this_hdr->sh_size;
4462 if (align > p->p_align
4463 && !m->p_align_valid
4464 && (p->p_type != PT_LOAD
4465 || (abfd->flags & D_PAGED) == 0))
4469 if (!m->p_flags_valid)
4472 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4474 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4480 /* Check that all sections are in a PT_LOAD segment.
4481 Don't check funky gdb generated core files. */
4482 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4483 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4485 Elf_Internal_Shdr *this_hdr;
4489 this_hdr = &(elf_section_data(sec)->this_hdr);
4490 if (this_hdr->sh_size != 0
4491 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4493 (*_bfd_error_handler)
4494 (_("%B: section `%A' can't be allocated in segment %d"),
4496 print_segment_map (m);
4497 bfd_set_error (bfd_error_bad_value);
4503 elf_tdata (abfd)->next_file_pos = off;
4507 /* Assign file positions for the other sections. */
4510 assign_file_positions_for_non_load_sections (bfd *abfd,
4511 struct bfd_link_info *link_info)
4513 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4514 Elf_Internal_Shdr **i_shdrpp;
4515 Elf_Internal_Shdr **hdrpp;
4516 Elf_Internal_Phdr *phdrs;
4517 Elf_Internal_Phdr *p;
4518 struct elf_segment_map *m;
4519 bfd_vma filehdr_vaddr, filehdr_paddr;
4520 bfd_vma phdrs_vaddr, phdrs_paddr;
4522 unsigned int num_sec;
4526 i_shdrpp = elf_elfsections (abfd);
4527 num_sec = elf_numsections (abfd);
4528 off = elf_tdata (abfd)->next_file_pos;
4529 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4531 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4532 Elf_Internal_Shdr *hdr;
4535 if (hdr->bfd_section != NULL
4536 && (hdr->bfd_section->filepos != 0
4537 || (hdr->sh_type == SHT_NOBITS
4538 && hdr->contents == NULL)))
4539 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4540 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4542 if (hdr->sh_size != 0)
4543 ((*_bfd_error_handler)
4544 (_("%B: warning: allocated section `%s' not in segment"),
4546 (hdr->bfd_section == NULL
4548 : hdr->bfd_section->name)));
4549 /* We don't need to page align empty sections. */
4550 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4551 off += vma_page_aligned_bias (hdr->sh_addr, off,
4554 off += vma_page_aligned_bias (hdr->sh_addr, off,
4556 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4559 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4560 && hdr->bfd_section == NULL)
4561 || hdr == i_shdrpp[tdata->symtab_section]
4562 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4563 || hdr == i_shdrpp[tdata->strtab_section])
4564 hdr->sh_offset = -1;
4566 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4569 /* Now that we have set the section file positions, we can set up
4570 the file positions for the non PT_LOAD segments. */
4574 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4576 phdrs = elf_tdata (abfd)->phdr;
4577 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4582 if (p->p_type != PT_LOAD)
4585 if (m->includes_filehdr)
4587 filehdr_vaddr = p->p_vaddr;
4588 filehdr_paddr = p->p_paddr;
4590 if (m->includes_phdrs)
4592 phdrs_vaddr = p->p_vaddr;
4593 phdrs_paddr = p->p_paddr;
4594 if (m->includes_filehdr)
4596 phdrs_vaddr += bed->s->sizeof_ehdr;
4597 phdrs_paddr += bed->s->sizeof_ehdr;
4602 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4608 if (p->p_type != PT_LOAD
4609 && (p->p_type != PT_NOTE
4610 || bfd_get_format (abfd) != bfd_core))
4612 Elf_Internal_Shdr *hdr;
4615 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4617 sect = m->sections[m->count - 1];
4618 hdr = &elf_section_data (sect)->this_hdr;
4619 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4620 if (hdr->sh_type != SHT_NOBITS)
4621 p->p_filesz += hdr->sh_size;
4623 if (p->p_type == PT_GNU_RELRO)
4625 /* When we get here, we are copying executable
4626 or shared library. But we need to use the same
4628 Elf_Internal_Phdr *lp;
4630 for (lp = phdrs; lp < phdrs + count; ++lp)
4632 if (lp->p_type == PT_LOAD
4633 && lp->p_paddr == p->p_paddr)
4637 if (lp < phdrs + count)
4639 /* We should use p_size if it is valid since it
4640 may contain the first few bytes of the next
4641 SEC_ALLOC section. */
4642 if (m->p_size_valid)
4643 p->p_filesz = m->p_size;
4646 p->p_vaddr = lp->p_vaddr;
4647 p->p_offset = lp->p_offset;
4648 p->p_memsz = p->p_filesz;
4655 p->p_offset = m->sections[0]->filepos;
4660 if (m->includes_filehdr)
4662 p->p_vaddr = filehdr_vaddr;
4663 if (! m->p_paddr_valid)
4664 p->p_paddr = filehdr_paddr;
4666 else if (m->includes_phdrs)
4668 p->p_vaddr = phdrs_vaddr;
4669 if (! m->p_paddr_valid)
4670 p->p_paddr = phdrs_paddr;
4672 else if (p->p_type == PT_GNU_RELRO)
4674 Elf_Internal_Phdr *lp;
4676 for (lp = phdrs; lp < phdrs + count; ++lp)
4678 if (lp->p_type == PT_LOAD
4679 && lp->p_vaddr <= link_info->relro_end
4680 && lp->p_vaddr >= link_info->relro_start
4681 && (lp->p_vaddr + lp->p_filesz
4682 >= link_info->relro_end))
4686 if (lp < phdrs + count
4687 && link_info->relro_end > lp->p_vaddr)
4689 p->p_vaddr = lp->p_vaddr;
4690 p->p_paddr = lp->p_paddr;
4691 p->p_offset = lp->p_offset;
4692 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4693 p->p_memsz = p->p_filesz;
4695 p->p_flags = (lp->p_flags & ~PF_W);
4699 memset (p, 0, sizeof *p);
4700 p->p_type = PT_NULL;
4706 elf_tdata (abfd)->next_file_pos = off;
4711 /* Work out the file positions of all the sections. This is called by
4712 _bfd_elf_compute_section_file_positions. All the section sizes and
4713 VMAs must be known before this is called.
4715 Reloc sections come in two flavours: Those processed specially as
4716 "side-channel" data attached to a section to which they apply, and
4717 those that bfd doesn't process as relocations. The latter sort are
4718 stored in a normal bfd section by bfd_section_from_shdr. We don't
4719 consider the former sort here, unless they form part of the loadable
4720 image. Reloc sections not assigned here will be handled later by
4721 assign_file_positions_for_relocs.
4723 We also don't set the positions of the .symtab and .strtab here. */
4726 assign_file_positions_except_relocs (bfd *abfd,
4727 struct bfd_link_info *link_info)
4729 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4730 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4732 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4734 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4735 && bfd_get_format (abfd) != bfd_core)
4737 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4738 unsigned int num_sec = elf_numsections (abfd);
4739 Elf_Internal_Shdr **hdrpp;
4742 /* Start after the ELF header. */
4743 off = i_ehdrp->e_ehsize;
4745 /* We are not creating an executable, which means that we are
4746 not creating a program header, and that the actual order of
4747 the sections in the file is unimportant. */
4748 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4750 Elf_Internal_Shdr *hdr;
4753 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4754 && hdr->bfd_section == NULL)
4755 || i == tdata->symtab_section
4756 || i == tdata->symtab_shndx_section
4757 || i == tdata->strtab_section)
4759 hdr->sh_offset = -1;
4762 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4769 /* Assign file positions for the loaded sections based on the
4770 assignment of sections to segments. */
4771 if (!assign_file_positions_for_load_sections (abfd, link_info))
4774 /* And for non-load sections. */
4775 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4778 if (bed->elf_backend_modify_program_headers != NULL)
4780 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4784 /* Write out the program headers. */
4785 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4786 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4787 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4790 off = tdata->next_file_pos;
4793 /* Place the section headers. */
4794 off = align_file_position (off, 1 << bed->s->log_file_align);
4795 i_ehdrp->e_shoff = off;
4796 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4798 tdata->next_file_pos = off;
4804 prep_headers (bfd *abfd)
4806 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4807 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4808 struct elf_strtab_hash *shstrtab;
4809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4811 i_ehdrp = elf_elfheader (abfd);
4813 shstrtab = _bfd_elf_strtab_init ();
4814 if (shstrtab == NULL)
4817 elf_shstrtab (abfd) = shstrtab;
4819 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4820 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4821 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4822 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4824 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4825 i_ehdrp->e_ident[EI_DATA] =
4826 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4827 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4829 if ((abfd->flags & DYNAMIC) != 0)
4830 i_ehdrp->e_type = ET_DYN;
4831 else if ((abfd->flags & EXEC_P) != 0)
4832 i_ehdrp->e_type = ET_EXEC;
4833 else if (bfd_get_format (abfd) == bfd_core)
4834 i_ehdrp->e_type = ET_CORE;
4836 i_ehdrp->e_type = ET_REL;
4838 switch (bfd_get_arch (abfd))
4840 case bfd_arch_unknown:
4841 i_ehdrp->e_machine = EM_NONE;
4844 /* There used to be a long list of cases here, each one setting
4845 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4846 in the corresponding bfd definition. To avoid duplication,
4847 the switch was removed. Machines that need special handling
4848 can generally do it in elf_backend_final_write_processing(),
4849 unless they need the information earlier than the final write.
4850 Such need can generally be supplied by replacing the tests for
4851 e_machine with the conditions used to determine it. */
4853 i_ehdrp->e_machine = bed->elf_machine_code;
4856 i_ehdrp->e_version = bed->s->ev_current;
4857 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4859 /* No program header, for now. */
4860 i_ehdrp->e_phoff = 0;
4861 i_ehdrp->e_phentsize = 0;
4862 i_ehdrp->e_phnum = 0;
4864 /* Each bfd section is section header entry. */
4865 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4866 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4868 /* If we're building an executable, we'll need a program header table. */
4869 if (abfd->flags & EXEC_P)
4870 /* It all happens later. */
4874 i_ehdrp->e_phentsize = 0;
4876 i_ehdrp->e_phoff = 0;
4879 elf_tdata (abfd)->symtab_hdr.sh_name =
4880 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4881 elf_tdata (abfd)->strtab_hdr.sh_name =
4882 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4883 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4884 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4885 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4886 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4887 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4893 /* Assign file positions for all the reloc sections which are not part
4894 of the loadable file image. */
4897 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4900 unsigned int i, num_sec;
4901 Elf_Internal_Shdr **shdrpp;
4903 off = elf_tdata (abfd)->next_file_pos;
4905 num_sec = elf_numsections (abfd);
4906 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4908 Elf_Internal_Shdr *shdrp;
4911 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4912 && shdrp->sh_offset == -1)
4913 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4916 elf_tdata (abfd)->next_file_pos = off;
4920 _bfd_elf_write_object_contents (bfd *abfd)
4922 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4923 Elf_Internal_Ehdr *i_ehdrp;
4924 Elf_Internal_Shdr **i_shdrp;
4926 unsigned int count, num_sec;
4928 if (! abfd->output_has_begun
4929 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4932 i_shdrp = elf_elfsections (abfd);
4933 i_ehdrp = elf_elfheader (abfd);
4936 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4940 _bfd_elf_assign_file_positions_for_relocs (abfd);
4942 /* After writing the headers, we need to write the sections too... */
4943 num_sec = elf_numsections (abfd);
4944 for (count = 1; count < num_sec; count++)
4946 if (bed->elf_backend_section_processing)
4947 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4948 if (i_shdrp[count]->contents)
4950 bfd_size_type amt = i_shdrp[count]->sh_size;
4952 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4953 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4958 /* Write out the section header names. */
4959 if (elf_shstrtab (abfd) != NULL
4960 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4961 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4964 if (bed->elf_backend_final_write_processing)
4965 (*bed->elf_backend_final_write_processing) (abfd,
4966 elf_tdata (abfd)->linker);
4968 if (!bed->s->write_shdrs_and_ehdr (abfd))
4971 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4972 if (elf_tdata (abfd)->after_write_object_contents)
4973 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4979 _bfd_elf_write_corefile_contents (bfd *abfd)
4981 /* Hopefully this can be done just like an object file. */
4982 return _bfd_elf_write_object_contents (abfd);
4985 /* Given a section, search the header to find them. */
4988 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4990 const struct elf_backend_data *bed;
4993 if (elf_section_data (asect) != NULL
4994 && elf_section_data (asect)->this_idx != 0)
4995 return elf_section_data (asect)->this_idx;
4997 if (bfd_is_abs_section (asect))
4999 else if (bfd_is_com_section (asect))
5001 else if (bfd_is_und_section (asect))
5006 bed = get_elf_backend_data (abfd);
5007 if (bed->elf_backend_section_from_bfd_section)
5011 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5015 if (index == SHN_BAD)
5016 bfd_set_error (bfd_error_nonrepresentable_section);
5021 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5025 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5027 asymbol *asym_ptr = *asym_ptr_ptr;
5029 flagword flags = asym_ptr->flags;
5031 /* When gas creates relocations against local labels, it creates its
5032 own symbol for the section, but does put the symbol into the
5033 symbol chain, so udata is 0. When the linker is generating
5034 relocatable output, this section symbol may be for one of the
5035 input sections rather than the output section. */
5036 if (asym_ptr->udata.i == 0
5037 && (flags & BSF_SECTION_SYM)
5038 && asym_ptr->section)
5043 sec = asym_ptr->section;
5044 if (sec->owner != abfd && sec->output_section != NULL)
5045 sec = sec->output_section;
5046 if (sec->owner == abfd
5047 && (indx = sec->index) < elf_num_section_syms (abfd)
5048 && elf_section_syms (abfd)[indx] != NULL)
5049 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5052 idx = asym_ptr->udata.i;
5056 /* This case can occur when using --strip-symbol on a symbol
5057 which is used in a relocation entry. */
5058 (*_bfd_error_handler)
5059 (_("%B: symbol `%s' required but not present"),
5060 abfd, bfd_asymbol_name (asym_ptr));
5061 bfd_set_error (bfd_error_no_symbols);
5068 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5069 (long) asym_ptr, asym_ptr->name, idx, flags,
5070 elf_symbol_flags (flags));
5078 /* Rewrite program header information. */
5081 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5083 Elf_Internal_Ehdr *iehdr;
5084 struct elf_segment_map *map;
5085 struct elf_segment_map *map_first;
5086 struct elf_segment_map **pointer_to_map;
5087 Elf_Internal_Phdr *segment;
5090 unsigned int num_segments;
5091 bfd_boolean phdr_included = FALSE;
5092 bfd_boolean p_paddr_valid;
5093 bfd_vma maxpagesize;
5094 struct elf_segment_map *phdr_adjust_seg = NULL;
5095 unsigned int phdr_adjust_num = 0;
5096 const struct elf_backend_data *bed;
5098 bed = get_elf_backend_data (ibfd);
5099 iehdr = elf_elfheader (ibfd);
5102 pointer_to_map = &map_first;
5104 num_segments = elf_elfheader (ibfd)->e_phnum;
5105 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5107 /* Returns the end address of the segment + 1. */
5108 #define SEGMENT_END(segment, start) \
5109 (start + (segment->p_memsz > segment->p_filesz \
5110 ? segment->p_memsz : segment->p_filesz))
5112 #define SECTION_SIZE(section, segment) \
5113 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5114 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5115 ? section->size : 0)
5117 /* Returns TRUE if the given section is contained within
5118 the given segment. VMA addresses are compared. */
5119 #define IS_CONTAINED_BY_VMA(section, segment) \
5120 (section->vma >= segment->p_vaddr \
5121 && (section->vma + SECTION_SIZE (section, segment) \
5122 <= (SEGMENT_END (segment, segment->p_vaddr))))
5124 /* Returns TRUE if the given section is contained within
5125 the given segment. LMA addresses are compared. */
5126 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5127 (section->lma >= base \
5128 && (section->lma + SECTION_SIZE (section, segment) \
5129 <= SEGMENT_END (segment, base)))
5131 /* Handle PT_NOTE segment. */
5132 #define IS_NOTE(p, s) \
5133 (p->p_type == PT_NOTE \
5134 && elf_section_type (s) == SHT_NOTE \
5135 && (bfd_vma) s->filepos >= p->p_offset \
5136 && ((bfd_vma) s->filepos + s->size \
5137 <= p->p_offset + p->p_filesz))
5139 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5141 #define IS_COREFILE_NOTE(p, s) \
5143 && bfd_get_format (ibfd) == bfd_core \
5147 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5148 linker, which generates a PT_INTERP section with p_vaddr and
5149 p_memsz set to 0. */
5150 #define IS_SOLARIS_PT_INTERP(p, s) \
5152 && p->p_paddr == 0 \
5153 && p->p_memsz == 0 \
5154 && p->p_filesz > 0 \
5155 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5157 && (bfd_vma) s->filepos >= p->p_offset \
5158 && ((bfd_vma) s->filepos + s->size \
5159 <= p->p_offset + p->p_filesz))
5161 /* Decide if the given section should be included in the given segment.
5162 A section will be included if:
5163 1. It is within the address space of the segment -- we use the LMA
5164 if that is set for the segment and the VMA otherwise,
5165 2. It is an allocated section or a NOTE section in a PT_NOTE
5167 3. There is an output section associated with it,
5168 4. The section has not already been allocated to a previous segment.
5169 5. PT_GNU_STACK segments do not include any sections.
5170 6. PT_TLS segment includes only SHF_TLS sections.
5171 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5172 8. PT_DYNAMIC should not contain empty sections at the beginning
5173 (with the possible exception of .dynamic). */
5174 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5175 ((((segment->p_paddr \
5176 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5177 : IS_CONTAINED_BY_VMA (section, segment)) \
5178 && (section->flags & SEC_ALLOC) != 0) \
5179 || IS_NOTE (segment, section)) \
5180 && segment->p_type != PT_GNU_STACK \
5181 && (segment->p_type != PT_TLS \
5182 || (section->flags & SEC_THREAD_LOCAL)) \
5183 && (segment->p_type == PT_LOAD \
5184 || segment->p_type == PT_TLS \
5185 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5186 && (segment->p_type != PT_DYNAMIC \
5187 || SECTION_SIZE (section, segment) > 0 \
5188 || (segment->p_paddr \
5189 ? segment->p_paddr != section->lma \
5190 : segment->p_vaddr != section->vma) \
5191 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5193 && !section->segment_mark)
5195 /* If the output section of a section in the input segment is NULL,
5196 it is removed from the corresponding output segment. */
5197 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5198 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5199 && section->output_section != NULL)
5201 /* Returns TRUE iff seg1 starts after the end of seg2. */
5202 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5203 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5205 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5206 their VMA address ranges and their LMA address ranges overlap.
5207 It is possible to have overlapping VMA ranges without overlapping LMA
5208 ranges. RedBoot images for example can have both .data and .bss mapped
5209 to the same VMA range, but with the .data section mapped to a different
5211 #define SEGMENT_OVERLAPS(seg1, seg2) \
5212 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5213 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5214 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5215 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5217 /* Initialise the segment mark field. */
5218 for (section = ibfd->sections; section != NULL; section = section->next)
5219 section->segment_mark = FALSE;
5221 /* The Solaris linker creates program headers in which all the
5222 p_paddr fields are zero. When we try to objcopy or strip such a
5223 file, we get confused. Check for this case, and if we find it
5224 don't set the p_paddr_valid fields. */
5225 p_paddr_valid = FALSE;
5226 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5229 if (segment->p_paddr != 0)
5231 p_paddr_valid = TRUE;
5235 /* Scan through the segments specified in the program header
5236 of the input BFD. For this first scan we look for overlaps
5237 in the loadable segments. These can be created by weird
5238 parameters to objcopy. Also, fix some solaris weirdness. */
5239 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5244 Elf_Internal_Phdr *segment2;
5246 if (segment->p_type == PT_INTERP)
5247 for (section = ibfd->sections; section; section = section->next)
5248 if (IS_SOLARIS_PT_INTERP (segment, section))
5250 /* Mininal change so that the normal section to segment
5251 assignment code will work. */
5252 segment->p_vaddr = section->vma;
5256 if (segment->p_type != PT_LOAD)
5258 /* Remove PT_GNU_RELRO segment. */
5259 if (segment->p_type == PT_GNU_RELRO)
5260 segment->p_type = PT_NULL;
5264 /* Determine if this segment overlaps any previous segments. */
5265 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5267 bfd_signed_vma extra_length;
5269 if (segment2->p_type != PT_LOAD
5270 || !SEGMENT_OVERLAPS (segment, segment2))
5273 /* Merge the two segments together. */
5274 if (segment2->p_vaddr < segment->p_vaddr)
5276 /* Extend SEGMENT2 to include SEGMENT and then delete
5278 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5279 - SEGMENT_END (segment2, segment2->p_vaddr));
5281 if (extra_length > 0)
5283 segment2->p_memsz += extra_length;
5284 segment2->p_filesz += extra_length;
5287 segment->p_type = PT_NULL;
5289 /* Since we have deleted P we must restart the outer loop. */
5291 segment = elf_tdata (ibfd)->phdr;
5296 /* Extend SEGMENT to include SEGMENT2 and then delete
5298 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5299 - SEGMENT_END (segment, segment->p_vaddr));
5301 if (extra_length > 0)
5303 segment->p_memsz += extra_length;
5304 segment->p_filesz += extra_length;
5307 segment2->p_type = PT_NULL;
5312 /* The second scan attempts to assign sections to segments. */
5313 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5317 unsigned int section_count;
5318 asection **sections;
5319 asection *output_section;
5321 bfd_vma matching_lma;
5322 bfd_vma suggested_lma;
5325 asection *first_section;
5326 bfd_boolean first_matching_lma;
5327 bfd_boolean first_suggested_lma;
5329 if (segment->p_type == PT_NULL)
5332 first_section = NULL;
5333 /* Compute how many sections might be placed into this segment. */
5334 for (section = ibfd->sections, section_count = 0;
5336 section = section->next)
5338 /* Find the first section in the input segment, which may be
5339 removed from the corresponding output segment. */
5340 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5342 if (first_section == NULL)
5343 first_section = section;
5344 if (section->output_section != NULL)
5349 /* Allocate a segment map big enough to contain
5350 all of the sections we have selected. */
5351 amt = sizeof (struct elf_segment_map);
5352 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5353 map = bfd_zalloc (obfd, amt);
5357 /* Initialise the fields of the segment map. Default to
5358 using the physical address of the segment in the input BFD. */
5360 map->p_type = segment->p_type;
5361 map->p_flags = segment->p_flags;
5362 map->p_flags_valid = 1;
5364 /* If the first section in the input segment is removed, there is
5365 no need to preserve segment physical address in the corresponding
5367 if (!first_section || first_section->output_section != NULL)
5369 map->p_paddr = segment->p_paddr;
5370 map->p_paddr_valid = p_paddr_valid;
5373 /* Determine if this segment contains the ELF file header
5374 and if it contains the program headers themselves. */
5375 map->includes_filehdr = (segment->p_offset == 0
5376 && segment->p_filesz >= iehdr->e_ehsize);
5377 map->includes_phdrs = 0;
5379 if (!phdr_included || segment->p_type != PT_LOAD)
5381 map->includes_phdrs =
5382 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5383 && (segment->p_offset + segment->p_filesz
5384 >= ((bfd_vma) iehdr->e_phoff
5385 + iehdr->e_phnum * iehdr->e_phentsize)));
5387 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5388 phdr_included = TRUE;
5391 if (section_count == 0)
5393 /* Special segments, such as the PT_PHDR segment, may contain
5394 no sections, but ordinary, loadable segments should contain
5395 something. They are allowed by the ELF spec however, so only
5396 a warning is produced. */
5397 if (segment->p_type == PT_LOAD)
5398 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5399 " detected, is this intentional ?\n"),
5403 *pointer_to_map = map;
5404 pointer_to_map = &map->next;
5409 /* Now scan the sections in the input BFD again and attempt
5410 to add their corresponding output sections to the segment map.
5411 The problem here is how to handle an output section which has
5412 been moved (ie had its LMA changed). There are four possibilities:
5414 1. None of the sections have been moved.
5415 In this case we can continue to use the segment LMA from the
5418 2. All of the sections have been moved by the same amount.
5419 In this case we can change the segment's LMA to match the LMA
5420 of the first section.
5422 3. Some of the sections have been moved, others have not.
5423 In this case those sections which have not been moved can be
5424 placed in the current segment which will have to have its size,
5425 and possibly its LMA changed, and a new segment or segments will
5426 have to be created to contain the other sections.
5428 4. The sections have been moved, but not by the same amount.
5429 In this case we can change the segment's LMA to match the LMA
5430 of the first section and we will have to create a new segment
5431 or segments to contain the other sections.
5433 In order to save time, we allocate an array to hold the section
5434 pointers that we are interested in. As these sections get assigned
5435 to a segment, they are removed from this array. */
5437 sections = bfd_malloc2 (section_count, sizeof (asection *));
5438 if (sections == NULL)
5441 /* Step One: Scan for segment vs section LMA conflicts.
5442 Also add the sections to the section array allocated above.
5443 Also add the sections to the current segment. In the common
5444 case, where the sections have not been moved, this means that
5445 we have completely filled the segment, and there is nothing
5450 first_matching_lma = TRUE;
5451 first_suggested_lma = TRUE;
5453 for (section = ibfd->sections;
5455 section = section->next)
5456 if (section == first_section)
5459 for (j = 0; section != NULL; section = section->next)
5461 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5463 output_section = section->output_section;
5465 sections[j++] = section;
5467 /* The Solaris native linker always sets p_paddr to 0.
5468 We try to catch that case here, and set it to the
5469 correct value. Note - some backends require that
5470 p_paddr be left as zero. */
5472 && segment->p_vaddr != 0
5473 && !bed->want_p_paddr_set_to_zero
5475 && output_section->lma != 0
5476 && output_section->vma == (segment->p_vaddr
5477 + (map->includes_filehdr
5480 + (map->includes_phdrs
5482 * iehdr->e_phentsize)
5484 map->p_paddr = segment->p_vaddr;
5486 /* Match up the physical address of the segment with the
5487 LMA address of the output section. */
5488 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5489 || IS_COREFILE_NOTE (segment, section)
5490 || (bed->want_p_paddr_set_to_zero
5491 && IS_CONTAINED_BY_VMA (output_section, segment)))
5493 if (first_matching_lma || output_section->lma < matching_lma)
5495 matching_lma = output_section->lma;
5496 first_matching_lma = FALSE;
5499 /* We assume that if the section fits within the segment
5500 then it does not overlap any other section within that
5502 map->sections[isec++] = output_section;
5504 else if (first_suggested_lma)
5506 suggested_lma = output_section->lma;
5507 first_suggested_lma = FALSE;
5510 if (j == section_count)
5515 BFD_ASSERT (j == section_count);
5517 /* Step Two: Adjust the physical address of the current segment,
5519 if (isec == section_count)
5521 /* All of the sections fitted within the segment as currently
5522 specified. This is the default case. Add the segment to
5523 the list of built segments and carry on to process the next
5524 program header in the input BFD. */
5525 map->count = section_count;
5526 *pointer_to_map = map;
5527 pointer_to_map = &map->next;
5530 && !bed->want_p_paddr_set_to_zero
5531 && matching_lma != map->p_paddr
5532 && !map->includes_filehdr
5533 && !map->includes_phdrs)
5534 /* There is some padding before the first section in the
5535 segment. So, we must account for that in the output
5537 map->p_vaddr_offset = matching_lma - map->p_paddr;
5544 if (!first_matching_lma)
5546 /* At least one section fits inside the current segment.
5547 Keep it, but modify its physical address to match the
5548 LMA of the first section that fitted. */
5549 map->p_paddr = matching_lma;
5553 /* None of the sections fitted inside the current segment.
5554 Change the current segment's physical address to match
5555 the LMA of the first section. */
5556 map->p_paddr = suggested_lma;
5559 /* Offset the segment physical address from the lma
5560 to allow for space taken up by elf headers. */
5561 if (map->includes_filehdr)
5563 if (map->p_paddr >= iehdr->e_ehsize)
5564 map->p_paddr -= iehdr->e_ehsize;
5567 map->includes_filehdr = FALSE;
5568 map->includes_phdrs = FALSE;
5572 if (map->includes_phdrs)
5574 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5576 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5578 /* iehdr->e_phnum is just an estimate of the number
5579 of program headers that we will need. Make a note
5580 here of the number we used and the segment we chose
5581 to hold these headers, so that we can adjust the
5582 offset when we know the correct value. */
5583 phdr_adjust_num = iehdr->e_phnum;
5584 phdr_adjust_seg = map;
5587 map->includes_phdrs = FALSE;
5591 /* Step Three: Loop over the sections again, this time assigning
5592 those that fit to the current segment and removing them from the
5593 sections array; but making sure not to leave large gaps. Once all
5594 possible sections have been assigned to the current segment it is
5595 added to the list of built segments and if sections still remain
5596 to be assigned, a new segment is constructed before repeating
5603 first_suggested_lma = TRUE;
5605 /* Fill the current segment with sections that fit. */
5606 for (j = 0; j < section_count; j++)
5608 section = sections[j];
5610 if (section == NULL)
5613 output_section = section->output_section;
5615 BFD_ASSERT (output_section != NULL);
5617 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5618 || IS_COREFILE_NOTE (segment, section))
5620 if (map->count == 0)
5622 /* If the first section in a segment does not start at
5623 the beginning of the segment, then something is
5625 if (output_section->lma
5627 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5628 + (map->includes_phdrs
5629 ? iehdr->e_phnum * iehdr->e_phentsize
5637 prev_sec = map->sections[map->count - 1];
5639 /* If the gap between the end of the previous section
5640 and the start of this section is more than
5641 maxpagesize then we need to start a new segment. */
5642 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5644 < BFD_ALIGN (output_section->lma, maxpagesize))
5645 || (prev_sec->lma + prev_sec->size
5646 > output_section->lma))
5648 if (first_suggested_lma)
5650 suggested_lma = output_section->lma;
5651 first_suggested_lma = FALSE;
5658 map->sections[map->count++] = output_section;
5661 section->segment_mark = TRUE;
5663 else if (first_suggested_lma)
5665 suggested_lma = output_section->lma;
5666 first_suggested_lma = FALSE;
5670 BFD_ASSERT (map->count > 0);
5672 /* Add the current segment to the list of built segments. */
5673 *pointer_to_map = map;
5674 pointer_to_map = &map->next;
5676 if (isec < section_count)
5678 /* We still have not allocated all of the sections to
5679 segments. Create a new segment here, initialise it
5680 and carry on looping. */
5681 amt = sizeof (struct elf_segment_map);
5682 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5683 map = bfd_alloc (obfd, amt);
5690 /* Initialise the fields of the segment map. Set the physical
5691 physical address to the LMA of the first section that has
5692 not yet been assigned. */
5694 map->p_type = segment->p_type;
5695 map->p_flags = segment->p_flags;
5696 map->p_flags_valid = 1;
5697 map->p_paddr = suggested_lma;
5698 map->p_paddr_valid = p_paddr_valid;
5699 map->includes_filehdr = 0;
5700 map->includes_phdrs = 0;
5703 while (isec < section_count);
5708 elf_tdata (obfd)->segment_map = map_first;
5710 /* If we had to estimate the number of program headers that were
5711 going to be needed, then check our estimate now and adjust
5712 the offset if necessary. */
5713 if (phdr_adjust_seg != NULL)
5717 for (count = 0, map = map_first; map != NULL; map = map->next)
5720 if (count > phdr_adjust_num)
5721 phdr_adjust_seg->p_paddr
5722 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5727 #undef IS_CONTAINED_BY_VMA
5728 #undef IS_CONTAINED_BY_LMA
5730 #undef IS_COREFILE_NOTE
5731 #undef IS_SOLARIS_PT_INTERP
5732 #undef IS_SECTION_IN_INPUT_SEGMENT
5733 #undef INCLUDE_SECTION_IN_SEGMENT
5734 #undef SEGMENT_AFTER_SEGMENT
5735 #undef SEGMENT_OVERLAPS
5739 /* Copy ELF program header information. */
5742 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5744 Elf_Internal_Ehdr *iehdr;
5745 struct elf_segment_map *map;
5746 struct elf_segment_map *map_first;
5747 struct elf_segment_map **pointer_to_map;
5748 Elf_Internal_Phdr *segment;
5750 unsigned int num_segments;
5751 bfd_boolean phdr_included = FALSE;
5752 bfd_boolean p_paddr_valid;
5754 iehdr = elf_elfheader (ibfd);
5757 pointer_to_map = &map_first;
5759 /* If all the segment p_paddr fields are zero, don't set
5760 map->p_paddr_valid. */
5761 p_paddr_valid = FALSE;
5762 num_segments = elf_elfheader (ibfd)->e_phnum;
5763 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5766 if (segment->p_paddr != 0)
5768 p_paddr_valid = TRUE;
5772 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5777 unsigned int section_count;
5779 Elf_Internal_Shdr *this_hdr;
5780 asection *first_section = NULL;
5781 asection *lowest_section = NULL;
5783 /* Compute how many sections are in this segment. */
5784 for (section = ibfd->sections, section_count = 0;
5786 section = section->next)
5788 this_hdr = &(elf_section_data(section)->this_hdr);
5789 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5792 first_section = lowest_section = section;
5793 if (section->lma < lowest_section->lma)
5794 lowest_section = section;
5799 /* Allocate a segment map big enough to contain
5800 all of the sections we have selected. */
5801 amt = sizeof (struct elf_segment_map);
5802 if (section_count != 0)
5803 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5804 map = bfd_zalloc (obfd, amt);
5808 /* Initialize the fields of the output segment map with the
5811 map->p_type = segment->p_type;
5812 map->p_flags = segment->p_flags;
5813 map->p_flags_valid = 1;
5814 map->p_paddr = segment->p_paddr;
5815 map->p_paddr_valid = p_paddr_valid;
5816 map->p_align = segment->p_align;
5817 map->p_align_valid = 1;
5818 map->p_vaddr_offset = 0;
5820 if (map->p_type == PT_GNU_RELRO
5821 && segment->p_filesz == segment->p_memsz)
5823 /* The PT_GNU_RELRO segment may contain the first a few
5824 bytes in the .got.plt section even if the whole .got.plt
5825 section isn't in the PT_GNU_RELRO segment. We won't
5826 change the size of the PT_GNU_RELRO segment. */
5827 map->p_size = segment->p_filesz;
5828 map->p_size_valid = 1;
5831 /* Determine if this segment contains the ELF file header
5832 and if it contains the program headers themselves. */
5833 map->includes_filehdr = (segment->p_offset == 0
5834 && segment->p_filesz >= iehdr->e_ehsize);
5836 map->includes_phdrs = 0;
5837 if (! phdr_included || segment->p_type != PT_LOAD)
5839 map->includes_phdrs =
5840 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5841 && (segment->p_offset + segment->p_filesz
5842 >= ((bfd_vma) iehdr->e_phoff
5843 + iehdr->e_phnum * iehdr->e_phentsize)));
5845 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5846 phdr_included = TRUE;
5849 if (!map->includes_phdrs
5850 && !map->includes_filehdr
5851 && map->p_paddr_valid)
5852 /* There is some other padding before the first section. */
5853 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5854 - segment->p_paddr);
5856 if (section_count != 0)
5858 unsigned int isec = 0;
5860 for (section = first_section;
5862 section = section->next)
5864 this_hdr = &(elf_section_data(section)->this_hdr);
5865 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5867 map->sections[isec++] = section->output_section;
5868 if (isec == section_count)
5874 map->count = section_count;
5875 *pointer_to_map = map;
5876 pointer_to_map = &map->next;
5879 elf_tdata (obfd)->segment_map = map_first;
5883 /* Copy private BFD data. This copies or rewrites ELF program header
5887 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5889 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5890 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5893 if (elf_tdata (ibfd)->phdr == NULL)
5896 if (ibfd->xvec == obfd->xvec)
5898 /* Check to see if any sections in the input BFD
5899 covered by ELF program header have changed. */
5900 Elf_Internal_Phdr *segment;
5901 asection *section, *osec;
5902 unsigned int i, num_segments;
5903 Elf_Internal_Shdr *this_hdr;
5904 const struct elf_backend_data *bed;
5906 bed = get_elf_backend_data (ibfd);
5908 /* Regenerate the segment map if p_paddr is set to 0. */
5909 if (bed->want_p_paddr_set_to_zero)
5912 /* Initialize the segment mark field. */
5913 for (section = obfd->sections; section != NULL;
5914 section = section->next)
5915 section->segment_mark = FALSE;
5917 num_segments = elf_elfheader (ibfd)->e_phnum;
5918 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5922 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5923 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5924 which severly confuses things, so always regenerate the segment
5925 map in this case. */
5926 if (segment->p_paddr == 0
5927 && segment->p_memsz == 0
5928 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5931 for (section = ibfd->sections;
5932 section != NULL; section = section->next)
5934 /* We mark the output section so that we know it comes
5935 from the input BFD. */
5936 osec = section->output_section;
5938 osec->segment_mark = TRUE;
5940 /* Check if this section is covered by the segment. */
5941 this_hdr = &(elf_section_data(section)->this_hdr);
5942 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5944 /* FIXME: Check if its output section is changed or
5945 removed. What else do we need to check? */
5947 || section->flags != osec->flags
5948 || section->lma != osec->lma
5949 || section->vma != osec->vma
5950 || section->size != osec->size
5951 || section->rawsize != osec->rawsize
5952 || section->alignment_power != osec->alignment_power)
5958 /* Check to see if any output section do not come from the
5960 for (section = obfd->sections; section != NULL;
5961 section = section->next)
5963 if (section->segment_mark == FALSE)
5966 section->segment_mark = FALSE;
5969 return copy_elf_program_header (ibfd, obfd);
5973 return rewrite_elf_program_header (ibfd, obfd);
5976 /* Initialize private output section information from input section. */
5979 _bfd_elf_init_private_section_data (bfd *ibfd,
5983 struct bfd_link_info *link_info)
5986 Elf_Internal_Shdr *ihdr, *ohdr;
5987 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5989 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5990 || obfd->xvec->flavour != bfd_target_elf_flavour)
5993 /* Don't copy the output ELF section type from input if the
5994 output BFD section flags have been set to something different.
5995 elf_fake_sections will set ELF section type based on BFD
5997 if (elf_section_type (osec) == SHT_NULL
5998 && (osec->flags == isec->flags || !osec->flags))
5999 elf_section_type (osec) = elf_section_type (isec);
6001 /* FIXME: Is this correct for all OS/PROC specific flags? */
6002 elf_section_flags (osec) |= (elf_section_flags (isec)
6003 & (SHF_MASKOS | SHF_MASKPROC));
6005 /* Set things up for objcopy and relocatable link. The output
6006 SHT_GROUP section will have its elf_next_in_group pointing back
6007 to the input group members. Ignore linker created group section.
6008 See elfNN_ia64_object_p in elfxx-ia64.c. */
6011 if (elf_sec_group (isec) == NULL
6012 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6014 if (elf_section_flags (isec) & SHF_GROUP)
6015 elf_section_flags (osec) |= SHF_GROUP;
6016 elf_next_in_group (osec) = elf_next_in_group (isec);
6017 elf_group_name (osec) = elf_group_name (isec);
6021 ihdr = &elf_section_data (isec)->this_hdr;
6023 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6024 don't use the output section of the linked-to section since it
6025 may be NULL at this point. */
6026 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6028 ohdr = &elf_section_data (osec)->this_hdr;
6029 ohdr->sh_flags |= SHF_LINK_ORDER;
6030 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6033 osec->use_rela_p = isec->use_rela_p;
6038 /* Copy private section information. This copies over the entsize
6039 field, and sometimes the info field. */
6042 _bfd_elf_copy_private_section_data (bfd *ibfd,
6047 Elf_Internal_Shdr *ihdr, *ohdr;
6049 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6050 || obfd->xvec->flavour != bfd_target_elf_flavour)
6053 ihdr = &elf_section_data (isec)->this_hdr;
6054 ohdr = &elf_section_data (osec)->this_hdr;
6056 ohdr->sh_entsize = ihdr->sh_entsize;
6058 if (ihdr->sh_type == SHT_SYMTAB
6059 || ihdr->sh_type == SHT_DYNSYM
6060 || ihdr->sh_type == SHT_GNU_verneed
6061 || ihdr->sh_type == SHT_GNU_verdef)
6062 ohdr->sh_info = ihdr->sh_info;
6064 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6068 /* Copy private header information. */
6071 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6075 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6076 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6079 /* Copy over private BFD data if it has not already been copied.
6080 This must be done here, rather than in the copy_private_bfd_data
6081 entry point, because the latter is called after the section
6082 contents have been set, which means that the program headers have
6083 already been worked out. */
6084 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6086 if (! copy_private_bfd_data (ibfd, obfd))
6090 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6091 but this might be wrong if we deleted the group section. */
6092 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6093 if (elf_section_type (isec) == SHT_GROUP
6094 && isec->output_section == NULL)
6096 asection *first = elf_next_in_group (isec);
6097 asection *s = first;
6100 if (s->output_section != NULL)
6102 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6103 elf_group_name (s->output_section) = NULL;
6105 s = elf_next_in_group (s);
6114 /* Copy private symbol information. If this symbol is in a section
6115 which we did not map into a BFD section, try to map the section
6116 index correctly. We use special macro definitions for the mapped
6117 section indices; these definitions are interpreted by the
6118 swap_out_syms function. */
6120 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6121 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6122 #define MAP_STRTAB (SHN_HIOS + 3)
6123 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6124 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6127 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6132 elf_symbol_type *isym, *osym;
6134 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6135 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6138 isym = elf_symbol_from (ibfd, isymarg);
6139 osym = elf_symbol_from (obfd, osymarg);
6142 && isym->internal_elf_sym.st_shndx != 0
6144 && bfd_is_abs_section (isym->symbol.section))
6148 shndx = isym->internal_elf_sym.st_shndx;
6149 if (shndx == elf_onesymtab (ibfd))
6150 shndx = MAP_ONESYMTAB;
6151 else if (shndx == elf_dynsymtab (ibfd))
6152 shndx = MAP_DYNSYMTAB;
6153 else if (shndx == elf_tdata (ibfd)->strtab_section)
6155 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6156 shndx = MAP_SHSTRTAB;
6157 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6158 shndx = MAP_SYM_SHNDX;
6159 osym->internal_elf_sym.st_shndx = shndx;
6165 /* Swap out the symbols. */
6168 swap_out_syms (bfd *abfd,
6169 struct bfd_strtab_hash **sttp,
6172 const struct elf_backend_data *bed;
6175 struct bfd_strtab_hash *stt;
6176 Elf_Internal_Shdr *symtab_hdr;
6177 Elf_Internal_Shdr *symtab_shndx_hdr;
6178 Elf_Internal_Shdr *symstrtab_hdr;
6179 bfd_byte *outbound_syms;
6180 bfd_byte *outbound_shndx;
6183 bfd_boolean name_local_sections;
6185 if (!elf_map_symbols (abfd))
6188 /* Dump out the symtabs. */
6189 stt = _bfd_elf_stringtab_init ();
6193 bed = get_elf_backend_data (abfd);
6194 symcount = bfd_get_symcount (abfd);
6195 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6196 symtab_hdr->sh_type = SHT_SYMTAB;
6197 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6198 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6199 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6200 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6202 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6203 symstrtab_hdr->sh_type = SHT_STRTAB;
6205 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6206 if (outbound_syms == NULL)
6208 _bfd_stringtab_free (stt);
6211 symtab_hdr->contents = outbound_syms;
6213 outbound_shndx = NULL;
6214 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6215 if (symtab_shndx_hdr->sh_name != 0)
6217 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6218 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6219 sizeof (Elf_External_Sym_Shndx));
6220 if (outbound_shndx == NULL)
6222 _bfd_stringtab_free (stt);
6226 symtab_shndx_hdr->contents = outbound_shndx;
6227 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6228 symtab_shndx_hdr->sh_size = amt;
6229 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6230 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6233 /* Now generate the data (for "contents"). */
6235 /* Fill in zeroth symbol and swap it out. */
6236 Elf_Internal_Sym sym;
6242 sym.st_shndx = SHN_UNDEF;
6243 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6244 outbound_syms += bed->s->sizeof_sym;
6245 if (outbound_shndx != NULL)
6246 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6250 = (bed->elf_backend_name_local_section_symbols
6251 && bed->elf_backend_name_local_section_symbols (abfd));
6253 syms = bfd_get_outsymbols (abfd);
6254 for (idx = 0; idx < symcount; idx++)
6256 Elf_Internal_Sym sym;
6257 bfd_vma value = syms[idx]->value;
6258 elf_symbol_type *type_ptr;
6259 flagword flags = syms[idx]->flags;
6262 if (!name_local_sections
6263 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6265 /* Local section symbols have no name. */
6270 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6273 if (sym.st_name == (unsigned long) -1)
6275 _bfd_stringtab_free (stt);
6280 type_ptr = elf_symbol_from (abfd, syms[idx]);
6282 if ((flags & BSF_SECTION_SYM) == 0
6283 && bfd_is_com_section (syms[idx]->section))
6285 /* ELF common symbols put the alignment into the `value' field,
6286 and the size into the `size' field. This is backwards from
6287 how BFD handles it, so reverse it here. */
6288 sym.st_size = value;
6289 if (type_ptr == NULL
6290 || type_ptr->internal_elf_sym.st_value == 0)
6291 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6293 sym.st_value = type_ptr->internal_elf_sym.st_value;
6294 sym.st_shndx = _bfd_elf_section_from_bfd_section
6295 (abfd, syms[idx]->section);
6299 asection *sec = syms[idx]->section;
6302 if (sec->output_section)
6304 value += sec->output_offset;
6305 sec = sec->output_section;
6308 /* Don't add in the section vma for relocatable output. */
6309 if (! relocatable_p)
6311 sym.st_value = value;
6312 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6314 if (bfd_is_abs_section (sec)
6316 && type_ptr->internal_elf_sym.st_shndx != 0)
6318 /* This symbol is in a real ELF section which we did
6319 not create as a BFD section. Undo the mapping done
6320 by copy_private_symbol_data. */
6321 shndx = type_ptr->internal_elf_sym.st_shndx;
6325 shndx = elf_onesymtab (abfd);
6328 shndx = elf_dynsymtab (abfd);
6331 shndx = elf_tdata (abfd)->strtab_section;
6334 shndx = elf_tdata (abfd)->shstrtab_section;
6337 shndx = elf_tdata (abfd)->symtab_shndx_section;
6345 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6347 if (shndx == SHN_BAD)
6351 /* Writing this would be a hell of a lot easier if
6352 we had some decent documentation on bfd, and
6353 knew what to expect of the library, and what to
6354 demand of applications. For example, it
6355 appears that `objcopy' might not set the
6356 section of a symbol to be a section that is
6357 actually in the output file. */
6358 sec2 = bfd_get_section_by_name (abfd, sec->name);
6361 _bfd_error_handler (_("\
6362 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6363 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6365 bfd_set_error (bfd_error_invalid_operation);
6366 _bfd_stringtab_free (stt);
6370 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6371 BFD_ASSERT (shndx != SHN_BAD);
6375 sym.st_shndx = shndx;
6378 if ((flags & BSF_THREAD_LOCAL) != 0)
6380 else if ((flags & BSF_FUNCTION) != 0)
6382 else if ((flags & BSF_OBJECT) != 0)
6384 else if ((flags & BSF_RELC) != 0)
6386 else if ((flags & BSF_SRELC) != 0)
6391 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6394 /* Processor-specific types. */
6395 if (type_ptr != NULL
6396 && bed->elf_backend_get_symbol_type)
6397 type = ((*bed->elf_backend_get_symbol_type)
6398 (&type_ptr->internal_elf_sym, type));
6400 if (flags & BSF_SECTION_SYM)
6402 if (flags & BSF_GLOBAL)
6403 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6405 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6407 else if (bfd_is_com_section (syms[idx]->section))
6409 #ifdef USE_STT_COMMON
6410 if (type == STT_OBJECT)
6411 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6414 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6417 else if (bfd_is_und_section (syms[idx]->section))
6418 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6422 else if (flags & BSF_FILE)
6423 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6426 int bind = STB_LOCAL;
6428 if (flags & BSF_LOCAL)
6430 else if (flags & BSF_WEAK)
6432 else if (flags & BSF_GLOBAL)
6435 sym.st_info = ELF_ST_INFO (bind, type);
6438 if (type_ptr != NULL)
6439 sym.st_other = type_ptr->internal_elf_sym.st_other;
6443 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6444 outbound_syms += bed->s->sizeof_sym;
6445 if (outbound_shndx != NULL)
6446 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6450 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6451 symstrtab_hdr->sh_type = SHT_STRTAB;
6453 symstrtab_hdr->sh_flags = 0;
6454 symstrtab_hdr->sh_addr = 0;
6455 symstrtab_hdr->sh_entsize = 0;
6456 symstrtab_hdr->sh_link = 0;
6457 symstrtab_hdr->sh_info = 0;
6458 symstrtab_hdr->sh_addralign = 1;
6463 /* Return the number of bytes required to hold the symtab vector.
6465 Note that we base it on the count plus 1, since we will null terminate
6466 the vector allocated based on this size. However, the ELF symbol table
6467 always has a dummy entry as symbol #0, so it ends up even. */
6470 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6474 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6476 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6477 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6479 symtab_size -= sizeof (asymbol *);
6485 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6489 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6491 if (elf_dynsymtab (abfd) == 0)
6493 bfd_set_error (bfd_error_invalid_operation);
6497 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6498 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6500 symtab_size -= sizeof (asymbol *);
6506 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6509 return (asect->reloc_count + 1) * sizeof (arelent *);
6512 /* Canonicalize the relocs. */
6515 _bfd_elf_canonicalize_reloc (bfd *abfd,
6522 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6524 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6527 tblptr = section->relocation;
6528 for (i = 0; i < section->reloc_count; i++)
6529 *relptr++ = tblptr++;
6533 return section->reloc_count;
6537 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6539 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6540 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6543 bfd_get_symcount (abfd) = symcount;
6548 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6549 asymbol **allocation)
6551 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6552 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6555 bfd_get_dynamic_symcount (abfd) = symcount;
6559 /* Return the size required for the dynamic reloc entries. Any loadable
6560 section that was actually installed in the BFD, and has type SHT_REL
6561 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6562 dynamic reloc section. */
6565 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6570 if (elf_dynsymtab (abfd) == 0)
6572 bfd_set_error (bfd_error_invalid_operation);
6576 ret = sizeof (arelent *);
6577 for (s = abfd->sections; s != NULL; s = s->next)
6578 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6579 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6580 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6581 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6582 * sizeof (arelent *));
6587 /* Canonicalize the dynamic relocation entries. Note that we return the
6588 dynamic relocations as a single block, although they are actually
6589 associated with particular sections; the interface, which was
6590 designed for SunOS style shared libraries, expects that there is only
6591 one set of dynamic relocs. Any loadable section that was actually
6592 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6593 dynamic symbol table, is considered to be a dynamic reloc section. */
6596 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6600 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6604 if (elf_dynsymtab (abfd) == 0)
6606 bfd_set_error (bfd_error_invalid_operation);
6610 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6612 for (s = abfd->sections; s != NULL; s = s->next)
6614 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6615 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6616 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6621 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6623 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6625 for (i = 0; i < count; i++)
6636 /* Read in the version information. */
6639 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6641 bfd_byte *contents = NULL;
6642 unsigned int freeidx = 0;
6644 if (elf_dynverref (abfd) != 0)
6646 Elf_Internal_Shdr *hdr;
6647 Elf_External_Verneed *everneed;
6648 Elf_Internal_Verneed *iverneed;
6650 bfd_byte *contents_end;
6652 hdr = &elf_tdata (abfd)->dynverref_hdr;
6654 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6655 sizeof (Elf_Internal_Verneed));
6656 if (elf_tdata (abfd)->verref == NULL)
6659 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6661 contents = bfd_malloc (hdr->sh_size);
6662 if (contents == NULL)
6664 error_return_verref:
6665 elf_tdata (abfd)->verref = NULL;
6666 elf_tdata (abfd)->cverrefs = 0;
6669 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6670 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6671 goto error_return_verref;
6673 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6674 goto error_return_verref;
6676 BFD_ASSERT (sizeof (Elf_External_Verneed)
6677 == sizeof (Elf_External_Vernaux));
6678 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6679 everneed = (Elf_External_Verneed *) contents;
6680 iverneed = elf_tdata (abfd)->verref;
6681 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6683 Elf_External_Vernaux *evernaux;
6684 Elf_Internal_Vernaux *ivernaux;
6687 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6689 iverneed->vn_bfd = abfd;
6691 iverneed->vn_filename =
6692 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6694 if (iverneed->vn_filename == NULL)
6695 goto error_return_verref;
6697 if (iverneed->vn_cnt == 0)
6698 iverneed->vn_auxptr = NULL;
6701 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6702 sizeof (Elf_Internal_Vernaux));
6703 if (iverneed->vn_auxptr == NULL)
6704 goto error_return_verref;
6707 if (iverneed->vn_aux
6708 > (size_t) (contents_end - (bfd_byte *) everneed))
6709 goto error_return_verref;
6711 evernaux = ((Elf_External_Vernaux *)
6712 ((bfd_byte *) everneed + iverneed->vn_aux));
6713 ivernaux = iverneed->vn_auxptr;
6714 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6716 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6718 ivernaux->vna_nodename =
6719 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6720 ivernaux->vna_name);
6721 if (ivernaux->vna_nodename == NULL)
6722 goto error_return_verref;
6724 if (j + 1 < iverneed->vn_cnt)
6725 ivernaux->vna_nextptr = ivernaux + 1;
6727 ivernaux->vna_nextptr = NULL;
6729 if (ivernaux->vna_next
6730 > (size_t) (contents_end - (bfd_byte *) evernaux))
6731 goto error_return_verref;
6733 evernaux = ((Elf_External_Vernaux *)
6734 ((bfd_byte *) evernaux + ivernaux->vna_next));
6736 if (ivernaux->vna_other > freeidx)
6737 freeidx = ivernaux->vna_other;
6740 if (i + 1 < hdr->sh_info)
6741 iverneed->vn_nextref = iverneed + 1;
6743 iverneed->vn_nextref = NULL;
6745 if (iverneed->vn_next
6746 > (size_t) (contents_end - (bfd_byte *) everneed))
6747 goto error_return_verref;
6749 everneed = ((Elf_External_Verneed *)
6750 ((bfd_byte *) everneed + iverneed->vn_next));
6757 if (elf_dynverdef (abfd) != 0)
6759 Elf_Internal_Shdr *hdr;
6760 Elf_External_Verdef *everdef;
6761 Elf_Internal_Verdef *iverdef;
6762 Elf_Internal_Verdef *iverdefarr;
6763 Elf_Internal_Verdef iverdefmem;
6765 unsigned int maxidx;
6766 bfd_byte *contents_end_def, *contents_end_aux;
6768 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6770 contents = bfd_malloc (hdr->sh_size);
6771 if (contents == NULL)
6773 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6774 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6777 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6780 BFD_ASSERT (sizeof (Elf_External_Verdef)
6781 >= sizeof (Elf_External_Verdaux));
6782 contents_end_def = contents + hdr->sh_size
6783 - sizeof (Elf_External_Verdef);
6784 contents_end_aux = contents + hdr->sh_size
6785 - sizeof (Elf_External_Verdaux);
6787 /* We know the number of entries in the section but not the maximum
6788 index. Therefore we have to run through all entries and find
6790 everdef = (Elf_External_Verdef *) contents;
6792 for (i = 0; i < hdr->sh_info; ++i)
6794 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6796 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6797 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6799 if (iverdefmem.vd_next
6800 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6803 everdef = ((Elf_External_Verdef *)
6804 ((bfd_byte *) everdef + iverdefmem.vd_next));
6807 if (default_imported_symver)
6809 if (freeidx > maxidx)
6814 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6815 sizeof (Elf_Internal_Verdef));
6816 if (elf_tdata (abfd)->verdef == NULL)
6819 elf_tdata (abfd)->cverdefs = maxidx;
6821 everdef = (Elf_External_Verdef *) contents;
6822 iverdefarr = elf_tdata (abfd)->verdef;
6823 for (i = 0; i < hdr->sh_info; i++)
6825 Elf_External_Verdaux *everdaux;
6826 Elf_Internal_Verdaux *iverdaux;
6829 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6831 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6833 error_return_verdef:
6834 elf_tdata (abfd)->verdef = NULL;
6835 elf_tdata (abfd)->cverdefs = 0;
6839 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6840 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6842 iverdef->vd_bfd = abfd;
6844 if (iverdef->vd_cnt == 0)
6845 iverdef->vd_auxptr = NULL;
6848 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6849 sizeof (Elf_Internal_Verdaux));
6850 if (iverdef->vd_auxptr == NULL)
6851 goto error_return_verdef;
6855 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6856 goto error_return_verdef;
6858 everdaux = ((Elf_External_Verdaux *)
6859 ((bfd_byte *) everdef + iverdef->vd_aux));
6860 iverdaux = iverdef->vd_auxptr;
6861 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6863 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6865 iverdaux->vda_nodename =
6866 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6867 iverdaux->vda_name);
6868 if (iverdaux->vda_nodename == NULL)
6869 goto error_return_verdef;
6871 if (j + 1 < iverdef->vd_cnt)
6872 iverdaux->vda_nextptr = iverdaux + 1;
6874 iverdaux->vda_nextptr = NULL;
6876 if (iverdaux->vda_next
6877 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6878 goto error_return_verdef;
6880 everdaux = ((Elf_External_Verdaux *)
6881 ((bfd_byte *) everdaux + iverdaux->vda_next));
6884 if (iverdef->vd_cnt)
6885 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6887 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6888 iverdef->vd_nextdef = iverdef + 1;
6890 iverdef->vd_nextdef = NULL;
6892 everdef = ((Elf_External_Verdef *)
6893 ((bfd_byte *) everdef + iverdef->vd_next));
6899 else if (default_imported_symver)
6906 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6907 sizeof (Elf_Internal_Verdef));
6908 if (elf_tdata (abfd)->verdef == NULL)
6911 elf_tdata (abfd)->cverdefs = freeidx;
6914 /* Create a default version based on the soname. */
6915 if (default_imported_symver)
6917 Elf_Internal_Verdef *iverdef;
6918 Elf_Internal_Verdaux *iverdaux;
6920 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6922 iverdef->vd_version = VER_DEF_CURRENT;
6923 iverdef->vd_flags = 0;
6924 iverdef->vd_ndx = freeidx;
6925 iverdef->vd_cnt = 1;
6927 iverdef->vd_bfd = abfd;
6929 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6930 if (iverdef->vd_nodename == NULL)
6931 goto error_return_verdef;
6932 iverdef->vd_nextdef = NULL;
6933 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6934 if (iverdef->vd_auxptr == NULL)
6935 goto error_return_verdef;
6937 iverdaux = iverdef->vd_auxptr;
6938 iverdaux->vda_nodename = iverdef->vd_nodename;
6939 iverdaux->vda_nextptr = NULL;
6945 if (contents != NULL)
6951 _bfd_elf_make_empty_symbol (bfd *abfd)
6953 elf_symbol_type *newsym;
6954 bfd_size_type amt = sizeof (elf_symbol_type);
6956 newsym = bfd_zalloc (abfd, amt);
6961 newsym->symbol.the_bfd = abfd;
6962 return &newsym->symbol;
6967 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6971 bfd_symbol_info (symbol, ret);
6974 /* Return whether a symbol name implies a local symbol. Most targets
6975 use this function for the is_local_label_name entry point, but some
6979 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6982 /* Normal local symbols start with ``.L''. */
6983 if (name[0] == '.' && name[1] == 'L')
6986 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6987 DWARF debugging symbols starting with ``..''. */
6988 if (name[0] == '.' && name[1] == '.')
6991 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6992 emitting DWARF debugging output. I suspect this is actually a
6993 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6994 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6995 underscore to be emitted on some ELF targets). For ease of use,
6996 we treat such symbols as local. */
6997 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7004 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7005 asymbol *symbol ATTRIBUTE_UNUSED)
7012 _bfd_elf_set_arch_mach (bfd *abfd,
7013 enum bfd_architecture arch,
7014 unsigned long machine)
7016 /* If this isn't the right architecture for this backend, and this
7017 isn't the generic backend, fail. */
7018 if (arch != get_elf_backend_data (abfd)->arch
7019 && arch != bfd_arch_unknown
7020 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7023 return bfd_default_set_arch_mach (abfd, arch, machine);
7026 /* Find the function to a particular section and offset,
7027 for error reporting. */
7030 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7034 const char **filename_ptr,
7035 const char **functionname_ptr)
7037 const char *filename;
7038 asymbol *func, *file;
7041 /* ??? Given multiple file symbols, it is impossible to reliably
7042 choose the right file name for global symbols. File symbols are
7043 local symbols, and thus all file symbols must sort before any
7044 global symbols. The ELF spec may be interpreted to say that a
7045 file symbol must sort before other local symbols, but currently
7046 ld -r doesn't do this. So, for ld -r output, it is possible to
7047 make a better choice of file name for local symbols by ignoring
7048 file symbols appearing after a given local symbol. */
7049 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7055 state = nothing_seen;
7057 for (p = symbols; *p != NULL; p++)
7061 q = (elf_symbol_type *) *p;
7063 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7069 if (state == symbol_seen)
7070 state = file_after_symbol_seen;
7074 if (bfd_get_section (&q->symbol) == section
7075 && q->symbol.value >= low_func
7076 && q->symbol.value <= offset)
7078 func = (asymbol *) q;
7079 low_func = q->symbol.value;
7082 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7083 || state != file_after_symbol_seen))
7084 filename = bfd_asymbol_name (file);
7088 if (state == nothing_seen)
7089 state = symbol_seen;
7096 *filename_ptr = filename;
7097 if (functionname_ptr)
7098 *functionname_ptr = bfd_asymbol_name (func);
7103 /* Find the nearest line to a particular section and offset,
7104 for error reporting. */
7107 _bfd_elf_find_nearest_line (bfd *abfd,
7111 const char **filename_ptr,
7112 const char **functionname_ptr,
7113 unsigned int *line_ptr)
7117 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7118 filename_ptr, functionname_ptr,
7121 if (!*functionname_ptr)
7122 elf_find_function (abfd, section, symbols, offset,
7123 *filename_ptr ? NULL : filename_ptr,
7129 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7130 filename_ptr, functionname_ptr,
7132 &elf_tdata (abfd)->dwarf2_find_line_info))
7134 if (!*functionname_ptr)
7135 elf_find_function (abfd, section, symbols, offset,
7136 *filename_ptr ? NULL : filename_ptr,
7142 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7143 &found, filename_ptr,
7144 functionname_ptr, line_ptr,
7145 &elf_tdata (abfd)->line_info))
7147 if (found && (*functionname_ptr || *line_ptr))
7150 if (symbols == NULL)
7153 if (! elf_find_function (abfd, section, symbols, offset,
7154 filename_ptr, functionname_ptr))
7161 /* Find the line for a symbol. */
7164 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7165 const char **filename_ptr, unsigned int *line_ptr)
7167 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7168 filename_ptr, line_ptr, 0,
7169 &elf_tdata (abfd)->dwarf2_find_line_info);
7172 /* After a call to bfd_find_nearest_line, successive calls to
7173 bfd_find_inliner_info can be used to get source information about
7174 each level of function inlining that terminated at the address
7175 passed to bfd_find_nearest_line. Currently this is only supported
7176 for DWARF2 with appropriate DWARF3 extensions. */
7179 _bfd_elf_find_inliner_info (bfd *abfd,
7180 const char **filename_ptr,
7181 const char **functionname_ptr,
7182 unsigned int *line_ptr)
7185 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7186 functionname_ptr, line_ptr,
7187 & elf_tdata (abfd)->dwarf2_find_line_info);
7192 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7194 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7195 int ret = bed->s->sizeof_ehdr;
7197 if (!info->relocatable)
7199 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7201 if (phdr_size == (bfd_size_type) -1)
7203 struct elf_segment_map *m;
7206 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7207 phdr_size += bed->s->sizeof_phdr;
7210 phdr_size = get_program_header_size (abfd, info);
7213 elf_tdata (abfd)->program_header_size = phdr_size;
7221 _bfd_elf_set_section_contents (bfd *abfd,
7223 const void *location,
7225 bfd_size_type count)
7227 Elf_Internal_Shdr *hdr;
7230 if (! abfd->output_has_begun
7231 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7234 hdr = &elf_section_data (section)->this_hdr;
7235 pos = hdr->sh_offset + offset;
7236 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7237 || bfd_bwrite (location, count, abfd) != count)
7244 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7245 arelent *cache_ptr ATTRIBUTE_UNUSED,
7246 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7251 /* Try to convert a non-ELF reloc into an ELF one. */
7254 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7256 /* Check whether we really have an ELF howto. */
7258 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7260 bfd_reloc_code_real_type code;
7261 reloc_howto_type *howto;
7263 /* Alien reloc: Try to determine its type to replace it with an
7264 equivalent ELF reloc. */
7266 if (areloc->howto->pc_relative)
7268 switch (areloc->howto->bitsize)
7271 code = BFD_RELOC_8_PCREL;
7274 code = BFD_RELOC_12_PCREL;
7277 code = BFD_RELOC_16_PCREL;
7280 code = BFD_RELOC_24_PCREL;
7283 code = BFD_RELOC_32_PCREL;
7286 code = BFD_RELOC_64_PCREL;
7292 howto = bfd_reloc_type_lookup (abfd, code);
7294 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7296 if (howto->pcrel_offset)
7297 areloc->addend += areloc->address;
7299 areloc->addend -= areloc->address; /* addend is unsigned!! */
7304 switch (areloc->howto->bitsize)
7310 code = BFD_RELOC_14;
7313 code = BFD_RELOC_16;
7316 code = BFD_RELOC_26;
7319 code = BFD_RELOC_32;
7322 code = BFD_RELOC_64;
7328 howto = bfd_reloc_type_lookup (abfd, code);
7332 areloc->howto = howto;
7340 (*_bfd_error_handler)
7341 (_("%B: unsupported relocation type %s"),
7342 abfd, areloc->howto->name);
7343 bfd_set_error (bfd_error_bad_value);
7348 _bfd_elf_close_and_cleanup (bfd *abfd)
7350 if (bfd_get_format (abfd) == bfd_object)
7352 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7353 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7354 _bfd_dwarf2_cleanup_debug_info (abfd);
7357 return _bfd_generic_close_and_cleanup (abfd);
7360 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7361 in the relocation's offset. Thus we cannot allow any sort of sanity
7362 range-checking to interfere. There is nothing else to do in processing
7365 bfd_reloc_status_type
7366 _bfd_elf_rel_vtable_reloc_fn
7367 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7368 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7369 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7370 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7372 return bfd_reloc_ok;
7375 /* Elf core file support. Much of this only works on native
7376 toolchains, since we rely on knowing the
7377 machine-dependent procfs structure in order to pick
7378 out details about the corefile. */
7380 #ifdef HAVE_SYS_PROCFS_H
7381 # include <sys/procfs.h>
7384 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7387 elfcore_make_pid (bfd *abfd)
7389 return ((elf_tdata (abfd)->core_lwpid << 16)
7390 + (elf_tdata (abfd)->core_pid));
7393 /* If there isn't a section called NAME, make one, using
7394 data from SECT. Note, this function will generate a
7395 reference to NAME, so you shouldn't deallocate or
7399 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7403 if (bfd_get_section_by_name (abfd, name) != NULL)
7406 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7410 sect2->size = sect->size;
7411 sect2->filepos = sect->filepos;
7412 sect2->alignment_power = sect->alignment_power;
7416 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7417 actually creates up to two pseudosections:
7418 - For the single-threaded case, a section named NAME, unless
7419 such a section already exists.
7420 - For the multi-threaded case, a section named "NAME/PID", where
7421 PID is elfcore_make_pid (abfd).
7422 Both pseudosections have identical contents. */
7424 _bfd_elfcore_make_pseudosection (bfd *abfd,
7430 char *threaded_name;
7434 /* Build the section name. */
7436 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7437 len = strlen (buf) + 1;
7438 threaded_name = bfd_alloc (abfd, len);
7439 if (threaded_name == NULL)
7441 memcpy (threaded_name, buf, len);
7443 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7448 sect->filepos = filepos;
7449 sect->alignment_power = 2;
7451 return elfcore_maybe_make_sect (abfd, name, sect);
7454 /* prstatus_t exists on:
7456 linux 2.[01] + glibc
7460 #if defined (HAVE_PRSTATUS_T)
7463 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7468 if (note->descsz == sizeof (prstatus_t))
7472 size = sizeof (prstat.pr_reg);
7473 offset = offsetof (prstatus_t, pr_reg);
7474 memcpy (&prstat, note->descdata, sizeof (prstat));
7476 /* Do not overwrite the core signal if it
7477 has already been set by another thread. */
7478 if (elf_tdata (abfd)->core_signal == 0)
7479 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7480 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7482 /* pr_who exists on:
7485 pr_who doesn't exist on:
7488 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7489 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7492 #if defined (HAVE_PRSTATUS32_T)
7493 else if (note->descsz == sizeof (prstatus32_t))
7495 /* 64-bit host, 32-bit corefile */
7496 prstatus32_t prstat;
7498 size = sizeof (prstat.pr_reg);
7499 offset = offsetof (prstatus32_t, pr_reg);
7500 memcpy (&prstat, note->descdata, sizeof (prstat));
7502 /* Do not overwrite the core signal if it
7503 has already been set by another thread. */
7504 if (elf_tdata (abfd)->core_signal == 0)
7505 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7506 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7508 /* pr_who exists on:
7511 pr_who doesn't exist on:
7514 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7515 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7518 #endif /* HAVE_PRSTATUS32_T */
7521 /* Fail - we don't know how to handle any other
7522 note size (ie. data object type). */
7526 /* Make a ".reg/999" section and a ".reg" section. */
7527 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7528 size, note->descpos + offset);
7530 #endif /* defined (HAVE_PRSTATUS_T) */
7532 /* Create a pseudosection containing the exact contents of NOTE. */
7534 elfcore_make_note_pseudosection (bfd *abfd,
7536 Elf_Internal_Note *note)
7538 return _bfd_elfcore_make_pseudosection (abfd, name,
7539 note->descsz, note->descpos);
7542 /* There isn't a consistent prfpregset_t across platforms,
7543 but it doesn't matter, because we don't have to pick this
7544 data structure apart. */
7547 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7549 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7552 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7553 type of NT_PRXFPREG. Just include the whole note's contents
7557 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7559 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7563 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7565 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7569 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7571 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7574 #if defined (HAVE_PRPSINFO_T)
7575 typedef prpsinfo_t elfcore_psinfo_t;
7576 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7577 typedef prpsinfo32_t elfcore_psinfo32_t;
7581 #if defined (HAVE_PSINFO_T)
7582 typedef psinfo_t elfcore_psinfo_t;
7583 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7584 typedef psinfo32_t elfcore_psinfo32_t;
7588 /* return a malloc'ed copy of a string at START which is at
7589 most MAX bytes long, possibly without a terminating '\0'.
7590 the copy will always have a terminating '\0'. */
7593 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7596 char *end = memchr (start, '\0', max);
7604 dups = bfd_alloc (abfd, len + 1);
7608 memcpy (dups, start, len);
7614 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7616 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7618 if (note->descsz == sizeof (elfcore_psinfo_t))
7620 elfcore_psinfo_t psinfo;
7622 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7624 elf_tdata (abfd)->core_program
7625 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7626 sizeof (psinfo.pr_fname));
7628 elf_tdata (abfd)->core_command
7629 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7630 sizeof (psinfo.pr_psargs));
7632 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7633 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7635 /* 64-bit host, 32-bit corefile */
7636 elfcore_psinfo32_t psinfo;
7638 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7640 elf_tdata (abfd)->core_program
7641 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7642 sizeof (psinfo.pr_fname));
7644 elf_tdata (abfd)->core_command
7645 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7646 sizeof (psinfo.pr_psargs));
7652 /* Fail - we don't know how to handle any other
7653 note size (ie. data object type). */
7657 /* Note that for some reason, a spurious space is tacked
7658 onto the end of the args in some (at least one anyway)
7659 implementations, so strip it off if it exists. */
7662 char *command = elf_tdata (abfd)->core_command;
7663 int n = strlen (command);
7665 if (0 < n && command[n - 1] == ' ')
7666 command[n - 1] = '\0';
7671 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7673 #if defined (HAVE_PSTATUS_T)
7675 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7677 if (note->descsz == sizeof (pstatus_t)
7678 #if defined (HAVE_PXSTATUS_T)
7679 || note->descsz == sizeof (pxstatus_t)
7685 memcpy (&pstat, note->descdata, sizeof (pstat));
7687 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7689 #if defined (HAVE_PSTATUS32_T)
7690 else if (note->descsz == sizeof (pstatus32_t))
7692 /* 64-bit host, 32-bit corefile */
7695 memcpy (&pstat, note->descdata, sizeof (pstat));
7697 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7700 /* Could grab some more details from the "representative"
7701 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7702 NT_LWPSTATUS note, presumably. */
7706 #endif /* defined (HAVE_PSTATUS_T) */
7708 #if defined (HAVE_LWPSTATUS_T)
7710 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7712 lwpstatus_t lwpstat;
7718 if (note->descsz != sizeof (lwpstat)
7719 #if defined (HAVE_LWPXSTATUS_T)
7720 && note->descsz != sizeof (lwpxstatus_t)
7725 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7727 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7728 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7730 /* Make a ".reg/999" section. */
7732 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7733 len = strlen (buf) + 1;
7734 name = bfd_alloc (abfd, len);
7737 memcpy (name, buf, len);
7739 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7743 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7744 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7745 sect->filepos = note->descpos
7746 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7749 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7750 sect->size = sizeof (lwpstat.pr_reg);
7751 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7754 sect->alignment_power = 2;
7756 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7759 /* Make a ".reg2/999" section */
7761 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7762 len = strlen (buf) + 1;
7763 name = bfd_alloc (abfd, len);
7766 memcpy (name, buf, len);
7768 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7772 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7773 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7774 sect->filepos = note->descpos
7775 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7778 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7779 sect->size = sizeof (lwpstat.pr_fpreg);
7780 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7783 sect->alignment_power = 2;
7785 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7787 #endif /* defined (HAVE_LWPSTATUS_T) */
7790 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7797 int is_active_thread;
7800 if (note->descsz < 728)
7803 if (! CONST_STRNEQ (note->namedata, "win32"))
7806 type = bfd_get_32 (abfd, note->descdata);
7810 case 1 /* NOTE_INFO_PROCESS */:
7811 /* FIXME: need to add ->core_command. */
7812 /* process_info.pid */
7813 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7814 /* process_info.signal */
7815 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7818 case 2 /* NOTE_INFO_THREAD */:
7819 /* Make a ".reg/999" section. */
7820 /* thread_info.tid */
7821 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7823 len = strlen (buf) + 1;
7824 name = bfd_alloc (abfd, len);
7828 memcpy (name, buf, len);
7830 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7834 /* sizeof (thread_info.thread_context) */
7836 /* offsetof (thread_info.thread_context) */
7837 sect->filepos = note->descpos + 12;
7838 sect->alignment_power = 2;
7840 /* thread_info.is_active_thread */
7841 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7843 if (is_active_thread)
7844 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7848 case 3 /* NOTE_INFO_MODULE */:
7849 /* Make a ".module/xxxxxxxx" section. */
7850 /* module_info.base_address */
7851 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7852 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
7854 len = strlen (buf) + 1;
7855 name = bfd_alloc (abfd, len);
7859 memcpy (name, buf, len);
7861 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7866 sect->size = note->descsz;
7867 sect->filepos = note->descpos;
7868 sect->alignment_power = 2;
7879 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7881 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7889 if (bed->elf_backend_grok_prstatus)
7890 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7892 #if defined (HAVE_PRSTATUS_T)
7893 return elfcore_grok_prstatus (abfd, note);
7898 #if defined (HAVE_PSTATUS_T)
7900 return elfcore_grok_pstatus (abfd, note);
7903 #if defined (HAVE_LWPSTATUS_T)
7905 return elfcore_grok_lwpstatus (abfd, note);
7908 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7909 return elfcore_grok_prfpreg (abfd, note);
7911 case NT_WIN32PSTATUS:
7912 return elfcore_grok_win32pstatus (abfd, note);
7914 case NT_PRXFPREG: /* Linux SSE extension */
7915 if (note->namesz == 6
7916 && strcmp (note->namedata, "LINUX") == 0)
7917 return elfcore_grok_prxfpreg (abfd, note);
7922 if (note->namesz == 6
7923 && strcmp (note->namedata, "LINUX") == 0)
7924 return elfcore_grok_ppc_vmx (abfd, note);
7929 if (note->namesz == 6
7930 && strcmp (note->namedata, "LINUX") == 0)
7931 return elfcore_grok_ppc_vsx (abfd, note);
7937 if (bed->elf_backend_grok_psinfo)
7938 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7940 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7941 return elfcore_grok_psinfo (abfd, note);
7948 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7953 sect->size = note->descsz;
7954 sect->filepos = note->descpos;
7955 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7963 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7965 elf_tdata (abfd)->build_id_size = note->descsz;
7966 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7967 if (elf_tdata (abfd)->build_id == NULL)
7970 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7976 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
7983 case NT_GNU_BUILD_ID:
7984 return elfobj_grok_gnu_build_id (abfd, note);
7989 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7993 cp = strchr (note->namedata, '@');
7996 *lwpidp = atoi(cp + 1);
8003 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8005 /* Signal number at offset 0x08. */
8006 elf_tdata (abfd)->core_signal
8007 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8009 /* Process ID at offset 0x50. */
8010 elf_tdata (abfd)->core_pid
8011 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8013 /* Command name at 0x7c (max 32 bytes, including nul). */
8014 elf_tdata (abfd)->core_command
8015 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8017 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8022 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8026 if (elfcore_netbsd_get_lwpid (note, &lwp))
8027 elf_tdata (abfd)->core_lwpid = lwp;
8029 if (note->type == NT_NETBSDCORE_PROCINFO)
8031 /* NetBSD-specific core "procinfo". Note that we expect to
8032 find this note before any of the others, which is fine,
8033 since the kernel writes this note out first when it
8034 creates a core file. */
8036 return elfcore_grok_netbsd_procinfo (abfd, note);
8039 /* As of Jan 2002 there are no other machine-independent notes
8040 defined for NetBSD core files. If the note type is less
8041 than the start of the machine-dependent note types, we don't
8044 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8048 switch (bfd_get_arch (abfd))
8050 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8051 PT_GETFPREGS == mach+2. */
8053 case bfd_arch_alpha:
8054 case bfd_arch_sparc:
8057 case NT_NETBSDCORE_FIRSTMACH+0:
8058 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8060 case NT_NETBSDCORE_FIRSTMACH+2:
8061 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8067 /* On all other arch's, PT_GETREGS == mach+1 and
8068 PT_GETFPREGS == mach+3. */
8073 case NT_NETBSDCORE_FIRSTMACH+1:
8074 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8076 case NT_NETBSDCORE_FIRSTMACH+3:
8077 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8087 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8089 void *ddata = note->descdata;
8096 /* nto_procfs_status 'pid' field is at offset 0. */
8097 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8099 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8100 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8102 /* nto_procfs_status 'flags' field is at offset 8. */
8103 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8105 /* nto_procfs_status 'what' field is at offset 14. */
8106 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8108 elf_tdata (abfd)->core_signal = sig;
8109 elf_tdata (abfd)->core_lwpid = *tid;
8112 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8113 do not come from signals so we make sure we set the current
8114 thread just in case. */
8115 if (flags & 0x00000080)
8116 elf_tdata (abfd)->core_lwpid = *tid;
8118 /* Make a ".qnx_core_status/%d" section. */
8119 sprintf (buf, ".qnx_core_status/%ld", *tid);
8121 name = bfd_alloc (abfd, strlen (buf) + 1);
8126 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8130 sect->size = note->descsz;
8131 sect->filepos = note->descpos;
8132 sect->alignment_power = 2;
8134 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8138 elfcore_grok_nto_regs (bfd *abfd,
8139 Elf_Internal_Note *note,
8147 /* Make a "(base)/%d" section. */
8148 sprintf (buf, "%s/%ld", base, tid);
8150 name = bfd_alloc (abfd, strlen (buf) + 1);
8155 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8159 sect->size = note->descsz;
8160 sect->filepos = note->descpos;
8161 sect->alignment_power = 2;
8163 /* This is the current thread. */
8164 if (elf_tdata (abfd)->core_lwpid == tid)
8165 return elfcore_maybe_make_sect (abfd, base, sect);
8170 #define BFD_QNT_CORE_INFO 7
8171 #define BFD_QNT_CORE_STATUS 8
8172 #define BFD_QNT_CORE_GREG 9
8173 #define BFD_QNT_CORE_FPREG 10
8176 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8178 /* Every GREG section has a STATUS section before it. Store the
8179 tid from the previous call to pass down to the next gregs
8181 static long tid = 1;
8185 case BFD_QNT_CORE_INFO:
8186 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8187 case BFD_QNT_CORE_STATUS:
8188 return elfcore_grok_nto_status (abfd, note, &tid);
8189 case BFD_QNT_CORE_GREG:
8190 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8191 case BFD_QNT_CORE_FPREG:
8192 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8199 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8205 /* Use note name as section name. */
8207 name = bfd_alloc (abfd, len);
8210 memcpy (name, note->namedata, len);
8211 name[len - 1] = '\0';
8213 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8217 sect->size = note->descsz;
8218 sect->filepos = note->descpos;
8219 sect->alignment_power = 1;
8224 /* Function: elfcore_write_note
8227 buffer to hold note, and current size of buffer
8231 size of data for note
8233 Writes note to end of buffer. ELF64 notes are written exactly as
8234 for ELF32, despite the current (as of 2006) ELF gabi specifying
8235 that they ought to have 8-byte namesz and descsz field, and have
8236 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8239 Pointer to realloc'd buffer, *BUFSIZ updated. */
8242 elfcore_write_note (bfd *abfd,
8250 Elf_External_Note *xnp;
8257 namesz = strlen (name) + 1;
8259 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8261 buf = realloc (buf, *bufsiz + newspace);
8264 dest = buf + *bufsiz;
8265 *bufsiz += newspace;
8266 xnp = (Elf_External_Note *) dest;
8267 H_PUT_32 (abfd, namesz, xnp->namesz);
8268 H_PUT_32 (abfd, size, xnp->descsz);
8269 H_PUT_32 (abfd, type, xnp->type);
8273 memcpy (dest, name, namesz);
8281 memcpy (dest, input, size);
8291 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8293 elfcore_write_prpsinfo (bfd *abfd,
8299 const char *note_name = "CORE";
8300 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8302 if (bed->elf_backend_write_core_note != NULL)
8305 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8306 NT_PRPSINFO, fname, psargs);
8311 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8312 if (bed->s->elfclass == ELFCLASS32)
8314 #if defined (HAVE_PSINFO32_T)
8316 int note_type = NT_PSINFO;
8319 int note_type = NT_PRPSINFO;
8322 memset (&data, 0, sizeof (data));
8323 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8324 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8325 return elfcore_write_note (abfd, buf, bufsiz,
8326 note_name, note_type, &data, sizeof (data));
8331 #if defined (HAVE_PSINFO_T)
8333 int note_type = NT_PSINFO;
8336 int note_type = NT_PRPSINFO;
8339 memset (&data, 0, sizeof (data));
8340 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8341 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8342 return elfcore_write_note (abfd, buf, bufsiz,
8343 note_name, note_type, &data, sizeof (data));
8346 #endif /* PSINFO_T or PRPSINFO_T */
8348 #if defined (HAVE_PRSTATUS_T)
8350 elfcore_write_prstatus (bfd *abfd,
8357 const char *note_name = "CORE";
8358 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8360 if (bed->elf_backend_write_core_note != NULL)
8363 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8365 pid, cursig, gregs);
8370 #if defined (HAVE_PRSTATUS32_T)
8371 if (bed->s->elfclass == ELFCLASS32)
8373 prstatus32_t prstat;
8375 memset (&prstat, 0, sizeof (prstat));
8376 prstat.pr_pid = pid;
8377 prstat.pr_cursig = cursig;
8378 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8379 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8380 NT_PRSTATUS, &prstat, sizeof (prstat));
8387 memset (&prstat, 0, sizeof (prstat));
8388 prstat.pr_pid = pid;
8389 prstat.pr_cursig = cursig;
8390 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8391 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8392 NT_PRSTATUS, &prstat, sizeof (prstat));
8395 #endif /* HAVE_PRSTATUS_T */
8397 #if defined (HAVE_LWPSTATUS_T)
8399 elfcore_write_lwpstatus (bfd *abfd,
8406 lwpstatus_t lwpstat;
8407 const char *note_name = "CORE";
8409 memset (&lwpstat, 0, sizeof (lwpstat));
8410 lwpstat.pr_lwpid = pid >> 16;
8411 lwpstat.pr_cursig = cursig;
8412 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8413 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8414 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8416 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8417 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8419 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8420 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8423 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8424 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8426 #endif /* HAVE_LWPSTATUS_T */
8428 #if defined (HAVE_PSTATUS_T)
8430 elfcore_write_pstatus (bfd *abfd,
8434 int cursig ATTRIBUTE_UNUSED,
8435 const void *gregs ATTRIBUTE_UNUSED)
8437 const char *note_name = "CORE";
8438 #if defined (HAVE_PSTATUS32_T)
8439 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8441 if (bed->s->elfclass == ELFCLASS32)
8445 memset (&pstat, 0, sizeof (pstat));
8446 pstat.pr_pid = pid & 0xffff;
8447 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8448 NT_PSTATUS, &pstat, sizeof (pstat));
8456 memset (&pstat, 0, sizeof (pstat));
8457 pstat.pr_pid = pid & 0xffff;
8458 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8459 NT_PSTATUS, &pstat, sizeof (pstat));
8463 #endif /* HAVE_PSTATUS_T */
8466 elfcore_write_prfpreg (bfd *abfd,
8472 const char *note_name = "CORE";
8473 return elfcore_write_note (abfd, buf, bufsiz,
8474 note_name, NT_FPREGSET, fpregs, size);
8478 elfcore_write_prxfpreg (bfd *abfd,
8481 const void *xfpregs,
8484 char *note_name = "LINUX";
8485 return elfcore_write_note (abfd, buf, bufsiz,
8486 note_name, NT_PRXFPREG, xfpregs, size);
8490 elfcore_write_ppc_vmx (bfd *abfd,
8493 const void *ppc_vmx,
8496 char *note_name = "LINUX";
8497 return elfcore_write_note (abfd, buf, bufsiz,
8498 note_name, NT_PPC_VMX, ppc_vmx, size);
8502 elfcore_write_ppc_vsx (bfd *abfd,
8505 const void *ppc_vsx,
8508 char *note_name = "LINUX";
8509 return elfcore_write_note (abfd, buf, bufsiz,
8510 note_name, NT_PPC_VSX, ppc_vsx, size);
8514 elfcore_write_register_note (bfd *abfd,
8517 const char *section,
8521 if (strcmp (section, ".reg2") == 0)
8522 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8523 if (strcmp (section, ".reg-xfp") == 0)
8524 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8525 if (strcmp (section, ".reg-ppc-vmx") == 0)
8526 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8527 if (strcmp (section, ".reg-ppc-vsx") == 0)
8528 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8533 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8538 while (p < buf + size)
8540 /* FIXME: bad alignment assumption. */
8541 Elf_External_Note *xnp = (Elf_External_Note *) p;
8542 Elf_Internal_Note in;
8544 if (offsetof (Elf_External_Note, name) > buf - p + size)
8547 in.type = H_GET_32 (abfd, xnp->type);
8549 in.namesz = H_GET_32 (abfd, xnp->namesz);
8550 in.namedata = xnp->name;
8551 if (in.namesz > buf - in.namedata + size)
8554 in.descsz = H_GET_32 (abfd, xnp->descsz);
8555 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8556 in.descpos = offset + (in.descdata - buf);
8558 && (in.descdata >= buf + size
8559 || in.descsz > buf - in.descdata + size))
8562 switch (bfd_get_format (abfd))
8568 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8570 if (! elfcore_grok_netbsd_note (abfd, &in))
8573 else if (CONST_STRNEQ (in.namedata, "QNX"))
8575 if (! elfcore_grok_nto_note (abfd, &in))
8578 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8580 if (! elfcore_grok_spu_note (abfd, &in))
8585 if (! elfcore_grok_note (abfd, &in))
8591 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8593 if (! elfobj_grok_gnu_note (abfd, &in))
8599 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8606 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8613 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8616 buf = bfd_malloc (size);
8620 if (bfd_bread (buf, size, abfd) != size
8621 || !elf_parse_notes (abfd, buf, size, offset))
8631 /* Providing external access to the ELF program header table. */
8633 /* Return an upper bound on the number of bytes required to store a
8634 copy of ABFD's program header table entries. Return -1 if an error
8635 occurs; bfd_get_error will return an appropriate code. */
8638 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8640 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8642 bfd_set_error (bfd_error_wrong_format);
8646 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8649 /* Copy ABFD's program header table entries to *PHDRS. The entries
8650 will be stored as an array of Elf_Internal_Phdr structures, as
8651 defined in include/elf/internal.h. To find out how large the
8652 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8654 Return the number of program header table entries read, or -1 if an
8655 error occurs; bfd_get_error will return an appropriate code. */
8658 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8662 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8664 bfd_set_error (bfd_error_wrong_format);
8668 num_phdrs = elf_elfheader (abfd)->e_phnum;
8669 memcpy (phdrs, elf_tdata (abfd)->phdr,
8670 num_phdrs * sizeof (Elf_Internal_Phdr));
8675 enum elf_reloc_type_class
8676 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8678 return reloc_class_normal;
8681 /* For RELA architectures, return the relocation value for a
8682 relocation against a local symbol. */
8685 _bfd_elf_rela_local_sym (bfd *abfd,
8686 Elf_Internal_Sym *sym,
8688 Elf_Internal_Rela *rel)
8690 asection *sec = *psec;
8693 relocation = (sec->output_section->vma
8694 + sec->output_offset
8696 if ((sec->flags & SEC_MERGE)
8697 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8698 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8701 _bfd_merged_section_offset (abfd, psec,
8702 elf_section_data (sec)->sec_info,
8703 sym->st_value + rel->r_addend);
8706 /* If we have changed the section, and our original section is
8707 marked with SEC_EXCLUDE, it means that the original
8708 SEC_MERGE section has been completely subsumed in some
8709 other SEC_MERGE section. In this case, we need to leave
8710 some info around for --emit-relocs. */
8711 if ((sec->flags & SEC_EXCLUDE) != 0)
8712 sec->kept_section = *psec;
8715 rel->r_addend -= relocation;
8716 rel->r_addend += sec->output_section->vma + sec->output_offset;
8722 _bfd_elf_rel_local_sym (bfd *abfd,
8723 Elf_Internal_Sym *sym,
8727 asection *sec = *psec;
8729 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8730 return sym->st_value + addend;
8732 return _bfd_merged_section_offset (abfd, psec,
8733 elf_section_data (sec)->sec_info,
8734 sym->st_value + addend);
8738 _bfd_elf_section_offset (bfd *abfd,
8739 struct bfd_link_info *info,
8743 switch (sec->sec_info_type)
8745 case ELF_INFO_TYPE_STABS:
8746 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8748 case ELF_INFO_TYPE_EH_FRAME:
8749 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8755 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8756 reconstruct an ELF file by reading the segments out of remote memory
8757 based on the ELF file header at EHDR_VMA and the ELF program headers it
8758 points to. If not null, *LOADBASEP is filled in with the difference
8759 between the VMAs from which the segments were read, and the VMAs the
8760 file headers (and hence BFD's idea of each section's VMA) put them at.
8762 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8763 remote memory at target address VMA into the local buffer at MYADDR; it
8764 should return zero on success or an `errno' code on failure. TEMPL must
8765 be a BFD for an ELF target with the word size and byte order found in
8766 the remote memory. */
8769 bfd_elf_bfd_from_remote_memory
8773 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8775 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8776 (templ, ehdr_vma, loadbasep, target_read_memory);
8780 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8781 long symcount ATTRIBUTE_UNUSED,
8782 asymbol **syms ATTRIBUTE_UNUSED,
8787 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8790 const char *relplt_name;
8791 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8795 Elf_Internal_Shdr *hdr;
8801 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8804 if (dynsymcount <= 0)
8807 if (!bed->plt_sym_val)
8810 relplt_name = bed->relplt_name;
8811 if (relplt_name == NULL)
8812 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8813 relplt = bfd_get_section_by_name (abfd, relplt_name);
8817 hdr = &elf_section_data (relplt)->this_hdr;
8818 if (hdr->sh_link != elf_dynsymtab (abfd)
8819 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8822 plt = bfd_get_section_by_name (abfd, ".plt");
8826 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8827 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8830 count = relplt->size / hdr->sh_entsize;
8831 size = count * sizeof (asymbol);
8832 p = relplt->relocation;
8833 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8834 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8836 s = *ret = bfd_malloc (size);
8840 names = (char *) (s + count);
8841 p = relplt->relocation;
8843 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8848 addr = bed->plt_sym_val (i, plt, p);
8849 if (addr == (bfd_vma) -1)
8852 *s = **p->sym_ptr_ptr;
8853 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8854 we are defining a symbol, ensure one of them is set. */
8855 if ((s->flags & BSF_LOCAL) == 0)
8856 s->flags |= BSF_GLOBAL;
8857 s->flags |= BSF_SYNTHETIC;
8859 s->value = addr - plt->vma;
8862 len = strlen ((*p->sym_ptr_ptr)->name);
8863 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8865 memcpy (names, "@plt", sizeof ("@plt"));
8866 names += sizeof ("@plt");
8873 /* It is only used by x86-64 so far. */
8874 asection _bfd_elf_large_com_section
8875 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8876 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8879 _bfd_elf_set_osabi (bfd * abfd,
8880 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8882 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8884 i_ehdrp = elf_elfheader (abfd);
8886 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8890 /* Return TRUE for ELF symbol types that represent functions.
8891 This is the default version of this function, which is sufficient for
8892 most targets. It returns true if TYPE is STT_FUNC. */
8895 _bfd_elf_is_function_type (unsigned int type)
8897 return (type == STT_FUNC);