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 || bfd_seek (abfd, offset, SEEK_SET) != 0
301 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
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);
307 bfd_release (abfd, shstrtab);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp[shindex]->sh_size = 0;
315 shstrtab[shstrtabsize] = '\0';
316 i_shdrp[shindex]->contents = shstrtab;
318 return (char *) shstrtab;
322 bfd_elf_string_from_elf_section (bfd *abfd,
323 unsigned int shindex,
324 unsigned int strindex)
326 Elf_Internal_Shdr *hdr;
331 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
334 hdr = elf_elfsections (abfd)[shindex];
336 if (hdr->contents == NULL)
338 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
340 /* PR 17512: file: f057ec89. */
341 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
346 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
350 if (strindex >= hdr->sh_size)
352 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
353 (*_bfd_error_handler)
354 (_("%B: invalid string offset %u >= %lu for section `%s'"),
355 abfd, strindex, (unsigned long) hdr->sh_size,
356 (shindex == shstrndx && strindex == hdr->sh_name
358 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
362 return ((char *) hdr->contents) + strindex;
365 /* Read and convert symbols to internal format.
366 SYMCOUNT specifies the number of symbols to read, starting from
367 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
368 are non-NULL, they are used to store the internal symbols, external
369 symbols, and symbol section index extensions, respectively.
370 Returns a pointer to the internal symbol buffer (malloced if necessary)
371 or NULL if there were no symbols or some kind of problem. */
374 bfd_elf_get_elf_syms (bfd *ibfd,
375 Elf_Internal_Shdr *symtab_hdr,
378 Elf_Internal_Sym *intsym_buf,
380 Elf_External_Sym_Shndx *extshndx_buf)
382 Elf_Internal_Shdr *shndx_hdr;
384 const bfd_byte *esym;
385 Elf_External_Sym_Shndx *alloc_extshndx;
386 Elf_External_Sym_Shndx *shndx;
387 Elf_Internal_Sym *alloc_intsym;
388 Elf_Internal_Sym *isym;
389 Elf_Internal_Sym *isymend;
390 const struct elf_backend_data *bed;
395 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
401 /* Normal syms might have section extension entries. */
403 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
404 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
406 /* Read the symbols. */
408 alloc_extshndx = NULL;
410 bed = get_elf_backend_data (ibfd);
411 extsym_size = bed->s->sizeof_sym;
412 amt = symcount * extsym_size;
413 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
414 if (extsym_buf == NULL)
416 alloc_ext = bfd_malloc2 (symcount, extsym_size);
417 extsym_buf = alloc_ext;
419 if (extsym_buf == NULL
420 || bfd_seek (ibfd, pos, SEEK_SET) != 0
421 || bfd_bread (extsym_buf, amt, ibfd) != amt)
427 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
431 amt = symcount * sizeof (Elf_External_Sym_Shndx);
432 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
433 if (extshndx_buf == NULL)
435 alloc_extshndx = (Elf_External_Sym_Shndx *)
436 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
437 extshndx_buf = alloc_extshndx;
439 if (extshndx_buf == NULL
440 || bfd_seek (ibfd, pos, SEEK_SET) != 0
441 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
448 if (intsym_buf == NULL)
450 alloc_intsym = (Elf_Internal_Sym *)
451 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
452 intsym_buf = alloc_intsym;
453 if (intsym_buf == NULL)
457 /* Convert the symbols to internal form. */
458 isymend = intsym_buf + symcount;
459 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
460 shndx = extshndx_buf;
462 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
463 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
465 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
466 (*_bfd_error_handler) (_("%B symbol number %lu references "
467 "nonexistent SHT_SYMTAB_SHNDX section"),
468 ibfd, (unsigned long) symoffset);
469 if (alloc_intsym != NULL)
476 if (alloc_ext != NULL)
478 if (alloc_extshndx != NULL)
479 free (alloc_extshndx);
484 /* Look up a symbol name. */
486 bfd_elf_sym_name (bfd *abfd,
487 Elf_Internal_Shdr *symtab_hdr,
488 Elf_Internal_Sym *isym,
492 unsigned int iname = isym->st_name;
493 unsigned int shindex = symtab_hdr->sh_link;
495 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
496 /* Check for a bogus st_shndx to avoid crashing. */
497 && isym->st_shndx < elf_numsections (abfd))
499 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
500 shindex = elf_elfheader (abfd)->e_shstrndx;
503 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
506 else if (sym_sec && *name == '\0')
507 name = bfd_section_name (abfd, sym_sec);
512 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
513 sections. The first element is the flags, the rest are section
516 typedef union elf_internal_group {
517 Elf_Internal_Shdr *shdr;
519 } Elf_Internal_Group;
521 /* Return the name of the group signature symbol. Why isn't the
522 signature just a string? */
525 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
527 Elf_Internal_Shdr *hdr;
528 unsigned char esym[sizeof (Elf64_External_Sym)];
529 Elf_External_Sym_Shndx eshndx;
530 Elf_Internal_Sym isym;
532 /* First we need to ensure the symbol table is available. Make sure
533 that it is a symbol table section. */
534 if (ghdr->sh_link >= elf_numsections (abfd))
536 hdr = elf_elfsections (abfd) [ghdr->sh_link];
537 if (hdr->sh_type != SHT_SYMTAB
538 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
541 /* Go read the symbol. */
542 hdr = &elf_tdata (abfd)->symtab_hdr;
543 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
544 &isym, esym, &eshndx) == NULL)
547 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
550 /* Set next_in_group list pointer, and group name for NEWSECT. */
553 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
555 unsigned int num_group = elf_tdata (abfd)->num_group;
557 /* If num_group is zero, read in all SHT_GROUP sections. The count
558 is set to -1 if there are no SHT_GROUP sections. */
561 unsigned int i, shnum;
563 /* First count the number of groups. If we have a SHT_GROUP
564 section with just a flag word (ie. sh_size is 4), ignore it. */
565 shnum = elf_numsections (abfd);
568 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
569 ( (shdr)->sh_type == SHT_GROUP \
570 && (shdr)->sh_size >= minsize \
571 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
572 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
574 for (i = 0; i < shnum; i++)
576 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
578 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
584 num_group = (unsigned) -1;
585 elf_tdata (abfd)->num_group = num_group;
589 /* We keep a list of elf section headers for group sections,
590 so we can find them quickly. */
593 elf_tdata (abfd)->num_group = num_group;
594 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
595 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
596 if (elf_tdata (abfd)->group_sect_ptr == NULL)
600 for (i = 0; i < shnum; i++)
602 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
604 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
607 Elf_Internal_Group *dest;
609 /* Add to list of sections. */
610 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
613 /* Read the raw contents. */
614 BFD_ASSERT (sizeof (*dest) >= 4);
615 amt = shdr->sh_size * sizeof (*dest) / 4;
616 shdr->contents = (unsigned char *)
617 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
618 /* PR binutils/4110: Handle corrupt group headers. */
619 if (shdr->contents == NULL)
622 (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
623 bfd_set_error (bfd_error_bad_value);
628 memset (shdr->contents, 0, amt);
630 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
631 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
635 (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size);
636 bfd_set_error (bfd_error_bad_value);
638 /* PR 17510: If the group contents are even partially
639 corrupt, do not allow any of the contents to be used. */
640 memset (shdr->contents, 0, amt);
644 /* Translate raw contents, a flag word followed by an
645 array of elf section indices all in target byte order,
646 to the flag word followed by an array of elf section
648 src = shdr->contents + shdr->sh_size;
649 dest = (Elf_Internal_Group *) (shdr->contents + amt);
656 idx = H_GET_32 (abfd, src);
657 if (src == shdr->contents)
660 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
661 shdr->bfd_section->flags
662 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
667 ((*_bfd_error_handler)
668 (_("%B: invalid SHT_GROUP entry"), abfd));
671 dest->shdr = elf_elfsections (abfd)[idx];
676 /* PR 17510: Corrupt binaries might contain invalid groups. */
677 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
679 elf_tdata (abfd)->num_group = num_group;
681 /* If all groups are invalid then fail. */
684 elf_tdata (abfd)->group_sect_ptr = NULL;
685 elf_tdata (abfd)->num_group = num_group = -1;
686 (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd);
687 bfd_set_error (bfd_error_bad_value);
693 if (num_group != (unsigned) -1)
697 for (i = 0; i < num_group; i++)
699 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
700 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
701 unsigned int n_elt = shdr->sh_size / 4;
703 /* Look through this group's sections to see if current
704 section is a member. */
706 if ((++idx)->shdr == hdr)
710 /* We are a member of this group. Go looking through
711 other members to see if any others are linked via
713 idx = (Elf_Internal_Group *) shdr->contents;
714 n_elt = shdr->sh_size / 4;
716 if ((s = (++idx)->shdr->bfd_section) != NULL
717 && elf_next_in_group (s) != NULL)
721 /* Snarf the group name from other member, and
722 insert current section in circular list. */
723 elf_group_name (newsect) = elf_group_name (s);
724 elf_next_in_group (newsect) = elf_next_in_group (s);
725 elf_next_in_group (s) = newsect;
731 gname = group_signature (abfd, shdr);
734 elf_group_name (newsect) = gname;
736 /* Start a circular list with one element. */
737 elf_next_in_group (newsect) = newsect;
740 /* If the group section has been created, point to the
742 if (shdr->bfd_section != NULL)
743 elf_next_in_group (shdr->bfd_section) = newsect;
751 if (elf_group_name (newsect) == NULL)
753 (*_bfd_error_handler) (_("%B: no group info for section %A"),
761 _bfd_elf_setup_sections (bfd *abfd)
764 unsigned int num_group = elf_tdata (abfd)->num_group;
765 bfd_boolean result = TRUE;
768 /* Process SHF_LINK_ORDER. */
769 for (s = abfd->sections; s != NULL; s = s->next)
771 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
772 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
774 unsigned int elfsec = this_hdr->sh_link;
775 /* FIXME: The old Intel compiler and old strip/objcopy may
776 not set the sh_link or sh_info fields. Hence we could
777 get the situation where elfsec is 0. */
780 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
781 if (bed->link_order_error_handler)
782 bed->link_order_error_handler
783 (_("%B: warning: sh_link not set for section `%A'"),
788 asection *linksec = NULL;
790 if (elfsec < elf_numsections (abfd))
792 this_hdr = elf_elfsections (abfd)[elfsec];
793 linksec = this_hdr->bfd_section;
797 Some strip/objcopy may leave an incorrect value in
798 sh_link. We don't want to proceed. */
801 (*_bfd_error_handler)
802 (_("%B: sh_link [%d] in section `%A' is incorrect"),
803 s->owner, s, elfsec);
807 elf_linked_to_section (s) = linksec;
812 /* Process section groups. */
813 if (num_group == (unsigned) -1)
816 for (i = 0; i < num_group; i++)
818 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
819 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
820 unsigned int n_elt = shdr->sh_size / 4;
823 if ((++idx)->shdr->bfd_section)
824 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
825 else if (idx->shdr->sh_type == SHT_RELA
826 || idx->shdr->sh_type == SHT_REL)
827 /* We won't include relocation sections in section groups in
828 output object files. We adjust the group section size here
829 so that relocatable link will work correctly when
830 relocation sections are in section group in input object
832 shdr->bfd_section->size -= 4;
835 /* There are some unknown sections in the group. */
836 (*_bfd_error_handler)
837 (_("%B: unknown [%d] section `%s' in group [%s]"),
839 (unsigned int) idx->shdr->sh_type,
840 bfd_elf_string_from_elf_section (abfd,
841 (elf_elfheader (abfd)
844 shdr->bfd_section->name);
852 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
854 return elf_next_in_group (sec) != NULL;
857 /* Make a BFD section from an ELF section. We store a pointer to the
858 BFD section in the bfd_section field of the header. */
861 _bfd_elf_make_section_from_shdr (bfd *abfd,
862 Elf_Internal_Shdr *hdr,
868 const struct elf_backend_data *bed;
870 if (hdr->bfd_section != NULL)
873 newsect = bfd_make_section_anyway (abfd, name);
877 hdr->bfd_section = newsect;
878 elf_section_data (newsect)->this_hdr = *hdr;
879 elf_section_data (newsect)->this_idx = shindex;
881 /* Always use the real type/flags. */
882 elf_section_type (newsect) = hdr->sh_type;
883 elf_section_flags (newsect) = hdr->sh_flags;
885 newsect->filepos = hdr->sh_offset;
887 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
888 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
889 || ! bfd_set_section_alignment (abfd, newsect,
890 bfd_log2 (hdr->sh_addralign)))
893 flags = SEC_NO_FLAGS;
894 if (hdr->sh_type != SHT_NOBITS)
895 flags |= SEC_HAS_CONTENTS;
896 if (hdr->sh_type == SHT_GROUP)
897 flags |= SEC_GROUP | SEC_EXCLUDE;
898 if ((hdr->sh_flags & SHF_ALLOC) != 0)
901 if (hdr->sh_type != SHT_NOBITS)
904 if ((hdr->sh_flags & SHF_WRITE) == 0)
905 flags |= SEC_READONLY;
906 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
908 else if ((flags & SEC_LOAD) != 0)
910 if ((hdr->sh_flags & SHF_MERGE) != 0)
913 newsect->entsize = hdr->sh_entsize;
914 if ((hdr->sh_flags & SHF_STRINGS) != 0)
915 flags |= SEC_STRINGS;
917 if (hdr->sh_flags & SHF_GROUP)
918 if (!setup_group (abfd, hdr, newsect))
920 if ((hdr->sh_flags & SHF_TLS) != 0)
921 flags |= SEC_THREAD_LOCAL;
922 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
923 flags |= SEC_EXCLUDE;
925 if ((flags & SEC_ALLOC) == 0)
927 /* The debugging sections appear to be recognized only by name,
928 not any sort of flag. Their SEC_ALLOC bits are cleared. */
935 else if (name[1] == 'g' && name[2] == 'n')
936 p = ".gnu.linkonce.wi.", n = 17;
937 else if (name[1] == 'g' && name[2] == 'd')
938 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
939 else if (name[1] == 'l')
941 else if (name[1] == 's')
943 else if (name[1] == 'z')
944 p = ".zdebug", n = 7;
947 if (p != NULL && strncmp (name, p, n) == 0)
948 flags |= SEC_DEBUGGING;
952 /* As a GNU extension, if the name begins with .gnu.linkonce, we
953 only link a single copy of the section. This is used to support
954 g++. g++ will emit each template expansion in its own section.
955 The symbols will be defined as weak, so that multiple definitions
956 are permitted. The GNU linker extension is to actually discard
957 all but one of the sections. */
958 if (CONST_STRNEQ (name, ".gnu.linkonce")
959 && elf_next_in_group (newsect) == NULL)
960 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
962 bed = get_elf_backend_data (abfd);
963 if (bed->elf_backend_section_flags)
964 if (! bed->elf_backend_section_flags (&flags, hdr))
967 if (! bfd_set_section_flags (abfd, newsect, flags))
970 /* We do not parse the PT_NOTE segments as we are interested even in the
971 separate debug info files which may have the segments offsets corrupted.
972 PT_NOTEs from the core files are currently not parsed using BFD. */
973 if (hdr->sh_type == SHT_NOTE)
977 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
980 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
984 if ((flags & SEC_ALLOC) != 0)
986 Elf_Internal_Phdr *phdr;
987 unsigned int i, nload;
989 /* Some ELF linkers produce binaries with all the program header
990 p_paddr fields zero. If we have such a binary with more than
991 one PT_LOAD header, then leave the section lma equal to vma
992 so that we don't create sections with overlapping lma. */
993 phdr = elf_tdata (abfd)->phdr;
994 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
995 if (phdr->p_paddr != 0)
997 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
999 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1002 phdr = elf_tdata (abfd)->phdr;
1003 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1005 if (((phdr->p_type == PT_LOAD
1006 && (hdr->sh_flags & SHF_TLS) == 0)
1007 || phdr->p_type == PT_TLS)
1008 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1010 if ((flags & SEC_LOAD) == 0)
1011 newsect->lma = (phdr->p_paddr
1012 + hdr->sh_addr - phdr->p_vaddr);
1014 /* We used to use the same adjustment for SEC_LOAD
1015 sections, but that doesn't work if the segment
1016 is packed with code from multiple VMAs.
1017 Instead we calculate the section LMA based on
1018 the segment LMA. It is assumed that the
1019 segment will contain sections with contiguous
1020 LMAs, even if the VMAs are not. */
1021 newsect->lma = (phdr->p_paddr
1022 + hdr->sh_offset - phdr->p_offset);
1024 /* With contiguous segments, we can't tell from file
1025 offsets whether a section with zero size should
1026 be placed at the end of one segment or the
1027 beginning of the next. Decide based on vaddr. */
1028 if (hdr->sh_addr >= phdr->p_vaddr
1029 && (hdr->sh_addr + hdr->sh_size
1030 <= phdr->p_vaddr + phdr->p_memsz))
1036 /* Compress/decompress DWARF debug sections with names: .debug_* and
1037 .zdebug_*, after the section flags is set. */
1038 if ((flags & SEC_DEBUGGING)
1039 && ((name[1] == 'd' && name[6] == '_')
1040 || (name[1] == 'z' && name[7] == '_')))
1042 enum { nothing, compress, decompress } action = nothing;
1045 if (bfd_is_section_compressed (abfd, newsect))
1047 /* Compressed section. Check if we should decompress. */
1048 if ((abfd->flags & BFD_DECOMPRESS))
1049 action = decompress;
1053 /* Normal section. Check if we should compress. */
1054 if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
1064 if (!bfd_init_section_compress_status (abfd, newsect))
1066 (*_bfd_error_handler)
1067 (_("%B: unable to initialize compress status for section %s"),
1073 unsigned int len = strlen (name);
1075 new_name = bfd_alloc (abfd, len + 2);
1076 if (new_name == NULL)
1080 memcpy (new_name + 2, name + 1, len);
1084 if (!bfd_init_section_decompress_status (abfd, newsect))
1086 (*_bfd_error_handler)
1087 (_("%B: unable to initialize decompress status for section %s"),
1093 unsigned int len = strlen (name);
1095 new_name = bfd_alloc (abfd, len);
1096 if (new_name == NULL)
1099 memcpy (new_name + 1, name + 2, len - 1);
1103 if (new_name != NULL)
1104 bfd_rename_section (abfd, newsect, new_name);
1110 const char *const bfd_elf_section_type_names[] = {
1111 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1112 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1113 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1116 /* ELF relocs are against symbols. If we are producing relocatable
1117 output, and the reloc is against an external symbol, and nothing
1118 has given us any additional addend, the resulting reloc will also
1119 be against the same symbol. In such a case, we don't want to
1120 change anything about the way the reloc is handled, since it will
1121 all be done at final link time. Rather than put special case code
1122 into bfd_perform_relocation, all the reloc types use this howto
1123 function. It just short circuits the reloc if producing
1124 relocatable output against an external symbol. */
1126 bfd_reloc_status_type
1127 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1128 arelent *reloc_entry,
1130 void *data ATTRIBUTE_UNUSED,
1131 asection *input_section,
1133 char **error_message ATTRIBUTE_UNUSED)
1135 if (output_bfd != NULL
1136 && (symbol->flags & BSF_SECTION_SYM) == 0
1137 && (! reloc_entry->howto->partial_inplace
1138 || reloc_entry->addend == 0))
1140 reloc_entry->address += input_section->output_offset;
1141 return bfd_reloc_ok;
1144 return bfd_reloc_continue;
1147 /* Copy the program header and other data from one object module to
1151 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1153 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1154 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1157 if (!elf_flags_init (obfd))
1159 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1160 elf_flags_init (obfd) = TRUE;
1163 elf_gp (obfd) = elf_gp (ibfd);
1165 /* Also copy the EI_OSABI field. */
1166 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1167 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1169 /* Copy object attributes. */
1170 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1175 get_segment_type (unsigned int p_type)
1180 case PT_NULL: pt = "NULL"; break;
1181 case PT_LOAD: pt = "LOAD"; break;
1182 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1183 case PT_INTERP: pt = "INTERP"; break;
1184 case PT_NOTE: pt = "NOTE"; break;
1185 case PT_SHLIB: pt = "SHLIB"; break;
1186 case PT_PHDR: pt = "PHDR"; break;
1187 case PT_TLS: pt = "TLS"; break;
1188 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1189 case PT_GNU_STACK: pt = "STACK"; break;
1190 case PT_GNU_RELRO: pt = "RELRO"; break;
1191 default: pt = NULL; break;
1196 /* Print out the program headers. */
1199 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1201 FILE *f = (FILE *) farg;
1202 Elf_Internal_Phdr *p;
1204 bfd_byte *dynbuf = NULL;
1206 p = elf_tdata (abfd)->phdr;
1211 fprintf (f, _("\nProgram Header:\n"));
1212 c = elf_elfheader (abfd)->e_phnum;
1213 for (i = 0; i < c; i++, p++)
1215 const char *pt = get_segment_type (p->p_type);
1220 sprintf (buf, "0x%lx", p->p_type);
1223 fprintf (f, "%8s off 0x", pt);
1224 bfd_fprintf_vma (abfd, f, p->p_offset);
1225 fprintf (f, " vaddr 0x");
1226 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1227 fprintf (f, " paddr 0x");
1228 bfd_fprintf_vma (abfd, f, p->p_paddr);
1229 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1230 fprintf (f, " filesz 0x");
1231 bfd_fprintf_vma (abfd, f, p->p_filesz);
1232 fprintf (f, " memsz 0x");
1233 bfd_fprintf_vma (abfd, f, p->p_memsz);
1234 fprintf (f, " flags %c%c%c",
1235 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1236 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1237 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1238 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1239 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1244 s = bfd_get_section_by_name (abfd, ".dynamic");
1247 unsigned int elfsec;
1248 unsigned long shlink;
1249 bfd_byte *extdyn, *extdynend;
1251 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1253 fprintf (f, _("\nDynamic Section:\n"));
1255 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1258 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1259 if (elfsec == SHN_BAD)
1261 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1263 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1264 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1267 /* PR 17512: file: 6f427532. */
1268 if (s->size < extdynsize)
1270 extdynend = extdyn + s->size;
1271 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1273 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1275 Elf_Internal_Dyn dyn;
1276 const char *name = "";
1278 bfd_boolean stringp;
1279 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1281 (*swap_dyn_in) (abfd, extdyn, &dyn);
1283 if (dyn.d_tag == DT_NULL)
1290 if (bed->elf_backend_get_target_dtag)
1291 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1293 if (!strcmp (name, ""))
1295 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1300 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1301 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1302 case DT_PLTGOT: name = "PLTGOT"; break;
1303 case DT_HASH: name = "HASH"; break;
1304 case DT_STRTAB: name = "STRTAB"; break;
1305 case DT_SYMTAB: name = "SYMTAB"; break;
1306 case DT_RELA: name = "RELA"; break;
1307 case DT_RELASZ: name = "RELASZ"; break;
1308 case DT_RELAENT: name = "RELAENT"; break;
1309 case DT_STRSZ: name = "STRSZ"; break;
1310 case DT_SYMENT: name = "SYMENT"; break;
1311 case DT_INIT: name = "INIT"; break;
1312 case DT_FINI: name = "FINI"; break;
1313 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1314 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1315 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1316 case DT_REL: name = "REL"; break;
1317 case DT_RELSZ: name = "RELSZ"; break;
1318 case DT_RELENT: name = "RELENT"; break;
1319 case DT_PLTREL: name = "PLTREL"; break;
1320 case DT_DEBUG: name = "DEBUG"; break;
1321 case DT_TEXTREL: name = "TEXTREL"; break;
1322 case DT_JMPREL: name = "JMPREL"; break;
1323 case DT_BIND_NOW: name = "BIND_NOW"; break;
1324 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1325 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1326 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1327 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1328 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1329 case DT_FLAGS: name = "FLAGS"; break;
1330 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1331 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1332 case DT_CHECKSUM: name = "CHECKSUM"; break;
1333 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1334 case DT_MOVEENT: name = "MOVEENT"; break;
1335 case DT_MOVESZ: name = "MOVESZ"; break;
1336 case DT_FEATURE: name = "FEATURE"; break;
1337 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1338 case DT_SYMINSZ: name = "SYMINSZ"; break;
1339 case DT_SYMINENT: name = "SYMINENT"; break;
1340 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1341 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1342 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1343 case DT_PLTPAD: name = "PLTPAD"; break;
1344 case DT_MOVETAB: name = "MOVETAB"; break;
1345 case DT_SYMINFO: name = "SYMINFO"; break;
1346 case DT_RELACOUNT: name = "RELACOUNT"; break;
1347 case DT_RELCOUNT: name = "RELCOUNT"; break;
1348 case DT_FLAGS_1: name = "FLAGS_1"; break;
1349 case DT_VERSYM: name = "VERSYM"; break;
1350 case DT_VERDEF: name = "VERDEF"; break;
1351 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1352 case DT_VERNEED: name = "VERNEED"; break;
1353 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1354 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1355 case DT_USED: name = "USED"; break;
1356 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1357 case DT_GNU_HASH: name = "GNU_HASH"; break;
1360 fprintf (f, " %-20s ", name);
1364 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1369 unsigned int tagv = dyn.d_un.d_val;
1371 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1374 fprintf (f, "%s", string);
1383 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1384 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1386 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1390 if (elf_dynverdef (abfd) != 0)
1392 Elf_Internal_Verdef *t;
1394 fprintf (f, _("\nVersion definitions:\n"));
1395 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1397 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1398 t->vd_flags, t->vd_hash,
1399 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1400 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1402 Elf_Internal_Verdaux *a;
1405 for (a = t->vd_auxptr->vda_nextptr;
1409 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1415 if (elf_dynverref (abfd) != 0)
1417 Elf_Internal_Verneed *t;
1419 fprintf (f, _("\nVersion References:\n"));
1420 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1422 Elf_Internal_Vernaux *a;
1424 fprintf (f, _(" required from %s:\n"),
1425 t->vn_filename ? t->vn_filename : "<corrupt>");
1426 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1427 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1428 a->vna_flags, a->vna_other,
1429 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1441 /* Display ELF-specific fields of a symbol. */
1444 bfd_elf_print_symbol (bfd *abfd,
1447 bfd_print_symbol_type how)
1449 FILE *file = (FILE *) filep;
1452 case bfd_print_symbol_name:
1453 fprintf (file, "%s", symbol->name);
1455 case bfd_print_symbol_more:
1456 fprintf (file, "elf ");
1457 bfd_fprintf_vma (abfd, file, symbol->value);
1458 fprintf (file, " %lx", (unsigned long) symbol->flags);
1460 case bfd_print_symbol_all:
1462 const char *section_name;
1463 const char *name = NULL;
1464 const struct elf_backend_data *bed;
1465 unsigned char st_other;
1468 section_name = symbol->section ? symbol->section->name : "(*none*)";
1470 bed = get_elf_backend_data (abfd);
1471 if (bed->elf_backend_print_symbol_all)
1472 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1476 name = symbol->name;
1477 bfd_print_symbol_vandf (abfd, file, symbol);
1480 fprintf (file, " %s\t", section_name);
1481 /* Print the "other" value for a symbol. For common symbols,
1482 we've already printed the size; now print the alignment.
1483 For other symbols, we have no specified alignment, and
1484 we've printed the address; now print the size. */
1485 if (symbol->section && bfd_is_com_section (symbol->section))
1486 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1488 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1489 bfd_fprintf_vma (abfd, file, val);
1491 /* If we have version information, print it. */
1492 if (elf_dynversym (abfd) != 0
1493 && (elf_dynverdef (abfd) != 0
1494 || elf_dynverref (abfd) != 0))
1496 unsigned int vernum;
1497 const char *version_string;
1499 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1502 version_string = "";
1503 else if (vernum == 1)
1504 version_string = "Base";
1505 else if (vernum <= elf_tdata (abfd)->cverdefs)
1507 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1510 Elf_Internal_Verneed *t;
1512 version_string = "";
1513 for (t = elf_tdata (abfd)->verref;
1517 Elf_Internal_Vernaux *a;
1519 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1521 if (a->vna_other == vernum)
1523 version_string = a->vna_nodename;
1530 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1531 fprintf (file, " %-11s", version_string);
1536 fprintf (file, " (%s)", version_string);
1537 for (i = 10 - strlen (version_string); i > 0; --i)
1542 /* If the st_other field is not zero, print it. */
1543 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1548 case STV_INTERNAL: fprintf (file, " .internal"); break;
1549 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1550 case STV_PROTECTED: fprintf (file, " .protected"); break;
1552 /* Some other non-defined flags are also present, so print
1554 fprintf (file, " 0x%02x", (unsigned int) st_other);
1557 fprintf (file, " %s", name);
1563 /* Allocate an ELF string table--force the first byte to be zero. */
1565 struct bfd_strtab_hash *
1566 _bfd_elf_stringtab_init (void)
1568 struct bfd_strtab_hash *ret;
1570 ret = _bfd_stringtab_init ();
1575 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1576 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1577 if (loc == (bfd_size_type) -1)
1579 _bfd_stringtab_free (ret);
1586 /* ELF .o/exec file reading */
1588 /* Create a new bfd section from an ELF section header. */
1591 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1593 Elf_Internal_Shdr *hdr;
1594 Elf_Internal_Ehdr *ehdr;
1595 const struct elf_backend_data *bed;
1597 bfd_boolean ret = TRUE;
1598 static bfd_boolean * sections_being_created = NULL;
1599 static bfd * sections_being_created_abfd = NULL;
1600 static unsigned int nesting = 0;
1602 if (shindex >= elf_numsections (abfd))
1607 /* PR17512: A corrupt ELF binary might contain a recursive group of
1608 sections, each with string indicies pointing to the next in the
1609 loop. Detect this here, by refusing to load a section that we are
1610 already in the process of loading. We only trigger this test if
1611 we have nested at least three sections deep as normal ELF binaries
1612 can expect to recurse at least once.
1614 FIXME: It would be better if this array was attached to the bfd,
1615 rather than being held in a static pointer. */
1617 if (sections_being_created_abfd != abfd)
1618 sections_being_created = NULL;
1619 if (sections_being_created == NULL)
1621 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1622 sections_being_created = (bfd_boolean *)
1623 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
1624 sections_being_created_abfd = abfd;
1626 if (sections_being_created [shindex])
1628 (*_bfd_error_handler)
1629 (_("%B: warning: loop in section dependencies detected"), abfd);
1632 sections_being_created [shindex] = TRUE;
1635 hdr = elf_elfsections (abfd)[shindex];
1636 ehdr = elf_elfheader (abfd);
1637 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1642 bed = get_elf_backend_data (abfd);
1643 switch (hdr->sh_type)
1646 /* Inactive section. Throw it away. */
1649 case SHT_PROGBITS: /* Normal section with contents. */
1650 case SHT_NOBITS: /* .bss section. */
1651 case SHT_HASH: /* .hash section. */
1652 case SHT_NOTE: /* .note section. */
1653 case SHT_INIT_ARRAY: /* .init_array section. */
1654 case SHT_FINI_ARRAY: /* .fini_array section. */
1655 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1656 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1657 case SHT_GNU_HASH: /* .gnu.hash section. */
1658 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1661 case SHT_DYNAMIC: /* Dynamic linking information. */
1662 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1665 if (hdr->sh_link > elf_numsections (abfd))
1667 /* PR 10478: Accept Solaris binaries with a sh_link
1668 field set to SHN_BEFORE or SHN_AFTER. */
1669 switch (bfd_get_arch (abfd))
1672 case bfd_arch_sparc:
1673 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1674 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1676 /* Otherwise fall through. */
1681 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1683 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1685 Elf_Internal_Shdr *dynsymhdr;
1687 /* The shared libraries distributed with hpux11 have a bogus
1688 sh_link field for the ".dynamic" section. Find the
1689 string table for the ".dynsym" section instead. */
1690 if (elf_dynsymtab (abfd) != 0)
1692 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1693 hdr->sh_link = dynsymhdr->sh_link;
1697 unsigned int i, num_sec;
1699 num_sec = elf_numsections (abfd);
1700 for (i = 1; i < num_sec; i++)
1702 dynsymhdr = elf_elfsections (abfd)[i];
1703 if (dynsymhdr->sh_type == SHT_DYNSYM)
1705 hdr->sh_link = dynsymhdr->sh_link;
1713 case SHT_SYMTAB: /* A symbol table. */
1714 if (elf_onesymtab (abfd) == shindex)
1717 if (hdr->sh_entsize != bed->s->sizeof_sym)
1720 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1722 if (hdr->sh_size != 0)
1724 /* Some assemblers erroneously set sh_info to one with a
1725 zero sh_size. ld sees this as a global symbol count
1726 of (unsigned) -1. Fix it here. */
1731 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1732 elf_onesymtab (abfd) = shindex;
1733 elf_tdata (abfd)->symtab_hdr = *hdr;
1734 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1735 abfd->flags |= HAS_SYMS;
1737 /* Sometimes a shared object will map in the symbol table. If
1738 SHF_ALLOC is set, and this is a shared object, then we also
1739 treat this section as a BFD section. We can not base the
1740 decision purely on SHF_ALLOC, because that flag is sometimes
1741 set in a relocatable object file, which would confuse the
1743 if ((hdr->sh_flags & SHF_ALLOC) != 0
1744 && (abfd->flags & DYNAMIC) != 0
1745 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1749 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1750 can't read symbols without that section loaded as well. It
1751 is most likely specified by the next section header. */
1752 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1754 unsigned int i, num_sec;
1756 num_sec = elf_numsections (abfd);
1757 for (i = shindex + 1; i < num_sec; i++)
1759 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1760 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1761 && hdr2->sh_link == shindex)
1765 for (i = 1; i < shindex; i++)
1767 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1768 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1769 && hdr2->sh_link == shindex)
1773 ret = bfd_section_from_shdr (abfd, i);
1777 case SHT_DYNSYM: /* A dynamic symbol table. */
1778 if (elf_dynsymtab (abfd) == shindex)
1781 if (hdr->sh_entsize != bed->s->sizeof_sym)
1784 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1786 if (hdr->sh_size != 0)
1789 /* Some linkers erroneously set sh_info to one with a
1790 zero sh_size. ld sees this as a global symbol count
1791 of (unsigned) -1. Fix it here. */
1796 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1797 elf_dynsymtab (abfd) = shindex;
1798 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1799 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1800 abfd->flags |= HAS_SYMS;
1802 /* Besides being a symbol table, we also treat this as a regular
1803 section, so that objcopy can handle it. */
1804 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1807 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
1808 if (elf_symtab_shndx (abfd) == shindex)
1811 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1812 elf_symtab_shndx (abfd) = shindex;
1813 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1814 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1817 case SHT_STRTAB: /* A string table. */
1818 if (hdr->bfd_section != NULL)
1821 if (ehdr->e_shstrndx == shindex)
1823 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1824 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1828 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1831 elf_tdata (abfd)->strtab_hdr = *hdr;
1832 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1836 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1839 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1840 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1841 elf_elfsections (abfd)[shindex] = hdr;
1842 /* We also treat this as a regular section, so that objcopy
1844 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1849 /* If the string table isn't one of the above, then treat it as a
1850 regular section. We need to scan all the headers to be sure,
1851 just in case this strtab section appeared before the above. */
1852 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1854 unsigned int i, num_sec;
1856 num_sec = elf_numsections (abfd);
1857 for (i = 1; i < num_sec; i++)
1859 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1860 if (hdr2->sh_link == shindex)
1862 /* Prevent endless recursion on broken objects. */
1865 if (! bfd_section_from_shdr (abfd, i))
1867 if (elf_onesymtab (abfd) == i)
1869 if (elf_dynsymtab (abfd) == i)
1870 goto dynsymtab_strtab;
1874 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1879 /* *These* do a lot of work -- but build no sections! */
1881 asection *target_sect;
1882 Elf_Internal_Shdr *hdr2, **p_hdr;
1883 unsigned int num_sec = elf_numsections (abfd);
1884 struct bfd_elf_section_data *esdt;
1888 != (bfd_size_type) (hdr->sh_type == SHT_REL
1889 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1892 /* Check for a bogus link to avoid crashing. */
1893 if (hdr->sh_link >= num_sec)
1895 ((*_bfd_error_handler)
1896 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1897 abfd, hdr->sh_link, name, shindex));
1898 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1903 /* For some incomprehensible reason Oracle distributes
1904 libraries for Solaris in which some of the objects have
1905 bogus sh_link fields. It would be nice if we could just
1906 reject them, but, unfortunately, some people need to use
1907 them. We scan through the section headers; if we find only
1908 one suitable symbol table, we clobber the sh_link to point
1909 to it. I hope this doesn't break anything.
1911 Don't do it on executable nor shared library. */
1912 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1913 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1914 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1920 for (scan = 1; scan < num_sec; scan++)
1922 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1923 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1934 hdr->sh_link = found;
1937 /* Get the symbol table. */
1938 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1939 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1940 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1943 /* If this reloc section does not use the main symbol table we
1944 don't treat it as a reloc section. BFD can't adequately
1945 represent such a section, so at least for now, we don't
1946 try. We just present it as a normal section. We also
1947 can't use it as a reloc section if it points to the null
1948 section, an invalid section, another reloc section, or its
1949 sh_link points to the null section. */
1950 if (hdr->sh_link != elf_onesymtab (abfd)
1951 || hdr->sh_link == SHN_UNDEF
1952 || hdr->sh_info == SHN_UNDEF
1953 || hdr->sh_info >= num_sec
1954 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1955 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1957 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1962 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1965 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1966 if (target_sect == NULL)
1969 esdt = elf_section_data (target_sect);
1970 if (hdr->sh_type == SHT_RELA)
1971 p_hdr = &esdt->rela.hdr;
1973 p_hdr = &esdt->rel.hdr;
1975 /* PR 17512: file: 0b4f81b7. */
1978 amt = sizeof (*hdr2);
1979 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1984 elf_elfsections (abfd)[shindex] = hdr2;
1985 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1986 target_sect->flags |= SEC_RELOC;
1987 target_sect->relocation = NULL;
1988 target_sect->rel_filepos = hdr->sh_offset;
1989 /* In the section to which the relocations apply, mark whether
1990 its relocations are of the REL or RELA variety. */
1991 if (hdr->sh_size != 0)
1993 if (hdr->sh_type == SHT_RELA)
1994 target_sect->use_rela_p = 1;
1996 abfd->flags |= HAS_RELOC;
2000 case SHT_GNU_verdef:
2001 elf_dynverdef (abfd) = shindex;
2002 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2003 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2006 case SHT_GNU_versym:
2007 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2010 elf_dynversym (abfd) = shindex;
2011 elf_tdata (abfd)->dynversym_hdr = *hdr;
2012 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2015 case SHT_GNU_verneed:
2016 elf_dynverref (abfd) = shindex;
2017 elf_tdata (abfd)->dynverref_hdr = *hdr;
2018 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2025 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2028 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2031 if (hdr->contents != NULL)
2033 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2034 unsigned int n_elt = hdr->sh_size / sizeof (* idx);
2039 if (idx->flags & GRP_COMDAT)
2040 hdr->bfd_section->flags
2041 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2043 /* We try to keep the same section order as it comes in. */
2045 while (--n_elt != 0)
2049 if (idx->shdr != NULL
2050 && (s = idx->shdr->bfd_section) != NULL
2051 && elf_next_in_group (s) != NULL)
2053 elf_next_in_group (hdr->bfd_section) = s;
2061 /* Possibly an attributes section. */
2062 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2063 || hdr->sh_type == bed->obj_attrs_section_type)
2065 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2067 _bfd_elf_parse_attributes (abfd, hdr);
2071 /* Check for any processor-specific section types. */
2072 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2075 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2077 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2078 /* FIXME: How to properly handle allocated section reserved
2079 for applications? */
2080 (*_bfd_error_handler)
2081 (_("%B: don't know how to handle allocated, application "
2082 "specific section `%s' [0x%8x]"),
2083 abfd, name, hdr->sh_type);
2086 /* Allow sections reserved for applications. */
2087 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2092 else if (hdr->sh_type >= SHT_LOPROC
2093 && hdr->sh_type <= SHT_HIPROC)
2094 /* FIXME: We should handle this section. */
2095 (*_bfd_error_handler)
2096 (_("%B: don't know how to handle processor specific section "
2098 abfd, name, hdr->sh_type);
2099 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2101 /* Unrecognised OS-specific sections. */
2102 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2103 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2104 required to correctly process the section and the file should
2105 be rejected with an error message. */
2106 (*_bfd_error_handler)
2107 (_("%B: don't know how to handle OS specific section "
2109 abfd, name, hdr->sh_type);
2112 /* Otherwise it should be processed. */
2113 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2118 /* FIXME: We should handle this section. */
2119 (*_bfd_error_handler)
2120 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2121 abfd, name, hdr->sh_type);
2129 if (sections_being_created && sections_being_created_abfd == abfd)
2130 sections_being_created [shindex] = FALSE;
2131 if (-- nesting == 0)
2133 sections_being_created = NULL;
2134 sections_being_created_abfd = abfd;
2139 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2142 bfd_sym_from_r_symndx (struct sym_cache *cache,
2144 unsigned long r_symndx)
2146 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2148 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2150 Elf_Internal_Shdr *symtab_hdr;
2151 unsigned char esym[sizeof (Elf64_External_Sym)];
2152 Elf_External_Sym_Shndx eshndx;
2154 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2155 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2156 &cache->sym[ent], esym, &eshndx) == NULL)
2159 if (cache->abfd != abfd)
2161 memset (cache->indx, -1, sizeof (cache->indx));
2164 cache->indx[ent] = r_symndx;
2167 return &cache->sym[ent];
2170 /* Given an ELF section number, retrieve the corresponding BFD
2174 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2176 if (sec_index >= elf_numsections (abfd))
2178 return elf_elfsections (abfd)[sec_index]->bfd_section;
2181 static const struct bfd_elf_special_section special_sections_b[] =
2183 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2184 { NULL, 0, 0, 0, 0 }
2187 static const struct bfd_elf_special_section special_sections_c[] =
2189 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2190 { NULL, 0, 0, 0, 0 }
2193 static const struct bfd_elf_special_section special_sections_d[] =
2195 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2196 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2197 /* There are more DWARF sections than these, but they needn't be added here
2198 unless you have to cope with broken compilers that don't emit section
2199 attributes or you want to help the user writing assembler. */
2200 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2201 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2202 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2203 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2204 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2205 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2206 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2207 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2208 { NULL, 0, 0, 0, 0 }
2211 static const struct bfd_elf_special_section special_sections_f[] =
2213 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2214 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2215 { NULL, 0, 0, 0, 0 }
2218 static const struct bfd_elf_special_section special_sections_g[] =
2220 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2221 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2222 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2223 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2224 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2225 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2226 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2227 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2228 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2229 { NULL, 0, 0, 0, 0 }
2232 static const struct bfd_elf_special_section special_sections_h[] =
2234 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2235 { NULL, 0, 0, 0, 0 }
2238 static const struct bfd_elf_special_section special_sections_i[] =
2240 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2241 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2242 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2243 { NULL, 0, 0, 0, 0 }
2246 static const struct bfd_elf_special_section special_sections_l[] =
2248 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2249 { NULL, 0, 0, 0, 0 }
2252 static const struct bfd_elf_special_section special_sections_n[] =
2254 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2255 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2256 { NULL, 0, 0, 0, 0 }
2259 static const struct bfd_elf_special_section special_sections_p[] =
2261 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2262 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2263 { NULL, 0, 0, 0, 0 }
2266 static const struct bfd_elf_special_section special_sections_r[] =
2268 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2269 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2270 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2271 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2272 { NULL, 0, 0, 0, 0 }
2275 static const struct bfd_elf_special_section special_sections_s[] =
2277 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2278 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2279 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2280 /* See struct bfd_elf_special_section declaration for the semantics of
2281 this special case where .prefix_length != strlen (.prefix). */
2282 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2283 { NULL, 0, 0, 0, 0 }
2286 static const struct bfd_elf_special_section special_sections_t[] =
2288 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2289 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2290 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2291 { NULL, 0, 0, 0, 0 }
2294 static const struct bfd_elf_special_section special_sections_z[] =
2296 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2297 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2298 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2299 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2300 { NULL, 0, 0, 0, 0 }
2303 static const struct bfd_elf_special_section * const special_sections[] =
2305 special_sections_b, /* 'b' */
2306 special_sections_c, /* 'c' */
2307 special_sections_d, /* 'd' */
2309 special_sections_f, /* 'f' */
2310 special_sections_g, /* 'g' */
2311 special_sections_h, /* 'h' */
2312 special_sections_i, /* 'i' */
2315 special_sections_l, /* 'l' */
2317 special_sections_n, /* 'n' */
2319 special_sections_p, /* 'p' */
2321 special_sections_r, /* 'r' */
2322 special_sections_s, /* 's' */
2323 special_sections_t, /* 't' */
2329 special_sections_z /* 'z' */
2332 const struct bfd_elf_special_section *
2333 _bfd_elf_get_special_section (const char *name,
2334 const struct bfd_elf_special_section *spec,
2340 len = strlen (name);
2342 for (i = 0; spec[i].prefix != NULL; i++)
2345 int prefix_len = spec[i].prefix_length;
2347 if (len < prefix_len)
2349 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2352 suffix_len = spec[i].suffix_length;
2353 if (suffix_len <= 0)
2355 if (name[prefix_len] != 0)
2357 if (suffix_len == 0)
2359 if (name[prefix_len] != '.'
2360 && (suffix_len == -2
2361 || (rela && spec[i].type == SHT_REL)))
2367 if (len < prefix_len + suffix_len)
2369 if (memcmp (name + len - suffix_len,
2370 spec[i].prefix + prefix_len,
2380 const struct bfd_elf_special_section *
2381 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2384 const struct bfd_elf_special_section *spec;
2385 const struct elf_backend_data *bed;
2387 /* See if this is one of the special sections. */
2388 if (sec->name == NULL)
2391 bed = get_elf_backend_data (abfd);
2392 spec = bed->special_sections;
2395 spec = _bfd_elf_get_special_section (sec->name,
2396 bed->special_sections,
2402 if (sec->name[0] != '.')
2405 i = sec->name[1] - 'b';
2406 if (i < 0 || i > 'z' - 'b')
2409 spec = special_sections[i];
2414 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2418 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2420 struct bfd_elf_section_data *sdata;
2421 const struct elf_backend_data *bed;
2422 const struct bfd_elf_special_section *ssect;
2424 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2427 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2431 sec->used_by_bfd = sdata;
2434 /* Indicate whether or not this section should use RELA relocations. */
2435 bed = get_elf_backend_data (abfd);
2436 sec->use_rela_p = bed->default_use_rela_p;
2438 /* When we read a file, we don't need to set ELF section type and
2439 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2440 anyway. We will set ELF section type and flags for all linker
2441 created sections. If user specifies BFD section flags, we will
2442 set ELF section type and flags based on BFD section flags in
2443 elf_fake_sections. Special handling for .init_array/.fini_array
2444 output sections since they may contain .ctors/.dtors input
2445 sections. We don't want _bfd_elf_init_private_section_data to
2446 copy ELF section type from .ctors/.dtors input sections. */
2447 if (abfd->direction != read_direction
2448 || (sec->flags & SEC_LINKER_CREATED) != 0)
2450 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2453 || (sec->flags & SEC_LINKER_CREATED) != 0
2454 || ssect->type == SHT_INIT_ARRAY
2455 || ssect->type == SHT_FINI_ARRAY))
2457 elf_section_type (sec) = ssect->type;
2458 elf_section_flags (sec) = ssect->attr;
2462 return _bfd_generic_new_section_hook (abfd, sec);
2465 /* Create a new bfd section from an ELF program header.
2467 Since program segments have no names, we generate a synthetic name
2468 of the form segment<NUM>, where NUM is generally the index in the
2469 program header table. For segments that are split (see below) we
2470 generate the names segment<NUM>a and segment<NUM>b.
2472 Note that some program segments may have a file size that is different than
2473 (less than) the memory size. All this means is that at execution the
2474 system must allocate the amount of memory specified by the memory size,
2475 but only initialize it with the first "file size" bytes read from the
2476 file. This would occur for example, with program segments consisting
2477 of combined data+bss.
2479 To handle the above situation, this routine generates TWO bfd sections
2480 for the single program segment. The first has the length specified by
2481 the file size of the segment, and the second has the length specified
2482 by the difference between the two sizes. In effect, the segment is split
2483 into its initialized and uninitialized parts.
2488 _bfd_elf_make_section_from_phdr (bfd *abfd,
2489 Elf_Internal_Phdr *hdr,
2491 const char *type_name)
2499 split = ((hdr->p_memsz > 0)
2500 && (hdr->p_filesz > 0)
2501 && (hdr->p_memsz > hdr->p_filesz));
2503 if (hdr->p_filesz > 0)
2505 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2506 len = strlen (namebuf) + 1;
2507 name = (char *) bfd_alloc (abfd, len);
2510 memcpy (name, namebuf, len);
2511 newsect = bfd_make_section (abfd, name);
2512 if (newsect == NULL)
2514 newsect->vma = hdr->p_vaddr;
2515 newsect->lma = hdr->p_paddr;
2516 newsect->size = hdr->p_filesz;
2517 newsect->filepos = hdr->p_offset;
2518 newsect->flags |= SEC_HAS_CONTENTS;
2519 newsect->alignment_power = bfd_log2 (hdr->p_align);
2520 if (hdr->p_type == PT_LOAD)
2522 newsect->flags |= SEC_ALLOC;
2523 newsect->flags |= SEC_LOAD;
2524 if (hdr->p_flags & PF_X)
2526 /* FIXME: all we known is that it has execute PERMISSION,
2528 newsect->flags |= SEC_CODE;
2531 if (!(hdr->p_flags & PF_W))
2533 newsect->flags |= SEC_READONLY;
2537 if (hdr->p_memsz > hdr->p_filesz)
2541 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2542 len = strlen (namebuf) + 1;
2543 name = (char *) bfd_alloc (abfd, len);
2546 memcpy (name, namebuf, len);
2547 newsect = bfd_make_section (abfd, name);
2548 if (newsect == NULL)
2550 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2551 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2552 newsect->size = hdr->p_memsz - hdr->p_filesz;
2553 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2554 align = newsect->vma & -newsect->vma;
2555 if (align == 0 || align > hdr->p_align)
2556 align = hdr->p_align;
2557 newsect->alignment_power = bfd_log2 (align);
2558 if (hdr->p_type == PT_LOAD)
2560 /* Hack for gdb. Segments that have not been modified do
2561 not have their contents written to a core file, on the
2562 assumption that a debugger can find the contents in the
2563 executable. We flag this case by setting the fake
2564 section size to zero. Note that "real" bss sections will
2565 always have their contents dumped to the core file. */
2566 if (bfd_get_format (abfd) == bfd_core)
2568 newsect->flags |= SEC_ALLOC;
2569 if (hdr->p_flags & PF_X)
2570 newsect->flags |= SEC_CODE;
2572 if (!(hdr->p_flags & PF_W))
2573 newsect->flags |= SEC_READONLY;
2580 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2582 const struct elf_backend_data *bed;
2584 switch (hdr->p_type)
2587 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2590 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2593 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2596 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2599 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2601 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2606 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2609 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2611 case PT_GNU_EH_FRAME:
2612 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2616 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2619 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2622 /* Check for any processor-specific program segment types. */
2623 bed = get_elf_backend_data (abfd);
2624 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2628 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2632 _bfd_elf_single_rel_hdr (asection *sec)
2634 if (elf_section_data (sec)->rel.hdr)
2636 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2637 return elf_section_data (sec)->rel.hdr;
2640 return elf_section_data (sec)->rela.hdr;
2643 /* Allocate and initialize a section-header for a new reloc section,
2644 containing relocations against ASECT. It is stored in RELDATA. If
2645 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2649 _bfd_elf_init_reloc_shdr (bfd *abfd,
2650 struct bfd_elf_section_reloc_data *reldata,
2652 bfd_boolean use_rela_p)
2654 Elf_Internal_Shdr *rel_hdr;
2656 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2659 amt = sizeof (Elf_Internal_Shdr);
2660 BFD_ASSERT (reldata->hdr == NULL);
2661 rel_hdr = bfd_zalloc (abfd, amt);
2662 reldata->hdr = rel_hdr;
2664 amt = sizeof ".rela" + strlen (asect->name);
2665 name = (char *) bfd_alloc (abfd, amt);
2668 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2670 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2672 if (rel_hdr->sh_name == (unsigned int) -1)
2674 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2675 rel_hdr->sh_entsize = (use_rela_p
2676 ? bed->s->sizeof_rela
2677 : bed->s->sizeof_rel);
2678 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2679 rel_hdr->sh_flags = 0;
2680 rel_hdr->sh_addr = 0;
2681 rel_hdr->sh_size = 0;
2682 rel_hdr->sh_offset = 0;
2687 /* Return the default section type based on the passed in section flags. */
2690 bfd_elf_get_default_section_type (flagword flags)
2692 if ((flags & SEC_ALLOC) != 0
2693 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2695 return SHT_PROGBITS;
2698 struct fake_section_arg
2700 struct bfd_link_info *link_info;
2704 /* Set up an ELF internal section header for a section. */
2707 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2709 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2710 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2711 struct bfd_elf_section_data *esd = elf_section_data (asect);
2712 Elf_Internal_Shdr *this_hdr;
2713 unsigned int sh_type;
2717 /* We already failed; just get out of the bfd_map_over_sections
2722 this_hdr = &esd->this_hdr;
2724 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2725 asect->name, FALSE);
2726 if (this_hdr->sh_name == (unsigned int) -1)
2732 /* Don't clear sh_flags. Assembler may set additional bits. */
2734 if ((asect->flags & SEC_ALLOC) != 0
2735 || asect->user_set_vma)
2736 this_hdr->sh_addr = asect->vma;
2738 this_hdr->sh_addr = 0;
2740 this_hdr->sh_offset = 0;
2741 this_hdr->sh_size = asect->size;
2742 this_hdr->sh_link = 0;
2743 /* PR 17512: file: 0eb809fe, 8b0535ee. */
2744 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
2746 (*_bfd_error_handler)
2747 (_("%B: error: Alignment power %d of section `%A' is too big"),
2748 abfd, asect, asect->alignment_power);
2752 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2753 /* The sh_entsize and sh_info fields may have been set already by
2754 copy_private_section_data. */
2756 this_hdr->bfd_section = asect;
2757 this_hdr->contents = NULL;
2759 /* If the section type is unspecified, we set it based on
2761 if ((asect->flags & SEC_GROUP) != 0)
2762 sh_type = SHT_GROUP;
2764 sh_type = bfd_elf_get_default_section_type (asect->flags);
2766 if (this_hdr->sh_type == SHT_NULL)
2767 this_hdr->sh_type = sh_type;
2768 else if (this_hdr->sh_type == SHT_NOBITS
2769 && sh_type == SHT_PROGBITS
2770 && (asect->flags & SEC_ALLOC) != 0)
2772 /* Warn if we are changing a NOBITS section to PROGBITS, but
2773 allow the link to proceed. This can happen when users link
2774 non-bss input sections to bss output sections, or emit data
2775 to a bss output section via a linker script. */
2776 (*_bfd_error_handler)
2777 (_("warning: section `%A' type changed to PROGBITS"), asect);
2778 this_hdr->sh_type = sh_type;
2781 switch (this_hdr->sh_type)
2787 case SHT_INIT_ARRAY:
2788 case SHT_FINI_ARRAY:
2789 case SHT_PREINIT_ARRAY:
2796 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2800 this_hdr->sh_entsize = bed->s->sizeof_sym;
2804 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2808 if (get_elf_backend_data (abfd)->may_use_rela_p)
2809 this_hdr->sh_entsize = bed->s->sizeof_rela;
2813 if (get_elf_backend_data (abfd)->may_use_rel_p)
2814 this_hdr->sh_entsize = bed->s->sizeof_rel;
2817 case SHT_GNU_versym:
2818 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2821 case SHT_GNU_verdef:
2822 this_hdr->sh_entsize = 0;
2823 /* objcopy or strip will copy over sh_info, but may not set
2824 cverdefs. The linker will set cverdefs, but sh_info will be
2826 if (this_hdr->sh_info == 0)
2827 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2829 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2830 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2833 case SHT_GNU_verneed:
2834 this_hdr->sh_entsize = 0;
2835 /* objcopy or strip will copy over sh_info, but may not set
2836 cverrefs. The linker will set cverrefs, but sh_info will be
2838 if (this_hdr->sh_info == 0)
2839 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2841 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2842 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2846 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2850 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2854 if ((asect->flags & SEC_ALLOC) != 0)
2855 this_hdr->sh_flags |= SHF_ALLOC;
2856 if ((asect->flags & SEC_READONLY) == 0)
2857 this_hdr->sh_flags |= SHF_WRITE;
2858 if ((asect->flags & SEC_CODE) != 0)
2859 this_hdr->sh_flags |= SHF_EXECINSTR;
2860 if ((asect->flags & SEC_MERGE) != 0)
2862 this_hdr->sh_flags |= SHF_MERGE;
2863 this_hdr->sh_entsize = asect->entsize;
2864 if ((asect->flags & SEC_STRINGS) != 0)
2865 this_hdr->sh_flags |= SHF_STRINGS;
2867 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2868 this_hdr->sh_flags |= SHF_GROUP;
2869 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2871 this_hdr->sh_flags |= SHF_TLS;
2872 if (asect->size == 0
2873 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2875 struct bfd_link_order *o = asect->map_tail.link_order;
2877 this_hdr->sh_size = 0;
2880 this_hdr->sh_size = o->offset + o->size;
2881 if (this_hdr->sh_size != 0)
2882 this_hdr->sh_type = SHT_NOBITS;
2886 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2887 this_hdr->sh_flags |= SHF_EXCLUDE;
2889 /* If the section has relocs, set up a section header for the
2890 SHT_REL[A] section. If two relocation sections are required for
2891 this section, it is up to the processor-specific back-end to
2892 create the other. */
2893 if ((asect->flags & SEC_RELOC) != 0)
2895 /* When doing a relocatable link, create both REL and RELA sections if
2898 /* Do the normal setup if we wouldn't create any sections here. */
2899 && esd->rel.count + esd->rela.count > 0
2900 && (arg->link_info->relocatable || arg->link_info->emitrelocations))
2902 if (esd->rel.count && esd->rel.hdr == NULL
2903 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
2908 if (esd->rela.count && esd->rela.hdr == NULL
2909 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
2915 else if (!_bfd_elf_init_reloc_shdr (abfd,
2917 ? &esd->rela : &esd->rel),
2923 /* Check for processor-specific section types. */
2924 sh_type = this_hdr->sh_type;
2925 if (bed->elf_backend_fake_sections
2926 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2929 if (sh_type == SHT_NOBITS && asect->size != 0)
2931 /* Don't change the header type from NOBITS if we are being
2932 called for objcopy --only-keep-debug. */
2933 this_hdr->sh_type = sh_type;
2937 /* Fill in the contents of a SHT_GROUP section. Called from
2938 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2939 when ELF targets use the generic linker, ld. Called for ld -r
2940 from bfd_elf_final_link. */
2943 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2945 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2946 asection *elt, *first;
2950 /* Ignore linker created group section. See elfNN_ia64_object_p in
2952 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2956 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2958 unsigned long symindx = 0;
2960 /* elf_group_id will have been set up by objcopy and the
2962 if (elf_group_id (sec) != NULL)
2963 symindx = elf_group_id (sec)->udata.i;
2967 /* If called from the assembler, swap_out_syms will have set up
2968 elf_section_syms. */
2969 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2970 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2972 elf_section_data (sec)->this_hdr.sh_info = symindx;
2974 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2976 /* The ELF backend linker sets sh_info to -2 when the group
2977 signature symbol is global, and thus the index can't be
2978 set until all local symbols are output. */
2979 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2980 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2981 unsigned long symndx = sec_data->this_hdr.sh_info;
2982 unsigned long extsymoff = 0;
2983 struct elf_link_hash_entry *h;
2985 if (!elf_bad_symtab (igroup->owner))
2987 Elf_Internal_Shdr *symtab_hdr;
2989 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2990 extsymoff = symtab_hdr->sh_info;
2992 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2993 while (h->root.type == bfd_link_hash_indirect
2994 || h->root.type == bfd_link_hash_warning)
2995 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2997 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3000 /* The contents won't be allocated for "ld -r" or objcopy. */
3002 if (sec->contents == NULL)
3005 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3007 /* Arrange for the section to be written out. */
3008 elf_section_data (sec)->this_hdr.contents = sec->contents;
3009 if (sec->contents == NULL)
3016 loc = sec->contents + sec->size;
3018 /* Get the pointer to the first section in the group that gas
3019 squirreled away here. objcopy arranges for this to be set to the
3020 start of the input section group. */
3021 first = elt = elf_next_in_group (sec);
3023 /* First element is a flag word. Rest of section is elf section
3024 indices for all the sections of the group. Write them backwards
3025 just to keep the group in the same order as given in .section
3026 directives, not that it matters. */
3033 s = s->output_section;
3035 && !bfd_is_abs_section (s))
3037 unsigned int idx = elf_section_data (s)->this_idx;
3040 H_PUT_32 (abfd, idx, loc);
3042 elt = elf_next_in_group (elt);
3047 if ((loc -= 4) != sec->contents)
3050 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3053 /* Assign all ELF section numbers. The dummy first section is handled here
3054 too. The link/info pointers for the standard section types are filled
3055 in here too, while we're at it. */
3058 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3060 struct elf_obj_tdata *t = elf_tdata (abfd);
3062 unsigned int section_number, secn;
3063 Elf_Internal_Shdr **i_shdrp;
3064 struct bfd_elf_section_data *d;
3065 bfd_boolean need_symtab;
3069 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3071 /* SHT_GROUP sections are in relocatable files only. */
3072 if (link_info == NULL || link_info->relocatable)
3074 /* Put SHT_GROUP sections first. */
3075 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3077 d = elf_section_data (sec);
3079 if (d->this_hdr.sh_type == SHT_GROUP)
3081 if (sec->flags & SEC_LINKER_CREATED)
3083 /* Remove the linker created SHT_GROUP sections. */
3084 bfd_section_list_remove (abfd, sec);
3085 abfd->section_count--;
3088 d->this_idx = section_number++;
3093 for (sec = abfd->sections; sec; sec = sec->next)
3095 d = elf_section_data (sec);
3097 if (d->this_hdr.sh_type != SHT_GROUP)
3098 d->this_idx = section_number++;
3099 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3102 d->rel.idx = section_number++;
3103 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3110 d->rela.idx = section_number++;
3111 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3117 elf_shstrtab_sec (abfd) = section_number++;
3118 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3119 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3121 need_symtab = (bfd_get_symcount (abfd) > 0
3122 || (link_info == NULL
3123 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3127 elf_onesymtab (abfd) = section_number++;
3128 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3129 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3131 elf_symtab_shndx (abfd) = section_number++;
3132 t->symtab_shndx_hdr.sh_name
3133 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3134 ".symtab_shndx", FALSE);
3135 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3138 elf_strtab_sec (abfd) = section_number++;
3139 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3142 if (section_number >= SHN_LORESERVE)
3144 _bfd_error_handler (_("%B: too many sections: %u"),
3145 abfd, section_number);
3149 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3150 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3152 elf_numsections (abfd) = section_number;
3153 elf_elfheader (abfd)->e_shnum = section_number;
3155 /* Set up the list of section header pointers, in agreement with the
3157 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3158 sizeof (Elf_Internal_Shdr *));
3159 if (i_shdrp == NULL)
3162 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3163 sizeof (Elf_Internal_Shdr));
3164 if (i_shdrp[0] == NULL)
3166 bfd_release (abfd, i_shdrp);
3170 elf_elfsections (abfd) = i_shdrp;
3172 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3175 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3176 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3178 i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
3179 t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
3181 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3182 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3185 for (sec = abfd->sections; sec; sec = sec->next)
3190 d = elf_section_data (sec);
3192 i_shdrp[d->this_idx] = &d->this_hdr;
3193 if (d->rel.idx != 0)
3194 i_shdrp[d->rel.idx] = d->rel.hdr;
3195 if (d->rela.idx != 0)
3196 i_shdrp[d->rela.idx] = d->rela.hdr;
3198 /* Fill in the sh_link and sh_info fields while we're at it. */
3200 /* sh_link of a reloc section is the section index of the symbol
3201 table. sh_info is the section index of the section to which
3202 the relocation entries apply. */
3203 if (d->rel.idx != 0)
3205 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3206 d->rel.hdr->sh_info = d->this_idx;
3207 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3209 if (d->rela.idx != 0)
3211 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3212 d->rela.hdr->sh_info = d->this_idx;
3213 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3216 /* We need to set up sh_link for SHF_LINK_ORDER. */
3217 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3219 s = elf_linked_to_section (sec);
3222 /* elf_linked_to_section points to the input section. */
3223 if (link_info != NULL)
3225 /* Check discarded linkonce section. */
3226 if (discarded_section (s))
3229 (*_bfd_error_handler)
3230 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3231 abfd, d->this_hdr.bfd_section,
3233 /* Point to the kept section if it has the same
3234 size as the discarded one. */
3235 kept = _bfd_elf_check_kept_section (s, link_info);
3238 bfd_set_error (bfd_error_bad_value);
3244 s = s->output_section;
3245 BFD_ASSERT (s != NULL);
3249 /* Handle objcopy. */
3250 if (s->output_section == NULL)
3252 (*_bfd_error_handler)
3253 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3254 abfd, d->this_hdr.bfd_section, s, s->owner);
3255 bfd_set_error (bfd_error_bad_value);
3258 s = s->output_section;
3260 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3265 The Intel C compiler generates SHT_IA_64_UNWIND with
3266 SHF_LINK_ORDER. But it doesn't set the sh_link or
3267 sh_info fields. Hence we could get the situation
3269 const struct elf_backend_data *bed
3270 = get_elf_backend_data (abfd);
3271 if (bed->link_order_error_handler)
3272 bed->link_order_error_handler
3273 (_("%B: warning: sh_link not set for section `%A'"),
3278 switch (d->this_hdr.sh_type)
3282 /* A reloc section which we are treating as a normal BFD
3283 section. sh_link is the section index of the symbol
3284 table. sh_info is the section index of the section to
3285 which the relocation entries apply. We assume that an
3286 allocated reloc section uses the dynamic symbol table.
3287 FIXME: How can we be sure? */
3288 s = bfd_get_section_by_name (abfd, ".dynsym");
3290 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3292 /* We look up the section the relocs apply to by name. */
3294 if (d->this_hdr.sh_type == SHT_REL)
3298 s = bfd_get_section_by_name (abfd, name);
3301 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3302 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3307 /* We assume that a section named .stab*str is a stabs
3308 string section. We look for a section with the same name
3309 but without the trailing ``str'', and set its sh_link
3310 field to point to this section. */
3311 if (CONST_STRNEQ (sec->name, ".stab")
3312 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3317 len = strlen (sec->name);
3318 alc = (char *) bfd_malloc (len - 2);
3321 memcpy (alc, sec->name, len - 3);
3322 alc[len - 3] = '\0';
3323 s = bfd_get_section_by_name (abfd, alc);
3327 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3329 /* This is a .stab section. */
3330 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3331 elf_section_data (s)->this_hdr.sh_entsize
3332 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3339 case SHT_GNU_verneed:
3340 case SHT_GNU_verdef:
3341 /* sh_link is the section header index of the string table
3342 used for the dynamic entries, or the symbol table, or the
3344 s = bfd_get_section_by_name (abfd, ".dynstr");
3346 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3349 case SHT_GNU_LIBLIST:
3350 /* sh_link is the section header index of the prelink library
3351 list used for the dynamic entries, or the symbol table, or
3352 the version strings. */
3353 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3354 ? ".dynstr" : ".gnu.libstr");
3356 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3361 case SHT_GNU_versym:
3362 /* sh_link is the section header index of the symbol table
3363 this hash table or version table is for. */
3364 s = bfd_get_section_by_name (abfd, ".dynsym");
3366 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3370 d->this_hdr.sh_link = elf_onesymtab (abfd);
3374 for (secn = 1; secn < section_number; ++secn)
3375 if (i_shdrp[secn] == NULL)
3376 i_shdrp[secn] = i_shdrp[0];
3378 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3379 i_shdrp[secn]->sh_name);
3384 sym_is_global (bfd *abfd, asymbol *sym)
3386 /* If the backend has a special mapping, use it. */
3387 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3388 if (bed->elf_backend_sym_is_global)
3389 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3391 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3392 || bfd_is_und_section (bfd_get_section (sym))
3393 || bfd_is_com_section (bfd_get_section (sym)));
3396 /* Don't output section symbols for sections that are not going to be
3397 output, that are duplicates or there is no BFD section. */
3400 ignore_section_sym (bfd *abfd, asymbol *sym)
3402 elf_symbol_type *type_ptr;
3404 if ((sym->flags & BSF_SECTION_SYM) == 0)
3407 type_ptr = elf_symbol_from (abfd, sym);
3408 return ((type_ptr != NULL
3409 && type_ptr->internal_elf_sym.st_shndx != 0
3410 && bfd_is_abs_section (sym->section))
3411 || !(sym->section->owner == abfd
3412 || (sym->section->output_section->owner == abfd
3413 && sym->section->output_offset == 0)
3414 || bfd_is_abs_section (sym->section)));
3417 /* Map symbol from it's internal number to the external number, moving
3418 all local symbols to be at the head of the list. */
3421 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3423 unsigned int symcount = bfd_get_symcount (abfd);
3424 asymbol **syms = bfd_get_outsymbols (abfd);
3425 asymbol **sect_syms;
3426 unsigned int num_locals = 0;
3427 unsigned int num_globals = 0;
3428 unsigned int num_locals2 = 0;
3429 unsigned int num_globals2 = 0;
3436 fprintf (stderr, "elf_map_symbols\n");
3440 for (asect = abfd->sections; asect; asect = asect->next)
3442 if (max_index < asect->index)
3443 max_index = asect->index;
3447 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3448 if (sect_syms == NULL)
3450 elf_section_syms (abfd) = sect_syms;
3451 elf_num_section_syms (abfd) = max_index;
3453 /* Init sect_syms entries for any section symbols we have already
3454 decided to output. */
3455 for (idx = 0; idx < symcount; idx++)
3457 asymbol *sym = syms[idx];
3459 if ((sym->flags & BSF_SECTION_SYM) != 0
3461 && !ignore_section_sym (abfd, sym)
3462 && !bfd_is_abs_section (sym->section))
3464 asection *sec = sym->section;
3466 if (sec->owner != abfd)
3467 sec = sec->output_section;
3469 sect_syms[sec->index] = syms[idx];
3473 /* Classify all of the symbols. */
3474 for (idx = 0; idx < symcount; idx++)
3476 if (sym_is_global (abfd, syms[idx]))
3478 else if (!ignore_section_sym (abfd, syms[idx]))
3482 /* We will be adding a section symbol for each normal BFD section. Most
3483 sections will already have a section symbol in outsymbols, but
3484 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3485 at least in that case. */
3486 for (asect = abfd->sections; asect; asect = asect->next)
3488 if (sect_syms[asect->index] == NULL)
3490 if (!sym_is_global (abfd, asect->symbol))
3497 /* Now sort the symbols so the local symbols are first. */
3498 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3499 sizeof (asymbol *));
3501 if (new_syms == NULL)
3504 for (idx = 0; idx < symcount; idx++)
3506 asymbol *sym = syms[idx];
3509 if (sym_is_global (abfd, sym))
3510 i = num_locals + num_globals2++;
3511 else if (!ignore_section_sym (abfd, sym))
3516 sym->udata.i = i + 1;
3518 for (asect = abfd->sections; asect; asect = asect->next)
3520 if (sect_syms[asect->index] == NULL)
3522 asymbol *sym = asect->symbol;
3525 sect_syms[asect->index] = sym;
3526 if (!sym_is_global (abfd, sym))
3529 i = num_locals + num_globals2++;
3531 sym->udata.i = i + 1;
3535 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3537 *pnum_locals = num_locals;
3541 /* Align to the maximum file alignment that could be required for any
3542 ELF data structure. */
3544 static inline file_ptr
3545 align_file_position (file_ptr off, int align)
3547 return (off + align - 1) & ~(align - 1);
3550 /* Assign a file position to a section, optionally aligning to the
3551 required section alignment. */
3554 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3558 if (align && i_shdrp->sh_addralign > 1)
3559 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3560 i_shdrp->sh_offset = offset;
3561 if (i_shdrp->bfd_section != NULL)
3562 i_shdrp->bfd_section->filepos = offset;
3563 if (i_shdrp->sh_type != SHT_NOBITS)
3564 offset += i_shdrp->sh_size;
3568 /* Compute the file positions we are going to put the sections at, and
3569 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3570 is not NULL, this is being called by the ELF backend linker. */
3573 _bfd_elf_compute_section_file_positions (bfd *abfd,
3574 struct bfd_link_info *link_info)
3576 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3577 struct fake_section_arg fsargs;
3579 struct bfd_strtab_hash *strtab = NULL;
3580 Elf_Internal_Shdr *shstrtab_hdr;
3581 bfd_boolean need_symtab;
3583 if (abfd->output_has_begun)
3586 /* Do any elf backend specific processing first. */
3587 if (bed->elf_backend_begin_write_processing)
3588 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3590 if (! prep_headers (abfd))
3593 /* Post process the headers if necessary. */
3594 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3596 fsargs.failed = FALSE;
3597 fsargs.link_info = link_info;
3598 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3602 if (!assign_section_numbers (abfd, link_info))
3605 /* The backend linker builds symbol table information itself. */
3606 need_symtab = (link_info == NULL
3607 && (bfd_get_symcount (abfd) > 0
3608 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3612 /* Non-zero if doing a relocatable link. */
3613 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3615 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3620 if (link_info == NULL)
3622 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3627 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3628 /* sh_name was set in prep_headers. */
3629 shstrtab_hdr->sh_type = SHT_STRTAB;
3630 shstrtab_hdr->sh_flags = 0;
3631 shstrtab_hdr->sh_addr = 0;
3632 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3633 shstrtab_hdr->sh_entsize = 0;
3634 shstrtab_hdr->sh_link = 0;
3635 shstrtab_hdr->sh_info = 0;
3636 /* sh_offset is set in assign_file_positions_except_relocs. */
3637 shstrtab_hdr->sh_addralign = 1;
3639 if (!assign_file_positions_except_relocs (abfd, link_info))
3645 Elf_Internal_Shdr *hdr;
3647 off = elf_next_file_pos (abfd);
3649 hdr = &elf_tdata (abfd)->symtab_hdr;
3650 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3652 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3653 if (hdr->sh_size != 0)
3654 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3656 hdr = &elf_tdata (abfd)->strtab_hdr;
3657 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3659 elf_next_file_pos (abfd) = off;
3661 /* Now that we know where the .strtab section goes, write it
3663 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3664 || ! _bfd_stringtab_emit (abfd, strtab))
3666 _bfd_stringtab_free (strtab);
3669 abfd->output_has_begun = TRUE;
3674 /* Make an initial estimate of the size of the program header. If we
3675 get the number wrong here, we'll redo section placement. */
3677 static bfd_size_type
3678 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3682 const struct elf_backend_data *bed;
3684 /* Assume we will need exactly two PT_LOAD segments: one for text
3685 and one for data. */
3688 s = bfd_get_section_by_name (abfd, ".interp");
3689 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3691 /* If we have a loadable interpreter section, we need a
3692 PT_INTERP segment. In this case, assume we also need a
3693 PT_PHDR segment, although that may not be true for all
3698 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3700 /* We need a PT_DYNAMIC segment. */
3704 if (info != NULL && info->relro)
3706 /* We need a PT_GNU_RELRO segment. */
3710 if (elf_eh_frame_hdr (abfd))
3712 /* We need a PT_GNU_EH_FRAME segment. */
3716 if (elf_stack_flags (abfd))
3718 /* We need a PT_GNU_STACK segment. */
3722 for (s = abfd->sections; s != NULL; s = s->next)
3724 if ((s->flags & SEC_LOAD) != 0
3725 && CONST_STRNEQ (s->name, ".note"))
3727 /* We need a PT_NOTE segment. */
3729 /* Try to create just one PT_NOTE segment
3730 for all adjacent loadable .note* sections.
3731 gABI requires that within a PT_NOTE segment
3732 (and also inside of each SHT_NOTE section)
3733 each note is padded to a multiple of 4 size,
3734 so we check whether the sections are correctly
3736 if (s->alignment_power == 2)
3737 while (s->next != NULL
3738 && s->next->alignment_power == 2
3739 && (s->next->flags & SEC_LOAD) != 0
3740 && CONST_STRNEQ (s->next->name, ".note"))
3745 for (s = abfd->sections; s != NULL; s = s->next)
3747 if (s->flags & SEC_THREAD_LOCAL)
3749 /* We need a PT_TLS segment. */
3755 /* Let the backend count up any program headers it might need. */
3756 bed = get_elf_backend_data (abfd);
3757 if (bed->elf_backend_additional_program_headers)
3761 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3767 return segs * bed->s->sizeof_phdr;
3770 /* Find the segment that contains the output_section of section. */
3773 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3775 struct elf_segment_map *m;
3776 Elf_Internal_Phdr *p;
3778 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
3784 for (i = m->count - 1; i >= 0; i--)
3785 if (m->sections[i] == section)
3792 /* Create a mapping from a set of sections to a program segment. */
3794 static struct elf_segment_map *
3795 make_mapping (bfd *abfd,
3796 asection **sections,
3801 struct elf_segment_map *m;
3806 amt = sizeof (struct elf_segment_map);
3807 amt += (to - from - 1) * sizeof (asection *);
3808 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3812 m->p_type = PT_LOAD;
3813 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3814 m->sections[i - from] = *hdrpp;
3815 m->count = to - from;
3817 if (from == 0 && phdr)
3819 /* Include the headers in the first PT_LOAD segment. */
3820 m->includes_filehdr = 1;
3821 m->includes_phdrs = 1;
3827 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3830 struct elf_segment_map *
3831 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3833 struct elf_segment_map *m;
3835 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3836 sizeof (struct elf_segment_map));
3840 m->p_type = PT_DYNAMIC;
3842 m->sections[0] = dynsec;
3847 /* Possibly add or remove segments from the segment map. */
3850 elf_modify_segment_map (bfd *abfd,
3851 struct bfd_link_info *info,
3852 bfd_boolean remove_empty_load)
3854 struct elf_segment_map **m;
3855 const struct elf_backend_data *bed;
3857 /* The placement algorithm assumes that non allocated sections are
3858 not in PT_LOAD segments. We ensure this here by removing such
3859 sections from the segment map. We also remove excluded
3860 sections. Finally, any PT_LOAD segment without sections is
3862 m = &elf_seg_map (abfd);
3865 unsigned int i, new_count;
3867 for (new_count = 0, i = 0; i < (*m)->count; i++)
3869 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3870 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3871 || (*m)->p_type != PT_LOAD))
3873 (*m)->sections[new_count] = (*m)->sections[i];
3877 (*m)->count = new_count;
3879 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3885 bed = get_elf_backend_data (abfd);
3886 if (bed->elf_backend_modify_segment_map != NULL)
3888 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3895 /* Set up a mapping from BFD sections to program segments. */
3898 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3901 struct elf_segment_map *m;
3902 asection **sections = NULL;
3903 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3904 bfd_boolean no_user_phdrs;
3906 no_user_phdrs = elf_seg_map (abfd) == NULL;
3909 info->user_phdrs = !no_user_phdrs;
3911 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3915 struct elf_segment_map *mfirst;
3916 struct elf_segment_map **pm;
3919 unsigned int phdr_index;
3920 bfd_vma maxpagesize;
3922 bfd_boolean phdr_in_segment = TRUE;
3923 bfd_boolean writable;
3925 asection *first_tls = NULL;
3926 asection *dynsec, *eh_frame_hdr;
3928 bfd_vma addr_mask, wrap_to = 0;
3930 /* Select the allocated sections, and sort them. */
3932 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3933 sizeof (asection *));
3934 if (sections == NULL)
3937 /* Calculate top address, avoiding undefined behaviour of shift
3938 left operator when shift count is equal to size of type
3940 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3941 addr_mask = (addr_mask << 1) + 1;
3944 for (s = abfd->sections; s != NULL; s = s->next)
3946 if ((s->flags & SEC_ALLOC) != 0)
3950 /* A wrapping section potentially clashes with header. */
3951 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3952 wrap_to = (s->lma + s->size) & addr_mask;
3955 BFD_ASSERT (i <= bfd_count_sections (abfd));
3958 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3960 /* Build the mapping. */
3965 /* If we have a .interp section, then create a PT_PHDR segment for
3966 the program headers and a PT_INTERP segment for the .interp
3968 s = bfd_get_section_by_name (abfd, ".interp");
3969 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3971 amt = sizeof (struct elf_segment_map);
3972 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3976 m->p_type = PT_PHDR;
3977 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3978 m->p_flags = PF_R | PF_X;
3979 m->p_flags_valid = 1;
3980 m->includes_phdrs = 1;
3985 amt = sizeof (struct elf_segment_map);
3986 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3990 m->p_type = PT_INTERP;
3998 /* Look through the sections. We put sections in the same program
3999 segment when the start of the second section can be placed within
4000 a few bytes of the end of the first section. */
4004 maxpagesize = bed->maxpagesize;
4005 /* PR 17512: file: c8455299.
4006 Avoid divide-by-zero errors later on.
4007 FIXME: Should we abort if the maxpagesize is zero ? */
4008 if (maxpagesize == 0)
4011 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4013 && (dynsec->flags & SEC_LOAD) == 0)
4016 /* Deal with -Ttext or something similar such that the first section
4017 is not adjacent to the program headers. This is an
4018 approximation, since at this point we don't know exactly how many
4019 program headers we will need. */
4022 bfd_size_type phdr_size = elf_program_header_size (abfd);
4024 if (phdr_size == (bfd_size_type) -1)
4025 phdr_size = get_program_header_size (abfd, info);
4026 phdr_size += bed->s->sizeof_ehdr;
4027 if ((abfd->flags & D_PAGED) == 0
4028 || (sections[0]->lma & addr_mask) < phdr_size
4029 || ((sections[0]->lma & addr_mask) % maxpagesize
4030 < phdr_size % maxpagesize)
4031 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
4032 phdr_in_segment = FALSE;
4035 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4038 bfd_boolean new_segment;
4042 /* See if this section and the last one will fit in the same
4045 if (last_hdr == NULL)
4047 /* If we don't have a segment yet, then we don't need a new
4048 one (we build the last one after this loop). */
4049 new_segment = FALSE;
4051 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4053 /* If this section has a different relation between the
4054 virtual address and the load address, then we need a new
4058 else if (hdr->lma < last_hdr->lma + last_size
4059 || last_hdr->lma + last_size < last_hdr->lma)
4061 /* If this section has a load address that makes it overlap
4062 the previous section, then we need a new segment. */
4065 /* In the next test we have to be careful when last_hdr->lma is close
4066 to the end of the address space. If the aligned address wraps
4067 around to the start of the address space, then there are no more
4068 pages left in memory and it is OK to assume that the current
4069 section can be included in the current segment. */
4070 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4072 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4075 /* If putting this section in this segment would force us to
4076 skip a page in the segment, then we need a new segment. */
4079 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4080 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4082 /* We don't want to put a loadable section after a
4083 nonloadable section in the same segment.
4084 Consider .tbss sections as loadable for this purpose. */
4087 else if ((abfd->flags & D_PAGED) == 0)
4089 /* If the file is not demand paged, which means that we
4090 don't require the sections to be correctly aligned in the
4091 file, then there is no other reason for a new segment. */
4092 new_segment = FALSE;
4095 && (hdr->flags & SEC_READONLY) == 0
4096 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4097 != (hdr->lma & -maxpagesize)))
4099 /* We don't want to put a writable section in a read only
4100 segment, unless they are on the same page in memory
4101 anyhow. We already know that the last section does not
4102 bring us past the current section on the page, so the
4103 only case in which the new section is not on the same
4104 page as the previous section is when the previous section
4105 ends precisely on a page boundary. */
4110 /* Otherwise, we can use the same segment. */
4111 new_segment = FALSE;
4114 /* Allow interested parties a chance to override our decision. */
4115 if (last_hdr != NULL
4117 && info->callbacks->override_segment_assignment != NULL)
4119 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4125 if ((hdr->flags & SEC_READONLY) == 0)
4128 /* .tbss sections effectively have zero size. */
4129 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4130 != SEC_THREAD_LOCAL)
4131 last_size = hdr->size;
4137 /* We need a new program segment. We must create a new program
4138 header holding all the sections from phdr_index until hdr. */
4140 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4147 if ((hdr->flags & SEC_READONLY) == 0)
4153 /* .tbss sections effectively have zero size. */
4154 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4155 last_size = hdr->size;
4159 phdr_in_segment = FALSE;
4162 /* Create a final PT_LOAD program segment, but not if it's just
4164 if (last_hdr != NULL
4165 && (i - phdr_index != 1
4166 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4167 != SEC_THREAD_LOCAL)))
4169 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4177 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4180 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4187 /* For each batch of consecutive loadable .note sections,
4188 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4189 because if we link together nonloadable .note sections and
4190 loadable .note sections, we will generate two .note sections
4191 in the output file. FIXME: Using names for section types is
4193 for (s = abfd->sections; s != NULL; s = s->next)
4195 if ((s->flags & SEC_LOAD) != 0
4196 && CONST_STRNEQ (s->name, ".note"))
4201 amt = sizeof (struct elf_segment_map);
4202 if (s->alignment_power == 2)
4203 for (s2 = s; s2->next != NULL; s2 = s2->next)
4205 if (s2->next->alignment_power == 2
4206 && (s2->next->flags & SEC_LOAD) != 0
4207 && CONST_STRNEQ (s2->next->name, ".note")
4208 && align_power (s2->lma + s2->size, 2)
4214 amt += (count - 1) * sizeof (asection *);
4215 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4219 m->p_type = PT_NOTE;
4223 m->sections[m->count - count--] = s;
4224 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4227 m->sections[m->count - 1] = s;
4228 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4232 if (s->flags & SEC_THREAD_LOCAL)
4240 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4243 amt = sizeof (struct elf_segment_map);
4244 amt += (tls_count - 1) * sizeof (asection *);
4245 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4250 m->count = tls_count;
4251 /* Mandated PF_R. */
4253 m->p_flags_valid = 1;
4255 for (i = 0; i < (unsigned int) tls_count; ++i)
4257 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4260 (_("%B: TLS sections are not adjacent:"), abfd);
4263 while (i < (unsigned int) tls_count)
4265 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4267 _bfd_error_handler (_(" TLS: %A"), s);
4271 _bfd_error_handler (_(" non-TLS: %A"), s);
4274 bfd_set_error (bfd_error_bad_value);
4285 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4287 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4288 if (eh_frame_hdr != NULL
4289 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4291 amt = sizeof (struct elf_segment_map);
4292 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4296 m->p_type = PT_GNU_EH_FRAME;
4298 m->sections[0] = eh_frame_hdr->output_section;
4304 if (elf_stack_flags (abfd))
4306 amt = sizeof (struct elf_segment_map);
4307 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4311 m->p_type = PT_GNU_STACK;
4312 m->p_flags = elf_stack_flags (abfd);
4313 m->p_align = bed->stack_align;
4314 m->p_flags_valid = 1;
4315 m->p_align_valid = m->p_align != 0;
4316 if (info->stacksize > 0)
4318 m->p_size = info->stacksize;
4319 m->p_size_valid = 1;
4326 if (info != NULL && info->relro)
4328 for (m = mfirst; m != NULL; m = m->next)
4330 if (m->p_type == PT_LOAD
4332 && m->sections[0]->vma >= info->relro_start
4333 && m->sections[0]->vma < info->relro_end)
4336 while (--i != (unsigned) -1)
4337 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4338 == (SEC_LOAD | SEC_HAS_CONTENTS))
4341 if (i != (unsigned) -1)
4346 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4349 amt = sizeof (struct elf_segment_map);
4350 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4354 m->p_type = PT_GNU_RELRO;
4356 m->p_flags_valid = 1;
4364 elf_seg_map (abfd) = mfirst;
4367 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4370 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4372 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4377 if (sections != NULL)
4382 /* Sort sections by address. */
4385 elf_sort_sections (const void *arg1, const void *arg2)
4387 const asection *sec1 = *(const asection **) arg1;
4388 const asection *sec2 = *(const asection **) arg2;
4389 bfd_size_type size1, size2;
4391 /* Sort by LMA first, since this is the address used to
4392 place the section into a segment. */
4393 if (sec1->lma < sec2->lma)
4395 else if (sec1->lma > sec2->lma)
4398 /* Then sort by VMA. Normally the LMA and the VMA will be
4399 the same, and this will do nothing. */
4400 if (sec1->vma < sec2->vma)
4402 else if (sec1->vma > sec2->vma)
4405 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4407 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4413 /* If the indicies are the same, do not return 0
4414 here, but continue to try the next comparison. */
4415 if (sec1->target_index - sec2->target_index != 0)
4416 return sec1->target_index - sec2->target_index;
4421 else if (TOEND (sec2))
4426 /* Sort by size, to put zero sized sections
4427 before others at the same address. */
4429 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4430 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4437 return sec1->target_index - sec2->target_index;
4440 /* Ian Lance Taylor writes:
4442 We shouldn't be using % with a negative signed number. That's just
4443 not good. We have to make sure either that the number is not
4444 negative, or that the number has an unsigned type. When the types
4445 are all the same size they wind up as unsigned. When file_ptr is a
4446 larger signed type, the arithmetic winds up as signed long long,
4449 What we're trying to say here is something like ``increase OFF by
4450 the least amount that will cause it to be equal to the VMA modulo
4452 /* In other words, something like:
4454 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4455 off_offset = off % bed->maxpagesize;
4456 if (vma_offset < off_offset)
4457 adjustment = vma_offset + bed->maxpagesize - off_offset;
4459 adjustment = vma_offset - off_offset;
4461 which can can be collapsed into the expression below. */
4464 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4466 /* PR binutils/16199: Handle an alignment of zero. */
4467 if (maxpagesize == 0)
4469 return ((vma - off) % maxpagesize);
4473 print_segment_map (const struct elf_segment_map *m)
4476 const char *pt = get_segment_type (m->p_type);
4481 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4482 sprintf (buf, "LOPROC+%7.7x",
4483 (unsigned int) (m->p_type - PT_LOPROC));
4484 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4485 sprintf (buf, "LOOS+%7.7x",
4486 (unsigned int) (m->p_type - PT_LOOS));
4488 snprintf (buf, sizeof (buf), "%8.8x",
4489 (unsigned int) m->p_type);
4493 fprintf (stderr, "%s:", pt);
4494 for (j = 0; j < m->count; j++)
4495 fprintf (stderr, " %s", m->sections [j]->name);
4501 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4506 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4508 buf = bfd_zmalloc (len);
4511 ret = bfd_bwrite (buf, len, abfd) == len;
4516 /* Assign file positions to the sections based on the mapping from
4517 sections to segments. This function also sets up some fields in
4521 assign_file_positions_for_load_sections (bfd *abfd,
4522 struct bfd_link_info *link_info)
4524 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4525 struct elf_segment_map *m;
4526 Elf_Internal_Phdr *phdrs;
4527 Elf_Internal_Phdr *p;
4529 bfd_size_type maxpagesize;
4532 bfd_vma header_pad = 0;
4534 if (link_info == NULL
4535 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4539 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4543 header_pad = m->header_size;
4548 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4549 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4553 /* PR binutils/12467. */
4554 elf_elfheader (abfd)->e_phoff = 0;
4555 elf_elfheader (abfd)->e_phentsize = 0;
4558 elf_elfheader (abfd)->e_phnum = alloc;
4560 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4561 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4563 BFD_ASSERT (elf_program_header_size (abfd)
4564 >= alloc * bed->s->sizeof_phdr);
4568 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4572 /* We're writing the size in elf_program_header_size (abfd),
4573 see assign_file_positions_except_relocs, so make sure we have
4574 that amount allocated, with trailing space cleared.
4575 The variable alloc contains the computed need, while
4576 elf_program_header_size (abfd) contains the size used for the
4578 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4579 where the layout is forced to according to a larger size in the
4580 last iterations for the testcase ld-elf/header. */
4581 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4583 phdrs = (Elf_Internal_Phdr *)
4585 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4586 sizeof (Elf_Internal_Phdr));
4587 elf_tdata (abfd)->phdr = phdrs;
4592 if ((abfd->flags & D_PAGED) != 0)
4593 maxpagesize = bed->maxpagesize;
4595 off = bed->s->sizeof_ehdr;
4596 off += alloc * bed->s->sizeof_phdr;
4597 if (header_pad < (bfd_vma) off)
4603 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4605 m = m->next, p++, j++)
4609 bfd_boolean no_contents;
4611 /* If elf_segment_map is not from map_sections_to_segments, the
4612 sections may not be correctly ordered. NOTE: sorting should
4613 not be done to the PT_NOTE section of a corefile, which may
4614 contain several pseudo-sections artificially created by bfd.
4615 Sorting these pseudo-sections breaks things badly. */
4617 && !(elf_elfheader (abfd)->e_type == ET_CORE
4618 && m->p_type == PT_NOTE))
4619 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4622 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4623 number of sections with contents contributing to both p_filesz
4624 and p_memsz, followed by a number of sections with no contents
4625 that just contribute to p_memsz. In this loop, OFF tracks next
4626 available file offset for PT_LOAD and PT_NOTE segments. */
4627 p->p_type = m->p_type;
4628 p->p_flags = m->p_flags;
4633 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4635 if (m->p_paddr_valid)
4636 p->p_paddr = m->p_paddr;
4637 else if (m->count == 0)
4640 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4642 if (p->p_type == PT_LOAD
4643 && (abfd->flags & D_PAGED) != 0)
4645 /* p_align in demand paged PT_LOAD segments effectively stores
4646 the maximum page size. When copying an executable with
4647 objcopy, we set m->p_align from the input file. Use this
4648 value for maxpagesize rather than bed->maxpagesize, which
4649 may be different. Note that we use maxpagesize for PT_TLS
4650 segment alignment later in this function, so we are relying
4651 on at least one PT_LOAD segment appearing before a PT_TLS
4653 if (m->p_align_valid)
4654 maxpagesize = m->p_align;
4656 p->p_align = maxpagesize;
4658 else if (m->p_align_valid)
4659 p->p_align = m->p_align;
4660 else if (m->count == 0)
4661 p->p_align = 1 << bed->s->log_file_align;
4665 no_contents = FALSE;
4667 if (p->p_type == PT_LOAD
4670 bfd_size_type align;
4671 unsigned int align_power = 0;
4673 if (m->p_align_valid)
4677 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4679 unsigned int secalign;
4681 secalign = bfd_get_section_alignment (abfd, *secpp);
4682 if (secalign > align_power)
4683 align_power = secalign;
4685 align = (bfd_size_type) 1 << align_power;
4686 if (align < maxpagesize)
4687 align = maxpagesize;
4690 for (i = 0; i < m->count; i++)
4691 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4692 /* If we aren't making room for this section, then
4693 it must be SHT_NOBITS regardless of what we've
4694 set via struct bfd_elf_special_section. */
4695 elf_section_type (m->sections[i]) = SHT_NOBITS;
4697 /* Find out whether this segment contains any loadable
4700 for (i = 0; i < m->count; i++)
4701 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4703 no_contents = FALSE;
4707 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4711 /* We shouldn't need to align the segment on disk since
4712 the segment doesn't need file space, but the gABI
4713 arguably requires the alignment and glibc ld.so
4714 checks it. So to comply with the alignment
4715 requirement but not waste file space, we adjust
4716 p_offset for just this segment. (OFF_ADJUST is
4717 subtracted from OFF later.) This may put p_offset
4718 past the end of file, but that shouldn't matter. */
4723 /* Make sure the .dynamic section is the first section in the
4724 PT_DYNAMIC segment. */
4725 else if (p->p_type == PT_DYNAMIC
4727 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4730 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4732 bfd_set_error (bfd_error_bad_value);
4735 /* Set the note section type to SHT_NOTE. */
4736 else if (p->p_type == PT_NOTE)
4737 for (i = 0; i < m->count; i++)
4738 elf_section_type (m->sections[i]) = SHT_NOTE;
4744 if (m->includes_filehdr)
4746 if (!m->p_flags_valid)
4748 p->p_filesz = bed->s->sizeof_ehdr;
4749 p->p_memsz = bed->s->sizeof_ehdr;
4752 if (p->p_vaddr < (bfd_vma) off)
4754 (*_bfd_error_handler)
4755 (_("%B: Not enough room for program headers, try linking with -N"),
4757 bfd_set_error (bfd_error_bad_value);
4762 if (!m->p_paddr_valid)
4767 if (m->includes_phdrs)
4769 if (!m->p_flags_valid)
4772 if (!m->includes_filehdr)
4774 p->p_offset = bed->s->sizeof_ehdr;
4778 p->p_vaddr -= off - p->p_offset;
4779 if (!m->p_paddr_valid)
4780 p->p_paddr -= off - p->p_offset;
4784 p->p_filesz += alloc * bed->s->sizeof_phdr;
4785 p->p_memsz += alloc * bed->s->sizeof_phdr;
4788 p->p_filesz += header_pad;
4789 p->p_memsz += header_pad;
4793 if (p->p_type == PT_LOAD
4794 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4796 if (!m->includes_filehdr && !m->includes_phdrs)
4802 adjust = off - (p->p_offset + p->p_filesz);
4804 p->p_filesz += adjust;
4805 p->p_memsz += adjust;
4809 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4810 maps. Set filepos for sections in PT_LOAD segments, and in
4811 core files, for sections in PT_NOTE segments.
4812 assign_file_positions_for_non_load_sections will set filepos
4813 for other sections and update p_filesz for other segments. */
4814 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4817 bfd_size_type align;
4818 Elf_Internal_Shdr *this_hdr;
4821 this_hdr = &elf_section_data (sec)->this_hdr;
4822 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4824 if ((p->p_type == PT_LOAD
4825 || p->p_type == PT_TLS)
4826 && (this_hdr->sh_type != SHT_NOBITS
4827 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4828 && ((this_hdr->sh_flags & SHF_TLS) == 0
4829 || p->p_type == PT_TLS))))
4831 bfd_vma p_start = p->p_paddr;
4832 bfd_vma p_end = p_start + p->p_memsz;
4833 bfd_vma s_start = sec->lma;
4834 bfd_vma adjust = s_start - p_end;
4838 || p_end < p_start))
4840 (*_bfd_error_handler)
4841 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4842 (unsigned long) s_start, (unsigned long) p_end);
4846 p->p_memsz += adjust;
4848 if (this_hdr->sh_type != SHT_NOBITS)
4850 if (p->p_filesz + adjust < p->p_memsz)
4852 /* We have a PROGBITS section following NOBITS ones.
4853 Allocate file space for the NOBITS section(s) and
4855 adjust = p->p_memsz - p->p_filesz;
4856 if (!write_zeros (abfd, off, adjust))
4860 p->p_filesz += adjust;
4864 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4866 /* The section at i == 0 is the one that actually contains
4870 this_hdr->sh_offset = sec->filepos = off;
4871 off += this_hdr->sh_size;
4872 p->p_filesz = this_hdr->sh_size;
4878 /* The rest are fake sections that shouldn't be written. */
4887 if (p->p_type == PT_LOAD)
4889 this_hdr->sh_offset = sec->filepos = off;
4890 if (this_hdr->sh_type != SHT_NOBITS)
4891 off += this_hdr->sh_size;
4893 else if (this_hdr->sh_type == SHT_NOBITS
4894 && (this_hdr->sh_flags & SHF_TLS) != 0
4895 && this_hdr->sh_offset == 0)
4897 /* This is a .tbss section that didn't get a PT_LOAD.
4898 (See _bfd_elf_map_sections_to_segments "Create a
4899 final PT_LOAD".) Set sh_offset to the value it
4900 would have if we had created a zero p_filesz and
4901 p_memsz PT_LOAD header for the section. This
4902 also makes the PT_TLS header have the same
4904 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
4906 this_hdr->sh_offset = sec->filepos = off + adjust;
4909 if (this_hdr->sh_type != SHT_NOBITS)
4911 p->p_filesz += this_hdr->sh_size;
4912 /* A load section without SHF_ALLOC is something like
4913 a note section in a PT_NOTE segment. These take
4914 file space but are not loaded into memory. */
4915 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4916 p->p_memsz += this_hdr->sh_size;
4918 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4920 if (p->p_type == PT_TLS)
4921 p->p_memsz += this_hdr->sh_size;
4923 /* .tbss is special. It doesn't contribute to p_memsz of
4925 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4926 p->p_memsz += this_hdr->sh_size;
4929 if (align > p->p_align
4930 && !m->p_align_valid
4931 && (p->p_type != PT_LOAD
4932 || (abfd->flags & D_PAGED) == 0))
4936 if (!m->p_flags_valid)
4939 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4941 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4948 /* Check that all sections are in a PT_LOAD segment.
4949 Don't check funky gdb generated core files. */
4950 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4952 bfd_boolean check_vma = TRUE;
4954 for (i = 1; i < m->count; i++)
4955 if (m->sections[i]->vma == m->sections[i - 1]->vma
4956 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4957 ->this_hdr), p) != 0
4958 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4959 ->this_hdr), p) != 0)
4961 /* Looks like we have overlays packed into the segment. */
4966 for (i = 0; i < m->count; i++)
4968 Elf_Internal_Shdr *this_hdr;
4971 sec = m->sections[i];
4972 this_hdr = &(elf_section_data(sec)->this_hdr);
4973 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
4974 && !ELF_TBSS_SPECIAL (this_hdr, p))
4976 (*_bfd_error_handler)
4977 (_("%B: section `%A' can't be allocated in segment %d"),
4979 print_segment_map (m);
4985 elf_next_file_pos (abfd) = off;
4989 /* Assign file positions for the other sections. */
4992 assign_file_positions_for_non_load_sections (bfd *abfd,
4993 struct bfd_link_info *link_info)
4995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4996 Elf_Internal_Shdr **i_shdrpp;
4997 Elf_Internal_Shdr **hdrpp;
4998 Elf_Internal_Phdr *phdrs;
4999 Elf_Internal_Phdr *p;
5000 struct elf_segment_map *m;
5001 struct elf_segment_map *hdrs_segment;
5002 bfd_vma filehdr_vaddr, filehdr_paddr;
5003 bfd_vma phdrs_vaddr, phdrs_paddr;
5005 unsigned int num_sec;
5009 i_shdrpp = elf_elfsections (abfd);
5010 num_sec = elf_numsections (abfd);
5011 off = elf_next_file_pos (abfd);
5012 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5014 Elf_Internal_Shdr *hdr;
5017 if (hdr->bfd_section != NULL
5018 && (hdr->bfd_section->filepos != 0
5019 || (hdr->sh_type == SHT_NOBITS
5020 && hdr->contents == NULL)))
5021 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5022 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5024 if (hdr->sh_size != 0)
5025 (*_bfd_error_handler)
5026 (_("%B: warning: allocated section `%s' not in segment"),
5028 (hdr->bfd_section == NULL
5030 : hdr->bfd_section->name));
5031 /* We don't need to page align empty sections. */
5032 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5033 off += vma_page_aligned_bias (hdr->sh_addr, off,
5036 off += vma_page_aligned_bias (hdr->sh_addr, off,
5038 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5041 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5042 && hdr->bfd_section == NULL)
5043 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5044 || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
5045 || hdr == i_shdrpp[elf_strtab_sec (abfd)])
5046 hdr->sh_offset = -1;
5048 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5051 /* Now that we have set the section file positions, we can set up
5052 the file positions for the non PT_LOAD segments. */
5056 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5058 hdrs_segment = NULL;
5059 phdrs = elf_tdata (abfd)->phdr;
5060 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5063 if (p->p_type != PT_LOAD)
5066 if (m->includes_filehdr)
5068 filehdr_vaddr = p->p_vaddr;
5069 filehdr_paddr = p->p_paddr;
5071 if (m->includes_phdrs)
5073 phdrs_vaddr = p->p_vaddr;
5074 phdrs_paddr = p->p_paddr;
5075 if (m->includes_filehdr)
5078 phdrs_vaddr += bed->s->sizeof_ehdr;
5079 phdrs_paddr += bed->s->sizeof_ehdr;
5084 if (hdrs_segment != NULL && link_info != NULL)
5086 /* There is a segment that contains both the file headers and the
5087 program headers, so provide a symbol __ehdr_start pointing there.
5088 A program can use this to examine itself robustly. */
5090 struct elf_link_hash_entry *hash
5091 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5092 FALSE, FALSE, TRUE);
5093 /* If the symbol was referenced and not defined, define it. */
5095 && (hash->root.type == bfd_link_hash_new
5096 || hash->root.type == bfd_link_hash_undefined
5097 || hash->root.type == bfd_link_hash_undefweak
5098 || hash->root.type == bfd_link_hash_common))
5101 if (hdrs_segment->count != 0)
5102 /* The segment contains sections, so use the first one. */
5103 s = hdrs_segment->sections[0];
5105 /* Use the first (i.e. lowest-addressed) section in any segment. */
5106 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5115 hash->root.u.def.value = filehdr_vaddr - s->vma;
5116 hash->root.u.def.section = s;
5120 hash->root.u.def.value = filehdr_vaddr;
5121 hash->root.u.def.section = bfd_abs_section_ptr;
5124 hash->root.type = bfd_link_hash_defined;
5125 hash->def_regular = 1;
5130 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5132 if (p->p_type == PT_GNU_RELRO)
5134 const Elf_Internal_Phdr *lp;
5135 struct elf_segment_map *lm;
5137 if (link_info != NULL)
5139 /* During linking the range of the RELRO segment is passed
5141 for (lm = elf_seg_map (abfd), lp = phdrs;
5143 lm = lm->next, lp++)
5145 if (lp->p_type == PT_LOAD
5146 && lp->p_vaddr < link_info->relro_end
5148 && lm->sections[0]->vma >= link_info->relro_start)
5152 BFD_ASSERT (lm != NULL);
5156 /* Otherwise we are copying an executable or shared
5157 library, but we need to use the same linker logic. */
5158 for (lp = phdrs; lp < phdrs + count; ++lp)
5160 if (lp->p_type == PT_LOAD
5161 && lp->p_paddr == p->p_paddr)
5166 if (lp < phdrs + count)
5168 p->p_vaddr = lp->p_vaddr;
5169 p->p_paddr = lp->p_paddr;
5170 p->p_offset = lp->p_offset;
5171 if (link_info != NULL)
5172 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5173 else if (m->p_size_valid)
5174 p->p_filesz = m->p_size;
5177 p->p_memsz = p->p_filesz;
5178 /* Preserve the alignment and flags if they are valid. The
5179 gold linker generates RW/4 for the PT_GNU_RELRO section.
5180 It is better for objcopy/strip to honor these attributes
5181 otherwise gdb will choke when using separate debug files.
5183 if (!m->p_align_valid)
5185 if (!m->p_flags_valid)
5186 p->p_flags = (lp->p_flags & ~PF_W);
5190 memset (p, 0, sizeof *p);
5191 p->p_type = PT_NULL;
5194 else if (p->p_type == PT_GNU_STACK)
5196 if (m->p_size_valid)
5197 p->p_memsz = m->p_size;
5199 else if (m->count != 0)
5201 if (p->p_type != PT_LOAD
5202 && (p->p_type != PT_NOTE
5203 || bfd_get_format (abfd) != bfd_core))
5205 if (m->includes_filehdr || m->includes_phdrs)
5207 /* PR 17512: file: 2195325e. */
5208 (*_bfd_error_handler)
5209 (_("%B: warning: non-load segment includes file header and/or program header"),
5215 p->p_offset = m->sections[0]->filepos;
5216 for (i = m->count; i-- != 0;)
5218 asection *sect = m->sections[i];
5219 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5220 if (hdr->sh_type != SHT_NOBITS)
5222 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5229 else if (m->includes_filehdr)
5231 p->p_vaddr = filehdr_vaddr;
5232 if (! m->p_paddr_valid)
5233 p->p_paddr = filehdr_paddr;
5235 else if (m->includes_phdrs)
5237 p->p_vaddr = phdrs_vaddr;
5238 if (! m->p_paddr_valid)
5239 p->p_paddr = phdrs_paddr;
5243 elf_next_file_pos (abfd) = off;
5248 /* Work out the file positions of all the sections. This is called by
5249 _bfd_elf_compute_section_file_positions. All the section sizes and
5250 VMAs must be known before this is called.
5252 Reloc sections come in two flavours: Those processed specially as
5253 "side-channel" data attached to a section to which they apply, and
5254 those that bfd doesn't process as relocations. The latter sort are
5255 stored in a normal bfd section by bfd_section_from_shdr. We don't
5256 consider the former sort here, unless they form part of the loadable
5257 image. Reloc sections not assigned here will be handled later by
5258 assign_file_positions_for_relocs.
5260 We also don't set the positions of the .symtab and .strtab here. */
5263 assign_file_positions_except_relocs (bfd *abfd,
5264 struct bfd_link_info *link_info)
5266 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5267 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5268 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5270 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5271 && bfd_get_format (abfd) != bfd_core)
5273 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5274 unsigned int num_sec = elf_numsections (abfd);
5275 Elf_Internal_Shdr **hdrpp;
5279 /* Start after the ELF header. */
5280 off = i_ehdrp->e_ehsize;
5282 /* We are not creating an executable, which means that we are
5283 not creating a program header, and that the actual order of
5284 the sections in the file is unimportant. */
5285 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5287 Elf_Internal_Shdr *hdr;
5290 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5291 && hdr->bfd_section == NULL)
5292 || i == elf_onesymtab (abfd)
5293 || i == elf_symtab_shndx (abfd)
5294 || i == elf_strtab_sec (abfd))
5296 hdr->sh_offset = -1;
5299 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5302 elf_next_file_pos (abfd) = off;
5308 /* Assign file positions for the loaded sections based on the
5309 assignment of sections to segments. */
5310 if (!assign_file_positions_for_load_sections (abfd, link_info))
5313 /* And for non-load sections. */
5314 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5317 if (bed->elf_backend_modify_program_headers != NULL)
5319 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5323 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5324 if (link_info != NULL
5325 && link_info->executable
5326 && link_info->shared)
5328 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
5329 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
5330 Elf_Internal_Phdr *end_segment = &segment[num_segments];
5332 /* Find the lowest p_vaddr in PT_LOAD segments. */
5333 bfd_vma p_vaddr = (bfd_vma) -1;
5334 for (; segment < end_segment; segment++)
5335 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
5336 p_vaddr = segment->p_vaddr;
5338 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5339 segments is non-zero. */
5341 i_ehdrp->e_type = ET_EXEC;
5344 /* Write out the program headers. */
5345 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5346 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5347 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5355 prep_headers (bfd *abfd)
5357 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5358 struct elf_strtab_hash *shstrtab;
5359 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5361 i_ehdrp = elf_elfheader (abfd);
5363 shstrtab = _bfd_elf_strtab_init ();
5364 if (shstrtab == NULL)
5367 elf_shstrtab (abfd) = shstrtab;
5369 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5370 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5371 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5372 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5374 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5375 i_ehdrp->e_ident[EI_DATA] =
5376 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5377 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5379 if ((abfd->flags & DYNAMIC) != 0)
5380 i_ehdrp->e_type = ET_DYN;
5381 else if ((abfd->flags & EXEC_P) != 0)
5382 i_ehdrp->e_type = ET_EXEC;
5383 else if (bfd_get_format (abfd) == bfd_core)
5384 i_ehdrp->e_type = ET_CORE;
5386 i_ehdrp->e_type = ET_REL;
5388 switch (bfd_get_arch (abfd))
5390 case bfd_arch_unknown:
5391 i_ehdrp->e_machine = EM_NONE;
5394 /* There used to be a long list of cases here, each one setting
5395 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5396 in the corresponding bfd definition. To avoid duplication,
5397 the switch was removed. Machines that need special handling
5398 can generally do it in elf_backend_final_write_processing(),
5399 unless they need the information earlier than the final write.
5400 Such need can generally be supplied by replacing the tests for
5401 e_machine with the conditions used to determine it. */
5403 i_ehdrp->e_machine = bed->elf_machine_code;
5406 i_ehdrp->e_version = bed->s->ev_current;
5407 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5409 /* No program header, for now. */
5410 i_ehdrp->e_phoff = 0;
5411 i_ehdrp->e_phentsize = 0;
5412 i_ehdrp->e_phnum = 0;
5414 /* Each bfd section is section header entry. */
5415 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5416 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5418 /* If we're building an executable, we'll need a program header table. */
5419 if (abfd->flags & EXEC_P)
5420 /* It all happens later. */
5424 i_ehdrp->e_phentsize = 0;
5425 i_ehdrp->e_phoff = 0;
5428 elf_tdata (abfd)->symtab_hdr.sh_name =
5429 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5430 elf_tdata (abfd)->strtab_hdr.sh_name =
5431 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5432 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5433 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5434 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5435 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
5436 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5442 /* Assign file positions for all the reloc sections which are not part
5443 of the loadable file image, and the file position of section headers. */
5446 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5449 unsigned int i, num_sec;
5450 Elf_Internal_Shdr **shdrpp;
5451 Elf_Internal_Ehdr *i_ehdrp;
5452 const struct elf_backend_data *bed;
5454 off = elf_next_file_pos (abfd);
5456 num_sec = elf_numsections (abfd);
5457 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5459 Elf_Internal_Shdr *shdrp;
5462 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5463 && shdrp->sh_offset == -1)
5464 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5467 /* Place the section headers. */
5468 i_ehdrp = elf_elfheader (abfd);
5469 bed = get_elf_backend_data (abfd);
5470 off = align_file_position (off, 1 << bed->s->log_file_align);
5471 i_ehdrp->e_shoff = off;
5472 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5473 elf_next_file_pos (abfd) = off;
5477 _bfd_elf_write_object_contents (bfd *abfd)
5479 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5480 Elf_Internal_Shdr **i_shdrp;
5482 unsigned int count, num_sec;
5483 struct elf_obj_tdata *t;
5485 if (! abfd->output_has_begun
5486 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5489 i_shdrp = elf_elfsections (abfd);
5492 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5496 _bfd_elf_assign_file_positions_for_relocs (abfd);
5498 /* After writing the headers, we need to write the sections too... */
5499 num_sec = elf_numsections (abfd);
5500 for (count = 1; count < num_sec; count++)
5502 if (bed->elf_backend_section_processing)
5503 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5504 if (i_shdrp[count]->contents)
5506 bfd_size_type amt = i_shdrp[count]->sh_size;
5508 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5509 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5514 /* Write out the section header names. */
5515 t = elf_tdata (abfd);
5516 if (elf_shstrtab (abfd) != NULL
5517 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5518 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5521 if (bed->elf_backend_final_write_processing)
5522 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5524 if (!bed->s->write_shdrs_and_ehdr (abfd))
5527 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5528 if (t->o->build_id.after_write_object_contents != NULL)
5529 return (*t->o->build_id.after_write_object_contents) (abfd);
5535 _bfd_elf_write_corefile_contents (bfd *abfd)
5537 /* Hopefully this can be done just like an object file. */
5538 return _bfd_elf_write_object_contents (abfd);
5541 /* Given a section, search the header to find them. */
5544 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5546 const struct elf_backend_data *bed;
5547 unsigned int sec_index;
5549 if (elf_section_data (asect) != NULL
5550 && elf_section_data (asect)->this_idx != 0)
5551 return elf_section_data (asect)->this_idx;
5553 if (bfd_is_abs_section (asect))
5554 sec_index = SHN_ABS;
5555 else if (bfd_is_com_section (asect))
5556 sec_index = SHN_COMMON;
5557 else if (bfd_is_und_section (asect))
5558 sec_index = SHN_UNDEF;
5560 sec_index = SHN_BAD;
5562 bed = get_elf_backend_data (abfd);
5563 if (bed->elf_backend_section_from_bfd_section)
5565 int retval = sec_index;
5567 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5571 if (sec_index == SHN_BAD)
5572 bfd_set_error (bfd_error_nonrepresentable_section);
5577 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5581 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5583 asymbol *asym_ptr = *asym_ptr_ptr;
5585 flagword flags = asym_ptr->flags;
5587 /* When gas creates relocations against local labels, it creates its
5588 own symbol for the section, but does put the symbol into the
5589 symbol chain, so udata is 0. When the linker is generating
5590 relocatable output, this section symbol may be for one of the
5591 input sections rather than the output section. */
5592 if (asym_ptr->udata.i == 0
5593 && (flags & BSF_SECTION_SYM)
5594 && asym_ptr->section)
5599 sec = asym_ptr->section;
5600 if (sec->owner != abfd && sec->output_section != NULL)
5601 sec = sec->output_section;
5602 if (sec->owner == abfd
5603 && (indx = sec->index) < elf_num_section_syms (abfd)
5604 && elf_section_syms (abfd)[indx] != NULL)
5605 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5608 idx = asym_ptr->udata.i;
5612 /* This case can occur when using --strip-symbol on a symbol
5613 which is used in a relocation entry. */
5614 (*_bfd_error_handler)
5615 (_("%B: symbol `%s' required but not present"),
5616 abfd, bfd_asymbol_name (asym_ptr));
5617 bfd_set_error (bfd_error_no_symbols);
5624 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5625 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5633 /* Rewrite program header information. */
5636 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5638 Elf_Internal_Ehdr *iehdr;
5639 struct elf_segment_map *map;
5640 struct elf_segment_map *map_first;
5641 struct elf_segment_map **pointer_to_map;
5642 Elf_Internal_Phdr *segment;
5645 unsigned int num_segments;
5646 bfd_boolean phdr_included = FALSE;
5647 bfd_boolean p_paddr_valid;
5648 bfd_vma maxpagesize;
5649 struct elf_segment_map *phdr_adjust_seg = NULL;
5650 unsigned int phdr_adjust_num = 0;
5651 const struct elf_backend_data *bed;
5653 bed = get_elf_backend_data (ibfd);
5654 iehdr = elf_elfheader (ibfd);
5657 pointer_to_map = &map_first;
5659 num_segments = elf_elfheader (ibfd)->e_phnum;
5660 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5662 /* Returns the end address of the segment + 1. */
5663 #define SEGMENT_END(segment, start) \
5664 (start + (segment->p_memsz > segment->p_filesz \
5665 ? segment->p_memsz : segment->p_filesz))
5667 #define SECTION_SIZE(section, segment) \
5668 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5669 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5670 ? section->size : 0)
5672 /* Returns TRUE if the given section is contained within
5673 the given segment. VMA addresses are compared. */
5674 #define IS_CONTAINED_BY_VMA(section, segment) \
5675 (section->vma >= segment->p_vaddr \
5676 && (section->vma + SECTION_SIZE (section, segment) \
5677 <= (SEGMENT_END (segment, segment->p_vaddr))))
5679 /* Returns TRUE if the given section is contained within
5680 the given segment. LMA addresses are compared. */
5681 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5682 (section->lma >= base \
5683 && (section->lma + SECTION_SIZE (section, segment) \
5684 <= SEGMENT_END (segment, base)))
5686 /* Handle PT_NOTE segment. */
5687 #define IS_NOTE(p, s) \
5688 (p->p_type == PT_NOTE \
5689 && elf_section_type (s) == SHT_NOTE \
5690 && (bfd_vma) s->filepos >= p->p_offset \
5691 && ((bfd_vma) s->filepos + s->size \
5692 <= p->p_offset + p->p_filesz))
5694 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5696 #define IS_COREFILE_NOTE(p, s) \
5698 && bfd_get_format (ibfd) == bfd_core \
5702 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5703 linker, which generates a PT_INTERP section with p_vaddr and
5704 p_memsz set to 0. */
5705 #define IS_SOLARIS_PT_INTERP(p, s) \
5707 && p->p_paddr == 0 \
5708 && p->p_memsz == 0 \
5709 && p->p_filesz > 0 \
5710 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5712 && (bfd_vma) s->filepos >= p->p_offset \
5713 && ((bfd_vma) s->filepos + s->size \
5714 <= p->p_offset + p->p_filesz))
5716 /* Decide if the given section should be included in the given segment.
5717 A section will be included if:
5718 1. It is within the address space of the segment -- we use the LMA
5719 if that is set for the segment and the VMA otherwise,
5720 2. It is an allocated section or a NOTE section in a PT_NOTE
5722 3. There is an output section associated with it,
5723 4. The section has not already been allocated to a previous segment.
5724 5. PT_GNU_STACK segments do not include any sections.
5725 6. PT_TLS segment includes only SHF_TLS sections.
5726 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5727 8. PT_DYNAMIC should not contain empty sections at the beginning
5728 (with the possible exception of .dynamic). */
5729 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5730 ((((segment->p_paddr \
5731 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5732 : IS_CONTAINED_BY_VMA (section, segment)) \
5733 && (section->flags & SEC_ALLOC) != 0) \
5734 || IS_NOTE (segment, section)) \
5735 && segment->p_type != PT_GNU_STACK \
5736 && (segment->p_type != PT_TLS \
5737 || (section->flags & SEC_THREAD_LOCAL)) \
5738 && (segment->p_type == PT_LOAD \
5739 || segment->p_type == PT_TLS \
5740 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5741 && (segment->p_type != PT_DYNAMIC \
5742 || SECTION_SIZE (section, segment) > 0 \
5743 || (segment->p_paddr \
5744 ? segment->p_paddr != section->lma \
5745 : segment->p_vaddr != section->vma) \
5746 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5748 && !section->segment_mark)
5750 /* If the output section of a section in the input segment is NULL,
5751 it is removed from the corresponding output segment. */
5752 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5753 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5754 && section->output_section != NULL)
5756 /* Returns TRUE iff seg1 starts after the end of seg2. */
5757 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5758 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5760 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5761 their VMA address ranges and their LMA address ranges overlap.
5762 It is possible to have overlapping VMA ranges without overlapping LMA
5763 ranges. RedBoot images for example can have both .data and .bss mapped
5764 to the same VMA range, but with the .data section mapped to a different
5766 #define SEGMENT_OVERLAPS(seg1, seg2) \
5767 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5768 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5769 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5770 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5772 /* Initialise the segment mark field. */
5773 for (section = ibfd->sections; section != NULL; section = section->next)
5774 section->segment_mark = FALSE;
5776 /* The Solaris linker creates program headers in which all the
5777 p_paddr fields are zero. When we try to objcopy or strip such a
5778 file, we get confused. Check for this case, and if we find it
5779 don't set the p_paddr_valid fields. */
5780 p_paddr_valid = FALSE;
5781 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5784 if (segment->p_paddr != 0)
5786 p_paddr_valid = TRUE;
5790 /* Scan through the segments specified in the program header
5791 of the input BFD. For this first scan we look for overlaps
5792 in the loadable segments. These can be created by weird
5793 parameters to objcopy. Also, fix some solaris weirdness. */
5794 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5799 Elf_Internal_Phdr *segment2;
5801 if (segment->p_type == PT_INTERP)
5802 for (section = ibfd->sections; section; section = section->next)
5803 if (IS_SOLARIS_PT_INTERP (segment, section))
5805 /* Mininal change so that the normal section to segment
5806 assignment code will work. */
5807 segment->p_vaddr = section->vma;
5811 if (segment->p_type != PT_LOAD)
5813 /* Remove PT_GNU_RELRO segment. */
5814 if (segment->p_type == PT_GNU_RELRO)
5815 segment->p_type = PT_NULL;
5819 /* Determine if this segment overlaps any previous segments. */
5820 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5822 bfd_signed_vma extra_length;
5824 if (segment2->p_type != PT_LOAD
5825 || !SEGMENT_OVERLAPS (segment, segment2))
5828 /* Merge the two segments together. */
5829 if (segment2->p_vaddr < segment->p_vaddr)
5831 /* Extend SEGMENT2 to include SEGMENT and then delete
5833 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5834 - SEGMENT_END (segment2, segment2->p_vaddr));
5836 if (extra_length > 0)
5838 segment2->p_memsz += extra_length;
5839 segment2->p_filesz += extra_length;
5842 segment->p_type = PT_NULL;
5844 /* Since we have deleted P we must restart the outer loop. */
5846 segment = elf_tdata (ibfd)->phdr;
5851 /* Extend SEGMENT to include SEGMENT2 and then delete
5853 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5854 - SEGMENT_END (segment, segment->p_vaddr));
5856 if (extra_length > 0)
5858 segment->p_memsz += extra_length;
5859 segment->p_filesz += extra_length;
5862 segment2->p_type = PT_NULL;
5867 /* The second scan attempts to assign sections to segments. */
5868 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5872 unsigned int section_count;
5873 asection **sections;
5874 asection *output_section;
5876 bfd_vma matching_lma;
5877 bfd_vma suggested_lma;
5880 asection *first_section;
5881 bfd_boolean first_matching_lma;
5882 bfd_boolean first_suggested_lma;
5884 if (segment->p_type == PT_NULL)
5887 first_section = NULL;
5888 /* Compute how many sections might be placed into this segment. */
5889 for (section = ibfd->sections, section_count = 0;
5891 section = section->next)
5893 /* Find the first section in the input segment, which may be
5894 removed from the corresponding output segment. */
5895 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5897 if (first_section == NULL)
5898 first_section = section;
5899 if (section->output_section != NULL)
5904 /* Allocate a segment map big enough to contain
5905 all of the sections we have selected. */
5906 amt = sizeof (struct elf_segment_map);
5907 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5908 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5912 /* Initialise the fields of the segment map. Default to
5913 using the physical address of the segment in the input BFD. */
5915 map->p_type = segment->p_type;
5916 map->p_flags = segment->p_flags;
5917 map->p_flags_valid = 1;
5919 /* If the first section in the input segment is removed, there is
5920 no need to preserve segment physical address in the corresponding
5922 if (!first_section || first_section->output_section != NULL)
5924 map->p_paddr = segment->p_paddr;
5925 map->p_paddr_valid = p_paddr_valid;
5928 /* Determine if this segment contains the ELF file header
5929 and if it contains the program headers themselves. */
5930 map->includes_filehdr = (segment->p_offset == 0
5931 && segment->p_filesz >= iehdr->e_ehsize);
5932 map->includes_phdrs = 0;
5934 if (!phdr_included || segment->p_type != PT_LOAD)
5936 map->includes_phdrs =
5937 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5938 && (segment->p_offset + segment->p_filesz
5939 >= ((bfd_vma) iehdr->e_phoff
5940 + iehdr->e_phnum * iehdr->e_phentsize)));
5942 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5943 phdr_included = TRUE;
5946 if (section_count == 0)
5948 /* Special segments, such as the PT_PHDR segment, may contain
5949 no sections, but ordinary, loadable segments should contain
5950 something. They are allowed by the ELF spec however, so only
5951 a warning is produced. */
5952 if (segment->p_type == PT_LOAD)
5953 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5954 " detected, is this intentional ?\n"),
5958 *pointer_to_map = map;
5959 pointer_to_map = &map->next;
5964 /* Now scan the sections in the input BFD again and attempt
5965 to add their corresponding output sections to the segment map.
5966 The problem here is how to handle an output section which has
5967 been moved (ie had its LMA changed). There are four possibilities:
5969 1. None of the sections have been moved.
5970 In this case we can continue to use the segment LMA from the
5973 2. All of the sections have been moved by the same amount.
5974 In this case we can change the segment's LMA to match the LMA
5975 of the first section.
5977 3. Some of the sections have been moved, others have not.
5978 In this case those sections which have not been moved can be
5979 placed in the current segment which will have to have its size,
5980 and possibly its LMA changed, and a new segment or segments will
5981 have to be created to contain the other sections.
5983 4. The sections have been moved, but not by the same amount.
5984 In this case we can change the segment's LMA to match the LMA
5985 of the first section and we will have to create a new segment
5986 or segments to contain the other sections.
5988 In order to save time, we allocate an array to hold the section
5989 pointers that we are interested in. As these sections get assigned
5990 to a segment, they are removed from this array. */
5992 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5993 if (sections == NULL)
5996 /* Step One: Scan for segment vs section LMA conflicts.
5997 Also add the sections to the section array allocated above.
5998 Also add the sections to the current segment. In the common
5999 case, where the sections have not been moved, this means that
6000 we have completely filled the segment, and there is nothing
6005 first_matching_lma = TRUE;
6006 first_suggested_lma = TRUE;
6008 for (section = ibfd->sections;
6010 section = section->next)
6011 if (section == first_section)
6014 for (j = 0; section != NULL; section = section->next)
6016 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
6018 output_section = section->output_section;
6020 sections[j++] = section;
6022 /* The Solaris native linker always sets p_paddr to 0.
6023 We try to catch that case here, and set it to the
6024 correct value. Note - some backends require that
6025 p_paddr be left as zero. */
6027 && segment->p_vaddr != 0
6028 && !bed->want_p_paddr_set_to_zero
6030 && output_section->lma != 0
6031 && output_section->vma == (segment->p_vaddr
6032 + (map->includes_filehdr
6035 + (map->includes_phdrs
6037 * iehdr->e_phentsize)
6039 map->p_paddr = segment->p_vaddr;
6041 /* Match up the physical address of the segment with the
6042 LMA address of the output section. */
6043 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6044 || IS_COREFILE_NOTE (segment, section)
6045 || (bed->want_p_paddr_set_to_zero
6046 && IS_CONTAINED_BY_VMA (output_section, segment)))
6048 if (first_matching_lma || output_section->lma < matching_lma)
6050 matching_lma = output_section->lma;
6051 first_matching_lma = FALSE;
6054 /* We assume that if the section fits within the segment
6055 then it does not overlap any other section within that
6057 map->sections[isec++] = output_section;
6059 else if (first_suggested_lma)
6061 suggested_lma = output_section->lma;
6062 first_suggested_lma = FALSE;
6065 if (j == section_count)
6070 BFD_ASSERT (j == section_count);
6072 /* Step Two: Adjust the physical address of the current segment,
6074 if (isec == section_count)
6076 /* All of the sections fitted within the segment as currently
6077 specified. This is the default case. Add the segment to
6078 the list of built segments and carry on to process the next
6079 program header in the input BFD. */
6080 map->count = section_count;
6081 *pointer_to_map = map;
6082 pointer_to_map = &map->next;
6085 && !bed->want_p_paddr_set_to_zero
6086 && matching_lma != map->p_paddr
6087 && !map->includes_filehdr
6088 && !map->includes_phdrs)
6089 /* There is some padding before the first section in the
6090 segment. So, we must account for that in the output
6092 map->p_vaddr_offset = matching_lma - map->p_paddr;
6099 if (!first_matching_lma)
6101 /* At least one section fits inside the current segment.
6102 Keep it, but modify its physical address to match the
6103 LMA of the first section that fitted. */
6104 map->p_paddr = matching_lma;
6108 /* None of the sections fitted inside the current segment.
6109 Change the current segment's physical address to match
6110 the LMA of the first section. */
6111 map->p_paddr = suggested_lma;
6114 /* Offset the segment physical address from the lma
6115 to allow for space taken up by elf headers. */
6116 if (map->includes_filehdr)
6118 if (map->p_paddr >= iehdr->e_ehsize)
6119 map->p_paddr -= iehdr->e_ehsize;
6122 map->includes_filehdr = FALSE;
6123 map->includes_phdrs = FALSE;
6127 if (map->includes_phdrs)
6129 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6131 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6133 /* iehdr->e_phnum is just an estimate of the number
6134 of program headers that we will need. Make a note
6135 here of the number we used and the segment we chose
6136 to hold these headers, so that we can adjust the
6137 offset when we know the correct value. */
6138 phdr_adjust_num = iehdr->e_phnum;
6139 phdr_adjust_seg = map;
6142 map->includes_phdrs = FALSE;
6146 /* Step Three: Loop over the sections again, this time assigning
6147 those that fit to the current segment and removing them from the
6148 sections array; but making sure not to leave large gaps. Once all
6149 possible sections have been assigned to the current segment it is
6150 added to the list of built segments and if sections still remain
6151 to be assigned, a new segment is constructed before repeating
6158 first_suggested_lma = TRUE;
6160 /* Fill the current segment with sections that fit. */
6161 for (j = 0; j < section_count; j++)
6163 section = sections[j];
6165 if (section == NULL)
6168 output_section = section->output_section;
6170 BFD_ASSERT (output_section != NULL);
6172 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6173 || IS_COREFILE_NOTE (segment, section))
6175 if (map->count == 0)
6177 /* If the first section in a segment does not start at
6178 the beginning of the segment, then something is
6180 if (output_section->lma
6182 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6183 + (map->includes_phdrs
6184 ? iehdr->e_phnum * iehdr->e_phentsize
6192 prev_sec = map->sections[map->count - 1];
6194 /* If the gap between the end of the previous section
6195 and the start of this section is more than
6196 maxpagesize then we need to start a new segment. */
6197 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6199 < BFD_ALIGN (output_section->lma, maxpagesize))
6200 || (prev_sec->lma + prev_sec->size
6201 > output_section->lma))
6203 if (first_suggested_lma)
6205 suggested_lma = output_section->lma;
6206 first_suggested_lma = FALSE;
6213 map->sections[map->count++] = output_section;
6216 section->segment_mark = TRUE;
6218 else if (first_suggested_lma)
6220 suggested_lma = output_section->lma;
6221 first_suggested_lma = FALSE;
6225 BFD_ASSERT (map->count > 0);
6227 /* Add the current segment to the list of built segments. */
6228 *pointer_to_map = map;
6229 pointer_to_map = &map->next;
6231 if (isec < section_count)
6233 /* We still have not allocated all of the sections to
6234 segments. Create a new segment here, initialise it
6235 and carry on looping. */
6236 amt = sizeof (struct elf_segment_map);
6237 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6238 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6245 /* Initialise the fields of the segment map. Set the physical
6246 physical address to the LMA of the first section that has
6247 not yet been assigned. */
6249 map->p_type = segment->p_type;
6250 map->p_flags = segment->p_flags;
6251 map->p_flags_valid = 1;
6252 map->p_paddr = suggested_lma;
6253 map->p_paddr_valid = p_paddr_valid;
6254 map->includes_filehdr = 0;
6255 map->includes_phdrs = 0;
6258 while (isec < section_count);
6263 elf_seg_map (obfd) = map_first;
6265 /* If we had to estimate the number of program headers that were
6266 going to be needed, then check our estimate now and adjust
6267 the offset if necessary. */
6268 if (phdr_adjust_seg != NULL)
6272 for (count = 0, map = map_first; map != NULL; map = map->next)
6275 if (count > phdr_adjust_num)
6276 phdr_adjust_seg->p_paddr
6277 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6282 #undef IS_CONTAINED_BY_VMA
6283 #undef IS_CONTAINED_BY_LMA
6285 #undef IS_COREFILE_NOTE
6286 #undef IS_SOLARIS_PT_INTERP
6287 #undef IS_SECTION_IN_INPUT_SEGMENT
6288 #undef INCLUDE_SECTION_IN_SEGMENT
6289 #undef SEGMENT_AFTER_SEGMENT
6290 #undef SEGMENT_OVERLAPS
6294 /* Copy ELF program header information. */
6297 copy_elf_program_header (bfd *ibfd, bfd *obfd)
6299 Elf_Internal_Ehdr *iehdr;
6300 struct elf_segment_map *map;
6301 struct elf_segment_map *map_first;
6302 struct elf_segment_map **pointer_to_map;
6303 Elf_Internal_Phdr *segment;
6305 unsigned int num_segments;
6306 bfd_boolean phdr_included = FALSE;
6307 bfd_boolean p_paddr_valid;
6309 iehdr = elf_elfheader (ibfd);
6312 pointer_to_map = &map_first;
6314 /* If all the segment p_paddr fields are zero, don't set
6315 map->p_paddr_valid. */
6316 p_paddr_valid = FALSE;
6317 num_segments = elf_elfheader (ibfd)->e_phnum;
6318 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6321 if (segment->p_paddr != 0)
6323 p_paddr_valid = TRUE;
6327 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6332 unsigned int section_count;
6334 Elf_Internal_Shdr *this_hdr;
6335 asection *first_section = NULL;
6336 asection *lowest_section;
6338 /* Compute how many sections are in this segment. */
6339 for (section = ibfd->sections, section_count = 0;
6341 section = section->next)
6343 this_hdr = &(elf_section_data(section)->this_hdr);
6344 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6346 if (first_section == NULL)
6347 first_section = section;
6352 /* Allocate a segment map big enough to contain
6353 all of the sections we have selected. */
6354 amt = sizeof (struct elf_segment_map);
6355 if (section_count != 0)
6356 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6357 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6361 /* Initialize the fields of the output segment map with the
6364 map->p_type = segment->p_type;
6365 map->p_flags = segment->p_flags;
6366 map->p_flags_valid = 1;
6367 map->p_paddr = segment->p_paddr;
6368 map->p_paddr_valid = p_paddr_valid;
6369 map->p_align = segment->p_align;
6370 map->p_align_valid = 1;
6371 map->p_vaddr_offset = 0;
6373 if (map->p_type == PT_GNU_RELRO
6374 || map->p_type == PT_GNU_STACK)
6376 /* The PT_GNU_RELRO segment may contain the first a few
6377 bytes in the .got.plt section even if the whole .got.plt
6378 section isn't in the PT_GNU_RELRO segment. We won't
6379 change the size of the PT_GNU_RELRO segment.
6380 Similarly, PT_GNU_STACK size is significant on uclinux
6382 map->p_size = segment->p_memsz;
6383 map->p_size_valid = 1;
6386 /* Determine if this segment contains the ELF file header
6387 and if it contains the program headers themselves. */
6388 map->includes_filehdr = (segment->p_offset == 0
6389 && segment->p_filesz >= iehdr->e_ehsize);
6391 map->includes_phdrs = 0;
6392 if (! phdr_included || segment->p_type != PT_LOAD)
6394 map->includes_phdrs =
6395 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6396 && (segment->p_offset + segment->p_filesz
6397 >= ((bfd_vma) iehdr->e_phoff
6398 + iehdr->e_phnum * iehdr->e_phentsize)));
6400 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6401 phdr_included = TRUE;
6404 lowest_section = NULL;
6405 if (section_count != 0)
6407 unsigned int isec = 0;
6409 for (section = first_section;
6411 section = section->next)
6413 this_hdr = &(elf_section_data(section)->this_hdr);
6414 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6416 map->sections[isec++] = section->output_section;
6417 if ((section->flags & SEC_ALLOC) != 0)
6421 if (lowest_section == NULL
6422 || section->lma < lowest_section->lma)
6423 lowest_section = section;
6425 /* Section lmas are set up from PT_LOAD header
6426 p_paddr in _bfd_elf_make_section_from_shdr.
6427 If this header has a p_paddr that disagrees
6428 with the section lma, flag the p_paddr as
6430 if ((section->flags & SEC_LOAD) != 0)
6431 seg_off = this_hdr->sh_offset - segment->p_offset;
6433 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6434 if (section->lma - segment->p_paddr != seg_off)
6435 map->p_paddr_valid = FALSE;
6437 if (isec == section_count)
6443 if (map->includes_filehdr && lowest_section != NULL)
6444 /* We need to keep the space used by the headers fixed. */
6445 map->header_size = lowest_section->vma - segment->p_vaddr;
6447 if (!map->includes_phdrs
6448 && !map->includes_filehdr
6449 && map->p_paddr_valid)
6450 /* There is some other padding before the first section. */
6451 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6452 - segment->p_paddr);
6454 map->count = section_count;
6455 *pointer_to_map = map;
6456 pointer_to_map = &map->next;
6459 elf_seg_map (obfd) = map_first;
6463 /* Copy private BFD data. This copies or rewrites ELF program header
6467 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6469 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6470 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6473 if (elf_tdata (ibfd)->phdr == NULL)
6476 if (ibfd->xvec == obfd->xvec)
6478 /* Check to see if any sections in the input BFD
6479 covered by ELF program header have changed. */
6480 Elf_Internal_Phdr *segment;
6481 asection *section, *osec;
6482 unsigned int i, num_segments;
6483 Elf_Internal_Shdr *this_hdr;
6484 const struct elf_backend_data *bed;
6486 bed = get_elf_backend_data (ibfd);
6488 /* Regenerate the segment map if p_paddr is set to 0. */
6489 if (bed->want_p_paddr_set_to_zero)
6492 /* Initialize the segment mark field. */
6493 for (section = obfd->sections; section != NULL;
6494 section = section->next)
6495 section->segment_mark = FALSE;
6497 num_segments = elf_elfheader (ibfd)->e_phnum;
6498 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6502 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6503 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6504 which severly confuses things, so always regenerate the segment
6505 map in this case. */
6506 if (segment->p_paddr == 0
6507 && segment->p_memsz == 0
6508 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6511 for (section = ibfd->sections;
6512 section != NULL; section = section->next)
6514 /* We mark the output section so that we know it comes
6515 from the input BFD. */
6516 osec = section->output_section;
6518 osec->segment_mark = TRUE;
6520 /* Check if this section is covered by the segment. */
6521 this_hdr = &(elf_section_data(section)->this_hdr);
6522 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6524 /* FIXME: Check if its output section is changed or
6525 removed. What else do we need to check? */
6527 || section->flags != osec->flags
6528 || section->lma != osec->lma
6529 || section->vma != osec->vma
6530 || section->size != osec->size
6531 || section->rawsize != osec->rawsize
6532 || section->alignment_power != osec->alignment_power)
6538 /* Check to see if any output section do not come from the
6540 for (section = obfd->sections; section != NULL;
6541 section = section->next)
6543 if (section->segment_mark == FALSE)
6546 section->segment_mark = FALSE;
6549 return copy_elf_program_header (ibfd, obfd);
6553 if (ibfd->xvec == obfd->xvec)
6555 /* When rewriting program header, set the output maxpagesize to
6556 the maximum alignment of input PT_LOAD segments. */
6557 Elf_Internal_Phdr *segment;
6559 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6560 bfd_vma maxpagesize = 0;
6562 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6565 if (segment->p_type == PT_LOAD
6566 && maxpagesize < segment->p_align)
6568 /* PR 17512: file: f17299af. */
6569 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
6570 (*_bfd_error_handler) (_("\
6571 %B: warning: segment alignment of 0x%llx is too large"),
6572 ibfd, (long long) segment->p_align);
6574 maxpagesize = segment->p_align;
6577 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6578 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6581 return rewrite_elf_program_header (ibfd, obfd);
6584 /* Initialize private output section information from input section. */
6587 _bfd_elf_init_private_section_data (bfd *ibfd,
6591 struct bfd_link_info *link_info)
6594 Elf_Internal_Shdr *ihdr, *ohdr;
6595 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6597 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6598 || obfd->xvec->flavour != bfd_target_elf_flavour)
6601 BFD_ASSERT (elf_section_data (osec) != NULL);
6603 /* For objcopy and relocatable link, don't copy the output ELF
6604 section type from input if the output BFD section flags have been
6605 set to something different. For a final link allow some flags
6606 that the linker clears to differ. */
6607 if (elf_section_type (osec) == SHT_NULL
6608 && (osec->flags == isec->flags
6610 && ((osec->flags ^ isec->flags)
6611 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6612 elf_section_type (osec) = elf_section_type (isec);
6614 /* FIXME: Is this correct for all OS/PROC specific flags? */
6615 elf_section_flags (osec) |= (elf_section_flags (isec)
6616 & (SHF_MASKOS | SHF_MASKPROC));
6618 /* Set things up for objcopy and relocatable link. The output
6619 SHT_GROUP section will have its elf_next_in_group pointing back
6620 to the input group members. Ignore linker created group section.
6621 See elfNN_ia64_object_p in elfxx-ia64.c. */
6624 if (elf_sec_group (isec) == NULL
6625 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6627 if (elf_section_flags (isec) & SHF_GROUP)
6628 elf_section_flags (osec) |= SHF_GROUP;
6629 elf_next_in_group (osec) = elf_next_in_group (isec);
6630 elf_section_data (osec)->group = elf_section_data (isec)->group;
6634 ihdr = &elf_section_data (isec)->this_hdr;
6636 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6637 don't use the output section of the linked-to section since it
6638 may be NULL at this point. */
6639 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6641 ohdr = &elf_section_data (osec)->this_hdr;
6642 ohdr->sh_flags |= SHF_LINK_ORDER;
6643 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6646 osec->use_rela_p = isec->use_rela_p;
6651 /* Copy private section information. This copies over the entsize
6652 field, and sometimes the info field. */
6655 _bfd_elf_copy_private_section_data (bfd *ibfd,
6660 Elf_Internal_Shdr *ihdr, *ohdr;
6662 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6663 || obfd->xvec->flavour != bfd_target_elf_flavour)
6666 ihdr = &elf_section_data (isec)->this_hdr;
6667 ohdr = &elf_section_data (osec)->this_hdr;
6669 ohdr->sh_entsize = ihdr->sh_entsize;
6671 if (ihdr->sh_type == SHT_SYMTAB
6672 || ihdr->sh_type == SHT_DYNSYM
6673 || ihdr->sh_type == SHT_GNU_verneed
6674 || ihdr->sh_type == SHT_GNU_verdef)
6675 ohdr->sh_info = ihdr->sh_info;
6677 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6681 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6682 necessary if we are removing either the SHT_GROUP section or any of
6683 the group member sections. DISCARDED is the value that a section's
6684 output_section has if the section will be discarded, NULL when this
6685 function is called from objcopy, bfd_abs_section_ptr when called
6689 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6693 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6694 if (elf_section_type (isec) == SHT_GROUP)
6696 asection *first = elf_next_in_group (isec);
6697 asection *s = first;
6698 bfd_size_type removed = 0;
6702 /* If this member section is being output but the
6703 SHT_GROUP section is not, then clear the group info
6704 set up by _bfd_elf_copy_private_section_data. */
6705 if (s->output_section != discarded
6706 && isec->output_section == discarded)
6708 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6709 elf_group_name (s->output_section) = NULL;
6711 /* Conversely, if the member section is not being output
6712 but the SHT_GROUP section is, then adjust its size. */
6713 else if (s->output_section == discarded
6714 && isec->output_section != discarded)
6716 s = elf_next_in_group (s);
6722 if (discarded != NULL)
6724 /* If we've been called for ld -r, then we need to
6725 adjust the input section size. This function may
6726 be called multiple times, so save the original
6728 if (isec->rawsize == 0)
6729 isec->rawsize = isec->size;
6730 isec->size = isec->rawsize - removed;
6734 /* Adjust the output section size when called from
6736 isec->output_section->size -= removed;
6744 /* Copy private header information. */
6747 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6749 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6750 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6753 /* Copy over private BFD data if it has not already been copied.
6754 This must be done here, rather than in the copy_private_bfd_data
6755 entry point, because the latter is called after the section
6756 contents have been set, which means that the program headers have
6757 already been worked out. */
6758 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
6760 if (! copy_private_bfd_data (ibfd, obfd))
6764 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6767 /* Copy private symbol information. If this symbol is in a section
6768 which we did not map into a BFD section, try to map the section
6769 index correctly. We use special macro definitions for the mapped
6770 section indices; these definitions are interpreted by the
6771 swap_out_syms function. */
6773 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6774 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6775 #define MAP_STRTAB (SHN_HIOS + 3)
6776 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6777 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6780 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6785 elf_symbol_type *isym, *osym;
6787 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6788 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6791 isym = elf_symbol_from (ibfd, isymarg);
6792 osym = elf_symbol_from (obfd, osymarg);
6795 && isym->internal_elf_sym.st_shndx != 0
6797 && bfd_is_abs_section (isym->symbol.section))
6801 shndx = isym->internal_elf_sym.st_shndx;
6802 if (shndx == elf_onesymtab (ibfd))
6803 shndx = MAP_ONESYMTAB;
6804 else if (shndx == elf_dynsymtab (ibfd))
6805 shndx = MAP_DYNSYMTAB;
6806 else if (shndx == elf_strtab_sec (ibfd))
6808 else if (shndx == elf_shstrtab_sec (ibfd))
6809 shndx = MAP_SHSTRTAB;
6810 else if (shndx == elf_symtab_shndx (ibfd))
6811 shndx = MAP_SYM_SHNDX;
6812 osym->internal_elf_sym.st_shndx = shndx;
6818 /* Swap out the symbols. */
6821 swap_out_syms (bfd *abfd,
6822 struct bfd_strtab_hash **sttp,
6825 const struct elf_backend_data *bed;
6828 struct bfd_strtab_hash *stt;
6829 Elf_Internal_Shdr *symtab_hdr;
6830 Elf_Internal_Shdr *symtab_shndx_hdr;
6831 Elf_Internal_Shdr *symstrtab_hdr;
6832 bfd_byte *outbound_syms;
6833 bfd_byte *outbound_shndx;
6835 unsigned int num_locals;
6837 bfd_boolean name_local_sections;
6839 if (!elf_map_symbols (abfd, &num_locals))
6842 /* Dump out the symtabs. */
6843 stt = _bfd_elf_stringtab_init ();
6847 bed = get_elf_backend_data (abfd);
6848 symcount = bfd_get_symcount (abfd);
6849 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6850 symtab_hdr->sh_type = SHT_SYMTAB;
6851 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6852 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6853 symtab_hdr->sh_info = num_locals + 1;
6854 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6856 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6857 symstrtab_hdr->sh_type = SHT_STRTAB;
6859 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6860 bed->s->sizeof_sym);
6861 if (outbound_syms == NULL)
6863 _bfd_stringtab_free (stt);
6866 symtab_hdr->contents = outbound_syms;
6868 outbound_shndx = NULL;
6869 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6870 if (symtab_shndx_hdr->sh_name != 0)
6872 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6873 outbound_shndx = (bfd_byte *)
6874 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6875 if (outbound_shndx == NULL)
6877 _bfd_stringtab_free (stt);
6881 symtab_shndx_hdr->contents = outbound_shndx;
6882 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6883 symtab_shndx_hdr->sh_size = amt;
6884 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6885 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6888 /* Now generate the data (for "contents"). */
6890 /* Fill in zeroth symbol and swap it out. */
6891 Elf_Internal_Sym sym;
6897 sym.st_shndx = SHN_UNDEF;
6898 sym.st_target_internal = 0;
6899 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6900 outbound_syms += bed->s->sizeof_sym;
6901 if (outbound_shndx != NULL)
6902 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6906 = (bed->elf_backend_name_local_section_symbols
6907 && bed->elf_backend_name_local_section_symbols (abfd));
6909 syms = bfd_get_outsymbols (abfd);
6910 for (idx = 0; idx < symcount; idx++)
6912 Elf_Internal_Sym sym;
6913 bfd_vma value = syms[idx]->value;
6914 elf_symbol_type *type_ptr;
6915 flagword flags = syms[idx]->flags;
6918 if (!name_local_sections
6919 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6921 /* Local section symbols have no name. */
6926 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6929 if (sym.st_name == (unsigned long) -1)
6931 _bfd_stringtab_free (stt);
6936 type_ptr = elf_symbol_from (abfd, syms[idx]);
6938 if ((flags & BSF_SECTION_SYM) == 0
6939 && bfd_is_com_section (syms[idx]->section))
6941 /* ELF common symbols put the alignment into the `value' field,
6942 and the size into the `size' field. This is backwards from
6943 how BFD handles it, so reverse it here. */
6944 sym.st_size = value;
6945 if (type_ptr == NULL
6946 || type_ptr->internal_elf_sym.st_value == 0)
6947 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6949 sym.st_value = type_ptr->internal_elf_sym.st_value;
6950 sym.st_shndx = _bfd_elf_section_from_bfd_section
6951 (abfd, syms[idx]->section);
6955 asection *sec = syms[idx]->section;
6958 if (sec->output_section)
6960 value += sec->output_offset;
6961 sec = sec->output_section;
6964 /* Don't add in the section vma for relocatable output. */
6965 if (! relocatable_p)
6967 sym.st_value = value;
6968 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6970 if (bfd_is_abs_section (sec)
6972 && type_ptr->internal_elf_sym.st_shndx != 0)
6974 /* This symbol is in a real ELF section which we did
6975 not create as a BFD section. Undo the mapping done
6976 by copy_private_symbol_data. */
6977 shndx = type_ptr->internal_elf_sym.st_shndx;
6981 shndx = elf_onesymtab (abfd);
6984 shndx = elf_dynsymtab (abfd);
6987 shndx = elf_strtab_sec (abfd);
6990 shndx = elf_shstrtab_sec (abfd);
6993 shndx = elf_symtab_shndx (abfd);
7002 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
7004 if (shndx == SHN_BAD)
7008 /* Writing this would be a hell of a lot easier if
7009 we had some decent documentation on bfd, and
7010 knew what to expect of the library, and what to
7011 demand of applications. For example, it
7012 appears that `objcopy' might not set the
7013 section of a symbol to be a section that is
7014 actually in the output file. */
7015 sec2 = bfd_get_section_by_name (abfd, sec->name);
7018 _bfd_error_handler (_("\
7019 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7020 syms[idx]->name ? syms[idx]->name : "<Local sym>",
7022 bfd_set_error (bfd_error_invalid_operation);
7023 _bfd_stringtab_free (stt);
7027 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
7028 BFD_ASSERT (shndx != SHN_BAD);
7032 sym.st_shndx = shndx;
7035 if ((flags & BSF_THREAD_LOCAL) != 0)
7037 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
7038 type = STT_GNU_IFUNC;
7039 else if ((flags & BSF_FUNCTION) != 0)
7041 else if ((flags & BSF_OBJECT) != 0)
7043 else if ((flags & BSF_RELC) != 0)
7045 else if ((flags & BSF_SRELC) != 0)
7050 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7053 /* Processor-specific types. */
7054 if (type_ptr != NULL
7055 && bed->elf_backend_get_symbol_type)
7056 type = ((*bed->elf_backend_get_symbol_type)
7057 (&type_ptr->internal_elf_sym, type));
7059 if (flags & BSF_SECTION_SYM)
7061 if (flags & BSF_GLOBAL)
7062 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7064 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7066 else if (bfd_is_com_section (syms[idx]->section))
7068 #ifdef USE_STT_COMMON
7069 if (type == STT_OBJECT)
7070 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
7073 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
7075 else if (bfd_is_und_section (syms[idx]->section))
7076 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7080 else if (flags & BSF_FILE)
7081 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7084 int bind = STB_LOCAL;
7086 if (flags & BSF_LOCAL)
7088 else if (flags & BSF_GNU_UNIQUE)
7089 bind = STB_GNU_UNIQUE;
7090 else if (flags & BSF_WEAK)
7092 else if (flags & BSF_GLOBAL)
7095 sym.st_info = ELF_ST_INFO (bind, type);
7098 if (type_ptr != NULL)
7100 sym.st_other = type_ptr->internal_elf_sym.st_other;
7101 sym.st_target_internal
7102 = type_ptr->internal_elf_sym.st_target_internal;
7107 sym.st_target_internal = 0;
7110 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
7111 outbound_syms += bed->s->sizeof_sym;
7112 if (outbound_shndx != NULL)
7113 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
7117 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
7118 symstrtab_hdr->sh_type = SHT_STRTAB;
7120 symstrtab_hdr->sh_flags = 0;
7121 symstrtab_hdr->sh_addr = 0;
7122 symstrtab_hdr->sh_entsize = 0;
7123 symstrtab_hdr->sh_link = 0;
7124 symstrtab_hdr->sh_info = 0;
7125 symstrtab_hdr->sh_addralign = 1;
7130 /* Return the number of bytes required to hold the symtab vector.
7132 Note that we base it on the count plus 1, since we will null terminate
7133 the vector allocated based on this size. However, the ELF symbol table
7134 always has a dummy entry as symbol #0, so it ends up even. */
7137 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
7141 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7143 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7144 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7146 symtab_size -= sizeof (asymbol *);
7152 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7156 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7158 if (elf_dynsymtab (abfd) == 0)
7160 bfd_set_error (bfd_error_invalid_operation);
7164 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7165 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7167 symtab_size -= sizeof (asymbol *);
7173 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7176 return (asect->reloc_count + 1) * sizeof (arelent *);
7179 /* Canonicalize the relocs. */
7182 _bfd_elf_canonicalize_reloc (bfd *abfd,
7189 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7191 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7194 tblptr = section->relocation;
7195 for (i = 0; i < section->reloc_count; i++)
7196 *relptr++ = tblptr++;
7200 return section->reloc_count;
7204 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7206 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7207 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7210 bfd_get_symcount (abfd) = symcount;
7215 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7216 asymbol **allocation)
7218 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7219 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7222 bfd_get_dynamic_symcount (abfd) = symcount;
7226 /* Return the size required for the dynamic reloc entries. Any loadable
7227 section that was actually installed in the BFD, and has type SHT_REL
7228 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7229 dynamic reloc section. */
7232 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7237 if (elf_dynsymtab (abfd) == 0)
7239 bfd_set_error (bfd_error_invalid_operation);
7243 ret = sizeof (arelent *);
7244 for (s = abfd->sections; s != NULL; s = s->next)
7245 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7246 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7247 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7248 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7249 * sizeof (arelent *));
7254 /* Canonicalize the dynamic relocation entries. Note that we return the
7255 dynamic relocations as a single block, although they are actually
7256 associated with particular sections; the interface, which was
7257 designed for SunOS style shared libraries, expects that there is only
7258 one set of dynamic relocs. Any loadable section that was actually
7259 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7260 dynamic symbol table, is considered to be a dynamic reloc section. */
7263 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7267 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7271 if (elf_dynsymtab (abfd) == 0)
7273 bfd_set_error (bfd_error_invalid_operation);
7277 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7279 for (s = abfd->sections; s != NULL; s = s->next)
7281 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7282 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7283 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7288 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7290 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7292 for (i = 0; i < count; i++)
7303 /* Read in the version information. */
7306 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7308 bfd_byte *contents = NULL;
7309 unsigned int freeidx = 0;
7311 if (elf_dynverref (abfd) != 0)
7313 Elf_Internal_Shdr *hdr;
7314 Elf_External_Verneed *everneed;
7315 Elf_Internal_Verneed *iverneed;
7317 bfd_byte *contents_end;
7319 hdr = &elf_tdata (abfd)->dynverref_hdr;
7322 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7323 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
7325 elf_tdata (abfd)->verref = NULL;
7327 if (elf_tdata (abfd)->verref == NULL)
7330 elf_tdata (abfd)->cverrefs = hdr->sh_info;
7332 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7333 if (contents == NULL)
7335 error_return_verref:
7336 elf_tdata (abfd)->verref = NULL;
7337 elf_tdata (abfd)->cverrefs = 0;
7340 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7341 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7342 goto error_return_verref;
7344 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
7345 goto error_return_verref;
7347 BFD_ASSERT (sizeof (Elf_External_Verneed)
7348 == sizeof (Elf_External_Vernaux));
7349 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7350 everneed = (Elf_External_Verneed *) contents;
7351 iverneed = elf_tdata (abfd)->verref;
7352 for (i = 0; i < hdr->sh_info; i++, iverneed++)
7354 Elf_External_Vernaux *evernaux;
7355 Elf_Internal_Vernaux *ivernaux;
7358 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7360 iverneed->vn_bfd = abfd;
7362 iverneed->vn_filename =
7363 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7365 if (iverneed->vn_filename == NULL)
7366 goto error_return_verref;
7368 if (iverneed->vn_cnt == 0)
7369 iverneed->vn_auxptr = NULL;
7372 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7373 bfd_alloc2 (abfd, iverneed->vn_cnt,
7374 sizeof (Elf_Internal_Vernaux));
7375 if (iverneed->vn_auxptr == NULL)
7376 goto error_return_verref;
7379 if (iverneed->vn_aux
7380 > (size_t) (contents_end - (bfd_byte *) everneed))
7381 goto error_return_verref;
7383 evernaux = ((Elf_External_Vernaux *)
7384 ((bfd_byte *) everneed + iverneed->vn_aux));
7385 ivernaux = iverneed->vn_auxptr;
7386 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7388 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7390 ivernaux->vna_nodename =
7391 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7392 ivernaux->vna_name);
7393 if (ivernaux->vna_nodename == NULL)
7394 goto error_return_verref;
7396 if (j + 1 < iverneed->vn_cnt)
7397 ivernaux->vna_nextptr = ivernaux + 1;
7399 ivernaux->vna_nextptr = NULL;
7401 if (ivernaux->vna_next
7402 > (size_t) (contents_end - (bfd_byte *) evernaux))
7403 goto error_return_verref;
7405 evernaux = ((Elf_External_Vernaux *)
7406 ((bfd_byte *) evernaux + ivernaux->vna_next));
7408 if (ivernaux->vna_other > freeidx)
7409 freeidx = ivernaux->vna_other;
7412 if (i + 1 < hdr->sh_info)
7413 iverneed->vn_nextref = iverneed + 1;
7415 iverneed->vn_nextref = NULL;
7417 if (iverneed->vn_next
7418 > (size_t) (contents_end - (bfd_byte *) everneed))
7419 goto error_return_verref;
7421 everneed = ((Elf_External_Verneed *)
7422 ((bfd_byte *) everneed + iverneed->vn_next));
7429 if (elf_dynverdef (abfd) != 0)
7431 Elf_Internal_Shdr *hdr;
7432 Elf_External_Verdef *everdef;
7433 Elf_Internal_Verdef *iverdef;
7434 Elf_Internal_Verdef *iverdefarr;
7435 Elf_Internal_Verdef iverdefmem;
7437 unsigned int maxidx;
7438 bfd_byte *contents_end_def, *contents_end_aux;
7440 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7442 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7443 if (contents == NULL)
7445 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7446 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7449 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
7452 BFD_ASSERT (sizeof (Elf_External_Verdef)
7453 >= sizeof (Elf_External_Verdaux));
7454 contents_end_def = contents + hdr->sh_size
7455 - sizeof (Elf_External_Verdef);
7456 contents_end_aux = contents + hdr->sh_size
7457 - sizeof (Elf_External_Verdaux);
7459 /* We know the number of entries in the section but not the maximum
7460 index. Therefore we have to run through all entries and find
7462 everdef = (Elf_External_Verdef *) contents;
7464 for (i = 0; i < hdr->sh_info; ++i)
7466 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7468 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7469 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7471 if (iverdefmem.vd_next
7472 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7475 everdef = ((Elf_External_Verdef *)
7476 ((bfd_byte *) everdef + iverdefmem.vd_next));
7479 if (default_imported_symver)
7481 if (freeidx > maxidx)
7487 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7488 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7490 elf_tdata (abfd)->verdef = NULL;
7492 if (elf_tdata (abfd)->verdef == NULL)
7495 elf_tdata (abfd)->cverdefs = maxidx;
7497 everdef = (Elf_External_Verdef *) contents;
7498 iverdefarr = elf_tdata (abfd)->verdef;
7499 for (i = 0; i < hdr->sh_info; i++)
7501 Elf_External_Verdaux *everdaux;
7502 Elf_Internal_Verdaux *iverdaux;
7505 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7507 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7509 error_return_verdef:
7510 elf_tdata (abfd)->verdef = NULL;
7511 elf_tdata (abfd)->cverdefs = 0;
7515 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7516 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7518 iverdef->vd_bfd = abfd;
7520 if (iverdef->vd_cnt == 0)
7521 iverdef->vd_auxptr = NULL;
7524 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7525 bfd_alloc2 (abfd, iverdef->vd_cnt,
7526 sizeof (Elf_Internal_Verdaux));
7527 if (iverdef->vd_auxptr == NULL)
7528 goto error_return_verdef;
7532 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7533 goto error_return_verdef;
7535 everdaux = ((Elf_External_Verdaux *)
7536 ((bfd_byte *) everdef + iverdef->vd_aux));
7537 iverdaux = iverdef->vd_auxptr;
7538 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7540 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7542 iverdaux->vda_nodename =
7543 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7544 iverdaux->vda_name);
7545 if (iverdaux->vda_nodename == NULL)
7546 goto error_return_verdef;
7548 if (j + 1 < iverdef->vd_cnt)
7549 iverdaux->vda_nextptr = iverdaux + 1;
7551 iverdaux->vda_nextptr = NULL;
7553 if (iverdaux->vda_next
7554 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7555 goto error_return_verdef;
7557 everdaux = ((Elf_External_Verdaux *)
7558 ((bfd_byte *) everdaux + iverdaux->vda_next));
7561 if (iverdef->vd_cnt)
7562 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7564 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7565 iverdef->vd_nextdef = iverdef + 1;
7567 iverdef->vd_nextdef = NULL;
7569 everdef = ((Elf_External_Verdef *)
7570 ((bfd_byte *) everdef + iverdef->vd_next));
7576 else if (default_imported_symver)
7583 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7584 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7585 if (elf_tdata (abfd)->verdef == NULL)
7588 elf_tdata (abfd)->cverdefs = freeidx;
7591 /* Create a default version based on the soname. */
7592 if (default_imported_symver)
7594 Elf_Internal_Verdef *iverdef;
7595 Elf_Internal_Verdaux *iverdaux;
7597 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
7599 iverdef->vd_version = VER_DEF_CURRENT;
7600 iverdef->vd_flags = 0;
7601 iverdef->vd_ndx = freeidx;
7602 iverdef->vd_cnt = 1;
7604 iverdef->vd_bfd = abfd;
7606 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7607 if (iverdef->vd_nodename == NULL)
7608 goto error_return_verdef;
7609 iverdef->vd_nextdef = NULL;
7610 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7611 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7612 if (iverdef->vd_auxptr == NULL)
7613 goto error_return_verdef;
7615 iverdaux = iverdef->vd_auxptr;
7616 iverdaux->vda_nodename = iverdef->vd_nodename;
7617 iverdaux->vda_nextptr = NULL;
7623 if (contents != NULL)
7629 _bfd_elf_make_empty_symbol (bfd *abfd)
7631 elf_symbol_type *newsym;
7633 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
7636 newsym->symbol.the_bfd = abfd;
7637 return &newsym->symbol;
7641 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7645 bfd_symbol_info (symbol, ret);
7648 /* Return whether a symbol name implies a local symbol. Most targets
7649 use this function for the is_local_label_name entry point, but some
7653 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7656 /* Normal local symbols start with ``.L''. */
7657 if (name[0] == '.' && name[1] == 'L')
7660 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7661 DWARF debugging symbols starting with ``..''. */
7662 if (name[0] == '.' && name[1] == '.')
7665 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7666 emitting DWARF debugging output. I suspect this is actually a
7667 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7668 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7669 underscore to be emitted on some ELF targets). For ease of use,
7670 we treat such symbols as local. */
7671 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7678 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7679 asymbol *symbol ATTRIBUTE_UNUSED)
7686 _bfd_elf_set_arch_mach (bfd *abfd,
7687 enum bfd_architecture arch,
7688 unsigned long machine)
7690 /* If this isn't the right architecture for this backend, and this
7691 isn't the generic backend, fail. */
7692 if (arch != get_elf_backend_data (abfd)->arch
7693 && arch != bfd_arch_unknown
7694 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7697 return bfd_default_set_arch_mach (abfd, arch, machine);
7700 /* Find the nearest line to a particular section and offset,
7701 for error reporting. */
7704 _bfd_elf_find_nearest_line (bfd *abfd,
7708 const char **filename_ptr,
7709 const char **functionname_ptr,
7710 unsigned int *line_ptr,
7711 unsigned int *discriminator_ptr)
7715 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7716 filename_ptr, functionname_ptr,
7717 line_ptr, discriminator_ptr,
7718 dwarf_debug_sections, 0,
7719 &elf_tdata (abfd)->dwarf2_find_line_info)
7720 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
7721 filename_ptr, functionname_ptr,
7724 if (!*functionname_ptr)
7725 _bfd_elf_find_function (abfd, symbols, section, offset,
7726 *filename_ptr ? NULL : filename_ptr,
7731 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7732 &found, filename_ptr,
7733 functionname_ptr, line_ptr,
7734 &elf_tdata (abfd)->line_info))
7736 if (found && (*functionname_ptr || *line_ptr))
7739 if (symbols == NULL)
7742 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
7743 filename_ptr, functionname_ptr))
7750 /* Find the line for a symbol. */
7753 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7754 const char **filename_ptr, unsigned int *line_ptr)
7756 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
7757 filename_ptr, NULL, line_ptr, NULL,
7758 dwarf_debug_sections, 0,
7759 &elf_tdata (abfd)->dwarf2_find_line_info);
7762 /* After a call to bfd_find_nearest_line, successive calls to
7763 bfd_find_inliner_info can be used to get source information about
7764 each level of function inlining that terminated at the address
7765 passed to bfd_find_nearest_line. Currently this is only supported
7766 for DWARF2 with appropriate DWARF3 extensions. */
7769 _bfd_elf_find_inliner_info (bfd *abfd,
7770 const char **filename_ptr,
7771 const char **functionname_ptr,
7772 unsigned int *line_ptr)
7775 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7776 functionname_ptr, line_ptr,
7777 & elf_tdata (abfd)->dwarf2_find_line_info);
7782 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7784 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7785 int ret = bed->s->sizeof_ehdr;
7787 if (!info->relocatable)
7789 bfd_size_type phdr_size = elf_program_header_size (abfd);
7791 if (phdr_size == (bfd_size_type) -1)
7793 struct elf_segment_map *m;
7796 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
7797 phdr_size += bed->s->sizeof_phdr;
7800 phdr_size = get_program_header_size (abfd, info);
7803 elf_program_header_size (abfd) = phdr_size;
7811 _bfd_elf_set_section_contents (bfd *abfd,
7813 const void *location,
7815 bfd_size_type count)
7817 Elf_Internal_Shdr *hdr;
7820 if (! abfd->output_has_begun
7821 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7824 hdr = &elf_section_data (section)->this_hdr;
7825 pos = hdr->sh_offset + offset;
7826 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7827 || bfd_bwrite (location, count, abfd) != count)
7834 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7835 arelent *cache_ptr ATTRIBUTE_UNUSED,
7836 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7841 /* Try to convert a non-ELF reloc into an ELF one. */
7844 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7846 /* Check whether we really have an ELF howto. */
7848 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7850 bfd_reloc_code_real_type code;
7851 reloc_howto_type *howto;
7853 /* Alien reloc: Try to determine its type to replace it with an
7854 equivalent ELF reloc. */
7856 if (areloc->howto->pc_relative)
7858 switch (areloc->howto->bitsize)
7861 code = BFD_RELOC_8_PCREL;
7864 code = BFD_RELOC_12_PCREL;
7867 code = BFD_RELOC_16_PCREL;
7870 code = BFD_RELOC_24_PCREL;
7873 code = BFD_RELOC_32_PCREL;
7876 code = BFD_RELOC_64_PCREL;
7882 howto = bfd_reloc_type_lookup (abfd, code);
7884 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7886 if (howto->pcrel_offset)
7887 areloc->addend += areloc->address;
7889 areloc->addend -= areloc->address; /* addend is unsigned!! */
7894 switch (areloc->howto->bitsize)
7900 code = BFD_RELOC_14;
7903 code = BFD_RELOC_16;
7906 code = BFD_RELOC_26;
7909 code = BFD_RELOC_32;
7912 code = BFD_RELOC_64;
7918 howto = bfd_reloc_type_lookup (abfd, code);
7922 areloc->howto = howto;
7930 (*_bfd_error_handler)
7931 (_("%B: unsupported relocation type %s"),
7932 abfd, areloc->howto->name);
7933 bfd_set_error (bfd_error_bad_value);
7938 _bfd_elf_close_and_cleanup (bfd *abfd)
7940 struct elf_obj_tdata *tdata = elf_tdata (abfd);
7941 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
7943 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
7944 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7945 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
7948 return _bfd_generic_close_and_cleanup (abfd);
7951 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7952 in the relocation's offset. Thus we cannot allow any sort of sanity
7953 range-checking to interfere. There is nothing else to do in processing
7956 bfd_reloc_status_type
7957 _bfd_elf_rel_vtable_reloc_fn
7958 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7959 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7960 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7961 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7963 return bfd_reloc_ok;
7966 /* Elf core file support. Much of this only works on native
7967 toolchains, since we rely on knowing the
7968 machine-dependent procfs structure in order to pick
7969 out details about the corefile. */
7971 #ifdef HAVE_SYS_PROCFS_H
7972 /* Needed for new procfs interface on sparc-solaris. */
7973 # define _STRUCTURED_PROC 1
7974 # include <sys/procfs.h>
7977 /* Return a PID that identifies a "thread" for threaded cores, or the
7978 PID of the main process for non-threaded cores. */
7981 elfcore_make_pid (bfd *abfd)
7985 pid = elf_tdata (abfd)->core->lwpid;
7987 pid = elf_tdata (abfd)->core->pid;
7992 /* If there isn't a section called NAME, make one, using
7993 data from SECT. Note, this function will generate a
7994 reference to NAME, so you shouldn't deallocate or
7998 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
8002 if (bfd_get_section_by_name (abfd, name) != NULL)
8005 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
8009 sect2->size = sect->size;
8010 sect2->filepos = sect->filepos;
8011 sect2->alignment_power = sect->alignment_power;
8015 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8016 actually creates up to two pseudosections:
8017 - For the single-threaded case, a section named NAME, unless
8018 such a section already exists.
8019 - For the multi-threaded case, a section named "NAME/PID", where
8020 PID is elfcore_make_pid (abfd).
8021 Both pseudosections have identical contents. */
8023 _bfd_elfcore_make_pseudosection (bfd *abfd,
8029 char *threaded_name;
8033 /* Build the section name. */
8035 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8036 len = strlen (buf) + 1;
8037 threaded_name = (char *) bfd_alloc (abfd, len);
8038 if (threaded_name == NULL)
8040 memcpy (threaded_name, buf, len);
8042 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8047 sect->filepos = filepos;
8048 sect->alignment_power = 2;
8050 return elfcore_maybe_make_sect (abfd, name, sect);
8053 /* prstatus_t exists on:
8055 linux 2.[01] + glibc
8059 #if defined (HAVE_PRSTATUS_T)
8062 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8067 if (note->descsz == sizeof (prstatus_t))
8071 size = sizeof (prstat.pr_reg);
8072 offset = offsetof (prstatus_t, pr_reg);
8073 memcpy (&prstat, note->descdata, sizeof (prstat));
8075 /* Do not overwrite the core signal if it
8076 has already been set by another thread. */
8077 if (elf_tdata (abfd)->core->signal == 0)
8078 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8079 if (elf_tdata (abfd)->core->pid == 0)
8080 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8082 /* pr_who exists on:
8085 pr_who doesn't exist on:
8088 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8089 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8091 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8094 #if defined (HAVE_PRSTATUS32_T)
8095 else if (note->descsz == sizeof (prstatus32_t))
8097 /* 64-bit host, 32-bit corefile */
8098 prstatus32_t prstat;
8100 size = sizeof (prstat.pr_reg);
8101 offset = offsetof (prstatus32_t, pr_reg);
8102 memcpy (&prstat, note->descdata, sizeof (prstat));
8104 /* Do not overwrite the core signal if it
8105 has already been set by another thread. */
8106 if (elf_tdata (abfd)->core->signal == 0)
8107 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8108 if (elf_tdata (abfd)->core->pid == 0)
8109 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8111 /* pr_who exists on:
8114 pr_who doesn't exist on:
8117 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8118 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8120 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8123 #endif /* HAVE_PRSTATUS32_T */
8126 /* Fail - we don't know how to handle any other
8127 note size (ie. data object type). */
8131 /* Make a ".reg/999" section and a ".reg" section. */
8132 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8133 size, note->descpos + offset);
8135 #endif /* defined (HAVE_PRSTATUS_T) */
8137 /* Create a pseudosection containing the exact contents of NOTE. */
8139 elfcore_make_note_pseudosection (bfd *abfd,
8141 Elf_Internal_Note *note)
8143 return _bfd_elfcore_make_pseudosection (abfd, name,
8144 note->descsz, note->descpos);
8147 /* There isn't a consistent prfpregset_t across platforms,
8148 but it doesn't matter, because we don't have to pick this
8149 data structure apart. */
8152 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8154 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8157 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8158 type of NT_PRXFPREG. Just include the whole note's contents
8162 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8164 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8167 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8168 with a note type of NT_X86_XSTATE. Just include the whole note's
8169 contents literally. */
8172 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8174 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8178 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8180 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8184 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8186 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8190 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8192 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8196 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8198 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8202 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8204 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8208 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8210 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8214 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8216 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8220 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8222 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8226 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8228 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8232 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8234 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8238 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8240 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8244 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8246 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8250 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8252 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8256 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8258 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8262 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8264 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8267 #if defined (HAVE_PRPSINFO_T)
8268 typedef prpsinfo_t elfcore_psinfo_t;
8269 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8270 typedef prpsinfo32_t elfcore_psinfo32_t;
8274 #if defined (HAVE_PSINFO_T)
8275 typedef psinfo_t elfcore_psinfo_t;
8276 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8277 typedef psinfo32_t elfcore_psinfo32_t;
8281 /* return a malloc'ed copy of a string at START which is at
8282 most MAX bytes long, possibly without a terminating '\0'.
8283 the copy will always have a terminating '\0'. */
8286 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8289 char *end = (char *) memchr (start, '\0', max);
8297 dups = (char *) bfd_alloc (abfd, len + 1);
8301 memcpy (dups, start, len);
8307 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8309 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8311 if (note->descsz == sizeof (elfcore_psinfo_t))
8313 elfcore_psinfo_t psinfo;
8315 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8317 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8318 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8320 elf_tdata (abfd)->core->program
8321 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8322 sizeof (psinfo.pr_fname));
8324 elf_tdata (abfd)->core->command
8325 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8326 sizeof (psinfo.pr_psargs));
8328 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8329 else if (note->descsz == sizeof (elfcore_psinfo32_t))
8331 /* 64-bit host, 32-bit corefile */
8332 elfcore_psinfo32_t psinfo;
8334 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8336 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8337 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8339 elf_tdata (abfd)->core->program
8340 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8341 sizeof (psinfo.pr_fname));
8343 elf_tdata (abfd)->core->command
8344 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8345 sizeof (psinfo.pr_psargs));
8351 /* Fail - we don't know how to handle any other
8352 note size (ie. data object type). */
8356 /* Note that for some reason, a spurious space is tacked
8357 onto the end of the args in some (at least one anyway)
8358 implementations, so strip it off if it exists. */
8361 char *command = elf_tdata (abfd)->core->command;
8362 int n = strlen (command);
8364 if (0 < n && command[n - 1] == ' ')
8365 command[n - 1] = '\0';
8370 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8372 #if defined (HAVE_PSTATUS_T)
8374 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8376 if (note->descsz == sizeof (pstatus_t)
8377 #if defined (HAVE_PXSTATUS_T)
8378 || note->descsz == sizeof (pxstatus_t)
8384 memcpy (&pstat, note->descdata, sizeof (pstat));
8386 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8388 #if defined (HAVE_PSTATUS32_T)
8389 else if (note->descsz == sizeof (pstatus32_t))
8391 /* 64-bit host, 32-bit corefile */
8394 memcpy (&pstat, note->descdata, sizeof (pstat));
8396 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8399 /* Could grab some more details from the "representative"
8400 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8401 NT_LWPSTATUS note, presumably. */
8405 #endif /* defined (HAVE_PSTATUS_T) */
8407 #if defined (HAVE_LWPSTATUS_T)
8409 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8411 lwpstatus_t lwpstat;
8417 if (note->descsz != sizeof (lwpstat)
8418 #if defined (HAVE_LWPXSTATUS_T)
8419 && note->descsz != sizeof (lwpxstatus_t)
8424 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8426 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8427 /* Do not overwrite the core signal if it has already been set by
8429 if (elf_tdata (abfd)->core->signal == 0)
8430 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8432 /* Make a ".reg/999" section. */
8434 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8435 len = strlen (buf) + 1;
8436 name = bfd_alloc (abfd, len);
8439 memcpy (name, buf, len);
8441 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8445 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8446 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8447 sect->filepos = note->descpos
8448 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8451 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8452 sect->size = sizeof (lwpstat.pr_reg);
8453 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8456 sect->alignment_power = 2;
8458 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8461 /* Make a ".reg2/999" section */
8463 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8464 len = strlen (buf) + 1;
8465 name = bfd_alloc (abfd, len);
8468 memcpy (name, buf, len);
8470 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8474 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8475 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8476 sect->filepos = note->descpos
8477 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8480 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8481 sect->size = sizeof (lwpstat.pr_fpreg);
8482 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8485 sect->alignment_power = 2;
8487 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8489 #endif /* defined (HAVE_LWPSTATUS_T) */
8492 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8499 int is_active_thread;
8502 if (note->descsz < 728)
8505 if (! CONST_STRNEQ (note->namedata, "win32"))
8508 type = bfd_get_32 (abfd, note->descdata);
8512 case 1 /* NOTE_INFO_PROCESS */:
8513 /* FIXME: need to add ->core->command. */
8514 /* process_info.pid */
8515 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
8516 /* process_info.signal */
8517 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
8520 case 2 /* NOTE_INFO_THREAD */:
8521 /* Make a ".reg/999" section. */
8522 /* thread_info.tid */
8523 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8525 len = strlen (buf) + 1;
8526 name = (char *) bfd_alloc (abfd, len);
8530 memcpy (name, buf, len);
8532 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8536 /* sizeof (thread_info.thread_context) */
8538 /* offsetof (thread_info.thread_context) */
8539 sect->filepos = note->descpos + 12;
8540 sect->alignment_power = 2;
8542 /* thread_info.is_active_thread */
8543 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8545 if (is_active_thread)
8546 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8550 case 3 /* NOTE_INFO_MODULE */:
8551 /* Make a ".module/xxxxxxxx" section. */
8552 /* module_info.base_address */
8553 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8554 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8556 len = strlen (buf) + 1;
8557 name = (char *) bfd_alloc (abfd, len);
8561 memcpy (name, buf, len);
8563 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8568 sect->size = note->descsz;
8569 sect->filepos = note->descpos;
8570 sect->alignment_power = 2;
8581 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8583 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8591 if (bed->elf_backend_grok_prstatus)
8592 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8594 #if defined (HAVE_PRSTATUS_T)
8595 return elfcore_grok_prstatus (abfd, note);
8600 #if defined (HAVE_PSTATUS_T)
8602 return elfcore_grok_pstatus (abfd, note);
8605 #if defined (HAVE_LWPSTATUS_T)
8607 return elfcore_grok_lwpstatus (abfd, note);
8610 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8611 return elfcore_grok_prfpreg (abfd, note);
8613 case NT_WIN32PSTATUS:
8614 return elfcore_grok_win32pstatus (abfd, note);
8616 case NT_PRXFPREG: /* Linux SSE extension */
8617 if (note->namesz == 6
8618 && strcmp (note->namedata, "LINUX") == 0)
8619 return elfcore_grok_prxfpreg (abfd, note);
8623 case NT_X86_XSTATE: /* Linux XSAVE extension */
8624 if (note->namesz == 6
8625 && strcmp (note->namedata, "LINUX") == 0)
8626 return elfcore_grok_xstatereg (abfd, note);
8631 if (note->namesz == 6
8632 && strcmp (note->namedata, "LINUX") == 0)
8633 return elfcore_grok_ppc_vmx (abfd, note);
8638 if (note->namesz == 6
8639 && strcmp (note->namedata, "LINUX") == 0)
8640 return elfcore_grok_ppc_vsx (abfd, note);
8644 case NT_S390_HIGH_GPRS:
8645 if (note->namesz == 6
8646 && strcmp (note->namedata, "LINUX") == 0)
8647 return elfcore_grok_s390_high_gprs (abfd, note);
8652 if (note->namesz == 6
8653 && strcmp (note->namedata, "LINUX") == 0)
8654 return elfcore_grok_s390_timer (abfd, note);
8658 case NT_S390_TODCMP:
8659 if (note->namesz == 6
8660 && strcmp (note->namedata, "LINUX") == 0)
8661 return elfcore_grok_s390_todcmp (abfd, note);
8665 case NT_S390_TODPREG:
8666 if (note->namesz == 6
8667 && strcmp (note->namedata, "LINUX") == 0)
8668 return elfcore_grok_s390_todpreg (abfd, note);
8673 if (note->namesz == 6
8674 && strcmp (note->namedata, "LINUX") == 0)
8675 return elfcore_grok_s390_ctrs (abfd, note);
8679 case NT_S390_PREFIX:
8680 if (note->namesz == 6
8681 && strcmp (note->namedata, "LINUX") == 0)
8682 return elfcore_grok_s390_prefix (abfd, note);
8686 case NT_S390_LAST_BREAK:
8687 if (note->namesz == 6
8688 && strcmp (note->namedata, "LINUX") == 0)
8689 return elfcore_grok_s390_last_break (abfd, note);
8693 case NT_S390_SYSTEM_CALL:
8694 if (note->namesz == 6
8695 && strcmp (note->namedata, "LINUX") == 0)
8696 return elfcore_grok_s390_system_call (abfd, note);
8701 if (note->namesz == 6
8702 && strcmp (note->namedata, "LINUX") == 0)
8703 return elfcore_grok_s390_tdb (abfd, note);
8708 if (note->namesz == 6
8709 && strcmp (note->namedata, "LINUX") == 0)
8710 return elfcore_grok_arm_vfp (abfd, note);
8715 if (note->namesz == 6
8716 && strcmp (note->namedata, "LINUX") == 0)
8717 return elfcore_grok_aarch_tls (abfd, note);
8721 case NT_ARM_HW_BREAK:
8722 if (note->namesz == 6
8723 && strcmp (note->namedata, "LINUX") == 0)
8724 return elfcore_grok_aarch_hw_break (abfd, note);
8728 case NT_ARM_HW_WATCH:
8729 if (note->namesz == 6
8730 && strcmp (note->namedata, "LINUX") == 0)
8731 return elfcore_grok_aarch_hw_watch (abfd, note);
8737 if (bed->elf_backend_grok_psinfo)
8738 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8740 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8741 return elfcore_grok_psinfo (abfd, note);
8748 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8753 sect->size = note->descsz;
8754 sect->filepos = note->descpos;
8755 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8761 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
8765 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
8771 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8773 struct elf_obj_tdata *t;
8775 if (note->descsz == 0)
8778 t = elf_tdata (abfd);
8779 t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
8780 if (t->build_id == NULL)
8783 t->build_id->size = note->descsz;
8784 memcpy (t->build_id->data, note->descdata, note->descsz);
8790 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8797 case NT_GNU_BUILD_ID:
8798 return elfobj_grok_gnu_build_id (abfd, note);
8803 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
8805 struct sdt_note *cur =
8806 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
8809 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
8810 cur->size = (bfd_size_type) note->descsz;
8811 memcpy (cur->data, note->descdata, note->descsz);
8813 elf_tdata (abfd)->sdt_note_head = cur;
8819 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
8824 return elfobj_grok_stapsdt_note_1 (abfd, note);
8832 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8836 cp = strchr (note->namedata, '@');
8839 *lwpidp = atoi(cp + 1);
8846 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8848 /* Signal number at offset 0x08. */
8849 elf_tdata (abfd)->core->signal
8850 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8852 /* Process ID at offset 0x50. */
8853 elf_tdata (abfd)->core->pid
8854 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8856 /* Command name at 0x7c (max 32 bytes, including nul). */
8857 elf_tdata (abfd)->core->command
8858 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8860 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8865 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8869 if (elfcore_netbsd_get_lwpid (note, &lwp))
8870 elf_tdata (abfd)->core->lwpid = lwp;
8872 if (note->type == NT_NETBSDCORE_PROCINFO)
8874 /* NetBSD-specific core "procinfo". Note that we expect to
8875 find this note before any of the others, which is fine,
8876 since the kernel writes this note out first when it
8877 creates a core file. */
8879 return elfcore_grok_netbsd_procinfo (abfd, note);
8882 /* As of Jan 2002 there are no other machine-independent notes
8883 defined for NetBSD core files. If the note type is less
8884 than the start of the machine-dependent note types, we don't
8887 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8891 switch (bfd_get_arch (abfd))
8893 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8894 PT_GETFPREGS == mach+2. */
8896 case bfd_arch_alpha:
8897 case bfd_arch_sparc:
8900 case NT_NETBSDCORE_FIRSTMACH+0:
8901 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8903 case NT_NETBSDCORE_FIRSTMACH+2:
8904 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8910 /* On all other arch's, PT_GETREGS == mach+1 and
8911 PT_GETFPREGS == mach+3. */
8916 case NT_NETBSDCORE_FIRSTMACH+1:
8917 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8919 case NT_NETBSDCORE_FIRSTMACH+3:
8920 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8930 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8932 /* Signal number at offset 0x08. */
8933 elf_tdata (abfd)->core->signal
8934 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8936 /* Process ID at offset 0x20. */
8937 elf_tdata (abfd)->core->pid
8938 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8940 /* Command name at 0x48 (max 32 bytes, including nul). */
8941 elf_tdata (abfd)->core->command
8942 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8948 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8950 if (note->type == NT_OPENBSD_PROCINFO)
8951 return elfcore_grok_openbsd_procinfo (abfd, note);
8953 if (note->type == NT_OPENBSD_REGS)
8954 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8956 if (note->type == NT_OPENBSD_FPREGS)
8957 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8959 if (note->type == NT_OPENBSD_XFPREGS)
8960 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8962 if (note->type == NT_OPENBSD_AUXV)
8964 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8969 sect->size = note->descsz;
8970 sect->filepos = note->descpos;
8971 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8976 if (note->type == NT_OPENBSD_WCOOKIE)
8978 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8983 sect->size = note->descsz;
8984 sect->filepos = note->descpos;
8985 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8994 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8996 void *ddata = note->descdata;
9003 /* nto_procfs_status 'pid' field is at offset 0. */
9004 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
9006 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9007 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
9009 /* nto_procfs_status 'flags' field is at offset 8. */
9010 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
9012 /* nto_procfs_status 'what' field is at offset 14. */
9013 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9015 elf_tdata (abfd)->core->signal = sig;
9016 elf_tdata (abfd)->core->lwpid = *tid;
9019 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9020 do not come from signals so we make sure we set the current
9021 thread just in case. */
9022 if (flags & 0x00000080)
9023 elf_tdata (abfd)->core->lwpid = *tid;
9025 /* Make a ".qnx_core_status/%d" section. */
9026 sprintf (buf, ".qnx_core_status/%ld", *tid);
9028 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9033 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9037 sect->size = note->descsz;
9038 sect->filepos = note->descpos;
9039 sect->alignment_power = 2;
9041 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9045 elfcore_grok_nto_regs (bfd *abfd,
9046 Elf_Internal_Note *note,
9054 /* Make a "(base)/%d" section. */
9055 sprintf (buf, "%s/%ld", base, tid);
9057 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9062 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9066 sect->size = note->descsz;
9067 sect->filepos = note->descpos;
9068 sect->alignment_power = 2;
9070 /* This is the current thread. */
9071 if (elf_tdata (abfd)->core->lwpid == tid)
9072 return elfcore_maybe_make_sect (abfd, base, sect);
9077 #define BFD_QNT_CORE_INFO 7
9078 #define BFD_QNT_CORE_STATUS 8
9079 #define BFD_QNT_CORE_GREG 9
9080 #define BFD_QNT_CORE_FPREG 10
9083 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9085 /* Every GREG section has a STATUS section before it. Store the
9086 tid from the previous call to pass down to the next gregs
9088 static long tid = 1;
9092 case BFD_QNT_CORE_INFO:
9093 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9094 case BFD_QNT_CORE_STATUS:
9095 return elfcore_grok_nto_status (abfd, note, &tid);
9096 case BFD_QNT_CORE_GREG:
9097 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9098 case BFD_QNT_CORE_FPREG:
9099 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9106 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9112 /* Use note name as section name. */
9114 name = (char *) bfd_alloc (abfd, len);
9117 memcpy (name, note->namedata, len);
9118 name[len - 1] = '\0';
9120 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9124 sect->size = note->descsz;
9125 sect->filepos = note->descpos;
9126 sect->alignment_power = 1;
9131 /* Function: elfcore_write_note
9134 buffer to hold note, and current size of buffer
9138 size of data for note
9140 Writes note to end of buffer. ELF64 notes are written exactly as
9141 for ELF32, despite the current (as of 2006) ELF gabi specifying
9142 that they ought to have 8-byte namesz and descsz field, and have
9143 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9146 Pointer to realloc'd buffer, *BUFSIZ updated. */
9149 elfcore_write_note (bfd *abfd,
9157 Elf_External_Note *xnp;
9164 namesz = strlen (name) + 1;
9166 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9168 buf = (char *) realloc (buf, *bufsiz + newspace);
9171 dest = buf + *bufsiz;
9172 *bufsiz += newspace;
9173 xnp = (Elf_External_Note *) dest;
9174 H_PUT_32 (abfd, namesz, xnp->namesz);
9175 H_PUT_32 (abfd, size, xnp->descsz);
9176 H_PUT_32 (abfd, type, xnp->type);
9180 memcpy (dest, name, namesz);
9188 memcpy (dest, input, size);
9199 elfcore_write_prpsinfo (bfd *abfd,
9205 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9207 if (bed->elf_backend_write_core_note != NULL)
9210 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9211 NT_PRPSINFO, fname, psargs);
9216 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9217 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9218 if (bed->s->elfclass == ELFCLASS32)
9220 #if defined (HAVE_PSINFO32_T)
9222 int note_type = NT_PSINFO;
9225 int note_type = NT_PRPSINFO;
9228 memset (&data, 0, sizeof (data));
9229 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9230 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9231 return elfcore_write_note (abfd, buf, bufsiz,
9232 "CORE", note_type, &data, sizeof (data));
9237 #if defined (HAVE_PSINFO_T)
9239 int note_type = NT_PSINFO;
9242 int note_type = NT_PRPSINFO;
9245 memset (&data, 0, sizeof (data));
9246 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9247 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9248 return elfcore_write_note (abfd, buf, bufsiz,
9249 "CORE", note_type, &data, sizeof (data));
9251 #endif /* PSINFO_T or PRPSINFO_T */
9258 elfcore_write_linux_prpsinfo32
9259 (bfd *abfd, char *buf, int *bufsiz,
9260 const struct elf_internal_linux_prpsinfo *prpsinfo)
9262 struct elf_external_linux_prpsinfo32 data;
9264 memset (&data, 0, sizeof (data));
9265 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9267 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9268 &data, sizeof (data));
9272 elfcore_write_linux_prpsinfo64
9273 (bfd *abfd, char *buf, int *bufsiz,
9274 const struct elf_internal_linux_prpsinfo *prpsinfo)
9276 struct elf_external_linux_prpsinfo64 data;
9278 memset (&data, 0, sizeof (data));
9279 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9281 return elfcore_write_note (abfd, buf, bufsiz,
9282 "CORE", NT_PRPSINFO, &data, sizeof (data));
9286 elfcore_write_prstatus (bfd *abfd,
9293 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9295 if (bed->elf_backend_write_core_note != NULL)
9298 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9300 pid, cursig, gregs);
9305 #if defined (HAVE_PRSTATUS_T)
9306 #if defined (HAVE_PRSTATUS32_T)
9307 if (bed->s->elfclass == ELFCLASS32)
9309 prstatus32_t prstat;
9311 memset (&prstat, 0, sizeof (prstat));
9312 prstat.pr_pid = pid;
9313 prstat.pr_cursig = cursig;
9314 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9315 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9316 NT_PRSTATUS, &prstat, sizeof (prstat));
9323 memset (&prstat, 0, sizeof (prstat));
9324 prstat.pr_pid = pid;
9325 prstat.pr_cursig = cursig;
9326 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9327 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9328 NT_PRSTATUS, &prstat, sizeof (prstat));
9330 #endif /* HAVE_PRSTATUS_T */
9336 #if defined (HAVE_LWPSTATUS_T)
9338 elfcore_write_lwpstatus (bfd *abfd,
9345 lwpstatus_t lwpstat;
9346 const char *note_name = "CORE";
9348 memset (&lwpstat, 0, sizeof (lwpstat));
9349 lwpstat.pr_lwpid = pid >> 16;
9350 lwpstat.pr_cursig = cursig;
9351 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9352 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9353 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9355 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9356 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9358 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9359 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9362 return elfcore_write_note (abfd, buf, bufsiz, note_name,
9363 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9365 #endif /* HAVE_LWPSTATUS_T */
9367 #if defined (HAVE_PSTATUS_T)
9369 elfcore_write_pstatus (bfd *abfd,
9373 int cursig ATTRIBUTE_UNUSED,
9374 const void *gregs ATTRIBUTE_UNUSED)
9376 const char *note_name = "CORE";
9377 #if defined (HAVE_PSTATUS32_T)
9378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9380 if (bed->s->elfclass == ELFCLASS32)
9384 memset (&pstat, 0, sizeof (pstat));
9385 pstat.pr_pid = pid & 0xffff;
9386 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9387 NT_PSTATUS, &pstat, sizeof (pstat));
9395 memset (&pstat, 0, sizeof (pstat));
9396 pstat.pr_pid = pid & 0xffff;
9397 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9398 NT_PSTATUS, &pstat, sizeof (pstat));
9402 #endif /* HAVE_PSTATUS_T */
9405 elfcore_write_prfpreg (bfd *abfd,
9411 const char *note_name = "CORE";
9412 return elfcore_write_note (abfd, buf, bufsiz,
9413 note_name, NT_FPREGSET, fpregs, size);
9417 elfcore_write_prxfpreg (bfd *abfd,
9420 const void *xfpregs,
9423 char *note_name = "LINUX";
9424 return elfcore_write_note (abfd, buf, bufsiz,
9425 note_name, NT_PRXFPREG, xfpregs, size);
9429 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9430 const void *xfpregs, int size)
9432 char *note_name = "LINUX";
9433 return elfcore_write_note (abfd, buf, bufsiz,
9434 note_name, NT_X86_XSTATE, xfpregs, size);
9438 elfcore_write_ppc_vmx (bfd *abfd,
9441 const void *ppc_vmx,
9444 char *note_name = "LINUX";
9445 return elfcore_write_note (abfd, buf, bufsiz,
9446 note_name, NT_PPC_VMX, ppc_vmx, size);
9450 elfcore_write_ppc_vsx (bfd *abfd,
9453 const void *ppc_vsx,
9456 char *note_name = "LINUX";
9457 return elfcore_write_note (abfd, buf, bufsiz,
9458 note_name, NT_PPC_VSX, ppc_vsx, size);
9462 elfcore_write_s390_high_gprs (bfd *abfd,
9465 const void *s390_high_gprs,
9468 char *note_name = "LINUX";
9469 return elfcore_write_note (abfd, buf, bufsiz,
9470 note_name, NT_S390_HIGH_GPRS,
9471 s390_high_gprs, size);
9475 elfcore_write_s390_timer (bfd *abfd,
9478 const void *s390_timer,
9481 char *note_name = "LINUX";
9482 return elfcore_write_note (abfd, buf, bufsiz,
9483 note_name, NT_S390_TIMER, s390_timer, size);
9487 elfcore_write_s390_todcmp (bfd *abfd,
9490 const void *s390_todcmp,
9493 char *note_name = "LINUX";
9494 return elfcore_write_note (abfd, buf, bufsiz,
9495 note_name, NT_S390_TODCMP, s390_todcmp, size);
9499 elfcore_write_s390_todpreg (bfd *abfd,
9502 const void *s390_todpreg,
9505 char *note_name = "LINUX";
9506 return elfcore_write_note (abfd, buf, bufsiz,
9507 note_name, NT_S390_TODPREG, s390_todpreg, size);
9511 elfcore_write_s390_ctrs (bfd *abfd,
9514 const void *s390_ctrs,
9517 char *note_name = "LINUX";
9518 return elfcore_write_note (abfd, buf, bufsiz,
9519 note_name, NT_S390_CTRS, s390_ctrs, size);
9523 elfcore_write_s390_prefix (bfd *abfd,
9526 const void *s390_prefix,
9529 char *note_name = "LINUX";
9530 return elfcore_write_note (abfd, buf, bufsiz,
9531 note_name, NT_S390_PREFIX, s390_prefix, size);
9535 elfcore_write_s390_last_break (bfd *abfd,
9538 const void *s390_last_break,
9541 char *note_name = "LINUX";
9542 return elfcore_write_note (abfd, buf, bufsiz,
9543 note_name, NT_S390_LAST_BREAK,
9544 s390_last_break, size);
9548 elfcore_write_s390_system_call (bfd *abfd,
9551 const void *s390_system_call,
9554 char *note_name = "LINUX";
9555 return elfcore_write_note (abfd, buf, bufsiz,
9556 note_name, NT_S390_SYSTEM_CALL,
9557 s390_system_call, size);
9561 elfcore_write_s390_tdb (bfd *abfd,
9564 const void *s390_tdb,
9567 char *note_name = "LINUX";
9568 return elfcore_write_note (abfd, buf, bufsiz,
9569 note_name, NT_S390_TDB, s390_tdb, size);
9573 elfcore_write_arm_vfp (bfd *abfd,
9576 const void *arm_vfp,
9579 char *note_name = "LINUX";
9580 return elfcore_write_note (abfd, buf, bufsiz,
9581 note_name, NT_ARM_VFP, arm_vfp, size);
9585 elfcore_write_aarch_tls (bfd *abfd,
9588 const void *aarch_tls,
9591 char *note_name = "LINUX";
9592 return elfcore_write_note (abfd, buf, bufsiz,
9593 note_name, NT_ARM_TLS, aarch_tls, size);
9597 elfcore_write_aarch_hw_break (bfd *abfd,
9600 const void *aarch_hw_break,
9603 char *note_name = "LINUX";
9604 return elfcore_write_note (abfd, buf, bufsiz,
9605 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
9609 elfcore_write_aarch_hw_watch (bfd *abfd,
9612 const void *aarch_hw_watch,
9615 char *note_name = "LINUX";
9616 return elfcore_write_note (abfd, buf, bufsiz,
9617 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
9621 elfcore_write_register_note (bfd *abfd,
9624 const char *section,
9628 if (strcmp (section, ".reg2") == 0)
9629 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9630 if (strcmp (section, ".reg-xfp") == 0)
9631 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9632 if (strcmp (section, ".reg-xstate") == 0)
9633 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9634 if (strcmp (section, ".reg-ppc-vmx") == 0)
9635 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9636 if (strcmp (section, ".reg-ppc-vsx") == 0)
9637 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9638 if (strcmp (section, ".reg-s390-high-gprs") == 0)
9639 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9640 if (strcmp (section, ".reg-s390-timer") == 0)
9641 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9642 if (strcmp (section, ".reg-s390-todcmp") == 0)
9643 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9644 if (strcmp (section, ".reg-s390-todpreg") == 0)
9645 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9646 if (strcmp (section, ".reg-s390-ctrs") == 0)
9647 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9648 if (strcmp (section, ".reg-s390-prefix") == 0)
9649 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9650 if (strcmp (section, ".reg-s390-last-break") == 0)
9651 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
9652 if (strcmp (section, ".reg-s390-system-call") == 0)
9653 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
9654 if (strcmp (section, ".reg-s390-tdb") == 0)
9655 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
9656 if (strcmp (section, ".reg-arm-vfp") == 0)
9657 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
9658 if (strcmp (section, ".reg-aarch-tls") == 0)
9659 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
9660 if (strcmp (section, ".reg-aarch-hw-break") == 0)
9661 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
9662 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
9663 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
9668 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9673 while (p < buf + size)
9675 /* FIXME: bad alignment assumption. */
9676 Elf_External_Note *xnp = (Elf_External_Note *) p;
9677 Elf_Internal_Note in;
9679 if (offsetof (Elf_External_Note, name) > buf - p + size)
9682 in.type = H_GET_32 (abfd, xnp->type);
9684 in.namesz = H_GET_32 (abfd, xnp->namesz);
9685 in.namedata = xnp->name;
9686 if (in.namesz > buf - in.namedata + size)
9689 in.descsz = H_GET_32 (abfd, xnp->descsz);
9690 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9691 in.descpos = offset + (in.descdata - buf);
9693 && (in.descdata >= buf + size
9694 || in.descsz > buf - in.descdata + size))
9697 switch (bfd_get_format (abfd))
9703 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
9705 if (! elfcore_grok_netbsd_note (abfd, &in))
9708 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
9710 if (! elfcore_grok_openbsd_note (abfd, &in))
9713 else if (CONST_STRNEQ (in.namedata, "QNX"))
9715 if (! elfcore_grok_nto_note (abfd, &in))
9718 else if (CONST_STRNEQ (in.namedata, "SPU/"))
9720 if (! elfcore_grok_spu_note (abfd, &in))
9725 if (! elfcore_grok_note (abfd, &in))
9731 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9733 if (! elfobj_grok_gnu_note (abfd, &in))
9736 else if (in.namesz == sizeof "stapsdt"
9737 && strcmp (in.namedata, "stapsdt") == 0)
9739 if (! elfobj_grok_stapsdt_note (abfd, &in))
9745 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9752 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9759 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9762 buf = (char *) bfd_malloc (size + 1);
9766 /* PR 17512: file: ec08f814
9767 0-termintate the buffer so that string searches will not overflow. */
9770 if (bfd_bread (buf, size, abfd) != size
9771 || !elf_parse_notes (abfd, buf, size, offset))
9781 /* Providing external access to the ELF program header table. */
9783 /* Return an upper bound on the number of bytes required to store a
9784 copy of ABFD's program header table entries. Return -1 if an error
9785 occurs; bfd_get_error will return an appropriate code. */
9788 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9790 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9792 bfd_set_error (bfd_error_wrong_format);
9796 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9799 /* Copy ABFD's program header table entries to *PHDRS. The entries
9800 will be stored as an array of Elf_Internal_Phdr structures, as
9801 defined in include/elf/internal.h. To find out how large the
9802 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9804 Return the number of program header table entries read, or -1 if an
9805 error occurs; bfd_get_error will return an appropriate code. */
9808 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9812 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9814 bfd_set_error (bfd_error_wrong_format);
9818 num_phdrs = elf_elfheader (abfd)->e_phnum;
9819 memcpy (phdrs, elf_tdata (abfd)->phdr,
9820 num_phdrs * sizeof (Elf_Internal_Phdr));
9825 enum elf_reloc_type_class
9826 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
9827 const asection *rel_sec ATTRIBUTE_UNUSED,
9828 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9830 return reloc_class_normal;
9833 /* For RELA architectures, return the relocation value for a
9834 relocation against a local symbol. */
9837 _bfd_elf_rela_local_sym (bfd *abfd,
9838 Elf_Internal_Sym *sym,
9840 Elf_Internal_Rela *rel)
9842 asection *sec = *psec;
9845 relocation = (sec->output_section->vma
9846 + sec->output_offset
9848 if ((sec->flags & SEC_MERGE)
9849 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9850 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
9853 _bfd_merged_section_offset (abfd, psec,
9854 elf_section_data (sec)->sec_info,
9855 sym->st_value + rel->r_addend);
9858 /* If we have changed the section, and our original section is
9859 marked with SEC_EXCLUDE, it means that the original
9860 SEC_MERGE section has been completely subsumed in some
9861 other SEC_MERGE section. In this case, we need to leave
9862 some info around for --emit-relocs. */
9863 if ((sec->flags & SEC_EXCLUDE) != 0)
9864 sec->kept_section = *psec;
9867 rel->r_addend -= relocation;
9868 rel->r_addend += sec->output_section->vma + sec->output_offset;
9874 _bfd_elf_rel_local_sym (bfd *abfd,
9875 Elf_Internal_Sym *sym,
9879 asection *sec = *psec;
9881 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
9882 return sym->st_value + addend;
9884 return _bfd_merged_section_offset (abfd, psec,
9885 elf_section_data (sec)->sec_info,
9886 sym->st_value + addend);
9890 _bfd_elf_section_offset (bfd *abfd,
9891 struct bfd_link_info *info,
9895 switch (sec->sec_info_type)
9897 case SEC_INFO_TYPE_STABS:
9898 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9900 case SEC_INFO_TYPE_EH_FRAME:
9901 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9903 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
9905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9906 bfd_size_type address_size = bed->s->arch_size / 8;
9907 offset = sec->size - offset - address_size;
9913 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9914 reconstruct an ELF file by reading the segments out of remote memory
9915 based on the ELF file header at EHDR_VMA and the ELF program headers it
9916 points to. If not null, *LOADBASEP is filled in with the difference
9917 between the VMAs from which the segments were read, and the VMAs the
9918 file headers (and hence BFD's idea of each section's VMA) put them at.
9920 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9921 remote memory at target address VMA into the local buffer at MYADDR; it
9922 should return zero on success or an `errno' code on failure. TEMPL must
9923 be a BFD for an ELF target with the word size and byte order found in
9924 the remote memory. */
9927 bfd_elf_bfd_from_remote_memory
9932 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
9934 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9935 (templ, ehdr_vma, size, loadbasep, target_read_memory);
9939 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9940 long symcount ATTRIBUTE_UNUSED,
9941 asymbol **syms ATTRIBUTE_UNUSED,
9946 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9949 const char *relplt_name;
9950 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9954 Elf_Internal_Shdr *hdr;
9960 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9963 if (dynsymcount <= 0)
9966 if (!bed->plt_sym_val)
9969 relplt_name = bed->relplt_name;
9970 if (relplt_name == NULL)
9971 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9972 relplt = bfd_get_section_by_name (abfd, relplt_name);
9976 hdr = &elf_section_data (relplt)->this_hdr;
9977 if (hdr->sh_link != elf_dynsymtab (abfd)
9978 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9981 plt = bfd_get_section_by_name (abfd, ".plt");
9985 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9986 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9989 count = relplt->size / hdr->sh_entsize;
9990 size = count * sizeof (asymbol);
9991 p = relplt->relocation;
9992 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9994 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9998 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
10000 size += sizeof ("+0x") - 1 + 8;
10005 s = *ret = (asymbol *) bfd_malloc (size);
10009 names = (char *) (s + count);
10010 p = relplt->relocation;
10012 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10017 addr = bed->plt_sym_val (i, plt, p);
10018 if (addr == (bfd_vma) -1)
10021 *s = **p->sym_ptr_ptr;
10022 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10023 we are defining a symbol, ensure one of them is set. */
10024 if ((s->flags & BSF_LOCAL) == 0)
10025 s->flags |= BSF_GLOBAL;
10026 s->flags |= BSF_SYNTHETIC;
10028 s->value = addr - plt->vma;
10031 len = strlen ((*p->sym_ptr_ptr)->name);
10032 memcpy (names, (*p->sym_ptr_ptr)->name, len);
10034 if (p->addend != 0)
10038 memcpy (names, "+0x", sizeof ("+0x") - 1);
10039 names += sizeof ("+0x") - 1;
10040 bfd_sprintf_vma (abfd, buf, p->addend);
10041 for (a = buf; *a == '0'; ++a)
10044 memcpy (names, a, len);
10047 memcpy (names, "@plt", sizeof ("@plt"));
10048 names += sizeof ("@plt");
10055 /* It is only used by x86-64 so far. */
10056 asection _bfd_elf_large_com_section
10057 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10058 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10061 _bfd_elf_post_process_headers (bfd * abfd,
10062 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10064 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
10066 i_ehdrp = elf_elfheader (abfd);
10068 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10070 /* To make things simpler for the loader on Linux systems we set the
10071 osabi field to ELFOSABI_GNU if the binary contains symbols of
10072 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10073 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10074 && elf_tdata (abfd)->has_gnu_symbols)
10075 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10079 /* Return TRUE for ELF symbol types that represent functions.
10080 This is the default version of this function, which is sufficient for
10081 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10084 _bfd_elf_is_function_type (unsigned int type)
10086 return (type == STT_FUNC
10087 || type == STT_GNU_IFUNC);
10090 /* If the ELF symbol SYM might be a function in SEC, return the
10091 function size and set *CODE_OFF to the function's entry point,
10092 otherwise return zero. */
10095 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10098 bfd_size_type size;
10100 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10101 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10102 || sym->section != sec)
10105 *code_off = sym->value;
10107 if (!(sym->flags & BSF_SYNTHETIC))
10108 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;