1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2014 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-psinfo.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
53 static bfd_boolean prep_headers (bfd *);
54 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
55 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
56 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg)
199 const unsigned char *name = (const unsigned char *) namearg;
204 while ((ch = *name++) != '\0')
207 if ((g = (h & 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h & 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg)
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd *abfd,
238 enum elf_target_id object_id)
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
245 elf_object_id (abfd) = object_id;
246 if (abfd->direction != read_direction)
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
259 bfd_elf_make_object (bfd *abfd)
261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
267 bfd_elf_mkcorefile (bfd *abfd)
269 /* I think this can be done just like an object file. */
270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
277 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
279 Elf_Internal_Shdr **i_shdrp;
280 bfd_byte *shstrtab = NULL;
282 bfd_size_type shstrtabsize;
284 i_shdrp = elf_elfsections (abfd);
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
290 shstrtab = i_shdrp[shindex]->contents;
291 if (shstrtab == NULL)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize + 1 <= 1
300 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL
301 || bfd_seek (abfd, offset, SEEK_SET) != 0)
303 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
305 if (bfd_get_error () != bfd_error_system_call)
306 bfd_set_error (bfd_error_file_truncated);
308 /* Once we've failed to read it, make sure we don't keep
309 trying. Otherwise, we'll keep allocating space for
310 the string table over and over. */
311 i_shdrp[shindex]->sh_size = 0;
314 shstrtab[shstrtabsize] = '\0';
315 i_shdrp[shindex]->contents = shstrtab;
317 return (char *) shstrtab;
321 bfd_elf_string_from_elf_section (bfd *abfd,
322 unsigned int shindex,
323 unsigned int strindex)
325 Elf_Internal_Shdr *hdr;
330 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
333 hdr = elf_elfsections (abfd)[shindex];
335 if (hdr->contents == NULL
336 && bfd_elf_get_str_section (abfd, shindex) == NULL)
339 if (strindex >= hdr->sh_size)
341 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
342 (*_bfd_error_handler)
343 (_("%B: invalid string offset %u >= %lu for section `%s'"),
344 abfd, strindex, (unsigned long) hdr->sh_size,
345 (shindex == shstrndx && strindex == hdr->sh_name
347 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
351 return ((char *) hdr->contents) + strindex;
354 /* Read and convert symbols to internal format.
355 SYMCOUNT specifies the number of symbols to read, starting from
356 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
357 are non-NULL, they are used to store the internal symbols, external
358 symbols, and symbol section index extensions, respectively.
359 Returns a pointer to the internal symbol buffer (malloced if necessary)
360 or NULL if there were no symbols or some kind of problem. */
363 bfd_elf_get_elf_syms (bfd *ibfd,
364 Elf_Internal_Shdr *symtab_hdr,
367 Elf_Internal_Sym *intsym_buf,
369 Elf_External_Sym_Shndx *extshndx_buf)
371 Elf_Internal_Shdr *shndx_hdr;
373 const bfd_byte *esym;
374 Elf_External_Sym_Shndx *alloc_extshndx;
375 Elf_External_Sym_Shndx *shndx;
376 Elf_Internal_Sym *alloc_intsym;
377 Elf_Internal_Sym *isym;
378 Elf_Internal_Sym *isymend;
379 const struct elf_backend_data *bed;
384 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
390 /* Normal syms might have section extension entries. */
392 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
393 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
395 /* Read the symbols. */
397 alloc_extshndx = NULL;
399 bed = get_elf_backend_data (ibfd);
400 extsym_size = bed->s->sizeof_sym;
401 amt = symcount * extsym_size;
402 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
403 if (extsym_buf == NULL)
405 alloc_ext = bfd_malloc2 (symcount, extsym_size);
406 extsym_buf = alloc_ext;
408 if (extsym_buf == NULL
409 || bfd_seek (ibfd, pos, SEEK_SET) != 0
410 || bfd_bread (extsym_buf, amt, ibfd) != amt)
416 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
420 amt = symcount * sizeof (Elf_External_Sym_Shndx);
421 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
422 if (extshndx_buf == NULL)
424 alloc_extshndx = (Elf_External_Sym_Shndx *)
425 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
426 extshndx_buf = alloc_extshndx;
428 if (extshndx_buf == NULL
429 || bfd_seek (ibfd, pos, SEEK_SET) != 0
430 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
437 if (intsym_buf == NULL)
439 alloc_intsym = (Elf_Internal_Sym *)
440 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
441 intsym_buf = alloc_intsym;
442 if (intsym_buf == NULL)
446 /* Convert the symbols to internal form. */
447 isymend = intsym_buf + symcount;
448 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
449 shndx = extshndx_buf;
451 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
452 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
454 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
455 (*_bfd_error_handler) (_("%B symbol number %lu references "
456 "nonexistent SHT_SYMTAB_SHNDX section"),
457 ibfd, (unsigned long) symoffset);
458 if (alloc_intsym != NULL)
465 if (alloc_ext != NULL)
467 if (alloc_extshndx != NULL)
468 free (alloc_extshndx);
473 /* Look up a symbol name. */
475 bfd_elf_sym_name (bfd *abfd,
476 Elf_Internal_Shdr *symtab_hdr,
477 Elf_Internal_Sym *isym,
481 unsigned int iname = isym->st_name;
482 unsigned int shindex = symtab_hdr->sh_link;
484 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
485 /* Check for a bogus st_shndx to avoid crashing. */
486 && isym->st_shndx < elf_numsections (abfd))
488 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
489 shindex = elf_elfheader (abfd)->e_shstrndx;
492 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
495 else if (sym_sec && *name == '\0')
496 name = bfd_section_name (abfd, sym_sec);
501 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
502 sections. The first element is the flags, the rest are section
505 typedef union elf_internal_group {
506 Elf_Internal_Shdr *shdr;
508 } Elf_Internal_Group;
510 /* Return the name of the group signature symbol. Why isn't the
511 signature just a string? */
514 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
516 Elf_Internal_Shdr *hdr;
517 unsigned char esym[sizeof (Elf64_External_Sym)];
518 Elf_External_Sym_Shndx eshndx;
519 Elf_Internal_Sym isym;
521 /* First we need to ensure the symbol table is available. Make sure
522 that it is a symbol table section. */
523 if (ghdr->sh_link >= elf_numsections (abfd))
525 hdr = elf_elfsections (abfd) [ghdr->sh_link];
526 if (hdr->sh_type != SHT_SYMTAB
527 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
530 /* Go read the symbol. */
531 hdr = &elf_tdata (abfd)->symtab_hdr;
532 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
533 &isym, esym, &eshndx) == NULL)
536 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
539 /* Set next_in_group list pointer, and group name for NEWSECT. */
542 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
544 unsigned int num_group = elf_tdata (abfd)->num_group;
546 /* If num_group is zero, read in all SHT_GROUP sections. The count
547 is set to -1 if there are no SHT_GROUP sections. */
550 unsigned int i, shnum;
552 /* First count the number of groups. If we have a SHT_GROUP
553 section with just a flag word (ie. sh_size is 4), ignore it. */
554 shnum = elf_numsections (abfd);
557 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
558 ( (shdr)->sh_type == SHT_GROUP \
559 && (shdr)->sh_size >= minsize \
560 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
561 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
563 for (i = 0; i < shnum; i++)
565 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
567 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
573 num_group = (unsigned) -1;
574 elf_tdata (abfd)->num_group = num_group;
578 /* We keep a list of elf section headers for group sections,
579 so we can find them quickly. */
582 elf_tdata (abfd)->num_group = num_group;
583 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
584 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
585 if (elf_tdata (abfd)->group_sect_ptr == NULL)
589 for (i = 0; i < shnum; i++)
591 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
593 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
596 Elf_Internal_Group *dest;
598 /* Add to list of sections. */
599 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
602 /* Read the raw contents. */
603 BFD_ASSERT (sizeof (*dest) >= 4);
604 amt = shdr->sh_size * sizeof (*dest) / 4;
605 shdr->contents = (unsigned char *)
606 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
607 /* PR binutils/4110: Handle corrupt group headers. */
608 if (shdr->contents == NULL)
611 (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
612 bfd_set_error (bfd_error_bad_value);
617 memset (shdr->contents, 0, amt);
619 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
620 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
624 (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size);
625 bfd_set_error (bfd_error_bad_value);
627 /* PR 17510: If the group contents are even partially
628 corrupt, do not allow any of the contents to be used. */
629 memset (shdr->contents, 0, amt);
633 /* Translate raw contents, a flag word followed by an
634 array of elf section indices all in target byte order,
635 to the flag word followed by an array of elf section
637 src = shdr->contents + shdr->sh_size;
638 dest = (Elf_Internal_Group *) (shdr->contents + amt);
645 idx = H_GET_32 (abfd, src);
646 if (src == shdr->contents)
649 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
650 shdr->bfd_section->flags
651 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
656 ((*_bfd_error_handler)
657 (_("%B: invalid SHT_GROUP entry"), abfd));
660 dest->shdr = elf_elfsections (abfd)[idx];
665 /* PR 17510: Corrupt binaries might contain invalid groups. */
666 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
668 elf_tdata (abfd)->num_group = num_group;
670 /* If all groups are invalid then fail. */
673 elf_tdata (abfd)->group_sect_ptr = NULL;
674 elf_tdata (abfd)->num_group = num_group = -1;
675 (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd);
676 bfd_set_error (bfd_error_bad_value);
682 if (num_group != (unsigned) -1)
686 for (i = 0; i < num_group; i++)
688 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
689 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
690 unsigned int n_elt = shdr->sh_size / 4;
692 /* Look through this group's sections to see if current
693 section is a member. */
695 if ((++idx)->shdr == hdr)
699 /* We are a member of this group. Go looking through
700 other members to see if any others are linked via
702 idx = (Elf_Internal_Group *) shdr->contents;
703 n_elt = shdr->sh_size / 4;
705 if ((s = (++idx)->shdr->bfd_section) != NULL
706 && elf_next_in_group (s) != NULL)
710 /* Snarf the group name from other member, and
711 insert current section in circular list. */
712 elf_group_name (newsect) = elf_group_name (s);
713 elf_next_in_group (newsect) = elf_next_in_group (s);
714 elf_next_in_group (s) = newsect;
720 gname = group_signature (abfd, shdr);
723 elf_group_name (newsect) = gname;
725 /* Start a circular list with one element. */
726 elf_next_in_group (newsect) = newsect;
729 /* If the group section has been created, point to the
731 if (shdr->bfd_section != NULL)
732 elf_next_in_group (shdr->bfd_section) = newsect;
740 if (elf_group_name (newsect) == NULL)
742 (*_bfd_error_handler) (_("%B: no group info for section %A"),
750 _bfd_elf_setup_sections (bfd *abfd)
753 unsigned int num_group = elf_tdata (abfd)->num_group;
754 bfd_boolean result = TRUE;
757 /* Process SHF_LINK_ORDER. */
758 for (s = abfd->sections; s != NULL; s = s->next)
760 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
761 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
763 unsigned int elfsec = this_hdr->sh_link;
764 /* FIXME: The old Intel compiler and old strip/objcopy may
765 not set the sh_link or sh_info fields. Hence we could
766 get the situation where elfsec is 0. */
769 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
770 if (bed->link_order_error_handler)
771 bed->link_order_error_handler
772 (_("%B: warning: sh_link not set for section `%A'"),
777 asection *linksec = NULL;
779 if (elfsec < elf_numsections (abfd))
781 this_hdr = elf_elfsections (abfd)[elfsec];
782 linksec = this_hdr->bfd_section;
786 Some strip/objcopy may leave an incorrect value in
787 sh_link. We don't want to proceed. */
790 (*_bfd_error_handler)
791 (_("%B: sh_link [%d] in section `%A' is incorrect"),
792 s->owner, s, elfsec);
796 elf_linked_to_section (s) = linksec;
801 /* Process section groups. */
802 if (num_group == (unsigned) -1)
805 for (i = 0; i < num_group; i++)
807 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
808 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
809 unsigned int n_elt = shdr->sh_size / 4;
812 if ((++idx)->shdr->bfd_section)
813 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
814 else if (idx->shdr->sh_type == SHT_RELA
815 || idx->shdr->sh_type == SHT_REL)
816 /* We won't include relocation sections in section groups in
817 output object files. We adjust the group section size here
818 so that relocatable link will work correctly when
819 relocation sections are in section group in input object
821 shdr->bfd_section->size -= 4;
824 /* There are some unknown sections in the group. */
825 (*_bfd_error_handler)
826 (_("%B: unknown [%d] section `%s' in group [%s]"),
828 (unsigned int) idx->shdr->sh_type,
829 bfd_elf_string_from_elf_section (abfd,
830 (elf_elfheader (abfd)
833 shdr->bfd_section->name);
841 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
843 return elf_next_in_group (sec) != NULL;
846 /* Make a BFD section from an ELF section. We store a pointer to the
847 BFD section in the bfd_section field of the header. */
850 _bfd_elf_make_section_from_shdr (bfd *abfd,
851 Elf_Internal_Shdr *hdr,
857 const struct elf_backend_data *bed;
859 if (hdr->bfd_section != NULL)
862 newsect = bfd_make_section_anyway (abfd, name);
866 hdr->bfd_section = newsect;
867 elf_section_data (newsect)->this_hdr = *hdr;
868 elf_section_data (newsect)->this_idx = shindex;
870 /* Always use the real type/flags. */
871 elf_section_type (newsect) = hdr->sh_type;
872 elf_section_flags (newsect) = hdr->sh_flags;
874 newsect->filepos = hdr->sh_offset;
876 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
877 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
878 || ! bfd_set_section_alignment (abfd, newsect,
879 bfd_log2 (hdr->sh_addralign)))
882 flags = SEC_NO_FLAGS;
883 if (hdr->sh_type != SHT_NOBITS)
884 flags |= SEC_HAS_CONTENTS;
885 if (hdr->sh_type == SHT_GROUP)
886 flags |= SEC_GROUP | SEC_EXCLUDE;
887 if ((hdr->sh_flags & SHF_ALLOC) != 0)
890 if (hdr->sh_type != SHT_NOBITS)
893 if ((hdr->sh_flags & SHF_WRITE) == 0)
894 flags |= SEC_READONLY;
895 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
897 else if ((flags & SEC_LOAD) != 0)
899 if ((hdr->sh_flags & SHF_MERGE) != 0)
902 newsect->entsize = hdr->sh_entsize;
903 if ((hdr->sh_flags & SHF_STRINGS) != 0)
904 flags |= SEC_STRINGS;
906 if (hdr->sh_flags & SHF_GROUP)
907 if (!setup_group (abfd, hdr, newsect))
909 if ((hdr->sh_flags & SHF_TLS) != 0)
910 flags |= SEC_THREAD_LOCAL;
911 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
912 flags |= SEC_EXCLUDE;
914 if ((flags & SEC_ALLOC) == 0)
916 /* The debugging sections appear to be recognized only by name,
917 not any sort of flag. Their SEC_ALLOC bits are cleared. */
924 else if (name[1] == 'g' && name[2] == 'n')
925 p = ".gnu.linkonce.wi.", n = 17;
926 else if (name[1] == 'g' && name[2] == 'd')
927 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
928 else if (name[1] == 'l')
930 else if (name[1] == 's')
932 else if (name[1] == 'z')
933 p = ".zdebug", n = 7;
936 if (p != NULL && strncmp (name, p, n) == 0)
937 flags |= SEC_DEBUGGING;
941 /* As a GNU extension, if the name begins with .gnu.linkonce, we
942 only link a single copy of the section. This is used to support
943 g++. g++ will emit each template expansion in its own section.
944 The symbols will be defined as weak, so that multiple definitions
945 are permitted. The GNU linker extension is to actually discard
946 all but one of the sections. */
947 if (CONST_STRNEQ (name, ".gnu.linkonce")
948 && elf_next_in_group (newsect) == NULL)
949 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
951 bed = get_elf_backend_data (abfd);
952 if (bed->elf_backend_section_flags)
953 if (! bed->elf_backend_section_flags (&flags, hdr))
956 if (! bfd_set_section_flags (abfd, newsect, flags))
959 /* We do not parse the PT_NOTE segments as we are interested even in the
960 separate debug info files which may have the segments offsets corrupted.
961 PT_NOTEs from the core files are currently not parsed using BFD. */
962 if (hdr->sh_type == SHT_NOTE)
966 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
969 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
973 if ((flags & SEC_ALLOC) != 0)
975 Elf_Internal_Phdr *phdr;
976 unsigned int i, nload;
978 /* Some ELF linkers produce binaries with all the program header
979 p_paddr fields zero. If we have such a binary with more than
980 one PT_LOAD header, then leave the section lma equal to vma
981 so that we don't create sections with overlapping lma. */
982 phdr = elf_tdata (abfd)->phdr;
983 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
984 if (phdr->p_paddr != 0)
986 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
988 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
991 phdr = elf_tdata (abfd)->phdr;
992 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
994 if (((phdr->p_type == PT_LOAD
995 && (hdr->sh_flags & SHF_TLS) == 0)
996 || phdr->p_type == PT_TLS)
997 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
999 if ((flags & SEC_LOAD) == 0)
1000 newsect->lma = (phdr->p_paddr
1001 + hdr->sh_addr - phdr->p_vaddr);
1003 /* We used to use the same adjustment for SEC_LOAD
1004 sections, but that doesn't work if the segment
1005 is packed with code from multiple VMAs.
1006 Instead we calculate the section LMA based on
1007 the segment LMA. It is assumed that the
1008 segment will contain sections with contiguous
1009 LMAs, even if the VMAs are not. */
1010 newsect->lma = (phdr->p_paddr
1011 + hdr->sh_offset - phdr->p_offset);
1013 /* With contiguous segments, we can't tell from file
1014 offsets whether a section with zero size should
1015 be placed at the end of one segment or the
1016 beginning of the next. Decide based on vaddr. */
1017 if (hdr->sh_addr >= phdr->p_vaddr
1018 && (hdr->sh_addr + hdr->sh_size
1019 <= phdr->p_vaddr + phdr->p_memsz))
1025 /* Compress/decompress DWARF debug sections with names: .debug_* and
1026 .zdebug_*, after the section flags is set. */
1027 if ((flags & SEC_DEBUGGING)
1028 && ((name[1] == 'd' && name[6] == '_')
1029 || (name[1] == 'z' && name[7] == '_')))
1031 enum { nothing, compress, decompress } action = nothing;
1034 if (bfd_is_section_compressed (abfd, newsect))
1036 /* Compressed section. Check if we should decompress. */
1037 if ((abfd->flags & BFD_DECOMPRESS))
1038 action = decompress;
1042 /* Normal section. Check if we should compress. */
1043 if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
1053 if (!bfd_init_section_compress_status (abfd, newsect))
1055 (*_bfd_error_handler)
1056 (_("%B: unable to initialize compress status for section %s"),
1062 unsigned int len = strlen (name);
1064 new_name = bfd_alloc (abfd, len + 2);
1065 if (new_name == NULL)
1069 memcpy (new_name + 2, name + 1, len);
1073 if (!bfd_init_section_decompress_status (abfd, newsect))
1075 (*_bfd_error_handler)
1076 (_("%B: unable to initialize decompress status for section %s"),
1082 unsigned int len = strlen (name);
1084 new_name = bfd_alloc (abfd, len);
1085 if (new_name == NULL)
1088 memcpy (new_name + 1, name + 2, len - 1);
1092 if (new_name != NULL)
1093 bfd_rename_section (abfd, newsect, new_name);
1099 const char *const bfd_elf_section_type_names[] = {
1100 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1101 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1102 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1105 /* ELF relocs are against symbols. If we are producing relocatable
1106 output, and the reloc is against an external symbol, and nothing
1107 has given us any additional addend, the resulting reloc will also
1108 be against the same symbol. In such a case, we don't want to
1109 change anything about the way the reloc is handled, since it will
1110 all be done at final link time. Rather than put special case code
1111 into bfd_perform_relocation, all the reloc types use this howto
1112 function. It just short circuits the reloc if producing
1113 relocatable output against an external symbol. */
1115 bfd_reloc_status_type
1116 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1117 arelent *reloc_entry,
1119 void *data ATTRIBUTE_UNUSED,
1120 asection *input_section,
1122 char **error_message ATTRIBUTE_UNUSED)
1124 if (output_bfd != NULL
1125 && (symbol->flags & BSF_SECTION_SYM) == 0
1126 && (! reloc_entry->howto->partial_inplace
1127 || reloc_entry->addend == 0))
1129 reloc_entry->address += input_section->output_offset;
1130 return bfd_reloc_ok;
1133 return bfd_reloc_continue;
1136 /* Copy the program header and other data from one object module to
1140 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1142 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1143 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1146 if (!elf_flags_init (obfd))
1148 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1149 elf_flags_init (obfd) = TRUE;
1152 elf_gp (obfd) = elf_gp (ibfd);
1154 /* Also copy the EI_OSABI field. */
1155 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1156 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1158 /* Copy object attributes. */
1159 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1164 get_segment_type (unsigned int p_type)
1169 case PT_NULL: pt = "NULL"; break;
1170 case PT_LOAD: pt = "LOAD"; break;
1171 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1172 case PT_INTERP: pt = "INTERP"; break;
1173 case PT_NOTE: pt = "NOTE"; break;
1174 case PT_SHLIB: pt = "SHLIB"; break;
1175 case PT_PHDR: pt = "PHDR"; break;
1176 case PT_TLS: pt = "TLS"; break;
1177 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1178 case PT_GNU_STACK: pt = "STACK"; break;
1179 case PT_GNU_RELRO: pt = "RELRO"; break;
1180 default: pt = NULL; break;
1185 /* Print out the program headers. */
1188 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1190 FILE *f = (FILE *) farg;
1191 Elf_Internal_Phdr *p;
1193 bfd_byte *dynbuf = NULL;
1195 p = elf_tdata (abfd)->phdr;
1200 fprintf (f, _("\nProgram Header:\n"));
1201 c = elf_elfheader (abfd)->e_phnum;
1202 for (i = 0; i < c; i++, p++)
1204 const char *pt = get_segment_type (p->p_type);
1209 sprintf (buf, "0x%lx", p->p_type);
1212 fprintf (f, "%8s off 0x", pt);
1213 bfd_fprintf_vma (abfd, f, p->p_offset);
1214 fprintf (f, " vaddr 0x");
1215 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1216 fprintf (f, " paddr 0x");
1217 bfd_fprintf_vma (abfd, f, p->p_paddr);
1218 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1219 fprintf (f, " filesz 0x");
1220 bfd_fprintf_vma (abfd, f, p->p_filesz);
1221 fprintf (f, " memsz 0x");
1222 bfd_fprintf_vma (abfd, f, p->p_memsz);
1223 fprintf (f, " flags %c%c%c",
1224 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1225 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1226 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1227 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1228 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1233 s = bfd_get_section_by_name (abfd, ".dynamic");
1236 unsigned int elfsec;
1237 unsigned long shlink;
1238 bfd_byte *extdyn, *extdynend;
1240 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1242 fprintf (f, _("\nDynamic Section:\n"));
1244 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1247 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1248 if (elfsec == SHN_BAD)
1250 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1252 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1253 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1256 extdynend = extdyn + s->size;
1257 for (; extdyn < extdynend; extdyn += extdynsize)
1259 Elf_Internal_Dyn dyn;
1260 const char *name = "";
1262 bfd_boolean stringp;
1263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1265 (*swap_dyn_in) (abfd, extdyn, &dyn);
1267 if (dyn.d_tag == DT_NULL)
1274 if (bed->elf_backend_get_target_dtag)
1275 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1277 if (!strcmp (name, ""))
1279 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1284 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1285 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1286 case DT_PLTGOT: name = "PLTGOT"; break;
1287 case DT_HASH: name = "HASH"; break;
1288 case DT_STRTAB: name = "STRTAB"; break;
1289 case DT_SYMTAB: name = "SYMTAB"; break;
1290 case DT_RELA: name = "RELA"; break;
1291 case DT_RELASZ: name = "RELASZ"; break;
1292 case DT_RELAENT: name = "RELAENT"; break;
1293 case DT_STRSZ: name = "STRSZ"; break;
1294 case DT_SYMENT: name = "SYMENT"; break;
1295 case DT_INIT: name = "INIT"; break;
1296 case DT_FINI: name = "FINI"; break;
1297 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1298 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1299 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1300 case DT_REL: name = "REL"; break;
1301 case DT_RELSZ: name = "RELSZ"; break;
1302 case DT_RELENT: name = "RELENT"; break;
1303 case DT_PLTREL: name = "PLTREL"; break;
1304 case DT_DEBUG: name = "DEBUG"; break;
1305 case DT_TEXTREL: name = "TEXTREL"; break;
1306 case DT_JMPREL: name = "JMPREL"; break;
1307 case DT_BIND_NOW: name = "BIND_NOW"; break;
1308 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1309 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1310 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1311 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1312 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1313 case DT_FLAGS: name = "FLAGS"; break;
1314 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1315 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1316 case DT_CHECKSUM: name = "CHECKSUM"; break;
1317 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1318 case DT_MOVEENT: name = "MOVEENT"; break;
1319 case DT_MOVESZ: name = "MOVESZ"; break;
1320 case DT_FEATURE: name = "FEATURE"; break;
1321 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1322 case DT_SYMINSZ: name = "SYMINSZ"; break;
1323 case DT_SYMINENT: name = "SYMINENT"; break;
1324 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1325 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1326 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1327 case DT_PLTPAD: name = "PLTPAD"; break;
1328 case DT_MOVETAB: name = "MOVETAB"; break;
1329 case DT_SYMINFO: name = "SYMINFO"; break;
1330 case DT_RELACOUNT: name = "RELACOUNT"; break;
1331 case DT_RELCOUNT: name = "RELCOUNT"; break;
1332 case DT_FLAGS_1: name = "FLAGS_1"; break;
1333 case DT_VERSYM: name = "VERSYM"; break;
1334 case DT_VERDEF: name = "VERDEF"; break;
1335 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1336 case DT_VERNEED: name = "VERNEED"; break;
1337 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1338 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1339 case DT_USED: name = "USED"; break;
1340 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1341 case DT_GNU_HASH: name = "GNU_HASH"; break;
1344 fprintf (f, " %-20s ", name);
1348 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1353 unsigned int tagv = dyn.d_un.d_val;
1355 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1358 fprintf (f, "%s", string);
1367 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1368 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1370 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1374 if (elf_dynverdef (abfd) != 0)
1376 Elf_Internal_Verdef *t;
1378 fprintf (f, _("\nVersion definitions:\n"));
1379 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1381 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1382 t->vd_flags, t->vd_hash,
1383 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1384 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1386 Elf_Internal_Verdaux *a;
1389 for (a = t->vd_auxptr->vda_nextptr;
1393 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1399 if (elf_dynverref (abfd) != 0)
1401 Elf_Internal_Verneed *t;
1403 fprintf (f, _("\nVersion References:\n"));
1404 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1406 Elf_Internal_Vernaux *a;
1408 fprintf (f, _(" required from %s:\n"),
1409 t->vn_filename ? t->vn_filename : "<corrupt>");
1410 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1411 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1412 a->vna_flags, a->vna_other,
1413 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1425 /* Get version string. */
1428 bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1429 bfd_boolean *hidden)
1431 const char *version_string = NULL;
1432 if (elf_dynversym (abfd) != 0
1433 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1435 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1437 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1438 vernum &= VERSYM_VERSION;
1441 version_string = "";
1442 else if (vernum == 1)
1443 version_string = "Base";
1444 else if (vernum <= elf_tdata (abfd)->cverdefs)
1446 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1449 Elf_Internal_Verneed *t;
1451 version_string = "";
1452 for (t = elf_tdata (abfd)->verref;
1456 Elf_Internal_Vernaux *a;
1458 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1460 if (a->vna_other == vernum)
1462 version_string = a->vna_nodename;
1469 return version_string;
1472 /* Display ELF-specific fields of a symbol. */
1475 bfd_elf_print_symbol (bfd *abfd,
1478 bfd_print_symbol_type how)
1480 FILE *file = (FILE *) filep;
1483 case bfd_print_symbol_name:
1484 fprintf (file, "%s", symbol->name);
1486 case bfd_print_symbol_more:
1487 fprintf (file, "elf ");
1488 bfd_fprintf_vma (abfd, file, symbol->value);
1489 fprintf (file, " %lx", (unsigned long) symbol->flags);
1491 case bfd_print_symbol_all:
1493 const char *section_name;
1494 const char *name = NULL;
1495 const struct elf_backend_data *bed;
1496 unsigned char st_other;
1498 const char *version_string;
1501 section_name = symbol->section ? symbol->section->name : "(*none*)";
1503 bed = get_elf_backend_data (abfd);
1504 if (bed->elf_backend_print_symbol_all)
1505 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1509 name = symbol->name;
1510 bfd_print_symbol_vandf (abfd, file, symbol);
1513 fprintf (file, " %s\t", section_name);
1514 /* Print the "other" value for a symbol. For common symbols,
1515 we've already printed the size; now print the alignment.
1516 For other symbols, we have no specified alignment, and
1517 we've printed the address; now print the size. */
1518 if (symbol->section && bfd_is_com_section (symbol->section))
1519 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1521 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1522 bfd_fprintf_vma (abfd, file, val);
1524 /* If we have version information, print it. */
1525 version_string = bfd_elf_get_symbol_version_string (abfd,
1531 fprintf (file, " %-11s", version_string);
1536 fprintf (file, " (%s)", version_string);
1537 for (i = 10 - strlen (version_string); i > 0; --i)
1542 /* If the st_other field is not zero, print it. */
1543 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1548 case STV_INTERNAL: fprintf (file, " .internal"); break;
1549 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1550 case STV_PROTECTED: fprintf (file, " .protected"); break;
1552 /* Some other non-defined flags are also present, so print
1554 fprintf (file, " 0x%02x", (unsigned int) st_other);
1557 fprintf (file, " %s", name);
1563 /* Allocate an ELF string table--force the first byte to be zero. */
1565 struct bfd_strtab_hash *
1566 _bfd_elf_stringtab_init (void)
1568 struct bfd_strtab_hash *ret;
1570 ret = _bfd_stringtab_init ();
1575 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1576 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1577 if (loc == (bfd_size_type) -1)
1579 _bfd_stringtab_free (ret);
1586 /* ELF .o/exec file reading */
1588 /* Create a new bfd section from an ELF section header. */
1591 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1593 Elf_Internal_Shdr *hdr;
1594 Elf_Internal_Ehdr *ehdr;
1595 const struct elf_backend_data *bed;
1597 bfd_boolean ret = TRUE;
1598 static bfd_boolean * sections_being_created = NULL;
1599 static bfd * sections_being_created_abfd = NULL;
1600 static unsigned int nesting = 0;
1602 if (shindex >= elf_numsections (abfd))
1607 /* PR17512: A corrupt ELF binary might contain a recursive group of
1608 sections, each the string indicies pointing to the next in the
1609 loop. Detect this here, by refusing to load a section that we are
1610 already in the process of loading. We only trigger this test if
1611 we have nested at least three sections deep as normal ELF binaries
1612 can expect to recurse at least once.
1614 FIXME: It would be better if this array was attached to the bfd,
1615 rather than being held in a static pointer. */
1617 if (sections_being_created_abfd != abfd)
1618 sections_being_created = NULL;
1619 if (sections_being_created == NULL)
1621 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1622 sections_being_created = (bfd_boolean *)
1623 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
1624 sections_being_created_abfd = abfd;
1626 if (sections_being_created [shindex])
1628 (*_bfd_error_handler)
1629 (_("%B: warning: loop in section dependencies detected"), abfd);
1632 sections_being_created [shindex] = TRUE;
1635 hdr = elf_elfsections (abfd)[shindex];
1636 ehdr = elf_elfheader (abfd);
1637 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1642 bed = get_elf_backend_data (abfd);
1643 switch (hdr->sh_type)
1646 /* Inactive section. Throw it away. */
1649 case SHT_PROGBITS: /* Normal section with contents. */
1650 case SHT_NOBITS: /* .bss section. */
1651 case SHT_HASH: /* .hash section. */
1652 case SHT_NOTE: /* .note section. */
1653 case SHT_INIT_ARRAY: /* .init_array section. */
1654 case SHT_FINI_ARRAY: /* .fini_array section. */
1655 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1656 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1657 case SHT_GNU_HASH: /* .gnu.hash section. */
1658 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1661 case SHT_DYNAMIC: /* Dynamic linking information. */
1662 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1665 if (hdr->sh_link > elf_numsections (abfd))
1667 /* PR 10478: Accept Solaris binaries with a sh_link
1668 field set to SHN_BEFORE or SHN_AFTER. */
1669 switch (bfd_get_arch (abfd))
1672 case bfd_arch_sparc:
1673 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1674 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1676 /* Otherwise fall through. */
1681 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1683 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1685 Elf_Internal_Shdr *dynsymhdr;
1687 /* The shared libraries distributed with hpux11 have a bogus
1688 sh_link field for the ".dynamic" section. Find the
1689 string table for the ".dynsym" section instead. */
1690 if (elf_dynsymtab (abfd) != 0)
1692 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1693 hdr->sh_link = dynsymhdr->sh_link;
1697 unsigned int i, num_sec;
1699 num_sec = elf_numsections (abfd);
1700 for (i = 1; i < num_sec; i++)
1702 dynsymhdr = elf_elfsections (abfd)[i];
1703 if (dynsymhdr->sh_type == SHT_DYNSYM)
1705 hdr->sh_link = dynsymhdr->sh_link;
1713 case SHT_SYMTAB: /* A symbol table. */
1714 if (elf_onesymtab (abfd) == shindex)
1717 if (hdr->sh_entsize != bed->s->sizeof_sym)
1720 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1722 if (hdr->sh_size != 0)
1724 /* Some assemblers erroneously set sh_info to one with a
1725 zero sh_size. ld sees this as a global symbol count
1726 of (unsigned) -1. Fix it here. */
1731 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1732 elf_onesymtab (abfd) = shindex;
1733 elf_tdata (abfd)->symtab_hdr = *hdr;
1734 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1735 abfd->flags |= HAS_SYMS;
1737 /* Sometimes a shared object will map in the symbol table. If
1738 SHF_ALLOC is set, and this is a shared object, then we also
1739 treat this section as a BFD section. We can not base the
1740 decision purely on SHF_ALLOC, because that flag is sometimes
1741 set in a relocatable object file, which would confuse the
1743 if ((hdr->sh_flags & SHF_ALLOC) != 0
1744 && (abfd->flags & DYNAMIC) != 0
1745 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1749 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1750 can't read symbols without that section loaded as well. It
1751 is most likely specified by the next section header. */
1752 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1754 unsigned int i, num_sec;
1756 num_sec = elf_numsections (abfd);
1757 for (i = shindex + 1; i < num_sec; i++)
1759 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1760 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1761 && hdr2->sh_link == shindex)
1765 for (i = 1; i < shindex; i++)
1767 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1768 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1769 && hdr2->sh_link == shindex)
1773 ret = bfd_section_from_shdr (abfd, i);
1777 case SHT_DYNSYM: /* A dynamic symbol table. */
1778 if (elf_dynsymtab (abfd) == shindex)
1781 if (hdr->sh_entsize != bed->s->sizeof_sym)
1784 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1786 if (hdr->sh_size != 0)
1789 /* Some linkers erroneously set sh_info to one with a
1790 zero sh_size. ld sees this as a global symbol count
1791 of (unsigned) -1. Fix it here. */
1796 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1797 elf_dynsymtab (abfd) = shindex;
1798 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1799 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1800 abfd->flags |= HAS_SYMS;
1802 /* Besides being a symbol table, we also treat this as a regular
1803 section, so that objcopy can handle it. */
1804 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1807 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
1808 if (elf_symtab_shndx (abfd) == shindex)
1811 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1812 elf_symtab_shndx (abfd) = shindex;
1813 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1814 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1817 case SHT_STRTAB: /* A string table. */
1818 if (hdr->bfd_section != NULL)
1821 if (ehdr->e_shstrndx == shindex)
1823 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1824 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1828 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1831 elf_tdata (abfd)->strtab_hdr = *hdr;
1832 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1836 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1839 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1840 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1841 elf_elfsections (abfd)[shindex] = hdr;
1842 /* We also treat this as a regular section, so that objcopy
1844 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1849 /* If the string table isn't one of the above, then treat it as a
1850 regular section. We need to scan all the headers to be sure,
1851 just in case this strtab section appeared before the above. */
1852 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1854 unsigned int i, num_sec;
1856 num_sec = elf_numsections (abfd);
1857 for (i = 1; i < num_sec; i++)
1859 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1860 if (hdr2->sh_link == shindex)
1862 /* Prevent endless recursion on broken objects. */
1865 if (! bfd_section_from_shdr (abfd, i))
1867 if (elf_onesymtab (abfd) == i)
1869 if (elf_dynsymtab (abfd) == i)
1870 goto dynsymtab_strtab;
1874 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1879 /* *These* do a lot of work -- but build no sections! */
1881 asection *target_sect;
1882 Elf_Internal_Shdr *hdr2, **p_hdr;
1883 unsigned int num_sec = elf_numsections (abfd);
1884 struct bfd_elf_section_data *esdt;
1888 != (bfd_size_type) (hdr->sh_type == SHT_REL
1889 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1892 /* Check for a bogus link to avoid crashing. */
1893 if (hdr->sh_link >= num_sec)
1895 ((*_bfd_error_handler)
1896 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1897 abfd, hdr->sh_link, name, shindex));
1898 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1903 /* For some incomprehensible reason Oracle distributes
1904 libraries for Solaris in which some of the objects have
1905 bogus sh_link fields. It would be nice if we could just
1906 reject them, but, unfortunately, some people need to use
1907 them. We scan through the section headers; if we find only
1908 one suitable symbol table, we clobber the sh_link to point
1909 to it. I hope this doesn't break anything.
1911 Don't do it on executable nor shared library. */
1912 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1913 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1914 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1920 for (scan = 1; scan < num_sec; scan++)
1922 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1923 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1934 hdr->sh_link = found;
1937 /* Get the symbol table. */
1938 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1939 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1940 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1943 /* If this reloc section does not use the main symbol table we
1944 don't treat it as a reloc section. BFD can't adequately
1945 represent such a section, so at least for now, we don't
1946 try. We just present it as a normal section. We also
1947 can't use it as a reloc section if it points to the null
1948 section, an invalid section, another reloc section, or its
1949 sh_link points to the null section. */
1950 if (hdr->sh_link != elf_onesymtab (abfd)
1951 || hdr->sh_link == SHN_UNDEF
1952 || hdr->sh_info == SHN_UNDEF
1953 || hdr->sh_info >= num_sec
1954 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1955 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1957 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1962 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1965 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1966 if (target_sect == NULL)
1969 esdt = elf_section_data (target_sect);
1970 if (hdr->sh_type == SHT_RELA)
1971 p_hdr = &esdt->rela.hdr;
1973 p_hdr = &esdt->rel.hdr;
1975 BFD_ASSERT (*p_hdr == NULL);
1976 amt = sizeof (*hdr2);
1977 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1982 elf_elfsections (abfd)[shindex] = hdr2;
1983 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1984 target_sect->flags |= SEC_RELOC;
1985 target_sect->relocation = NULL;
1986 target_sect->rel_filepos = hdr->sh_offset;
1987 /* In the section to which the relocations apply, mark whether
1988 its relocations are of the REL or RELA variety. */
1989 if (hdr->sh_size != 0)
1991 if (hdr->sh_type == SHT_RELA)
1992 target_sect->use_rela_p = 1;
1994 abfd->flags |= HAS_RELOC;
1998 case SHT_GNU_verdef:
1999 elf_dynverdef (abfd) = shindex;
2000 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2001 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2004 case SHT_GNU_versym:
2005 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2008 elf_dynversym (abfd) = shindex;
2009 elf_tdata (abfd)->dynversym_hdr = *hdr;
2010 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2013 case SHT_GNU_verneed:
2014 elf_dynverref (abfd) = shindex;
2015 elf_tdata (abfd)->dynverref_hdr = *hdr;
2016 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2023 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2026 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2029 if (hdr->contents != NULL)
2031 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2032 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
2035 if (idx->flags & GRP_COMDAT)
2036 hdr->bfd_section->flags
2037 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2039 /* We try to keep the same section order as it comes in. */
2041 while (--n_elt != 0)
2045 if (idx->shdr != NULL
2046 && (s = idx->shdr->bfd_section) != NULL
2047 && elf_next_in_group (s) != NULL)
2049 elf_next_in_group (hdr->bfd_section) = s;
2057 /* Possibly an attributes section. */
2058 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2059 || hdr->sh_type == bed->obj_attrs_section_type)
2061 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2063 _bfd_elf_parse_attributes (abfd, hdr);
2067 /* Check for any processor-specific section types. */
2068 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2071 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2073 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2074 /* FIXME: How to properly handle allocated section reserved
2075 for applications? */
2076 (*_bfd_error_handler)
2077 (_("%B: don't know how to handle allocated, application "
2078 "specific section `%s' [0x%8x]"),
2079 abfd, name, hdr->sh_type);
2082 /* Allow sections reserved for applications. */
2083 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2088 else if (hdr->sh_type >= SHT_LOPROC
2089 && hdr->sh_type <= SHT_HIPROC)
2090 /* FIXME: We should handle this section. */
2091 (*_bfd_error_handler)
2092 (_("%B: don't know how to handle processor specific section "
2094 abfd, name, hdr->sh_type);
2095 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2097 /* Unrecognised OS-specific sections. */
2098 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2099 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2100 required to correctly process the section and the file should
2101 be rejected with an error message. */
2102 (*_bfd_error_handler)
2103 (_("%B: don't know how to handle OS specific section "
2105 abfd, name, hdr->sh_type);
2108 /* Otherwise it should be processed. */
2109 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2114 /* FIXME: We should handle this section. */
2115 (*_bfd_error_handler)
2116 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2117 abfd, name, hdr->sh_type);
2125 if (sections_being_created && sections_being_created_abfd == abfd)
2126 sections_being_created [shindex] = FALSE;
2127 if (-- nesting == 0)
2129 sections_being_created = NULL;
2130 sections_being_created_abfd = abfd;
2135 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2138 bfd_sym_from_r_symndx (struct sym_cache *cache,
2140 unsigned long r_symndx)
2142 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2144 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2146 Elf_Internal_Shdr *symtab_hdr;
2147 unsigned char esym[sizeof (Elf64_External_Sym)];
2148 Elf_External_Sym_Shndx eshndx;
2150 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2151 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2152 &cache->sym[ent], esym, &eshndx) == NULL)
2155 if (cache->abfd != abfd)
2157 memset (cache->indx, -1, sizeof (cache->indx));
2160 cache->indx[ent] = r_symndx;
2163 return &cache->sym[ent];
2166 /* Given an ELF section number, retrieve the corresponding BFD
2170 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2172 if (sec_index >= elf_numsections (abfd))
2174 return elf_elfsections (abfd)[sec_index]->bfd_section;
2177 static const struct bfd_elf_special_section special_sections_b[] =
2179 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2180 { NULL, 0, 0, 0, 0 }
2183 static const struct bfd_elf_special_section special_sections_c[] =
2185 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2186 { NULL, 0, 0, 0, 0 }
2189 static const struct bfd_elf_special_section special_sections_d[] =
2191 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2192 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2193 /* There are more DWARF sections than these, but they needn't be added here
2194 unless you have to cope with broken compilers that don't emit section
2195 attributes or you want to help the user writing assembler. */
2196 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2197 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2198 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2199 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2200 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2201 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2202 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2203 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2204 { NULL, 0, 0, 0, 0 }
2207 static const struct bfd_elf_special_section special_sections_f[] =
2209 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2210 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2211 { NULL, 0, 0, 0, 0 }
2214 static const struct bfd_elf_special_section special_sections_g[] =
2216 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2217 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2218 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2219 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2220 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2221 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2222 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2223 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2224 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2225 { NULL, 0, 0, 0, 0 }
2228 static const struct bfd_elf_special_section special_sections_h[] =
2230 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2231 { NULL, 0, 0, 0, 0 }
2234 static const struct bfd_elf_special_section special_sections_i[] =
2236 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2237 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2238 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2239 { NULL, 0, 0, 0, 0 }
2242 static const struct bfd_elf_special_section special_sections_l[] =
2244 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2245 { NULL, 0, 0, 0, 0 }
2248 static const struct bfd_elf_special_section special_sections_n[] =
2250 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2251 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2252 { NULL, 0, 0, 0, 0 }
2255 static const struct bfd_elf_special_section special_sections_p[] =
2257 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2258 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2259 { NULL, 0, 0, 0, 0 }
2262 static const struct bfd_elf_special_section special_sections_r[] =
2264 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2265 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2266 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2267 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2268 { NULL, 0, 0, 0, 0 }
2271 static const struct bfd_elf_special_section special_sections_s[] =
2273 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2274 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2275 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2276 /* See struct bfd_elf_special_section declaration for the semantics of
2277 this special case where .prefix_length != strlen (.prefix). */
2278 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2279 { NULL, 0, 0, 0, 0 }
2282 static const struct bfd_elf_special_section special_sections_t[] =
2284 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2285 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2286 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2287 { NULL, 0, 0, 0, 0 }
2290 static const struct bfd_elf_special_section special_sections_z[] =
2292 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2293 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2294 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2295 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2296 { NULL, 0, 0, 0, 0 }
2299 static const struct bfd_elf_special_section * const special_sections[] =
2301 special_sections_b, /* 'b' */
2302 special_sections_c, /* 'c' */
2303 special_sections_d, /* 'd' */
2305 special_sections_f, /* 'f' */
2306 special_sections_g, /* 'g' */
2307 special_sections_h, /* 'h' */
2308 special_sections_i, /* 'i' */
2311 special_sections_l, /* 'l' */
2313 special_sections_n, /* 'n' */
2315 special_sections_p, /* 'p' */
2317 special_sections_r, /* 'r' */
2318 special_sections_s, /* 's' */
2319 special_sections_t, /* 't' */
2325 special_sections_z /* 'z' */
2328 const struct bfd_elf_special_section *
2329 _bfd_elf_get_special_section (const char *name,
2330 const struct bfd_elf_special_section *spec,
2336 len = strlen (name);
2338 for (i = 0; spec[i].prefix != NULL; i++)
2341 int prefix_len = spec[i].prefix_length;
2343 if (len < prefix_len)
2345 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2348 suffix_len = spec[i].suffix_length;
2349 if (suffix_len <= 0)
2351 if (name[prefix_len] != 0)
2353 if (suffix_len == 0)
2355 if (name[prefix_len] != '.'
2356 && (suffix_len == -2
2357 || (rela && spec[i].type == SHT_REL)))
2363 if (len < prefix_len + suffix_len)
2365 if (memcmp (name + len - suffix_len,
2366 spec[i].prefix + prefix_len,
2376 const struct bfd_elf_special_section *
2377 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2380 const struct bfd_elf_special_section *spec;
2381 const struct elf_backend_data *bed;
2383 /* See if this is one of the special sections. */
2384 if (sec->name == NULL)
2387 bed = get_elf_backend_data (abfd);
2388 spec = bed->special_sections;
2391 spec = _bfd_elf_get_special_section (sec->name,
2392 bed->special_sections,
2398 if (sec->name[0] != '.')
2401 i = sec->name[1] - 'b';
2402 if (i < 0 || i > 'z' - 'b')
2405 spec = special_sections[i];
2410 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2414 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2416 struct bfd_elf_section_data *sdata;
2417 const struct elf_backend_data *bed;
2418 const struct bfd_elf_special_section *ssect;
2420 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2423 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2427 sec->used_by_bfd = sdata;
2430 /* Indicate whether or not this section should use RELA relocations. */
2431 bed = get_elf_backend_data (abfd);
2432 sec->use_rela_p = bed->default_use_rela_p;
2434 /* When we read a file, we don't need to set ELF section type and
2435 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2436 anyway. We will set ELF section type and flags for all linker
2437 created sections. If user specifies BFD section flags, we will
2438 set ELF section type and flags based on BFD section flags in
2439 elf_fake_sections. Special handling for .init_array/.fini_array
2440 output sections since they may contain .ctors/.dtors input
2441 sections. We don't want _bfd_elf_init_private_section_data to
2442 copy ELF section type from .ctors/.dtors input sections. */
2443 if (abfd->direction != read_direction
2444 || (sec->flags & SEC_LINKER_CREATED) != 0)
2446 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2449 || (sec->flags & SEC_LINKER_CREATED) != 0
2450 || ssect->type == SHT_INIT_ARRAY
2451 || ssect->type == SHT_FINI_ARRAY))
2453 elf_section_type (sec) = ssect->type;
2454 elf_section_flags (sec) = ssect->attr;
2458 return _bfd_generic_new_section_hook (abfd, sec);
2461 /* Create a new bfd section from an ELF program header.
2463 Since program segments have no names, we generate a synthetic name
2464 of the form segment<NUM>, where NUM is generally the index in the
2465 program header table. For segments that are split (see below) we
2466 generate the names segment<NUM>a and segment<NUM>b.
2468 Note that some program segments may have a file size that is different than
2469 (less than) the memory size. All this means is that at execution the
2470 system must allocate the amount of memory specified by the memory size,
2471 but only initialize it with the first "file size" bytes read from the
2472 file. This would occur for example, with program segments consisting
2473 of combined data+bss.
2475 To handle the above situation, this routine generates TWO bfd sections
2476 for the single program segment. The first has the length specified by
2477 the file size of the segment, and the second has the length specified
2478 by the difference between the two sizes. In effect, the segment is split
2479 into its initialized and uninitialized parts.
2484 _bfd_elf_make_section_from_phdr (bfd *abfd,
2485 Elf_Internal_Phdr *hdr,
2487 const char *type_name)
2495 split = ((hdr->p_memsz > 0)
2496 && (hdr->p_filesz > 0)
2497 && (hdr->p_memsz > hdr->p_filesz));
2499 if (hdr->p_filesz > 0)
2501 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2502 len = strlen (namebuf) + 1;
2503 name = (char *) bfd_alloc (abfd, len);
2506 memcpy (name, namebuf, len);
2507 newsect = bfd_make_section (abfd, name);
2508 if (newsect == NULL)
2510 newsect->vma = hdr->p_vaddr;
2511 newsect->lma = hdr->p_paddr;
2512 newsect->size = hdr->p_filesz;
2513 newsect->filepos = hdr->p_offset;
2514 newsect->flags |= SEC_HAS_CONTENTS;
2515 newsect->alignment_power = bfd_log2 (hdr->p_align);
2516 if (hdr->p_type == PT_LOAD)
2518 newsect->flags |= SEC_ALLOC;
2519 newsect->flags |= SEC_LOAD;
2520 if (hdr->p_flags & PF_X)
2522 /* FIXME: all we known is that it has execute PERMISSION,
2524 newsect->flags |= SEC_CODE;
2527 if (!(hdr->p_flags & PF_W))
2529 newsect->flags |= SEC_READONLY;
2533 if (hdr->p_memsz > hdr->p_filesz)
2537 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2538 len = strlen (namebuf) + 1;
2539 name = (char *) bfd_alloc (abfd, len);
2542 memcpy (name, namebuf, len);
2543 newsect = bfd_make_section (abfd, name);
2544 if (newsect == NULL)
2546 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2547 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2548 newsect->size = hdr->p_memsz - hdr->p_filesz;
2549 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2550 align = newsect->vma & -newsect->vma;
2551 if (align == 0 || align > hdr->p_align)
2552 align = hdr->p_align;
2553 newsect->alignment_power = bfd_log2 (align);
2554 if (hdr->p_type == PT_LOAD)
2556 /* Hack for gdb. Segments that have not been modified do
2557 not have their contents written to a core file, on the
2558 assumption that a debugger can find the contents in the
2559 executable. We flag this case by setting the fake
2560 section size to zero. Note that "real" bss sections will
2561 always have their contents dumped to the core file. */
2562 if (bfd_get_format (abfd) == bfd_core)
2564 newsect->flags |= SEC_ALLOC;
2565 if (hdr->p_flags & PF_X)
2566 newsect->flags |= SEC_CODE;
2568 if (!(hdr->p_flags & PF_W))
2569 newsect->flags |= SEC_READONLY;
2576 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2578 const struct elf_backend_data *bed;
2580 switch (hdr->p_type)
2583 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2586 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2589 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2592 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2595 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2597 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2602 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2605 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2607 case PT_GNU_EH_FRAME:
2608 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2612 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2615 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2618 /* Check for any processor-specific program segment types. */
2619 bed = get_elf_backend_data (abfd);
2620 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2624 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2628 _bfd_elf_single_rel_hdr (asection *sec)
2630 if (elf_section_data (sec)->rel.hdr)
2632 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2633 return elf_section_data (sec)->rel.hdr;
2636 return elf_section_data (sec)->rela.hdr;
2639 /* Allocate and initialize a section-header for a new reloc section,
2640 containing relocations against ASECT. It is stored in RELDATA. If
2641 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2645 _bfd_elf_init_reloc_shdr (bfd *abfd,
2646 struct bfd_elf_section_reloc_data *reldata,
2648 bfd_boolean use_rela_p)
2650 Elf_Internal_Shdr *rel_hdr;
2652 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2655 amt = sizeof (Elf_Internal_Shdr);
2656 BFD_ASSERT (reldata->hdr == NULL);
2657 rel_hdr = bfd_zalloc (abfd, amt);
2658 reldata->hdr = rel_hdr;
2660 amt = sizeof ".rela" + strlen (asect->name);
2661 name = (char *) bfd_alloc (abfd, amt);
2664 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2666 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2668 if (rel_hdr->sh_name == (unsigned int) -1)
2670 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2671 rel_hdr->sh_entsize = (use_rela_p
2672 ? bed->s->sizeof_rela
2673 : bed->s->sizeof_rel);
2674 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2675 rel_hdr->sh_flags = 0;
2676 rel_hdr->sh_addr = 0;
2677 rel_hdr->sh_size = 0;
2678 rel_hdr->sh_offset = 0;
2683 /* Return the default section type based on the passed in section flags. */
2686 bfd_elf_get_default_section_type (flagword flags)
2688 if ((flags & SEC_ALLOC) != 0
2689 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2691 return SHT_PROGBITS;
2694 struct fake_section_arg
2696 struct bfd_link_info *link_info;
2700 /* Set up an ELF internal section header for a section. */
2703 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2705 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2706 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2707 struct bfd_elf_section_data *esd = elf_section_data (asect);
2708 Elf_Internal_Shdr *this_hdr;
2709 unsigned int sh_type;
2713 /* We already failed; just get out of the bfd_map_over_sections
2718 this_hdr = &esd->this_hdr;
2720 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2721 asect->name, FALSE);
2722 if (this_hdr->sh_name == (unsigned int) -1)
2728 /* Don't clear sh_flags. Assembler may set additional bits. */
2730 if ((asect->flags & SEC_ALLOC) != 0
2731 || asect->user_set_vma)
2732 this_hdr->sh_addr = asect->vma;
2734 this_hdr->sh_addr = 0;
2736 this_hdr->sh_offset = 0;
2737 this_hdr->sh_size = asect->size;
2738 this_hdr->sh_link = 0;
2739 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2740 /* The sh_entsize and sh_info fields may have been set already by
2741 copy_private_section_data. */
2743 this_hdr->bfd_section = asect;
2744 this_hdr->contents = NULL;
2746 /* If the section type is unspecified, we set it based on
2748 if ((asect->flags & SEC_GROUP) != 0)
2749 sh_type = SHT_GROUP;
2751 sh_type = bfd_elf_get_default_section_type (asect->flags);
2753 if (this_hdr->sh_type == SHT_NULL)
2754 this_hdr->sh_type = sh_type;
2755 else if (this_hdr->sh_type == SHT_NOBITS
2756 && sh_type == SHT_PROGBITS
2757 && (asect->flags & SEC_ALLOC) != 0)
2759 /* Warn if we are changing a NOBITS section to PROGBITS, but
2760 allow the link to proceed. This can happen when users link
2761 non-bss input sections to bss output sections, or emit data
2762 to a bss output section via a linker script. */
2763 (*_bfd_error_handler)
2764 (_("warning: section `%A' type changed to PROGBITS"), asect);
2765 this_hdr->sh_type = sh_type;
2768 switch (this_hdr->sh_type)
2774 case SHT_INIT_ARRAY:
2775 case SHT_FINI_ARRAY:
2776 case SHT_PREINIT_ARRAY:
2783 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2787 this_hdr->sh_entsize = bed->s->sizeof_sym;
2791 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2795 if (get_elf_backend_data (abfd)->may_use_rela_p)
2796 this_hdr->sh_entsize = bed->s->sizeof_rela;
2800 if (get_elf_backend_data (abfd)->may_use_rel_p)
2801 this_hdr->sh_entsize = bed->s->sizeof_rel;
2804 case SHT_GNU_versym:
2805 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2808 case SHT_GNU_verdef:
2809 this_hdr->sh_entsize = 0;
2810 /* objcopy or strip will copy over sh_info, but may not set
2811 cverdefs. The linker will set cverdefs, but sh_info will be
2813 if (this_hdr->sh_info == 0)
2814 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2816 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2817 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2820 case SHT_GNU_verneed:
2821 this_hdr->sh_entsize = 0;
2822 /* objcopy or strip will copy over sh_info, but may not set
2823 cverrefs. The linker will set cverrefs, but sh_info will be
2825 if (this_hdr->sh_info == 0)
2826 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2828 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2829 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2833 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2837 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2841 if ((asect->flags & SEC_ALLOC) != 0)
2842 this_hdr->sh_flags |= SHF_ALLOC;
2843 if ((asect->flags & SEC_READONLY) == 0)
2844 this_hdr->sh_flags |= SHF_WRITE;
2845 if ((asect->flags & SEC_CODE) != 0)
2846 this_hdr->sh_flags |= SHF_EXECINSTR;
2847 if ((asect->flags & SEC_MERGE) != 0)
2849 this_hdr->sh_flags |= SHF_MERGE;
2850 this_hdr->sh_entsize = asect->entsize;
2851 if ((asect->flags & SEC_STRINGS) != 0)
2852 this_hdr->sh_flags |= SHF_STRINGS;
2854 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2855 this_hdr->sh_flags |= SHF_GROUP;
2856 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2858 this_hdr->sh_flags |= SHF_TLS;
2859 if (asect->size == 0
2860 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2862 struct bfd_link_order *o = asect->map_tail.link_order;
2864 this_hdr->sh_size = 0;
2867 this_hdr->sh_size = o->offset + o->size;
2868 if (this_hdr->sh_size != 0)
2869 this_hdr->sh_type = SHT_NOBITS;
2873 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2874 this_hdr->sh_flags |= SHF_EXCLUDE;
2876 /* If the section has relocs, set up a section header for the
2877 SHT_REL[A] section. If two relocation sections are required for
2878 this section, it is up to the processor-specific back-end to
2879 create the other. */
2880 if ((asect->flags & SEC_RELOC) != 0)
2882 /* When doing a relocatable link, create both REL and RELA sections if
2885 /* Do the normal setup if we wouldn't create any sections here. */
2886 && esd->rel.count + esd->rela.count > 0
2887 && (arg->link_info->relocatable || arg->link_info->emitrelocations))
2889 if (esd->rel.count && esd->rel.hdr == NULL
2890 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
2895 if (esd->rela.count && esd->rela.hdr == NULL
2896 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
2902 else if (!_bfd_elf_init_reloc_shdr (abfd,
2904 ? &esd->rela : &esd->rel),
2910 /* Check for processor-specific section types. */
2911 sh_type = this_hdr->sh_type;
2912 if (bed->elf_backend_fake_sections
2913 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2916 if (sh_type == SHT_NOBITS && asect->size != 0)
2918 /* Don't change the header type from NOBITS if we are being
2919 called for objcopy --only-keep-debug. */
2920 this_hdr->sh_type = sh_type;
2924 /* Fill in the contents of a SHT_GROUP section. Called from
2925 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2926 when ELF targets use the generic linker, ld. Called for ld -r
2927 from bfd_elf_final_link. */
2930 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2932 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2933 asection *elt, *first;
2937 /* Ignore linker created group section. See elfNN_ia64_object_p in
2939 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2943 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2945 unsigned long symindx = 0;
2947 /* elf_group_id will have been set up by objcopy and the
2949 if (elf_group_id (sec) != NULL)
2950 symindx = elf_group_id (sec)->udata.i;
2954 /* If called from the assembler, swap_out_syms will have set up
2955 elf_section_syms. */
2956 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2957 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2959 elf_section_data (sec)->this_hdr.sh_info = symindx;
2961 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2963 /* The ELF backend linker sets sh_info to -2 when the group
2964 signature symbol is global, and thus the index can't be
2965 set until all local symbols are output. */
2966 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2967 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2968 unsigned long symndx = sec_data->this_hdr.sh_info;
2969 unsigned long extsymoff = 0;
2970 struct elf_link_hash_entry *h;
2972 if (!elf_bad_symtab (igroup->owner))
2974 Elf_Internal_Shdr *symtab_hdr;
2976 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2977 extsymoff = symtab_hdr->sh_info;
2979 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2980 while (h->root.type == bfd_link_hash_indirect
2981 || h->root.type == bfd_link_hash_warning)
2982 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2984 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2987 /* The contents won't be allocated for "ld -r" or objcopy. */
2989 if (sec->contents == NULL)
2992 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2994 /* Arrange for the section to be written out. */
2995 elf_section_data (sec)->this_hdr.contents = sec->contents;
2996 if (sec->contents == NULL)
3003 loc = sec->contents + sec->size;
3005 /* Get the pointer to the first section in the group that gas
3006 squirreled away here. objcopy arranges for this to be set to the
3007 start of the input section group. */
3008 first = elt = elf_next_in_group (sec);
3010 /* First element is a flag word. Rest of section is elf section
3011 indices for all the sections of the group. Write them backwards
3012 just to keep the group in the same order as given in .section
3013 directives, not that it matters. */
3020 s = s->output_section;
3022 && !bfd_is_abs_section (s))
3024 unsigned int idx = elf_section_data (s)->this_idx;
3027 H_PUT_32 (abfd, idx, loc);
3029 elt = elf_next_in_group (elt);
3034 if ((loc -= 4) != sec->contents)
3037 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3040 /* Assign all ELF section numbers. The dummy first section is handled here
3041 too. The link/info pointers for the standard section types are filled
3042 in here too, while we're at it. */
3045 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3047 struct elf_obj_tdata *t = elf_tdata (abfd);
3049 unsigned int section_number, secn;
3050 Elf_Internal_Shdr **i_shdrp;
3051 struct bfd_elf_section_data *d;
3052 bfd_boolean need_symtab;
3056 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3058 /* SHT_GROUP sections are in relocatable files only. */
3059 if (link_info == NULL || link_info->relocatable)
3061 /* Put SHT_GROUP sections first. */
3062 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3064 d = elf_section_data (sec);
3066 if (d->this_hdr.sh_type == SHT_GROUP)
3068 if (sec->flags & SEC_LINKER_CREATED)
3070 /* Remove the linker created SHT_GROUP sections. */
3071 bfd_section_list_remove (abfd, sec);
3072 abfd->section_count--;
3075 d->this_idx = section_number++;
3080 for (sec = abfd->sections; sec; sec = sec->next)
3082 d = elf_section_data (sec);
3084 if (d->this_hdr.sh_type != SHT_GROUP)
3085 d->this_idx = section_number++;
3086 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3089 d->rel.idx = section_number++;
3090 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3097 d->rela.idx = section_number++;
3098 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3104 elf_shstrtab_sec (abfd) = section_number++;
3105 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3106 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3108 need_symtab = (bfd_get_symcount (abfd) > 0
3109 || (link_info == NULL
3110 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3114 elf_onesymtab (abfd) = section_number++;
3115 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3116 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3118 elf_symtab_shndx (abfd) = section_number++;
3119 t->symtab_shndx_hdr.sh_name
3120 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3121 ".symtab_shndx", FALSE);
3122 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3125 elf_strtab_sec (abfd) = section_number++;
3126 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3129 if (section_number >= SHN_LORESERVE)
3131 _bfd_error_handler (_("%B: too many sections: %u"),
3132 abfd, section_number);
3136 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3137 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3139 elf_numsections (abfd) = section_number;
3140 elf_elfheader (abfd)->e_shnum = section_number;
3142 /* Set up the list of section header pointers, in agreement with the
3144 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3145 sizeof (Elf_Internal_Shdr *));
3146 if (i_shdrp == NULL)
3149 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3150 sizeof (Elf_Internal_Shdr));
3151 if (i_shdrp[0] == NULL)
3153 bfd_release (abfd, i_shdrp);
3157 elf_elfsections (abfd) = i_shdrp;
3159 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3162 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3163 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3165 i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
3166 t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
3168 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3169 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3172 for (sec = abfd->sections; sec; sec = sec->next)
3177 d = elf_section_data (sec);
3179 i_shdrp[d->this_idx] = &d->this_hdr;
3180 if (d->rel.idx != 0)
3181 i_shdrp[d->rel.idx] = d->rel.hdr;
3182 if (d->rela.idx != 0)
3183 i_shdrp[d->rela.idx] = d->rela.hdr;
3185 /* Fill in the sh_link and sh_info fields while we're at it. */
3187 /* sh_link of a reloc section is the section index of the symbol
3188 table. sh_info is the section index of the section to which
3189 the relocation entries apply. */
3190 if (d->rel.idx != 0)
3192 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3193 d->rel.hdr->sh_info = d->this_idx;
3194 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3196 if (d->rela.idx != 0)
3198 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3199 d->rela.hdr->sh_info = d->this_idx;
3200 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3203 /* We need to set up sh_link for SHF_LINK_ORDER. */
3204 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3206 s = elf_linked_to_section (sec);
3209 /* elf_linked_to_section points to the input section. */
3210 if (link_info != NULL)
3212 /* Check discarded linkonce section. */
3213 if (discarded_section (s))
3216 (*_bfd_error_handler)
3217 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3218 abfd, d->this_hdr.bfd_section,
3220 /* Point to the kept section if it has the same
3221 size as the discarded one. */
3222 kept = _bfd_elf_check_kept_section (s, link_info);
3225 bfd_set_error (bfd_error_bad_value);
3231 s = s->output_section;
3232 BFD_ASSERT (s != NULL);
3236 /* Handle objcopy. */
3237 if (s->output_section == NULL)
3239 (*_bfd_error_handler)
3240 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3241 abfd, d->this_hdr.bfd_section, s, s->owner);
3242 bfd_set_error (bfd_error_bad_value);
3245 s = s->output_section;
3247 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3252 The Intel C compiler generates SHT_IA_64_UNWIND with
3253 SHF_LINK_ORDER. But it doesn't set the sh_link or
3254 sh_info fields. Hence we could get the situation
3256 const struct elf_backend_data *bed
3257 = get_elf_backend_data (abfd);
3258 if (bed->link_order_error_handler)
3259 bed->link_order_error_handler
3260 (_("%B: warning: sh_link not set for section `%A'"),
3265 switch (d->this_hdr.sh_type)
3269 /* A reloc section which we are treating as a normal BFD
3270 section. sh_link is the section index of the symbol
3271 table. sh_info is the section index of the section to
3272 which the relocation entries apply. We assume that an
3273 allocated reloc section uses the dynamic symbol table.
3274 FIXME: How can we be sure? */
3275 s = bfd_get_section_by_name (abfd, ".dynsym");
3277 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3279 /* We look up the section the relocs apply to by name. */
3281 if (d->this_hdr.sh_type == SHT_REL)
3285 s = bfd_get_section_by_name (abfd, name);
3288 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3289 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3294 /* We assume that a section named .stab*str is a stabs
3295 string section. We look for a section with the same name
3296 but without the trailing ``str'', and set its sh_link
3297 field to point to this section. */
3298 if (CONST_STRNEQ (sec->name, ".stab")
3299 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3304 len = strlen (sec->name);
3305 alc = (char *) bfd_malloc (len - 2);
3308 memcpy (alc, sec->name, len - 3);
3309 alc[len - 3] = '\0';
3310 s = bfd_get_section_by_name (abfd, alc);
3314 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3316 /* This is a .stab section. */
3317 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3318 elf_section_data (s)->this_hdr.sh_entsize
3319 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3326 case SHT_GNU_verneed:
3327 case SHT_GNU_verdef:
3328 /* sh_link is the section header index of the string table
3329 used for the dynamic entries, or the symbol table, or the
3331 s = bfd_get_section_by_name (abfd, ".dynstr");
3333 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3336 case SHT_GNU_LIBLIST:
3337 /* sh_link is the section header index of the prelink library
3338 list used for the dynamic entries, or the symbol table, or
3339 the version strings. */
3340 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3341 ? ".dynstr" : ".gnu.libstr");
3343 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3348 case SHT_GNU_versym:
3349 /* sh_link is the section header index of the symbol table
3350 this hash table or version table is for. */
3351 s = bfd_get_section_by_name (abfd, ".dynsym");
3353 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3357 d->this_hdr.sh_link = elf_onesymtab (abfd);
3361 for (secn = 1; secn < section_number; ++secn)
3362 if (i_shdrp[secn] == NULL)
3363 i_shdrp[secn] = i_shdrp[0];
3365 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3366 i_shdrp[secn]->sh_name);
3371 sym_is_global (bfd *abfd, asymbol *sym)
3373 /* If the backend has a special mapping, use it. */
3374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3375 if (bed->elf_backend_sym_is_global)
3376 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3378 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3379 || bfd_is_und_section (bfd_get_section (sym))
3380 || bfd_is_com_section (bfd_get_section (sym)));
3383 /* Don't output section symbols for sections that are not going to be
3384 output, that are duplicates or there is no BFD section. */
3387 ignore_section_sym (bfd *abfd, asymbol *sym)
3389 elf_symbol_type *type_ptr;
3391 if ((sym->flags & BSF_SECTION_SYM) == 0)
3394 type_ptr = elf_symbol_from (abfd, sym);
3395 return ((type_ptr != NULL
3396 && type_ptr->internal_elf_sym.st_shndx != 0
3397 && bfd_is_abs_section (sym->section))
3398 || !(sym->section->owner == abfd
3399 || (sym->section->output_section->owner == abfd
3400 && sym->section->output_offset == 0)
3401 || bfd_is_abs_section (sym->section)));
3404 /* Map symbol from it's internal number to the external number, moving
3405 all local symbols to be at the head of the list. */
3408 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3410 unsigned int symcount = bfd_get_symcount (abfd);
3411 asymbol **syms = bfd_get_outsymbols (abfd);
3412 asymbol **sect_syms;
3413 unsigned int num_locals = 0;
3414 unsigned int num_globals = 0;
3415 unsigned int num_locals2 = 0;
3416 unsigned int num_globals2 = 0;
3423 fprintf (stderr, "elf_map_symbols\n");
3427 for (asect = abfd->sections; asect; asect = asect->next)
3429 if (max_index < asect->index)
3430 max_index = asect->index;
3434 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3435 if (sect_syms == NULL)
3437 elf_section_syms (abfd) = sect_syms;
3438 elf_num_section_syms (abfd) = max_index;
3440 /* Init sect_syms entries for any section symbols we have already
3441 decided to output. */
3442 for (idx = 0; idx < symcount; idx++)
3444 asymbol *sym = syms[idx];
3446 if ((sym->flags & BSF_SECTION_SYM) != 0
3448 && !ignore_section_sym (abfd, sym)
3449 && !bfd_is_abs_section (sym->section))
3451 asection *sec = sym->section;
3453 if (sec->owner != abfd)
3454 sec = sec->output_section;
3456 sect_syms[sec->index] = syms[idx];
3460 /* Classify all of the symbols. */
3461 for (idx = 0; idx < symcount; idx++)
3463 if (sym_is_global (abfd, syms[idx]))
3465 else if (!ignore_section_sym (abfd, syms[idx]))
3469 /* We will be adding a section symbol for each normal BFD section. Most
3470 sections will already have a section symbol in outsymbols, but
3471 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3472 at least in that case. */
3473 for (asect = abfd->sections; asect; asect = asect->next)
3475 if (sect_syms[asect->index] == NULL)
3477 if (!sym_is_global (abfd, asect->symbol))
3484 /* Now sort the symbols so the local symbols are first. */
3485 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3486 sizeof (asymbol *));
3488 if (new_syms == NULL)
3491 for (idx = 0; idx < symcount; idx++)
3493 asymbol *sym = syms[idx];
3496 if (sym_is_global (abfd, sym))
3497 i = num_locals + num_globals2++;
3498 else if (!ignore_section_sym (abfd, sym))
3503 sym->udata.i = i + 1;
3505 for (asect = abfd->sections; asect; asect = asect->next)
3507 if (sect_syms[asect->index] == NULL)
3509 asymbol *sym = asect->symbol;
3512 sect_syms[asect->index] = sym;
3513 if (!sym_is_global (abfd, sym))
3516 i = num_locals + num_globals2++;
3518 sym->udata.i = i + 1;
3522 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3524 *pnum_locals = num_locals;
3528 /* Align to the maximum file alignment that could be required for any
3529 ELF data structure. */
3531 static inline file_ptr
3532 align_file_position (file_ptr off, int align)
3534 return (off + align - 1) & ~(align - 1);
3537 /* Assign a file position to a section, optionally aligning to the
3538 required section alignment. */
3541 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3545 if (align && i_shdrp->sh_addralign > 1)
3546 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3547 i_shdrp->sh_offset = offset;
3548 if (i_shdrp->bfd_section != NULL)
3549 i_shdrp->bfd_section->filepos = offset;
3550 if (i_shdrp->sh_type != SHT_NOBITS)
3551 offset += i_shdrp->sh_size;
3555 /* Compute the file positions we are going to put the sections at, and
3556 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3557 is not NULL, this is being called by the ELF backend linker. */
3560 _bfd_elf_compute_section_file_positions (bfd *abfd,
3561 struct bfd_link_info *link_info)
3563 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3564 struct fake_section_arg fsargs;
3566 struct bfd_strtab_hash *strtab = NULL;
3567 Elf_Internal_Shdr *shstrtab_hdr;
3568 bfd_boolean need_symtab;
3570 if (abfd->output_has_begun)
3573 /* Do any elf backend specific processing first. */
3574 if (bed->elf_backend_begin_write_processing)
3575 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3577 if (! prep_headers (abfd))
3580 /* Post process the headers if necessary. */
3581 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3583 fsargs.failed = FALSE;
3584 fsargs.link_info = link_info;
3585 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3589 if (!assign_section_numbers (abfd, link_info))
3592 /* The backend linker builds symbol table information itself. */
3593 need_symtab = (link_info == NULL
3594 && (bfd_get_symcount (abfd) > 0
3595 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3599 /* Non-zero if doing a relocatable link. */
3600 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3602 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3607 if (link_info == NULL)
3609 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3614 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3615 /* sh_name was set in prep_headers. */
3616 shstrtab_hdr->sh_type = SHT_STRTAB;
3617 shstrtab_hdr->sh_flags = 0;
3618 shstrtab_hdr->sh_addr = 0;
3619 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3620 shstrtab_hdr->sh_entsize = 0;
3621 shstrtab_hdr->sh_link = 0;
3622 shstrtab_hdr->sh_info = 0;
3623 /* sh_offset is set in assign_file_positions_except_relocs. */
3624 shstrtab_hdr->sh_addralign = 1;
3626 if (!assign_file_positions_except_relocs (abfd, link_info))
3632 Elf_Internal_Shdr *hdr;
3634 off = elf_next_file_pos (abfd);
3636 hdr = &elf_tdata (abfd)->symtab_hdr;
3637 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3639 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3640 if (hdr->sh_size != 0)
3641 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3643 hdr = &elf_tdata (abfd)->strtab_hdr;
3644 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3646 elf_next_file_pos (abfd) = off;
3648 /* Now that we know where the .strtab section goes, write it
3650 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3651 || ! _bfd_stringtab_emit (abfd, strtab))
3653 _bfd_stringtab_free (strtab);
3656 abfd->output_has_begun = TRUE;
3661 /* Make an initial estimate of the size of the program header. If we
3662 get the number wrong here, we'll redo section placement. */
3664 static bfd_size_type
3665 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3669 const struct elf_backend_data *bed;
3671 /* Assume we will need exactly two PT_LOAD segments: one for text
3672 and one for data. */
3675 s = bfd_get_section_by_name (abfd, ".interp");
3676 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3678 /* If we have a loadable interpreter section, we need a
3679 PT_INTERP segment. In this case, assume we also need a
3680 PT_PHDR segment, although that may not be true for all
3685 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3687 /* We need a PT_DYNAMIC segment. */
3691 if (info != NULL && info->relro)
3693 /* We need a PT_GNU_RELRO segment. */
3697 if (elf_eh_frame_hdr (abfd))
3699 /* We need a PT_GNU_EH_FRAME segment. */
3703 if (elf_stack_flags (abfd))
3705 /* We need a PT_GNU_STACK segment. */
3709 for (s = abfd->sections; s != NULL; s = s->next)
3711 if ((s->flags & SEC_LOAD) != 0
3712 && CONST_STRNEQ (s->name, ".note"))
3714 /* We need a PT_NOTE segment. */
3716 /* Try to create just one PT_NOTE segment
3717 for all adjacent loadable .note* sections.
3718 gABI requires that within a PT_NOTE segment
3719 (and also inside of each SHT_NOTE section)
3720 each note is padded to a multiple of 4 size,
3721 so we check whether the sections are correctly
3723 if (s->alignment_power == 2)
3724 while (s->next != NULL
3725 && s->next->alignment_power == 2
3726 && (s->next->flags & SEC_LOAD) != 0
3727 && CONST_STRNEQ (s->next->name, ".note"))
3732 for (s = abfd->sections; s != NULL; s = s->next)
3734 if (s->flags & SEC_THREAD_LOCAL)
3736 /* We need a PT_TLS segment. */
3742 /* Let the backend count up any program headers it might need. */
3743 bed = get_elf_backend_data (abfd);
3744 if (bed->elf_backend_additional_program_headers)
3748 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3754 return segs * bed->s->sizeof_phdr;
3757 /* Find the segment that contains the output_section of section. */
3760 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3762 struct elf_segment_map *m;
3763 Elf_Internal_Phdr *p;
3765 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
3771 for (i = m->count - 1; i >= 0; i--)
3772 if (m->sections[i] == section)
3779 /* Create a mapping from a set of sections to a program segment. */
3781 static struct elf_segment_map *
3782 make_mapping (bfd *abfd,
3783 asection **sections,
3788 struct elf_segment_map *m;
3793 amt = sizeof (struct elf_segment_map);
3794 amt += (to - from - 1) * sizeof (asection *);
3795 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3799 m->p_type = PT_LOAD;
3800 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3801 m->sections[i - from] = *hdrpp;
3802 m->count = to - from;
3804 if (from == 0 && phdr)
3806 /* Include the headers in the first PT_LOAD segment. */
3807 m->includes_filehdr = 1;
3808 m->includes_phdrs = 1;
3814 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3817 struct elf_segment_map *
3818 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3820 struct elf_segment_map *m;
3822 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3823 sizeof (struct elf_segment_map));
3827 m->p_type = PT_DYNAMIC;
3829 m->sections[0] = dynsec;
3834 /* Possibly add or remove segments from the segment map. */
3837 elf_modify_segment_map (bfd *abfd,
3838 struct bfd_link_info *info,
3839 bfd_boolean remove_empty_load)
3841 struct elf_segment_map **m;
3842 const struct elf_backend_data *bed;
3844 /* The placement algorithm assumes that non allocated sections are
3845 not in PT_LOAD segments. We ensure this here by removing such
3846 sections from the segment map. We also remove excluded
3847 sections. Finally, any PT_LOAD segment without sections is
3849 m = &elf_seg_map (abfd);
3852 unsigned int i, new_count;
3854 for (new_count = 0, i = 0; i < (*m)->count; i++)
3856 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3857 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3858 || (*m)->p_type != PT_LOAD))
3860 (*m)->sections[new_count] = (*m)->sections[i];
3864 (*m)->count = new_count;
3866 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3872 bed = get_elf_backend_data (abfd);
3873 if (bed->elf_backend_modify_segment_map != NULL)
3875 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3882 /* Set up a mapping from BFD sections to program segments. */
3885 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3888 struct elf_segment_map *m;
3889 asection **sections = NULL;
3890 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3891 bfd_boolean no_user_phdrs;
3893 no_user_phdrs = elf_seg_map (abfd) == NULL;
3896 info->user_phdrs = !no_user_phdrs;
3898 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3902 struct elf_segment_map *mfirst;
3903 struct elf_segment_map **pm;
3906 unsigned int phdr_index;
3907 bfd_vma maxpagesize;
3909 bfd_boolean phdr_in_segment = TRUE;
3910 bfd_boolean writable;
3912 asection *first_tls = NULL;
3913 asection *dynsec, *eh_frame_hdr;
3915 bfd_vma addr_mask, wrap_to = 0;
3917 /* Select the allocated sections, and sort them. */
3919 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3920 sizeof (asection *));
3921 if (sections == NULL)
3924 /* Calculate top address, avoiding undefined behaviour of shift
3925 left operator when shift count is equal to size of type
3927 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3928 addr_mask = (addr_mask << 1) + 1;
3931 for (s = abfd->sections; s != NULL; s = s->next)
3933 if ((s->flags & SEC_ALLOC) != 0)
3937 /* A wrapping section potentially clashes with header. */
3938 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3939 wrap_to = (s->lma + s->size) & addr_mask;
3942 BFD_ASSERT (i <= bfd_count_sections (abfd));
3945 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3947 /* Build the mapping. */
3952 /* If we have a .interp section, then create a PT_PHDR segment for
3953 the program headers and a PT_INTERP segment for the .interp
3955 s = bfd_get_section_by_name (abfd, ".interp");
3956 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3958 amt = sizeof (struct elf_segment_map);
3959 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3963 m->p_type = PT_PHDR;
3964 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3965 m->p_flags = PF_R | PF_X;
3966 m->p_flags_valid = 1;
3967 m->includes_phdrs = 1;
3972 amt = sizeof (struct elf_segment_map);
3973 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3977 m->p_type = PT_INTERP;
3985 /* Look through the sections. We put sections in the same program
3986 segment when the start of the second section can be placed within
3987 a few bytes of the end of the first section. */
3991 maxpagesize = bed->maxpagesize;
3993 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3995 && (dynsec->flags & SEC_LOAD) == 0)
3998 /* Deal with -Ttext or something similar such that the first section
3999 is not adjacent to the program headers. This is an
4000 approximation, since at this point we don't know exactly how many
4001 program headers we will need. */
4004 bfd_size_type phdr_size = elf_program_header_size (abfd);
4006 if (phdr_size == (bfd_size_type) -1)
4007 phdr_size = get_program_header_size (abfd, info);
4008 phdr_size += bed->s->sizeof_ehdr;
4009 if ((abfd->flags & D_PAGED) == 0
4010 || (sections[0]->lma & addr_mask) < phdr_size
4011 || ((sections[0]->lma & addr_mask) % maxpagesize
4012 < phdr_size % maxpagesize)
4013 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
4014 phdr_in_segment = FALSE;
4017 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4020 bfd_boolean new_segment;
4024 /* See if this section and the last one will fit in the same
4027 if (last_hdr == NULL)
4029 /* If we don't have a segment yet, then we don't need a new
4030 one (we build the last one after this loop). */
4031 new_segment = FALSE;
4033 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4035 /* If this section has a different relation between the
4036 virtual address and the load address, then we need a new
4040 else if (hdr->lma < last_hdr->lma + last_size
4041 || last_hdr->lma + last_size < last_hdr->lma)
4043 /* If this section has a load address that makes it overlap
4044 the previous section, then we need a new segment. */
4047 /* In the next test we have to be careful when last_hdr->lma is close
4048 to the end of the address space. If the aligned address wraps
4049 around to the start of the address space, then there are no more
4050 pages left in memory and it is OK to assume that the current
4051 section can be included in the current segment. */
4052 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4054 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4057 /* If putting this section in this segment would force us to
4058 skip a page in the segment, then we need a new segment. */
4061 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4062 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4064 /* We don't want to put a loadable section after a
4065 nonloadable section in the same segment.
4066 Consider .tbss sections as loadable for this purpose. */
4069 else if ((abfd->flags & D_PAGED) == 0)
4071 /* If the file is not demand paged, which means that we
4072 don't require the sections to be correctly aligned in the
4073 file, then there is no other reason for a new segment. */
4074 new_segment = FALSE;
4077 && (hdr->flags & SEC_READONLY) == 0
4078 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4079 != (hdr->lma & -maxpagesize)))
4081 /* We don't want to put a writable section in a read only
4082 segment, unless they are on the same page in memory
4083 anyhow. We already know that the last section does not
4084 bring us past the current section on the page, so the
4085 only case in which the new section is not on the same
4086 page as the previous section is when the previous section
4087 ends precisely on a page boundary. */
4092 /* Otherwise, we can use the same segment. */
4093 new_segment = FALSE;
4096 /* Allow interested parties a chance to override our decision. */
4097 if (last_hdr != NULL
4099 && info->callbacks->override_segment_assignment != NULL)
4101 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4107 if ((hdr->flags & SEC_READONLY) == 0)
4110 /* .tbss sections effectively have zero size. */
4111 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4112 != SEC_THREAD_LOCAL)
4113 last_size = hdr->size;
4119 /* We need a new program segment. We must create a new program
4120 header holding all the sections from phdr_index until hdr. */
4122 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4129 if ((hdr->flags & SEC_READONLY) == 0)
4135 /* .tbss sections effectively have zero size. */
4136 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4137 last_size = hdr->size;
4141 phdr_in_segment = FALSE;
4144 /* Create a final PT_LOAD program segment, but not if it's just
4146 if (last_hdr != NULL
4147 && (i - phdr_index != 1
4148 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4149 != SEC_THREAD_LOCAL)))
4151 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4159 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4162 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4169 /* For each batch of consecutive loadable .note sections,
4170 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4171 because if we link together nonloadable .note sections and
4172 loadable .note sections, we will generate two .note sections
4173 in the output file. FIXME: Using names for section types is
4175 for (s = abfd->sections; s != NULL; s = s->next)
4177 if ((s->flags & SEC_LOAD) != 0
4178 && CONST_STRNEQ (s->name, ".note"))
4183 amt = sizeof (struct elf_segment_map);
4184 if (s->alignment_power == 2)
4185 for (s2 = s; s2->next != NULL; s2 = s2->next)
4187 if (s2->next->alignment_power == 2
4188 && (s2->next->flags & SEC_LOAD) != 0
4189 && CONST_STRNEQ (s2->next->name, ".note")
4190 && align_power (s2->lma + s2->size, 2)
4196 amt += (count - 1) * sizeof (asection *);
4197 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4201 m->p_type = PT_NOTE;
4205 m->sections[m->count - count--] = s;
4206 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4209 m->sections[m->count - 1] = s;
4210 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4214 if (s->flags & SEC_THREAD_LOCAL)
4222 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4225 amt = sizeof (struct elf_segment_map);
4226 amt += (tls_count - 1) * sizeof (asection *);
4227 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4232 m->count = tls_count;
4233 /* Mandated PF_R. */
4235 m->p_flags_valid = 1;
4237 for (i = 0; i < (unsigned int) tls_count; ++i)
4239 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4242 (_("%B: TLS sections are not adjacent:"), abfd);
4245 while (i < (unsigned int) tls_count)
4247 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4249 _bfd_error_handler (_(" TLS: %A"), s);
4253 _bfd_error_handler (_(" non-TLS: %A"), s);
4256 bfd_set_error (bfd_error_bad_value);
4267 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4269 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4270 if (eh_frame_hdr != NULL
4271 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4273 amt = sizeof (struct elf_segment_map);
4274 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4278 m->p_type = PT_GNU_EH_FRAME;
4280 m->sections[0] = eh_frame_hdr->output_section;
4286 if (elf_stack_flags (abfd))
4288 amt = sizeof (struct elf_segment_map);
4289 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4293 m->p_type = PT_GNU_STACK;
4294 m->p_flags = elf_stack_flags (abfd);
4295 m->p_align = bed->stack_align;
4296 m->p_flags_valid = 1;
4297 m->p_align_valid = m->p_align != 0;
4298 if (info->stacksize > 0)
4300 m->p_size = info->stacksize;
4301 m->p_size_valid = 1;
4308 if (info != NULL && info->relro)
4310 for (m = mfirst; m != NULL; m = m->next)
4312 if (m->p_type == PT_LOAD
4314 && m->sections[0]->vma >= info->relro_start
4315 && m->sections[0]->vma < info->relro_end)
4318 while (--i != (unsigned) -1)
4319 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4320 == (SEC_LOAD | SEC_HAS_CONTENTS))
4323 if (i != (unsigned) -1)
4328 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4331 amt = sizeof (struct elf_segment_map);
4332 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4336 m->p_type = PT_GNU_RELRO;
4338 m->p_flags_valid = 1;
4346 elf_seg_map (abfd) = mfirst;
4349 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4352 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4354 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4359 if (sections != NULL)
4364 /* Sort sections by address. */
4367 elf_sort_sections (const void *arg1, const void *arg2)
4369 const asection *sec1 = *(const asection **) arg1;
4370 const asection *sec2 = *(const asection **) arg2;
4371 bfd_size_type size1, size2;
4373 /* Sort by LMA first, since this is the address used to
4374 place the section into a segment. */
4375 if (sec1->lma < sec2->lma)
4377 else if (sec1->lma > sec2->lma)
4380 /* Then sort by VMA. Normally the LMA and the VMA will be
4381 the same, and this will do nothing. */
4382 if (sec1->vma < sec2->vma)
4384 else if (sec1->vma > sec2->vma)
4387 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4389 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4395 /* If the indicies are the same, do not return 0
4396 here, but continue to try the next comparison. */
4397 if (sec1->target_index - sec2->target_index != 0)
4398 return sec1->target_index - sec2->target_index;
4403 else if (TOEND (sec2))
4408 /* Sort by size, to put zero sized sections
4409 before others at the same address. */
4411 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4412 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4419 return sec1->target_index - sec2->target_index;
4422 /* Ian Lance Taylor writes:
4424 We shouldn't be using % with a negative signed number. That's just
4425 not good. We have to make sure either that the number is not
4426 negative, or that the number has an unsigned type. When the types
4427 are all the same size they wind up as unsigned. When file_ptr is a
4428 larger signed type, the arithmetic winds up as signed long long,
4431 What we're trying to say here is something like ``increase OFF by
4432 the least amount that will cause it to be equal to the VMA modulo
4434 /* In other words, something like:
4436 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4437 off_offset = off % bed->maxpagesize;
4438 if (vma_offset < off_offset)
4439 adjustment = vma_offset + bed->maxpagesize - off_offset;
4441 adjustment = vma_offset - off_offset;
4443 which can can be collapsed into the expression below. */
4446 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4448 /* PR binutils/16199: Handle an alignment of zero. */
4449 if (maxpagesize == 0)
4451 return ((vma - off) % maxpagesize);
4455 print_segment_map (const struct elf_segment_map *m)
4458 const char *pt = get_segment_type (m->p_type);
4463 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4464 sprintf (buf, "LOPROC+%7.7x",
4465 (unsigned int) (m->p_type - PT_LOPROC));
4466 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4467 sprintf (buf, "LOOS+%7.7x",
4468 (unsigned int) (m->p_type - PT_LOOS));
4470 snprintf (buf, sizeof (buf), "%8.8x",
4471 (unsigned int) m->p_type);
4475 fprintf (stderr, "%s:", pt);
4476 for (j = 0; j < m->count; j++)
4477 fprintf (stderr, " %s", m->sections [j]->name);
4483 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4488 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4490 buf = bfd_zmalloc (len);
4493 ret = bfd_bwrite (buf, len, abfd) == len;
4498 /* Assign file positions to the sections based on the mapping from
4499 sections to segments. This function also sets up some fields in
4503 assign_file_positions_for_load_sections (bfd *abfd,
4504 struct bfd_link_info *link_info)
4506 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4507 struct elf_segment_map *m;
4508 Elf_Internal_Phdr *phdrs;
4509 Elf_Internal_Phdr *p;
4511 bfd_size_type maxpagesize;
4514 bfd_vma header_pad = 0;
4516 if (link_info == NULL
4517 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4521 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4525 header_pad = m->header_size;
4530 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4531 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4535 /* PR binutils/12467. */
4536 elf_elfheader (abfd)->e_phoff = 0;
4537 elf_elfheader (abfd)->e_phentsize = 0;
4540 elf_elfheader (abfd)->e_phnum = alloc;
4542 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4543 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4545 BFD_ASSERT (elf_program_header_size (abfd)
4546 >= alloc * bed->s->sizeof_phdr);
4550 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4554 /* We're writing the size in elf_program_header_size (abfd),
4555 see assign_file_positions_except_relocs, so make sure we have
4556 that amount allocated, with trailing space cleared.
4557 The variable alloc contains the computed need, while
4558 elf_program_header_size (abfd) contains the size used for the
4560 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4561 where the layout is forced to according to a larger size in the
4562 last iterations for the testcase ld-elf/header. */
4563 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4565 phdrs = (Elf_Internal_Phdr *)
4567 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4568 sizeof (Elf_Internal_Phdr));
4569 elf_tdata (abfd)->phdr = phdrs;
4574 if ((abfd->flags & D_PAGED) != 0)
4575 maxpagesize = bed->maxpagesize;
4577 off = bed->s->sizeof_ehdr;
4578 off += alloc * bed->s->sizeof_phdr;
4579 if (header_pad < (bfd_vma) off)
4585 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4587 m = m->next, p++, j++)
4591 bfd_boolean no_contents;
4593 /* If elf_segment_map is not from map_sections_to_segments, the
4594 sections may not be correctly ordered. NOTE: sorting should
4595 not be done to the PT_NOTE section of a corefile, which may
4596 contain several pseudo-sections artificially created by bfd.
4597 Sorting these pseudo-sections breaks things badly. */
4599 && !(elf_elfheader (abfd)->e_type == ET_CORE
4600 && m->p_type == PT_NOTE))
4601 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4604 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4605 number of sections with contents contributing to both p_filesz
4606 and p_memsz, followed by a number of sections with no contents
4607 that just contribute to p_memsz. In this loop, OFF tracks next
4608 available file offset for PT_LOAD and PT_NOTE segments. */
4609 p->p_type = m->p_type;
4610 p->p_flags = m->p_flags;
4615 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4617 if (m->p_paddr_valid)
4618 p->p_paddr = m->p_paddr;
4619 else if (m->count == 0)
4622 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4624 if (p->p_type == PT_LOAD
4625 && (abfd->flags & D_PAGED) != 0)
4627 /* p_align in demand paged PT_LOAD segments effectively stores
4628 the maximum page size. When copying an executable with
4629 objcopy, we set m->p_align from the input file. Use this
4630 value for maxpagesize rather than bed->maxpagesize, which
4631 may be different. Note that we use maxpagesize for PT_TLS
4632 segment alignment later in this function, so we are relying
4633 on at least one PT_LOAD segment appearing before a PT_TLS
4635 if (m->p_align_valid)
4636 maxpagesize = m->p_align;
4638 p->p_align = maxpagesize;
4640 else if (m->p_align_valid)
4641 p->p_align = m->p_align;
4642 else if (m->count == 0)
4643 p->p_align = 1 << bed->s->log_file_align;
4647 no_contents = FALSE;
4649 if (p->p_type == PT_LOAD
4652 bfd_size_type align;
4653 unsigned int align_power = 0;
4655 if (m->p_align_valid)
4659 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4661 unsigned int secalign;
4663 secalign = bfd_get_section_alignment (abfd, *secpp);
4664 if (secalign > align_power)
4665 align_power = secalign;
4667 align = (bfd_size_type) 1 << align_power;
4668 if (align < maxpagesize)
4669 align = maxpagesize;
4672 for (i = 0; i < m->count; i++)
4673 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4674 /* If we aren't making room for this section, then
4675 it must be SHT_NOBITS regardless of what we've
4676 set via struct bfd_elf_special_section. */
4677 elf_section_type (m->sections[i]) = SHT_NOBITS;
4679 /* Find out whether this segment contains any loadable
4682 for (i = 0; i < m->count; i++)
4683 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4685 no_contents = FALSE;
4689 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4693 /* We shouldn't need to align the segment on disk since
4694 the segment doesn't need file space, but the gABI
4695 arguably requires the alignment and glibc ld.so
4696 checks it. So to comply with the alignment
4697 requirement but not waste file space, we adjust
4698 p_offset for just this segment. (OFF_ADJUST is
4699 subtracted from OFF later.) This may put p_offset
4700 past the end of file, but that shouldn't matter. */
4705 /* Make sure the .dynamic section is the first section in the
4706 PT_DYNAMIC segment. */
4707 else if (p->p_type == PT_DYNAMIC
4709 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4712 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4714 bfd_set_error (bfd_error_bad_value);
4717 /* Set the note section type to SHT_NOTE. */
4718 else if (p->p_type == PT_NOTE)
4719 for (i = 0; i < m->count; i++)
4720 elf_section_type (m->sections[i]) = SHT_NOTE;
4726 if (m->includes_filehdr)
4728 if (!m->p_flags_valid)
4730 p->p_filesz = bed->s->sizeof_ehdr;
4731 p->p_memsz = bed->s->sizeof_ehdr;
4734 if (p->p_vaddr < (bfd_vma) off)
4736 (*_bfd_error_handler)
4737 (_("%B: Not enough room for program headers, try linking with -N"),
4739 bfd_set_error (bfd_error_bad_value);
4744 if (!m->p_paddr_valid)
4749 if (m->includes_phdrs)
4751 if (!m->p_flags_valid)
4754 if (!m->includes_filehdr)
4756 p->p_offset = bed->s->sizeof_ehdr;
4760 p->p_vaddr -= off - p->p_offset;
4761 if (!m->p_paddr_valid)
4762 p->p_paddr -= off - p->p_offset;
4766 p->p_filesz += alloc * bed->s->sizeof_phdr;
4767 p->p_memsz += alloc * bed->s->sizeof_phdr;
4770 p->p_filesz += header_pad;
4771 p->p_memsz += header_pad;
4775 if (p->p_type == PT_LOAD
4776 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4778 if (!m->includes_filehdr && !m->includes_phdrs)
4784 adjust = off - (p->p_offset + p->p_filesz);
4786 p->p_filesz += adjust;
4787 p->p_memsz += adjust;
4791 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4792 maps. Set filepos for sections in PT_LOAD segments, and in
4793 core files, for sections in PT_NOTE segments.
4794 assign_file_positions_for_non_load_sections will set filepos
4795 for other sections and update p_filesz for other segments. */
4796 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4799 bfd_size_type align;
4800 Elf_Internal_Shdr *this_hdr;
4803 this_hdr = &elf_section_data (sec)->this_hdr;
4804 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4806 if ((p->p_type == PT_LOAD
4807 || p->p_type == PT_TLS)
4808 && (this_hdr->sh_type != SHT_NOBITS
4809 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4810 && ((this_hdr->sh_flags & SHF_TLS) == 0
4811 || p->p_type == PT_TLS))))
4813 bfd_vma p_start = p->p_paddr;
4814 bfd_vma p_end = p_start + p->p_memsz;
4815 bfd_vma s_start = sec->lma;
4816 bfd_vma adjust = s_start - p_end;
4820 || p_end < p_start))
4822 (*_bfd_error_handler)
4823 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4824 (unsigned long) s_start, (unsigned long) p_end);
4828 p->p_memsz += adjust;
4830 if (this_hdr->sh_type != SHT_NOBITS)
4832 if (p->p_filesz + adjust < p->p_memsz)
4834 /* We have a PROGBITS section following NOBITS ones.
4835 Allocate file space for the NOBITS section(s) and
4837 adjust = p->p_memsz - p->p_filesz;
4838 if (!write_zeros (abfd, off, adjust))
4842 p->p_filesz += adjust;
4846 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4848 /* The section at i == 0 is the one that actually contains
4852 this_hdr->sh_offset = sec->filepos = off;
4853 off += this_hdr->sh_size;
4854 p->p_filesz = this_hdr->sh_size;
4860 /* The rest are fake sections that shouldn't be written. */
4869 if (p->p_type == PT_LOAD)
4871 this_hdr->sh_offset = sec->filepos = off;
4872 if (this_hdr->sh_type != SHT_NOBITS)
4873 off += this_hdr->sh_size;
4875 else if (this_hdr->sh_type == SHT_NOBITS
4876 && (this_hdr->sh_flags & SHF_TLS) != 0
4877 && this_hdr->sh_offset == 0)
4879 /* This is a .tbss section that didn't get a PT_LOAD.
4880 (See _bfd_elf_map_sections_to_segments "Create a
4881 final PT_LOAD".) Set sh_offset to the value it
4882 would have if we had created a zero p_filesz and
4883 p_memsz PT_LOAD header for the section. This
4884 also makes the PT_TLS header have the same
4886 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
4888 this_hdr->sh_offset = sec->filepos = off + adjust;
4891 if (this_hdr->sh_type != SHT_NOBITS)
4893 p->p_filesz += this_hdr->sh_size;
4894 /* A load section without SHF_ALLOC is something like
4895 a note section in a PT_NOTE segment. These take
4896 file space but are not loaded into memory. */
4897 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4898 p->p_memsz += this_hdr->sh_size;
4900 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4902 if (p->p_type == PT_TLS)
4903 p->p_memsz += this_hdr->sh_size;
4905 /* .tbss is special. It doesn't contribute to p_memsz of
4907 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4908 p->p_memsz += this_hdr->sh_size;
4911 if (align > p->p_align
4912 && !m->p_align_valid
4913 && (p->p_type != PT_LOAD
4914 || (abfd->flags & D_PAGED) == 0))
4918 if (!m->p_flags_valid)
4921 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4923 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4930 /* Check that all sections are in a PT_LOAD segment.
4931 Don't check funky gdb generated core files. */
4932 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4934 bfd_boolean check_vma = TRUE;
4936 for (i = 1; i < m->count; i++)
4937 if (m->sections[i]->vma == m->sections[i - 1]->vma
4938 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4939 ->this_hdr), p) != 0
4940 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4941 ->this_hdr), p) != 0)
4943 /* Looks like we have overlays packed into the segment. */
4948 for (i = 0; i < m->count; i++)
4950 Elf_Internal_Shdr *this_hdr;
4953 sec = m->sections[i];
4954 this_hdr = &(elf_section_data(sec)->this_hdr);
4955 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
4956 && !ELF_TBSS_SPECIAL (this_hdr, p))
4958 (*_bfd_error_handler)
4959 (_("%B: section `%A' can't be allocated in segment %d"),
4961 print_segment_map (m);
4967 elf_next_file_pos (abfd) = off;
4971 /* Assign file positions for the other sections. */
4974 assign_file_positions_for_non_load_sections (bfd *abfd,
4975 struct bfd_link_info *link_info)
4977 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4978 Elf_Internal_Shdr **i_shdrpp;
4979 Elf_Internal_Shdr **hdrpp;
4980 Elf_Internal_Phdr *phdrs;
4981 Elf_Internal_Phdr *p;
4982 struct elf_segment_map *m;
4983 struct elf_segment_map *hdrs_segment;
4984 bfd_vma filehdr_vaddr, filehdr_paddr;
4985 bfd_vma phdrs_vaddr, phdrs_paddr;
4987 unsigned int num_sec;
4991 i_shdrpp = elf_elfsections (abfd);
4992 num_sec = elf_numsections (abfd);
4993 off = elf_next_file_pos (abfd);
4994 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4996 Elf_Internal_Shdr *hdr;
4999 if (hdr->bfd_section != NULL
5000 && (hdr->bfd_section->filepos != 0
5001 || (hdr->sh_type == SHT_NOBITS
5002 && hdr->contents == NULL)))
5003 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5004 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5006 if (hdr->sh_size != 0)
5007 (*_bfd_error_handler)
5008 (_("%B: warning: allocated section `%s' not in segment"),
5010 (hdr->bfd_section == NULL
5012 : hdr->bfd_section->name));
5013 /* We don't need to page align empty sections. */
5014 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5015 off += vma_page_aligned_bias (hdr->sh_addr, off,
5018 off += vma_page_aligned_bias (hdr->sh_addr, off,
5020 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5023 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5024 && hdr->bfd_section == NULL)
5025 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5026 || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
5027 || hdr == i_shdrpp[elf_strtab_sec (abfd)])
5028 hdr->sh_offset = -1;
5030 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5033 /* Now that we have set the section file positions, we can set up
5034 the file positions for the non PT_LOAD segments. */
5038 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5040 hdrs_segment = NULL;
5041 phdrs = elf_tdata (abfd)->phdr;
5042 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5045 if (p->p_type != PT_LOAD)
5048 if (m->includes_filehdr)
5050 filehdr_vaddr = p->p_vaddr;
5051 filehdr_paddr = p->p_paddr;
5053 if (m->includes_phdrs)
5055 phdrs_vaddr = p->p_vaddr;
5056 phdrs_paddr = p->p_paddr;
5057 if (m->includes_filehdr)
5060 phdrs_vaddr += bed->s->sizeof_ehdr;
5061 phdrs_paddr += bed->s->sizeof_ehdr;
5066 if (hdrs_segment != NULL && link_info != NULL)
5068 /* There is a segment that contains both the file headers and the
5069 program headers, so provide a symbol __ehdr_start pointing there.
5070 A program can use this to examine itself robustly. */
5072 struct elf_link_hash_entry *hash
5073 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5074 FALSE, FALSE, TRUE);
5075 /* If the symbol was referenced and not defined, define it. */
5077 && (hash->root.type == bfd_link_hash_new
5078 || hash->root.type == bfd_link_hash_undefined
5079 || hash->root.type == bfd_link_hash_undefweak
5080 || hash->root.type == bfd_link_hash_common))
5083 if (hdrs_segment->count != 0)
5084 /* The segment contains sections, so use the first one. */
5085 s = hdrs_segment->sections[0];
5087 /* Use the first (i.e. lowest-addressed) section in any segment. */
5088 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5097 hash->root.u.def.value = filehdr_vaddr - s->vma;
5098 hash->root.u.def.section = s;
5102 hash->root.u.def.value = filehdr_vaddr;
5103 hash->root.u.def.section = bfd_abs_section_ptr;
5106 hash->root.type = bfd_link_hash_defined;
5107 hash->def_regular = 1;
5112 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5114 if (p->p_type == PT_GNU_RELRO)
5116 const Elf_Internal_Phdr *lp;
5117 struct elf_segment_map *lm;
5119 if (link_info != NULL)
5121 /* During linking the range of the RELRO segment is passed
5123 for (lm = elf_seg_map (abfd), lp = phdrs;
5125 lm = lm->next, lp++)
5127 if (lp->p_type == PT_LOAD
5128 && lp->p_vaddr < link_info->relro_end
5130 && lm->sections[0]->vma >= link_info->relro_start)
5134 BFD_ASSERT (lm != NULL);
5138 /* Otherwise we are copying an executable or shared
5139 library, but we need to use the same linker logic. */
5140 for (lp = phdrs; lp < phdrs + count; ++lp)
5142 if (lp->p_type == PT_LOAD
5143 && lp->p_paddr == p->p_paddr)
5148 if (lp < phdrs + count)
5150 p->p_vaddr = lp->p_vaddr;
5151 p->p_paddr = lp->p_paddr;
5152 p->p_offset = lp->p_offset;
5153 if (link_info != NULL)
5154 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5155 else if (m->p_size_valid)
5156 p->p_filesz = m->p_size;
5159 p->p_memsz = p->p_filesz;
5160 /* Preserve the alignment and flags if they are valid. The
5161 gold linker generates RW/4 for the PT_GNU_RELRO section.
5162 It is better for objcopy/strip to honor these attributes
5163 otherwise gdb will choke when using separate debug files.
5165 if (!m->p_align_valid)
5167 if (!m->p_flags_valid)
5168 p->p_flags = (lp->p_flags & ~PF_W);
5172 memset (p, 0, sizeof *p);
5173 p->p_type = PT_NULL;
5176 else if (p->p_type == PT_GNU_STACK)
5178 if (m->p_size_valid)
5179 p->p_memsz = m->p_size;
5181 else if (m->count != 0)
5183 if (p->p_type != PT_LOAD
5184 && (p->p_type != PT_NOTE
5185 || bfd_get_format (abfd) != bfd_core))
5187 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
5190 p->p_offset = m->sections[0]->filepos;
5191 for (i = m->count; i-- != 0;)
5193 asection *sect = m->sections[i];
5194 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5195 if (hdr->sh_type != SHT_NOBITS)
5197 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5204 else if (m->includes_filehdr)
5206 p->p_vaddr = filehdr_vaddr;
5207 if (! m->p_paddr_valid)
5208 p->p_paddr = filehdr_paddr;
5210 else if (m->includes_phdrs)
5212 p->p_vaddr = phdrs_vaddr;
5213 if (! m->p_paddr_valid)
5214 p->p_paddr = phdrs_paddr;
5218 elf_next_file_pos (abfd) = off;
5223 /* Work out the file positions of all the sections. This is called by
5224 _bfd_elf_compute_section_file_positions. All the section sizes and
5225 VMAs must be known before this is called.
5227 Reloc sections come in two flavours: Those processed specially as
5228 "side-channel" data attached to a section to which they apply, and
5229 those that bfd doesn't process as relocations. The latter sort are
5230 stored in a normal bfd section by bfd_section_from_shdr. We don't
5231 consider the former sort here, unless they form part of the loadable
5232 image. Reloc sections not assigned here will be handled later by
5233 assign_file_positions_for_relocs.
5235 We also don't set the positions of the .symtab and .strtab here. */
5238 assign_file_positions_except_relocs (bfd *abfd,
5239 struct bfd_link_info *link_info)
5241 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5242 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5243 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5245 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5246 && bfd_get_format (abfd) != bfd_core)
5248 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5249 unsigned int num_sec = elf_numsections (abfd);
5250 Elf_Internal_Shdr **hdrpp;
5254 /* Start after the ELF header. */
5255 off = i_ehdrp->e_ehsize;
5257 /* We are not creating an executable, which means that we are
5258 not creating a program header, and that the actual order of
5259 the sections in the file is unimportant. */
5260 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5262 Elf_Internal_Shdr *hdr;
5265 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5266 && hdr->bfd_section == NULL)
5267 || i == elf_onesymtab (abfd)
5268 || i == elf_symtab_shndx (abfd)
5269 || i == elf_strtab_sec (abfd))
5271 hdr->sh_offset = -1;
5274 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5277 elf_next_file_pos (abfd) = off;
5283 /* Assign file positions for the loaded sections based on the
5284 assignment of sections to segments. */
5285 if (!assign_file_positions_for_load_sections (abfd, link_info))
5288 /* And for non-load sections. */
5289 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5292 if (bed->elf_backend_modify_program_headers != NULL)
5294 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5298 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5299 if (link_info != NULL
5300 && link_info->executable
5301 && link_info->shared)
5303 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
5304 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
5305 Elf_Internal_Phdr *end_segment = &segment[num_segments];
5307 /* Find the lowest p_vaddr in PT_LOAD segments. */
5308 bfd_vma p_vaddr = (bfd_vma) -1;
5309 for (; segment < end_segment; segment++)
5310 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
5311 p_vaddr = segment->p_vaddr;
5313 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5314 segments is non-zero. */
5316 i_ehdrp->e_type = ET_EXEC;
5319 /* Write out the program headers. */
5320 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5321 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5322 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5330 prep_headers (bfd *abfd)
5332 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5333 struct elf_strtab_hash *shstrtab;
5334 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5336 i_ehdrp = elf_elfheader (abfd);
5338 shstrtab = _bfd_elf_strtab_init ();
5339 if (shstrtab == NULL)
5342 elf_shstrtab (abfd) = shstrtab;
5344 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5345 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5346 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5347 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5349 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5350 i_ehdrp->e_ident[EI_DATA] =
5351 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5352 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5354 if ((abfd->flags & DYNAMIC) != 0)
5355 i_ehdrp->e_type = ET_DYN;
5356 else if ((abfd->flags & EXEC_P) != 0)
5357 i_ehdrp->e_type = ET_EXEC;
5358 else if (bfd_get_format (abfd) == bfd_core)
5359 i_ehdrp->e_type = ET_CORE;
5361 i_ehdrp->e_type = ET_REL;
5363 switch (bfd_get_arch (abfd))
5365 case bfd_arch_unknown:
5366 i_ehdrp->e_machine = EM_NONE;
5369 /* There used to be a long list of cases here, each one setting
5370 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5371 in the corresponding bfd definition. To avoid duplication,
5372 the switch was removed. Machines that need special handling
5373 can generally do it in elf_backend_final_write_processing(),
5374 unless they need the information earlier than the final write.
5375 Such need can generally be supplied by replacing the tests for
5376 e_machine with the conditions used to determine it. */
5378 i_ehdrp->e_machine = bed->elf_machine_code;
5381 i_ehdrp->e_version = bed->s->ev_current;
5382 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5384 /* No program header, for now. */
5385 i_ehdrp->e_phoff = 0;
5386 i_ehdrp->e_phentsize = 0;
5387 i_ehdrp->e_phnum = 0;
5389 /* Each bfd section is section header entry. */
5390 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5391 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5393 /* If we're building an executable, we'll need a program header table. */
5394 if (abfd->flags & EXEC_P)
5395 /* It all happens later. */
5399 i_ehdrp->e_phentsize = 0;
5400 i_ehdrp->e_phoff = 0;
5403 elf_tdata (abfd)->symtab_hdr.sh_name =
5404 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5405 elf_tdata (abfd)->strtab_hdr.sh_name =
5406 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5407 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5408 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5409 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5410 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
5411 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5417 /* Assign file positions for all the reloc sections which are not part
5418 of the loadable file image, and the file position of section headers. */
5421 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5424 unsigned int i, num_sec;
5425 Elf_Internal_Shdr **shdrpp;
5426 Elf_Internal_Ehdr *i_ehdrp;
5427 const struct elf_backend_data *bed;
5429 off = elf_next_file_pos (abfd);
5431 num_sec = elf_numsections (abfd);
5432 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5434 Elf_Internal_Shdr *shdrp;
5437 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5438 && shdrp->sh_offset == -1)
5439 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5442 /* Place the section headers. */
5443 i_ehdrp = elf_elfheader (abfd);
5444 bed = get_elf_backend_data (abfd);
5445 off = align_file_position (off, 1 << bed->s->log_file_align);
5446 i_ehdrp->e_shoff = off;
5447 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5448 elf_next_file_pos (abfd) = off;
5452 _bfd_elf_write_object_contents (bfd *abfd)
5454 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5455 Elf_Internal_Shdr **i_shdrp;
5457 unsigned int count, num_sec;
5458 struct elf_obj_tdata *t;
5460 if (! abfd->output_has_begun
5461 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5464 i_shdrp = elf_elfsections (abfd);
5467 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5471 _bfd_elf_assign_file_positions_for_relocs (abfd);
5473 /* After writing the headers, we need to write the sections too... */
5474 num_sec = elf_numsections (abfd);
5475 for (count = 1; count < num_sec; count++)
5477 if (bed->elf_backend_section_processing)
5478 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5479 if (i_shdrp[count]->contents)
5481 bfd_size_type amt = i_shdrp[count]->sh_size;
5483 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5484 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5489 /* Write out the section header names. */
5490 t = elf_tdata (abfd);
5491 if (elf_shstrtab (abfd) != NULL
5492 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5493 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5496 if (bed->elf_backend_final_write_processing)
5497 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5499 if (!bed->s->write_shdrs_and_ehdr (abfd))
5502 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5503 if (t->o->build_id.after_write_object_contents != NULL)
5504 return (*t->o->build_id.after_write_object_contents) (abfd);
5510 _bfd_elf_write_corefile_contents (bfd *abfd)
5512 /* Hopefully this can be done just like an object file. */
5513 return _bfd_elf_write_object_contents (abfd);
5516 /* Given a section, search the header to find them. */
5519 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5521 const struct elf_backend_data *bed;
5522 unsigned int sec_index;
5524 if (elf_section_data (asect) != NULL
5525 && elf_section_data (asect)->this_idx != 0)
5526 return elf_section_data (asect)->this_idx;
5528 if (bfd_is_abs_section (asect))
5529 sec_index = SHN_ABS;
5530 else if (bfd_is_com_section (asect))
5531 sec_index = SHN_COMMON;
5532 else if (bfd_is_und_section (asect))
5533 sec_index = SHN_UNDEF;
5535 sec_index = SHN_BAD;
5537 bed = get_elf_backend_data (abfd);
5538 if (bed->elf_backend_section_from_bfd_section)
5540 int retval = sec_index;
5542 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5546 if (sec_index == SHN_BAD)
5547 bfd_set_error (bfd_error_nonrepresentable_section);
5552 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5556 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5558 asymbol *asym_ptr = *asym_ptr_ptr;
5560 flagword flags = asym_ptr->flags;
5562 /* When gas creates relocations against local labels, it creates its
5563 own symbol for the section, but does put the symbol into the
5564 symbol chain, so udata is 0. When the linker is generating
5565 relocatable output, this section symbol may be for one of the
5566 input sections rather than the output section. */
5567 if (asym_ptr->udata.i == 0
5568 && (flags & BSF_SECTION_SYM)
5569 && asym_ptr->section)
5574 sec = asym_ptr->section;
5575 if (sec->owner != abfd && sec->output_section != NULL)
5576 sec = sec->output_section;
5577 if (sec->owner == abfd
5578 && (indx = sec->index) < elf_num_section_syms (abfd)
5579 && elf_section_syms (abfd)[indx] != NULL)
5580 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5583 idx = asym_ptr->udata.i;
5587 /* This case can occur when using --strip-symbol on a symbol
5588 which is used in a relocation entry. */
5589 (*_bfd_error_handler)
5590 (_("%B: symbol `%s' required but not present"),
5591 abfd, bfd_asymbol_name (asym_ptr));
5592 bfd_set_error (bfd_error_no_symbols);
5599 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5600 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5608 /* Rewrite program header information. */
5611 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5613 Elf_Internal_Ehdr *iehdr;
5614 struct elf_segment_map *map;
5615 struct elf_segment_map *map_first;
5616 struct elf_segment_map **pointer_to_map;
5617 Elf_Internal_Phdr *segment;
5620 unsigned int num_segments;
5621 bfd_boolean phdr_included = FALSE;
5622 bfd_boolean p_paddr_valid;
5623 bfd_vma maxpagesize;
5624 struct elf_segment_map *phdr_adjust_seg = NULL;
5625 unsigned int phdr_adjust_num = 0;
5626 const struct elf_backend_data *bed;
5628 bed = get_elf_backend_data (ibfd);
5629 iehdr = elf_elfheader (ibfd);
5632 pointer_to_map = &map_first;
5634 num_segments = elf_elfheader (ibfd)->e_phnum;
5635 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5637 /* Returns the end address of the segment + 1. */
5638 #define SEGMENT_END(segment, start) \
5639 (start + (segment->p_memsz > segment->p_filesz \
5640 ? segment->p_memsz : segment->p_filesz))
5642 #define SECTION_SIZE(section, segment) \
5643 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5644 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5645 ? section->size : 0)
5647 /* Returns TRUE if the given section is contained within
5648 the given segment. VMA addresses are compared. */
5649 #define IS_CONTAINED_BY_VMA(section, segment) \
5650 (section->vma >= segment->p_vaddr \
5651 && (section->vma + SECTION_SIZE (section, segment) \
5652 <= (SEGMENT_END (segment, segment->p_vaddr))))
5654 /* Returns TRUE if the given section is contained within
5655 the given segment. LMA addresses are compared. */
5656 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5657 (section->lma >= base \
5658 && (section->lma + SECTION_SIZE (section, segment) \
5659 <= SEGMENT_END (segment, base)))
5661 /* Handle PT_NOTE segment. */
5662 #define IS_NOTE(p, s) \
5663 (p->p_type == PT_NOTE \
5664 && elf_section_type (s) == SHT_NOTE \
5665 && (bfd_vma) s->filepos >= p->p_offset \
5666 && ((bfd_vma) s->filepos + s->size \
5667 <= p->p_offset + p->p_filesz))
5669 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5671 #define IS_COREFILE_NOTE(p, s) \
5673 && bfd_get_format (ibfd) == bfd_core \
5677 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5678 linker, which generates a PT_INTERP section with p_vaddr and
5679 p_memsz set to 0. */
5680 #define IS_SOLARIS_PT_INTERP(p, s) \
5682 && p->p_paddr == 0 \
5683 && p->p_memsz == 0 \
5684 && p->p_filesz > 0 \
5685 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5687 && (bfd_vma) s->filepos >= p->p_offset \
5688 && ((bfd_vma) s->filepos + s->size \
5689 <= p->p_offset + p->p_filesz))
5691 /* Decide if the given section should be included in the given segment.
5692 A section will be included if:
5693 1. It is within the address space of the segment -- we use the LMA
5694 if that is set for the segment and the VMA otherwise,
5695 2. It is an allocated section or a NOTE section in a PT_NOTE
5697 3. There is an output section associated with it,
5698 4. The section has not already been allocated to a previous segment.
5699 5. PT_GNU_STACK segments do not include any sections.
5700 6. PT_TLS segment includes only SHF_TLS sections.
5701 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5702 8. PT_DYNAMIC should not contain empty sections at the beginning
5703 (with the possible exception of .dynamic). */
5704 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5705 ((((segment->p_paddr \
5706 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5707 : IS_CONTAINED_BY_VMA (section, segment)) \
5708 && (section->flags & SEC_ALLOC) != 0) \
5709 || IS_NOTE (segment, section)) \
5710 && segment->p_type != PT_GNU_STACK \
5711 && (segment->p_type != PT_TLS \
5712 || (section->flags & SEC_THREAD_LOCAL)) \
5713 && (segment->p_type == PT_LOAD \
5714 || segment->p_type == PT_TLS \
5715 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5716 && (segment->p_type != PT_DYNAMIC \
5717 || SECTION_SIZE (section, segment) > 0 \
5718 || (segment->p_paddr \
5719 ? segment->p_paddr != section->lma \
5720 : segment->p_vaddr != section->vma) \
5721 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5723 && !section->segment_mark)
5725 /* If the output section of a section in the input segment is NULL,
5726 it is removed from the corresponding output segment. */
5727 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5728 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5729 && section->output_section != NULL)
5731 /* Returns TRUE iff seg1 starts after the end of seg2. */
5732 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5733 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5735 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5736 their VMA address ranges and their LMA address ranges overlap.
5737 It is possible to have overlapping VMA ranges without overlapping LMA
5738 ranges. RedBoot images for example can have both .data and .bss mapped
5739 to the same VMA range, but with the .data section mapped to a different
5741 #define SEGMENT_OVERLAPS(seg1, seg2) \
5742 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5743 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5744 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5745 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5747 /* Initialise the segment mark field. */
5748 for (section = ibfd->sections; section != NULL; section = section->next)
5749 section->segment_mark = FALSE;
5751 /* The Solaris linker creates program headers in which all the
5752 p_paddr fields are zero. When we try to objcopy or strip such a
5753 file, we get confused. Check for this case, and if we find it
5754 don't set the p_paddr_valid fields. */
5755 p_paddr_valid = FALSE;
5756 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5759 if (segment->p_paddr != 0)
5761 p_paddr_valid = TRUE;
5765 /* Scan through the segments specified in the program header
5766 of the input BFD. For this first scan we look for overlaps
5767 in the loadable segments. These can be created by weird
5768 parameters to objcopy. Also, fix some solaris weirdness. */
5769 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5774 Elf_Internal_Phdr *segment2;
5776 if (segment->p_type == PT_INTERP)
5777 for (section = ibfd->sections; section; section = section->next)
5778 if (IS_SOLARIS_PT_INTERP (segment, section))
5780 /* Mininal change so that the normal section to segment
5781 assignment code will work. */
5782 segment->p_vaddr = section->vma;
5786 if (segment->p_type != PT_LOAD)
5788 /* Remove PT_GNU_RELRO segment. */
5789 if (segment->p_type == PT_GNU_RELRO)
5790 segment->p_type = PT_NULL;
5794 /* Determine if this segment overlaps any previous segments. */
5795 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5797 bfd_signed_vma extra_length;
5799 if (segment2->p_type != PT_LOAD
5800 || !SEGMENT_OVERLAPS (segment, segment2))
5803 /* Merge the two segments together. */
5804 if (segment2->p_vaddr < segment->p_vaddr)
5806 /* Extend SEGMENT2 to include SEGMENT and then delete
5808 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5809 - SEGMENT_END (segment2, segment2->p_vaddr));
5811 if (extra_length > 0)
5813 segment2->p_memsz += extra_length;
5814 segment2->p_filesz += extra_length;
5817 segment->p_type = PT_NULL;
5819 /* Since we have deleted P we must restart the outer loop. */
5821 segment = elf_tdata (ibfd)->phdr;
5826 /* Extend SEGMENT to include SEGMENT2 and then delete
5828 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5829 - SEGMENT_END (segment, segment->p_vaddr));
5831 if (extra_length > 0)
5833 segment->p_memsz += extra_length;
5834 segment->p_filesz += extra_length;
5837 segment2->p_type = PT_NULL;
5842 /* The second scan attempts to assign sections to segments. */
5843 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5847 unsigned int section_count;
5848 asection **sections;
5849 asection *output_section;
5851 bfd_vma matching_lma;
5852 bfd_vma suggested_lma;
5855 asection *first_section;
5856 bfd_boolean first_matching_lma;
5857 bfd_boolean first_suggested_lma;
5859 if (segment->p_type == PT_NULL)
5862 first_section = NULL;
5863 /* Compute how many sections might be placed into this segment. */
5864 for (section = ibfd->sections, section_count = 0;
5866 section = section->next)
5868 /* Find the first section in the input segment, which may be
5869 removed from the corresponding output segment. */
5870 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5872 if (first_section == NULL)
5873 first_section = section;
5874 if (section->output_section != NULL)
5879 /* Allocate a segment map big enough to contain
5880 all of the sections we have selected. */
5881 amt = sizeof (struct elf_segment_map);
5882 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5883 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5887 /* Initialise the fields of the segment map. Default to
5888 using the physical address of the segment in the input BFD. */
5890 map->p_type = segment->p_type;
5891 map->p_flags = segment->p_flags;
5892 map->p_flags_valid = 1;
5894 /* If the first section in the input segment is removed, there is
5895 no need to preserve segment physical address in the corresponding
5897 if (!first_section || first_section->output_section != NULL)
5899 map->p_paddr = segment->p_paddr;
5900 map->p_paddr_valid = p_paddr_valid;
5903 /* Determine if this segment contains the ELF file header
5904 and if it contains the program headers themselves. */
5905 map->includes_filehdr = (segment->p_offset == 0
5906 && segment->p_filesz >= iehdr->e_ehsize);
5907 map->includes_phdrs = 0;
5909 if (!phdr_included || segment->p_type != PT_LOAD)
5911 map->includes_phdrs =
5912 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5913 && (segment->p_offset + segment->p_filesz
5914 >= ((bfd_vma) iehdr->e_phoff
5915 + iehdr->e_phnum * iehdr->e_phentsize)));
5917 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5918 phdr_included = TRUE;
5921 if (section_count == 0)
5923 /* Special segments, such as the PT_PHDR segment, may contain
5924 no sections, but ordinary, loadable segments should contain
5925 something. They are allowed by the ELF spec however, so only
5926 a warning is produced. */
5927 if (segment->p_type == PT_LOAD)
5928 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5929 " detected, is this intentional ?\n"),
5933 *pointer_to_map = map;
5934 pointer_to_map = &map->next;
5939 /* Now scan the sections in the input BFD again and attempt
5940 to add their corresponding output sections to the segment map.
5941 The problem here is how to handle an output section which has
5942 been moved (ie had its LMA changed). There are four possibilities:
5944 1. None of the sections have been moved.
5945 In this case we can continue to use the segment LMA from the
5948 2. All of the sections have been moved by the same amount.
5949 In this case we can change the segment's LMA to match the LMA
5950 of the first section.
5952 3. Some of the sections have been moved, others have not.
5953 In this case those sections which have not been moved can be
5954 placed in the current segment which will have to have its size,
5955 and possibly its LMA changed, and a new segment or segments will
5956 have to be created to contain the other sections.
5958 4. The sections have been moved, but not by the same amount.
5959 In this case we can change the segment's LMA to match the LMA
5960 of the first section and we will have to create a new segment
5961 or segments to contain the other sections.
5963 In order to save time, we allocate an array to hold the section
5964 pointers that we are interested in. As these sections get assigned
5965 to a segment, they are removed from this array. */
5967 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5968 if (sections == NULL)
5971 /* Step One: Scan for segment vs section LMA conflicts.
5972 Also add the sections to the section array allocated above.
5973 Also add the sections to the current segment. In the common
5974 case, where the sections have not been moved, this means that
5975 we have completely filled the segment, and there is nothing
5980 first_matching_lma = TRUE;
5981 first_suggested_lma = TRUE;
5983 for (section = ibfd->sections;
5985 section = section->next)
5986 if (section == first_section)
5989 for (j = 0; section != NULL; section = section->next)
5991 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5993 output_section = section->output_section;
5995 sections[j++] = section;
5997 /* The Solaris native linker always sets p_paddr to 0.
5998 We try to catch that case here, and set it to the
5999 correct value. Note - some backends require that
6000 p_paddr be left as zero. */
6002 && segment->p_vaddr != 0
6003 && !bed->want_p_paddr_set_to_zero
6005 && output_section->lma != 0
6006 && output_section->vma == (segment->p_vaddr
6007 + (map->includes_filehdr
6010 + (map->includes_phdrs
6012 * iehdr->e_phentsize)
6014 map->p_paddr = segment->p_vaddr;
6016 /* Match up the physical address of the segment with the
6017 LMA address of the output section. */
6018 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6019 || IS_COREFILE_NOTE (segment, section)
6020 || (bed->want_p_paddr_set_to_zero
6021 && IS_CONTAINED_BY_VMA (output_section, segment)))
6023 if (first_matching_lma || output_section->lma < matching_lma)
6025 matching_lma = output_section->lma;
6026 first_matching_lma = FALSE;
6029 /* We assume that if the section fits within the segment
6030 then it does not overlap any other section within that
6032 map->sections[isec++] = output_section;
6034 else if (first_suggested_lma)
6036 suggested_lma = output_section->lma;
6037 first_suggested_lma = FALSE;
6040 if (j == section_count)
6045 BFD_ASSERT (j == section_count);
6047 /* Step Two: Adjust the physical address of the current segment,
6049 if (isec == section_count)
6051 /* All of the sections fitted within the segment as currently
6052 specified. This is the default case. Add the segment to
6053 the list of built segments and carry on to process the next
6054 program header in the input BFD. */
6055 map->count = section_count;
6056 *pointer_to_map = map;
6057 pointer_to_map = &map->next;
6060 && !bed->want_p_paddr_set_to_zero
6061 && matching_lma != map->p_paddr
6062 && !map->includes_filehdr
6063 && !map->includes_phdrs)
6064 /* There is some padding before the first section in the
6065 segment. So, we must account for that in the output
6067 map->p_vaddr_offset = matching_lma - map->p_paddr;
6074 if (!first_matching_lma)
6076 /* At least one section fits inside the current segment.
6077 Keep it, but modify its physical address to match the
6078 LMA of the first section that fitted. */
6079 map->p_paddr = matching_lma;
6083 /* None of the sections fitted inside the current segment.
6084 Change the current segment's physical address to match
6085 the LMA of the first section. */
6086 map->p_paddr = suggested_lma;
6089 /* Offset the segment physical address from the lma
6090 to allow for space taken up by elf headers. */
6091 if (map->includes_filehdr)
6093 if (map->p_paddr >= iehdr->e_ehsize)
6094 map->p_paddr -= iehdr->e_ehsize;
6097 map->includes_filehdr = FALSE;
6098 map->includes_phdrs = FALSE;
6102 if (map->includes_phdrs)
6104 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6106 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6108 /* iehdr->e_phnum is just an estimate of the number
6109 of program headers that we will need. Make a note
6110 here of the number we used and the segment we chose
6111 to hold these headers, so that we can adjust the
6112 offset when we know the correct value. */
6113 phdr_adjust_num = iehdr->e_phnum;
6114 phdr_adjust_seg = map;
6117 map->includes_phdrs = FALSE;
6121 /* Step Three: Loop over the sections again, this time assigning
6122 those that fit to the current segment and removing them from the
6123 sections array; but making sure not to leave large gaps. Once all
6124 possible sections have been assigned to the current segment it is
6125 added to the list of built segments and if sections still remain
6126 to be assigned, a new segment is constructed before repeating
6133 first_suggested_lma = TRUE;
6135 /* Fill the current segment with sections that fit. */
6136 for (j = 0; j < section_count; j++)
6138 section = sections[j];
6140 if (section == NULL)
6143 output_section = section->output_section;
6145 BFD_ASSERT (output_section != NULL);
6147 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6148 || IS_COREFILE_NOTE (segment, section))
6150 if (map->count == 0)
6152 /* If the first section in a segment does not start at
6153 the beginning of the segment, then something is
6155 if (output_section->lma
6157 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6158 + (map->includes_phdrs
6159 ? iehdr->e_phnum * iehdr->e_phentsize
6167 prev_sec = map->sections[map->count - 1];
6169 /* If the gap between the end of the previous section
6170 and the start of this section is more than
6171 maxpagesize then we need to start a new segment. */
6172 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6174 < BFD_ALIGN (output_section->lma, maxpagesize))
6175 || (prev_sec->lma + prev_sec->size
6176 > output_section->lma))
6178 if (first_suggested_lma)
6180 suggested_lma = output_section->lma;
6181 first_suggested_lma = FALSE;
6188 map->sections[map->count++] = output_section;
6191 section->segment_mark = TRUE;
6193 else if (first_suggested_lma)
6195 suggested_lma = output_section->lma;
6196 first_suggested_lma = FALSE;
6200 BFD_ASSERT (map->count > 0);
6202 /* Add the current segment to the list of built segments. */
6203 *pointer_to_map = map;
6204 pointer_to_map = &map->next;
6206 if (isec < section_count)
6208 /* We still have not allocated all of the sections to
6209 segments. Create a new segment here, initialise it
6210 and carry on looping. */
6211 amt = sizeof (struct elf_segment_map);
6212 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6213 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6220 /* Initialise the fields of the segment map. Set the physical
6221 physical address to the LMA of the first section that has
6222 not yet been assigned. */
6224 map->p_type = segment->p_type;
6225 map->p_flags = segment->p_flags;
6226 map->p_flags_valid = 1;
6227 map->p_paddr = suggested_lma;
6228 map->p_paddr_valid = p_paddr_valid;
6229 map->includes_filehdr = 0;
6230 map->includes_phdrs = 0;
6233 while (isec < section_count);
6238 elf_seg_map (obfd) = map_first;
6240 /* If we had to estimate the number of program headers that were
6241 going to be needed, then check our estimate now and adjust
6242 the offset if necessary. */
6243 if (phdr_adjust_seg != NULL)
6247 for (count = 0, map = map_first; map != NULL; map = map->next)
6250 if (count > phdr_adjust_num)
6251 phdr_adjust_seg->p_paddr
6252 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6257 #undef IS_CONTAINED_BY_VMA
6258 #undef IS_CONTAINED_BY_LMA
6260 #undef IS_COREFILE_NOTE
6261 #undef IS_SOLARIS_PT_INTERP
6262 #undef IS_SECTION_IN_INPUT_SEGMENT
6263 #undef INCLUDE_SECTION_IN_SEGMENT
6264 #undef SEGMENT_AFTER_SEGMENT
6265 #undef SEGMENT_OVERLAPS
6269 /* Copy ELF program header information. */
6272 copy_elf_program_header (bfd *ibfd, bfd *obfd)
6274 Elf_Internal_Ehdr *iehdr;
6275 struct elf_segment_map *map;
6276 struct elf_segment_map *map_first;
6277 struct elf_segment_map **pointer_to_map;
6278 Elf_Internal_Phdr *segment;
6280 unsigned int num_segments;
6281 bfd_boolean phdr_included = FALSE;
6282 bfd_boolean p_paddr_valid;
6284 iehdr = elf_elfheader (ibfd);
6287 pointer_to_map = &map_first;
6289 /* If all the segment p_paddr fields are zero, don't set
6290 map->p_paddr_valid. */
6291 p_paddr_valid = FALSE;
6292 num_segments = elf_elfheader (ibfd)->e_phnum;
6293 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6296 if (segment->p_paddr != 0)
6298 p_paddr_valid = TRUE;
6302 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6307 unsigned int section_count;
6309 Elf_Internal_Shdr *this_hdr;
6310 asection *first_section = NULL;
6311 asection *lowest_section;
6313 /* Compute how many sections are in this segment. */
6314 for (section = ibfd->sections, section_count = 0;
6316 section = section->next)
6318 this_hdr = &(elf_section_data(section)->this_hdr);
6319 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6321 if (first_section == NULL)
6322 first_section = section;
6327 /* Allocate a segment map big enough to contain
6328 all of the sections we have selected. */
6329 amt = sizeof (struct elf_segment_map);
6330 if (section_count != 0)
6331 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6332 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6336 /* Initialize the fields of the output segment map with the
6339 map->p_type = segment->p_type;
6340 map->p_flags = segment->p_flags;
6341 map->p_flags_valid = 1;
6342 map->p_paddr = segment->p_paddr;
6343 map->p_paddr_valid = p_paddr_valid;
6344 map->p_align = segment->p_align;
6345 map->p_align_valid = 1;
6346 map->p_vaddr_offset = 0;
6348 if (map->p_type == PT_GNU_RELRO
6349 || map->p_type == PT_GNU_STACK)
6351 /* The PT_GNU_RELRO segment may contain the first a few
6352 bytes in the .got.plt section even if the whole .got.plt
6353 section isn't in the PT_GNU_RELRO segment. We won't
6354 change the size of the PT_GNU_RELRO segment.
6355 Similarly, PT_GNU_STACK size is significant on uclinux
6357 map->p_size = segment->p_memsz;
6358 map->p_size_valid = 1;
6361 /* Determine if this segment contains the ELF file header
6362 and if it contains the program headers themselves. */
6363 map->includes_filehdr = (segment->p_offset == 0
6364 && segment->p_filesz >= iehdr->e_ehsize);
6366 map->includes_phdrs = 0;
6367 if (! phdr_included || segment->p_type != PT_LOAD)
6369 map->includes_phdrs =
6370 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6371 && (segment->p_offset + segment->p_filesz
6372 >= ((bfd_vma) iehdr->e_phoff
6373 + iehdr->e_phnum * iehdr->e_phentsize)));
6375 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6376 phdr_included = TRUE;
6379 lowest_section = NULL;
6380 if (section_count != 0)
6382 unsigned int isec = 0;
6384 for (section = first_section;
6386 section = section->next)
6388 this_hdr = &(elf_section_data(section)->this_hdr);
6389 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6391 map->sections[isec++] = section->output_section;
6392 if ((section->flags & SEC_ALLOC) != 0)
6396 if (lowest_section == NULL
6397 || section->lma < lowest_section->lma)
6398 lowest_section = section;
6400 /* Section lmas are set up from PT_LOAD header
6401 p_paddr in _bfd_elf_make_section_from_shdr.
6402 If this header has a p_paddr that disagrees
6403 with the section lma, flag the p_paddr as
6405 if ((section->flags & SEC_LOAD) != 0)
6406 seg_off = this_hdr->sh_offset - segment->p_offset;
6408 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6409 if (section->lma - segment->p_paddr != seg_off)
6410 map->p_paddr_valid = FALSE;
6412 if (isec == section_count)
6418 if (map->includes_filehdr && lowest_section != NULL)
6419 /* We need to keep the space used by the headers fixed. */
6420 map->header_size = lowest_section->vma - segment->p_vaddr;
6422 if (!map->includes_phdrs
6423 && !map->includes_filehdr
6424 && map->p_paddr_valid)
6425 /* There is some other padding before the first section. */
6426 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6427 - segment->p_paddr);
6429 map->count = section_count;
6430 *pointer_to_map = map;
6431 pointer_to_map = &map->next;
6434 elf_seg_map (obfd) = map_first;
6438 /* Copy private BFD data. This copies or rewrites ELF program header
6442 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6444 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6445 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6448 if (elf_tdata (ibfd)->phdr == NULL)
6451 if (ibfd->xvec == obfd->xvec)
6453 /* Check to see if any sections in the input BFD
6454 covered by ELF program header have changed. */
6455 Elf_Internal_Phdr *segment;
6456 asection *section, *osec;
6457 unsigned int i, num_segments;
6458 Elf_Internal_Shdr *this_hdr;
6459 const struct elf_backend_data *bed;
6461 bed = get_elf_backend_data (ibfd);
6463 /* Regenerate the segment map if p_paddr is set to 0. */
6464 if (bed->want_p_paddr_set_to_zero)
6467 /* Initialize the segment mark field. */
6468 for (section = obfd->sections; section != NULL;
6469 section = section->next)
6470 section->segment_mark = FALSE;
6472 num_segments = elf_elfheader (ibfd)->e_phnum;
6473 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6477 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6478 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6479 which severly confuses things, so always regenerate the segment
6480 map in this case. */
6481 if (segment->p_paddr == 0
6482 && segment->p_memsz == 0
6483 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6486 for (section = ibfd->sections;
6487 section != NULL; section = section->next)
6489 /* We mark the output section so that we know it comes
6490 from the input BFD. */
6491 osec = section->output_section;
6493 osec->segment_mark = TRUE;
6495 /* Check if this section is covered by the segment. */
6496 this_hdr = &(elf_section_data(section)->this_hdr);
6497 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6499 /* FIXME: Check if its output section is changed or
6500 removed. What else do we need to check? */
6502 || section->flags != osec->flags
6503 || section->lma != osec->lma
6504 || section->vma != osec->vma
6505 || section->size != osec->size
6506 || section->rawsize != osec->rawsize
6507 || section->alignment_power != osec->alignment_power)
6513 /* Check to see if any output section do not come from the
6515 for (section = obfd->sections; section != NULL;
6516 section = section->next)
6518 if (section->segment_mark == FALSE)
6521 section->segment_mark = FALSE;
6524 return copy_elf_program_header (ibfd, obfd);
6528 if (ibfd->xvec == obfd->xvec)
6530 /* When rewriting program header, set the output maxpagesize to
6531 the maximum alignment of input PT_LOAD segments. */
6532 Elf_Internal_Phdr *segment;
6534 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6535 bfd_vma maxpagesize = 0;
6537 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6540 if (segment->p_type == PT_LOAD
6541 && maxpagesize < segment->p_align)
6542 maxpagesize = segment->p_align;
6544 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6545 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6548 return rewrite_elf_program_header (ibfd, obfd);
6551 /* Initialize private output section information from input section. */
6554 _bfd_elf_init_private_section_data (bfd *ibfd,
6558 struct bfd_link_info *link_info)
6561 Elf_Internal_Shdr *ihdr, *ohdr;
6562 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6564 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6565 || obfd->xvec->flavour != bfd_target_elf_flavour)
6568 BFD_ASSERT (elf_section_data (osec) != NULL);
6570 /* For objcopy and relocatable link, don't copy the output ELF
6571 section type from input if the output BFD section flags have been
6572 set to something different. For a final link allow some flags
6573 that the linker clears to differ. */
6574 if (elf_section_type (osec) == SHT_NULL
6575 && (osec->flags == isec->flags
6577 && ((osec->flags ^ isec->flags)
6578 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6579 elf_section_type (osec) = elf_section_type (isec);
6581 /* FIXME: Is this correct for all OS/PROC specific flags? */
6582 elf_section_flags (osec) |= (elf_section_flags (isec)
6583 & (SHF_MASKOS | SHF_MASKPROC));
6585 /* Set things up for objcopy and relocatable link. The output
6586 SHT_GROUP section will have its elf_next_in_group pointing back
6587 to the input group members. Ignore linker created group section.
6588 See elfNN_ia64_object_p in elfxx-ia64.c. */
6591 if (elf_sec_group (isec) == NULL
6592 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6594 if (elf_section_flags (isec) & SHF_GROUP)
6595 elf_section_flags (osec) |= SHF_GROUP;
6596 elf_next_in_group (osec) = elf_next_in_group (isec);
6597 elf_section_data (osec)->group = elf_section_data (isec)->group;
6601 ihdr = &elf_section_data (isec)->this_hdr;
6603 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6604 don't use the output section of the linked-to section since it
6605 may be NULL at this point. */
6606 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6608 ohdr = &elf_section_data (osec)->this_hdr;
6609 ohdr->sh_flags |= SHF_LINK_ORDER;
6610 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6613 osec->use_rela_p = isec->use_rela_p;
6618 /* Copy private section information. This copies over the entsize
6619 field, and sometimes the info field. */
6622 _bfd_elf_copy_private_section_data (bfd *ibfd,
6627 Elf_Internal_Shdr *ihdr, *ohdr;
6629 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6630 || obfd->xvec->flavour != bfd_target_elf_flavour)
6633 ihdr = &elf_section_data (isec)->this_hdr;
6634 ohdr = &elf_section_data (osec)->this_hdr;
6636 ohdr->sh_entsize = ihdr->sh_entsize;
6638 if (ihdr->sh_type == SHT_SYMTAB
6639 || ihdr->sh_type == SHT_DYNSYM
6640 || ihdr->sh_type == SHT_GNU_verneed
6641 || ihdr->sh_type == SHT_GNU_verdef)
6642 ohdr->sh_info = ihdr->sh_info;
6644 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6648 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6649 necessary if we are removing either the SHT_GROUP section or any of
6650 the group member sections. DISCARDED is the value that a section's
6651 output_section has if the section will be discarded, NULL when this
6652 function is called from objcopy, bfd_abs_section_ptr when called
6656 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6660 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6661 if (elf_section_type (isec) == SHT_GROUP)
6663 asection *first = elf_next_in_group (isec);
6664 asection *s = first;
6665 bfd_size_type removed = 0;
6669 /* If this member section is being output but the
6670 SHT_GROUP section is not, then clear the group info
6671 set up by _bfd_elf_copy_private_section_data. */
6672 if (s->output_section != discarded
6673 && isec->output_section == discarded)
6675 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6676 elf_group_name (s->output_section) = NULL;
6678 /* Conversely, if the member section is not being output
6679 but the SHT_GROUP section is, then adjust its size. */
6680 else if (s->output_section == discarded
6681 && isec->output_section != discarded)
6683 s = elf_next_in_group (s);
6689 if (discarded != NULL)
6691 /* If we've been called for ld -r, then we need to
6692 adjust the input section size. This function may
6693 be called multiple times, so save the original
6695 if (isec->rawsize == 0)
6696 isec->rawsize = isec->size;
6697 isec->size = isec->rawsize - removed;
6701 /* Adjust the output section size when called from
6703 isec->output_section->size -= removed;
6711 /* Copy private header information. */
6714 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6716 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6717 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6720 /* Copy over private BFD data if it has not already been copied.
6721 This must be done here, rather than in the copy_private_bfd_data
6722 entry point, because the latter is called after the section
6723 contents have been set, which means that the program headers have
6724 already been worked out. */
6725 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
6727 if (! copy_private_bfd_data (ibfd, obfd))
6731 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6734 /* Copy private symbol information. If this symbol is in a section
6735 which we did not map into a BFD section, try to map the section
6736 index correctly. We use special macro definitions for the mapped
6737 section indices; these definitions are interpreted by the
6738 swap_out_syms function. */
6740 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6741 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6742 #define MAP_STRTAB (SHN_HIOS + 3)
6743 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6744 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6747 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6752 elf_symbol_type *isym, *osym;
6754 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6755 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6758 isym = elf_symbol_from (ibfd, isymarg);
6759 osym = elf_symbol_from (obfd, osymarg);
6762 && isym->internal_elf_sym.st_shndx != 0
6764 && bfd_is_abs_section (isym->symbol.section))
6768 shndx = isym->internal_elf_sym.st_shndx;
6769 if (shndx == elf_onesymtab (ibfd))
6770 shndx = MAP_ONESYMTAB;
6771 else if (shndx == elf_dynsymtab (ibfd))
6772 shndx = MAP_DYNSYMTAB;
6773 else if (shndx == elf_strtab_sec (ibfd))
6775 else if (shndx == elf_shstrtab_sec (ibfd))
6776 shndx = MAP_SHSTRTAB;
6777 else if (shndx == elf_symtab_shndx (ibfd))
6778 shndx = MAP_SYM_SHNDX;
6779 osym->internal_elf_sym.st_shndx = shndx;
6785 /* Swap out the symbols. */
6788 swap_out_syms (bfd *abfd,
6789 struct bfd_strtab_hash **sttp,
6792 const struct elf_backend_data *bed;
6795 struct bfd_strtab_hash *stt;
6796 Elf_Internal_Shdr *symtab_hdr;
6797 Elf_Internal_Shdr *symtab_shndx_hdr;
6798 Elf_Internal_Shdr *symstrtab_hdr;
6799 bfd_byte *outbound_syms;
6800 bfd_byte *outbound_shndx;
6802 unsigned int num_locals;
6804 bfd_boolean name_local_sections;
6806 if (!elf_map_symbols (abfd, &num_locals))
6809 /* Dump out the symtabs. */
6810 stt = _bfd_elf_stringtab_init ();
6814 bed = get_elf_backend_data (abfd);
6815 symcount = bfd_get_symcount (abfd);
6816 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6817 symtab_hdr->sh_type = SHT_SYMTAB;
6818 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6819 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6820 symtab_hdr->sh_info = num_locals + 1;
6821 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6823 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6824 symstrtab_hdr->sh_type = SHT_STRTAB;
6826 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6827 bed->s->sizeof_sym);
6828 if (outbound_syms == NULL)
6830 _bfd_stringtab_free (stt);
6833 symtab_hdr->contents = outbound_syms;
6835 outbound_shndx = NULL;
6836 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6837 if (symtab_shndx_hdr->sh_name != 0)
6839 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6840 outbound_shndx = (bfd_byte *)
6841 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6842 if (outbound_shndx == NULL)
6844 _bfd_stringtab_free (stt);
6848 symtab_shndx_hdr->contents = outbound_shndx;
6849 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6850 symtab_shndx_hdr->sh_size = amt;
6851 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6852 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6855 /* Now generate the data (for "contents"). */
6857 /* Fill in zeroth symbol and swap it out. */
6858 Elf_Internal_Sym sym;
6864 sym.st_shndx = SHN_UNDEF;
6865 sym.st_target_internal = 0;
6866 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6867 outbound_syms += bed->s->sizeof_sym;
6868 if (outbound_shndx != NULL)
6869 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6873 = (bed->elf_backend_name_local_section_symbols
6874 && bed->elf_backend_name_local_section_symbols (abfd));
6876 syms = bfd_get_outsymbols (abfd);
6877 for (idx = 0; idx < symcount; idx++)
6879 Elf_Internal_Sym sym;
6880 bfd_vma value = syms[idx]->value;
6881 elf_symbol_type *type_ptr;
6882 flagword flags = syms[idx]->flags;
6885 if (!name_local_sections
6886 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6888 /* Local section symbols have no name. */
6893 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6896 if (sym.st_name == (unsigned long) -1)
6898 _bfd_stringtab_free (stt);
6903 type_ptr = elf_symbol_from (abfd, syms[idx]);
6905 if ((flags & BSF_SECTION_SYM) == 0
6906 && bfd_is_com_section (syms[idx]->section))
6908 /* ELF common symbols put the alignment into the `value' field,
6909 and the size into the `size' field. This is backwards from
6910 how BFD handles it, so reverse it here. */
6911 sym.st_size = value;
6912 if (type_ptr == NULL
6913 || type_ptr->internal_elf_sym.st_value == 0)
6914 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6916 sym.st_value = type_ptr->internal_elf_sym.st_value;
6917 sym.st_shndx = _bfd_elf_section_from_bfd_section
6918 (abfd, syms[idx]->section);
6922 asection *sec = syms[idx]->section;
6925 if (sec->output_section)
6927 value += sec->output_offset;
6928 sec = sec->output_section;
6931 /* Don't add in the section vma for relocatable output. */
6932 if (! relocatable_p)
6934 sym.st_value = value;
6935 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6937 if (bfd_is_abs_section (sec)
6939 && type_ptr->internal_elf_sym.st_shndx != 0)
6941 /* This symbol is in a real ELF section which we did
6942 not create as a BFD section. Undo the mapping done
6943 by copy_private_symbol_data. */
6944 shndx = type_ptr->internal_elf_sym.st_shndx;
6948 shndx = elf_onesymtab (abfd);
6951 shndx = elf_dynsymtab (abfd);
6954 shndx = elf_strtab_sec (abfd);
6957 shndx = elf_shstrtab_sec (abfd);
6960 shndx = elf_symtab_shndx (abfd);
6969 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6971 if (shndx == SHN_BAD)
6975 /* Writing this would be a hell of a lot easier if
6976 we had some decent documentation on bfd, and
6977 knew what to expect of the library, and what to
6978 demand of applications. For example, it
6979 appears that `objcopy' might not set the
6980 section of a symbol to be a section that is
6981 actually in the output file. */
6982 sec2 = bfd_get_section_by_name (abfd, sec->name);
6985 _bfd_error_handler (_("\
6986 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6987 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6989 bfd_set_error (bfd_error_invalid_operation);
6990 _bfd_stringtab_free (stt);
6994 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6995 BFD_ASSERT (shndx != SHN_BAD);
6999 sym.st_shndx = shndx;
7002 if ((flags & BSF_THREAD_LOCAL) != 0)
7004 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
7005 type = STT_GNU_IFUNC;
7006 else if ((flags & BSF_FUNCTION) != 0)
7008 else if ((flags & BSF_OBJECT) != 0)
7010 else if ((flags & BSF_RELC) != 0)
7012 else if ((flags & BSF_SRELC) != 0)
7017 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7020 /* Processor-specific types. */
7021 if (type_ptr != NULL
7022 && bed->elf_backend_get_symbol_type)
7023 type = ((*bed->elf_backend_get_symbol_type)
7024 (&type_ptr->internal_elf_sym, type));
7026 if (flags & BSF_SECTION_SYM)
7028 if (flags & BSF_GLOBAL)
7029 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7031 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7033 else if (bfd_is_com_section (syms[idx]->section))
7035 #ifdef USE_STT_COMMON
7036 if (type == STT_OBJECT)
7037 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
7040 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
7042 else if (bfd_is_und_section (syms[idx]->section))
7043 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7047 else if (flags & BSF_FILE)
7048 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7051 int bind = STB_LOCAL;
7053 if (flags & BSF_LOCAL)
7055 else if (flags & BSF_GNU_UNIQUE)
7056 bind = STB_GNU_UNIQUE;
7057 else if (flags & BSF_WEAK)
7059 else if (flags & BSF_GLOBAL)
7062 sym.st_info = ELF_ST_INFO (bind, type);
7065 if (type_ptr != NULL)
7067 sym.st_other = type_ptr->internal_elf_sym.st_other;
7068 sym.st_target_internal
7069 = type_ptr->internal_elf_sym.st_target_internal;
7074 sym.st_target_internal = 0;
7077 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
7078 outbound_syms += bed->s->sizeof_sym;
7079 if (outbound_shndx != NULL)
7080 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
7084 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
7085 symstrtab_hdr->sh_type = SHT_STRTAB;
7087 symstrtab_hdr->sh_flags = 0;
7088 symstrtab_hdr->sh_addr = 0;
7089 symstrtab_hdr->sh_entsize = 0;
7090 symstrtab_hdr->sh_link = 0;
7091 symstrtab_hdr->sh_info = 0;
7092 symstrtab_hdr->sh_addralign = 1;
7097 /* Return the number of bytes required to hold the symtab vector.
7099 Note that we base it on the count plus 1, since we will null terminate
7100 the vector allocated based on this size. However, the ELF symbol table
7101 always has a dummy entry as symbol #0, so it ends up even. */
7104 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
7108 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7110 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7111 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7113 symtab_size -= sizeof (asymbol *);
7119 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7123 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7125 if (elf_dynsymtab (abfd) == 0)
7127 bfd_set_error (bfd_error_invalid_operation);
7131 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7132 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7134 symtab_size -= sizeof (asymbol *);
7140 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7143 return (asect->reloc_count + 1) * sizeof (arelent *);
7146 /* Canonicalize the relocs. */
7149 _bfd_elf_canonicalize_reloc (bfd *abfd,
7156 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7158 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7161 tblptr = section->relocation;
7162 for (i = 0; i < section->reloc_count; i++)
7163 *relptr++ = tblptr++;
7167 return section->reloc_count;
7171 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7173 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7174 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7177 bfd_get_symcount (abfd) = symcount;
7182 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7183 asymbol **allocation)
7185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7186 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7189 bfd_get_dynamic_symcount (abfd) = symcount;
7193 /* Return the size required for the dynamic reloc entries. Any loadable
7194 section that was actually installed in the BFD, and has type SHT_REL
7195 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7196 dynamic reloc section. */
7199 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7204 if (elf_dynsymtab (abfd) == 0)
7206 bfd_set_error (bfd_error_invalid_operation);
7210 ret = sizeof (arelent *);
7211 for (s = abfd->sections; s != NULL; s = s->next)
7212 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7213 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7214 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7215 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7216 * sizeof (arelent *));
7221 /* Canonicalize the dynamic relocation entries. Note that we return the
7222 dynamic relocations as a single block, although they are actually
7223 associated with particular sections; the interface, which was
7224 designed for SunOS style shared libraries, expects that there is only
7225 one set of dynamic relocs. Any loadable section that was actually
7226 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7227 dynamic symbol table, is considered to be a dynamic reloc section. */
7230 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7234 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7238 if (elf_dynsymtab (abfd) == 0)
7240 bfd_set_error (bfd_error_invalid_operation);
7244 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7246 for (s = abfd->sections; s != NULL; s = s->next)
7248 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7249 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7250 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7255 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7257 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7259 for (i = 0; i < count; i++)
7270 /* Read in the version information. */
7273 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7275 bfd_byte *contents = NULL;
7276 unsigned int freeidx = 0;
7278 if (elf_dynverref (abfd) != 0)
7280 Elf_Internal_Shdr *hdr;
7281 Elf_External_Verneed *everneed;
7282 Elf_Internal_Verneed *iverneed;
7284 bfd_byte *contents_end;
7286 hdr = &elf_tdata (abfd)->dynverref_hdr;
7289 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7290 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
7292 elf_tdata (abfd)->verref = NULL;
7294 if (elf_tdata (abfd)->verref == NULL)
7297 elf_tdata (abfd)->cverrefs = hdr->sh_info;
7299 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7300 if (contents == NULL)
7302 error_return_verref:
7303 elf_tdata (abfd)->verref = NULL;
7304 elf_tdata (abfd)->cverrefs = 0;
7307 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7308 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7309 goto error_return_verref;
7311 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
7312 goto error_return_verref;
7314 BFD_ASSERT (sizeof (Elf_External_Verneed)
7315 == sizeof (Elf_External_Vernaux));
7316 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7317 everneed = (Elf_External_Verneed *) contents;
7318 iverneed = elf_tdata (abfd)->verref;
7319 for (i = 0; i < hdr->sh_info; i++, iverneed++)
7321 Elf_External_Vernaux *evernaux;
7322 Elf_Internal_Vernaux *ivernaux;
7325 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7327 iverneed->vn_bfd = abfd;
7329 iverneed->vn_filename =
7330 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7332 if (iverneed->vn_filename == NULL)
7333 goto error_return_verref;
7335 if (iverneed->vn_cnt == 0)
7336 iverneed->vn_auxptr = NULL;
7339 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7340 bfd_alloc2 (abfd, iverneed->vn_cnt,
7341 sizeof (Elf_Internal_Vernaux));
7342 if (iverneed->vn_auxptr == NULL)
7343 goto error_return_verref;
7346 if (iverneed->vn_aux
7347 > (size_t) (contents_end - (bfd_byte *) everneed))
7348 goto error_return_verref;
7350 evernaux = ((Elf_External_Vernaux *)
7351 ((bfd_byte *) everneed + iverneed->vn_aux));
7352 ivernaux = iverneed->vn_auxptr;
7353 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7355 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7357 ivernaux->vna_nodename =
7358 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7359 ivernaux->vna_name);
7360 if (ivernaux->vna_nodename == NULL)
7361 goto error_return_verref;
7363 if (j + 1 < iverneed->vn_cnt)
7364 ivernaux->vna_nextptr = ivernaux + 1;
7366 ivernaux->vna_nextptr = NULL;
7368 if (ivernaux->vna_next
7369 > (size_t) (contents_end - (bfd_byte *) evernaux))
7370 goto error_return_verref;
7372 evernaux = ((Elf_External_Vernaux *)
7373 ((bfd_byte *) evernaux + ivernaux->vna_next));
7375 if (ivernaux->vna_other > freeidx)
7376 freeidx = ivernaux->vna_other;
7379 if (i + 1 < hdr->sh_info)
7380 iverneed->vn_nextref = iverneed + 1;
7382 iverneed->vn_nextref = NULL;
7384 if (iverneed->vn_next
7385 > (size_t) (contents_end - (bfd_byte *) everneed))
7386 goto error_return_verref;
7388 everneed = ((Elf_External_Verneed *)
7389 ((bfd_byte *) everneed + iverneed->vn_next));
7396 if (elf_dynverdef (abfd) != 0)
7398 Elf_Internal_Shdr *hdr;
7399 Elf_External_Verdef *everdef;
7400 Elf_Internal_Verdef *iverdef;
7401 Elf_Internal_Verdef *iverdefarr;
7402 Elf_Internal_Verdef iverdefmem;
7404 unsigned int maxidx;
7405 bfd_byte *contents_end_def, *contents_end_aux;
7407 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7409 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7410 if (contents == NULL)
7412 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7413 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7416 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
7419 BFD_ASSERT (sizeof (Elf_External_Verdef)
7420 >= sizeof (Elf_External_Verdaux));
7421 contents_end_def = contents + hdr->sh_size
7422 - sizeof (Elf_External_Verdef);
7423 contents_end_aux = contents + hdr->sh_size
7424 - sizeof (Elf_External_Verdaux);
7426 /* We know the number of entries in the section but not the maximum
7427 index. Therefore we have to run through all entries and find
7429 everdef = (Elf_External_Verdef *) contents;
7431 for (i = 0; i < hdr->sh_info; ++i)
7433 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7435 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7436 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7438 if (iverdefmem.vd_next
7439 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7442 everdef = ((Elf_External_Verdef *)
7443 ((bfd_byte *) everdef + iverdefmem.vd_next));
7446 if (default_imported_symver)
7448 if (freeidx > maxidx)
7454 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7455 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7457 elf_tdata (abfd)->verdef = NULL;
7459 if (elf_tdata (abfd)->verdef == NULL)
7462 elf_tdata (abfd)->cverdefs = maxidx;
7464 everdef = (Elf_External_Verdef *) contents;
7465 iverdefarr = elf_tdata (abfd)->verdef;
7466 for (i = 0; i < hdr->sh_info; i++)
7468 Elf_External_Verdaux *everdaux;
7469 Elf_Internal_Verdaux *iverdaux;
7472 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7474 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7476 error_return_verdef:
7477 elf_tdata (abfd)->verdef = NULL;
7478 elf_tdata (abfd)->cverdefs = 0;
7482 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7483 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7485 iverdef->vd_bfd = abfd;
7487 if (iverdef->vd_cnt == 0)
7488 iverdef->vd_auxptr = NULL;
7491 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7492 bfd_alloc2 (abfd, iverdef->vd_cnt,
7493 sizeof (Elf_Internal_Verdaux));
7494 if (iverdef->vd_auxptr == NULL)
7495 goto error_return_verdef;
7499 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7500 goto error_return_verdef;
7502 everdaux = ((Elf_External_Verdaux *)
7503 ((bfd_byte *) everdef + iverdef->vd_aux));
7504 iverdaux = iverdef->vd_auxptr;
7505 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7507 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7509 iverdaux->vda_nodename =
7510 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7511 iverdaux->vda_name);
7512 if (iverdaux->vda_nodename == NULL)
7513 goto error_return_verdef;
7515 if (j + 1 < iverdef->vd_cnt)
7516 iverdaux->vda_nextptr = iverdaux + 1;
7518 iverdaux->vda_nextptr = NULL;
7520 if (iverdaux->vda_next
7521 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7522 goto error_return_verdef;
7524 everdaux = ((Elf_External_Verdaux *)
7525 ((bfd_byte *) everdaux + iverdaux->vda_next));
7528 if (iverdef->vd_cnt)
7529 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7531 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7532 iverdef->vd_nextdef = iverdef + 1;
7534 iverdef->vd_nextdef = NULL;
7536 everdef = ((Elf_External_Verdef *)
7537 ((bfd_byte *) everdef + iverdef->vd_next));
7543 else if (default_imported_symver)
7550 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7551 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7552 if (elf_tdata (abfd)->verdef == NULL)
7555 elf_tdata (abfd)->cverdefs = freeidx;
7558 /* Create a default version based on the soname. */
7559 if (default_imported_symver)
7561 Elf_Internal_Verdef *iverdef;
7562 Elf_Internal_Verdaux *iverdaux;
7564 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
7566 iverdef->vd_version = VER_DEF_CURRENT;
7567 iverdef->vd_flags = 0;
7568 iverdef->vd_ndx = freeidx;
7569 iverdef->vd_cnt = 1;
7571 iverdef->vd_bfd = abfd;
7573 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7574 if (iverdef->vd_nodename == NULL)
7575 goto error_return_verdef;
7576 iverdef->vd_nextdef = NULL;
7577 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7578 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7579 if (iverdef->vd_auxptr == NULL)
7580 goto error_return_verdef;
7582 iverdaux = iverdef->vd_auxptr;
7583 iverdaux->vda_nodename = iverdef->vd_nodename;
7584 iverdaux->vda_nextptr = NULL;
7590 if (contents != NULL)
7596 _bfd_elf_make_empty_symbol (bfd *abfd)
7598 elf_symbol_type *newsym;
7600 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
7603 newsym->symbol.the_bfd = abfd;
7604 return &newsym->symbol;
7608 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7612 bfd_symbol_info (symbol, ret);
7615 /* Return whether a symbol name implies a local symbol. Most targets
7616 use this function for the is_local_label_name entry point, but some
7620 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7623 /* Normal local symbols start with ``.L''. */
7624 if (name[0] == '.' && name[1] == 'L')
7627 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7628 DWARF debugging symbols starting with ``..''. */
7629 if (name[0] == '.' && name[1] == '.')
7632 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7633 emitting DWARF debugging output. I suspect this is actually a
7634 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7635 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7636 underscore to be emitted on some ELF targets). For ease of use,
7637 we treat such symbols as local. */
7638 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7645 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7646 asymbol *symbol ATTRIBUTE_UNUSED)
7653 _bfd_elf_set_arch_mach (bfd *abfd,
7654 enum bfd_architecture arch,
7655 unsigned long machine)
7657 /* If this isn't the right architecture for this backend, and this
7658 isn't the generic backend, fail. */
7659 if (arch != get_elf_backend_data (abfd)->arch
7660 && arch != bfd_arch_unknown
7661 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7664 return bfd_default_set_arch_mach (abfd, arch, machine);
7667 /* Find the function to a particular section and offset,
7668 for error reporting. */
7671 elf_find_function (bfd *abfd,
7675 const char **filename_ptr,
7676 const char **functionname_ptr)
7678 struct elf_find_function_cache
7680 asection *last_section;
7682 const char *filename;
7683 bfd_size_type func_size;
7686 if (symbols == NULL)
7689 cache = elf_tdata (abfd)->elf_find_function_cache;
7692 cache = bfd_zalloc (abfd, sizeof (*cache));
7693 elf_tdata (abfd)->elf_find_function_cache = cache;
7697 if (cache->last_section != section
7698 || cache->func == NULL
7699 || offset < cache->func->value
7700 || offset >= cache->func->value + cache->func_size)
7705 /* ??? Given multiple file symbols, it is impossible to reliably
7706 choose the right file name for global symbols. File symbols are
7707 local symbols, and thus all file symbols must sort before any
7708 global symbols. The ELF spec may be interpreted to say that a
7709 file symbol must sort before other local symbols, but currently
7710 ld -r doesn't do this. So, for ld -r output, it is possible to
7711 make a better choice of file name for local symbols by ignoring
7712 file symbols appearing after a given local symbol. */
7713 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7714 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7718 state = nothing_seen;
7719 cache->filename = NULL;
7721 cache->func_size = 0;
7722 cache->last_section = section;
7724 for (p = symbols; *p != NULL; p++)
7730 if ((sym->flags & BSF_FILE) != 0)
7733 if (state == symbol_seen)
7734 state = file_after_symbol_seen;
7738 size = bed->maybe_function_sym (sym, section, &code_off);
7740 && code_off <= offset
7741 && (code_off > low_func
7742 || (code_off == low_func
7743 && size > cache->func_size)))
7746 cache->func_size = size;
7747 cache->filename = NULL;
7748 low_func = code_off;
7750 && ((sym->flags & BSF_LOCAL) != 0
7751 || state != file_after_symbol_seen))
7752 cache->filename = bfd_asymbol_name (file);
7754 if (state == nothing_seen)
7755 state = symbol_seen;
7759 if (cache->func == NULL)
7763 *filename_ptr = cache->filename;
7764 if (functionname_ptr)
7765 *functionname_ptr = bfd_asymbol_name (cache->func);
7770 /* Find the nearest line to a particular section and offset,
7771 for error reporting. */
7774 _bfd_elf_find_nearest_line (bfd *abfd,
7778 const char **filename_ptr,
7779 const char **functionname_ptr,
7780 unsigned int *line_ptr,
7781 unsigned int *discriminator_ptr)
7785 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7786 filename_ptr, functionname_ptr,
7787 line_ptr, discriminator_ptr,
7788 dwarf_debug_sections, 0,
7789 &elf_tdata (abfd)->dwarf2_find_line_info))
7791 if (!*functionname_ptr)
7792 elf_find_function (abfd, symbols, section, offset,
7793 *filename_ptr ? NULL : filename_ptr,
7799 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
7800 filename_ptr, functionname_ptr, line_ptr))
7802 if (!*functionname_ptr)
7803 elf_find_function (abfd, symbols, section, offset,
7804 *filename_ptr ? NULL : filename_ptr,
7810 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7811 &found, filename_ptr,
7812 functionname_ptr, line_ptr,
7813 &elf_tdata (abfd)->line_info))
7815 if (found && (*functionname_ptr || *line_ptr))
7818 if (symbols == NULL)
7821 if (! elf_find_function (abfd, symbols, section, offset,
7822 filename_ptr, functionname_ptr))
7829 /* Find the line for a symbol. */
7832 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7833 const char **filename_ptr, unsigned int *line_ptr)
7835 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
7836 filename_ptr, NULL, line_ptr, NULL,
7837 dwarf_debug_sections, 0,
7838 &elf_tdata (abfd)->dwarf2_find_line_info);
7841 /* After a call to bfd_find_nearest_line, successive calls to
7842 bfd_find_inliner_info can be used to get source information about
7843 each level of function inlining that terminated at the address
7844 passed to bfd_find_nearest_line. Currently this is only supported
7845 for DWARF2 with appropriate DWARF3 extensions. */
7848 _bfd_elf_find_inliner_info (bfd *abfd,
7849 const char **filename_ptr,
7850 const char **functionname_ptr,
7851 unsigned int *line_ptr)
7854 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7855 functionname_ptr, line_ptr,
7856 & elf_tdata (abfd)->dwarf2_find_line_info);
7861 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7864 int ret = bed->s->sizeof_ehdr;
7866 if (!info->relocatable)
7868 bfd_size_type phdr_size = elf_program_header_size (abfd);
7870 if (phdr_size == (bfd_size_type) -1)
7872 struct elf_segment_map *m;
7875 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
7876 phdr_size += bed->s->sizeof_phdr;
7879 phdr_size = get_program_header_size (abfd, info);
7882 elf_program_header_size (abfd) = phdr_size;
7890 _bfd_elf_set_section_contents (bfd *abfd,
7892 const void *location,
7894 bfd_size_type count)
7896 Elf_Internal_Shdr *hdr;
7899 if (! abfd->output_has_begun
7900 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7903 hdr = &elf_section_data (section)->this_hdr;
7904 pos = hdr->sh_offset + offset;
7905 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7906 || bfd_bwrite (location, count, abfd) != count)
7913 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7914 arelent *cache_ptr ATTRIBUTE_UNUSED,
7915 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7920 /* Try to convert a non-ELF reloc into an ELF one. */
7923 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7925 /* Check whether we really have an ELF howto. */
7927 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7929 bfd_reloc_code_real_type code;
7930 reloc_howto_type *howto;
7932 /* Alien reloc: Try to determine its type to replace it with an
7933 equivalent ELF reloc. */
7935 if (areloc->howto->pc_relative)
7937 switch (areloc->howto->bitsize)
7940 code = BFD_RELOC_8_PCREL;
7943 code = BFD_RELOC_12_PCREL;
7946 code = BFD_RELOC_16_PCREL;
7949 code = BFD_RELOC_24_PCREL;
7952 code = BFD_RELOC_32_PCREL;
7955 code = BFD_RELOC_64_PCREL;
7961 howto = bfd_reloc_type_lookup (abfd, code);
7963 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7965 if (howto->pcrel_offset)
7966 areloc->addend += areloc->address;
7968 areloc->addend -= areloc->address; /* addend is unsigned!! */
7973 switch (areloc->howto->bitsize)
7979 code = BFD_RELOC_14;
7982 code = BFD_RELOC_16;
7985 code = BFD_RELOC_26;
7988 code = BFD_RELOC_32;
7991 code = BFD_RELOC_64;
7997 howto = bfd_reloc_type_lookup (abfd, code);
8001 areloc->howto = howto;
8009 (*_bfd_error_handler)
8010 (_("%B: unsupported relocation type %s"),
8011 abfd, areloc->howto->name);
8012 bfd_set_error (bfd_error_bad_value);
8017 _bfd_elf_close_and_cleanup (bfd *abfd)
8019 struct elf_obj_tdata *tdata = elf_tdata (abfd);
8020 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
8022 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
8023 _bfd_elf_strtab_free (elf_shstrtab (abfd));
8024 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
8027 return _bfd_generic_close_and_cleanup (abfd);
8030 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8031 in the relocation's offset. Thus we cannot allow any sort of sanity
8032 range-checking to interfere. There is nothing else to do in processing
8035 bfd_reloc_status_type
8036 _bfd_elf_rel_vtable_reloc_fn
8037 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
8038 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
8039 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
8040 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
8042 return bfd_reloc_ok;
8045 /* Elf core file support. Much of this only works on native
8046 toolchains, since we rely on knowing the
8047 machine-dependent procfs structure in order to pick
8048 out details about the corefile. */
8050 #ifdef HAVE_SYS_PROCFS_H
8051 /* Needed for new procfs interface on sparc-solaris. */
8052 # define _STRUCTURED_PROC 1
8053 # include <sys/procfs.h>
8056 /* Return a PID that identifies a "thread" for threaded cores, or the
8057 PID of the main process for non-threaded cores. */
8060 elfcore_make_pid (bfd *abfd)
8064 pid = elf_tdata (abfd)->core->lwpid;
8066 pid = elf_tdata (abfd)->core->pid;
8071 /* If there isn't a section called NAME, make one, using
8072 data from SECT. Note, this function will generate a
8073 reference to NAME, so you shouldn't deallocate or
8077 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
8081 if (bfd_get_section_by_name (abfd, name) != NULL)
8084 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
8088 sect2->size = sect->size;
8089 sect2->filepos = sect->filepos;
8090 sect2->alignment_power = sect->alignment_power;
8094 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8095 actually creates up to two pseudosections:
8096 - For the single-threaded case, a section named NAME, unless
8097 such a section already exists.
8098 - For the multi-threaded case, a section named "NAME/PID", where
8099 PID is elfcore_make_pid (abfd).
8100 Both pseudosections have identical contents. */
8102 _bfd_elfcore_make_pseudosection (bfd *abfd,
8108 char *threaded_name;
8112 /* Build the section name. */
8114 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8115 len = strlen (buf) + 1;
8116 threaded_name = (char *) bfd_alloc (abfd, len);
8117 if (threaded_name == NULL)
8119 memcpy (threaded_name, buf, len);
8121 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8126 sect->filepos = filepos;
8127 sect->alignment_power = 2;
8129 return elfcore_maybe_make_sect (abfd, name, sect);
8132 /* prstatus_t exists on:
8134 linux 2.[01] + glibc
8138 #if defined (HAVE_PRSTATUS_T)
8141 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8146 if (note->descsz == sizeof (prstatus_t))
8150 size = sizeof (prstat.pr_reg);
8151 offset = offsetof (prstatus_t, pr_reg);
8152 memcpy (&prstat, note->descdata, sizeof (prstat));
8154 /* Do not overwrite the core signal if it
8155 has already been set by another thread. */
8156 if (elf_tdata (abfd)->core->signal == 0)
8157 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8158 if (elf_tdata (abfd)->core->pid == 0)
8159 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8161 /* pr_who exists on:
8164 pr_who doesn't exist on:
8167 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8168 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8170 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8173 #if defined (HAVE_PRSTATUS32_T)
8174 else if (note->descsz == sizeof (prstatus32_t))
8176 /* 64-bit host, 32-bit corefile */
8177 prstatus32_t prstat;
8179 size = sizeof (prstat.pr_reg);
8180 offset = offsetof (prstatus32_t, pr_reg);
8181 memcpy (&prstat, note->descdata, sizeof (prstat));
8183 /* Do not overwrite the core signal if it
8184 has already been set by another thread. */
8185 if (elf_tdata (abfd)->core->signal == 0)
8186 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8187 if (elf_tdata (abfd)->core->pid == 0)
8188 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8190 /* pr_who exists on:
8193 pr_who doesn't exist on:
8196 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8197 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8199 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8202 #endif /* HAVE_PRSTATUS32_T */
8205 /* Fail - we don't know how to handle any other
8206 note size (ie. data object type). */
8210 /* Make a ".reg/999" section and a ".reg" section. */
8211 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8212 size, note->descpos + offset);
8214 #endif /* defined (HAVE_PRSTATUS_T) */
8216 /* Create a pseudosection containing the exact contents of NOTE. */
8218 elfcore_make_note_pseudosection (bfd *abfd,
8220 Elf_Internal_Note *note)
8222 return _bfd_elfcore_make_pseudosection (abfd, name,
8223 note->descsz, note->descpos);
8226 /* There isn't a consistent prfpregset_t across platforms,
8227 but it doesn't matter, because we don't have to pick this
8228 data structure apart. */
8231 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8233 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8236 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8237 type of NT_PRXFPREG. Just include the whole note's contents
8241 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8243 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8246 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8247 with a note type of NT_X86_XSTATE. Just include the whole note's
8248 contents literally. */
8251 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8253 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8257 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8259 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8263 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8265 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8269 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8271 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8275 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8277 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8281 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8283 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8287 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8289 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8293 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8295 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8299 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8301 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8305 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8307 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8311 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8313 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8317 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8319 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8323 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8325 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8329 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8331 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8335 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8337 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8341 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8343 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8346 #if defined (HAVE_PRPSINFO_T)
8347 typedef prpsinfo_t elfcore_psinfo_t;
8348 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8349 typedef prpsinfo32_t elfcore_psinfo32_t;
8353 #if defined (HAVE_PSINFO_T)
8354 typedef psinfo_t elfcore_psinfo_t;
8355 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8356 typedef psinfo32_t elfcore_psinfo32_t;
8360 /* return a malloc'ed copy of a string at START which is at
8361 most MAX bytes long, possibly without a terminating '\0'.
8362 the copy will always have a terminating '\0'. */
8365 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8368 char *end = (char *) memchr (start, '\0', max);
8376 dups = (char *) bfd_alloc (abfd, len + 1);
8380 memcpy (dups, start, len);
8386 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8388 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8390 if (note->descsz == sizeof (elfcore_psinfo_t))
8392 elfcore_psinfo_t psinfo;
8394 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8396 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8397 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8399 elf_tdata (abfd)->core->program
8400 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8401 sizeof (psinfo.pr_fname));
8403 elf_tdata (abfd)->core->command
8404 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8405 sizeof (psinfo.pr_psargs));
8407 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8408 else if (note->descsz == sizeof (elfcore_psinfo32_t))
8410 /* 64-bit host, 32-bit corefile */
8411 elfcore_psinfo32_t psinfo;
8413 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8415 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8416 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8418 elf_tdata (abfd)->core->program
8419 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8420 sizeof (psinfo.pr_fname));
8422 elf_tdata (abfd)->core->command
8423 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8424 sizeof (psinfo.pr_psargs));
8430 /* Fail - we don't know how to handle any other
8431 note size (ie. data object type). */
8435 /* Note that for some reason, a spurious space is tacked
8436 onto the end of the args in some (at least one anyway)
8437 implementations, so strip it off if it exists. */
8440 char *command = elf_tdata (abfd)->core->command;
8441 int n = strlen (command);
8443 if (0 < n && command[n - 1] == ' ')
8444 command[n - 1] = '\0';
8449 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8451 #if defined (HAVE_PSTATUS_T)
8453 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8455 if (note->descsz == sizeof (pstatus_t)
8456 #if defined (HAVE_PXSTATUS_T)
8457 || note->descsz == sizeof (pxstatus_t)
8463 memcpy (&pstat, note->descdata, sizeof (pstat));
8465 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8467 #if defined (HAVE_PSTATUS32_T)
8468 else if (note->descsz == sizeof (pstatus32_t))
8470 /* 64-bit host, 32-bit corefile */
8473 memcpy (&pstat, note->descdata, sizeof (pstat));
8475 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8478 /* Could grab some more details from the "representative"
8479 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8480 NT_LWPSTATUS note, presumably. */
8484 #endif /* defined (HAVE_PSTATUS_T) */
8486 #if defined (HAVE_LWPSTATUS_T)
8488 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8490 lwpstatus_t lwpstat;
8496 if (note->descsz != sizeof (lwpstat)
8497 #if defined (HAVE_LWPXSTATUS_T)
8498 && note->descsz != sizeof (lwpxstatus_t)
8503 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8505 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8506 /* Do not overwrite the core signal if it has already been set by
8508 if (elf_tdata (abfd)->core->signal == 0)
8509 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8511 /* Make a ".reg/999" section. */
8513 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8514 len = strlen (buf) + 1;
8515 name = bfd_alloc (abfd, len);
8518 memcpy (name, buf, len);
8520 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8524 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8525 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8526 sect->filepos = note->descpos
8527 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8530 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8531 sect->size = sizeof (lwpstat.pr_reg);
8532 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8535 sect->alignment_power = 2;
8537 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8540 /* Make a ".reg2/999" section */
8542 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8543 len = strlen (buf) + 1;
8544 name = bfd_alloc (abfd, len);
8547 memcpy (name, buf, len);
8549 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8553 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8554 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8555 sect->filepos = note->descpos
8556 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8559 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8560 sect->size = sizeof (lwpstat.pr_fpreg);
8561 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8564 sect->alignment_power = 2;
8566 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8568 #endif /* defined (HAVE_LWPSTATUS_T) */
8571 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8578 int is_active_thread;
8581 if (note->descsz < 728)
8584 if (! CONST_STRNEQ (note->namedata, "win32"))
8587 type = bfd_get_32 (abfd, note->descdata);
8591 case 1 /* NOTE_INFO_PROCESS */:
8592 /* FIXME: need to add ->core->command. */
8593 /* process_info.pid */
8594 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
8595 /* process_info.signal */
8596 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
8599 case 2 /* NOTE_INFO_THREAD */:
8600 /* Make a ".reg/999" section. */
8601 /* thread_info.tid */
8602 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8604 len = strlen (buf) + 1;
8605 name = (char *) bfd_alloc (abfd, len);
8609 memcpy (name, buf, len);
8611 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8615 /* sizeof (thread_info.thread_context) */
8617 /* offsetof (thread_info.thread_context) */
8618 sect->filepos = note->descpos + 12;
8619 sect->alignment_power = 2;
8621 /* thread_info.is_active_thread */
8622 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8624 if (is_active_thread)
8625 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8629 case 3 /* NOTE_INFO_MODULE */:
8630 /* Make a ".module/xxxxxxxx" section. */
8631 /* module_info.base_address */
8632 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8633 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8635 len = strlen (buf) + 1;
8636 name = (char *) bfd_alloc (abfd, len);
8640 memcpy (name, buf, len);
8642 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8647 sect->size = note->descsz;
8648 sect->filepos = note->descpos;
8649 sect->alignment_power = 2;
8660 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8662 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8670 if (bed->elf_backend_grok_prstatus)
8671 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8673 #if defined (HAVE_PRSTATUS_T)
8674 return elfcore_grok_prstatus (abfd, note);
8679 #if defined (HAVE_PSTATUS_T)
8681 return elfcore_grok_pstatus (abfd, note);
8684 #if defined (HAVE_LWPSTATUS_T)
8686 return elfcore_grok_lwpstatus (abfd, note);
8689 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8690 return elfcore_grok_prfpreg (abfd, note);
8692 case NT_WIN32PSTATUS:
8693 return elfcore_grok_win32pstatus (abfd, note);
8695 case NT_PRXFPREG: /* Linux SSE extension */
8696 if (note->namesz == 6
8697 && strcmp (note->namedata, "LINUX") == 0)
8698 return elfcore_grok_prxfpreg (abfd, note);
8702 case NT_X86_XSTATE: /* Linux XSAVE extension */
8703 if (note->namesz == 6
8704 && strcmp (note->namedata, "LINUX") == 0)
8705 return elfcore_grok_xstatereg (abfd, note);
8710 if (note->namesz == 6
8711 && strcmp (note->namedata, "LINUX") == 0)
8712 return elfcore_grok_ppc_vmx (abfd, note);
8717 if (note->namesz == 6
8718 && strcmp (note->namedata, "LINUX") == 0)
8719 return elfcore_grok_ppc_vsx (abfd, note);
8723 case NT_S390_HIGH_GPRS:
8724 if (note->namesz == 6
8725 && strcmp (note->namedata, "LINUX") == 0)
8726 return elfcore_grok_s390_high_gprs (abfd, note);
8731 if (note->namesz == 6
8732 && strcmp (note->namedata, "LINUX") == 0)
8733 return elfcore_grok_s390_timer (abfd, note);
8737 case NT_S390_TODCMP:
8738 if (note->namesz == 6
8739 && strcmp (note->namedata, "LINUX") == 0)
8740 return elfcore_grok_s390_todcmp (abfd, note);
8744 case NT_S390_TODPREG:
8745 if (note->namesz == 6
8746 && strcmp (note->namedata, "LINUX") == 0)
8747 return elfcore_grok_s390_todpreg (abfd, note);
8752 if (note->namesz == 6
8753 && strcmp (note->namedata, "LINUX") == 0)
8754 return elfcore_grok_s390_ctrs (abfd, note);
8758 case NT_S390_PREFIX:
8759 if (note->namesz == 6
8760 && strcmp (note->namedata, "LINUX") == 0)
8761 return elfcore_grok_s390_prefix (abfd, note);
8765 case NT_S390_LAST_BREAK:
8766 if (note->namesz == 6
8767 && strcmp (note->namedata, "LINUX") == 0)
8768 return elfcore_grok_s390_last_break (abfd, note);
8772 case NT_S390_SYSTEM_CALL:
8773 if (note->namesz == 6
8774 && strcmp (note->namedata, "LINUX") == 0)
8775 return elfcore_grok_s390_system_call (abfd, note);
8780 if (note->namesz == 6
8781 && strcmp (note->namedata, "LINUX") == 0)
8782 return elfcore_grok_s390_tdb (abfd, note);
8787 if (note->namesz == 6
8788 && strcmp (note->namedata, "LINUX") == 0)
8789 return elfcore_grok_arm_vfp (abfd, note);
8794 if (note->namesz == 6
8795 && strcmp (note->namedata, "LINUX") == 0)
8796 return elfcore_grok_aarch_tls (abfd, note);
8800 case NT_ARM_HW_BREAK:
8801 if (note->namesz == 6
8802 && strcmp (note->namedata, "LINUX") == 0)
8803 return elfcore_grok_aarch_hw_break (abfd, note);
8807 case NT_ARM_HW_WATCH:
8808 if (note->namesz == 6
8809 && strcmp (note->namedata, "LINUX") == 0)
8810 return elfcore_grok_aarch_hw_watch (abfd, note);
8816 if (bed->elf_backend_grok_psinfo)
8817 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8819 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8820 return elfcore_grok_psinfo (abfd, note);
8827 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8832 sect->size = note->descsz;
8833 sect->filepos = note->descpos;
8834 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8840 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
8844 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
8850 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8852 struct elf_obj_tdata *t;
8854 if (note->descsz == 0)
8857 t = elf_tdata (abfd);
8858 t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
8859 if (t->build_id == NULL)
8862 t->build_id->size = note->descsz;
8863 memcpy (t->build_id->data, note->descdata, note->descsz);
8869 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8876 case NT_GNU_BUILD_ID:
8877 return elfobj_grok_gnu_build_id (abfd, note);
8882 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
8884 struct sdt_note *cur =
8885 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
8888 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
8889 cur->size = (bfd_size_type) note->descsz;
8890 memcpy (cur->data, note->descdata, note->descsz);
8892 elf_tdata (abfd)->sdt_note_head = cur;
8898 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
8903 return elfobj_grok_stapsdt_note_1 (abfd, note);
8911 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8915 cp = strchr (note->namedata, '@');
8918 *lwpidp = atoi(cp + 1);
8925 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8927 /* Signal number at offset 0x08. */
8928 elf_tdata (abfd)->core->signal
8929 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8931 /* Process ID at offset 0x50. */
8932 elf_tdata (abfd)->core->pid
8933 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8935 /* Command name at 0x7c (max 32 bytes, including nul). */
8936 elf_tdata (abfd)->core->command
8937 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8939 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8944 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8948 if (elfcore_netbsd_get_lwpid (note, &lwp))
8949 elf_tdata (abfd)->core->lwpid = lwp;
8951 if (note->type == NT_NETBSDCORE_PROCINFO)
8953 /* NetBSD-specific core "procinfo". Note that we expect to
8954 find this note before any of the others, which is fine,
8955 since the kernel writes this note out first when it
8956 creates a core file. */
8958 return elfcore_grok_netbsd_procinfo (abfd, note);
8961 /* As of Jan 2002 there are no other machine-independent notes
8962 defined for NetBSD core files. If the note type is less
8963 than the start of the machine-dependent note types, we don't
8966 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8970 switch (bfd_get_arch (abfd))
8972 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8973 PT_GETFPREGS == mach+2. */
8975 case bfd_arch_alpha:
8976 case bfd_arch_sparc:
8979 case NT_NETBSDCORE_FIRSTMACH+0:
8980 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8982 case NT_NETBSDCORE_FIRSTMACH+2:
8983 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8989 /* On all other arch's, PT_GETREGS == mach+1 and
8990 PT_GETFPREGS == mach+3. */
8995 case NT_NETBSDCORE_FIRSTMACH+1:
8996 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8998 case NT_NETBSDCORE_FIRSTMACH+3:
8999 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9009 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
9011 /* Signal number at offset 0x08. */
9012 elf_tdata (abfd)->core->signal
9013 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
9015 /* Process ID at offset 0x20. */
9016 elf_tdata (abfd)->core->pid
9017 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
9019 /* Command name at 0x48 (max 32 bytes, including nul). */
9020 elf_tdata (abfd)->core->command
9021 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
9027 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
9029 if (note->type == NT_OPENBSD_PROCINFO)
9030 return elfcore_grok_openbsd_procinfo (abfd, note);
9032 if (note->type == NT_OPENBSD_REGS)
9033 return elfcore_make_note_pseudosection (abfd, ".reg", note);
9035 if (note->type == NT_OPENBSD_FPREGS)
9036 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9038 if (note->type == NT_OPENBSD_XFPREGS)
9039 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9041 if (note->type == NT_OPENBSD_AUXV)
9043 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9048 sect->size = note->descsz;
9049 sect->filepos = note->descpos;
9050 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9055 if (note->type == NT_OPENBSD_WCOOKIE)
9057 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
9062 sect->size = note->descsz;
9063 sect->filepos = note->descpos;
9064 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9073 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
9075 void *ddata = note->descdata;
9082 /* nto_procfs_status 'pid' field is at offset 0. */
9083 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
9085 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9086 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
9088 /* nto_procfs_status 'flags' field is at offset 8. */
9089 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
9091 /* nto_procfs_status 'what' field is at offset 14. */
9092 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9094 elf_tdata (abfd)->core->signal = sig;
9095 elf_tdata (abfd)->core->lwpid = *tid;
9098 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9099 do not come from signals so we make sure we set the current
9100 thread just in case. */
9101 if (flags & 0x00000080)
9102 elf_tdata (abfd)->core->lwpid = *tid;
9104 /* Make a ".qnx_core_status/%d" section. */
9105 sprintf (buf, ".qnx_core_status/%ld", *tid);
9107 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9112 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9116 sect->size = note->descsz;
9117 sect->filepos = note->descpos;
9118 sect->alignment_power = 2;
9120 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9124 elfcore_grok_nto_regs (bfd *abfd,
9125 Elf_Internal_Note *note,
9133 /* Make a "(base)/%d" section. */
9134 sprintf (buf, "%s/%ld", base, tid);
9136 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9141 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9145 sect->size = note->descsz;
9146 sect->filepos = note->descpos;
9147 sect->alignment_power = 2;
9149 /* This is the current thread. */
9150 if (elf_tdata (abfd)->core->lwpid == tid)
9151 return elfcore_maybe_make_sect (abfd, base, sect);
9156 #define BFD_QNT_CORE_INFO 7
9157 #define BFD_QNT_CORE_STATUS 8
9158 #define BFD_QNT_CORE_GREG 9
9159 #define BFD_QNT_CORE_FPREG 10
9162 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9164 /* Every GREG section has a STATUS section before it. Store the
9165 tid from the previous call to pass down to the next gregs
9167 static long tid = 1;
9171 case BFD_QNT_CORE_INFO:
9172 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9173 case BFD_QNT_CORE_STATUS:
9174 return elfcore_grok_nto_status (abfd, note, &tid);
9175 case BFD_QNT_CORE_GREG:
9176 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9177 case BFD_QNT_CORE_FPREG:
9178 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9185 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9191 /* Use note name as section name. */
9193 name = (char *) bfd_alloc (abfd, len);
9196 memcpy (name, note->namedata, len);
9197 name[len - 1] = '\0';
9199 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9203 sect->size = note->descsz;
9204 sect->filepos = note->descpos;
9205 sect->alignment_power = 1;
9210 /* Function: elfcore_write_note
9213 buffer to hold note, and current size of buffer
9217 size of data for note
9219 Writes note to end of buffer. ELF64 notes are written exactly as
9220 for ELF32, despite the current (as of 2006) ELF gabi specifying
9221 that they ought to have 8-byte namesz and descsz field, and have
9222 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9225 Pointer to realloc'd buffer, *BUFSIZ updated. */
9228 elfcore_write_note (bfd *abfd,
9236 Elf_External_Note *xnp;
9243 namesz = strlen (name) + 1;
9245 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9247 buf = (char *) realloc (buf, *bufsiz + newspace);
9250 dest = buf + *bufsiz;
9251 *bufsiz += newspace;
9252 xnp = (Elf_External_Note *) dest;
9253 H_PUT_32 (abfd, namesz, xnp->namesz);
9254 H_PUT_32 (abfd, size, xnp->descsz);
9255 H_PUT_32 (abfd, type, xnp->type);
9259 memcpy (dest, name, namesz);
9267 memcpy (dest, input, size);
9278 elfcore_write_prpsinfo (bfd *abfd,
9284 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9286 if (bed->elf_backend_write_core_note != NULL)
9289 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9290 NT_PRPSINFO, fname, psargs);
9295 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9296 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9297 if (bed->s->elfclass == ELFCLASS32)
9299 #if defined (HAVE_PSINFO32_T)
9301 int note_type = NT_PSINFO;
9304 int note_type = NT_PRPSINFO;
9307 memset (&data, 0, sizeof (data));
9308 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9309 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9310 return elfcore_write_note (abfd, buf, bufsiz,
9311 "CORE", note_type, &data, sizeof (data));
9316 #if defined (HAVE_PSINFO_T)
9318 int note_type = NT_PSINFO;
9321 int note_type = NT_PRPSINFO;
9324 memset (&data, 0, sizeof (data));
9325 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9326 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9327 return elfcore_write_note (abfd, buf, bufsiz,
9328 "CORE", note_type, &data, sizeof (data));
9330 #endif /* PSINFO_T or PRPSINFO_T */
9337 elfcore_write_linux_prpsinfo32
9338 (bfd *abfd, char *buf, int *bufsiz,
9339 const struct elf_internal_linux_prpsinfo *prpsinfo)
9341 struct elf_external_linux_prpsinfo32 data;
9343 memset (&data, 0, sizeof (data));
9344 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9346 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9347 &data, sizeof (data));
9351 elfcore_write_linux_prpsinfo64
9352 (bfd *abfd, char *buf, int *bufsiz,
9353 const struct elf_internal_linux_prpsinfo *prpsinfo)
9355 struct elf_external_linux_prpsinfo64 data;
9357 memset (&data, 0, sizeof (data));
9358 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9360 return elfcore_write_note (abfd, buf, bufsiz,
9361 "CORE", NT_PRPSINFO, &data, sizeof (data));
9365 elfcore_write_prstatus (bfd *abfd,
9372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9374 if (bed->elf_backend_write_core_note != NULL)
9377 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9379 pid, cursig, gregs);
9384 #if defined (HAVE_PRSTATUS_T)
9385 #if defined (HAVE_PRSTATUS32_T)
9386 if (bed->s->elfclass == ELFCLASS32)
9388 prstatus32_t prstat;
9390 memset (&prstat, 0, sizeof (prstat));
9391 prstat.pr_pid = pid;
9392 prstat.pr_cursig = cursig;
9393 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9394 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9395 NT_PRSTATUS, &prstat, sizeof (prstat));
9402 memset (&prstat, 0, sizeof (prstat));
9403 prstat.pr_pid = pid;
9404 prstat.pr_cursig = cursig;
9405 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9406 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9407 NT_PRSTATUS, &prstat, sizeof (prstat));
9409 #endif /* HAVE_PRSTATUS_T */
9415 #if defined (HAVE_LWPSTATUS_T)
9417 elfcore_write_lwpstatus (bfd *abfd,
9424 lwpstatus_t lwpstat;
9425 const char *note_name = "CORE";
9427 memset (&lwpstat, 0, sizeof (lwpstat));
9428 lwpstat.pr_lwpid = pid >> 16;
9429 lwpstat.pr_cursig = cursig;
9430 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9431 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9432 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9434 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9435 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9437 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9438 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9441 return elfcore_write_note (abfd, buf, bufsiz, note_name,
9442 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9444 #endif /* HAVE_LWPSTATUS_T */
9446 #if defined (HAVE_PSTATUS_T)
9448 elfcore_write_pstatus (bfd *abfd,
9452 int cursig ATTRIBUTE_UNUSED,
9453 const void *gregs ATTRIBUTE_UNUSED)
9455 const char *note_name = "CORE";
9456 #if defined (HAVE_PSTATUS32_T)
9457 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9459 if (bed->s->elfclass == ELFCLASS32)
9463 memset (&pstat, 0, sizeof (pstat));
9464 pstat.pr_pid = pid & 0xffff;
9465 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9466 NT_PSTATUS, &pstat, sizeof (pstat));
9474 memset (&pstat, 0, sizeof (pstat));
9475 pstat.pr_pid = pid & 0xffff;
9476 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9477 NT_PSTATUS, &pstat, sizeof (pstat));
9481 #endif /* HAVE_PSTATUS_T */
9484 elfcore_write_prfpreg (bfd *abfd,
9490 const char *note_name = "CORE";
9491 return elfcore_write_note (abfd, buf, bufsiz,
9492 note_name, NT_FPREGSET, fpregs, size);
9496 elfcore_write_prxfpreg (bfd *abfd,
9499 const void *xfpregs,
9502 char *note_name = "LINUX";
9503 return elfcore_write_note (abfd, buf, bufsiz,
9504 note_name, NT_PRXFPREG, xfpregs, size);
9508 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9509 const void *xfpregs, int size)
9511 char *note_name = "LINUX";
9512 return elfcore_write_note (abfd, buf, bufsiz,
9513 note_name, NT_X86_XSTATE, xfpregs, size);
9517 elfcore_write_ppc_vmx (bfd *abfd,
9520 const void *ppc_vmx,
9523 char *note_name = "LINUX";
9524 return elfcore_write_note (abfd, buf, bufsiz,
9525 note_name, NT_PPC_VMX, ppc_vmx, size);
9529 elfcore_write_ppc_vsx (bfd *abfd,
9532 const void *ppc_vsx,
9535 char *note_name = "LINUX";
9536 return elfcore_write_note (abfd, buf, bufsiz,
9537 note_name, NT_PPC_VSX, ppc_vsx, size);
9541 elfcore_write_s390_high_gprs (bfd *abfd,
9544 const void *s390_high_gprs,
9547 char *note_name = "LINUX";
9548 return elfcore_write_note (abfd, buf, bufsiz,
9549 note_name, NT_S390_HIGH_GPRS,
9550 s390_high_gprs, size);
9554 elfcore_write_s390_timer (bfd *abfd,
9557 const void *s390_timer,
9560 char *note_name = "LINUX";
9561 return elfcore_write_note (abfd, buf, bufsiz,
9562 note_name, NT_S390_TIMER, s390_timer, size);
9566 elfcore_write_s390_todcmp (bfd *abfd,
9569 const void *s390_todcmp,
9572 char *note_name = "LINUX";
9573 return elfcore_write_note (abfd, buf, bufsiz,
9574 note_name, NT_S390_TODCMP, s390_todcmp, size);
9578 elfcore_write_s390_todpreg (bfd *abfd,
9581 const void *s390_todpreg,
9584 char *note_name = "LINUX";
9585 return elfcore_write_note (abfd, buf, bufsiz,
9586 note_name, NT_S390_TODPREG, s390_todpreg, size);
9590 elfcore_write_s390_ctrs (bfd *abfd,
9593 const void *s390_ctrs,
9596 char *note_name = "LINUX";
9597 return elfcore_write_note (abfd, buf, bufsiz,
9598 note_name, NT_S390_CTRS, s390_ctrs, size);
9602 elfcore_write_s390_prefix (bfd *abfd,
9605 const void *s390_prefix,
9608 char *note_name = "LINUX";
9609 return elfcore_write_note (abfd, buf, bufsiz,
9610 note_name, NT_S390_PREFIX, s390_prefix, size);
9614 elfcore_write_s390_last_break (bfd *abfd,
9617 const void *s390_last_break,
9620 char *note_name = "LINUX";
9621 return elfcore_write_note (abfd, buf, bufsiz,
9622 note_name, NT_S390_LAST_BREAK,
9623 s390_last_break, size);
9627 elfcore_write_s390_system_call (bfd *abfd,
9630 const void *s390_system_call,
9633 char *note_name = "LINUX";
9634 return elfcore_write_note (abfd, buf, bufsiz,
9635 note_name, NT_S390_SYSTEM_CALL,
9636 s390_system_call, size);
9640 elfcore_write_s390_tdb (bfd *abfd,
9643 const void *s390_tdb,
9646 char *note_name = "LINUX";
9647 return elfcore_write_note (abfd, buf, bufsiz,
9648 note_name, NT_S390_TDB, s390_tdb, size);
9652 elfcore_write_arm_vfp (bfd *abfd,
9655 const void *arm_vfp,
9658 char *note_name = "LINUX";
9659 return elfcore_write_note (abfd, buf, bufsiz,
9660 note_name, NT_ARM_VFP, arm_vfp, size);
9664 elfcore_write_aarch_tls (bfd *abfd,
9667 const void *aarch_tls,
9670 char *note_name = "LINUX";
9671 return elfcore_write_note (abfd, buf, bufsiz,
9672 note_name, NT_ARM_TLS, aarch_tls, size);
9676 elfcore_write_aarch_hw_break (bfd *abfd,
9679 const void *aarch_hw_break,
9682 char *note_name = "LINUX";
9683 return elfcore_write_note (abfd, buf, bufsiz,
9684 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
9688 elfcore_write_aarch_hw_watch (bfd *abfd,
9691 const void *aarch_hw_watch,
9694 char *note_name = "LINUX";
9695 return elfcore_write_note (abfd, buf, bufsiz,
9696 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
9700 elfcore_write_register_note (bfd *abfd,
9703 const char *section,
9707 if (strcmp (section, ".reg2") == 0)
9708 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9709 if (strcmp (section, ".reg-xfp") == 0)
9710 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9711 if (strcmp (section, ".reg-xstate") == 0)
9712 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9713 if (strcmp (section, ".reg-ppc-vmx") == 0)
9714 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9715 if (strcmp (section, ".reg-ppc-vsx") == 0)
9716 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9717 if (strcmp (section, ".reg-s390-high-gprs") == 0)
9718 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9719 if (strcmp (section, ".reg-s390-timer") == 0)
9720 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9721 if (strcmp (section, ".reg-s390-todcmp") == 0)
9722 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9723 if (strcmp (section, ".reg-s390-todpreg") == 0)
9724 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9725 if (strcmp (section, ".reg-s390-ctrs") == 0)
9726 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9727 if (strcmp (section, ".reg-s390-prefix") == 0)
9728 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9729 if (strcmp (section, ".reg-s390-last-break") == 0)
9730 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
9731 if (strcmp (section, ".reg-s390-system-call") == 0)
9732 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
9733 if (strcmp (section, ".reg-s390-tdb") == 0)
9734 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
9735 if (strcmp (section, ".reg-arm-vfp") == 0)
9736 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
9737 if (strcmp (section, ".reg-aarch-tls") == 0)
9738 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
9739 if (strcmp (section, ".reg-aarch-hw-break") == 0)
9740 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
9741 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
9742 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
9747 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9752 while (p < buf + size)
9754 /* FIXME: bad alignment assumption. */
9755 Elf_External_Note *xnp = (Elf_External_Note *) p;
9756 Elf_Internal_Note in;
9758 if (offsetof (Elf_External_Note, name) > buf - p + size)
9761 in.type = H_GET_32 (abfd, xnp->type);
9763 in.namesz = H_GET_32 (abfd, xnp->namesz);
9764 in.namedata = xnp->name;
9765 if (in.namesz > buf - in.namedata + size)
9768 in.descsz = H_GET_32 (abfd, xnp->descsz);
9769 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9770 in.descpos = offset + (in.descdata - buf);
9772 && (in.descdata >= buf + size
9773 || in.descsz > buf - in.descdata + size))
9776 switch (bfd_get_format (abfd))
9782 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
9784 if (! elfcore_grok_netbsd_note (abfd, &in))
9787 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
9789 if (! elfcore_grok_openbsd_note (abfd, &in))
9792 else if (CONST_STRNEQ (in.namedata, "QNX"))
9794 if (! elfcore_grok_nto_note (abfd, &in))
9797 else if (CONST_STRNEQ (in.namedata, "SPU/"))
9799 if (! elfcore_grok_spu_note (abfd, &in))
9804 if (! elfcore_grok_note (abfd, &in))
9810 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9812 if (! elfobj_grok_gnu_note (abfd, &in))
9815 else if (in.namesz == sizeof "stapsdt"
9816 && strcmp (in.namedata, "stapsdt") == 0)
9818 if (! elfobj_grok_stapsdt_note (abfd, &in))
9824 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9831 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9838 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9841 buf = (char *) bfd_malloc (size);
9845 if (bfd_bread (buf, size, abfd) != size
9846 || !elf_parse_notes (abfd, buf, size, offset))
9856 /* Providing external access to the ELF program header table. */
9858 /* Return an upper bound on the number of bytes required to store a
9859 copy of ABFD's program header table entries. Return -1 if an error
9860 occurs; bfd_get_error will return an appropriate code. */
9863 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9865 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9867 bfd_set_error (bfd_error_wrong_format);
9871 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9874 /* Copy ABFD's program header table entries to *PHDRS. The entries
9875 will be stored as an array of Elf_Internal_Phdr structures, as
9876 defined in include/elf/internal.h. To find out how large the
9877 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9879 Return the number of program header table entries read, or -1 if an
9880 error occurs; bfd_get_error will return an appropriate code. */
9883 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9887 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9889 bfd_set_error (bfd_error_wrong_format);
9893 num_phdrs = elf_elfheader (abfd)->e_phnum;
9894 memcpy (phdrs, elf_tdata (abfd)->phdr,
9895 num_phdrs * sizeof (Elf_Internal_Phdr));
9900 enum elf_reloc_type_class
9901 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
9902 const asection *rel_sec ATTRIBUTE_UNUSED,
9903 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9905 return reloc_class_normal;
9908 /* For RELA architectures, return the relocation value for a
9909 relocation against a local symbol. */
9912 _bfd_elf_rela_local_sym (bfd *abfd,
9913 Elf_Internal_Sym *sym,
9915 Elf_Internal_Rela *rel)
9917 asection *sec = *psec;
9920 relocation = (sec->output_section->vma
9921 + sec->output_offset
9923 if ((sec->flags & SEC_MERGE)
9924 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9925 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
9928 _bfd_merged_section_offset (abfd, psec,
9929 elf_section_data (sec)->sec_info,
9930 sym->st_value + rel->r_addend);
9933 /* If we have changed the section, and our original section is
9934 marked with SEC_EXCLUDE, it means that the original
9935 SEC_MERGE section has been completely subsumed in some
9936 other SEC_MERGE section. In this case, we need to leave
9937 some info around for --emit-relocs. */
9938 if ((sec->flags & SEC_EXCLUDE) != 0)
9939 sec->kept_section = *psec;
9942 rel->r_addend -= relocation;
9943 rel->r_addend += sec->output_section->vma + sec->output_offset;
9949 _bfd_elf_rel_local_sym (bfd *abfd,
9950 Elf_Internal_Sym *sym,
9954 asection *sec = *psec;
9956 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
9957 return sym->st_value + addend;
9959 return _bfd_merged_section_offset (abfd, psec,
9960 elf_section_data (sec)->sec_info,
9961 sym->st_value + addend);
9965 _bfd_elf_section_offset (bfd *abfd,
9966 struct bfd_link_info *info,
9970 switch (sec->sec_info_type)
9972 case SEC_INFO_TYPE_STABS:
9973 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9975 case SEC_INFO_TYPE_EH_FRAME:
9976 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9978 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
9980 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9981 bfd_size_type address_size = bed->s->arch_size / 8;
9982 offset = sec->size - offset - address_size;
9988 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9989 reconstruct an ELF file by reading the segments out of remote memory
9990 based on the ELF file header at EHDR_VMA and the ELF program headers it
9991 points to. If not null, *LOADBASEP is filled in with the difference
9992 between the VMAs from which the segments were read, and the VMAs the
9993 file headers (and hence BFD's idea of each section's VMA) put them at.
9995 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9996 remote memory at target address VMA into the local buffer at MYADDR; it
9997 should return zero on success or an `errno' code on failure. TEMPL must
9998 be a BFD for an ELF target with the word size and byte order found in
9999 the remote memory. */
10002 bfd_elf_bfd_from_remote_memory
10005 bfd_size_type size,
10006 bfd_vma *loadbasep,
10007 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
10009 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
10010 (templ, ehdr_vma, size, loadbasep, target_read_memory);
10014 _bfd_elf_get_synthetic_symtab (bfd *abfd,
10015 long symcount ATTRIBUTE_UNUSED,
10016 asymbol **syms ATTRIBUTE_UNUSED,
10021 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10024 const char *relplt_name;
10025 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
10029 Elf_Internal_Shdr *hdr;
10035 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
10038 if (dynsymcount <= 0)
10041 if (!bed->plt_sym_val)
10044 relplt_name = bed->relplt_name;
10045 if (relplt_name == NULL)
10046 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
10047 relplt = bfd_get_section_by_name (abfd, relplt_name);
10048 if (relplt == NULL)
10051 hdr = &elf_section_data (relplt)->this_hdr;
10052 if (hdr->sh_link != elf_dynsymtab (abfd)
10053 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
10056 plt = bfd_get_section_by_name (abfd, ".plt");
10060 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
10061 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
10064 count = relplt->size / hdr->sh_entsize;
10065 size = count * sizeof (asymbol);
10066 p = relplt->relocation;
10067 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10069 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
10070 if (p->addend != 0)
10073 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
10075 size += sizeof ("+0x") - 1 + 8;
10080 s = *ret = (asymbol *) bfd_malloc (size);
10084 names = (char *) (s + count);
10085 p = relplt->relocation;
10087 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10092 addr = bed->plt_sym_val (i, plt, p);
10093 if (addr == (bfd_vma) -1)
10096 *s = **p->sym_ptr_ptr;
10097 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10098 we are defining a symbol, ensure one of them is set. */
10099 if ((s->flags & BSF_LOCAL) == 0)
10100 s->flags |= BSF_GLOBAL;
10101 s->flags |= BSF_SYNTHETIC;
10103 s->value = addr - plt->vma;
10106 len = strlen ((*p->sym_ptr_ptr)->name);
10107 memcpy (names, (*p->sym_ptr_ptr)->name, len);
10109 if (p->addend != 0)
10113 memcpy (names, "+0x", sizeof ("+0x") - 1);
10114 names += sizeof ("+0x") - 1;
10115 bfd_sprintf_vma (abfd, buf, p->addend);
10116 for (a = buf; *a == '0'; ++a)
10119 memcpy (names, a, len);
10122 memcpy (names, "@plt", sizeof ("@plt"));
10123 names += sizeof ("@plt");
10130 /* It is only used by x86-64 so far. */
10131 asection _bfd_elf_large_com_section
10132 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10133 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10136 _bfd_elf_post_process_headers (bfd * abfd,
10137 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10139 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
10141 i_ehdrp = elf_elfheader (abfd);
10143 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10145 /* To make things simpler for the loader on Linux systems we set the
10146 osabi field to ELFOSABI_GNU if the binary contains symbols of
10147 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10148 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10149 && elf_tdata (abfd)->has_gnu_symbols)
10150 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10154 /* Return TRUE for ELF symbol types that represent functions.
10155 This is the default version of this function, which is sufficient for
10156 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10159 _bfd_elf_is_function_type (unsigned int type)
10161 return (type == STT_FUNC
10162 || type == STT_GNU_IFUNC);
10165 /* If the ELF symbol SYM might be a function in SEC, return the
10166 function size and set *CODE_OFF to the function's entry point,
10167 otherwise return zero. */
10170 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10173 bfd_size_type size;
10175 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10176 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10177 || sym->section != sec)
10180 *code_off = sym->value;
10182 if (!(sym->flags & BSF_SYNTHETIC))
10183 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;