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;
1581 bfd_boolean ret = TRUE;
1582 static bfd_boolean * sections_being_created = NULL;
1583 static bfd * sections_being_created_abfd = NULL;
1584 static unsigned int nesting = 0;
1586 if (shindex >= elf_numsections (abfd))
1591 /* PR17512: A corrupt ELF binary might contain a recursive group of
1592 sections, each the string indicies pointing to the next in the
1593 loop. Detect this here, by refusing to load a section that we are
1594 already in the process of loading. We only trigger this test if
1595 we have nested at least three sections deep as normal ELF binaries
1596 can expect to recurse at least once.
1598 FIXME: It would be better if this array was attached to the bfd,
1599 rather than being held in a static pointer. */
1601 if (sections_being_created_abfd != abfd)
1602 sections_being_created = NULL;
1603 if (sections_being_created == NULL)
1605 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1606 sections_being_created = (bfd_boolean *)
1607 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
1608 sections_being_created_abfd = abfd;
1610 if (sections_being_created [shindex])
1612 (*_bfd_error_handler)
1613 (_("%B: warning: loop in section dependencies detected"), abfd);
1616 sections_being_created [shindex] = TRUE;
1619 hdr = elf_elfsections (abfd)[shindex];
1620 ehdr = elf_elfheader (abfd);
1621 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1626 bed = get_elf_backend_data (abfd);
1627 switch (hdr->sh_type)
1630 /* Inactive section. Throw it away. */
1633 case SHT_PROGBITS: /* Normal section with contents. */
1634 case SHT_NOBITS: /* .bss section. */
1635 case SHT_HASH: /* .hash section. */
1636 case SHT_NOTE: /* .note section. */
1637 case SHT_INIT_ARRAY: /* .init_array section. */
1638 case SHT_FINI_ARRAY: /* .fini_array section. */
1639 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1640 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1641 case SHT_GNU_HASH: /* .gnu.hash section. */
1642 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1645 case SHT_DYNAMIC: /* Dynamic linking information. */
1646 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1649 if (hdr->sh_link > elf_numsections (abfd))
1651 /* PR 10478: Accept Solaris binaries with a sh_link
1652 field set to SHN_BEFORE or SHN_AFTER. */
1653 switch (bfd_get_arch (abfd))
1656 case bfd_arch_sparc:
1657 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1658 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1660 /* Otherwise fall through. */
1665 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1667 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1669 Elf_Internal_Shdr *dynsymhdr;
1671 /* The shared libraries distributed with hpux11 have a bogus
1672 sh_link field for the ".dynamic" section. Find the
1673 string table for the ".dynsym" section instead. */
1674 if (elf_dynsymtab (abfd) != 0)
1676 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1677 hdr->sh_link = dynsymhdr->sh_link;
1681 unsigned int i, num_sec;
1683 num_sec = elf_numsections (abfd);
1684 for (i = 1; i < num_sec; i++)
1686 dynsymhdr = elf_elfsections (abfd)[i];
1687 if (dynsymhdr->sh_type == SHT_DYNSYM)
1689 hdr->sh_link = dynsymhdr->sh_link;
1697 case SHT_SYMTAB: /* A symbol table. */
1698 if (elf_onesymtab (abfd) == shindex)
1701 if (hdr->sh_entsize != bed->s->sizeof_sym)
1704 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1706 if (hdr->sh_size != 0)
1708 /* Some assemblers erroneously set sh_info to one with a
1709 zero sh_size. ld sees this as a global symbol count
1710 of (unsigned) -1. Fix it here. */
1715 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1716 elf_onesymtab (abfd) = shindex;
1717 elf_tdata (abfd)->symtab_hdr = *hdr;
1718 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1719 abfd->flags |= HAS_SYMS;
1721 /* Sometimes a shared object will map in the symbol table. If
1722 SHF_ALLOC is set, and this is a shared object, then we also
1723 treat this section as a BFD section. We can not base the
1724 decision purely on SHF_ALLOC, because that flag is sometimes
1725 set in a relocatable object file, which would confuse the
1727 if ((hdr->sh_flags & SHF_ALLOC) != 0
1728 && (abfd->flags & DYNAMIC) != 0
1729 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1733 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1734 can't read symbols without that section loaded as well. It
1735 is most likely specified by the next section header. */
1736 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1738 unsigned int i, num_sec;
1740 num_sec = elf_numsections (abfd);
1741 for (i = shindex + 1; i < num_sec; i++)
1743 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1744 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1745 && hdr2->sh_link == shindex)
1749 for (i = 1; i < shindex; i++)
1751 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1752 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1753 && hdr2->sh_link == shindex)
1757 ret = bfd_section_from_shdr (abfd, i);
1761 case SHT_DYNSYM: /* A dynamic symbol table. */
1762 if (elf_dynsymtab (abfd) == shindex)
1765 if (hdr->sh_entsize != bed->s->sizeof_sym)
1768 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1770 if (hdr->sh_size != 0)
1773 /* Some linkers erroneously set sh_info to one with a
1774 zero sh_size. ld sees this as a global symbol count
1775 of (unsigned) -1. Fix it here. */
1780 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1781 elf_dynsymtab (abfd) = shindex;
1782 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1783 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1784 abfd->flags |= HAS_SYMS;
1786 /* Besides being a symbol table, we also treat this as a regular
1787 section, so that objcopy can handle it. */
1788 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1791 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
1792 if (elf_symtab_shndx (abfd) == shindex)
1795 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1796 elf_symtab_shndx (abfd) = shindex;
1797 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1798 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1801 case SHT_STRTAB: /* A string table. */
1802 if (hdr->bfd_section != NULL)
1805 if (ehdr->e_shstrndx == shindex)
1807 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1808 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1812 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1815 elf_tdata (abfd)->strtab_hdr = *hdr;
1816 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1820 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1823 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1824 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1825 elf_elfsections (abfd)[shindex] = hdr;
1826 /* We also treat this as a regular section, so that objcopy
1828 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1833 /* If the string table isn't one of the above, then treat it as a
1834 regular section. We need to scan all the headers to be sure,
1835 just in case this strtab section appeared before the above. */
1836 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1838 unsigned int i, num_sec;
1840 num_sec = elf_numsections (abfd);
1841 for (i = 1; i < num_sec; i++)
1843 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1844 if (hdr2->sh_link == shindex)
1846 /* Prevent endless recursion on broken objects. */
1849 if (! bfd_section_from_shdr (abfd, i))
1851 if (elf_onesymtab (abfd) == i)
1853 if (elf_dynsymtab (abfd) == i)
1854 goto dynsymtab_strtab;
1858 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1863 /* *These* do a lot of work -- but build no sections! */
1865 asection *target_sect;
1866 Elf_Internal_Shdr *hdr2, **p_hdr;
1867 unsigned int num_sec = elf_numsections (abfd);
1868 struct bfd_elf_section_data *esdt;
1872 != (bfd_size_type) (hdr->sh_type == SHT_REL
1873 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1876 /* Check for a bogus link to avoid crashing. */
1877 if (hdr->sh_link >= num_sec)
1879 ((*_bfd_error_handler)
1880 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1881 abfd, hdr->sh_link, name, shindex));
1882 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1887 /* For some incomprehensible reason Oracle distributes
1888 libraries for Solaris in which some of the objects have
1889 bogus sh_link fields. It would be nice if we could just
1890 reject them, but, unfortunately, some people need to use
1891 them. We scan through the section headers; if we find only
1892 one suitable symbol table, we clobber the sh_link to point
1893 to it. I hope this doesn't break anything.
1895 Don't do it on executable nor shared library. */
1896 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1897 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1898 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1904 for (scan = 1; scan < num_sec; scan++)
1906 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1907 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1918 hdr->sh_link = found;
1921 /* Get the symbol table. */
1922 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1923 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1924 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1927 /* If this reloc section does not use the main symbol table we
1928 don't treat it as a reloc section. BFD can't adequately
1929 represent such a section, so at least for now, we don't
1930 try. We just present it as a normal section. We also
1931 can't use it as a reloc section if it points to the null
1932 section, an invalid section, another reloc section, or its
1933 sh_link points to the null section. */
1934 if (hdr->sh_link != elf_onesymtab (abfd)
1935 || hdr->sh_link == SHN_UNDEF
1936 || hdr->sh_info == SHN_UNDEF
1937 || hdr->sh_info >= num_sec
1938 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1939 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1941 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1946 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1949 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1950 if (target_sect == NULL)
1953 esdt = elf_section_data (target_sect);
1954 if (hdr->sh_type == SHT_RELA)
1955 p_hdr = &esdt->rela.hdr;
1957 p_hdr = &esdt->rel.hdr;
1959 BFD_ASSERT (*p_hdr == NULL);
1960 amt = sizeof (*hdr2);
1961 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1966 elf_elfsections (abfd)[shindex] = hdr2;
1967 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1968 target_sect->flags |= SEC_RELOC;
1969 target_sect->relocation = NULL;
1970 target_sect->rel_filepos = hdr->sh_offset;
1971 /* In the section to which the relocations apply, mark whether
1972 its relocations are of the REL or RELA variety. */
1973 if (hdr->sh_size != 0)
1975 if (hdr->sh_type == SHT_RELA)
1976 target_sect->use_rela_p = 1;
1978 abfd->flags |= HAS_RELOC;
1982 case SHT_GNU_verdef:
1983 elf_dynverdef (abfd) = shindex;
1984 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1985 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1988 case SHT_GNU_versym:
1989 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1992 elf_dynversym (abfd) = shindex;
1993 elf_tdata (abfd)->dynversym_hdr = *hdr;
1994 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1997 case SHT_GNU_verneed:
1998 elf_dynverref (abfd) = shindex;
1999 elf_tdata (abfd)->dynverref_hdr = *hdr;
2000 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2007 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2010 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2013 if (hdr->contents != NULL)
2015 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2016 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
2019 if (idx->flags & GRP_COMDAT)
2020 hdr->bfd_section->flags
2021 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2023 /* We try to keep the same section order as it comes in. */
2025 while (--n_elt != 0)
2029 if (idx->shdr != NULL
2030 && (s = idx->shdr->bfd_section) != NULL
2031 && elf_next_in_group (s) != NULL)
2033 elf_next_in_group (hdr->bfd_section) = s;
2041 /* Possibly an attributes section. */
2042 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2043 || hdr->sh_type == bed->obj_attrs_section_type)
2045 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2047 _bfd_elf_parse_attributes (abfd, hdr);
2051 /* Check for any processor-specific section types. */
2052 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2055 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2057 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2058 /* FIXME: How to properly handle allocated section reserved
2059 for applications? */
2060 (*_bfd_error_handler)
2061 (_("%B: don't know how to handle allocated, application "
2062 "specific section `%s' [0x%8x]"),
2063 abfd, name, hdr->sh_type);
2066 /* Allow sections reserved for applications. */
2067 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2072 else if (hdr->sh_type >= SHT_LOPROC
2073 && hdr->sh_type <= SHT_HIPROC)
2074 /* FIXME: We should handle this section. */
2075 (*_bfd_error_handler)
2076 (_("%B: don't know how to handle processor specific section "
2078 abfd, name, hdr->sh_type);
2079 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2081 /* Unrecognised OS-specific sections. */
2082 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2083 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2084 required to correctly process the section and the file should
2085 be rejected with an error message. */
2086 (*_bfd_error_handler)
2087 (_("%B: don't know how to handle OS specific section "
2089 abfd, name, hdr->sh_type);
2092 /* Otherwise it should be processed. */
2093 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2098 /* FIXME: We should handle this section. */
2099 (*_bfd_error_handler)
2100 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2101 abfd, name, hdr->sh_type);
2109 if (sections_being_created && sections_being_created_abfd == abfd)
2110 sections_being_created [shindex] = FALSE;
2111 if (-- nesting == 0)
2113 sections_being_created = NULL;
2114 sections_being_created_abfd = abfd;
2119 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2122 bfd_sym_from_r_symndx (struct sym_cache *cache,
2124 unsigned long r_symndx)
2126 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2128 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2130 Elf_Internal_Shdr *symtab_hdr;
2131 unsigned char esym[sizeof (Elf64_External_Sym)];
2132 Elf_External_Sym_Shndx eshndx;
2134 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2135 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2136 &cache->sym[ent], esym, &eshndx) == NULL)
2139 if (cache->abfd != abfd)
2141 memset (cache->indx, -1, sizeof (cache->indx));
2144 cache->indx[ent] = r_symndx;
2147 return &cache->sym[ent];
2150 /* Given an ELF section number, retrieve the corresponding BFD
2154 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2156 if (sec_index >= elf_numsections (abfd))
2158 return elf_elfsections (abfd)[sec_index]->bfd_section;
2161 static const struct bfd_elf_special_section special_sections_b[] =
2163 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2164 { NULL, 0, 0, 0, 0 }
2167 static const struct bfd_elf_special_section special_sections_c[] =
2169 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2170 { NULL, 0, 0, 0, 0 }
2173 static const struct bfd_elf_special_section special_sections_d[] =
2175 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2176 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2177 /* There are more DWARF sections than these, but they needn't be added here
2178 unless you have to cope with broken compilers that don't emit section
2179 attributes or you want to help the user writing assembler. */
2180 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2181 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2182 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2183 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2184 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2185 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2186 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2187 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2188 { NULL, 0, 0, 0, 0 }
2191 static const struct bfd_elf_special_section special_sections_f[] =
2193 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2194 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2195 { NULL, 0, 0, 0, 0 }
2198 static const struct bfd_elf_special_section special_sections_g[] =
2200 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2201 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2202 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2203 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2204 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2205 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2206 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2207 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2208 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2209 { NULL, 0, 0, 0, 0 }
2212 static const struct bfd_elf_special_section special_sections_h[] =
2214 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2215 { NULL, 0, 0, 0, 0 }
2218 static const struct bfd_elf_special_section special_sections_i[] =
2220 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2221 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2222 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2223 { NULL, 0, 0, 0, 0 }
2226 static const struct bfd_elf_special_section special_sections_l[] =
2228 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2229 { NULL, 0, 0, 0, 0 }
2232 static const struct bfd_elf_special_section special_sections_n[] =
2234 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2235 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2236 { NULL, 0, 0, 0, 0 }
2239 static const struct bfd_elf_special_section special_sections_p[] =
2241 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2242 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2243 { NULL, 0, 0, 0, 0 }
2246 static const struct bfd_elf_special_section special_sections_r[] =
2248 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2249 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2250 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2251 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2252 { NULL, 0, 0, 0, 0 }
2255 static const struct bfd_elf_special_section special_sections_s[] =
2257 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2258 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2259 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2260 /* See struct bfd_elf_special_section declaration for the semantics of
2261 this special case where .prefix_length != strlen (.prefix). */
2262 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2263 { NULL, 0, 0, 0, 0 }
2266 static const struct bfd_elf_special_section special_sections_t[] =
2268 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2269 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2270 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2271 { NULL, 0, 0, 0, 0 }
2274 static const struct bfd_elf_special_section special_sections_z[] =
2276 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2277 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2278 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2279 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2280 { NULL, 0, 0, 0, 0 }
2283 static const struct bfd_elf_special_section * const special_sections[] =
2285 special_sections_b, /* 'b' */
2286 special_sections_c, /* 'c' */
2287 special_sections_d, /* 'd' */
2289 special_sections_f, /* 'f' */
2290 special_sections_g, /* 'g' */
2291 special_sections_h, /* 'h' */
2292 special_sections_i, /* 'i' */
2295 special_sections_l, /* 'l' */
2297 special_sections_n, /* 'n' */
2299 special_sections_p, /* 'p' */
2301 special_sections_r, /* 'r' */
2302 special_sections_s, /* 's' */
2303 special_sections_t, /* 't' */
2309 special_sections_z /* 'z' */
2312 const struct bfd_elf_special_section *
2313 _bfd_elf_get_special_section (const char *name,
2314 const struct bfd_elf_special_section *spec,
2320 len = strlen (name);
2322 for (i = 0; spec[i].prefix != NULL; i++)
2325 int prefix_len = spec[i].prefix_length;
2327 if (len < prefix_len)
2329 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2332 suffix_len = spec[i].suffix_length;
2333 if (suffix_len <= 0)
2335 if (name[prefix_len] != 0)
2337 if (suffix_len == 0)
2339 if (name[prefix_len] != '.'
2340 && (suffix_len == -2
2341 || (rela && spec[i].type == SHT_REL)))
2347 if (len < prefix_len + suffix_len)
2349 if (memcmp (name + len - suffix_len,
2350 spec[i].prefix + prefix_len,
2360 const struct bfd_elf_special_section *
2361 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2364 const struct bfd_elf_special_section *spec;
2365 const struct elf_backend_data *bed;
2367 /* See if this is one of the special sections. */
2368 if (sec->name == NULL)
2371 bed = get_elf_backend_data (abfd);
2372 spec = bed->special_sections;
2375 spec = _bfd_elf_get_special_section (sec->name,
2376 bed->special_sections,
2382 if (sec->name[0] != '.')
2385 i = sec->name[1] - 'b';
2386 if (i < 0 || i > 'z' - 'b')
2389 spec = special_sections[i];
2394 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2398 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2400 struct bfd_elf_section_data *sdata;
2401 const struct elf_backend_data *bed;
2402 const struct bfd_elf_special_section *ssect;
2404 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2407 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2411 sec->used_by_bfd = sdata;
2414 /* Indicate whether or not this section should use RELA relocations. */
2415 bed = get_elf_backend_data (abfd);
2416 sec->use_rela_p = bed->default_use_rela_p;
2418 /* When we read a file, we don't need to set ELF section type and
2419 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2420 anyway. We will set ELF section type and flags for all linker
2421 created sections. If user specifies BFD section flags, we will
2422 set ELF section type and flags based on BFD section flags in
2423 elf_fake_sections. Special handling for .init_array/.fini_array
2424 output sections since they may contain .ctors/.dtors input
2425 sections. We don't want _bfd_elf_init_private_section_data to
2426 copy ELF section type from .ctors/.dtors input sections. */
2427 if (abfd->direction != read_direction
2428 || (sec->flags & SEC_LINKER_CREATED) != 0)
2430 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2433 || (sec->flags & SEC_LINKER_CREATED) != 0
2434 || ssect->type == SHT_INIT_ARRAY
2435 || ssect->type == SHT_FINI_ARRAY))
2437 elf_section_type (sec) = ssect->type;
2438 elf_section_flags (sec) = ssect->attr;
2442 return _bfd_generic_new_section_hook (abfd, sec);
2445 /* Create a new bfd section from an ELF program header.
2447 Since program segments have no names, we generate a synthetic name
2448 of the form segment<NUM>, where NUM is generally the index in the
2449 program header table. For segments that are split (see below) we
2450 generate the names segment<NUM>a and segment<NUM>b.
2452 Note that some program segments may have a file size that is different than
2453 (less than) the memory size. All this means is that at execution the
2454 system must allocate the amount of memory specified by the memory size,
2455 but only initialize it with the first "file size" bytes read from the
2456 file. This would occur for example, with program segments consisting
2457 of combined data+bss.
2459 To handle the above situation, this routine generates TWO bfd sections
2460 for the single program segment. The first has the length specified by
2461 the file size of the segment, and the second has the length specified
2462 by the difference between the two sizes. In effect, the segment is split
2463 into its initialized and uninitialized parts.
2468 _bfd_elf_make_section_from_phdr (bfd *abfd,
2469 Elf_Internal_Phdr *hdr,
2471 const char *type_name)
2479 split = ((hdr->p_memsz > 0)
2480 && (hdr->p_filesz > 0)
2481 && (hdr->p_memsz > hdr->p_filesz));
2483 if (hdr->p_filesz > 0)
2485 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2486 len = strlen (namebuf) + 1;
2487 name = (char *) bfd_alloc (abfd, len);
2490 memcpy (name, namebuf, len);
2491 newsect = bfd_make_section (abfd, name);
2492 if (newsect == NULL)
2494 newsect->vma = hdr->p_vaddr;
2495 newsect->lma = hdr->p_paddr;
2496 newsect->size = hdr->p_filesz;
2497 newsect->filepos = hdr->p_offset;
2498 newsect->flags |= SEC_HAS_CONTENTS;
2499 newsect->alignment_power = bfd_log2 (hdr->p_align);
2500 if (hdr->p_type == PT_LOAD)
2502 newsect->flags |= SEC_ALLOC;
2503 newsect->flags |= SEC_LOAD;
2504 if (hdr->p_flags & PF_X)
2506 /* FIXME: all we known is that it has execute PERMISSION,
2508 newsect->flags |= SEC_CODE;
2511 if (!(hdr->p_flags & PF_W))
2513 newsect->flags |= SEC_READONLY;
2517 if (hdr->p_memsz > hdr->p_filesz)
2521 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2522 len = strlen (namebuf) + 1;
2523 name = (char *) bfd_alloc (abfd, len);
2526 memcpy (name, namebuf, len);
2527 newsect = bfd_make_section (abfd, name);
2528 if (newsect == NULL)
2530 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2531 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2532 newsect->size = hdr->p_memsz - hdr->p_filesz;
2533 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2534 align = newsect->vma & -newsect->vma;
2535 if (align == 0 || align > hdr->p_align)
2536 align = hdr->p_align;
2537 newsect->alignment_power = bfd_log2 (align);
2538 if (hdr->p_type == PT_LOAD)
2540 /* Hack for gdb. Segments that have not been modified do
2541 not have their contents written to a core file, on the
2542 assumption that a debugger can find the contents in the
2543 executable. We flag this case by setting the fake
2544 section size to zero. Note that "real" bss sections will
2545 always have their contents dumped to the core file. */
2546 if (bfd_get_format (abfd) == bfd_core)
2548 newsect->flags |= SEC_ALLOC;
2549 if (hdr->p_flags & PF_X)
2550 newsect->flags |= SEC_CODE;
2552 if (!(hdr->p_flags & PF_W))
2553 newsect->flags |= SEC_READONLY;
2560 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2562 const struct elf_backend_data *bed;
2564 switch (hdr->p_type)
2567 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2570 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2573 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2576 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2579 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2581 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2586 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2589 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2591 case PT_GNU_EH_FRAME:
2592 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2596 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2599 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2602 /* Check for any processor-specific program segment types. */
2603 bed = get_elf_backend_data (abfd);
2604 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2608 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2612 _bfd_elf_single_rel_hdr (asection *sec)
2614 if (elf_section_data (sec)->rel.hdr)
2616 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2617 return elf_section_data (sec)->rel.hdr;
2620 return elf_section_data (sec)->rela.hdr;
2623 /* Allocate and initialize a section-header for a new reloc section,
2624 containing relocations against ASECT. It is stored in RELDATA. If
2625 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2629 _bfd_elf_init_reloc_shdr (bfd *abfd,
2630 struct bfd_elf_section_reloc_data *reldata,
2632 bfd_boolean use_rela_p)
2634 Elf_Internal_Shdr *rel_hdr;
2636 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2639 amt = sizeof (Elf_Internal_Shdr);
2640 BFD_ASSERT (reldata->hdr == NULL);
2641 rel_hdr = bfd_zalloc (abfd, amt);
2642 reldata->hdr = rel_hdr;
2644 amt = sizeof ".rela" + strlen (asect->name);
2645 name = (char *) bfd_alloc (abfd, amt);
2648 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2650 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2652 if (rel_hdr->sh_name == (unsigned int) -1)
2654 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2655 rel_hdr->sh_entsize = (use_rela_p
2656 ? bed->s->sizeof_rela
2657 : bed->s->sizeof_rel);
2658 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2659 rel_hdr->sh_flags = 0;
2660 rel_hdr->sh_addr = 0;
2661 rel_hdr->sh_size = 0;
2662 rel_hdr->sh_offset = 0;
2667 /* Return the default section type based on the passed in section flags. */
2670 bfd_elf_get_default_section_type (flagword flags)
2672 if ((flags & SEC_ALLOC) != 0
2673 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2675 return SHT_PROGBITS;
2678 struct fake_section_arg
2680 struct bfd_link_info *link_info;
2684 /* Set up an ELF internal section header for a section. */
2687 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2689 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2690 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2691 struct bfd_elf_section_data *esd = elf_section_data (asect);
2692 Elf_Internal_Shdr *this_hdr;
2693 unsigned int sh_type;
2697 /* We already failed; just get out of the bfd_map_over_sections
2702 this_hdr = &esd->this_hdr;
2704 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2705 asect->name, FALSE);
2706 if (this_hdr->sh_name == (unsigned int) -1)
2712 /* Don't clear sh_flags. Assembler may set additional bits. */
2714 if ((asect->flags & SEC_ALLOC) != 0
2715 || asect->user_set_vma)
2716 this_hdr->sh_addr = asect->vma;
2718 this_hdr->sh_addr = 0;
2720 this_hdr->sh_offset = 0;
2721 this_hdr->sh_size = asect->size;
2722 this_hdr->sh_link = 0;
2723 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2724 /* The sh_entsize and sh_info fields may have been set already by
2725 copy_private_section_data. */
2727 this_hdr->bfd_section = asect;
2728 this_hdr->contents = NULL;
2730 /* If the section type is unspecified, we set it based on
2732 if ((asect->flags & SEC_GROUP) != 0)
2733 sh_type = SHT_GROUP;
2735 sh_type = bfd_elf_get_default_section_type (asect->flags);
2737 if (this_hdr->sh_type == SHT_NULL)
2738 this_hdr->sh_type = sh_type;
2739 else if (this_hdr->sh_type == SHT_NOBITS
2740 && sh_type == SHT_PROGBITS
2741 && (asect->flags & SEC_ALLOC) != 0)
2743 /* Warn if we are changing a NOBITS section to PROGBITS, but
2744 allow the link to proceed. This can happen when users link
2745 non-bss input sections to bss output sections, or emit data
2746 to a bss output section via a linker script. */
2747 (*_bfd_error_handler)
2748 (_("warning: section `%A' type changed to PROGBITS"), asect);
2749 this_hdr->sh_type = sh_type;
2752 switch (this_hdr->sh_type)
2758 case SHT_INIT_ARRAY:
2759 case SHT_FINI_ARRAY:
2760 case SHT_PREINIT_ARRAY:
2767 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2771 this_hdr->sh_entsize = bed->s->sizeof_sym;
2775 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2779 if (get_elf_backend_data (abfd)->may_use_rela_p)
2780 this_hdr->sh_entsize = bed->s->sizeof_rela;
2784 if (get_elf_backend_data (abfd)->may_use_rel_p)
2785 this_hdr->sh_entsize = bed->s->sizeof_rel;
2788 case SHT_GNU_versym:
2789 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2792 case SHT_GNU_verdef:
2793 this_hdr->sh_entsize = 0;
2794 /* objcopy or strip will copy over sh_info, but may not set
2795 cverdefs. The linker will set cverdefs, but sh_info will be
2797 if (this_hdr->sh_info == 0)
2798 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2800 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2801 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2804 case SHT_GNU_verneed:
2805 this_hdr->sh_entsize = 0;
2806 /* objcopy or strip will copy over sh_info, but may not set
2807 cverrefs. The linker will set cverrefs, but sh_info will be
2809 if (this_hdr->sh_info == 0)
2810 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2812 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2813 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2817 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2821 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2825 if ((asect->flags & SEC_ALLOC) != 0)
2826 this_hdr->sh_flags |= SHF_ALLOC;
2827 if ((asect->flags & SEC_READONLY) == 0)
2828 this_hdr->sh_flags |= SHF_WRITE;
2829 if ((asect->flags & SEC_CODE) != 0)
2830 this_hdr->sh_flags |= SHF_EXECINSTR;
2831 if ((asect->flags & SEC_MERGE) != 0)
2833 this_hdr->sh_flags |= SHF_MERGE;
2834 this_hdr->sh_entsize = asect->entsize;
2835 if ((asect->flags & SEC_STRINGS) != 0)
2836 this_hdr->sh_flags |= SHF_STRINGS;
2838 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2839 this_hdr->sh_flags |= SHF_GROUP;
2840 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2842 this_hdr->sh_flags |= SHF_TLS;
2843 if (asect->size == 0
2844 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2846 struct bfd_link_order *o = asect->map_tail.link_order;
2848 this_hdr->sh_size = 0;
2851 this_hdr->sh_size = o->offset + o->size;
2852 if (this_hdr->sh_size != 0)
2853 this_hdr->sh_type = SHT_NOBITS;
2857 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2858 this_hdr->sh_flags |= SHF_EXCLUDE;
2860 /* If the section has relocs, set up a section header for the
2861 SHT_REL[A] section. If two relocation sections are required for
2862 this section, it is up to the processor-specific back-end to
2863 create the other. */
2864 if ((asect->flags & SEC_RELOC) != 0)
2866 /* When doing a relocatable link, create both REL and RELA sections if
2869 /* Do the normal setup if we wouldn't create any sections here. */
2870 && esd->rel.count + esd->rela.count > 0
2871 && (arg->link_info->relocatable || arg->link_info->emitrelocations))
2873 if (esd->rel.count && esd->rel.hdr == NULL
2874 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
2879 if (esd->rela.count && esd->rela.hdr == NULL
2880 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
2886 else if (!_bfd_elf_init_reloc_shdr (abfd,
2888 ? &esd->rela : &esd->rel),
2894 /* Check for processor-specific section types. */
2895 sh_type = this_hdr->sh_type;
2896 if (bed->elf_backend_fake_sections
2897 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2900 if (sh_type == SHT_NOBITS && asect->size != 0)
2902 /* Don't change the header type from NOBITS if we are being
2903 called for objcopy --only-keep-debug. */
2904 this_hdr->sh_type = sh_type;
2908 /* Fill in the contents of a SHT_GROUP section. Called from
2909 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2910 when ELF targets use the generic linker, ld. Called for ld -r
2911 from bfd_elf_final_link. */
2914 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2916 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2917 asection *elt, *first;
2921 /* Ignore linker created group section. See elfNN_ia64_object_p in
2923 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2927 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2929 unsigned long symindx = 0;
2931 /* elf_group_id will have been set up by objcopy and the
2933 if (elf_group_id (sec) != NULL)
2934 symindx = elf_group_id (sec)->udata.i;
2938 /* If called from the assembler, swap_out_syms will have set up
2939 elf_section_syms. */
2940 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2941 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2943 elf_section_data (sec)->this_hdr.sh_info = symindx;
2945 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2947 /* The ELF backend linker sets sh_info to -2 when the group
2948 signature symbol is global, and thus the index can't be
2949 set until all local symbols are output. */
2950 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2951 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2952 unsigned long symndx = sec_data->this_hdr.sh_info;
2953 unsigned long extsymoff = 0;
2954 struct elf_link_hash_entry *h;
2956 if (!elf_bad_symtab (igroup->owner))
2958 Elf_Internal_Shdr *symtab_hdr;
2960 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2961 extsymoff = symtab_hdr->sh_info;
2963 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2964 while (h->root.type == bfd_link_hash_indirect
2965 || h->root.type == bfd_link_hash_warning)
2966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2968 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2971 /* The contents won't be allocated for "ld -r" or objcopy. */
2973 if (sec->contents == NULL)
2976 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2978 /* Arrange for the section to be written out. */
2979 elf_section_data (sec)->this_hdr.contents = sec->contents;
2980 if (sec->contents == NULL)
2987 loc = sec->contents + sec->size;
2989 /* Get the pointer to the first section in the group that gas
2990 squirreled away here. objcopy arranges for this to be set to the
2991 start of the input section group. */
2992 first = elt = elf_next_in_group (sec);
2994 /* First element is a flag word. Rest of section is elf section
2995 indices for all the sections of the group. Write them backwards
2996 just to keep the group in the same order as given in .section
2997 directives, not that it matters. */
3004 s = s->output_section;
3006 && !bfd_is_abs_section (s))
3008 unsigned int idx = elf_section_data (s)->this_idx;
3011 H_PUT_32 (abfd, idx, loc);
3013 elt = elf_next_in_group (elt);
3018 if ((loc -= 4) != sec->contents)
3021 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3024 /* Assign all ELF section numbers. The dummy first section is handled here
3025 too. The link/info pointers for the standard section types are filled
3026 in here too, while we're at it. */
3029 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3031 struct elf_obj_tdata *t = elf_tdata (abfd);
3033 unsigned int section_number, secn;
3034 Elf_Internal_Shdr **i_shdrp;
3035 struct bfd_elf_section_data *d;
3036 bfd_boolean need_symtab;
3040 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3042 /* SHT_GROUP sections are in relocatable files only. */
3043 if (link_info == NULL || link_info->relocatable)
3045 /* Put SHT_GROUP sections first. */
3046 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3048 d = elf_section_data (sec);
3050 if (d->this_hdr.sh_type == SHT_GROUP)
3052 if (sec->flags & SEC_LINKER_CREATED)
3054 /* Remove the linker created SHT_GROUP sections. */
3055 bfd_section_list_remove (abfd, sec);
3056 abfd->section_count--;
3059 d->this_idx = section_number++;
3064 for (sec = abfd->sections; sec; sec = sec->next)
3066 d = elf_section_data (sec);
3068 if (d->this_hdr.sh_type != SHT_GROUP)
3069 d->this_idx = section_number++;
3070 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3073 d->rel.idx = section_number++;
3074 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3081 d->rela.idx = section_number++;
3082 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3088 elf_shstrtab_sec (abfd) = section_number++;
3089 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3090 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3092 need_symtab = (bfd_get_symcount (abfd) > 0
3093 || (link_info == NULL
3094 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3098 elf_onesymtab (abfd) = section_number++;
3099 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3100 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3102 elf_symtab_shndx (abfd) = section_number++;
3103 t->symtab_shndx_hdr.sh_name
3104 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3105 ".symtab_shndx", FALSE);
3106 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3109 elf_strtab_sec (abfd) = section_number++;
3110 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3113 if (section_number >= SHN_LORESERVE)
3115 _bfd_error_handler (_("%B: too many sections: %u"),
3116 abfd, section_number);
3120 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3121 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3123 elf_numsections (abfd) = section_number;
3124 elf_elfheader (abfd)->e_shnum = section_number;
3126 /* Set up the list of section header pointers, in agreement with the
3128 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3129 sizeof (Elf_Internal_Shdr *));
3130 if (i_shdrp == NULL)
3133 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3134 sizeof (Elf_Internal_Shdr));
3135 if (i_shdrp[0] == NULL)
3137 bfd_release (abfd, i_shdrp);
3141 elf_elfsections (abfd) = i_shdrp;
3143 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3146 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3147 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3149 i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
3150 t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
3152 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3153 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3156 for (sec = abfd->sections; sec; sec = sec->next)
3161 d = elf_section_data (sec);
3163 i_shdrp[d->this_idx] = &d->this_hdr;
3164 if (d->rel.idx != 0)
3165 i_shdrp[d->rel.idx] = d->rel.hdr;
3166 if (d->rela.idx != 0)
3167 i_shdrp[d->rela.idx] = d->rela.hdr;
3169 /* Fill in the sh_link and sh_info fields while we're at it. */
3171 /* sh_link of a reloc section is the section index of the symbol
3172 table. sh_info is the section index of the section to which
3173 the relocation entries apply. */
3174 if (d->rel.idx != 0)
3176 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3177 d->rel.hdr->sh_info = d->this_idx;
3178 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3180 if (d->rela.idx != 0)
3182 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3183 d->rela.hdr->sh_info = d->this_idx;
3184 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3187 /* We need to set up sh_link for SHF_LINK_ORDER. */
3188 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3190 s = elf_linked_to_section (sec);
3193 /* elf_linked_to_section points to the input section. */
3194 if (link_info != NULL)
3196 /* Check discarded linkonce section. */
3197 if (discarded_section (s))
3200 (*_bfd_error_handler)
3201 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3202 abfd, d->this_hdr.bfd_section,
3204 /* Point to the kept section if it has the same
3205 size as the discarded one. */
3206 kept = _bfd_elf_check_kept_section (s, link_info);
3209 bfd_set_error (bfd_error_bad_value);
3215 s = s->output_section;
3216 BFD_ASSERT (s != NULL);
3220 /* Handle objcopy. */
3221 if (s->output_section == NULL)
3223 (*_bfd_error_handler)
3224 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3225 abfd, d->this_hdr.bfd_section, s, s->owner);
3226 bfd_set_error (bfd_error_bad_value);
3229 s = s->output_section;
3231 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3236 The Intel C compiler generates SHT_IA_64_UNWIND with
3237 SHF_LINK_ORDER. But it doesn't set the sh_link or
3238 sh_info fields. Hence we could get the situation
3240 const struct elf_backend_data *bed
3241 = get_elf_backend_data (abfd);
3242 if (bed->link_order_error_handler)
3243 bed->link_order_error_handler
3244 (_("%B: warning: sh_link not set for section `%A'"),
3249 switch (d->this_hdr.sh_type)
3253 /* A reloc section which we are treating as a normal BFD
3254 section. sh_link is the section index of the symbol
3255 table. sh_info is the section index of the section to
3256 which the relocation entries apply. We assume that an
3257 allocated reloc section uses the dynamic symbol table.
3258 FIXME: How can we be sure? */
3259 s = bfd_get_section_by_name (abfd, ".dynsym");
3261 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3263 /* We look up the section the relocs apply to by name. */
3265 if (d->this_hdr.sh_type == SHT_REL)
3269 s = bfd_get_section_by_name (abfd, name);
3272 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3273 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3278 /* We assume that a section named .stab*str is a stabs
3279 string section. We look for a section with the same name
3280 but without the trailing ``str'', and set its sh_link
3281 field to point to this section. */
3282 if (CONST_STRNEQ (sec->name, ".stab")
3283 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3288 len = strlen (sec->name);
3289 alc = (char *) bfd_malloc (len - 2);
3292 memcpy (alc, sec->name, len - 3);
3293 alc[len - 3] = '\0';
3294 s = bfd_get_section_by_name (abfd, alc);
3298 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3300 /* This is a .stab section. */
3301 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3302 elf_section_data (s)->this_hdr.sh_entsize
3303 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3310 case SHT_GNU_verneed:
3311 case SHT_GNU_verdef:
3312 /* sh_link is the section header index of the string table
3313 used for the dynamic entries, or the symbol table, or the
3315 s = bfd_get_section_by_name (abfd, ".dynstr");
3317 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3320 case SHT_GNU_LIBLIST:
3321 /* sh_link is the section header index of the prelink library
3322 list used for the dynamic entries, or the symbol table, or
3323 the version strings. */
3324 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3325 ? ".dynstr" : ".gnu.libstr");
3327 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3332 case SHT_GNU_versym:
3333 /* sh_link is the section header index of the symbol table
3334 this hash table or version table is for. */
3335 s = bfd_get_section_by_name (abfd, ".dynsym");
3337 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3341 d->this_hdr.sh_link = elf_onesymtab (abfd);
3345 for (secn = 1; secn < section_number; ++secn)
3346 if (i_shdrp[secn] == NULL)
3347 i_shdrp[secn] = i_shdrp[0];
3349 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3350 i_shdrp[secn]->sh_name);
3355 sym_is_global (bfd *abfd, asymbol *sym)
3357 /* If the backend has a special mapping, use it. */
3358 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3359 if (bed->elf_backend_sym_is_global)
3360 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3362 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3363 || bfd_is_und_section (bfd_get_section (sym))
3364 || bfd_is_com_section (bfd_get_section (sym)));
3367 /* Don't output section symbols for sections that are not going to be
3368 output, that are duplicates or there is no BFD section. */
3371 ignore_section_sym (bfd *abfd, asymbol *sym)
3373 elf_symbol_type *type_ptr;
3375 if ((sym->flags & BSF_SECTION_SYM) == 0)
3378 type_ptr = elf_symbol_from (abfd, sym);
3379 return ((type_ptr != NULL
3380 && type_ptr->internal_elf_sym.st_shndx != 0
3381 && bfd_is_abs_section (sym->section))
3382 || !(sym->section->owner == abfd
3383 || (sym->section->output_section->owner == abfd
3384 && sym->section->output_offset == 0)
3385 || bfd_is_abs_section (sym->section)));
3388 /* Map symbol from it's internal number to the external number, moving
3389 all local symbols to be at the head of the list. */
3392 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3394 unsigned int symcount = bfd_get_symcount (abfd);
3395 asymbol **syms = bfd_get_outsymbols (abfd);
3396 asymbol **sect_syms;
3397 unsigned int num_locals = 0;
3398 unsigned int num_globals = 0;
3399 unsigned int num_locals2 = 0;
3400 unsigned int num_globals2 = 0;
3407 fprintf (stderr, "elf_map_symbols\n");
3411 for (asect = abfd->sections; asect; asect = asect->next)
3413 if (max_index < asect->index)
3414 max_index = asect->index;
3418 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3419 if (sect_syms == NULL)
3421 elf_section_syms (abfd) = sect_syms;
3422 elf_num_section_syms (abfd) = max_index;
3424 /* Init sect_syms entries for any section symbols we have already
3425 decided to output. */
3426 for (idx = 0; idx < symcount; idx++)
3428 asymbol *sym = syms[idx];
3430 if ((sym->flags & BSF_SECTION_SYM) != 0
3432 && !ignore_section_sym (abfd, sym)
3433 && !bfd_is_abs_section (sym->section))
3435 asection *sec = sym->section;
3437 if (sec->owner != abfd)
3438 sec = sec->output_section;
3440 sect_syms[sec->index] = syms[idx];
3444 /* Classify all of the symbols. */
3445 for (idx = 0; idx < symcount; idx++)
3447 if (sym_is_global (abfd, syms[idx]))
3449 else if (!ignore_section_sym (abfd, syms[idx]))
3453 /* We will be adding a section symbol for each normal BFD section. Most
3454 sections will already have a section symbol in outsymbols, but
3455 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3456 at least in that case. */
3457 for (asect = abfd->sections; asect; asect = asect->next)
3459 if (sect_syms[asect->index] == NULL)
3461 if (!sym_is_global (abfd, asect->symbol))
3468 /* Now sort the symbols so the local symbols are first. */
3469 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3470 sizeof (asymbol *));
3472 if (new_syms == NULL)
3475 for (idx = 0; idx < symcount; idx++)
3477 asymbol *sym = syms[idx];
3480 if (sym_is_global (abfd, sym))
3481 i = num_locals + num_globals2++;
3482 else if (!ignore_section_sym (abfd, sym))
3487 sym->udata.i = i + 1;
3489 for (asect = abfd->sections; asect; asect = asect->next)
3491 if (sect_syms[asect->index] == NULL)
3493 asymbol *sym = asect->symbol;
3496 sect_syms[asect->index] = sym;
3497 if (!sym_is_global (abfd, sym))
3500 i = num_locals + num_globals2++;
3502 sym->udata.i = i + 1;
3506 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3508 *pnum_locals = num_locals;
3512 /* Align to the maximum file alignment that could be required for any
3513 ELF data structure. */
3515 static inline file_ptr
3516 align_file_position (file_ptr off, int align)
3518 return (off + align - 1) & ~(align - 1);
3521 /* Assign a file position to a section, optionally aligning to the
3522 required section alignment. */
3525 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3529 if (align && i_shdrp->sh_addralign > 1)
3530 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3531 i_shdrp->sh_offset = offset;
3532 if (i_shdrp->bfd_section != NULL)
3533 i_shdrp->bfd_section->filepos = offset;
3534 if (i_shdrp->sh_type != SHT_NOBITS)
3535 offset += i_shdrp->sh_size;
3539 /* Compute the file positions we are going to put the sections at, and
3540 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3541 is not NULL, this is being called by the ELF backend linker. */
3544 _bfd_elf_compute_section_file_positions (bfd *abfd,
3545 struct bfd_link_info *link_info)
3547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3548 struct fake_section_arg fsargs;
3550 struct bfd_strtab_hash *strtab = NULL;
3551 Elf_Internal_Shdr *shstrtab_hdr;
3552 bfd_boolean need_symtab;
3554 if (abfd->output_has_begun)
3557 /* Do any elf backend specific processing first. */
3558 if (bed->elf_backend_begin_write_processing)
3559 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3561 if (! prep_headers (abfd))
3564 /* Post process the headers if necessary. */
3565 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3567 fsargs.failed = FALSE;
3568 fsargs.link_info = link_info;
3569 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3573 if (!assign_section_numbers (abfd, link_info))
3576 /* The backend linker builds symbol table information itself. */
3577 need_symtab = (link_info == NULL
3578 && (bfd_get_symcount (abfd) > 0
3579 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3583 /* Non-zero if doing a relocatable link. */
3584 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3586 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3591 if (link_info == NULL)
3593 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3598 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3599 /* sh_name was set in prep_headers. */
3600 shstrtab_hdr->sh_type = SHT_STRTAB;
3601 shstrtab_hdr->sh_flags = 0;
3602 shstrtab_hdr->sh_addr = 0;
3603 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3604 shstrtab_hdr->sh_entsize = 0;
3605 shstrtab_hdr->sh_link = 0;
3606 shstrtab_hdr->sh_info = 0;
3607 /* sh_offset is set in assign_file_positions_except_relocs. */
3608 shstrtab_hdr->sh_addralign = 1;
3610 if (!assign_file_positions_except_relocs (abfd, link_info))
3616 Elf_Internal_Shdr *hdr;
3618 off = elf_next_file_pos (abfd);
3620 hdr = &elf_tdata (abfd)->symtab_hdr;
3621 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3623 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3624 if (hdr->sh_size != 0)
3625 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3627 hdr = &elf_tdata (abfd)->strtab_hdr;
3628 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3630 elf_next_file_pos (abfd) = off;
3632 /* Now that we know where the .strtab section goes, write it
3634 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3635 || ! _bfd_stringtab_emit (abfd, strtab))
3637 _bfd_stringtab_free (strtab);
3640 abfd->output_has_begun = TRUE;
3645 /* Make an initial estimate of the size of the program header. If we
3646 get the number wrong here, we'll redo section placement. */
3648 static bfd_size_type
3649 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3653 const struct elf_backend_data *bed;
3655 /* Assume we will need exactly two PT_LOAD segments: one for text
3656 and one for data. */
3659 s = bfd_get_section_by_name (abfd, ".interp");
3660 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3662 /* If we have a loadable interpreter section, we need a
3663 PT_INTERP segment. In this case, assume we also need a
3664 PT_PHDR segment, although that may not be true for all
3669 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3671 /* We need a PT_DYNAMIC segment. */
3675 if (info != NULL && info->relro)
3677 /* We need a PT_GNU_RELRO segment. */
3681 if (elf_eh_frame_hdr (abfd))
3683 /* We need a PT_GNU_EH_FRAME segment. */
3687 if (elf_stack_flags (abfd))
3689 /* We need a PT_GNU_STACK segment. */
3693 for (s = abfd->sections; s != NULL; s = s->next)
3695 if ((s->flags & SEC_LOAD) != 0
3696 && CONST_STRNEQ (s->name, ".note"))
3698 /* We need a PT_NOTE segment. */
3700 /* Try to create just one PT_NOTE segment
3701 for all adjacent loadable .note* sections.
3702 gABI requires that within a PT_NOTE segment
3703 (and also inside of each SHT_NOTE section)
3704 each note is padded to a multiple of 4 size,
3705 so we check whether the sections are correctly
3707 if (s->alignment_power == 2)
3708 while (s->next != NULL
3709 && s->next->alignment_power == 2
3710 && (s->next->flags & SEC_LOAD) != 0
3711 && CONST_STRNEQ (s->next->name, ".note"))
3716 for (s = abfd->sections; s != NULL; s = s->next)
3718 if (s->flags & SEC_THREAD_LOCAL)
3720 /* We need a PT_TLS segment. */
3726 /* Let the backend count up any program headers it might need. */
3727 bed = get_elf_backend_data (abfd);
3728 if (bed->elf_backend_additional_program_headers)
3732 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3738 return segs * bed->s->sizeof_phdr;
3741 /* Find the segment that contains the output_section of section. */
3744 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3746 struct elf_segment_map *m;
3747 Elf_Internal_Phdr *p;
3749 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
3755 for (i = m->count - 1; i >= 0; i--)
3756 if (m->sections[i] == section)
3763 /* Create a mapping from a set of sections to a program segment. */
3765 static struct elf_segment_map *
3766 make_mapping (bfd *abfd,
3767 asection **sections,
3772 struct elf_segment_map *m;
3777 amt = sizeof (struct elf_segment_map);
3778 amt += (to - from - 1) * sizeof (asection *);
3779 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3783 m->p_type = PT_LOAD;
3784 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3785 m->sections[i - from] = *hdrpp;
3786 m->count = to - from;
3788 if (from == 0 && phdr)
3790 /* Include the headers in the first PT_LOAD segment. */
3791 m->includes_filehdr = 1;
3792 m->includes_phdrs = 1;
3798 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3801 struct elf_segment_map *
3802 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3804 struct elf_segment_map *m;
3806 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3807 sizeof (struct elf_segment_map));
3811 m->p_type = PT_DYNAMIC;
3813 m->sections[0] = dynsec;
3818 /* Possibly add or remove segments from the segment map. */
3821 elf_modify_segment_map (bfd *abfd,
3822 struct bfd_link_info *info,
3823 bfd_boolean remove_empty_load)
3825 struct elf_segment_map **m;
3826 const struct elf_backend_data *bed;
3828 /* The placement algorithm assumes that non allocated sections are
3829 not in PT_LOAD segments. We ensure this here by removing such
3830 sections from the segment map. We also remove excluded
3831 sections. Finally, any PT_LOAD segment without sections is
3833 m = &elf_seg_map (abfd);
3836 unsigned int i, new_count;
3838 for (new_count = 0, i = 0; i < (*m)->count; i++)
3840 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3841 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3842 || (*m)->p_type != PT_LOAD))
3844 (*m)->sections[new_count] = (*m)->sections[i];
3848 (*m)->count = new_count;
3850 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3856 bed = get_elf_backend_data (abfd);
3857 if (bed->elf_backend_modify_segment_map != NULL)
3859 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3866 /* Set up a mapping from BFD sections to program segments. */
3869 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3872 struct elf_segment_map *m;
3873 asection **sections = NULL;
3874 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3875 bfd_boolean no_user_phdrs;
3877 no_user_phdrs = elf_seg_map (abfd) == NULL;
3880 info->user_phdrs = !no_user_phdrs;
3882 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3886 struct elf_segment_map *mfirst;
3887 struct elf_segment_map **pm;
3890 unsigned int phdr_index;
3891 bfd_vma maxpagesize;
3893 bfd_boolean phdr_in_segment = TRUE;
3894 bfd_boolean writable;
3896 asection *first_tls = NULL;
3897 asection *dynsec, *eh_frame_hdr;
3899 bfd_vma addr_mask, wrap_to = 0;
3901 /* Select the allocated sections, and sort them. */
3903 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3904 sizeof (asection *));
3905 if (sections == NULL)
3908 /* Calculate top address, avoiding undefined behaviour of shift
3909 left operator when shift count is equal to size of type
3911 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3912 addr_mask = (addr_mask << 1) + 1;
3915 for (s = abfd->sections; s != NULL; s = s->next)
3917 if ((s->flags & SEC_ALLOC) != 0)
3921 /* A wrapping section potentially clashes with header. */
3922 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3923 wrap_to = (s->lma + s->size) & addr_mask;
3926 BFD_ASSERT (i <= bfd_count_sections (abfd));
3929 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3931 /* Build the mapping. */
3936 /* If we have a .interp section, then create a PT_PHDR segment for
3937 the program headers and a PT_INTERP segment for the .interp
3939 s = bfd_get_section_by_name (abfd, ".interp");
3940 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3942 amt = sizeof (struct elf_segment_map);
3943 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3947 m->p_type = PT_PHDR;
3948 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3949 m->p_flags = PF_R | PF_X;
3950 m->p_flags_valid = 1;
3951 m->includes_phdrs = 1;
3956 amt = sizeof (struct elf_segment_map);
3957 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3961 m->p_type = PT_INTERP;
3969 /* Look through the sections. We put sections in the same program
3970 segment when the start of the second section can be placed within
3971 a few bytes of the end of the first section. */
3975 maxpagesize = bed->maxpagesize;
3977 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3979 && (dynsec->flags & SEC_LOAD) == 0)
3982 /* Deal with -Ttext or something similar such that the first section
3983 is not adjacent to the program headers. This is an
3984 approximation, since at this point we don't know exactly how many
3985 program headers we will need. */
3988 bfd_size_type phdr_size = elf_program_header_size (abfd);
3990 if (phdr_size == (bfd_size_type) -1)
3991 phdr_size = get_program_header_size (abfd, info);
3992 phdr_size += bed->s->sizeof_ehdr;
3993 if ((abfd->flags & D_PAGED) == 0
3994 || (sections[0]->lma & addr_mask) < phdr_size
3995 || ((sections[0]->lma & addr_mask) % maxpagesize
3996 < phdr_size % maxpagesize)
3997 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
3998 phdr_in_segment = FALSE;
4001 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4004 bfd_boolean new_segment;
4008 /* See if this section and the last one will fit in the same
4011 if (last_hdr == NULL)
4013 /* If we don't have a segment yet, then we don't need a new
4014 one (we build the last one after this loop). */
4015 new_segment = FALSE;
4017 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4019 /* If this section has a different relation between the
4020 virtual address and the load address, then we need a new
4024 else if (hdr->lma < last_hdr->lma + last_size
4025 || last_hdr->lma + last_size < last_hdr->lma)
4027 /* If this section has a load address that makes it overlap
4028 the previous section, then we need a new segment. */
4031 /* In the next test we have to be careful when last_hdr->lma is close
4032 to the end of the address space. If the aligned address wraps
4033 around to the start of the address space, then there are no more
4034 pages left in memory and it is OK to assume that the current
4035 section can be included in the current segment. */
4036 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4038 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4041 /* If putting this section in this segment would force us to
4042 skip a page in the segment, then we need a new segment. */
4045 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4046 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4048 /* We don't want to put a loadable section after a
4049 nonloadable section in the same segment.
4050 Consider .tbss sections as loadable for this purpose. */
4053 else if ((abfd->flags & D_PAGED) == 0)
4055 /* If the file is not demand paged, which means that we
4056 don't require the sections to be correctly aligned in the
4057 file, then there is no other reason for a new segment. */
4058 new_segment = FALSE;
4061 && (hdr->flags & SEC_READONLY) == 0
4062 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4063 != (hdr->lma & -maxpagesize)))
4065 /* We don't want to put a writable section in a read only
4066 segment, unless they are on the same page in memory
4067 anyhow. We already know that the last section does not
4068 bring us past the current section on the page, so the
4069 only case in which the new section is not on the same
4070 page as the previous section is when the previous section
4071 ends precisely on a page boundary. */
4076 /* Otherwise, we can use the same segment. */
4077 new_segment = FALSE;
4080 /* Allow interested parties a chance to override our decision. */
4081 if (last_hdr != NULL
4083 && info->callbacks->override_segment_assignment != NULL)
4085 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4091 if ((hdr->flags & SEC_READONLY) == 0)
4094 /* .tbss sections effectively have zero size. */
4095 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4096 != SEC_THREAD_LOCAL)
4097 last_size = hdr->size;
4103 /* We need a new program segment. We must create a new program
4104 header holding all the sections from phdr_index until hdr. */
4106 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4113 if ((hdr->flags & SEC_READONLY) == 0)
4119 /* .tbss sections effectively have zero size. */
4120 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4121 last_size = hdr->size;
4125 phdr_in_segment = FALSE;
4128 /* Create a final PT_LOAD program segment, but not if it's just
4130 if (last_hdr != NULL
4131 && (i - phdr_index != 1
4132 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4133 != SEC_THREAD_LOCAL)))
4135 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4143 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4146 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4153 /* For each batch of consecutive loadable .note sections,
4154 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4155 because if we link together nonloadable .note sections and
4156 loadable .note sections, we will generate two .note sections
4157 in the output file. FIXME: Using names for section types is
4159 for (s = abfd->sections; s != NULL; s = s->next)
4161 if ((s->flags & SEC_LOAD) != 0
4162 && CONST_STRNEQ (s->name, ".note"))
4167 amt = sizeof (struct elf_segment_map);
4168 if (s->alignment_power == 2)
4169 for (s2 = s; s2->next != NULL; s2 = s2->next)
4171 if (s2->next->alignment_power == 2
4172 && (s2->next->flags & SEC_LOAD) != 0
4173 && CONST_STRNEQ (s2->next->name, ".note")
4174 && align_power (s2->lma + s2->size, 2)
4180 amt += (count - 1) * sizeof (asection *);
4181 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4185 m->p_type = PT_NOTE;
4189 m->sections[m->count - count--] = s;
4190 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4193 m->sections[m->count - 1] = s;
4194 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4198 if (s->flags & SEC_THREAD_LOCAL)
4206 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4209 amt = sizeof (struct elf_segment_map);
4210 amt += (tls_count - 1) * sizeof (asection *);
4211 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4216 m->count = tls_count;
4217 /* Mandated PF_R. */
4219 m->p_flags_valid = 1;
4221 for (i = 0; i < (unsigned int) tls_count; ++i)
4223 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4226 (_("%B: TLS sections are not adjacent:"), abfd);
4229 while (i < (unsigned int) tls_count)
4231 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4233 _bfd_error_handler (_(" TLS: %A"), s);
4237 _bfd_error_handler (_(" non-TLS: %A"), s);
4240 bfd_set_error (bfd_error_bad_value);
4251 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4253 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4254 if (eh_frame_hdr != NULL
4255 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4257 amt = sizeof (struct elf_segment_map);
4258 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4262 m->p_type = PT_GNU_EH_FRAME;
4264 m->sections[0] = eh_frame_hdr->output_section;
4270 if (elf_stack_flags (abfd))
4272 amt = sizeof (struct elf_segment_map);
4273 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4277 m->p_type = PT_GNU_STACK;
4278 m->p_flags = elf_stack_flags (abfd);
4279 m->p_align = bed->stack_align;
4280 m->p_flags_valid = 1;
4281 m->p_align_valid = m->p_align != 0;
4282 if (info->stacksize > 0)
4284 m->p_size = info->stacksize;
4285 m->p_size_valid = 1;
4292 if (info != NULL && info->relro)
4294 for (m = mfirst; m != NULL; m = m->next)
4296 if (m->p_type == PT_LOAD
4298 && m->sections[0]->vma >= info->relro_start
4299 && m->sections[0]->vma < info->relro_end)
4302 while (--i != (unsigned) -1)
4303 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4304 == (SEC_LOAD | SEC_HAS_CONTENTS))
4307 if (i != (unsigned) -1)
4312 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4315 amt = sizeof (struct elf_segment_map);
4316 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4320 m->p_type = PT_GNU_RELRO;
4322 m->p_flags_valid = 1;
4330 elf_seg_map (abfd) = mfirst;
4333 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4336 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4338 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4343 if (sections != NULL)
4348 /* Sort sections by address. */
4351 elf_sort_sections (const void *arg1, const void *arg2)
4353 const asection *sec1 = *(const asection **) arg1;
4354 const asection *sec2 = *(const asection **) arg2;
4355 bfd_size_type size1, size2;
4357 /* Sort by LMA first, since this is the address used to
4358 place the section into a segment. */
4359 if (sec1->lma < sec2->lma)
4361 else if (sec1->lma > sec2->lma)
4364 /* Then sort by VMA. Normally the LMA and the VMA will be
4365 the same, and this will do nothing. */
4366 if (sec1->vma < sec2->vma)
4368 else if (sec1->vma > sec2->vma)
4371 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4373 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4379 /* If the indicies are the same, do not return 0
4380 here, but continue to try the next comparison. */
4381 if (sec1->target_index - sec2->target_index != 0)
4382 return sec1->target_index - sec2->target_index;
4387 else if (TOEND (sec2))
4392 /* Sort by size, to put zero sized sections
4393 before others at the same address. */
4395 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4396 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4403 return sec1->target_index - sec2->target_index;
4406 /* Ian Lance Taylor writes:
4408 We shouldn't be using % with a negative signed number. That's just
4409 not good. We have to make sure either that the number is not
4410 negative, or that the number has an unsigned type. When the types
4411 are all the same size they wind up as unsigned. When file_ptr is a
4412 larger signed type, the arithmetic winds up as signed long long,
4415 What we're trying to say here is something like ``increase OFF by
4416 the least amount that will cause it to be equal to the VMA modulo
4418 /* In other words, something like:
4420 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4421 off_offset = off % bed->maxpagesize;
4422 if (vma_offset < off_offset)
4423 adjustment = vma_offset + bed->maxpagesize - off_offset;
4425 adjustment = vma_offset - off_offset;
4427 which can can be collapsed into the expression below. */
4430 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4432 /* PR binutils/16199: Handle an alignment of zero. */
4433 if (maxpagesize == 0)
4435 return ((vma - off) % maxpagesize);
4439 print_segment_map (const struct elf_segment_map *m)
4442 const char *pt = get_segment_type (m->p_type);
4447 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4448 sprintf (buf, "LOPROC+%7.7x",
4449 (unsigned int) (m->p_type - PT_LOPROC));
4450 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4451 sprintf (buf, "LOOS+%7.7x",
4452 (unsigned int) (m->p_type - PT_LOOS));
4454 snprintf (buf, sizeof (buf), "%8.8x",
4455 (unsigned int) m->p_type);
4459 fprintf (stderr, "%s:", pt);
4460 for (j = 0; j < m->count; j++)
4461 fprintf (stderr, " %s", m->sections [j]->name);
4467 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4472 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4474 buf = bfd_zmalloc (len);
4477 ret = bfd_bwrite (buf, len, abfd) == len;
4482 /* Assign file positions to the sections based on the mapping from
4483 sections to segments. This function also sets up some fields in
4487 assign_file_positions_for_load_sections (bfd *abfd,
4488 struct bfd_link_info *link_info)
4490 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4491 struct elf_segment_map *m;
4492 Elf_Internal_Phdr *phdrs;
4493 Elf_Internal_Phdr *p;
4495 bfd_size_type maxpagesize;
4498 bfd_vma header_pad = 0;
4500 if (link_info == NULL
4501 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4505 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4509 header_pad = m->header_size;
4514 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4515 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4519 /* PR binutils/12467. */
4520 elf_elfheader (abfd)->e_phoff = 0;
4521 elf_elfheader (abfd)->e_phentsize = 0;
4524 elf_elfheader (abfd)->e_phnum = alloc;
4526 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4527 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4529 BFD_ASSERT (elf_program_header_size (abfd)
4530 >= alloc * bed->s->sizeof_phdr);
4534 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4538 /* We're writing the size in elf_program_header_size (abfd),
4539 see assign_file_positions_except_relocs, so make sure we have
4540 that amount allocated, with trailing space cleared.
4541 The variable alloc contains the computed need, while
4542 elf_program_header_size (abfd) contains the size used for the
4544 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4545 where the layout is forced to according to a larger size in the
4546 last iterations for the testcase ld-elf/header. */
4547 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4549 phdrs = (Elf_Internal_Phdr *)
4551 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4552 sizeof (Elf_Internal_Phdr));
4553 elf_tdata (abfd)->phdr = phdrs;
4558 if ((abfd->flags & D_PAGED) != 0)
4559 maxpagesize = bed->maxpagesize;
4561 off = bed->s->sizeof_ehdr;
4562 off += alloc * bed->s->sizeof_phdr;
4563 if (header_pad < (bfd_vma) off)
4569 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4571 m = m->next, p++, j++)
4575 bfd_boolean no_contents;
4577 /* If elf_segment_map is not from map_sections_to_segments, the
4578 sections may not be correctly ordered. NOTE: sorting should
4579 not be done to the PT_NOTE section of a corefile, which may
4580 contain several pseudo-sections artificially created by bfd.
4581 Sorting these pseudo-sections breaks things badly. */
4583 && !(elf_elfheader (abfd)->e_type == ET_CORE
4584 && m->p_type == PT_NOTE))
4585 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4588 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4589 number of sections with contents contributing to both p_filesz
4590 and p_memsz, followed by a number of sections with no contents
4591 that just contribute to p_memsz. In this loop, OFF tracks next
4592 available file offset for PT_LOAD and PT_NOTE segments. */
4593 p->p_type = m->p_type;
4594 p->p_flags = m->p_flags;
4599 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4601 if (m->p_paddr_valid)
4602 p->p_paddr = m->p_paddr;
4603 else if (m->count == 0)
4606 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4608 if (p->p_type == PT_LOAD
4609 && (abfd->flags & D_PAGED) != 0)
4611 /* p_align in demand paged PT_LOAD segments effectively stores
4612 the maximum page size. When copying an executable with
4613 objcopy, we set m->p_align from the input file. Use this
4614 value for maxpagesize rather than bed->maxpagesize, which
4615 may be different. Note that we use maxpagesize for PT_TLS
4616 segment alignment later in this function, so we are relying
4617 on at least one PT_LOAD segment appearing before a PT_TLS
4619 if (m->p_align_valid)
4620 maxpagesize = m->p_align;
4622 p->p_align = maxpagesize;
4624 else if (m->p_align_valid)
4625 p->p_align = m->p_align;
4626 else if (m->count == 0)
4627 p->p_align = 1 << bed->s->log_file_align;
4631 no_contents = FALSE;
4633 if (p->p_type == PT_LOAD
4636 bfd_size_type align;
4637 unsigned int align_power = 0;
4639 if (m->p_align_valid)
4643 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4645 unsigned int secalign;
4647 secalign = bfd_get_section_alignment (abfd, *secpp);
4648 if (secalign > align_power)
4649 align_power = secalign;
4651 align = (bfd_size_type) 1 << align_power;
4652 if (align < maxpagesize)
4653 align = maxpagesize;
4656 for (i = 0; i < m->count; i++)
4657 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4658 /* If we aren't making room for this section, then
4659 it must be SHT_NOBITS regardless of what we've
4660 set via struct bfd_elf_special_section. */
4661 elf_section_type (m->sections[i]) = SHT_NOBITS;
4663 /* Find out whether this segment contains any loadable
4666 for (i = 0; i < m->count; i++)
4667 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4669 no_contents = FALSE;
4673 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4677 /* We shouldn't need to align the segment on disk since
4678 the segment doesn't need file space, but the gABI
4679 arguably requires the alignment and glibc ld.so
4680 checks it. So to comply with the alignment
4681 requirement but not waste file space, we adjust
4682 p_offset for just this segment. (OFF_ADJUST is
4683 subtracted from OFF later.) This may put p_offset
4684 past the end of file, but that shouldn't matter. */
4689 /* Make sure the .dynamic section is the first section in the
4690 PT_DYNAMIC segment. */
4691 else if (p->p_type == PT_DYNAMIC
4693 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4696 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4698 bfd_set_error (bfd_error_bad_value);
4701 /* Set the note section type to SHT_NOTE. */
4702 else if (p->p_type == PT_NOTE)
4703 for (i = 0; i < m->count; i++)
4704 elf_section_type (m->sections[i]) = SHT_NOTE;
4710 if (m->includes_filehdr)
4712 if (!m->p_flags_valid)
4714 p->p_filesz = bed->s->sizeof_ehdr;
4715 p->p_memsz = bed->s->sizeof_ehdr;
4718 if (p->p_vaddr < (bfd_vma) off)
4720 (*_bfd_error_handler)
4721 (_("%B: Not enough room for program headers, try linking with -N"),
4723 bfd_set_error (bfd_error_bad_value);
4728 if (!m->p_paddr_valid)
4733 if (m->includes_phdrs)
4735 if (!m->p_flags_valid)
4738 if (!m->includes_filehdr)
4740 p->p_offset = bed->s->sizeof_ehdr;
4744 p->p_vaddr -= off - p->p_offset;
4745 if (!m->p_paddr_valid)
4746 p->p_paddr -= off - p->p_offset;
4750 p->p_filesz += alloc * bed->s->sizeof_phdr;
4751 p->p_memsz += alloc * bed->s->sizeof_phdr;
4754 p->p_filesz += header_pad;
4755 p->p_memsz += header_pad;
4759 if (p->p_type == PT_LOAD
4760 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4762 if (!m->includes_filehdr && !m->includes_phdrs)
4768 adjust = off - (p->p_offset + p->p_filesz);
4770 p->p_filesz += adjust;
4771 p->p_memsz += adjust;
4775 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4776 maps. Set filepos for sections in PT_LOAD segments, and in
4777 core files, for sections in PT_NOTE segments.
4778 assign_file_positions_for_non_load_sections will set filepos
4779 for other sections and update p_filesz for other segments. */
4780 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4783 bfd_size_type align;
4784 Elf_Internal_Shdr *this_hdr;
4787 this_hdr = &elf_section_data (sec)->this_hdr;
4788 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4790 if ((p->p_type == PT_LOAD
4791 || p->p_type == PT_TLS)
4792 && (this_hdr->sh_type != SHT_NOBITS
4793 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4794 && ((this_hdr->sh_flags & SHF_TLS) == 0
4795 || p->p_type == PT_TLS))))
4797 bfd_vma p_start = p->p_paddr;
4798 bfd_vma p_end = p_start + p->p_memsz;
4799 bfd_vma s_start = sec->lma;
4800 bfd_vma adjust = s_start - p_end;
4804 || p_end < p_start))
4806 (*_bfd_error_handler)
4807 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4808 (unsigned long) s_start, (unsigned long) p_end);
4812 p->p_memsz += adjust;
4814 if (this_hdr->sh_type != SHT_NOBITS)
4816 if (p->p_filesz + adjust < p->p_memsz)
4818 /* We have a PROGBITS section following NOBITS ones.
4819 Allocate file space for the NOBITS section(s) and
4821 adjust = p->p_memsz - p->p_filesz;
4822 if (!write_zeros (abfd, off, adjust))
4826 p->p_filesz += adjust;
4830 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4832 /* The section at i == 0 is the one that actually contains
4836 this_hdr->sh_offset = sec->filepos = off;
4837 off += this_hdr->sh_size;
4838 p->p_filesz = this_hdr->sh_size;
4844 /* The rest are fake sections that shouldn't be written. */
4853 if (p->p_type == PT_LOAD)
4855 this_hdr->sh_offset = sec->filepos = off;
4856 if (this_hdr->sh_type != SHT_NOBITS)
4857 off += this_hdr->sh_size;
4859 else if (this_hdr->sh_type == SHT_NOBITS
4860 && (this_hdr->sh_flags & SHF_TLS) != 0
4861 && this_hdr->sh_offset == 0)
4863 /* This is a .tbss section that didn't get a PT_LOAD.
4864 (See _bfd_elf_map_sections_to_segments "Create a
4865 final PT_LOAD".) Set sh_offset to the value it
4866 would have if we had created a zero p_filesz and
4867 p_memsz PT_LOAD header for the section. This
4868 also makes the PT_TLS header have the same
4870 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
4872 this_hdr->sh_offset = sec->filepos = off + adjust;
4875 if (this_hdr->sh_type != SHT_NOBITS)
4877 p->p_filesz += this_hdr->sh_size;
4878 /* A load section without SHF_ALLOC is something like
4879 a note section in a PT_NOTE segment. These take
4880 file space but are not loaded into memory. */
4881 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4882 p->p_memsz += this_hdr->sh_size;
4884 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4886 if (p->p_type == PT_TLS)
4887 p->p_memsz += this_hdr->sh_size;
4889 /* .tbss is special. It doesn't contribute to p_memsz of
4891 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4892 p->p_memsz += this_hdr->sh_size;
4895 if (align > p->p_align
4896 && !m->p_align_valid
4897 && (p->p_type != PT_LOAD
4898 || (abfd->flags & D_PAGED) == 0))
4902 if (!m->p_flags_valid)
4905 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4907 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4914 /* Check that all sections are in a PT_LOAD segment.
4915 Don't check funky gdb generated core files. */
4916 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4918 bfd_boolean check_vma = TRUE;
4920 for (i = 1; i < m->count; i++)
4921 if (m->sections[i]->vma == m->sections[i - 1]->vma
4922 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4923 ->this_hdr), p) != 0
4924 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4925 ->this_hdr), p) != 0)
4927 /* Looks like we have overlays packed into the segment. */
4932 for (i = 0; i < m->count; i++)
4934 Elf_Internal_Shdr *this_hdr;
4937 sec = m->sections[i];
4938 this_hdr = &(elf_section_data(sec)->this_hdr);
4939 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
4940 && !ELF_TBSS_SPECIAL (this_hdr, p))
4942 (*_bfd_error_handler)
4943 (_("%B: section `%A' can't be allocated in segment %d"),
4945 print_segment_map (m);
4951 elf_next_file_pos (abfd) = off;
4955 /* Assign file positions for the other sections. */
4958 assign_file_positions_for_non_load_sections (bfd *abfd,
4959 struct bfd_link_info *link_info)
4961 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4962 Elf_Internal_Shdr **i_shdrpp;
4963 Elf_Internal_Shdr **hdrpp;
4964 Elf_Internal_Phdr *phdrs;
4965 Elf_Internal_Phdr *p;
4966 struct elf_segment_map *m;
4967 struct elf_segment_map *hdrs_segment;
4968 bfd_vma filehdr_vaddr, filehdr_paddr;
4969 bfd_vma phdrs_vaddr, phdrs_paddr;
4971 unsigned int num_sec;
4975 i_shdrpp = elf_elfsections (abfd);
4976 num_sec = elf_numsections (abfd);
4977 off = elf_next_file_pos (abfd);
4978 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4980 Elf_Internal_Shdr *hdr;
4983 if (hdr->bfd_section != NULL
4984 && (hdr->bfd_section->filepos != 0
4985 || (hdr->sh_type == SHT_NOBITS
4986 && hdr->contents == NULL)))
4987 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4988 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4990 if (hdr->sh_size != 0)
4991 (*_bfd_error_handler)
4992 (_("%B: warning: allocated section `%s' not in segment"),
4994 (hdr->bfd_section == NULL
4996 : hdr->bfd_section->name));
4997 /* We don't need to page align empty sections. */
4998 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4999 off += vma_page_aligned_bias (hdr->sh_addr, off,
5002 off += vma_page_aligned_bias (hdr->sh_addr, off,
5004 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5007 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5008 && hdr->bfd_section == NULL)
5009 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5010 || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
5011 || hdr == i_shdrpp[elf_strtab_sec (abfd)])
5012 hdr->sh_offset = -1;
5014 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5017 /* Now that we have set the section file positions, we can set up
5018 the file positions for the non PT_LOAD segments. */
5022 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5024 hdrs_segment = NULL;
5025 phdrs = elf_tdata (abfd)->phdr;
5026 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5029 if (p->p_type != PT_LOAD)
5032 if (m->includes_filehdr)
5034 filehdr_vaddr = p->p_vaddr;
5035 filehdr_paddr = p->p_paddr;
5037 if (m->includes_phdrs)
5039 phdrs_vaddr = p->p_vaddr;
5040 phdrs_paddr = p->p_paddr;
5041 if (m->includes_filehdr)
5044 phdrs_vaddr += bed->s->sizeof_ehdr;
5045 phdrs_paddr += bed->s->sizeof_ehdr;
5050 if (hdrs_segment != NULL && link_info != NULL)
5052 /* There is a segment that contains both the file headers and the
5053 program headers, so provide a symbol __ehdr_start pointing there.
5054 A program can use this to examine itself robustly. */
5056 struct elf_link_hash_entry *hash
5057 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5058 FALSE, FALSE, TRUE);
5059 /* If the symbol was referenced and not defined, define it. */
5061 && (hash->root.type == bfd_link_hash_new
5062 || hash->root.type == bfd_link_hash_undefined
5063 || hash->root.type == bfd_link_hash_undefweak
5064 || hash->root.type == bfd_link_hash_common))
5067 if (hdrs_segment->count != 0)
5068 /* The segment contains sections, so use the first one. */
5069 s = hdrs_segment->sections[0];
5071 /* Use the first (i.e. lowest-addressed) section in any segment. */
5072 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5081 hash->root.u.def.value = filehdr_vaddr - s->vma;
5082 hash->root.u.def.section = s;
5086 hash->root.u.def.value = filehdr_vaddr;
5087 hash->root.u.def.section = bfd_abs_section_ptr;
5090 hash->root.type = bfd_link_hash_defined;
5091 hash->def_regular = 1;
5096 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5098 if (p->p_type == PT_GNU_RELRO)
5100 const Elf_Internal_Phdr *lp;
5101 struct elf_segment_map *lm;
5103 if (link_info != NULL)
5105 /* During linking the range of the RELRO segment is passed
5107 for (lm = elf_seg_map (abfd), lp = phdrs;
5109 lm = lm->next, lp++)
5111 if (lp->p_type == PT_LOAD
5112 && lp->p_vaddr < link_info->relro_end
5114 && lm->sections[0]->vma >= link_info->relro_start)
5118 BFD_ASSERT (lm != NULL);
5122 /* Otherwise we are copying an executable or shared
5123 library, but we need to use the same linker logic. */
5124 for (lp = phdrs; lp < phdrs + count; ++lp)
5126 if (lp->p_type == PT_LOAD
5127 && lp->p_paddr == p->p_paddr)
5132 if (lp < phdrs + count)
5134 p->p_vaddr = lp->p_vaddr;
5135 p->p_paddr = lp->p_paddr;
5136 p->p_offset = lp->p_offset;
5137 if (link_info != NULL)
5138 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5139 else if (m->p_size_valid)
5140 p->p_filesz = m->p_size;
5143 p->p_memsz = p->p_filesz;
5144 /* Preserve the alignment and flags if they are valid. The
5145 gold linker generates RW/4 for the PT_GNU_RELRO section.
5146 It is better for objcopy/strip to honor these attributes
5147 otherwise gdb will choke when using separate debug files.
5149 if (!m->p_align_valid)
5151 if (!m->p_flags_valid)
5152 p->p_flags = (lp->p_flags & ~PF_W);
5156 memset (p, 0, sizeof *p);
5157 p->p_type = PT_NULL;
5160 else if (p->p_type == PT_GNU_STACK)
5162 if (m->p_size_valid)
5163 p->p_memsz = m->p_size;
5165 else if (m->count != 0)
5167 if (p->p_type != PT_LOAD
5168 && (p->p_type != PT_NOTE
5169 || bfd_get_format (abfd) != bfd_core))
5171 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
5174 p->p_offset = m->sections[0]->filepos;
5175 for (i = m->count; i-- != 0;)
5177 asection *sect = m->sections[i];
5178 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5179 if (hdr->sh_type != SHT_NOBITS)
5181 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5188 else if (m->includes_filehdr)
5190 p->p_vaddr = filehdr_vaddr;
5191 if (! m->p_paddr_valid)
5192 p->p_paddr = filehdr_paddr;
5194 else if (m->includes_phdrs)
5196 p->p_vaddr = phdrs_vaddr;
5197 if (! m->p_paddr_valid)
5198 p->p_paddr = phdrs_paddr;
5202 elf_next_file_pos (abfd) = off;
5207 /* Work out the file positions of all the sections. This is called by
5208 _bfd_elf_compute_section_file_positions. All the section sizes and
5209 VMAs must be known before this is called.
5211 Reloc sections come in two flavours: Those processed specially as
5212 "side-channel" data attached to a section to which they apply, and
5213 those that bfd doesn't process as relocations. The latter sort are
5214 stored in a normal bfd section by bfd_section_from_shdr. We don't
5215 consider the former sort here, unless they form part of the loadable
5216 image. Reloc sections not assigned here will be handled later by
5217 assign_file_positions_for_relocs.
5219 We also don't set the positions of the .symtab and .strtab here. */
5222 assign_file_positions_except_relocs (bfd *abfd,
5223 struct bfd_link_info *link_info)
5225 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5226 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5227 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5229 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5230 && bfd_get_format (abfd) != bfd_core)
5232 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5233 unsigned int num_sec = elf_numsections (abfd);
5234 Elf_Internal_Shdr **hdrpp;
5238 /* Start after the ELF header. */
5239 off = i_ehdrp->e_ehsize;
5241 /* We are not creating an executable, which means that we are
5242 not creating a program header, and that the actual order of
5243 the sections in the file is unimportant. */
5244 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5246 Elf_Internal_Shdr *hdr;
5249 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5250 && hdr->bfd_section == NULL)
5251 || i == elf_onesymtab (abfd)
5252 || i == elf_symtab_shndx (abfd)
5253 || i == elf_strtab_sec (abfd))
5255 hdr->sh_offset = -1;
5258 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5261 elf_next_file_pos (abfd) = off;
5267 /* Assign file positions for the loaded sections based on the
5268 assignment of sections to segments. */
5269 if (!assign_file_positions_for_load_sections (abfd, link_info))
5272 /* And for non-load sections. */
5273 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5276 if (bed->elf_backend_modify_program_headers != NULL)
5278 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5282 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5283 if (link_info != NULL
5284 && link_info->executable
5285 && link_info->shared)
5287 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
5288 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
5289 Elf_Internal_Phdr *end_segment = &segment[num_segments];
5291 /* Find the lowest p_vaddr in PT_LOAD segments. */
5292 bfd_vma p_vaddr = (bfd_vma) -1;
5293 for (; segment < end_segment; segment++)
5294 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
5295 p_vaddr = segment->p_vaddr;
5297 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5298 segments is non-zero. */
5300 i_ehdrp->e_type = ET_EXEC;
5303 /* Write out the program headers. */
5304 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5305 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5306 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5314 prep_headers (bfd *abfd)
5316 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5317 struct elf_strtab_hash *shstrtab;
5318 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5320 i_ehdrp = elf_elfheader (abfd);
5322 shstrtab = _bfd_elf_strtab_init ();
5323 if (shstrtab == NULL)
5326 elf_shstrtab (abfd) = shstrtab;
5328 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5329 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5330 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5331 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5333 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5334 i_ehdrp->e_ident[EI_DATA] =
5335 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5336 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5338 if ((abfd->flags & DYNAMIC) != 0)
5339 i_ehdrp->e_type = ET_DYN;
5340 else if ((abfd->flags & EXEC_P) != 0)
5341 i_ehdrp->e_type = ET_EXEC;
5342 else if (bfd_get_format (abfd) == bfd_core)
5343 i_ehdrp->e_type = ET_CORE;
5345 i_ehdrp->e_type = ET_REL;
5347 switch (bfd_get_arch (abfd))
5349 case bfd_arch_unknown:
5350 i_ehdrp->e_machine = EM_NONE;
5353 /* There used to be a long list of cases here, each one setting
5354 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5355 in the corresponding bfd definition. To avoid duplication,
5356 the switch was removed. Machines that need special handling
5357 can generally do it in elf_backend_final_write_processing(),
5358 unless they need the information earlier than the final write.
5359 Such need can generally be supplied by replacing the tests for
5360 e_machine with the conditions used to determine it. */
5362 i_ehdrp->e_machine = bed->elf_machine_code;
5365 i_ehdrp->e_version = bed->s->ev_current;
5366 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5368 /* No program header, for now. */
5369 i_ehdrp->e_phoff = 0;
5370 i_ehdrp->e_phentsize = 0;
5371 i_ehdrp->e_phnum = 0;
5373 /* Each bfd section is section header entry. */
5374 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5375 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5377 /* If we're building an executable, we'll need a program header table. */
5378 if (abfd->flags & EXEC_P)
5379 /* It all happens later. */
5383 i_ehdrp->e_phentsize = 0;
5384 i_ehdrp->e_phoff = 0;
5387 elf_tdata (abfd)->symtab_hdr.sh_name =
5388 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5389 elf_tdata (abfd)->strtab_hdr.sh_name =
5390 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5391 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5392 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5393 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5394 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
5395 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5401 /* Assign file positions for all the reloc sections which are not part
5402 of the loadable file image, and the file position of section headers. */
5405 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5408 unsigned int i, num_sec;
5409 Elf_Internal_Shdr **shdrpp;
5410 Elf_Internal_Ehdr *i_ehdrp;
5411 const struct elf_backend_data *bed;
5413 off = elf_next_file_pos (abfd);
5415 num_sec = elf_numsections (abfd);
5416 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5418 Elf_Internal_Shdr *shdrp;
5421 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5422 && shdrp->sh_offset == -1)
5423 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5426 /* Place the section headers. */
5427 i_ehdrp = elf_elfheader (abfd);
5428 bed = get_elf_backend_data (abfd);
5429 off = align_file_position (off, 1 << bed->s->log_file_align);
5430 i_ehdrp->e_shoff = off;
5431 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5432 elf_next_file_pos (abfd) = off;
5436 _bfd_elf_write_object_contents (bfd *abfd)
5438 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5439 Elf_Internal_Shdr **i_shdrp;
5441 unsigned int count, num_sec;
5442 struct elf_obj_tdata *t;
5444 if (! abfd->output_has_begun
5445 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5448 i_shdrp = elf_elfsections (abfd);
5451 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5455 _bfd_elf_assign_file_positions_for_relocs (abfd);
5457 /* After writing the headers, we need to write the sections too... */
5458 num_sec = elf_numsections (abfd);
5459 for (count = 1; count < num_sec; count++)
5461 if (bed->elf_backend_section_processing)
5462 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5463 if (i_shdrp[count]->contents)
5465 bfd_size_type amt = i_shdrp[count]->sh_size;
5467 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5468 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5473 /* Write out the section header names. */
5474 t = elf_tdata (abfd);
5475 if (elf_shstrtab (abfd) != NULL
5476 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5477 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5480 if (bed->elf_backend_final_write_processing)
5481 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5483 if (!bed->s->write_shdrs_and_ehdr (abfd))
5486 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5487 if (t->o->build_id.after_write_object_contents != NULL)
5488 return (*t->o->build_id.after_write_object_contents) (abfd);
5494 _bfd_elf_write_corefile_contents (bfd *abfd)
5496 /* Hopefully this can be done just like an object file. */
5497 return _bfd_elf_write_object_contents (abfd);
5500 /* Given a section, search the header to find them. */
5503 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5505 const struct elf_backend_data *bed;
5506 unsigned int sec_index;
5508 if (elf_section_data (asect) != NULL
5509 && elf_section_data (asect)->this_idx != 0)
5510 return elf_section_data (asect)->this_idx;
5512 if (bfd_is_abs_section (asect))
5513 sec_index = SHN_ABS;
5514 else if (bfd_is_com_section (asect))
5515 sec_index = SHN_COMMON;
5516 else if (bfd_is_und_section (asect))
5517 sec_index = SHN_UNDEF;
5519 sec_index = SHN_BAD;
5521 bed = get_elf_backend_data (abfd);
5522 if (bed->elf_backend_section_from_bfd_section)
5524 int retval = sec_index;
5526 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5530 if (sec_index == SHN_BAD)
5531 bfd_set_error (bfd_error_nonrepresentable_section);
5536 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5540 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5542 asymbol *asym_ptr = *asym_ptr_ptr;
5544 flagword flags = asym_ptr->flags;
5546 /* When gas creates relocations against local labels, it creates its
5547 own symbol for the section, but does put the symbol into the
5548 symbol chain, so udata is 0. When the linker is generating
5549 relocatable output, this section symbol may be for one of the
5550 input sections rather than the output section. */
5551 if (asym_ptr->udata.i == 0
5552 && (flags & BSF_SECTION_SYM)
5553 && asym_ptr->section)
5558 sec = asym_ptr->section;
5559 if (sec->owner != abfd && sec->output_section != NULL)
5560 sec = sec->output_section;
5561 if (sec->owner == abfd
5562 && (indx = sec->index) < elf_num_section_syms (abfd)
5563 && elf_section_syms (abfd)[indx] != NULL)
5564 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5567 idx = asym_ptr->udata.i;
5571 /* This case can occur when using --strip-symbol on a symbol
5572 which is used in a relocation entry. */
5573 (*_bfd_error_handler)
5574 (_("%B: symbol `%s' required but not present"),
5575 abfd, bfd_asymbol_name (asym_ptr));
5576 bfd_set_error (bfd_error_no_symbols);
5583 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5584 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5592 /* Rewrite program header information. */
5595 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5597 Elf_Internal_Ehdr *iehdr;
5598 struct elf_segment_map *map;
5599 struct elf_segment_map *map_first;
5600 struct elf_segment_map **pointer_to_map;
5601 Elf_Internal_Phdr *segment;
5604 unsigned int num_segments;
5605 bfd_boolean phdr_included = FALSE;
5606 bfd_boolean p_paddr_valid;
5607 bfd_vma maxpagesize;
5608 struct elf_segment_map *phdr_adjust_seg = NULL;
5609 unsigned int phdr_adjust_num = 0;
5610 const struct elf_backend_data *bed;
5612 bed = get_elf_backend_data (ibfd);
5613 iehdr = elf_elfheader (ibfd);
5616 pointer_to_map = &map_first;
5618 num_segments = elf_elfheader (ibfd)->e_phnum;
5619 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5621 /* Returns the end address of the segment + 1. */
5622 #define SEGMENT_END(segment, start) \
5623 (start + (segment->p_memsz > segment->p_filesz \
5624 ? segment->p_memsz : segment->p_filesz))
5626 #define SECTION_SIZE(section, segment) \
5627 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5628 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5629 ? section->size : 0)
5631 /* Returns TRUE if the given section is contained within
5632 the given segment. VMA addresses are compared. */
5633 #define IS_CONTAINED_BY_VMA(section, segment) \
5634 (section->vma >= segment->p_vaddr \
5635 && (section->vma + SECTION_SIZE (section, segment) \
5636 <= (SEGMENT_END (segment, segment->p_vaddr))))
5638 /* Returns TRUE if the given section is contained within
5639 the given segment. LMA addresses are compared. */
5640 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5641 (section->lma >= base \
5642 && (section->lma + SECTION_SIZE (section, segment) \
5643 <= SEGMENT_END (segment, base)))
5645 /* Handle PT_NOTE segment. */
5646 #define IS_NOTE(p, s) \
5647 (p->p_type == PT_NOTE \
5648 && elf_section_type (s) == SHT_NOTE \
5649 && (bfd_vma) s->filepos >= p->p_offset \
5650 && ((bfd_vma) s->filepos + s->size \
5651 <= p->p_offset + p->p_filesz))
5653 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5655 #define IS_COREFILE_NOTE(p, s) \
5657 && bfd_get_format (ibfd) == bfd_core \
5661 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5662 linker, which generates a PT_INTERP section with p_vaddr and
5663 p_memsz set to 0. */
5664 #define IS_SOLARIS_PT_INTERP(p, s) \
5666 && p->p_paddr == 0 \
5667 && p->p_memsz == 0 \
5668 && p->p_filesz > 0 \
5669 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5671 && (bfd_vma) s->filepos >= p->p_offset \
5672 && ((bfd_vma) s->filepos + s->size \
5673 <= p->p_offset + p->p_filesz))
5675 /* Decide if the given section should be included in the given segment.
5676 A section will be included if:
5677 1. It is within the address space of the segment -- we use the LMA
5678 if that is set for the segment and the VMA otherwise,
5679 2. It is an allocated section or a NOTE section in a PT_NOTE
5681 3. There is an output section associated with it,
5682 4. The section has not already been allocated to a previous segment.
5683 5. PT_GNU_STACK segments do not include any sections.
5684 6. PT_TLS segment includes only SHF_TLS sections.
5685 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5686 8. PT_DYNAMIC should not contain empty sections at the beginning
5687 (with the possible exception of .dynamic). */
5688 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5689 ((((segment->p_paddr \
5690 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5691 : IS_CONTAINED_BY_VMA (section, segment)) \
5692 && (section->flags & SEC_ALLOC) != 0) \
5693 || IS_NOTE (segment, section)) \
5694 && segment->p_type != PT_GNU_STACK \
5695 && (segment->p_type != PT_TLS \
5696 || (section->flags & SEC_THREAD_LOCAL)) \
5697 && (segment->p_type == PT_LOAD \
5698 || segment->p_type == PT_TLS \
5699 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5700 && (segment->p_type != PT_DYNAMIC \
5701 || SECTION_SIZE (section, segment) > 0 \
5702 || (segment->p_paddr \
5703 ? segment->p_paddr != section->lma \
5704 : segment->p_vaddr != section->vma) \
5705 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5707 && !section->segment_mark)
5709 /* If the output section of a section in the input segment is NULL,
5710 it is removed from the corresponding output segment. */
5711 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5712 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5713 && section->output_section != NULL)
5715 /* Returns TRUE iff seg1 starts after the end of seg2. */
5716 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5717 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5719 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5720 their VMA address ranges and their LMA address ranges overlap.
5721 It is possible to have overlapping VMA ranges without overlapping LMA
5722 ranges. RedBoot images for example can have both .data and .bss mapped
5723 to the same VMA range, but with the .data section mapped to a different
5725 #define SEGMENT_OVERLAPS(seg1, seg2) \
5726 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5727 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5728 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5729 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5731 /* Initialise the segment mark field. */
5732 for (section = ibfd->sections; section != NULL; section = section->next)
5733 section->segment_mark = FALSE;
5735 /* The Solaris linker creates program headers in which all the
5736 p_paddr fields are zero. When we try to objcopy or strip such a
5737 file, we get confused. Check for this case, and if we find it
5738 don't set the p_paddr_valid fields. */
5739 p_paddr_valid = FALSE;
5740 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5743 if (segment->p_paddr != 0)
5745 p_paddr_valid = TRUE;
5749 /* Scan through the segments specified in the program header
5750 of the input BFD. For this first scan we look for overlaps
5751 in the loadable segments. These can be created by weird
5752 parameters to objcopy. Also, fix some solaris weirdness. */
5753 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5758 Elf_Internal_Phdr *segment2;
5760 if (segment->p_type == PT_INTERP)
5761 for (section = ibfd->sections; section; section = section->next)
5762 if (IS_SOLARIS_PT_INTERP (segment, section))
5764 /* Mininal change so that the normal section to segment
5765 assignment code will work. */
5766 segment->p_vaddr = section->vma;
5770 if (segment->p_type != PT_LOAD)
5772 /* Remove PT_GNU_RELRO segment. */
5773 if (segment->p_type == PT_GNU_RELRO)
5774 segment->p_type = PT_NULL;
5778 /* Determine if this segment overlaps any previous segments. */
5779 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5781 bfd_signed_vma extra_length;
5783 if (segment2->p_type != PT_LOAD
5784 || !SEGMENT_OVERLAPS (segment, segment2))
5787 /* Merge the two segments together. */
5788 if (segment2->p_vaddr < segment->p_vaddr)
5790 /* Extend SEGMENT2 to include SEGMENT and then delete
5792 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5793 - SEGMENT_END (segment2, segment2->p_vaddr));
5795 if (extra_length > 0)
5797 segment2->p_memsz += extra_length;
5798 segment2->p_filesz += extra_length;
5801 segment->p_type = PT_NULL;
5803 /* Since we have deleted P we must restart the outer loop. */
5805 segment = elf_tdata (ibfd)->phdr;
5810 /* Extend SEGMENT to include SEGMENT2 and then delete
5812 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5813 - SEGMENT_END (segment, segment->p_vaddr));
5815 if (extra_length > 0)
5817 segment->p_memsz += extra_length;
5818 segment->p_filesz += extra_length;
5821 segment2->p_type = PT_NULL;
5826 /* The second scan attempts to assign sections to segments. */
5827 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5831 unsigned int section_count;
5832 asection **sections;
5833 asection *output_section;
5835 bfd_vma matching_lma;
5836 bfd_vma suggested_lma;
5839 asection *first_section;
5840 bfd_boolean first_matching_lma;
5841 bfd_boolean first_suggested_lma;
5843 if (segment->p_type == PT_NULL)
5846 first_section = NULL;
5847 /* Compute how many sections might be placed into this segment. */
5848 for (section = ibfd->sections, section_count = 0;
5850 section = section->next)
5852 /* Find the first section in the input segment, which may be
5853 removed from the corresponding output segment. */
5854 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5856 if (first_section == NULL)
5857 first_section = section;
5858 if (section->output_section != NULL)
5863 /* Allocate a segment map big enough to contain
5864 all of the sections we have selected. */
5865 amt = sizeof (struct elf_segment_map);
5866 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5867 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5871 /* Initialise the fields of the segment map. Default to
5872 using the physical address of the segment in the input BFD. */
5874 map->p_type = segment->p_type;
5875 map->p_flags = segment->p_flags;
5876 map->p_flags_valid = 1;
5878 /* If the first section in the input segment is removed, there is
5879 no need to preserve segment physical address in the corresponding
5881 if (!first_section || first_section->output_section != NULL)
5883 map->p_paddr = segment->p_paddr;
5884 map->p_paddr_valid = p_paddr_valid;
5887 /* Determine if this segment contains the ELF file header
5888 and if it contains the program headers themselves. */
5889 map->includes_filehdr = (segment->p_offset == 0
5890 && segment->p_filesz >= iehdr->e_ehsize);
5891 map->includes_phdrs = 0;
5893 if (!phdr_included || segment->p_type != PT_LOAD)
5895 map->includes_phdrs =
5896 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5897 && (segment->p_offset + segment->p_filesz
5898 >= ((bfd_vma) iehdr->e_phoff
5899 + iehdr->e_phnum * iehdr->e_phentsize)));
5901 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5902 phdr_included = TRUE;
5905 if (section_count == 0)
5907 /* Special segments, such as the PT_PHDR segment, may contain
5908 no sections, but ordinary, loadable segments should contain
5909 something. They are allowed by the ELF spec however, so only
5910 a warning is produced. */
5911 if (segment->p_type == PT_LOAD)
5912 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5913 " detected, is this intentional ?\n"),
5917 *pointer_to_map = map;
5918 pointer_to_map = &map->next;
5923 /* Now scan the sections in the input BFD again and attempt
5924 to add their corresponding output sections to the segment map.
5925 The problem here is how to handle an output section which has
5926 been moved (ie had its LMA changed). There are four possibilities:
5928 1. None of the sections have been moved.
5929 In this case we can continue to use the segment LMA from the
5932 2. All of the sections have been moved by the same amount.
5933 In this case we can change the segment's LMA to match the LMA
5934 of the first section.
5936 3. Some of the sections have been moved, others have not.
5937 In this case those sections which have not been moved can be
5938 placed in the current segment which will have to have its size,
5939 and possibly its LMA changed, and a new segment or segments will
5940 have to be created to contain the other sections.
5942 4. The sections have been moved, but not by the same amount.
5943 In this case we can change the segment's LMA to match the LMA
5944 of the first section and we will have to create a new segment
5945 or segments to contain the other sections.
5947 In order to save time, we allocate an array to hold the section
5948 pointers that we are interested in. As these sections get assigned
5949 to a segment, they are removed from this array. */
5951 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5952 if (sections == NULL)
5955 /* Step One: Scan for segment vs section LMA conflicts.
5956 Also add the sections to the section array allocated above.
5957 Also add the sections to the current segment. In the common
5958 case, where the sections have not been moved, this means that
5959 we have completely filled the segment, and there is nothing
5964 first_matching_lma = TRUE;
5965 first_suggested_lma = TRUE;
5967 for (section = ibfd->sections;
5969 section = section->next)
5970 if (section == first_section)
5973 for (j = 0; section != NULL; section = section->next)
5975 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5977 output_section = section->output_section;
5979 sections[j++] = section;
5981 /* The Solaris native linker always sets p_paddr to 0.
5982 We try to catch that case here, and set it to the
5983 correct value. Note - some backends require that
5984 p_paddr be left as zero. */
5986 && segment->p_vaddr != 0
5987 && !bed->want_p_paddr_set_to_zero
5989 && output_section->lma != 0
5990 && output_section->vma == (segment->p_vaddr
5991 + (map->includes_filehdr
5994 + (map->includes_phdrs
5996 * iehdr->e_phentsize)
5998 map->p_paddr = segment->p_vaddr;
6000 /* Match up the physical address of the segment with the
6001 LMA address of the output section. */
6002 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6003 || IS_COREFILE_NOTE (segment, section)
6004 || (bed->want_p_paddr_set_to_zero
6005 && IS_CONTAINED_BY_VMA (output_section, segment)))
6007 if (first_matching_lma || output_section->lma < matching_lma)
6009 matching_lma = output_section->lma;
6010 first_matching_lma = FALSE;
6013 /* We assume that if the section fits within the segment
6014 then it does not overlap any other section within that
6016 map->sections[isec++] = output_section;
6018 else if (first_suggested_lma)
6020 suggested_lma = output_section->lma;
6021 first_suggested_lma = FALSE;
6024 if (j == section_count)
6029 BFD_ASSERT (j == section_count);
6031 /* Step Two: Adjust the physical address of the current segment,
6033 if (isec == section_count)
6035 /* All of the sections fitted within the segment as currently
6036 specified. This is the default case. Add the segment to
6037 the list of built segments and carry on to process the next
6038 program header in the input BFD. */
6039 map->count = section_count;
6040 *pointer_to_map = map;
6041 pointer_to_map = &map->next;
6044 && !bed->want_p_paddr_set_to_zero
6045 && matching_lma != map->p_paddr
6046 && !map->includes_filehdr
6047 && !map->includes_phdrs)
6048 /* There is some padding before the first section in the
6049 segment. So, we must account for that in the output
6051 map->p_vaddr_offset = matching_lma - map->p_paddr;
6058 if (!first_matching_lma)
6060 /* At least one section fits inside the current segment.
6061 Keep it, but modify its physical address to match the
6062 LMA of the first section that fitted. */
6063 map->p_paddr = matching_lma;
6067 /* None of the sections fitted inside the current segment.
6068 Change the current segment's physical address to match
6069 the LMA of the first section. */
6070 map->p_paddr = suggested_lma;
6073 /* Offset the segment physical address from the lma
6074 to allow for space taken up by elf headers. */
6075 if (map->includes_filehdr)
6077 if (map->p_paddr >= iehdr->e_ehsize)
6078 map->p_paddr -= iehdr->e_ehsize;
6081 map->includes_filehdr = FALSE;
6082 map->includes_phdrs = FALSE;
6086 if (map->includes_phdrs)
6088 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6090 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6092 /* iehdr->e_phnum is just an estimate of the number
6093 of program headers that we will need. Make a note
6094 here of the number we used and the segment we chose
6095 to hold these headers, so that we can adjust the
6096 offset when we know the correct value. */
6097 phdr_adjust_num = iehdr->e_phnum;
6098 phdr_adjust_seg = map;
6101 map->includes_phdrs = FALSE;
6105 /* Step Three: Loop over the sections again, this time assigning
6106 those that fit to the current segment and removing them from the
6107 sections array; but making sure not to leave large gaps. Once all
6108 possible sections have been assigned to the current segment it is
6109 added to the list of built segments and if sections still remain
6110 to be assigned, a new segment is constructed before repeating
6117 first_suggested_lma = TRUE;
6119 /* Fill the current segment with sections that fit. */
6120 for (j = 0; j < section_count; j++)
6122 section = sections[j];
6124 if (section == NULL)
6127 output_section = section->output_section;
6129 BFD_ASSERT (output_section != NULL);
6131 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6132 || IS_COREFILE_NOTE (segment, section))
6134 if (map->count == 0)
6136 /* If the first section in a segment does not start at
6137 the beginning of the segment, then something is
6139 if (output_section->lma
6141 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6142 + (map->includes_phdrs
6143 ? iehdr->e_phnum * iehdr->e_phentsize
6151 prev_sec = map->sections[map->count - 1];
6153 /* If the gap between the end of the previous section
6154 and the start of this section is more than
6155 maxpagesize then we need to start a new segment. */
6156 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6158 < BFD_ALIGN (output_section->lma, maxpagesize))
6159 || (prev_sec->lma + prev_sec->size
6160 > output_section->lma))
6162 if (first_suggested_lma)
6164 suggested_lma = output_section->lma;
6165 first_suggested_lma = FALSE;
6172 map->sections[map->count++] = output_section;
6175 section->segment_mark = TRUE;
6177 else if (first_suggested_lma)
6179 suggested_lma = output_section->lma;
6180 first_suggested_lma = FALSE;
6184 BFD_ASSERT (map->count > 0);
6186 /* Add the current segment to the list of built segments. */
6187 *pointer_to_map = map;
6188 pointer_to_map = &map->next;
6190 if (isec < section_count)
6192 /* We still have not allocated all of the sections to
6193 segments. Create a new segment here, initialise it
6194 and carry on looping. */
6195 amt = sizeof (struct elf_segment_map);
6196 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6197 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6204 /* Initialise the fields of the segment map. Set the physical
6205 physical address to the LMA of the first section that has
6206 not yet been assigned. */
6208 map->p_type = segment->p_type;
6209 map->p_flags = segment->p_flags;
6210 map->p_flags_valid = 1;
6211 map->p_paddr = suggested_lma;
6212 map->p_paddr_valid = p_paddr_valid;
6213 map->includes_filehdr = 0;
6214 map->includes_phdrs = 0;
6217 while (isec < section_count);
6222 elf_seg_map (obfd) = map_first;
6224 /* If we had to estimate the number of program headers that were
6225 going to be needed, then check our estimate now and adjust
6226 the offset if necessary. */
6227 if (phdr_adjust_seg != NULL)
6231 for (count = 0, map = map_first; map != NULL; map = map->next)
6234 if (count > phdr_adjust_num)
6235 phdr_adjust_seg->p_paddr
6236 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6241 #undef IS_CONTAINED_BY_VMA
6242 #undef IS_CONTAINED_BY_LMA
6244 #undef IS_COREFILE_NOTE
6245 #undef IS_SOLARIS_PT_INTERP
6246 #undef IS_SECTION_IN_INPUT_SEGMENT
6247 #undef INCLUDE_SECTION_IN_SEGMENT
6248 #undef SEGMENT_AFTER_SEGMENT
6249 #undef SEGMENT_OVERLAPS
6253 /* Copy ELF program header information. */
6256 copy_elf_program_header (bfd *ibfd, bfd *obfd)
6258 Elf_Internal_Ehdr *iehdr;
6259 struct elf_segment_map *map;
6260 struct elf_segment_map *map_first;
6261 struct elf_segment_map **pointer_to_map;
6262 Elf_Internal_Phdr *segment;
6264 unsigned int num_segments;
6265 bfd_boolean phdr_included = FALSE;
6266 bfd_boolean p_paddr_valid;
6268 iehdr = elf_elfheader (ibfd);
6271 pointer_to_map = &map_first;
6273 /* If all the segment p_paddr fields are zero, don't set
6274 map->p_paddr_valid. */
6275 p_paddr_valid = FALSE;
6276 num_segments = elf_elfheader (ibfd)->e_phnum;
6277 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6280 if (segment->p_paddr != 0)
6282 p_paddr_valid = TRUE;
6286 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6291 unsigned int section_count;
6293 Elf_Internal_Shdr *this_hdr;
6294 asection *first_section = NULL;
6295 asection *lowest_section;
6297 /* Compute how many sections are in this segment. */
6298 for (section = ibfd->sections, section_count = 0;
6300 section = section->next)
6302 this_hdr = &(elf_section_data(section)->this_hdr);
6303 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6305 if (first_section == NULL)
6306 first_section = section;
6311 /* Allocate a segment map big enough to contain
6312 all of the sections we have selected. */
6313 amt = sizeof (struct elf_segment_map);
6314 if (section_count != 0)
6315 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6316 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6320 /* Initialize the fields of the output segment map with the
6323 map->p_type = segment->p_type;
6324 map->p_flags = segment->p_flags;
6325 map->p_flags_valid = 1;
6326 map->p_paddr = segment->p_paddr;
6327 map->p_paddr_valid = p_paddr_valid;
6328 map->p_align = segment->p_align;
6329 map->p_align_valid = 1;
6330 map->p_vaddr_offset = 0;
6332 if (map->p_type == PT_GNU_RELRO
6333 || map->p_type == PT_GNU_STACK)
6335 /* The PT_GNU_RELRO segment may contain the first a few
6336 bytes in the .got.plt section even if the whole .got.plt
6337 section isn't in the PT_GNU_RELRO segment. We won't
6338 change the size of the PT_GNU_RELRO segment.
6339 Similarly, PT_GNU_STACK size is significant on uclinux
6341 map->p_size = segment->p_memsz;
6342 map->p_size_valid = 1;
6345 /* Determine if this segment contains the ELF file header
6346 and if it contains the program headers themselves. */
6347 map->includes_filehdr = (segment->p_offset == 0
6348 && segment->p_filesz >= iehdr->e_ehsize);
6350 map->includes_phdrs = 0;
6351 if (! phdr_included || segment->p_type != PT_LOAD)
6353 map->includes_phdrs =
6354 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6355 && (segment->p_offset + segment->p_filesz
6356 >= ((bfd_vma) iehdr->e_phoff
6357 + iehdr->e_phnum * iehdr->e_phentsize)));
6359 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6360 phdr_included = TRUE;
6363 lowest_section = NULL;
6364 if (section_count != 0)
6366 unsigned int isec = 0;
6368 for (section = first_section;
6370 section = section->next)
6372 this_hdr = &(elf_section_data(section)->this_hdr);
6373 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6375 map->sections[isec++] = section->output_section;
6376 if ((section->flags & SEC_ALLOC) != 0)
6380 if (lowest_section == NULL
6381 || section->lma < lowest_section->lma)
6382 lowest_section = section;
6384 /* Section lmas are set up from PT_LOAD header
6385 p_paddr in _bfd_elf_make_section_from_shdr.
6386 If this header has a p_paddr that disagrees
6387 with the section lma, flag the p_paddr as
6389 if ((section->flags & SEC_LOAD) != 0)
6390 seg_off = this_hdr->sh_offset - segment->p_offset;
6392 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6393 if (section->lma - segment->p_paddr != seg_off)
6394 map->p_paddr_valid = FALSE;
6396 if (isec == section_count)
6402 if (map->includes_filehdr && lowest_section != NULL)
6403 /* We need to keep the space used by the headers fixed. */
6404 map->header_size = lowest_section->vma - segment->p_vaddr;
6406 if (!map->includes_phdrs
6407 && !map->includes_filehdr
6408 && map->p_paddr_valid)
6409 /* There is some other padding before the first section. */
6410 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6411 - segment->p_paddr);
6413 map->count = section_count;
6414 *pointer_to_map = map;
6415 pointer_to_map = &map->next;
6418 elf_seg_map (obfd) = map_first;
6422 /* Copy private BFD data. This copies or rewrites ELF program header
6426 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6428 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6429 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6432 if (elf_tdata (ibfd)->phdr == NULL)
6435 if (ibfd->xvec == obfd->xvec)
6437 /* Check to see if any sections in the input BFD
6438 covered by ELF program header have changed. */
6439 Elf_Internal_Phdr *segment;
6440 asection *section, *osec;
6441 unsigned int i, num_segments;
6442 Elf_Internal_Shdr *this_hdr;
6443 const struct elf_backend_data *bed;
6445 bed = get_elf_backend_data (ibfd);
6447 /* Regenerate the segment map if p_paddr is set to 0. */
6448 if (bed->want_p_paddr_set_to_zero)
6451 /* Initialize the segment mark field. */
6452 for (section = obfd->sections; section != NULL;
6453 section = section->next)
6454 section->segment_mark = FALSE;
6456 num_segments = elf_elfheader (ibfd)->e_phnum;
6457 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6461 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6462 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6463 which severly confuses things, so always regenerate the segment
6464 map in this case. */
6465 if (segment->p_paddr == 0
6466 && segment->p_memsz == 0
6467 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6470 for (section = ibfd->sections;
6471 section != NULL; section = section->next)
6473 /* We mark the output section so that we know it comes
6474 from the input BFD. */
6475 osec = section->output_section;
6477 osec->segment_mark = TRUE;
6479 /* Check if this section is covered by the segment. */
6480 this_hdr = &(elf_section_data(section)->this_hdr);
6481 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6483 /* FIXME: Check if its output section is changed or
6484 removed. What else do we need to check? */
6486 || section->flags != osec->flags
6487 || section->lma != osec->lma
6488 || section->vma != osec->vma
6489 || section->size != osec->size
6490 || section->rawsize != osec->rawsize
6491 || section->alignment_power != osec->alignment_power)
6497 /* Check to see if any output section do not come from the
6499 for (section = obfd->sections; section != NULL;
6500 section = section->next)
6502 if (section->segment_mark == FALSE)
6505 section->segment_mark = FALSE;
6508 return copy_elf_program_header (ibfd, obfd);
6512 if (ibfd->xvec == obfd->xvec)
6514 /* When rewriting program header, set the output maxpagesize to
6515 the maximum alignment of input PT_LOAD segments. */
6516 Elf_Internal_Phdr *segment;
6518 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6519 bfd_vma maxpagesize = 0;
6521 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6524 if (segment->p_type == PT_LOAD
6525 && maxpagesize < segment->p_align)
6526 maxpagesize = segment->p_align;
6528 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6529 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6532 return rewrite_elf_program_header (ibfd, obfd);
6535 /* Initialize private output section information from input section. */
6538 _bfd_elf_init_private_section_data (bfd *ibfd,
6542 struct bfd_link_info *link_info)
6545 Elf_Internal_Shdr *ihdr, *ohdr;
6546 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6548 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6549 || obfd->xvec->flavour != bfd_target_elf_flavour)
6552 BFD_ASSERT (elf_section_data (osec) != NULL);
6554 /* For objcopy and relocatable link, don't copy the output ELF
6555 section type from input if the output BFD section flags have been
6556 set to something different. For a final link allow some flags
6557 that the linker clears to differ. */
6558 if (elf_section_type (osec) == SHT_NULL
6559 && (osec->flags == isec->flags
6561 && ((osec->flags ^ isec->flags)
6562 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6563 elf_section_type (osec) = elf_section_type (isec);
6565 /* FIXME: Is this correct for all OS/PROC specific flags? */
6566 elf_section_flags (osec) |= (elf_section_flags (isec)
6567 & (SHF_MASKOS | SHF_MASKPROC));
6569 /* Set things up for objcopy and relocatable link. The output
6570 SHT_GROUP section will have its elf_next_in_group pointing back
6571 to the input group members. Ignore linker created group section.
6572 See elfNN_ia64_object_p in elfxx-ia64.c. */
6575 if (elf_sec_group (isec) == NULL
6576 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6578 if (elf_section_flags (isec) & SHF_GROUP)
6579 elf_section_flags (osec) |= SHF_GROUP;
6580 elf_next_in_group (osec) = elf_next_in_group (isec);
6581 elf_section_data (osec)->group = elf_section_data (isec)->group;
6585 ihdr = &elf_section_data (isec)->this_hdr;
6587 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6588 don't use the output section of the linked-to section since it
6589 may be NULL at this point. */
6590 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6592 ohdr = &elf_section_data (osec)->this_hdr;
6593 ohdr->sh_flags |= SHF_LINK_ORDER;
6594 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6597 osec->use_rela_p = isec->use_rela_p;
6602 /* Copy private section information. This copies over the entsize
6603 field, and sometimes the info field. */
6606 _bfd_elf_copy_private_section_data (bfd *ibfd,
6611 Elf_Internal_Shdr *ihdr, *ohdr;
6613 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6614 || obfd->xvec->flavour != bfd_target_elf_flavour)
6617 ihdr = &elf_section_data (isec)->this_hdr;
6618 ohdr = &elf_section_data (osec)->this_hdr;
6620 ohdr->sh_entsize = ihdr->sh_entsize;
6622 if (ihdr->sh_type == SHT_SYMTAB
6623 || ihdr->sh_type == SHT_DYNSYM
6624 || ihdr->sh_type == SHT_GNU_verneed
6625 || ihdr->sh_type == SHT_GNU_verdef)
6626 ohdr->sh_info = ihdr->sh_info;
6628 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6632 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6633 necessary if we are removing either the SHT_GROUP section or any of
6634 the group member sections. DISCARDED is the value that a section's
6635 output_section has if the section will be discarded, NULL when this
6636 function is called from objcopy, bfd_abs_section_ptr when called
6640 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6644 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6645 if (elf_section_type (isec) == SHT_GROUP)
6647 asection *first = elf_next_in_group (isec);
6648 asection *s = first;
6649 bfd_size_type removed = 0;
6653 /* If this member section is being output but the
6654 SHT_GROUP section is not, then clear the group info
6655 set up by _bfd_elf_copy_private_section_data. */
6656 if (s->output_section != discarded
6657 && isec->output_section == discarded)
6659 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6660 elf_group_name (s->output_section) = NULL;
6662 /* Conversely, if the member section is not being output
6663 but the SHT_GROUP section is, then adjust its size. */
6664 else if (s->output_section == discarded
6665 && isec->output_section != discarded)
6667 s = elf_next_in_group (s);
6673 if (discarded != NULL)
6675 /* If we've been called for ld -r, then we need to
6676 adjust the input section size. This function may
6677 be called multiple times, so save the original
6679 if (isec->rawsize == 0)
6680 isec->rawsize = isec->size;
6681 isec->size = isec->rawsize - removed;
6685 /* Adjust the output section size when called from
6687 isec->output_section->size -= removed;
6695 /* Copy private header information. */
6698 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6700 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6701 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6704 /* Copy over private BFD data if it has not already been copied.
6705 This must be done here, rather than in the copy_private_bfd_data
6706 entry point, because the latter is called after the section
6707 contents have been set, which means that the program headers have
6708 already been worked out. */
6709 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
6711 if (! copy_private_bfd_data (ibfd, obfd))
6715 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6718 /* Copy private symbol information. If this symbol is in a section
6719 which we did not map into a BFD section, try to map the section
6720 index correctly. We use special macro definitions for the mapped
6721 section indices; these definitions are interpreted by the
6722 swap_out_syms function. */
6724 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6725 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6726 #define MAP_STRTAB (SHN_HIOS + 3)
6727 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6728 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6731 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6736 elf_symbol_type *isym, *osym;
6738 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6739 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6742 isym = elf_symbol_from (ibfd, isymarg);
6743 osym = elf_symbol_from (obfd, osymarg);
6746 && isym->internal_elf_sym.st_shndx != 0
6748 && bfd_is_abs_section (isym->symbol.section))
6752 shndx = isym->internal_elf_sym.st_shndx;
6753 if (shndx == elf_onesymtab (ibfd))
6754 shndx = MAP_ONESYMTAB;
6755 else if (shndx == elf_dynsymtab (ibfd))
6756 shndx = MAP_DYNSYMTAB;
6757 else if (shndx == elf_strtab_sec (ibfd))
6759 else if (shndx == elf_shstrtab_sec (ibfd))
6760 shndx = MAP_SHSTRTAB;
6761 else if (shndx == elf_symtab_shndx (ibfd))
6762 shndx = MAP_SYM_SHNDX;
6763 osym->internal_elf_sym.st_shndx = shndx;
6769 /* Swap out the symbols. */
6772 swap_out_syms (bfd *abfd,
6773 struct bfd_strtab_hash **sttp,
6776 const struct elf_backend_data *bed;
6779 struct bfd_strtab_hash *stt;
6780 Elf_Internal_Shdr *symtab_hdr;
6781 Elf_Internal_Shdr *symtab_shndx_hdr;
6782 Elf_Internal_Shdr *symstrtab_hdr;
6783 bfd_byte *outbound_syms;
6784 bfd_byte *outbound_shndx;
6786 unsigned int num_locals;
6788 bfd_boolean name_local_sections;
6790 if (!elf_map_symbols (abfd, &num_locals))
6793 /* Dump out the symtabs. */
6794 stt = _bfd_elf_stringtab_init ();
6798 bed = get_elf_backend_data (abfd);
6799 symcount = bfd_get_symcount (abfd);
6800 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6801 symtab_hdr->sh_type = SHT_SYMTAB;
6802 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6803 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6804 symtab_hdr->sh_info = num_locals + 1;
6805 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6807 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6808 symstrtab_hdr->sh_type = SHT_STRTAB;
6810 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6811 bed->s->sizeof_sym);
6812 if (outbound_syms == NULL)
6814 _bfd_stringtab_free (stt);
6817 symtab_hdr->contents = outbound_syms;
6819 outbound_shndx = NULL;
6820 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6821 if (symtab_shndx_hdr->sh_name != 0)
6823 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6824 outbound_shndx = (bfd_byte *)
6825 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6826 if (outbound_shndx == NULL)
6828 _bfd_stringtab_free (stt);
6832 symtab_shndx_hdr->contents = outbound_shndx;
6833 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6834 symtab_shndx_hdr->sh_size = amt;
6835 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6836 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6839 /* Now generate the data (for "contents"). */
6841 /* Fill in zeroth symbol and swap it out. */
6842 Elf_Internal_Sym sym;
6848 sym.st_shndx = SHN_UNDEF;
6849 sym.st_target_internal = 0;
6850 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6851 outbound_syms += bed->s->sizeof_sym;
6852 if (outbound_shndx != NULL)
6853 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6857 = (bed->elf_backend_name_local_section_symbols
6858 && bed->elf_backend_name_local_section_symbols (abfd));
6860 syms = bfd_get_outsymbols (abfd);
6861 for (idx = 0; idx < symcount; idx++)
6863 Elf_Internal_Sym sym;
6864 bfd_vma value = syms[idx]->value;
6865 elf_symbol_type *type_ptr;
6866 flagword flags = syms[idx]->flags;
6869 if (!name_local_sections
6870 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6872 /* Local section symbols have no name. */
6877 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6880 if (sym.st_name == (unsigned long) -1)
6882 _bfd_stringtab_free (stt);
6887 type_ptr = elf_symbol_from (abfd, syms[idx]);
6889 if ((flags & BSF_SECTION_SYM) == 0
6890 && bfd_is_com_section (syms[idx]->section))
6892 /* ELF common symbols put the alignment into the `value' field,
6893 and the size into the `size' field. This is backwards from
6894 how BFD handles it, so reverse it here. */
6895 sym.st_size = value;
6896 if (type_ptr == NULL
6897 || type_ptr->internal_elf_sym.st_value == 0)
6898 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6900 sym.st_value = type_ptr->internal_elf_sym.st_value;
6901 sym.st_shndx = _bfd_elf_section_from_bfd_section
6902 (abfd, syms[idx]->section);
6906 asection *sec = syms[idx]->section;
6909 if (sec->output_section)
6911 value += sec->output_offset;
6912 sec = sec->output_section;
6915 /* Don't add in the section vma for relocatable output. */
6916 if (! relocatable_p)
6918 sym.st_value = value;
6919 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6921 if (bfd_is_abs_section (sec)
6923 && type_ptr->internal_elf_sym.st_shndx != 0)
6925 /* This symbol is in a real ELF section which we did
6926 not create as a BFD section. Undo the mapping done
6927 by copy_private_symbol_data. */
6928 shndx = type_ptr->internal_elf_sym.st_shndx;
6932 shndx = elf_onesymtab (abfd);
6935 shndx = elf_dynsymtab (abfd);
6938 shndx = elf_strtab_sec (abfd);
6941 shndx = elf_shstrtab_sec (abfd);
6944 shndx = elf_symtab_shndx (abfd);
6953 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6955 if (shndx == SHN_BAD)
6959 /* Writing this would be a hell of a lot easier if
6960 we had some decent documentation on bfd, and
6961 knew what to expect of the library, and what to
6962 demand of applications. For example, it
6963 appears that `objcopy' might not set the
6964 section of a symbol to be a section that is
6965 actually in the output file. */
6966 sec2 = bfd_get_section_by_name (abfd, sec->name);
6969 _bfd_error_handler (_("\
6970 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6971 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6973 bfd_set_error (bfd_error_invalid_operation);
6974 _bfd_stringtab_free (stt);
6978 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6979 BFD_ASSERT (shndx != SHN_BAD);
6983 sym.st_shndx = shndx;
6986 if ((flags & BSF_THREAD_LOCAL) != 0)
6988 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6989 type = STT_GNU_IFUNC;
6990 else if ((flags & BSF_FUNCTION) != 0)
6992 else if ((flags & BSF_OBJECT) != 0)
6994 else if ((flags & BSF_RELC) != 0)
6996 else if ((flags & BSF_SRELC) != 0)
7001 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7004 /* Processor-specific types. */
7005 if (type_ptr != NULL
7006 && bed->elf_backend_get_symbol_type)
7007 type = ((*bed->elf_backend_get_symbol_type)
7008 (&type_ptr->internal_elf_sym, type));
7010 if (flags & BSF_SECTION_SYM)
7012 if (flags & BSF_GLOBAL)
7013 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7015 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7017 else if (bfd_is_com_section (syms[idx]->section))
7019 #ifdef USE_STT_COMMON
7020 if (type == STT_OBJECT)
7021 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
7024 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
7026 else if (bfd_is_und_section (syms[idx]->section))
7027 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7031 else if (flags & BSF_FILE)
7032 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7035 int bind = STB_LOCAL;
7037 if (flags & BSF_LOCAL)
7039 else if (flags & BSF_GNU_UNIQUE)
7040 bind = STB_GNU_UNIQUE;
7041 else if (flags & BSF_WEAK)
7043 else if (flags & BSF_GLOBAL)
7046 sym.st_info = ELF_ST_INFO (bind, type);
7049 if (type_ptr != NULL)
7051 sym.st_other = type_ptr->internal_elf_sym.st_other;
7052 sym.st_target_internal
7053 = type_ptr->internal_elf_sym.st_target_internal;
7058 sym.st_target_internal = 0;
7061 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
7062 outbound_syms += bed->s->sizeof_sym;
7063 if (outbound_shndx != NULL)
7064 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
7068 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
7069 symstrtab_hdr->sh_type = SHT_STRTAB;
7071 symstrtab_hdr->sh_flags = 0;
7072 symstrtab_hdr->sh_addr = 0;
7073 symstrtab_hdr->sh_entsize = 0;
7074 symstrtab_hdr->sh_link = 0;
7075 symstrtab_hdr->sh_info = 0;
7076 symstrtab_hdr->sh_addralign = 1;
7081 /* Return the number of bytes required to hold the symtab vector.
7083 Note that we base it on the count plus 1, since we will null terminate
7084 the vector allocated based on this size. However, the ELF symbol table
7085 always has a dummy entry as symbol #0, so it ends up even. */
7088 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
7092 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7094 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7095 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7097 symtab_size -= sizeof (asymbol *);
7103 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7107 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7109 if (elf_dynsymtab (abfd) == 0)
7111 bfd_set_error (bfd_error_invalid_operation);
7115 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7116 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7118 symtab_size -= sizeof (asymbol *);
7124 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7127 return (asect->reloc_count + 1) * sizeof (arelent *);
7130 /* Canonicalize the relocs. */
7133 _bfd_elf_canonicalize_reloc (bfd *abfd,
7140 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7142 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7145 tblptr = section->relocation;
7146 for (i = 0; i < section->reloc_count; i++)
7147 *relptr++ = tblptr++;
7151 return section->reloc_count;
7155 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7158 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7161 bfd_get_symcount (abfd) = symcount;
7166 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7167 asymbol **allocation)
7169 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7170 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7173 bfd_get_dynamic_symcount (abfd) = symcount;
7177 /* Return the size required for the dynamic reloc entries. Any loadable
7178 section that was actually installed in the BFD, and has type SHT_REL
7179 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7180 dynamic reloc section. */
7183 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7188 if (elf_dynsymtab (abfd) == 0)
7190 bfd_set_error (bfd_error_invalid_operation);
7194 ret = sizeof (arelent *);
7195 for (s = abfd->sections; s != NULL; s = s->next)
7196 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7197 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7198 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7199 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7200 * sizeof (arelent *));
7205 /* Canonicalize the dynamic relocation entries. Note that we return the
7206 dynamic relocations as a single block, although they are actually
7207 associated with particular sections; the interface, which was
7208 designed for SunOS style shared libraries, expects that there is only
7209 one set of dynamic relocs. Any loadable section that was actually
7210 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7211 dynamic symbol table, is considered to be a dynamic reloc section. */
7214 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7218 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7222 if (elf_dynsymtab (abfd) == 0)
7224 bfd_set_error (bfd_error_invalid_operation);
7228 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7230 for (s = abfd->sections; s != NULL; s = s->next)
7232 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7233 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7234 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7239 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7241 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7243 for (i = 0; i < count; i++)
7254 /* Read in the version information. */
7257 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7259 bfd_byte *contents = NULL;
7260 unsigned int freeidx = 0;
7262 if (elf_dynverref (abfd) != 0)
7264 Elf_Internal_Shdr *hdr;
7265 Elf_External_Verneed *everneed;
7266 Elf_Internal_Verneed *iverneed;
7268 bfd_byte *contents_end;
7270 hdr = &elf_tdata (abfd)->dynverref_hdr;
7273 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7274 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
7276 elf_tdata (abfd)->verref = NULL;
7278 if (elf_tdata (abfd)->verref == NULL)
7281 elf_tdata (abfd)->cverrefs = hdr->sh_info;
7283 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7284 if (contents == NULL)
7286 error_return_verref:
7287 elf_tdata (abfd)->verref = NULL;
7288 elf_tdata (abfd)->cverrefs = 0;
7291 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7292 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7293 goto error_return_verref;
7295 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
7296 goto error_return_verref;
7298 BFD_ASSERT (sizeof (Elf_External_Verneed)
7299 == sizeof (Elf_External_Vernaux));
7300 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7301 everneed = (Elf_External_Verneed *) contents;
7302 iverneed = elf_tdata (abfd)->verref;
7303 for (i = 0; i < hdr->sh_info; i++, iverneed++)
7305 Elf_External_Vernaux *evernaux;
7306 Elf_Internal_Vernaux *ivernaux;
7309 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7311 iverneed->vn_bfd = abfd;
7313 iverneed->vn_filename =
7314 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7316 if (iverneed->vn_filename == NULL)
7317 goto error_return_verref;
7319 if (iverneed->vn_cnt == 0)
7320 iverneed->vn_auxptr = NULL;
7323 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7324 bfd_alloc2 (abfd, iverneed->vn_cnt,
7325 sizeof (Elf_Internal_Vernaux));
7326 if (iverneed->vn_auxptr == NULL)
7327 goto error_return_verref;
7330 if (iverneed->vn_aux
7331 > (size_t) (contents_end - (bfd_byte *) everneed))
7332 goto error_return_verref;
7334 evernaux = ((Elf_External_Vernaux *)
7335 ((bfd_byte *) everneed + iverneed->vn_aux));
7336 ivernaux = iverneed->vn_auxptr;
7337 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7339 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7341 ivernaux->vna_nodename =
7342 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7343 ivernaux->vna_name);
7344 if (ivernaux->vna_nodename == NULL)
7345 goto error_return_verref;
7347 if (j + 1 < iverneed->vn_cnt)
7348 ivernaux->vna_nextptr = ivernaux + 1;
7350 ivernaux->vna_nextptr = NULL;
7352 if (ivernaux->vna_next
7353 > (size_t) (contents_end - (bfd_byte *) evernaux))
7354 goto error_return_verref;
7356 evernaux = ((Elf_External_Vernaux *)
7357 ((bfd_byte *) evernaux + ivernaux->vna_next));
7359 if (ivernaux->vna_other > freeidx)
7360 freeidx = ivernaux->vna_other;
7363 if (i + 1 < hdr->sh_info)
7364 iverneed->vn_nextref = iverneed + 1;
7366 iverneed->vn_nextref = NULL;
7368 if (iverneed->vn_next
7369 > (size_t) (contents_end - (bfd_byte *) everneed))
7370 goto error_return_verref;
7372 everneed = ((Elf_External_Verneed *)
7373 ((bfd_byte *) everneed + iverneed->vn_next));
7380 if (elf_dynverdef (abfd) != 0)
7382 Elf_Internal_Shdr *hdr;
7383 Elf_External_Verdef *everdef;
7384 Elf_Internal_Verdef *iverdef;
7385 Elf_Internal_Verdef *iverdefarr;
7386 Elf_Internal_Verdef iverdefmem;
7388 unsigned int maxidx;
7389 bfd_byte *contents_end_def, *contents_end_aux;
7391 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7393 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7394 if (contents == NULL)
7396 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7397 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7400 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
7403 BFD_ASSERT (sizeof (Elf_External_Verdef)
7404 >= sizeof (Elf_External_Verdaux));
7405 contents_end_def = contents + hdr->sh_size
7406 - sizeof (Elf_External_Verdef);
7407 contents_end_aux = contents + hdr->sh_size
7408 - sizeof (Elf_External_Verdaux);
7410 /* We know the number of entries in the section but not the maximum
7411 index. Therefore we have to run through all entries and find
7413 everdef = (Elf_External_Verdef *) contents;
7415 for (i = 0; i < hdr->sh_info; ++i)
7417 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7419 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7420 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7422 if (iverdefmem.vd_next
7423 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7426 everdef = ((Elf_External_Verdef *)
7427 ((bfd_byte *) everdef + iverdefmem.vd_next));
7430 if (default_imported_symver)
7432 if (freeidx > maxidx)
7438 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7439 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7441 elf_tdata (abfd)->verdef = NULL;
7443 if (elf_tdata (abfd)->verdef == NULL)
7446 elf_tdata (abfd)->cverdefs = maxidx;
7448 everdef = (Elf_External_Verdef *) contents;
7449 iverdefarr = elf_tdata (abfd)->verdef;
7450 for (i = 0; i < hdr->sh_info; i++)
7452 Elf_External_Verdaux *everdaux;
7453 Elf_Internal_Verdaux *iverdaux;
7456 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7458 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7460 error_return_verdef:
7461 elf_tdata (abfd)->verdef = NULL;
7462 elf_tdata (abfd)->cverdefs = 0;
7466 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7467 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7469 iverdef->vd_bfd = abfd;
7471 if (iverdef->vd_cnt == 0)
7472 iverdef->vd_auxptr = NULL;
7475 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7476 bfd_alloc2 (abfd, iverdef->vd_cnt,
7477 sizeof (Elf_Internal_Verdaux));
7478 if (iverdef->vd_auxptr == NULL)
7479 goto error_return_verdef;
7483 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7484 goto error_return_verdef;
7486 everdaux = ((Elf_External_Verdaux *)
7487 ((bfd_byte *) everdef + iverdef->vd_aux));
7488 iverdaux = iverdef->vd_auxptr;
7489 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7491 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7493 iverdaux->vda_nodename =
7494 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7495 iverdaux->vda_name);
7496 if (iverdaux->vda_nodename == NULL)
7497 goto error_return_verdef;
7499 if (j + 1 < iverdef->vd_cnt)
7500 iverdaux->vda_nextptr = iverdaux + 1;
7502 iverdaux->vda_nextptr = NULL;
7504 if (iverdaux->vda_next
7505 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7506 goto error_return_verdef;
7508 everdaux = ((Elf_External_Verdaux *)
7509 ((bfd_byte *) everdaux + iverdaux->vda_next));
7512 if (iverdef->vd_cnt)
7513 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7515 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7516 iverdef->vd_nextdef = iverdef + 1;
7518 iverdef->vd_nextdef = NULL;
7520 everdef = ((Elf_External_Verdef *)
7521 ((bfd_byte *) everdef + iverdef->vd_next));
7527 else if (default_imported_symver)
7534 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7535 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7536 if (elf_tdata (abfd)->verdef == NULL)
7539 elf_tdata (abfd)->cverdefs = freeidx;
7542 /* Create a default version based on the soname. */
7543 if (default_imported_symver)
7545 Elf_Internal_Verdef *iverdef;
7546 Elf_Internal_Verdaux *iverdaux;
7548 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
7550 iverdef->vd_version = VER_DEF_CURRENT;
7551 iverdef->vd_flags = 0;
7552 iverdef->vd_ndx = freeidx;
7553 iverdef->vd_cnt = 1;
7555 iverdef->vd_bfd = abfd;
7557 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7558 if (iverdef->vd_nodename == NULL)
7559 goto error_return_verdef;
7560 iverdef->vd_nextdef = NULL;
7561 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7562 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7563 if (iverdef->vd_auxptr == NULL)
7564 goto error_return_verdef;
7566 iverdaux = iverdef->vd_auxptr;
7567 iverdaux->vda_nodename = iverdef->vd_nodename;
7568 iverdaux->vda_nextptr = NULL;
7574 if (contents != NULL)
7580 _bfd_elf_make_empty_symbol (bfd *abfd)
7582 elf_symbol_type *newsym;
7584 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
7587 newsym->symbol.the_bfd = abfd;
7588 return &newsym->symbol;
7592 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7596 bfd_symbol_info (symbol, ret);
7599 /* Return whether a symbol name implies a local symbol. Most targets
7600 use this function for the is_local_label_name entry point, but some
7604 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7607 /* Normal local symbols start with ``.L''. */
7608 if (name[0] == '.' && name[1] == 'L')
7611 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7612 DWARF debugging symbols starting with ``..''. */
7613 if (name[0] == '.' && name[1] == '.')
7616 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7617 emitting DWARF debugging output. I suspect this is actually a
7618 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7619 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7620 underscore to be emitted on some ELF targets). For ease of use,
7621 we treat such symbols as local. */
7622 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7629 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7630 asymbol *symbol ATTRIBUTE_UNUSED)
7637 _bfd_elf_set_arch_mach (bfd *abfd,
7638 enum bfd_architecture arch,
7639 unsigned long machine)
7641 /* If this isn't the right architecture for this backend, and this
7642 isn't the generic backend, fail. */
7643 if (arch != get_elf_backend_data (abfd)->arch
7644 && arch != bfd_arch_unknown
7645 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7648 return bfd_default_set_arch_mach (abfd, arch, machine);
7651 /* Find the function to a particular section and offset,
7652 for error reporting. */
7655 elf_find_function (bfd *abfd,
7659 const char **filename_ptr,
7660 const char **functionname_ptr)
7662 struct elf_find_function_cache
7664 asection *last_section;
7666 const char *filename;
7667 bfd_size_type func_size;
7670 if (symbols == NULL)
7673 cache = elf_tdata (abfd)->elf_find_function_cache;
7676 cache = bfd_zalloc (abfd, sizeof (*cache));
7677 elf_tdata (abfd)->elf_find_function_cache = cache;
7681 if (cache->last_section != section
7682 || cache->func == NULL
7683 || offset < cache->func->value
7684 || offset >= cache->func->value + cache->func_size)
7689 /* ??? Given multiple file symbols, it is impossible to reliably
7690 choose the right file name for global symbols. File symbols are
7691 local symbols, and thus all file symbols must sort before any
7692 global symbols. The ELF spec may be interpreted to say that a
7693 file symbol must sort before other local symbols, but currently
7694 ld -r doesn't do this. So, for ld -r output, it is possible to
7695 make a better choice of file name for local symbols by ignoring
7696 file symbols appearing after a given local symbol. */
7697 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7698 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7702 state = nothing_seen;
7703 cache->filename = NULL;
7705 cache->func_size = 0;
7706 cache->last_section = section;
7708 for (p = symbols; *p != NULL; p++)
7714 if ((sym->flags & BSF_FILE) != 0)
7717 if (state == symbol_seen)
7718 state = file_after_symbol_seen;
7722 size = bed->maybe_function_sym (sym, section, &code_off);
7724 && code_off <= offset
7725 && (code_off > low_func
7726 || (code_off == low_func
7727 && size > cache->func_size)))
7730 cache->func_size = size;
7731 cache->filename = NULL;
7732 low_func = code_off;
7734 && ((sym->flags & BSF_LOCAL) != 0
7735 || state != file_after_symbol_seen))
7736 cache->filename = bfd_asymbol_name (file);
7738 if (state == nothing_seen)
7739 state = symbol_seen;
7743 if (cache->func == NULL)
7747 *filename_ptr = cache->filename;
7748 if (functionname_ptr)
7749 *functionname_ptr = bfd_asymbol_name (cache->func);
7754 /* Find the nearest line to a particular section and offset,
7755 for error reporting. */
7758 _bfd_elf_find_nearest_line (bfd *abfd,
7762 const char **filename_ptr,
7763 const char **functionname_ptr,
7764 unsigned int *line_ptr,
7765 unsigned int *discriminator_ptr)
7769 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7770 filename_ptr, functionname_ptr,
7771 line_ptr, discriminator_ptr,
7772 dwarf_debug_sections, 0,
7773 &elf_tdata (abfd)->dwarf2_find_line_info))
7775 if (!*functionname_ptr)
7776 elf_find_function (abfd, symbols, section, offset,
7777 *filename_ptr ? NULL : filename_ptr,
7783 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
7784 filename_ptr, functionname_ptr, line_ptr))
7786 if (!*functionname_ptr)
7787 elf_find_function (abfd, symbols, section, offset,
7788 *filename_ptr ? NULL : filename_ptr,
7794 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7795 &found, filename_ptr,
7796 functionname_ptr, line_ptr,
7797 &elf_tdata (abfd)->line_info))
7799 if (found && (*functionname_ptr || *line_ptr))
7802 if (symbols == NULL)
7805 if (! elf_find_function (abfd, symbols, section, offset,
7806 filename_ptr, functionname_ptr))
7813 /* Find the line for a symbol. */
7816 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7817 const char **filename_ptr, unsigned int *line_ptr)
7819 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
7820 filename_ptr, NULL, line_ptr, NULL,
7821 dwarf_debug_sections, 0,
7822 &elf_tdata (abfd)->dwarf2_find_line_info);
7825 /* After a call to bfd_find_nearest_line, successive calls to
7826 bfd_find_inliner_info can be used to get source information about
7827 each level of function inlining that terminated at the address
7828 passed to bfd_find_nearest_line. Currently this is only supported
7829 for DWARF2 with appropriate DWARF3 extensions. */
7832 _bfd_elf_find_inliner_info (bfd *abfd,
7833 const char **filename_ptr,
7834 const char **functionname_ptr,
7835 unsigned int *line_ptr)
7838 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7839 functionname_ptr, line_ptr,
7840 & elf_tdata (abfd)->dwarf2_find_line_info);
7845 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7847 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7848 int ret = bed->s->sizeof_ehdr;
7850 if (!info->relocatable)
7852 bfd_size_type phdr_size = elf_program_header_size (abfd);
7854 if (phdr_size == (bfd_size_type) -1)
7856 struct elf_segment_map *m;
7859 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
7860 phdr_size += bed->s->sizeof_phdr;
7863 phdr_size = get_program_header_size (abfd, info);
7866 elf_program_header_size (abfd) = phdr_size;
7874 _bfd_elf_set_section_contents (bfd *abfd,
7876 const void *location,
7878 bfd_size_type count)
7880 Elf_Internal_Shdr *hdr;
7883 if (! abfd->output_has_begun
7884 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7887 hdr = &elf_section_data (section)->this_hdr;
7888 pos = hdr->sh_offset + offset;
7889 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7890 || bfd_bwrite (location, count, abfd) != count)
7897 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7898 arelent *cache_ptr ATTRIBUTE_UNUSED,
7899 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7904 /* Try to convert a non-ELF reloc into an ELF one. */
7907 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7909 /* Check whether we really have an ELF howto. */
7911 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7913 bfd_reloc_code_real_type code;
7914 reloc_howto_type *howto;
7916 /* Alien reloc: Try to determine its type to replace it with an
7917 equivalent ELF reloc. */
7919 if (areloc->howto->pc_relative)
7921 switch (areloc->howto->bitsize)
7924 code = BFD_RELOC_8_PCREL;
7927 code = BFD_RELOC_12_PCREL;
7930 code = BFD_RELOC_16_PCREL;
7933 code = BFD_RELOC_24_PCREL;
7936 code = BFD_RELOC_32_PCREL;
7939 code = BFD_RELOC_64_PCREL;
7945 howto = bfd_reloc_type_lookup (abfd, code);
7947 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7949 if (howto->pcrel_offset)
7950 areloc->addend += areloc->address;
7952 areloc->addend -= areloc->address; /* addend is unsigned!! */
7957 switch (areloc->howto->bitsize)
7963 code = BFD_RELOC_14;
7966 code = BFD_RELOC_16;
7969 code = BFD_RELOC_26;
7972 code = BFD_RELOC_32;
7975 code = BFD_RELOC_64;
7981 howto = bfd_reloc_type_lookup (abfd, code);
7985 areloc->howto = howto;
7993 (*_bfd_error_handler)
7994 (_("%B: unsupported relocation type %s"),
7995 abfd, areloc->howto->name);
7996 bfd_set_error (bfd_error_bad_value);
8001 _bfd_elf_close_and_cleanup (bfd *abfd)
8003 struct elf_obj_tdata *tdata = elf_tdata (abfd);
8004 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
8006 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
8007 _bfd_elf_strtab_free (elf_shstrtab (abfd));
8008 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
8011 return _bfd_generic_close_and_cleanup (abfd);
8014 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8015 in the relocation's offset. Thus we cannot allow any sort of sanity
8016 range-checking to interfere. There is nothing else to do in processing
8019 bfd_reloc_status_type
8020 _bfd_elf_rel_vtable_reloc_fn
8021 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
8022 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
8023 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
8024 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
8026 return bfd_reloc_ok;
8029 /* Elf core file support. Much of this only works on native
8030 toolchains, since we rely on knowing the
8031 machine-dependent procfs structure in order to pick
8032 out details about the corefile. */
8034 #ifdef HAVE_SYS_PROCFS_H
8035 /* Needed for new procfs interface on sparc-solaris. */
8036 # define _STRUCTURED_PROC 1
8037 # include <sys/procfs.h>
8040 /* Return a PID that identifies a "thread" for threaded cores, or the
8041 PID of the main process for non-threaded cores. */
8044 elfcore_make_pid (bfd *abfd)
8048 pid = elf_tdata (abfd)->core->lwpid;
8050 pid = elf_tdata (abfd)->core->pid;
8055 /* If there isn't a section called NAME, make one, using
8056 data from SECT. Note, this function will generate a
8057 reference to NAME, so you shouldn't deallocate or
8061 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
8065 if (bfd_get_section_by_name (abfd, name) != NULL)
8068 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
8072 sect2->size = sect->size;
8073 sect2->filepos = sect->filepos;
8074 sect2->alignment_power = sect->alignment_power;
8078 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8079 actually creates up to two pseudosections:
8080 - For the single-threaded case, a section named NAME, unless
8081 such a section already exists.
8082 - For the multi-threaded case, a section named "NAME/PID", where
8083 PID is elfcore_make_pid (abfd).
8084 Both pseudosections have identical contents. */
8086 _bfd_elfcore_make_pseudosection (bfd *abfd,
8092 char *threaded_name;
8096 /* Build the section name. */
8098 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8099 len = strlen (buf) + 1;
8100 threaded_name = (char *) bfd_alloc (abfd, len);
8101 if (threaded_name == NULL)
8103 memcpy (threaded_name, buf, len);
8105 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8110 sect->filepos = filepos;
8111 sect->alignment_power = 2;
8113 return elfcore_maybe_make_sect (abfd, name, sect);
8116 /* prstatus_t exists on:
8118 linux 2.[01] + glibc
8122 #if defined (HAVE_PRSTATUS_T)
8125 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8130 if (note->descsz == sizeof (prstatus_t))
8134 size = sizeof (prstat.pr_reg);
8135 offset = offsetof (prstatus_t, pr_reg);
8136 memcpy (&prstat, note->descdata, sizeof (prstat));
8138 /* Do not overwrite the core signal if it
8139 has already been set by another thread. */
8140 if (elf_tdata (abfd)->core->signal == 0)
8141 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8142 if (elf_tdata (abfd)->core->pid == 0)
8143 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8145 /* pr_who exists on:
8148 pr_who doesn't exist on:
8151 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8152 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8154 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8157 #if defined (HAVE_PRSTATUS32_T)
8158 else if (note->descsz == sizeof (prstatus32_t))
8160 /* 64-bit host, 32-bit corefile */
8161 prstatus32_t prstat;
8163 size = sizeof (prstat.pr_reg);
8164 offset = offsetof (prstatus32_t, pr_reg);
8165 memcpy (&prstat, note->descdata, sizeof (prstat));
8167 /* Do not overwrite the core signal if it
8168 has already been set by another thread. */
8169 if (elf_tdata (abfd)->core->signal == 0)
8170 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8171 if (elf_tdata (abfd)->core->pid == 0)
8172 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8174 /* pr_who exists on:
8177 pr_who doesn't exist on:
8180 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8181 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8183 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8186 #endif /* HAVE_PRSTATUS32_T */
8189 /* Fail - we don't know how to handle any other
8190 note size (ie. data object type). */
8194 /* Make a ".reg/999" section and a ".reg" section. */
8195 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8196 size, note->descpos + offset);
8198 #endif /* defined (HAVE_PRSTATUS_T) */
8200 /* Create a pseudosection containing the exact contents of NOTE. */
8202 elfcore_make_note_pseudosection (bfd *abfd,
8204 Elf_Internal_Note *note)
8206 return _bfd_elfcore_make_pseudosection (abfd, name,
8207 note->descsz, note->descpos);
8210 /* There isn't a consistent prfpregset_t across platforms,
8211 but it doesn't matter, because we don't have to pick this
8212 data structure apart. */
8215 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8217 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8220 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8221 type of NT_PRXFPREG. Just include the whole note's contents
8225 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8227 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8230 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8231 with a note type of NT_X86_XSTATE. Just include the whole note's
8232 contents literally. */
8235 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8237 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8241 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8243 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8247 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8249 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8253 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8255 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8259 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8261 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8265 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8267 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8271 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8273 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8277 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8279 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8283 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8285 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8289 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8291 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8295 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8297 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8301 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8303 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8307 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8309 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8313 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8315 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8319 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8321 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8325 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8327 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8330 #if defined (HAVE_PRPSINFO_T)
8331 typedef prpsinfo_t elfcore_psinfo_t;
8332 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8333 typedef prpsinfo32_t elfcore_psinfo32_t;
8337 #if defined (HAVE_PSINFO_T)
8338 typedef psinfo_t elfcore_psinfo_t;
8339 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8340 typedef psinfo32_t elfcore_psinfo32_t;
8344 /* return a malloc'ed copy of a string at START which is at
8345 most MAX bytes long, possibly without a terminating '\0'.
8346 the copy will always have a terminating '\0'. */
8349 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8352 char *end = (char *) memchr (start, '\0', max);
8360 dups = (char *) bfd_alloc (abfd, len + 1);
8364 memcpy (dups, start, len);
8370 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8372 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8374 if (note->descsz == sizeof (elfcore_psinfo_t))
8376 elfcore_psinfo_t psinfo;
8378 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8380 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8381 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8383 elf_tdata (abfd)->core->program
8384 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8385 sizeof (psinfo.pr_fname));
8387 elf_tdata (abfd)->core->command
8388 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8389 sizeof (psinfo.pr_psargs));
8391 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8392 else if (note->descsz == sizeof (elfcore_psinfo32_t))
8394 /* 64-bit host, 32-bit corefile */
8395 elfcore_psinfo32_t psinfo;
8397 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8399 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8400 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8402 elf_tdata (abfd)->core->program
8403 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8404 sizeof (psinfo.pr_fname));
8406 elf_tdata (abfd)->core->command
8407 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8408 sizeof (psinfo.pr_psargs));
8414 /* Fail - we don't know how to handle any other
8415 note size (ie. data object type). */
8419 /* Note that for some reason, a spurious space is tacked
8420 onto the end of the args in some (at least one anyway)
8421 implementations, so strip it off if it exists. */
8424 char *command = elf_tdata (abfd)->core->command;
8425 int n = strlen (command);
8427 if (0 < n && command[n - 1] == ' ')
8428 command[n - 1] = '\0';
8433 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8435 #if defined (HAVE_PSTATUS_T)
8437 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8439 if (note->descsz == sizeof (pstatus_t)
8440 #if defined (HAVE_PXSTATUS_T)
8441 || note->descsz == sizeof (pxstatus_t)
8447 memcpy (&pstat, note->descdata, sizeof (pstat));
8449 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8451 #if defined (HAVE_PSTATUS32_T)
8452 else if (note->descsz == sizeof (pstatus32_t))
8454 /* 64-bit host, 32-bit corefile */
8457 memcpy (&pstat, note->descdata, sizeof (pstat));
8459 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8462 /* Could grab some more details from the "representative"
8463 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8464 NT_LWPSTATUS note, presumably. */
8468 #endif /* defined (HAVE_PSTATUS_T) */
8470 #if defined (HAVE_LWPSTATUS_T)
8472 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8474 lwpstatus_t lwpstat;
8480 if (note->descsz != sizeof (lwpstat)
8481 #if defined (HAVE_LWPXSTATUS_T)
8482 && note->descsz != sizeof (lwpxstatus_t)
8487 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8489 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8490 /* Do not overwrite the core signal if it has already been set by
8492 if (elf_tdata (abfd)->core->signal == 0)
8493 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8495 /* Make a ".reg/999" section. */
8497 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8498 len = strlen (buf) + 1;
8499 name = bfd_alloc (abfd, len);
8502 memcpy (name, buf, len);
8504 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8508 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8509 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8510 sect->filepos = note->descpos
8511 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8514 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8515 sect->size = sizeof (lwpstat.pr_reg);
8516 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8519 sect->alignment_power = 2;
8521 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8524 /* Make a ".reg2/999" section */
8526 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8527 len = strlen (buf) + 1;
8528 name = bfd_alloc (abfd, len);
8531 memcpy (name, buf, len);
8533 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8537 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8538 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8539 sect->filepos = note->descpos
8540 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8543 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8544 sect->size = sizeof (lwpstat.pr_fpreg);
8545 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8548 sect->alignment_power = 2;
8550 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8552 #endif /* defined (HAVE_LWPSTATUS_T) */
8555 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8562 int is_active_thread;
8565 if (note->descsz < 728)
8568 if (! CONST_STRNEQ (note->namedata, "win32"))
8571 type = bfd_get_32 (abfd, note->descdata);
8575 case 1 /* NOTE_INFO_PROCESS */:
8576 /* FIXME: need to add ->core->command. */
8577 /* process_info.pid */
8578 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
8579 /* process_info.signal */
8580 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
8583 case 2 /* NOTE_INFO_THREAD */:
8584 /* Make a ".reg/999" section. */
8585 /* thread_info.tid */
8586 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8588 len = strlen (buf) + 1;
8589 name = (char *) bfd_alloc (abfd, len);
8593 memcpy (name, buf, len);
8595 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8599 /* sizeof (thread_info.thread_context) */
8601 /* offsetof (thread_info.thread_context) */
8602 sect->filepos = note->descpos + 12;
8603 sect->alignment_power = 2;
8605 /* thread_info.is_active_thread */
8606 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8608 if (is_active_thread)
8609 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8613 case 3 /* NOTE_INFO_MODULE */:
8614 /* Make a ".module/xxxxxxxx" section. */
8615 /* module_info.base_address */
8616 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8617 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8619 len = strlen (buf) + 1;
8620 name = (char *) bfd_alloc (abfd, len);
8624 memcpy (name, buf, len);
8626 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8631 sect->size = note->descsz;
8632 sect->filepos = note->descpos;
8633 sect->alignment_power = 2;
8644 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8646 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8654 if (bed->elf_backend_grok_prstatus)
8655 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8657 #if defined (HAVE_PRSTATUS_T)
8658 return elfcore_grok_prstatus (abfd, note);
8663 #if defined (HAVE_PSTATUS_T)
8665 return elfcore_grok_pstatus (abfd, note);
8668 #if defined (HAVE_LWPSTATUS_T)
8670 return elfcore_grok_lwpstatus (abfd, note);
8673 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8674 return elfcore_grok_prfpreg (abfd, note);
8676 case NT_WIN32PSTATUS:
8677 return elfcore_grok_win32pstatus (abfd, note);
8679 case NT_PRXFPREG: /* Linux SSE extension */
8680 if (note->namesz == 6
8681 && strcmp (note->namedata, "LINUX") == 0)
8682 return elfcore_grok_prxfpreg (abfd, note);
8686 case NT_X86_XSTATE: /* Linux XSAVE extension */
8687 if (note->namesz == 6
8688 && strcmp (note->namedata, "LINUX") == 0)
8689 return elfcore_grok_xstatereg (abfd, note);
8694 if (note->namesz == 6
8695 && strcmp (note->namedata, "LINUX") == 0)
8696 return elfcore_grok_ppc_vmx (abfd, note);
8701 if (note->namesz == 6
8702 && strcmp (note->namedata, "LINUX") == 0)
8703 return elfcore_grok_ppc_vsx (abfd, note);
8707 case NT_S390_HIGH_GPRS:
8708 if (note->namesz == 6
8709 && strcmp (note->namedata, "LINUX") == 0)
8710 return elfcore_grok_s390_high_gprs (abfd, note);
8715 if (note->namesz == 6
8716 && strcmp (note->namedata, "LINUX") == 0)
8717 return elfcore_grok_s390_timer (abfd, note);
8721 case NT_S390_TODCMP:
8722 if (note->namesz == 6
8723 && strcmp (note->namedata, "LINUX") == 0)
8724 return elfcore_grok_s390_todcmp (abfd, note);
8728 case NT_S390_TODPREG:
8729 if (note->namesz == 6
8730 && strcmp (note->namedata, "LINUX") == 0)
8731 return elfcore_grok_s390_todpreg (abfd, note);
8736 if (note->namesz == 6
8737 && strcmp (note->namedata, "LINUX") == 0)
8738 return elfcore_grok_s390_ctrs (abfd, note);
8742 case NT_S390_PREFIX:
8743 if (note->namesz == 6
8744 && strcmp (note->namedata, "LINUX") == 0)
8745 return elfcore_grok_s390_prefix (abfd, note);
8749 case NT_S390_LAST_BREAK:
8750 if (note->namesz == 6
8751 && strcmp (note->namedata, "LINUX") == 0)
8752 return elfcore_grok_s390_last_break (abfd, note);
8756 case NT_S390_SYSTEM_CALL:
8757 if (note->namesz == 6
8758 && strcmp (note->namedata, "LINUX") == 0)
8759 return elfcore_grok_s390_system_call (abfd, note);
8764 if (note->namesz == 6
8765 && strcmp (note->namedata, "LINUX") == 0)
8766 return elfcore_grok_s390_tdb (abfd, note);
8771 if (note->namesz == 6
8772 && strcmp (note->namedata, "LINUX") == 0)
8773 return elfcore_grok_arm_vfp (abfd, note);
8778 if (note->namesz == 6
8779 && strcmp (note->namedata, "LINUX") == 0)
8780 return elfcore_grok_aarch_tls (abfd, note);
8784 case NT_ARM_HW_BREAK:
8785 if (note->namesz == 6
8786 && strcmp (note->namedata, "LINUX") == 0)
8787 return elfcore_grok_aarch_hw_break (abfd, note);
8791 case NT_ARM_HW_WATCH:
8792 if (note->namesz == 6
8793 && strcmp (note->namedata, "LINUX") == 0)
8794 return elfcore_grok_aarch_hw_watch (abfd, note);
8800 if (bed->elf_backend_grok_psinfo)
8801 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8803 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8804 return elfcore_grok_psinfo (abfd, note);
8811 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8816 sect->size = note->descsz;
8817 sect->filepos = note->descpos;
8818 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8824 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
8828 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
8834 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8836 struct elf_obj_tdata *t;
8838 if (note->descsz == 0)
8841 t = elf_tdata (abfd);
8842 t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
8843 if (t->build_id == NULL)
8846 t->build_id->size = note->descsz;
8847 memcpy (t->build_id->data, note->descdata, note->descsz);
8853 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8860 case NT_GNU_BUILD_ID:
8861 return elfobj_grok_gnu_build_id (abfd, note);
8866 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
8868 struct sdt_note *cur =
8869 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
8872 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
8873 cur->size = (bfd_size_type) note->descsz;
8874 memcpy (cur->data, note->descdata, note->descsz);
8876 elf_tdata (abfd)->sdt_note_head = cur;
8882 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
8887 return elfobj_grok_stapsdt_note_1 (abfd, note);
8895 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8899 cp = strchr (note->namedata, '@');
8902 *lwpidp = atoi(cp + 1);
8909 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8911 /* Signal number at offset 0x08. */
8912 elf_tdata (abfd)->core->signal
8913 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8915 /* Process ID at offset 0x50. */
8916 elf_tdata (abfd)->core->pid
8917 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8919 /* Command name at 0x7c (max 32 bytes, including nul). */
8920 elf_tdata (abfd)->core->command
8921 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8923 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8928 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8932 if (elfcore_netbsd_get_lwpid (note, &lwp))
8933 elf_tdata (abfd)->core->lwpid = lwp;
8935 if (note->type == NT_NETBSDCORE_PROCINFO)
8937 /* NetBSD-specific core "procinfo". Note that we expect to
8938 find this note before any of the others, which is fine,
8939 since the kernel writes this note out first when it
8940 creates a core file. */
8942 return elfcore_grok_netbsd_procinfo (abfd, note);
8945 /* As of Jan 2002 there are no other machine-independent notes
8946 defined for NetBSD core files. If the note type is less
8947 than the start of the machine-dependent note types, we don't
8950 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8954 switch (bfd_get_arch (abfd))
8956 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8957 PT_GETFPREGS == mach+2. */
8959 case bfd_arch_alpha:
8960 case bfd_arch_sparc:
8963 case NT_NETBSDCORE_FIRSTMACH+0:
8964 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8966 case NT_NETBSDCORE_FIRSTMACH+2:
8967 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8973 /* On all other arch's, PT_GETREGS == mach+1 and
8974 PT_GETFPREGS == mach+3. */
8979 case NT_NETBSDCORE_FIRSTMACH+1:
8980 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8982 case NT_NETBSDCORE_FIRSTMACH+3:
8983 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8993 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8995 /* Signal number at offset 0x08. */
8996 elf_tdata (abfd)->core->signal
8997 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8999 /* Process ID at offset 0x20. */
9000 elf_tdata (abfd)->core->pid
9001 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
9003 /* Command name at 0x48 (max 32 bytes, including nul). */
9004 elf_tdata (abfd)->core->command
9005 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
9011 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
9013 if (note->type == NT_OPENBSD_PROCINFO)
9014 return elfcore_grok_openbsd_procinfo (abfd, note);
9016 if (note->type == NT_OPENBSD_REGS)
9017 return elfcore_make_note_pseudosection (abfd, ".reg", note);
9019 if (note->type == NT_OPENBSD_FPREGS)
9020 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9022 if (note->type == NT_OPENBSD_XFPREGS)
9023 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9025 if (note->type == NT_OPENBSD_AUXV)
9027 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9032 sect->size = note->descsz;
9033 sect->filepos = note->descpos;
9034 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9039 if (note->type == NT_OPENBSD_WCOOKIE)
9041 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
9046 sect->size = note->descsz;
9047 sect->filepos = note->descpos;
9048 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9057 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
9059 void *ddata = note->descdata;
9066 /* nto_procfs_status 'pid' field is at offset 0. */
9067 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
9069 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9070 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
9072 /* nto_procfs_status 'flags' field is at offset 8. */
9073 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
9075 /* nto_procfs_status 'what' field is at offset 14. */
9076 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9078 elf_tdata (abfd)->core->signal = sig;
9079 elf_tdata (abfd)->core->lwpid = *tid;
9082 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9083 do not come from signals so we make sure we set the current
9084 thread just in case. */
9085 if (flags & 0x00000080)
9086 elf_tdata (abfd)->core->lwpid = *tid;
9088 /* Make a ".qnx_core_status/%d" section. */
9089 sprintf (buf, ".qnx_core_status/%ld", *tid);
9091 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9096 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9100 sect->size = note->descsz;
9101 sect->filepos = note->descpos;
9102 sect->alignment_power = 2;
9104 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9108 elfcore_grok_nto_regs (bfd *abfd,
9109 Elf_Internal_Note *note,
9117 /* Make a "(base)/%d" section. */
9118 sprintf (buf, "%s/%ld", base, tid);
9120 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9125 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9129 sect->size = note->descsz;
9130 sect->filepos = note->descpos;
9131 sect->alignment_power = 2;
9133 /* This is the current thread. */
9134 if (elf_tdata (abfd)->core->lwpid == tid)
9135 return elfcore_maybe_make_sect (abfd, base, sect);
9140 #define BFD_QNT_CORE_INFO 7
9141 #define BFD_QNT_CORE_STATUS 8
9142 #define BFD_QNT_CORE_GREG 9
9143 #define BFD_QNT_CORE_FPREG 10
9146 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9148 /* Every GREG section has a STATUS section before it. Store the
9149 tid from the previous call to pass down to the next gregs
9151 static long tid = 1;
9155 case BFD_QNT_CORE_INFO:
9156 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9157 case BFD_QNT_CORE_STATUS:
9158 return elfcore_grok_nto_status (abfd, note, &tid);
9159 case BFD_QNT_CORE_GREG:
9160 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9161 case BFD_QNT_CORE_FPREG:
9162 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9169 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9175 /* Use note name as section name. */
9177 name = (char *) bfd_alloc (abfd, len);
9180 memcpy (name, note->namedata, len);
9181 name[len - 1] = '\0';
9183 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9187 sect->size = note->descsz;
9188 sect->filepos = note->descpos;
9189 sect->alignment_power = 1;
9194 /* Function: elfcore_write_note
9197 buffer to hold note, and current size of buffer
9201 size of data for note
9203 Writes note to end of buffer. ELF64 notes are written exactly as
9204 for ELF32, despite the current (as of 2006) ELF gabi specifying
9205 that they ought to have 8-byte namesz and descsz field, and have
9206 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9209 Pointer to realloc'd buffer, *BUFSIZ updated. */
9212 elfcore_write_note (bfd *abfd,
9220 Elf_External_Note *xnp;
9227 namesz = strlen (name) + 1;
9229 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9231 buf = (char *) realloc (buf, *bufsiz + newspace);
9234 dest = buf + *bufsiz;
9235 *bufsiz += newspace;
9236 xnp = (Elf_External_Note *) dest;
9237 H_PUT_32 (abfd, namesz, xnp->namesz);
9238 H_PUT_32 (abfd, size, xnp->descsz);
9239 H_PUT_32 (abfd, type, xnp->type);
9243 memcpy (dest, name, namesz);
9251 memcpy (dest, input, size);
9262 elfcore_write_prpsinfo (bfd *abfd,
9268 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9270 if (bed->elf_backend_write_core_note != NULL)
9273 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9274 NT_PRPSINFO, fname, psargs);
9279 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9280 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9281 if (bed->s->elfclass == ELFCLASS32)
9283 #if defined (HAVE_PSINFO32_T)
9285 int note_type = NT_PSINFO;
9288 int note_type = NT_PRPSINFO;
9291 memset (&data, 0, sizeof (data));
9292 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9293 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9294 return elfcore_write_note (abfd, buf, bufsiz,
9295 "CORE", note_type, &data, sizeof (data));
9300 #if defined (HAVE_PSINFO_T)
9302 int note_type = NT_PSINFO;
9305 int note_type = NT_PRPSINFO;
9308 memset (&data, 0, sizeof (data));
9309 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9310 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9311 return elfcore_write_note (abfd, buf, bufsiz,
9312 "CORE", note_type, &data, sizeof (data));
9314 #endif /* PSINFO_T or PRPSINFO_T */
9321 elfcore_write_linux_prpsinfo32
9322 (bfd *abfd, char *buf, int *bufsiz,
9323 const struct elf_internal_linux_prpsinfo *prpsinfo)
9325 struct elf_external_linux_prpsinfo32 data;
9327 memset (&data, 0, sizeof (data));
9328 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9330 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9331 &data, sizeof (data));
9335 elfcore_write_linux_prpsinfo64
9336 (bfd *abfd, char *buf, int *bufsiz,
9337 const struct elf_internal_linux_prpsinfo *prpsinfo)
9339 struct elf_external_linux_prpsinfo64 data;
9341 memset (&data, 0, sizeof (data));
9342 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9344 return elfcore_write_note (abfd, buf, bufsiz,
9345 "CORE", NT_PRPSINFO, &data, sizeof (data));
9349 elfcore_write_prstatus (bfd *abfd,
9356 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9358 if (bed->elf_backend_write_core_note != NULL)
9361 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9363 pid, cursig, gregs);
9368 #if defined (HAVE_PRSTATUS_T)
9369 #if defined (HAVE_PRSTATUS32_T)
9370 if (bed->s->elfclass == ELFCLASS32)
9372 prstatus32_t prstat;
9374 memset (&prstat, 0, sizeof (prstat));
9375 prstat.pr_pid = pid;
9376 prstat.pr_cursig = cursig;
9377 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9378 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9379 NT_PRSTATUS, &prstat, sizeof (prstat));
9386 memset (&prstat, 0, sizeof (prstat));
9387 prstat.pr_pid = pid;
9388 prstat.pr_cursig = cursig;
9389 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9390 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9391 NT_PRSTATUS, &prstat, sizeof (prstat));
9393 #endif /* HAVE_PRSTATUS_T */
9399 #if defined (HAVE_LWPSTATUS_T)
9401 elfcore_write_lwpstatus (bfd *abfd,
9408 lwpstatus_t lwpstat;
9409 const char *note_name = "CORE";
9411 memset (&lwpstat, 0, sizeof (lwpstat));
9412 lwpstat.pr_lwpid = pid >> 16;
9413 lwpstat.pr_cursig = cursig;
9414 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9415 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9416 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9418 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9419 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9421 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9422 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9425 return elfcore_write_note (abfd, buf, bufsiz, note_name,
9426 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9428 #endif /* HAVE_LWPSTATUS_T */
9430 #if defined (HAVE_PSTATUS_T)
9432 elfcore_write_pstatus (bfd *abfd,
9436 int cursig ATTRIBUTE_UNUSED,
9437 const void *gregs ATTRIBUTE_UNUSED)
9439 const char *note_name = "CORE";
9440 #if defined (HAVE_PSTATUS32_T)
9441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9443 if (bed->s->elfclass == ELFCLASS32)
9447 memset (&pstat, 0, sizeof (pstat));
9448 pstat.pr_pid = pid & 0xffff;
9449 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9450 NT_PSTATUS, &pstat, sizeof (pstat));
9458 memset (&pstat, 0, sizeof (pstat));
9459 pstat.pr_pid = pid & 0xffff;
9460 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9461 NT_PSTATUS, &pstat, sizeof (pstat));
9465 #endif /* HAVE_PSTATUS_T */
9468 elfcore_write_prfpreg (bfd *abfd,
9474 const char *note_name = "CORE";
9475 return elfcore_write_note (abfd, buf, bufsiz,
9476 note_name, NT_FPREGSET, fpregs, size);
9480 elfcore_write_prxfpreg (bfd *abfd,
9483 const void *xfpregs,
9486 char *note_name = "LINUX";
9487 return elfcore_write_note (abfd, buf, bufsiz,
9488 note_name, NT_PRXFPREG, xfpregs, size);
9492 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9493 const void *xfpregs, int size)
9495 char *note_name = "LINUX";
9496 return elfcore_write_note (abfd, buf, bufsiz,
9497 note_name, NT_X86_XSTATE, xfpregs, size);
9501 elfcore_write_ppc_vmx (bfd *abfd,
9504 const void *ppc_vmx,
9507 char *note_name = "LINUX";
9508 return elfcore_write_note (abfd, buf, bufsiz,
9509 note_name, NT_PPC_VMX, ppc_vmx, size);
9513 elfcore_write_ppc_vsx (bfd *abfd,
9516 const void *ppc_vsx,
9519 char *note_name = "LINUX";
9520 return elfcore_write_note (abfd, buf, bufsiz,
9521 note_name, NT_PPC_VSX, ppc_vsx, size);
9525 elfcore_write_s390_high_gprs (bfd *abfd,
9528 const void *s390_high_gprs,
9531 char *note_name = "LINUX";
9532 return elfcore_write_note (abfd, buf, bufsiz,
9533 note_name, NT_S390_HIGH_GPRS,
9534 s390_high_gprs, size);
9538 elfcore_write_s390_timer (bfd *abfd,
9541 const void *s390_timer,
9544 char *note_name = "LINUX";
9545 return elfcore_write_note (abfd, buf, bufsiz,
9546 note_name, NT_S390_TIMER, s390_timer, size);
9550 elfcore_write_s390_todcmp (bfd *abfd,
9553 const void *s390_todcmp,
9556 char *note_name = "LINUX";
9557 return elfcore_write_note (abfd, buf, bufsiz,
9558 note_name, NT_S390_TODCMP, s390_todcmp, size);
9562 elfcore_write_s390_todpreg (bfd *abfd,
9565 const void *s390_todpreg,
9568 char *note_name = "LINUX";
9569 return elfcore_write_note (abfd, buf, bufsiz,
9570 note_name, NT_S390_TODPREG, s390_todpreg, size);
9574 elfcore_write_s390_ctrs (bfd *abfd,
9577 const void *s390_ctrs,
9580 char *note_name = "LINUX";
9581 return elfcore_write_note (abfd, buf, bufsiz,
9582 note_name, NT_S390_CTRS, s390_ctrs, size);
9586 elfcore_write_s390_prefix (bfd *abfd,
9589 const void *s390_prefix,
9592 char *note_name = "LINUX";
9593 return elfcore_write_note (abfd, buf, bufsiz,
9594 note_name, NT_S390_PREFIX, s390_prefix, size);
9598 elfcore_write_s390_last_break (bfd *abfd,
9601 const void *s390_last_break,
9604 char *note_name = "LINUX";
9605 return elfcore_write_note (abfd, buf, bufsiz,
9606 note_name, NT_S390_LAST_BREAK,
9607 s390_last_break, size);
9611 elfcore_write_s390_system_call (bfd *abfd,
9614 const void *s390_system_call,
9617 char *note_name = "LINUX";
9618 return elfcore_write_note (abfd, buf, bufsiz,
9619 note_name, NT_S390_SYSTEM_CALL,
9620 s390_system_call, size);
9624 elfcore_write_s390_tdb (bfd *abfd,
9627 const void *s390_tdb,
9630 char *note_name = "LINUX";
9631 return elfcore_write_note (abfd, buf, bufsiz,
9632 note_name, NT_S390_TDB, s390_tdb, size);
9636 elfcore_write_arm_vfp (bfd *abfd,
9639 const void *arm_vfp,
9642 char *note_name = "LINUX";
9643 return elfcore_write_note (abfd, buf, bufsiz,
9644 note_name, NT_ARM_VFP, arm_vfp, size);
9648 elfcore_write_aarch_tls (bfd *abfd,
9651 const void *aarch_tls,
9654 char *note_name = "LINUX";
9655 return elfcore_write_note (abfd, buf, bufsiz,
9656 note_name, NT_ARM_TLS, aarch_tls, size);
9660 elfcore_write_aarch_hw_break (bfd *abfd,
9663 const void *aarch_hw_break,
9666 char *note_name = "LINUX";
9667 return elfcore_write_note (abfd, buf, bufsiz,
9668 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
9672 elfcore_write_aarch_hw_watch (bfd *abfd,
9675 const void *aarch_hw_watch,
9678 char *note_name = "LINUX";
9679 return elfcore_write_note (abfd, buf, bufsiz,
9680 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
9684 elfcore_write_register_note (bfd *abfd,
9687 const char *section,
9691 if (strcmp (section, ".reg2") == 0)
9692 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9693 if (strcmp (section, ".reg-xfp") == 0)
9694 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9695 if (strcmp (section, ".reg-xstate") == 0)
9696 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9697 if (strcmp (section, ".reg-ppc-vmx") == 0)
9698 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9699 if (strcmp (section, ".reg-ppc-vsx") == 0)
9700 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9701 if (strcmp (section, ".reg-s390-high-gprs") == 0)
9702 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9703 if (strcmp (section, ".reg-s390-timer") == 0)
9704 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9705 if (strcmp (section, ".reg-s390-todcmp") == 0)
9706 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9707 if (strcmp (section, ".reg-s390-todpreg") == 0)
9708 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9709 if (strcmp (section, ".reg-s390-ctrs") == 0)
9710 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9711 if (strcmp (section, ".reg-s390-prefix") == 0)
9712 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9713 if (strcmp (section, ".reg-s390-last-break") == 0)
9714 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
9715 if (strcmp (section, ".reg-s390-system-call") == 0)
9716 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
9717 if (strcmp (section, ".reg-s390-tdb") == 0)
9718 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
9719 if (strcmp (section, ".reg-arm-vfp") == 0)
9720 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
9721 if (strcmp (section, ".reg-aarch-tls") == 0)
9722 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
9723 if (strcmp (section, ".reg-aarch-hw-break") == 0)
9724 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
9725 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
9726 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
9731 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9736 while (p < buf + size)
9738 /* FIXME: bad alignment assumption. */
9739 Elf_External_Note *xnp = (Elf_External_Note *) p;
9740 Elf_Internal_Note in;
9742 if (offsetof (Elf_External_Note, name) > buf - p + size)
9745 in.type = H_GET_32 (abfd, xnp->type);
9747 in.namesz = H_GET_32 (abfd, xnp->namesz);
9748 in.namedata = xnp->name;
9749 if (in.namesz > buf - in.namedata + size)
9752 in.descsz = H_GET_32 (abfd, xnp->descsz);
9753 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9754 in.descpos = offset + (in.descdata - buf);
9756 && (in.descdata >= buf + size
9757 || in.descsz > buf - in.descdata + size))
9760 switch (bfd_get_format (abfd))
9766 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
9768 if (! elfcore_grok_netbsd_note (abfd, &in))
9771 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
9773 if (! elfcore_grok_openbsd_note (abfd, &in))
9776 else if (CONST_STRNEQ (in.namedata, "QNX"))
9778 if (! elfcore_grok_nto_note (abfd, &in))
9781 else if (CONST_STRNEQ (in.namedata, "SPU/"))
9783 if (! elfcore_grok_spu_note (abfd, &in))
9788 if (! elfcore_grok_note (abfd, &in))
9794 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9796 if (! elfobj_grok_gnu_note (abfd, &in))
9799 else if (in.namesz == sizeof "stapsdt"
9800 && strcmp (in.namedata, "stapsdt") == 0)
9802 if (! elfobj_grok_stapsdt_note (abfd, &in))
9808 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9815 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9822 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9825 buf = (char *) bfd_malloc (size);
9829 if (bfd_bread (buf, size, abfd) != size
9830 || !elf_parse_notes (abfd, buf, size, offset))
9840 /* Providing external access to the ELF program header table. */
9842 /* Return an upper bound on the number of bytes required to store a
9843 copy of ABFD's program header table entries. Return -1 if an error
9844 occurs; bfd_get_error will return an appropriate code. */
9847 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9849 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9851 bfd_set_error (bfd_error_wrong_format);
9855 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9858 /* Copy ABFD's program header table entries to *PHDRS. The entries
9859 will be stored as an array of Elf_Internal_Phdr structures, as
9860 defined in include/elf/internal.h. To find out how large the
9861 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9863 Return the number of program header table entries read, or -1 if an
9864 error occurs; bfd_get_error will return an appropriate code. */
9867 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9871 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9873 bfd_set_error (bfd_error_wrong_format);
9877 num_phdrs = elf_elfheader (abfd)->e_phnum;
9878 memcpy (phdrs, elf_tdata (abfd)->phdr,
9879 num_phdrs * sizeof (Elf_Internal_Phdr));
9884 enum elf_reloc_type_class
9885 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
9886 const asection *rel_sec ATTRIBUTE_UNUSED,
9887 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9889 return reloc_class_normal;
9892 /* For RELA architectures, return the relocation value for a
9893 relocation against a local symbol. */
9896 _bfd_elf_rela_local_sym (bfd *abfd,
9897 Elf_Internal_Sym *sym,
9899 Elf_Internal_Rela *rel)
9901 asection *sec = *psec;
9904 relocation = (sec->output_section->vma
9905 + sec->output_offset
9907 if ((sec->flags & SEC_MERGE)
9908 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9909 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
9912 _bfd_merged_section_offset (abfd, psec,
9913 elf_section_data (sec)->sec_info,
9914 sym->st_value + rel->r_addend);
9917 /* If we have changed the section, and our original section is
9918 marked with SEC_EXCLUDE, it means that the original
9919 SEC_MERGE section has been completely subsumed in some
9920 other SEC_MERGE section. In this case, we need to leave
9921 some info around for --emit-relocs. */
9922 if ((sec->flags & SEC_EXCLUDE) != 0)
9923 sec->kept_section = *psec;
9926 rel->r_addend -= relocation;
9927 rel->r_addend += sec->output_section->vma + sec->output_offset;
9933 _bfd_elf_rel_local_sym (bfd *abfd,
9934 Elf_Internal_Sym *sym,
9938 asection *sec = *psec;
9940 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
9941 return sym->st_value + addend;
9943 return _bfd_merged_section_offset (abfd, psec,
9944 elf_section_data (sec)->sec_info,
9945 sym->st_value + addend);
9949 _bfd_elf_section_offset (bfd *abfd,
9950 struct bfd_link_info *info,
9954 switch (sec->sec_info_type)
9956 case SEC_INFO_TYPE_STABS:
9957 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9959 case SEC_INFO_TYPE_EH_FRAME:
9960 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9962 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
9964 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9965 bfd_size_type address_size = bed->s->arch_size / 8;
9966 offset = sec->size - offset - address_size;
9972 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9973 reconstruct an ELF file by reading the segments out of remote memory
9974 based on the ELF file header at EHDR_VMA and the ELF program headers it
9975 points to. If not null, *LOADBASEP is filled in with the difference
9976 between the VMAs from which the segments were read, and the VMAs the
9977 file headers (and hence BFD's idea of each section's VMA) put them at.
9979 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9980 remote memory at target address VMA into the local buffer at MYADDR; it
9981 should return zero on success or an `errno' code on failure. TEMPL must
9982 be a BFD for an ELF target with the word size and byte order found in
9983 the remote memory. */
9986 bfd_elf_bfd_from_remote_memory
9991 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
9993 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9994 (templ, ehdr_vma, size, loadbasep, target_read_memory);
9998 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9999 long symcount ATTRIBUTE_UNUSED,
10000 asymbol **syms ATTRIBUTE_UNUSED,
10005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10008 const char *relplt_name;
10009 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
10013 Elf_Internal_Shdr *hdr;
10019 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
10022 if (dynsymcount <= 0)
10025 if (!bed->plt_sym_val)
10028 relplt_name = bed->relplt_name;
10029 if (relplt_name == NULL)
10030 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
10031 relplt = bfd_get_section_by_name (abfd, relplt_name);
10032 if (relplt == NULL)
10035 hdr = &elf_section_data (relplt)->this_hdr;
10036 if (hdr->sh_link != elf_dynsymtab (abfd)
10037 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
10040 plt = bfd_get_section_by_name (abfd, ".plt");
10044 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
10045 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
10048 count = relplt->size / hdr->sh_entsize;
10049 size = count * sizeof (asymbol);
10050 p = relplt->relocation;
10051 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10053 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
10054 if (p->addend != 0)
10057 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
10059 size += sizeof ("+0x") - 1 + 8;
10064 s = *ret = (asymbol *) bfd_malloc (size);
10068 names = (char *) (s + count);
10069 p = relplt->relocation;
10071 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10076 addr = bed->plt_sym_val (i, plt, p);
10077 if (addr == (bfd_vma) -1)
10080 *s = **p->sym_ptr_ptr;
10081 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10082 we are defining a symbol, ensure one of them is set. */
10083 if ((s->flags & BSF_LOCAL) == 0)
10084 s->flags |= BSF_GLOBAL;
10085 s->flags |= BSF_SYNTHETIC;
10087 s->value = addr - plt->vma;
10090 len = strlen ((*p->sym_ptr_ptr)->name);
10091 memcpy (names, (*p->sym_ptr_ptr)->name, len);
10093 if (p->addend != 0)
10097 memcpy (names, "+0x", sizeof ("+0x") - 1);
10098 names += sizeof ("+0x") - 1;
10099 bfd_sprintf_vma (abfd, buf, p->addend);
10100 for (a = buf; *a == '0'; ++a)
10103 memcpy (names, a, len);
10106 memcpy (names, "@plt", sizeof ("@plt"));
10107 names += sizeof ("@plt");
10114 /* It is only used by x86-64 so far. */
10115 asection _bfd_elf_large_com_section
10116 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10117 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10120 _bfd_elf_post_process_headers (bfd * abfd,
10121 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10123 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
10125 i_ehdrp = elf_elfheader (abfd);
10127 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10129 /* To make things simpler for the loader on Linux systems we set the
10130 osabi field to ELFOSABI_GNU if the binary contains symbols of
10131 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10132 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10133 && elf_tdata (abfd)->has_gnu_symbols)
10134 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10138 /* Return TRUE for ELF symbol types that represent functions.
10139 This is the default version of this function, which is sufficient for
10140 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10143 _bfd_elf_is_function_type (unsigned int type)
10145 return (type == STT_FUNC
10146 || type == STT_GNU_IFUNC);
10149 /* If the ELF symbol SYM might be a function in SEC, return the
10150 function size and set *CODE_OFF to the function's entry point,
10151 otherwise return zero. */
10154 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10157 bfd_size_type size;
10159 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10160 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10161 || sym->section != sec)
10164 *code_off = sym->value;
10166 if (!(sym->flags & BSF_SYNTHETIC))
10167 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;