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, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
29 BFD support for ELF formats is being worked on.
30 Currently, the best supported back ends are for sparc and i386
31 (running svr4 or Solaris 2).
33 Documentation of the internals of the support code still needs
34 to be written. The code is changing quickly enough that we
35 haven't bothered yet. */
37 /* For sparc64-cross-sparc32. */
45 #include "libiberty.h"
46 #include "safe-ctype.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bfd_boolean prep_headers (bfd *);
55 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
56 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
57 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
67 _bfd_elf_swap_verdef_in (bfd *abfd,
68 const Elf_External_Verdef *src,
69 Elf_Internal_Verdef *dst)
71 dst->vd_version = H_GET_16 (abfd, src->vd_version);
72 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
73 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
74 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
75 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
76 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
77 dst->vd_next = H_GET_32 (abfd, src->vd_next);
80 /* Swap out a Verdef structure. */
83 _bfd_elf_swap_verdef_out (bfd *abfd,
84 const Elf_Internal_Verdef *src,
85 Elf_External_Verdef *dst)
87 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
88 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
89 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
92 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
93 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
96 /* Swap in a Verdaux structure. */
99 _bfd_elf_swap_verdaux_in (bfd *abfd,
100 const Elf_External_Verdaux *src,
101 Elf_Internal_Verdaux *dst)
103 dst->vda_name = H_GET_32 (abfd, src->vda_name);
104 dst->vda_next = H_GET_32 (abfd, src->vda_next);
107 /* Swap out a Verdaux structure. */
110 _bfd_elf_swap_verdaux_out (bfd *abfd,
111 const Elf_Internal_Verdaux *src,
112 Elf_External_Verdaux *dst)
114 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
115 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
118 /* Swap in a Verneed structure. */
121 _bfd_elf_swap_verneed_in (bfd *abfd,
122 const Elf_External_Verneed *src,
123 Elf_Internal_Verneed *dst)
125 dst->vn_version = H_GET_16 (abfd, src->vn_version);
126 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
127 dst->vn_file = H_GET_32 (abfd, src->vn_file);
128 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
129 dst->vn_next = H_GET_32 (abfd, src->vn_next);
132 /* Swap out a Verneed structure. */
135 _bfd_elf_swap_verneed_out (bfd *abfd,
136 const Elf_Internal_Verneed *src,
137 Elf_External_Verneed *dst)
139 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
140 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
141 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
142 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
143 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
146 /* Swap in a Vernaux structure. */
149 _bfd_elf_swap_vernaux_in (bfd *abfd,
150 const Elf_External_Vernaux *src,
151 Elf_Internal_Vernaux *dst)
153 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
154 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
155 dst->vna_other = H_GET_16 (abfd, src->vna_other);
156 dst->vna_name = H_GET_32 (abfd, src->vna_name);
157 dst->vna_next = H_GET_32 (abfd, src->vna_next);
160 /* Swap out a Vernaux structure. */
163 _bfd_elf_swap_vernaux_out (bfd *abfd,
164 const Elf_Internal_Vernaux *src,
165 Elf_External_Vernaux *dst)
167 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
168 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
169 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
170 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
171 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
174 /* Swap in a Versym structure. */
177 _bfd_elf_swap_versym_in (bfd *abfd,
178 const Elf_External_Versym *src,
179 Elf_Internal_Versym *dst)
181 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
184 /* Swap out a Versym structure. */
187 _bfd_elf_swap_versym_out (bfd *abfd,
188 const Elf_Internal_Versym *src,
189 Elf_External_Versym *dst)
191 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
194 /* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
198 bfd_elf_hash (const char *namearg)
200 const unsigned char *name = (const unsigned char *) namearg;
205 while ((ch = *name++) != '\0')
208 if ((g = (h & 0xf0000000)) != 0)
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
216 return h & 0xffffffff;
219 /* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
223 bfd_elf_gnu_hash (const char *namearg)
225 const unsigned char *name = (const unsigned char *) namearg;
226 unsigned long h = 5381;
229 while ((ch = *name++) != '\0')
230 h = (h << 5) + h + ch;
231 return h & 0xffffffff;
234 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
237 bfd_elf_allocate_object (bfd *abfd,
239 enum elf_target_id object_id)
241 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
242 abfd->tdata.any = bfd_zalloc (abfd, object_size);
243 if (abfd->tdata.any == NULL)
246 elf_object_id (abfd) = object_id;
247 elf_program_header_size (abfd) = (bfd_size_type) -1;
253 bfd_elf_make_object (bfd *abfd)
255 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
256 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
261 bfd_elf_mkcorefile (bfd *abfd)
263 /* I think this can be done just like an object file. */
264 return abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd);
268 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
270 Elf_Internal_Shdr **i_shdrp;
271 bfd_byte *shstrtab = NULL;
273 bfd_size_type shstrtabsize;
275 i_shdrp = elf_elfsections (abfd);
277 || shindex >= elf_numsections (abfd)
278 || i_shdrp[shindex] == 0)
281 shstrtab = i_shdrp[shindex]->contents;
282 if (shstrtab == NULL)
284 /* No cached one, attempt to read, and cache what we read. */
285 offset = i_shdrp[shindex]->sh_offset;
286 shstrtabsize = i_shdrp[shindex]->sh_size;
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize + 1 <= 1
291 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL
292 || bfd_seek (abfd, offset, SEEK_SET) != 0)
294 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
296 if (bfd_get_error () != bfd_error_system_call)
297 bfd_set_error (bfd_error_file_truncated);
299 /* Once we've failed to read it, make sure we don't keep
300 trying. Otherwise, we'll keep allocating space for
301 the string table over and over. */
302 i_shdrp[shindex]->sh_size = 0;
305 shstrtab[shstrtabsize] = '\0';
306 i_shdrp[shindex]->contents = shstrtab;
308 return (char *) shstrtab;
312 bfd_elf_string_from_elf_section (bfd *abfd,
313 unsigned int shindex,
314 unsigned int strindex)
316 Elf_Internal_Shdr *hdr;
321 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
324 hdr = elf_elfsections (abfd)[shindex];
326 if (hdr->contents == NULL
327 && bfd_elf_get_str_section (abfd, shindex) == NULL)
330 if (strindex >= hdr->sh_size)
332 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
333 (*_bfd_error_handler)
334 (_("%B: invalid string offset %u >= %lu for section `%s'"),
335 abfd, strindex, (unsigned long) hdr->sh_size,
336 (shindex == shstrndx && strindex == hdr->sh_name
338 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
342 return ((char *) hdr->contents) + strindex;
345 /* Read and convert symbols to internal format.
346 SYMCOUNT specifies the number of symbols to read, starting from
347 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
348 are non-NULL, they are used to store the internal symbols, external
349 symbols, and symbol section index extensions, respectively.
350 Returns a pointer to the internal symbol buffer (malloced if necessary)
351 or NULL if there were no symbols or some kind of problem. */
354 bfd_elf_get_elf_syms (bfd *ibfd,
355 Elf_Internal_Shdr *symtab_hdr,
358 Elf_Internal_Sym *intsym_buf,
360 Elf_External_Sym_Shndx *extshndx_buf)
362 Elf_Internal_Shdr *shndx_hdr;
364 const bfd_byte *esym;
365 Elf_External_Sym_Shndx *alloc_extshndx;
366 Elf_External_Sym_Shndx *shndx;
367 Elf_Internal_Sym *alloc_intsym;
368 Elf_Internal_Sym *isym;
369 Elf_Internal_Sym *isymend;
370 const struct elf_backend_data *bed;
375 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
381 /* Normal syms might have section extension entries. */
383 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
384 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
386 /* Read the symbols. */
388 alloc_extshndx = NULL;
390 bed = get_elf_backend_data (ibfd);
391 extsym_size = bed->s->sizeof_sym;
392 amt = symcount * extsym_size;
393 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
394 if (extsym_buf == NULL)
396 alloc_ext = bfd_malloc2 (symcount, extsym_size);
397 extsym_buf = alloc_ext;
399 if (extsym_buf == NULL
400 || bfd_seek (ibfd, pos, SEEK_SET) != 0
401 || bfd_bread (extsym_buf, amt, ibfd) != amt)
407 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
411 amt = symcount * sizeof (Elf_External_Sym_Shndx);
412 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
413 if (extshndx_buf == NULL)
415 alloc_extshndx = (Elf_External_Sym_Shndx *)
416 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
417 extshndx_buf = alloc_extshndx;
419 if (extshndx_buf == NULL
420 || bfd_seek (ibfd, pos, SEEK_SET) != 0
421 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
428 if (intsym_buf == NULL)
430 alloc_intsym = (Elf_Internal_Sym *)
431 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
432 intsym_buf = alloc_intsym;
433 if (intsym_buf == NULL)
437 /* Convert the symbols to internal form. */
438 isymend = intsym_buf + symcount;
439 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
440 shndx = extshndx_buf;
442 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
443 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
445 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
446 (*_bfd_error_handler) (_("%B symbol number %lu references "
447 "nonexistent SHT_SYMTAB_SHNDX section"),
448 ibfd, (unsigned long) symoffset);
449 if (alloc_intsym != NULL)
456 if (alloc_ext != NULL)
458 if (alloc_extshndx != NULL)
459 free (alloc_extshndx);
464 /* Look up a symbol name. */
466 bfd_elf_sym_name (bfd *abfd,
467 Elf_Internal_Shdr *symtab_hdr,
468 Elf_Internal_Sym *isym,
472 unsigned int iname = isym->st_name;
473 unsigned int shindex = symtab_hdr->sh_link;
475 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
476 /* Check for a bogus st_shndx to avoid crashing. */
477 && isym->st_shndx < elf_numsections (abfd))
479 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
480 shindex = elf_elfheader (abfd)->e_shstrndx;
483 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
486 else if (sym_sec && *name == '\0')
487 name = bfd_section_name (abfd, sym_sec);
492 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
493 sections. The first element is the flags, the rest are section
496 typedef union elf_internal_group {
497 Elf_Internal_Shdr *shdr;
499 } Elf_Internal_Group;
501 /* Return the name of the group signature symbol. Why isn't the
502 signature just a string? */
505 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
507 Elf_Internal_Shdr *hdr;
508 unsigned char esym[sizeof (Elf64_External_Sym)];
509 Elf_External_Sym_Shndx eshndx;
510 Elf_Internal_Sym isym;
512 /* First we need to ensure the symbol table is available. Make sure
513 that it is a symbol table section. */
514 if (ghdr->sh_link >= elf_numsections (abfd))
516 hdr = elf_elfsections (abfd) [ghdr->sh_link];
517 if (hdr->sh_type != SHT_SYMTAB
518 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
521 /* Go read the symbol. */
522 hdr = &elf_tdata (abfd)->symtab_hdr;
523 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
524 &isym, esym, &eshndx) == NULL)
527 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
530 /* Set next_in_group list pointer, and group name for NEWSECT. */
533 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
535 unsigned int num_group = elf_tdata (abfd)->num_group;
537 /* If num_group is zero, read in all SHT_GROUP sections. The count
538 is set to -1 if there are no SHT_GROUP sections. */
541 unsigned int i, shnum;
543 /* First count the number of groups. If we have a SHT_GROUP
544 section with just a flag word (ie. sh_size is 4), ignore it. */
545 shnum = elf_numsections (abfd);
548 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
549 ( (shdr)->sh_type == SHT_GROUP \
550 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
551 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
552 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
554 for (i = 0; i < shnum; i++)
556 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
558 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
564 num_group = (unsigned) -1;
565 elf_tdata (abfd)->num_group = num_group;
569 /* We keep a list of elf section headers for group sections,
570 so we can find them quickly. */
573 elf_tdata (abfd)->num_group = num_group;
574 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
575 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
576 if (elf_tdata (abfd)->group_sect_ptr == NULL)
580 for (i = 0; i < shnum; i++)
582 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
584 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
587 Elf_Internal_Group *dest;
589 /* Add to list of sections. */
590 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
593 /* Read the raw contents. */
594 BFD_ASSERT (sizeof (*dest) >= 4);
595 amt = shdr->sh_size * sizeof (*dest) / 4;
596 shdr->contents = (unsigned char *)
597 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
598 /* PR binutils/4110: Handle corrupt group headers. */
599 if (shdr->contents == NULL)
602 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
603 bfd_set_error (bfd_error_bad_value);
607 memset (shdr->contents, 0, amt);
609 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
610 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
614 /* Translate raw contents, a flag word followed by an
615 array of elf section indices all in target byte order,
616 to the flag word followed by an array of elf section
618 src = shdr->contents + shdr->sh_size;
619 dest = (Elf_Internal_Group *) (shdr->contents + amt);
626 idx = H_GET_32 (abfd, src);
627 if (src == shdr->contents)
630 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
631 shdr->bfd_section->flags
632 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
637 ((*_bfd_error_handler)
638 (_("%B: invalid SHT_GROUP entry"), abfd));
641 dest->shdr = elf_elfsections (abfd)[idx];
648 if (num_group != (unsigned) -1)
652 for (i = 0; i < num_group; i++)
654 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
655 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
656 unsigned int n_elt = shdr->sh_size / 4;
658 /* Look through this group's sections to see if current
659 section is a member. */
661 if ((++idx)->shdr == hdr)
665 /* We are a member of this group. Go looking through
666 other members to see if any others are linked via
668 idx = (Elf_Internal_Group *) shdr->contents;
669 n_elt = shdr->sh_size / 4;
671 if ((s = (++idx)->shdr->bfd_section) != NULL
672 && elf_next_in_group (s) != NULL)
676 /* Snarf the group name from other member, and
677 insert current section in circular list. */
678 elf_group_name (newsect) = elf_group_name (s);
679 elf_next_in_group (newsect) = elf_next_in_group (s);
680 elf_next_in_group (s) = newsect;
686 gname = group_signature (abfd, shdr);
689 elf_group_name (newsect) = gname;
691 /* Start a circular list with one element. */
692 elf_next_in_group (newsect) = newsect;
695 /* If the group section has been created, point to the
697 if (shdr->bfd_section != NULL)
698 elf_next_in_group (shdr->bfd_section) = newsect;
706 if (elf_group_name (newsect) == NULL)
708 (*_bfd_error_handler) (_("%B: no group info for section %A"),
715 _bfd_elf_setup_sections (bfd *abfd)
718 unsigned int num_group = elf_tdata (abfd)->num_group;
719 bfd_boolean result = TRUE;
722 /* Process SHF_LINK_ORDER. */
723 for (s = abfd->sections; s != NULL; s = s->next)
725 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
726 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
728 unsigned int elfsec = this_hdr->sh_link;
729 /* FIXME: The old Intel compiler and old strip/objcopy may
730 not set the sh_link or sh_info fields. Hence we could
731 get the situation where elfsec is 0. */
734 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
735 if (bed->link_order_error_handler)
736 bed->link_order_error_handler
737 (_("%B: warning: sh_link not set for section `%A'"),
742 asection *linksec = NULL;
744 if (elfsec < elf_numsections (abfd))
746 this_hdr = elf_elfsections (abfd)[elfsec];
747 linksec = this_hdr->bfd_section;
751 Some strip/objcopy may leave an incorrect value in
752 sh_link. We don't want to proceed. */
755 (*_bfd_error_handler)
756 (_("%B: sh_link [%d] in section `%A' is incorrect"),
757 s->owner, s, elfsec);
761 elf_linked_to_section (s) = linksec;
766 /* Process section groups. */
767 if (num_group == (unsigned) -1)
770 for (i = 0; i < num_group; i++)
772 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
773 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
774 unsigned int n_elt = shdr->sh_size / 4;
777 if ((++idx)->shdr->bfd_section)
778 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
779 else if (idx->shdr->sh_type == SHT_RELA
780 || idx->shdr->sh_type == SHT_REL)
781 /* We won't include relocation sections in section groups in
782 output object files. We adjust the group section size here
783 so that relocatable link will work correctly when
784 relocation sections are in section group in input object
786 shdr->bfd_section->size -= 4;
789 /* There are some unknown sections in the group. */
790 (*_bfd_error_handler)
791 (_("%B: unknown [%d] section `%s' in group [%s]"),
793 (unsigned int) idx->shdr->sh_type,
794 bfd_elf_string_from_elf_section (abfd,
795 (elf_elfheader (abfd)
798 shdr->bfd_section->name);
806 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
808 return elf_next_in_group (sec) != NULL;
811 /* Make a BFD section from an ELF section. We store a pointer to the
812 BFD section in the bfd_section field of the header. */
815 _bfd_elf_make_section_from_shdr (bfd *abfd,
816 Elf_Internal_Shdr *hdr,
822 const struct elf_backend_data *bed;
824 if (hdr->bfd_section != NULL)
826 BFD_ASSERT (strcmp (name,
827 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
831 newsect = bfd_make_section_anyway (abfd, name);
835 hdr->bfd_section = newsect;
836 elf_section_data (newsect)->this_hdr = *hdr;
837 elf_section_data (newsect)->this_idx = shindex;
839 /* Always use the real type/flags. */
840 elf_section_type (newsect) = hdr->sh_type;
841 elf_section_flags (newsect) = hdr->sh_flags;
843 newsect->filepos = hdr->sh_offset;
845 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
846 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
847 || ! bfd_set_section_alignment (abfd, newsect,
848 bfd_log2 (hdr->sh_addralign)))
851 flags = SEC_NO_FLAGS;
852 if (hdr->sh_type != SHT_NOBITS)
853 flags |= SEC_HAS_CONTENTS;
854 if (hdr->sh_type == SHT_GROUP)
855 flags |= SEC_GROUP | SEC_EXCLUDE;
856 if ((hdr->sh_flags & SHF_ALLOC) != 0)
859 if (hdr->sh_type != SHT_NOBITS)
862 if ((hdr->sh_flags & SHF_WRITE) == 0)
863 flags |= SEC_READONLY;
864 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
866 else if ((flags & SEC_LOAD) != 0)
868 if ((hdr->sh_flags & SHF_MERGE) != 0)
871 newsect->entsize = hdr->sh_entsize;
872 if ((hdr->sh_flags & SHF_STRINGS) != 0)
873 flags |= SEC_STRINGS;
875 if (hdr->sh_flags & SHF_GROUP)
876 if (!setup_group (abfd, hdr, newsect))
878 if ((hdr->sh_flags & SHF_TLS) != 0)
879 flags |= SEC_THREAD_LOCAL;
880 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
881 flags |= SEC_EXCLUDE;
883 if ((flags & SEC_ALLOC) == 0)
885 /* The debugging sections appear to be recognized only by name,
886 not any sort of flag. Their SEC_ALLOC bits are cleared. */
891 } debug_sections [] =
893 { STRING_COMMA_LEN ("debug") }, /* 'd' */
894 { NULL, 0 }, /* 'e' */
895 { NULL, 0 }, /* 'f' */
896 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
897 { NULL, 0 }, /* 'h' */
898 { NULL, 0 }, /* 'i' */
899 { NULL, 0 }, /* 'j' */
900 { NULL, 0 }, /* 'k' */
901 { STRING_COMMA_LEN ("line") }, /* 'l' */
902 { NULL, 0 }, /* 'm' */
903 { NULL, 0 }, /* 'n' */
904 { NULL, 0 }, /* 'o' */
905 { NULL, 0 }, /* 'p' */
906 { NULL, 0 }, /* 'q' */
907 { NULL, 0 }, /* 'r' */
908 { STRING_COMMA_LEN ("stab") }, /* 's' */
909 { NULL, 0 }, /* 't' */
910 { NULL, 0 }, /* 'u' */
911 { NULL, 0 }, /* 'v' */
912 { NULL, 0 }, /* 'w' */
913 { NULL, 0 }, /* 'x' */
914 { NULL, 0 }, /* 'y' */
915 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
920 int i = name [1] - 'd';
922 && i < (int) ARRAY_SIZE (debug_sections)
923 && debug_sections [i].name != NULL
924 && strncmp (&name [1], debug_sections [i].name,
925 debug_sections [i].len) == 0)
926 flags |= SEC_DEBUGGING;
930 /* As a GNU extension, if the name begins with .gnu.linkonce, we
931 only link a single copy of the section. This is used to support
932 g++. g++ will emit each template expansion in its own section.
933 The symbols will be defined as weak, so that multiple definitions
934 are permitted. The GNU linker extension is to actually discard
935 all but one of the sections. */
936 if (CONST_STRNEQ (name, ".gnu.linkonce")
937 && elf_next_in_group (newsect) == NULL)
938 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
940 bed = get_elf_backend_data (abfd);
941 if (bed->elf_backend_section_flags)
942 if (! bed->elf_backend_section_flags (&flags, hdr))
945 if (! bfd_set_section_flags (abfd, newsect, flags))
948 /* We do not parse the PT_NOTE segments as we are interested even in the
949 separate debug info files which may have the segments offsets corrupted.
950 PT_NOTEs from the core files are currently not parsed using BFD. */
951 if (hdr->sh_type == SHT_NOTE)
955 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
958 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
962 if ((flags & SEC_ALLOC) != 0)
964 Elf_Internal_Phdr *phdr;
965 unsigned int i, nload;
967 /* Some ELF linkers produce binaries with all the program header
968 p_paddr fields zero. If we have such a binary with more than
969 one PT_LOAD header, then leave the section lma equal to vma
970 so that we don't create sections with overlapping lma. */
971 phdr = elf_tdata (abfd)->phdr;
972 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
973 if (phdr->p_paddr != 0)
975 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
977 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
980 phdr = elf_tdata (abfd)->phdr;
981 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
983 if (phdr->p_type == PT_LOAD
984 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
986 if ((flags & SEC_LOAD) == 0)
987 newsect->lma = (phdr->p_paddr
988 + hdr->sh_addr - phdr->p_vaddr);
990 /* We used to use the same adjustment for SEC_LOAD
991 sections, but that doesn't work if the segment
992 is packed with code from multiple VMAs.
993 Instead we calculate the section LMA based on
994 the segment LMA. It is assumed that the
995 segment will contain sections with contiguous
996 LMAs, even if the VMAs are not. */
997 newsect->lma = (phdr->p_paddr
998 + hdr->sh_offset - phdr->p_offset);
1000 /* With contiguous segments, we can't tell from file
1001 offsets whether a section with zero size should
1002 be placed at the end of one segment or the
1003 beginning of the next. Decide based on vaddr. */
1004 if (hdr->sh_addr >= phdr->p_vaddr
1005 && (hdr->sh_addr + hdr->sh_size
1006 <= phdr->p_vaddr + phdr->p_memsz))
1015 const char *const bfd_elf_section_type_names[] = {
1016 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1017 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1018 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1021 /* ELF relocs are against symbols. If we are producing relocatable
1022 output, and the reloc is against an external symbol, and nothing
1023 has given us any additional addend, the resulting reloc will also
1024 be against the same symbol. In such a case, we don't want to
1025 change anything about the way the reloc is handled, since it will
1026 all be done at final link time. Rather than put special case code
1027 into bfd_perform_relocation, all the reloc types use this howto
1028 function. It just short circuits the reloc if producing
1029 relocatable output against an external symbol. */
1031 bfd_reloc_status_type
1032 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1033 arelent *reloc_entry,
1035 void *data ATTRIBUTE_UNUSED,
1036 asection *input_section,
1038 char **error_message ATTRIBUTE_UNUSED)
1040 if (output_bfd != NULL
1041 && (symbol->flags & BSF_SECTION_SYM) == 0
1042 && (! reloc_entry->howto->partial_inplace
1043 || reloc_entry->addend == 0))
1045 reloc_entry->address += input_section->output_offset;
1046 return bfd_reloc_ok;
1049 return bfd_reloc_continue;
1052 /* Copy the program header and other data from one object module to
1056 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1058 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1059 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1062 BFD_ASSERT (!elf_flags_init (obfd)
1063 || (elf_elfheader (obfd)->e_flags
1064 == elf_elfheader (ibfd)->e_flags));
1066 elf_gp (obfd) = elf_gp (ibfd);
1067 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1068 elf_flags_init (obfd) = TRUE;
1070 /* Copy object attributes. */
1071 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1076 get_segment_type (unsigned int p_type)
1081 case PT_NULL: pt = "NULL"; break;
1082 case PT_LOAD: pt = "LOAD"; break;
1083 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1084 case PT_INTERP: pt = "INTERP"; break;
1085 case PT_NOTE: pt = "NOTE"; break;
1086 case PT_SHLIB: pt = "SHLIB"; break;
1087 case PT_PHDR: pt = "PHDR"; break;
1088 case PT_TLS: pt = "TLS"; break;
1089 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1090 case PT_GNU_STACK: pt = "STACK"; break;
1091 case PT_GNU_RELRO: pt = "RELRO"; break;
1092 default: pt = NULL; break;
1097 /* Print out the program headers. */
1100 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1102 FILE *f = (FILE *) farg;
1103 Elf_Internal_Phdr *p;
1105 bfd_byte *dynbuf = NULL;
1107 p = elf_tdata (abfd)->phdr;
1112 fprintf (f, _("\nProgram Header:\n"));
1113 c = elf_elfheader (abfd)->e_phnum;
1114 for (i = 0; i < c; i++, p++)
1116 const char *pt = get_segment_type (p->p_type);
1121 sprintf (buf, "0x%lx", p->p_type);
1124 fprintf (f, "%8s off 0x", pt);
1125 bfd_fprintf_vma (abfd, f, p->p_offset);
1126 fprintf (f, " vaddr 0x");
1127 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1128 fprintf (f, " paddr 0x");
1129 bfd_fprintf_vma (abfd, f, p->p_paddr);
1130 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1131 fprintf (f, " filesz 0x");
1132 bfd_fprintf_vma (abfd, f, p->p_filesz);
1133 fprintf (f, " memsz 0x");
1134 bfd_fprintf_vma (abfd, f, p->p_memsz);
1135 fprintf (f, " flags %c%c%c",
1136 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1137 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1138 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1139 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1140 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1145 s = bfd_get_section_by_name (abfd, ".dynamic");
1148 unsigned int elfsec;
1149 unsigned long shlink;
1150 bfd_byte *extdyn, *extdynend;
1152 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1154 fprintf (f, _("\nDynamic Section:\n"));
1156 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1159 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1160 if (elfsec == SHN_BAD)
1162 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1164 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1165 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1168 extdynend = extdyn + s->size;
1169 for (; extdyn < extdynend; extdyn += extdynsize)
1171 Elf_Internal_Dyn dyn;
1172 const char *name = "";
1174 bfd_boolean stringp;
1175 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1177 (*swap_dyn_in) (abfd, extdyn, &dyn);
1179 if (dyn.d_tag == DT_NULL)
1186 if (bed->elf_backend_get_target_dtag)
1187 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1189 if (!strcmp (name, ""))
1191 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1196 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1197 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1198 case DT_PLTGOT: name = "PLTGOT"; break;
1199 case DT_HASH: name = "HASH"; break;
1200 case DT_STRTAB: name = "STRTAB"; break;
1201 case DT_SYMTAB: name = "SYMTAB"; break;
1202 case DT_RELA: name = "RELA"; break;
1203 case DT_RELASZ: name = "RELASZ"; break;
1204 case DT_RELAENT: name = "RELAENT"; break;
1205 case DT_STRSZ: name = "STRSZ"; break;
1206 case DT_SYMENT: name = "SYMENT"; break;
1207 case DT_INIT: name = "INIT"; break;
1208 case DT_FINI: name = "FINI"; break;
1209 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1210 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1211 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1212 case DT_REL: name = "REL"; break;
1213 case DT_RELSZ: name = "RELSZ"; break;
1214 case DT_RELENT: name = "RELENT"; break;
1215 case DT_PLTREL: name = "PLTREL"; break;
1216 case DT_DEBUG: name = "DEBUG"; break;
1217 case DT_TEXTREL: name = "TEXTREL"; break;
1218 case DT_JMPREL: name = "JMPREL"; break;
1219 case DT_BIND_NOW: name = "BIND_NOW"; break;
1220 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1221 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1222 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1223 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1224 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1225 case DT_FLAGS: name = "FLAGS"; break;
1226 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1227 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1228 case DT_CHECKSUM: name = "CHECKSUM"; break;
1229 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1230 case DT_MOVEENT: name = "MOVEENT"; break;
1231 case DT_MOVESZ: name = "MOVESZ"; break;
1232 case DT_FEATURE: name = "FEATURE"; break;
1233 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1234 case DT_SYMINSZ: name = "SYMINSZ"; break;
1235 case DT_SYMINENT: name = "SYMINENT"; break;
1236 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1237 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1238 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1239 case DT_PLTPAD: name = "PLTPAD"; break;
1240 case DT_MOVETAB: name = "MOVETAB"; break;
1241 case DT_SYMINFO: name = "SYMINFO"; break;
1242 case DT_RELACOUNT: name = "RELACOUNT"; break;
1243 case DT_RELCOUNT: name = "RELCOUNT"; break;
1244 case DT_FLAGS_1: name = "FLAGS_1"; break;
1245 case DT_VERSYM: name = "VERSYM"; break;
1246 case DT_VERDEF: name = "VERDEF"; break;
1247 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1248 case DT_VERNEED: name = "VERNEED"; break;
1249 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1250 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1251 case DT_USED: name = "USED"; break;
1252 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1253 case DT_GNU_HASH: name = "GNU_HASH"; break;
1256 fprintf (f, " %-20s ", name);
1260 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1265 unsigned int tagv = dyn.d_un.d_val;
1267 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1270 fprintf (f, "%s", string);
1279 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1280 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1282 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1286 if (elf_dynverdef (abfd) != 0)
1288 Elf_Internal_Verdef *t;
1290 fprintf (f, _("\nVersion definitions:\n"));
1291 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1293 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1294 t->vd_flags, t->vd_hash,
1295 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1296 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1298 Elf_Internal_Verdaux *a;
1301 for (a = t->vd_auxptr->vda_nextptr;
1305 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1311 if (elf_dynverref (abfd) != 0)
1313 Elf_Internal_Verneed *t;
1315 fprintf (f, _("\nVersion References:\n"));
1316 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1318 Elf_Internal_Vernaux *a;
1320 fprintf (f, _(" required from %s:\n"),
1321 t->vn_filename ? t->vn_filename : "<corrupt>");
1322 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1323 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1324 a->vna_flags, a->vna_other,
1325 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1337 /* Display ELF-specific fields of a symbol. */
1340 bfd_elf_print_symbol (bfd *abfd,
1343 bfd_print_symbol_type how)
1345 FILE *file = (FILE *) filep;
1348 case bfd_print_symbol_name:
1349 fprintf (file, "%s", symbol->name);
1351 case bfd_print_symbol_more:
1352 fprintf (file, "elf ");
1353 bfd_fprintf_vma (abfd, file, symbol->value);
1354 fprintf (file, " %lx", (unsigned long) symbol->flags);
1356 case bfd_print_symbol_all:
1358 const char *section_name;
1359 const char *name = NULL;
1360 const struct elf_backend_data *bed;
1361 unsigned char st_other;
1364 section_name = symbol->section ? symbol->section->name : "(*none*)";
1366 bed = get_elf_backend_data (abfd);
1367 if (bed->elf_backend_print_symbol_all)
1368 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1372 name = symbol->name;
1373 bfd_print_symbol_vandf (abfd, file, symbol);
1376 fprintf (file, " %s\t", section_name);
1377 /* Print the "other" value for a symbol. For common symbols,
1378 we've already printed the size; now print the alignment.
1379 For other symbols, we have no specified alignment, and
1380 we've printed the address; now print the size. */
1381 if (symbol->section && bfd_is_com_section (symbol->section))
1382 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1384 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1385 bfd_fprintf_vma (abfd, file, val);
1387 /* If we have version information, print it. */
1388 if (elf_tdata (abfd)->dynversym_section != 0
1389 && (elf_tdata (abfd)->dynverdef_section != 0
1390 || elf_tdata (abfd)->dynverref_section != 0))
1392 unsigned int vernum;
1393 const char *version_string;
1395 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1398 version_string = "";
1399 else if (vernum == 1)
1400 version_string = "Base";
1401 else if (vernum <= elf_tdata (abfd)->cverdefs)
1403 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1406 Elf_Internal_Verneed *t;
1408 version_string = "";
1409 for (t = elf_tdata (abfd)->verref;
1413 Elf_Internal_Vernaux *a;
1415 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1417 if (a->vna_other == vernum)
1419 version_string = a->vna_nodename;
1426 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1427 fprintf (file, " %-11s", version_string);
1432 fprintf (file, " (%s)", version_string);
1433 for (i = 10 - strlen (version_string); i > 0; --i)
1438 /* If the st_other field is not zero, print it. */
1439 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1444 case STV_INTERNAL: fprintf (file, " .internal"); break;
1445 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1446 case STV_PROTECTED: fprintf (file, " .protected"); break;
1448 /* Some other non-defined flags are also present, so print
1450 fprintf (file, " 0x%02x", (unsigned int) st_other);
1453 fprintf (file, " %s", name);
1459 /* Allocate an ELF string table--force the first byte to be zero. */
1461 struct bfd_strtab_hash *
1462 _bfd_elf_stringtab_init (void)
1464 struct bfd_strtab_hash *ret;
1466 ret = _bfd_stringtab_init ();
1471 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1472 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1473 if (loc == (bfd_size_type) -1)
1475 _bfd_stringtab_free (ret);
1482 /* ELF .o/exec file reading */
1484 /* Create a new bfd section from an ELF section header. */
1487 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1489 Elf_Internal_Shdr *hdr;
1490 Elf_Internal_Ehdr *ehdr;
1491 const struct elf_backend_data *bed;
1494 if (shindex >= elf_numsections (abfd))
1497 hdr = elf_elfsections (abfd)[shindex];
1498 ehdr = elf_elfheader (abfd);
1499 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1504 bed = get_elf_backend_data (abfd);
1505 switch (hdr->sh_type)
1508 /* Inactive section. Throw it away. */
1511 case SHT_PROGBITS: /* Normal section with contents. */
1512 case SHT_NOBITS: /* .bss section. */
1513 case SHT_HASH: /* .hash section. */
1514 case SHT_NOTE: /* .note section. */
1515 case SHT_INIT_ARRAY: /* .init_array section. */
1516 case SHT_FINI_ARRAY: /* .fini_array section. */
1517 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1518 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1519 case SHT_GNU_HASH: /* .gnu.hash section. */
1520 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1522 case SHT_DYNAMIC: /* Dynamic linking information. */
1523 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1525 if (hdr->sh_link > elf_numsections (abfd))
1527 /* PR 10478: Accept Solaris binaries with a sh_link
1528 field set to SHN_BEFORE or SHN_AFTER. */
1529 switch (bfd_get_arch (abfd))
1532 case bfd_arch_sparc:
1533 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1534 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1536 /* Otherwise fall through. */
1541 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1543 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1545 Elf_Internal_Shdr *dynsymhdr;
1547 /* The shared libraries distributed with hpux11 have a bogus
1548 sh_link field for the ".dynamic" section. Find the
1549 string table for the ".dynsym" section instead. */
1550 if (elf_dynsymtab (abfd) != 0)
1552 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1553 hdr->sh_link = dynsymhdr->sh_link;
1557 unsigned int i, num_sec;
1559 num_sec = elf_numsections (abfd);
1560 for (i = 1; i < num_sec; i++)
1562 dynsymhdr = elf_elfsections (abfd)[i];
1563 if (dynsymhdr->sh_type == SHT_DYNSYM)
1565 hdr->sh_link = dynsymhdr->sh_link;
1573 case SHT_SYMTAB: /* A symbol table */
1574 if (elf_onesymtab (abfd) == shindex)
1577 if (hdr->sh_entsize != bed->s->sizeof_sym)
1579 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1581 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1582 elf_onesymtab (abfd) = shindex;
1583 elf_tdata (abfd)->symtab_hdr = *hdr;
1584 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1585 abfd->flags |= HAS_SYMS;
1587 /* Sometimes a shared object will map in the symbol table. If
1588 SHF_ALLOC is set, and this is a shared object, then we also
1589 treat this section as a BFD section. We can not base the
1590 decision purely on SHF_ALLOC, because that flag is sometimes
1591 set in a relocatable object file, which would confuse the
1593 if ((hdr->sh_flags & SHF_ALLOC) != 0
1594 && (abfd->flags & DYNAMIC) != 0
1595 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1599 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1600 can't read symbols without that section loaded as well. It
1601 is most likely specified by the next section header. */
1602 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1604 unsigned int i, num_sec;
1606 num_sec = elf_numsections (abfd);
1607 for (i = shindex + 1; i < num_sec; i++)
1609 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1610 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1611 && hdr2->sh_link == shindex)
1615 for (i = 1; i < shindex; i++)
1617 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1618 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1619 && hdr2->sh_link == shindex)
1623 return bfd_section_from_shdr (abfd, i);
1627 case SHT_DYNSYM: /* A dynamic symbol table */
1628 if (elf_dynsymtab (abfd) == shindex)
1631 if (hdr->sh_entsize != bed->s->sizeof_sym)
1633 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1634 elf_dynsymtab (abfd) = shindex;
1635 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1636 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1637 abfd->flags |= HAS_SYMS;
1639 /* Besides being a symbol table, we also treat this as a regular
1640 section, so that objcopy can handle it. */
1641 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1643 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1644 if (elf_symtab_shndx (abfd) == shindex)
1647 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1648 elf_symtab_shndx (abfd) = shindex;
1649 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1650 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1653 case SHT_STRTAB: /* A string table */
1654 if (hdr->bfd_section != NULL)
1656 if (ehdr->e_shstrndx == shindex)
1658 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1659 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1662 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1665 elf_tdata (abfd)->strtab_hdr = *hdr;
1666 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1669 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1672 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1673 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1674 elf_elfsections (abfd)[shindex] = hdr;
1675 /* We also treat this as a regular section, so that objcopy
1677 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1681 /* If the string table isn't one of the above, then treat it as a
1682 regular section. We need to scan all the headers to be sure,
1683 just in case this strtab section appeared before the above. */
1684 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1686 unsigned int i, num_sec;
1688 num_sec = elf_numsections (abfd);
1689 for (i = 1; i < num_sec; i++)
1691 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1692 if (hdr2->sh_link == shindex)
1694 /* Prevent endless recursion on broken objects. */
1697 if (! bfd_section_from_shdr (abfd, i))
1699 if (elf_onesymtab (abfd) == i)
1701 if (elf_dynsymtab (abfd) == i)
1702 goto dynsymtab_strtab;
1706 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1710 /* *These* do a lot of work -- but build no sections! */
1712 asection *target_sect;
1713 Elf_Internal_Shdr *hdr2;
1714 unsigned int num_sec = elf_numsections (abfd);
1717 != (bfd_size_type) (hdr->sh_type == SHT_REL
1718 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1721 /* Check for a bogus link to avoid crashing. */
1722 if (hdr->sh_link >= num_sec)
1724 ((*_bfd_error_handler)
1725 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1726 abfd, hdr->sh_link, name, shindex));
1727 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1731 /* For some incomprehensible reason Oracle distributes
1732 libraries for Solaris in which some of the objects have
1733 bogus sh_link fields. It would be nice if we could just
1734 reject them, but, unfortunately, some people need to use
1735 them. We scan through the section headers; if we find only
1736 one suitable symbol table, we clobber the sh_link to point
1737 to it. I hope this doesn't break anything.
1739 Don't do it on executable nor shared library. */
1740 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1741 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1742 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1748 for (scan = 1; scan < num_sec; scan++)
1750 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1751 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1762 hdr->sh_link = found;
1765 /* Get the symbol table. */
1766 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1767 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1768 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1771 /* If this reloc section does not use the main symbol table we
1772 don't treat it as a reloc section. BFD can't adequately
1773 represent such a section, so at least for now, we don't
1774 try. We just present it as a normal section. We also
1775 can't use it as a reloc section if it points to the null
1776 section, an invalid section, another reloc section, or its
1777 sh_link points to the null section. */
1778 if (hdr->sh_link != elf_onesymtab (abfd)
1779 || hdr->sh_link == SHN_UNDEF
1780 || hdr->sh_info == SHN_UNDEF
1781 || hdr->sh_info >= num_sec
1782 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1783 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1784 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1787 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1789 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1790 if (target_sect == NULL)
1793 if ((target_sect->flags & SEC_RELOC) == 0
1794 || target_sect->reloc_count == 0)
1795 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1799 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1800 amt = sizeof (*hdr2);
1801 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1804 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1807 elf_elfsections (abfd)[shindex] = hdr2;
1808 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1809 target_sect->flags |= SEC_RELOC;
1810 target_sect->relocation = NULL;
1811 target_sect->rel_filepos = hdr->sh_offset;
1812 /* In the section to which the relocations apply, mark whether
1813 its relocations are of the REL or RELA variety. */
1814 if (hdr->sh_size != 0)
1815 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1816 abfd->flags |= HAS_RELOC;
1820 case SHT_GNU_verdef:
1821 elf_dynverdef (abfd) = shindex;
1822 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1823 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1825 case SHT_GNU_versym:
1826 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1828 elf_dynversym (abfd) = shindex;
1829 elf_tdata (abfd)->dynversym_hdr = *hdr;
1830 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1832 case SHT_GNU_verneed:
1833 elf_dynverref (abfd) = shindex;
1834 elf_tdata (abfd)->dynverref_hdr = *hdr;
1835 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1841 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1843 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1845 if (hdr->contents != NULL)
1847 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1848 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1851 if (idx->flags & GRP_COMDAT)
1852 hdr->bfd_section->flags
1853 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1855 /* We try to keep the same section order as it comes in. */
1857 while (--n_elt != 0)
1861 if (idx->shdr != NULL
1862 && (s = idx->shdr->bfd_section) != NULL
1863 && elf_next_in_group (s) != NULL)
1865 elf_next_in_group (hdr->bfd_section) = s;
1873 /* Possibly an attributes section. */
1874 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1875 || hdr->sh_type == bed->obj_attrs_section_type)
1877 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1879 _bfd_elf_parse_attributes (abfd, hdr);
1883 /* Check for any processor-specific section types. */
1884 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1887 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1889 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1890 /* FIXME: How to properly handle allocated section reserved
1891 for applications? */
1892 (*_bfd_error_handler)
1893 (_("%B: don't know how to handle allocated, application "
1894 "specific section `%s' [0x%8x]"),
1895 abfd, name, hdr->sh_type);
1897 /* Allow sections reserved for applications. */
1898 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1901 else if (hdr->sh_type >= SHT_LOPROC
1902 && hdr->sh_type <= SHT_HIPROC)
1903 /* FIXME: We should handle this section. */
1904 (*_bfd_error_handler)
1905 (_("%B: don't know how to handle processor specific section "
1907 abfd, name, hdr->sh_type);
1908 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1910 /* Unrecognised OS-specific sections. */
1911 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1912 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1913 required to correctly process the section and the file should
1914 be rejected with an error message. */
1915 (*_bfd_error_handler)
1916 (_("%B: don't know how to handle OS specific section "
1918 abfd, name, hdr->sh_type);
1920 /* Otherwise it should be processed. */
1921 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1924 /* FIXME: We should handle this section. */
1925 (*_bfd_error_handler)
1926 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1927 abfd, name, hdr->sh_type);
1935 /* Return the local symbol specified by ABFD, R_SYMNDX. */
1938 bfd_sym_from_r_symndx (struct sym_cache *cache,
1940 unsigned long r_symndx)
1942 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1944 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1946 Elf_Internal_Shdr *symtab_hdr;
1947 unsigned char esym[sizeof (Elf64_External_Sym)];
1948 Elf_External_Sym_Shndx eshndx;
1950 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1951 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1952 &cache->sym[ent], esym, &eshndx) == NULL)
1955 if (cache->abfd != abfd)
1957 memset (cache->indx, -1, sizeof (cache->indx));
1960 cache->indx[ent] = r_symndx;
1963 return &cache->sym[ent];
1966 /* Given an ELF section number, retrieve the corresponding BFD
1970 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
1972 if (sec_index >= elf_numsections (abfd))
1974 return elf_elfsections (abfd)[sec_index]->bfd_section;
1977 static const struct bfd_elf_special_section special_sections_b[] =
1979 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1980 { NULL, 0, 0, 0, 0 }
1983 static const struct bfd_elf_special_section special_sections_c[] =
1985 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1986 { NULL, 0, 0, 0, 0 }
1989 static const struct bfd_elf_special_section special_sections_d[] =
1991 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1992 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1993 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
1994 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
1995 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
1996 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
1997 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
1998 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
1999 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2000 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2001 { NULL, 0, 0, 0, 0 }
2004 static const struct bfd_elf_special_section special_sections_f[] =
2006 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2007 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2008 { NULL, 0, 0, 0, 0 }
2011 static const struct bfd_elf_special_section special_sections_g[] =
2013 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2014 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2015 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2016 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2017 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2018 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2019 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2020 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2021 { NULL, 0, 0, 0, 0 }
2024 static const struct bfd_elf_special_section special_sections_h[] =
2026 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2027 { NULL, 0, 0, 0, 0 }
2030 static const struct bfd_elf_special_section special_sections_i[] =
2032 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2033 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2034 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2035 { NULL, 0, 0, 0, 0 }
2038 static const struct bfd_elf_special_section special_sections_l[] =
2040 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2041 { NULL, 0, 0, 0, 0 }
2044 static const struct bfd_elf_special_section special_sections_n[] =
2046 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2047 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2048 { NULL, 0, 0, 0, 0 }
2051 static const struct bfd_elf_special_section special_sections_p[] =
2053 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2054 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2055 { NULL, 0, 0, 0, 0 }
2058 static const struct bfd_elf_special_section special_sections_r[] =
2060 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2061 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2062 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2063 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2064 { NULL, 0, 0, 0, 0 }
2067 static const struct bfd_elf_special_section special_sections_s[] =
2069 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2070 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2071 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2072 /* See struct bfd_elf_special_section declaration for the semantics of
2073 this special case where .prefix_length != strlen (.prefix). */
2074 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2075 { NULL, 0, 0, 0, 0 }
2078 static const struct bfd_elf_special_section special_sections_t[] =
2080 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2081 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2082 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2083 { NULL, 0, 0, 0, 0 }
2086 static const struct bfd_elf_special_section special_sections_z[] =
2088 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2089 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2090 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2091 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2092 { NULL, 0, 0, 0, 0 }
2095 static const struct bfd_elf_special_section *special_sections[] =
2097 special_sections_b, /* 'b' */
2098 special_sections_c, /* 'c' */
2099 special_sections_d, /* 'd' */
2101 special_sections_f, /* 'f' */
2102 special_sections_g, /* 'g' */
2103 special_sections_h, /* 'h' */
2104 special_sections_i, /* 'i' */
2107 special_sections_l, /* 'l' */
2109 special_sections_n, /* 'n' */
2111 special_sections_p, /* 'p' */
2113 special_sections_r, /* 'r' */
2114 special_sections_s, /* 's' */
2115 special_sections_t, /* 't' */
2121 special_sections_z /* 'z' */
2124 const struct bfd_elf_special_section *
2125 _bfd_elf_get_special_section (const char *name,
2126 const struct bfd_elf_special_section *spec,
2132 len = strlen (name);
2134 for (i = 0; spec[i].prefix != NULL; i++)
2137 int prefix_len = spec[i].prefix_length;
2139 if (len < prefix_len)
2141 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2144 suffix_len = spec[i].suffix_length;
2145 if (suffix_len <= 0)
2147 if (name[prefix_len] != 0)
2149 if (suffix_len == 0)
2151 if (name[prefix_len] != '.'
2152 && (suffix_len == -2
2153 || (rela && spec[i].type == SHT_REL)))
2159 if (len < prefix_len + suffix_len)
2161 if (memcmp (name + len - suffix_len,
2162 spec[i].prefix + prefix_len,
2172 const struct bfd_elf_special_section *
2173 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2176 const struct bfd_elf_special_section *spec;
2177 const struct elf_backend_data *bed;
2179 /* See if this is one of the special sections. */
2180 if (sec->name == NULL)
2183 bed = get_elf_backend_data (abfd);
2184 spec = bed->special_sections;
2187 spec = _bfd_elf_get_special_section (sec->name,
2188 bed->special_sections,
2194 if (sec->name[0] != '.')
2197 i = sec->name[1] - 'b';
2198 if (i < 0 || i > 'z' - 'b')
2201 spec = special_sections[i];
2206 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2210 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2212 struct bfd_elf_section_data *sdata;
2213 const struct elf_backend_data *bed;
2214 const struct bfd_elf_special_section *ssect;
2216 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2219 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2223 sec->used_by_bfd = sdata;
2226 /* Indicate whether or not this section should use RELA relocations. */
2227 bed = get_elf_backend_data (abfd);
2228 sec->use_rela_p = bed->default_use_rela_p;
2230 /* When we read a file, we don't need to set ELF section type and
2231 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2232 anyway. We will set ELF section type and flags for all linker
2233 created sections. If user specifies BFD section flags, we will
2234 set ELF section type and flags based on BFD section flags in
2235 elf_fake_sections. */
2236 if ((!sec->flags && abfd->direction != read_direction)
2237 || (sec->flags & SEC_LINKER_CREATED) != 0)
2239 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2242 elf_section_type (sec) = ssect->type;
2243 elf_section_flags (sec) = ssect->attr;
2247 return _bfd_generic_new_section_hook (abfd, sec);
2250 /* Create a new bfd section from an ELF program header.
2252 Since program segments have no names, we generate a synthetic name
2253 of the form segment<NUM>, where NUM is generally the index in the
2254 program header table. For segments that are split (see below) we
2255 generate the names segment<NUM>a and segment<NUM>b.
2257 Note that some program segments may have a file size that is different than
2258 (less than) the memory size. All this means is that at execution the
2259 system must allocate the amount of memory specified by the memory size,
2260 but only initialize it with the first "file size" bytes read from the
2261 file. This would occur for example, with program segments consisting
2262 of combined data+bss.
2264 To handle the above situation, this routine generates TWO bfd sections
2265 for the single program segment. The first has the length specified by
2266 the file size of the segment, and the second has the length specified
2267 by the difference between the two sizes. In effect, the segment is split
2268 into its initialized and uninitialized parts.
2273 _bfd_elf_make_section_from_phdr (bfd *abfd,
2274 Elf_Internal_Phdr *hdr,
2276 const char *type_name)
2284 split = ((hdr->p_memsz > 0)
2285 && (hdr->p_filesz > 0)
2286 && (hdr->p_memsz > hdr->p_filesz));
2288 if (hdr->p_filesz > 0)
2290 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2291 len = strlen (namebuf) + 1;
2292 name = (char *) bfd_alloc (abfd, len);
2295 memcpy (name, namebuf, len);
2296 newsect = bfd_make_section (abfd, name);
2297 if (newsect == NULL)
2299 newsect->vma = hdr->p_vaddr;
2300 newsect->lma = hdr->p_paddr;
2301 newsect->size = hdr->p_filesz;
2302 newsect->filepos = hdr->p_offset;
2303 newsect->flags |= SEC_HAS_CONTENTS;
2304 newsect->alignment_power = bfd_log2 (hdr->p_align);
2305 if (hdr->p_type == PT_LOAD)
2307 newsect->flags |= SEC_ALLOC;
2308 newsect->flags |= SEC_LOAD;
2309 if (hdr->p_flags & PF_X)
2311 /* FIXME: all we known is that it has execute PERMISSION,
2313 newsect->flags |= SEC_CODE;
2316 if (!(hdr->p_flags & PF_W))
2318 newsect->flags |= SEC_READONLY;
2322 if (hdr->p_memsz > hdr->p_filesz)
2326 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2327 len = strlen (namebuf) + 1;
2328 name = (char *) bfd_alloc (abfd, len);
2331 memcpy (name, namebuf, len);
2332 newsect = bfd_make_section (abfd, name);
2333 if (newsect == NULL)
2335 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2336 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2337 newsect->size = hdr->p_memsz - hdr->p_filesz;
2338 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2339 align = newsect->vma & -newsect->vma;
2340 if (align == 0 || align > hdr->p_align)
2341 align = hdr->p_align;
2342 newsect->alignment_power = bfd_log2 (align);
2343 if (hdr->p_type == PT_LOAD)
2345 /* Hack for gdb. Segments that have not been modified do
2346 not have their contents written to a core file, on the
2347 assumption that a debugger can find the contents in the
2348 executable. We flag this case by setting the fake
2349 section size to zero. Note that "real" bss sections will
2350 always have their contents dumped to the core file. */
2351 if (bfd_get_format (abfd) == bfd_core)
2353 newsect->flags |= SEC_ALLOC;
2354 if (hdr->p_flags & PF_X)
2355 newsect->flags |= SEC_CODE;
2357 if (!(hdr->p_flags & PF_W))
2358 newsect->flags |= SEC_READONLY;
2365 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2367 const struct elf_backend_data *bed;
2369 switch (hdr->p_type)
2372 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2375 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2378 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2381 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2384 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2386 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2391 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2394 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2396 case PT_GNU_EH_FRAME:
2397 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2401 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2404 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2407 /* Check for any processor-specific program segment types. */
2408 bed = get_elf_backend_data (abfd);
2409 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2413 /* Initialize REL_HDR, the section-header for new section, containing
2414 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2415 relocations; otherwise, we use REL relocations. */
2418 _bfd_elf_init_reloc_shdr (bfd *abfd,
2419 Elf_Internal_Shdr *rel_hdr,
2421 bfd_boolean use_rela_p)
2424 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2425 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2427 name = (char *) bfd_alloc (abfd, amt);
2430 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2432 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2434 if (rel_hdr->sh_name == (unsigned int) -1)
2436 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2437 rel_hdr->sh_entsize = (use_rela_p
2438 ? bed->s->sizeof_rela
2439 : bed->s->sizeof_rel);
2440 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2441 rel_hdr->sh_flags = 0;
2442 rel_hdr->sh_addr = 0;
2443 rel_hdr->sh_size = 0;
2444 rel_hdr->sh_offset = 0;
2449 /* Return the default section type based on the passed in section flags. */
2452 bfd_elf_get_default_section_type (flagword flags)
2454 if ((flags & SEC_ALLOC) != 0
2455 && ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0
2456 || (flags & SEC_NEVER_LOAD) != 0))
2458 return SHT_PROGBITS;
2461 /* Set up an ELF internal section header for a section. */
2464 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2466 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2467 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2468 Elf_Internal_Shdr *this_hdr;
2469 unsigned int sh_type;
2473 /* We already failed; just get out of the bfd_map_over_sections
2478 this_hdr = &elf_section_data (asect)->this_hdr;
2480 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2481 asect->name, FALSE);
2482 if (this_hdr->sh_name == (unsigned int) -1)
2488 /* Don't clear sh_flags. Assembler may set additional bits. */
2490 if ((asect->flags & SEC_ALLOC) != 0
2491 || asect->user_set_vma)
2492 this_hdr->sh_addr = asect->vma;
2494 this_hdr->sh_addr = 0;
2496 this_hdr->sh_offset = 0;
2497 this_hdr->sh_size = asect->size;
2498 this_hdr->sh_link = 0;
2499 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2500 /* The sh_entsize and sh_info fields may have been set already by
2501 copy_private_section_data. */
2503 this_hdr->bfd_section = asect;
2504 this_hdr->contents = NULL;
2506 /* If the section type is unspecified, we set it based on
2508 if ((asect->flags & SEC_GROUP) != 0)
2509 sh_type = SHT_GROUP;
2511 sh_type = bfd_elf_get_default_section_type (asect->flags);
2513 if (this_hdr->sh_type == SHT_NULL)
2514 this_hdr->sh_type = sh_type;
2515 else if (this_hdr->sh_type == SHT_NOBITS
2516 && sh_type == SHT_PROGBITS
2517 && (asect->flags & SEC_ALLOC) != 0)
2519 /* Warn if we are changing a NOBITS section to PROGBITS, but
2520 allow the link to proceed. This can happen when users link
2521 non-bss input sections to bss output sections, or emit data
2522 to a bss output section via a linker script. */
2523 (*_bfd_error_handler)
2524 (_("warning: section `%A' type changed to PROGBITS"), asect);
2525 this_hdr->sh_type = sh_type;
2528 switch (this_hdr->sh_type)
2534 case SHT_INIT_ARRAY:
2535 case SHT_FINI_ARRAY:
2536 case SHT_PREINIT_ARRAY:
2543 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2547 this_hdr->sh_entsize = bed->s->sizeof_sym;
2551 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2555 if (get_elf_backend_data (abfd)->may_use_rela_p)
2556 this_hdr->sh_entsize = bed->s->sizeof_rela;
2560 if (get_elf_backend_data (abfd)->may_use_rel_p)
2561 this_hdr->sh_entsize = bed->s->sizeof_rel;
2564 case SHT_GNU_versym:
2565 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2568 case SHT_GNU_verdef:
2569 this_hdr->sh_entsize = 0;
2570 /* objcopy or strip will copy over sh_info, but may not set
2571 cverdefs. The linker will set cverdefs, but sh_info will be
2573 if (this_hdr->sh_info == 0)
2574 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2576 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2577 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2580 case SHT_GNU_verneed:
2581 this_hdr->sh_entsize = 0;
2582 /* objcopy or strip will copy over sh_info, but may not set
2583 cverrefs. The linker will set cverrefs, but sh_info will be
2585 if (this_hdr->sh_info == 0)
2586 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2588 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2589 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2593 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2597 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2601 if ((asect->flags & SEC_ALLOC) != 0)
2602 this_hdr->sh_flags |= SHF_ALLOC;
2603 if ((asect->flags & SEC_READONLY) == 0)
2604 this_hdr->sh_flags |= SHF_WRITE;
2605 if ((asect->flags & SEC_CODE) != 0)
2606 this_hdr->sh_flags |= SHF_EXECINSTR;
2607 if ((asect->flags & SEC_MERGE) != 0)
2609 this_hdr->sh_flags |= SHF_MERGE;
2610 this_hdr->sh_entsize = asect->entsize;
2611 if ((asect->flags & SEC_STRINGS) != 0)
2612 this_hdr->sh_flags |= SHF_STRINGS;
2614 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2615 this_hdr->sh_flags |= SHF_GROUP;
2616 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2618 this_hdr->sh_flags |= SHF_TLS;
2619 if (asect->size == 0
2620 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2622 struct bfd_link_order *o = asect->map_tail.link_order;
2624 this_hdr->sh_size = 0;
2627 this_hdr->sh_size = o->offset + o->size;
2628 if (this_hdr->sh_size != 0)
2629 this_hdr->sh_type = SHT_NOBITS;
2633 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2634 this_hdr->sh_flags |= SHF_EXCLUDE;
2636 /* Check for processor-specific section types. */
2637 sh_type = this_hdr->sh_type;
2638 if (bed->elf_backend_fake_sections
2639 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2642 if (sh_type == SHT_NOBITS && asect->size != 0)
2644 /* Don't change the header type from NOBITS if we are being
2645 called for objcopy --only-keep-debug. */
2646 this_hdr->sh_type = sh_type;
2649 /* If the section has relocs, set up a section header for the
2650 SHT_REL[A] section. If two relocation sections are required for
2651 this section, it is up to the processor-specific back-end to
2652 create the other. */
2653 if ((asect->flags & SEC_RELOC) != 0
2654 && !_bfd_elf_init_reloc_shdr (abfd,
2655 &elf_section_data (asect)->rel_hdr,
2661 /* Fill in the contents of a SHT_GROUP section. Called from
2662 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2663 when ELF targets use the generic linker, ld. Called for ld -r
2664 from bfd_elf_final_link. */
2667 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2669 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2670 asection *elt, *first;
2674 /* Ignore linker created group section. See elfNN_ia64_object_p in
2676 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2680 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2682 unsigned long symindx = 0;
2684 /* elf_group_id will have been set up by objcopy and the
2686 if (elf_group_id (sec) != NULL)
2687 symindx = elf_group_id (sec)->udata.i;
2691 /* If called from the assembler, swap_out_syms will have set up
2692 elf_section_syms. */
2693 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2694 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2696 elf_section_data (sec)->this_hdr.sh_info = symindx;
2698 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2700 /* The ELF backend linker sets sh_info to -2 when the group
2701 signature symbol is global, and thus the index can't be
2702 set until all local symbols are output. */
2703 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2704 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2705 unsigned long symndx = sec_data->this_hdr.sh_info;
2706 unsigned long extsymoff = 0;
2707 struct elf_link_hash_entry *h;
2709 if (!elf_bad_symtab (igroup->owner))
2711 Elf_Internal_Shdr *symtab_hdr;
2713 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2714 extsymoff = symtab_hdr->sh_info;
2716 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2717 while (h->root.type == bfd_link_hash_indirect
2718 || h->root.type == bfd_link_hash_warning)
2719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2721 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2724 /* The contents won't be allocated for "ld -r" or objcopy. */
2726 if (sec->contents == NULL)
2729 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2731 /* Arrange for the section to be written out. */
2732 elf_section_data (sec)->this_hdr.contents = sec->contents;
2733 if (sec->contents == NULL)
2740 loc = sec->contents + sec->size;
2742 /* Get the pointer to the first section in the group that gas
2743 squirreled away here. objcopy arranges for this to be set to the
2744 start of the input section group. */
2745 first = elt = elf_next_in_group (sec);
2747 /* First element is a flag word. Rest of section is elf section
2748 indices for all the sections of the group. Write them backwards
2749 just to keep the group in the same order as given in .section
2750 directives, not that it matters. */
2757 s = s->output_section;
2759 && !bfd_is_abs_section (s))
2761 unsigned int idx = elf_section_data (s)->this_idx;
2764 H_PUT_32 (abfd, idx, loc);
2766 elt = elf_next_in_group (elt);
2771 if ((loc -= 4) != sec->contents)
2774 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2777 /* Assign all ELF section numbers. The dummy first section is handled here
2778 too. The link/info pointers for the standard section types are filled
2779 in here too, while we're at it. */
2782 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2784 struct elf_obj_tdata *t = elf_tdata (abfd);
2786 unsigned int section_number, secn;
2787 Elf_Internal_Shdr **i_shdrp;
2788 struct bfd_elf_section_data *d;
2789 bfd_boolean need_symtab;
2793 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2795 /* SHT_GROUP sections are in relocatable files only. */
2796 if (link_info == NULL || link_info->relocatable)
2798 /* Put SHT_GROUP sections first. */
2799 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2801 d = elf_section_data (sec);
2803 if (d->this_hdr.sh_type == SHT_GROUP)
2805 if (sec->flags & SEC_LINKER_CREATED)
2807 /* Remove the linker created SHT_GROUP sections. */
2808 bfd_section_list_remove (abfd, sec);
2809 abfd->section_count--;
2812 d->this_idx = section_number++;
2817 for (sec = abfd->sections; sec; sec = sec->next)
2819 d = elf_section_data (sec);
2821 if (d->this_hdr.sh_type != SHT_GROUP)
2822 d->this_idx = section_number++;
2823 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2824 if ((sec->flags & SEC_RELOC) == 0)
2828 d->rel_idx = section_number++;
2829 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2834 d->rel_idx2 = section_number++;
2835 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2841 t->shstrtab_section = section_number++;
2842 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2843 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2845 need_symtab = (bfd_get_symcount (abfd) > 0
2846 || (link_info == NULL
2847 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2851 t->symtab_section = section_number++;
2852 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2853 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2855 t->symtab_shndx_section = section_number++;
2856 t->symtab_shndx_hdr.sh_name
2857 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2858 ".symtab_shndx", FALSE);
2859 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2862 t->strtab_section = section_number++;
2863 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2866 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2867 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2869 elf_numsections (abfd) = section_number;
2870 elf_elfheader (abfd)->e_shnum = section_number;
2872 /* Set up the list of section header pointers, in agreement with the
2874 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
2875 sizeof (Elf_Internal_Shdr *));
2876 if (i_shdrp == NULL)
2879 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
2880 sizeof (Elf_Internal_Shdr));
2881 if (i_shdrp[0] == NULL)
2883 bfd_release (abfd, i_shdrp);
2887 elf_elfsections (abfd) = i_shdrp;
2889 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2892 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2893 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2895 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2896 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2898 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2899 t->symtab_hdr.sh_link = t->strtab_section;
2902 for (sec = abfd->sections; sec; sec = sec->next)
2907 d = elf_section_data (sec);
2909 i_shdrp[d->this_idx] = &d->this_hdr;
2910 if (d->rel_idx != 0)
2911 i_shdrp[d->rel_idx] = &d->rel_hdr;
2912 if (d->rel_idx2 != 0)
2913 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2915 /* Fill in the sh_link and sh_info fields while we're at it. */
2917 /* sh_link of a reloc section is the section index of the symbol
2918 table. sh_info is the section index of the section to which
2919 the relocation entries apply. */
2920 if (d->rel_idx != 0)
2922 d->rel_hdr.sh_link = t->symtab_section;
2923 d->rel_hdr.sh_info = d->this_idx;
2925 if (d->rel_idx2 != 0)
2927 d->rel_hdr2->sh_link = t->symtab_section;
2928 d->rel_hdr2->sh_info = d->this_idx;
2931 /* We need to set up sh_link for SHF_LINK_ORDER. */
2932 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2934 s = elf_linked_to_section (sec);
2937 /* elf_linked_to_section points to the input section. */
2938 if (link_info != NULL)
2940 /* Check discarded linkonce section. */
2941 if (elf_discarded_section (s))
2944 (*_bfd_error_handler)
2945 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2946 abfd, d->this_hdr.bfd_section,
2948 /* Point to the kept section if it has the same
2949 size as the discarded one. */
2950 kept = _bfd_elf_check_kept_section (s, link_info);
2953 bfd_set_error (bfd_error_bad_value);
2959 s = s->output_section;
2960 BFD_ASSERT (s != NULL);
2964 /* Handle objcopy. */
2965 if (s->output_section == NULL)
2967 (*_bfd_error_handler)
2968 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2969 abfd, d->this_hdr.bfd_section, s, s->owner);
2970 bfd_set_error (bfd_error_bad_value);
2973 s = s->output_section;
2975 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2980 The Intel C compiler generates SHT_IA_64_UNWIND with
2981 SHF_LINK_ORDER. But it doesn't set the sh_link or
2982 sh_info fields. Hence we could get the situation
2984 const struct elf_backend_data *bed
2985 = get_elf_backend_data (abfd);
2986 if (bed->link_order_error_handler)
2987 bed->link_order_error_handler
2988 (_("%B: warning: sh_link not set for section `%A'"),
2993 switch (d->this_hdr.sh_type)
2997 /* A reloc section which we are treating as a normal BFD
2998 section. sh_link is the section index of the symbol
2999 table. sh_info is the section index of the section to
3000 which the relocation entries apply. We assume that an
3001 allocated reloc section uses the dynamic symbol table.
3002 FIXME: How can we be sure? */
3003 s = bfd_get_section_by_name (abfd, ".dynsym");
3005 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3007 /* We look up the section the relocs apply to by name. */
3009 if (d->this_hdr.sh_type == SHT_REL)
3013 s = bfd_get_section_by_name (abfd, name);
3015 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3019 /* We assume that a section named .stab*str is a stabs
3020 string section. We look for a section with the same name
3021 but without the trailing ``str'', and set its sh_link
3022 field to point to this section. */
3023 if (CONST_STRNEQ (sec->name, ".stab")
3024 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3029 len = strlen (sec->name);
3030 alc = (char *) bfd_malloc (len - 2);
3033 memcpy (alc, sec->name, len - 3);
3034 alc[len - 3] = '\0';
3035 s = bfd_get_section_by_name (abfd, alc);
3039 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3041 /* This is a .stab section. */
3042 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3043 elf_section_data (s)->this_hdr.sh_entsize
3044 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3051 case SHT_GNU_verneed:
3052 case SHT_GNU_verdef:
3053 /* sh_link is the section header index of the string table
3054 used for the dynamic entries, or the symbol table, or the
3056 s = bfd_get_section_by_name (abfd, ".dynstr");
3058 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3061 case SHT_GNU_LIBLIST:
3062 /* sh_link is the section header index of the prelink library
3063 list used for the dynamic entries, or the symbol table, or
3064 the version strings. */
3065 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3066 ? ".dynstr" : ".gnu.libstr");
3068 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3073 case SHT_GNU_versym:
3074 /* sh_link is the section header index of the symbol table
3075 this hash table or version table is for. */
3076 s = bfd_get_section_by_name (abfd, ".dynsym");
3078 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3082 d->this_hdr.sh_link = t->symtab_section;
3086 for (secn = 1; secn < section_number; ++secn)
3087 if (i_shdrp[secn] == NULL)
3088 i_shdrp[secn] = i_shdrp[0];
3090 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3091 i_shdrp[secn]->sh_name);
3095 /* Map symbol from it's internal number to the external number, moving
3096 all local symbols to be at the head of the list. */
3099 sym_is_global (bfd *abfd, asymbol *sym)
3101 /* If the backend has a special mapping, use it. */
3102 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3103 if (bed->elf_backend_sym_is_global)
3104 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3106 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3107 || bfd_is_und_section (bfd_get_section (sym))
3108 || bfd_is_com_section (bfd_get_section (sym)));
3111 /* Don't output section symbols for sections that are not going to be
3115 ignore_section_sym (bfd *abfd, asymbol *sym)
3117 return ((sym->flags & BSF_SECTION_SYM) != 0
3118 && !(sym->section->owner == abfd
3119 || (sym->section->output_section->owner == abfd
3120 && sym->section->output_offset == 0)));
3124 elf_map_symbols (bfd *abfd)
3126 unsigned int symcount = bfd_get_symcount (abfd);
3127 asymbol **syms = bfd_get_outsymbols (abfd);
3128 asymbol **sect_syms;
3129 unsigned int num_locals = 0;
3130 unsigned int num_globals = 0;
3131 unsigned int num_locals2 = 0;
3132 unsigned int num_globals2 = 0;
3139 fprintf (stderr, "elf_map_symbols\n");
3143 for (asect = abfd->sections; asect; asect = asect->next)
3145 if (max_index < asect->index)
3146 max_index = asect->index;
3150 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3151 if (sect_syms == NULL)
3153 elf_section_syms (abfd) = sect_syms;
3154 elf_num_section_syms (abfd) = max_index;
3156 /* Init sect_syms entries for any section symbols we have already
3157 decided to output. */
3158 for (idx = 0; idx < symcount; idx++)
3160 asymbol *sym = syms[idx];
3162 if ((sym->flags & BSF_SECTION_SYM) != 0
3164 && !ignore_section_sym (abfd, sym))
3166 asection *sec = sym->section;
3168 if (sec->owner != abfd)
3169 sec = sec->output_section;
3171 sect_syms[sec->index] = syms[idx];
3175 /* Classify all of the symbols. */
3176 for (idx = 0; idx < symcount; idx++)
3178 if (ignore_section_sym (abfd, syms[idx]))
3180 if (!sym_is_global (abfd, syms[idx]))
3186 /* We will be adding a section symbol for each normal BFD section. Most
3187 sections will already have a section symbol in outsymbols, but
3188 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3189 at least in that case. */
3190 for (asect = abfd->sections; asect; asect = asect->next)
3192 if (sect_syms[asect->index] == NULL)
3194 if (!sym_is_global (abfd, asect->symbol))
3201 /* Now sort the symbols so the local symbols are first. */
3202 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3203 sizeof (asymbol *));
3205 if (new_syms == NULL)
3208 for (idx = 0; idx < symcount; idx++)
3210 asymbol *sym = syms[idx];
3213 if (ignore_section_sym (abfd, sym))
3215 if (!sym_is_global (abfd, sym))
3218 i = num_locals + num_globals2++;
3220 sym->udata.i = i + 1;
3222 for (asect = abfd->sections; asect; asect = asect->next)
3224 if (sect_syms[asect->index] == NULL)
3226 asymbol *sym = asect->symbol;
3229 sect_syms[asect->index] = sym;
3230 if (!sym_is_global (abfd, sym))
3233 i = num_locals + num_globals2++;
3235 sym->udata.i = i + 1;
3239 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3241 elf_num_locals (abfd) = num_locals;
3242 elf_num_globals (abfd) = num_globals;
3246 /* Align to the maximum file alignment that could be required for any
3247 ELF data structure. */
3249 static inline file_ptr
3250 align_file_position (file_ptr off, int align)
3252 return (off + align - 1) & ~(align - 1);
3255 /* Assign a file position to a section, optionally aligning to the
3256 required section alignment. */
3259 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3263 if (align && i_shdrp->sh_addralign > 1)
3264 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3265 i_shdrp->sh_offset = offset;
3266 if (i_shdrp->bfd_section != NULL)
3267 i_shdrp->bfd_section->filepos = offset;
3268 if (i_shdrp->sh_type != SHT_NOBITS)
3269 offset += i_shdrp->sh_size;
3273 /* Compute the file positions we are going to put the sections at, and
3274 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3275 is not NULL, this is being called by the ELF backend linker. */
3278 _bfd_elf_compute_section_file_positions (bfd *abfd,
3279 struct bfd_link_info *link_info)
3281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3283 struct bfd_strtab_hash *strtab = NULL;
3284 Elf_Internal_Shdr *shstrtab_hdr;
3285 bfd_boolean need_symtab;
3287 if (abfd->output_has_begun)
3290 /* Do any elf backend specific processing first. */
3291 if (bed->elf_backend_begin_write_processing)
3292 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3294 if (! prep_headers (abfd))
3297 /* Post process the headers if necessary. */
3298 if (bed->elf_backend_post_process_headers)
3299 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3302 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3306 if (!assign_section_numbers (abfd, link_info))
3309 /* The backend linker builds symbol table information itself. */
3310 need_symtab = (link_info == NULL
3311 && (bfd_get_symcount (abfd) > 0
3312 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3316 /* Non-zero if doing a relocatable link. */
3317 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3319 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3323 if (link_info == NULL)
3325 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3330 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3331 /* sh_name was set in prep_headers. */
3332 shstrtab_hdr->sh_type = SHT_STRTAB;
3333 shstrtab_hdr->sh_flags = 0;
3334 shstrtab_hdr->sh_addr = 0;
3335 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3336 shstrtab_hdr->sh_entsize = 0;
3337 shstrtab_hdr->sh_link = 0;
3338 shstrtab_hdr->sh_info = 0;
3339 /* sh_offset is set in assign_file_positions_except_relocs. */
3340 shstrtab_hdr->sh_addralign = 1;
3342 if (!assign_file_positions_except_relocs (abfd, link_info))
3348 Elf_Internal_Shdr *hdr;
3350 off = elf_tdata (abfd)->next_file_pos;
3352 hdr = &elf_tdata (abfd)->symtab_hdr;
3353 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3355 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3356 if (hdr->sh_size != 0)
3357 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3359 hdr = &elf_tdata (abfd)->strtab_hdr;
3360 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3362 elf_tdata (abfd)->next_file_pos = off;
3364 /* Now that we know where the .strtab section goes, write it
3366 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3367 || ! _bfd_stringtab_emit (abfd, strtab))
3369 _bfd_stringtab_free (strtab);
3372 abfd->output_has_begun = TRUE;
3377 /* Make an initial estimate of the size of the program header. If we
3378 get the number wrong here, we'll redo section placement. */
3380 static bfd_size_type
3381 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3385 const struct elf_backend_data *bed;
3387 /* Assume we will need exactly two PT_LOAD segments: one for text
3388 and one for data. */
3391 s = bfd_get_section_by_name (abfd, ".interp");
3392 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3394 /* If we have a loadable interpreter section, we need a
3395 PT_INTERP segment. In this case, assume we also need a
3396 PT_PHDR segment, although that may not be true for all
3401 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3403 /* We need a PT_DYNAMIC segment. */
3407 if (info != NULL && info->relro)
3409 /* We need a PT_GNU_RELRO segment. */
3413 if (elf_tdata (abfd)->eh_frame_hdr)
3415 /* We need a PT_GNU_EH_FRAME segment. */
3419 if (elf_tdata (abfd)->stack_flags)
3421 /* We need a PT_GNU_STACK segment. */
3425 for (s = abfd->sections; s != NULL; s = s->next)
3427 if ((s->flags & SEC_LOAD) != 0
3428 && CONST_STRNEQ (s->name, ".note"))
3430 /* We need a PT_NOTE segment. */
3432 /* Try to create just one PT_NOTE segment
3433 for all adjacent loadable .note* sections.
3434 gABI requires that within a PT_NOTE segment
3435 (and also inside of each SHT_NOTE section)
3436 each note is padded to a multiple of 4 size,
3437 so we check whether the sections are correctly
3439 if (s->alignment_power == 2)
3440 while (s->next != NULL
3441 && s->next->alignment_power == 2
3442 && (s->next->flags & SEC_LOAD) != 0
3443 && CONST_STRNEQ (s->next->name, ".note"))
3448 for (s = abfd->sections; s != NULL; s = s->next)
3450 if (s->flags & SEC_THREAD_LOCAL)
3452 /* We need a PT_TLS segment. */
3458 /* Let the backend count up any program headers it might need. */
3459 bed = get_elf_backend_data (abfd);
3460 if (bed->elf_backend_additional_program_headers)
3464 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3470 return segs * bed->s->sizeof_phdr;
3473 /* Find the segment that contains the output_section of section. */
3476 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3478 struct elf_segment_map *m;
3479 Elf_Internal_Phdr *p;
3481 for (m = elf_tdata (abfd)->segment_map,
3482 p = elf_tdata (abfd)->phdr;
3488 for (i = m->count - 1; i >= 0; i--)
3489 if (m->sections[i] == section)
3496 /* Create a mapping from a set of sections to a program segment. */
3498 static struct elf_segment_map *
3499 make_mapping (bfd *abfd,
3500 asection **sections,
3505 struct elf_segment_map *m;
3510 amt = sizeof (struct elf_segment_map);
3511 amt += (to - from - 1) * sizeof (asection *);
3512 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3516 m->p_type = PT_LOAD;
3517 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3518 m->sections[i - from] = *hdrpp;
3519 m->count = to - from;
3521 if (from == 0 && phdr)
3523 /* Include the headers in the first PT_LOAD segment. */
3524 m->includes_filehdr = 1;
3525 m->includes_phdrs = 1;
3531 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3534 struct elf_segment_map *
3535 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3537 struct elf_segment_map *m;
3539 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3540 sizeof (struct elf_segment_map));
3544 m->p_type = PT_DYNAMIC;
3546 m->sections[0] = dynsec;
3551 /* Possibly add or remove segments from the segment map. */
3554 elf_modify_segment_map (bfd *abfd,
3555 struct bfd_link_info *info,
3556 bfd_boolean remove_empty_load)
3558 struct elf_segment_map **m;
3559 const struct elf_backend_data *bed;
3561 /* The placement algorithm assumes that non allocated sections are
3562 not in PT_LOAD segments. We ensure this here by removing such
3563 sections from the segment map. We also remove excluded
3564 sections. Finally, any PT_LOAD segment without sections is
3566 m = &elf_tdata (abfd)->segment_map;
3569 unsigned int i, new_count;
3571 for (new_count = 0, i = 0; i < (*m)->count; i++)
3573 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3574 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3575 || (*m)->p_type != PT_LOAD))
3577 (*m)->sections[new_count] = (*m)->sections[i];
3581 (*m)->count = new_count;
3583 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3589 bed = get_elf_backend_data (abfd);
3590 if (bed->elf_backend_modify_segment_map != NULL)
3592 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3599 /* Set up a mapping from BFD sections to program segments. */
3602 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3605 struct elf_segment_map *m;
3606 asection **sections = NULL;
3607 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3608 bfd_boolean no_user_phdrs;
3610 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3611 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3615 struct elf_segment_map *mfirst;
3616 struct elf_segment_map **pm;
3619 unsigned int phdr_index;
3620 bfd_vma maxpagesize;
3622 bfd_boolean phdr_in_segment = TRUE;
3623 bfd_boolean writable;
3625 asection *first_tls = NULL;
3626 asection *dynsec, *eh_frame_hdr;
3628 bfd_vma addr_mask, wrap_to = 0;
3630 /* Select the allocated sections, and sort them. */
3632 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3633 sizeof (asection *));
3634 if (sections == NULL)
3637 /* Calculate top address, avoiding undefined behaviour of shift
3638 left operator when shift count is equal to size of type
3640 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3641 addr_mask = (addr_mask << 1) + 1;
3644 for (s = abfd->sections; s != NULL; s = s->next)
3646 if ((s->flags & SEC_ALLOC) != 0)
3650 /* A wrapping section potentially clashes with header. */
3651 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3652 wrap_to = (s->lma + s->size) & addr_mask;
3655 BFD_ASSERT (i <= bfd_count_sections (abfd));
3658 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3660 /* Build the mapping. */
3665 /* If we have a .interp section, then create a PT_PHDR segment for
3666 the program headers and a PT_INTERP segment for the .interp
3668 s = bfd_get_section_by_name (abfd, ".interp");
3669 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3671 amt = sizeof (struct elf_segment_map);
3672 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3676 m->p_type = PT_PHDR;
3677 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3678 m->p_flags = PF_R | PF_X;
3679 m->p_flags_valid = 1;
3680 m->includes_phdrs = 1;
3685 amt = sizeof (struct elf_segment_map);
3686 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3690 m->p_type = PT_INTERP;
3698 /* Look through the sections. We put sections in the same program
3699 segment when the start of the second section can be placed within
3700 a few bytes of the end of the first section. */
3704 maxpagesize = bed->maxpagesize;
3706 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3708 && (dynsec->flags & SEC_LOAD) == 0)
3711 /* Deal with -Ttext or something similar such that the first section
3712 is not adjacent to the program headers. This is an
3713 approximation, since at this point we don't know exactly how many
3714 program headers we will need. */
3717 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3719 if (phdr_size == (bfd_size_type) -1)
3720 phdr_size = get_program_header_size (abfd, info);
3721 if ((abfd->flags & D_PAGED) == 0
3722 || (sections[0]->lma & addr_mask) < phdr_size
3723 || ((sections[0]->lma & addr_mask) % maxpagesize
3724 < phdr_size % maxpagesize)
3725 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
3726 phdr_in_segment = FALSE;
3729 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3732 bfd_boolean new_segment;
3736 /* See if this section and the last one will fit in the same
3739 if (last_hdr == NULL)
3741 /* If we don't have a segment yet, then we don't need a new
3742 one (we build the last one after this loop). */
3743 new_segment = FALSE;
3745 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3747 /* If this section has a different relation between the
3748 virtual address and the load address, then we need a new
3752 else if (hdr->lma < last_hdr->lma + last_size
3753 || last_hdr->lma + last_size < last_hdr->lma)
3755 /* If this section has a load address that makes it overlap
3756 the previous section, then we need a new segment. */
3759 /* In the next test we have to be careful when last_hdr->lma is close
3760 to the end of the address space. If the aligned address wraps
3761 around to the start of the address space, then there are no more
3762 pages left in memory and it is OK to assume that the current
3763 section can be included in the current segment. */
3764 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3766 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3769 /* If putting this section in this segment would force us to
3770 skip a page in the segment, then we need a new segment. */
3773 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3774 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3776 /* We don't want to put a loadable section after a
3777 nonloadable section in the same segment.
3778 Consider .tbss sections as loadable for this purpose. */
3781 else if ((abfd->flags & D_PAGED) == 0)
3783 /* If the file is not demand paged, which means that we
3784 don't require the sections to be correctly aligned in the
3785 file, then there is no other reason for a new segment. */
3786 new_segment = FALSE;
3789 && (hdr->flags & SEC_READONLY) == 0
3790 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
3791 != (hdr->lma & -maxpagesize)))
3793 /* We don't want to put a writable section in a read only
3794 segment, unless they are on the same page in memory
3795 anyhow. We already know that the last section does not
3796 bring us past the current section on the page, so the
3797 only case in which the new section is not on the same
3798 page as the previous section is when the previous section
3799 ends precisely on a page boundary. */
3804 /* Otherwise, we can use the same segment. */
3805 new_segment = FALSE;
3808 /* Allow interested parties a chance to override our decision. */
3809 if (last_hdr != NULL
3811 && info->callbacks->override_segment_assignment != NULL)
3813 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3819 if ((hdr->flags & SEC_READONLY) == 0)
3822 /* .tbss sections effectively have zero size. */
3823 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3824 != SEC_THREAD_LOCAL)
3825 last_size = hdr->size;
3831 /* We need a new program segment. We must create a new program
3832 header holding all the sections from phdr_index until hdr. */
3834 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3841 if ((hdr->flags & SEC_READONLY) == 0)
3847 /* .tbss sections effectively have zero size. */
3848 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3849 last_size = hdr->size;
3853 phdr_in_segment = FALSE;
3856 /* Create a final PT_LOAD program segment. */
3857 if (last_hdr != NULL)
3859 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3867 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3870 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3877 /* For each batch of consecutive loadable .note sections,
3878 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3879 because if we link together nonloadable .note sections and
3880 loadable .note sections, we will generate two .note sections
3881 in the output file. FIXME: Using names for section types is
3883 for (s = abfd->sections; s != NULL; s = s->next)
3885 if ((s->flags & SEC_LOAD) != 0
3886 && CONST_STRNEQ (s->name, ".note"))
3891 amt = sizeof (struct elf_segment_map);
3892 if (s->alignment_power == 2)
3893 for (s2 = s; s2->next != NULL; s2 = s2->next)
3895 if (s2->next->alignment_power == 2
3896 && (s2->next->flags & SEC_LOAD) != 0
3897 && CONST_STRNEQ (s2->next->name, ".note")
3898 && align_power (s2->lma + s2->size, 2)
3904 amt += (count - 1) * sizeof (asection *);
3905 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3909 m->p_type = PT_NOTE;
3913 m->sections[m->count - count--] = s;
3914 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3917 m->sections[m->count - 1] = s;
3918 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3922 if (s->flags & SEC_THREAD_LOCAL)
3930 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3933 amt = sizeof (struct elf_segment_map);
3934 amt += (tls_count - 1) * sizeof (asection *);
3935 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3940 m->count = tls_count;
3941 /* Mandated PF_R. */
3943 m->p_flags_valid = 1;
3944 for (i = 0; i < (unsigned int) tls_count; ++i)
3946 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3947 m->sections[i] = first_tls;
3948 first_tls = first_tls->next;
3955 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3957 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3958 if (eh_frame_hdr != NULL
3959 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3961 amt = sizeof (struct elf_segment_map);
3962 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3966 m->p_type = PT_GNU_EH_FRAME;
3968 m->sections[0] = eh_frame_hdr->output_section;
3974 if (elf_tdata (abfd)->stack_flags)
3976 amt = sizeof (struct elf_segment_map);
3977 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3981 m->p_type = PT_GNU_STACK;
3982 m->p_flags = elf_tdata (abfd)->stack_flags;
3983 m->p_flags_valid = 1;
3989 if (info != NULL && info->relro)
3991 for (m = mfirst; m != NULL; m = m->next)
3993 if (m->p_type == PT_LOAD)
3995 asection *last = m->sections[m->count - 1];
3996 bfd_vma vaddr = m->sections[0]->vma;
3997 bfd_vma filesz = last->vma - vaddr + last->size;
3999 if (vaddr < info->relro_end
4000 && vaddr >= info->relro_start
4001 && (vaddr + filesz) >= info->relro_end)
4006 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4009 amt = sizeof (struct elf_segment_map);
4010 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4014 m->p_type = PT_GNU_RELRO;
4016 m->p_flags_valid = 1;
4024 elf_tdata (abfd)->segment_map = mfirst;
4027 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4030 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4032 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4037 if (sections != NULL)
4042 /* Sort sections by address. */
4045 elf_sort_sections (const void *arg1, const void *arg2)
4047 const asection *sec1 = *(const asection **) arg1;
4048 const asection *sec2 = *(const asection **) arg2;
4049 bfd_size_type size1, size2;
4051 /* Sort by LMA first, since this is the address used to
4052 place the section into a segment. */
4053 if (sec1->lma < sec2->lma)
4055 else if (sec1->lma > sec2->lma)
4058 /* Then sort by VMA. Normally the LMA and the VMA will be
4059 the same, and this will do nothing. */
4060 if (sec1->vma < sec2->vma)
4062 else if (sec1->vma > sec2->vma)
4065 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4067 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4073 /* If the indicies are the same, do not return 0
4074 here, but continue to try the next comparison. */
4075 if (sec1->target_index - sec2->target_index != 0)
4076 return sec1->target_index - sec2->target_index;
4081 else if (TOEND (sec2))
4086 /* Sort by size, to put zero sized sections
4087 before others at the same address. */
4089 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4090 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4097 return sec1->target_index - sec2->target_index;
4100 /* Ian Lance Taylor writes:
4102 We shouldn't be using % with a negative signed number. That's just
4103 not good. We have to make sure either that the number is not
4104 negative, or that the number has an unsigned type. When the types
4105 are all the same size they wind up as unsigned. When file_ptr is a
4106 larger signed type, the arithmetic winds up as signed long long,
4109 What we're trying to say here is something like ``increase OFF by
4110 the least amount that will cause it to be equal to the VMA modulo
4112 /* In other words, something like:
4114 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4115 off_offset = off % bed->maxpagesize;
4116 if (vma_offset < off_offset)
4117 adjustment = vma_offset + bed->maxpagesize - off_offset;
4119 adjustment = vma_offset - off_offset;
4121 which can can be collapsed into the expression below. */
4124 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4126 return ((vma - off) % maxpagesize);
4130 print_segment_map (const struct elf_segment_map *m)
4133 const char *pt = get_segment_type (m->p_type);
4138 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4139 sprintf (buf, "LOPROC+%7.7x",
4140 (unsigned int) (m->p_type - PT_LOPROC));
4141 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4142 sprintf (buf, "LOOS+%7.7x",
4143 (unsigned int) (m->p_type - PT_LOOS));
4145 snprintf (buf, sizeof (buf), "%8.8x",
4146 (unsigned int) m->p_type);
4149 fprintf (stderr, "%s:", pt);
4150 for (j = 0; j < m->count; j++)
4151 fprintf (stderr, " %s", m->sections [j]->name);
4156 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4161 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4163 buf = bfd_zmalloc (len);
4166 ret = bfd_bwrite (buf, len, abfd) == len;
4171 /* Assign file positions to the sections based on the mapping from
4172 sections to segments. This function also sets up some fields in
4176 assign_file_positions_for_load_sections (bfd *abfd,
4177 struct bfd_link_info *link_info)
4179 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4180 struct elf_segment_map *m;
4181 Elf_Internal_Phdr *phdrs;
4182 Elf_Internal_Phdr *p;
4184 bfd_size_type maxpagesize;
4187 bfd_vma header_pad = 0;
4189 if (link_info == NULL
4190 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4194 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4198 header_pad = m->header_size;
4201 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4202 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4203 elf_elfheader (abfd)->e_phnum = alloc;
4205 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4206 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4208 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4209 >= alloc * bed->s->sizeof_phdr);
4213 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4217 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4218 see assign_file_positions_except_relocs, so make sure we have
4219 that amount allocated, with trailing space cleared.
4220 The variable alloc contains the computed need, while elf_tdata
4221 (abfd)->program_header_size contains the size used for the
4223 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4224 where the layout is forced to according to a larger size in the
4225 last iterations for the testcase ld-elf/header. */
4226 BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4228 phdrs = (Elf_Internal_Phdr *)
4230 (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr),
4231 sizeof (Elf_Internal_Phdr));
4232 elf_tdata (abfd)->phdr = phdrs;
4237 if ((abfd->flags & D_PAGED) != 0)
4238 maxpagesize = bed->maxpagesize;
4240 off = bed->s->sizeof_ehdr;
4241 off += alloc * bed->s->sizeof_phdr;
4242 if (header_pad < (bfd_vma) off)
4248 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4250 m = m->next, p++, j++)
4254 bfd_boolean no_contents;
4256 /* If elf_segment_map is not from map_sections_to_segments, the
4257 sections may not be correctly ordered. NOTE: sorting should
4258 not be done to the PT_NOTE section of a corefile, which may
4259 contain several pseudo-sections artificially created by bfd.
4260 Sorting these pseudo-sections breaks things badly. */
4262 && !(elf_elfheader (abfd)->e_type == ET_CORE
4263 && m->p_type == PT_NOTE))
4264 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4267 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4268 number of sections with contents contributing to both p_filesz
4269 and p_memsz, followed by a number of sections with no contents
4270 that just contribute to p_memsz. In this loop, OFF tracks next
4271 available file offset for PT_LOAD and PT_NOTE segments. */
4272 p->p_type = m->p_type;
4273 p->p_flags = m->p_flags;
4278 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4280 if (m->p_paddr_valid)
4281 p->p_paddr = m->p_paddr;
4282 else if (m->count == 0)
4285 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4287 if (p->p_type == PT_LOAD
4288 && (abfd->flags & D_PAGED) != 0)
4290 /* p_align in demand paged PT_LOAD segments effectively stores
4291 the maximum page size. When copying an executable with
4292 objcopy, we set m->p_align from the input file. Use this
4293 value for maxpagesize rather than bed->maxpagesize, which
4294 may be different. Note that we use maxpagesize for PT_TLS
4295 segment alignment later in this function, so we are relying
4296 on at least one PT_LOAD segment appearing before a PT_TLS
4298 if (m->p_align_valid)
4299 maxpagesize = m->p_align;
4301 p->p_align = maxpagesize;
4303 else if (m->p_align_valid)
4304 p->p_align = m->p_align;
4305 else if (m->count == 0)
4306 p->p_align = 1 << bed->s->log_file_align;
4310 no_contents = FALSE;
4312 if (p->p_type == PT_LOAD
4315 bfd_size_type align;
4316 unsigned int align_power = 0;
4318 if (m->p_align_valid)
4322 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4324 unsigned int secalign;
4326 secalign = bfd_get_section_alignment (abfd, *secpp);
4327 if (secalign > align_power)
4328 align_power = secalign;
4330 align = (bfd_size_type) 1 << align_power;
4331 if (align < maxpagesize)
4332 align = maxpagesize;
4335 for (i = 0; i < m->count; i++)
4336 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4337 /* If we aren't making room for this section, then
4338 it must be SHT_NOBITS regardless of what we've
4339 set via struct bfd_elf_special_section. */
4340 elf_section_type (m->sections[i]) = SHT_NOBITS;
4342 /* Find out whether this segment contains any loadable
4345 for (i = 0; i < m->count; i++)
4346 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4348 no_contents = FALSE;
4352 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4356 /* We shouldn't need to align the segment on disk since
4357 the segment doesn't need file space, but the gABI
4358 arguably requires the alignment and glibc ld.so
4359 checks it. So to comply with the alignment
4360 requirement but not waste file space, we adjust
4361 p_offset for just this segment. (OFF_ADJUST is
4362 subtracted from OFF later.) This may put p_offset
4363 past the end of file, but that shouldn't matter. */
4368 /* Make sure the .dynamic section is the first section in the
4369 PT_DYNAMIC segment. */
4370 else if (p->p_type == PT_DYNAMIC
4372 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4375 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4377 bfd_set_error (bfd_error_bad_value);
4380 /* Set the note section type to SHT_NOTE. */
4381 else if (p->p_type == PT_NOTE)
4382 for (i = 0; i < m->count; i++)
4383 elf_section_type (m->sections[i]) = SHT_NOTE;
4389 if (m->includes_filehdr)
4391 if (!m->p_flags_valid)
4393 p->p_filesz = bed->s->sizeof_ehdr;
4394 p->p_memsz = bed->s->sizeof_ehdr;
4397 BFD_ASSERT (p->p_type == PT_LOAD);
4399 if (p->p_vaddr < (bfd_vma) off)
4401 (*_bfd_error_handler)
4402 (_("%B: Not enough room for program headers, try linking with -N"),
4404 bfd_set_error (bfd_error_bad_value);
4409 if (!m->p_paddr_valid)
4414 if (m->includes_phdrs)
4416 if (!m->p_flags_valid)
4419 if (!m->includes_filehdr)
4421 p->p_offset = bed->s->sizeof_ehdr;
4425 BFD_ASSERT (p->p_type == PT_LOAD);
4426 p->p_vaddr -= off - p->p_offset;
4427 if (!m->p_paddr_valid)
4428 p->p_paddr -= off - p->p_offset;
4432 p->p_filesz += alloc * bed->s->sizeof_phdr;
4433 p->p_memsz += alloc * bed->s->sizeof_phdr;
4436 p->p_filesz += header_pad;
4437 p->p_memsz += header_pad;
4441 if (p->p_type == PT_LOAD
4442 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4444 if (!m->includes_filehdr && !m->includes_phdrs)
4450 adjust = off - (p->p_offset + p->p_filesz);
4452 p->p_filesz += adjust;
4453 p->p_memsz += adjust;
4457 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4458 maps. Set filepos for sections in PT_LOAD segments, and in
4459 core files, for sections in PT_NOTE segments.
4460 assign_file_positions_for_non_load_sections will set filepos
4461 for other sections and update p_filesz for other segments. */
4462 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4465 bfd_size_type align;
4466 Elf_Internal_Shdr *this_hdr;
4469 this_hdr = &elf_section_data (sec)->this_hdr;
4470 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4472 if ((p->p_type == PT_LOAD
4473 || p->p_type == PT_TLS)
4474 && (this_hdr->sh_type != SHT_NOBITS
4475 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4476 && ((this_hdr->sh_flags & SHF_TLS) == 0
4477 || p->p_type == PT_TLS))))
4479 bfd_vma p_start = p->p_paddr;
4480 bfd_vma p_end = p_start + p->p_memsz;
4481 bfd_vma s_start = sec->lma;
4482 bfd_vma adjust = s_start - p_end;
4486 || p_end < p_start))
4488 (*_bfd_error_handler)
4489 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4490 (unsigned long) s_start, (unsigned long) p_end);
4494 p->p_memsz += adjust;
4496 if (this_hdr->sh_type != SHT_NOBITS)
4498 if (p->p_filesz + adjust < p->p_memsz)
4500 /* We have a PROGBITS section following NOBITS ones.
4501 Allocate file space for the NOBITS section(s) and
4503 adjust = p->p_memsz - p->p_filesz;
4504 if (!write_zeros (abfd, off, adjust))
4508 p->p_filesz += adjust;
4512 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4514 /* The section at i == 0 is the one that actually contains
4518 this_hdr->sh_offset = sec->filepos = off;
4519 off += this_hdr->sh_size;
4520 p->p_filesz = this_hdr->sh_size;
4526 /* The rest are fake sections that shouldn't be written. */
4535 if (p->p_type == PT_LOAD)
4537 this_hdr->sh_offset = sec->filepos = off;
4538 if (this_hdr->sh_type != SHT_NOBITS)
4539 off += this_hdr->sh_size;
4542 if (this_hdr->sh_type != SHT_NOBITS)
4544 p->p_filesz += this_hdr->sh_size;
4545 /* A load section without SHF_ALLOC is something like
4546 a note section in a PT_NOTE segment. These take
4547 file space but are not loaded into memory. */
4548 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4549 p->p_memsz += this_hdr->sh_size;
4551 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4553 if (p->p_type == PT_TLS)
4554 p->p_memsz += this_hdr->sh_size;
4556 /* .tbss is special. It doesn't contribute to p_memsz of
4558 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4559 p->p_memsz += this_hdr->sh_size;
4562 if (align > p->p_align
4563 && !m->p_align_valid
4564 && (p->p_type != PT_LOAD
4565 || (abfd->flags & D_PAGED) == 0))
4569 if (!m->p_flags_valid)
4572 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4574 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4580 /* Check that all sections are in a PT_LOAD segment.
4581 Don't check funky gdb generated core files. */
4582 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4584 bfd_boolean check_vma = TRUE;
4586 for (i = 1; i < m->count; i++)
4587 if (m->sections[i]->vma == m->sections[i - 1]->vma
4588 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4589 ->this_hdr), p) != 0
4590 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4591 ->this_hdr), p) != 0)
4593 /* Looks like we have overlays packed into the segment. */
4598 for (i = 0; i < m->count; i++)
4600 Elf_Internal_Shdr *this_hdr;
4603 sec = m->sections[i];
4604 this_hdr = &(elf_section_data(sec)->this_hdr);
4605 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0))
4607 (*_bfd_error_handler)
4608 (_("%B: section `%A' can't be allocated in segment %d"),
4610 print_segment_map (m);
4616 elf_tdata (abfd)->next_file_pos = off;
4620 /* Assign file positions for the other sections. */
4623 assign_file_positions_for_non_load_sections (bfd *abfd,
4624 struct bfd_link_info *link_info)
4626 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4627 Elf_Internal_Shdr **i_shdrpp;
4628 Elf_Internal_Shdr **hdrpp;
4629 Elf_Internal_Phdr *phdrs;
4630 Elf_Internal_Phdr *p;
4631 struct elf_segment_map *m;
4632 bfd_vma filehdr_vaddr, filehdr_paddr;
4633 bfd_vma phdrs_vaddr, phdrs_paddr;
4635 unsigned int num_sec;
4639 i_shdrpp = elf_elfsections (abfd);
4640 num_sec = elf_numsections (abfd);
4641 off = elf_tdata (abfd)->next_file_pos;
4642 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4644 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4645 Elf_Internal_Shdr *hdr;
4648 if (hdr->bfd_section != NULL
4649 && (hdr->bfd_section->filepos != 0
4650 || (hdr->sh_type == SHT_NOBITS
4651 && hdr->contents == NULL)))
4652 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4653 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4655 (*_bfd_error_handler)
4656 (_("%B: warning: allocated section `%s' not in segment"),
4658 (hdr->bfd_section == NULL
4660 : hdr->bfd_section->name));
4661 /* We don't need to page align empty sections. */
4662 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4663 off += vma_page_aligned_bias (hdr->sh_addr, off,
4666 off += vma_page_aligned_bias (hdr->sh_addr, off,
4668 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4671 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4672 && hdr->bfd_section == NULL)
4673 || hdr == i_shdrpp[tdata->symtab_section]
4674 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4675 || hdr == i_shdrpp[tdata->strtab_section])
4676 hdr->sh_offset = -1;
4678 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4681 /* Now that we have set the section file positions, we can set up
4682 the file positions for the non PT_LOAD segments. */
4686 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4688 phdrs = elf_tdata (abfd)->phdr;
4689 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4694 if (p->p_type != PT_LOAD)
4697 if (m->includes_filehdr)
4699 filehdr_vaddr = p->p_vaddr;
4700 filehdr_paddr = p->p_paddr;
4702 if (m->includes_phdrs)
4704 phdrs_vaddr = p->p_vaddr;
4705 phdrs_paddr = p->p_paddr;
4706 if (m->includes_filehdr)
4708 phdrs_vaddr += bed->s->sizeof_ehdr;
4709 phdrs_paddr += bed->s->sizeof_ehdr;
4714 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4718 if (p->p_type == PT_GNU_RELRO)
4720 const Elf_Internal_Phdr *lp;
4722 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4724 if (link_info != NULL)
4726 /* During linking the range of the RELRO segment is passed
4728 for (lp = phdrs; lp < phdrs + count; ++lp)
4730 if (lp->p_type == PT_LOAD
4731 && lp->p_vaddr >= link_info->relro_start
4732 && lp->p_vaddr < link_info->relro_end
4733 && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4739 /* Otherwise we are copying an executable or shared
4740 library, but we need to use the same linker logic. */
4741 for (lp = phdrs; lp < phdrs + count; ++lp)
4743 if (lp->p_type == PT_LOAD
4744 && lp->p_paddr == p->p_paddr)
4749 if (lp < phdrs + count)
4751 p->p_vaddr = lp->p_vaddr;
4752 p->p_paddr = lp->p_paddr;
4753 p->p_offset = lp->p_offset;
4754 if (link_info != NULL)
4755 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4756 else if (m->p_size_valid)
4757 p->p_filesz = m->p_size;
4760 p->p_memsz = p->p_filesz;
4762 p->p_flags = (lp->p_flags & ~PF_W);
4766 memset (p, 0, sizeof *p);
4767 p->p_type = PT_NULL;
4770 else if (m->count != 0)
4772 if (p->p_type != PT_LOAD
4773 && (p->p_type != PT_NOTE
4774 || bfd_get_format (abfd) != bfd_core))
4776 Elf_Internal_Shdr *hdr;
4779 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4781 sect = m->sections[m->count - 1];
4782 hdr = &elf_section_data (sect)->this_hdr;
4783 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4784 if (hdr->sh_type != SHT_NOBITS)
4785 p->p_filesz += hdr->sh_size;
4786 p->p_offset = m->sections[0]->filepos;
4789 else if (m->includes_filehdr)
4791 p->p_vaddr = filehdr_vaddr;
4792 if (! m->p_paddr_valid)
4793 p->p_paddr = filehdr_paddr;
4795 else if (m->includes_phdrs)
4797 p->p_vaddr = phdrs_vaddr;
4798 if (! m->p_paddr_valid)
4799 p->p_paddr = phdrs_paddr;
4803 elf_tdata (abfd)->next_file_pos = off;
4808 /* Work out the file positions of all the sections. This is called by
4809 _bfd_elf_compute_section_file_positions. All the section sizes and
4810 VMAs must be known before this is called.
4812 Reloc sections come in two flavours: Those processed specially as
4813 "side-channel" data attached to a section to which they apply, and
4814 those that bfd doesn't process as relocations. The latter sort are
4815 stored in a normal bfd section by bfd_section_from_shdr. We don't
4816 consider the former sort here, unless they form part of the loadable
4817 image. Reloc sections not assigned here will be handled later by
4818 assign_file_positions_for_relocs.
4820 We also don't set the positions of the .symtab and .strtab here. */
4823 assign_file_positions_except_relocs (bfd *abfd,
4824 struct bfd_link_info *link_info)
4826 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4827 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4829 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4831 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4832 && bfd_get_format (abfd) != bfd_core)
4834 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4835 unsigned int num_sec = elf_numsections (abfd);
4836 Elf_Internal_Shdr **hdrpp;
4839 /* Start after the ELF header. */
4840 off = i_ehdrp->e_ehsize;
4842 /* We are not creating an executable, which means that we are
4843 not creating a program header, and that the actual order of
4844 the sections in the file is unimportant. */
4845 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4847 Elf_Internal_Shdr *hdr;
4850 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4851 && hdr->bfd_section == NULL)
4852 || i == tdata->symtab_section
4853 || i == tdata->symtab_shndx_section
4854 || i == tdata->strtab_section)
4856 hdr->sh_offset = -1;
4859 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4866 /* Assign file positions for the loaded sections based on the
4867 assignment of sections to segments. */
4868 if (!assign_file_positions_for_load_sections (abfd, link_info))
4871 /* And for non-load sections. */
4872 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4875 if (bed->elf_backend_modify_program_headers != NULL)
4877 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4881 /* Write out the program headers. */
4882 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4883 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4884 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4887 off = tdata->next_file_pos;
4890 /* Place the section headers. */
4891 off = align_file_position (off, 1 << bed->s->log_file_align);
4892 i_ehdrp->e_shoff = off;
4893 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4895 tdata->next_file_pos = off;
4901 prep_headers (bfd *abfd)
4903 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
4904 struct elf_strtab_hash *shstrtab;
4905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4907 i_ehdrp = elf_elfheader (abfd);
4909 shstrtab = _bfd_elf_strtab_init ();
4910 if (shstrtab == NULL)
4913 elf_shstrtab (abfd) = shstrtab;
4915 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4916 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4917 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4918 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4920 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4921 i_ehdrp->e_ident[EI_DATA] =
4922 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4923 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4925 if ((abfd->flags & DYNAMIC) != 0)
4926 i_ehdrp->e_type = ET_DYN;
4927 else if ((abfd->flags & EXEC_P) != 0)
4928 i_ehdrp->e_type = ET_EXEC;
4929 else if (bfd_get_format (abfd) == bfd_core)
4930 i_ehdrp->e_type = ET_CORE;
4932 i_ehdrp->e_type = ET_REL;
4934 switch (bfd_get_arch (abfd))
4936 case bfd_arch_unknown:
4937 i_ehdrp->e_machine = EM_NONE;
4940 /* There used to be a long list of cases here, each one setting
4941 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4942 in the corresponding bfd definition. To avoid duplication,
4943 the switch was removed. Machines that need special handling
4944 can generally do it in elf_backend_final_write_processing(),
4945 unless they need the information earlier than the final write.
4946 Such need can generally be supplied by replacing the tests for
4947 e_machine with the conditions used to determine it. */
4949 i_ehdrp->e_machine = bed->elf_machine_code;
4952 i_ehdrp->e_version = bed->s->ev_current;
4953 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4955 /* No program header, for now. */
4956 i_ehdrp->e_phoff = 0;
4957 i_ehdrp->e_phentsize = 0;
4958 i_ehdrp->e_phnum = 0;
4960 /* Each bfd section is section header entry. */
4961 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4962 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4964 /* If we're building an executable, we'll need a program header table. */
4965 if (abfd->flags & EXEC_P)
4966 /* It all happens later. */
4970 i_ehdrp->e_phentsize = 0;
4971 i_ehdrp->e_phoff = 0;
4974 elf_tdata (abfd)->symtab_hdr.sh_name =
4975 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4976 elf_tdata (abfd)->strtab_hdr.sh_name =
4977 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4978 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4979 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4980 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4981 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4982 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4988 /* Assign file positions for all the reloc sections which are not part
4989 of the loadable file image. */
4992 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4995 unsigned int i, num_sec;
4996 Elf_Internal_Shdr **shdrpp;
4998 off = elf_tdata (abfd)->next_file_pos;
5000 num_sec = elf_numsections (abfd);
5001 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5003 Elf_Internal_Shdr *shdrp;
5006 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5007 && shdrp->sh_offset == -1)
5008 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5011 elf_tdata (abfd)->next_file_pos = off;
5015 _bfd_elf_write_object_contents (bfd *abfd)
5017 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5018 Elf_Internal_Shdr **i_shdrp;
5020 unsigned int count, num_sec;
5022 if (! abfd->output_has_begun
5023 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5026 i_shdrp = elf_elfsections (abfd);
5029 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5033 _bfd_elf_assign_file_positions_for_relocs (abfd);
5035 /* After writing the headers, we need to write the sections too... */
5036 num_sec = elf_numsections (abfd);
5037 for (count = 1; count < num_sec; count++)
5039 if (bed->elf_backend_section_processing)
5040 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5041 if (i_shdrp[count]->contents)
5043 bfd_size_type amt = i_shdrp[count]->sh_size;
5045 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5046 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5051 /* Write out the section header names. */
5052 if (elf_shstrtab (abfd) != NULL
5053 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5054 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5057 if (bed->elf_backend_final_write_processing)
5058 (*bed->elf_backend_final_write_processing) (abfd,
5059 elf_tdata (abfd)->linker);
5061 if (!bed->s->write_shdrs_and_ehdr (abfd))
5064 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5065 if (elf_tdata (abfd)->after_write_object_contents)
5066 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
5072 _bfd_elf_write_corefile_contents (bfd *abfd)
5074 /* Hopefully this can be done just like an object file. */
5075 return _bfd_elf_write_object_contents (abfd);
5078 /* Given a section, search the header to find them. */
5081 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5083 const struct elf_backend_data *bed;
5084 unsigned int sec_index;
5086 if (elf_section_data (asect) != NULL
5087 && elf_section_data (asect)->this_idx != 0)
5088 return elf_section_data (asect)->this_idx;
5090 if (bfd_is_abs_section (asect))
5091 sec_index = SHN_ABS;
5092 else if (bfd_is_com_section (asect))
5093 sec_index = SHN_COMMON;
5094 else if (bfd_is_und_section (asect))
5095 sec_index = SHN_UNDEF;
5097 sec_index = SHN_BAD;
5099 bed = get_elf_backend_data (abfd);
5100 if (bed->elf_backend_section_from_bfd_section)
5102 int retval = sec_index;
5104 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5108 if (sec_index == SHN_BAD)
5109 bfd_set_error (bfd_error_nonrepresentable_section);
5114 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5118 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5120 asymbol *asym_ptr = *asym_ptr_ptr;
5122 flagword flags = asym_ptr->flags;
5124 /* When gas creates relocations against local labels, it creates its
5125 own symbol for the section, but does put the symbol into the
5126 symbol chain, so udata is 0. When the linker is generating
5127 relocatable output, this section symbol may be for one of the
5128 input sections rather than the output section. */
5129 if (asym_ptr->udata.i == 0
5130 && (flags & BSF_SECTION_SYM)
5131 && asym_ptr->section)
5136 sec = asym_ptr->section;
5137 if (sec->owner != abfd && sec->output_section != NULL)
5138 sec = sec->output_section;
5139 if (sec->owner == abfd
5140 && (indx = sec->index) < elf_num_section_syms (abfd)
5141 && elf_section_syms (abfd)[indx] != NULL)
5142 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5145 idx = asym_ptr->udata.i;
5149 /* This case can occur when using --strip-symbol on a symbol
5150 which is used in a relocation entry. */
5151 (*_bfd_error_handler)
5152 (_("%B: symbol `%s' required but not present"),
5153 abfd, bfd_asymbol_name (asym_ptr));
5154 bfd_set_error (bfd_error_no_symbols);
5161 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5162 (long) asym_ptr, asym_ptr->name, idx, flags,
5163 elf_symbol_flags (flags));
5171 /* Rewrite program header information. */
5174 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5176 Elf_Internal_Ehdr *iehdr;
5177 struct elf_segment_map *map;
5178 struct elf_segment_map *map_first;
5179 struct elf_segment_map **pointer_to_map;
5180 Elf_Internal_Phdr *segment;
5183 unsigned int num_segments;
5184 bfd_boolean phdr_included = FALSE;
5185 bfd_boolean p_paddr_valid;
5186 bfd_vma maxpagesize;
5187 struct elf_segment_map *phdr_adjust_seg = NULL;
5188 unsigned int phdr_adjust_num = 0;
5189 const struct elf_backend_data *bed;
5191 bed = get_elf_backend_data (ibfd);
5192 iehdr = elf_elfheader (ibfd);
5195 pointer_to_map = &map_first;
5197 num_segments = elf_elfheader (ibfd)->e_phnum;
5198 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5200 /* Returns the end address of the segment + 1. */
5201 #define SEGMENT_END(segment, start) \
5202 (start + (segment->p_memsz > segment->p_filesz \
5203 ? segment->p_memsz : segment->p_filesz))
5205 #define SECTION_SIZE(section, segment) \
5206 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5207 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5208 ? section->size : 0)
5210 /* Returns TRUE if the given section is contained within
5211 the given segment. VMA addresses are compared. */
5212 #define IS_CONTAINED_BY_VMA(section, segment) \
5213 (section->vma >= segment->p_vaddr \
5214 && (section->vma + SECTION_SIZE (section, segment) \
5215 <= (SEGMENT_END (segment, segment->p_vaddr))))
5217 /* Returns TRUE if the given section is contained within
5218 the given segment. LMA addresses are compared. */
5219 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5220 (section->lma >= base \
5221 && (section->lma + SECTION_SIZE (section, segment) \
5222 <= SEGMENT_END (segment, base)))
5224 /* Handle PT_NOTE segment. */
5225 #define IS_NOTE(p, s) \
5226 (p->p_type == PT_NOTE \
5227 && elf_section_type (s) == SHT_NOTE \
5228 && (bfd_vma) s->filepos >= p->p_offset \
5229 && ((bfd_vma) s->filepos + s->size \
5230 <= p->p_offset + p->p_filesz))
5232 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5234 #define IS_COREFILE_NOTE(p, s) \
5236 && bfd_get_format (ibfd) == bfd_core \
5240 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5241 linker, which generates a PT_INTERP section with p_vaddr and
5242 p_memsz set to 0. */
5243 #define IS_SOLARIS_PT_INTERP(p, s) \
5245 && p->p_paddr == 0 \
5246 && p->p_memsz == 0 \
5247 && p->p_filesz > 0 \
5248 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5250 && (bfd_vma) s->filepos >= p->p_offset \
5251 && ((bfd_vma) s->filepos + s->size \
5252 <= p->p_offset + p->p_filesz))
5254 /* Decide if the given section should be included in the given segment.
5255 A section will be included if:
5256 1. It is within the address space of the segment -- we use the LMA
5257 if that is set for the segment and the VMA otherwise,
5258 2. It is an allocated section or a NOTE section in a PT_NOTE
5260 3. There is an output section associated with it,
5261 4. The section has not already been allocated to a previous segment.
5262 5. PT_GNU_STACK segments do not include any sections.
5263 6. PT_TLS segment includes only SHF_TLS sections.
5264 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5265 8. PT_DYNAMIC should not contain empty sections at the beginning
5266 (with the possible exception of .dynamic). */
5267 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5268 ((((segment->p_paddr \
5269 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5270 : IS_CONTAINED_BY_VMA (section, segment)) \
5271 && (section->flags & SEC_ALLOC) != 0) \
5272 || IS_NOTE (segment, section)) \
5273 && segment->p_type != PT_GNU_STACK \
5274 && (segment->p_type != PT_TLS \
5275 || (section->flags & SEC_THREAD_LOCAL)) \
5276 && (segment->p_type == PT_LOAD \
5277 || segment->p_type == PT_TLS \
5278 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5279 && (segment->p_type != PT_DYNAMIC \
5280 || SECTION_SIZE (section, segment) > 0 \
5281 || (segment->p_paddr \
5282 ? segment->p_paddr != section->lma \
5283 : segment->p_vaddr != section->vma) \
5284 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5286 && !section->segment_mark)
5288 /* If the output section of a section in the input segment is NULL,
5289 it is removed from the corresponding output segment. */
5290 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5291 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5292 && section->output_section != NULL)
5294 /* Returns TRUE iff seg1 starts after the end of seg2. */
5295 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5296 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5298 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5299 their VMA address ranges and their LMA address ranges overlap.
5300 It is possible to have overlapping VMA ranges without overlapping LMA
5301 ranges. RedBoot images for example can have both .data and .bss mapped
5302 to the same VMA range, but with the .data section mapped to a different
5304 #define SEGMENT_OVERLAPS(seg1, seg2) \
5305 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5306 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5307 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5308 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5310 /* Initialise the segment mark field. */
5311 for (section = ibfd->sections; section != NULL; section = section->next)
5312 section->segment_mark = FALSE;
5314 /* The Solaris linker creates program headers in which all the
5315 p_paddr fields are zero. When we try to objcopy or strip such a
5316 file, we get confused. Check for this case, and if we find it
5317 don't set the p_paddr_valid fields. */
5318 p_paddr_valid = FALSE;
5319 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5322 if (segment->p_paddr != 0)
5324 p_paddr_valid = TRUE;
5328 /* Scan through the segments specified in the program header
5329 of the input BFD. For this first scan we look for overlaps
5330 in the loadable segments. These can be created by weird
5331 parameters to objcopy. Also, fix some solaris weirdness. */
5332 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5337 Elf_Internal_Phdr *segment2;
5339 if (segment->p_type == PT_INTERP)
5340 for (section = ibfd->sections; section; section = section->next)
5341 if (IS_SOLARIS_PT_INTERP (segment, section))
5343 /* Mininal change so that the normal section to segment
5344 assignment code will work. */
5345 segment->p_vaddr = section->vma;
5349 if (segment->p_type != PT_LOAD)
5351 /* Remove PT_GNU_RELRO segment. */
5352 if (segment->p_type == PT_GNU_RELRO)
5353 segment->p_type = PT_NULL;
5357 /* Determine if this segment overlaps any previous segments. */
5358 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5360 bfd_signed_vma extra_length;
5362 if (segment2->p_type != PT_LOAD
5363 || !SEGMENT_OVERLAPS (segment, segment2))
5366 /* Merge the two segments together. */
5367 if (segment2->p_vaddr < segment->p_vaddr)
5369 /* Extend SEGMENT2 to include SEGMENT and then delete
5371 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5372 - SEGMENT_END (segment2, segment2->p_vaddr));
5374 if (extra_length > 0)
5376 segment2->p_memsz += extra_length;
5377 segment2->p_filesz += extra_length;
5380 segment->p_type = PT_NULL;
5382 /* Since we have deleted P we must restart the outer loop. */
5384 segment = elf_tdata (ibfd)->phdr;
5389 /* Extend SEGMENT to include SEGMENT2 and then delete
5391 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5392 - SEGMENT_END (segment, segment->p_vaddr));
5394 if (extra_length > 0)
5396 segment->p_memsz += extra_length;
5397 segment->p_filesz += extra_length;
5400 segment2->p_type = PT_NULL;
5405 /* The second scan attempts to assign sections to segments. */
5406 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5410 unsigned int section_count;
5411 asection **sections;
5412 asection *output_section;
5414 bfd_vma matching_lma;
5415 bfd_vma suggested_lma;
5418 asection *first_section;
5419 bfd_boolean first_matching_lma;
5420 bfd_boolean first_suggested_lma;
5422 if (segment->p_type == PT_NULL)
5425 first_section = NULL;
5426 /* Compute how many sections might be placed into this segment. */
5427 for (section = ibfd->sections, section_count = 0;
5429 section = section->next)
5431 /* Find the first section in the input segment, which may be
5432 removed from the corresponding output segment. */
5433 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5435 if (first_section == NULL)
5436 first_section = section;
5437 if (section->output_section != NULL)
5442 /* Allocate a segment map big enough to contain
5443 all of the sections we have selected. */
5444 amt = sizeof (struct elf_segment_map);
5445 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5446 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5450 /* Initialise the fields of the segment map. Default to
5451 using the physical address of the segment in the input BFD. */
5453 map->p_type = segment->p_type;
5454 map->p_flags = segment->p_flags;
5455 map->p_flags_valid = 1;
5457 /* If the first section in the input segment is removed, there is
5458 no need to preserve segment physical address in the corresponding
5460 if (!first_section || first_section->output_section != NULL)
5462 map->p_paddr = segment->p_paddr;
5463 map->p_paddr_valid = p_paddr_valid;
5466 /* Determine if this segment contains the ELF file header
5467 and if it contains the program headers themselves. */
5468 map->includes_filehdr = (segment->p_offset == 0
5469 && segment->p_filesz >= iehdr->e_ehsize);
5470 map->includes_phdrs = 0;
5472 if (!phdr_included || segment->p_type != PT_LOAD)
5474 map->includes_phdrs =
5475 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5476 && (segment->p_offset + segment->p_filesz
5477 >= ((bfd_vma) iehdr->e_phoff
5478 + iehdr->e_phnum * iehdr->e_phentsize)));
5480 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5481 phdr_included = TRUE;
5484 if (section_count == 0)
5486 /* Special segments, such as the PT_PHDR segment, may contain
5487 no sections, but ordinary, loadable segments should contain
5488 something. They are allowed by the ELF spec however, so only
5489 a warning is produced. */
5490 if (segment->p_type == PT_LOAD)
5491 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5492 " detected, is this intentional ?\n"),
5496 *pointer_to_map = map;
5497 pointer_to_map = &map->next;
5502 /* Now scan the sections in the input BFD again and attempt
5503 to add their corresponding output sections to the segment map.
5504 The problem here is how to handle an output section which has
5505 been moved (ie had its LMA changed). There are four possibilities:
5507 1. None of the sections have been moved.
5508 In this case we can continue to use the segment LMA from the
5511 2. All of the sections have been moved by the same amount.
5512 In this case we can change the segment's LMA to match the LMA
5513 of the first section.
5515 3. Some of the sections have been moved, others have not.
5516 In this case those sections which have not been moved can be
5517 placed in the current segment which will have to have its size,
5518 and possibly its LMA changed, and a new segment or segments will
5519 have to be created to contain the other sections.
5521 4. The sections have been moved, but not by the same amount.
5522 In this case we can change the segment's LMA to match the LMA
5523 of the first section and we will have to create a new segment
5524 or segments to contain the other sections.
5526 In order to save time, we allocate an array to hold the section
5527 pointers that we are interested in. As these sections get assigned
5528 to a segment, they are removed from this array. */
5530 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5531 if (sections == NULL)
5534 /* Step One: Scan for segment vs section LMA conflicts.
5535 Also add the sections to the section array allocated above.
5536 Also add the sections to the current segment. In the common
5537 case, where the sections have not been moved, this means that
5538 we have completely filled the segment, and there is nothing
5543 first_matching_lma = TRUE;
5544 first_suggested_lma = TRUE;
5546 for (section = ibfd->sections;
5548 section = section->next)
5549 if (section == first_section)
5552 for (j = 0; section != NULL; section = section->next)
5554 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5556 output_section = section->output_section;
5558 sections[j++] = section;
5560 /* The Solaris native linker always sets p_paddr to 0.
5561 We try to catch that case here, and set it to the
5562 correct value. Note - some backends require that
5563 p_paddr be left as zero. */
5565 && segment->p_vaddr != 0
5566 && !bed->want_p_paddr_set_to_zero
5568 && output_section->lma != 0
5569 && output_section->vma == (segment->p_vaddr
5570 + (map->includes_filehdr
5573 + (map->includes_phdrs
5575 * iehdr->e_phentsize)
5577 map->p_paddr = segment->p_vaddr;
5579 /* Match up the physical address of the segment with the
5580 LMA address of the output section. */
5581 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5582 || IS_COREFILE_NOTE (segment, section)
5583 || (bed->want_p_paddr_set_to_zero
5584 && IS_CONTAINED_BY_VMA (output_section, segment)))
5586 if (first_matching_lma || output_section->lma < matching_lma)
5588 matching_lma = output_section->lma;
5589 first_matching_lma = FALSE;
5592 /* We assume that if the section fits within the segment
5593 then it does not overlap any other section within that
5595 map->sections[isec++] = output_section;
5597 else if (first_suggested_lma)
5599 suggested_lma = output_section->lma;
5600 first_suggested_lma = FALSE;
5603 if (j == section_count)
5608 BFD_ASSERT (j == section_count);
5610 /* Step Two: Adjust the physical address of the current segment,
5612 if (isec == section_count)
5614 /* All of the sections fitted within the segment as currently
5615 specified. This is the default case. Add the segment to
5616 the list of built segments and carry on to process the next
5617 program header in the input BFD. */
5618 map->count = section_count;
5619 *pointer_to_map = map;
5620 pointer_to_map = &map->next;
5623 && !bed->want_p_paddr_set_to_zero
5624 && matching_lma != map->p_paddr
5625 && !map->includes_filehdr
5626 && !map->includes_phdrs)
5627 /* There is some padding before the first section in the
5628 segment. So, we must account for that in the output
5630 map->p_vaddr_offset = matching_lma - map->p_paddr;
5637 if (!first_matching_lma)
5639 /* At least one section fits inside the current segment.
5640 Keep it, but modify its physical address to match the
5641 LMA of the first section that fitted. */
5642 map->p_paddr = matching_lma;
5646 /* None of the sections fitted inside the current segment.
5647 Change the current segment's physical address to match
5648 the LMA of the first section. */
5649 map->p_paddr = suggested_lma;
5652 /* Offset the segment physical address from the lma
5653 to allow for space taken up by elf headers. */
5654 if (map->includes_filehdr)
5656 if (map->p_paddr >= iehdr->e_ehsize)
5657 map->p_paddr -= iehdr->e_ehsize;
5660 map->includes_filehdr = FALSE;
5661 map->includes_phdrs = FALSE;
5665 if (map->includes_phdrs)
5667 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5669 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5671 /* iehdr->e_phnum is just an estimate of the number
5672 of program headers that we will need. Make a note
5673 here of the number we used and the segment we chose
5674 to hold these headers, so that we can adjust the
5675 offset when we know the correct value. */
5676 phdr_adjust_num = iehdr->e_phnum;
5677 phdr_adjust_seg = map;
5680 map->includes_phdrs = FALSE;
5684 /* Step Three: Loop over the sections again, this time assigning
5685 those that fit to the current segment and removing them from the
5686 sections array; but making sure not to leave large gaps. Once all
5687 possible sections have been assigned to the current segment it is
5688 added to the list of built segments and if sections still remain
5689 to be assigned, a new segment is constructed before repeating
5696 first_suggested_lma = TRUE;
5698 /* Fill the current segment with sections that fit. */
5699 for (j = 0; j < section_count; j++)
5701 section = sections[j];
5703 if (section == NULL)
5706 output_section = section->output_section;
5708 BFD_ASSERT (output_section != NULL);
5710 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5711 || IS_COREFILE_NOTE (segment, section))
5713 if (map->count == 0)
5715 /* If the first section in a segment does not start at
5716 the beginning of the segment, then something is
5718 if (output_section->lma
5720 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5721 + (map->includes_phdrs
5722 ? iehdr->e_phnum * iehdr->e_phentsize
5730 prev_sec = map->sections[map->count - 1];
5732 /* If the gap between the end of the previous section
5733 and the start of this section is more than
5734 maxpagesize then we need to start a new segment. */
5735 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5737 < BFD_ALIGN (output_section->lma, maxpagesize))
5738 || (prev_sec->lma + prev_sec->size
5739 > output_section->lma))
5741 if (first_suggested_lma)
5743 suggested_lma = output_section->lma;
5744 first_suggested_lma = FALSE;
5751 map->sections[map->count++] = output_section;
5754 section->segment_mark = TRUE;
5756 else if (first_suggested_lma)
5758 suggested_lma = output_section->lma;
5759 first_suggested_lma = FALSE;
5763 BFD_ASSERT (map->count > 0);
5765 /* Add the current segment to the list of built segments. */
5766 *pointer_to_map = map;
5767 pointer_to_map = &map->next;
5769 if (isec < section_count)
5771 /* We still have not allocated all of the sections to
5772 segments. Create a new segment here, initialise it
5773 and carry on looping. */
5774 amt = sizeof (struct elf_segment_map);
5775 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5776 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5783 /* Initialise the fields of the segment map. Set the physical
5784 physical address to the LMA of the first section that has
5785 not yet been assigned. */
5787 map->p_type = segment->p_type;
5788 map->p_flags = segment->p_flags;
5789 map->p_flags_valid = 1;
5790 map->p_paddr = suggested_lma;
5791 map->p_paddr_valid = p_paddr_valid;
5792 map->includes_filehdr = 0;
5793 map->includes_phdrs = 0;
5796 while (isec < section_count);
5801 elf_tdata (obfd)->segment_map = map_first;
5803 /* If we had to estimate the number of program headers that were
5804 going to be needed, then check our estimate now and adjust
5805 the offset if necessary. */
5806 if (phdr_adjust_seg != NULL)
5810 for (count = 0, map = map_first; map != NULL; map = map->next)
5813 if (count > phdr_adjust_num)
5814 phdr_adjust_seg->p_paddr
5815 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5820 #undef IS_CONTAINED_BY_VMA
5821 #undef IS_CONTAINED_BY_LMA
5823 #undef IS_COREFILE_NOTE
5824 #undef IS_SOLARIS_PT_INTERP
5825 #undef IS_SECTION_IN_INPUT_SEGMENT
5826 #undef INCLUDE_SECTION_IN_SEGMENT
5827 #undef SEGMENT_AFTER_SEGMENT
5828 #undef SEGMENT_OVERLAPS
5832 /* Copy ELF program header information. */
5835 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5837 Elf_Internal_Ehdr *iehdr;
5838 struct elf_segment_map *map;
5839 struct elf_segment_map *map_first;
5840 struct elf_segment_map **pointer_to_map;
5841 Elf_Internal_Phdr *segment;
5843 unsigned int num_segments;
5844 bfd_boolean phdr_included = FALSE;
5845 bfd_boolean p_paddr_valid;
5847 iehdr = elf_elfheader (ibfd);
5850 pointer_to_map = &map_first;
5852 /* If all the segment p_paddr fields are zero, don't set
5853 map->p_paddr_valid. */
5854 p_paddr_valid = FALSE;
5855 num_segments = elf_elfheader (ibfd)->e_phnum;
5856 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5859 if (segment->p_paddr != 0)
5861 p_paddr_valid = TRUE;
5865 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5870 unsigned int section_count;
5872 Elf_Internal_Shdr *this_hdr;
5873 asection *first_section = NULL;
5874 asection *lowest_section;
5876 /* Compute how many sections are in this segment. */
5877 for (section = ibfd->sections, section_count = 0;
5879 section = section->next)
5881 this_hdr = &(elf_section_data(section)->this_hdr);
5882 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
5884 if (first_section == NULL)
5885 first_section = section;
5890 /* Allocate a segment map big enough to contain
5891 all of the sections we have selected. */
5892 amt = sizeof (struct elf_segment_map);
5893 if (section_count != 0)
5894 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5895 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5899 /* Initialize the fields of the output segment map with the
5902 map->p_type = segment->p_type;
5903 map->p_flags = segment->p_flags;
5904 map->p_flags_valid = 1;
5905 map->p_paddr = segment->p_paddr;
5906 map->p_paddr_valid = p_paddr_valid;
5907 map->p_align = segment->p_align;
5908 map->p_align_valid = 1;
5909 map->p_vaddr_offset = 0;
5911 if (map->p_type == PT_GNU_RELRO)
5913 /* The PT_GNU_RELRO segment may contain the first a few
5914 bytes in the .got.plt section even if the whole .got.plt
5915 section isn't in the PT_GNU_RELRO segment. We won't
5916 change the size of the PT_GNU_RELRO segment. */
5917 map->p_size = segment->p_memsz;
5918 map->p_size_valid = 1;
5921 /* Determine if this segment contains the ELF file header
5922 and if it contains the program headers themselves. */
5923 map->includes_filehdr = (segment->p_offset == 0
5924 && segment->p_filesz >= iehdr->e_ehsize);
5926 map->includes_phdrs = 0;
5927 if (! phdr_included || segment->p_type != PT_LOAD)
5929 map->includes_phdrs =
5930 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5931 && (segment->p_offset + segment->p_filesz
5932 >= ((bfd_vma) iehdr->e_phoff
5933 + iehdr->e_phnum * iehdr->e_phentsize)));
5935 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5936 phdr_included = TRUE;
5939 lowest_section = first_section;
5940 if (section_count != 0)
5942 unsigned int isec = 0;
5944 for (section = first_section;
5946 section = section->next)
5948 this_hdr = &(elf_section_data(section)->this_hdr);
5949 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
5951 map->sections[isec++] = section->output_section;
5952 if (section->lma < lowest_section->lma)
5953 lowest_section = section;
5954 if ((section->flags & SEC_ALLOC) != 0)
5958 /* Section lmas are set up from PT_LOAD header
5959 p_paddr in _bfd_elf_make_section_from_shdr.
5960 If this header has a p_paddr that disagrees
5961 with the section lma, flag the p_paddr as
5963 if ((section->flags & SEC_LOAD) != 0)
5964 seg_off = this_hdr->sh_offset - segment->p_offset;
5966 seg_off = this_hdr->sh_addr - segment->p_vaddr;
5967 if (section->lma - segment->p_paddr != seg_off)
5968 map->p_paddr_valid = FALSE;
5970 if (isec == section_count)
5976 if (map->includes_filehdr && lowest_section != NULL)
5977 /* We need to keep the space used by the headers fixed. */
5978 map->header_size = lowest_section->vma - segment->p_vaddr;
5980 if (!map->includes_phdrs
5981 && !map->includes_filehdr
5982 && map->p_paddr_valid)
5983 /* There is some other padding before the first section. */
5984 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5985 - segment->p_paddr);
5987 map->count = section_count;
5988 *pointer_to_map = map;
5989 pointer_to_map = &map->next;
5992 elf_tdata (obfd)->segment_map = map_first;
5996 /* Copy private BFD data. This copies or rewrites ELF program header
6000 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6002 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6003 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6006 if (elf_tdata (ibfd)->phdr == NULL)
6009 if (ibfd->xvec == obfd->xvec)
6011 /* Check to see if any sections in the input BFD
6012 covered by ELF program header have changed. */
6013 Elf_Internal_Phdr *segment;
6014 asection *section, *osec;
6015 unsigned int i, num_segments;
6016 Elf_Internal_Shdr *this_hdr;
6017 const struct elf_backend_data *bed;
6019 bed = get_elf_backend_data (ibfd);
6021 /* Regenerate the segment map if p_paddr is set to 0. */
6022 if (bed->want_p_paddr_set_to_zero)
6025 /* Initialize the segment mark field. */
6026 for (section = obfd->sections; section != NULL;
6027 section = section->next)
6028 section->segment_mark = FALSE;
6030 num_segments = elf_elfheader (ibfd)->e_phnum;
6031 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6035 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6036 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6037 which severly confuses things, so always regenerate the segment
6038 map in this case. */
6039 if (segment->p_paddr == 0
6040 && segment->p_memsz == 0
6041 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6044 for (section = ibfd->sections;
6045 section != NULL; section = section->next)
6047 /* We mark the output section so that we know it comes
6048 from the input BFD. */
6049 osec = section->output_section;
6051 osec->segment_mark = TRUE;
6053 /* Check if this section is covered by the segment. */
6054 this_hdr = &(elf_section_data(section)->this_hdr);
6055 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6057 /* FIXME: Check if its output section is changed or
6058 removed. What else do we need to check? */
6060 || section->flags != osec->flags
6061 || section->lma != osec->lma
6062 || section->vma != osec->vma
6063 || section->size != osec->size
6064 || section->rawsize != osec->rawsize
6065 || section->alignment_power != osec->alignment_power)
6071 /* Check to see if any output section do not come from the
6073 for (section = obfd->sections; section != NULL;
6074 section = section->next)
6076 if (section->segment_mark == FALSE)
6079 section->segment_mark = FALSE;
6082 return copy_elf_program_header (ibfd, obfd);
6086 return rewrite_elf_program_header (ibfd, obfd);
6089 /* Initialize private output section information from input section. */
6092 _bfd_elf_init_private_section_data (bfd *ibfd,
6096 struct bfd_link_info *link_info)
6099 Elf_Internal_Shdr *ihdr, *ohdr;
6100 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6102 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6103 || obfd->xvec->flavour != bfd_target_elf_flavour)
6106 /* For objcopy and relocatable link, don't copy the output ELF
6107 section type from input if the output BFD section flags have been
6108 set to something different. For a final link allow some flags
6109 that the linker clears to differ. */
6110 if (elf_section_type (osec) == SHT_NULL
6111 && (osec->flags == isec->flags
6113 && ((osec->flags ^ isec->flags)
6114 & ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES)) == 0)))
6115 elf_section_type (osec) = elf_section_type (isec);
6117 /* FIXME: Is this correct for all OS/PROC specific flags? */
6118 elf_section_flags (osec) |= (elf_section_flags (isec)
6119 & (SHF_MASKOS | SHF_MASKPROC));
6121 /* Set things up for objcopy and relocatable link. The output
6122 SHT_GROUP section will have its elf_next_in_group pointing back
6123 to the input group members. Ignore linker created group section.
6124 See elfNN_ia64_object_p in elfxx-ia64.c. */
6127 if (elf_sec_group (isec) == NULL
6128 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6130 if (elf_section_flags (isec) & SHF_GROUP)
6131 elf_section_flags (osec) |= SHF_GROUP;
6132 elf_next_in_group (osec) = elf_next_in_group (isec);
6133 elf_section_data (osec)->group = elf_section_data (isec)->group;
6137 ihdr = &elf_section_data (isec)->this_hdr;
6139 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6140 don't use the output section of the linked-to section since it
6141 may be NULL at this point. */
6142 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6144 ohdr = &elf_section_data (osec)->this_hdr;
6145 ohdr->sh_flags |= SHF_LINK_ORDER;
6146 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6149 osec->use_rela_p = isec->use_rela_p;
6154 /* Copy private section information. This copies over the entsize
6155 field, and sometimes the info field. */
6158 _bfd_elf_copy_private_section_data (bfd *ibfd,
6163 Elf_Internal_Shdr *ihdr, *ohdr;
6165 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6166 || obfd->xvec->flavour != bfd_target_elf_flavour)
6169 ihdr = &elf_section_data (isec)->this_hdr;
6170 ohdr = &elf_section_data (osec)->this_hdr;
6172 ohdr->sh_entsize = ihdr->sh_entsize;
6174 if (ihdr->sh_type == SHT_SYMTAB
6175 || ihdr->sh_type == SHT_DYNSYM
6176 || ihdr->sh_type == SHT_GNU_verneed
6177 || ihdr->sh_type == SHT_GNU_verdef)
6178 ohdr->sh_info = ihdr->sh_info;
6180 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6184 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6185 necessary if we are removing either the SHT_GROUP section or any of
6186 the group member sections. DISCARDED is the value that a section's
6187 output_section has if the section will be discarded, NULL when this
6188 function is called from objcopy, bfd_abs_section_ptr when called
6192 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6196 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6197 if (elf_section_type (isec) == SHT_GROUP)
6199 asection *first = elf_next_in_group (isec);
6200 asection *s = first;
6201 bfd_size_type removed = 0;
6205 /* If this member section is being output but the
6206 SHT_GROUP section is not, then clear the group info
6207 set up by _bfd_elf_copy_private_section_data. */
6208 if (s->output_section != discarded
6209 && isec->output_section == discarded)
6211 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6212 elf_group_name (s->output_section) = NULL;
6214 /* Conversely, if the member section is not being output
6215 but the SHT_GROUP section is, then adjust its size. */
6216 else if (s->output_section == discarded
6217 && isec->output_section != discarded)
6219 s = elf_next_in_group (s);
6225 if (discarded != NULL)
6227 /* If we've been called for ld -r, then we need to
6228 adjust the input section size. This function may
6229 be called multiple times, so save the original
6231 if (isec->rawsize == 0)
6232 isec->rawsize = isec->size;
6233 isec->size = isec->rawsize - removed;
6237 /* Adjust the output section size when called from
6239 isec->output_section->size -= removed;
6247 /* Copy private header information. */
6250 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6252 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6253 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6256 /* Copy over private BFD data if it has not already been copied.
6257 This must be done here, rather than in the copy_private_bfd_data
6258 entry point, because the latter is called after the section
6259 contents have been set, which means that the program headers have
6260 already been worked out. */
6261 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6263 if (! copy_private_bfd_data (ibfd, obfd))
6267 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6270 /* Copy private symbol information. If this symbol is in a section
6271 which we did not map into a BFD section, try to map the section
6272 index correctly. We use special macro definitions for the mapped
6273 section indices; these definitions are interpreted by the
6274 swap_out_syms function. */
6276 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6277 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6278 #define MAP_STRTAB (SHN_HIOS + 3)
6279 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6280 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6283 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6288 elf_symbol_type *isym, *osym;
6290 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6291 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6294 isym = elf_symbol_from (ibfd, isymarg);
6295 osym = elf_symbol_from (obfd, osymarg);
6298 && isym->internal_elf_sym.st_shndx != 0
6300 && bfd_is_abs_section (isym->symbol.section))
6304 shndx = isym->internal_elf_sym.st_shndx;
6305 if (shndx == elf_onesymtab (ibfd))
6306 shndx = MAP_ONESYMTAB;
6307 else if (shndx == elf_dynsymtab (ibfd))
6308 shndx = MAP_DYNSYMTAB;
6309 else if (shndx == elf_tdata (ibfd)->strtab_section)
6311 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6312 shndx = MAP_SHSTRTAB;
6313 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6314 shndx = MAP_SYM_SHNDX;
6315 osym->internal_elf_sym.st_shndx = shndx;
6321 /* Swap out the symbols. */
6324 swap_out_syms (bfd *abfd,
6325 struct bfd_strtab_hash **sttp,
6328 const struct elf_backend_data *bed;
6331 struct bfd_strtab_hash *stt;
6332 Elf_Internal_Shdr *symtab_hdr;
6333 Elf_Internal_Shdr *symtab_shndx_hdr;
6334 Elf_Internal_Shdr *symstrtab_hdr;
6335 bfd_byte *outbound_syms;
6336 bfd_byte *outbound_shndx;
6339 bfd_boolean name_local_sections;
6341 if (!elf_map_symbols (abfd))
6344 /* Dump out the symtabs. */
6345 stt = _bfd_elf_stringtab_init ();
6349 bed = get_elf_backend_data (abfd);
6350 symcount = bfd_get_symcount (abfd);
6351 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6352 symtab_hdr->sh_type = SHT_SYMTAB;
6353 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6354 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6355 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6356 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6358 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6359 symstrtab_hdr->sh_type = SHT_STRTAB;
6361 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6362 bed->s->sizeof_sym);
6363 if (outbound_syms == NULL)
6365 _bfd_stringtab_free (stt);
6368 symtab_hdr->contents = outbound_syms;
6370 outbound_shndx = NULL;
6371 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6372 if (symtab_shndx_hdr->sh_name != 0)
6374 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6375 outbound_shndx = (bfd_byte *)
6376 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6377 if (outbound_shndx == NULL)
6379 _bfd_stringtab_free (stt);
6383 symtab_shndx_hdr->contents = outbound_shndx;
6384 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6385 symtab_shndx_hdr->sh_size = amt;
6386 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6387 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6390 /* Now generate the data (for "contents"). */
6392 /* Fill in zeroth symbol and swap it out. */
6393 Elf_Internal_Sym sym;
6399 sym.st_shndx = SHN_UNDEF;
6400 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6401 outbound_syms += bed->s->sizeof_sym;
6402 if (outbound_shndx != NULL)
6403 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6407 = (bed->elf_backend_name_local_section_symbols
6408 && bed->elf_backend_name_local_section_symbols (abfd));
6410 syms = bfd_get_outsymbols (abfd);
6411 for (idx = 0; idx < symcount; idx++)
6413 Elf_Internal_Sym sym;
6414 bfd_vma value = syms[idx]->value;
6415 elf_symbol_type *type_ptr;
6416 flagword flags = syms[idx]->flags;
6419 if (!name_local_sections
6420 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6422 /* Local section symbols have no name. */
6427 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6430 if (sym.st_name == (unsigned long) -1)
6432 _bfd_stringtab_free (stt);
6437 type_ptr = elf_symbol_from (abfd, syms[idx]);
6439 if ((flags & BSF_SECTION_SYM) == 0
6440 && bfd_is_com_section (syms[idx]->section))
6442 /* ELF common symbols put the alignment into the `value' field,
6443 and the size into the `size' field. This is backwards from
6444 how BFD handles it, so reverse it here. */
6445 sym.st_size = value;
6446 if (type_ptr == NULL
6447 || type_ptr->internal_elf_sym.st_value == 0)
6448 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6450 sym.st_value = type_ptr->internal_elf_sym.st_value;
6451 sym.st_shndx = _bfd_elf_section_from_bfd_section
6452 (abfd, syms[idx]->section);
6456 asection *sec = syms[idx]->section;
6459 if (sec->output_section)
6461 value += sec->output_offset;
6462 sec = sec->output_section;
6465 /* Don't add in the section vma for relocatable output. */
6466 if (! relocatable_p)
6468 sym.st_value = value;
6469 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6471 if (bfd_is_abs_section (sec)
6473 && type_ptr->internal_elf_sym.st_shndx != 0)
6475 /* This symbol is in a real ELF section which we did
6476 not create as a BFD section. Undo the mapping done
6477 by copy_private_symbol_data. */
6478 shndx = type_ptr->internal_elf_sym.st_shndx;
6482 shndx = elf_onesymtab (abfd);
6485 shndx = elf_dynsymtab (abfd);
6488 shndx = elf_tdata (abfd)->strtab_section;
6491 shndx = elf_tdata (abfd)->shstrtab_section;
6494 shndx = elf_tdata (abfd)->symtab_shndx_section;
6502 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6504 if (shndx == SHN_BAD)
6508 /* Writing this would be a hell of a lot easier if
6509 we had some decent documentation on bfd, and
6510 knew what to expect of the library, and what to
6511 demand of applications. For example, it
6512 appears that `objcopy' might not set the
6513 section of a symbol to be a section that is
6514 actually in the output file. */
6515 sec2 = bfd_get_section_by_name (abfd, sec->name);
6518 _bfd_error_handler (_("\
6519 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6520 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6522 bfd_set_error (bfd_error_invalid_operation);
6523 _bfd_stringtab_free (stt);
6527 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6528 BFD_ASSERT (shndx != SHN_BAD);
6532 sym.st_shndx = shndx;
6535 if ((flags & BSF_THREAD_LOCAL) != 0)
6537 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6538 type = STT_GNU_IFUNC;
6539 else if ((flags & BSF_FUNCTION) != 0)
6541 else if ((flags & BSF_OBJECT) != 0)
6543 else if ((flags & BSF_RELC) != 0)
6545 else if ((flags & BSF_SRELC) != 0)
6550 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6553 /* Processor-specific types. */
6554 if (type_ptr != NULL
6555 && bed->elf_backend_get_symbol_type)
6556 type = ((*bed->elf_backend_get_symbol_type)
6557 (&type_ptr->internal_elf_sym, type));
6559 if (flags & BSF_SECTION_SYM)
6561 if (flags & BSF_GLOBAL)
6562 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6564 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6566 else if (bfd_is_com_section (syms[idx]->section))
6568 #ifdef USE_STT_COMMON
6569 if (type == STT_OBJECT)
6570 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6573 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6575 else if (bfd_is_und_section (syms[idx]->section))
6576 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6580 else if (flags & BSF_FILE)
6581 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6584 int bind = STB_LOCAL;
6586 if (flags & BSF_LOCAL)
6588 else if (flags & BSF_GNU_UNIQUE)
6589 bind = STB_GNU_UNIQUE;
6590 else if (flags & BSF_WEAK)
6592 else if (flags & BSF_GLOBAL)
6595 sym.st_info = ELF_ST_INFO (bind, type);
6598 if (type_ptr != NULL)
6599 sym.st_other = type_ptr->internal_elf_sym.st_other;
6603 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6604 outbound_syms += bed->s->sizeof_sym;
6605 if (outbound_shndx != NULL)
6606 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6610 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6611 symstrtab_hdr->sh_type = SHT_STRTAB;
6613 symstrtab_hdr->sh_flags = 0;
6614 symstrtab_hdr->sh_addr = 0;
6615 symstrtab_hdr->sh_entsize = 0;
6616 symstrtab_hdr->sh_link = 0;
6617 symstrtab_hdr->sh_info = 0;
6618 symstrtab_hdr->sh_addralign = 1;
6623 /* Return the number of bytes required to hold the symtab vector.
6625 Note that we base it on the count plus 1, since we will null terminate
6626 the vector allocated based on this size. However, the ELF symbol table
6627 always has a dummy entry as symbol #0, so it ends up even. */
6630 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6634 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6636 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6637 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6639 symtab_size -= sizeof (asymbol *);
6645 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6649 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6651 if (elf_dynsymtab (abfd) == 0)
6653 bfd_set_error (bfd_error_invalid_operation);
6657 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6658 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6660 symtab_size -= sizeof (asymbol *);
6666 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6669 return (asect->reloc_count + 1) * sizeof (arelent *);
6672 /* Canonicalize the relocs. */
6675 _bfd_elf_canonicalize_reloc (bfd *abfd,
6682 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6684 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6687 tblptr = section->relocation;
6688 for (i = 0; i < section->reloc_count; i++)
6689 *relptr++ = tblptr++;
6693 return section->reloc_count;
6697 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6699 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6700 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6703 bfd_get_symcount (abfd) = symcount;
6708 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6709 asymbol **allocation)
6711 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6712 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6715 bfd_get_dynamic_symcount (abfd) = symcount;
6719 /* Return the size required for the dynamic reloc entries. Any loadable
6720 section that was actually installed in the BFD, and has type SHT_REL
6721 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6722 dynamic reloc section. */
6725 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6730 if (elf_dynsymtab (abfd) == 0)
6732 bfd_set_error (bfd_error_invalid_operation);
6736 ret = sizeof (arelent *);
6737 for (s = abfd->sections; s != NULL; s = s->next)
6738 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6739 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6740 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6741 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6742 * sizeof (arelent *));
6747 /* Canonicalize the dynamic relocation entries. Note that we return the
6748 dynamic relocations as a single block, although they are actually
6749 associated with particular sections; the interface, which was
6750 designed for SunOS style shared libraries, expects that there is only
6751 one set of dynamic relocs. Any loadable section that was actually
6752 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6753 dynamic symbol table, is considered to be a dynamic reloc section. */
6756 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6760 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6764 if (elf_dynsymtab (abfd) == 0)
6766 bfd_set_error (bfd_error_invalid_operation);
6770 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6772 for (s = abfd->sections; s != NULL; s = s->next)
6774 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6775 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6776 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6781 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6783 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6785 for (i = 0; i < count; i++)
6796 /* Read in the version information. */
6799 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6801 bfd_byte *contents = NULL;
6802 unsigned int freeidx = 0;
6804 if (elf_dynverref (abfd) != 0)
6806 Elf_Internal_Shdr *hdr;
6807 Elf_External_Verneed *everneed;
6808 Elf_Internal_Verneed *iverneed;
6810 bfd_byte *contents_end;
6812 hdr = &elf_tdata (abfd)->dynverref_hdr;
6814 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
6815 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
6816 if (elf_tdata (abfd)->verref == NULL)
6819 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6821 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6822 if (contents == NULL)
6824 error_return_verref:
6825 elf_tdata (abfd)->verref = NULL;
6826 elf_tdata (abfd)->cverrefs = 0;
6829 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6830 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6831 goto error_return_verref;
6833 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6834 goto error_return_verref;
6836 BFD_ASSERT (sizeof (Elf_External_Verneed)
6837 == sizeof (Elf_External_Vernaux));
6838 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6839 everneed = (Elf_External_Verneed *) contents;
6840 iverneed = elf_tdata (abfd)->verref;
6841 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6843 Elf_External_Vernaux *evernaux;
6844 Elf_Internal_Vernaux *ivernaux;
6847 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6849 iverneed->vn_bfd = abfd;
6851 iverneed->vn_filename =
6852 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6854 if (iverneed->vn_filename == NULL)
6855 goto error_return_verref;
6857 if (iverneed->vn_cnt == 0)
6858 iverneed->vn_auxptr = NULL;
6861 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
6862 bfd_alloc2 (abfd, iverneed->vn_cnt,
6863 sizeof (Elf_Internal_Vernaux));
6864 if (iverneed->vn_auxptr == NULL)
6865 goto error_return_verref;
6868 if (iverneed->vn_aux
6869 > (size_t) (contents_end - (bfd_byte *) everneed))
6870 goto error_return_verref;
6872 evernaux = ((Elf_External_Vernaux *)
6873 ((bfd_byte *) everneed + iverneed->vn_aux));
6874 ivernaux = iverneed->vn_auxptr;
6875 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6877 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6879 ivernaux->vna_nodename =
6880 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6881 ivernaux->vna_name);
6882 if (ivernaux->vna_nodename == NULL)
6883 goto error_return_verref;
6885 if (j + 1 < iverneed->vn_cnt)
6886 ivernaux->vna_nextptr = ivernaux + 1;
6888 ivernaux->vna_nextptr = NULL;
6890 if (ivernaux->vna_next
6891 > (size_t) (contents_end - (bfd_byte *) evernaux))
6892 goto error_return_verref;
6894 evernaux = ((Elf_External_Vernaux *)
6895 ((bfd_byte *) evernaux + ivernaux->vna_next));
6897 if (ivernaux->vna_other > freeidx)
6898 freeidx = ivernaux->vna_other;
6901 if (i + 1 < hdr->sh_info)
6902 iverneed->vn_nextref = iverneed + 1;
6904 iverneed->vn_nextref = NULL;
6906 if (iverneed->vn_next
6907 > (size_t) (contents_end - (bfd_byte *) everneed))
6908 goto error_return_verref;
6910 everneed = ((Elf_External_Verneed *)
6911 ((bfd_byte *) everneed + iverneed->vn_next));
6918 if (elf_dynverdef (abfd) != 0)
6920 Elf_Internal_Shdr *hdr;
6921 Elf_External_Verdef *everdef;
6922 Elf_Internal_Verdef *iverdef;
6923 Elf_Internal_Verdef *iverdefarr;
6924 Elf_Internal_Verdef iverdefmem;
6926 unsigned int maxidx;
6927 bfd_byte *contents_end_def, *contents_end_aux;
6929 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6931 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6932 if (contents == NULL)
6934 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6935 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6938 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6941 BFD_ASSERT (sizeof (Elf_External_Verdef)
6942 >= sizeof (Elf_External_Verdaux));
6943 contents_end_def = contents + hdr->sh_size
6944 - sizeof (Elf_External_Verdef);
6945 contents_end_aux = contents + hdr->sh_size
6946 - sizeof (Elf_External_Verdaux);
6948 /* We know the number of entries in the section but not the maximum
6949 index. Therefore we have to run through all entries and find
6951 everdef = (Elf_External_Verdef *) contents;
6953 for (i = 0; i < hdr->sh_info; ++i)
6955 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6957 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6958 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6960 if (iverdefmem.vd_next
6961 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6964 everdef = ((Elf_External_Verdef *)
6965 ((bfd_byte *) everdef + iverdefmem.vd_next));
6968 if (default_imported_symver)
6970 if (freeidx > maxidx)
6975 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6976 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
6977 if (elf_tdata (abfd)->verdef == NULL)
6980 elf_tdata (abfd)->cverdefs = maxidx;
6982 everdef = (Elf_External_Verdef *) contents;
6983 iverdefarr = elf_tdata (abfd)->verdef;
6984 for (i = 0; i < hdr->sh_info; i++)
6986 Elf_External_Verdaux *everdaux;
6987 Elf_Internal_Verdaux *iverdaux;
6990 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6992 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6994 error_return_verdef:
6995 elf_tdata (abfd)->verdef = NULL;
6996 elf_tdata (abfd)->cverdefs = 0;
7000 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7001 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7003 iverdef->vd_bfd = abfd;
7005 if (iverdef->vd_cnt == 0)
7006 iverdef->vd_auxptr = NULL;
7009 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7010 bfd_alloc2 (abfd, iverdef->vd_cnt,
7011 sizeof (Elf_Internal_Verdaux));
7012 if (iverdef->vd_auxptr == NULL)
7013 goto error_return_verdef;
7017 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7018 goto error_return_verdef;
7020 everdaux = ((Elf_External_Verdaux *)
7021 ((bfd_byte *) everdef + iverdef->vd_aux));
7022 iverdaux = iverdef->vd_auxptr;
7023 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7025 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7027 iverdaux->vda_nodename =
7028 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7029 iverdaux->vda_name);
7030 if (iverdaux->vda_nodename == NULL)
7031 goto error_return_verdef;
7033 if (j + 1 < iverdef->vd_cnt)
7034 iverdaux->vda_nextptr = iverdaux + 1;
7036 iverdaux->vda_nextptr = NULL;
7038 if (iverdaux->vda_next
7039 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7040 goto error_return_verdef;
7042 everdaux = ((Elf_External_Verdaux *)
7043 ((bfd_byte *) everdaux + iverdaux->vda_next));
7046 if (iverdef->vd_cnt)
7047 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7049 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7050 iverdef->vd_nextdef = iverdef + 1;
7052 iverdef->vd_nextdef = NULL;
7054 everdef = ((Elf_External_Verdef *)
7055 ((bfd_byte *) everdef + iverdef->vd_next));
7061 else if (default_imported_symver)
7068 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7069 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7070 if (elf_tdata (abfd)->verdef == NULL)
7073 elf_tdata (abfd)->cverdefs = freeidx;
7076 /* Create a default version based on the soname. */
7077 if (default_imported_symver)
7079 Elf_Internal_Verdef *iverdef;
7080 Elf_Internal_Verdaux *iverdaux;
7082 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
7084 iverdef->vd_version = VER_DEF_CURRENT;
7085 iverdef->vd_flags = 0;
7086 iverdef->vd_ndx = freeidx;
7087 iverdef->vd_cnt = 1;
7089 iverdef->vd_bfd = abfd;
7091 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7092 if (iverdef->vd_nodename == NULL)
7093 goto error_return_verdef;
7094 iverdef->vd_nextdef = NULL;
7095 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7096 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7097 if (iverdef->vd_auxptr == NULL)
7098 goto error_return_verdef;
7100 iverdaux = iverdef->vd_auxptr;
7101 iverdaux->vda_nodename = iverdef->vd_nodename;
7102 iverdaux->vda_nextptr = NULL;
7108 if (contents != NULL)
7114 _bfd_elf_make_empty_symbol (bfd *abfd)
7116 elf_symbol_type *newsym;
7117 bfd_size_type amt = sizeof (elf_symbol_type);
7119 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
7124 newsym->symbol.the_bfd = abfd;
7125 return &newsym->symbol;
7130 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7134 bfd_symbol_info (symbol, ret);
7137 /* Return whether a symbol name implies a local symbol. Most targets
7138 use this function for the is_local_label_name entry point, but some
7142 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7145 /* Normal local symbols start with ``.L''. */
7146 if (name[0] == '.' && name[1] == 'L')
7149 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7150 DWARF debugging symbols starting with ``..''. */
7151 if (name[0] == '.' && name[1] == '.')
7154 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7155 emitting DWARF debugging output. I suspect this is actually a
7156 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7157 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7158 underscore to be emitted on some ELF targets). For ease of use,
7159 we treat such symbols as local. */
7160 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7167 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7168 asymbol *symbol ATTRIBUTE_UNUSED)
7175 _bfd_elf_set_arch_mach (bfd *abfd,
7176 enum bfd_architecture arch,
7177 unsigned long machine)
7179 /* If this isn't the right architecture for this backend, and this
7180 isn't the generic backend, fail. */
7181 if (arch != get_elf_backend_data (abfd)->arch
7182 && arch != bfd_arch_unknown
7183 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7186 return bfd_default_set_arch_mach (abfd, arch, machine);
7189 /* Find the function to a particular section and offset,
7190 for error reporting. */
7193 elf_find_function (bfd *abfd,
7197 const char **filename_ptr,
7198 const char **functionname_ptr)
7200 const char *filename;
7201 asymbol *func, *file;
7204 /* ??? Given multiple file symbols, it is impossible to reliably
7205 choose the right file name for global symbols. File symbols are
7206 local symbols, and thus all file symbols must sort before any
7207 global symbols. The ELF spec may be interpreted to say that a
7208 file symbol must sort before other local symbols, but currently
7209 ld -r doesn't do this. So, for ld -r output, it is possible to
7210 make a better choice of file name for local symbols by ignoring
7211 file symbols appearing after a given local symbol. */
7212 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7213 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7219 state = nothing_seen;
7221 for (p = symbols; *p != NULL; p++)
7226 q = (elf_symbol_type *) *p;
7228 type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
7233 if (state == symbol_seen)
7234 state = file_after_symbol_seen;
7237 if (!bed->is_function_type (type))
7240 if (bfd_get_section (&q->symbol) == section
7241 && q->symbol.value >= low_func
7242 && q->symbol.value <= offset)
7244 func = (asymbol *) q;
7245 low_func = q->symbol.value;
7248 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7249 || state != file_after_symbol_seen))
7250 filename = bfd_asymbol_name (file);
7254 if (state == nothing_seen)
7255 state = symbol_seen;
7262 *filename_ptr = filename;
7263 if (functionname_ptr)
7264 *functionname_ptr = bfd_asymbol_name (func);
7269 /* Find the nearest line to a particular section and offset,
7270 for error reporting. */
7273 _bfd_elf_find_nearest_line (bfd *abfd,
7277 const char **filename_ptr,
7278 const char **functionname_ptr,
7279 unsigned int *line_ptr)
7283 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7284 filename_ptr, functionname_ptr,
7287 if (!*functionname_ptr)
7288 elf_find_function (abfd, section, symbols, offset,
7289 *filename_ptr ? NULL : filename_ptr,
7295 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7296 filename_ptr, functionname_ptr,
7298 &elf_tdata (abfd)->dwarf2_find_line_info))
7300 if (!*functionname_ptr)
7301 elf_find_function (abfd, section, symbols, offset,
7302 *filename_ptr ? NULL : filename_ptr,
7308 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7309 &found, filename_ptr,
7310 functionname_ptr, line_ptr,
7311 &elf_tdata (abfd)->line_info))
7313 if (found && (*functionname_ptr || *line_ptr))
7316 if (symbols == NULL)
7319 if (! elf_find_function (abfd, section, symbols, offset,
7320 filename_ptr, functionname_ptr))
7327 /* Find the line for a symbol. */
7330 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7331 const char **filename_ptr, unsigned int *line_ptr)
7333 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7334 filename_ptr, line_ptr, 0,
7335 &elf_tdata (abfd)->dwarf2_find_line_info);
7338 /* After a call to bfd_find_nearest_line, successive calls to
7339 bfd_find_inliner_info can be used to get source information about
7340 each level of function inlining that terminated at the address
7341 passed to bfd_find_nearest_line. Currently this is only supported
7342 for DWARF2 with appropriate DWARF3 extensions. */
7345 _bfd_elf_find_inliner_info (bfd *abfd,
7346 const char **filename_ptr,
7347 const char **functionname_ptr,
7348 unsigned int *line_ptr)
7351 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7352 functionname_ptr, line_ptr,
7353 & elf_tdata (abfd)->dwarf2_find_line_info);
7358 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7360 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7361 int ret = bed->s->sizeof_ehdr;
7363 if (!info->relocatable)
7365 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7367 if (phdr_size == (bfd_size_type) -1)
7369 struct elf_segment_map *m;
7372 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7373 phdr_size += bed->s->sizeof_phdr;
7376 phdr_size = get_program_header_size (abfd, info);
7379 elf_tdata (abfd)->program_header_size = phdr_size;
7387 _bfd_elf_set_section_contents (bfd *abfd,
7389 const void *location,
7391 bfd_size_type count)
7393 Elf_Internal_Shdr *hdr;
7396 if (! abfd->output_has_begun
7397 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7400 hdr = &elf_section_data (section)->this_hdr;
7401 pos = hdr->sh_offset + offset;
7402 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7403 || bfd_bwrite (location, count, abfd) != count)
7410 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7411 arelent *cache_ptr ATTRIBUTE_UNUSED,
7412 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7417 /* Try to convert a non-ELF reloc into an ELF one. */
7420 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7422 /* Check whether we really have an ELF howto. */
7424 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7426 bfd_reloc_code_real_type code;
7427 reloc_howto_type *howto;
7429 /* Alien reloc: Try to determine its type to replace it with an
7430 equivalent ELF reloc. */
7432 if (areloc->howto->pc_relative)
7434 switch (areloc->howto->bitsize)
7437 code = BFD_RELOC_8_PCREL;
7440 code = BFD_RELOC_12_PCREL;
7443 code = BFD_RELOC_16_PCREL;
7446 code = BFD_RELOC_24_PCREL;
7449 code = BFD_RELOC_32_PCREL;
7452 code = BFD_RELOC_64_PCREL;
7458 howto = bfd_reloc_type_lookup (abfd, code);
7460 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7462 if (howto->pcrel_offset)
7463 areloc->addend += areloc->address;
7465 areloc->addend -= areloc->address; /* addend is unsigned!! */
7470 switch (areloc->howto->bitsize)
7476 code = BFD_RELOC_14;
7479 code = BFD_RELOC_16;
7482 code = BFD_RELOC_26;
7485 code = BFD_RELOC_32;
7488 code = BFD_RELOC_64;
7494 howto = bfd_reloc_type_lookup (abfd, code);
7498 areloc->howto = howto;
7506 (*_bfd_error_handler)
7507 (_("%B: unsupported relocation type %s"),
7508 abfd, areloc->howto->name);
7509 bfd_set_error (bfd_error_bad_value);
7514 _bfd_elf_close_and_cleanup (bfd *abfd)
7516 if (bfd_get_format (abfd) == bfd_object)
7518 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7519 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7520 _bfd_dwarf2_cleanup_debug_info (abfd);
7523 return _bfd_generic_close_and_cleanup (abfd);
7526 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7527 in the relocation's offset. Thus we cannot allow any sort of sanity
7528 range-checking to interfere. There is nothing else to do in processing
7531 bfd_reloc_status_type
7532 _bfd_elf_rel_vtable_reloc_fn
7533 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7534 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7535 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7536 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7538 return bfd_reloc_ok;
7541 /* Elf core file support. Much of this only works on native
7542 toolchains, since we rely on knowing the
7543 machine-dependent procfs structure in order to pick
7544 out details about the corefile. */
7546 #ifdef HAVE_SYS_PROCFS_H
7547 /* Needed for new procfs interface on sparc-solaris. */
7548 # define _STRUCTURED_PROC 1
7549 # include <sys/procfs.h>
7552 /* Return a PID that identifies a "thread" for threaded cores, or the
7553 PID of the main process for non-threaded cores. */
7556 elfcore_make_pid (bfd *abfd)
7560 pid = elf_tdata (abfd)->core_lwpid;
7562 pid = elf_tdata (abfd)->core_pid;
7567 /* If there isn't a section called NAME, make one, using
7568 data from SECT. Note, this function will generate a
7569 reference to NAME, so you shouldn't deallocate or
7573 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7577 if (bfd_get_section_by_name (abfd, name) != NULL)
7580 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7584 sect2->size = sect->size;
7585 sect2->filepos = sect->filepos;
7586 sect2->alignment_power = sect->alignment_power;
7590 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7591 actually creates up to two pseudosections:
7592 - For the single-threaded case, a section named NAME, unless
7593 such a section already exists.
7594 - For the multi-threaded case, a section named "NAME/PID", where
7595 PID is elfcore_make_pid (abfd).
7596 Both pseudosections have identical contents. */
7598 _bfd_elfcore_make_pseudosection (bfd *abfd,
7604 char *threaded_name;
7608 /* Build the section name. */
7610 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7611 len = strlen (buf) + 1;
7612 threaded_name = (char *) bfd_alloc (abfd, len);
7613 if (threaded_name == NULL)
7615 memcpy (threaded_name, buf, len);
7617 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7622 sect->filepos = filepos;
7623 sect->alignment_power = 2;
7625 return elfcore_maybe_make_sect (abfd, name, sect);
7628 /* prstatus_t exists on:
7630 linux 2.[01] + glibc
7634 #if defined (HAVE_PRSTATUS_T)
7637 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7642 if (note->descsz == sizeof (prstatus_t))
7646 size = sizeof (prstat.pr_reg);
7647 offset = offsetof (prstatus_t, pr_reg);
7648 memcpy (&prstat, note->descdata, sizeof (prstat));
7650 /* Do not overwrite the core signal if it
7651 has already been set by another thread. */
7652 if (elf_tdata (abfd)->core_signal == 0)
7653 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7654 if (elf_tdata (abfd)->core_pid == 0)
7655 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7657 /* pr_who exists on:
7660 pr_who doesn't exist on:
7663 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7664 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7666 elf_tdata (abfd)->core_lwpid = prstat.pr_pid;
7669 #if defined (HAVE_PRSTATUS32_T)
7670 else if (note->descsz == sizeof (prstatus32_t))
7672 /* 64-bit host, 32-bit corefile */
7673 prstatus32_t prstat;
7675 size = sizeof (prstat.pr_reg);
7676 offset = offsetof (prstatus32_t, pr_reg);
7677 memcpy (&prstat, note->descdata, sizeof (prstat));
7679 /* Do not overwrite the core signal if it
7680 has already been set by another thread. */
7681 if (elf_tdata (abfd)->core_signal == 0)
7682 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7683 if (elf_tdata (abfd)->core_pid == 0)
7684 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7686 /* pr_who exists on:
7689 pr_who doesn't exist on:
7692 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7693 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7695 elf_tdata (abfd)->core_lwpid = prstat.pr_pid;
7698 #endif /* HAVE_PRSTATUS32_T */
7701 /* Fail - we don't know how to handle any other
7702 note size (ie. data object type). */
7706 /* Make a ".reg/999" section and a ".reg" section. */
7707 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7708 size, note->descpos + offset);
7710 #endif /* defined (HAVE_PRSTATUS_T) */
7712 /* Create a pseudosection containing the exact contents of NOTE. */
7714 elfcore_make_note_pseudosection (bfd *abfd,
7716 Elf_Internal_Note *note)
7718 return _bfd_elfcore_make_pseudosection (abfd, name,
7719 note->descsz, note->descpos);
7722 /* There isn't a consistent prfpregset_t across platforms,
7723 but it doesn't matter, because we don't have to pick this
7724 data structure apart. */
7727 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7729 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7732 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7733 type of NT_PRXFPREG. Just include the whole note's contents
7737 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7739 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7742 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
7743 with a note type of NT_X86_XSTATE. Just include the whole note's
7744 contents literally. */
7747 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
7749 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
7753 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7755 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7759 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7761 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7765 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
7767 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
7771 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
7773 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
7777 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
7779 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
7783 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
7785 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
7789 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
7791 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
7795 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
7797 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
7800 #if defined (HAVE_PRPSINFO_T)
7801 typedef prpsinfo_t elfcore_psinfo_t;
7802 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7803 typedef prpsinfo32_t elfcore_psinfo32_t;
7807 #if defined (HAVE_PSINFO_T)
7808 typedef psinfo_t elfcore_psinfo_t;
7809 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7810 typedef psinfo32_t elfcore_psinfo32_t;
7814 /* return a malloc'ed copy of a string at START which is at
7815 most MAX bytes long, possibly without a terminating '\0'.
7816 the copy will always have a terminating '\0'. */
7819 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7822 char *end = (char *) memchr (start, '\0', max);
7830 dups = (char *) bfd_alloc (abfd, len + 1);
7834 memcpy (dups, start, len);
7840 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7842 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7844 if (note->descsz == sizeof (elfcore_psinfo_t))
7846 elfcore_psinfo_t psinfo;
7848 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7850 elf_tdata (abfd)->core_program
7851 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7852 sizeof (psinfo.pr_fname));
7854 elf_tdata (abfd)->core_command
7855 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7856 sizeof (psinfo.pr_psargs));
7858 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7859 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7861 /* 64-bit host, 32-bit corefile */
7862 elfcore_psinfo32_t psinfo;
7864 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7866 elf_tdata (abfd)->core_program
7867 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7868 sizeof (psinfo.pr_fname));
7870 elf_tdata (abfd)->core_command
7871 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7872 sizeof (psinfo.pr_psargs));
7878 /* Fail - we don't know how to handle any other
7879 note size (ie. data object type). */
7883 /* Note that for some reason, a spurious space is tacked
7884 onto the end of the args in some (at least one anyway)
7885 implementations, so strip it off if it exists. */
7888 char *command = elf_tdata (abfd)->core_command;
7889 int n = strlen (command);
7891 if (0 < n && command[n - 1] == ' ')
7892 command[n - 1] = '\0';
7897 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7899 #if defined (HAVE_PSTATUS_T)
7901 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7903 if (note->descsz == sizeof (pstatus_t)
7904 #if defined (HAVE_PXSTATUS_T)
7905 || note->descsz == sizeof (pxstatus_t)
7911 memcpy (&pstat, note->descdata, sizeof (pstat));
7913 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7915 #if defined (HAVE_PSTATUS32_T)
7916 else if (note->descsz == sizeof (pstatus32_t))
7918 /* 64-bit host, 32-bit corefile */
7921 memcpy (&pstat, note->descdata, sizeof (pstat));
7923 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7926 /* Could grab some more details from the "representative"
7927 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7928 NT_LWPSTATUS note, presumably. */
7932 #endif /* defined (HAVE_PSTATUS_T) */
7934 #if defined (HAVE_LWPSTATUS_T)
7936 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7938 lwpstatus_t lwpstat;
7944 if (note->descsz != sizeof (lwpstat)
7945 #if defined (HAVE_LWPXSTATUS_T)
7946 && note->descsz != sizeof (lwpxstatus_t)
7951 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7953 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7954 /* Do not overwrite the core signal if it has already been set by
7956 if (elf_tdata (abfd)->core_signal == 0)
7957 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7959 /* Make a ".reg/999" section. */
7961 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7962 len = strlen (buf) + 1;
7963 name = bfd_alloc (abfd, len);
7966 memcpy (name, buf, len);
7968 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7972 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7973 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7974 sect->filepos = note->descpos
7975 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7978 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7979 sect->size = sizeof (lwpstat.pr_reg);
7980 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7983 sect->alignment_power = 2;
7985 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7988 /* Make a ".reg2/999" section */
7990 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7991 len = strlen (buf) + 1;
7992 name = bfd_alloc (abfd, len);
7995 memcpy (name, buf, len);
7997 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8001 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8002 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8003 sect->filepos = note->descpos
8004 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8007 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8008 sect->size = sizeof (lwpstat.pr_fpreg);
8009 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8012 sect->alignment_power = 2;
8014 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8016 #endif /* defined (HAVE_LWPSTATUS_T) */
8019 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8026 int is_active_thread;
8029 if (note->descsz < 728)
8032 if (! CONST_STRNEQ (note->namedata, "win32"))
8035 type = bfd_get_32 (abfd, note->descdata);
8039 case 1 /* NOTE_INFO_PROCESS */:
8040 /* FIXME: need to add ->core_command. */
8041 /* process_info.pid */
8042 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
8043 /* process_info.signal */
8044 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
8047 case 2 /* NOTE_INFO_THREAD */:
8048 /* Make a ".reg/999" section. */
8049 /* thread_info.tid */
8050 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8052 len = strlen (buf) + 1;
8053 name = (char *) bfd_alloc (abfd, len);
8057 memcpy (name, buf, len);
8059 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8063 /* sizeof (thread_info.thread_context) */
8065 /* offsetof (thread_info.thread_context) */
8066 sect->filepos = note->descpos + 12;
8067 sect->alignment_power = 2;
8069 /* thread_info.is_active_thread */
8070 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8072 if (is_active_thread)
8073 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8077 case 3 /* NOTE_INFO_MODULE */:
8078 /* Make a ".module/xxxxxxxx" section. */
8079 /* module_info.base_address */
8080 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8081 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8083 len = strlen (buf) + 1;
8084 name = (char *) bfd_alloc (abfd, len);
8088 memcpy (name, buf, len);
8090 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8095 sect->size = note->descsz;
8096 sect->filepos = note->descpos;
8097 sect->alignment_power = 2;
8108 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8110 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8118 if (bed->elf_backend_grok_prstatus)
8119 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8121 #if defined (HAVE_PRSTATUS_T)
8122 return elfcore_grok_prstatus (abfd, note);
8127 #if defined (HAVE_PSTATUS_T)
8129 return elfcore_grok_pstatus (abfd, note);
8132 #if defined (HAVE_LWPSTATUS_T)
8134 return elfcore_grok_lwpstatus (abfd, note);
8137 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8138 return elfcore_grok_prfpreg (abfd, note);
8140 case NT_WIN32PSTATUS:
8141 return elfcore_grok_win32pstatus (abfd, note);
8143 case NT_PRXFPREG: /* Linux SSE extension */
8144 if (note->namesz == 6
8145 && strcmp (note->namedata, "LINUX") == 0)
8146 return elfcore_grok_prxfpreg (abfd, note);
8150 case NT_X86_XSTATE: /* Linux XSAVE extension */
8151 if (note->namesz == 6
8152 && strcmp (note->namedata, "LINUX") == 0)
8153 return elfcore_grok_xstatereg (abfd, note);
8158 if (note->namesz == 6
8159 && strcmp (note->namedata, "LINUX") == 0)
8160 return elfcore_grok_ppc_vmx (abfd, note);
8165 if (note->namesz == 6
8166 && strcmp (note->namedata, "LINUX") == 0)
8167 return elfcore_grok_ppc_vsx (abfd, note);
8171 case NT_S390_HIGH_GPRS:
8172 if (note->namesz == 6
8173 && strcmp (note->namedata, "LINUX") == 0)
8174 return elfcore_grok_s390_high_gprs (abfd, note);
8179 if (note->namesz == 6
8180 && strcmp (note->namedata, "LINUX") == 0)
8181 return elfcore_grok_s390_timer (abfd, note);
8185 case NT_S390_TODCMP:
8186 if (note->namesz == 6
8187 && strcmp (note->namedata, "LINUX") == 0)
8188 return elfcore_grok_s390_todcmp (abfd, note);
8192 case NT_S390_TODPREG:
8193 if (note->namesz == 6
8194 && strcmp (note->namedata, "LINUX") == 0)
8195 return elfcore_grok_s390_todpreg (abfd, note);
8200 if (note->namesz == 6
8201 && strcmp (note->namedata, "LINUX") == 0)
8202 return elfcore_grok_s390_ctrs (abfd, note);
8206 case NT_S390_PREFIX:
8207 if (note->namesz == 6
8208 && strcmp (note->namedata, "LINUX") == 0)
8209 return elfcore_grok_s390_prefix (abfd, note);
8215 if (bed->elf_backend_grok_psinfo)
8216 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8218 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8219 return elfcore_grok_psinfo (abfd, note);
8226 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8231 sect->size = note->descsz;
8232 sect->filepos = note->descpos;
8233 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8241 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8243 elf_tdata (abfd)->build_id_size = note->descsz;
8244 elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz);
8245 if (elf_tdata (abfd)->build_id == NULL)
8248 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
8254 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8261 case NT_GNU_BUILD_ID:
8262 return elfobj_grok_gnu_build_id (abfd, note);
8267 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8271 cp = strchr (note->namedata, '@');
8274 *lwpidp = atoi(cp + 1);
8281 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8283 /* Signal number at offset 0x08. */
8284 elf_tdata (abfd)->core_signal
8285 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8287 /* Process ID at offset 0x50. */
8288 elf_tdata (abfd)->core_pid
8289 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8291 /* Command name at 0x7c (max 32 bytes, including nul). */
8292 elf_tdata (abfd)->core_command
8293 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8295 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8300 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8304 if (elfcore_netbsd_get_lwpid (note, &lwp))
8305 elf_tdata (abfd)->core_lwpid = lwp;
8307 if (note->type == NT_NETBSDCORE_PROCINFO)
8309 /* NetBSD-specific core "procinfo". Note that we expect to
8310 find this note before any of the others, which is fine,
8311 since the kernel writes this note out first when it
8312 creates a core file. */
8314 return elfcore_grok_netbsd_procinfo (abfd, note);
8317 /* As of Jan 2002 there are no other machine-independent notes
8318 defined for NetBSD core files. If the note type is less
8319 than the start of the machine-dependent note types, we don't
8322 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8326 switch (bfd_get_arch (abfd))
8328 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8329 PT_GETFPREGS == mach+2. */
8331 case bfd_arch_alpha:
8332 case bfd_arch_sparc:
8335 case NT_NETBSDCORE_FIRSTMACH+0:
8336 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8338 case NT_NETBSDCORE_FIRSTMACH+2:
8339 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8345 /* On all other arch's, PT_GETREGS == mach+1 and
8346 PT_GETFPREGS == mach+3. */
8351 case NT_NETBSDCORE_FIRSTMACH+1:
8352 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8354 case NT_NETBSDCORE_FIRSTMACH+3:
8355 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8365 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8367 /* Signal number at offset 0x08. */
8368 elf_tdata (abfd)->core_signal
8369 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8371 /* Process ID at offset 0x20. */
8372 elf_tdata (abfd)->core_pid
8373 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8375 /* Command name at 0x48 (max 32 bytes, including nul). */
8376 elf_tdata (abfd)->core_command
8377 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8383 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8385 if (note->type == NT_OPENBSD_PROCINFO)
8386 return elfcore_grok_openbsd_procinfo (abfd, note);
8388 if (note->type == NT_OPENBSD_REGS)
8389 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8391 if (note->type == NT_OPENBSD_FPREGS)
8392 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8394 if (note->type == NT_OPENBSD_XFPREGS)
8395 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8397 if (note->type == NT_OPENBSD_AUXV)
8399 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8404 sect->size = note->descsz;
8405 sect->filepos = note->descpos;
8406 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8411 if (note->type == NT_OPENBSD_WCOOKIE)
8413 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8418 sect->size = note->descsz;
8419 sect->filepos = note->descpos;
8420 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8429 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8431 void *ddata = note->descdata;
8438 /* nto_procfs_status 'pid' field is at offset 0. */
8439 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8441 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8442 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8444 /* nto_procfs_status 'flags' field is at offset 8. */
8445 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8447 /* nto_procfs_status 'what' field is at offset 14. */
8448 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8450 elf_tdata (abfd)->core_signal = sig;
8451 elf_tdata (abfd)->core_lwpid = *tid;
8454 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8455 do not come from signals so we make sure we set the current
8456 thread just in case. */
8457 if (flags & 0x00000080)
8458 elf_tdata (abfd)->core_lwpid = *tid;
8460 /* Make a ".qnx_core_status/%d" section. */
8461 sprintf (buf, ".qnx_core_status/%ld", *tid);
8463 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8468 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8472 sect->size = note->descsz;
8473 sect->filepos = note->descpos;
8474 sect->alignment_power = 2;
8476 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8480 elfcore_grok_nto_regs (bfd *abfd,
8481 Elf_Internal_Note *note,
8489 /* Make a "(base)/%d" section. */
8490 sprintf (buf, "%s/%ld", base, tid);
8492 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8497 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8501 sect->size = note->descsz;
8502 sect->filepos = note->descpos;
8503 sect->alignment_power = 2;
8505 /* This is the current thread. */
8506 if (elf_tdata (abfd)->core_lwpid == tid)
8507 return elfcore_maybe_make_sect (abfd, base, sect);
8512 #define BFD_QNT_CORE_INFO 7
8513 #define BFD_QNT_CORE_STATUS 8
8514 #define BFD_QNT_CORE_GREG 9
8515 #define BFD_QNT_CORE_FPREG 10
8518 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8520 /* Every GREG section has a STATUS section before it. Store the
8521 tid from the previous call to pass down to the next gregs
8523 static long tid = 1;
8527 case BFD_QNT_CORE_INFO:
8528 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8529 case BFD_QNT_CORE_STATUS:
8530 return elfcore_grok_nto_status (abfd, note, &tid);
8531 case BFD_QNT_CORE_GREG:
8532 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8533 case BFD_QNT_CORE_FPREG:
8534 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8541 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8547 /* Use note name as section name. */
8549 name = (char *) bfd_alloc (abfd, len);
8552 memcpy (name, note->namedata, len);
8553 name[len - 1] = '\0';
8555 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8559 sect->size = note->descsz;
8560 sect->filepos = note->descpos;
8561 sect->alignment_power = 1;
8566 /* Function: elfcore_write_note
8569 buffer to hold note, and current size of buffer
8573 size of data for note
8575 Writes note to end of buffer. ELF64 notes are written exactly as
8576 for ELF32, despite the current (as of 2006) ELF gabi specifying
8577 that they ought to have 8-byte namesz and descsz field, and have
8578 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8581 Pointer to realloc'd buffer, *BUFSIZ updated. */
8584 elfcore_write_note (bfd *abfd,
8592 Elf_External_Note *xnp;
8599 namesz = strlen (name) + 1;
8601 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8603 buf = (char *) realloc (buf, *bufsiz + newspace);
8606 dest = buf + *bufsiz;
8607 *bufsiz += newspace;
8608 xnp = (Elf_External_Note *) dest;
8609 H_PUT_32 (abfd, namesz, xnp->namesz);
8610 H_PUT_32 (abfd, size, xnp->descsz);
8611 H_PUT_32 (abfd, type, xnp->type);
8615 memcpy (dest, name, namesz);
8623 memcpy (dest, input, size);
8633 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8635 elfcore_write_prpsinfo (bfd *abfd,
8641 const char *note_name = "CORE";
8642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8644 if (bed->elf_backend_write_core_note != NULL)
8647 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8648 NT_PRPSINFO, fname, psargs);
8653 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8654 if (bed->s->elfclass == ELFCLASS32)
8656 #if defined (HAVE_PSINFO32_T)
8658 int note_type = NT_PSINFO;
8661 int note_type = NT_PRPSINFO;
8664 memset (&data, 0, sizeof (data));
8665 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8666 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8667 return elfcore_write_note (abfd, buf, bufsiz,
8668 note_name, note_type, &data, sizeof (data));
8673 #if defined (HAVE_PSINFO_T)
8675 int note_type = NT_PSINFO;
8678 int note_type = NT_PRPSINFO;
8681 memset (&data, 0, sizeof (data));
8682 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8683 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8684 return elfcore_write_note (abfd, buf, bufsiz,
8685 note_name, note_type, &data, sizeof (data));
8688 #endif /* PSINFO_T or PRPSINFO_T */
8690 #if defined (HAVE_PRSTATUS_T)
8692 elfcore_write_prstatus (bfd *abfd,
8699 const char *note_name = "CORE";
8700 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8702 if (bed->elf_backend_write_core_note != NULL)
8705 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8707 pid, cursig, gregs);
8712 #if defined (HAVE_PRSTATUS32_T)
8713 if (bed->s->elfclass == ELFCLASS32)
8715 prstatus32_t prstat;
8717 memset (&prstat, 0, sizeof (prstat));
8718 prstat.pr_pid = pid;
8719 prstat.pr_cursig = cursig;
8720 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8721 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8722 NT_PRSTATUS, &prstat, sizeof (prstat));
8729 memset (&prstat, 0, sizeof (prstat));
8730 prstat.pr_pid = pid;
8731 prstat.pr_cursig = cursig;
8732 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8733 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8734 NT_PRSTATUS, &prstat, sizeof (prstat));
8737 #endif /* HAVE_PRSTATUS_T */
8739 #if defined (HAVE_LWPSTATUS_T)
8741 elfcore_write_lwpstatus (bfd *abfd,
8748 lwpstatus_t lwpstat;
8749 const char *note_name = "CORE";
8751 memset (&lwpstat, 0, sizeof (lwpstat));
8752 lwpstat.pr_lwpid = pid >> 16;
8753 lwpstat.pr_cursig = cursig;
8754 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8755 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8756 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8758 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8759 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8761 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8762 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8765 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8766 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8768 #endif /* HAVE_LWPSTATUS_T */
8770 #if defined (HAVE_PSTATUS_T)
8772 elfcore_write_pstatus (bfd *abfd,
8776 int cursig ATTRIBUTE_UNUSED,
8777 const void *gregs ATTRIBUTE_UNUSED)
8779 const char *note_name = "CORE";
8780 #if defined (HAVE_PSTATUS32_T)
8781 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8783 if (bed->s->elfclass == ELFCLASS32)
8787 memset (&pstat, 0, sizeof (pstat));
8788 pstat.pr_pid = pid & 0xffff;
8789 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8790 NT_PSTATUS, &pstat, sizeof (pstat));
8798 memset (&pstat, 0, sizeof (pstat));
8799 pstat.pr_pid = pid & 0xffff;
8800 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8801 NT_PSTATUS, &pstat, sizeof (pstat));
8805 #endif /* HAVE_PSTATUS_T */
8808 elfcore_write_prfpreg (bfd *abfd,
8814 const char *note_name = "CORE";
8815 return elfcore_write_note (abfd, buf, bufsiz,
8816 note_name, NT_FPREGSET, fpregs, size);
8820 elfcore_write_prxfpreg (bfd *abfd,
8823 const void *xfpregs,
8826 char *note_name = "LINUX";
8827 return elfcore_write_note (abfd, buf, bufsiz,
8828 note_name, NT_PRXFPREG, xfpregs, size);
8832 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
8833 const void *xfpregs, int size)
8835 char *note_name = "LINUX";
8836 return elfcore_write_note (abfd, buf, bufsiz,
8837 note_name, NT_X86_XSTATE, xfpregs, size);
8841 elfcore_write_ppc_vmx (bfd *abfd,
8844 const void *ppc_vmx,
8847 char *note_name = "LINUX";
8848 return elfcore_write_note (abfd, buf, bufsiz,
8849 note_name, NT_PPC_VMX, ppc_vmx, size);
8853 elfcore_write_ppc_vsx (bfd *abfd,
8856 const void *ppc_vsx,
8859 char *note_name = "LINUX";
8860 return elfcore_write_note (abfd, buf, bufsiz,
8861 note_name, NT_PPC_VSX, ppc_vsx, size);
8865 elfcore_write_s390_high_gprs (bfd *abfd,
8868 const void *s390_high_gprs,
8871 char *note_name = "LINUX";
8872 return elfcore_write_note (abfd, buf, bufsiz,
8873 note_name, NT_S390_HIGH_GPRS,
8874 s390_high_gprs, size);
8878 elfcore_write_s390_timer (bfd *abfd,
8881 const void *s390_timer,
8884 char *note_name = "LINUX";
8885 return elfcore_write_note (abfd, buf, bufsiz,
8886 note_name, NT_S390_TIMER, s390_timer, size);
8890 elfcore_write_s390_todcmp (bfd *abfd,
8893 const void *s390_todcmp,
8896 char *note_name = "LINUX";
8897 return elfcore_write_note (abfd, buf, bufsiz,
8898 note_name, NT_S390_TODCMP, s390_todcmp, size);
8902 elfcore_write_s390_todpreg (bfd *abfd,
8905 const void *s390_todpreg,
8908 char *note_name = "LINUX";
8909 return elfcore_write_note (abfd, buf, bufsiz,
8910 note_name, NT_S390_TODPREG, s390_todpreg, size);
8914 elfcore_write_s390_ctrs (bfd *abfd,
8917 const void *s390_ctrs,
8920 char *note_name = "LINUX";
8921 return elfcore_write_note (abfd, buf, bufsiz,
8922 note_name, NT_S390_CTRS, s390_ctrs, size);
8926 elfcore_write_s390_prefix (bfd *abfd,
8929 const void *s390_prefix,
8932 char *note_name = "LINUX";
8933 return elfcore_write_note (abfd, buf, bufsiz,
8934 note_name, NT_S390_PREFIX, s390_prefix, size);
8938 elfcore_write_register_note (bfd *abfd,
8941 const char *section,
8945 if (strcmp (section, ".reg2") == 0)
8946 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8947 if (strcmp (section, ".reg-xfp") == 0)
8948 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8949 if (strcmp (section, ".reg-xstate") == 0)
8950 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
8951 if (strcmp (section, ".reg-ppc-vmx") == 0)
8952 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8953 if (strcmp (section, ".reg-ppc-vsx") == 0)
8954 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8955 if (strcmp (section, ".reg-s390-high-gprs") == 0)
8956 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
8957 if (strcmp (section, ".reg-s390-timer") == 0)
8958 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
8959 if (strcmp (section, ".reg-s390-todcmp") == 0)
8960 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
8961 if (strcmp (section, ".reg-s390-todpreg") == 0)
8962 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
8963 if (strcmp (section, ".reg-s390-ctrs") == 0)
8964 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
8965 if (strcmp (section, ".reg-s390-prefix") == 0)
8966 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
8971 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8976 while (p < buf + size)
8978 /* FIXME: bad alignment assumption. */
8979 Elf_External_Note *xnp = (Elf_External_Note *) p;
8980 Elf_Internal_Note in;
8982 if (offsetof (Elf_External_Note, name) > buf - p + size)
8985 in.type = H_GET_32 (abfd, xnp->type);
8987 in.namesz = H_GET_32 (abfd, xnp->namesz);
8988 in.namedata = xnp->name;
8989 if (in.namesz > buf - in.namedata + size)
8992 in.descsz = H_GET_32 (abfd, xnp->descsz);
8993 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8994 in.descpos = offset + (in.descdata - buf);
8996 && (in.descdata >= buf + size
8997 || in.descsz > buf - in.descdata + size))
9000 switch (bfd_get_format (abfd))
9006 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
9008 if (! elfcore_grok_netbsd_note (abfd, &in))
9011 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
9013 if (! elfcore_grok_openbsd_note (abfd, &in))
9016 else if (CONST_STRNEQ (in.namedata, "QNX"))
9018 if (! elfcore_grok_nto_note (abfd, &in))
9021 else if (CONST_STRNEQ (in.namedata, "SPU/"))
9023 if (! elfcore_grok_spu_note (abfd, &in))
9028 if (! elfcore_grok_note (abfd, &in))
9034 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9036 if (! elfobj_grok_gnu_note (abfd, &in))
9042 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9049 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9056 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9059 buf = (char *) bfd_malloc (size);
9063 if (bfd_bread (buf, size, abfd) != size
9064 || !elf_parse_notes (abfd, buf, size, offset))
9074 /* Providing external access to the ELF program header table. */
9076 /* Return an upper bound on the number of bytes required to store a
9077 copy of ABFD's program header table entries. Return -1 if an error
9078 occurs; bfd_get_error will return an appropriate code. */
9081 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9083 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9085 bfd_set_error (bfd_error_wrong_format);
9089 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9092 /* Copy ABFD's program header table entries to *PHDRS. The entries
9093 will be stored as an array of Elf_Internal_Phdr structures, as
9094 defined in include/elf/internal.h. To find out how large the
9095 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9097 Return the number of program header table entries read, or -1 if an
9098 error occurs; bfd_get_error will return an appropriate code. */
9101 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9105 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9107 bfd_set_error (bfd_error_wrong_format);
9111 num_phdrs = elf_elfheader (abfd)->e_phnum;
9112 memcpy (phdrs, elf_tdata (abfd)->phdr,
9113 num_phdrs * sizeof (Elf_Internal_Phdr));
9118 enum elf_reloc_type_class
9119 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9121 return reloc_class_normal;
9124 /* For RELA architectures, return the relocation value for a
9125 relocation against a local symbol. */
9128 _bfd_elf_rela_local_sym (bfd *abfd,
9129 Elf_Internal_Sym *sym,
9131 Elf_Internal_Rela *rel)
9133 asection *sec = *psec;
9136 relocation = (sec->output_section->vma
9137 + sec->output_offset
9139 if ((sec->flags & SEC_MERGE)
9140 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9141 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
9144 _bfd_merged_section_offset (abfd, psec,
9145 elf_section_data (sec)->sec_info,
9146 sym->st_value + rel->r_addend);
9149 /* If we have changed the section, and our original section is
9150 marked with SEC_EXCLUDE, it means that the original
9151 SEC_MERGE section has been completely subsumed in some
9152 other SEC_MERGE section. In this case, we need to leave
9153 some info around for --emit-relocs. */
9154 if ((sec->flags & SEC_EXCLUDE) != 0)
9155 sec->kept_section = *psec;
9158 rel->r_addend -= relocation;
9159 rel->r_addend += sec->output_section->vma + sec->output_offset;
9165 _bfd_elf_rel_local_sym (bfd *abfd,
9166 Elf_Internal_Sym *sym,
9170 asection *sec = *psec;
9172 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
9173 return sym->st_value + addend;
9175 return _bfd_merged_section_offset (abfd, psec,
9176 elf_section_data (sec)->sec_info,
9177 sym->st_value + addend);
9181 _bfd_elf_section_offset (bfd *abfd,
9182 struct bfd_link_info *info,
9186 switch (sec->sec_info_type)
9188 case ELF_INFO_TYPE_STABS:
9189 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9191 case ELF_INFO_TYPE_EH_FRAME:
9192 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9198 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9199 reconstruct an ELF file by reading the segments out of remote memory
9200 based on the ELF file header at EHDR_VMA and the ELF program headers it
9201 points to. If not null, *LOADBASEP is filled in with the difference
9202 between the VMAs from which the segments were read, and the VMAs the
9203 file headers (and hence BFD's idea of each section's VMA) put them at.
9205 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9206 remote memory at target address VMA into the local buffer at MYADDR; it
9207 should return zero on success or an `errno' code on failure. TEMPL must
9208 be a BFD for an ELF target with the word size and byte order found in
9209 the remote memory. */
9212 bfd_elf_bfd_from_remote_memory
9216 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
9218 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9219 (templ, ehdr_vma, loadbasep, target_read_memory);
9223 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9224 long symcount ATTRIBUTE_UNUSED,
9225 asymbol **syms ATTRIBUTE_UNUSED,
9230 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9233 const char *relplt_name;
9234 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9238 Elf_Internal_Shdr *hdr;
9244 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9247 if (dynsymcount <= 0)
9250 if (!bed->plt_sym_val)
9253 relplt_name = bed->relplt_name;
9254 if (relplt_name == NULL)
9255 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9256 relplt = bfd_get_section_by_name (abfd, relplt_name);
9260 hdr = &elf_section_data (relplt)->this_hdr;
9261 if (hdr->sh_link != elf_dynsymtab (abfd)
9262 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9265 plt = bfd_get_section_by_name (abfd, ".plt");
9269 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9270 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9273 count = relplt->size / hdr->sh_entsize;
9274 size = count * sizeof (asymbol);
9275 p = relplt->relocation;
9276 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9278 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9282 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
9284 size += sizeof ("+0x") - 1 + 8;
9289 s = *ret = (asymbol *) bfd_malloc (size);
9293 names = (char *) (s + count);
9294 p = relplt->relocation;
9296 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9301 addr = bed->plt_sym_val (i, plt, p);
9302 if (addr == (bfd_vma) -1)
9305 *s = **p->sym_ptr_ptr;
9306 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9307 we are defining a symbol, ensure one of them is set. */
9308 if ((s->flags & BSF_LOCAL) == 0)
9309 s->flags |= BSF_GLOBAL;
9310 s->flags |= BSF_SYNTHETIC;
9312 s->value = addr - plt->vma;
9315 len = strlen ((*p->sym_ptr_ptr)->name);
9316 memcpy (names, (*p->sym_ptr_ptr)->name, len);
9322 memcpy (names, "+0x", sizeof ("+0x") - 1);
9323 names += sizeof ("+0x") - 1;
9324 bfd_sprintf_vma (abfd, buf, p->addend);
9325 for (a = buf; *a == '0'; ++a)
9328 memcpy (names, a, len);
9331 memcpy (names, "@plt", sizeof ("@plt"));
9332 names += sizeof ("@plt");
9339 /* It is only used by x86-64 so far. */
9340 asection _bfd_elf_large_com_section
9341 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9342 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9345 _bfd_elf_set_osabi (bfd * abfd,
9346 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9348 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
9350 i_ehdrp = elf_elfheader (abfd);
9352 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9354 /* To make things simpler for the loader on Linux systems we set the
9355 osabi field to ELFOSABI_LINUX if the binary contains symbols of
9356 the STT_GNU_IFUNC type. */
9357 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
9358 && elf_tdata (abfd)->has_ifunc_symbols)
9359 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
9363 /* Return TRUE for ELF symbol types that represent functions.
9364 This is the default version of this function, which is sufficient for
9365 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9368 _bfd_elf_is_function_type (unsigned int type)
9370 return (type == STT_FUNC
9371 || type == STT_GNU_IFUNC);