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);
657 idx = H_GET_32 (abfd, src);
658 if (src == shdr->contents)
661 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
662 shdr->bfd_section->flags
663 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
668 ((*_bfd_error_handler)
669 (_("%B: invalid SHT_GROUP entry"), abfd));
672 dest->shdr = elf_elfsections (abfd)[idx];
677 /* PR 17510: Corrupt binaries might contain invalid groups. */
678 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
680 elf_tdata (abfd)->num_group = num_group;
682 /* If all groups are invalid then fail. */
685 elf_tdata (abfd)->group_sect_ptr = NULL;
686 elf_tdata (abfd)->num_group = num_group = -1;
687 (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd);
688 bfd_set_error (bfd_error_bad_value);
694 if (num_group != (unsigned) -1)
698 for (i = 0; i < num_group; i++)
700 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
701 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
702 unsigned int n_elt = shdr->sh_size / 4;
704 /* Look through this group's sections to see if current
705 section is a member. */
707 if ((++idx)->shdr == hdr)
711 /* We are a member of this group. Go looking through
712 other members to see if any others are linked via
714 idx = (Elf_Internal_Group *) shdr->contents;
715 n_elt = shdr->sh_size / 4;
717 if ((s = (++idx)->shdr->bfd_section) != NULL
718 && elf_next_in_group (s) != NULL)
722 /* Snarf the group name from other member, and
723 insert current section in circular list. */
724 elf_group_name (newsect) = elf_group_name (s);
725 elf_next_in_group (newsect) = elf_next_in_group (s);
726 elf_next_in_group (s) = newsect;
732 gname = group_signature (abfd, shdr);
735 elf_group_name (newsect) = gname;
737 /* Start a circular list with one element. */
738 elf_next_in_group (newsect) = newsect;
741 /* If the group section has been created, point to the
743 if (shdr->bfd_section != NULL)
744 elf_next_in_group (shdr->bfd_section) = newsect;
752 if (elf_group_name (newsect) == NULL)
754 (*_bfd_error_handler) (_("%B: no group info for section %A"),
762 _bfd_elf_setup_sections (bfd *abfd)
765 unsigned int num_group = elf_tdata (abfd)->num_group;
766 bfd_boolean result = TRUE;
769 /* Process SHF_LINK_ORDER. */
770 for (s = abfd->sections; s != NULL; s = s->next)
772 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
773 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
775 unsigned int elfsec = this_hdr->sh_link;
776 /* FIXME: The old Intel compiler and old strip/objcopy may
777 not set the sh_link or sh_info fields. Hence we could
778 get the situation where elfsec is 0. */
781 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
782 if (bed->link_order_error_handler)
783 bed->link_order_error_handler
784 (_("%B: warning: sh_link not set for section `%A'"),
789 asection *linksec = NULL;
791 if (elfsec < elf_numsections (abfd))
793 this_hdr = elf_elfsections (abfd)[elfsec];
794 linksec = this_hdr->bfd_section;
798 Some strip/objcopy may leave an incorrect value in
799 sh_link. We don't want to proceed. */
802 (*_bfd_error_handler)
803 (_("%B: sh_link [%d] in section `%A' is incorrect"),
804 s->owner, s, elfsec);
808 elf_linked_to_section (s) = linksec;
813 /* Process section groups. */
814 if (num_group == (unsigned) -1)
817 for (i = 0; i < num_group; i++)
819 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
820 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
821 unsigned int n_elt = shdr->sh_size / 4;
824 if ((++idx)->shdr->bfd_section)
825 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
826 else if (idx->shdr->sh_type == SHT_RELA
827 || idx->shdr->sh_type == SHT_REL)
828 /* We won't include relocation sections in section groups in
829 output object files. We adjust the group section size here
830 so that relocatable link will work correctly when
831 relocation sections are in section group in input object
833 shdr->bfd_section->size -= 4;
836 /* There are some unknown sections in the group. */
837 (*_bfd_error_handler)
838 (_("%B: unknown [%d] section `%s' in group [%s]"),
840 (unsigned int) idx->shdr->sh_type,
841 bfd_elf_string_from_elf_section (abfd,
842 (elf_elfheader (abfd)
845 shdr->bfd_section->name);
853 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
855 return elf_next_in_group (sec) != NULL;
858 /* Make a BFD section from an ELF section. We store a pointer to the
859 BFD section in the bfd_section field of the header. */
862 _bfd_elf_make_section_from_shdr (bfd *abfd,
863 Elf_Internal_Shdr *hdr,
869 const struct elf_backend_data *bed;
871 if (hdr->bfd_section != NULL)
874 newsect = bfd_make_section_anyway (abfd, name);
878 hdr->bfd_section = newsect;
879 elf_section_data (newsect)->this_hdr = *hdr;
880 elf_section_data (newsect)->this_idx = shindex;
882 /* Always use the real type/flags. */
883 elf_section_type (newsect) = hdr->sh_type;
884 elf_section_flags (newsect) = hdr->sh_flags;
886 newsect->filepos = hdr->sh_offset;
888 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
889 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
890 || ! bfd_set_section_alignment (abfd, newsect,
891 bfd_log2 (hdr->sh_addralign)))
894 flags = SEC_NO_FLAGS;
895 if (hdr->sh_type != SHT_NOBITS)
896 flags |= SEC_HAS_CONTENTS;
897 if (hdr->sh_type == SHT_GROUP)
898 flags |= SEC_GROUP | SEC_EXCLUDE;
899 if ((hdr->sh_flags & SHF_ALLOC) != 0)
902 if (hdr->sh_type != SHT_NOBITS)
905 if ((hdr->sh_flags & SHF_WRITE) == 0)
906 flags |= SEC_READONLY;
907 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
909 else if ((flags & SEC_LOAD) != 0)
911 if ((hdr->sh_flags & SHF_MERGE) != 0)
914 newsect->entsize = hdr->sh_entsize;
915 if ((hdr->sh_flags & SHF_STRINGS) != 0)
916 flags |= SEC_STRINGS;
918 if (hdr->sh_flags & SHF_GROUP)
919 if (!setup_group (abfd, hdr, newsect))
921 if ((hdr->sh_flags & SHF_TLS) != 0)
922 flags |= SEC_THREAD_LOCAL;
923 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
924 flags |= SEC_EXCLUDE;
926 if ((flags & SEC_ALLOC) == 0)
928 /* The debugging sections appear to be recognized only by name,
929 not any sort of flag. Their SEC_ALLOC bits are cleared. */
936 else if (name[1] == 'g' && name[2] == 'n')
937 p = ".gnu.linkonce.wi.", n = 17;
938 else if (name[1] == 'g' && name[2] == 'd')
939 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
940 else if (name[1] == 'l')
942 else if (name[1] == 's')
944 else if (name[1] == 'z')
945 p = ".zdebug", n = 7;
948 if (p != NULL && strncmp (name, p, n) == 0)
949 flags |= SEC_DEBUGGING;
953 /* As a GNU extension, if the name begins with .gnu.linkonce, we
954 only link a single copy of the section. This is used to support
955 g++. g++ will emit each template expansion in its own section.
956 The symbols will be defined as weak, so that multiple definitions
957 are permitted. The GNU linker extension is to actually discard
958 all but one of the sections. */
959 if (CONST_STRNEQ (name, ".gnu.linkonce")
960 && elf_next_in_group (newsect) == NULL)
961 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
963 bed = get_elf_backend_data (abfd);
964 if (bed->elf_backend_section_flags)
965 if (! bed->elf_backend_section_flags (&flags, hdr))
968 if (! bfd_set_section_flags (abfd, newsect, flags))
971 /* We do not parse the PT_NOTE segments as we are interested even in the
972 separate debug info files which may have the segments offsets corrupted.
973 PT_NOTEs from the core files are currently not parsed using BFD. */
974 if (hdr->sh_type == SHT_NOTE)
978 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
981 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
985 if ((flags & SEC_ALLOC) != 0)
987 Elf_Internal_Phdr *phdr;
988 unsigned int i, nload;
990 /* Some ELF linkers produce binaries with all the program header
991 p_paddr fields zero. If we have such a binary with more than
992 one PT_LOAD header, then leave the section lma equal to vma
993 so that we don't create sections with overlapping lma. */
994 phdr = elf_tdata (abfd)->phdr;
995 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
996 if (phdr->p_paddr != 0)
998 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1000 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1003 phdr = elf_tdata (abfd)->phdr;
1004 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1006 if (((phdr->p_type == PT_LOAD
1007 && (hdr->sh_flags & SHF_TLS) == 0)
1008 || phdr->p_type == PT_TLS)
1009 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1011 if ((flags & SEC_LOAD) == 0)
1012 newsect->lma = (phdr->p_paddr
1013 + hdr->sh_addr - phdr->p_vaddr);
1015 /* We used to use the same adjustment for SEC_LOAD
1016 sections, but that doesn't work if the segment
1017 is packed with code from multiple VMAs.
1018 Instead we calculate the section LMA based on
1019 the segment LMA. It is assumed that the
1020 segment will contain sections with contiguous
1021 LMAs, even if the VMAs are not. */
1022 newsect->lma = (phdr->p_paddr
1023 + hdr->sh_offset - phdr->p_offset);
1025 /* With contiguous segments, we can't tell from file
1026 offsets whether a section with zero size should
1027 be placed at the end of one segment or the
1028 beginning of the next. Decide based on vaddr. */
1029 if (hdr->sh_addr >= phdr->p_vaddr
1030 && (hdr->sh_addr + hdr->sh_size
1031 <= phdr->p_vaddr + phdr->p_memsz))
1037 /* Compress/decompress DWARF debug sections with names: .debug_* and
1038 .zdebug_*, after the section flags is set. */
1039 if ((flags & SEC_DEBUGGING)
1040 && ((name[1] == 'd' && name[6] == '_')
1041 || (name[1] == 'z' && name[7] == '_')))
1043 enum { nothing, compress, decompress } action = nothing;
1046 if (bfd_is_section_compressed (abfd, newsect))
1048 /* Compressed section. Check if we should decompress. */
1049 if ((abfd->flags & BFD_DECOMPRESS))
1050 action = decompress;
1054 /* Normal section. Check if we should compress. */
1055 if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
1065 if (!bfd_init_section_compress_status (abfd, newsect))
1067 (*_bfd_error_handler)
1068 (_("%B: unable to initialize compress status for section %s"),
1074 unsigned int len = strlen (name);
1076 new_name = bfd_alloc (abfd, len + 2);
1077 if (new_name == NULL)
1081 memcpy (new_name + 2, name + 1, len);
1085 if (!bfd_init_section_decompress_status (abfd, newsect))
1087 (*_bfd_error_handler)
1088 (_("%B: unable to initialize decompress status for section %s"),
1094 unsigned int len = strlen (name);
1096 new_name = bfd_alloc (abfd, len);
1097 if (new_name == NULL)
1100 memcpy (new_name + 1, name + 2, len - 1);
1104 if (new_name != NULL)
1105 bfd_rename_section (abfd, newsect, new_name);
1111 const char *const bfd_elf_section_type_names[] = {
1112 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1113 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1114 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1117 /* ELF relocs are against symbols. If we are producing relocatable
1118 output, and the reloc is against an external symbol, and nothing
1119 has given us any additional addend, the resulting reloc will also
1120 be against the same symbol. In such a case, we don't want to
1121 change anything about the way the reloc is handled, since it will
1122 all be done at final link time. Rather than put special case code
1123 into bfd_perform_relocation, all the reloc types use this howto
1124 function. It just short circuits the reloc if producing
1125 relocatable output against an external symbol. */
1127 bfd_reloc_status_type
1128 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1129 arelent *reloc_entry,
1131 void *data ATTRIBUTE_UNUSED,
1132 asection *input_section,
1134 char **error_message ATTRIBUTE_UNUSED)
1136 if (output_bfd != NULL
1137 && (symbol->flags & BSF_SECTION_SYM) == 0
1138 && (! reloc_entry->howto->partial_inplace
1139 || reloc_entry->addend == 0))
1141 reloc_entry->address += input_section->output_offset;
1142 return bfd_reloc_ok;
1145 return bfd_reloc_continue;
1148 /* Copy the program header and other data from one object module to
1152 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1154 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1155 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1158 if (!elf_flags_init (obfd))
1160 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1161 elf_flags_init (obfd) = TRUE;
1164 elf_gp (obfd) = elf_gp (ibfd);
1166 /* Also copy the EI_OSABI field. */
1167 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1168 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1170 /* Copy object attributes. */
1171 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1176 get_segment_type (unsigned int p_type)
1181 case PT_NULL: pt = "NULL"; break;
1182 case PT_LOAD: pt = "LOAD"; break;
1183 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1184 case PT_INTERP: pt = "INTERP"; break;
1185 case PT_NOTE: pt = "NOTE"; break;
1186 case PT_SHLIB: pt = "SHLIB"; break;
1187 case PT_PHDR: pt = "PHDR"; break;
1188 case PT_TLS: pt = "TLS"; break;
1189 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1190 case PT_GNU_STACK: pt = "STACK"; break;
1191 case PT_GNU_RELRO: pt = "RELRO"; break;
1192 default: pt = NULL; break;
1197 /* Print out the program headers. */
1200 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1202 FILE *f = (FILE *) farg;
1203 Elf_Internal_Phdr *p;
1205 bfd_byte *dynbuf = NULL;
1207 p = elf_tdata (abfd)->phdr;
1212 fprintf (f, _("\nProgram Header:\n"));
1213 c = elf_elfheader (abfd)->e_phnum;
1214 for (i = 0; i < c; i++, p++)
1216 const char *pt = get_segment_type (p->p_type);
1221 sprintf (buf, "0x%lx", p->p_type);
1224 fprintf (f, "%8s off 0x", pt);
1225 bfd_fprintf_vma (abfd, f, p->p_offset);
1226 fprintf (f, " vaddr 0x");
1227 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1228 fprintf (f, " paddr 0x");
1229 bfd_fprintf_vma (abfd, f, p->p_paddr);
1230 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1231 fprintf (f, " filesz 0x");
1232 bfd_fprintf_vma (abfd, f, p->p_filesz);
1233 fprintf (f, " memsz 0x");
1234 bfd_fprintf_vma (abfd, f, p->p_memsz);
1235 fprintf (f, " flags %c%c%c",
1236 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1237 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1238 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1239 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1240 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1245 s = bfd_get_section_by_name (abfd, ".dynamic");
1248 unsigned int elfsec;
1249 unsigned long shlink;
1250 bfd_byte *extdyn, *extdynend;
1252 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1254 fprintf (f, _("\nDynamic Section:\n"));
1256 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1259 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1260 if (elfsec == SHN_BAD)
1262 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1264 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1265 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1268 /* PR 17512: file: 6f427532. */
1269 if (s->size < extdynsize)
1271 extdynend = extdyn + s->size;
1272 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1274 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1276 Elf_Internal_Dyn dyn;
1277 const char *name = "";
1279 bfd_boolean stringp;
1280 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1282 (*swap_dyn_in) (abfd, extdyn, &dyn);
1284 if (dyn.d_tag == DT_NULL)
1291 if (bed->elf_backend_get_target_dtag)
1292 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1294 if (!strcmp (name, ""))
1296 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1301 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1302 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1303 case DT_PLTGOT: name = "PLTGOT"; break;
1304 case DT_HASH: name = "HASH"; break;
1305 case DT_STRTAB: name = "STRTAB"; break;
1306 case DT_SYMTAB: name = "SYMTAB"; break;
1307 case DT_RELA: name = "RELA"; break;
1308 case DT_RELASZ: name = "RELASZ"; break;
1309 case DT_RELAENT: name = "RELAENT"; break;
1310 case DT_STRSZ: name = "STRSZ"; break;
1311 case DT_SYMENT: name = "SYMENT"; break;
1312 case DT_INIT: name = "INIT"; break;
1313 case DT_FINI: name = "FINI"; break;
1314 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1315 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1316 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1317 case DT_REL: name = "REL"; break;
1318 case DT_RELSZ: name = "RELSZ"; break;
1319 case DT_RELENT: name = "RELENT"; break;
1320 case DT_PLTREL: name = "PLTREL"; break;
1321 case DT_DEBUG: name = "DEBUG"; break;
1322 case DT_TEXTREL: name = "TEXTREL"; break;
1323 case DT_JMPREL: name = "JMPREL"; break;
1324 case DT_BIND_NOW: name = "BIND_NOW"; break;
1325 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1326 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1327 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1328 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1329 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1330 case DT_FLAGS: name = "FLAGS"; break;
1331 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1332 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1333 case DT_CHECKSUM: name = "CHECKSUM"; break;
1334 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1335 case DT_MOVEENT: name = "MOVEENT"; break;
1336 case DT_MOVESZ: name = "MOVESZ"; break;
1337 case DT_FEATURE: name = "FEATURE"; break;
1338 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1339 case DT_SYMINSZ: name = "SYMINSZ"; break;
1340 case DT_SYMINENT: name = "SYMINENT"; break;
1341 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1342 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1343 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1344 case DT_PLTPAD: name = "PLTPAD"; break;
1345 case DT_MOVETAB: name = "MOVETAB"; break;
1346 case DT_SYMINFO: name = "SYMINFO"; break;
1347 case DT_RELACOUNT: name = "RELACOUNT"; break;
1348 case DT_RELCOUNT: name = "RELCOUNT"; break;
1349 case DT_FLAGS_1: name = "FLAGS_1"; break;
1350 case DT_VERSYM: name = "VERSYM"; break;
1351 case DT_VERDEF: name = "VERDEF"; break;
1352 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1353 case DT_VERNEED: name = "VERNEED"; break;
1354 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1355 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1356 case DT_USED: name = "USED"; break;
1357 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1358 case DT_GNU_HASH: name = "GNU_HASH"; break;
1361 fprintf (f, " %-20s ", name);
1365 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1370 unsigned int tagv = dyn.d_un.d_val;
1372 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1375 fprintf (f, "%s", string);
1384 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1385 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1387 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1391 if (elf_dynverdef (abfd) != 0)
1393 Elf_Internal_Verdef *t;
1395 fprintf (f, _("\nVersion definitions:\n"));
1396 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1398 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1399 t->vd_flags, t->vd_hash,
1400 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1401 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1403 Elf_Internal_Verdaux *a;
1406 for (a = t->vd_auxptr->vda_nextptr;
1410 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1416 if (elf_dynverref (abfd) != 0)
1418 Elf_Internal_Verneed *t;
1420 fprintf (f, _("\nVersion References:\n"));
1421 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1423 Elf_Internal_Vernaux *a;
1425 fprintf (f, _(" required from %s:\n"),
1426 t->vn_filename ? t->vn_filename : "<corrupt>");
1427 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1428 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1429 a->vna_flags, a->vna_other,
1430 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1442 /* Get version string. */
1445 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1446 bfd_boolean *hidden)
1448 const char *version_string = NULL;
1449 if (elf_dynversym (abfd) != 0
1450 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1452 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1454 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1455 vernum &= VERSYM_VERSION;
1458 version_string = "";
1459 else if (vernum == 1)
1460 version_string = "Base";
1461 else if (vernum <= elf_tdata (abfd)->cverdefs)
1463 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1466 Elf_Internal_Verneed *t;
1468 version_string = "";
1469 for (t = elf_tdata (abfd)->verref;
1473 Elf_Internal_Vernaux *a;
1475 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1477 if (a->vna_other == vernum)
1479 version_string = a->vna_nodename;
1486 return version_string;
1489 /* Display ELF-specific fields of a symbol. */
1492 bfd_elf_print_symbol (bfd *abfd,
1495 bfd_print_symbol_type how)
1497 FILE *file = (FILE *) filep;
1500 case bfd_print_symbol_name:
1501 fprintf (file, "%s", symbol->name);
1503 case bfd_print_symbol_more:
1504 fprintf (file, "elf ");
1505 bfd_fprintf_vma (abfd, file, symbol->value);
1506 fprintf (file, " %lx", (unsigned long) symbol->flags);
1508 case bfd_print_symbol_all:
1510 const char *section_name;
1511 const char *name = NULL;
1512 const struct elf_backend_data *bed;
1513 unsigned char st_other;
1515 const char *version_string;
1518 section_name = symbol->section ? symbol->section->name : "(*none*)";
1520 bed = get_elf_backend_data (abfd);
1521 if (bed->elf_backend_print_symbol_all)
1522 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1526 name = symbol->name;
1527 bfd_print_symbol_vandf (abfd, file, symbol);
1530 fprintf (file, " %s\t", section_name);
1531 /* Print the "other" value for a symbol. For common symbols,
1532 we've already printed the size; now print the alignment.
1533 For other symbols, we have no specified alignment, and
1534 we've printed the address; now print the size. */
1535 if (symbol->section && bfd_is_com_section (symbol->section))
1536 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1538 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1539 bfd_fprintf_vma (abfd, file, val);
1541 /* If we have version information, print it. */
1542 version_string = _bfd_elf_get_symbol_version_string (abfd,
1548 fprintf (file, " %-11s", version_string);
1553 fprintf (file, " (%s)", version_string);
1554 for (i = 10 - strlen (version_string); i > 0; --i)
1559 /* If the st_other field is not zero, print it. */
1560 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1565 case STV_INTERNAL: fprintf (file, " .internal"); break;
1566 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1567 case STV_PROTECTED: fprintf (file, " .protected"); break;
1569 /* Some other non-defined flags are also present, so print
1571 fprintf (file, " 0x%02x", (unsigned int) st_other);
1574 fprintf (file, " %s", name);
1580 /* Allocate an ELF string table--force the first byte to be zero. */
1582 struct bfd_strtab_hash *
1583 _bfd_elf_stringtab_init (void)
1585 struct bfd_strtab_hash *ret;
1587 ret = _bfd_stringtab_init ();
1592 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1593 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1594 if (loc == (bfd_size_type) -1)
1596 _bfd_stringtab_free (ret);
1603 /* ELF .o/exec file reading */
1605 /* Create a new bfd section from an ELF section header. */
1608 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1610 Elf_Internal_Shdr *hdr;
1611 Elf_Internal_Ehdr *ehdr;
1612 const struct elf_backend_data *bed;
1614 bfd_boolean ret = TRUE;
1615 static bfd_boolean * sections_being_created = NULL;
1616 static bfd * sections_being_created_abfd = NULL;
1617 static unsigned int nesting = 0;
1619 if (shindex >= elf_numsections (abfd))
1624 /* PR17512: A corrupt ELF binary might contain a recursive group of
1625 sections, with each the string indicies pointing to the next in the
1626 loop. Detect this here, by refusing to load a section that we are
1627 already in the process of loading. We only trigger this test if
1628 we have nested at least three sections deep as normal ELF binaries
1629 can expect to recurse at least once.
1631 FIXME: It would be better if this array was attached to the bfd,
1632 rather than being held in a static pointer. */
1634 if (sections_being_created_abfd != abfd)
1635 sections_being_created = NULL;
1636 if (sections_being_created == NULL)
1638 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1639 sections_being_created = (bfd_boolean *)
1640 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
1641 sections_being_created_abfd = abfd;
1643 if (sections_being_created [shindex])
1645 (*_bfd_error_handler)
1646 (_("%B: warning: loop in section dependencies detected"), abfd);
1649 sections_being_created [shindex] = TRUE;
1652 hdr = elf_elfsections (abfd)[shindex];
1653 ehdr = elf_elfheader (abfd);
1654 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1659 bed = get_elf_backend_data (abfd);
1660 switch (hdr->sh_type)
1663 /* Inactive section. Throw it away. */
1666 case SHT_PROGBITS: /* Normal section with contents. */
1667 case SHT_NOBITS: /* .bss section. */
1668 case SHT_HASH: /* .hash section. */
1669 case SHT_NOTE: /* .note section. */
1670 case SHT_INIT_ARRAY: /* .init_array section. */
1671 case SHT_FINI_ARRAY: /* .fini_array section. */
1672 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1673 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1674 case SHT_GNU_HASH: /* .gnu.hash section. */
1675 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1678 case SHT_DYNAMIC: /* Dynamic linking information. */
1679 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1682 if (hdr->sh_link > elf_numsections (abfd))
1684 /* PR 10478: Accept Solaris binaries with a sh_link
1685 field set to SHN_BEFORE or SHN_AFTER. */
1686 switch (bfd_get_arch (abfd))
1689 case bfd_arch_sparc:
1690 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1691 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1693 /* Otherwise fall through. */
1698 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1700 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1702 Elf_Internal_Shdr *dynsymhdr;
1704 /* The shared libraries distributed with hpux11 have a bogus
1705 sh_link field for the ".dynamic" section. Find the
1706 string table for the ".dynsym" section instead. */
1707 if (elf_dynsymtab (abfd) != 0)
1709 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1710 hdr->sh_link = dynsymhdr->sh_link;
1714 unsigned int i, num_sec;
1716 num_sec = elf_numsections (abfd);
1717 for (i = 1; i < num_sec; i++)
1719 dynsymhdr = elf_elfsections (abfd)[i];
1720 if (dynsymhdr->sh_type == SHT_DYNSYM)
1722 hdr->sh_link = dynsymhdr->sh_link;
1730 case SHT_SYMTAB: /* A symbol table. */
1731 if (elf_onesymtab (abfd) == shindex)
1734 if (hdr->sh_entsize != bed->s->sizeof_sym)
1737 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1739 if (hdr->sh_size != 0)
1741 /* Some assemblers erroneously set sh_info to one with a
1742 zero sh_size. ld sees this as a global symbol count
1743 of (unsigned) -1. Fix it here. */
1748 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1749 elf_onesymtab (abfd) = shindex;
1750 elf_tdata (abfd)->symtab_hdr = *hdr;
1751 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1752 abfd->flags |= HAS_SYMS;
1754 /* Sometimes a shared object will map in the symbol table. If
1755 SHF_ALLOC is set, and this is a shared object, then we also
1756 treat this section as a BFD section. We can not base the
1757 decision purely on SHF_ALLOC, because that flag is sometimes
1758 set in a relocatable object file, which would confuse the
1760 if ((hdr->sh_flags & SHF_ALLOC) != 0
1761 && (abfd->flags & DYNAMIC) != 0
1762 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1766 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1767 can't read symbols without that section loaded as well. It
1768 is most likely specified by the next section header. */
1769 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1771 unsigned int i, num_sec;
1773 num_sec = elf_numsections (abfd);
1774 for (i = shindex + 1; i < num_sec; i++)
1776 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1777 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1778 && hdr2->sh_link == shindex)
1782 for (i = 1; i < shindex; i++)
1784 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1785 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1786 && hdr2->sh_link == shindex)
1790 ret = bfd_section_from_shdr (abfd, i);
1794 case SHT_DYNSYM: /* A dynamic symbol table. */
1795 if (elf_dynsymtab (abfd) == shindex)
1798 if (hdr->sh_entsize != bed->s->sizeof_sym)
1801 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1803 if (hdr->sh_size != 0)
1806 /* Some linkers erroneously set sh_info to one with a
1807 zero sh_size. ld sees this as a global symbol count
1808 of (unsigned) -1. Fix it here. */
1813 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1814 elf_dynsymtab (abfd) = shindex;
1815 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1816 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1817 abfd->flags |= HAS_SYMS;
1819 /* Besides being a symbol table, we also treat this as a regular
1820 section, so that objcopy can handle it. */
1821 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1824 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
1825 if (elf_symtab_shndx (abfd) == shindex)
1828 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1829 elf_symtab_shndx (abfd) = shindex;
1830 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1831 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1834 case SHT_STRTAB: /* A string table. */
1835 if (hdr->bfd_section != NULL)
1838 if (ehdr->e_shstrndx == shindex)
1840 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1841 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1845 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1848 elf_tdata (abfd)->strtab_hdr = *hdr;
1849 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1853 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1856 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1857 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1858 elf_elfsections (abfd)[shindex] = hdr;
1859 /* We also treat this as a regular section, so that objcopy
1861 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1866 /* If the string table isn't one of the above, then treat it as a
1867 regular section. We need to scan all the headers to be sure,
1868 just in case this strtab section appeared before the above. */
1869 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1871 unsigned int i, num_sec;
1873 num_sec = elf_numsections (abfd);
1874 for (i = 1; i < num_sec; i++)
1876 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1877 if (hdr2->sh_link == shindex)
1879 /* Prevent endless recursion on broken objects. */
1882 if (! bfd_section_from_shdr (abfd, i))
1884 if (elf_onesymtab (abfd) == i)
1886 if (elf_dynsymtab (abfd) == i)
1887 goto dynsymtab_strtab;
1891 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1896 /* *These* do a lot of work -- but build no sections! */
1898 asection *target_sect;
1899 Elf_Internal_Shdr *hdr2, **p_hdr;
1900 unsigned int num_sec = elf_numsections (abfd);
1901 struct bfd_elf_section_data *esdt;
1905 != (bfd_size_type) (hdr->sh_type == SHT_REL
1906 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1909 /* Check for a bogus link to avoid crashing. */
1910 if (hdr->sh_link >= num_sec)
1912 ((*_bfd_error_handler)
1913 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1914 abfd, hdr->sh_link, name, shindex));
1915 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1920 /* For some incomprehensible reason Oracle distributes
1921 libraries for Solaris in which some of the objects have
1922 bogus sh_link fields. It would be nice if we could just
1923 reject them, but, unfortunately, some people need to use
1924 them. We scan through the section headers; if we find only
1925 one suitable symbol table, we clobber the sh_link to point
1926 to it. I hope this doesn't break anything.
1928 Don't do it on executable nor shared library. */
1929 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1930 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1931 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1937 for (scan = 1; scan < num_sec; scan++)
1939 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1940 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1951 hdr->sh_link = found;
1954 /* Get the symbol table. */
1955 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1956 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1957 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1960 /* If this reloc section does not use the main symbol table we
1961 don't treat it as a reloc section. BFD can't adequately
1962 represent such a section, so at least for now, we don't
1963 try. We just present it as a normal section. We also
1964 can't use it as a reloc section if it points to the null
1965 section, an invalid section, another reloc section, or its
1966 sh_link points to the null section. */
1967 if (hdr->sh_link != elf_onesymtab (abfd)
1968 || hdr->sh_link == SHN_UNDEF
1969 || hdr->sh_info == SHN_UNDEF
1970 || hdr->sh_info >= num_sec
1971 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1972 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1974 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1979 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1982 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1983 if (target_sect == NULL)
1986 esdt = elf_section_data (target_sect);
1987 if (hdr->sh_type == SHT_RELA)
1988 p_hdr = &esdt->rela.hdr;
1990 p_hdr = &esdt->rel.hdr;
1992 /* PR 17512: file: 0b4f81b7. */
1995 amt = sizeof (*hdr2);
1996 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2001 elf_elfsections (abfd)[shindex] = hdr2;
2002 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2003 target_sect->flags |= SEC_RELOC;
2004 target_sect->relocation = NULL;
2005 target_sect->rel_filepos = hdr->sh_offset;
2006 /* In the section to which the relocations apply, mark whether
2007 its relocations are of the REL or RELA variety. */
2008 if (hdr->sh_size != 0)
2010 if (hdr->sh_type == SHT_RELA)
2011 target_sect->use_rela_p = 1;
2013 abfd->flags |= HAS_RELOC;
2017 case SHT_GNU_verdef:
2018 elf_dynverdef (abfd) = shindex;
2019 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2020 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2023 case SHT_GNU_versym:
2024 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2027 elf_dynversym (abfd) = shindex;
2028 elf_tdata (abfd)->dynversym_hdr = *hdr;
2029 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2032 case SHT_GNU_verneed:
2033 elf_dynverref (abfd) = shindex;
2034 elf_tdata (abfd)->dynverref_hdr = *hdr;
2035 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2042 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2045 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2048 if (hdr->contents != NULL)
2050 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2051 unsigned int n_elt = hdr->sh_size / sizeof (* idx);
2056 if (idx->flags & GRP_COMDAT)
2057 hdr->bfd_section->flags
2058 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2060 /* We try to keep the same section order as it comes in. */
2063 while (--n_elt != 0)
2067 if (idx->shdr != NULL
2068 && (s = idx->shdr->bfd_section) != NULL
2069 && elf_next_in_group (s) != NULL)
2071 elf_next_in_group (hdr->bfd_section) = s;
2079 /* Possibly an attributes section. */
2080 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2081 || hdr->sh_type == bed->obj_attrs_section_type)
2083 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2085 _bfd_elf_parse_attributes (abfd, hdr);
2089 /* Check for any processor-specific section types. */
2090 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2093 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2095 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2096 /* FIXME: How to properly handle allocated section reserved
2097 for applications? */
2098 (*_bfd_error_handler)
2099 (_("%B: don't know how to handle allocated, application "
2100 "specific section `%s' [0x%8x]"),
2101 abfd, name, hdr->sh_type);
2104 /* Allow sections reserved for applications. */
2105 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2110 else if (hdr->sh_type >= SHT_LOPROC
2111 && hdr->sh_type <= SHT_HIPROC)
2112 /* FIXME: We should handle this section. */
2113 (*_bfd_error_handler)
2114 (_("%B: don't know how to handle processor specific section "
2116 abfd, name, hdr->sh_type);
2117 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2119 /* Unrecognised OS-specific sections. */
2120 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2121 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2122 required to correctly process the section and the file should
2123 be rejected with an error message. */
2124 (*_bfd_error_handler)
2125 (_("%B: don't know how to handle OS specific section "
2127 abfd, name, hdr->sh_type);
2130 /* Otherwise it should be processed. */
2131 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2136 /* FIXME: We should handle this section. */
2137 (*_bfd_error_handler)
2138 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2139 abfd, name, hdr->sh_type);
2147 if (sections_being_created && sections_being_created_abfd == abfd)
2148 sections_being_created [shindex] = FALSE;
2149 if (-- nesting == 0)
2151 sections_being_created = NULL;
2152 sections_being_created_abfd = abfd;
2157 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2160 bfd_sym_from_r_symndx (struct sym_cache *cache,
2162 unsigned long r_symndx)
2164 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2166 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2168 Elf_Internal_Shdr *symtab_hdr;
2169 unsigned char esym[sizeof (Elf64_External_Sym)];
2170 Elf_External_Sym_Shndx eshndx;
2172 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2173 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2174 &cache->sym[ent], esym, &eshndx) == NULL)
2177 if (cache->abfd != abfd)
2179 memset (cache->indx, -1, sizeof (cache->indx));
2182 cache->indx[ent] = r_symndx;
2185 return &cache->sym[ent];
2188 /* Given an ELF section number, retrieve the corresponding BFD
2192 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2194 if (sec_index >= elf_numsections (abfd))
2196 return elf_elfsections (abfd)[sec_index]->bfd_section;
2199 static const struct bfd_elf_special_section special_sections_b[] =
2201 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2202 { NULL, 0, 0, 0, 0 }
2205 static const struct bfd_elf_special_section special_sections_c[] =
2207 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2208 { NULL, 0, 0, 0, 0 }
2211 static const struct bfd_elf_special_section special_sections_d[] =
2213 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2214 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2215 /* There are more DWARF sections than these, but they needn't be added here
2216 unless you have to cope with broken compilers that don't emit section
2217 attributes or you want to help the user writing assembler. */
2218 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2219 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2220 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2221 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2222 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2223 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2224 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2225 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2226 { NULL, 0, 0, 0, 0 }
2229 static const struct bfd_elf_special_section special_sections_f[] =
2231 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2232 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2233 { NULL, 0, 0, 0, 0 }
2236 static const struct bfd_elf_special_section special_sections_g[] =
2238 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2239 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2240 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2241 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2242 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2243 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2244 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2245 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2246 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2247 { NULL, 0, 0, 0, 0 }
2250 static const struct bfd_elf_special_section special_sections_h[] =
2252 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2253 { NULL, 0, 0, 0, 0 }
2256 static const struct bfd_elf_special_section special_sections_i[] =
2258 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2259 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2260 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2261 { NULL, 0, 0, 0, 0 }
2264 static const struct bfd_elf_special_section special_sections_l[] =
2266 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2267 { NULL, 0, 0, 0, 0 }
2270 static const struct bfd_elf_special_section special_sections_n[] =
2272 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2273 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2274 { NULL, 0, 0, 0, 0 }
2277 static const struct bfd_elf_special_section special_sections_p[] =
2279 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2280 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2281 { NULL, 0, 0, 0, 0 }
2284 static const struct bfd_elf_special_section special_sections_r[] =
2286 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2287 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2288 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2289 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2290 { NULL, 0, 0, 0, 0 }
2293 static const struct bfd_elf_special_section special_sections_s[] =
2295 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2296 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2297 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2298 /* See struct bfd_elf_special_section declaration for the semantics of
2299 this special case where .prefix_length != strlen (.prefix). */
2300 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2301 { NULL, 0, 0, 0, 0 }
2304 static const struct bfd_elf_special_section special_sections_t[] =
2306 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2307 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2308 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2309 { NULL, 0, 0, 0, 0 }
2312 static const struct bfd_elf_special_section special_sections_z[] =
2314 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2315 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2316 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2317 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2318 { NULL, 0, 0, 0, 0 }
2321 static const struct bfd_elf_special_section * const special_sections[] =
2323 special_sections_b, /* 'b' */
2324 special_sections_c, /* 'c' */
2325 special_sections_d, /* 'd' */
2327 special_sections_f, /* 'f' */
2328 special_sections_g, /* 'g' */
2329 special_sections_h, /* 'h' */
2330 special_sections_i, /* 'i' */
2333 special_sections_l, /* 'l' */
2335 special_sections_n, /* 'n' */
2337 special_sections_p, /* 'p' */
2339 special_sections_r, /* 'r' */
2340 special_sections_s, /* 's' */
2341 special_sections_t, /* 't' */
2347 special_sections_z /* 'z' */
2350 const struct bfd_elf_special_section *
2351 _bfd_elf_get_special_section (const char *name,
2352 const struct bfd_elf_special_section *spec,
2358 len = strlen (name);
2360 for (i = 0; spec[i].prefix != NULL; i++)
2363 int prefix_len = spec[i].prefix_length;
2365 if (len < prefix_len)
2367 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2370 suffix_len = spec[i].suffix_length;
2371 if (suffix_len <= 0)
2373 if (name[prefix_len] != 0)
2375 if (suffix_len == 0)
2377 if (name[prefix_len] != '.'
2378 && (suffix_len == -2
2379 || (rela && spec[i].type == SHT_REL)))
2385 if (len < prefix_len + suffix_len)
2387 if (memcmp (name + len - suffix_len,
2388 spec[i].prefix + prefix_len,
2398 const struct bfd_elf_special_section *
2399 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2402 const struct bfd_elf_special_section *spec;
2403 const struct elf_backend_data *bed;
2405 /* See if this is one of the special sections. */
2406 if (sec->name == NULL)
2409 bed = get_elf_backend_data (abfd);
2410 spec = bed->special_sections;
2413 spec = _bfd_elf_get_special_section (sec->name,
2414 bed->special_sections,
2420 if (sec->name[0] != '.')
2423 i = sec->name[1] - 'b';
2424 if (i < 0 || i > 'z' - 'b')
2427 spec = special_sections[i];
2432 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2436 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2438 struct bfd_elf_section_data *sdata;
2439 const struct elf_backend_data *bed;
2440 const struct bfd_elf_special_section *ssect;
2442 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2445 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2449 sec->used_by_bfd = sdata;
2452 /* Indicate whether or not this section should use RELA relocations. */
2453 bed = get_elf_backend_data (abfd);
2454 sec->use_rela_p = bed->default_use_rela_p;
2456 /* When we read a file, we don't need to set ELF section type and
2457 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2458 anyway. We will set ELF section type and flags for all linker
2459 created sections. If user specifies BFD section flags, we will
2460 set ELF section type and flags based on BFD section flags in
2461 elf_fake_sections. Special handling for .init_array/.fini_array
2462 output sections since they may contain .ctors/.dtors input
2463 sections. We don't want _bfd_elf_init_private_section_data to
2464 copy ELF section type from .ctors/.dtors input sections. */
2465 if (abfd->direction != read_direction
2466 || (sec->flags & SEC_LINKER_CREATED) != 0)
2468 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2471 || (sec->flags & SEC_LINKER_CREATED) != 0
2472 || ssect->type == SHT_INIT_ARRAY
2473 || ssect->type == SHT_FINI_ARRAY))
2475 elf_section_type (sec) = ssect->type;
2476 elf_section_flags (sec) = ssect->attr;
2480 return _bfd_generic_new_section_hook (abfd, sec);
2483 /* Create a new bfd section from an ELF program header.
2485 Since program segments have no names, we generate a synthetic name
2486 of the form segment<NUM>, where NUM is generally the index in the
2487 program header table. For segments that are split (see below) we
2488 generate the names segment<NUM>a and segment<NUM>b.
2490 Note that some program segments may have a file size that is different than
2491 (less than) the memory size. All this means is that at execution the
2492 system must allocate the amount of memory specified by the memory size,
2493 but only initialize it with the first "file size" bytes read from the
2494 file. This would occur for example, with program segments consisting
2495 of combined data+bss.
2497 To handle the above situation, this routine generates TWO bfd sections
2498 for the single program segment. The first has the length specified by
2499 the file size of the segment, and the second has the length specified
2500 by the difference between the two sizes. In effect, the segment is split
2501 into its initialized and uninitialized parts.
2506 _bfd_elf_make_section_from_phdr (bfd *abfd,
2507 Elf_Internal_Phdr *hdr,
2509 const char *type_name)
2517 split = ((hdr->p_memsz > 0)
2518 && (hdr->p_filesz > 0)
2519 && (hdr->p_memsz > hdr->p_filesz));
2521 if (hdr->p_filesz > 0)
2523 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2524 len = strlen (namebuf) + 1;
2525 name = (char *) bfd_alloc (abfd, len);
2528 memcpy (name, namebuf, len);
2529 newsect = bfd_make_section (abfd, name);
2530 if (newsect == NULL)
2532 newsect->vma = hdr->p_vaddr;
2533 newsect->lma = hdr->p_paddr;
2534 newsect->size = hdr->p_filesz;
2535 newsect->filepos = hdr->p_offset;
2536 newsect->flags |= SEC_HAS_CONTENTS;
2537 newsect->alignment_power = bfd_log2 (hdr->p_align);
2538 if (hdr->p_type == PT_LOAD)
2540 newsect->flags |= SEC_ALLOC;
2541 newsect->flags |= SEC_LOAD;
2542 if (hdr->p_flags & PF_X)
2544 /* FIXME: all we known is that it has execute PERMISSION,
2546 newsect->flags |= SEC_CODE;
2549 if (!(hdr->p_flags & PF_W))
2551 newsect->flags |= SEC_READONLY;
2555 if (hdr->p_memsz > hdr->p_filesz)
2559 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2560 len = strlen (namebuf) + 1;
2561 name = (char *) bfd_alloc (abfd, len);
2564 memcpy (name, namebuf, len);
2565 newsect = bfd_make_section (abfd, name);
2566 if (newsect == NULL)
2568 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2569 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2570 newsect->size = hdr->p_memsz - hdr->p_filesz;
2571 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2572 align = newsect->vma & -newsect->vma;
2573 if (align == 0 || align > hdr->p_align)
2574 align = hdr->p_align;
2575 newsect->alignment_power = bfd_log2 (align);
2576 if (hdr->p_type == PT_LOAD)
2578 /* Hack for gdb. Segments that have not been modified do
2579 not have their contents written to a core file, on the
2580 assumption that a debugger can find the contents in the
2581 executable. We flag this case by setting the fake
2582 section size to zero. Note that "real" bss sections will
2583 always have their contents dumped to the core file. */
2584 if (bfd_get_format (abfd) == bfd_core)
2586 newsect->flags |= SEC_ALLOC;
2587 if (hdr->p_flags & PF_X)
2588 newsect->flags |= SEC_CODE;
2590 if (!(hdr->p_flags & PF_W))
2591 newsect->flags |= SEC_READONLY;
2598 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2600 const struct elf_backend_data *bed;
2602 switch (hdr->p_type)
2605 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2608 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2611 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2614 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2617 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2619 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2624 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2627 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2629 case PT_GNU_EH_FRAME:
2630 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2634 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2637 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2640 /* Check for any processor-specific program segment types. */
2641 bed = get_elf_backend_data (abfd);
2642 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2646 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2650 _bfd_elf_single_rel_hdr (asection *sec)
2652 if (elf_section_data (sec)->rel.hdr)
2654 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2655 return elf_section_data (sec)->rel.hdr;
2658 return elf_section_data (sec)->rela.hdr;
2661 /* Allocate and initialize a section-header for a new reloc section,
2662 containing relocations against ASECT. It is stored in RELDATA. If
2663 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2667 _bfd_elf_init_reloc_shdr (bfd *abfd,
2668 struct bfd_elf_section_reloc_data *reldata,
2670 bfd_boolean use_rela_p)
2672 Elf_Internal_Shdr *rel_hdr;
2674 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2677 amt = sizeof (Elf_Internal_Shdr);
2678 BFD_ASSERT (reldata->hdr == NULL);
2679 rel_hdr = bfd_zalloc (abfd, amt);
2680 reldata->hdr = rel_hdr;
2682 amt = sizeof ".rela" + strlen (asect->name);
2683 name = (char *) bfd_alloc (abfd, amt);
2686 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2688 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2690 if (rel_hdr->sh_name == (unsigned int) -1)
2692 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2693 rel_hdr->sh_entsize = (use_rela_p
2694 ? bed->s->sizeof_rela
2695 : bed->s->sizeof_rel);
2696 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2697 rel_hdr->sh_flags = 0;
2698 rel_hdr->sh_addr = 0;
2699 rel_hdr->sh_size = 0;
2700 rel_hdr->sh_offset = 0;
2705 /* Return the default section type based on the passed in section flags. */
2708 bfd_elf_get_default_section_type (flagword flags)
2710 if ((flags & SEC_ALLOC) != 0
2711 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2713 return SHT_PROGBITS;
2716 struct fake_section_arg
2718 struct bfd_link_info *link_info;
2722 /* Set up an ELF internal section header for a section. */
2725 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2727 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2728 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2729 struct bfd_elf_section_data *esd = elf_section_data (asect);
2730 Elf_Internal_Shdr *this_hdr;
2731 unsigned int sh_type;
2735 /* We already failed; just get out of the bfd_map_over_sections
2740 this_hdr = &esd->this_hdr;
2742 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2743 asect->name, FALSE);
2744 if (this_hdr->sh_name == (unsigned int) -1)
2750 /* Don't clear sh_flags. Assembler may set additional bits. */
2752 if ((asect->flags & SEC_ALLOC) != 0
2753 || asect->user_set_vma)
2754 this_hdr->sh_addr = asect->vma;
2756 this_hdr->sh_addr = 0;
2758 this_hdr->sh_offset = 0;
2759 this_hdr->sh_size = asect->size;
2760 this_hdr->sh_link = 0;
2761 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2762 /* The sh_entsize and sh_info fields may have been set already by
2763 copy_private_section_data. */
2765 this_hdr->bfd_section = asect;
2766 this_hdr->contents = NULL;
2768 /* If the section type is unspecified, we set it based on
2770 if ((asect->flags & SEC_GROUP) != 0)
2771 sh_type = SHT_GROUP;
2773 sh_type = bfd_elf_get_default_section_type (asect->flags);
2775 if (this_hdr->sh_type == SHT_NULL)
2776 this_hdr->sh_type = sh_type;
2777 else if (this_hdr->sh_type == SHT_NOBITS
2778 && sh_type == SHT_PROGBITS
2779 && (asect->flags & SEC_ALLOC) != 0)
2781 /* Warn if we are changing a NOBITS section to PROGBITS, but
2782 allow the link to proceed. This can happen when users link
2783 non-bss input sections to bss output sections, or emit data
2784 to a bss output section via a linker script. */
2785 (*_bfd_error_handler)
2786 (_("warning: section `%A' type changed to PROGBITS"), asect);
2787 this_hdr->sh_type = sh_type;
2790 switch (this_hdr->sh_type)
2796 case SHT_INIT_ARRAY:
2797 case SHT_FINI_ARRAY:
2798 case SHT_PREINIT_ARRAY:
2805 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2809 this_hdr->sh_entsize = bed->s->sizeof_sym;
2813 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2817 if (get_elf_backend_data (abfd)->may_use_rela_p)
2818 this_hdr->sh_entsize = bed->s->sizeof_rela;
2822 if (get_elf_backend_data (abfd)->may_use_rel_p)
2823 this_hdr->sh_entsize = bed->s->sizeof_rel;
2826 case SHT_GNU_versym:
2827 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2830 case SHT_GNU_verdef:
2831 this_hdr->sh_entsize = 0;
2832 /* objcopy or strip will copy over sh_info, but may not set
2833 cverdefs. The linker will set cverdefs, but sh_info will be
2835 if (this_hdr->sh_info == 0)
2836 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2838 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2839 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2842 case SHT_GNU_verneed:
2843 this_hdr->sh_entsize = 0;
2844 /* objcopy or strip will copy over sh_info, but may not set
2845 cverrefs. The linker will set cverrefs, but sh_info will be
2847 if (this_hdr->sh_info == 0)
2848 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2850 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2851 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2855 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2859 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2863 if ((asect->flags & SEC_ALLOC) != 0)
2864 this_hdr->sh_flags |= SHF_ALLOC;
2865 if ((asect->flags & SEC_READONLY) == 0)
2866 this_hdr->sh_flags |= SHF_WRITE;
2867 if ((asect->flags & SEC_CODE) != 0)
2868 this_hdr->sh_flags |= SHF_EXECINSTR;
2869 if ((asect->flags & SEC_MERGE) != 0)
2871 this_hdr->sh_flags |= SHF_MERGE;
2872 this_hdr->sh_entsize = asect->entsize;
2873 if ((asect->flags & SEC_STRINGS) != 0)
2874 this_hdr->sh_flags |= SHF_STRINGS;
2876 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2877 this_hdr->sh_flags |= SHF_GROUP;
2878 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2880 this_hdr->sh_flags |= SHF_TLS;
2881 if (asect->size == 0
2882 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2884 struct bfd_link_order *o = asect->map_tail.link_order;
2886 this_hdr->sh_size = 0;
2889 this_hdr->sh_size = o->offset + o->size;
2890 if (this_hdr->sh_size != 0)
2891 this_hdr->sh_type = SHT_NOBITS;
2895 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2896 this_hdr->sh_flags |= SHF_EXCLUDE;
2898 /* If the section has relocs, set up a section header for the
2899 SHT_REL[A] section. If two relocation sections are required for
2900 this section, it is up to the processor-specific back-end to
2901 create the other. */
2902 if ((asect->flags & SEC_RELOC) != 0)
2904 /* When doing a relocatable link, create both REL and RELA sections if
2907 /* Do the normal setup if we wouldn't create any sections here. */
2908 && esd->rel.count + esd->rela.count > 0
2909 && (arg->link_info->relocatable || arg->link_info->emitrelocations))
2911 if (esd->rel.count && esd->rel.hdr == NULL
2912 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
2917 if (esd->rela.count && esd->rela.hdr == NULL
2918 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
2924 else if (!_bfd_elf_init_reloc_shdr (abfd,
2926 ? &esd->rela : &esd->rel),
2932 /* Check for processor-specific section types. */
2933 sh_type = this_hdr->sh_type;
2934 if (bed->elf_backend_fake_sections
2935 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2938 if (sh_type == SHT_NOBITS && asect->size != 0)
2940 /* Don't change the header type from NOBITS if we are being
2941 called for objcopy --only-keep-debug. */
2942 this_hdr->sh_type = sh_type;
2946 /* Fill in the contents of a SHT_GROUP section. Called from
2947 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2948 when ELF targets use the generic linker, ld. Called for ld -r
2949 from bfd_elf_final_link. */
2952 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2954 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2955 asection *elt, *first;
2959 /* Ignore linker created group section. See elfNN_ia64_object_p in
2961 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2965 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2967 unsigned long symindx = 0;
2969 /* elf_group_id will have been set up by objcopy and the
2971 if (elf_group_id (sec) != NULL)
2972 symindx = elf_group_id (sec)->udata.i;
2976 /* If called from the assembler, swap_out_syms will have set up
2977 elf_section_syms. */
2978 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2979 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2981 elf_section_data (sec)->this_hdr.sh_info = symindx;
2983 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2985 /* The ELF backend linker sets sh_info to -2 when the group
2986 signature symbol is global, and thus the index can't be
2987 set until all local symbols are output. */
2988 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2989 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2990 unsigned long symndx = sec_data->this_hdr.sh_info;
2991 unsigned long extsymoff = 0;
2992 struct elf_link_hash_entry *h;
2994 if (!elf_bad_symtab (igroup->owner))
2996 Elf_Internal_Shdr *symtab_hdr;
2998 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2999 extsymoff = symtab_hdr->sh_info;
3001 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3002 while (h->root.type == bfd_link_hash_indirect
3003 || h->root.type == bfd_link_hash_warning)
3004 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3006 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3009 /* The contents won't be allocated for "ld -r" or objcopy. */
3011 if (sec->contents == NULL)
3014 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3016 /* Arrange for the section to be written out. */
3017 elf_section_data (sec)->this_hdr.contents = sec->contents;
3018 if (sec->contents == NULL)
3025 loc = sec->contents + sec->size;
3027 /* Get the pointer to the first section in the group that gas
3028 squirreled away here. objcopy arranges for this to be set to the
3029 start of the input section group. */
3030 first = elt = elf_next_in_group (sec);
3032 /* First element is a flag word. Rest of section is elf section
3033 indices for all the sections of the group. Write them backwards
3034 just to keep the group in the same order as given in .section
3035 directives, not that it matters. */
3042 s = s->output_section;
3044 && !bfd_is_abs_section (s))
3046 unsigned int idx = elf_section_data (s)->this_idx;
3049 H_PUT_32 (abfd, idx, loc);
3051 elt = elf_next_in_group (elt);
3056 if ((loc -= 4) != sec->contents)
3059 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3062 /* Assign all ELF section numbers. The dummy first section is handled here
3063 too. The link/info pointers for the standard section types are filled
3064 in here too, while we're at it. */
3067 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3069 struct elf_obj_tdata *t = elf_tdata (abfd);
3071 unsigned int section_number, secn;
3072 Elf_Internal_Shdr **i_shdrp;
3073 struct bfd_elf_section_data *d;
3074 bfd_boolean need_symtab;
3078 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3080 /* SHT_GROUP sections are in relocatable files only. */
3081 if (link_info == NULL || link_info->relocatable)
3083 /* Put SHT_GROUP sections first. */
3084 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3086 d = elf_section_data (sec);
3088 if (d->this_hdr.sh_type == SHT_GROUP)
3090 if (sec->flags & SEC_LINKER_CREATED)
3092 /* Remove the linker created SHT_GROUP sections. */
3093 bfd_section_list_remove (abfd, sec);
3094 abfd->section_count--;
3097 d->this_idx = section_number++;
3102 for (sec = abfd->sections; sec; sec = sec->next)
3104 d = elf_section_data (sec);
3106 if (d->this_hdr.sh_type != SHT_GROUP)
3107 d->this_idx = section_number++;
3108 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3111 d->rel.idx = section_number++;
3112 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3119 d->rela.idx = section_number++;
3120 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3126 elf_shstrtab_sec (abfd) = section_number++;
3127 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3128 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3130 need_symtab = (bfd_get_symcount (abfd) > 0
3131 || (link_info == NULL
3132 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3136 elf_onesymtab (abfd) = section_number++;
3137 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3138 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3140 elf_symtab_shndx (abfd) = section_number++;
3141 t->symtab_shndx_hdr.sh_name
3142 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3143 ".symtab_shndx", FALSE);
3144 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3147 elf_strtab_sec (abfd) = section_number++;
3148 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3151 if (section_number >= SHN_LORESERVE)
3153 _bfd_error_handler (_("%B: too many sections: %u"),
3154 abfd, section_number);
3158 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3159 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3161 elf_numsections (abfd) = section_number;
3162 elf_elfheader (abfd)->e_shnum = section_number;
3164 /* Set up the list of section header pointers, in agreement with the
3166 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3167 sizeof (Elf_Internal_Shdr *));
3168 if (i_shdrp == NULL)
3171 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3172 sizeof (Elf_Internal_Shdr));
3173 if (i_shdrp[0] == NULL)
3175 bfd_release (abfd, i_shdrp);
3179 elf_elfsections (abfd) = i_shdrp;
3181 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3184 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3185 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3187 i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
3188 t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
3190 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3191 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3194 for (sec = abfd->sections; sec; sec = sec->next)
3199 d = elf_section_data (sec);
3201 i_shdrp[d->this_idx] = &d->this_hdr;
3202 if (d->rel.idx != 0)
3203 i_shdrp[d->rel.idx] = d->rel.hdr;
3204 if (d->rela.idx != 0)
3205 i_shdrp[d->rela.idx] = d->rela.hdr;
3207 /* Fill in the sh_link and sh_info fields while we're at it. */
3209 /* sh_link of a reloc section is the section index of the symbol
3210 table. sh_info is the section index of the section to which
3211 the relocation entries apply. */
3212 if (d->rel.idx != 0)
3214 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3215 d->rel.hdr->sh_info = d->this_idx;
3216 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3218 if (d->rela.idx != 0)
3220 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3221 d->rela.hdr->sh_info = d->this_idx;
3222 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3225 /* We need to set up sh_link for SHF_LINK_ORDER. */
3226 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3228 s = elf_linked_to_section (sec);
3231 /* elf_linked_to_section points to the input section. */
3232 if (link_info != NULL)
3234 /* Check discarded linkonce section. */
3235 if (discarded_section (s))
3238 (*_bfd_error_handler)
3239 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3240 abfd, d->this_hdr.bfd_section,
3242 /* Point to the kept section if it has the same
3243 size as the discarded one. */
3244 kept = _bfd_elf_check_kept_section (s, link_info);
3247 bfd_set_error (bfd_error_bad_value);
3253 s = s->output_section;
3254 BFD_ASSERT (s != NULL);
3258 /* Handle objcopy. */
3259 if (s->output_section == NULL)
3261 (*_bfd_error_handler)
3262 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3263 abfd, d->this_hdr.bfd_section, s, s->owner);
3264 bfd_set_error (bfd_error_bad_value);
3267 s = s->output_section;
3269 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3274 The Intel C compiler generates SHT_IA_64_UNWIND with
3275 SHF_LINK_ORDER. But it doesn't set the sh_link or
3276 sh_info fields. Hence we could get the situation
3278 const struct elf_backend_data *bed
3279 = get_elf_backend_data (abfd);
3280 if (bed->link_order_error_handler)
3281 bed->link_order_error_handler
3282 (_("%B: warning: sh_link not set for section `%A'"),
3287 switch (d->this_hdr.sh_type)
3291 /* A reloc section which we are treating as a normal BFD
3292 section. sh_link is the section index of the symbol
3293 table. sh_info is the section index of the section to
3294 which the relocation entries apply. We assume that an
3295 allocated reloc section uses the dynamic symbol table.
3296 FIXME: How can we be sure? */
3297 s = bfd_get_section_by_name (abfd, ".dynsym");
3299 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3301 /* We look up the section the relocs apply to by name. */
3303 if (d->this_hdr.sh_type == SHT_REL)
3307 s = bfd_get_section_by_name (abfd, name);
3310 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3311 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3316 /* We assume that a section named .stab*str is a stabs
3317 string section. We look for a section with the same name
3318 but without the trailing ``str'', and set its sh_link
3319 field to point to this section. */
3320 if (CONST_STRNEQ (sec->name, ".stab")
3321 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3326 len = strlen (sec->name);
3327 alc = (char *) bfd_malloc (len - 2);
3330 memcpy (alc, sec->name, len - 3);
3331 alc[len - 3] = '\0';
3332 s = bfd_get_section_by_name (abfd, alc);
3336 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3338 /* This is a .stab section. */
3339 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3340 elf_section_data (s)->this_hdr.sh_entsize
3341 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3348 case SHT_GNU_verneed:
3349 case SHT_GNU_verdef:
3350 /* sh_link is the section header index of the string table
3351 used for the dynamic entries, or the symbol table, or the
3353 s = bfd_get_section_by_name (abfd, ".dynstr");
3355 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3358 case SHT_GNU_LIBLIST:
3359 /* sh_link is the section header index of the prelink library
3360 list used for the dynamic entries, or the symbol table, or
3361 the version strings. */
3362 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3363 ? ".dynstr" : ".gnu.libstr");
3365 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3370 case SHT_GNU_versym:
3371 /* sh_link is the section header index of the symbol table
3372 this hash table or version table is for. */
3373 s = bfd_get_section_by_name (abfd, ".dynsym");
3375 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3379 d->this_hdr.sh_link = elf_onesymtab (abfd);
3383 for (secn = 1; secn < section_number; ++secn)
3384 if (i_shdrp[secn] == NULL)
3385 i_shdrp[secn] = i_shdrp[0];
3387 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3388 i_shdrp[secn]->sh_name);
3393 sym_is_global (bfd *abfd, asymbol *sym)
3395 /* If the backend has a special mapping, use it. */
3396 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3397 if (bed->elf_backend_sym_is_global)
3398 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3400 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3401 || bfd_is_und_section (bfd_get_section (sym))
3402 || bfd_is_com_section (bfd_get_section (sym)));
3405 /* Don't output section symbols for sections that are not going to be
3406 output, that are duplicates or there is no BFD section. */
3409 ignore_section_sym (bfd *abfd, asymbol *sym)
3411 elf_symbol_type *type_ptr;
3413 if ((sym->flags & BSF_SECTION_SYM) == 0)
3416 type_ptr = elf_symbol_from (abfd, sym);
3417 return ((type_ptr != NULL
3418 && type_ptr->internal_elf_sym.st_shndx != 0
3419 && bfd_is_abs_section (sym->section))
3420 || !(sym->section->owner == abfd
3421 || (sym->section->output_section->owner == abfd
3422 && sym->section->output_offset == 0)
3423 || bfd_is_abs_section (sym->section)));
3426 /* Map symbol from it's internal number to the external number, moving
3427 all local symbols to be at the head of the list. */
3430 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3432 unsigned int symcount = bfd_get_symcount (abfd);
3433 asymbol **syms = bfd_get_outsymbols (abfd);
3434 asymbol **sect_syms;
3435 unsigned int num_locals = 0;
3436 unsigned int num_globals = 0;
3437 unsigned int num_locals2 = 0;
3438 unsigned int num_globals2 = 0;
3445 fprintf (stderr, "elf_map_symbols\n");
3449 for (asect = abfd->sections; asect; asect = asect->next)
3451 if (max_index < asect->index)
3452 max_index = asect->index;
3456 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3457 if (sect_syms == NULL)
3459 elf_section_syms (abfd) = sect_syms;
3460 elf_num_section_syms (abfd) = max_index;
3462 /* Init sect_syms entries for any section symbols we have already
3463 decided to output. */
3464 for (idx = 0; idx < symcount; idx++)
3466 asymbol *sym = syms[idx];
3468 if ((sym->flags & BSF_SECTION_SYM) != 0
3470 && !ignore_section_sym (abfd, sym)
3471 && !bfd_is_abs_section (sym->section))
3473 asection *sec = sym->section;
3475 if (sec->owner != abfd)
3476 sec = sec->output_section;
3478 sect_syms[sec->index] = syms[idx];
3482 /* Classify all of the symbols. */
3483 for (idx = 0; idx < symcount; idx++)
3485 if (sym_is_global (abfd, syms[idx]))
3487 else if (!ignore_section_sym (abfd, syms[idx]))
3491 /* We will be adding a section symbol for each normal BFD section. Most
3492 sections will already have a section symbol in outsymbols, but
3493 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3494 at least in that case. */
3495 for (asect = abfd->sections; asect; asect = asect->next)
3497 if (sect_syms[asect->index] == NULL)
3499 if (!sym_is_global (abfd, asect->symbol))
3506 /* Now sort the symbols so the local symbols are first. */
3507 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3508 sizeof (asymbol *));
3510 if (new_syms == NULL)
3513 for (idx = 0; idx < symcount; idx++)
3515 asymbol *sym = syms[idx];
3518 if (sym_is_global (abfd, sym))
3519 i = num_locals + num_globals2++;
3520 else if (!ignore_section_sym (abfd, sym))
3525 sym->udata.i = i + 1;
3527 for (asect = abfd->sections; asect; asect = asect->next)
3529 if (sect_syms[asect->index] == NULL)
3531 asymbol *sym = asect->symbol;
3534 sect_syms[asect->index] = sym;
3535 if (!sym_is_global (abfd, sym))
3538 i = num_locals + num_globals2++;
3540 sym->udata.i = i + 1;
3544 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3546 *pnum_locals = num_locals;
3550 /* Align to the maximum file alignment that could be required for any
3551 ELF data structure. */
3553 static inline file_ptr
3554 align_file_position (file_ptr off, int align)
3556 return (off + align - 1) & ~(align - 1);
3559 /* Assign a file position to a section, optionally aligning to the
3560 required section alignment. */
3563 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3567 if (align && i_shdrp->sh_addralign > 1)
3568 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3569 i_shdrp->sh_offset = offset;
3570 if (i_shdrp->bfd_section != NULL)
3571 i_shdrp->bfd_section->filepos = offset;
3572 if (i_shdrp->sh_type != SHT_NOBITS)
3573 offset += i_shdrp->sh_size;
3577 /* Compute the file positions we are going to put the sections at, and
3578 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3579 is not NULL, this is being called by the ELF backend linker. */
3582 _bfd_elf_compute_section_file_positions (bfd *abfd,
3583 struct bfd_link_info *link_info)
3585 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3586 struct fake_section_arg fsargs;
3588 struct bfd_strtab_hash *strtab = NULL;
3589 Elf_Internal_Shdr *shstrtab_hdr;
3590 bfd_boolean need_symtab;
3592 if (abfd->output_has_begun)
3595 /* Do any elf backend specific processing first. */
3596 if (bed->elf_backend_begin_write_processing)
3597 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3599 if (! prep_headers (abfd))
3602 /* Post process the headers if necessary. */
3603 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3605 fsargs.failed = FALSE;
3606 fsargs.link_info = link_info;
3607 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3611 if (!assign_section_numbers (abfd, link_info))
3614 /* The backend linker builds symbol table information itself. */
3615 need_symtab = (link_info == NULL
3616 && (bfd_get_symcount (abfd) > 0
3617 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3621 /* Non-zero if doing a relocatable link. */
3622 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3624 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3629 if (link_info == NULL)
3631 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3636 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3637 /* sh_name was set in prep_headers. */
3638 shstrtab_hdr->sh_type = SHT_STRTAB;
3639 shstrtab_hdr->sh_flags = 0;
3640 shstrtab_hdr->sh_addr = 0;
3641 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3642 shstrtab_hdr->sh_entsize = 0;
3643 shstrtab_hdr->sh_link = 0;
3644 shstrtab_hdr->sh_info = 0;
3645 /* sh_offset is set in assign_file_positions_except_relocs. */
3646 shstrtab_hdr->sh_addralign = 1;
3648 if (!assign_file_positions_except_relocs (abfd, link_info))
3654 Elf_Internal_Shdr *hdr;
3656 off = elf_next_file_pos (abfd);
3658 hdr = &elf_tdata (abfd)->symtab_hdr;
3659 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3661 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3662 if (hdr->sh_size != 0)
3663 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3665 hdr = &elf_tdata (abfd)->strtab_hdr;
3666 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3668 elf_next_file_pos (abfd) = off;
3670 /* Now that we know where the .strtab section goes, write it
3672 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3673 || ! _bfd_stringtab_emit (abfd, strtab))
3675 _bfd_stringtab_free (strtab);
3678 abfd->output_has_begun = TRUE;
3683 /* Make an initial estimate of the size of the program header. If we
3684 get the number wrong here, we'll redo section placement. */
3686 static bfd_size_type
3687 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3691 const struct elf_backend_data *bed;
3693 /* Assume we will need exactly two PT_LOAD segments: one for text
3694 and one for data. */
3697 s = bfd_get_section_by_name (abfd, ".interp");
3698 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3700 /* If we have a loadable interpreter section, we need a
3701 PT_INTERP segment. In this case, assume we also need a
3702 PT_PHDR segment, although that may not be true for all
3707 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3709 /* We need a PT_DYNAMIC segment. */
3713 if (info != NULL && info->relro)
3715 /* We need a PT_GNU_RELRO segment. */
3719 if (elf_eh_frame_hdr (abfd))
3721 /* We need a PT_GNU_EH_FRAME segment. */
3725 if (elf_stack_flags (abfd))
3727 /* We need a PT_GNU_STACK segment. */
3731 for (s = abfd->sections; s != NULL; s = s->next)
3733 if ((s->flags & SEC_LOAD) != 0
3734 && CONST_STRNEQ (s->name, ".note"))
3736 /* We need a PT_NOTE segment. */
3738 /* Try to create just one PT_NOTE segment
3739 for all adjacent loadable .note* sections.
3740 gABI requires that within a PT_NOTE segment
3741 (and also inside of each SHT_NOTE section)
3742 each note is padded to a multiple of 4 size,
3743 so we check whether the sections are correctly
3745 if (s->alignment_power == 2)
3746 while (s->next != NULL
3747 && s->next->alignment_power == 2
3748 && (s->next->flags & SEC_LOAD) != 0
3749 && CONST_STRNEQ (s->next->name, ".note"))
3754 for (s = abfd->sections; s != NULL; s = s->next)
3756 if (s->flags & SEC_THREAD_LOCAL)
3758 /* We need a PT_TLS segment. */
3764 /* Let the backend count up any program headers it might need. */
3765 bed = get_elf_backend_data (abfd);
3766 if (bed->elf_backend_additional_program_headers)
3770 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3776 return segs * bed->s->sizeof_phdr;
3779 /* Find the segment that contains the output_section of section. */
3782 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3784 struct elf_segment_map *m;
3785 Elf_Internal_Phdr *p;
3787 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
3793 for (i = m->count - 1; i >= 0; i--)
3794 if (m->sections[i] == section)
3801 /* Create a mapping from a set of sections to a program segment. */
3803 static struct elf_segment_map *
3804 make_mapping (bfd *abfd,
3805 asection **sections,
3810 struct elf_segment_map *m;
3815 amt = sizeof (struct elf_segment_map);
3816 amt += (to - from - 1) * sizeof (asection *);
3817 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3821 m->p_type = PT_LOAD;
3822 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3823 m->sections[i - from] = *hdrpp;
3824 m->count = to - from;
3826 if (from == 0 && phdr)
3828 /* Include the headers in the first PT_LOAD segment. */
3829 m->includes_filehdr = 1;
3830 m->includes_phdrs = 1;
3836 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3839 struct elf_segment_map *
3840 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3842 struct elf_segment_map *m;
3844 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3845 sizeof (struct elf_segment_map));
3849 m->p_type = PT_DYNAMIC;
3851 m->sections[0] = dynsec;
3856 /* Possibly add or remove segments from the segment map. */
3859 elf_modify_segment_map (bfd *abfd,
3860 struct bfd_link_info *info,
3861 bfd_boolean remove_empty_load)
3863 struct elf_segment_map **m;
3864 const struct elf_backend_data *bed;
3866 /* The placement algorithm assumes that non allocated sections are
3867 not in PT_LOAD segments. We ensure this here by removing such
3868 sections from the segment map. We also remove excluded
3869 sections. Finally, any PT_LOAD segment without sections is
3871 m = &elf_seg_map (abfd);
3874 unsigned int i, new_count;
3876 for (new_count = 0, i = 0; i < (*m)->count; i++)
3878 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3879 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3880 || (*m)->p_type != PT_LOAD))
3882 (*m)->sections[new_count] = (*m)->sections[i];
3886 (*m)->count = new_count;
3888 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3894 bed = get_elf_backend_data (abfd);
3895 if (bed->elf_backend_modify_segment_map != NULL)
3897 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3904 /* Set up a mapping from BFD sections to program segments. */
3907 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3910 struct elf_segment_map *m;
3911 asection **sections = NULL;
3912 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3913 bfd_boolean no_user_phdrs;
3915 no_user_phdrs = elf_seg_map (abfd) == NULL;
3918 info->user_phdrs = !no_user_phdrs;
3920 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3924 struct elf_segment_map *mfirst;
3925 struct elf_segment_map **pm;
3928 unsigned int phdr_index;
3929 bfd_vma maxpagesize;
3931 bfd_boolean phdr_in_segment = TRUE;
3932 bfd_boolean writable;
3934 asection *first_tls = NULL;
3935 asection *dynsec, *eh_frame_hdr;
3937 bfd_vma addr_mask, wrap_to = 0;
3939 /* Select the allocated sections, and sort them. */
3941 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3942 sizeof (asection *));
3943 if (sections == NULL)
3946 /* Calculate top address, avoiding undefined behaviour of shift
3947 left operator when shift count is equal to size of type
3949 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3950 addr_mask = (addr_mask << 1) + 1;
3953 for (s = abfd->sections; s != NULL; s = s->next)
3955 if ((s->flags & SEC_ALLOC) != 0)
3959 /* A wrapping section potentially clashes with header. */
3960 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3961 wrap_to = (s->lma + s->size) & addr_mask;
3964 BFD_ASSERT (i <= bfd_count_sections (abfd));
3967 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3969 /* Build the mapping. */
3974 /* If we have a .interp section, then create a PT_PHDR segment for
3975 the program headers and a PT_INTERP segment for the .interp
3977 s = bfd_get_section_by_name (abfd, ".interp");
3978 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3980 amt = sizeof (struct elf_segment_map);
3981 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3985 m->p_type = PT_PHDR;
3986 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3987 m->p_flags = PF_R | PF_X;
3988 m->p_flags_valid = 1;
3989 m->includes_phdrs = 1;
3994 amt = sizeof (struct elf_segment_map);
3995 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3999 m->p_type = PT_INTERP;
4007 /* Look through the sections. We put sections in the same program
4008 segment when the start of the second section can be placed within
4009 a few bytes of the end of the first section. */
4013 maxpagesize = bed->maxpagesize;
4015 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4017 && (dynsec->flags & SEC_LOAD) == 0)
4020 /* Deal with -Ttext or something similar such that the first section
4021 is not adjacent to the program headers. This is an
4022 approximation, since at this point we don't know exactly how many
4023 program headers we will need. */
4026 bfd_size_type phdr_size = elf_program_header_size (abfd);
4028 if (phdr_size == (bfd_size_type) -1)
4029 phdr_size = get_program_header_size (abfd, info);
4030 phdr_size += bed->s->sizeof_ehdr;
4031 if ((abfd->flags & D_PAGED) == 0
4032 || (sections[0]->lma & addr_mask) < phdr_size
4033 || ((sections[0]->lma & addr_mask) % maxpagesize
4034 < phdr_size % maxpagesize)
4035 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
4036 phdr_in_segment = FALSE;
4039 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4042 bfd_boolean new_segment;
4046 /* See if this section and the last one will fit in the same
4049 if (last_hdr == NULL)
4051 /* If we don't have a segment yet, then we don't need a new
4052 one (we build the last one after this loop). */
4053 new_segment = FALSE;
4055 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4057 /* If this section has a different relation between the
4058 virtual address and the load address, then we need a new
4062 else if (hdr->lma < last_hdr->lma + last_size
4063 || last_hdr->lma + last_size < last_hdr->lma)
4065 /* If this section has a load address that makes it overlap
4066 the previous section, then we need a new segment. */
4069 /* In the next test we have to be careful when last_hdr->lma is close
4070 to the end of the address space. If the aligned address wraps
4071 around to the start of the address space, then there are no more
4072 pages left in memory and it is OK to assume that the current
4073 section can be included in the current segment. */
4074 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4076 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4079 /* If putting this section in this segment would force us to
4080 skip a page in the segment, then we need a new segment. */
4083 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4084 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4086 /* We don't want to put a loadable section after a
4087 nonloadable section in the same segment.
4088 Consider .tbss sections as loadable for this purpose. */
4091 else if ((abfd->flags & D_PAGED) == 0)
4093 /* If the file is not demand paged, which means that we
4094 don't require the sections to be correctly aligned in the
4095 file, then there is no other reason for a new segment. */
4096 new_segment = FALSE;
4099 && (hdr->flags & SEC_READONLY) == 0
4100 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4101 != (hdr->lma & -maxpagesize)))
4103 /* We don't want to put a writable section in a read only
4104 segment, unless they are on the same page in memory
4105 anyhow. We already know that the last section does not
4106 bring us past the current section on the page, so the
4107 only case in which the new section is not on the same
4108 page as the previous section is when the previous section
4109 ends precisely on a page boundary. */
4114 /* Otherwise, we can use the same segment. */
4115 new_segment = FALSE;
4118 /* Allow interested parties a chance to override our decision. */
4119 if (last_hdr != NULL
4121 && info->callbacks->override_segment_assignment != NULL)
4123 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4129 if ((hdr->flags & SEC_READONLY) == 0)
4132 /* .tbss sections effectively have zero size. */
4133 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4134 != SEC_THREAD_LOCAL)
4135 last_size = hdr->size;
4141 /* We need a new program segment. We must create a new program
4142 header holding all the sections from phdr_index until hdr. */
4144 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4151 if ((hdr->flags & SEC_READONLY) == 0)
4157 /* .tbss sections effectively have zero size. */
4158 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4159 last_size = hdr->size;
4163 phdr_in_segment = FALSE;
4166 /* Create a final PT_LOAD program segment, but not if it's just
4168 if (last_hdr != NULL
4169 && (i - phdr_index != 1
4170 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4171 != SEC_THREAD_LOCAL)))
4173 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4181 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4184 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4191 /* For each batch of consecutive loadable .note sections,
4192 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4193 because if we link together nonloadable .note sections and
4194 loadable .note sections, we will generate two .note sections
4195 in the output file. FIXME: Using names for section types is
4197 for (s = abfd->sections; s != NULL; s = s->next)
4199 if ((s->flags & SEC_LOAD) != 0
4200 && CONST_STRNEQ (s->name, ".note"))
4205 amt = sizeof (struct elf_segment_map);
4206 if (s->alignment_power == 2)
4207 for (s2 = s; s2->next != NULL; s2 = s2->next)
4209 if (s2->next->alignment_power == 2
4210 && (s2->next->flags & SEC_LOAD) != 0
4211 && CONST_STRNEQ (s2->next->name, ".note")
4212 && align_power (s2->lma + s2->size, 2)
4218 amt += (count - 1) * sizeof (asection *);
4219 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4223 m->p_type = PT_NOTE;
4227 m->sections[m->count - count--] = s;
4228 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4231 m->sections[m->count - 1] = s;
4232 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4236 if (s->flags & SEC_THREAD_LOCAL)
4244 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4247 amt = sizeof (struct elf_segment_map);
4248 amt += (tls_count - 1) * sizeof (asection *);
4249 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4254 m->count = tls_count;
4255 /* Mandated PF_R. */
4257 m->p_flags_valid = 1;
4259 for (i = 0; i < (unsigned int) tls_count; ++i)
4261 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4264 (_("%B: TLS sections are not adjacent:"), abfd);
4267 while (i < (unsigned int) tls_count)
4269 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4271 _bfd_error_handler (_(" TLS: %A"), s);
4275 _bfd_error_handler (_(" non-TLS: %A"), s);
4278 bfd_set_error (bfd_error_bad_value);
4289 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4291 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4292 if (eh_frame_hdr != NULL
4293 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4295 amt = sizeof (struct elf_segment_map);
4296 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4300 m->p_type = PT_GNU_EH_FRAME;
4302 m->sections[0] = eh_frame_hdr->output_section;
4308 if (elf_stack_flags (abfd))
4310 amt = sizeof (struct elf_segment_map);
4311 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4315 m->p_type = PT_GNU_STACK;
4316 m->p_flags = elf_stack_flags (abfd);
4317 m->p_align = bed->stack_align;
4318 m->p_flags_valid = 1;
4319 m->p_align_valid = m->p_align != 0;
4320 if (info->stacksize > 0)
4322 m->p_size = info->stacksize;
4323 m->p_size_valid = 1;
4330 if (info != NULL && info->relro)
4332 for (m = mfirst; m != NULL; m = m->next)
4334 if (m->p_type == PT_LOAD
4336 && m->sections[0]->vma >= info->relro_start
4337 && m->sections[0]->vma < info->relro_end)
4340 while (--i != (unsigned) -1)
4341 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4342 == (SEC_LOAD | SEC_HAS_CONTENTS))
4345 if (i != (unsigned) -1)
4350 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4353 amt = sizeof (struct elf_segment_map);
4354 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4358 m->p_type = PT_GNU_RELRO;
4360 m->p_flags_valid = 1;
4368 elf_seg_map (abfd) = mfirst;
4371 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4374 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4376 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4381 if (sections != NULL)
4386 /* Sort sections by address. */
4389 elf_sort_sections (const void *arg1, const void *arg2)
4391 const asection *sec1 = *(const asection **) arg1;
4392 const asection *sec2 = *(const asection **) arg2;
4393 bfd_size_type size1, size2;
4395 /* Sort by LMA first, since this is the address used to
4396 place the section into a segment. */
4397 if (sec1->lma < sec2->lma)
4399 else if (sec1->lma > sec2->lma)
4402 /* Then sort by VMA. Normally the LMA and the VMA will be
4403 the same, and this will do nothing. */
4404 if (sec1->vma < sec2->vma)
4406 else if (sec1->vma > sec2->vma)
4409 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4411 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4417 /* If the indicies are the same, do not return 0
4418 here, but continue to try the next comparison. */
4419 if (sec1->target_index - sec2->target_index != 0)
4420 return sec1->target_index - sec2->target_index;
4425 else if (TOEND (sec2))
4430 /* Sort by size, to put zero sized sections
4431 before others at the same address. */
4433 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4434 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4441 return sec1->target_index - sec2->target_index;
4444 /* Ian Lance Taylor writes:
4446 We shouldn't be using % with a negative signed number. That's just
4447 not good. We have to make sure either that the number is not
4448 negative, or that the number has an unsigned type. When the types
4449 are all the same size they wind up as unsigned. When file_ptr is a
4450 larger signed type, the arithmetic winds up as signed long long,
4453 What we're trying to say here is something like ``increase OFF by
4454 the least amount that will cause it to be equal to the VMA modulo
4456 /* In other words, something like:
4458 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4459 off_offset = off % bed->maxpagesize;
4460 if (vma_offset < off_offset)
4461 adjustment = vma_offset + bed->maxpagesize - off_offset;
4463 adjustment = vma_offset - off_offset;
4465 which can can be collapsed into the expression below. */
4468 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4470 /* PR binutils/16199: Handle an alignment of zero. */
4471 if (maxpagesize == 0)
4473 return ((vma - off) % maxpagesize);
4477 print_segment_map (const struct elf_segment_map *m)
4480 const char *pt = get_segment_type (m->p_type);
4485 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4486 sprintf (buf, "LOPROC+%7.7x",
4487 (unsigned int) (m->p_type - PT_LOPROC));
4488 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4489 sprintf (buf, "LOOS+%7.7x",
4490 (unsigned int) (m->p_type - PT_LOOS));
4492 snprintf (buf, sizeof (buf), "%8.8x",
4493 (unsigned int) m->p_type);
4497 fprintf (stderr, "%s:", pt);
4498 for (j = 0; j < m->count; j++)
4499 fprintf (stderr, " %s", m->sections [j]->name);
4505 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4510 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4512 buf = bfd_zmalloc (len);
4515 ret = bfd_bwrite (buf, len, abfd) == len;
4520 /* Assign file positions to the sections based on the mapping from
4521 sections to segments. This function also sets up some fields in
4525 assign_file_positions_for_load_sections (bfd *abfd,
4526 struct bfd_link_info *link_info)
4528 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4529 struct elf_segment_map *m;
4530 Elf_Internal_Phdr *phdrs;
4531 Elf_Internal_Phdr *p;
4533 bfd_size_type maxpagesize;
4536 bfd_vma header_pad = 0;
4538 if (link_info == NULL
4539 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4543 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4547 header_pad = m->header_size;
4552 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4553 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4557 /* PR binutils/12467. */
4558 elf_elfheader (abfd)->e_phoff = 0;
4559 elf_elfheader (abfd)->e_phentsize = 0;
4562 elf_elfheader (abfd)->e_phnum = alloc;
4564 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4565 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4567 BFD_ASSERT (elf_program_header_size (abfd)
4568 >= alloc * bed->s->sizeof_phdr);
4572 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4576 /* We're writing the size in elf_program_header_size (abfd),
4577 see assign_file_positions_except_relocs, so make sure we have
4578 that amount allocated, with trailing space cleared.
4579 The variable alloc contains the computed need, while
4580 elf_program_header_size (abfd) contains the size used for the
4582 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4583 where the layout is forced to according to a larger size in the
4584 last iterations for the testcase ld-elf/header. */
4585 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4587 phdrs = (Elf_Internal_Phdr *)
4589 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4590 sizeof (Elf_Internal_Phdr));
4591 elf_tdata (abfd)->phdr = phdrs;
4596 if ((abfd->flags & D_PAGED) != 0)
4597 maxpagesize = bed->maxpagesize;
4599 off = bed->s->sizeof_ehdr;
4600 off += alloc * bed->s->sizeof_phdr;
4601 if (header_pad < (bfd_vma) off)
4607 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4609 m = m->next, p++, j++)
4613 bfd_boolean no_contents;
4615 /* If elf_segment_map is not from map_sections_to_segments, the
4616 sections may not be correctly ordered. NOTE: sorting should
4617 not be done to the PT_NOTE section of a corefile, which may
4618 contain several pseudo-sections artificially created by bfd.
4619 Sorting these pseudo-sections breaks things badly. */
4621 && !(elf_elfheader (abfd)->e_type == ET_CORE
4622 && m->p_type == PT_NOTE))
4623 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4626 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4627 number of sections with contents contributing to both p_filesz
4628 and p_memsz, followed by a number of sections with no contents
4629 that just contribute to p_memsz. In this loop, OFF tracks next
4630 available file offset for PT_LOAD and PT_NOTE segments. */
4631 p->p_type = m->p_type;
4632 p->p_flags = m->p_flags;
4637 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4639 if (m->p_paddr_valid)
4640 p->p_paddr = m->p_paddr;
4641 else if (m->count == 0)
4644 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4646 if (p->p_type == PT_LOAD
4647 && (abfd->flags & D_PAGED) != 0)
4649 /* p_align in demand paged PT_LOAD segments effectively stores
4650 the maximum page size. When copying an executable with
4651 objcopy, we set m->p_align from the input file. Use this
4652 value for maxpagesize rather than bed->maxpagesize, which
4653 may be different. Note that we use maxpagesize for PT_TLS
4654 segment alignment later in this function, so we are relying
4655 on at least one PT_LOAD segment appearing before a PT_TLS
4657 if (m->p_align_valid)
4658 maxpagesize = m->p_align;
4660 p->p_align = maxpagesize;
4662 else if (m->p_align_valid)
4663 p->p_align = m->p_align;
4664 else if (m->count == 0)
4665 p->p_align = 1 << bed->s->log_file_align;
4669 no_contents = FALSE;
4671 if (p->p_type == PT_LOAD
4674 bfd_size_type align;
4675 unsigned int align_power = 0;
4677 if (m->p_align_valid)
4681 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4683 unsigned int secalign;
4685 secalign = bfd_get_section_alignment (abfd, *secpp);
4686 if (secalign > align_power)
4687 align_power = secalign;
4689 align = (bfd_size_type) 1 << align_power;
4690 if (align < maxpagesize)
4691 align = maxpagesize;
4694 for (i = 0; i < m->count; i++)
4695 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4696 /* If we aren't making room for this section, then
4697 it must be SHT_NOBITS regardless of what we've
4698 set via struct bfd_elf_special_section. */
4699 elf_section_type (m->sections[i]) = SHT_NOBITS;
4701 /* Find out whether this segment contains any loadable
4704 for (i = 0; i < m->count; i++)
4705 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4707 no_contents = FALSE;
4711 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4715 /* We shouldn't need to align the segment on disk since
4716 the segment doesn't need file space, but the gABI
4717 arguably requires the alignment and glibc ld.so
4718 checks it. So to comply with the alignment
4719 requirement but not waste file space, we adjust
4720 p_offset for just this segment. (OFF_ADJUST is
4721 subtracted from OFF later.) This may put p_offset
4722 past the end of file, but that shouldn't matter. */
4727 /* Make sure the .dynamic section is the first section in the
4728 PT_DYNAMIC segment. */
4729 else if (p->p_type == PT_DYNAMIC
4731 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4734 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4736 bfd_set_error (bfd_error_bad_value);
4739 /* Set the note section type to SHT_NOTE. */
4740 else if (p->p_type == PT_NOTE)
4741 for (i = 0; i < m->count; i++)
4742 elf_section_type (m->sections[i]) = SHT_NOTE;
4748 if (m->includes_filehdr)
4750 if (!m->p_flags_valid)
4752 p->p_filesz = bed->s->sizeof_ehdr;
4753 p->p_memsz = bed->s->sizeof_ehdr;
4756 if (p->p_vaddr < (bfd_vma) off)
4758 (*_bfd_error_handler)
4759 (_("%B: Not enough room for program headers, try linking with -N"),
4761 bfd_set_error (bfd_error_bad_value);
4766 if (!m->p_paddr_valid)
4771 if (m->includes_phdrs)
4773 if (!m->p_flags_valid)
4776 if (!m->includes_filehdr)
4778 p->p_offset = bed->s->sizeof_ehdr;
4782 p->p_vaddr -= off - p->p_offset;
4783 if (!m->p_paddr_valid)
4784 p->p_paddr -= off - p->p_offset;
4788 p->p_filesz += alloc * bed->s->sizeof_phdr;
4789 p->p_memsz += alloc * bed->s->sizeof_phdr;
4792 p->p_filesz += header_pad;
4793 p->p_memsz += header_pad;
4797 if (p->p_type == PT_LOAD
4798 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4800 if (!m->includes_filehdr && !m->includes_phdrs)
4806 adjust = off - (p->p_offset + p->p_filesz);
4808 p->p_filesz += adjust;
4809 p->p_memsz += adjust;
4813 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4814 maps. Set filepos for sections in PT_LOAD segments, and in
4815 core files, for sections in PT_NOTE segments.
4816 assign_file_positions_for_non_load_sections will set filepos
4817 for other sections and update p_filesz for other segments. */
4818 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4821 bfd_size_type align;
4822 Elf_Internal_Shdr *this_hdr;
4825 this_hdr = &elf_section_data (sec)->this_hdr;
4826 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4828 if ((p->p_type == PT_LOAD
4829 || p->p_type == PT_TLS)
4830 && (this_hdr->sh_type != SHT_NOBITS
4831 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4832 && ((this_hdr->sh_flags & SHF_TLS) == 0
4833 || p->p_type == PT_TLS))))
4835 bfd_vma p_start = p->p_paddr;
4836 bfd_vma p_end = p_start + p->p_memsz;
4837 bfd_vma s_start = sec->lma;
4838 bfd_vma adjust = s_start - p_end;
4842 || p_end < p_start))
4844 (*_bfd_error_handler)
4845 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4846 (unsigned long) s_start, (unsigned long) p_end);
4850 p->p_memsz += adjust;
4852 if (this_hdr->sh_type != SHT_NOBITS)
4854 if (p->p_filesz + adjust < p->p_memsz)
4856 /* We have a PROGBITS section following NOBITS ones.
4857 Allocate file space for the NOBITS section(s) and
4859 adjust = p->p_memsz - p->p_filesz;
4860 if (!write_zeros (abfd, off, adjust))
4864 p->p_filesz += adjust;
4868 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4870 /* The section at i == 0 is the one that actually contains
4874 this_hdr->sh_offset = sec->filepos = off;
4875 off += this_hdr->sh_size;
4876 p->p_filesz = this_hdr->sh_size;
4882 /* The rest are fake sections that shouldn't be written. */
4891 if (p->p_type == PT_LOAD)
4893 this_hdr->sh_offset = sec->filepos = off;
4894 if (this_hdr->sh_type != SHT_NOBITS)
4895 off += this_hdr->sh_size;
4897 else if (this_hdr->sh_type == SHT_NOBITS
4898 && (this_hdr->sh_flags & SHF_TLS) != 0
4899 && this_hdr->sh_offset == 0)
4901 /* This is a .tbss section that didn't get a PT_LOAD.
4902 (See _bfd_elf_map_sections_to_segments "Create a
4903 final PT_LOAD".) Set sh_offset to the value it
4904 would have if we had created a zero p_filesz and
4905 p_memsz PT_LOAD header for the section. This
4906 also makes the PT_TLS header have the same
4908 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
4910 this_hdr->sh_offset = sec->filepos = off + adjust;
4913 if (this_hdr->sh_type != SHT_NOBITS)
4915 p->p_filesz += this_hdr->sh_size;
4916 /* A load section without SHF_ALLOC is something like
4917 a note section in a PT_NOTE segment. These take
4918 file space but are not loaded into memory. */
4919 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4920 p->p_memsz += this_hdr->sh_size;
4922 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4924 if (p->p_type == PT_TLS)
4925 p->p_memsz += this_hdr->sh_size;
4927 /* .tbss is special. It doesn't contribute to p_memsz of
4929 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4930 p->p_memsz += this_hdr->sh_size;
4933 if (align > p->p_align
4934 && !m->p_align_valid
4935 && (p->p_type != PT_LOAD
4936 || (abfd->flags & D_PAGED) == 0))
4940 if (!m->p_flags_valid)
4943 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4945 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4952 /* Check that all sections are in a PT_LOAD segment.
4953 Don't check funky gdb generated core files. */
4954 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4956 bfd_boolean check_vma = TRUE;
4958 for (i = 1; i < m->count; i++)
4959 if (m->sections[i]->vma == m->sections[i - 1]->vma
4960 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4961 ->this_hdr), p) != 0
4962 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4963 ->this_hdr), p) != 0)
4965 /* Looks like we have overlays packed into the segment. */
4970 for (i = 0; i < m->count; i++)
4972 Elf_Internal_Shdr *this_hdr;
4975 sec = m->sections[i];
4976 this_hdr = &(elf_section_data(sec)->this_hdr);
4977 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
4978 && !ELF_TBSS_SPECIAL (this_hdr, p))
4980 (*_bfd_error_handler)
4981 (_("%B: section `%A' can't be allocated in segment %d"),
4983 print_segment_map (m);
4989 elf_next_file_pos (abfd) = off;
4993 /* Assign file positions for the other sections. */
4996 assign_file_positions_for_non_load_sections (bfd *abfd,
4997 struct bfd_link_info *link_info)
4999 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5000 Elf_Internal_Shdr **i_shdrpp;
5001 Elf_Internal_Shdr **hdrpp;
5002 Elf_Internal_Phdr *phdrs;
5003 Elf_Internal_Phdr *p;
5004 struct elf_segment_map *m;
5005 struct elf_segment_map *hdrs_segment;
5006 bfd_vma filehdr_vaddr, filehdr_paddr;
5007 bfd_vma phdrs_vaddr, phdrs_paddr;
5009 unsigned int num_sec;
5013 i_shdrpp = elf_elfsections (abfd);
5014 num_sec = elf_numsections (abfd);
5015 off = elf_next_file_pos (abfd);
5016 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5018 Elf_Internal_Shdr *hdr;
5021 if (hdr->bfd_section != NULL
5022 && (hdr->bfd_section->filepos != 0
5023 || (hdr->sh_type == SHT_NOBITS
5024 && hdr->contents == NULL)))
5025 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5026 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5028 if (hdr->sh_size != 0)
5029 (*_bfd_error_handler)
5030 (_("%B: warning: allocated section `%s' not in segment"),
5032 (hdr->bfd_section == NULL
5034 : hdr->bfd_section->name));
5035 /* We don't need to page align empty sections. */
5036 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5037 off += vma_page_aligned_bias (hdr->sh_addr, off,
5040 off += vma_page_aligned_bias (hdr->sh_addr, off,
5042 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5045 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5046 && hdr->bfd_section == NULL)
5047 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5048 || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
5049 || hdr == i_shdrpp[elf_strtab_sec (abfd)])
5050 hdr->sh_offset = -1;
5052 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5055 /* Now that we have set the section file positions, we can set up
5056 the file positions for the non PT_LOAD segments. */
5060 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5062 hdrs_segment = NULL;
5063 phdrs = elf_tdata (abfd)->phdr;
5064 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5067 if (p->p_type != PT_LOAD)
5070 if (m->includes_filehdr)
5072 filehdr_vaddr = p->p_vaddr;
5073 filehdr_paddr = p->p_paddr;
5075 if (m->includes_phdrs)
5077 phdrs_vaddr = p->p_vaddr;
5078 phdrs_paddr = p->p_paddr;
5079 if (m->includes_filehdr)
5082 phdrs_vaddr += bed->s->sizeof_ehdr;
5083 phdrs_paddr += bed->s->sizeof_ehdr;
5088 if (hdrs_segment != NULL && link_info != NULL)
5090 /* There is a segment that contains both the file headers and the
5091 program headers, so provide a symbol __ehdr_start pointing there.
5092 A program can use this to examine itself robustly. */
5094 struct elf_link_hash_entry *hash
5095 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5096 FALSE, FALSE, TRUE);
5097 /* If the symbol was referenced and not defined, define it. */
5099 && (hash->root.type == bfd_link_hash_new
5100 || hash->root.type == bfd_link_hash_undefined
5101 || hash->root.type == bfd_link_hash_undefweak
5102 || hash->root.type == bfd_link_hash_common))
5105 if (hdrs_segment->count != 0)
5106 /* The segment contains sections, so use the first one. */
5107 s = hdrs_segment->sections[0];
5109 /* Use the first (i.e. lowest-addressed) section in any segment. */
5110 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5119 hash->root.u.def.value = filehdr_vaddr - s->vma;
5120 hash->root.u.def.section = s;
5124 hash->root.u.def.value = filehdr_vaddr;
5125 hash->root.u.def.section = bfd_abs_section_ptr;
5128 hash->root.type = bfd_link_hash_defined;
5129 hash->def_regular = 1;
5134 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5136 if (p->p_type == PT_GNU_RELRO)
5138 const Elf_Internal_Phdr *lp;
5139 struct elf_segment_map *lm;
5141 if (link_info != NULL)
5143 /* During linking the range of the RELRO segment is passed
5145 for (lm = elf_seg_map (abfd), lp = phdrs;
5147 lm = lm->next, lp++)
5149 if (lp->p_type == PT_LOAD
5150 && lp->p_vaddr < link_info->relro_end
5152 && lm->sections[0]->vma >= link_info->relro_start)
5156 BFD_ASSERT (lm != NULL);
5160 /* Otherwise we are copying an executable or shared
5161 library, but we need to use the same linker logic. */
5162 for (lp = phdrs; lp < phdrs + count; ++lp)
5164 if (lp->p_type == PT_LOAD
5165 && lp->p_paddr == p->p_paddr)
5170 if (lp < phdrs + count)
5172 p->p_vaddr = lp->p_vaddr;
5173 p->p_paddr = lp->p_paddr;
5174 p->p_offset = lp->p_offset;
5175 if (link_info != NULL)
5176 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5177 else if (m->p_size_valid)
5178 p->p_filesz = m->p_size;
5181 p->p_memsz = p->p_filesz;
5182 /* Preserve the alignment and flags if they are valid. The
5183 gold linker generates RW/4 for the PT_GNU_RELRO section.
5184 It is better for objcopy/strip to honor these attributes
5185 otherwise gdb will choke when using separate debug files.
5187 if (!m->p_align_valid)
5189 if (!m->p_flags_valid)
5190 p->p_flags = (lp->p_flags & ~PF_W);
5194 memset (p, 0, sizeof *p);
5195 p->p_type = PT_NULL;
5198 else if (p->p_type == PT_GNU_STACK)
5200 if (m->p_size_valid)
5201 p->p_memsz = m->p_size;
5203 else if (m->count != 0)
5205 if (p->p_type != PT_LOAD
5206 && (p->p_type != PT_NOTE
5207 || bfd_get_format (abfd) != bfd_core))
5209 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
5212 p->p_offset = m->sections[0]->filepos;
5213 for (i = m->count; i-- != 0;)
5215 asection *sect = m->sections[i];
5216 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5217 if (hdr->sh_type != SHT_NOBITS)
5219 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5226 else if (m->includes_filehdr)
5228 p->p_vaddr = filehdr_vaddr;
5229 if (! m->p_paddr_valid)
5230 p->p_paddr = filehdr_paddr;
5232 else if (m->includes_phdrs)
5234 p->p_vaddr = phdrs_vaddr;
5235 if (! m->p_paddr_valid)
5236 p->p_paddr = phdrs_paddr;
5240 elf_next_file_pos (abfd) = off;
5245 /* Work out the file positions of all the sections. This is called by
5246 _bfd_elf_compute_section_file_positions. All the section sizes and
5247 VMAs must be known before this is called.
5249 Reloc sections come in two flavours: Those processed specially as
5250 "side-channel" data attached to a section to which they apply, and
5251 those that bfd doesn't process as relocations. The latter sort are
5252 stored in a normal bfd section by bfd_section_from_shdr. We don't
5253 consider the former sort here, unless they form part of the loadable
5254 image. Reloc sections not assigned here will be handled later by
5255 assign_file_positions_for_relocs.
5257 We also don't set the positions of the .symtab and .strtab here. */
5260 assign_file_positions_except_relocs (bfd *abfd,
5261 struct bfd_link_info *link_info)
5263 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5264 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5265 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5267 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5268 && bfd_get_format (abfd) != bfd_core)
5270 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5271 unsigned int num_sec = elf_numsections (abfd);
5272 Elf_Internal_Shdr **hdrpp;
5276 /* Start after the ELF header. */
5277 off = i_ehdrp->e_ehsize;
5279 /* We are not creating an executable, which means that we are
5280 not creating a program header, and that the actual order of
5281 the sections in the file is unimportant. */
5282 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5284 Elf_Internal_Shdr *hdr;
5287 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5288 && hdr->bfd_section == NULL)
5289 || i == elf_onesymtab (abfd)
5290 || i == elf_symtab_shndx (abfd)
5291 || i == elf_strtab_sec (abfd))
5293 hdr->sh_offset = -1;
5296 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5299 elf_next_file_pos (abfd) = off;
5305 /* Assign file positions for the loaded sections based on the
5306 assignment of sections to segments. */
5307 if (!assign_file_positions_for_load_sections (abfd, link_info))
5310 /* And for non-load sections. */
5311 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5314 if (bed->elf_backend_modify_program_headers != NULL)
5316 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5320 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5321 if (link_info != NULL
5322 && link_info->executable
5323 && link_info->shared)
5325 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
5326 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
5327 Elf_Internal_Phdr *end_segment = &segment[num_segments];
5329 /* Find the lowest p_vaddr in PT_LOAD segments. */
5330 bfd_vma p_vaddr = (bfd_vma) -1;
5331 for (; segment < end_segment; segment++)
5332 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
5333 p_vaddr = segment->p_vaddr;
5335 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5336 segments is non-zero. */
5338 i_ehdrp->e_type = ET_EXEC;
5341 /* Write out the program headers. */
5342 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5343 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5344 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5352 prep_headers (bfd *abfd)
5354 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5355 struct elf_strtab_hash *shstrtab;
5356 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5358 i_ehdrp = elf_elfheader (abfd);
5360 shstrtab = _bfd_elf_strtab_init ();
5361 if (shstrtab == NULL)
5364 elf_shstrtab (abfd) = shstrtab;
5366 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5367 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5368 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5369 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5371 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5372 i_ehdrp->e_ident[EI_DATA] =
5373 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5374 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5376 if ((abfd->flags & DYNAMIC) != 0)
5377 i_ehdrp->e_type = ET_DYN;
5378 else if ((abfd->flags & EXEC_P) != 0)
5379 i_ehdrp->e_type = ET_EXEC;
5380 else if (bfd_get_format (abfd) == bfd_core)
5381 i_ehdrp->e_type = ET_CORE;
5383 i_ehdrp->e_type = ET_REL;
5385 switch (bfd_get_arch (abfd))
5387 case bfd_arch_unknown:
5388 i_ehdrp->e_machine = EM_NONE;
5391 /* There used to be a long list of cases here, each one setting
5392 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5393 in the corresponding bfd definition. To avoid duplication,
5394 the switch was removed. Machines that need special handling
5395 can generally do it in elf_backend_final_write_processing(),
5396 unless they need the information earlier than the final write.
5397 Such need can generally be supplied by replacing the tests for
5398 e_machine with the conditions used to determine it. */
5400 i_ehdrp->e_machine = bed->elf_machine_code;
5403 i_ehdrp->e_version = bed->s->ev_current;
5404 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5406 /* No program header, for now. */
5407 i_ehdrp->e_phoff = 0;
5408 i_ehdrp->e_phentsize = 0;
5409 i_ehdrp->e_phnum = 0;
5411 /* Each bfd section is section header entry. */
5412 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5413 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5415 /* If we're building an executable, we'll need a program header table. */
5416 if (abfd->flags & EXEC_P)
5417 /* It all happens later. */
5421 i_ehdrp->e_phentsize = 0;
5422 i_ehdrp->e_phoff = 0;
5425 elf_tdata (abfd)->symtab_hdr.sh_name =
5426 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5427 elf_tdata (abfd)->strtab_hdr.sh_name =
5428 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5429 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5430 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5431 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5432 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
5433 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5439 /* Assign file positions for all the reloc sections which are not part
5440 of the loadable file image, and the file position of section headers. */
5443 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5446 unsigned int i, num_sec;
5447 Elf_Internal_Shdr **shdrpp;
5448 Elf_Internal_Ehdr *i_ehdrp;
5449 const struct elf_backend_data *bed;
5451 off = elf_next_file_pos (abfd);
5453 num_sec = elf_numsections (abfd);
5454 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5456 Elf_Internal_Shdr *shdrp;
5459 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5460 && shdrp->sh_offset == -1)
5461 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5464 /* Place the section headers. */
5465 i_ehdrp = elf_elfheader (abfd);
5466 bed = get_elf_backend_data (abfd);
5467 off = align_file_position (off, 1 << bed->s->log_file_align);
5468 i_ehdrp->e_shoff = off;
5469 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5470 elf_next_file_pos (abfd) = off;
5474 _bfd_elf_write_object_contents (bfd *abfd)
5476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5477 Elf_Internal_Shdr **i_shdrp;
5479 unsigned int count, num_sec;
5480 struct elf_obj_tdata *t;
5482 if (! abfd->output_has_begun
5483 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5486 i_shdrp = elf_elfsections (abfd);
5489 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5493 _bfd_elf_assign_file_positions_for_relocs (abfd);
5495 /* After writing the headers, we need to write the sections too... */
5496 num_sec = elf_numsections (abfd);
5497 for (count = 1; count < num_sec; count++)
5499 if (bed->elf_backend_section_processing)
5500 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5501 if (i_shdrp[count]->contents)
5503 bfd_size_type amt = i_shdrp[count]->sh_size;
5505 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5506 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5511 /* Write out the section header names. */
5512 t = elf_tdata (abfd);
5513 if (elf_shstrtab (abfd) != NULL
5514 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5515 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5518 if (bed->elf_backend_final_write_processing)
5519 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5521 if (!bed->s->write_shdrs_and_ehdr (abfd))
5524 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5525 if (t->o->build_id.after_write_object_contents != NULL)
5526 return (*t->o->build_id.after_write_object_contents) (abfd);
5532 _bfd_elf_write_corefile_contents (bfd *abfd)
5534 /* Hopefully this can be done just like an object file. */
5535 return _bfd_elf_write_object_contents (abfd);
5538 /* Given a section, search the header to find them. */
5541 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5543 const struct elf_backend_data *bed;
5544 unsigned int sec_index;
5546 if (elf_section_data (asect) != NULL
5547 && elf_section_data (asect)->this_idx != 0)
5548 return elf_section_data (asect)->this_idx;
5550 if (bfd_is_abs_section (asect))
5551 sec_index = SHN_ABS;
5552 else if (bfd_is_com_section (asect))
5553 sec_index = SHN_COMMON;
5554 else if (bfd_is_und_section (asect))
5555 sec_index = SHN_UNDEF;
5557 sec_index = SHN_BAD;
5559 bed = get_elf_backend_data (abfd);
5560 if (bed->elf_backend_section_from_bfd_section)
5562 int retval = sec_index;
5564 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5568 if (sec_index == SHN_BAD)
5569 bfd_set_error (bfd_error_nonrepresentable_section);
5574 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5578 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5580 asymbol *asym_ptr = *asym_ptr_ptr;
5582 flagword flags = asym_ptr->flags;
5584 /* When gas creates relocations against local labels, it creates its
5585 own symbol for the section, but does put the symbol into the
5586 symbol chain, so udata is 0. When the linker is generating
5587 relocatable output, this section symbol may be for one of the
5588 input sections rather than the output section. */
5589 if (asym_ptr->udata.i == 0
5590 && (flags & BSF_SECTION_SYM)
5591 && asym_ptr->section)
5596 sec = asym_ptr->section;
5597 if (sec->owner != abfd && sec->output_section != NULL)
5598 sec = sec->output_section;
5599 if (sec->owner == abfd
5600 && (indx = sec->index) < elf_num_section_syms (abfd)
5601 && elf_section_syms (abfd)[indx] != NULL)
5602 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5605 idx = asym_ptr->udata.i;
5609 /* This case can occur when using --strip-symbol on a symbol
5610 which is used in a relocation entry. */
5611 (*_bfd_error_handler)
5612 (_("%B: symbol `%s' required but not present"),
5613 abfd, bfd_asymbol_name (asym_ptr));
5614 bfd_set_error (bfd_error_no_symbols);
5621 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5622 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5630 /* Rewrite program header information. */
5633 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5635 Elf_Internal_Ehdr *iehdr;
5636 struct elf_segment_map *map;
5637 struct elf_segment_map *map_first;
5638 struct elf_segment_map **pointer_to_map;
5639 Elf_Internal_Phdr *segment;
5642 unsigned int num_segments;
5643 bfd_boolean phdr_included = FALSE;
5644 bfd_boolean p_paddr_valid;
5645 bfd_vma maxpagesize;
5646 struct elf_segment_map *phdr_adjust_seg = NULL;
5647 unsigned int phdr_adjust_num = 0;
5648 const struct elf_backend_data *bed;
5650 bed = get_elf_backend_data (ibfd);
5651 iehdr = elf_elfheader (ibfd);
5654 pointer_to_map = &map_first;
5656 num_segments = elf_elfheader (ibfd)->e_phnum;
5657 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5659 /* Returns the end address of the segment + 1. */
5660 #define SEGMENT_END(segment, start) \
5661 (start + (segment->p_memsz > segment->p_filesz \
5662 ? segment->p_memsz : segment->p_filesz))
5664 #define SECTION_SIZE(section, segment) \
5665 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5666 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5667 ? section->size : 0)
5669 /* Returns TRUE if the given section is contained within
5670 the given segment. VMA addresses are compared. */
5671 #define IS_CONTAINED_BY_VMA(section, segment) \
5672 (section->vma >= segment->p_vaddr \
5673 && (section->vma + SECTION_SIZE (section, segment) \
5674 <= (SEGMENT_END (segment, segment->p_vaddr))))
5676 /* Returns TRUE if the given section is contained within
5677 the given segment. LMA addresses are compared. */
5678 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5679 (section->lma >= base \
5680 && (section->lma + SECTION_SIZE (section, segment) \
5681 <= SEGMENT_END (segment, base)))
5683 /* Handle PT_NOTE segment. */
5684 #define IS_NOTE(p, s) \
5685 (p->p_type == PT_NOTE \
5686 && elf_section_type (s) == SHT_NOTE \
5687 && (bfd_vma) s->filepos >= p->p_offset \
5688 && ((bfd_vma) s->filepos + s->size \
5689 <= p->p_offset + p->p_filesz))
5691 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5693 #define IS_COREFILE_NOTE(p, s) \
5695 && bfd_get_format (ibfd) == bfd_core \
5699 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5700 linker, which generates a PT_INTERP section with p_vaddr and
5701 p_memsz set to 0. */
5702 #define IS_SOLARIS_PT_INTERP(p, s) \
5704 && p->p_paddr == 0 \
5705 && p->p_memsz == 0 \
5706 && p->p_filesz > 0 \
5707 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5709 && (bfd_vma) s->filepos >= p->p_offset \
5710 && ((bfd_vma) s->filepos + s->size \
5711 <= p->p_offset + p->p_filesz))
5713 /* Decide if the given section should be included in the given segment.
5714 A section will be included if:
5715 1. It is within the address space of the segment -- we use the LMA
5716 if that is set for the segment and the VMA otherwise,
5717 2. It is an allocated section or a NOTE section in a PT_NOTE
5719 3. There is an output section associated with it,
5720 4. The section has not already been allocated to a previous segment.
5721 5. PT_GNU_STACK segments do not include any sections.
5722 6. PT_TLS segment includes only SHF_TLS sections.
5723 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5724 8. PT_DYNAMIC should not contain empty sections at the beginning
5725 (with the possible exception of .dynamic). */
5726 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5727 ((((segment->p_paddr \
5728 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5729 : IS_CONTAINED_BY_VMA (section, segment)) \
5730 && (section->flags & SEC_ALLOC) != 0) \
5731 || IS_NOTE (segment, section)) \
5732 && segment->p_type != PT_GNU_STACK \
5733 && (segment->p_type != PT_TLS \
5734 || (section->flags & SEC_THREAD_LOCAL)) \
5735 && (segment->p_type == PT_LOAD \
5736 || segment->p_type == PT_TLS \
5737 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5738 && (segment->p_type != PT_DYNAMIC \
5739 || SECTION_SIZE (section, segment) > 0 \
5740 || (segment->p_paddr \
5741 ? segment->p_paddr != section->lma \
5742 : segment->p_vaddr != section->vma) \
5743 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5745 && !section->segment_mark)
5747 /* If the output section of a section in the input segment is NULL,
5748 it is removed from the corresponding output segment. */
5749 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5750 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5751 && section->output_section != NULL)
5753 /* Returns TRUE iff seg1 starts after the end of seg2. */
5754 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5755 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5757 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5758 their VMA address ranges and their LMA address ranges overlap.
5759 It is possible to have overlapping VMA ranges without overlapping LMA
5760 ranges. RedBoot images for example can have both .data and .bss mapped
5761 to the same VMA range, but with the .data section mapped to a different
5763 #define SEGMENT_OVERLAPS(seg1, seg2) \
5764 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5765 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5766 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5767 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5769 /* Initialise the segment mark field. */
5770 for (section = ibfd->sections; section != NULL; section = section->next)
5771 section->segment_mark = FALSE;
5773 /* The Solaris linker creates program headers in which all the
5774 p_paddr fields are zero. When we try to objcopy or strip such a
5775 file, we get confused. Check for this case, and if we find it
5776 don't set the p_paddr_valid fields. */
5777 p_paddr_valid = FALSE;
5778 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5781 if (segment->p_paddr != 0)
5783 p_paddr_valid = TRUE;
5787 /* Scan through the segments specified in the program header
5788 of the input BFD. For this first scan we look for overlaps
5789 in the loadable segments. These can be created by weird
5790 parameters to objcopy. Also, fix some solaris weirdness. */
5791 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5796 Elf_Internal_Phdr *segment2;
5798 if (segment->p_type == PT_INTERP)
5799 for (section = ibfd->sections; section; section = section->next)
5800 if (IS_SOLARIS_PT_INTERP (segment, section))
5802 /* Mininal change so that the normal section to segment
5803 assignment code will work. */
5804 segment->p_vaddr = section->vma;
5808 if (segment->p_type != PT_LOAD)
5810 /* Remove PT_GNU_RELRO segment. */
5811 if (segment->p_type == PT_GNU_RELRO)
5812 segment->p_type = PT_NULL;
5816 /* Determine if this segment overlaps any previous segments. */
5817 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5819 bfd_signed_vma extra_length;
5821 if (segment2->p_type != PT_LOAD
5822 || !SEGMENT_OVERLAPS (segment, segment2))
5825 /* Merge the two segments together. */
5826 if (segment2->p_vaddr < segment->p_vaddr)
5828 /* Extend SEGMENT2 to include SEGMENT and then delete
5830 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5831 - SEGMENT_END (segment2, segment2->p_vaddr));
5833 if (extra_length > 0)
5835 segment2->p_memsz += extra_length;
5836 segment2->p_filesz += extra_length;
5839 segment->p_type = PT_NULL;
5841 /* Since we have deleted P we must restart the outer loop. */
5843 segment = elf_tdata (ibfd)->phdr;
5848 /* Extend SEGMENT to include SEGMENT2 and then delete
5850 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5851 - SEGMENT_END (segment, segment->p_vaddr));
5853 if (extra_length > 0)
5855 segment->p_memsz += extra_length;
5856 segment->p_filesz += extra_length;
5859 segment2->p_type = PT_NULL;
5864 /* The second scan attempts to assign sections to segments. */
5865 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5869 unsigned int section_count;
5870 asection **sections;
5871 asection *output_section;
5873 bfd_vma matching_lma;
5874 bfd_vma suggested_lma;
5877 asection *first_section;
5878 bfd_boolean first_matching_lma;
5879 bfd_boolean first_suggested_lma;
5881 if (segment->p_type == PT_NULL)
5884 first_section = NULL;
5885 /* Compute how many sections might be placed into this segment. */
5886 for (section = ibfd->sections, section_count = 0;
5888 section = section->next)
5890 /* Find the first section in the input segment, which may be
5891 removed from the corresponding output segment. */
5892 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5894 if (first_section == NULL)
5895 first_section = section;
5896 if (section->output_section != NULL)
5901 /* Allocate a segment map big enough to contain
5902 all of the sections we have selected. */
5903 amt = sizeof (struct elf_segment_map);
5904 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5905 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5909 /* Initialise the fields of the segment map. Default to
5910 using the physical address of the segment in the input BFD. */
5912 map->p_type = segment->p_type;
5913 map->p_flags = segment->p_flags;
5914 map->p_flags_valid = 1;
5916 /* If the first section in the input segment is removed, there is
5917 no need to preserve segment physical address in the corresponding
5919 if (!first_section || first_section->output_section != NULL)
5921 map->p_paddr = segment->p_paddr;
5922 map->p_paddr_valid = p_paddr_valid;
5925 /* Determine if this segment contains the ELF file header
5926 and if it contains the program headers themselves. */
5927 map->includes_filehdr = (segment->p_offset == 0
5928 && segment->p_filesz >= iehdr->e_ehsize);
5929 map->includes_phdrs = 0;
5931 if (!phdr_included || segment->p_type != PT_LOAD)
5933 map->includes_phdrs =
5934 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5935 && (segment->p_offset + segment->p_filesz
5936 >= ((bfd_vma) iehdr->e_phoff
5937 + iehdr->e_phnum * iehdr->e_phentsize)));
5939 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5940 phdr_included = TRUE;
5943 if (section_count == 0)
5945 /* Special segments, such as the PT_PHDR segment, may contain
5946 no sections, but ordinary, loadable segments should contain
5947 something. They are allowed by the ELF spec however, so only
5948 a warning is produced. */
5949 if (segment->p_type == PT_LOAD)
5950 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5951 " detected, is this intentional ?\n"),
5955 *pointer_to_map = map;
5956 pointer_to_map = &map->next;
5961 /* Now scan the sections in the input BFD again and attempt
5962 to add their corresponding output sections to the segment map.
5963 The problem here is how to handle an output section which has
5964 been moved (ie had its LMA changed). There are four possibilities:
5966 1. None of the sections have been moved.
5967 In this case we can continue to use the segment LMA from the
5970 2. All of the sections have been moved by the same amount.
5971 In this case we can change the segment's LMA to match the LMA
5972 of the first section.
5974 3. Some of the sections have been moved, others have not.
5975 In this case those sections which have not been moved can be
5976 placed in the current segment which will have to have its size,
5977 and possibly its LMA changed, and a new segment or segments will
5978 have to be created to contain the other sections.
5980 4. The sections have been moved, but not by the same amount.
5981 In this case we can change the segment's LMA to match the LMA
5982 of the first section and we will have to create a new segment
5983 or segments to contain the other sections.
5985 In order to save time, we allocate an array to hold the section
5986 pointers that we are interested in. As these sections get assigned
5987 to a segment, they are removed from this array. */
5989 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5990 if (sections == NULL)
5993 /* Step One: Scan for segment vs section LMA conflicts.
5994 Also add the sections to the section array allocated above.
5995 Also add the sections to the current segment. In the common
5996 case, where the sections have not been moved, this means that
5997 we have completely filled the segment, and there is nothing
6002 first_matching_lma = TRUE;
6003 first_suggested_lma = TRUE;
6005 for (section = ibfd->sections;
6007 section = section->next)
6008 if (section == first_section)
6011 for (j = 0; section != NULL; section = section->next)
6013 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
6015 output_section = section->output_section;
6017 sections[j++] = section;
6019 /* The Solaris native linker always sets p_paddr to 0.
6020 We try to catch that case here, and set it to the
6021 correct value. Note - some backends require that
6022 p_paddr be left as zero. */
6024 && segment->p_vaddr != 0
6025 && !bed->want_p_paddr_set_to_zero
6027 && output_section->lma != 0
6028 && output_section->vma == (segment->p_vaddr
6029 + (map->includes_filehdr
6032 + (map->includes_phdrs
6034 * iehdr->e_phentsize)
6036 map->p_paddr = segment->p_vaddr;
6038 /* Match up the physical address of the segment with the
6039 LMA address of the output section. */
6040 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6041 || IS_COREFILE_NOTE (segment, section)
6042 || (bed->want_p_paddr_set_to_zero
6043 && IS_CONTAINED_BY_VMA (output_section, segment)))
6045 if (first_matching_lma || output_section->lma < matching_lma)
6047 matching_lma = output_section->lma;
6048 first_matching_lma = FALSE;
6051 /* We assume that if the section fits within the segment
6052 then it does not overlap any other section within that
6054 map->sections[isec++] = output_section;
6056 else if (first_suggested_lma)
6058 suggested_lma = output_section->lma;
6059 first_suggested_lma = FALSE;
6062 if (j == section_count)
6067 BFD_ASSERT (j == section_count);
6069 /* Step Two: Adjust the physical address of the current segment,
6071 if (isec == section_count)
6073 /* All of the sections fitted within the segment as currently
6074 specified. This is the default case. Add the segment to
6075 the list of built segments and carry on to process the next
6076 program header in the input BFD. */
6077 map->count = section_count;
6078 *pointer_to_map = map;
6079 pointer_to_map = &map->next;
6082 && !bed->want_p_paddr_set_to_zero
6083 && matching_lma != map->p_paddr
6084 && !map->includes_filehdr
6085 && !map->includes_phdrs)
6086 /* There is some padding before the first section in the
6087 segment. So, we must account for that in the output
6089 map->p_vaddr_offset = matching_lma - map->p_paddr;
6096 if (!first_matching_lma)
6098 /* At least one section fits inside the current segment.
6099 Keep it, but modify its physical address to match the
6100 LMA of the first section that fitted. */
6101 map->p_paddr = matching_lma;
6105 /* None of the sections fitted inside the current segment.
6106 Change the current segment's physical address to match
6107 the LMA of the first section. */
6108 map->p_paddr = suggested_lma;
6111 /* Offset the segment physical address from the lma
6112 to allow for space taken up by elf headers. */
6113 if (map->includes_filehdr)
6115 if (map->p_paddr >= iehdr->e_ehsize)
6116 map->p_paddr -= iehdr->e_ehsize;
6119 map->includes_filehdr = FALSE;
6120 map->includes_phdrs = FALSE;
6124 if (map->includes_phdrs)
6126 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6128 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6130 /* iehdr->e_phnum is just an estimate of the number
6131 of program headers that we will need. Make a note
6132 here of the number we used and the segment we chose
6133 to hold these headers, so that we can adjust the
6134 offset when we know the correct value. */
6135 phdr_adjust_num = iehdr->e_phnum;
6136 phdr_adjust_seg = map;
6139 map->includes_phdrs = FALSE;
6143 /* Step Three: Loop over the sections again, this time assigning
6144 those that fit to the current segment and removing them from the
6145 sections array; but making sure not to leave large gaps. Once all
6146 possible sections have been assigned to the current segment it is
6147 added to the list of built segments and if sections still remain
6148 to be assigned, a new segment is constructed before repeating
6155 first_suggested_lma = TRUE;
6157 /* Fill the current segment with sections that fit. */
6158 for (j = 0; j < section_count; j++)
6160 section = sections[j];
6162 if (section == NULL)
6165 output_section = section->output_section;
6167 BFD_ASSERT (output_section != NULL);
6169 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6170 || IS_COREFILE_NOTE (segment, section))
6172 if (map->count == 0)
6174 /* If the first section in a segment does not start at
6175 the beginning of the segment, then something is
6177 if (output_section->lma
6179 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6180 + (map->includes_phdrs
6181 ? iehdr->e_phnum * iehdr->e_phentsize
6189 prev_sec = map->sections[map->count - 1];
6191 /* If the gap between the end of the previous section
6192 and the start of this section is more than
6193 maxpagesize then we need to start a new segment. */
6194 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6196 < BFD_ALIGN (output_section->lma, maxpagesize))
6197 || (prev_sec->lma + prev_sec->size
6198 > output_section->lma))
6200 if (first_suggested_lma)
6202 suggested_lma = output_section->lma;
6203 first_suggested_lma = FALSE;
6210 map->sections[map->count++] = output_section;
6213 section->segment_mark = TRUE;
6215 else if (first_suggested_lma)
6217 suggested_lma = output_section->lma;
6218 first_suggested_lma = FALSE;
6222 BFD_ASSERT (map->count > 0);
6224 /* Add the current segment to the list of built segments. */
6225 *pointer_to_map = map;
6226 pointer_to_map = &map->next;
6228 if (isec < section_count)
6230 /* We still have not allocated all of the sections to
6231 segments. Create a new segment here, initialise it
6232 and carry on looping. */
6233 amt = sizeof (struct elf_segment_map);
6234 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6235 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6242 /* Initialise the fields of the segment map. Set the physical
6243 physical address to the LMA of the first section that has
6244 not yet been assigned. */
6246 map->p_type = segment->p_type;
6247 map->p_flags = segment->p_flags;
6248 map->p_flags_valid = 1;
6249 map->p_paddr = suggested_lma;
6250 map->p_paddr_valid = p_paddr_valid;
6251 map->includes_filehdr = 0;
6252 map->includes_phdrs = 0;
6255 while (isec < section_count);
6260 elf_seg_map (obfd) = map_first;
6262 /* If we had to estimate the number of program headers that were
6263 going to be needed, then check our estimate now and adjust
6264 the offset if necessary. */
6265 if (phdr_adjust_seg != NULL)
6269 for (count = 0, map = map_first; map != NULL; map = map->next)
6272 if (count > phdr_adjust_num)
6273 phdr_adjust_seg->p_paddr
6274 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6279 #undef IS_CONTAINED_BY_VMA
6280 #undef IS_CONTAINED_BY_LMA
6282 #undef IS_COREFILE_NOTE
6283 #undef IS_SOLARIS_PT_INTERP
6284 #undef IS_SECTION_IN_INPUT_SEGMENT
6285 #undef INCLUDE_SECTION_IN_SEGMENT
6286 #undef SEGMENT_AFTER_SEGMENT
6287 #undef SEGMENT_OVERLAPS
6291 /* Copy ELF program header information. */
6294 copy_elf_program_header (bfd *ibfd, bfd *obfd)
6296 Elf_Internal_Ehdr *iehdr;
6297 struct elf_segment_map *map;
6298 struct elf_segment_map *map_first;
6299 struct elf_segment_map **pointer_to_map;
6300 Elf_Internal_Phdr *segment;
6302 unsigned int num_segments;
6303 bfd_boolean phdr_included = FALSE;
6304 bfd_boolean p_paddr_valid;
6306 iehdr = elf_elfheader (ibfd);
6309 pointer_to_map = &map_first;
6311 /* If all the segment p_paddr fields are zero, don't set
6312 map->p_paddr_valid. */
6313 p_paddr_valid = FALSE;
6314 num_segments = elf_elfheader (ibfd)->e_phnum;
6315 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6318 if (segment->p_paddr != 0)
6320 p_paddr_valid = TRUE;
6324 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6329 unsigned int section_count;
6331 Elf_Internal_Shdr *this_hdr;
6332 asection *first_section = NULL;
6333 asection *lowest_section;
6335 /* Compute how many sections are in this segment. */
6336 for (section = ibfd->sections, section_count = 0;
6338 section = section->next)
6340 this_hdr = &(elf_section_data(section)->this_hdr);
6341 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6343 if (first_section == NULL)
6344 first_section = section;
6349 /* Allocate a segment map big enough to contain
6350 all of the sections we have selected. */
6351 amt = sizeof (struct elf_segment_map);
6352 if (section_count != 0)
6353 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6354 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6358 /* Initialize the fields of the output segment map with the
6361 map->p_type = segment->p_type;
6362 map->p_flags = segment->p_flags;
6363 map->p_flags_valid = 1;
6364 map->p_paddr = segment->p_paddr;
6365 map->p_paddr_valid = p_paddr_valid;
6366 map->p_align = segment->p_align;
6367 map->p_align_valid = 1;
6368 map->p_vaddr_offset = 0;
6370 if (map->p_type == PT_GNU_RELRO
6371 || map->p_type == PT_GNU_STACK)
6373 /* The PT_GNU_RELRO segment may contain the first a few
6374 bytes in the .got.plt section even if the whole .got.plt
6375 section isn't in the PT_GNU_RELRO segment. We won't
6376 change the size of the PT_GNU_RELRO segment.
6377 Similarly, PT_GNU_STACK size is significant on uclinux
6379 map->p_size = segment->p_memsz;
6380 map->p_size_valid = 1;
6383 /* Determine if this segment contains the ELF file header
6384 and if it contains the program headers themselves. */
6385 map->includes_filehdr = (segment->p_offset == 0
6386 && segment->p_filesz >= iehdr->e_ehsize);
6388 map->includes_phdrs = 0;
6389 if (! phdr_included || segment->p_type != PT_LOAD)
6391 map->includes_phdrs =
6392 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6393 && (segment->p_offset + segment->p_filesz
6394 >= ((bfd_vma) iehdr->e_phoff
6395 + iehdr->e_phnum * iehdr->e_phentsize)));
6397 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6398 phdr_included = TRUE;
6401 lowest_section = NULL;
6402 if (section_count != 0)
6404 unsigned int isec = 0;
6406 for (section = first_section;
6408 section = section->next)
6410 this_hdr = &(elf_section_data(section)->this_hdr);
6411 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6413 map->sections[isec++] = section->output_section;
6414 if ((section->flags & SEC_ALLOC) != 0)
6418 if (lowest_section == NULL
6419 || section->lma < lowest_section->lma)
6420 lowest_section = section;
6422 /* Section lmas are set up from PT_LOAD header
6423 p_paddr in _bfd_elf_make_section_from_shdr.
6424 If this header has a p_paddr that disagrees
6425 with the section lma, flag the p_paddr as
6427 if ((section->flags & SEC_LOAD) != 0)
6428 seg_off = this_hdr->sh_offset - segment->p_offset;
6430 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6431 if (section->lma - segment->p_paddr != seg_off)
6432 map->p_paddr_valid = FALSE;
6434 if (isec == section_count)
6440 if (map->includes_filehdr && lowest_section != NULL)
6441 /* We need to keep the space used by the headers fixed. */
6442 map->header_size = lowest_section->vma - segment->p_vaddr;
6444 if (!map->includes_phdrs
6445 && !map->includes_filehdr
6446 && map->p_paddr_valid)
6447 /* There is some other padding before the first section. */
6448 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6449 - segment->p_paddr);
6451 map->count = section_count;
6452 *pointer_to_map = map;
6453 pointer_to_map = &map->next;
6456 elf_seg_map (obfd) = map_first;
6460 /* Copy private BFD data. This copies or rewrites ELF program header
6464 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6466 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6467 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6470 if (elf_tdata (ibfd)->phdr == NULL)
6473 if (ibfd->xvec == obfd->xvec)
6475 /* Check to see if any sections in the input BFD
6476 covered by ELF program header have changed. */
6477 Elf_Internal_Phdr *segment;
6478 asection *section, *osec;
6479 unsigned int i, num_segments;
6480 Elf_Internal_Shdr *this_hdr;
6481 const struct elf_backend_data *bed;
6483 bed = get_elf_backend_data (ibfd);
6485 /* Regenerate the segment map if p_paddr is set to 0. */
6486 if (bed->want_p_paddr_set_to_zero)
6489 /* Initialize the segment mark field. */
6490 for (section = obfd->sections; section != NULL;
6491 section = section->next)
6492 section->segment_mark = FALSE;
6494 num_segments = elf_elfheader (ibfd)->e_phnum;
6495 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6499 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6500 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6501 which severly confuses things, so always regenerate the segment
6502 map in this case. */
6503 if (segment->p_paddr == 0
6504 && segment->p_memsz == 0
6505 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6508 for (section = ibfd->sections;
6509 section != NULL; section = section->next)
6511 /* We mark the output section so that we know it comes
6512 from the input BFD. */
6513 osec = section->output_section;
6515 osec->segment_mark = TRUE;
6517 /* Check if this section is covered by the segment. */
6518 this_hdr = &(elf_section_data(section)->this_hdr);
6519 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6521 /* FIXME: Check if its output section is changed or
6522 removed. What else do we need to check? */
6524 || section->flags != osec->flags
6525 || section->lma != osec->lma
6526 || section->vma != osec->vma
6527 || section->size != osec->size
6528 || section->rawsize != osec->rawsize
6529 || section->alignment_power != osec->alignment_power)
6535 /* Check to see if any output section do not come from the
6537 for (section = obfd->sections; section != NULL;
6538 section = section->next)
6540 if (section->segment_mark == FALSE)
6543 section->segment_mark = FALSE;
6546 return copy_elf_program_header (ibfd, obfd);
6550 if (ibfd->xvec == obfd->xvec)
6552 /* When rewriting program header, set the output maxpagesize to
6553 the maximum alignment of input PT_LOAD segments. */
6554 Elf_Internal_Phdr *segment;
6556 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6557 bfd_vma maxpagesize = 0;
6559 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6562 if (segment->p_type == PT_LOAD
6563 && maxpagesize < segment->p_align)
6564 maxpagesize = segment->p_align;
6566 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6567 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6570 return rewrite_elf_program_header (ibfd, obfd);
6573 /* Initialize private output section information from input section. */
6576 _bfd_elf_init_private_section_data (bfd *ibfd,
6580 struct bfd_link_info *link_info)
6583 Elf_Internal_Shdr *ihdr, *ohdr;
6584 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6586 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6587 || obfd->xvec->flavour != bfd_target_elf_flavour)
6590 BFD_ASSERT (elf_section_data (osec) != NULL);
6592 /* For objcopy and relocatable link, don't copy the output ELF
6593 section type from input if the output BFD section flags have been
6594 set to something different. For a final link allow some flags
6595 that the linker clears to differ. */
6596 if (elf_section_type (osec) == SHT_NULL
6597 && (osec->flags == isec->flags
6599 && ((osec->flags ^ isec->flags)
6600 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6601 elf_section_type (osec) = elf_section_type (isec);
6603 /* FIXME: Is this correct for all OS/PROC specific flags? */
6604 elf_section_flags (osec) |= (elf_section_flags (isec)
6605 & (SHF_MASKOS | SHF_MASKPROC));
6607 /* Set things up for objcopy and relocatable link. The output
6608 SHT_GROUP section will have its elf_next_in_group pointing back
6609 to the input group members. Ignore linker created group section.
6610 See elfNN_ia64_object_p in elfxx-ia64.c. */
6613 if (elf_sec_group (isec) == NULL
6614 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6616 if (elf_section_flags (isec) & SHF_GROUP)
6617 elf_section_flags (osec) |= SHF_GROUP;
6618 elf_next_in_group (osec) = elf_next_in_group (isec);
6619 elf_section_data (osec)->group = elf_section_data (isec)->group;
6623 ihdr = &elf_section_data (isec)->this_hdr;
6625 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6626 don't use the output section of the linked-to section since it
6627 may be NULL at this point. */
6628 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6630 ohdr = &elf_section_data (osec)->this_hdr;
6631 ohdr->sh_flags |= SHF_LINK_ORDER;
6632 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6635 osec->use_rela_p = isec->use_rela_p;
6640 /* Copy private section information. This copies over the entsize
6641 field, and sometimes the info field. */
6644 _bfd_elf_copy_private_section_data (bfd *ibfd,
6649 Elf_Internal_Shdr *ihdr, *ohdr;
6651 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6652 || obfd->xvec->flavour != bfd_target_elf_flavour)
6655 ihdr = &elf_section_data (isec)->this_hdr;
6656 ohdr = &elf_section_data (osec)->this_hdr;
6658 ohdr->sh_entsize = ihdr->sh_entsize;
6660 if (ihdr->sh_type == SHT_SYMTAB
6661 || ihdr->sh_type == SHT_DYNSYM
6662 || ihdr->sh_type == SHT_GNU_verneed
6663 || ihdr->sh_type == SHT_GNU_verdef)
6664 ohdr->sh_info = ihdr->sh_info;
6666 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6670 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6671 necessary if we are removing either the SHT_GROUP section or any of
6672 the group member sections. DISCARDED is the value that a section's
6673 output_section has if the section will be discarded, NULL when this
6674 function is called from objcopy, bfd_abs_section_ptr when called
6678 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6682 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6683 if (elf_section_type (isec) == SHT_GROUP)
6685 asection *first = elf_next_in_group (isec);
6686 asection *s = first;
6687 bfd_size_type removed = 0;
6691 /* If this member section is being output but the
6692 SHT_GROUP section is not, then clear the group info
6693 set up by _bfd_elf_copy_private_section_data. */
6694 if (s->output_section != discarded
6695 && isec->output_section == discarded)
6697 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6698 elf_group_name (s->output_section) = NULL;
6700 /* Conversely, if the member section is not being output
6701 but the SHT_GROUP section is, then adjust its size. */
6702 else if (s->output_section == discarded
6703 && isec->output_section != discarded)
6705 s = elf_next_in_group (s);
6711 if (discarded != NULL)
6713 /* If we've been called for ld -r, then we need to
6714 adjust the input section size. This function may
6715 be called multiple times, so save the original
6717 if (isec->rawsize == 0)
6718 isec->rawsize = isec->size;
6719 isec->size = isec->rawsize - removed;
6723 /* Adjust the output section size when called from
6725 isec->output_section->size -= removed;
6733 /* Copy private header information. */
6736 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6738 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6739 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6742 /* Copy over private BFD data if it has not already been copied.
6743 This must be done here, rather than in the copy_private_bfd_data
6744 entry point, because the latter is called after the section
6745 contents have been set, which means that the program headers have
6746 already been worked out. */
6747 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
6749 if (! copy_private_bfd_data (ibfd, obfd))
6753 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6756 /* Copy private symbol information. If this symbol is in a section
6757 which we did not map into a BFD section, try to map the section
6758 index correctly. We use special macro definitions for the mapped
6759 section indices; these definitions are interpreted by the
6760 swap_out_syms function. */
6762 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6763 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6764 #define MAP_STRTAB (SHN_HIOS + 3)
6765 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6766 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6769 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6774 elf_symbol_type *isym, *osym;
6776 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6777 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6780 isym = elf_symbol_from (ibfd, isymarg);
6781 osym = elf_symbol_from (obfd, osymarg);
6784 && isym->internal_elf_sym.st_shndx != 0
6786 && bfd_is_abs_section (isym->symbol.section))
6790 shndx = isym->internal_elf_sym.st_shndx;
6791 if (shndx == elf_onesymtab (ibfd))
6792 shndx = MAP_ONESYMTAB;
6793 else if (shndx == elf_dynsymtab (ibfd))
6794 shndx = MAP_DYNSYMTAB;
6795 else if (shndx == elf_strtab_sec (ibfd))
6797 else if (shndx == elf_shstrtab_sec (ibfd))
6798 shndx = MAP_SHSTRTAB;
6799 else if (shndx == elf_symtab_shndx (ibfd))
6800 shndx = MAP_SYM_SHNDX;
6801 osym->internal_elf_sym.st_shndx = shndx;
6807 /* Swap out the symbols. */
6810 swap_out_syms (bfd *abfd,
6811 struct bfd_strtab_hash **sttp,
6814 const struct elf_backend_data *bed;
6817 struct bfd_strtab_hash *stt;
6818 Elf_Internal_Shdr *symtab_hdr;
6819 Elf_Internal_Shdr *symtab_shndx_hdr;
6820 Elf_Internal_Shdr *symstrtab_hdr;
6821 bfd_byte *outbound_syms;
6822 bfd_byte *outbound_shndx;
6824 unsigned int num_locals;
6826 bfd_boolean name_local_sections;
6828 if (!elf_map_symbols (abfd, &num_locals))
6831 /* Dump out the symtabs. */
6832 stt = _bfd_elf_stringtab_init ();
6836 bed = get_elf_backend_data (abfd);
6837 symcount = bfd_get_symcount (abfd);
6838 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6839 symtab_hdr->sh_type = SHT_SYMTAB;
6840 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6841 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6842 symtab_hdr->sh_info = num_locals + 1;
6843 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6845 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6846 symstrtab_hdr->sh_type = SHT_STRTAB;
6848 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6849 bed->s->sizeof_sym);
6850 if (outbound_syms == NULL)
6852 _bfd_stringtab_free (stt);
6855 symtab_hdr->contents = outbound_syms;
6857 outbound_shndx = NULL;
6858 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6859 if (symtab_shndx_hdr->sh_name != 0)
6861 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6862 outbound_shndx = (bfd_byte *)
6863 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6864 if (outbound_shndx == NULL)
6866 _bfd_stringtab_free (stt);
6870 symtab_shndx_hdr->contents = outbound_shndx;
6871 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6872 symtab_shndx_hdr->sh_size = amt;
6873 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6874 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6877 /* Now generate the data (for "contents"). */
6879 /* Fill in zeroth symbol and swap it out. */
6880 Elf_Internal_Sym sym;
6886 sym.st_shndx = SHN_UNDEF;
6887 sym.st_target_internal = 0;
6888 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6889 outbound_syms += bed->s->sizeof_sym;
6890 if (outbound_shndx != NULL)
6891 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6895 = (bed->elf_backend_name_local_section_symbols
6896 && bed->elf_backend_name_local_section_symbols (abfd));
6898 syms = bfd_get_outsymbols (abfd);
6899 for (idx = 0; idx < symcount; idx++)
6901 Elf_Internal_Sym sym;
6902 bfd_vma value = syms[idx]->value;
6903 elf_symbol_type *type_ptr;
6904 flagword flags = syms[idx]->flags;
6907 if (!name_local_sections
6908 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6910 /* Local section symbols have no name. */
6915 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6918 if (sym.st_name == (unsigned long) -1)
6920 _bfd_stringtab_free (stt);
6925 type_ptr = elf_symbol_from (abfd, syms[idx]);
6927 if ((flags & BSF_SECTION_SYM) == 0
6928 && bfd_is_com_section (syms[idx]->section))
6930 /* ELF common symbols put the alignment into the `value' field,
6931 and the size into the `size' field. This is backwards from
6932 how BFD handles it, so reverse it here. */
6933 sym.st_size = value;
6934 if (type_ptr == NULL
6935 || type_ptr->internal_elf_sym.st_value == 0)
6936 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6938 sym.st_value = type_ptr->internal_elf_sym.st_value;
6939 sym.st_shndx = _bfd_elf_section_from_bfd_section
6940 (abfd, syms[idx]->section);
6944 asection *sec = syms[idx]->section;
6947 if (sec->output_section)
6949 value += sec->output_offset;
6950 sec = sec->output_section;
6953 /* Don't add in the section vma for relocatable output. */
6954 if (! relocatable_p)
6956 sym.st_value = value;
6957 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6959 if (bfd_is_abs_section (sec)
6961 && type_ptr->internal_elf_sym.st_shndx != 0)
6963 /* This symbol is in a real ELF section which we did
6964 not create as a BFD section. Undo the mapping done
6965 by copy_private_symbol_data. */
6966 shndx = type_ptr->internal_elf_sym.st_shndx;
6970 shndx = elf_onesymtab (abfd);
6973 shndx = elf_dynsymtab (abfd);
6976 shndx = elf_strtab_sec (abfd);
6979 shndx = elf_shstrtab_sec (abfd);
6982 shndx = elf_symtab_shndx (abfd);
6991 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6993 if (shndx == SHN_BAD)
6997 /* Writing this would be a hell of a lot easier if
6998 we had some decent documentation on bfd, and
6999 knew what to expect of the library, and what to
7000 demand of applications. For example, it
7001 appears that `objcopy' might not set the
7002 section of a symbol to be a section that is
7003 actually in the output file. */
7004 sec2 = bfd_get_section_by_name (abfd, sec->name);
7007 _bfd_error_handler (_("\
7008 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7009 syms[idx]->name ? syms[idx]->name : "<Local sym>",
7011 bfd_set_error (bfd_error_invalid_operation);
7012 _bfd_stringtab_free (stt);
7016 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
7017 BFD_ASSERT (shndx != SHN_BAD);
7021 sym.st_shndx = shndx;
7024 if ((flags & BSF_THREAD_LOCAL) != 0)
7026 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
7027 type = STT_GNU_IFUNC;
7028 else if ((flags & BSF_FUNCTION) != 0)
7030 else if ((flags & BSF_OBJECT) != 0)
7032 else if ((flags & BSF_RELC) != 0)
7034 else if ((flags & BSF_SRELC) != 0)
7039 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7042 /* Processor-specific types. */
7043 if (type_ptr != NULL
7044 && bed->elf_backend_get_symbol_type)
7045 type = ((*bed->elf_backend_get_symbol_type)
7046 (&type_ptr->internal_elf_sym, type));
7048 if (flags & BSF_SECTION_SYM)
7050 if (flags & BSF_GLOBAL)
7051 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7053 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7055 else if (bfd_is_com_section (syms[idx]->section))
7057 #ifdef USE_STT_COMMON
7058 if (type == STT_OBJECT)
7059 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
7062 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
7064 else if (bfd_is_und_section (syms[idx]->section))
7065 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7069 else if (flags & BSF_FILE)
7070 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7073 int bind = STB_LOCAL;
7075 if (flags & BSF_LOCAL)
7077 else if (flags & BSF_GNU_UNIQUE)
7078 bind = STB_GNU_UNIQUE;
7079 else if (flags & BSF_WEAK)
7081 else if (flags & BSF_GLOBAL)
7084 sym.st_info = ELF_ST_INFO (bind, type);
7087 if (type_ptr != NULL)
7089 sym.st_other = type_ptr->internal_elf_sym.st_other;
7090 sym.st_target_internal
7091 = type_ptr->internal_elf_sym.st_target_internal;
7096 sym.st_target_internal = 0;
7099 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
7100 outbound_syms += bed->s->sizeof_sym;
7101 if (outbound_shndx != NULL)
7102 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
7106 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
7107 symstrtab_hdr->sh_type = SHT_STRTAB;
7109 symstrtab_hdr->sh_flags = 0;
7110 symstrtab_hdr->sh_addr = 0;
7111 symstrtab_hdr->sh_entsize = 0;
7112 symstrtab_hdr->sh_link = 0;
7113 symstrtab_hdr->sh_info = 0;
7114 symstrtab_hdr->sh_addralign = 1;
7119 /* Return the number of bytes required to hold the symtab vector.
7121 Note that we base it on the count plus 1, since we will null terminate
7122 the vector allocated based on this size. However, the ELF symbol table
7123 always has a dummy entry as symbol #0, so it ends up even. */
7126 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
7130 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7132 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7133 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7135 symtab_size -= sizeof (asymbol *);
7141 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7145 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7147 if (elf_dynsymtab (abfd) == 0)
7149 bfd_set_error (bfd_error_invalid_operation);
7153 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7154 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7156 symtab_size -= sizeof (asymbol *);
7162 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7165 return (asect->reloc_count + 1) * sizeof (arelent *);
7168 /* Canonicalize the relocs. */
7171 _bfd_elf_canonicalize_reloc (bfd *abfd,
7178 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7180 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7183 tblptr = section->relocation;
7184 for (i = 0; i < section->reloc_count; i++)
7185 *relptr++ = tblptr++;
7189 return section->reloc_count;
7193 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7195 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7196 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7199 bfd_get_symcount (abfd) = symcount;
7204 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7205 asymbol **allocation)
7207 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7208 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7211 bfd_get_dynamic_symcount (abfd) = symcount;
7215 /* Return the size required for the dynamic reloc entries. Any loadable
7216 section that was actually installed in the BFD, and has type SHT_REL
7217 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7218 dynamic reloc section. */
7221 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7226 if (elf_dynsymtab (abfd) == 0)
7228 bfd_set_error (bfd_error_invalid_operation);
7232 ret = sizeof (arelent *);
7233 for (s = abfd->sections; s != NULL; s = s->next)
7234 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7235 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7236 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7237 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7238 * sizeof (arelent *));
7243 /* Canonicalize the dynamic relocation entries. Note that we return the
7244 dynamic relocations as a single block, although they are actually
7245 associated with particular sections; the interface, which was
7246 designed for SunOS style shared libraries, expects that there is only
7247 one set of dynamic relocs. Any loadable section that was actually
7248 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7249 dynamic symbol table, is considered to be a dynamic reloc section. */
7252 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7256 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7260 if (elf_dynsymtab (abfd) == 0)
7262 bfd_set_error (bfd_error_invalid_operation);
7266 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7268 for (s = abfd->sections; s != NULL; s = s->next)
7270 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7271 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7272 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7277 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7279 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7281 for (i = 0; i < count; i++)
7292 /* Read in the version information. */
7295 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7297 bfd_byte *contents = NULL;
7298 unsigned int freeidx = 0;
7300 if (elf_dynverref (abfd) != 0)
7302 Elf_Internal_Shdr *hdr;
7303 Elf_External_Verneed *everneed;
7304 Elf_Internal_Verneed *iverneed;
7306 bfd_byte *contents_end;
7308 hdr = &elf_tdata (abfd)->dynverref_hdr;
7310 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed))
7312 error_return_bad_verref:
7313 (*_bfd_error_handler)
7314 (_("%B: .gnu.version_r invalid entry"), abfd);
7315 bfd_set_error (bfd_error_bad_value);
7316 error_return_verref:
7317 elf_tdata (abfd)->verref = NULL;
7318 elf_tdata (abfd)->cverrefs = 0;
7322 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7323 if (contents == NULL)
7324 goto error_return_verref;
7326 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7327 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7328 goto error_return_verref;
7330 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7331 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
7333 if (elf_tdata (abfd)->verref == NULL)
7334 goto error_return_verref;
7336 BFD_ASSERT (sizeof (Elf_External_Verneed)
7337 == sizeof (Elf_External_Vernaux));
7338 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7339 everneed = (Elf_External_Verneed *) contents;
7340 iverneed = elf_tdata (abfd)->verref;
7341 for (i = 0; i < hdr->sh_info; i++, iverneed++)
7343 Elf_External_Vernaux *evernaux;
7344 Elf_Internal_Vernaux *ivernaux;
7347 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7349 iverneed->vn_bfd = abfd;
7351 iverneed->vn_filename =
7352 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7354 if (iverneed->vn_filename == NULL)
7355 goto error_return_bad_verref;
7357 if (iverneed->vn_cnt == 0)
7358 iverneed->vn_auxptr = NULL;
7361 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7362 bfd_alloc2 (abfd, iverneed->vn_cnt,
7363 sizeof (Elf_Internal_Vernaux));
7364 if (iverneed->vn_auxptr == NULL)
7365 goto error_return_verref;
7368 if (iverneed->vn_aux
7369 > (size_t) (contents_end - (bfd_byte *) everneed))
7370 goto error_return_bad_verref;
7372 evernaux = ((Elf_External_Vernaux *)
7373 ((bfd_byte *) everneed + iverneed->vn_aux));
7374 ivernaux = iverneed->vn_auxptr;
7375 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7377 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7379 ivernaux->vna_nodename =
7380 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7381 ivernaux->vna_name);
7382 if (ivernaux->vna_nodename == NULL)
7383 goto error_return_bad_verref;
7385 if (ivernaux->vna_other > freeidx)
7386 freeidx = ivernaux->vna_other;
7388 ivernaux->vna_nextptr = NULL;
7389 if (ivernaux->vna_next == 0)
7391 iverneed->vn_cnt = j + 1;
7394 if (j + 1 < iverneed->vn_cnt)
7395 ivernaux->vna_nextptr = ivernaux + 1;
7397 if (ivernaux->vna_next
7398 > (size_t) (contents_end - (bfd_byte *) evernaux))
7399 goto error_return_bad_verref;
7401 evernaux = ((Elf_External_Vernaux *)
7402 ((bfd_byte *) evernaux + ivernaux->vna_next));
7405 iverneed->vn_nextref = NULL;
7406 if (iverneed->vn_next == 0)
7408 if (i + 1 < hdr->sh_info)
7409 iverneed->vn_nextref = iverneed + 1;
7411 if (iverneed->vn_next
7412 > (size_t) (contents_end - (bfd_byte *) everneed))
7413 goto error_return_bad_verref;
7415 everneed = ((Elf_External_Verneed *)
7416 ((bfd_byte *) everneed + iverneed->vn_next));
7418 elf_tdata (abfd)->cverrefs = i;
7424 if (elf_dynverdef (abfd) != 0)
7426 Elf_Internal_Shdr *hdr;
7427 Elf_External_Verdef *everdef;
7428 Elf_Internal_Verdef *iverdef;
7429 Elf_Internal_Verdef *iverdefarr;
7430 Elf_Internal_Verdef iverdefmem;
7432 unsigned int maxidx;
7433 bfd_byte *contents_end_def, *contents_end_aux;
7435 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7437 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
7439 error_return_bad_verdef:
7440 (*_bfd_error_handler)
7441 (_("%B: .gnu.version_d invalid entry"), abfd);
7442 bfd_set_error (bfd_error_bad_value);
7443 error_return_verdef:
7444 elf_tdata (abfd)->verdef = NULL;
7445 elf_tdata (abfd)->cverdefs = 0;
7449 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7450 if (contents == NULL)
7451 goto error_return_verdef;
7452 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7453 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7454 goto error_return_verdef;
7456 BFD_ASSERT (sizeof (Elf_External_Verdef)
7457 >= sizeof (Elf_External_Verdaux));
7458 contents_end_def = contents + hdr->sh_size
7459 - sizeof (Elf_External_Verdef);
7460 contents_end_aux = contents + hdr->sh_size
7461 - sizeof (Elf_External_Verdaux);
7463 /* We know the number of entries in the section but not the maximum
7464 index. Therefore we have to run through all entries and find
7466 everdef = (Elf_External_Verdef *) contents;
7468 for (i = 0; i < hdr->sh_info; ++i)
7470 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7472 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
7473 goto error_return_bad_verdef;
7474 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7475 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7477 if (iverdefmem.vd_next == 0)
7480 if (iverdefmem.vd_next
7481 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7482 goto error_return_bad_verdef;
7484 everdef = ((Elf_External_Verdef *)
7485 ((bfd_byte *) everdef + iverdefmem.vd_next));
7488 if (default_imported_symver)
7490 if (freeidx > maxidx)
7496 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7497 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7498 if (elf_tdata (abfd)->verdef == NULL)
7499 goto error_return_verdef;
7501 elf_tdata (abfd)->cverdefs = maxidx;
7503 everdef = (Elf_External_Verdef *) contents;
7504 iverdefarr = elf_tdata (abfd)->verdef;
7505 for (i = 0; i < hdr->sh_info; i++)
7507 Elf_External_Verdaux *everdaux;
7508 Elf_Internal_Verdaux *iverdaux;
7511 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7513 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7514 goto error_return_bad_verdef;
7516 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7517 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7519 iverdef->vd_bfd = abfd;
7521 if (iverdef->vd_cnt == 0)
7522 iverdef->vd_auxptr = NULL;
7525 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7526 bfd_alloc2 (abfd, iverdef->vd_cnt,
7527 sizeof (Elf_Internal_Verdaux));
7528 if (iverdef->vd_auxptr == NULL)
7529 goto error_return_verdef;
7533 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7534 goto error_return_bad_verdef;
7536 everdaux = ((Elf_External_Verdaux *)
7537 ((bfd_byte *) everdef + iverdef->vd_aux));
7538 iverdaux = iverdef->vd_auxptr;
7539 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7541 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7543 iverdaux->vda_nodename =
7544 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7545 iverdaux->vda_name);
7546 if (iverdaux->vda_nodename == NULL)
7547 goto error_return_bad_verdef;
7549 iverdaux->vda_nextptr = NULL;
7550 if (iverdaux->vda_next == 0)
7552 iverdef->vd_cnt = j + 1;
7555 if (j + 1 < iverdef->vd_cnt)
7556 iverdaux->vda_nextptr = iverdaux + 1;
7558 if (iverdaux->vda_next
7559 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7560 goto error_return_bad_verdef;
7562 everdaux = ((Elf_External_Verdaux *)
7563 ((bfd_byte *) everdaux + iverdaux->vda_next));
7566 if (iverdef->vd_cnt)
7567 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7569 iverdef->vd_nextdef = NULL;
7570 if (iverdef->vd_next == 0)
7572 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7573 iverdef->vd_nextdef = iverdef + 1;
7575 everdef = ((Elf_External_Verdef *)
7576 ((bfd_byte *) everdef + iverdef->vd_next));
7582 else if (default_imported_symver)
7589 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7590 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7591 if (elf_tdata (abfd)->verdef == NULL)
7594 elf_tdata (abfd)->cverdefs = freeidx;
7597 /* Create a default version based on the soname. */
7598 if (default_imported_symver)
7600 Elf_Internal_Verdef *iverdef;
7601 Elf_Internal_Verdaux *iverdaux;
7603 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
7605 iverdef->vd_version = VER_DEF_CURRENT;
7606 iverdef->vd_flags = 0;
7607 iverdef->vd_ndx = freeidx;
7608 iverdef->vd_cnt = 1;
7610 iverdef->vd_bfd = abfd;
7612 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7613 if (iverdef->vd_nodename == NULL)
7614 goto error_return_verdef;
7615 iverdef->vd_nextdef = NULL;
7616 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
7617 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
7618 if (iverdef->vd_auxptr == NULL)
7619 goto error_return_verdef;
7621 iverdaux = iverdef->vd_auxptr;
7622 iverdaux->vda_nodename = iverdef->vd_nodename;
7628 if (contents != NULL)
7634 _bfd_elf_make_empty_symbol (bfd *abfd)
7636 elf_symbol_type *newsym;
7638 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
7641 newsym->symbol.the_bfd = abfd;
7642 return &newsym->symbol;
7646 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7650 bfd_symbol_info (symbol, ret);
7653 /* Return whether a symbol name implies a local symbol. Most targets
7654 use this function for the is_local_label_name entry point, but some
7658 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7661 /* Normal local symbols start with ``.L''. */
7662 if (name[0] == '.' && name[1] == 'L')
7665 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7666 DWARF debugging symbols starting with ``..''. */
7667 if (name[0] == '.' && name[1] == '.')
7670 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7671 emitting DWARF debugging output. I suspect this is actually a
7672 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7673 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7674 underscore to be emitted on some ELF targets). For ease of use,
7675 we treat such symbols as local. */
7676 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7683 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7684 asymbol *symbol ATTRIBUTE_UNUSED)
7691 _bfd_elf_set_arch_mach (bfd *abfd,
7692 enum bfd_architecture arch,
7693 unsigned long machine)
7695 /* If this isn't the right architecture for this backend, and this
7696 isn't the generic backend, fail. */
7697 if (arch != get_elf_backend_data (abfd)->arch
7698 && arch != bfd_arch_unknown
7699 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7702 return bfd_default_set_arch_mach (abfd, arch, machine);
7705 /* Find the nearest line to a particular section and offset,
7706 for error reporting. */
7709 _bfd_elf_find_nearest_line (bfd *abfd,
7713 const char **filename_ptr,
7714 const char **functionname_ptr,
7715 unsigned int *line_ptr,
7716 unsigned int *discriminator_ptr)
7720 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7721 filename_ptr, functionname_ptr,
7722 line_ptr, discriminator_ptr,
7723 dwarf_debug_sections, 0,
7724 &elf_tdata (abfd)->dwarf2_find_line_info)
7725 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
7726 filename_ptr, functionname_ptr,
7729 if (!*functionname_ptr)
7730 _bfd_elf_find_function (abfd, symbols, section, offset,
7731 *filename_ptr ? NULL : filename_ptr,
7736 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7737 &found, filename_ptr,
7738 functionname_ptr, line_ptr,
7739 &elf_tdata (abfd)->line_info))
7741 if (found && (*functionname_ptr || *line_ptr))
7744 if (symbols == NULL)
7747 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
7748 filename_ptr, functionname_ptr))
7755 /* Find the line for a symbol. */
7758 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7759 const char **filename_ptr, unsigned int *line_ptr)
7761 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
7762 filename_ptr, NULL, line_ptr, NULL,
7763 dwarf_debug_sections, 0,
7764 &elf_tdata (abfd)->dwarf2_find_line_info);
7767 /* After a call to bfd_find_nearest_line, successive calls to
7768 bfd_find_inliner_info can be used to get source information about
7769 each level of function inlining that terminated at the address
7770 passed to bfd_find_nearest_line. Currently this is only supported
7771 for DWARF2 with appropriate DWARF3 extensions. */
7774 _bfd_elf_find_inliner_info (bfd *abfd,
7775 const char **filename_ptr,
7776 const char **functionname_ptr,
7777 unsigned int *line_ptr)
7780 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7781 functionname_ptr, line_ptr,
7782 & elf_tdata (abfd)->dwarf2_find_line_info);
7787 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7789 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7790 int ret = bed->s->sizeof_ehdr;
7792 if (!info->relocatable)
7794 bfd_size_type phdr_size = elf_program_header_size (abfd);
7796 if (phdr_size == (bfd_size_type) -1)
7798 struct elf_segment_map *m;
7801 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
7802 phdr_size += bed->s->sizeof_phdr;
7805 phdr_size = get_program_header_size (abfd, info);
7808 elf_program_header_size (abfd) = phdr_size;
7816 _bfd_elf_set_section_contents (bfd *abfd,
7818 const void *location,
7820 bfd_size_type count)
7822 Elf_Internal_Shdr *hdr;
7825 if (! abfd->output_has_begun
7826 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7829 hdr = &elf_section_data (section)->this_hdr;
7830 pos = hdr->sh_offset + offset;
7831 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7832 || bfd_bwrite (location, count, abfd) != count)
7839 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7840 arelent *cache_ptr ATTRIBUTE_UNUSED,
7841 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7846 /* Try to convert a non-ELF reloc into an ELF one. */
7849 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7851 /* Check whether we really have an ELF howto. */
7853 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7855 bfd_reloc_code_real_type code;
7856 reloc_howto_type *howto;
7858 /* Alien reloc: Try to determine its type to replace it with an
7859 equivalent ELF reloc. */
7861 if (areloc->howto->pc_relative)
7863 switch (areloc->howto->bitsize)
7866 code = BFD_RELOC_8_PCREL;
7869 code = BFD_RELOC_12_PCREL;
7872 code = BFD_RELOC_16_PCREL;
7875 code = BFD_RELOC_24_PCREL;
7878 code = BFD_RELOC_32_PCREL;
7881 code = BFD_RELOC_64_PCREL;
7887 howto = bfd_reloc_type_lookup (abfd, code);
7889 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7891 if (howto->pcrel_offset)
7892 areloc->addend += areloc->address;
7894 areloc->addend -= areloc->address; /* addend is unsigned!! */
7899 switch (areloc->howto->bitsize)
7905 code = BFD_RELOC_14;
7908 code = BFD_RELOC_16;
7911 code = BFD_RELOC_26;
7914 code = BFD_RELOC_32;
7917 code = BFD_RELOC_64;
7923 howto = bfd_reloc_type_lookup (abfd, code);
7927 areloc->howto = howto;
7935 (*_bfd_error_handler)
7936 (_("%B: unsupported relocation type %s"),
7937 abfd, areloc->howto->name);
7938 bfd_set_error (bfd_error_bad_value);
7943 _bfd_elf_close_and_cleanup (bfd *abfd)
7945 struct elf_obj_tdata *tdata = elf_tdata (abfd);
7946 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
7948 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
7949 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7950 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
7953 return _bfd_generic_close_and_cleanup (abfd);
7956 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7957 in the relocation's offset. Thus we cannot allow any sort of sanity
7958 range-checking to interfere. There is nothing else to do in processing
7961 bfd_reloc_status_type
7962 _bfd_elf_rel_vtable_reloc_fn
7963 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7964 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7965 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7966 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7968 return bfd_reloc_ok;
7971 /* Elf core file support. Much of this only works on native
7972 toolchains, since we rely on knowing the
7973 machine-dependent procfs structure in order to pick
7974 out details about the corefile. */
7976 #ifdef HAVE_SYS_PROCFS_H
7977 /* Needed for new procfs interface on sparc-solaris. */
7978 # define _STRUCTURED_PROC 1
7979 # include <sys/procfs.h>
7982 /* Return a PID that identifies a "thread" for threaded cores, or the
7983 PID of the main process for non-threaded cores. */
7986 elfcore_make_pid (bfd *abfd)
7990 pid = elf_tdata (abfd)->core->lwpid;
7992 pid = elf_tdata (abfd)->core->pid;
7997 /* If there isn't a section called NAME, make one, using
7998 data from SECT. Note, this function will generate a
7999 reference to NAME, so you shouldn't deallocate or
8003 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
8007 if (bfd_get_section_by_name (abfd, name) != NULL)
8010 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
8014 sect2->size = sect->size;
8015 sect2->filepos = sect->filepos;
8016 sect2->alignment_power = sect->alignment_power;
8020 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8021 actually creates up to two pseudosections:
8022 - For the single-threaded case, a section named NAME, unless
8023 such a section already exists.
8024 - For the multi-threaded case, a section named "NAME/PID", where
8025 PID is elfcore_make_pid (abfd).
8026 Both pseudosections have identical contents. */
8028 _bfd_elfcore_make_pseudosection (bfd *abfd,
8034 char *threaded_name;
8038 /* Build the section name. */
8040 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8041 len = strlen (buf) + 1;
8042 threaded_name = (char *) bfd_alloc (abfd, len);
8043 if (threaded_name == NULL)
8045 memcpy (threaded_name, buf, len);
8047 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8052 sect->filepos = filepos;
8053 sect->alignment_power = 2;
8055 return elfcore_maybe_make_sect (abfd, name, sect);
8058 /* prstatus_t exists on:
8060 linux 2.[01] + glibc
8064 #if defined (HAVE_PRSTATUS_T)
8067 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8072 if (note->descsz == sizeof (prstatus_t))
8076 size = sizeof (prstat.pr_reg);
8077 offset = offsetof (prstatus_t, pr_reg);
8078 memcpy (&prstat, note->descdata, sizeof (prstat));
8080 /* Do not overwrite the core signal if it
8081 has already been set by another thread. */
8082 if (elf_tdata (abfd)->core->signal == 0)
8083 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8084 if (elf_tdata (abfd)->core->pid == 0)
8085 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8087 /* pr_who exists on:
8090 pr_who doesn't exist on:
8093 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8094 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8096 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8099 #if defined (HAVE_PRSTATUS32_T)
8100 else if (note->descsz == sizeof (prstatus32_t))
8102 /* 64-bit host, 32-bit corefile */
8103 prstatus32_t prstat;
8105 size = sizeof (prstat.pr_reg);
8106 offset = offsetof (prstatus32_t, pr_reg);
8107 memcpy (&prstat, note->descdata, sizeof (prstat));
8109 /* Do not overwrite the core signal if it
8110 has already been set by another thread. */
8111 if (elf_tdata (abfd)->core->signal == 0)
8112 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8113 if (elf_tdata (abfd)->core->pid == 0)
8114 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8116 /* pr_who exists on:
8119 pr_who doesn't exist on:
8122 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8123 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8125 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8128 #endif /* HAVE_PRSTATUS32_T */
8131 /* Fail - we don't know how to handle any other
8132 note size (ie. data object type). */
8136 /* Make a ".reg/999" section and a ".reg" section. */
8137 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8138 size, note->descpos + offset);
8140 #endif /* defined (HAVE_PRSTATUS_T) */
8142 /* Create a pseudosection containing the exact contents of NOTE. */
8144 elfcore_make_note_pseudosection (bfd *abfd,
8146 Elf_Internal_Note *note)
8148 return _bfd_elfcore_make_pseudosection (abfd, name,
8149 note->descsz, note->descpos);
8152 /* There isn't a consistent prfpregset_t across platforms,
8153 but it doesn't matter, because we don't have to pick this
8154 data structure apart. */
8157 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8159 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8162 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8163 type of NT_PRXFPREG. Just include the whole note's contents
8167 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8169 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8172 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8173 with a note type of NT_X86_XSTATE. Just include the whole note's
8174 contents literally. */
8177 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8179 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8183 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8185 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8189 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8191 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8195 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8197 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8201 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8203 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8207 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8209 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8213 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8215 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8219 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8221 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8225 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8227 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8231 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8233 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8237 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8239 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8243 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8245 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8249 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8251 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8255 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8257 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8261 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8263 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8267 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8269 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8272 #if defined (HAVE_PRPSINFO_T)
8273 typedef prpsinfo_t elfcore_psinfo_t;
8274 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8275 typedef prpsinfo32_t elfcore_psinfo32_t;
8279 #if defined (HAVE_PSINFO_T)
8280 typedef psinfo_t elfcore_psinfo_t;
8281 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8282 typedef psinfo32_t elfcore_psinfo32_t;
8286 /* return a malloc'ed copy of a string at START which is at
8287 most MAX bytes long, possibly without a terminating '\0'.
8288 the copy will always have a terminating '\0'. */
8291 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8294 char *end = (char *) memchr (start, '\0', max);
8302 dups = (char *) bfd_alloc (abfd, len + 1);
8306 memcpy (dups, start, len);
8312 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8314 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8316 if (note->descsz == sizeof (elfcore_psinfo_t))
8318 elfcore_psinfo_t psinfo;
8320 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8322 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8323 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8325 elf_tdata (abfd)->core->program
8326 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8327 sizeof (psinfo.pr_fname));
8329 elf_tdata (abfd)->core->command
8330 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8331 sizeof (psinfo.pr_psargs));
8333 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8334 else if (note->descsz == sizeof (elfcore_psinfo32_t))
8336 /* 64-bit host, 32-bit corefile */
8337 elfcore_psinfo32_t psinfo;
8339 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8341 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8342 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8344 elf_tdata (abfd)->core->program
8345 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8346 sizeof (psinfo.pr_fname));
8348 elf_tdata (abfd)->core->command
8349 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8350 sizeof (psinfo.pr_psargs));
8356 /* Fail - we don't know how to handle any other
8357 note size (ie. data object type). */
8361 /* Note that for some reason, a spurious space is tacked
8362 onto the end of the args in some (at least one anyway)
8363 implementations, so strip it off if it exists. */
8366 char *command = elf_tdata (abfd)->core->command;
8367 int n = strlen (command);
8369 if (0 < n && command[n - 1] == ' ')
8370 command[n - 1] = '\0';
8375 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8377 #if defined (HAVE_PSTATUS_T)
8379 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8381 if (note->descsz == sizeof (pstatus_t)
8382 #if defined (HAVE_PXSTATUS_T)
8383 || note->descsz == sizeof (pxstatus_t)
8389 memcpy (&pstat, note->descdata, sizeof (pstat));
8391 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8393 #if defined (HAVE_PSTATUS32_T)
8394 else if (note->descsz == sizeof (pstatus32_t))
8396 /* 64-bit host, 32-bit corefile */
8399 memcpy (&pstat, note->descdata, sizeof (pstat));
8401 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8404 /* Could grab some more details from the "representative"
8405 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8406 NT_LWPSTATUS note, presumably. */
8410 #endif /* defined (HAVE_PSTATUS_T) */
8412 #if defined (HAVE_LWPSTATUS_T)
8414 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8416 lwpstatus_t lwpstat;
8422 if (note->descsz != sizeof (lwpstat)
8423 #if defined (HAVE_LWPXSTATUS_T)
8424 && note->descsz != sizeof (lwpxstatus_t)
8429 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8431 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8432 /* Do not overwrite the core signal if it has already been set by
8434 if (elf_tdata (abfd)->core->signal == 0)
8435 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8437 /* Make a ".reg/999" section. */
8439 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8440 len = strlen (buf) + 1;
8441 name = bfd_alloc (abfd, len);
8444 memcpy (name, buf, len);
8446 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8450 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8451 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8452 sect->filepos = note->descpos
8453 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8456 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8457 sect->size = sizeof (lwpstat.pr_reg);
8458 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8461 sect->alignment_power = 2;
8463 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8466 /* Make a ".reg2/999" section */
8468 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8469 len = strlen (buf) + 1;
8470 name = bfd_alloc (abfd, len);
8473 memcpy (name, buf, len);
8475 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8479 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8480 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8481 sect->filepos = note->descpos
8482 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8485 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8486 sect->size = sizeof (lwpstat.pr_fpreg);
8487 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8490 sect->alignment_power = 2;
8492 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8494 #endif /* defined (HAVE_LWPSTATUS_T) */
8497 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8504 int is_active_thread;
8507 if (note->descsz < 728)
8510 if (! CONST_STRNEQ (note->namedata, "win32"))
8513 type = bfd_get_32 (abfd, note->descdata);
8517 case 1 /* NOTE_INFO_PROCESS */:
8518 /* FIXME: need to add ->core->command. */
8519 /* process_info.pid */
8520 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
8521 /* process_info.signal */
8522 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
8525 case 2 /* NOTE_INFO_THREAD */:
8526 /* Make a ".reg/999" section. */
8527 /* thread_info.tid */
8528 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8530 len = strlen (buf) + 1;
8531 name = (char *) bfd_alloc (abfd, len);
8535 memcpy (name, buf, len);
8537 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8541 /* sizeof (thread_info.thread_context) */
8543 /* offsetof (thread_info.thread_context) */
8544 sect->filepos = note->descpos + 12;
8545 sect->alignment_power = 2;
8547 /* thread_info.is_active_thread */
8548 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8550 if (is_active_thread)
8551 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8555 case 3 /* NOTE_INFO_MODULE */:
8556 /* Make a ".module/xxxxxxxx" section. */
8557 /* module_info.base_address */
8558 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8559 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8561 len = strlen (buf) + 1;
8562 name = (char *) bfd_alloc (abfd, len);
8566 memcpy (name, buf, len);
8568 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8573 sect->size = note->descsz;
8574 sect->filepos = note->descpos;
8575 sect->alignment_power = 2;
8586 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8588 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8596 if (bed->elf_backend_grok_prstatus)
8597 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8599 #if defined (HAVE_PRSTATUS_T)
8600 return elfcore_grok_prstatus (abfd, note);
8605 #if defined (HAVE_PSTATUS_T)
8607 return elfcore_grok_pstatus (abfd, note);
8610 #if defined (HAVE_LWPSTATUS_T)
8612 return elfcore_grok_lwpstatus (abfd, note);
8615 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8616 return elfcore_grok_prfpreg (abfd, note);
8618 case NT_WIN32PSTATUS:
8619 return elfcore_grok_win32pstatus (abfd, note);
8621 case NT_PRXFPREG: /* Linux SSE extension */
8622 if (note->namesz == 6
8623 && strcmp (note->namedata, "LINUX") == 0)
8624 return elfcore_grok_prxfpreg (abfd, note);
8628 case NT_X86_XSTATE: /* Linux XSAVE extension */
8629 if (note->namesz == 6
8630 && strcmp (note->namedata, "LINUX") == 0)
8631 return elfcore_grok_xstatereg (abfd, note);
8636 if (note->namesz == 6
8637 && strcmp (note->namedata, "LINUX") == 0)
8638 return elfcore_grok_ppc_vmx (abfd, note);
8643 if (note->namesz == 6
8644 && strcmp (note->namedata, "LINUX") == 0)
8645 return elfcore_grok_ppc_vsx (abfd, note);
8649 case NT_S390_HIGH_GPRS:
8650 if (note->namesz == 6
8651 && strcmp (note->namedata, "LINUX") == 0)
8652 return elfcore_grok_s390_high_gprs (abfd, note);
8657 if (note->namesz == 6
8658 && strcmp (note->namedata, "LINUX") == 0)
8659 return elfcore_grok_s390_timer (abfd, note);
8663 case NT_S390_TODCMP:
8664 if (note->namesz == 6
8665 && strcmp (note->namedata, "LINUX") == 0)
8666 return elfcore_grok_s390_todcmp (abfd, note);
8670 case NT_S390_TODPREG:
8671 if (note->namesz == 6
8672 && strcmp (note->namedata, "LINUX") == 0)
8673 return elfcore_grok_s390_todpreg (abfd, note);
8678 if (note->namesz == 6
8679 && strcmp (note->namedata, "LINUX") == 0)
8680 return elfcore_grok_s390_ctrs (abfd, note);
8684 case NT_S390_PREFIX:
8685 if (note->namesz == 6
8686 && strcmp (note->namedata, "LINUX") == 0)
8687 return elfcore_grok_s390_prefix (abfd, note);
8691 case NT_S390_LAST_BREAK:
8692 if (note->namesz == 6
8693 && strcmp (note->namedata, "LINUX") == 0)
8694 return elfcore_grok_s390_last_break (abfd, note);
8698 case NT_S390_SYSTEM_CALL:
8699 if (note->namesz == 6
8700 && strcmp (note->namedata, "LINUX") == 0)
8701 return elfcore_grok_s390_system_call (abfd, note);
8706 if (note->namesz == 6
8707 && strcmp (note->namedata, "LINUX") == 0)
8708 return elfcore_grok_s390_tdb (abfd, note);
8713 if (note->namesz == 6
8714 && strcmp (note->namedata, "LINUX") == 0)
8715 return elfcore_grok_arm_vfp (abfd, note);
8720 if (note->namesz == 6
8721 && strcmp (note->namedata, "LINUX") == 0)
8722 return elfcore_grok_aarch_tls (abfd, note);
8726 case NT_ARM_HW_BREAK:
8727 if (note->namesz == 6
8728 && strcmp (note->namedata, "LINUX") == 0)
8729 return elfcore_grok_aarch_hw_break (abfd, note);
8733 case NT_ARM_HW_WATCH:
8734 if (note->namesz == 6
8735 && strcmp (note->namedata, "LINUX") == 0)
8736 return elfcore_grok_aarch_hw_watch (abfd, note);
8742 if (bed->elf_backend_grok_psinfo)
8743 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8745 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8746 return elfcore_grok_psinfo (abfd, note);
8753 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8758 sect->size = note->descsz;
8759 sect->filepos = note->descpos;
8760 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8766 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
8770 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
8776 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8778 struct elf_obj_tdata *t;
8780 if (note->descsz == 0)
8783 t = elf_tdata (abfd);
8784 t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
8785 if (t->build_id == NULL)
8788 t->build_id->size = note->descsz;
8789 memcpy (t->build_id->data, note->descdata, note->descsz);
8795 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8802 case NT_GNU_BUILD_ID:
8803 return elfobj_grok_gnu_build_id (abfd, note);
8808 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
8810 struct sdt_note *cur =
8811 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
8814 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
8815 cur->size = (bfd_size_type) note->descsz;
8816 memcpy (cur->data, note->descdata, note->descsz);
8818 elf_tdata (abfd)->sdt_note_head = cur;
8824 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
8829 return elfobj_grok_stapsdt_note_1 (abfd, note);
8837 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8841 cp = strchr (note->namedata, '@');
8844 *lwpidp = atoi(cp + 1);
8851 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8853 /* Signal number at offset 0x08. */
8854 elf_tdata (abfd)->core->signal
8855 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8857 /* Process ID at offset 0x50. */
8858 elf_tdata (abfd)->core->pid
8859 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8861 /* Command name at 0x7c (max 32 bytes, including nul). */
8862 elf_tdata (abfd)->core->command
8863 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8865 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8870 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8874 if (elfcore_netbsd_get_lwpid (note, &lwp))
8875 elf_tdata (abfd)->core->lwpid = lwp;
8877 if (note->type == NT_NETBSDCORE_PROCINFO)
8879 /* NetBSD-specific core "procinfo". Note that we expect to
8880 find this note before any of the others, which is fine,
8881 since the kernel writes this note out first when it
8882 creates a core file. */
8884 return elfcore_grok_netbsd_procinfo (abfd, note);
8887 /* As of Jan 2002 there are no other machine-independent notes
8888 defined for NetBSD core files. If the note type is less
8889 than the start of the machine-dependent note types, we don't
8892 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8896 switch (bfd_get_arch (abfd))
8898 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8899 PT_GETFPREGS == mach+2. */
8901 case bfd_arch_alpha:
8902 case bfd_arch_sparc:
8905 case NT_NETBSDCORE_FIRSTMACH+0:
8906 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8908 case NT_NETBSDCORE_FIRSTMACH+2:
8909 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8915 /* On all other arch's, PT_GETREGS == mach+1 and
8916 PT_GETFPREGS == mach+3. */
8921 case NT_NETBSDCORE_FIRSTMACH+1:
8922 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8924 case NT_NETBSDCORE_FIRSTMACH+3:
8925 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8935 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8937 /* Signal number at offset 0x08. */
8938 elf_tdata (abfd)->core->signal
8939 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8941 /* Process ID at offset 0x20. */
8942 elf_tdata (abfd)->core->pid
8943 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8945 /* Command name at 0x48 (max 32 bytes, including nul). */
8946 elf_tdata (abfd)->core->command
8947 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8953 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8955 if (note->type == NT_OPENBSD_PROCINFO)
8956 return elfcore_grok_openbsd_procinfo (abfd, note);
8958 if (note->type == NT_OPENBSD_REGS)
8959 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8961 if (note->type == NT_OPENBSD_FPREGS)
8962 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8964 if (note->type == NT_OPENBSD_XFPREGS)
8965 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8967 if (note->type == NT_OPENBSD_AUXV)
8969 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8974 sect->size = note->descsz;
8975 sect->filepos = note->descpos;
8976 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8981 if (note->type == NT_OPENBSD_WCOOKIE)
8983 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8988 sect->size = note->descsz;
8989 sect->filepos = note->descpos;
8990 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8999 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
9001 void *ddata = note->descdata;
9008 /* nto_procfs_status 'pid' field is at offset 0. */
9009 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
9011 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9012 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
9014 /* nto_procfs_status 'flags' field is at offset 8. */
9015 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
9017 /* nto_procfs_status 'what' field is at offset 14. */
9018 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9020 elf_tdata (abfd)->core->signal = sig;
9021 elf_tdata (abfd)->core->lwpid = *tid;
9024 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9025 do not come from signals so we make sure we set the current
9026 thread just in case. */
9027 if (flags & 0x00000080)
9028 elf_tdata (abfd)->core->lwpid = *tid;
9030 /* Make a ".qnx_core_status/%d" section. */
9031 sprintf (buf, ".qnx_core_status/%ld", *tid);
9033 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9038 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9042 sect->size = note->descsz;
9043 sect->filepos = note->descpos;
9044 sect->alignment_power = 2;
9046 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9050 elfcore_grok_nto_regs (bfd *abfd,
9051 Elf_Internal_Note *note,
9059 /* Make a "(base)/%d" section. */
9060 sprintf (buf, "%s/%ld", base, tid);
9062 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9067 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9071 sect->size = note->descsz;
9072 sect->filepos = note->descpos;
9073 sect->alignment_power = 2;
9075 /* This is the current thread. */
9076 if (elf_tdata (abfd)->core->lwpid == tid)
9077 return elfcore_maybe_make_sect (abfd, base, sect);
9082 #define BFD_QNT_CORE_INFO 7
9083 #define BFD_QNT_CORE_STATUS 8
9084 #define BFD_QNT_CORE_GREG 9
9085 #define BFD_QNT_CORE_FPREG 10
9088 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9090 /* Every GREG section has a STATUS section before it. Store the
9091 tid from the previous call to pass down to the next gregs
9093 static long tid = 1;
9097 case BFD_QNT_CORE_INFO:
9098 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9099 case BFD_QNT_CORE_STATUS:
9100 return elfcore_grok_nto_status (abfd, note, &tid);
9101 case BFD_QNT_CORE_GREG:
9102 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9103 case BFD_QNT_CORE_FPREG:
9104 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9111 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9117 /* Use note name as section name. */
9119 name = (char *) bfd_alloc (abfd, len);
9122 memcpy (name, note->namedata, len);
9123 name[len - 1] = '\0';
9125 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9129 sect->size = note->descsz;
9130 sect->filepos = note->descpos;
9131 sect->alignment_power = 1;
9136 /* Function: elfcore_write_note
9139 buffer to hold note, and current size of buffer
9143 size of data for note
9145 Writes note to end of buffer. ELF64 notes are written exactly as
9146 for ELF32, despite the current (as of 2006) ELF gabi specifying
9147 that they ought to have 8-byte namesz and descsz field, and have
9148 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9151 Pointer to realloc'd buffer, *BUFSIZ updated. */
9154 elfcore_write_note (bfd *abfd,
9162 Elf_External_Note *xnp;
9169 namesz = strlen (name) + 1;
9171 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9173 buf = (char *) realloc (buf, *bufsiz + newspace);
9176 dest = buf + *bufsiz;
9177 *bufsiz += newspace;
9178 xnp = (Elf_External_Note *) dest;
9179 H_PUT_32 (abfd, namesz, xnp->namesz);
9180 H_PUT_32 (abfd, size, xnp->descsz);
9181 H_PUT_32 (abfd, type, xnp->type);
9185 memcpy (dest, name, namesz);
9193 memcpy (dest, input, size);
9204 elfcore_write_prpsinfo (bfd *abfd,
9210 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9212 if (bed->elf_backend_write_core_note != NULL)
9215 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9216 NT_PRPSINFO, fname, psargs);
9221 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9222 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9223 if (bed->s->elfclass == ELFCLASS32)
9225 #if defined (HAVE_PSINFO32_T)
9227 int note_type = NT_PSINFO;
9230 int note_type = NT_PRPSINFO;
9233 memset (&data, 0, sizeof (data));
9234 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9235 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9236 return elfcore_write_note (abfd, buf, bufsiz,
9237 "CORE", note_type, &data, sizeof (data));
9242 #if defined (HAVE_PSINFO_T)
9244 int note_type = NT_PSINFO;
9247 int note_type = NT_PRPSINFO;
9250 memset (&data, 0, sizeof (data));
9251 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9252 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9253 return elfcore_write_note (abfd, buf, bufsiz,
9254 "CORE", note_type, &data, sizeof (data));
9256 #endif /* PSINFO_T or PRPSINFO_T */
9263 elfcore_write_linux_prpsinfo32
9264 (bfd *abfd, char *buf, int *bufsiz,
9265 const struct elf_internal_linux_prpsinfo *prpsinfo)
9267 struct elf_external_linux_prpsinfo32 data;
9269 memset (&data, 0, sizeof (data));
9270 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9272 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9273 &data, sizeof (data));
9277 elfcore_write_linux_prpsinfo64
9278 (bfd *abfd, char *buf, int *bufsiz,
9279 const struct elf_internal_linux_prpsinfo *prpsinfo)
9281 struct elf_external_linux_prpsinfo64 data;
9283 memset (&data, 0, sizeof (data));
9284 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9286 return elfcore_write_note (abfd, buf, bufsiz,
9287 "CORE", NT_PRPSINFO, &data, sizeof (data));
9291 elfcore_write_prstatus (bfd *abfd,
9298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9300 if (bed->elf_backend_write_core_note != NULL)
9303 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9305 pid, cursig, gregs);
9310 #if defined (HAVE_PRSTATUS_T)
9311 #if defined (HAVE_PRSTATUS32_T)
9312 if (bed->s->elfclass == ELFCLASS32)
9314 prstatus32_t prstat;
9316 memset (&prstat, 0, sizeof (prstat));
9317 prstat.pr_pid = pid;
9318 prstat.pr_cursig = cursig;
9319 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9320 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9321 NT_PRSTATUS, &prstat, sizeof (prstat));
9328 memset (&prstat, 0, sizeof (prstat));
9329 prstat.pr_pid = pid;
9330 prstat.pr_cursig = cursig;
9331 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9332 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9333 NT_PRSTATUS, &prstat, sizeof (prstat));
9335 #endif /* HAVE_PRSTATUS_T */
9341 #if defined (HAVE_LWPSTATUS_T)
9343 elfcore_write_lwpstatus (bfd *abfd,
9350 lwpstatus_t lwpstat;
9351 const char *note_name = "CORE";
9353 memset (&lwpstat, 0, sizeof (lwpstat));
9354 lwpstat.pr_lwpid = pid >> 16;
9355 lwpstat.pr_cursig = cursig;
9356 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9357 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9358 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9360 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9361 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9363 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9364 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9367 return elfcore_write_note (abfd, buf, bufsiz, note_name,
9368 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9370 #endif /* HAVE_LWPSTATUS_T */
9372 #if defined (HAVE_PSTATUS_T)
9374 elfcore_write_pstatus (bfd *abfd,
9378 int cursig ATTRIBUTE_UNUSED,
9379 const void *gregs ATTRIBUTE_UNUSED)
9381 const char *note_name = "CORE";
9382 #if defined (HAVE_PSTATUS32_T)
9383 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9385 if (bed->s->elfclass == ELFCLASS32)
9389 memset (&pstat, 0, sizeof (pstat));
9390 pstat.pr_pid = pid & 0xffff;
9391 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9392 NT_PSTATUS, &pstat, sizeof (pstat));
9400 memset (&pstat, 0, sizeof (pstat));
9401 pstat.pr_pid = pid & 0xffff;
9402 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9403 NT_PSTATUS, &pstat, sizeof (pstat));
9407 #endif /* HAVE_PSTATUS_T */
9410 elfcore_write_prfpreg (bfd *abfd,
9416 const char *note_name = "CORE";
9417 return elfcore_write_note (abfd, buf, bufsiz,
9418 note_name, NT_FPREGSET, fpregs, size);
9422 elfcore_write_prxfpreg (bfd *abfd,
9425 const void *xfpregs,
9428 char *note_name = "LINUX";
9429 return elfcore_write_note (abfd, buf, bufsiz,
9430 note_name, NT_PRXFPREG, xfpregs, size);
9434 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9435 const void *xfpregs, int size)
9437 char *note_name = "LINUX";
9438 return elfcore_write_note (abfd, buf, bufsiz,
9439 note_name, NT_X86_XSTATE, xfpregs, size);
9443 elfcore_write_ppc_vmx (bfd *abfd,
9446 const void *ppc_vmx,
9449 char *note_name = "LINUX";
9450 return elfcore_write_note (abfd, buf, bufsiz,
9451 note_name, NT_PPC_VMX, ppc_vmx, size);
9455 elfcore_write_ppc_vsx (bfd *abfd,
9458 const void *ppc_vsx,
9461 char *note_name = "LINUX";
9462 return elfcore_write_note (abfd, buf, bufsiz,
9463 note_name, NT_PPC_VSX, ppc_vsx, size);
9467 elfcore_write_s390_high_gprs (bfd *abfd,
9470 const void *s390_high_gprs,
9473 char *note_name = "LINUX";
9474 return elfcore_write_note (abfd, buf, bufsiz,
9475 note_name, NT_S390_HIGH_GPRS,
9476 s390_high_gprs, size);
9480 elfcore_write_s390_timer (bfd *abfd,
9483 const void *s390_timer,
9486 char *note_name = "LINUX";
9487 return elfcore_write_note (abfd, buf, bufsiz,
9488 note_name, NT_S390_TIMER, s390_timer, size);
9492 elfcore_write_s390_todcmp (bfd *abfd,
9495 const void *s390_todcmp,
9498 char *note_name = "LINUX";
9499 return elfcore_write_note (abfd, buf, bufsiz,
9500 note_name, NT_S390_TODCMP, s390_todcmp, size);
9504 elfcore_write_s390_todpreg (bfd *abfd,
9507 const void *s390_todpreg,
9510 char *note_name = "LINUX";
9511 return elfcore_write_note (abfd, buf, bufsiz,
9512 note_name, NT_S390_TODPREG, s390_todpreg, size);
9516 elfcore_write_s390_ctrs (bfd *abfd,
9519 const void *s390_ctrs,
9522 char *note_name = "LINUX";
9523 return elfcore_write_note (abfd, buf, bufsiz,
9524 note_name, NT_S390_CTRS, s390_ctrs, size);
9528 elfcore_write_s390_prefix (bfd *abfd,
9531 const void *s390_prefix,
9534 char *note_name = "LINUX";
9535 return elfcore_write_note (abfd, buf, bufsiz,
9536 note_name, NT_S390_PREFIX, s390_prefix, size);
9540 elfcore_write_s390_last_break (bfd *abfd,
9543 const void *s390_last_break,
9546 char *note_name = "LINUX";
9547 return elfcore_write_note (abfd, buf, bufsiz,
9548 note_name, NT_S390_LAST_BREAK,
9549 s390_last_break, size);
9553 elfcore_write_s390_system_call (bfd *abfd,
9556 const void *s390_system_call,
9559 char *note_name = "LINUX";
9560 return elfcore_write_note (abfd, buf, bufsiz,
9561 note_name, NT_S390_SYSTEM_CALL,
9562 s390_system_call, size);
9566 elfcore_write_s390_tdb (bfd *abfd,
9569 const void *s390_tdb,
9572 char *note_name = "LINUX";
9573 return elfcore_write_note (abfd, buf, bufsiz,
9574 note_name, NT_S390_TDB, s390_tdb, size);
9578 elfcore_write_arm_vfp (bfd *abfd,
9581 const void *arm_vfp,
9584 char *note_name = "LINUX";
9585 return elfcore_write_note (abfd, buf, bufsiz,
9586 note_name, NT_ARM_VFP, arm_vfp, size);
9590 elfcore_write_aarch_tls (bfd *abfd,
9593 const void *aarch_tls,
9596 char *note_name = "LINUX";
9597 return elfcore_write_note (abfd, buf, bufsiz,
9598 note_name, NT_ARM_TLS, aarch_tls, size);
9602 elfcore_write_aarch_hw_break (bfd *abfd,
9605 const void *aarch_hw_break,
9608 char *note_name = "LINUX";
9609 return elfcore_write_note (abfd, buf, bufsiz,
9610 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
9614 elfcore_write_aarch_hw_watch (bfd *abfd,
9617 const void *aarch_hw_watch,
9620 char *note_name = "LINUX";
9621 return elfcore_write_note (abfd, buf, bufsiz,
9622 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
9626 elfcore_write_register_note (bfd *abfd,
9629 const char *section,
9633 if (strcmp (section, ".reg2") == 0)
9634 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9635 if (strcmp (section, ".reg-xfp") == 0)
9636 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9637 if (strcmp (section, ".reg-xstate") == 0)
9638 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9639 if (strcmp (section, ".reg-ppc-vmx") == 0)
9640 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9641 if (strcmp (section, ".reg-ppc-vsx") == 0)
9642 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9643 if (strcmp (section, ".reg-s390-high-gprs") == 0)
9644 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9645 if (strcmp (section, ".reg-s390-timer") == 0)
9646 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9647 if (strcmp (section, ".reg-s390-todcmp") == 0)
9648 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9649 if (strcmp (section, ".reg-s390-todpreg") == 0)
9650 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9651 if (strcmp (section, ".reg-s390-ctrs") == 0)
9652 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9653 if (strcmp (section, ".reg-s390-prefix") == 0)
9654 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9655 if (strcmp (section, ".reg-s390-last-break") == 0)
9656 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
9657 if (strcmp (section, ".reg-s390-system-call") == 0)
9658 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
9659 if (strcmp (section, ".reg-s390-tdb") == 0)
9660 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
9661 if (strcmp (section, ".reg-arm-vfp") == 0)
9662 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
9663 if (strcmp (section, ".reg-aarch-tls") == 0)
9664 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
9665 if (strcmp (section, ".reg-aarch-hw-break") == 0)
9666 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
9667 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
9668 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
9673 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9678 while (p < buf + size)
9680 /* FIXME: bad alignment assumption. */
9681 Elf_External_Note *xnp = (Elf_External_Note *) p;
9682 Elf_Internal_Note in;
9684 if (offsetof (Elf_External_Note, name) > buf - p + size)
9687 in.type = H_GET_32 (abfd, xnp->type);
9689 in.namesz = H_GET_32 (abfd, xnp->namesz);
9690 in.namedata = xnp->name;
9691 if (in.namesz > buf - in.namedata + size)
9694 in.descsz = H_GET_32 (abfd, xnp->descsz);
9695 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9696 in.descpos = offset + (in.descdata - buf);
9698 && (in.descdata >= buf + size
9699 || in.descsz > buf - in.descdata + size))
9702 switch (bfd_get_format (abfd))
9709 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
9712 const char * string;
9714 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
9718 GROKER_ELEMENT ("", elfcore_grok_note),
9719 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
9720 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
9721 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
9722 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
9724 #undef GROKER_ELEMENT
9727 for (i = ARRAY_SIZE (grokers); i--;)
9729 if (in.namesz >= grokers[i].len
9730 && strncmp (in.namedata, grokers[i].string,
9731 grokers[i].len) == 0)
9733 if (! grokers[i].func (abfd, & in))
9742 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9744 if (! elfobj_grok_gnu_note (abfd, &in))
9747 else if (in.namesz == sizeof "stapsdt"
9748 && strcmp (in.namedata, "stapsdt") == 0)
9750 if (! elfobj_grok_stapsdt_note (abfd, &in))
9756 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9763 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9770 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9773 buf = (char *) bfd_malloc (size + 1);
9777 /* PR 17512: file: ec08f814
9778 0-termintate the buffer so that string searches will not overflow. */
9781 if (bfd_bread (buf, size, abfd) != size
9782 || !elf_parse_notes (abfd, buf, size, offset))
9792 /* Providing external access to the ELF program header table. */
9794 /* Return an upper bound on the number of bytes required to store a
9795 copy of ABFD's program header table entries. Return -1 if an error
9796 occurs; bfd_get_error will return an appropriate code. */
9799 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9801 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9803 bfd_set_error (bfd_error_wrong_format);
9807 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9810 /* Copy ABFD's program header table entries to *PHDRS. The entries
9811 will be stored as an array of Elf_Internal_Phdr structures, as
9812 defined in include/elf/internal.h. To find out how large the
9813 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9815 Return the number of program header table entries read, or -1 if an
9816 error occurs; bfd_get_error will return an appropriate code. */
9819 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9823 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9825 bfd_set_error (bfd_error_wrong_format);
9829 num_phdrs = elf_elfheader (abfd)->e_phnum;
9830 memcpy (phdrs, elf_tdata (abfd)->phdr,
9831 num_phdrs * sizeof (Elf_Internal_Phdr));
9836 enum elf_reloc_type_class
9837 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
9838 const asection *rel_sec ATTRIBUTE_UNUSED,
9839 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9841 return reloc_class_normal;
9844 /* For RELA architectures, return the relocation value for a
9845 relocation against a local symbol. */
9848 _bfd_elf_rela_local_sym (bfd *abfd,
9849 Elf_Internal_Sym *sym,
9851 Elf_Internal_Rela *rel)
9853 asection *sec = *psec;
9856 relocation = (sec->output_section->vma
9857 + sec->output_offset
9859 if ((sec->flags & SEC_MERGE)
9860 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9861 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
9864 _bfd_merged_section_offset (abfd, psec,
9865 elf_section_data (sec)->sec_info,
9866 sym->st_value + rel->r_addend);
9869 /* If we have changed the section, and our original section is
9870 marked with SEC_EXCLUDE, it means that the original
9871 SEC_MERGE section has been completely subsumed in some
9872 other SEC_MERGE section. In this case, we need to leave
9873 some info around for --emit-relocs. */
9874 if ((sec->flags & SEC_EXCLUDE) != 0)
9875 sec->kept_section = *psec;
9878 rel->r_addend -= relocation;
9879 rel->r_addend += sec->output_section->vma + sec->output_offset;
9885 _bfd_elf_rel_local_sym (bfd *abfd,
9886 Elf_Internal_Sym *sym,
9890 asection *sec = *psec;
9892 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
9893 return sym->st_value + addend;
9895 return _bfd_merged_section_offset (abfd, psec,
9896 elf_section_data (sec)->sec_info,
9897 sym->st_value + addend);
9901 _bfd_elf_section_offset (bfd *abfd,
9902 struct bfd_link_info *info,
9906 switch (sec->sec_info_type)
9908 case SEC_INFO_TYPE_STABS:
9909 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9911 case SEC_INFO_TYPE_EH_FRAME:
9912 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9914 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
9916 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9917 bfd_size_type address_size = bed->s->arch_size / 8;
9918 offset = sec->size - offset - address_size;
9924 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9925 reconstruct an ELF file by reading the segments out of remote memory
9926 based on the ELF file header at EHDR_VMA and the ELF program headers it
9927 points to. If not null, *LOADBASEP is filled in with the difference
9928 between the VMAs from which the segments were read, and the VMAs the
9929 file headers (and hence BFD's idea of each section's VMA) put them at.
9931 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9932 remote memory at target address VMA into the local buffer at MYADDR; it
9933 should return zero on success or an `errno' code on failure. TEMPL must
9934 be a BFD for an ELF target with the word size and byte order found in
9935 the remote memory. */
9938 bfd_elf_bfd_from_remote_memory
9943 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
9945 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9946 (templ, ehdr_vma, size, loadbasep, target_read_memory);
9950 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9951 long symcount ATTRIBUTE_UNUSED,
9952 asymbol **syms ATTRIBUTE_UNUSED,
9957 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9960 const char *relplt_name;
9961 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9965 Elf_Internal_Shdr *hdr;
9971 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9974 if (dynsymcount <= 0)
9977 if (!bed->plt_sym_val)
9980 relplt_name = bed->relplt_name;
9981 if (relplt_name == NULL)
9982 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9983 relplt = bfd_get_section_by_name (abfd, relplt_name);
9987 hdr = &elf_section_data (relplt)->this_hdr;
9988 if (hdr->sh_link != elf_dynsymtab (abfd)
9989 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9992 plt = bfd_get_section_by_name (abfd, ".plt");
9996 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9997 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
10000 count = relplt->size / hdr->sh_entsize;
10001 size = count * sizeof (asymbol);
10002 p = relplt->relocation;
10003 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10005 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
10006 if (p->addend != 0)
10009 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
10011 size += sizeof ("+0x") - 1 + 8;
10016 s = *ret = (asymbol *) bfd_malloc (size);
10020 names = (char *) (s + count);
10021 p = relplt->relocation;
10023 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10028 addr = bed->plt_sym_val (i, plt, p);
10029 if (addr == (bfd_vma) -1)
10032 *s = **p->sym_ptr_ptr;
10033 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10034 we are defining a symbol, ensure one of them is set. */
10035 if ((s->flags & BSF_LOCAL) == 0)
10036 s->flags |= BSF_GLOBAL;
10037 s->flags |= BSF_SYNTHETIC;
10039 s->value = addr - plt->vma;
10042 len = strlen ((*p->sym_ptr_ptr)->name);
10043 memcpy (names, (*p->sym_ptr_ptr)->name, len);
10045 if (p->addend != 0)
10049 memcpy (names, "+0x", sizeof ("+0x") - 1);
10050 names += sizeof ("+0x") - 1;
10051 bfd_sprintf_vma (abfd, buf, p->addend);
10052 for (a = buf; *a == '0'; ++a)
10055 memcpy (names, a, len);
10058 memcpy (names, "@plt", sizeof ("@plt"));
10059 names += sizeof ("@plt");
10066 /* It is only used by x86-64 so far. */
10067 asection _bfd_elf_large_com_section
10068 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10069 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10072 _bfd_elf_post_process_headers (bfd * abfd,
10073 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10075 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
10077 i_ehdrp = elf_elfheader (abfd);
10079 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10081 /* To make things simpler for the loader on Linux systems we set the
10082 osabi field to ELFOSABI_GNU if the binary contains symbols of
10083 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10084 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10085 && elf_tdata (abfd)->has_gnu_symbols)
10086 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10090 /* Return TRUE for ELF symbol types that represent functions.
10091 This is the default version of this function, which is sufficient for
10092 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10095 _bfd_elf_is_function_type (unsigned int type)
10097 return (type == STT_FUNC
10098 || type == STT_GNU_IFUNC);
10101 /* If the ELF symbol SYM might be a function in SEC, return the
10102 function size and set *CODE_OFF to the function's entry point,
10103 otherwise return zero. */
10106 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10109 bfd_size_type size;
10111 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10112 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10113 || sym->section != sec)
10116 *code_off = sym->value;
10118 if (!(sym->flags & BSF_SYNTHETIC))
10119 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;