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 /* Display ELF-specific fields of a symbol. */
1428 bfd_elf_print_symbol (bfd *abfd,
1431 bfd_print_symbol_type how)
1433 FILE *file = (FILE *) filep;
1436 case bfd_print_symbol_name:
1437 fprintf (file, "%s", symbol->name);
1439 case bfd_print_symbol_more:
1440 fprintf (file, "elf ");
1441 bfd_fprintf_vma (abfd, file, symbol->value);
1442 fprintf (file, " %lx", (unsigned long) symbol->flags);
1444 case bfd_print_symbol_all:
1446 const char *section_name;
1447 const char *name = NULL;
1448 const struct elf_backend_data *bed;
1449 unsigned char st_other;
1452 section_name = symbol->section ? symbol->section->name : "(*none*)";
1454 bed = get_elf_backend_data (abfd);
1455 if (bed->elf_backend_print_symbol_all)
1456 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1460 name = symbol->name;
1461 bfd_print_symbol_vandf (abfd, file, symbol);
1464 fprintf (file, " %s\t", section_name);
1465 /* Print the "other" value for a symbol. For common symbols,
1466 we've already printed the size; now print the alignment.
1467 For other symbols, we have no specified alignment, and
1468 we've printed the address; now print the size. */
1469 if (symbol->section && bfd_is_com_section (symbol->section))
1470 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1472 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1473 bfd_fprintf_vma (abfd, file, val);
1475 /* If we have version information, print it. */
1476 if (elf_dynversym (abfd) != 0
1477 && (elf_dynverdef (abfd) != 0
1478 || elf_dynverref (abfd) != 0))
1480 unsigned int vernum;
1481 const char *version_string;
1483 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1486 version_string = "";
1487 else if (vernum == 1)
1488 version_string = "Base";
1489 else if (vernum <= elf_tdata (abfd)->cverdefs)
1491 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1494 Elf_Internal_Verneed *t;
1496 version_string = "";
1497 for (t = elf_tdata (abfd)->verref;
1501 Elf_Internal_Vernaux *a;
1503 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1505 if (a->vna_other == vernum)
1507 version_string = a->vna_nodename;
1514 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1515 fprintf (file, " %-11s", version_string);
1520 fprintf (file, " (%s)", version_string);
1521 for (i = 10 - strlen (version_string); i > 0; --i)
1526 /* If the st_other field is not zero, print it. */
1527 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1532 case STV_INTERNAL: fprintf (file, " .internal"); break;
1533 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1534 case STV_PROTECTED: fprintf (file, " .protected"); break;
1536 /* Some other non-defined flags are also present, so print
1538 fprintf (file, " 0x%02x", (unsigned int) st_other);
1541 fprintf (file, " %s", name);
1547 /* Allocate an ELF string table--force the first byte to be zero. */
1549 struct bfd_strtab_hash *
1550 _bfd_elf_stringtab_init (void)
1552 struct bfd_strtab_hash *ret;
1554 ret = _bfd_stringtab_init ();
1559 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1560 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1561 if (loc == (bfd_size_type) -1)
1563 _bfd_stringtab_free (ret);
1570 /* ELF .o/exec file reading */
1572 /* Create a new bfd section from an ELF section header. */
1575 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1577 Elf_Internal_Shdr *hdr;
1578 Elf_Internal_Ehdr *ehdr;
1579 const struct elf_backend_data *bed;
1582 if (shindex >= elf_numsections (abfd))
1585 hdr = elf_elfsections (abfd)[shindex];
1586 ehdr = elf_elfheader (abfd);
1587 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1592 bed = get_elf_backend_data (abfd);
1593 switch (hdr->sh_type)
1596 /* Inactive section. Throw it away. */
1599 case SHT_PROGBITS: /* Normal section with contents. */
1600 case SHT_NOBITS: /* .bss section. */
1601 case SHT_HASH: /* .hash section. */
1602 case SHT_NOTE: /* .note section. */
1603 case SHT_INIT_ARRAY: /* .init_array section. */
1604 case SHT_FINI_ARRAY: /* .fini_array section. */
1605 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1606 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1607 case SHT_GNU_HASH: /* .gnu.hash section. */
1608 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1610 case SHT_DYNAMIC: /* Dynamic linking information. */
1611 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1613 if (hdr->sh_link > elf_numsections (abfd))
1615 /* PR 10478: Accept Solaris binaries with a sh_link
1616 field set to SHN_BEFORE or SHN_AFTER. */
1617 switch (bfd_get_arch (abfd))
1620 case bfd_arch_sparc:
1621 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1622 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1624 /* Otherwise fall through. */
1629 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1631 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1633 Elf_Internal_Shdr *dynsymhdr;
1635 /* The shared libraries distributed with hpux11 have a bogus
1636 sh_link field for the ".dynamic" section. Find the
1637 string table for the ".dynsym" section instead. */
1638 if (elf_dynsymtab (abfd) != 0)
1640 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1641 hdr->sh_link = dynsymhdr->sh_link;
1645 unsigned int i, num_sec;
1647 num_sec = elf_numsections (abfd);
1648 for (i = 1; i < num_sec; i++)
1650 dynsymhdr = elf_elfsections (abfd)[i];
1651 if (dynsymhdr->sh_type == SHT_DYNSYM)
1653 hdr->sh_link = dynsymhdr->sh_link;
1661 case SHT_SYMTAB: /* A symbol table */
1662 if (elf_onesymtab (abfd) == shindex)
1665 if (hdr->sh_entsize != bed->s->sizeof_sym)
1667 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1669 if (hdr->sh_size != 0)
1671 /* Some assemblers erroneously set sh_info to one with a
1672 zero sh_size. ld sees this as a global symbol count
1673 of (unsigned) -1. Fix it here. */
1677 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1678 elf_onesymtab (abfd) = shindex;
1679 elf_tdata (abfd)->symtab_hdr = *hdr;
1680 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1681 abfd->flags |= HAS_SYMS;
1683 /* Sometimes a shared object will map in the symbol table. If
1684 SHF_ALLOC is set, and this is a shared object, then we also
1685 treat this section as a BFD section. We can not base the
1686 decision purely on SHF_ALLOC, because that flag is sometimes
1687 set in a relocatable object file, which would confuse the
1689 if ((hdr->sh_flags & SHF_ALLOC) != 0
1690 && (abfd->flags & DYNAMIC) != 0
1691 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1695 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1696 can't read symbols without that section loaded as well. It
1697 is most likely specified by the next section header. */
1698 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1700 unsigned int i, num_sec;
1702 num_sec = elf_numsections (abfd);
1703 for (i = shindex + 1; i < num_sec; i++)
1705 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1706 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1707 && hdr2->sh_link == shindex)
1711 for (i = 1; i < shindex; i++)
1713 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1714 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1715 && hdr2->sh_link == shindex)
1719 return bfd_section_from_shdr (abfd, i);
1723 case SHT_DYNSYM: /* A dynamic symbol table */
1724 if (elf_dynsymtab (abfd) == shindex)
1727 if (hdr->sh_entsize != bed->s->sizeof_sym)
1729 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1731 if (hdr->sh_size != 0)
1733 /* Some linkers erroneously set sh_info to one with a
1734 zero sh_size. ld sees this as a global symbol count
1735 of (unsigned) -1. Fix it here. */
1739 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1740 elf_dynsymtab (abfd) = shindex;
1741 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1742 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1743 abfd->flags |= HAS_SYMS;
1745 /* Besides being a symbol table, we also treat this as a regular
1746 section, so that objcopy can handle it. */
1747 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1749 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1750 if (elf_symtab_shndx (abfd) == shindex)
1753 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1754 elf_symtab_shndx (abfd) = shindex;
1755 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1756 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1759 case SHT_STRTAB: /* A string table */
1760 if (hdr->bfd_section != NULL)
1762 if (ehdr->e_shstrndx == shindex)
1764 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1765 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1768 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1771 elf_tdata (abfd)->strtab_hdr = *hdr;
1772 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1775 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1778 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1779 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1780 elf_elfsections (abfd)[shindex] = hdr;
1781 /* We also treat this as a regular section, so that objcopy
1783 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1787 /* If the string table isn't one of the above, then treat it as a
1788 regular section. We need to scan all the headers to be sure,
1789 just in case this strtab section appeared before the above. */
1790 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1792 unsigned int i, num_sec;
1794 num_sec = elf_numsections (abfd);
1795 for (i = 1; i < num_sec; i++)
1797 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1798 if (hdr2->sh_link == shindex)
1800 /* Prevent endless recursion on broken objects. */
1803 if (! bfd_section_from_shdr (abfd, i))
1805 if (elf_onesymtab (abfd) == i)
1807 if (elf_dynsymtab (abfd) == i)
1808 goto dynsymtab_strtab;
1812 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1816 /* *These* do a lot of work -- but build no sections! */
1818 asection *target_sect;
1819 Elf_Internal_Shdr *hdr2, **p_hdr;
1820 unsigned int num_sec = elf_numsections (abfd);
1821 struct bfd_elf_section_data *esdt;
1825 != (bfd_size_type) (hdr->sh_type == SHT_REL
1826 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1829 /* Check for a bogus link to avoid crashing. */
1830 if (hdr->sh_link >= num_sec)
1832 ((*_bfd_error_handler)
1833 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1834 abfd, hdr->sh_link, name, shindex));
1835 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1839 /* For some incomprehensible reason Oracle distributes
1840 libraries for Solaris in which some of the objects have
1841 bogus sh_link fields. It would be nice if we could just
1842 reject them, but, unfortunately, some people need to use
1843 them. We scan through the section headers; if we find only
1844 one suitable symbol table, we clobber the sh_link to point
1845 to it. I hope this doesn't break anything.
1847 Don't do it on executable nor shared library. */
1848 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1849 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1850 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1856 for (scan = 1; scan < num_sec; scan++)
1858 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1859 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1870 hdr->sh_link = found;
1873 /* Get the symbol table. */
1874 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1875 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1876 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1879 /* If this reloc section does not use the main symbol table we
1880 don't treat it as a reloc section. BFD can't adequately
1881 represent such a section, so at least for now, we don't
1882 try. We just present it as a normal section. We also
1883 can't use it as a reloc section if it points to the null
1884 section, an invalid section, another reloc section, or its
1885 sh_link points to the null section. */
1886 if (hdr->sh_link != elf_onesymtab (abfd)
1887 || hdr->sh_link == SHN_UNDEF
1888 || hdr->sh_info == SHN_UNDEF
1889 || hdr->sh_info >= num_sec
1890 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1891 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1892 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1895 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1897 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1898 if (target_sect == NULL)
1901 esdt = elf_section_data (target_sect);
1902 if (hdr->sh_type == SHT_RELA)
1903 p_hdr = &esdt->rela.hdr;
1905 p_hdr = &esdt->rel.hdr;
1907 BFD_ASSERT (*p_hdr == NULL);
1908 amt = sizeof (*hdr2);
1909 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1914 elf_elfsections (abfd)[shindex] = hdr2;
1915 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1916 target_sect->flags |= SEC_RELOC;
1917 target_sect->relocation = NULL;
1918 target_sect->rel_filepos = hdr->sh_offset;
1919 /* In the section to which the relocations apply, mark whether
1920 its relocations are of the REL or RELA variety. */
1921 if (hdr->sh_size != 0)
1923 if (hdr->sh_type == SHT_RELA)
1924 target_sect->use_rela_p = 1;
1926 abfd->flags |= HAS_RELOC;
1930 case SHT_GNU_verdef:
1931 elf_dynverdef (abfd) = shindex;
1932 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1933 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1935 case SHT_GNU_versym:
1936 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1938 elf_dynversym (abfd) = shindex;
1939 elf_tdata (abfd)->dynversym_hdr = *hdr;
1940 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1942 case SHT_GNU_verneed:
1943 elf_dynverref (abfd) = shindex;
1944 elf_tdata (abfd)->dynverref_hdr = *hdr;
1945 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1951 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
1953 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1955 if (hdr->contents != NULL)
1957 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1958 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1961 if (idx->flags & GRP_COMDAT)
1962 hdr->bfd_section->flags
1963 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1965 /* We try to keep the same section order as it comes in. */
1967 while (--n_elt != 0)
1971 if (idx->shdr != NULL
1972 && (s = idx->shdr->bfd_section) != NULL
1973 && elf_next_in_group (s) != NULL)
1975 elf_next_in_group (hdr->bfd_section) = s;
1983 /* Possibly an attributes section. */
1984 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1985 || hdr->sh_type == bed->obj_attrs_section_type)
1987 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1989 _bfd_elf_parse_attributes (abfd, hdr);
1993 /* Check for any processor-specific section types. */
1994 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1997 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1999 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2000 /* FIXME: How to properly handle allocated section reserved
2001 for applications? */
2002 (*_bfd_error_handler)
2003 (_("%B: don't know how to handle allocated, application "
2004 "specific section `%s' [0x%8x]"),
2005 abfd, name, hdr->sh_type);
2007 /* Allow sections reserved for applications. */
2008 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2011 else if (hdr->sh_type >= SHT_LOPROC
2012 && hdr->sh_type <= SHT_HIPROC)
2013 /* FIXME: We should handle this section. */
2014 (*_bfd_error_handler)
2015 (_("%B: don't know how to handle processor specific section "
2017 abfd, name, hdr->sh_type);
2018 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2020 /* Unrecognised OS-specific sections. */
2021 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2022 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2023 required to correctly process the section and the file should
2024 be rejected with an error message. */
2025 (*_bfd_error_handler)
2026 (_("%B: don't know how to handle OS specific section "
2028 abfd, name, hdr->sh_type);
2030 /* Otherwise it should be processed. */
2031 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2034 /* FIXME: We should handle this section. */
2035 (*_bfd_error_handler)
2036 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2037 abfd, name, hdr->sh_type);
2045 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2048 bfd_sym_from_r_symndx (struct sym_cache *cache,
2050 unsigned long r_symndx)
2052 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2054 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2056 Elf_Internal_Shdr *symtab_hdr;
2057 unsigned char esym[sizeof (Elf64_External_Sym)];
2058 Elf_External_Sym_Shndx eshndx;
2060 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2061 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2062 &cache->sym[ent], esym, &eshndx) == NULL)
2065 if (cache->abfd != abfd)
2067 memset (cache->indx, -1, sizeof (cache->indx));
2070 cache->indx[ent] = r_symndx;
2073 return &cache->sym[ent];
2076 /* Given an ELF section number, retrieve the corresponding BFD
2080 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2082 if (sec_index >= elf_numsections (abfd))
2084 return elf_elfsections (abfd)[sec_index]->bfd_section;
2087 static const struct bfd_elf_special_section special_sections_b[] =
2089 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2090 { NULL, 0, 0, 0, 0 }
2093 static const struct bfd_elf_special_section special_sections_c[] =
2095 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2096 { NULL, 0, 0, 0, 0 }
2099 static const struct bfd_elf_special_section special_sections_d[] =
2101 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2102 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2103 /* There are more DWARF sections than these, but they needn't be added here
2104 unless you have to cope with broken compilers that don't emit section
2105 attributes or you want to help the user writing assembler. */
2106 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2107 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2108 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2109 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2110 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2111 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2112 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2113 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2114 { NULL, 0, 0, 0, 0 }
2117 static const struct bfd_elf_special_section special_sections_f[] =
2119 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2120 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2121 { NULL, 0, 0, 0, 0 }
2124 static const struct bfd_elf_special_section special_sections_g[] =
2126 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2127 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2128 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2129 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2130 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2131 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2132 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2133 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2134 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2135 { NULL, 0, 0, 0, 0 }
2138 static const struct bfd_elf_special_section special_sections_h[] =
2140 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2141 { NULL, 0, 0, 0, 0 }
2144 static const struct bfd_elf_special_section special_sections_i[] =
2146 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2147 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2148 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2149 { NULL, 0, 0, 0, 0 }
2152 static const struct bfd_elf_special_section special_sections_l[] =
2154 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2155 { NULL, 0, 0, 0, 0 }
2158 static const struct bfd_elf_special_section special_sections_n[] =
2160 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2161 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2162 { NULL, 0, 0, 0, 0 }
2165 static const struct bfd_elf_special_section special_sections_p[] =
2167 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2168 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2169 { NULL, 0, 0, 0, 0 }
2172 static const struct bfd_elf_special_section special_sections_r[] =
2174 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2175 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2176 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2177 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2178 { NULL, 0, 0, 0, 0 }
2181 static const struct bfd_elf_special_section special_sections_s[] =
2183 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2184 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2185 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2186 /* See struct bfd_elf_special_section declaration for the semantics of
2187 this special case where .prefix_length != strlen (.prefix). */
2188 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2189 { NULL, 0, 0, 0, 0 }
2192 static const struct bfd_elf_special_section special_sections_t[] =
2194 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2195 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2196 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2197 { NULL, 0, 0, 0, 0 }
2200 static const struct bfd_elf_special_section special_sections_z[] =
2202 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2203 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2204 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2205 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2206 { NULL, 0, 0, 0, 0 }
2209 static const struct bfd_elf_special_section * const special_sections[] =
2211 special_sections_b, /* 'b' */
2212 special_sections_c, /* 'c' */
2213 special_sections_d, /* 'd' */
2215 special_sections_f, /* 'f' */
2216 special_sections_g, /* 'g' */
2217 special_sections_h, /* 'h' */
2218 special_sections_i, /* 'i' */
2221 special_sections_l, /* 'l' */
2223 special_sections_n, /* 'n' */
2225 special_sections_p, /* 'p' */
2227 special_sections_r, /* 'r' */
2228 special_sections_s, /* 's' */
2229 special_sections_t, /* 't' */
2235 special_sections_z /* 'z' */
2238 const struct bfd_elf_special_section *
2239 _bfd_elf_get_special_section (const char *name,
2240 const struct bfd_elf_special_section *spec,
2246 len = strlen (name);
2248 for (i = 0; spec[i].prefix != NULL; i++)
2251 int prefix_len = spec[i].prefix_length;
2253 if (len < prefix_len)
2255 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2258 suffix_len = spec[i].suffix_length;
2259 if (suffix_len <= 0)
2261 if (name[prefix_len] != 0)
2263 if (suffix_len == 0)
2265 if (name[prefix_len] != '.'
2266 && (suffix_len == -2
2267 || (rela && spec[i].type == SHT_REL)))
2273 if (len < prefix_len + suffix_len)
2275 if (memcmp (name + len - suffix_len,
2276 spec[i].prefix + prefix_len,
2286 const struct bfd_elf_special_section *
2287 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2290 const struct bfd_elf_special_section *spec;
2291 const struct elf_backend_data *bed;
2293 /* See if this is one of the special sections. */
2294 if (sec->name == NULL)
2297 bed = get_elf_backend_data (abfd);
2298 spec = bed->special_sections;
2301 spec = _bfd_elf_get_special_section (sec->name,
2302 bed->special_sections,
2308 if (sec->name[0] != '.')
2311 i = sec->name[1] - 'b';
2312 if (i < 0 || i > 'z' - 'b')
2315 spec = special_sections[i];
2320 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2324 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2326 struct bfd_elf_section_data *sdata;
2327 const struct elf_backend_data *bed;
2328 const struct bfd_elf_special_section *ssect;
2330 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2333 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2337 sec->used_by_bfd = sdata;
2340 /* Indicate whether or not this section should use RELA relocations. */
2341 bed = get_elf_backend_data (abfd);
2342 sec->use_rela_p = bed->default_use_rela_p;
2344 /* When we read a file, we don't need to set ELF section type and
2345 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2346 anyway. We will set ELF section type and flags for all linker
2347 created sections. If user specifies BFD section flags, we will
2348 set ELF section type and flags based on BFD section flags in
2349 elf_fake_sections. Special handling for .init_array/.fini_array
2350 output sections since they may contain .ctors/.dtors input
2351 sections. We don't want _bfd_elf_init_private_section_data to
2352 copy ELF section type from .ctors/.dtors input sections. */
2353 if (abfd->direction != read_direction
2354 || (sec->flags & SEC_LINKER_CREATED) != 0)
2356 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2359 || (sec->flags & SEC_LINKER_CREATED) != 0
2360 || ssect->type == SHT_INIT_ARRAY
2361 || ssect->type == SHT_FINI_ARRAY))
2363 elf_section_type (sec) = ssect->type;
2364 elf_section_flags (sec) = ssect->attr;
2368 return _bfd_generic_new_section_hook (abfd, sec);
2371 /* Create a new bfd section from an ELF program header.
2373 Since program segments have no names, we generate a synthetic name
2374 of the form segment<NUM>, where NUM is generally the index in the
2375 program header table. For segments that are split (see below) we
2376 generate the names segment<NUM>a and segment<NUM>b.
2378 Note that some program segments may have a file size that is different than
2379 (less than) the memory size. All this means is that at execution the
2380 system must allocate the amount of memory specified by the memory size,
2381 but only initialize it with the first "file size" bytes read from the
2382 file. This would occur for example, with program segments consisting
2383 of combined data+bss.
2385 To handle the above situation, this routine generates TWO bfd sections
2386 for the single program segment. The first has the length specified by
2387 the file size of the segment, and the second has the length specified
2388 by the difference between the two sizes. In effect, the segment is split
2389 into its initialized and uninitialized parts.
2394 _bfd_elf_make_section_from_phdr (bfd *abfd,
2395 Elf_Internal_Phdr *hdr,
2397 const char *type_name)
2405 split = ((hdr->p_memsz > 0)
2406 && (hdr->p_filesz > 0)
2407 && (hdr->p_memsz > hdr->p_filesz));
2409 if (hdr->p_filesz > 0)
2411 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2412 len = strlen (namebuf) + 1;
2413 name = (char *) bfd_alloc (abfd, len);
2416 memcpy (name, namebuf, len);
2417 newsect = bfd_make_section (abfd, name);
2418 if (newsect == NULL)
2420 newsect->vma = hdr->p_vaddr;
2421 newsect->lma = hdr->p_paddr;
2422 newsect->size = hdr->p_filesz;
2423 newsect->filepos = hdr->p_offset;
2424 newsect->flags |= SEC_HAS_CONTENTS;
2425 newsect->alignment_power = bfd_log2 (hdr->p_align);
2426 if (hdr->p_type == PT_LOAD)
2428 newsect->flags |= SEC_ALLOC;
2429 newsect->flags |= SEC_LOAD;
2430 if (hdr->p_flags & PF_X)
2432 /* FIXME: all we known is that it has execute PERMISSION,
2434 newsect->flags |= SEC_CODE;
2437 if (!(hdr->p_flags & PF_W))
2439 newsect->flags |= SEC_READONLY;
2443 if (hdr->p_memsz > hdr->p_filesz)
2447 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2448 len = strlen (namebuf) + 1;
2449 name = (char *) bfd_alloc (abfd, len);
2452 memcpy (name, namebuf, len);
2453 newsect = bfd_make_section (abfd, name);
2454 if (newsect == NULL)
2456 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2457 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2458 newsect->size = hdr->p_memsz - hdr->p_filesz;
2459 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2460 align = newsect->vma & -newsect->vma;
2461 if (align == 0 || align > hdr->p_align)
2462 align = hdr->p_align;
2463 newsect->alignment_power = bfd_log2 (align);
2464 if (hdr->p_type == PT_LOAD)
2466 /* Hack for gdb. Segments that have not been modified do
2467 not have their contents written to a core file, on the
2468 assumption that a debugger can find the contents in the
2469 executable. We flag this case by setting the fake
2470 section size to zero. Note that "real" bss sections will
2471 always have their contents dumped to the core file. */
2472 if (bfd_get_format (abfd) == bfd_core)
2474 newsect->flags |= SEC_ALLOC;
2475 if (hdr->p_flags & PF_X)
2476 newsect->flags |= SEC_CODE;
2478 if (!(hdr->p_flags & PF_W))
2479 newsect->flags |= SEC_READONLY;
2486 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2488 const struct elf_backend_data *bed;
2490 switch (hdr->p_type)
2493 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2496 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2499 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2502 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2505 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2507 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2512 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2515 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2517 case PT_GNU_EH_FRAME:
2518 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2522 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2525 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2528 /* Check for any processor-specific program segment types. */
2529 bed = get_elf_backend_data (abfd);
2530 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2534 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2538 _bfd_elf_single_rel_hdr (asection *sec)
2540 if (elf_section_data (sec)->rel.hdr)
2542 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2543 return elf_section_data (sec)->rel.hdr;
2546 return elf_section_data (sec)->rela.hdr;
2549 /* Allocate and initialize a section-header for a new reloc section,
2550 containing relocations against ASECT. It is stored in RELDATA. If
2551 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2555 _bfd_elf_init_reloc_shdr (bfd *abfd,
2556 struct bfd_elf_section_reloc_data *reldata,
2558 bfd_boolean use_rela_p)
2560 Elf_Internal_Shdr *rel_hdr;
2562 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2565 amt = sizeof (Elf_Internal_Shdr);
2566 BFD_ASSERT (reldata->hdr == NULL);
2567 rel_hdr = bfd_zalloc (abfd, amt);
2568 reldata->hdr = rel_hdr;
2570 amt = sizeof ".rela" + strlen (asect->name);
2571 name = (char *) bfd_alloc (abfd, amt);
2574 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2576 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2578 if (rel_hdr->sh_name == (unsigned int) -1)
2580 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2581 rel_hdr->sh_entsize = (use_rela_p
2582 ? bed->s->sizeof_rela
2583 : bed->s->sizeof_rel);
2584 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2585 rel_hdr->sh_flags = 0;
2586 rel_hdr->sh_addr = 0;
2587 rel_hdr->sh_size = 0;
2588 rel_hdr->sh_offset = 0;
2593 /* Return the default section type based on the passed in section flags. */
2596 bfd_elf_get_default_section_type (flagword flags)
2598 if ((flags & SEC_ALLOC) != 0
2599 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2601 return SHT_PROGBITS;
2604 struct fake_section_arg
2606 struct bfd_link_info *link_info;
2610 /* Set up an ELF internal section header for a section. */
2613 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2615 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2616 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2617 struct bfd_elf_section_data *esd = elf_section_data (asect);
2618 Elf_Internal_Shdr *this_hdr;
2619 unsigned int sh_type;
2623 /* We already failed; just get out of the bfd_map_over_sections
2628 this_hdr = &esd->this_hdr;
2630 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2631 asect->name, FALSE);
2632 if (this_hdr->sh_name == (unsigned int) -1)
2638 /* Don't clear sh_flags. Assembler may set additional bits. */
2640 if ((asect->flags & SEC_ALLOC) != 0
2641 || asect->user_set_vma)
2642 this_hdr->sh_addr = asect->vma;
2644 this_hdr->sh_addr = 0;
2646 this_hdr->sh_offset = 0;
2647 this_hdr->sh_size = asect->size;
2648 this_hdr->sh_link = 0;
2649 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2650 /* The sh_entsize and sh_info fields may have been set already by
2651 copy_private_section_data. */
2653 this_hdr->bfd_section = asect;
2654 this_hdr->contents = NULL;
2656 /* If the section type is unspecified, we set it based on
2658 if ((asect->flags & SEC_GROUP) != 0)
2659 sh_type = SHT_GROUP;
2661 sh_type = bfd_elf_get_default_section_type (asect->flags);
2663 if (this_hdr->sh_type == SHT_NULL)
2664 this_hdr->sh_type = sh_type;
2665 else if (this_hdr->sh_type == SHT_NOBITS
2666 && sh_type == SHT_PROGBITS
2667 && (asect->flags & SEC_ALLOC) != 0)
2669 /* Warn if we are changing a NOBITS section to PROGBITS, but
2670 allow the link to proceed. This can happen when users link
2671 non-bss input sections to bss output sections, or emit data
2672 to a bss output section via a linker script. */
2673 (*_bfd_error_handler)
2674 (_("warning: section `%A' type changed to PROGBITS"), asect);
2675 this_hdr->sh_type = sh_type;
2678 switch (this_hdr->sh_type)
2684 case SHT_INIT_ARRAY:
2685 case SHT_FINI_ARRAY:
2686 case SHT_PREINIT_ARRAY:
2693 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2697 this_hdr->sh_entsize = bed->s->sizeof_sym;
2701 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2705 if (get_elf_backend_data (abfd)->may_use_rela_p)
2706 this_hdr->sh_entsize = bed->s->sizeof_rela;
2710 if (get_elf_backend_data (abfd)->may_use_rel_p)
2711 this_hdr->sh_entsize = bed->s->sizeof_rel;
2714 case SHT_GNU_versym:
2715 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2718 case SHT_GNU_verdef:
2719 this_hdr->sh_entsize = 0;
2720 /* objcopy or strip will copy over sh_info, but may not set
2721 cverdefs. The linker will set cverdefs, but sh_info will be
2723 if (this_hdr->sh_info == 0)
2724 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2726 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2727 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2730 case SHT_GNU_verneed:
2731 this_hdr->sh_entsize = 0;
2732 /* objcopy or strip will copy over sh_info, but may not set
2733 cverrefs. The linker will set cverrefs, but sh_info will be
2735 if (this_hdr->sh_info == 0)
2736 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2738 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2739 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2743 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2747 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2751 if ((asect->flags & SEC_ALLOC) != 0)
2752 this_hdr->sh_flags |= SHF_ALLOC;
2753 if ((asect->flags & SEC_READONLY) == 0)
2754 this_hdr->sh_flags |= SHF_WRITE;
2755 if ((asect->flags & SEC_CODE) != 0)
2756 this_hdr->sh_flags |= SHF_EXECINSTR;
2757 if ((asect->flags & SEC_MERGE) != 0)
2759 this_hdr->sh_flags |= SHF_MERGE;
2760 this_hdr->sh_entsize = asect->entsize;
2761 if ((asect->flags & SEC_STRINGS) != 0)
2762 this_hdr->sh_flags |= SHF_STRINGS;
2764 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2765 this_hdr->sh_flags |= SHF_GROUP;
2766 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2768 this_hdr->sh_flags |= SHF_TLS;
2769 if (asect->size == 0
2770 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2772 struct bfd_link_order *o = asect->map_tail.link_order;
2774 this_hdr->sh_size = 0;
2777 this_hdr->sh_size = o->offset + o->size;
2778 if (this_hdr->sh_size != 0)
2779 this_hdr->sh_type = SHT_NOBITS;
2783 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2784 this_hdr->sh_flags |= SHF_EXCLUDE;
2786 /* If the section has relocs, set up a section header for the
2787 SHT_REL[A] section. If two relocation sections are required for
2788 this section, it is up to the processor-specific back-end to
2789 create the other. */
2790 if ((asect->flags & SEC_RELOC) != 0)
2792 /* When doing a relocatable link, create both REL and RELA sections if
2795 /* Do the normal setup if we wouldn't create any sections here. */
2796 && esd->rel.count + esd->rela.count > 0
2797 && (arg->link_info->relocatable || arg->link_info->emitrelocations))
2799 if (esd->rel.count && esd->rel.hdr == NULL
2800 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
2805 if (esd->rela.count && esd->rela.hdr == NULL
2806 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
2812 else if (!_bfd_elf_init_reloc_shdr (abfd,
2814 ? &esd->rela : &esd->rel),
2820 /* Check for processor-specific section types. */
2821 sh_type = this_hdr->sh_type;
2822 if (bed->elf_backend_fake_sections
2823 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2826 if (sh_type == SHT_NOBITS && asect->size != 0)
2828 /* Don't change the header type from NOBITS if we are being
2829 called for objcopy --only-keep-debug. */
2830 this_hdr->sh_type = sh_type;
2834 /* Fill in the contents of a SHT_GROUP section. Called from
2835 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2836 when ELF targets use the generic linker, ld. Called for ld -r
2837 from bfd_elf_final_link. */
2840 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2842 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2843 asection *elt, *first;
2847 /* Ignore linker created group section. See elfNN_ia64_object_p in
2849 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2853 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2855 unsigned long symindx = 0;
2857 /* elf_group_id will have been set up by objcopy and the
2859 if (elf_group_id (sec) != NULL)
2860 symindx = elf_group_id (sec)->udata.i;
2864 /* If called from the assembler, swap_out_syms will have set up
2865 elf_section_syms. */
2866 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2867 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2869 elf_section_data (sec)->this_hdr.sh_info = symindx;
2871 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2873 /* The ELF backend linker sets sh_info to -2 when the group
2874 signature symbol is global, and thus the index can't be
2875 set until all local symbols are output. */
2876 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2877 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2878 unsigned long symndx = sec_data->this_hdr.sh_info;
2879 unsigned long extsymoff = 0;
2880 struct elf_link_hash_entry *h;
2882 if (!elf_bad_symtab (igroup->owner))
2884 Elf_Internal_Shdr *symtab_hdr;
2886 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2887 extsymoff = symtab_hdr->sh_info;
2889 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2890 while (h->root.type == bfd_link_hash_indirect
2891 || h->root.type == bfd_link_hash_warning)
2892 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2894 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2897 /* The contents won't be allocated for "ld -r" or objcopy. */
2899 if (sec->contents == NULL)
2902 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2904 /* Arrange for the section to be written out. */
2905 elf_section_data (sec)->this_hdr.contents = sec->contents;
2906 if (sec->contents == NULL)
2913 loc = sec->contents + sec->size;
2915 /* Get the pointer to the first section in the group that gas
2916 squirreled away here. objcopy arranges for this to be set to the
2917 start of the input section group. */
2918 first = elt = elf_next_in_group (sec);
2920 /* First element is a flag word. Rest of section is elf section
2921 indices for all the sections of the group. Write them backwards
2922 just to keep the group in the same order as given in .section
2923 directives, not that it matters. */
2930 s = s->output_section;
2932 && !bfd_is_abs_section (s))
2934 unsigned int idx = elf_section_data (s)->this_idx;
2937 H_PUT_32 (abfd, idx, loc);
2939 elt = elf_next_in_group (elt);
2944 if ((loc -= 4) != sec->contents)
2947 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2950 /* Assign all ELF section numbers. The dummy first section is handled here
2951 too. The link/info pointers for the standard section types are filled
2952 in here too, while we're at it. */
2955 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2957 struct elf_obj_tdata *t = elf_tdata (abfd);
2959 unsigned int section_number, secn;
2960 Elf_Internal_Shdr **i_shdrp;
2961 struct bfd_elf_section_data *d;
2962 bfd_boolean need_symtab;
2966 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2968 /* SHT_GROUP sections are in relocatable files only. */
2969 if (link_info == NULL || link_info->relocatable)
2971 /* Put SHT_GROUP sections first. */
2972 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2974 d = elf_section_data (sec);
2976 if (d->this_hdr.sh_type == SHT_GROUP)
2978 if (sec->flags & SEC_LINKER_CREATED)
2980 /* Remove the linker created SHT_GROUP sections. */
2981 bfd_section_list_remove (abfd, sec);
2982 abfd->section_count--;
2985 d->this_idx = section_number++;
2990 for (sec = abfd->sections; sec; sec = sec->next)
2992 d = elf_section_data (sec);
2994 if (d->this_hdr.sh_type != SHT_GROUP)
2995 d->this_idx = section_number++;
2996 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2999 d->rel.idx = section_number++;
3000 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3007 d->rela.idx = section_number++;
3008 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3014 elf_shstrtab_sec (abfd) = section_number++;
3015 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3016 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3018 need_symtab = (bfd_get_symcount (abfd) > 0
3019 || (link_info == NULL
3020 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3024 elf_onesymtab (abfd) = section_number++;
3025 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3026 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3028 elf_symtab_shndx (abfd) = section_number++;
3029 t->symtab_shndx_hdr.sh_name
3030 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3031 ".symtab_shndx", FALSE);
3032 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3035 elf_strtab_sec (abfd) = section_number++;
3036 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3039 if (section_number >= SHN_LORESERVE)
3041 _bfd_error_handler (_("%B: too many sections: %u"),
3042 abfd, section_number);
3046 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3047 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3049 elf_numsections (abfd) = section_number;
3050 elf_elfheader (abfd)->e_shnum = section_number;
3052 /* Set up the list of section header pointers, in agreement with the
3054 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3055 sizeof (Elf_Internal_Shdr *));
3056 if (i_shdrp == NULL)
3059 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3060 sizeof (Elf_Internal_Shdr));
3061 if (i_shdrp[0] == NULL)
3063 bfd_release (abfd, i_shdrp);
3067 elf_elfsections (abfd) = i_shdrp;
3069 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3072 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3073 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3075 i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
3076 t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
3078 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3079 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3082 for (sec = abfd->sections; sec; sec = sec->next)
3087 d = elf_section_data (sec);
3089 i_shdrp[d->this_idx] = &d->this_hdr;
3090 if (d->rel.idx != 0)
3091 i_shdrp[d->rel.idx] = d->rel.hdr;
3092 if (d->rela.idx != 0)
3093 i_shdrp[d->rela.idx] = d->rela.hdr;
3095 /* Fill in the sh_link and sh_info fields while we're at it. */
3097 /* sh_link of a reloc section is the section index of the symbol
3098 table. sh_info is the section index of the section to which
3099 the relocation entries apply. */
3100 if (d->rel.idx != 0)
3102 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3103 d->rel.hdr->sh_info = d->this_idx;
3104 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3106 if (d->rela.idx != 0)
3108 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3109 d->rela.hdr->sh_info = d->this_idx;
3110 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3113 /* We need to set up sh_link for SHF_LINK_ORDER. */
3114 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3116 s = elf_linked_to_section (sec);
3119 /* elf_linked_to_section points to the input section. */
3120 if (link_info != NULL)
3122 /* Check discarded linkonce section. */
3123 if (discarded_section (s))
3126 (*_bfd_error_handler)
3127 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3128 abfd, d->this_hdr.bfd_section,
3130 /* Point to the kept section if it has the same
3131 size as the discarded one. */
3132 kept = _bfd_elf_check_kept_section (s, link_info);
3135 bfd_set_error (bfd_error_bad_value);
3141 s = s->output_section;
3142 BFD_ASSERT (s != NULL);
3146 /* Handle objcopy. */
3147 if (s->output_section == NULL)
3149 (*_bfd_error_handler)
3150 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3151 abfd, d->this_hdr.bfd_section, s, s->owner);
3152 bfd_set_error (bfd_error_bad_value);
3155 s = s->output_section;
3157 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3162 The Intel C compiler generates SHT_IA_64_UNWIND with
3163 SHF_LINK_ORDER. But it doesn't set the sh_link or
3164 sh_info fields. Hence we could get the situation
3166 const struct elf_backend_data *bed
3167 = get_elf_backend_data (abfd);
3168 if (bed->link_order_error_handler)
3169 bed->link_order_error_handler
3170 (_("%B: warning: sh_link not set for section `%A'"),
3175 switch (d->this_hdr.sh_type)
3179 /* A reloc section which we are treating as a normal BFD
3180 section. sh_link is the section index of the symbol
3181 table. sh_info is the section index of the section to
3182 which the relocation entries apply. We assume that an
3183 allocated reloc section uses the dynamic symbol table.
3184 FIXME: How can we be sure? */
3185 s = bfd_get_section_by_name (abfd, ".dynsym");
3187 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3189 /* We look up the section the relocs apply to by name. */
3191 if (d->this_hdr.sh_type == SHT_REL)
3195 s = bfd_get_section_by_name (abfd, name);
3198 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3199 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3204 /* We assume that a section named .stab*str is a stabs
3205 string section. We look for a section with the same name
3206 but without the trailing ``str'', and set its sh_link
3207 field to point to this section. */
3208 if (CONST_STRNEQ (sec->name, ".stab")
3209 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3214 len = strlen (sec->name);
3215 alc = (char *) bfd_malloc (len - 2);
3218 memcpy (alc, sec->name, len - 3);
3219 alc[len - 3] = '\0';
3220 s = bfd_get_section_by_name (abfd, alc);
3224 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3226 /* This is a .stab section. */
3227 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3228 elf_section_data (s)->this_hdr.sh_entsize
3229 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3236 case SHT_GNU_verneed:
3237 case SHT_GNU_verdef:
3238 /* sh_link is the section header index of the string table
3239 used for the dynamic entries, or the symbol table, or the
3241 s = bfd_get_section_by_name (abfd, ".dynstr");
3243 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3246 case SHT_GNU_LIBLIST:
3247 /* sh_link is the section header index of the prelink library
3248 list used for the dynamic entries, or the symbol table, or
3249 the version strings. */
3250 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3251 ? ".dynstr" : ".gnu.libstr");
3253 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3258 case SHT_GNU_versym:
3259 /* sh_link is the section header index of the symbol table
3260 this hash table or version table is for. */
3261 s = bfd_get_section_by_name (abfd, ".dynsym");
3263 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3267 d->this_hdr.sh_link = elf_onesymtab (abfd);
3271 for (secn = 1; secn < section_number; ++secn)
3272 if (i_shdrp[secn] == NULL)
3273 i_shdrp[secn] = i_shdrp[0];
3275 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3276 i_shdrp[secn]->sh_name);
3281 sym_is_global (bfd *abfd, asymbol *sym)
3283 /* If the backend has a special mapping, use it. */
3284 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3285 if (bed->elf_backend_sym_is_global)
3286 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3288 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3289 || bfd_is_und_section (bfd_get_section (sym))
3290 || bfd_is_com_section (bfd_get_section (sym)));
3293 /* Don't output section symbols for sections that are not going to be
3294 output, that are duplicates or there is no BFD section. */
3297 ignore_section_sym (bfd *abfd, asymbol *sym)
3299 elf_symbol_type *type_ptr;
3301 if ((sym->flags & BSF_SECTION_SYM) == 0)
3304 type_ptr = elf_symbol_from (abfd, sym);
3305 return ((type_ptr != NULL
3306 && type_ptr->internal_elf_sym.st_shndx != 0
3307 && bfd_is_abs_section (sym->section))
3308 || !(sym->section->owner == abfd
3309 || (sym->section->output_section->owner == abfd
3310 && sym->section->output_offset == 0)
3311 || bfd_is_abs_section (sym->section)));
3314 /* Map symbol from it's internal number to the external number, moving
3315 all local symbols to be at the head of the list. */
3318 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3320 unsigned int symcount = bfd_get_symcount (abfd);
3321 asymbol **syms = bfd_get_outsymbols (abfd);
3322 asymbol **sect_syms;
3323 unsigned int num_locals = 0;
3324 unsigned int num_globals = 0;
3325 unsigned int num_locals2 = 0;
3326 unsigned int num_globals2 = 0;
3333 fprintf (stderr, "elf_map_symbols\n");
3337 for (asect = abfd->sections; asect; asect = asect->next)
3339 if (max_index < asect->index)
3340 max_index = asect->index;
3344 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3345 if (sect_syms == NULL)
3347 elf_section_syms (abfd) = sect_syms;
3348 elf_num_section_syms (abfd) = max_index;
3350 /* Init sect_syms entries for any section symbols we have already
3351 decided to output. */
3352 for (idx = 0; idx < symcount; idx++)
3354 asymbol *sym = syms[idx];
3356 if ((sym->flags & BSF_SECTION_SYM) != 0
3358 && !ignore_section_sym (abfd, sym)
3359 && !bfd_is_abs_section (sym->section))
3361 asection *sec = sym->section;
3363 if (sec->owner != abfd)
3364 sec = sec->output_section;
3366 sect_syms[sec->index] = syms[idx];
3370 /* Classify all of the symbols. */
3371 for (idx = 0; idx < symcount; idx++)
3373 if (sym_is_global (abfd, syms[idx]))
3375 else if (!ignore_section_sym (abfd, syms[idx]))
3379 /* We will be adding a section symbol for each normal BFD section. Most
3380 sections will already have a section symbol in outsymbols, but
3381 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3382 at least in that case. */
3383 for (asect = abfd->sections; asect; asect = asect->next)
3385 if (sect_syms[asect->index] == NULL)
3387 if (!sym_is_global (abfd, asect->symbol))
3394 /* Now sort the symbols so the local symbols are first. */
3395 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3396 sizeof (asymbol *));
3398 if (new_syms == NULL)
3401 for (idx = 0; idx < symcount; idx++)
3403 asymbol *sym = syms[idx];
3406 if (sym_is_global (abfd, sym))
3407 i = num_locals + num_globals2++;
3408 else if (!ignore_section_sym (abfd, sym))
3413 sym->udata.i = i + 1;
3415 for (asect = abfd->sections; asect; asect = asect->next)
3417 if (sect_syms[asect->index] == NULL)
3419 asymbol *sym = asect->symbol;
3422 sect_syms[asect->index] = sym;
3423 if (!sym_is_global (abfd, sym))
3426 i = num_locals + num_globals2++;
3428 sym->udata.i = i + 1;
3432 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3434 *pnum_locals = num_locals;
3438 /* Align to the maximum file alignment that could be required for any
3439 ELF data structure. */
3441 static inline file_ptr
3442 align_file_position (file_ptr off, int align)
3444 return (off + align - 1) & ~(align - 1);
3447 /* Assign a file position to a section, optionally aligning to the
3448 required section alignment. */
3451 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3455 if (align && i_shdrp->sh_addralign > 1)
3456 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3457 i_shdrp->sh_offset = offset;
3458 if (i_shdrp->bfd_section != NULL)
3459 i_shdrp->bfd_section->filepos = offset;
3460 if (i_shdrp->sh_type != SHT_NOBITS)
3461 offset += i_shdrp->sh_size;
3465 /* Compute the file positions we are going to put the sections at, and
3466 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3467 is not NULL, this is being called by the ELF backend linker. */
3470 _bfd_elf_compute_section_file_positions (bfd *abfd,
3471 struct bfd_link_info *link_info)
3473 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3474 struct fake_section_arg fsargs;
3476 struct bfd_strtab_hash *strtab = NULL;
3477 Elf_Internal_Shdr *shstrtab_hdr;
3478 bfd_boolean need_symtab;
3480 if (abfd->output_has_begun)
3483 /* Do any elf backend specific processing first. */
3484 if (bed->elf_backend_begin_write_processing)
3485 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3487 if (! prep_headers (abfd))
3490 /* Post process the headers if necessary. */
3491 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3493 fsargs.failed = FALSE;
3494 fsargs.link_info = link_info;
3495 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3499 if (!assign_section_numbers (abfd, link_info))
3502 /* The backend linker builds symbol table information itself. */
3503 need_symtab = (link_info == NULL
3504 && (bfd_get_symcount (abfd) > 0
3505 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3509 /* Non-zero if doing a relocatable link. */
3510 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3512 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3517 if (link_info == NULL)
3519 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3524 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3525 /* sh_name was set in prep_headers. */
3526 shstrtab_hdr->sh_type = SHT_STRTAB;
3527 shstrtab_hdr->sh_flags = 0;
3528 shstrtab_hdr->sh_addr = 0;
3529 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3530 shstrtab_hdr->sh_entsize = 0;
3531 shstrtab_hdr->sh_link = 0;
3532 shstrtab_hdr->sh_info = 0;
3533 /* sh_offset is set in assign_file_positions_except_relocs. */
3534 shstrtab_hdr->sh_addralign = 1;
3536 if (!assign_file_positions_except_relocs (abfd, link_info))
3542 Elf_Internal_Shdr *hdr;
3544 off = elf_next_file_pos (abfd);
3546 hdr = &elf_tdata (abfd)->symtab_hdr;
3547 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3549 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3550 if (hdr->sh_size != 0)
3551 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3553 hdr = &elf_tdata (abfd)->strtab_hdr;
3554 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3556 elf_next_file_pos (abfd) = off;
3558 /* Now that we know where the .strtab section goes, write it
3560 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3561 || ! _bfd_stringtab_emit (abfd, strtab))
3563 _bfd_stringtab_free (strtab);
3566 abfd->output_has_begun = TRUE;
3571 /* Make an initial estimate of the size of the program header. If we
3572 get the number wrong here, we'll redo section placement. */
3574 static bfd_size_type
3575 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3579 const struct elf_backend_data *bed;
3581 /* Assume we will need exactly two PT_LOAD segments: one for text
3582 and one for data. */
3585 s = bfd_get_section_by_name (abfd, ".interp");
3586 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3588 /* If we have a loadable interpreter section, we need a
3589 PT_INTERP segment. In this case, assume we also need a
3590 PT_PHDR segment, although that may not be true for all
3595 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3597 /* We need a PT_DYNAMIC segment. */
3601 if (info != NULL && info->relro)
3603 /* We need a PT_GNU_RELRO segment. */
3607 if (elf_eh_frame_hdr (abfd))
3609 /* We need a PT_GNU_EH_FRAME segment. */
3613 if (elf_stack_flags (abfd))
3615 /* We need a PT_GNU_STACK segment. */
3619 for (s = abfd->sections; s != NULL; s = s->next)
3621 if ((s->flags & SEC_LOAD) != 0
3622 && CONST_STRNEQ (s->name, ".note"))
3624 /* We need a PT_NOTE segment. */
3626 /* Try to create just one PT_NOTE segment
3627 for all adjacent loadable .note* sections.
3628 gABI requires that within a PT_NOTE segment
3629 (and also inside of each SHT_NOTE section)
3630 each note is padded to a multiple of 4 size,
3631 so we check whether the sections are correctly
3633 if (s->alignment_power == 2)
3634 while (s->next != NULL
3635 && s->next->alignment_power == 2
3636 && (s->next->flags & SEC_LOAD) != 0
3637 && CONST_STRNEQ (s->next->name, ".note"))
3642 for (s = abfd->sections; s != NULL; s = s->next)
3644 if (s->flags & SEC_THREAD_LOCAL)
3646 /* We need a PT_TLS segment. */
3652 /* Let the backend count up any program headers it might need. */
3653 bed = get_elf_backend_data (abfd);
3654 if (bed->elf_backend_additional_program_headers)
3658 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3664 return segs * bed->s->sizeof_phdr;
3667 /* Find the segment that contains the output_section of section. */
3670 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3672 struct elf_segment_map *m;
3673 Elf_Internal_Phdr *p;
3675 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
3681 for (i = m->count - 1; i >= 0; i--)
3682 if (m->sections[i] == section)
3689 /* Create a mapping from a set of sections to a program segment. */
3691 static struct elf_segment_map *
3692 make_mapping (bfd *abfd,
3693 asection **sections,
3698 struct elf_segment_map *m;
3703 amt = sizeof (struct elf_segment_map);
3704 amt += (to - from - 1) * sizeof (asection *);
3705 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3709 m->p_type = PT_LOAD;
3710 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3711 m->sections[i - from] = *hdrpp;
3712 m->count = to - from;
3714 if (from == 0 && phdr)
3716 /* Include the headers in the first PT_LOAD segment. */
3717 m->includes_filehdr = 1;
3718 m->includes_phdrs = 1;
3724 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3727 struct elf_segment_map *
3728 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3730 struct elf_segment_map *m;
3732 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3733 sizeof (struct elf_segment_map));
3737 m->p_type = PT_DYNAMIC;
3739 m->sections[0] = dynsec;
3744 /* Possibly add or remove segments from the segment map. */
3747 elf_modify_segment_map (bfd *abfd,
3748 struct bfd_link_info *info,
3749 bfd_boolean remove_empty_load)
3751 struct elf_segment_map **m;
3752 const struct elf_backend_data *bed;
3754 /* The placement algorithm assumes that non allocated sections are
3755 not in PT_LOAD segments. We ensure this here by removing such
3756 sections from the segment map. We also remove excluded
3757 sections. Finally, any PT_LOAD segment without sections is
3759 m = &elf_seg_map (abfd);
3762 unsigned int i, new_count;
3764 for (new_count = 0, i = 0; i < (*m)->count; i++)
3766 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3767 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3768 || (*m)->p_type != PT_LOAD))
3770 (*m)->sections[new_count] = (*m)->sections[i];
3774 (*m)->count = new_count;
3776 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3782 bed = get_elf_backend_data (abfd);
3783 if (bed->elf_backend_modify_segment_map != NULL)
3785 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3792 /* Set up a mapping from BFD sections to program segments. */
3795 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3798 struct elf_segment_map *m;
3799 asection **sections = NULL;
3800 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3801 bfd_boolean no_user_phdrs;
3803 no_user_phdrs = elf_seg_map (abfd) == NULL;
3806 info->user_phdrs = !no_user_phdrs;
3808 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3812 struct elf_segment_map *mfirst;
3813 struct elf_segment_map **pm;
3816 unsigned int phdr_index;
3817 bfd_vma maxpagesize;
3819 bfd_boolean phdr_in_segment = TRUE;
3820 bfd_boolean writable;
3822 asection *first_tls = NULL;
3823 asection *dynsec, *eh_frame_hdr;
3825 bfd_vma addr_mask, wrap_to = 0;
3827 /* Select the allocated sections, and sort them. */
3829 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3830 sizeof (asection *));
3831 if (sections == NULL)
3834 /* Calculate top address, avoiding undefined behaviour of shift
3835 left operator when shift count is equal to size of type
3837 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3838 addr_mask = (addr_mask << 1) + 1;
3841 for (s = abfd->sections; s != NULL; s = s->next)
3843 if ((s->flags & SEC_ALLOC) != 0)
3847 /* A wrapping section potentially clashes with header. */
3848 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3849 wrap_to = (s->lma + s->size) & addr_mask;
3852 BFD_ASSERT (i <= bfd_count_sections (abfd));
3855 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3857 /* Build the mapping. */
3862 /* If we have a .interp section, then create a PT_PHDR segment for
3863 the program headers and a PT_INTERP segment for the .interp
3865 s = bfd_get_section_by_name (abfd, ".interp");
3866 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3868 amt = sizeof (struct elf_segment_map);
3869 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3873 m->p_type = PT_PHDR;
3874 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3875 m->p_flags = PF_R | PF_X;
3876 m->p_flags_valid = 1;
3877 m->includes_phdrs = 1;
3882 amt = sizeof (struct elf_segment_map);
3883 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3887 m->p_type = PT_INTERP;
3895 /* Look through the sections. We put sections in the same program
3896 segment when the start of the second section can be placed within
3897 a few bytes of the end of the first section. */
3901 maxpagesize = bed->maxpagesize;
3903 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3905 && (dynsec->flags & SEC_LOAD) == 0)
3908 /* Deal with -Ttext or something similar such that the first section
3909 is not adjacent to the program headers. This is an
3910 approximation, since at this point we don't know exactly how many
3911 program headers we will need. */
3914 bfd_size_type phdr_size = elf_program_header_size (abfd);
3916 if (phdr_size == (bfd_size_type) -1)
3917 phdr_size = get_program_header_size (abfd, info);
3918 phdr_size += bed->s->sizeof_ehdr;
3919 if ((abfd->flags & D_PAGED) == 0
3920 || (sections[0]->lma & addr_mask) < phdr_size
3921 || ((sections[0]->lma & addr_mask) % maxpagesize
3922 < phdr_size % maxpagesize)
3923 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
3924 phdr_in_segment = FALSE;
3927 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3930 bfd_boolean new_segment;
3934 /* See if this section and the last one will fit in the same
3937 if (last_hdr == NULL)
3939 /* If we don't have a segment yet, then we don't need a new
3940 one (we build the last one after this loop). */
3941 new_segment = FALSE;
3943 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3945 /* If this section has a different relation between the
3946 virtual address and the load address, then we need a new
3950 else if (hdr->lma < last_hdr->lma + last_size
3951 || last_hdr->lma + last_size < last_hdr->lma)
3953 /* If this section has a load address that makes it overlap
3954 the previous section, then we need a new segment. */
3957 /* In the next test we have to be careful when last_hdr->lma is close
3958 to the end of the address space. If the aligned address wraps
3959 around to the start of the address space, then there are no more
3960 pages left in memory and it is OK to assume that the current
3961 section can be included in the current segment. */
3962 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3964 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3967 /* If putting this section in this segment would force us to
3968 skip a page in the segment, then we need a new segment. */
3971 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3972 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3974 /* We don't want to put a loadable section after a
3975 nonloadable section in the same segment.
3976 Consider .tbss sections as loadable for this purpose. */
3979 else if ((abfd->flags & D_PAGED) == 0)
3981 /* If the file is not demand paged, which means that we
3982 don't require the sections to be correctly aligned in the
3983 file, then there is no other reason for a new segment. */
3984 new_segment = FALSE;
3987 && (hdr->flags & SEC_READONLY) == 0
3988 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
3989 != (hdr->lma & -maxpagesize)))
3991 /* We don't want to put a writable section in a read only
3992 segment, unless they are on the same page in memory
3993 anyhow. We already know that the last section does not
3994 bring us past the current section on the page, so the
3995 only case in which the new section is not on the same
3996 page as the previous section is when the previous section
3997 ends precisely on a page boundary. */
4002 /* Otherwise, we can use the same segment. */
4003 new_segment = FALSE;
4006 /* Allow interested parties a chance to override our decision. */
4007 if (last_hdr != NULL
4009 && info->callbacks->override_segment_assignment != NULL)
4011 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4017 if ((hdr->flags & SEC_READONLY) == 0)
4020 /* .tbss sections effectively have zero size. */
4021 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4022 != SEC_THREAD_LOCAL)
4023 last_size = hdr->size;
4029 /* We need a new program segment. We must create a new program
4030 header holding all the sections from phdr_index until hdr. */
4032 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4039 if ((hdr->flags & SEC_READONLY) == 0)
4045 /* .tbss sections effectively have zero size. */
4046 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4047 last_size = hdr->size;
4051 phdr_in_segment = FALSE;
4054 /* Create a final PT_LOAD program segment, but not if it's just
4056 if (last_hdr != NULL
4057 && (i - phdr_index != 1
4058 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4059 != SEC_THREAD_LOCAL)))
4061 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4069 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4072 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4079 /* For each batch of consecutive loadable .note sections,
4080 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4081 because if we link together nonloadable .note sections and
4082 loadable .note sections, we will generate two .note sections
4083 in the output file. FIXME: Using names for section types is
4085 for (s = abfd->sections; s != NULL; s = s->next)
4087 if ((s->flags & SEC_LOAD) != 0
4088 && CONST_STRNEQ (s->name, ".note"))
4093 amt = sizeof (struct elf_segment_map);
4094 if (s->alignment_power == 2)
4095 for (s2 = s; s2->next != NULL; s2 = s2->next)
4097 if (s2->next->alignment_power == 2
4098 && (s2->next->flags & SEC_LOAD) != 0
4099 && CONST_STRNEQ (s2->next->name, ".note")
4100 && align_power (s2->lma + s2->size, 2)
4106 amt += (count - 1) * sizeof (asection *);
4107 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4111 m->p_type = PT_NOTE;
4115 m->sections[m->count - count--] = s;
4116 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4119 m->sections[m->count - 1] = s;
4120 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4124 if (s->flags & SEC_THREAD_LOCAL)
4132 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4135 amt = sizeof (struct elf_segment_map);
4136 amt += (tls_count - 1) * sizeof (asection *);
4137 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4142 m->count = tls_count;
4143 /* Mandated PF_R. */
4145 m->p_flags_valid = 1;
4147 for (i = 0; i < (unsigned int) tls_count; ++i)
4149 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4152 (_("%B: TLS sections are not adjacent:"), abfd);
4155 while (i < (unsigned int) tls_count)
4157 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4159 _bfd_error_handler (_(" TLS: %A"), s);
4163 _bfd_error_handler (_(" non-TLS: %A"), s);
4166 bfd_set_error (bfd_error_bad_value);
4177 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4179 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4180 if (eh_frame_hdr != NULL
4181 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4183 amt = sizeof (struct elf_segment_map);
4184 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4188 m->p_type = PT_GNU_EH_FRAME;
4190 m->sections[0] = eh_frame_hdr->output_section;
4196 if (elf_stack_flags (abfd))
4198 amt = sizeof (struct elf_segment_map);
4199 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4203 m->p_type = PT_GNU_STACK;
4204 m->p_flags = elf_stack_flags (abfd);
4205 m->p_align = bed->stack_align;
4206 m->p_flags_valid = 1;
4207 m->p_align_valid = m->p_align != 0;
4208 if (info->stacksize > 0)
4210 m->p_size = info->stacksize;
4211 m->p_size_valid = 1;
4218 if (info != NULL && info->relro)
4220 for (m = mfirst; m != NULL; m = m->next)
4222 if (m->p_type == PT_LOAD
4224 && m->sections[0]->vma >= info->relro_start
4225 && m->sections[0]->vma < info->relro_end)
4228 while (--i != (unsigned) -1)
4229 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4230 == (SEC_LOAD | SEC_HAS_CONTENTS))
4233 if (i != (unsigned) -1)
4238 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4241 amt = sizeof (struct elf_segment_map);
4242 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4246 m->p_type = PT_GNU_RELRO;
4248 m->p_flags_valid = 1;
4256 elf_seg_map (abfd) = mfirst;
4259 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4262 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4264 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4269 if (sections != NULL)
4274 /* Sort sections by address. */
4277 elf_sort_sections (const void *arg1, const void *arg2)
4279 const asection *sec1 = *(const asection **) arg1;
4280 const asection *sec2 = *(const asection **) arg2;
4281 bfd_size_type size1, size2;
4283 /* Sort by LMA first, since this is the address used to
4284 place the section into a segment. */
4285 if (sec1->lma < sec2->lma)
4287 else if (sec1->lma > sec2->lma)
4290 /* Then sort by VMA. Normally the LMA and the VMA will be
4291 the same, and this will do nothing. */
4292 if (sec1->vma < sec2->vma)
4294 else if (sec1->vma > sec2->vma)
4297 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4299 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4305 /* If the indicies are the same, do not return 0
4306 here, but continue to try the next comparison. */
4307 if (sec1->target_index - sec2->target_index != 0)
4308 return sec1->target_index - sec2->target_index;
4313 else if (TOEND (sec2))
4318 /* Sort by size, to put zero sized sections
4319 before others at the same address. */
4321 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4322 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4329 return sec1->target_index - sec2->target_index;
4332 /* Ian Lance Taylor writes:
4334 We shouldn't be using % with a negative signed number. That's just
4335 not good. We have to make sure either that the number is not
4336 negative, or that the number has an unsigned type. When the types
4337 are all the same size they wind up as unsigned. When file_ptr is a
4338 larger signed type, the arithmetic winds up as signed long long,
4341 What we're trying to say here is something like ``increase OFF by
4342 the least amount that will cause it to be equal to the VMA modulo
4344 /* In other words, something like:
4346 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4347 off_offset = off % bed->maxpagesize;
4348 if (vma_offset < off_offset)
4349 adjustment = vma_offset + bed->maxpagesize - off_offset;
4351 adjustment = vma_offset - off_offset;
4353 which can can be collapsed into the expression below. */
4356 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4358 /* PR binutils/16199: Handle an alignment of zero. */
4359 if (maxpagesize == 0)
4361 return ((vma - off) % maxpagesize);
4365 print_segment_map (const struct elf_segment_map *m)
4368 const char *pt = get_segment_type (m->p_type);
4373 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4374 sprintf (buf, "LOPROC+%7.7x",
4375 (unsigned int) (m->p_type - PT_LOPROC));
4376 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4377 sprintf (buf, "LOOS+%7.7x",
4378 (unsigned int) (m->p_type - PT_LOOS));
4380 snprintf (buf, sizeof (buf), "%8.8x",
4381 (unsigned int) m->p_type);
4385 fprintf (stderr, "%s:", pt);
4386 for (j = 0; j < m->count; j++)
4387 fprintf (stderr, " %s", m->sections [j]->name);
4393 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4398 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4400 buf = bfd_zmalloc (len);
4403 ret = bfd_bwrite (buf, len, abfd) == len;
4408 /* Assign file positions to the sections based on the mapping from
4409 sections to segments. This function also sets up some fields in
4413 assign_file_positions_for_load_sections (bfd *abfd,
4414 struct bfd_link_info *link_info)
4416 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4417 struct elf_segment_map *m;
4418 Elf_Internal_Phdr *phdrs;
4419 Elf_Internal_Phdr *p;
4421 bfd_size_type maxpagesize;
4424 bfd_vma header_pad = 0;
4426 if (link_info == NULL
4427 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4431 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4435 header_pad = m->header_size;
4440 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4441 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4445 /* PR binutils/12467. */
4446 elf_elfheader (abfd)->e_phoff = 0;
4447 elf_elfheader (abfd)->e_phentsize = 0;
4450 elf_elfheader (abfd)->e_phnum = alloc;
4452 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4453 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4455 BFD_ASSERT (elf_program_header_size (abfd)
4456 >= alloc * bed->s->sizeof_phdr);
4460 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4464 /* We're writing the size in elf_program_header_size (abfd),
4465 see assign_file_positions_except_relocs, so make sure we have
4466 that amount allocated, with trailing space cleared.
4467 The variable alloc contains the computed need, while
4468 elf_program_header_size (abfd) contains the size used for the
4470 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4471 where the layout is forced to according to a larger size in the
4472 last iterations for the testcase ld-elf/header. */
4473 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4475 phdrs = (Elf_Internal_Phdr *)
4477 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4478 sizeof (Elf_Internal_Phdr));
4479 elf_tdata (abfd)->phdr = phdrs;
4484 if ((abfd->flags & D_PAGED) != 0)
4485 maxpagesize = bed->maxpagesize;
4487 off = bed->s->sizeof_ehdr;
4488 off += alloc * bed->s->sizeof_phdr;
4489 if (header_pad < (bfd_vma) off)
4495 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4497 m = m->next, p++, j++)
4501 bfd_boolean no_contents;
4503 /* If elf_segment_map is not from map_sections_to_segments, the
4504 sections may not be correctly ordered. NOTE: sorting should
4505 not be done to the PT_NOTE section of a corefile, which may
4506 contain several pseudo-sections artificially created by bfd.
4507 Sorting these pseudo-sections breaks things badly. */
4509 && !(elf_elfheader (abfd)->e_type == ET_CORE
4510 && m->p_type == PT_NOTE))
4511 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4514 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4515 number of sections with contents contributing to both p_filesz
4516 and p_memsz, followed by a number of sections with no contents
4517 that just contribute to p_memsz. In this loop, OFF tracks next
4518 available file offset for PT_LOAD and PT_NOTE segments. */
4519 p->p_type = m->p_type;
4520 p->p_flags = m->p_flags;
4525 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4527 if (m->p_paddr_valid)
4528 p->p_paddr = m->p_paddr;
4529 else if (m->count == 0)
4532 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4534 if (p->p_type == PT_LOAD
4535 && (abfd->flags & D_PAGED) != 0)
4537 /* p_align in demand paged PT_LOAD segments effectively stores
4538 the maximum page size. When copying an executable with
4539 objcopy, we set m->p_align from the input file. Use this
4540 value for maxpagesize rather than bed->maxpagesize, which
4541 may be different. Note that we use maxpagesize for PT_TLS
4542 segment alignment later in this function, so we are relying
4543 on at least one PT_LOAD segment appearing before a PT_TLS
4545 if (m->p_align_valid)
4546 maxpagesize = m->p_align;
4548 p->p_align = maxpagesize;
4550 else if (m->p_align_valid)
4551 p->p_align = m->p_align;
4552 else if (m->count == 0)
4553 p->p_align = 1 << bed->s->log_file_align;
4557 no_contents = FALSE;
4559 if (p->p_type == PT_LOAD
4562 bfd_size_type align;
4563 unsigned int align_power = 0;
4565 if (m->p_align_valid)
4569 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4571 unsigned int secalign;
4573 secalign = bfd_get_section_alignment (abfd, *secpp);
4574 if (secalign > align_power)
4575 align_power = secalign;
4577 align = (bfd_size_type) 1 << align_power;
4578 if (align < maxpagesize)
4579 align = maxpagesize;
4582 for (i = 0; i < m->count; i++)
4583 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4584 /* If we aren't making room for this section, then
4585 it must be SHT_NOBITS regardless of what we've
4586 set via struct bfd_elf_special_section. */
4587 elf_section_type (m->sections[i]) = SHT_NOBITS;
4589 /* Find out whether this segment contains any loadable
4592 for (i = 0; i < m->count; i++)
4593 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4595 no_contents = FALSE;
4599 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4603 /* We shouldn't need to align the segment on disk since
4604 the segment doesn't need file space, but the gABI
4605 arguably requires the alignment and glibc ld.so
4606 checks it. So to comply with the alignment
4607 requirement but not waste file space, we adjust
4608 p_offset for just this segment. (OFF_ADJUST is
4609 subtracted from OFF later.) This may put p_offset
4610 past the end of file, but that shouldn't matter. */
4615 /* Make sure the .dynamic section is the first section in the
4616 PT_DYNAMIC segment. */
4617 else if (p->p_type == PT_DYNAMIC
4619 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4622 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4624 bfd_set_error (bfd_error_bad_value);
4627 /* Set the note section type to SHT_NOTE. */
4628 else if (p->p_type == PT_NOTE)
4629 for (i = 0; i < m->count; i++)
4630 elf_section_type (m->sections[i]) = SHT_NOTE;
4636 if (m->includes_filehdr)
4638 if (!m->p_flags_valid)
4640 p->p_filesz = bed->s->sizeof_ehdr;
4641 p->p_memsz = bed->s->sizeof_ehdr;
4644 if (p->p_vaddr < (bfd_vma) off)
4646 (*_bfd_error_handler)
4647 (_("%B: Not enough room for program headers, try linking with -N"),
4649 bfd_set_error (bfd_error_bad_value);
4654 if (!m->p_paddr_valid)
4659 if (m->includes_phdrs)
4661 if (!m->p_flags_valid)
4664 if (!m->includes_filehdr)
4666 p->p_offset = bed->s->sizeof_ehdr;
4670 p->p_vaddr -= off - p->p_offset;
4671 if (!m->p_paddr_valid)
4672 p->p_paddr -= off - p->p_offset;
4676 p->p_filesz += alloc * bed->s->sizeof_phdr;
4677 p->p_memsz += alloc * bed->s->sizeof_phdr;
4680 p->p_filesz += header_pad;
4681 p->p_memsz += header_pad;
4685 if (p->p_type == PT_LOAD
4686 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4688 if (!m->includes_filehdr && !m->includes_phdrs)
4694 adjust = off - (p->p_offset + p->p_filesz);
4696 p->p_filesz += adjust;
4697 p->p_memsz += adjust;
4701 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4702 maps. Set filepos for sections in PT_LOAD segments, and in
4703 core files, for sections in PT_NOTE segments.
4704 assign_file_positions_for_non_load_sections will set filepos
4705 for other sections and update p_filesz for other segments. */
4706 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4709 bfd_size_type align;
4710 Elf_Internal_Shdr *this_hdr;
4713 this_hdr = &elf_section_data (sec)->this_hdr;
4714 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4716 if ((p->p_type == PT_LOAD
4717 || p->p_type == PT_TLS)
4718 && (this_hdr->sh_type != SHT_NOBITS
4719 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4720 && ((this_hdr->sh_flags & SHF_TLS) == 0
4721 || p->p_type == PT_TLS))))
4723 bfd_vma p_start = p->p_paddr;
4724 bfd_vma p_end = p_start + p->p_memsz;
4725 bfd_vma s_start = sec->lma;
4726 bfd_vma adjust = s_start - p_end;
4730 || p_end < p_start))
4732 (*_bfd_error_handler)
4733 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4734 (unsigned long) s_start, (unsigned long) p_end);
4738 p->p_memsz += adjust;
4740 if (this_hdr->sh_type != SHT_NOBITS)
4742 if (p->p_filesz + adjust < p->p_memsz)
4744 /* We have a PROGBITS section following NOBITS ones.
4745 Allocate file space for the NOBITS section(s) and
4747 adjust = p->p_memsz - p->p_filesz;
4748 if (!write_zeros (abfd, off, adjust))
4752 p->p_filesz += adjust;
4756 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4758 /* The section at i == 0 is the one that actually contains
4762 this_hdr->sh_offset = sec->filepos = off;
4763 off += this_hdr->sh_size;
4764 p->p_filesz = this_hdr->sh_size;
4770 /* The rest are fake sections that shouldn't be written. */
4779 if (p->p_type == PT_LOAD)
4781 this_hdr->sh_offset = sec->filepos = off;
4782 if (this_hdr->sh_type != SHT_NOBITS)
4783 off += this_hdr->sh_size;
4785 else if (this_hdr->sh_type == SHT_NOBITS
4786 && (this_hdr->sh_flags & SHF_TLS) != 0
4787 && this_hdr->sh_offset == 0)
4789 /* This is a .tbss section that didn't get a PT_LOAD.
4790 (See _bfd_elf_map_sections_to_segments "Create a
4791 final PT_LOAD".) Set sh_offset to the value it
4792 would have if we had created a zero p_filesz and
4793 p_memsz PT_LOAD header for the section. This
4794 also makes the PT_TLS header have the same
4796 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
4798 this_hdr->sh_offset = sec->filepos = off + adjust;
4801 if (this_hdr->sh_type != SHT_NOBITS)
4803 p->p_filesz += this_hdr->sh_size;
4804 /* A load section without SHF_ALLOC is something like
4805 a note section in a PT_NOTE segment. These take
4806 file space but are not loaded into memory. */
4807 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4808 p->p_memsz += this_hdr->sh_size;
4810 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4812 if (p->p_type == PT_TLS)
4813 p->p_memsz += this_hdr->sh_size;
4815 /* .tbss is special. It doesn't contribute to p_memsz of
4817 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4818 p->p_memsz += this_hdr->sh_size;
4821 if (align > p->p_align
4822 && !m->p_align_valid
4823 && (p->p_type != PT_LOAD
4824 || (abfd->flags & D_PAGED) == 0))
4828 if (!m->p_flags_valid)
4831 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4833 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4840 /* Check that all sections are in a PT_LOAD segment.
4841 Don't check funky gdb generated core files. */
4842 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4844 bfd_boolean check_vma = TRUE;
4846 for (i = 1; i < m->count; i++)
4847 if (m->sections[i]->vma == m->sections[i - 1]->vma
4848 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4849 ->this_hdr), p) != 0
4850 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4851 ->this_hdr), p) != 0)
4853 /* Looks like we have overlays packed into the segment. */
4858 for (i = 0; i < m->count; i++)
4860 Elf_Internal_Shdr *this_hdr;
4863 sec = m->sections[i];
4864 this_hdr = &(elf_section_data(sec)->this_hdr);
4865 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
4866 && !ELF_TBSS_SPECIAL (this_hdr, p))
4868 (*_bfd_error_handler)
4869 (_("%B: section `%A' can't be allocated in segment %d"),
4871 print_segment_map (m);
4877 elf_next_file_pos (abfd) = off;
4881 /* Assign file positions for the other sections. */
4884 assign_file_positions_for_non_load_sections (bfd *abfd,
4885 struct bfd_link_info *link_info)
4887 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4888 Elf_Internal_Shdr **i_shdrpp;
4889 Elf_Internal_Shdr **hdrpp;
4890 Elf_Internal_Phdr *phdrs;
4891 Elf_Internal_Phdr *p;
4892 struct elf_segment_map *m;
4893 struct elf_segment_map *hdrs_segment;
4894 bfd_vma filehdr_vaddr, filehdr_paddr;
4895 bfd_vma phdrs_vaddr, phdrs_paddr;
4897 unsigned int num_sec;
4901 i_shdrpp = elf_elfsections (abfd);
4902 num_sec = elf_numsections (abfd);
4903 off = elf_next_file_pos (abfd);
4904 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4906 Elf_Internal_Shdr *hdr;
4909 if (hdr->bfd_section != NULL
4910 && (hdr->bfd_section->filepos != 0
4911 || (hdr->sh_type == SHT_NOBITS
4912 && hdr->contents == NULL)))
4913 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4914 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4916 if (hdr->sh_size != 0)
4917 (*_bfd_error_handler)
4918 (_("%B: warning: allocated section `%s' not in segment"),
4920 (hdr->bfd_section == NULL
4922 : hdr->bfd_section->name));
4923 /* We don't need to page align empty sections. */
4924 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4925 off += vma_page_aligned_bias (hdr->sh_addr, off,
4928 off += vma_page_aligned_bias (hdr->sh_addr, off,
4930 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4933 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4934 && hdr->bfd_section == NULL)
4935 || hdr == i_shdrpp[elf_onesymtab (abfd)]
4936 || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
4937 || hdr == i_shdrpp[elf_strtab_sec (abfd)])
4938 hdr->sh_offset = -1;
4940 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4943 /* Now that we have set the section file positions, we can set up
4944 the file positions for the non PT_LOAD segments. */
4948 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4950 hdrs_segment = NULL;
4951 phdrs = elf_tdata (abfd)->phdr;
4952 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
4955 if (p->p_type != PT_LOAD)
4958 if (m->includes_filehdr)
4960 filehdr_vaddr = p->p_vaddr;
4961 filehdr_paddr = p->p_paddr;
4963 if (m->includes_phdrs)
4965 phdrs_vaddr = p->p_vaddr;
4966 phdrs_paddr = p->p_paddr;
4967 if (m->includes_filehdr)
4970 phdrs_vaddr += bed->s->sizeof_ehdr;
4971 phdrs_paddr += bed->s->sizeof_ehdr;
4976 if (hdrs_segment != NULL && link_info != NULL)
4978 /* There is a segment that contains both the file headers and the
4979 program headers, so provide a symbol __ehdr_start pointing there.
4980 A program can use this to examine itself robustly. */
4982 struct elf_link_hash_entry *hash
4983 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
4984 FALSE, FALSE, TRUE);
4985 /* If the symbol was referenced and not defined, define it. */
4987 && (hash->root.type == bfd_link_hash_new
4988 || hash->root.type == bfd_link_hash_undefined
4989 || hash->root.type == bfd_link_hash_undefweak
4990 || hash->root.type == bfd_link_hash_common))
4993 if (hdrs_segment->count != 0)
4994 /* The segment contains sections, so use the first one. */
4995 s = hdrs_segment->sections[0];
4997 /* Use the first (i.e. lowest-addressed) section in any segment. */
4998 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5007 hash->root.u.def.value = filehdr_vaddr - s->vma;
5008 hash->root.u.def.section = s;
5012 hash->root.u.def.value = filehdr_vaddr;
5013 hash->root.u.def.section = bfd_abs_section_ptr;
5016 hash->root.type = bfd_link_hash_defined;
5017 hash->def_regular = 1;
5022 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5024 if (p->p_type == PT_GNU_RELRO)
5026 const Elf_Internal_Phdr *lp;
5027 struct elf_segment_map *lm;
5029 if (link_info != NULL)
5031 /* During linking the range of the RELRO segment is passed
5033 for (lm = elf_seg_map (abfd), lp = phdrs;
5035 lm = lm->next, lp++)
5037 if (lp->p_type == PT_LOAD
5038 && lp->p_vaddr < link_info->relro_end
5040 && lm->sections[0]->vma >= link_info->relro_start)
5044 BFD_ASSERT (lm != NULL);
5048 /* Otherwise we are copying an executable or shared
5049 library, but we need to use the same linker logic. */
5050 for (lp = phdrs; lp < phdrs + count; ++lp)
5052 if (lp->p_type == PT_LOAD
5053 && lp->p_paddr == p->p_paddr)
5058 if (lp < phdrs + count)
5060 p->p_vaddr = lp->p_vaddr;
5061 p->p_paddr = lp->p_paddr;
5062 p->p_offset = lp->p_offset;
5063 if (link_info != NULL)
5064 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5065 else if (m->p_size_valid)
5066 p->p_filesz = m->p_size;
5069 p->p_memsz = p->p_filesz;
5070 /* Preserve the alignment and flags if they are valid. The
5071 gold linker generates RW/4 for the PT_GNU_RELRO section.
5072 It is better for objcopy/strip to honor these attributes
5073 otherwise gdb will choke when using separate debug files.
5075 if (!m->p_align_valid)
5077 if (!m->p_flags_valid)
5078 p->p_flags = (lp->p_flags & ~PF_W);
5082 memset (p, 0, sizeof *p);
5083 p->p_type = PT_NULL;
5086 else if (p->p_type == PT_GNU_STACK)
5088 if (m->p_size_valid)
5089 p->p_memsz = m->p_size;
5091 else if (m->count != 0)
5093 if (p->p_type != PT_LOAD
5094 && (p->p_type != PT_NOTE
5095 || bfd_get_format (abfd) != bfd_core))
5097 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
5100 p->p_offset = m->sections[0]->filepos;
5101 for (i = m->count; i-- != 0;)
5103 asection *sect = m->sections[i];
5104 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5105 if (hdr->sh_type != SHT_NOBITS)
5107 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5114 else if (m->includes_filehdr)
5116 p->p_vaddr = filehdr_vaddr;
5117 if (! m->p_paddr_valid)
5118 p->p_paddr = filehdr_paddr;
5120 else if (m->includes_phdrs)
5122 p->p_vaddr = phdrs_vaddr;
5123 if (! m->p_paddr_valid)
5124 p->p_paddr = phdrs_paddr;
5128 elf_next_file_pos (abfd) = off;
5133 /* Work out the file positions of all the sections. This is called by
5134 _bfd_elf_compute_section_file_positions. All the section sizes and
5135 VMAs must be known before this is called.
5137 Reloc sections come in two flavours: Those processed specially as
5138 "side-channel" data attached to a section to which they apply, and
5139 those that bfd doesn't process as relocations. The latter sort are
5140 stored in a normal bfd section by bfd_section_from_shdr. We don't
5141 consider the former sort here, unless they form part of the loadable
5142 image. Reloc sections not assigned here will be handled later by
5143 assign_file_positions_for_relocs.
5145 We also don't set the positions of the .symtab and .strtab here. */
5148 assign_file_positions_except_relocs (bfd *abfd,
5149 struct bfd_link_info *link_info)
5151 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5152 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5153 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5155 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5156 && bfd_get_format (abfd) != bfd_core)
5158 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5159 unsigned int num_sec = elf_numsections (abfd);
5160 Elf_Internal_Shdr **hdrpp;
5164 /* Start after the ELF header. */
5165 off = i_ehdrp->e_ehsize;
5167 /* We are not creating an executable, which means that we are
5168 not creating a program header, and that the actual order of
5169 the sections in the file is unimportant. */
5170 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5172 Elf_Internal_Shdr *hdr;
5175 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5176 && hdr->bfd_section == NULL)
5177 || i == elf_onesymtab (abfd)
5178 || i == elf_symtab_shndx (abfd)
5179 || i == elf_strtab_sec (abfd))
5181 hdr->sh_offset = -1;
5184 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5187 elf_next_file_pos (abfd) = off;
5193 /* Assign file positions for the loaded sections based on the
5194 assignment of sections to segments. */
5195 if (!assign_file_positions_for_load_sections (abfd, link_info))
5198 /* And for non-load sections. */
5199 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5202 if (bed->elf_backend_modify_program_headers != NULL)
5204 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5208 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5209 if (link_info != NULL
5210 && link_info->executable
5211 && link_info->shared)
5213 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
5214 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
5215 Elf_Internal_Phdr *end_segment = &segment[num_segments];
5217 /* Find the lowest p_vaddr in PT_LOAD segments. */
5218 bfd_vma p_vaddr = (bfd_vma) -1;
5219 for (; segment < end_segment; segment++)
5220 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
5221 p_vaddr = segment->p_vaddr;
5223 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5224 segments is non-zero. */
5226 i_ehdrp->e_type = ET_EXEC;
5229 /* Write out the program headers. */
5230 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5231 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5232 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5240 prep_headers (bfd *abfd)
5242 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5243 struct elf_strtab_hash *shstrtab;
5244 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5246 i_ehdrp = elf_elfheader (abfd);
5248 shstrtab = _bfd_elf_strtab_init ();
5249 if (shstrtab == NULL)
5252 elf_shstrtab (abfd) = shstrtab;
5254 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5255 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5256 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5257 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5259 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5260 i_ehdrp->e_ident[EI_DATA] =
5261 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5262 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5264 if ((abfd->flags & DYNAMIC) != 0)
5265 i_ehdrp->e_type = ET_DYN;
5266 else if ((abfd->flags & EXEC_P) != 0)
5267 i_ehdrp->e_type = ET_EXEC;
5268 else if (bfd_get_format (abfd) == bfd_core)
5269 i_ehdrp->e_type = ET_CORE;
5271 i_ehdrp->e_type = ET_REL;
5273 switch (bfd_get_arch (abfd))
5275 case bfd_arch_unknown:
5276 i_ehdrp->e_machine = EM_NONE;
5279 /* There used to be a long list of cases here, each one setting
5280 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5281 in the corresponding bfd definition. To avoid duplication,
5282 the switch was removed. Machines that need special handling
5283 can generally do it in elf_backend_final_write_processing(),
5284 unless they need the information earlier than the final write.
5285 Such need can generally be supplied by replacing the tests for
5286 e_machine with the conditions used to determine it. */
5288 i_ehdrp->e_machine = bed->elf_machine_code;
5291 i_ehdrp->e_version = bed->s->ev_current;
5292 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5294 /* No program header, for now. */
5295 i_ehdrp->e_phoff = 0;
5296 i_ehdrp->e_phentsize = 0;
5297 i_ehdrp->e_phnum = 0;
5299 /* Each bfd section is section header entry. */
5300 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5301 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5303 /* If we're building an executable, we'll need a program header table. */
5304 if (abfd->flags & EXEC_P)
5305 /* It all happens later. */
5309 i_ehdrp->e_phentsize = 0;
5310 i_ehdrp->e_phoff = 0;
5313 elf_tdata (abfd)->symtab_hdr.sh_name =
5314 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5315 elf_tdata (abfd)->strtab_hdr.sh_name =
5316 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5317 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5318 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5319 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5320 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
5321 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5327 /* Assign file positions for all the reloc sections which are not part
5328 of the loadable file image, and the file position of section headers. */
5331 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5334 unsigned int i, num_sec;
5335 Elf_Internal_Shdr **shdrpp;
5336 Elf_Internal_Ehdr *i_ehdrp;
5337 const struct elf_backend_data *bed;
5339 off = elf_next_file_pos (abfd);
5341 num_sec = elf_numsections (abfd);
5342 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5344 Elf_Internal_Shdr *shdrp;
5347 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5348 && shdrp->sh_offset == -1)
5349 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5352 /* Place the section headers. */
5353 i_ehdrp = elf_elfheader (abfd);
5354 bed = get_elf_backend_data (abfd);
5355 off = align_file_position (off, 1 << bed->s->log_file_align);
5356 i_ehdrp->e_shoff = off;
5357 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5358 elf_next_file_pos (abfd) = off;
5362 _bfd_elf_write_object_contents (bfd *abfd)
5364 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5365 Elf_Internal_Shdr **i_shdrp;
5367 unsigned int count, num_sec;
5368 struct elf_obj_tdata *t;
5370 if (! abfd->output_has_begun
5371 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5374 i_shdrp = elf_elfsections (abfd);
5377 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5381 _bfd_elf_assign_file_positions_for_relocs (abfd);
5383 /* After writing the headers, we need to write the sections too... */
5384 num_sec = elf_numsections (abfd);
5385 for (count = 1; count < num_sec; count++)
5387 if (bed->elf_backend_section_processing)
5388 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5389 if (i_shdrp[count]->contents)
5391 bfd_size_type amt = i_shdrp[count]->sh_size;
5393 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5394 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5399 /* Write out the section header names. */
5400 t = elf_tdata (abfd);
5401 if (elf_shstrtab (abfd) != NULL
5402 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5403 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5406 if (bed->elf_backend_final_write_processing)
5407 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5409 if (!bed->s->write_shdrs_and_ehdr (abfd))
5412 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5413 if (t->o->build_id.after_write_object_contents != NULL)
5414 return (*t->o->build_id.after_write_object_contents) (abfd);
5420 _bfd_elf_write_corefile_contents (bfd *abfd)
5422 /* Hopefully this can be done just like an object file. */
5423 return _bfd_elf_write_object_contents (abfd);
5426 /* Given a section, search the header to find them. */
5429 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5431 const struct elf_backend_data *bed;
5432 unsigned int sec_index;
5434 if (elf_section_data (asect) != NULL
5435 && elf_section_data (asect)->this_idx != 0)
5436 return elf_section_data (asect)->this_idx;
5438 if (bfd_is_abs_section (asect))
5439 sec_index = SHN_ABS;
5440 else if (bfd_is_com_section (asect))
5441 sec_index = SHN_COMMON;
5442 else if (bfd_is_und_section (asect))
5443 sec_index = SHN_UNDEF;
5445 sec_index = SHN_BAD;
5447 bed = get_elf_backend_data (abfd);
5448 if (bed->elf_backend_section_from_bfd_section)
5450 int retval = sec_index;
5452 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5456 if (sec_index == SHN_BAD)
5457 bfd_set_error (bfd_error_nonrepresentable_section);
5462 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5466 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5468 asymbol *asym_ptr = *asym_ptr_ptr;
5470 flagword flags = asym_ptr->flags;
5472 /* When gas creates relocations against local labels, it creates its
5473 own symbol for the section, but does put the symbol into the
5474 symbol chain, so udata is 0. When the linker is generating
5475 relocatable output, this section symbol may be for one of the
5476 input sections rather than the output section. */
5477 if (asym_ptr->udata.i == 0
5478 && (flags & BSF_SECTION_SYM)
5479 && asym_ptr->section)
5484 sec = asym_ptr->section;
5485 if (sec->owner != abfd && sec->output_section != NULL)
5486 sec = sec->output_section;
5487 if (sec->owner == abfd
5488 && (indx = sec->index) < elf_num_section_syms (abfd)
5489 && elf_section_syms (abfd)[indx] != NULL)
5490 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5493 idx = asym_ptr->udata.i;
5497 /* This case can occur when using --strip-symbol on a symbol
5498 which is used in a relocation entry. */
5499 (*_bfd_error_handler)
5500 (_("%B: symbol `%s' required but not present"),
5501 abfd, bfd_asymbol_name (asym_ptr));
5502 bfd_set_error (bfd_error_no_symbols);
5509 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5510 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5518 /* Rewrite program header information. */
5521 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5523 Elf_Internal_Ehdr *iehdr;
5524 struct elf_segment_map *map;
5525 struct elf_segment_map *map_first;
5526 struct elf_segment_map **pointer_to_map;
5527 Elf_Internal_Phdr *segment;
5530 unsigned int num_segments;
5531 bfd_boolean phdr_included = FALSE;
5532 bfd_boolean p_paddr_valid;
5533 bfd_vma maxpagesize;
5534 struct elf_segment_map *phdr_adjust_seg = NULL;
5535 unsigned int phdr_adjust_num = 0;
5536 const struct elf_backend_data *bed;
5538 bed = get_elf_backend_data (ibfd);
5539 iehdr = elf_elfheader (ibfd);
5542 pointer_to_map = &map_first;
5544 num_segments = elf_elfheader (ibfd)->e_phnum;
5545 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5547 /* Returns the end address of the segment + 1. */
5548 #define SEGMENT_END(segment, start) \
5549 (start + (segment->p_memsz > segment->p_filesz \
5550 ? segment->p_memsz : segment->p_filesz))
5552 #define SECTION_SIZE(section, segment) \
5553 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5554 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5555 ? section->size : 0)
5557 /* Returns TRUE if the given section is contained within
5558 the given segment. VMA addresses are compared. */
5559 #define IS_CONTAINED_BY_VMA(section, segment) \
5560 (section->vma >= segment->p_vaddr \
5561 && (section->vma + SECTION_SIZE (section, segment) \
5562 <= (SEGMENT_END (segment, segment->p_vaddr))))
5564 /* Returns TRUE if the given section is contained within
5565 the given segment. LMA addresses are compared. */
5566 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5567 (section->lma >= base \
5568 && (section->lma + SECTION_SIZE (section, segment) \
5569 <= SEGMENT_END (segment, base)))
5571 /* Handle PT_NOTE segment. */
5572 #define IS_NOTE(p, s) \
5573 (p->p_type == PT_NOTE \
5574 && elf_section_type (s) == SHT_NOTE \
5575 && (bfd_vma) s->filepos >= p->p_offset \
5576 && ((bfd_vma) s->filepos + s->size \
5577 <= p->p_offset + p->p_filesz))
5579 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5581 #define IS_COREFILE_NOTE(p, s) \
5583 && bfd_get_format (ibfd) == bfd_core \
5587 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5588 linker, which generates a PT_INTERP section with p_vaddr and
5589 p_memsz set to 0. */
5590 #define IS_SOLARIS_PT_INTERP(p, s) \
5592 && p->p_paddr == 0 \
5593 && p->p_memsz == 0 \
5594 && p->p_filesz > 0 \
5595 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5597 && (bfd_vma) s->filepos >= p->p_offset \
5598 && ((bfd_vma) s->filepos + s->size \
5599 <= p->p_offset + p->p_filesz))
5601 /* Decide if the given section should be included in the given segment.
5602 A section will be included if:
5603 1. It is within the address space of the segment -- we use the LMA
5604 if that is set for the segment and the VMA otherwise,
5605 2. It is an allocated section or a NOTE section in a PT_NOTE
5607 3. There is an output section associated with it,
5608 4. The section has not already been allocated to a previous segment.
5609 5. PT_GNU_STACK segments do not include any sections.
5610 6. PT_TLS segment includes only SHF_TLS sections.
5611 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5612 8. PT_DYNAMIC should not contain empty sections at the beginning
5613 (with the possible exception of .dynamic). */
5614 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5615 ((((segment->p_paddr \
5616 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5617 : IS_CONTAINED_BY_VMA (section, segment)) \
5618 && (section->flags & SEC_ALLOC) != 0) \
5619 || IS_NOTE (segment, section)) \
5620 && segment->p_type != PT_GNU_STACK \
5621 && (segment->p_type != PT_TLS \
5622 || (section->flags & SEC_THREAD_LOCAL)) \
5623 && (segment->p_type == PT_LOAD \
5624 || segment->p_type == PT_TLS \
5625 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5626 && (segment->p_type != PT_DYNAMIC \
5627 || SECTION_SIZE (section, segment) > 0 \
5628 || (segment->p_paddr \
5629 ? segment->p_paddr != section->lma \
5630 : segment->p_vaddr != section->vma) \
5631 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5633 && !section->segment_mark)
5635 /* If the output section of a section in the input segment is NULL,
5636 it is removed from the corresponding output segment. */
5637 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5638 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5639 && section->output_section != NULL)
5641 /* Returns TRUE iff seg1 starts after the end of seg2. */
5642 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5643 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5645 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5646 their VMA address ranges and their LMA address ranges overlap.
5647 It is possible to have overlapping VMA ranges without overlapping LMA
5648 ranges. RedBoot images for example can have both .data and .bss mapped
5649 to the same VMA range, but with the .data section mapped to a different
5651 #define SEGMENT_OVERLAPS(seg1, seg2) \
5652 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5653 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5654 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5655 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5657 /* Initialise the segment mark field. */
5658 for (section = ibfd->sections; section != NULL; section = section->next)
5659 section->segment_mark = FALSE;
5661 /* The Solaris linker creates program headers in which all the
5662 p_paddr fields are zero. When we try to objcopy or strip such a
5663 file, we get confused. Check for this case, and if we find it
5664 don't set the p_paddr_valid fields. */
5665 p_paddr_valid = FALSE;
5666 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5669 if (segment->p_paddr != 0)
5671 p_paddr_valid = TRUE;
5675 /* Scan through the segments specified in the program header
5676 of the input BFD. For this first scan we look for overlaps
5677 in the loadable segments. These can be created by weird
5678 parameters to objcopy. Also, fix some solaris weirdness. */
5679 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5684 Elf_Internal_Phdr *segment2;
5686 if (segment->p_type == PT_INTERP)
5687 for (section = ibfd->sections; section; section = section->next)
5688 if (IS_SOLARIS_PT_INTERP (segment, section))
5690 /* Mininal change so that the normal section to segment
5691 assignment code will work. */
5692 segment->p_vaddr = section->vma;
5696 if (segment->p_type != PT_LOAD)
5698 /* Remove PT_GNU_RELRO segment. */
5699 if (segment->p_type == PT_GNU_RELRO)
5700 segment->p_type = PT_NULL;
5704 /* Determine if this segment overlaps any previous segments. */
5705 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5707 bfd_signed_vma extra_length;
5709 if (segment2->p_type != PT_LOAD
5710 || !SEGMENT_OVERLAPS (segment, segment2))
5713 /* Merge the two segments together. */
5714 if (segment2->p_vaddr < segment->p_vaddr)
5716 /* Extend SEGMENT2 to include SEGMENT and then delete
5718 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5719 - SEGMENT_END (segment2, segment2->p_vaddr));
5721 if (extra_length > 0)
5723 segment2->p_memsz += extra_length;
5724 segment2->p_filesz += extra_length;
5727 segment->p_type = PT_NULL;
5729 /* Since we have deleted P we must restart the outer loop. */
5731 segment = elf_tdata (ibfd)->phdr;
5736 /* Extend SEGMENT to include SEGMENT2 and then delete
5738 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5739 - SEGMENT_END (segment, segment->p_vaddr));
5741 if (extra_length > 0)
5743 segment->p_memsz += extra_length;
5744 segment->p_filesz += extra_length;
5747 segment2->p_type = PT_NULL;
5752 /* The second scan attempts to assign sections to segments. */
5753 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5757 unsigned int section_count;
5758 asection **sections;
5759 asection *output_section;
5761 bfd_vma matching_lma;
5762 bfd_vma suggested_lma;
5765 asection *first_section;
5766 bfd_boolean first_matching_lma;
5767 bfd_boolean first_suggested_lma;
5769 if (segment->p_type == PT_NULL)
5772 first_section = NULL;
5773 /* Compute how many sections might be placed into this segment. */
5774 for (section = ibfd->sections, section_count = 0;
5776 section = section->next)
5778 /* Find the first section in the input segment, which may be
5779 removed from the corresponding output segment. */
5780 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5782 if (first_section == NULL)
5783 first_section = section;
5784 if (section->output_section != NULL)
5789 /* Allocate a segment map big enough to contain
5790 all of the sections we have selected. */
5791 amt = sizeof (struct elf_segment_map);
5792 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5793 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5797 /* Initialise the fields of the segment map. Default to
5798 using the physical address of the segment in the input BFD. */
5800 map->p_type = segment->p_type;
5801 map->p_flags = segment->p_flags;
5802 map->p_flags_valid = 1;
5804 /* If the first section in the input segment is removed, there is
5805 no need to preserve segment physical address in the corresponding
5807 if (!first_section || first_section->output_section != NULL)
5809 map->p_paddr = segment->p_paddr;
5810 map->p_paddr_valid = p_paddr_valid;
5813 /* Determine if this segment contains the ELF file header
5814 and if it contains the program headers themselves. */
5815 map->includes_filehdr = (segment->p_offset == 0
5816 && segment->p_filesz >= iehdr->e_ehsize);
5817 map->includes_phdrs = 0;
5819 if (!phdr_included || segment->p_type != PT_LOAD)
5821 map->includes_phdrs =
5822 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5823 && (segment->p_offset + segment->p_filesz
5824 >= ((bfd_vma) iehdr->e_phoff
5825 + iehdr->e_phnum * iehdr->e_phentsize)));
5827 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5828 phdr_included = TRUE;
5831 if (section_count == 0)
5833 /* Special segments, such as the PT_PHDR segment, may contain
5834 no sections, but ordinary, loadable segments should contain
5835 something. They are allowed by the ELF spec however, so only
5836 a warning is produced. */
5837 if (segment->p_type == PT_LOAD)
5838 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5839 " detected, is this intentional ?\n"),
5843 *pointer_to_map = map;
5844 pointer_to_map = &map->next;
5849 /* Now scan the sections in the input BFD again and attempt
5850 to add their corresponding output sections to the segment map.
5851 The problem here is how to handle an output section which has
5852 been moved (ie had its LMA changed). There are four possibilities:
5854 1. None of the sections have been moved.
5855 In this case we can continue to use the segment LMA from the
5858 2. All of the sections have been moved by the same amount.
5859 In this case we can change the segment's LMA to match the LMA
5860 of the first section.
5862 3. Some of the sections have been moved, others have not.
5863 In this case those sections which have not been moved can be
5864 placed in the current segment which will have to have its size,
5865 and possibly its LMA changed, and a new segment or segments will
5866 have to be created to contain the other sections.
5868 4. The sections have been moved, but not by the same amount.
5869 In this case we can change the segment's LMA to match the LMA
5870 of the first section and we will have to create a new segment
5871 or segments to contain the other sections.
5873 In order to save time, we allocate an array to hold the section
5874 pointers that we are interested in. As these sections get assigned
5875 to a segment, they are removed from this array. */
5877 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5878 if (sections == NULL)
5881 /* Step One: Scan for segment vs section LMA conflicts.
5882 Also add the sections to the section array allocated above.
5883 Also add the sections to the current segment. In the common
5884 case, where the sections have not been moved, this means that
5885 we have completely filled the segment, and there is nothing
5890 first_matching_lma = TRUE;
5891 first_suggested_lma = TRUE;
5893 for (section = ibfd->sections;
5895 section = section->next)
5896 if (section == first_section)
5899 for (j = 0; section != NULL; section = section->next)
5901 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5903 output_section = section->output_section;
5905 sections[j++] = section;
5907 /* The Solaris native linker always sets p_paddr to 0.
5908 We try to catch that case here, and set it to the
5909 correct value. Note - some backends require that
5910 p_paddr be left as zero. */
5912 && segment->p_vaddr != 0
5913 && !bed->want_p_paddr_set_to_zero
5915 && output_section->lma != 0
5916 && output_section->vma == (segment->p_vaddr
5917 + (map->includes_filehdr
5920 + (map->includes_phdrs
5922 * iehdr->e_phentsize)
5924 map->p_paddr = segment->p_vaddr;
5926 /* Match up the physical address of the segment with the
5927 LMA address of the output section. */
5928 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5929 || IS_COREFILE_NOTE (segment, section)
5930 || (bed->want_p_paddr_set_to_zero
5931 && IS_CONTAINED_BY_VMA (output_section, segment)))
5933 if (first_matching_lma || output_section->lma < matching_lma)
5935 matching_lma = output_section->lma;
5936 first_matching_lma = FALSE;
5939 /* We assume that if the section fits within the segment
5940 then it does not overlap any other section within that
5942 map->sections[isec++] = output_section;
5944 else if (first_suggested_lma)
5946 suggested_lma = output_section->lma;
5947 first_suggested_lma = FALSE;
5950 if (j == section_count)
5955 BFD_ASSERT (j == section_count);
5957 /* Step Two: Adjust the physical address of the current segment,
5959 if (isec == section_count)
5961 /* All of the sections fitted within the segment as currently
5962 specified. This is the default case. Add the segment to
5963 the list of built segments and carry on to process the next
5964 program header in the input BFD. */
5965 map->count = section_count;
5966 *pointer_to_map = map;
5967 pointer_to_map = &map->next;
5970 && !bed->want_p_paddr_set_to_zero
5971 && matching_lma != map->p_paddr
5972 && !map->includes_filehdr
5973 && !map->includes_phdrs)
5974 /* There is some padding before the first section in the
5975 segment. So, we must account for that in the output
5977 map->p_vaddr_offset = matching_lma - map->p_paddr;
5984 if (!first_matching_lma)
5986 /* At least one section fits inside the current segment.
5987 Keep it, but modify its physical address to match the
5988 LMA of the first section that fitted. */
5989 map->p_paddr = matching_lma;
5993 /* None of the sections fitted inside the current segment.
5994 Change the current segment's physical address to match
5995 the LMA of the first section. */
5996 map->p_paddr = suggested_lma;
5999 /* Offset the segment physical address from the lma
6000 to allow for space taken up by elf headers. */
6001 if (map->includes_filehdr)
6003 if (map->p_paddr >= iehdr->e_ehsize)
6004 map->p_paddr -= iehdr->e_ehsize;
6007 map->includes_filehdr = FALSE;
6008 map->includes_phdrs = FALSE;
6012 if (map->includes_phdrs)
6014 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6016 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6018 /* iehdr->e_phnum is just an estimate of the number
6019 of program headers that we will need. Make a note
6020 here of the number we used and the segment we chose
6021 to hold these headers, so that we can adjust the
6022 offset when we know the correct value. */
6023 phdr_adjust_num = iehdr->e_phnum;
6024 phdr_adjust_seg = map;
6027 map->includes_phdrs = FALSE;
6031 /* Step Three: Loop over the sections again, this time assigning
6032 those that fit to the current segment and removing them from the
6033 sections array; but making sure not to leave large gaps. Once all
6034 possible sections have been assigned to the current segment it is
6035 added to the list of built segments and if sections still remain
6036 to be assigned, a new segment is constructed before repeating
6043 first_suggested_lma = TRUE;
6045 /* Fill the current segment with sections that fit. */
6046 for (j = 0; j < section_count; j++)
6048 section = sections[j];
6050 if (section == NULL)
6053 output_section = section->output_section;
6055 BFD_ASSERT (output_section != NULL);
6057 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6058 || IS_COREFILE_NOTE (segment, section))
6060 if (map->count == 0)
6062 /* If the first section in a segment does not start at
6063 the beginning of the segment, then something is
6065 if (output_section->lma
6067 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6068 + (map->includes_phdrs
6069 ? iehdr->e_phnum * iehdr->e_phentsize
6077 prev_sec = map->sections[map->count - 1];
6079 /* If the gap between the end of the previous section
6080 and the start of this section is more than
6081 maxpagesize then we need to start a new segment. */
6082 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6084 < BFD_ALIGN (output_section->lma, maxpagesize))
6085 || (prev_sec->lma + prev_sec->size
6086 > output_section->lma))
6088 if (first_suggested_lma)
6090 suggested_lma = output_section->lma;
6091 first_suggested_lma = FALSE;
6098 map->sections[map->count++] = output_section;
6101 section->segment_mark = TRUE;
6103 else if (first_suggested_lma)
6105 suggested_lma = output_section->lma;
6106 first_suggested_lma = FALSE;
6110 BFD_ASSERT (map->count > 0);
6112 /* Add the current segment to the list of built segments. */
6113 *pointer_to_map = map;
6114 pointer_to_map = &map->next;
6116 if (isec < section_count)
6118 /* We still have not allocated all of the sections to
6119 segments. Create a new segment here, initialise it
6120 and carry on looping. */
6121 amt = sizeof (struct elf_segment_map);
6122 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6123 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6130 /* Initialise the fields of the segment map. Set the physical
6131 physical address to the LMA of the first section that has
6132 not yet been assigned. */
6134 map->p_type = segment->p_type;
6135 map->p_flags = segment->p_flags;
6136 map->p_flags_valid = 1;
6137 map->p_paddr = suggested_lma;
6138 map->p_paddr_valid = p_paddr_valid;
6139 map->includes_filehdr = 0;
6140 map->includes_phdrs = 0;
6143 while (isec < section_count);
6148 elf_seg_map (obfd) = map_first;
6150 /* If we had to estimate the number of program headers that were
6151 going to be needed, then check our estimate now and adjust
6152 the offset if necessary. */
6153 if (phdr_adjust_seg != NULL)
6157 for (count = 0, map = map_first; map != NULL; map = map->next)
6160 if (count > phdr_adjust_num)
6161 phdr_adjust_seg->p_paddr
6162 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6167 #undef IS_CONTAINED_BY_VMA
6168 #undef IS_CONTAINED_BY_LMA
6170 #undef IS_COREFILE_NOTE
6171 #undef IS_SOLARIS_PT_INTERP
6172 #undef IS_SECTION_IN_INPUT_SEGMENT
6173 #undef INCLUDE_SECTION_IN_SEGMENT
6174 #undef SEGMENT_AFTER_SEGMENT
6175 #undef SEGMENT_OVERLAPS
6179 /* Copy ELF program header information. */
6182 copy_elf_program_header (bfd *ibfd, bfd *obfd)
6184 Elf_Internal_Ehdr *iehdr;
6185 struct elf_segment_map *map;
6186 struct elf_segment_map *map_first;
6187 struct elf_segment_map **pointer_to_map;
6188 Elf_Internal_Phdr *segment;
6190 unsigned int num_segments;
6191 bfd_boolean phdr_included = FALSE;
6192 bfd_boolean p_paddr_valid;
6194 iehdr = elf_elfheader (ibfd);
6197 pointer_to_map = &map_first;
6199 /* If all the segment p_paddr fields are zero, don't set
6200 map->p_paddr_valid. */
6201 p_paddr_valid = FALSE;
6202 num_segments = elf_elfheader (ibfd)->e_phnum;
6203 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6206 if (segment->p_paddr != 0)
6208 p_paddr_valid = TRUE;
6212 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6217 unsigned int section_count;
6219 Elf_Internal_Shdr *this_hdr;
6220 asection *first_section = NULL;
6221 asection *lowest_section;
6223 /* Compute how many sections are in this segment. */
6224 for (section = ibfd->sections, section_count = 0;
6226 section = section->next)
6228 this_hdr = &(elf_section_data(section)->this_hdr);
6229 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6231 if (first_section == NULL)
6232 first_section = section;
6237 /* Allocate a segment map big enough to contain
6238 all of the sections we have selected. */
6239 amt = sizeof (struct elf_segment_map);
6240 if (section_count != 0)
6241 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6242 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6246 /* Initialize the fields of the output segment map with the
6249 map->p_type = segment->p_type;
6250 map->p_flags = segment->p_flags;
6251 map->p_flags_valid = 1;
6252 map->p_paddr = segment->p_paddr;
6253 map->p_paddr_valid = p_paddr_valid;
6254 map->p_align = segment->p_align;
6255 map->p_align_valid = 1;
6256 map->p_vaddr_offset = 0;
6258 if (map->p_type == PT_GNU_RELRO
6259 || map->p_type == PT_GNU_STACK)
6261 /* The PT_GNU_RELRO segment may contain the first a few
6262 bytes in the .got.plt section even if the whole .got.plt
6263 section isn't in the PT_GNU_RELRO segment. We won't
6264 change the size of the PT_GNU_RELRO segment.
6265 Similarly, PT_GNU_STACK size is significant on uclinux
6267 map->p_size = segment->p_memsz;
6268 map->p_size_valid = 1;
6271 /* Determine if this segment contains the ELF file header
6272 and if it contains the program headers themselves. */
6273 map->includes_filehdr = (segment->p_offset == 0
6274 && segment->p_filesz >= iehdr->e_ehsize);
6276 map->includes_phdrs = 0;
6277 if (! phdr_included || segment->p_type != PT_LOAD)
6279 map->includes_phdrs =
6280 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6281 && (segment->p_offset + segment->p_filesz
6282 >= ((bfd_vma) iehdr->e_phoff
6283 + iehdr->e_phnum * iehdr->e_phentsize)));
6285 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6286 phdr_included = TRUE;
6289 lowest_section = NULL;
6290 if (section_count != 0)
6292 unsigned int isec = 0;
6294 for (section = first_section;
6296 section = section->next)
6298 this_hdr = &(elf_section_data(section)->this_hdr);
6299 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6301 map->sections[isec++] = section->output_section;
6302 if ((section->flags & SEC_ALLOC) != 0)
6306 if (lowest_section == NULL
6307 || section->lma < lowest_section->lma)
6308 lowest_section = section;
6310 /* Section lmas are set up from PT_LOAD header
6311 p_paddr in _bfd_elf_make_section_from_shdr.
6312 If this header has a p_paddr that disagrees
6313 with the section lma, flag the p_paddr as
6315 if ((section->flags & SEC_LOAD) != 0)
6316 seg_off = this_hdr->sh_offset - segment->p_offset;
6318 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6319 if (section->lma - segment->p_paddr != seg_off)
6320 map->p_paddr_valid = FALSE;
6322 if (isec == section_count)
6328 if (map->includes_filehdr && lowest_section != NULL)
6329 /* We need to keep the space used by the headers fixed. */
6330 map->header_size = lowest_section->vma - segment->p_vaddr;
6332 if (!map->includes_phdrs
6333 && !map->includes_filehdr
6334 && map->p_paddr_valid)
6335 /* There is some other padding before the first section. */
6336 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6337 - segment->p_paddr);
6339 map->count = section_count;
6340 *pointer_to_map = map;
6341 pointer_to_map = &map->next;
6344 elf_seg_map (obfd) = map_first;
6348 /* Copy private BFD data. This copies or rewrites ELF program header
6352 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6354 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6355 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6358 if (elf_tdata (ibfd)->phdr == NULL)
6361 if (ibfd->xvec == obfd->xvec)
6363 /* Check to see if any sections in the input BFD
6364 covered by ELF program header have changed. */
6365 Elf_Internal_Phdr *segment;
6366 asection *section, *osec;
6367 unsigned int i, num_segments;
6368 Elf_Internal_Shdr *this_hdr;
6369 const struct elf_backend_data *bed;
6371 bed = get_elf_backend_data (ibfd);
6373 /* Regenerate the segment map if p_paddr is set to 0. */
6374 if (bed->want_p_paddr_set_to_zero)
6377 /* Initialize the segment mark field. */
6378 for (section = obfd->sections; section != NULL;
6379 section = section->next)
6380 section->segment_mark = FALSE;
6382 num_segments = elf_elfheader (ibfd)->e_phnum;
6383 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6387 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6388 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6389 which severly confuses things, so always regenerate the segment
6390 map in this case. */
6391 if (segment->p_paddr == 0
6392 && segment->p_memsz == 0
6393 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6396 for (section = ibfd->sections;
6397 section != NULL; section = section->next)
6399 /* We mark the output section so that we know it comes
6400 from the input BFD. */
6401 osec = section->output_section;
6403 osec->segment_mark = TRUE;
6405 /* Check if this section is covered by the segment. */
6406 this_hdr = &(elf_section_data(section)->this_hdr);
6407 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6409 /* FIXME: Check if its output section is changed or
6410 removed. What else do we need to check? */
6412 || section->flags != osec->flags
6413 || section->lma != osec->lma
6414 || section->vma != osec->vma
6415 || section->size != osec->size
6416 || section->rawsize != osec->rawsize
6417 || section->alignment_power != osec->alignment_power)
6423 /* Check to see if any output section do not come from the
6425 for (section = obfd->sections; section != NULL;
6426 section = section->next)
6428 if (section->segment_mark == FALSE)
6431 section->segment_mark = FALSE;
6434 return copy_elf_program_header (ibfd, obfd);
6438 if (ibfd->xvec == obfd->xvec)
6440 /* When rewriting program header, set the output maxpagesize to
6441 the maximum alignment of input PT_LOAD segments. */
6442 Elf_Internal_Phdr *segment;
6444 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6445 bfd_vma maxpagesize = 0;
6447 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6450 if (segment->p_type == PT_LOAD
6451 && maxpagesize < segment->p_align)
6452 maxpagesize = segment->p_align;
6454 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6455 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6458 return rewrite_elf_program_header (ibfd, obfd);
6461 /* Initialize private output section information from input section. */
6464 _bfd_elf_init_private_section_data (bfd *ibfd,
6468 struct bfd_link_info *link_info)
6471 Elf_Internal_Shdr *ihdr, *ohdr;
6472 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6474 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6475 || obfd->xvec->flavour != bfd_target_elf_flavour)
6478 BFD_ASSERT (elf_section_data (osec) != NULL);
6480 /* For objcopy and relocatable link, don't copy the output ELF
6481 section type from input if the output BFD section flags have been
6482 set to something different. For a final link allow some flags
6483 that the linker clears to differ. */
6484 if (elf_section_type (osec) == SHT_NULL
6485 && (osec->flags == isec->flags
6487 && ((osec->flags ^ isec->flags)
6488 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6489 elf_section_type (osec) = elf_section_type (isec);
6491 /* FIXME: Is this correct for all OS/PROC specific flags? */
6492 elf_section_flags (osec) |= (elf_section_flags (isec)
6493 & (SHF_MASKOS | SHF_MASKPROC));
6495 /* Set things up for objcopy and relocatable link. The output
6496 SHT_GROUP section will have its elf_next_in_group pointing back
6497 to the input group members. Ignore linker created group section.
6498 See elfNN_ia64_object_p in elfxx-ia64.c. */
6501 if (elf_sec_group (isec) == NULL
6502 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6504 if (elf_section_flags (isec) & SHF_GROUP)
6505 elf_section_flags (osec) |= SHF_GROUP;
6506 elf_next_in_group (osec) = elf_next_in_group (isec);
6507 elf_section_data (osec)->group = elf_section_data (isec)->group;
6511 ihdr = &elf_section_data (isec)->this_hdr;
6513 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6514 don't use the output section of the linked-to section since it
6515 may be NULL at this point. */
6516 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6518 ohdr = &elf_section_data (osec)->this_hdr;
6519 ohdr->sh_flags |= SHF_LINK_ORDER;
6520 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6523 osec->use_rela_p = isec->use_rela_p;
6528 /* Copy private section information. This copies over the entsize
6529 field, and sometimes the info field. */
6532 _bfd_elf_copy_private_section_data (bfd *ibfd,
6537 Elf_Internal_Shdr *ihdr, *ohdr;
6539 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6540 || obfd->xvec->flavour != bfd_target_elf_flavour)
6543 ihdr = &elf_section_data (isec)->this_hdr;
6544 ohdr = &elf_section_data (osec)->this_hdr;
6546 ohdr->sh_entsize = ihdr->sh_entsize;
6548 if (ihdr->sh_type == SHT_SYMTAB
6549 || ihdr->sh_type == SHT_DYNSYM
6550 || ihdr->sh_type == SHT_GNU_verneed
6551 || ihdr->sh_type == SHT_GNU_verdef)
6552 ohdr->sh_info = ihdr->sh_info;
6554 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6558 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6559 necessary if we are removing either the SHT_GROUP section or any of
6560 the group member sections. DISCARDED is the value that a section's
6561 output_section has if the section will be discarded, NULL when this
6562 function is called from objcopy, bfd_abs_section_ptr when called
6566 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6570 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6571 if (elf_section_type (isec) == SHT_GROUP)
6573 asection *first = elf_next_in_group (isec);
6574 asection *s = first;
6575 bfd_size_type removed = 0;
6579 /* If this member section is being output but the
6580 SHT_GROUP section is not, then clear the group info
6581 set up by _bfd_elf_copy_private_section_data. */
6582 if (s->output_section != discarded
6583 && isec->output_section == discarded)
6585 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6586 elf_group_name (s->output_section) = NULL;
6588 /* Conversely, if the member section is not being output
6589 but the SHT_GROUP section is, then adjust its size. */
6590 else if (s->output_section == discarded
6591 && isec->output_section != discarded)
6593 s = elf_next_in_group (s);
6599 if (discarded != NULL)
6601 /* If we've been called for ld -r, then we need to
6602 adjust the input section size. This function may
6603 be called multiple times, so save the original
6605 if (isec->rawsize == 0)
6606 isec->rawsize = isec->size;
6607 isec->size = isec->rawsize - removed;
6611 /* Adjust the output section size when called from
6613 isec->output_section->size -= removed;
6621 /* Copy private header information. */
6624 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6626 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6627 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6630 /* Copy over private BFD data if it has not already been copied.
6631 This must be done here, rather than in the copy_private_bfd_data
6632 entry point, because the latter is called after the section
6633 contents have been set, which means that the program headers have
6634 already been worked out. */
6635 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
6637 if (! copy_private_bfd_data (ibfd, obfd))
6641 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6644 /* Copy private symbol information. If this symbol is in a section
6645 which we did not map into a BFD section, try to map the section
6646 index correctly. We use special macro definitions for the mapped
6647 section indices; these definitions are interpreted by the
6648 swap_out_syms function. */
6650 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6651 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6652 #define MAP_STRTAB (SHN_HIOS + 3)
6653 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6654 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6657 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6662 elf_symbol_type *isym, *osym;
6664 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6665 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6668 isym = elf_symbol_from (ibfd, isymarg);
6669 osym = elf_symbol_from (obfd, osymarg);
6672 && isym->internal_elf_sym.st_shndx != 0
6674 && bfd_is_abs_section (isym->symbol.section))
6678 shndx = isym->internal_elf_sym.st_shndx;
6679 if (shndx == elf_onesymtab (ibfd))
6680 shndx = MAP_ONESYMTAB;
6681 else if (shndx == elf_dynsymtab (ibfd))
6682 shndx = MAP_DYNSYMTAB;
6683 else if (shndx == elf_strtab_sec (ibfd))
6685 else if (shndx == elf_shstrtab_sec (ibfd))
6686 shndx = MAP_SHSTRTAB;
6687 else if (shndx == elf_symtab_shndx (ibfd))
6688 shndx = MAP_SYM_SHNDX;
6689 osym->internal_elf_sym.st_shndx = shndx;
6695 /* Swap out the symbols. */
6698 swap_out_syms (bfd *abfd,
6699 struct bfd_strtab_hash **sttp,
6702 const struct elf_backend_data *bed;
6705 struct bfd_strtab_hash *stt;
6706 Elf_Internal_Shdr *symtab_hdr;
6707 Elf_Internal_Shdr *symtab_shndx_hdr;
6708 Elf_Internal_Shdr *symstrtab_hdr;
6709 bfd_byte *outbound_syms;
6710 bfd_byte *outbound_shndx;
6712 unsigned int num_locals;
6714 bfd_boolean name_local_sections;
6716 if (!elf_map_symbols (abfd, &num_locals))
6719 /* Dump out the symtabs. */
6720 stt = _bfd_elf_stringtab_init ();
6724 bed = get_elf_backend_data (abfd);
6725 symcount = bfd_get_symcount (abfd);
6726 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6727 symtab_hdr->sh_type = SHT_SYMTAB;
6728 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6729 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6730 symtab_hdr->sh_info = num_locals + 1;
6731 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6733 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6734 symstrtab_hdr->sh_type = SHT_STRTAB;
6736 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6737 bed->s->sizeof_sym);
6738 if (outbound_syms == NULL)
6740 _bfd_stringtab_free (stt);
6743 symtab_hdr->contents = outbound_syms;
6745 outbound_shndx = NULL;
6746 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6747 if (symtab_shndx_hdr->sh_name != 0)
6749 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6750 outbound_shndx = (bfd_byte *)
6751 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6752 if (outbound_shndx == NULL)
6754 _bfd_stringtab_free (stt);
6758 symtab_shndx_hdr->contents = outbound_shndx;
6759 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6760 symtab_shndx_hdr->sh_size = amt;
6761 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6762 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6765 /* Now generate the data (for "contents"). */
6767 /* Fill in zeroth symbol and swap it out. */
6768 Elf_Internal_Sym sym;
6774 sym.st_shndx = SHN_UNDEF;
6775 sym.st_target_internal = 0;
6776 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6777 outbound_syms += bed->s->sizeof_sym;
6778 if (outbound_shndx != NULL)
6779 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6783 = (bed->elf_backend_name_local_section_symbols
6784 && bed->elf_backend_name_local_section_symbols (abfd));
6786 syms = bfd_get_outsymbols (abfd);
6787 for (idx = 0; idx < symcount; idx++)
6789 Elf_Internal_Sym sym;
6790 bfd_vma value = syms[idx]->value;
6791 elf_symbol_type *type_ptr;
6792 flagword flags = syms[idx]->flags;
6795 if (!name_local_sections
6796 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6798 /* Local section symbols have no name. */
6803 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6806 if (sym.st_name == (unsigned long) -1)
6808 _bfd_stringtab_free (stt);
6813 type_ptr = elf_symbol_from (abfd, syms[idx]);
6815 if ((flags & BSF_SECTION_SYM) == 0
6816 && bfd_is_com_section (syms[idx]->section))
6818 /* ELF common symbols put the alignment into the `value' field,
6819 and the size into the `size' field. This is backwards from
6820 how BFD handles it, so reverse it here. */
6821 sym.st_size = value;
6822 if (type_ptr == NULL
6823 || type_ptr->internal_elf_sym.st_value == 0)
6824 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6826 sym.st_value = type_ptr->internal_elf_sym.st_value;
6827 sym.st_shndx = _bfd_elf_section_from_bfd_section
6828 (abfd, syms[idx]->section);
6832 asection *sec = syms[idx]->section;
6835 if (sec->output_section)
6837 value += sec->output_offset;
6838 sec = sec->output_section;
6841 /* Don't add in the section vma for relocatable output. */
6842 if (! relocatable_p)
6844 sym.st_value = value;
6845 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6847 if (bfd_is_abs_section (sec)
6849 && type_ptr->internal_elf_sym.st_shndx != 0)
6851 /* This symbol is in a real ELF section which we did
6852 not create as a BFD section. Undo the mapping done
6853 by copy_private_symbol_data. */
6854 shndx = type_ptr->internal_elf_sym.st_shndx;
6858 shndx = elf_onesymtab (abfd);
6861 shndx = elf_dynsymtab (abfd);
6864 shndx = elf_strtab_sec (abfd);
6867 shndx = elf_shstrtab_sec (abfd);
6870 shndx = elf_symtab_shndx (abfd);
6879 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6881 if (shndx == SHN_BAD)
6885 /* Writing this would be a hell of a lot easier if
6886 we had some decent documentation on bfd, and
6887 knew what to expect of the library, and what to
6888 demand of applications. For example, it
6889 appears that `objcopy' might not set the
6890 section of a symbol to be a section that is
6891 actually in the output file. */
6892 sec2 = bfd_get_section_by_name (abfd, sec->name);
6895 _bfd_error_handler (_("\
6896 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6897 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6899 bfd_set_error (bfd_error_invalid_operation);
6900 _bfd_stringtab_free (stt);
6904 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6905 BFD_ASSERT (shndx != SHN_BAD);
6909 sym.st_shndx = shndx;
6912 if ((flags & BSF_THREAD_LOCAL) != 0)
6914 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6915 type = STT_GNU_IFUNC;
6916 else if ((flags & BSF_FUNCTION) != 0)
6918 else if ((flags & BSF_OBJECT) != 0)
6920 else if ((flags & BSF_RELC) != 0)
6922 else if ((flags & BSF_SRELC) != 0)
6927 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6930 /* Processor-specific types. */
6931 if (type_ptr != NULL
6932 && bed->elf_backend_get_symbol_type)
6933 type = ((*bed->elf_backend_get_symbol_type)
6934 (&type_ptr->internal_elf_sym, type));
6936 if (flags & BSF_SECTION_SYM)
6938 if (flags & BSF_GLOBAL)
6939 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6941 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6943 else if (bfd_is_com_section (syms[idx]->section))
6945 #ifdef USE_STT_COMMON
6946 if (type == STT_OBJECT)
6947 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6950 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6952 else if (bfd_is_und_section (syms[idx]->section))
6953 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6957 else if (flags & BSF_FILE)
6958 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6961 int bind = STB_LOCAL;
6963 if (flags & BSF_LOCAL)
6965 else if (flags & BSF_GNU_UNIQUE)
6966 bind = STB_GNU_UNIQUE;
6967 else if (flags & BSF_WEAK)
6969 else if (flags & BSF_GLOBAL)
6972 sym.st_info = ELF_ST_INFO (bind, type);
6975 if (type_ptr != NULL)
6977 sym.st_other = type_ptr->internal_elf_sym.st_other;
6978 sym.st_target_internal
6979 = type_ptr->internal_elf_sym.st_target_internal;
6984 sym.st_target_internal = 0;
6987 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6988 outbound_syms += bed->s->sizeof_sym;
6989 if (outbound_shndx != NULL)
6990 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6994 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6995 symstrtab_hdr->sh_type = SHT_STRTAB;
6997 symstrtab_hdr->sh_flags = 0;
6998 symstrtab_hdr->sh_addr = 0;
6999 symstrtab_hdr->sh_entsize = 0;
7000 symstrtab_hdr->sh_link = 0;
7001 symstrtab_hdr->sh_info = 0;
7002 symstrtab_hdr->sh_addralign = 1;
7007 /* Return the number of bytes required to hold the symtab vector.
7009 Note that we base it on the count plus 1, since we will null terminate
7010 the vector allocated based on this size. However, the ELF symbol table
7011 always has a dummy entry as symbol #0, so it ends up even. */
7014 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
7018 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7020 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7021 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7023 symtab_size -= sizeof (asymbol *);
7029 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7033 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7035 if (elf_dynsymtab (abfd) == 0)
7037 bfd_set_error (bfd_error_invalid_operation);
7041 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7042 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7044 symtab_size -= sizeof (asymbol *);
7050 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7053 return (asect->reloc_count + 1) * sizeof (arelent *);
7056 /* Canonicalize the relocs. */
7059 _bfd_elf_canonicalize_reloc (bfd *abfd,
7066 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7068 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7071 tblptr = section->relocation;
7072 for (i = 0; i < section->reloc_count; i++)
7073 *relptr++ = tblptr++;
7077 return section->reloc_count;
7081 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7083 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7084 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7087 bfd_get_symcount (abfd) = symcount;
7092 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7093 asymbol **allocation)
7095 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7096 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7099 bfd_get_dynamic_symcount (abfd) = symcount;
7103 /* Return the size required for the dynamic reloc entries. Any loadable
7104 section that was actually installed in the BFD, and has type SHT_REL
7105 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7106 dynamic reloc section. */
7109 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7114 if (elf_dynsymtab (abfd) == 0)
7116 bfd_set_error (bfd_error_invalid_operation);
7120 ret = sizeof (arelent *);
7121 for (s = abfd->sections; s != NULL; s = s->next)
7122 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7123 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7124 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7125 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7126 * sizeof (arelent *));
7131 /* Canonicalize the dynamic relocation entries. Note that we return the
7132 dynamic relocations as a single block, although they are actually
7133 associated with particular sections; the interface, which was
7134 designed for SunOS style shared libraries, expects that there is only
7135 one set of dynamic relocs. Any loadable section that was actually
7136 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7137 dynamic symbol table, is considered to be a dynamic reloc section. */
7140 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7144 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7148 if (elf_dynsymtab (abfd) == 0)
7150 bfd_set_error (bfd_error_invalid_operation);
7154 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7156 for (s = abfd->sections; s != NULL; s = s->next)
7158 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7159 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7160 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7165 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7167 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7169 for (i = 0; i < count; i++)
7180 /* Read in the version information. */
7183 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7185 bfd_byte *contents = NULL;
7186 unsigned int freeidx = 0;
7188 if (elf_dynverref (abfd) != 0)
7190 Elf_Internal_Shdr *hdr;
7191 Elf_External_Verneed *everneed;
7192 Elf_Internal_Verneed *iverneed;
7194 bfd_byte *contents_end;
7196 hdr = &elf_tdata (abfd)->dynverref_hdr;
7198 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7199 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
7200 if (elf_tdata (abfd)->verref == NULL)
7203 elf_tdata (abfd)->cverrefs = hdr->sh_info;
7205 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7206 if (contents == NULL)
7208 error_return_verref:
7209 elf_tdata (abfd)->verref = NULL;
7210 elf_tdata (abfd)->cverrefs = 0;
7213 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7214 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7215 goto error_return_verref;
7217 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
7218 goto error_return_verref;
7220 BFD_ASSERT (sizeof (Elf_External_Verneed)
7221 == sizeof (Elf_External_Vernaux));
7222 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7223 everneed = (Elf_External_Verneed *) contents;
7224 iverneed = elf_tdata (abfd)->verref;
7225 for (i = 0; i < hdr->sh_info; i++, iverneed++)
7227 Elf_External_Vernaux *evernaux;
7228 Elf_Internal_Vernaux *ivernaux;
7231 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7233 iverneed->vn_bfd = abfd;
7235 iverneed->vn_filename =
7236 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7238 if (iverneed->vn_filename == NULL)
7239 goto error_return_verref;
7241 if (iverneed->vn_cnt == 0)
7242 iverneed->vn_auxptr = NULL;
7245 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7246 bfd_alloc2 (abfd, iverneed->vn_cnt,
7247 sizeof (Elf_Internal_Vernaux));
7248 if (iverneed->vn_auxptr == NULL)
7249 goto error_return_verref;
7252 if (iverneed->vn_aux
7253 > (size_t) (contents_end - (bfd_byte *) everneed))
7254 goto error_return_verref;
7256 evernaux = ((Elf_External_Vernaux *)
7257 ((bfd_byte *) everneed + iverneed->vn_aux));
7258 ivernaux = iverneed->vn_auxptr;
7259 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7261 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7263 ivernaux->vna_nodename =
7264 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7265 ivernaux->vna_name);
7266 if (ivernaux->vna_nodename == NULL)
7267 goto error_return_verref;
7269 if (j + 1 < iverneed->vn_cnt)
7270 ivernaux->vna_nextptr = ivernaux + 1;
7272 ivernaux->vna_nextptr = NULL;
7274 if (ivernaux->vna_next
7275 > (size_t) (contents_end - (bfd_byte *) evernaux))
7276 goto error_return_verref;
7278 evernaux = ((Elf_External_Vernaux *)
7279 ((bfd_byte *) evernaux + ivernaux->vna_next));
7281 if (ivernaux->vna_other > freeidx)
7282 freeidx = ivernaux->vna_other;
7285 if (i + 1 < hdr->sh_info)
7286 iverneed->vn_nextref = iverneed + 1;
7288 iverneed->vn_nextref = NULL;
7290 if (iverneed->vn_next
7291 > (size_t) (contents_end - (bfd_byte *) everneed))
7292 goto error_return_verref;
7294 everneed = ((Elf_External_Verneed *)
7295 ((bfd_byte *) everneed + iverneed->vn_next));
7302 if (elf_dynverdef (abfd) != 0)
7304 Elf_Internal_Shdr *hdr;
7305 Elf_External_Verdef *everdef;
7306 Elf_Internal_Verdef *iverdef;
7307 Elf_Internal_Verdef *iverdefarr;
7308 Elf_Internal_Verdef iverdefmem;
7310 unsigned int maxidx;
7311 bfd_byte *contents_end_def, *contents_end_aux;
7313 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7315 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7316 if (contents == NULL)
7318 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7319 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7322 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
7325 BFD_ASSERT (sizeof (Elf_External_Verdef)
7326 >= sizeof (Elf_External_Verdaux));
7327 contents_end_def = contents + hdr->sh_size
7328 - sizeof (Elf_External_Verdef);
7329 contents_end_aux = contents + hdr->sh_size
7330 - sizeof (Elf_External_Verdaux);
7332 /* We know the number of entries in the section but not the maximum
7333 index. Therefore we have to run through all entries and find
7335 everdef = (Elf_External_Verdef *) contents;
7337 for (i = 0; i < hdr->sh_info; ++i)
7339 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7341 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7342 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7344 if (iverdefmem.vd_next
7345 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7348 everdef = ((Elf_External_Verdef *)
7349 ((bfd_byte *) everdef + iverdefmem.vd_next));
7352 if (default_imported_symver)
7354 if (freeidx > maxidx)
7359 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7360 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7361 if (elf_tdata (abfd)->verdef == NULL)
7364 elf_tdata (abfd)->cverdefs = maxidx;
7366 everdef = (Elf_External_Verdef *) contents;
7367 iverdefarr = elf_tdata (abfd)->verdef;
7368 for (i = 0; i < hdr->sh_info; i++)
7370 Elf_External_Verdaux *everdaux;
7371 Elf_Internal_Verdaux *iverdaux;
7374 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7376 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7378 error_return_verdef:
7379 elf_tdata (abfd)->verdef = NULL;
7380 elf_tdata (abfd)->cverdefs = 0;
7384 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7385 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7387 iverdef->vd_bfd = abfd;
7389 if (iverdef->vd_cnt == 0)
7390 iverdef->vd_auxptr = NULL;
7393 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7394 bfd_alloc2 (abfd, iverdef->vd_cnt,
7395 sizeof (Elf_Internal_Verdaux));
7396 if (iverdef->vd_auxptr == NULL)
7397 goto error_return_verdef;
7401 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7402 goto error_return_verdef;
7404 everdaux = ((Elf_External_Verdaux *)
7405 ((bfd_byte *) everdef + iverdef->vd_aux));
7406 iverdaux = iverdef->vd_auxptr;
7407 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7409 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7411 iverdaux->vda_nodename =
7412 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7413 iverdaux->vda_name);
7414 if (iverdaux->vda_nodename == NULL)
7415 goto error_return_verdef;
7417 if (j + 1 < iverdef->vd_cnt)
7418 iverdaux->vda_nextptr = iverdaux + 1;
7420 iverdaux->vda_nextptr = NULL;
7422 if (iverdaux->vda_next
7423 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7424 goto error_return_verdef;
7426 everdaux = ((Elf_External_Verdaux *)
7427 ((bfd_byte *) everdaux + iverdaux->vda_next));
7430 if (iverdef->vd_cnt)
7431 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7433 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7434 iverdef->vd_nextdef = iverdef + 1;
7436 iverdef->vd_nextdef = NULL;
7438 everdef = ((Elf_External_Verdef *)
7439 ((bfd_byte *) everdef + iverdef->vd_next));
7445 else if (default_imported_symver)
7452 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7453 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7454 if (elf_tdata (abfd)->verdef == NULL)
7457 elf_tdata (abfd)->cverdefs = freeidx;
7460 /* Create a default version based on the soname. */
7461 if (default_imported_symver)
7463 Elf_Internal_Verdef *iverdef;
7464 Elf_Internal_Verdaux *iverdaux;
7466 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
7468 iverdef->vd_version = VER_DEF_CURRENT;
7469 iverdef->vd_flags = 0;
7470 iverdef->vd_ndx = freeidx;
7471 iverdef->vd_cnt = 1;
7473 iverdef->vd_bfd = abfd;
7475 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7476 if (iverdef->vd_nodename == NULL)
7477 goto error_return_verdef;
7478 iverdef->vd_nextdef = NULL;
7479 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7480 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7481 if (iverdef->vd_auxptr == NULL)
7482 goto error_return_verdef;
7484 iverdaux = iverdef->vd_auxptr;
7485 iverdaux->vda_nodename = iverdef->vd_nodename;
7486 iverdaux->vda_nextptr = NULL;
7492 if (contents != NULL)
7498 _bfd_elf_make_empty_symbol (bfd *abfd)
7500 elf_symbol_type *newsym;
7501 bfd_size_type amt = sizeof (elf_symbol_type);
7503 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
7508 newsym->symbol.the_bfd = abfd;
7509 return &newsym->symbol;
7514 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7518 bfd_symbol_info (symbol, ret);
7521 /* Return whether a symbol name implies a local symbol. Most targets
7522 use this function for the is_local_label_name entry point, but some
7526 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7529 /* Normal local symbols start with ``.L''. */
7530 if (name[0] == '.' && name[1] == 'L')
7533 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7534 DWARF debugging symbols starting with ``..''. */
7535 if (name[0] == '.' && name[1] == '.')
7538 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7539 emitting DWARF debugging output. I suspect this is actually a
7540 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7541 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7542 underscore to be emitted on some ELF targets). For ease of use,
7543 we treat such symbols as local. */
7544 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7551 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7552 asymbol *symbol ATTRIBUTE_UNUSED)
7559 _bfd_elf_set_arch_mach (bfd *abfd,
7560 enum bfd_architecture arch,
7561 unsigned long machine)
7563 /* If this isn't the right architecture for this backend, and this
7564 isn't the generic backend, fail. */
7565 if (arch != get_elf_backend_data (abfd)->arch
7566 && arch != bfd_arch_unknown
7567 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7570 return bfd_default_set_arch_mach (abfd, arch, machine);
7573 /* Find the function to a particular section and offset,
7574 for error reporting. */
7577 elf_find_function (bfd *abfd,
7581 const char **filename_ptr,
7582 const char **functionname_ptr)
7584 struct elf_find_function_cache
7586 asection *last_section;
7588 const char *filename;
7589 bfd_size_type func_size;
7592 if (symbols == NULL)
7595 cache = elf_tdata (abfd)->elf_find_function_cache;
7598 cache = bfd_zalloc (abfd, sizeof (*cache));
7599 elf_tdata (abfd)->elf_find_function_cache = cache;
7603 if (cache->last_section != section
7604 || cache->func == NULL
7605 || offset < cache->func->value
7606 || offset >= cache->func->value + cache->func_size)
7611 /* ??? Given multiple file symbols, it is impossible to reliably
7612 choose the right file name for global symbols. File symbols are
7613 local symbols, and thus all file symbols must sort before any
7614 global symbols. The ELF spec may be interpreted to say that a
7615 file symbol must sort before other local symbols, but currently
7616 ld -r doesn't do this. So, for ld -r output, it is possible to
7617 make a better choice of file name for local symbols by ignoring
7618 file symbols appearing after a given local symbol. */
7619 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7620 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7624 state = nothing_seen;
7625 cache->filename = NULL;
7627 cache->func_size = 0;
7628 cache->last_section = section;
7630 for (p = symbols; *p != NULL; p++)
7636 if ((sym->flags & BSF_FILE) != 0)
7639 if (state == symbol_seen)
7640 state = file_after_symbol_seen;
7644 size = bed->maybe_function_sym (sym, section, &code_off);
7646 && code_off <= offset
7647 && (code_off > low_func
7648 || (code_off == low_func
7649 && size > cache->func_size)))
7652 cache->func_size = size;
7653 cache->filename = NULL;
7654 low_func = code_off;
7656 && ((sym->flags & BSF_LOCAL) != 0
7657 || state != file_after_symbol_seen))
7658 cache->filename = bfd_asymbol_name (file);
7660 if (state == nothing_seen)
7661 state = symbol_seen;
7665 if (cache->func == NULL)
7669 *filename_ptr = cache->filename;
7670 if (functionname_ptr)
7671 *functionname_ptr = bfd_asymbol_name (cache->func);
7676 /* Find the nearest line to a particular section and offset,
7677 for error reporting. */
7680 _bfd_elf_find_nearest_line (bfd *abfd,
7684 const char **filename_ptr,
7685 const char **functionname_ptr,
7686 unsigned int *line_ptr,
7687 unsigned int *discriminator_ptr)
7691 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7692 filename_ptr, functionname_ptr,
7693 line_ptr, discriminator_ptr,
7694 dwarf_debug_sections, 0,
7695 &elf_tdata (abfd)->dwarf2_find_line_info))
7697 if (!*functionname_ptr)
7698 elf_find_function (abfd, symbols, section, offset,
7699 *filename_ptr ? NULL : filename_ptr,
7705 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
7706 filename_ptr, functionname_ptr, line_ptr))
7708 if (!*functionname_ptr)
7709 elf_find_function (abfd, symbols, section, offset,
7710 *filename_ptr ? NULL : filename_ptr,
7716 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7717 &found, filename_ptr,
7718 functionname_ptr, line_ptr,
7719 &elf_tdata (abfd)->line_info))
7721 if (found && (*functionname_ptr || *line_ptr))
7724 if (symbols == NULL)
7727 if (! elf_find_function (abfd, symbols, section, offset,
7728 filename_ptr, functionname_ptr))
7735 /* Find the line for a symbol. */
7738 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7739 const char **filename_ptr, unsigned int *line_ptr)
7741 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
7742 filename_ptr, NULL, line_ptr, NULL,
7743 dwarf_debug_sections, 0,
7744 &elf_tdata (abfd)->dwarf2_find_line_info);
7747 /* After a call to bfd_find_nearest_line, successive calls to
7748 bfd_find_inliner_info can be used to get source information about
7749 each level of function inlining that terminated at the address
7750 passed to bfd_find_nearest_line. Currently this is only supported
7751 for DWARF2 with appropriate DWARF3 extensions. */
7754 _bfd_elf_find_inliner_info (bfd *abfd,
7755 const char **filename_ptr,
7756 const char **functionname_ptr,
7757 unsigned int *line_ptr)
7760 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7761 functionname_ptr, line_ptr,
7762 & elf_tdata (abfd)->dwarf2_find_line_info);
7767 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7769 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7770 int ret = bed->s->sizeof_ehdr;
7772 if (!info->relocatable)
7774 bfd_size_type phdr_size = elf_program_header_size (abfd);
7776 if (phdr_size == (bfd_size_type) -1)
7778 struct elf_segment_map *m;
7781 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
7782 phdr_size += bed->s->sizeof_phdr;
7785 phdr_size = get_program_header_size (abfd, info);
7788 elf_program_header_size (abfd) = phdr_size;
7796 _bfd_elf_set_section_contents (bfd *abfd,
7798 const void *location,
7800 bfd_size_type count)
7802 Elf_Internal_Shdr *hdr;
7805 if (! abfd->output_has_begun
7806 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7809 hdr = &elf_section_data (section)->this_hdr;
7810 pos = hdr->sh_offset + offset;
7811 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7812 || bfd_bwrite (location, count, abfd) != count)
7819 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7820 arelent *cache_ptr ATTRIBUTE_UNUSED,
7821 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7826 /* Try to convert a non-ELF reloc into an ELF one. */
7829 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7831 /* Check whether we really have an ELF howto. */
7833 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7835 bfd_reloc_code_real_type code;
7836 reloc_howto_type *howto;
7838 /* Alien reloc: Try to determine its type to replace it with an
7839 equivalent ELF reloc. */
7841 if (areloc->howto->pc_relative)
7843 switch (areloc->howto->bitsize)
7846 code = BFD_RELOC_8_PCREL;
7849 code = BFD_RELOC_12_PCREL;
7852 code = BFD_RELOC_16_PCREL;
7855 code = BFD_RELOC_24_PCREL;
7858 code = BFD_RELOC_32_PCREL;
7861 code = BFD_RELOC_64_PCREL;
7867 howto = bfd_reloc_type_lookup (abfd, code);
7869 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7871 if (howto->pcrel_offset)
7872 areloc->addend += areloc->address;
7874 areloc->addend -= areloc->address; /* addend is unsigned!! */
7879 switch (areloc->howto->bitsize)
7885 code = BFD_RELOC_14;
7888 code = BFD_RELOC_16;
7891 code = BFD_RELOC_26;
7894 code = BFD_RELOC_32;
7897 code = BFD_RELOC_64;
7903 howto = bfd_reloc_type_lookup (abfd, code);
7907 areloc->howto = howto;
7915 (*_bfd_error_handler)
7916 (_("%B: unsupported relocation type %s"),
7917 abfd, areloc->howto->name);
7918 bfd_set_error (bfd_error_bad_value);
7923 _bfd_elf_close_and_cleanup (bfd *abfd)
7925 struct elf_obj_tdata *tdata = elf_tdata (abfd);
7926 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
7928 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
7929 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7930 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
7933 return _bfd_generic_close_and_cleanup (abfd);
7936 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7937 in the relocation's offset. Thus we cannot allow any sort of sanity
7938 range-checking to interfere. There is nothing else to do in processing
7941 bfd_reloc_status_type
7942 _bfd_elf_rel_vtable_reloc_fn
7943 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7944 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7945 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7946 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7948 return bfd_reloc_ok;
7951 /* Elf core file support. Much of this only works on native
7952 toolchains, since we rely on knowing the
7953 machine-dependent procfs structure in order to pick
7954 out details about the corefile. */
7956 #ifdef HAVE_SYS_PROCFS_H
7957 /* Needed for new procfs interface on sparc-solaris. */
7958 # define _STRUCTURED_PROC 1
7959 # include <sys/procfs.h>
7962 /* Return a PID that identifies a "thread" for threaded cores, or the
7963 PID of the main process for non-threaded cores. */
7966 elfcore_make_pid (bfd *abfd)
7970 pid = elf_tdata (abfd)->core->lwpid;
7972 pid = elf_tdata (abfd)->core->pid;
7977 /* If there isn't a section called NAME, make one, using
7978 data from SECT. Note, this function will generate a
7979 reference to NAME, so you shouldn't deallocate or
7983 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7987 if (bfd_get_section_by_name (abfd, name) != NULL)
7990 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7994 sect2->size = sect->size;
7995 sect2->filepos = sect->filepos;
7996 sect2->alignment_power = sect->alignment_power;
8000 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8001 actually creates up to two pseudosections:
8002 - For the single-threaded case, a section named NAME, unless
8003 such a section already exists.
8004 - For the multi-threaded case, a section named "NAME/PID", where
8005 PID is elfcore_make_pid (abfd).
8006 Both pseudosections have identical contents. */
8008 _bfd_elfcore_make_pseudosection (bfd *abfd,
8014 char *threaded_name;
8018 /* Build the section name. */
8020 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8021 len = strlen (buf) + 1;
8022 threaded_name = (char *) bfd_alloc (abfd, len);
8023 if (threaded_name == NULL)
8025 memcpy (threaded_name, buf, len);
8027 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8032 sect->filepos = filepos;
8033 sect->alignment_power = 2;
8035 return elfcore_maybe_make_sect (abfd, name, sect);
8038 /* prstatus_t exists on:
8040 linux 2.[01] + glibc
8044 #if defined (HAVE_PRSTATUS_T)
8047 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8052 if (note->descsz == sizeof (prstatus_t))
8056 size = sizeof (prstat.pr_reg);
8057 offset = offsetof (prstatus_t, pr_reg);
8058 memcpy (&prstat, note->descdata, sizeof (prstat));
8060 /* Do not overwrite the core signal if it
8061 has already been set by another thread. */
8062 if (elf_tdata (abfd)->core->signal == 0)
8063 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8064 if (elf_tdata (abfd)->core->pid == 0)
8065 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8067 /* pr_who exists on:
8070 pr_who doesn't exist on:
8073 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8074 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8076 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8079 #if defined (HAVE_PRSTATUS32_T)
8080 else if (note->descsz == sizeof (prstatus32_t))
8082 /* 64-bit host, 32-bit corefile */
8083 prstatus32_t prstat;
8085 size = sizeof (prstat.pr_reg);
8086 offset = offsetof (prstatus32_t, pr_reg);
8087 memcpy (&prstat, note->descdata, sizeof (prstat));
8089 /* Do not overwrite the core signal if it
8090 has already been set by another thread. */
8091 if (elf_tdata (abfd)->core->signal == 0)
8092 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8093 if (elf_tdata (abfd)->core->pid == 0)
8094 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8096 /* pr_who exists on:
8099 pr_who doesn't exist on:
8102 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8103 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8105 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8108 #endif /* HAVE_PRSTATUS32_T */
8111 /* Fail - we don't know how to handle any other
8112 note size (ie. data object type). */
8116 /* Make a ".reg/999" section and a ".reg" section. */
8117 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8118 size, note->descpos + offset);
8120 #endif /* defined (HAVE_PRSTATUS_T) */
8122 /* Create a pseudosection containing the exact contents of NOTE. */
8124 elfcore_make_note_pseudosection (bfd *abfd,
8126 Elf_Internal_Note *note)
8128 return _bfd_elfcore_make_pseudosection (abfd, name,
8129 note->descsz, note->descpos);
8132 /* There isn't a consistent prfpregset_t across platforms,
8133 but it doesn't matter, because we don't have to pick this
8134 data structure apart. */
8137 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8139 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8142 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8143 type of NT_PRXFPREG. Just include the whole note's contents
8147 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8149 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8152 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8153 with a note type of NT_X86_XSTATE. Just include the whole note's
8154 contents literally. */
8157 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8159 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8163 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8165 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8169 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8171 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8175 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8177 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8181 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8183 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8187 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8189 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8193 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8195 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8199 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8201 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8205 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8207 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8211 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8213 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8217 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8219 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8223 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8225 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8229 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8231 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8235 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8237 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8241 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8243 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8247 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8249 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8252 #if defined (HAVE_PRPSINFO_T)
8253 typedef prpsinfo_t elfcore_psinfo_t;
8254 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8255 typedef prpsinfo32_t elfcore_psinfo32_t;
8259 #if defined (HAVE_PSINFO_T)
8260 typedef psinfo_t elfcore_psinfo_t;
8261 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8262 typedef psinfo32_t elfcore_psinfo32_t;
8266 /* return a malloc'ed copy of a string at START which is at
8267 most MAX bytes long, possibly without a terminating '\0'.
8268 the copy will always have a terminating '\0'. */
8271 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8274 char *end = (char *) memchr (start, '\0', max);
8282 dups = (char *) bfd_alloc (abfd, len + 1);
8286 memcpy (dups, start, len);
8292 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8294 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8296 if (note->descsz == sizeof (elfcore_psinfo_t))
8298 elfcore_psinfo_t psinfo;
8300 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8302 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8303 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8305 elf_tdata (abfd)->core->program
8306 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8307 sizeof (psinfo.pr_fname));
8309 elf_tdata (abfd)->core->command
8310 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8311 sizeof (psinfo.pr_psargs));
8313 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8314 else if (note->descsz == sizeof (elfcore_psinfo32_t))
8316 /* 64-bit host, 32-bit corefile */
8317 elfcore_psinfo32_t psinfo;
8319 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8321 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8322 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8324 elf_tdata (abfd)->core->program
8325 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8326 sizeof (psinfo.pr_fname));
8328 elf_tdata (abfd)->core->command
8329 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8330 sizeof (psinfo.pr_psargs));
8336 /* Fail - we don't know how to handle any other
8337 note size (ie. data object type). */
8341 /* Note that for some reason, a spurious space is tacked
8342 onto the end of the args in some (at least one anyway)
8343 implementations, so strip it off if it exists. */
8346 char *command = elf_tdata (abfd)->core->command;
8347 int n = strlen (command);
8349 if (0 < n && command[n - 1] == ' ')
8350 command[n - 1] = '\0';
8355 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8357 #if defined (HAVE_PSTATUS_T)
8359 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8361 if (note->descsz == sizeof (pstatus_t)
8362 #if defined (HAVE_PXSTATUS_T)
8363 || note->descsz == sizeof (pxstatus_t)
8369 memcpy (&pstat, note->descdata, sizeof (pstat));
8371 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8373 #if defined (HAVE_PSTATUS32_T)
8374 else if (note->descsz == sizeof (pstatus32_t))
8376 /* 64-bit host, 32-bit corefile */
8379 memcpy (&pstat, note->descdata, sizeof (pstat));
8381 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8384 /* Could grab some more details from the "representative"
8385 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8386 NT_LWPSTATUS note, presumably. */
8390 #endif /* defined (HAVE_PSTATUS_T) */
8392 #if defined (HAVE_LWPSTATUS_T)
8394 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8396 lwpstatus_t lwpstat;
8402 if (note->descsz != sizeof (lwpstat)
8403 #if defined (HAVE_LWPXSTATUS_T)
8404 && note->descsz != sizeof (lwpxstatus_t)
8409 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8411 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8412 /* Do not overwrite the core signal if it has already been set by
8414 if (elf_tdata (abfd)->core->signal == 0)
8415 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8417 /* Make a ".reg/999" section. */
8419 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8420 len = strlen (buf) + 1;
8421 name = bfd_alloc (abfd, len);
8424 memcpy (name, buf, len);
8426 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8430 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8431 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8432 sect->filepos = note->descpos
8433 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8436 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8437 sect->size = sizeof (lwpstat.pr_reg);
8438 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8441 sect->alignment_power = 2;
8443 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8446 /* Make a ".reg2/999" section */
8448 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8449 len = strlen (buf) + 1;
8450 name = bfd_alloc (abfd, len);
8453 memcpy (name, buf, len);
8455 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8459 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8460 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8461 sect->filepos = note->descpos
8462 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8465 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8466 sect->size = sizeof (lwpstat.pr_fpreg);
8467 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8470 sect->alignment_power = 2;
8472 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8474 #endif /* defined (HAVE_LWPSTATUS_T) */
8477 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8484 int is_active_thread;
8487 if (note->descsz < 728)
8490 if (! CONST_STRNEQ (note->namedata, "win32"))
8493 type = bfd_get_32 (abfd, note->descdata);
8497 case 1 /* NOTE_INFO_PROCESS */:
8498 /* FIXME: need to add ->core->command. */
8499 /* process_info.pid */
8500 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
8501 /* process_info.signal */
8502 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
8505 case 2 /* NOTE_INFO_THREAD */:
8506 /* Make a ".reg/999" section. */
8507 /* thread_info.tid */
8508 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8510 len = strlen (buf) + 1;
8511 name = (char *) bfd_alloc (abfd, len);
8515 memcpy (name, buf, len);
8517 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8521 /* sizeof (thread_info.thread_context) */
8523 /* offsetof (thread_info.thread_context) */
8524 sect->filepos = note->descpos + 12;
8525 sect->alignment_power = 2;
8527 /* thread_info.is_active_thread */
8528 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8530 if (is_active_thread)
8531 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8535 case 3 /* NOTE_INFO_MODULE */:
8536 /* Make a ".module/xxxxxxxx" section. */
8537 /* module_info.base_address */
8538 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8539 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8541 len = strlen (buf) + 1;
8542 name = (char *) bfd_alloc (abfd, len);
8546 memcpy (name, buf, len);
8548 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8553 sect->size = note->descsz;
8554 sect->filepos = note->descpos;
8555 sect->alignment_power = 2;
8566 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8568 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8576 if (bed->elf_backend_grok_prstatus)
8577 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8579 #if defined (HAVE_PRSTATUS_T)
8580 return elfcore_grok_prstatus (abfd, note);
8585 #if defined (HAVE_PSTATUS_T)
8587 return elfcore_grok_pstatus (abfd, note);
8590 #if defined (HAVE_LWPSTATUS_T)
8592 return elfcore_grok_lwpstatus (abfd, note);
8595 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8596 return elfcore_grok_prfpreg (abfd, note);
8598 case NT_WIN32PSTATUS:
8599 return elfcore_grok_win32pstatus (abfd, note);
8601 case NT_PRXFPREG: /* Linux SSE extension */
8602 if (note->namesz == 6
8603 && strcmp (note->namedata, "LINUX") == 0)
8604 return elfcore_grok_prxfpreg (abfd, note);
8608 case NT_X86_XSTATE: /* Linux XSAVE extension */
8609 if (note->namesz == 6
8610 && strcmp (note->namedata, "LINUX") == 0)
8611 return elfcore_grok_xstatereg (abfd, note);
8616 if (note->namesz == 6
8617 && strcmp (note->namedata, "LINUX") == 0)
8618 return elfcore_grok_ppc_vmx (abfd, note);
8623 if (note->namesz == 6
8624 && strcmp (note->namedata, "LINUX") == 0)
8625 return elfcore_grok_ppc_vsx (abfd, note);
8629 case NT_S390_HIGH_GPRS:
8630 if (note->namesz == 6
8631 && strcmp (note->namedata, "LINUX") == 0)
8632 return elfcore_grok_s390_high_gprs (abfd, note);
8637 if (note->namesz == 6
8638 && strcmp (note->namedata, "LINUX") == 0)
8639 return elfcore_grok_s390_timer (abfd, note);
8643 case NT_S390_TODCMP:
8644 if (note->namesz == 6
8645 && strcmp (note->namedata, "LINUX") == 0)
8646 return elfcore_grok_s390_todcmp (abfd, note);
8650 case NT_S390_TODPREG:
8651 if (note->namesz == 6
8652 && strcmp (note->namedata, "LINUX") == 0)
8653 return elfcore_grok_s390_todpreg (abfd, note);
8658 if (note->namesz == 6
8659 && strcmp (note->namedata, "LINUX") == 0)
8660 return elfcore_grok_s390_ctrs (abfd, note);
8664 case NT_S390_PREFIX:
8665 if (note->namesz == 6
8666 && strcmp (note->namedata, "LINUX") == 0)
8667 return elfcore_grok_s390_prefix (abfd, note);
8671 case NT_S390_LAST_BREAK:
8672 if (note->namesz == 6
8673 && strcmp (note->namedata, "LINUX") == 0)
8674 return elfcore_grok_s390_last_break (abfd, note);
8678 case NT_S390_SYSTEM_CALL:
8679 if (note->namesz == 6
8680 && strcmp (note->namedata, "LINUX") == 0)
8681 return elfcore_grok_s390_system_call (abfd, note);
8686 if (note->namesz == 6
8687 && strcmp (note->namedata, "LINUX") == 0)
8688 return elfcore_grok_s390_tdb (abfd, note);
8693 if (note->namesz == 6
8694 && strcmp (note->namedata, "LINUX") == 0)
8695 return elfcore_grok_arm_vfp (abfd, note);
8700 if (note->namesz == 6
8701 && strcmp (note->namedata, "LINUX") == 0)
8702 return elfcore_grok_aarch_tls (abfd, note);
8706 case NT_ARM_HW_BREAK:
8707 if (note->namesz == 6
8708 && strcmp (note->namedata, "LINUX") == 0)
8709 return elfcore_grok_aarch_hw_break (abfd, note);
8713 case NT_ARM_HW_WATCH:
8714 if (note->namesz == 6
8715 && strcmp (note->namedata, "LINUX") == 0)
8716 return elfcore_grok_aarch_hw_watch (abfd, note);
8722 if (bed->elf_backend_grok_psinfo)
8723 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8725 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8726 return elfcore_grok_psinfo (abfd, note);
8733 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8738 sect->size = note->descsz;
8739 sect->filepos = note->descpos;
8740 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8746 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
8750 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
8756 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8758 struct elf_obj_tdata *t;
8760 if (note->descsz == 0)
8763 t = elf_tdata (abfd);
8764 t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
8765 if (t->build_id == NULL)
8768 t->build_id->size = note->descsz;
8769 memcpy (t->build_id->data, note->descdata, note->descsz);
8775 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8782 case NT_GNU_BUILD_ID:
8783 return elfobj_grok_gnu_build_id (abfd, note);
8788 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
8790 struct sdt_note *cur =
8791 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
8794 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
8795 cur->size = (bfd_size_type) note->descsz;
8796 memcpy (cur->data, note->descdata, note->descsz);
8798 elf_tdata (abfd)->sdt_note_head = cur;
8804 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
8809 return elfobj_grok_stapsdt_note_1 (abfd, note);
8817 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8821 cp = strchr (note->namedata, '@');
8824 *lwpidp = atoi(cp + 1);
8831 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8833 /* Signal number at offset 0x08. */
8834 elf_tdata (abfd)->core->signal
8835 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8837 /* Process ID at offset 0x50. */
8838 elf_tdata (abfd)->core->pid
8839 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8841 /* Command name at 0x7c (max 32 bytes, including nul). */
8842 elf_tdata (abfd)->core->command
8843 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8845 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8850 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8854 if (elfcore_netbsd_get_lwpid (note, &lwp))
8855 elf_tdata (abfd)->core->lwpid = lwp;
8857 if (note->type == NT_NETBSDCORE_PROCINFO)
8859 /* NetBSD-specific core "procinfo". Note that we expect to
8860 find this note before any of the others, which is fine,
8861 since the kernel writes this note out first when it
8862 creates a core file. */
8864 return elfcore_grok_netbsd_procinfo (abfd, note);
8867 /* As of Jan 2002 there are no other machine-independent notes
8868 defined for NetBSD core files. If the note type is less
8869 than the start of the machine-dependent note types, we don't
8872 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8876 switch (bfd_get_arch (abfd))
8878 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8879 PT_GETFPREGS == mach+2. */
8881 case bfd_arch_alpha:
8882 case bfd_arch_sparc:
8885 case NT_NETBSDCORE_FIRSTMACH+0:
8886 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8888 case NT_NETBSDCORE_FIRSTMACH+2:
8889 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8895 /* On all other arch's, PT_GETREGS == mach+1 and
8896 PT_GETFPREGS == mach+3. */
8901 case NT_NETBSDCORE_FIRSTMACH+1:
8902 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8904 case NT_NETBSDCORE_FIRSTMACH+3:
8905 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8915 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8917 /* Signal number at offset 0x08. */
8918 elf_tdata (abfd)->core->signal
8919 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8921 /* Process ID at offset 0x20. */
8922 elf_tdata (abfd)->core->pid
8923 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8925 /* Command name at 0x48 (max 32 bytes, including nul). */
8926 elf_tdata (abfd)->core->command
8927 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8933 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8935 if (note->type == NT_OPENBSD_PROCINFO)
8936 return elfcore_grok_openbsd_procinfo (abfd, note);
8938 if (note->type == NT_OPENBSD_REGS)
8939 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8941 if (note->type == NT_OPENBSD_FPREGS)
8942 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8944 if (note->type == NT_OPENBSD_XFPREGS)
8945 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8947 if (note->type == NT_OPENBSD_AUXV)
8949 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8954 sect->size = note->descsz;
8955 sect->filepos = note->descpos;
8956 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8961 if (note->type == NT_OPENBSD_WCOOKIE)
8963 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8968 sect->size = note->descsz;
8969 sect->filepos = note->descpos;
8970 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8979 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8981 void *ddata = note->descdata;
8988 /* nto_procfs_status 'pid' field is at offset 0. */
8989 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8991 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8992 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8994 /* nto_procfs_status 'flags' field is at offset 8. */
8995 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8997 /* nto_procfs_status 'what' field is at offset 14. */
8998 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9000 elf_tdata (abfd)->core->signal = sig;
9001 elf_tdata (abfd)->core->lwpid = *tid;
9004 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9005 do not come from signals so we make sure we set the current
9006 thread just in case. */
9007 if (flags & 0x00000080)
9008 elf_tdata (abfd)->core->lwpid = *tid;
9010 /* Make a ".qnx_core_status/%d" section. */
9011 sprintf (buf, ".qnx_core_status/%ld", *tid);
9013 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9018 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9022 sect->size = note->descsz;
9023 sect->filepos = note->descpos;
9024 sect->alignment_power = 2;
9026 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9030 elfcore_grok_nto_regs (bfd *abfd,
9031 Elf_Internal_Note *note,
9039 /* Make a "(base)/%d" section. */
9040 sprintf (buf, "%s/%ld", base, tid);
9042 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9047 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9051 sect->size = note->descsz;
9052 sect->filepos = note->descpos;
9053 sect->alignment_power = 2;
9055 /* This is the current thread. */
9056 if (elf_tdata (abfd)->core->lwpid == tid)
9057 return elfcore_maybe_make_sect (abfd, base, sect);
9062 #define BFD_QNT_CORE_INFO 7
9063 #define BFD_QNT_CORE_STATUS 8
9064 #define BFD_QNT_CORE_GREG 9
9065 #define BFD_QNT_CORE_FPREG 10
9068 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9070 /* Every GREG section has a STATUS section before it. Store the
9071 tid from the previous call to pass down to the next gregs
9073 static long tid = 1;
9077 case BFD_QNT_CORE_INFO:
9078 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9079 case BFD_QNT_CORE_STATUS:
9080 return elfcore_grok_nto_status (abfd, note, &tid);
9081 case BFD_QNT_CORE_GREG:
9082 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9083 case BFD_QNT_CORE_FPREG:
9084 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9091 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9097 /* Use note name as section name. */
9099 name = (char *) bfd_alloc (abfd, len);
9102 memcpy (name, note->namedata, len);
9103 name[len - 1] = '\0';
9105 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9109 sect->size = note->descsz;
9110 sect->filepos = note->descpos;
9111 sect->alignment_power = 1;
9116 /* Function: elfcore_write_note
9119 buffer to hold note, and current size of buffer
9123 size of data for note
9125 Writes note to end of buffer. ELF64 notes are written exactly as
9126 for ELF32, despite the current (as of 2006) ELF gabi specifying
9127 that they ought to have 8-byte namesz and descsz field, and have
9128 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9131 Pointer to realloc'd buffer, *BUFSIZ updated. */
9134 elfcore_write_note (bfd *abfd,
9142 Elf_External_Note *xnp;
9149 namesz = strlen (name) + 1;
9151 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9153 buf = (char *) realloc (buf, *bufsiz + newspace);
9156 dest = buf + *bufsiz;
9157 *bufsiz += newspace;
9158 xnp = (Elf_External_Note *) dest;
9159 H_PUT_32 (abfd, namesz, xnp->namesz);
9160 H_PUT_32 (abfd, size, xnp->descsz);
9161 H_PUT_32 (abfd, type, xnp->type);
9165 memcpy (dest, name, namesz);
9173 memcpy (dest, input, size);
9184 elfcore_write_prpsinfo (bfd *abfd,
9190 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9192 if (bed->elf_backend_write_core_note != NULL)
9195 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9196 NT_PRPSINFO, fname, psargs);
9201 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9202 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9203 if (bed->s->elfclass == ELFCLASS32)
9205 #if defined (HAVE_PSINFO32_T)
9207 int note_type = NT_PSINFO;
9210 int note_type = NT_PRPSINFO;
9213 memset (&data, 0, sizeof (data));
9214 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9215 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9216 return elfcore_write_note (abfd, buf, bufsiz,
9217 "CORE", note_type, &data, sizeof (data));
9222 #if defined (HAVE_PSINFO_T)
9224 int note_type = NT_PSINFO;
9227 int note_type = NT_PRPSINFO;
9230 memset (&data, 0, sizeof (data));
9231 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9232 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9233 return elfcore_write_note (abfd, buf, bufsiz,
9234 "CORE", note_type, &data, sizeof (data));
9236 #endif /* PSINFO_T or PRPSINFO_T */
9243 elfcore_write_linux_prpsinfo32
9244 (bfd *abfd, char *buf, int *bufsiz,
9245 const struct elf_internal_linux_prpsinfo *prpsinfo)
9247 struct elf_external_linux_prpsinfo32 data;
9249 memset (&data, 0, sizeof (data));
9250 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9252 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9253 &data, sizeof (data));
9257 elfcore_write_linux_prpsinfo64
9258 (bfd *abfd, char *buf, int *bufsiz,
9259 const struct elf_internal_linux_prpsinfo *prpsinfo)
9261 struct elf_external_linux_prpsinfo64 data;
9263 memset (&data, 0, sizeof (data));
9264 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9266 return elfcore_write_note (abfd, buf, bufsiz,
9267 "CORE", NT_PRPSINFO, &data, sizeof (data));
9271 elfcore_write_prstatus (bfd *abfd,
9278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9280 if (bed->elf_backend_write_core_note != NULL)
9283 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9285 pid, cursig, gregs);
9290 #if defined (HAVE_PRSTATUS_T)
9291 #if defined (HAVE_PRSTATUS32_T)
9292 if (bed->s->elfclass == ELFCLASS32)
9294 prstatus32_t prstat;
9296 memset (&prstat, 0, sizeof (prstat));
9297 prstat.pr_pid = pid;
9298 prstat.pr_cursig = cursig;
9299 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9300 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9301 NT_PRSTATUS, &prstat, sizeof (prstat));
9308 memset (&prstat, 0, sizeof (prstat));
9309 prstat.pr_pid = pid;
9310 prstat.pr_cursig = cursig;
9311 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9312 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9313 NT_PRSTATUS, &prstat, sizeof (prstat));
9315 #endif /* HAVE_PRSTATUS_T */
9321 #if defined (HAVE_LWPSTATUS_T)
9323 elfcore_write_lwpstatus (bfd *abfd,
9330 lwpstatus_t lwpstat;
9331 const char *note_name = "CORE";
9333 memset (&lwpstat, 0, sizeof (lwpstat));
9334 lwpstat.pr_lwpid = pid >> 16;
9335 lwpstat.pr_cursig = cursig;
9336 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9337 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9338 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9340 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9341 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9343 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9344 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9347 return elfcore_write_note (abfd, buf, bufsiz, note_name,
9348 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9350 #endif /* HAVE_LWPSTATUS_T */
9352 #if defined (HAVE_PSTATUS_T)
9354 elfcore_write_pstatus (bfd *abfd,
9358 int cursig ATTRIBUTE_UNUSED,
9359 const void *gregs ATTRIBUTE_UNUSED)
9361 const char *note_name = "CORE";
9362 #if defined (HAVE_PSTATUS32_T)
9363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9365 if (bed->s->elfclass == ELFCLASS32)
9369 memset (&pstat, 0, sizeof (pstat));
9370 pstat.pr_pid = pid & 0xffff;
9371 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9372 NT_PSTATUS, &pstat, sizeof (pstat));
9380 memset (&pstat, 0, sizeof (pstat));
9381 pstat.pr_pid = pid & 0xffff;
9382 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9383 NT_PSTATUS, &pstat, sizeof (pstat));
9387 #endif /* HAVE_PSTATUS_T */
9390 elfcore_write_prfpreg (bfd *abfd,
9396 const char *note_name = "CORE";
9397 return elfcore_write_note (abfd, buf, bufsiz,
9398 note_name, NT_FPREGSET, fpregs, size);
9402 elfcore_write_prxfpreg (bfd *abfd,
9405 const void *xfpregs,
9408 char *note_name = "LINUX";
9409 return elfcore_write_note (abfd, buf, bufsiz,
9410 note_name, NT_PRXFPREG, xfpregs, size);
9414 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9415 const void *xfpregs, int size)
9417 char *note_name = "LINUX";
9418 return elfcore_write_note (abfd, buf, bufsiz,
9419 note_name, NT_X86_XSTATE, xfpregs, size);
9423 elfcore_write_ppc_vmx (bfd *abfd,
9426 const void *ppc_vmx,
9429 char *note_name = "LINUX";
9430 return elfcore_write_note (abfd, buf, bufsiz,
9431 note_name, NT_PPC_VMX, ppc_vmx, size);
9435 elfcore_write_ppc_vsx (bfd *abfd,
9438 const void *ppc_vsx,
9441 char *note_name = "LINUX";
9442 return elfcore_write_note (abfd, buf, bufsiz,
9443 note_name, NT_PPC_VSX, ppc_vsx, size);
9447 elfcore_write_s390_high_gprs (bfd *abfd,
9450 const void *s390_high_gprs,
9453 char *note_name = "LINUX";
9454 return elfcore_write_note (abfd, buf, bufsiz,
9455 note_name, NT_S390_HIGH_GPRS,
9456 s390_high_gprs, size);
9460 elfcore_write_s390_timer (bfd *abfd,
9463 const void *s390_timer,
9466 char *note_name = "LINUX";
9467 return elfcore_write_note (abfd, buf, bufsiz,
9468 note_name, NT_S390_TIMER, s390_timer, size);
9472 elfcore_write_s390_todcmp (bfd *abfd,
9475 const void *s390_todcmp,
9478 char *note_name = "LINUX";
9479 return elfcore_write_note (abfd, buf, bufsiz,
9480 note_name, NT_S390_TODCMP, s390_todcmp, size);
9484 elfcore_write_s390_todpreg (bfd *abfd,
9487 const void *s390_todpreg,
9490 char *note_name = "LINUX";
9491 return elfcore_write_note (abfd, buf, bufsiz,
9492 note_name, NT_S390_TODPREG, s390_todpreg, size);
9496 elfcore_write_s390_ctrs (bfd *abfd,
9499 const void *s390_ctrs,
9502 char *note_name = "LINUX";
9503 return elfcore_write_note (abfd, buf, bufsiz,
9504 note_name, NT_S390_CTRS, s390_ctrs, size);
9508 elfcore_write_s390_prefix (bfd *abfd,
9511 const void *s390_prefix,
9514 char *note_name = "LINUX";
9515 return elfcore_write_note (abfd, buf, bufsiz,
9516 note_name, NT_S390_PREFIX, s390_prefix, size);
9520 elfcore_write_s390_last_break (bfd *abfd,
9523 const void *s390_last_break,
9526 char *note_name = "LINUX";
9527 return elfcore_write_note (abfd, buf, bufsiz,
9528 note_name, NT_S390_LAST_BREAK,
9529 s390_last_break, size);
9533 elfcore_write_s390_system_call (bfd *abfd,
9536 const void *s390_system_call,
9539 char *note_name = "LINUX";
9540 return elfcore_write_note (abfd, buf, bufsiz,
9541 note_name, NT_S390_SYSTEM_CALL,
9542 s390_system_call, size);
9546 elfcore_write_s390_tdb (bfd *abfd,
9549 const void *s390_tdb,
9552 char *note_name = "LINUX";
9553 return elfcore_write_note (abfd, buf, bufsiz,
9554 note_name, NT_S390_TDB, s390_tdb, size);
9558 elfcore_write_arm_vfp (bfd *abfd,
9561 const void *arm_vfp,
9564 char *note_name = "LINUX";
9565 return elfcore_write_note (abfd, buf, bufsiz,
9566 note_name, NT_ARM_VFP, arm_vfp, size);
9570 elfcore_write_aarch_tls (bfd *abfd,
9573 const void *aarch_tls,
9576 char *note_name = "LINUX";
9577 return elfcore_write_note (abfd, buf, bufsiz,
9578 note_name, NT_ARM_TLS, aarch_tls, size);
9582 elfcore_write_aarch_hw_break (bfd *abfd,
9585 const void *aarch_hw_break,
9588 char *note_name = "LINUX";
9589 return elfcore_write_note (abfd, buf, bufsiz,
9590 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
9594 elfcore_write_aarch_hw_watch (bfd *abfd,
9597 const void *aarch_hw_watch,
9600 char *note_name = "LINUX";
9601 return elfcore_write_note (abfd, buf, bufsiz,
9602 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
9606 elfcore_write_register_note (bfd *abfd,
9609 const char *section,
9613 if (strcmp (section, ".reg2") == 0)
9614 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9615 if (strcmp (section, ".reg-xfp") == 0)
9616 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9617 if (strcmp (section, ".reg-xstate") == 0)
9618 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9619 if (strcmp (section, ".reg-ppc-vmx") == 0)
9620 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9621 if (strcmp (section, ".reg-ppc-vsx") == 0)
9622 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9623 if (strcmp (section, ".reg-s390-high-gprs") == 0)
9624 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9625 if (strcmp (section, ".reg-s390-timer") == 0)
9626 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9627 if (strcmp (section, ".reg-s390-todcmp") == 0)
9628 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9629 if (strcmp (section, ".reg-s390-todpreg") == 0)
9630 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9631 if (strcmp (section, ".reg-s390-ctrs") == 0)
9632 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9633 if (strcmp (section, ".reg-s390-prefix") == 0)
9634 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9635 if (strcmp (section, ".reg-s390-last-break") == 0)
9636 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
9637 if (strcmp (section, ".reg-s390-system-call") == 0)
9638 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
9639 if (strcmp (section, ".reg-s390-tdb") == 0)
9640 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
9641 if (strcmp (section, ".reg-arm-vfp") == 0)
9642 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
9643 if (strcmp (section, ".reg-aarch-tls") == 0)
9644 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
9645 if (strcmp (section, ".reg-aarch-hw-break") == 0)
9646 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
9647 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
9648 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
9653 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9658 while (p < buf + size)
9660 /* FIXME: bad alignment assumption. */
9661 Elf_External_Note *xnp = (Elf_External_Note *) p;
9662 Elf_Internal_Note in;
9664 if (offsetof (Elf_External_Note, name) > buf - p + size)
9667 in.type = H_GET_32 (abfd, xnp->type);
9669 in.namesz = H_GET_32 (abfd, xnp->namesz);
9670 in.namedata = xnp->name;
9671 if (in.namesz > buf - in.namedata + size)
9674 in.descsz = H_GET_32 (abfd, xnp->descsz);
9675 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9676 in.descpos = offset + (in.descdata - buf);
9678 && (in.descdata >= buf + size
9679 || in.descsz > buf - in.descdata + size))
9682 switch (bfd_get_format (abfd))
9688 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
9690 if (! elfcore_grok_netbsd_note (abfd, &in))
9693 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
9695 if (! elfcore_grok_openbsd_note (abfd, &in))
9698 else if (CONST_STRNEQ (in.namedata, "QNX"))
9700 if (! elfcore_grok_nto_note (abfd, &in))
9703 else if (CONST_STRNEQ (in.namedata, "SPU/"))
9705 if (! elfcore_grok_spu_note (abfd, &in))
9710 if (! elfcore_grok_note (abfd, &in))
9716 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9718 if (! elfobj_grok_gnu_note (abfd, &in))
9721 else if (in.namesz == sizeof "stapsdt"
9722 && strcmp (in.namedata, "stapsdt") == 0)
9724 if (! elfobj_grok_stapsdt_note (abfd, &in))
9730 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9737 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9744 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9747 buf = (char *) bfd_malloc (size);
9751 if (bfd_bread (buf, size, abfd) != size
9752 || !elf_parse_notes (abfd, buf, size, offset))
9762 /* Providing external access to the ELF program header table. */
9764 /* Return an upper bound on the number of bytes required to store a
9765 copy of ABFD's program header table entries. Return -1 if an error
9766 occurs; bfd_get_error will return an appropriate code. */
9769 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9771 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9773 bfd_set_error (bfd_error_wrong_format);
9777 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9780 /* Copy ABFD's program header table entries to *PHDRS. The entries
9781 will be stored as an array of Elf_Internal_Phdr structures, as
9782 defined in include/elf/internal.h. To find out how large the
9783 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9785 Return the number of program header table entries read, or -1 if an
9786 error occurs; bfd_get_error will return an appropriate code. */
9789 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9793 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9795 bfd_set_error (bfd_error_wrong_format);
9799 num_phdrs = elf_elfheader (abfd)->e_phnum;
9800 memcpy (phdrs, elf_tdata (abfd)->phdr,
9801 num_phdrs * sizeof (Elf_Internal_Phdr));
9806 enum elf_reloc_type_class
9807 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
9808 const asection *rel_sec ATTRIBUTE_UNUSED,
9809 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9811 return reloc_class_normal;
9814 /* For RELA architectures, return the relocation value for a
9815 relocation against a local symbol. */
9818 _bfd_elf_rela_local_sym (bfd *abfd,
9819 Elf_Internal_Sym *sym,
9821 Elf_Internal_Rela *rel)
9823 asection *sec = *psec;
9826 relocation = (sec->output_section->vma
9827 + sec->output_offset
9829 if ((sec->flags & SEC_MERGE)
9830 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9831 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
9834 _bfd_merged_section_offset (abfd, psec,
9835 elf_section_data (sec)->sec_info,
9836 sym->st_value + rel->r_addend);
9839 /* If we have changed the section, and our original section is
9840 marked with SEC_EXCLUDE, it means that the original
9841 SEC_MERGE section has been completely subsumed in some
9842 other SEC_MERGE section. In this case, we need to leave
9843 some info around for --emit-relocs. */
9844 if ((sec->flags & SEC_EXCLUDE) != 0)
9845 sec->kept_section = *psec;
9848 rel->r_addend -= relocation;
9849 rel->r_addend += sec->output_section->vma + sec->output_offset;
9855 _bfd_elf_rel_local_sym (bfd *abfd,
9856 Elf_Internal_Sym *sym,
9860 asection *sec = *psec;
9862 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
9863 return sym->st_value + addend;
9865 return _bfd_merged_section_offset (abfd, psec,
9866 elf_section_data (sec)->sec_info,
9867 sym->st_value + addend);
9871 _bfd_elf_section_offset (bfd *abfd,
9872 struct bfd_link_info *info,
9876 switch (sec->sec_info_type)
9878 case SEC_INFO_TYPE_STABS:
9879 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9881 case SEC_INFO_TYPE_EH_FRAME:
9882 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9884 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
9886 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9887 bfd_size_type address_size = bed->s->arch_size / 8;
9888 offset = sec->size - offset - address_size;
9894 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9895 reconstruct an ELF file by reading the segments out of remote memory
9896 based on the ELF file header at EHDR_VMA and the ELF program headers it
9897 points to. If not null, *LOADBASEP is filled in with the difference
9898 between the VMAs from which the segments were read, and the VMAs the
9899 file headers (and hence BFD's idea of each section's VMA) put them at.
9901 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9902 remote memory at target address VMA into the local buffer at MYADDR; it
9903 should return zero on success or an `errno' code on failure. TEMPL must
9904 be a BFD for an ELF target with the word size and byte order found in
9905 the remote memory. */
9908 bfd_elf_bfd_from_remote_memory
9913 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
9915 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9916 (templ, ehdr_vma, size, loadbasep, target_read_memory);
9920 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9921 long symcount ATTRIBUTE_UNUSED,
9922 asymbol **syms ATTRIBUTE_UNUSED,
9927 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9930 const char *relplt_name;
9931 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9935 Elf_Internal_Shdr *hdr;
9941 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9944 if (dynsymcount <= 0)
9947 if (!bed->plt_sym_val)
9950 relplt_name = bed->relplt_name;
9951 if (relplt_name == NULL)
9952 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9953 relplt = bfd_get_section_by_name (abfd, relplt_name);
9957 hdr = &elf_section_data (relplt)->this_hdr;
9958 if (hdr->sh_link != elf_dynsymtab (abfd)
9959 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9962 plt = bfd_get_section_by_name (abfd, ".plt");
9966 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9967 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9970 count = relplt->size / hdr->sh_entsize;
9971 size = count * sizeof (asymbol);
9972 p = relplt->relocation;
9973 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9975 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9979 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
9981 size += sizeof ("+0x") - 1 + 8;
9986 s = *ret = (asymbol *) bfd_malloc (size);
9990 names = (char *) (s + count);
9991 p = relplt->relocation;
9993 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9998 addr = bed->plt_sym_val (i, plt, p);
9999 if (addr == (bfd_vma) -1)
10002 *s = **p->sym_ptr_ptr;
10003 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10004 we are defining a symbol, ensure one of them is set. */
10005 if ((s->flags & BSF_LOCAL) == 0)
10006 s->flags |= BSF_GLOBAL;
10007 s->flags |= BSF_SYNTHETIC;
10009 s->value = addr - plt->vma;
10012 len = strlen ((*p->sym_ptr_ptr)->name);
10013 memcpy (names, (*p->sym_ptr_ptr)->name, len);
10015 if (p->addend != 0)
10019 memcpy (names, "+0x", sizeof ("+0x") - 1);
10020 names += sizeof ("+0x") - 1;
10021 bfd_sprintf_vma (abfd, buf, p->addend);
10022 for (a = buf; *a == '0'; ++a)
10025 memcpy (names, a, len);
10028 memcpy (names, "@plt", sizeof ("@plt"));
10029 names += sizeof ("@plt");
10036 /* It is only used by x86-64 so far. */
10037 asection _bfd_elf_large_com_section
10038 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10039 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10042 _bfd_elf_post_process_headers (bfd * abfd,
10043 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10045 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
10047 i_ehdrp = elf_elfheader (abfd);
10049 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10051 /* To make things simpler for the loader on Linux systems we set the
10052 osabi field to ELFOSABI_GNU if the binary contains symbols of
10053 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10054 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10055 && elf_tdata (abfd)->has_gnu_symbols)
10056 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10060 /* Return TRUE for ELF symbol types that represent functions.
10061 This is the default version of this function, which is sufficient for
10062 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10065 _bfd_elf_is_function_type (unsigned int type)
10067 return (type == STT_FUNC
10068 || type == STT_GNU_IFUNC);
10071 /* If the ELF symbol SYM might be a function in SEC, return the
10072 function size and set *CODE_OFF to the function's entry point,
10073 otherwise return zero. */
10076 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10079 bfd_size_type size;
10081 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10082 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10083 || sym->section != sec)
10086 *code_off = sym->value;
10088 if (!(sym->flags & BSF_SYNTHETIC))
10089 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;