1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2019 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. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bfd_boolean
prep_headers (bfd
*);
55 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
56 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
,
58 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 file_ptr offset
, size_t align
);
61 /* Swap version information in and out. The version information is
62 currently size independent. If that ever changes, this code will
63 need to move into elfcode.h. */
65 /* Swap in a Verdef structure. */
68 _bfd_elf_swap_verdef_in (bfd
*abfd
,
69 const Elf_External_Verdef
*src
,
70 Elf_Internal_Verdef
*dst
)
72 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
73 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
74 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
75 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
76 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
77 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
78 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
81 /* Swap out a Verdef structure. */
84 _bfd_elf_swap_verdef_out (bfd
*abfd
,
85 const Elf_Internal_Verdef
*src
,
86 Elf_External_Verdef
*dst
)
88 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
89 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
90 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
91 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
92 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
93 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
94 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
97 /* Swap in a Verdaux structure. */
100 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
101 const Elf_External_Verdaux
*src
,
102 Elf_Internal_Verdaux
*dst
)
104 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
105 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
108 /* Swap out a Verdaux structure. */
111 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
112 const Elf_Internal_Verdaux
*src
,
113 Elf_External_Verdaux
*dst
)
115 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
116 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
119 /* Swap in a Verneed structure. */
122 _bfd_elf_swap_verneed_in (bfd
*abfd
,
123 const Elf_External_Verneed
*src
,
124 Elf_Internal_Verneed
*dst
)
126 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
127 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
128 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
129 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
130 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
133 /* Swap out a Verneed structure. */
136 _bfd_elf_swap_verneed_out (bfd
*abfd
,
137 const Elf_Internal_Verneed
*src
,
138 Elf_External_Verneed
*dst
)
140 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
141 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
142 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
143 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
144 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
147 /* Swap in a Vernaux structure. */
150 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
151 const Elf_External_Vernaux
*src
,
152 Elf_Internal_Vernaux
*dst
)
154 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
155 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
156 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
157 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
158 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
161 /* Swap out a Vernaux structure. */
164 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
165 const Elf_Internal_Vernaux
*src
,
166 Elf_External_Vernaux
*dst
)
168 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
169 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
170 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
171 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
172 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
175 /* Swap in a Versym structure. */
178 _bfd_elf_swap_versym_in (bfd
*abfd
,
179 const Elf_External_Versym
*src
,
180 Elf_Internal_Versym
*dst
)
182 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
185 /* Swap out a Versym structure. */
188 _bfd_elf_swap_versym_out (bfd
*abfd
,
189 const Elf_Internal_Versym
*src
,
190 Elf_External_Versym
*dst
)
192 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
195 /* Standard ELF hash function. Do not change this function; you will
196 cause invalid hash tables to be generated. */
199 bfd_elf_hash (const char *namearg
)
201 const unsigned char *name
= (const unsigned char *) namearg
;
206 while ((ch
= *name
++) != '\0')
209 if ((g
= (h
& 0xf0000000)) != 0)
212 /* The ELF ABI says `h &= ~g', but this is equivalent in
213 this case and on some machines one insn instead of two. */
217 return h
& 0xffffffff;
220 /* DT_GNU_HASH hash function. Do not change this function; you will
221 cause invalid hash tables to be generated. */
224 bfd_elf_gnu_hash (const char *namearg
)
226 const unsigned char *name
= (const unsigned char *) namearg
;
227 unsigned long h
= 5381;
230 while ((ch
= *name
++) != '\0')
231 h
= (h
<< 5) + h
+ ch
;
232 return h
& 0xffffffff;
235 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
236 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
238 bfd_elf_allocate_object (bfd
*abfd
,
240 enum elf_target_id object_id
)
242 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
243 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
244 if (abfd
->tdata
.any
== NULL
)
247 elf_object_id (abfd
) = object_id
;
248 if (abfd
->direction
!= read_direction
)
250 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
253 elf_tdata (abfd
)->o
= o
;
254 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
261 bfd_elf_make_object (bfd
*abfd
)
263 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
264 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
269 bfd_elf_mkcorefile (bfd
*abfd
)
271 /* I think this can be done just like an object file. */
272 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
274 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
275 return elf_tdata (abfd
)->core
!= NULL
;
279 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
281 Elf_Internal_Shdr
**i_shdrp
;
282 bfd_byte
*shstrtab
= NULL
;
284 bfd_size_type shstrtabsize
;
286 i_shdrp
= elf_elfsections (abfd
);
288 || shindex
>= elf_numsections (abfd
)
289 || i_shdrp
[shindex
] == 0)
292 shstrtab
= i_shdrp
[shindex
]->contents
;
293 if (shstrtab
== NULL
)
295 /* No cached one, attempt to read, and cache what we read. */
296 offset
= i_shdrp
[shindex
]->sh_offset
;
297 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
299 /* Allocate and clear an extra byte at the end, to prevent crashes
300 in case the string table is not terminated. */
301 if (shstrtabsize
+ 1 <= 1
302 || shstrtabsize
> bfd_get_file_size (abfd
)
303 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
304 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
306 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
308 if (bfd_get_error () != bfd_error_system_call
)
309 bfd_set_error (bfd_error_file_truncated
);
310 bfd_release (abfd
, shstrtab
);
312 /* Once we've failed to read it, make sure we don't keep
313 trying. Otherwise, we'll keep allocating space for
314 the string table over and over. */
315 i_shdrp
[shindex
]->sh_size
= 0;
318 shstrtab
[shstrtabsize
] = '\0';
319 i_shdrp
[shindex
]->contents
= shstrtab
;
321 return (char *) shstrtab
;
325 bfd_elf_string_from_elf_section (bfd
*abfd
,
326 unsigned int shindex
,
327 unsigned int strindex
)
329 Elf_Internal_Shdr
*hdr
;
334 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
337 hdr
= elf_elfsections (abfd
)[shindex
];
339 if (hdr
->contents
== NULL
)
341 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
343 /* PR 17512: file: f057ec89. */
344 /* xgettext:c-format */
345 _bfd_error_handler (_("%pB: attempt to load strings from"
346 " a non-string section (number %d)"),
351 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
356 /* PR 24273: The string section's contents may have already
357 been loaded elsewhere, eg because a corrupt file has the
358 string section index in the ELF header pointing at a group
359 section. So be paranoid, and test that the last byte of
360 the section is zero. */
361 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
365 if (strindex
>= hdr
->sh_size
)
367 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
369 /* xgettext:c-format */
370 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
371 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
372 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
374 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
378 return ((char *) hdr
->contents
) + strindex
;
381 /* Read and convert symbols to internal format.
382 SYMCOUNT specifies the number of symbols to read, starting from
383 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
384 are non-NULL, they are used to store the internal symbols, external
385 symbols, and symbol section index extensions, respectively.
386 Returns a pointer to the internal symbol buffer (malloced if necessary)
387 or NULL if there were no symbols or some kind of problem. */
390 bfd_elf_get_elf_syms (bfd
*ibfd
,
391 Elf_Internal_Shdr
*symtab_hdr
,
394 Elf_Internal_Sym
*intsym_buf
,
396 Elf_External_Sym_Shndx
*extshndx_buf
)
398 Elf_Internal_Shdr
*shndx_hdr
;
400 const bfd_byte
*esym
;
401 Elf_External_Sym_Shndx
*alloc_extshndx
;
402 Elf_External_Sym_Shndx
*shndx
;
403 Elf_Internal_Sym
*alloc_intsym
;
404 Elf_Internal_Sym
*isym
;
405 Elf_Internal_Sym
*isymend
;
406 const struct elf_backend_data
*bed
;
411 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
417 /* Normal syms might have section extension entries. */
419 if (elf_symtab_shndx_list (ibfd
) != NULL
)
421 elf_section_list
* entry
;
422 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
424 /* Find an index section that is linked to this symtab section. */
425 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
428 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
431 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
433 shndx_hdr
= & entry
->hdr
;
438 if (shndx_hdr
== NULL
)
440 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
441 /* Not really accurate, but this was how the old code used to work. */
442 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
443 /* Otherwise we do nothing. The assumption is that
444 the index table will not be needed. */
448 /* Read the symbols. */
450 alloc_extshndx
= NULL
;
452 bed
= get_elf_backend_data (ibfd
);
453 extsym_size
= bed
->s
->sizeof_sym
;
454 amt
= (bfd_size_type
) symcount
* extsym_size
;
455 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
456 if (extsym_buf
== NULL
)
458 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
459 extsym_buf
= alloc_ext
;
461 if (extsym_buf
== NULL
462 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
463 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
469 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
473 amt
= (bfd_size_type
) symcount
* sizeof (Elf_External_Sym_Shndx
);
474 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
475 if (extshndx_buf
== NULL
)
477 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
478 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
479 extshndx_buf
= alloc_extshndx
;
481 if (extshndx_buf
== NULL
482 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
483 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
490 if (intsym_buf
== NULL
)
492 alloc_intsym
= (Elf_Internal_Sym
*)
493 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
494 intsym_buf
= alloc_intsym
;
495 if (intsym_buf
== NULL
)
499 /* Convert the symbols to internal form. */
500 isymend
= intsym_buf
+ symcount
;
501 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
502 shndx
= extshndx_buf
;
504 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
505 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
507 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
508 /* xgettext:c-format */
509 _bfd_error_handler (_("%pB symbol number %lu references"
510 " nonexistent SHT_SYMTAB_SHNDX section"),
511 ibfd
, (unsigned long) symoffset
);
512 if (alloc_intsym
!= NULL
)
519 if (alloc_ext
!= NULL
)
521 if (alloc_extshndx
!= NULL
)
522 free (alloc_extshndx
);
527 /* Look up a symbol name. */
529 bfd_elf_sym_name (bfd
*abfd
,
530 Elf_Internal_Shdr
*symtab_hdr
,
531 Elf_Internal_Sym
*isym
,
535 unsigned int iname
= isym
->st_name
;
536 unsigned int shindex
= symtab_hdr
->sh_link
;
538 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
539 /* Check for a bogus st_shndx to avoid crashing. */
540 && isym
->st_shndx
< elf_numsections (abfd
))
542 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
543 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
546 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
549 else if (sym_sec
&& *name
== '\0')
550 name
= bfd_section_name (sym_sec
);
555 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
556 sections. The first element is the flags, the rest are section
559 typedef union elf_internal_group
{
560 Elf_Internal_Shdr
*shdr
;
562 } Elf_Internal_Group
;
564 /* Return the name of the group signature symbol. Why isn't the
565 signature just a string? */
568 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
570 Elf_Internal_Shdr
*hdr
;
571 unsigned char esym
[sizeof (Elf64_External_Sym
)];
572 Elf_External_Sym_Shndx eshndx
;
573 Elf_Internal_Sym isym
;
575 /* First we need to ensure the symbol table is available. Make sure
576 that it is a symbol table section. */
577 if (ghdr
->sh_link
>= elf_numsections (abfd
))
579 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
580 if (hdr
->sh_type
!= SHT_SYMTAB
581 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
584 /* Go read the symbol. */
585 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
586 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
587 &isym
, esym
, &eshndx
) == NULL
)
590 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
593 /* Set next_in_group list pointer, and group name for NEWSECT. */
596 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
598 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
600 /* If num_group is zero, read in all SHT_GROUP sections. The count
601 is set to -1 if there are no SHT_GROUP sections. */
604 unsigned int i
, shnum
;
606 /* First count the number of groups. If we have a SHT_GROUP
607 section with just a flag word (ie. sh_size is 4), ignore it. */
608 shnum
= elf_numsections (abfd
);
611 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
612 ( (shdr)->sh_type == SHT_GROUP \
613 && (shdr)->sh_size >= minsize \
614 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
615 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
617 for (i
= 0; i
< shnum
; i
++)
619 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
621 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
627 num_group
= (unsigned) -1;
628 elf_tdata (abfd
)->num_group
= num_group
;
629 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
633 /* We keep a list of elf section headers for group sections,
634 so we can find them quickly. */
637 elf_tdata (abfd
)->num_group
= num_group
;
638 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
639 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
640 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
642 memset (elf_tdata (abfd
)->group_sect_ptr
, 0,
643 num_group
* sizeof (Elf_Internal_Shdr
*));
646 for (i
= 0; i
< shnum
; i
++)
648 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
650 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
653 Elf_Internal_Group
*dest
;
655 /* Make sure the group section has a BFD section
657 if (!bfd_section_from_shdr (abfd
, i
))
660 /* Add to list of sections. */
661 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
664 /* Read the raw contents. */
665 BFD_ASSERT (sizeof (*dest
) >= 4);
666 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
667 shdr
->contents
= (unsigned char *)
668 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
669 /* PR binutils/4110: Handle corrupt group headers. */
670 if (shdr
->contents
== NULL
)
673 /* xgettext:c-format */
674 (_("%pB: corrupt size field in group section"
675 " header: %#" PRIx64
),
676 abfd
, (uint64_t) shdr
->sh_size
);
677 bfd_set_error (bfd_error_bad_value
);
682 memset (shdr
->contents
, 0, amt
);
684 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
685 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
689 /* xgettext:c-format */
690 (_("%pB: invalid size field in group section"
691 " header: %#" PRIx64
""),
692 abfd
, (uint64_t) shdr
->sh_size
);
693 bfd_set_error (bfd_error_bad_value
);
695 /* PR 17510: If the group contents are even
696 partially corrupt, do not allow any of the
697 contents to be used. */
698 memset (shdr
->contents
, 0, amt
);
702 /* Translate raw contents, a flag word followed by an
703 array of elf section indices all in target byte order,
704 to the flag word followed by an array of elf section
706 src
= shdr
->contents
+ shdr
->sh_size
;
707 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
715 idx
= H_GET_32 (abfd
, src
);
716 if (src
== shdr
->contents
)
719 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
720 shdr
->bfd_section
->flags
721 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
726 dest
->shdr
= elf_elfsections (abfd
)[idx
];
727 /* PR binutils/23199: All sections in a
728 section group should be marked with
729 SHF_GROUP. But some tools generate
730 broken objects without SHF_GROUP. Fix
732 dest
->shdr
->sh_flags
|= SHF_GROUP
;
735 || dest
->shdr
->sh_type
== SHT_GROUP
)
738 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
746 /* PR 17510: Corrupt binaries might contain invalid groups. */
747 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
749 elf_tdata (abfd
)->num_group
= num_group
;
751 /* If all groups are invalid then fail. */
754 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
755 elf_tdata (abfd
)->num_group
= num_group
= -1;
757 (_("%pB: no valid group sections found"), abfd
);
758 bfd_set_error (bfd_error_bad_value
);
764 if (num_group
!= (unsigned) -1)
766 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
769 for (j
= 0; j
< num_group
; j
++)
771 /* Begin search from previous found group. */
772 unsigned i
= (j
+ search_offset
) % num_group
;
774 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
775 Elf_Internal_Group
*idx
;
781 idx
= (Elf_Internal_Group
*) shdr
->contents
;
782 if (idx
== NULL
|| shdr
->sh_size
< 4)
784 /* See PR 21957 for a reproducer. */
785 /* xgettext:c-format */
786 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
787 abfd
, shdr
->bfd_section
);
788 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
789 bfd_set_error (bfd_error_bad_value
);
792 n_elt
= shdr
->sh_size
/ 4;
794 /* Look through this group's sections to see if current
795 section is a member. */
797 if ((++idx
)->shdr
== hdr
)
801 /* We are a member of this group. Go looking through
802 other members to see if any others are linked via
804 idx
= (Elf_Internal_Group
*) shdr
->contents
;
805 n_elt
= shdr
->sh_size
/ 4;
807 if ((++idx
)->shdr
!= NULL
808 && (s
= idx
->shdr
->bfd_section
) != NULL
809 && elf_next_in_group (s
) != NULL
)
813 /* Snarf the group name from other member, and
814 insert current section in circular list. */
815 elf_group_name (newsect
) = elf_group_name (s
);
816 elf_next_in_group (newsect
) = elf_next_in_group (s
);
817 elf_next_in_group (s
) = newsect
;
823 gname
= group_signature (abfd
, shdr
);
826 elf_group_name (newsect
) = gname
;
828 /* Start a circular list with one element. */
829 elf_next_in_group (newsect
) = newsect
;
832 /* If the group section has been created, point to the
834 if (shdr
->bfd_section
!= NULL
)
835 elf_next_in_group (shdr
->bfd_section
) = newsect
;
837 elf_tdata (abfd
)->group_search_offset
= i
;
844 if (elf_group_name (newsect
) == NULL
)
846 /* xgettext:c-format */
847 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
855 _bfd_elf_setup_sections (bfd
*abfd
)
858 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
859 bfd_boolean result
= TRUE
;
862 /* Process SHF_LINK_ORDER. */
863 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
865 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
866 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
868 unsigned int elfsec
= this_hdr
->sh_link
;
869 /* FIXME: The old Intel compiler and old strip/objcopy may
870 not set the sh_link or sh_info fields. Hence we could
871 get the situation where elfsec is 0. */
874 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
875 if (bed
->link_order_error_handler
)
876 bed
->link_order_error_handler
877 /* xgettext:c-format */
878 (_("%pB: warning: sh_link not set for section `%pA'"),
883 asection
*linksec
= NULL
;
885 if (elfsec
< elf_numsections (abfd
))
887 this_hdr
= elf_elfsections (abfd
)[elfsec
];
888 linksec
= this_hdr
->bfd_section
;
892 Some strip/objcopy may leave an incorrect value in
893 sh_link. We don't want to proceed. */
897 /* xgettext:c-format */
898 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
899 s
->owner
, elfsec
, s
);
903 elf_linked_to_section (s
) = linksec
;
906 else if (this_hdr
->sh_type
== SHT_GROUP
907 && elf_next_in_group (s
) == NULL
)
910 /* xgettext:c-format */
911 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
912 abfd
, elf_section_data (s
)->this_idx
);
917 /* Process section groups. */
918 if (num_group
== (unsigned) -1)
921 for (i
= 0; i
< num_group
; i
++)
923 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
924 Elf_Internal_Group
*idx
;
927 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
928 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
931 /* xgettext:c-format */
932 (_("%pB: section group entry number %u is corrupt"),
938 idx
= (Elf_Internal_Group
*) shdr
->contents
;
939 n_elt
= shdr
->sh_size
/ 4;
945 if (idx
->shdr
== NULL
)
947 else if (idx
->shdr
->bfd_section
)
948 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
949 else if (idx
->shdr
->sh_type
!= SHT_RELA
950 && idx
->shdr
->sh_type
!= SHT_REL
)
952 /* There are some unknown sections in the group. */
954 /* xgettext:c-format */
955 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
958 bfd_elf_string_from_elf_section (abfd
,
959 (elf_elfheader (abfd
)
972 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
974 return elf_next_in_group (sec
) != NULL
;
978 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
980 if (elf_sec_group (sec
) != NULL
)
981 return elf_group_name (sec
);
986 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
988 unsigned int len
= strlen (name
);
989 char *new_name
= bfd_alloc (abfd
, len
+ 2);
990 if (new_name
== NULL
)
994 memcpy (new_name
+ 2, name
+ 1, len
);
999 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
1001 unsigned int len
= strlen (name
);
1002 char *new_name
= bfd_alloc (abfd
, len
);
1003 if (new_name
== NULL
)
1006 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1010 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
1014 int16_t major_version
;
1015 int16_t minor_version
;
1016 unsigned char slim_object
;
1018 /* Flags is a private field that is not defined publicly. */
1022 /* Make a BFD section from an ELF section. We store a pointer to the
1023 BFD section in the bfd_section field of the header. */
1026 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1027 Elf_Internal_Shdr
*hdr
,
1033 const struct elf_backend_data
*bed
;
1035 if (hdr
->bfd_section
!= NULL
)
1038 newsect
= bfd_make_section_anyway (abfd
, name
);
1039 if (newsect
== NULL
)
1042 hdr
->bfd_section
= newsect
;
1043 elf_section_data (newsect
)->this_hdr
= *hdr
;
1044 elf_section_data (newsect
)->this_idx
= shindex
;
1046 /* Always use the real type/flags. */
1047 elf_section_type (newsect
) = hdr
->sh_type
;
1048 elf_section_flags (newsect
) = hdr
->sh_flags
;
1050 newsect
->filepos
= hdr
->sh_offset
;
1052 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
)
1053 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1054 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
)))
1057 flags
= SEC_NO_FLAGS
;
1058 if (hdr
->sh_type
!= SHT_NOBITS
)
1059 flags
|= SEC_HAS_CONTENTS
;
1060 if (hdr
->sh_type
== SHT_GROUP
)
1062 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1065 if (hdr
->sh_type
!= SHT_NOBITS
)
1068 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1069 flags
|= SEC_READONLY
;
1070 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1072 else if ((flags
& SEC_LOAD
) != 0)
1074 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1077 newsect
->entsize
= hdr
->sh_entsize
;
1079 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1080 flags
|= SEC_STRINGS
;
1081 if (hdr
->sh_flags
& SHF_GROUP
)
1082 if (!setup_group (abfd
, hdr
, newsect
))
1084 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1085 flags
|= SEC_THREAD_LOCAL
;
1086 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1087 flags
|= SEC_EXCLUDE
;
1089 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1091 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1092 but binutils as of 2019-07-23 did not set the EI_OSABI header
1096 case ELFOSABI_FREEBSD
:
1097 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1098 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1102 if ((flags
& SEC_ALLOC
) == 0)
1104 /* The debugging sections appear to be recognized only by name,
1105 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1106 if (name
[0] == '.')
1111 p
= ".debug", n
= 6;
1112 else if (name
[1] == 'g' && name
[2] == 'n')
1113 p
= ".gnu.linkonce.wi.", n
= 17;
1114 else if (name
[1] == 'g' && name
[2] == 'd')
1115 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1116 else if (name
[1] == 'l')
1118 else if (name
[1] == 's')
1120 else if (name
[1] == 'z')
1121 p
= ".zdebug", n
= 7;
1124 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1125 flags
|= SEC_DEBUGGING
;
1129 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1130 only link a single copy of the section. This is used to support
1131 g++. g++ will emit each template expansion in its own section.
1132 The symbols will be defined as weak, so that multiple definitions
1133 are permitted. The GNU linker extension is to actually discard
1134 all but one of the sections. */
1135 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1136 && elf_next_in_group (newsect
) == NULL
)
1137 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1139 bed
= get_elf_backend_data (abfd
);
1140 if (bed
->elf_backend_section_flags
)
1141 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1144 if (!bfd_set_section_flags (newsect
, flags
))
1147 /* We do not parse the PT_NOTE segments as we are interested even in the
1148 separate debug info files which may have the segments offsets corrupted.
1149 PT_NOTEs from the core files are currently not parsed using BFD. */
1150 if (hdr
->sh_type
== SHT_NOTE
)
1154 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1157 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1158 hdr
->sh_offset
, hdr
->sh_addralign
);
1162 if ((flags
& SEC_ALLOC
) != 0)
1164 Elf_Internal_Phdr
*phdr
;
1165 unsigned int i
, nload
;
1167 /* Some ELF linkers produce binaries with all the program header
1168 p_paddr fields zero. If we have such a binary with more than
1169 one PT_LOAD header, then leave the section lma equal to vma
1170 so that we don't create sections with overlapping lma. */
1171 phdr
= elf_tdata (abfd
)->phdr
;
1172 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1173 if (phdr
->p_paddr
!= 0)
1175 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1177 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1180 phdr
= elf_tdata (abfd
)->phdr
;
1181 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1183 if (((phdr
->p_type
== PT_LOAD
1184 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1185 || phdr
->p_type
== PT_TLS
)
1186 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1188 if ((flags
& SEC_LOAD
) == 0)
1189 newsect
->lma
= (phdr
->p_paddr
1190 + hdr
->sh_addr
- phdr
->p_vaddr
);
1192 /* We used to use the same adjustment for SEC_LOAD
1193 sections, but that doesn't work if the segment
1194 is packed with code from multiple VMAs.
1195 Instead we calculate the section LMA based on
1196 the segment LMA. It is assumed that the
1197 segment will contain sections with contiguous
1198 LMAs, even if the VMAs are not. */
1199 newsect
->lma
= (phdr
->p_paddr
1200 + hdr
->sh_offset
- phdr
->p_offset
);
1202 /* With contiguous segments, we can't tell from file
1203 offsets whether a section with zero size should
1204 be placed at the end of one segment or the
1205 beginning of the next. Decide based on vaddr. */
1206 if (hdr
->sh_addr
>= phdr
->p_vaddr
1207 && (hdr
->sh_addr
+ hdr
->sh_size
1208 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1214 /* Compress/decompress DWARF debug sections with names: .debug_* and
1215 .zdebug_*, after the section flags is set. */
1216 if ((flags
& SEC_DEBUGGING
)
1217 && ((name
[1] == 'd' && name
[6] == '_')
1218 || (name
[1] == 'z' && name
[7] == '_')))
1220 enum { nothing
, compress
, decompress
} action
= nothing
;
1221 int compression_header_size
;
1222 bfd_size_type uncompressed_size
;
1223 unsigned int uncompressed_align_power
;
1224 bfd_boolean compressed
1225 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1226 &compression_header_size
,
1228 &uncompressed_align_power
);
1231 /* Compressed section. Check if we should decompress. */
1232 if ((abfd
->flags
& BFD_DECOMPRESS
))
1233 action
= decompress
;
1236 /* Compress the uncompressed section or convert from/to .zdebug*
1237 section. Check if we should compress. */
1238 if (action
== nothing
)
1240 if (newsect
->size
!= 0
1241 && (abfd
->flags
& BFD_COMPRESS
)
1242 && compression_header_size
>= 0
1243 && uncompressed_size
> 0
1245 || ((compression_header_size
> 0)
1246 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1252 if (action
== compress
)
1254 if (!bfd_init_section_compress_status (abfd
, newsect
))
1257 /* xgettext:c-format */
1258 (_("%pB: unable to initialize compress status for section %s"),
1265 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1268 /* xgettext:c-format */
1269 (_("%pB: unable to initialize decompress status for section %s"),
1275 if (abfd
->is_linker_input
)
1278 && (action
== decompress
1279 || (action
== compress
1280 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1282 /* Convert section name from .zdebug_* to .debug_* so
1283 that linker will consider this section as a debug
1285 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1286 if (new_name
== NULL
)
1288 bfd_rename_section (newsect
, new_name
);
1292 /* For objdump, don't rename the section. For objcopy, delay
1293 section rename to elf_fake_sections. */
1294 newsect
->flags
|= SEC_ELF_RENAME
;
1297 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1299 const char *lto_section_name
= ".gnu.lto_.lto.";
1300 if (strncmp (name
, lto_section_name
, strlen (lto_section_name
)) == 0)
1302 struct lto_section lsection
;
1303 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1304 sizeof (struct lto_section
)))
1305 abfd
->lto_slim_object
= lsection
.slim_object
;
1311 const char *const bfd_elf_section_type_names
[] =
1313 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1314 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1315 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1318 /* ELF relocs are against symbols. If we are producing relocatable
1319 output, and the reloc is against an external symbol, and nothing
1320 has given us any additional addend, the resulting reloc will also
1321 be against the same symbol. In such a case, we don't want to
1322 change anything about the way the reloc is handled, since it will
1323 all be done at final link time. Rather than put special case code
1324 into bfd_perform_relocation, all the reloc types use this howto
1325 function. It just short circuits the reloc if producing
1326 relocatable output against an external symbol. */
1328 bfd_reloc_status_type
1329 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1330 arelent
*reloc_entry
,
1332 void *data ATTRIBUTE_UNUSED
,
1333 asection
*input_section
,
1335 char **error_message ATTRIBUTE_UNUSED
)
1337 if (output_bfd
!= NULL
1338 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1339 && (! reloc_entry
->howto
->partial_inplace
1340 || reloc_entry
->addend
== 0))
1342 reloc_entry
->address
+= input_section
->output_offset
;
1343 return bfd_reloc_ok
;
1346 return bfd_reloc_continue
;
1349 /* Returns TRUE if section A matches section B.
1350 Names, addresses and links may be different, but everything else
1351 should be the same. */
1354 section_match (const Elf_Internal_Shdr
* a
,
1355 const Elf_Internal_Shdr
* b
)
1357 if (a
->sh_type
!= b
->sh_type
1358 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1359 || a
->sh_addralign
!= b
->sh_addralign
1360 || a
->sh_entsize
!= b
->sh_entsize
)
1362 if (a
->sh_type
== SHT_SYMTAB
1363 || a
->sh_type
== SHT_STRTAB
)
1365 return a
->sh_size
== b
->sh_size
;
1368 /* Find a section in OBFD that has the same characteristics
1369 as IHEADER. Return the index of this section or SHN_UNDEF if
1370 none can be found. Check's section HINT first, as this is likely
1371 to be the correct section. */
1374 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1375 const unsigned int hint
)
1377 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1380 BFD_ASSERT (iheader
!= NULL
);
1382 /* See PR 20922 for a reproducer of the NULL test. */
1383 if (hint
< elf_numsections (obfd
)
1384 && oheaders
[hint
] != NULL
1385 && section_match (oheaders
[hint
], iheader
))
1388 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1390 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1392 if (oheader
== NULL
)
1394 if (section_match (oheader
, iheader
))
1395 /* FIXME: Do we care if there is a potential for
1396 multiple matches ? */
1403 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1404 Processor specific section, based upon a matching input section.
1405 Returns TRUE upon success, FALSE otherwise. */
1408 copy_special_section_fields (const bfd
*ibfd
,
1410 const Elf_Internal_Shdr
*iheader
,
1411 Elf_Internal_Shdr
*oheader
,
1412 const unsigned int secnum
)
1414 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1415 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1416 bfd_boolean changed
= FALSE
;
1417 unsigned int sh_link
;
1419 if (oheader
->sh_type
== SHT_NOBITS
)
1421 /* This is a feature for objcopy --only-keep-debug:
1422 When a section's type is changed to NOBITS, we preserve
1423 the sh_link and sh_info fields so that they can be
1424 matched up with the original.
1426 Note: Strictly speaking these assignments are wrong.
1427 The sh_link and sh_info fields should point to the
1428 relevent sections in the output BFD, which may not be in
1429 the same location as they were in the input BFD. But
1430 the whole point of this action is to preserve the
1431 original values of the sh_link and sh_info fields, so
1432 that they can be matched up with the section headers in
1433 the original file. So strictly speaking we may be
1434 creating an invalid ELF file, but it is only for a file
1435 that just contains debug info and only for sections
1436 without any contents. */
1437 if (oheader
->sh_link
== 0)
1438 oheader
->sh_link
= iheader
->sh_link
;
1439 if (oheader
->sh_info
== 0)
1440 oheader
->sh_info
= iheader
->sh_info
;
1444 /* Allow the target a chance to decide how these fields should be set. */
1445 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1446 && bed
->elf_backend_copy_special_section_fields
1447 (ibfd
, obfd
, iheader
, oheader
))
1450 /* We have an iheader which might match oheader, and which has non-zero
1451 sh_info and/or sh_link fields. Attempt to follow those links and find
1452 the section in the output bfd which corresponds to the linked section
1453 in the input bfd. */
1454 if (iheader
->sh_link
!= SHN_UNDEF
)
1456 /* See PR 20931 for a reproducer. */
1457 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1460 /* xgettext:c-format */
1461 (_("%pB: invalid sh_link field (%d) in section number %d"),
1462 ibfd
, iheader
->sh_link
, secnum
);
1466 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1467 if (sh_link
!= SHN_UNDEF
)
1469 oheader
->sh_link
= sh_link
;
1473 /* FIXME: Should we install iheader->sh_link
1474 if we could not find a match ? */
1476 /* xgettext:c-format */
1477 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1480 if (iheader
->sh_info
)
1482 /* The sh_info field can hold arbitrary information, but if the
1483 SHF_LINK_INFO flag is set then it should be interpreted as a
1485 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1487 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1489 if (sh_link
!= SHN_UNDEF
)
1490 oheader
->sh_flags
|= SHF_INFO_LINK
;
1493 /* No idea what it means - just copy it. */
1494 sh_link
= iheader
->sh_info
;
1496 if (sh_link
!= SHN_UNDEF
)
1498 oheader
->sh_info
= sh_link
;
1503 /* xgettext:c-format */
1504 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1510 /* Copy the program header and other data from one object module to
1514 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1516 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1517 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1518 const struct elf_backend_data
*bed
;
1521 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1522 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1525 if (!elf_flags_init (obfd
))
1527 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1528 elf_flags_init (obfd
) = TRUE
;
1531 elf_gp (obfd
) = elf_gp (ibfd
);
1533 /* Also copy the EI_OSABI field. */
1534 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1535 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1537 /* If set, copy the EI_ABIVERSION field. */
1538 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1539 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1540 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1542 /* Copy object attributes. */
1543 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1545 if (iheaders
== NULL
|| oheaders
== NULL
)
1548 bed
= get_elf_backend_data (obfd
);
1550 /* Possibly copy other fields in the section header. */
1551 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1554 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1556 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1557 because of a special case need for generating separate debug info
1558 files. See below for more details. */
1560 || (oheader
->sh_type
!= SHT_NOBITS
1561 && oheader
->sh_type
< SHT_LOOS
))
1564 /* Ignore empty sections, and sections whose
1565 fields have already been initialised. */
1566 if (oheader
->sh_size
== 0
1567 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1570 /* Scan for the matching section in the input bfd.
1571 First we try for a direct mapping between the input and output sections. */
1572 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1574 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1576 if (iheader
== NULL
)
1579 if (oheader
->bfd_section
!= NULL
1580 && iheader
->bfd_section
!= NULL
1581 && iheader
->bfd_section
->output_section
!= NULL
1582 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1584 /* We have found a connection from the input section to the
1585 output section. Attempt to copy the header fields. If
1586 this fails then do not try any further sections - there
1587 should only be a one-to-one mapping between input and output. */
1588 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1589 j
= elf_numsections (ibfd
);
1594 if (j
< elf_numsections (ibfd
))
1597 /* That failed. So try to deduce the corresponding input section.
1598 Unfortunately we cannot compare names as the output string table
1599 is empty, so instead we check size, address and type. */
1600 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1602 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1604 if (iheader
== NULL
)
1607 /* Try matching fields in the input section's header.
1608 Since --only-keep-debug turns all non-debug sections into
1609 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1611 if ((oheader
->sh_type
== SHT_NOBITS
1612 || iheader
->sh_type
== oheader
->sh_type
)
1613 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1614 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1615 && iheader
->sh_addralign
== oheader
->sh_addralign
1616 && iheader
->sh_entsize
== oheader
->sh_entsize
1617 && iheader
->sh_size
== oheader
->sh_size
1618 && iheader
->sh_addr
== oheader
->sh_addr
1619 && (iheader
->sh_info
!= oheader
->sh_info
1620 || iheader
->sh_link
!= oheader
->sh_link
))
1622 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1627 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1629 /* Final attempt. Call the backend copy function
1630 with a NULL input section. */
1631 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1632 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1640 get_segment_type (unsigned int p_type
)
1645 case PT_NULL
: pt
= "NULL"; break;
1646 case PT_LOAD
: pt
= "LOAD"; break;
1647 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1648 case PT_INTERP
: pt
= "INTERP"; break;
1649 case PT_NOTE
: pt
= "NOTE"; break;
1650 case PT_SHLIB
: pt
= "SHLIB"; break;
1651 case PT_PHDR
: pt
= "PHDR"; break;
1652 case PT_TLS
: pt
= "TLS"; break;
1653 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1654 case PT_GNU_STACK
: pt
= "STACK"; break;
1655 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1656 default: pt
= NULL
; break;
1661 /* Print out the program headers. */
1664 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1666 FILE *f
= (FILE *) farg
;
1667 Elf_Internal_Phdr
*p
;
1669 bfd_byte
*dynbuf
= NULL
;
1671 p
= elf_tdata (abfd
)->phdr
;
1676 fprintf (f
, _("\nProgram Header:\n"));
1677 c
= elf_elfheader (abfd
)->e_phnum
;
1678 for (i
= 0; i
< c
; i
++, p
++)
1680 const char *pt
= get_segment_type (p
->p_type
);
1685 sprintf (buf
, "0x%lx", p
->p_type
);
1688 fprintf (f
, "%8s off 0x", pt
);
1689 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1690 fprintf (f
, " vaddr 0x");
1691 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1692 fprintf (f
, " paddr 0x");
1693 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1694 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1695 fprintf (f
, " filesz 0x");
1696 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1697 fprintf (f
, " memsz 0x");
1698 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1699 fprintf (f
, " flags %c%c%c",
1700 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1701 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1702 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1703 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1704 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1709 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1712 unsigned int elfsec
;
1713 unsigned long shlink
;
1714 bfd_byte
*extdyn
, *extdynend
;
1716 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1718 fprintf (f
, _("\nDynamic Section:\n"));
1720 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1723 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1724 if (elfsec
== SHN_BAD
)
1726 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1728 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1729 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1732 /* PR 17512: file: 6f427532. */
1733 if (s
->size
< extdynsize
)
1735 extdynend
= extdyn
+ s
->size
;
1736 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1738 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1740 Elf_Internal_Dyn dyn
;
1741 const char *name
= "";
1743 bfd_boolean stringp
;
1744 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1746 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1748 if (dyn
.d_tag
== DT_NULL
)
1755 if (bed
->elf_backend_get_target_dtag
)
1756 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1758 if (!strcmp (name
, ""))
1760 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1765 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1766 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1767 case DT_PLTGOT
: name
= "PLTGOT"; break;
1768 case DT_HASH
: name
= "HASH"; break;
1769 case DT_STRTAB
: name
= "STRTAB"; break;
1770 case DT_SYMTAB
: name
= "SYMTAB"; break;
1771 case DT_RELA
: name
= "RELA"; break;
1772 case DT_RELASZ
: name
= "RELASZ"; break;
1773 case DT_RELAENT
: name
= "RELAENT"; break;
1774 case DT_STRSZ
: name
= "STRSZ"; break;
1775 case DT_SYMENT
: name
= "SYMENT"; break;
1776 case DT_INIT
: name
= "INIT"; break;
1777 case DT_FINI
: name
= "FINI"; break;
1778 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1779 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1780 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1781 case DT_REL
: name
= "REL"; break;
1782 case DT_RELSZ
: name
= "RELSZ"; break;
1783 case DT_RELENT
: name
= "RELENT"; break;
1784 case DT_PLTREL
: name
= "PLTREL"; break;
1785 case DT_DEBUG
: name
= "DEBUG"; break;
1786 case DT_TEXTREL
: name
= "TEXTREL"; break;
1787 case DT_JMPREL
: name
= "JMPREL"; break;
1788 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1789 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1790 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1791 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1792 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1793 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1794 case DT_FLAGS
: name
= "FLAGS"; break;
1795 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1796 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1797 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1798 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1799 case DT_MOVEENT
: name
= "MOVEENT"; break;
1800 case DT_MOVESZ
: name
= "MOVESZ"; break;
1801 case DT_FEATURE
: name
= "FEATURE"; break;
1802 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1803 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1804 case DT_SYMINENT
: name
= "SYMINENT"; break;
1805 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1806 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1807 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1808 case DT_PLTPAD
: name
= "PLTPAD"; break;
1809 case DT_MOVETAB
: name
= "MOVETAB"; break;
1810 case DT_SYMINFO
: name
= "SYMINFO"; break;
1811 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1812 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1813 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1814 case DT_VERSYM
: name
= "VERSYM"; break;
1815 case DT_VERDEF
: name
= "VERDEF"; break;
1816 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1817 case DT_VERNEED
: name
= "VERNEED"; break;
1818 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1819 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1820 case DT_USED
: name
= "USED"; break;
1821 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1822 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1825 fprintf (f
, " %-20s ", name
);
1829 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1834 unsigned int tagv
= dyn
.d_un
.d_val
;
1836 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1839 fprintf (f
, "%s", string
);
1848 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1849 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1851 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1855 if (elf_dynverdef (abfd
) != 0)
1857 Elf_Internal_Verdef
*t
;
1859 fprintf (f
, _("\nVersion definitions:\n"));
1860 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1862 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1863 t
->vd_flags
, t
->vd_hash
,
1864 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1865 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1867 Elf_Internal_Verdaux
*a
;
1870 for (a
= t
->vd_auxptr
->vda_nextptr
;
1874 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1880 if (elf_dynverref (abfd
) != 0)
1882 Elf_Internal_Verneed
*t
;
1884 fprintf (f
, _("\nVersion References:\n"));
1885 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1887 Elf_Internal_Vernaux
*a
;
1889 fprintf (f
, _(" required from %s:\n"),
1890 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1891 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1892 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1893 a
->vna_flags
, a
->vna_other
,
1894 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1906 /* Get version string. */
1909 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1910 bfd_boolean
*hidden
)
1912 const char *version_string
= NULL
;
1913 if (elf_dynversym (abfd
) != 0
1914 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1916 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1918 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1919 vernum
&= VERSYM_VERSION
;
1922 version_string
= "";
1923 else if (vernum
== 1
1924 && (vernum
> elf_tdata (abfd
)->cverdefs
1925 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1927 version_string
= "Base";
1928 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1930 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1933 Elf_Internal_Verneed
*t
;
1935 version_string
= _("<corrupt>");
1936 for (t
= elf_tdata (abfd
)->verref
;
1940 Elf_Internal_Vernaux
*a
;
1942 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1944 if (a
->vna_other
== vernum
)
1946 version_string
= a
->vna_nodename
;
1953 return version_string
;
1956 /* Display ELF-specific fields of a symbol. */
1959 bfd_elf_print_symbol (bfd
*abfd
,
1962 bfd_print_symbol_type how
)
1964 FILE *file
= (FILE *) filep
;
1967 case bfd_print_symbol_name
:
1968 fprintf (file
, "%s", symbol
->name
);
1970 case bfd_print_symbol_more
:
1971 fprintf (file
, "elf ");
1972 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1973 fprintf (file
, " %x", symbol
->flags
);
1975 case bfd_print_symbol_all
:
1977 const char *section_name
;
1978 const char *name
= NULL
;
1979 const struct elf_backend_data
*bed
;
1980 unsigned char st_other
;
1982 const char *version_string
;
1985 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1987 bed
= get_elf_backend_data (abfd
);
1988 if (bed
->elf_backend_print_symbol_all
)
1989 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1993 name
= symbol
->name
;
1994 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1997 fprintf (file
, " %s\t", section_name
);
1998 /* Print the "other" value for a symbol. For common symbols,
1999 we've already printed the size; now print the alignment.
2000 For other symbols, we have no specified alignment, and
2001 we've printed the address; now print the size. */
2002 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
2003 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
2005 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
2006 bfd_fprintf_vma (abfd
, file
, val
);
2008 /* If we have version information, print it. */
2009 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2015 fprintf (file
, " %-11s", version_string
);
2020 fprintf (file
, " (%s)", version_string
);
2021 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2026 /* If the st_other field is not zero, print it. */
2027 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2032 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2033 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2034 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2036 /* Some other non-defined flags are also present, so print
2038 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2041 fprintf (file
, " %s", name
);
2047 /* ELF .o/exec file reading */
2049 /* Create a new bfd section from an ELF section header. */
2052 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2054 Elf_Internal_Shdr
*hdr
;
2055 Elf_Internal_Ehdr
*ehdr
;
2056 const struct elf_backend_data
*bed
;
2058 bfd_boolean ret
= TRUE
;
2059 static bfd_boolean
* sections_being_created
= NULL
;
2060 static bfd
* sections_being_created_abfd
= NULL
;
2061 static unsigned int nesting
= 0;
2063 if (shindex
>= elf_numsections (abfd
))
2068 /* PR17512: A corrupt ELF binary might contain a recursive group of
2069 sections, with each the string indices pointing to the next in the
2070 loop. Detect this here, by refusing to load a section that we are
2071 already in the process of loading. We only trigger this test if
2072 we have nested at least three sections deep as normal ELF binaries
2073 can expect to recurse at least once.
2075 FIXME: It would be better if this array was attached to the bfd,
2076 rather than being held in a static pointer. */
2078 if (sections_being_created_abfd
!= abfd
)
2079 sections_being_created
= NULL
;
2080 if (sections_being_created
== NULL
)
2082 sections_being_created
= (bfd_boolean
*)
2083 bfd_zalloc2 (abfd
, elf_numsections (abfd
), sizeof (bfd_boolean
));
2084 sections_being_created_abfd
= abfd
;
2086 if (sections_being_created
[shindex
])
2089 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2092 sections_being_created
[shindex
] = TRUE
;
2095 hdr
= elf_elfsections (abfd
)[shindex
];
2096 ehdr
= elf_elfheader (abfd
);
2097 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2102 bed
= get_elf_backend_data (abfd
);
2103 switch (hdr
->sh_type
)
2106 /* Inactive section. Throw it away. */
2109 case SHT_PROGBITS
: /* Normal section with contents. */
2110 case SHT_NOBITS
: /* .bss section. */
2111 case SHT_HASH
: /* .hash section. */
2112 case SHT_NOTE
: /* .note section. */
2113 case SHT_INIT_ARRAY
: /* .init_array section. */
2114 case SHT_FINI_ARRAY
: /* .fini_array section. */
2115 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2116 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2117 case SHT_GNU_HASH
: /* .gnu.hash section. */
2118 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2121 case SHT_DYNAMIC
: /* Dynamic linking information. */
2122 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2125 if (hdr
->sh_link
> elf_numsections (abfd
))
2127 /* PR 10478: Accept Solaris binaries with a sh_link
2128 field set to SHN_BEFORE or SHN_AFTER. */
2129 switch (bfd_get_arch (abfd
))
2132 case bfd_arch_sparc
:
2133 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2134 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2136 /* Otherwise fall through. */
2141 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2143 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2145 Elf_Internal_Shdr
*dynsymhdr
;
2147 /* The shared libraries distributed with hpux11 have a bogus
2148 sh_link field for the ".dynamic" section. Find the
2149 string table for the ".dynsym" section instead. */
2150 if (elf_dynsymtab (abfd
) != 0)
2152 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2153 hdr
->sh_link
= dynsymhdr
->sh_link
;
2157 unsigned int i
, num_sec
;
2159 num_sec
= elf_numsections (abfd
);
2160 for (i
= 1; i
< num_sec
; i
++)
2162 dynsymhdr
= elf_elfsections (abfd
)[i
];
2163 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2165 hdr
->sh_link
= dynsymhdr
->sh_link
;
2173 case SHT_SYMTAB
: /* A symbol table. */
2174 if (elf_onesymtab (abfd
) == shindex
)
2177 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2180 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2182 if (hdr
->sh_size
!= 0)
2184 /* Some assemblers erroneously set sh_info to one with a
2185 zero sh_size. ld sees this as a global symbol count
2186 of (unsigned) -1. Fix it here. */
2191 /* PR 18854: A binary might contain more than one symbol table.
2192 Unusual, but possible. Warn, but continue. */
2193 if (elf_onesymtab (abfd
) != 0)
2196 /* xgettext:c-format */
2197 (_("%pB: warning: multiple symbol tables detected"
2198 " - ignoring the table in section %u"),
2202 elf_onesymtab (abfd
) = shindex
;
2203 elf_symtab_hdr (abfd
) = *hdr
;
2204 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2205 abfd
->flags
|= HAS_SYMS
;
2207 /* Sometimes a shared object will map in the symbol table. If
2208 SHF_ALLOC is set, and this is a shared object, then we also
2209 treat this section as a BFD section. We can not base the
2210 decision purely on SHF_ALLOC, because that flag is sometimes
2211 set in a relocatable object file, which would confuse the
2213 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2214 && (abfd
->flags
& DYNAMIC
) != 0
2215 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2219 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2220 can't read symbols without that section loaded as well. It
2221 is most likely specified by the next section header. */
2223 elf_section_list
* entry
;
2224 unsigned int i
, num_sec
;
2226 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2227 if (entry
->hdr
.sh_link
== shindex
)
2230 num_sec
= elf_numsections (abfd
);
2231 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2233 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2235 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2236 && hdr2
->sh_link
== shindex
)
2241 for (i
= 1; i
< shindex
; i
++)
2243 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2245 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2246 && hdr2
->sh_link
== shindex
)
2251 ret
= bfd_section_from_shdr (abfd
, i
);
2252 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2256 case SHT_DYNSYM
: /* A dynamic symbol table. */
2257 if (elf_dynsymtab (abfd
) == shindex
)
2260 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2263 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2265 if (hdr
->sh_size
!= 0)
2268 /* Some linkers erroneously set sh_info to one with a
2269 zero sh_size. ld sees this as a global symbol count
2270 of (unsigned) -1. Fix it here. */
2275 /* PR 18854: A binary might contain more than one dynamic symbol table.
2276 Unusual, but possible. Warn, but continue. */
2277 if (elf_dynsymtab (abfd
) != 0)
2280 /* xgettext:c-format */
2281 (_("%pB: warning: multiple dynamic symbol tables detected"
2282 " - ignoring the table in section %u"),
2286 elf_dynsymtab (abfd
) = shindex
;
2287 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2288 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2289 abfd
->flags
|= HAS_SYMS
;
2291 /* Besides being a symbol table, we also treat this as a regular
2292 section, so that objcopy can handle it. */
2293 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2296 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2298 elf_section_list
* entry
;
2300 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2301 if (entry
->ndx
== shindex
)
2304 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2307 entry
->ndx
= shindex
;
2309 entry
->next
= elf_symtab_shndx_list (abfd
);
2310 elf_symtab_shndx_list (abfd
) = entry
;
2311 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2315 case SHT_STRTAB
: /* A string table. */
2316 if (hdr
->bfd_section
!= NULL
)
2319 if (ehdr
->e_shstrndx
== shindex
)
2321 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2322 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2326 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2329 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2330 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2334 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2337 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2338 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2339 elf_elfsections (abfd
)[shindex
] = hdr
;
2340 /* We also treat this as a regular section, so that objcopy
2342 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2347 /* If the string table isn't one of the above, then treat it as a
2348 regular section. We need to scan all the headers to be sure,
2349 just in case this strtab section appeared before the above. */
2350 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2352 unsigned int i
, num_sec
;
2354 num_sec
= elf_numsections (abfd
);
2355 for (i
= 1; i
< num_sec
; i
++)
2357 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2358 if (hdr2
->sh_link
== shindex
)
2360 /* Prevent endless recursion on broken objects. */
2363 if (! bfd_section_from_shdr (abfd
, i
))
2365 if (elf_onesymtab (abfd
) == i
)
2367 if (elf_dynsymtab (abfd
) == i
)
2368 goto dynsymtab_strtab
;
2372 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2377 /* *These* do a lot of work -- but build no sections! */
2379 asection
*target_sect
;
2380 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2381 unsigned int num_sec
= elf_numsections (abfd
);
2382 struct bfd_elf_section_data
*esdt
;
2385 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2386 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2389 /* Check for a bogus link to avoid crashing. */
2390 if (hdr
->sh_link
>= num_sec
)
2393 /* xgettext:c-format */
2394 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2395 abfd
, hdr
->sh_link
, name
, shindex
);
2396 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2401 /* For some incomprehensible reason Oracle distributes
2402 libraries for Solaris in which some of the objects have
2403 bogus sh_link fields. It would be nice if we could just
2404 reject them, but, unfortunately, some people need to use
2405 them. We scan through the section headers; if we find only
2406 one suitable symbol table, we clobber the sh_link to point
2407 to it. I hope this doesn't break anything.
2409 Don't do it on executable nor shared library. */
2410 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2411 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2412 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2418 for (scan
= 1; scan
< num_sec
; scan
++)
2420 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2421 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2432 hdr
->sh_link
= found
;
2435 /* Get the symbol table. */
2436 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2437 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2438 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2441 /* If this is an alloc section in an executable or shared
2442 library, or the reloc section does not use the main symbol
2443 table we don't treat it as a reloc section. BFD can't
2444 adequately represent such a section, so at least for now,
2445 we don't try. We just present it as a normal section. We
2446 also can't use it as a reloc section if it points to the
2447 null section, an invalid section, another reloc section, or
2448 its sh_link points to the null section. */
2449 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2450 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2451 || hdr
->sh_link
== SHN_UNDEF
2452 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2453 || hdr
->sh_info
== SHN_UNDEF
2454 || hdr
->sh_info
>= num_sec
2455 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2456 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2458 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2463 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2466 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2467 if (target_sect
== NULL
)
2470 esdt
= elf_section_data (target_sect
);
2471 if (hdr
->sh_type
== SHT_RELA
)
2472 p_hdr
= &esdt
->rela
.hdr
;
2474 p_hdr
= &esdt
->rel
.hdr
;
2476 /* PR 17512: file: 0b4f81b7.
2477 Also see PR 24456, for a file which deliberately has two reloc
2482 /* xgettext:c-format */
2483 (_("%pB: warning: multiple relocation sections for section %pA \
2484 found - ignoring all but the first"),
2488 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2493 elf_elfsections (abfd
)[shindex
] = hdr2
;
2494 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2495 * bed
->s
->int_rels_per_ext_rel
);
2496 target_sect
->flags
|= SEC_RELOC
;
2497 target_sect
->relocation
= NULL
;
2498 target_sect
->rel_filepos
= hdr
->sh_offset
;
2499 /* In the section to which the relocations apply, mark whether
2500 its relocations are of the REL or RELA variety. */
2501 if (hdr
->sh_size
!= 0)
2503 if (hdr
->sh_type
== SHT_RELA
)
2504 target_sect
->use_rela_p
= 1;
2506 abfd
->flags
|= HAS_RELOC
;
2510 case SHT_GNU_verdef
:
2511 elf_dynverdef (abfd
) = shindex
;
2512 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2513 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2516 case SHT_GNU_versym
:
2517 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2520 elf_dynversym (abfd
) = shindex
;
2521 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2522 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2525 case SHT_GNU_verneed
:
2526 elf_dynverref (abfd
) = shindex
;
2527 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2528 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2535 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2538 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2544 /* Possibly an attributes section. */
2545 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2546 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2548 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2550 _bfd_elf_parse_attributes (abfd
, hdr
);
2554 /* Check for any processor-specific section types. */
2555 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2558 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2560 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2561 /* FIXME: How to properly handle allocated section reserved
2562 for applications? */
2564 /* xgettext:c-format */
2565 (_("%pB: unknown type [%#x] section `%s'"),
2566 abfd
, hdr
->sh_type
, name
);
2569 /* Allow sections reserved for applications. */
2570 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2575 else if (hdr
->sh_type
>= SHT_LOPROC
2576 && hdr
->sh_type
<= SHT_HIPROC
)
2577 /* FIXME: We should handle this section. */
2579 /* xgettext:c-format */
2580 (_("%pB: unknown type [%#x] section `%s'"),
2581 abfd
, hdr
->sh_type
, name
);
2582 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2584 /* Unrecognised OS-specific sections. */
2585 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2586 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2587 required to correctly process the section and the file should
2588 be rejected with an error message. */
2590 /* xgettext:c-format */
2591 (_("%pB: unknown type [%#x] section `%s'"),
2592 abfd
, hdr
->sh_type
, name
);
2595 /* Otherwise it should be processed. */
2596 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2601 /* FIXME: We should handle this section. */
2603 /* xgettext:c-format */
2604 (_("%pB: unknown type [%#x] section `%s'"),
2605 abfd
, hdr
->sh_type
, name
);
2613 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2614 sections_being_created
[shindex
] = FALSE
;
2615 if (-- nesting
== 0)
2617 sections_being_created
= NULL
;
2618 sections_being_created_abfd
= abfd
;
2623 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2626 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2628 unsigned long r_symndx
)
2630 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2632 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2634 Elf_Internal_Shdr
*symtab_hdr
;
2635 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2636 Elf_External_Sym_Shndx eshndx
;
2638 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2639 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2640 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2643 if (cache
->abfd
!= abfd
)
2645 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2648 cache
->indx
[ent
] = r_symndx
;
2651 return &cache
->sym
[ent
];
2654 /* Given an ELF section number, retrieve the corresponding BFD
2658 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2660 if (sec_index
>= elf_numsections (abfd
))
2662 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2665 static const struct bfd_elf_special_section special_sections_b
[] =
2667 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2668 { NULL
, 0, 0, 0, 0 }
2671 static const struct bfd_elf_special_section special_sections_c
[] =
2673 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2674 { NULL
, 0, 0, 0, 0 }
2677 static const struct bfd_elf_special_section special_sections_d
[] =
2679 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2680 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2681 /* There are more DWARF sections than these, but they needn't be added here
2682 unless you have to cope with broken compilers that don't emit section
2683 attributes or you want to help the user writing assembler. */
2684 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2685 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2686 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2687 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2688 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2689 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2690 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2691 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2692 { NULL
, 0, 0, 0, 0 }
2695 static const struct bfd_elf_special_section special_sections_f
[] =
2697 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2698 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2699 { NULL
, 0 , 0, 0, 0 }
2702 static const struct bfd_elf_special_section special_sections_g
[] =
2704 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2705 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2706 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2707 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2708 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2709 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2710 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2711 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2712 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2713 { NULL
, 0, 0, 0, 0 }
2716 static const struct bfd_elf_special_section special_sections_h
[] =
2718 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2719 { NULL
, 0, 0, 0, 0 }
2722 static const struct bfd_elf_special_section special_sections_i
[] =
2724 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2725 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2726 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2727 { NULL
, 0, 0, 0, 0 }
2730 static const struct bfd_elf_special_section special_sections_l
[] =
2732 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2733 { NULL
, 0, 0, 0, 0 }
2736 static const struct bfd_elf_special_section special_sections_n
[] =
2738 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2739 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2740 { NULL
, 0, 0, 0, 0 }
2743 static const struct bfd_elf_special_section special_sections_p
[] =
2745 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2746 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2747 { NULL
, 0, 0, 0, 0 }
2750 static const struct bfd_elf_special_section special_sections_r
[] =
2752 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2753 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2754 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2755 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2756 { NULL
, 0, 0, 0, 0 }
2759 static const struct bfd_elf_special_section special_sections_s
[] =
2761 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2762 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2763 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2764 /* See struct bfd_elf_special_section declaration for the semantics of
2765 this special case where .prefix_length != strlen (.prefix). */
2766 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2767 { NULL
, 0, 0, 0, 0 }
2770 static const struct bfd_elf_special_section special_sections_t
[] =
2772 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2773 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2774 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2775 { NULL
, 0, 0, 0, 0 }
2778 static const struct bfd_elf_special_section special_sections_z
[] =
2780 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2781 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2782 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2783 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2784 { NULL
, 0, 0, 0, 0 }
2787 static const struct bfd_elf_special_section
* const special_sections
[] =
2789 special_sections_b
, /* 'b' */
2790 special_sections_c
, /* 'c' */
2791 special_sections_d
, /* 'd' */
2793 special_sections_f
, /* 'f' */
2794 special_sections_g
, /* 'g' */
2795 special_sections_h
, /* 'h' */
2796 special_sections_i
, /* 'i' */
2799 special_sections_l
, /* 'l' */
2801 special_sections_n
, /* 'n' */
2803 special_sections_p
, /* 'p' */
2805 special_sections_r
, /* 'r' */
2806 special_sections_s
, /* 's' */
2807 special_sections_t
, /* 't' */
2813 special_sections_z
/* 'z' */
2816 const struct bfd_elf_special_section
*
2817 _bfd_elf_get_special_section (const char *name
,
2818 const struct bfd_elf_special_section
*spec
,
2824 len
= strlen (name
);
2826 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2829 int prefix_len
= spec
[i
].prefix_length
;
2831 if (len
< prefix_len
)
2833 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2836 suffix_len
= spec
[i
].suffix_length
;
2837 if (suffix_len
<= 0)
2839 if (name
[prefix_len
] != 0)
2841 if (suffix_len
== 0)
2843 if (name
[prefix_len
] != '.'
2844 && (suffix_len
== -2
2845 || (rela
&& spec
[i
].type
== SHT_REL
)))
2851 if (len
< prefix_len
+ suffix_len
)
2853 if (memcmp (name
+ len
- suffix_len
,
2854 spec
[i
].prefix
+ prefix_len
,
2864 const struct bfd_elf_special_section
*
2865 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2868 const struct bfd_elf_special_section
*spec
;
2869 const struct elf_backend_data
*bed
;
2871 /* See if this is one of the special sections. */
2872 if (sec
->name
== NULL
)
2875 bed
= get_elf_backend_data (abfd
);
2876 spec
= bed
->special_sections
;
2879 spec
= _bfd_elf_get_special_section (sec
->name
,
2880 bed
->special_sections
,
2886 if (sec
->name
[0] != '.')
2889 i
= sec
->name
[1] - 'b';
2890 if (i
< 0 || i
> 'z' - 'b')
2893 spec
= special_sections
[i
];
2898 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2902 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2904 struct bfd_elf_section_data
*sdata
;
2905 const struct elf_backend_data
*bed
;
2906 const struct bfd_elf_special_section
*ssect
;
2908 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2911 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2915 sec
->used_by_bfd
= sdata
;
2918 /* Indicate whether or not this section should use RELA relocations. */
2919 bed
= get_elf_backend_data (abfd
);
2920 sec
->use_rela_p
= bed
->default_use_rela_p
;
2922 /* When we read a file, we don't need to set ELF section type and
2923 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2924 anyway. We will set ELF section type and flags for all linker
2925 created sections. If user specifies BFD section flags, we will
2926 set ELF section type and flags based on BFD section flags in
2927 elf_fake_sections. Special handling for .init_array/.fini_array
2928 output sections since they may contain .ctors/.dtors input
2929 sections. We don't want _bfd_elf_init_private_section_data to
2930 copy ELF section type from .ctors/.dtors input sections. */
2931 if (abfd
->direction
!= read_direction
2932 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2934 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2937 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2938 || ssect
->type
== SHT_INIT_ARRAY
2939 || ssect
->type
== SHT_FINI_ARRAY
))
2941 elf_section_type (sec
) = ssect
->type
;
2942 elf_section_flags (sec
) = ssect
->attr
;
2946 return _bfd_generic_new_section_hook (abfd
, sec
);
2949 /* Create a new bfd section from an ELF program header.
2951 Since program segments have no names, we generate a synthetic name
2952 of the form segment<NUM>, where NUM is generally the index in the
2953 program header table. For segments that are split (see below) we
2954 generate the names segment<NUM>a and segment<NUM>b.
2956 Note that some program segments may have a file size that is different than
2957 (less than) the memory size. All this means is that at execution the
2958 system must allocate the amount of memory specified by the memory size,
2959 but only initialize it with the first "file size" bytes read from the
2960 file. This would occur for example, with program segments consisting
2961 of combined data+bss.
2963 To handle the above situation, this routine generates TWO bfd sections
2964 for the single program segment. The first has the length specified by
2965 the file size of the segment, and the second has the length specified
2966 by the difference between the two sizes. In effect, the segment is split
2967 into its initialized and uninitialized parts.
2972 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2973 Elf_Internal_Phdr
*hdr
,
2975 const char *type_name
)
2983 split
= ((hdr
->p_memsz
> 0)
2984 && (hdr
->p_filesz
> 0)
2985 && (hdr
->p_memsz
> hdr
->p_filesz
));
2987 if (hdr
->p_filesz
> 0)
2989 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2990 len
= strlen (namebuf
) + 1;
2991 name
= (char *) bfd_alloc (abfd
, len
);
2994 memcpy (name
, namebuf
, len
);
2995 newsect
= bfd_make_section (abfd
, name
);
2996 if (newsect
== NULL
)
2998 newsect
->vma
= hdr
->p_vaddr
;
2999 newsect
->lma
= hdr
->p_paddr
;
3000 newsect
->size
= hdr
->p_filesz
;
3001 newsect
->filepos
= hdr
->p_offset
;
3002 newsect
->flags
|= SEC_HAS_CONTENTS
;
3003 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
3004 if (hdr
->p_type
== PT_LOAD
)
3006 newsect
->flags
|= SEC_ALLOC
;
3007 newsect
->flags
|= SEC_LOAD
;
3008 if (hdr
->p_flags
& PF_X
)
3010 /* FIXME: all we known is that it has execute PERMISSION,
3012 newsect
->flags
|= SEC_CODE
;
3015 if (!(hdr
->p_flags
& PF_W
))
3017 newsect
->flags
|= SEC_READONLY
;
3021 if (hdr
->p_memsz
> hdr
->p_filesz
)
3025 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3026 len
= strlen (namebuf
) + 1;
3027 name
= (char *) bfd_alloc (abfd
, len
);
3030 memcpy (name
, namebuf
, len
);
3031 newsect
= bfd_make_section (abfd
, name
);
3032 if (newsect
== NULL
)
3034 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
3035 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
3036 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3037 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3038 align
= newsect
->vma
& -newsect
->vma
;
3039 if (align
== 0 || align
> hdr
->p_align
)
3040 align
= hdr
->p_align
;
3041 newsect
->alignment_power
= bfd_log2 (align
);
3042 if (hdr
->p_type
== PT_LOAD
)
3044 /* Hack for gdb. Segments that have not been modified do
3045 not have their contents written to a core file, on the
3046 assumption that a debugger can find the contents in the
3047 executable. We flag this case by setting the fake
3048 section size to zero. Note that "real" bss sections will
3049 always have their contents dumped to the core file. */
3050 if (bfd_get_format (abfd
) == bfd_core
)
3052 newsect
->flags
|= SEC_ALLOC
;
3053 if (hdr
->p_flags
& PF_X
)
3054 newsect
->flags
|= SEC_CODE
;
3056 if (!(hdr
->p_flags
& PF_W
))
3057 newsect
->flags
|= SEC_READONLY
;
3064 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3066 const struct elf_backend_data
*bed
;
3068 switch (hdr
->p_type
)
3071 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3074 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
3077 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3080 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3083 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3085 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3091 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3094 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3096 case PT_GNU_EH_FRAME
:
3097 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3101 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3104 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3107 /* Check for any processor-specific program segment types. */
3108 bed
= get_elf_backend_data (abfd
);
3109 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3113 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3117 _bfd_elf_single_rel_hdr (asection
*sec
)
3119 if (elf_section_data (sec
)->rel
.hdr
)
3121 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3122 return elf_section_data (sec
)->rel
.hdr
;
3125 return elf_section_data (sec
)->rela
.hdr
;
3129 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3130 Elf_Internal_Shdr
*rel_hdr
,
3131 const char *sec_name
,
3132 bfd_boolean use_rela_p
)
3134 char *name
= (char *) bfd_alloc (abfd
,
3135 sizeof ".rela" + strlen (sec_name
));
3139 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3141 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3143 if (rel_hdr
->sh_name
== (unsigned int) -1)
3149 /* Allocate and initialize a section-header for a new reloc section,
3150 containing relocations against ASECT. It is stored in RELDATA. If
3151 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3155 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3156 struct bfd_elf_section_reloc_data
*reldata
,
3157 const char *sec_name
,
3158 bfd_boolean use_rela_p
,
3159 bfd_boolean delay_st_name_p
)
3161 Elf_Internal_Shdr
*rel_hdr
;
3162 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3164 BFD_ASSERT (reldata
->hdr
== NULL
);
3165 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3166 reldata
->hdr
= rel_hdr
;
3168 if (delay_st_name_p
)
3169 rel_hdr
->sh_name
= (unsigned int) -1;
3170 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3173 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3174 rel_hdr
->sh_entsize
= (use_rela_p
3175 ? bed
->s
->sizeof_rela
3176 : bed
->s
->sizeof_rel
);
3177 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3178 rel_hdr
->sh_flags
= 0;
3179 rel_hdr
->sh_addr
= 0;
3180 rel_hdr
->sh_size
= 0;
3181 rel_hdr
->sh_offset
= 0;
3186 /* Return the default section type based on the passed in section flags. */
3189 bfd_elf_get_default_section_type (flagword flags
)
3191 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3192 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3194 return SHT_PROGBITS
;
3197 struct fake_section_arg
3199 struct bfd_link_info
*link_info
;
3203 /* Set up an ELF internal section header for a section. */
3206 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3208 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3209 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3210 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3211 Elf_Internal_Shdr
*this_hdr
;
3212 unsigned int sh_type
;
3213 const char *name
= asect
->name
;
3214 bfd_boolean delay_st_name_p
= FALSE
;
3218 /* We already failed; just get out of the bfd_map_over_sections
3223 this_hdr
= &esd
->this_hdr
;
3227 /* ld: compress DWARF debug sections with names: .debug_*. */
3228 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3229 && (asect
->flags
& SEC_DEBUGGING
)
3233 /* Set SEC_ELF_COMPRESS to indicate this section should be
3235 asect
->flags
|= SEC_ELF_COMPRESS
;
3237 /* If this section will be compressed, delay adding section
3238 name to section name section after it is compressed in
3239 _bfd_elf_assign_file_positions_for_non_load. */
3240 delay_st_name_p
= TRUE
;
3243 else if ((asect
->flags
& SEC_ELF_RENAME
))
3245 /* objcopy: rename output DWARF debug section. */
3246 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3248 /* When we decompress or compress with SHF_COMPRESSED,
3249 convert section name from .zdebug_* to .debug_* if
3253 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3254 if (new_name
== NULL
)
3262 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3264 /* PR binutils/18087: Compression does not always make a
3265 section smaller. So only rename the section when
3266 compression has actually taken place. If input section
3267 name is .zdebug_*, we should never compress it again. */
3268 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3269 if (new_name
== NULL
)
3274 BFD_ASSERT (name
[1] != 'z');
3279 if (delay_st_name_p
)
3280 this_hdr
->sh_name
= (unsigned int) -1;
3284 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3286 if (this_hdr
->sh_name
== (unsigned int) -1)
3293 /* Don't clear sh_flags. Assembler may set additional bits. */
3295 if ((asect
->flags
& SEC_ALLOC
) != 0
3296 || asect
->user_set_vma
)
3297 this_hdr
->sh_addr
= asect
->vma
;
3299 this_hdr
->sh_addr
= 0;
3301 this_hdr
->sh_offset
= 0;
3302 this_hdr
->sh_size
= asect
->size
;
3303 this_hdr
->sh_link
= 0;
3304 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3305 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3308 /* xgettext:c-format */
3309 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3310 abfd
, asect
->alignment_power
, asect
);
3314 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3315 /* The sh_entsize and sh_info fields may have been set already by
3316 copy_private_section_data. */
3318 this_hdr
->bfd_section
= asect
;
3319 this_hdr
->contents
= NULL
;
3321 /* If the section type is unspecified, we set it based on
3323 if ((asect
->flags
& SEC_GROUP
) != 0)
3324 sh_type
= SHT_GROUP
;
3326 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3328 if (this_hdr
->sh_type
== SHT_NULL
)
3329 this_hdr
->sh_type
= sh_type
;
3330 else if (this_hdr
->sh_type
== SHT_NOBITS
3331 && sh_type
== SHT_PROGBITS
3332 && (asect
->flags
& SEC_ALLOC
) != 0)
3334 /* Warn if we are changing a NOBITS section to PROGBITS, but
3335 allow the link to proceed. This can happen when users link
3336 non-bss input sections to bss output sections, or emit data
3337 to a bss output section via a linker script. */
3339 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3340 this_hdr
->sh_type
= sh_type
;
3343 switch (this_hdr
->sh_type
)
3354 case SHT_INIT_ARRAY
:
3355 case SHT_FINI_ARRAY
:
3356 case SHT_PREINIT_ARRAY
:
3357 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3361 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3365 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3369 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3373 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3374 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3378 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3379 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3382 case SHT_GNU_versym
:
3383 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3386 case SHT_GNU_verdef
:
3387 this_hdr
->sh_entsize
= 0;
3388 /* objcopy or strip will copy over sh_info, but may not set
3389 cverdefs. The linker will set cverdefs, but sh_info will be
3391 if (this_hdr
->sh_info
== 0)
3392 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3394 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3395 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3398 case SHT_GNU_verneed
:
3399 this_hdr
->sh_entsize
= 0;
3400 /* objcopy or strip will copy over sh_info, but may not set
3401 cverrefs. The linker will set cverrefs, but sh_info will be
3403 if (this_hdr
->sh_info
== 0)
3404 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3406 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3407 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3411 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3415 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3419 if ((asect
->flags
& SEC_ALLOC
) != 0)
3420 this_hdr
->sh_flags
|= SHF_ALLOC
;
3421 if ((asect
->flags
& SEC_READONLY
) == 0)
3422 this_hdr
->sh_flags
|= SHF_WRITE
;
3423 if ((asect
->flags
& SEC_CODE
) != 0)
3424 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3425 if ((asect
->flags
& SEC_MERGE
) != 0)
3427 this_hdr
->sh_flags
|= SHF_MERGE
;
3428 this_hdr
->sh_entsize
= asect
->entsize
;
3430 if ((asect
->flags
& SEC_STRINGS
) != 0)
3431 this_hdr
->sh_flags
|= SHF_STRINGS
;
3432 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3433 this_hdr
->sh_flags
|= SHF_GROUP
;
3434 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3436 this_hdr
->sh_flags
|= SHF_TLS
;
3437 if (asect
->size
== 0
3438 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3440 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3442 this_hdr
->sh_size
= 0;
3445 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3446 if (this_hdr
->sh_size
!= 0)
3447 this_hdr
->sh_type
= SHT_NOBITS
;
3451 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3452 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3454 /* If the section has relocs, set up a section header for the
3455 SHT_REL[A] section. If two relocation sections are required for
3456 this section, it is up to the processor-specific back-end to
3457 create the other. */
3458 if ((asect
->flags
& SEC_RELOC
) != 0)
3460 /* When doing a relocatable link, create both REL and RELA sections if
3463 /* Do the normal setup if we wouldn't create any sections here. */
3464 && esd
->rel
.count
+ esd
->rela
.count
> 0
3465 && (bfd_link_relocatable (arg
->link_info
)
3466 || arg
->link_info
->emitrelocations
))
3468 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3469 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3470 FALSE
, delay_st_name_p
))
3475 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3476 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3477 TRUE
, delay_st_name_p
))
3483 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3485 ? &esd
->rela
: &esd
->rel
),
3495 /* Check for processor-specific section types. */
3496 sh_type
= this_hdr
->sh_type
;
3497 if (bed
->elf_backend_fake_sections
3498 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3504 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3506 /* Don't change the header type from NOBITS if we are being
3507 called for objcopy --only-keep-debug. */
3508 this_hdr
->sh_type
= sh_type
;
3512 /* Fill in the contents of a SHT_GROUP section. Called from
3513 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3514 when ELF targets use the generic linker, ld. Called for ld -r
3515 from bfd_elf_final_link. */
3518 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3520 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3521 asection
*elt
, *first
;
3525 /* Ignore linker created group section. See elfNN_ia64_object_p in
3527 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3532 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3534 unsigned long symindx
= 0;
3536 /* elf_group_id will have been set up by objcopy and the
3538 if (elf_group_id (sec
) != NULL
)
3539 symindx
= elf_group_id (sec
)->udata
.i
;
3543 /* If called from the assembler, swap_out_syms will have set up
3544 elf_section_syms. */
3545 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3546 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3548 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3550 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3552 /* The ELF backend linker sets sh_info to -2 when the group
3553 signature symbol is global, and thus the index can't be
3554 set until all local symbols are output. */
3556 struct bfd_elf_section_data
*sec_data
;
3557 unsigned long symndx
;
3558 unsigned long extsymoff
;
3559 struct elf_link_hash_entry
*h
;
3561 /* The point of this little dance to the first SHF_GROUP section
3562 then back to the SHT_GROUP section is that this gets us to
3563 the SHT_GROUP in the input object. */
3564 igroup
= elf_sec_group (elf_next_in_group (sec
));
3565 sec_data
= elf_section_data (igroup
);
3566 symndx
= sec_data
->this_hdr
.sh_info
;
3568 if (!elf_bad_symtab (igroup
->owner
))
3570 Elf_Internal_Shdr
*symtab_hdr
;
3572 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3573 extsymoff
= symtab_hdr
->sh_info
;
3575 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3576 while (h
->root
.type
== bfd_link_hash_indirect
3577 || h
->root
.type
== bfd_link_hash_warning
)
3578 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3580 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3583 /* The contents won't be allocated for "ld -r" or objcopy. */
3585 if (sec
->contents
== NULL
)
3588 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3590 /* Arrange for the section to be written out. */
3591 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3592 if (sec
->contents
== NULL
)
3599 loc
= sec
->contents
+ sec
->size
;
3601 /* Get the pointer to the first section in the group that gas
3602 squirreled away here. objcopy arranges for this to be set to the
3603 start of the input section group. */
3604 first
= elt
= elf_next_in_group (sec
);
3606 /* First element is a flag word. Rest of section is elf section
3607 indices for all the sections of the group. Write them backwards
3608 just to keep the group in the same order as given in .section
3609 directives, not that it matters. */
3616 s
= s
->output_section
;
3618 && !bfd_is_abs_section (s
))
3620 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3621 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3623 if (elf_sec
->rel
.hdr
!= NULL
3625 || (input_elf_sec
->rel
.hdr
!= NULL
3626 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3628 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3630 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3632 if (elf_sec
->rela
.hdr
!= NULL
3634 || (input_elf_sec
->rela
.hdr
!= NULL
3635 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3637 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3639 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3642 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3644 elt
= elf_next_in_group (elt
);
3650 BFD_ASSERT (loc
== sec
->contents
);
3652 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3655 /* Given NAME, the name of a relocation section stripped of its
3656 .rel/.rela prefix, return the section in ABFD to which the
3657 relocations apply. */
3660 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3662 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3663 section likely apply to .got.plt or .got section. */
3664 if (get_elf_backend_data (abfd
)->want_got_plt
3665 && strcmp (name
, ".plt") == 0)
3670 sec
= bfd_get_section_by_name (abfd
, name
);
3676 return bfd_get_section_by_name (abfd
, name
);
3679 /* Return the section to which RELOC_SEC applies. */
3682 elf_get_reloc_section (asection
*reloc_sec
)
3687 const struct elf_backend_data
*bed
;
3689 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3690 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3693 /* We look up the section the relocs apply to by name. */
3694 name
= reloc_sec
->name
;
3695 if (strncmp (name
, ".rel", 4) != 0)
3698 if (type
== SHT_RELA
&& *name
++ != 'a')
3701 abfd
= reloc_sec
->owner
;
3702 bed
= get_elf_backend_data (abfd
);
3703 return bed
->get_reloc_section (abfd
, name
);
3706 /* Assign all ELF section numbers. The dummy first section is handled here
3707 too. The link/info pointers for the standard section types are filled
3708 in here too, while we're at it. */
3711 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3713 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3715 unsigned int section_number
;
3716 Elf_Internal_Shdr
**i_shdrp
;
3717 struct bfd_elf_section_data
*d
;
3718 bfd_boolean need_symtab
;
3722 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3724 /* SHT_GROUP sections are in relocatable files only. */
3725 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3727 size_t reloc_count
= 0;
3729 /* Put SHT_GROUP sections first. */
3730 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3732 d
= elf_section_data (sec
);
3734 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3736 if (sec
->flags
& SEC_LINKER_CREATED
)
3738 /* Remove the linker created SHT_GROUP sections. */
3739 bfd_section_list_remove (abfd
, sec
);
3740 abfd
->section_count
--;
3743 d
->this_idx
= section_number
++;
3746 /* Count relocations. */
3747 reloc_count
+= sec
->reloc_count
;
3750 /* Clear HAS_RELOC if there are no relocations. */
3751 if (reloc_count
== 0)
3752 abfd
->flags
&= ~HAS_RELOC
;
3755 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3757 d
= elf_section_data (sec
);
3759 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3760 d
->this_idx
= section_number
++;
3761 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3762 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3765 d
->rel
.idx
= section_number
++;
3766 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3767 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3774 d
->rela
.idx
= section_number
++;
3775 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3776 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3782 need_symtab
= (bfd_get_symcount (abfd
) > 0
3783 || (link_info
== NULL
3784 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3788 elf_onesymtab (abfd
) = section_number
++;
3789 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3790 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3792 elf_section_list
*entry
;
3794 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3796 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3797 entry
->ndx
= section_number
++;
3798 elf_symtab_shndx_list (abfd
) = entry
;
3800 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3801 ".symtab_shndx", FALSE
);
3802 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3805 elf_strtab_sec (abfd
) = section_number
++;
3806 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3809 elf_shstrtab_sec (abfd
) = section_number
++;
3810 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3811 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3813 if (section_number
>= SHN_LORESERVE
)
3815 /* xgettext:c-format */
3816 _bfd_error_handler (_("%pB: too many sections: %u"),
3817 abfd
, section_number
);
3821 elf_numsections (abfd
) = section_number
;
3822 elf_elfheader (abfd
)->e_shnum
= section_number
;
3824 /* Set up the list of section header pointers, in agreement with the
3826 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3827 sizeof (Elf_Internal_Shdr
*));
3828 if (i_shdrp
== NULL
)
3831 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3832 sizeof (Elf_Internal_Shdr
));
3833 if (i_shdrp
[0] == NULL
)
3835 bfd_release (abfd
, i_shdrp
);
3839 elf_elfsections (abfd
) = i_shdrp
;
3841 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3844 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3845 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3847 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3848 BFD_ASSERT (entry
!= NULL
);
3849 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3850 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3852 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3853 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3856 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3860 d
= elf_section_data (sec
);
3862 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3863 if (d
->rel
.idx
!= 0)
3864 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3865 if (d
->rela
.idx
!= 0)
3866 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3868 /* Fill in the sh_link and sh_info fields while we're at it. */
3870 /* sh_link of a reloc section is the section index of the symbol
3871 table. sh_info is the section index of the section to which
3872 the relocation entries apply. */
3873 if (d
->rel
.idx
!= 0)
3875 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3876 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3877 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3879 if (d
->rela
.idx
!= 0)
3881 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3882 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3883 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3886 /* We need to set up sh_link for SHF_LINK_ORDER. */
3887 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3889 s
= elf_linked_to_section (sec
);
3892 /* elf_linked_to_section points to the input section. */
3893 if (link_info
!= NULL
)
3895 /* Check discarded linkonce section. */
3896 if (discarded_section (s
))
3900 /* xgettext:c-format */
3901 (_("%pB: sh_link of section `%pA' points to"
3902 " discarded section `%pA' of `%pB'"),
3903 abfd
, d
->this_hdr
.bfd_section
,
3905 /* Point to the kept section if it has the same
3906 size as the discarded one. */
3907 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3910 bfd_set_error (bfd_error_bad_value
);
3916 s
= s
->output_section
;
3917 BFD_ASSERT (s
!= NULL
);
3921 /* Handle objcopy. */
3922 if (s
->output_section
== NULL
)
3925 /* xgettext:c-format */
3926 (_("%pB: sh_link of section `%pA' points to"
3927 " removed section `%pA' of `%pB'"),
3928 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3929 bfd_set_error (bfd_error_bad_value
);
3932 s
= s
->output_section
;
3934 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3939 The Intel C compiler generates SHT_IA_64_UNWIND with
3940 SHF_LINK_ORDER. But it doesn't set the sh_link or
3941 sh_info fields. Hence we could get the situation
3943 const struct elf_backend_data
*bed
3944 = get_elf_backend_data (abfd
);
3945 if (bed
->link_order_error_handler
)
3946 bed
->link_order_error_handler
3947 /* xgettext:c-format */
3948 (_("%pB: warning: sh_link not set for section `%pA'"),
3953 switch (d
->this_hdr
.sh_type
)
3957 /* A reloc section which we are treating as a normal BFD
3958 section. sh_link is the section index of the symbol
3959 table. sh_info is the section index of the section to
3960 which the relocation entries apply. We assume that an
3961 allocated reloc section uses the dynamic symbol table.
3962 FIXME: How can we be sure? */
3963 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3965 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3967 s
= elf_get_reloc_section (sec
);
3970 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3971 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3976 /* We assume that a section named .stab*str is a stabs
3977 string section. We look for a section with the same name
3978 but without the trailing ``str'', and set its sh_link
3979 field to point to this section. */
3980 if (CONST_STRNEQ (sec
->name
, ".stab")
3981 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3986 len
= strlen (sec
->name
);
3987 alc
= (char *) bfd_malloc (len
- 2);
3990 memcpy (alc
, sec
->name
, len
- 3);
3991 alc
[len
- 3] = '\0';
3992 s
= bfd_get_section_by_name (abfd
, alc
);
3996 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3998 /* This is a .stab section. */
3999 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
4000 elf_section_data (s
)->this_hdr
.sh_entsize
4001 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
4008 case SHT_GNU_verneed
:
4009 case SHT_GNU_verdef
:
4010 /* sh_link is the section header index of the string table
4011 used for the dynamic entries, or the symbol table, or the
4013 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4015 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4018 case SHT_GNU_LIBLIST
:
4019 /* sh_link is the section header index of the prelink library
4020 list used for the dynamic entries, or the symbol table, or
4021 the version strings. */
4022 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4023 ? ".dynstr" : ".gnu.libstr");
4025 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4030 case SHT_GNU_versym
:
4031 /* sh_link is the section header index of the symbol table
4032 this hash table or version table is for. */
4033 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4035 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4039 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4043 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4044 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4045 debug section name from .debug_* to .zdebug_* if needed. */
4051 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4053 /* If the backend has a special mapping, use it. */
4054 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4055 if (bed
->elf_backend_sym_is_global
)
4056 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4058 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4059 || bfd_is_und_section (bfd_asymbol_section (sym
))
4060 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4063 /* Filter global symbols of ABFD to include in the import library. All
4064 SYMCOUNT symbols of ABFD can be examined from their pointers in
4065 SYMS. Pointers of symbols to keep should be stored contiguously at
4066 the beginning of that array.
4068 Returns the number of symbols to keep. */
4071 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4072 asymbol
**syms
, long symcount
)
4074 long src_count
, dst_count
= 0;
4076 for (src_count
= 0; src_count
< symcount
; src_count
++)
4078 asymbol
*sym
= syms
[src_count
];
4079 char *name
= (char *) bfd_asymbol_name (sym
);
4080 struct bfd_link_hash_entry
*h
;
4082 if (!sym_is_global (abfd
, sym
))
4085 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4088 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4090 if (h
->linker_def
|| h
->ldscript_def
)
4093 syms
[dst_count
++] = sym
;
4096 syms
[dst_count
] = NULL
;
4101 /* Don't output section symbols for sections that are not going to be
4102 output, that are duplicates or there is no BFD section. */
4105 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4107 elf_symbol_type
*type_ptr
;
4112 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4115 if (sym
->section
== NULL
)
4118 type_ptr
= elf_symbol_from (abfd
, sym
);
4119 return ((type_ptr
!= NULL
4120 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4121 && bfd_is_abs_section (sym
->section
))
4122 || !(sym
->section
->owner
== abfd
4123 || (sym
->section
->output_section
!= NULL
4124 && sym
->section
->output_section
->owner
== abfd
4125 && sym
->section
->output_offset
== 0)
4126 || bfd_is_abs_section (sym
->section
)));
4129 /* Map symbol from it's internal number to the external number, moving
4130 all local symbols to be at the head of the list. */
4133 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4135 unsigned int symcount
= bfd_get_symcount (abfd
);
4136 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4137 asymbol
**sect_syms
;
4138 unsigned int num_locals
= 0;
4139 unsigned int num_globals
= 0;
4140 unsigned int num_locals2
= 0;
4141 unsigned int num_globals2
= 0;
4142 unsigned int max_index
= 0;
4148 fprintf (stderr
, "elf_map_symbols\n");
4152 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4154 if (max_index
< asect
->index
)
4155 max_index
= asect
->index
;
4159 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
4160 if (sect_syms
== NULL
)
4162 elf_section_syms (abfd
) = sect_syms
;
4163 elf_num_section_syms (abfd
) = max_index
;
4165 /* Init sect_syms entries for any section symbols we have already
4166 decided to output. */
4167 for (idx
= 0; idx
< symcount
; idx
++)
4169 asymbol
*sym
= syms
[idx
];
4171 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4173 && !ignore_section_sym (abfd
, sym
)
4174 && !bfd_is_abs_section (sym
->section
))
4176 asection
*sec
= sym
->section
;
4178 if (sec
->owner
!= abfd
)
4179 sec
= sec
->output_section
;
4181 sect_syms
[sec
->index
] = syms
[idx
];
4185 /* Classify all of the symbols. */
4186 for (idx
= 0; idx
< symcount
; idx
++)
4188 if (sym_is_global (abfd
, syms
[idx
]))
4190 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4194 /* We will be adding a section symbol for each normal BFD section. Most
4195 sections will already have a section symbol in outsymbols, but
4196 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4197 at least in that case. */
4198 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4200 if (sect_syms
[asect
->index
] == NULL
)
4202 if (!sym_is_global (abfd
, asect
->symbol
))
4209 /* Now sort the symbols so the local symbols are first. */
4210 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4211 sizeof (asymbol
*));
4213 if (new_syms
== NULL
)
4216 for (idx
= 0; idx
< symcount
; idx
++)
4218 asymbol
*sym
= syms
[idx
];
4221 if (sym_is_global (abfd
, sym
))
4222 i
= num_locals
+ num_globals2
++;
4223 else if (!ignore_section_sym (abfd
, sym
))
4228 sym
->udata
.i
= i
+ 1;
4230 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4232 if (sect_syms
[asect
->index
] == NULL
)
4234 asymbol
*sym
= asect
->symbol
;
4237 sect_syms
[asect
->index
] = sym
;
4238 if (!sym_is_global (abfd
, sym
))
4241 i
= num_locals
+ num_globals2
++;
4243 sym
->udata
.i
= i
+ 1;
4247 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4249 *pnum_locals
= num_locals
;
4253 /* Align to the maximum file alignment that could be required for any
4254 ELF data structure. */
4256 static inline file_ptr
4257 align_file_position (file_ptr off
, int align
)
4259 return (off
+ align
- 1) & ~(align
- 1);
4262 /* Assign a file position to a section, optionally aligning to the
4263 required section alignment. */
4266 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4270 if (align
&& i_shdrp
->sh_addralign
> 1)
4271 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4272 i_shdrp
->sh_offset
= offset
;
4273 if (i_shdrp
->bfd_section
!= NULL
)
4274 i_shdrp
->bfd_section
->filepos
= offset
;
4275 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4276 offset
+= i_shdrp
->sh_size
;
4280 /* Compute the file positions we are going to put the sections at, and
4281 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4282 is not NULL, this is being called by the ELF backend linker. */
4285 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4286 struct bfd_link_info
*link_info
)
4288 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4289 struct fake_section_arg fsargs
;
4291 struct elf_strtab_hash
*strtab
= NULL
;
4292 Elf_Internal_Shdr
*shstrtab_hdr
;
4293 bfd_boolean need_symtab
;
4295 if (abfd
->output_has_begun
)
4298 /* Do any elf backend specific processing first. */
4299 if (bed
->elf_backend_begin_write_processing
)
4300 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4302 if (! prep_headers (abfd
))
4305 /* Post process the headers if necessary. */
4306 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4308 fsargs
.failed
= FALSE
;
4309 fsargs
.link_info
= link_info
;
4310 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4314 if (!assign_section_numbers (abfd
, link_info
))
4317 /* The backend linker builds symbol table information itself. */
4318 need_symtab
= (link_info
== NULL
4319 && (bfd_get_symcount (abfd
) > 0
4320 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4324 /* Non-zero if doing a relocatable link. */
4325 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4327 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4332 if (link_info
== NULL
)
4334 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4339 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4340 /* sh_name was set in prep_headers. */
4341 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4342 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4343 shstrtab_hdr
->sh_addr
= 0;
4344 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4345 shstrtab_hdr
->sh_entsize
= 0;
4346 shstrtab_hdr
->sh_link
= 0;
4347 shstrtab_hdr
->sh_info
= 0;
4348 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4349 shstrtab_hdr
->sh_addralign
= 1;
4351 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4357 Elf_Internal_Shdr
*hdr
;
4359 off
= elf_next_file_pos (abfd
);
4361 hdr
= & elf_symtab_hdr (abfd
);
4362 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4364 if (elf_symtab_shndx_list (abfd
) != NULL
)
4366 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4367 if (hdr
->sh_size
!= 0)
4368 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4369 /* FIXME: What about other symtab_shndx sections in the list ? */
4372 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4373 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4375 elf_next_file_pos (abfd
) = off
;
4377 /* Now that we know where the .strtab section goes, write it
4379 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4380 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4382 _bfd_elf_strtab_free (strtab
);
4385 abfd
->output_has_begun
= TRUE
;
4390 /* Make an initial estimate of the size of the program header. If we
4391 get the number wrong here, we'll redo section placement. */
4393 static bfd_size_type
4394 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4398 const struct elf_backend_data
*bed
;
4400 /* Assume we will need exactly two PT_LOAD segments: one for text
4401 and one for data. */
4404 s
= bfd_get_section_by_name (abfd
, ".interp");
4405 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4407 /* If we have a loadable interpreter section, we need a
4408 PT_INTERP segment. In this case, assume we also need a
4409 PT_PHDR segment, although that may not be true for all
4414 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4416 /* We need a PT_DYNAMIC segment. */
4420 if (info
!= NULL
&& info
->relro
)
4422 /* We need a PT_GNU_RELRO segment. */
4426 if (elf_eh_frame_hdr (abfd
))
4428 /* We need a PT_GNU_EH_FRAME segment. */
4432 if (elf_stack_flags (abfd
))
4434 /* We need a PT_GNU_STACK segment. */
4438 s
= bfd_get_section_by_name (abfd
,
4439 NOTE_GNU_PROPERTY_SECTION_NAME
);
4440 if (s
!= NULL
&& s
->size
!= 0)
4442 /* We need a PT_GNU_PROPERTY segment. */
4446 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4448 if ((s
->flags
& SEC_LOAD
) != 0
4449 && elf_section_type (s
) == SHT_NOTE
)
4451 unsigned int alignment_power
;
4452 /* We need a PT_NOTE segment. */
4454 /* Try to create just one PT_NOTE segment for all adjacent
4455 loadable SHT_NOTE sections. gABI requires that within a
4456 PT_NOTE segment (and also inside of each SHT_NOTE section)
4457 each note should have the same alignment. So we check
4458 whether the sections are correctly aligned. */
4459 alignment_power
= s
->alignment_power
;
4460 while (s
->next
!= NULL
4461 && s
->next
->alignment_power
== alignment_power
4462 && (s
->next
->flags
& SEC_LOAD
) != 0
4463 && elf_section_type (s
->next
) == SHT_NOTE
)
4468 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4470 if (s
->flags
& SEC_THREAD_LOCAL
)
4472 /* We need a PT_TLS segment. */
4478 bed
= get_elf_backend_data (abfd
);
4480 if ((abfd
->flags
& D_PAGED
) != 0
4481 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4483 /* Add a PT_GNU_MBIND segment for each mbind section. */
4484 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4485 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4486 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4488 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4491 /* xgettext:c-format */
4492 (_("%pB: GNU_MBIND section `%pA' has invalid "
4493 "sh_info field: %d"),
4494 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4497 /* Align mbind section to page size. */
4498 if (s
->alignment_power
< page_align_power
)
4499 s
->alignment_power
= page_align_power
;
4504 /* Let the backend count up any program headers it might need. */
4505 if (bed
->elf_backend_additional_program_headers
)
4509 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4515 return segs
* bed
->s
->sizeof_phdr
;
4518 /* Find the segment that contains the output_section of section. */
4521 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4523 struct elf_segment_map
*m
;
4524 Elf_Internal_Phdr
*p
;
4526 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4532 for (i
= m
->count
- 1; i
>= 0; i
--)
4533 if (m
->sections
[i
] == section
)
4540 /* Create a mapping from a set of sections to a program segment. */
4542 static struct elf_segment_map
*
4543 make_mapping (bfd
*abfd
,
4544 asection
**sections
,
4549 struct elf_segment_map
*m
;
4554 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4555 amt
+= (to
- from
) * sizeof (asection
*);
4556 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4560 m
->p_type
= PT_LOAD
;
4561 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4562 m
->sections
[i
- from
] = *hdrpp
;
4563 m
->count
= to
- from
;
4565 if (from
== 0 && phdr
)
4567 /* Include the headers in the first PT_LOAD segment. */
4568 m
->includes_filehdr
= 1;
4569 m
->includes_phdrs
= 1;
4575 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4578 struct elf_segment_map
*
4579 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4581 struct elf_segment_map
*m
;
4583 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4584 sizeof (struct elf_segment_map
));
4588 m
->p_type
= PT_DYNAMIC
;
4590 m
->sections
[0] = dynsec
;
4595 /* Possibly add or remove segments from the segment map. */
4598 elf_modify_segment_map (bfd
*abfd
,
4599 struct bfd_link_info
*info
,
4600 bfd_boolean remove_empty_load
)
4602 struct elf_segment_map
**m
;
4603 const struct elf_backend_data
*bed
;
4605 /* The placement algorithm assumes that non allocated sections are
4606 not in PT_LOAD segments. We ensure this here by removing such
4607 sections from the segment map. We also remove excluded
4608 sections. Finally, any PT_LOAD segment without sections is
4610 m
= &elf_seg_map (abfd
);
4613 unsigned int i
, new_count
;
4615 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4617 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4618 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4619 || (*m
)->p_type
!= PT_LOAD
))
4621 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4625 (*m
)->count
= new_count
;
4627 if (remove_empty_load
4628 && (*m
)->p_type
== PT_LOAD
4630 && !(*m
)->includes_phdrs
)
4636 bed
= get_elf_backend_data (abfd
);
4637 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4639 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4646 #define IS_TBSS(s) \
4647 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4649 /* Set up a mapping from BFD sections to program segments. */
4652 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4655 struct elf_segment_map
*m
;
4656 asection
**sections
= NULL
;
4657 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4658 bfd_boolean no_user_phdrs
;
4660 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4663 info
->user_phdrs
= !no_user_phdrs
;
4665 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4669 struct elf_segment_map
*mfirst
;
4670 struct elf_segment_map
**pm
;
4673 unsigned int hdr_index
;
4674 bfd_vma maxpagesize
;
4676 bfd_boolean phdr_in_segment
;
4677 bfd_boolean writable
;
4678 bfd_boolean executable
;
4680 asection
*first_tls
= NULL
;
4681 asection
*first_mbind
= NULL
;
4682 asection
*dynsec
, *eh_frame_hdr
;
4684 bfd_vma addr_mask
, wrap_to
= 0;
4685 bfd_size_type phdr_size
;
4687 /* Select the allocated sections, and sort them. */
4689 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4690 sizeof (asection
*));
4691 if (sections
== NULL
)
4694 /* Calculate top address, avoiding undefined behaviour of shift
4695 left operator when shift count is equal to size of type
4697 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4698 addr_mask
= (addr_mask
<< 1) + 1;
4701 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4703 if ((s
->flags
& SEC_ALLOC
) != 0)
4707 /* A wrapping section potentially clashes with header. */
4708 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4709 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4712 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4715 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4717 phdr_size
= elf_program_header_size (abfd
);
4718 if (phdr_size
== (bfd_size_type
) -1)
4719 phdr_size
= get_program_header_size (abfd
, info
);
4720 phdr_size
+= bed
->s
->sizeof_ehdr
;
4721 maxpagesize
= bed
->maxpagesize
;
4722 if (maxpagesize
== 0)
4724 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4726 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4727 >= (phdr_size
& (maxpagesize
- 1))))
4728 /* For compatibility with old scripts that may not be using
4729 SIZEOF_HEADERS, add headers when it looks like space has
4730 been left for them. */
4731 phdr_in_segment
= TRUE
;
4733 /* Build the mapping. */
4737 /* If we have a .interp section, then create a PT_PHDR segment for
4738 the program headers and a PT_INTERP segment for the .interp
4740 s
= bfd_get_section_by_name (abfd
, ".interp");
4741 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4743 amt
= sizeof (struct elf_segment_map
);
4744 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4748 m
->p_type
= PT_PHDR
;
4750 m
->p_flags_valid
= 1;
4751 m
->includes_phdrs
= 1;
4752 phdr_in_segment
= TRUE
;
4756 amt
= sizeof (struct elf_segment_map
);
4757 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4761 m
->p_type
= PT_INTERP
;
4769 /* Look through the sections. We put sections in the same program
4770 segment when the start of the second section can be placed within
4771 a few bytes of the end of the first section. */
4777 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4779 && (dynsec
->flags
& SEC_LOAD
) == 0)
4782 if ((abfd
->flags
& D_PAGED
) == 0)
4783 phdr_in_segment
= FALSE
;
4785 /* Deal with -Ttext or something similar such that the first section
4786 is not adjacent to the program headers. This is an
4787 approximation, since at this point we don't know exactly how many
4788 program headers we will need. */
4789 if (phdr_in_segment
&& count
> 0)
4792 bfd_boolean separate_phdr
= FALSE
;
4794 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4796 && info
->separate_code
4797 && (sections
[0]->flags
& SEC_CODE
) != 0)
4799 /* If data sections should be separate from code and
4800 thus not executable, and the first section is
4801 executable then put the file and program headers in
4802 their own PT_LOAD. */
4803 separate_phdr
= TRUE
;
4804 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4805 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4807 /* The file and program headers are currently on the
4808 same page as the first section. Put them on the
4809 previous page if we can. */
4810 if (phdr_lma
>= maxpagesize
)
4811 phdr_lma
-= maxpagesize
;
4813 separate_phdr
= FALSE
;
4816 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4817 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4818 /* If file and program headers would be placed at the end
4819 of memory then it's probably better to omit them. */
4820 phdr_in_segment
= FALSE
;
4821 else if (phdr_lma
< wrap_to
)
4822 /* If a section wraps around to where we'll be placing
4823 file and program headers, then the headers will be
4825 phdr_in_segment
= FALSE
;
4826 else if (separate_phdr
)
4828 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4831 m
->p_paddr
= phdr_lma
;
4833 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4834 m
->p_paddr_valid
= 1;
4837 phdr_in_segment
= FALSE
;
4841 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4844 bfd_boolean new_segment
;
4848 /* See if this section and the last one will fit in the same
4851 if (last_hdr
== NULL
)
4853 /* If we don't have a segment yet, then we don't need a new
4854 one (we build the last one after this loop). */
4855 new_segment
= FALSE
;
4857 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4859 /* If this section has a different relation between the
4860 virtual address and the load address, then we need a new
4864 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4865 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4867 /* If this section has a load address that makes it overlap
4868 the previous section, then we need a new segment. */
4871 else if ((abfd
->flags
& D_PAGED
) != 0
4872 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4873 == (hdr
->lma
& -maxpagesize
)))
4875 /* If we are demand paged then we can't map two disk
4876 pages onto the same memory page. */
4877 new_segment
= FALSE
;
4879 /* In the next test we have to be careful when last_hdr->lma is close
4880 to the end of the address space. If the aligned address wraps
4881 around to the start of the address space, then there are no more
4882 pages left in memory and it is OK to assume that the current
4883 section can be included in the current segment. */
4884 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4885 + maxpagesize
> last_hdr
->lma
)
4886 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4887 + maxpagesize
<= hdr
->lma
))
4889 /* If putting this section in this segment would force us to
4890 skip a page in the segment, then we need a new segment. */
4893 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4894 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4896 /* We don't want to put a loaded section after a
4897 nonloaded (ie. bss style) section in the same segment
4898 as that will force the non-loaded section to be loaded.
4899 Consider .tbss sections as loaded for this purpose. */
4902 else if ((abfd
->flags
& D_PAGED
) == 0)
4904 /* If the file is not demand paged, which means that we
4905 don't require the sections to be correctly aligned in the
4906 file, then there is no other reason for a new segment. */
4907 new_segment
= FALSE
;
4909 else if (info
!= NULL
4910 && info
->separate_code
4911 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4916 && (hdr
->flags
& SEC_READONLY
) == 0)
4918 /* We don't want to put a writable section in a read only
4924 /* Otherwise, we can use the same segment. */
4925 new_segment
= FALSE
;
4928 /* Allow interested parties a chance to override our decision. */
4929 if (last_hdr
!= NULL
4931 && info
->callbacks
->override_segment_assignment
!= NULL
)
4933 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4939 if ((hdr
->flags
& SEC_READONLY
) == 0)
4941 if ((hdr
->flags
& SEC_CODE
) != 0)
4944 /* .tbss sections effectively have zero size. */
4945 last_size
= !IS_TBSS (hdr
) ? hdr
->size
: 0;
4949 /* We need a new program segment. We must create a new program
4950 header holding all the sections from hdr_index until hdr. */
4952 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4959 if ((hdr
->flags
& SEC_READONLY
) == 0)
4964 if ((hdr
->flags
& SEC_CODE
) == 0)
4970 /* .tbss sections effectively have zero size. */
4971 last_size
= !IS_TBSS (hdr
) ? hdr
->size
: 0;
4973 phdr_in_segment
= FALSE
;
4976 /* Create a final PT_LOAD program segment, but not if it's just
4978 if (last_hdr
!= NULL
4979 && (i
- hdr_index
!= 1
4980 || !IS_TBSS (last_hdr
)))
4982 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4990 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4993 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5000 /* For each batch of consecutive loadable SHT_NOTE sections,
5001 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5002 because if we link together nonloadable .note sections and
5003 loadable .note sections, we will generate two .note sections
5004 in the output file. */
5005 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5007 if ((s
->flags
& SEC_LOAD
) != 0
5008 && elf_section_type (s
) == SHT_NOTE
)
5011 unsigned int alignment_power
= s
->alignment_power
;
5014 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5016 if (s2
->next
->alignment_power
== alignment_power
5017 && (s2
->next
->flags
& SEC_LOAD
) != 0
5018 && elf_section_type (s2
->next
) == SHT_NOTE
5019 && align_power (s2
->lma
+ s2
->size
,
5026 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5027 amt
+= count
* sizeof (asection
*);
5028 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5032 m
->p_type
= PT_NOTE
;
5036 m
->sections
[m
->count
- count
--] = s
;
5037 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5040 m
->sections
[m
->count
- 1] = s
;
5041 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5045 if (s
->flags
& SEC_THREAD_LOCAL
)
5051 if (first_mbind
== NULL
5052 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5056 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5059 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5060 amt
+= tls_count
* sizeof (asection
*);
5061 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5066 m
->count
= tls_count
;
5067 /* Mandated PF_R. */
5069 m
->p_flags_valid
= 1;
5071 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
5073 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5076 (_("%pB: TLS sections are not adjacent:"), abfd
);
5079 while (i
< (unsigned int) tls_count
)
5081 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5083 _bfd_error_handler (_(" TLS: %pA"), s
);
5087 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5090 bfd_set_error (bfd_error_bad_value
);
5102 && (abfd
->flags
& D_PAGED
) != 0
5103 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5104 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5105 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5106 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5108 /* Mandated PF_R. */
5109 unsigned long p_flags
= PF_R
;
5110 if ((s
->flags
& SEC_READONLY
) == 0)
5112 if ((s
->flags
& SEC_CODE
) != 0)
5115 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5116 m
= bfd_zalloc (abfd
, amt
);
5120 m
->p_type
= (PT_GNU_MBIND_LO
5121 + elf_section_data (s
)->this_hdr
.sh_info
);
5123 m
->p_flags_valid
= 1;
5125 m
->p_flags
= p_flags
;
5131 s
= bfd_get_section_by_name (abfd
,
5132 NOTE_GNU_PROPERTY_SECTION_NAME
);
5133 if (s
!= NULL
&& s
->size
!= 0)
5135 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5136 m
= bfd_zalloc (abfd
, amt
);
5140 m
->p_type
= PT_GNU_PROPERTY
;
5142 m
->p_flags_valid
= 1;
5149 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5151 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5152 if (eh_frame_hdr
!= NULL
5153 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5155 amt
= sizeof (struct elf_segment_map
);
5156 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5160 m
->p_type
= PT_GNU_EH_FRAME
;
5162 m
->sections
[0] = eh_frame_hdr
->output_section
;
5168 if (elf_stack_flags (abfd
))
5170 amt
= sizeof (struct elf_segment_map
);
5171 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5175 m
->p_type
= PT_GNU_STACK
;
5176 m
->p_flags
= elf_stack_flags (abfd
);
5177 m
->p_align
= bed
->stack_align
;
5178 m
->p_flags_valid
= 1;
5179 m
->p_align_valid
= m
->p_align
!= 0;
5180 if (info
->stacksize
> 0)
5182 m
->p_size
= info
->stacksize
;
5183 m
->p_size_valid
= 1;
5190 if (info
!= NULL
&& info
->relro
)
5192 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5194 if (m
->p_type
== PT_LOAD
5196 && m
->sections
[0]->vma
>= info
->relro_start
5197 && m
->sections
[0]->vma
< info
->relro_end
)
5200 while (--i
!= (unsigned) -1)
5201 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5202 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5205 if (i
!= (unsigned) -1)
5210 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5213 amt
= sizeof (struct elf_segment_map
);
5214 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5218 m
->p_type
= PT_GNU_RELRO
;
5225 elf_seg_map (abfd
) = mfirst
;
5228 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5231 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5233 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5238 if (sections
!= NULL
)
5243 /* Sort sections by address. */
5246 elf_sort_sections (const void *arg1
, const void *arg2
)
5248 const asection
*sec1
= *(const asection
**) arg1
;
5249 const asection
*sec2
= *(const asection
**) arg2
;
5250 bfd_size_type size1
, size2
;
5252 /* Sort by LMA first, since this is the address used to
5253 place the section into a segment. */
5254 if (sec1
->lma
< sec2
->lma
)
5256 else if (sec1
->lma
> sec2
->lma
)
5259 /* Then sort by VMA. Normally the LMA and the VMA will be
5260 the same, and this will do nothing. */
5261 if (sec1
->vma
< sec2
->vma
)
5263 else if (sec1
->vma
> sec2
->vma
)
5266 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5268 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5274 /* If the indices are the same, do not return 0
5275 here, but continue to try the next comparison. */
5276 if (sec1
->target_index
- sec2
->target_index
!= 0)
5277 return sec1
->target_index
- sec2
->target_index
;
5282 else if (TOEND (sec2
))
5287 /* Sort by size, to put zero sized sections
5288 before others at the same address. */
5290 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5291 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5298 return sec1
->target_index
- sec2
->target_index
;
5301 /* Ian Lance Taylor writes:
5303 We shouldn't be using % with a negative signed number. That's just
5304 not good. We have to make sure either that the number is not
5305 negative, or that the number has an unsigned type. When the types
5306 are all the same size they wind up as unsigned. When file_ptr is a
5307 larger signed type, the arithmetic winds up as signed long long,
5310 What we're trying to say here is something like ``increase OFF by
5311 the least amount that will cause it to be equal to the VMA modulo
5313 /* In other words, something like:
5315 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5316 off_offset = off % bed->maxpagesize;
5317 if (vma_offset < off_offset)
5318 adjustment = vma_offset + bed->maxpagesize - off_offset;
5320 adjustment = vma_offset - off_offset;
5322 which can be collapsed into the expression below. */
5325 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5327 /* PR binutils/16199: Handle an alignment of zero. */
5328 if (maxpagesize
== 0)
5330 return ((vma
- off
) % maxpagesize
);
5334 print_segment_map (const struct elf_segment_map
*m
)
5337 const char *pt
= get_segment_type (m
->p_type
);
5342 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5343 sprintf (buf
, "LOPROC+%7.7x",
5344 (unsigned int) (m
->p_type
- PT_LOPROC
));
5345 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5346 sprintf (buf
, "LOOS+%7.7x",
5347 (unsigned int) (m
->p_type
- PT_LOOS
));
5349 snprintf (buf
, sizeof (buf
), "%8.8x",
5350 (unsigned int) m
->p_type
);
5354 fprintf (stderr
, "%s:", pt
);
5355 for (j
= 0; j
< m
->count
; j
++)
5356 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5362 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5367 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5369 buf
= bfd_zmalloc (len
);
5372 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5377 /* Assign file positions to the sections based on the mapping from
5378 sections to segments. This function also sets up some fields in
5382 assign_file_positions_for_load_sections (bfd
*abfd
,
5383 struct bfd_link_info
*link_info
)
5385 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5386 struct elf_segment_map
*m
;
5387 Elf_Internal_Phdr
*phdrs
;
5388 Elf_Internal_Phdr
*p
;
5390 bfd_size_type maxpagesize
;
5391 unsigned int pt_load_count
= 0;
5394 bfd_vma header_pad
= 0;
5396 if (link_info
== NULL
5397 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5401 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5405 header_pad
= m
->header_size
;
5410 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5411 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5415 /* PR binutils/12467. */
5416 elf_elfheader (abfd
)->e_phoff
= 0;
5417 elf_elfheader (abfd
)->e_phentsize
= 0;
5420 elf_elfheader (abfd
)->e_phnum
= alloc
;
5422 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5423 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5425 BFD_ASSERT (elf_program_header_size (abfd
)
5426 >= alloc
* bed
->s
->sizeof_phdr
);
5430 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5434 /* We're writing the size in elf_program_header_size (abfd),
5435 see assign_file_positions_except_relocs, so make sure we have
5436 that amount allocated, with trailing space cleared.
5437 The variable alloc contains the computed need, while
5438 elf_program_header_size (abfd) contains the size used for the
5440 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5441 where the layout is forced to according to a larger size in the
5442 last iterations for the testcase ld-elf/header. */
5443 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5445 phdrs
= (Elf_Internal_Phdr
*)
5447 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5448 sizeof (Elf_Internal_Phdr
));
5449 elf_tdata (abfd
)->phdr
= phdrs
;
5454 if ((abfd
->flags
& D_PAGED
) != 0)
5455 maxpagesize
= bed
->maxpagesize
;
5457 off
= bed
->s
->sizeof_ehdr
;
5458 off
+= alloc
* bed
->s
->sizeof_phdr
;
5459 if (header_pad
< (bfd_vma
) off
)
5465 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5467 m
= m
->next
, p
++, j
++)
5471 bfd_boolean no_contents
;
5473 /* If elf_segment_map is not from map_sections_to_segments, the
5474 sections may not be correctly ordered. NOTE: sorting should
5475 not be done to the PT_NOTE section of a corefile, which may
5476 contain several pseudo-sections artificially created by bfd.
5477 Sorting these pseudo-sections breaks things badly. */
5479 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5480 && m
->p_type
== PT_NOTE
))
5481 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5484 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5485 number of sections with contents contributing to both p_filesz
5486 and p_memsz, followed by a number of sections with no contents
5487 that just contribute to p_memsz. In this loop, OFF tracks next
5488 available file offset for PT_LOAD and PT_NOTE segments. */
5489 p
->p_type
= m
->p_type
;
5490 p
->p_flags
= m
->p_flags
;
5493 p
->p_vaddr
= m
->p_vaddr_offset
;
5495 p
->p_vaddr
= m
->sections
[0]->vma
+ m
->p_vaddr_offset
;
5497 if (m
->p_paddr_valid
)
5498 p
->p_paddr
= m
->p_paddr
;
5499 else if (m
->count
== 0)
5502 p
->p_paddr
= m
->sections
[0]->lma
+ m
->p_vaddr_offset
;
5504 if (p
->p_type
== PT_LOAD
5505 && (abfd
->flags
& D_PAGED
) != 0)
5507 /* p_align in demand paged PT_LOAD segments effectively stores
5508 the maximum page size. When copying an executable with
5509 objcopy, we set m->p_align from the input file. Use this
5510 value for maxpagesize rather than bed->maxpagesize, which
5511 may be different. Note that we use maxpagesize for PT_TLS
5512 segment alignment later in this function, so we are relying
5513 on at least one PT_LOAD segment appearing before a PT_TLS
5515 if (m
->p_align_valid
)
5516 maxpagesize
= m
->p_align
;
5518 p
->p_align
= maxpagesize
;
5521 else if (m
->p_align_valid
)
5522 p
->p_align
= m
->p_align
;
5523 else if (m
->count
== 0)
5524 p
->p_align
= 1 << bed
->s
->log_file_align
;
5528 no_contents
= FALSE
;
5530 if (p
->p_type
== PT_LOAD
5533 bfd_size_type align
;
5534 unsigned int align_power
= 0;
5536 if (m
->p_align_valid
)
5540 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5542 unsigned int secalign
;
5544 secalign
= bfd_section_alignment (*secpp
);
5545 if (secalign
> align_power
)
5546 align_power
= secalign
;
5548 align
= (bfd_size_type
) 1 << align_power
;
5549 if (align
< maxpagesize
)
5550 align
= maxpagesize
;
5553 for (i
= 0; i
< m
->count
; i
++)
5554 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5555 /* If we aren't making room for this section, then
5556 it must be SHT_NOBITS regardless of what we've
5557 set via struct bfd_elf_special_section. */
5558 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5560 /* Find out whether this segment contains any loadable
5563 for (i
= 0; i
< m
->count
; i
++)
5564 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5566 no_contents
= FALSE
;
5570 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5572 /* Broken hardware and/or kernel require that files do not
5573 map the same page with different permissions on some hppa
5575 if (pt_load_count
> 1
5576 && bed
->no_page_alias
5577 && (off
& (maxpagesize
- 1)) != 0
5578 && (off
& -maxpagesize
) == ((off
+ off_adjust
) & -maxpagesize
))
5579 off_adjust
+= maxpagesize
;
5583 /* We shouldn't need to align the segment on disk since
5584 the segment doesn't need file space, but the gABI
5585 arguably requires the alignment and glibc ld.so
5586 checks it. So to comply with the alignment
5587 requirement but not waste file space, we adjust
5588 p_offset for just this segment. (OFF_ADJUST is
5589 subtracted from OFF later.) This may put p_offset
5590 past the end of file, but that shouldn't matter. */
5595 /* Make sure the .dynamic section is the first section in the
5596 PT_DYNAMIC segment. */
5597 else if (p
->p_type
== PT_DYNAMIC
5599 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5602 (_("%pB: The first section in the PT_DYNAMIC segment"
5603 " is not the .dynamic section"),
5605 bfd_set_error (bfd_error_bad_value
);
5608 /* Set the note section type to SHT_NOTE. */
5609 else if (p
->p_type
== PT_NOTE
)
5610 for (i
= 0; i
< m
->count
; i
++)
5611 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5617 if (m
->includes_filehdr
)
5619 if (!m
->p_flags_valid
)
5621 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5622 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5625 if (p
->p_vaddr
< (bfd_vma
) off
5626 || (!m
->p_paddr_valid
5627 && p
->p_paddr
< (bfd_vma
) off
))
5630 (_("%pB: not enough room for program headers,"
5631 " try linking with -N"),
5633 bfd_set_error (bfd_error_bad_value
);
5638 if (!m
->p_paddr_valid
)
5643 if (m
->includes_phdrs
)
5645 if (!m
->p_flags_valid
)
5648 if (!m
->includes_filehdr
)
5650 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5654 p
->p_vaddr
-= off
- p
->p_offset
;
5655 if (!m
->p_paddr_valid
)
5656 p
->p_paddr
-= off
- p
->p_offset
;
5660 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5661 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5664 p
->p_filesz
+= header_pad
;
5665 p
->p_memsz
+= header_pad
;
5669 if (p
->p_type
== PT_LOAD
5670 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5672 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5678 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5680 p
->p_filesz
+= adjust
;
5681 p
->p_memsz
+= adjust
;
5685 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5686 maps. Set filepos for sections in PT_LOAD segments, and in
5687 core files, for sections in PT_NOTE segments.
5688 assign_file_positions_for_non_load_sections will set filepos
5689 for other sections and update p_filesz for other segments. */
5690 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5693 bfd_size_type align
;
5694 Elf_Internal_Shdr
*this_hdr
;
5697 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5698 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5700 if ((p
->p_type
== PT_LOAD
5701 || p
->p_type
== PT_TLS
)
5702 && (this_hdr
->sh_type
!= SHT_NOBITS
5703 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5704 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5705 || p
->p_type
== PT_TLS
))))
5707 bfd_vma p_start
= p
->p_paddr
;
5708 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5709 bfd_vma s_start
= sec
->lma
;
5710 bfd_vma adjust
= s_start
- p_end
;
5714 || p_end
< p_start
))
5717 /* xgettext:c-format */
5718 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5719 abfd
, sec
, (uint64_t) s_start
, (uint64_t) p_end
);
5723 p
->p_memsz
+= adjust
;
5725 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5727 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5729 /* We have a PROGBITS section following NOBITS ones.
5730 Allocate file space for the NOBITS section(s) and
5732 adjust
= p
->p_memsz
- p
->p_filesz
;
5733 if (!write_zeros (abfd
, off
, adjust
))
5737 p
->p_filesz
+= adjust
;
5741 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5743 /* The section at i == 0 is the one that actually contains
5747 this_hdr
->sh_offset
= sec
->filepos
= off
;
5748 off
+= this_hdr
->sh_size
;
5749 p
->p_filesz
= this_hdr
->sh_size
;
5755 /* The rest are fake sections that shouldn't be written. */
5764 if (p
->p_type
== PT_LOAD
)
5766 this_hdr
->sh_offset
= sec
->filepos
= off
;
5767 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5768 off
+= this_hdr
->sh_size
;
5770 else if (this_hdr
->sh_type
== SHT_NOBITS
5771 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5772 && this_hdr
->sh_offset
== 0)
5774 /* This is a .tbss section that didn't get a PT_LOAD.
5775 (See _bfd_elf_map_sections_to_segments "Create a
5776 final PT_LOAD".) Set sh_offset to the value it
5777 would have if we had created a zero p_filesz and
5778 p_memsz PT_LOAD header for the section. This
5779 also makes the PT_TLS header have the same
5781 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5783 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5786 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5788 p
->p_filesz
+= this_hdr
->sh_size
;
5789 /* A load section without SHF_ALLOC is something like
5790 a note section in a PT_NOTE segment. These take
5791 file space but are not loaded into memory. */
5792 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5793 p
->p_memsz
+= this_hdr
->sh_size
;
5795 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5797 if (p
->p_type
== PT_TLS
)
5798 p
->p_memsz
+= this_hdr
->sh_size
;
5800 /* .tbss is special. It doesn't contribute to p_memsz of
5802 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5803 p
->p_memsz
+= this_hdr
->sh_size
;
5806 if (align
> p
->p_align
5807 && !m
->p_align_valid
5808 && (p
->p_type
!= PT_LOAD
5809 || (abfd
->flags
& D_PAGED
) == 0))
5813 if (!m
->p_flags_valid
)
5816 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5818 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5825 /* Check that all sections are in a PT_LOAD segment.
5826 Don't check funky gdb generated core files. */
5827 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5829 bfd_boolean check_vma
= TRUE
;
5831 for (i
= 1; i
< m
->count
; i
++)
5832 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5833 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5834 ->this_hdr
), p
) != 0
5835 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5836 ->this_hdr
), p
) != 0)
5838 /* Looks like we have overlays packed into the segment. */
5843 for (i
= 0; i
< m
->count
; i
++)
5845 Elf_Internal_Shdr
*this_hdr
;
5848 sec
= m
->sections
[i
];
5849 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5850 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5851 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5854 /* xgettext:c-format */
5855 (_("%pB: section `%pA' can't be allocated in segment %d"),
5857 print_segment_map (m
);
5863 elf_next_file_pos (abfd
) = off
;
5867 /* Determine if a bfd is a debuginfo file. Unfortunately there
5868 is no defined method for detecting such files, so we have to
5869 use heuristics instead. */
5872 is_debuginfo_file (bfd
*abfd
)
5874 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5877 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
5878 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
5879 Elf_Internal_Shdr
**headerp
;
5881 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
5883 Elf_Internal_Shdr
*header
= * headerp
;
5885 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
5886 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
5887 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
5888 && header
->sh_type
!= SHT_NOBITS
5889 && header
->sh_type
!= SHT_NOTE
)
5896 /* Assign file positions for the other sections. */
5899 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5900 struct bfd_link_info
*link_info
)
5902 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5903 Elf_Internal_Shdr
**i_shdrpp
;
5904 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5905 Elf_Internal_Phdr
*phdrs
;
5906 Elf_Internal_Phdr
*p
;
5907 struct elf_segment_map
*m
;
5908 struct elf_segment_map
*hdrs_segment
;
5909 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5910 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5914 i_shdrpp
= elf_elfsections (abfd
);
5915 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5916 off
= elf_next_file_pos (abfd
);
5917 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5919 Elf_Internal_Shdr
*hdr
;
5922 if (hdr
->bfd_section
!= NULL
5923 && (hdr
->bfd_section
->filepos
!= 0
5924 || (hdr
->sh_type
== SHT_NOBITS
5925 && hdr
->contents
== NULL
)))
5926 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5927 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5929 if (hdr
->sh_size
!= 0
5930 /* PR 24717 - debuginfo files are known to be not strictly
5931 compliant with the ELF standard. In particular they often
5932 have .note.gnu.property sections that are outside of any
5933 loadable segment. This is not a problem for such files,
5934 so do not warn about them. */
5935 && ! is_debuginfo_file (abfd
))
5937 /* xgettext:c-format */
5938 (_("%pB: warning: allocated section `%s' not in segment"),
5940 (hdr
->bfd_section
== NULL
5942 : hdr
->bfd_section
->name
));
5943 /* We don't need to page align empty sections. */
5944 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5945 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5948 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5950 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5953 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5954 && hdr
->bfd_section
== NULL
)
5955 || (hdr
->bfd_section
!= NULL
5956 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5957 /* Compress DWARF debug sections. */
5958 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5959 || (elf_symtab_shndx_list (abfd
) != NULL
5960 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5961 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5962 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5963 hdr
->sh_offset
= -1;
5965 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5968 /* Now that we have set the section file positions, we can set up
5969 the file positions for the non PT_LOAD segments. */
5973 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5975 hdrs_segment
= NULL
;
5976 phdrs
= elf_tdata (abfd
)->phdr
;
5977 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5980 if (p
->p_type
!= PT_LOAD
)
5983 if (m
->includes_filehdr
)
5985 filehdr_vaddr
= p
->p_vaddr
;
5986 filehdr_paddr
= p
->p_paddr
;
5988 if (m
->includes_phdrs
)
5990 phdrs_vaddr
= p
->p_vaddr
;
5991 phdrs_paddr
= p
->p_paddr
;
5992 if (m
->includes_filehdr
)
5995 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5996 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
6001 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
6003 /* There is a segment that contains both the file headers and the
6004 program headers, so provide a symbol __ehdr_start pointing there.
6005 A program can use this to examine itself robustly. */
6007 struct elf_link_hash_entry
*hash
6008 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6009 FALSE
, FALSE
, TRUE
);
6010 /* If the symbol was referenced and not defined, define it. */
6012 && (hash
->root
.type
== bfd_link_hash_new
6013 || hash
->root
.type
== bfd_link_hash_undefined
6014 || hash
->root
.type
== bfd_link_hash_undefweak
6015 || hash
->root
.type
== bfd_link_hash_common
))
6018 if (hdrs_segment
->count
!= 0)
6019 /* The segment contains sections, so use the first one. */
6020 s
= hdrs_segment
->sections
[0];
6022 /* Use the first (i.e. lowest-addressed) section in any segment. */
6023 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6032 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6033 hash
->root
.u
.def
.section
= s
;
6037 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6038 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6041 hash
->root
.type
= bfd_link_hash_defined
;
6042 hash
->def_regular
= 1;
6047 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6049 if (p
->p_type
== PT_GNU_RELRO
)
6054 if (link_info
!= NULL
)
6056 /* During linking the range of the RELRO segment is passed
6057 in link_info. Note that there may be padding between
6058 relro_start and the first RELRO section. */
6059 start
= link_info
->relro_start
;
6060 end
= link_info
->relro_end
;
6062 else if (m
->count
!= 0)
6064 if (!m
->p_size_valid
)
6066 start
= m
->sections
[0]->vma
;
6067 end
= start
+ m
->p_size
;
6078 struct elf_segment_map
*lm
;
6079 const Elf_Internal_Phdr
*lp
;
6082 /* Find a LOAD segment containing a section in the RELRO
6084 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6086 lm
= lm
->next
, lp
++)
6088 if (lp
->p_type
== PT_LOAD
6090 && (lm
->sections
[lm
->count
- 1]->vma
6091 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6092 ? lm
->sections
[lm
->count
- 1]->size
6094 && lm
->sections
[0]->vma
< end
)
6100 /* Find the section starting the RELRO segment. */
6101 for (i
= 0; i
< lm
->count
; i
++)
6103 asection
*s
= lm
->sections
[i
];
6112 p
->p_vaddr
= lm
->sections
[i
]->vma
;
6113 p
->p_paddr
= lm
->sections
[i
]->lma
;
6114 p
->p_offset
= lm
->sections
[i
]->filepos
;
6115 p
->p_memsz
= end
- p
->p_vaddr
;
6116 p
->p_filesz
= p
->p_memsz
;
6118 /* The RELRO segment typically ends a few bytes
6119 into .got.plt but other layouts are possible.
6120 In cases where the end does not match any
6121 loaded section (for instance is in file
6122 padding), trim p_filesz back to correspond to
6123 the end of loaded section contents. */
6124 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6125 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6127 /* Preserve the alignment and flags if they are
6128 valid. The gold linker generates RW/4 for
6129 the PT_GNU_RELRO section. It is better for
6130 objcopy/strip to honor these attributes
6131 otherwise gdb will choke when using separate
6133 if (!m
->p_align_valid
)
6135 if (!m
->p_flags_valid
)
6141 if (link_info
!= NULL
)
6144 memset (p
, 0, sizeof *p
);
6146 else if (p
->p_type
== PT_GNU_STACK
)
6148 if (m
->p_size_valid
)
6149 p
->p_memsz
= m
->p_size
;
6151 else if (m
->count
!= 0)
6155 if (p
->p_type
!= PT_LOAD
6156 && (p
->p_type
!= PT_NOTE
6157 || bfd_get_format (abfd
) != bfd_core
))
6159 /* A user specified segment layout may include a PHDR
6160 segment that overlaps with a LOAD segment... */
6161 if (p
->p_type
== PT_PHDR
)
6167 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6169 /* PR 17512: file: 2195325e. */
6171 (_("%pB: error: non-load segment %d includes file header "
6172 "and/or program header"),
6173 abfd
, (int) (p
- phdrs
));
6178 p
->p_offset
= m
->sections
[0]->filepos
;
6179 for (i
= m
->count
; i
-- != 0;)
6181 asection
*sect
= m
->sections
[i
];
6182 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6183 if (hdr
->sh_type
!= SHT_NOBITS
)
6185 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6192 else if (m
->includes_filehdr
)
6194 p
->p_vaddr
= filehdr_vaddr
;
6195 if (! m
->p_paddr_valid
)
6196 p
->p_paddr
= filehdr_paddr
;
6198 else if (m
->includes_phdrs
)
6200 p
->p_vaddr
= phdrs_vaddr
;
6201 if (! m
->p_paddr_valid
)
6202 p
->p_paddr
= phdrs_paddr
;
6206 elf_next_file_pos (abfd
) = off
;
6211 static elf_section_list
*
6212 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6214 for (;list
!= NULL
; list
= list
->next
)
6220 /* Work out the file positions of all the sections. This is called by
6221 _bfd_elf_compute_section_file_positions. All the section sizes and
6222 VMAs must be known before this is called.
6224 Reloc sections come in two flavours: Those processed specially as
6225 "side-channel" data attached to a section to which they apply, and
6226 those that bfd doesn't process as relocations. The latter sort are
6227 stored in a normal bfd section by bfd_section_from_shdr. We don't
6228 consider the former sort here, unless they form part of the loadable
6229 image. Reloc sections not assigned here will be handled later by
6230 assign_file_positions_for_relocs.
6232 We also don't set the positions of the .symtab and .strtab here. */
6235 assign_file_positions_except_relocs (bfd
*abfd
,
6236 struct bfd_link_info
*link_info
)
6238 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6239 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6240 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6242 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6243 && bfd_get_format (abfd
) != bfd_core
)
6245 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6246 unsigned int num_sec
= elf_numsections (abfd
);
6247 Elf_Internal_Shdr
**hdrpp
;
6251 /* Start after the ELF header. */
6252 off
= i_ehdrp
->e_ehsize
;
6254 /* We are not creating an executable, which means that we are
6255 not creating a program header, and that the actual order of
6256 the sections in the file is unimportant. */
6257 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6259 Elf_Internal_Shdr
*hdr
;
6262 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6263 && hdr
->bfd_section
== NULL
)
6264 || (hdr
->bfd_section
!= NULL
6265 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
6266 /* Compress DWARF debug sections. */
6267 || i
== elf_onesymtab (abfd
)
6268 || (elf_symtab_shndx_list (abfd
) != NULL
6269 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6270 || i
== elf_strtab_sec (abfd
)
6271 || i
== elf_shstrtab_sec (abfd
))
6273 hdr
->sh_offset
= -1;
6276 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6279 elf_next_file_pos (abfd
) = off
;
6285 /* Assign file positions for the loaded sections based on the
6286 assignment of sections to segments. */
6287 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6290 /* And for non-load sections. */
6291 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6294 if (bed
->elf_backend_modify_program_headers
!= NULL
)
6296 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
6300 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6301 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6303 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
6304 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
6305 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6307 /* Find the lowest p_vaddr in PT_LOAD segments. */
6308 bfd_vma p_vaddr
= (bfd_vma
) -1;
6309 for (; segment
< end_segment
; segment
++)
6310 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6311 p_vaddr
= segment
->p_vaddr
;
6313 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6314 segments is non-zero. */
6316 i_ehdrp
->e_type
= ET_EXEC
;
6319 /* Write out the program headers. */
6320 alloc
= elf_elfheader (abfd
)->e_phnum
;
6324 /* PR ld/20815 - Check that the program header segment, if present, will
6325 be loaded into memory. FIXME: The check below is not sufficient as
6326 really all PT_LOAD segments should be checked before issuing an error
6327 message. Plus the PHDR segment does not have to be the first segment
6328 in the program header table. But this version of the check should
6329 catch all real world use cases.
6331 FIXME: We used to have code here to sort the PT_LOAD segments into
6332 ascending order, as per the ELF spec. But this breaks some programs,
6333 including the Linux kernel. But really either the spec should be
6334 changed or the programs updated. */
6336 && tdata
->phdr
[0].p_type
== PT_PHDR
6337 && (bed
->elf_backend_allow_non_load_phdr
== NULL
6338 || !bed
->elf_backend_allow_non_load_phdr (abfd
, tdata
->phdr
,
6340 && tdata
->phdr
[1].p_type
== PT_LOAD
6341 && (tdata
->phdr
[1].p_vaddr
> tdata
->phdr
[0].p_vaddr
6342 || (tdata
->phdr
[1].p_vaddr
+ tdata
->phdr
[1].p_memsz
6343 < tdata
->phdr
[0].p_vaddr
+ tdata
->phdr
[0].p_memsz
)))
6345 /* The fix for this error is usually to edit the linker script being
6346 used and set up the program headers manually. Either that or
6347 leave room for the headers at the start of the SECTIONS. */
6348 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6349 " by LOAD segment"),
6354 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
6355 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6363 prep_headers (bfd
*abfd
)
6365 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6366 struct elf_strtab_hash
*shstrtab
;
6367 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6369 i_ehdrp
= elf_elfheader (abfd
);
6371 shstrtab
= _bfd_elf_strtab_init ();
6372 if (shstrtab
== NULL
)
6375 elf_shstrtab (abfd
) = shstrtab
;
6377 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6378 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6379 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6380 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6382 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6383 i_ehdrp
->e_ident
[EI_DATA
] =
6384 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6385 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6387 if ((abfd
->flags
& DYNAMIC
) != 0)
6388 i_ehdrp
->e_type
= ET_DYN
;
6389 else if ((abfd
->flags
& EXEC_P
) != 0)
6390 i_ehdrp
->e_type
= ET_EXEC
;
6391 else if (bfd_get_format (abfd
) == bfd_core
)
6392 i_ehdrp
->e_type
= ET_CORE
;
6394 i_ehdrp
->e_type
= ET_REL
;
6396 switch (bfd_get_arch (abfd
))
6398 case bfd_arch_unknown
:
6399 i_ehdrp
->e_machine
= EM_NONE
;
6402 /* There used to be a long list of cases here, each one setting
6403 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6404 in the corresponding bfd definition. To avoid duplication,
6405 the switch was removed. Machines that need special handling
6406 can generally do it in elf_backend_final_write_processing(),
6407 unless they need the information earlier than the final write.
6408 Such need can generally be supplied by replacing the tests for
6409 e_machine with the conditions used to determine it. */
6411 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6414 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6415 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6417 /* No program header, for now. */
6418 i_ehdrp
->e_phoff
= 0;
6419 i_ehdrp
->e_phentsize
= 0;
6420 i_ehdrp
->e_phnum
= 0;
6422 /* Each bfd section is section header entry. */
6423 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6424 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6426 /* If we're building an executable, we'll need a program header table. */
6427 if (abfd
->flags
& EXEC_P
)
6428 /* It all happens later. */
6432 i_ehdrp
->e_phentsize
= 0;
6433 i_ehdrp
->e_phoff
= 0;
6436 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6437 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6438 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6439 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6440 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6441 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6442 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6443 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6444 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6450 /* Assign file positions for all the reloc sections which are not part
6451 of the loadable file image, and the file position of section headers. */
6454 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6457 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6458 Elf_Internal_Shdr
*shdrp
;
6459 Elf_Internal_Ehdr
*i_ehdrp
;
6460 const struct elf_backend_data
*bed
;
6462 off
= elf_next_file_pos (abfd
);
6464 shdrpp
= elf_elfsections (abfd
);
6465 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6466 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6469 if (shdrp
->sh_offset
== -1)
6471 asection
*sec
= shdrp
->bfd_section
;
6472 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6473 || shdrp
->sh_type
== SHT_RELA
);
6475 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6479 const char *name
= sec
->name
;
6480 struct bfd_elf_section_data
*d
;
6482 /* Compress DWARF debug sections. */
6483 if (!bfd_compress_section (abfd
, sec
,
6487 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6488 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6490 /* If section is compressed with zlib-gnu, convert
6491 section name from .debug_* to .zdebug_*. */
6493 = convert_debug_to_zdebug (abfd
, name
);
6494 if (new_name
== NULL
)
6498 /* Add section name to section name section. */
6499 if (shdrp
->sh_name
!= (unsigned int) -1)
6502 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6504 d
= elf_section_data (sec
);
6506 /* Add reloc section name to section name section. */
6508 && !_bfd_elf_set_reloc_sh_name (abfd
,
6513 && !_bfd_elf_set_reloc_sh_name (abfd
,
6518 /* Update section size and contents. */
6519 shdrp
->sh_size
= sec
->size
;
6520 shdrp
->contents
= sec
->contents
;
6521 shdrp
->bfd_section
->contents
= NULL
;
6523 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6530 /* Place section name section after DWARF debug sections have been
6532 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6533 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6534 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6535 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6537 /* Place the section headers. */
6538 i_ehdrp
= elf_elfheader (abfd
);
6539 bed
= get_elf_backend_data (abfd
);
6540 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6541 i_ehdrp
->e_shoff
= off
;
6542 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6543 elf_next_file_pos (abfd
) = off
;
6549 _bfd_elf_write_object_contents (bfd
*abfd
)
6551 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6552 Elf_Internal_Shdr
**i_shdrp
;
6554 unsigned int count
, num_sec
;
6555 struct elf_obj_tdata
*t
;
6557 if (! abfd
->output_has_begun
6558 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6560 /* Do not rewrite ELF data when the BFD has been opened for update.
6561 abfd->output_has_begun was set to TRUE on opening, so creation of new
6562 sections, and modification of existing section sizes was restricted.
6563 This means the ELF header, program headers and section headers can't have
6565 If the contents of any sections has been modified, then those changes have
6566 already been written to the BFD. */
6567 else if (abfd
->direction
== both_direction
)
6569 BFD_ASSERT (abfd
->output_has_begun
);
6573 i_shdrp
= elf_elfsections (abfd
);
6576 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6580 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6583 /* After writing the headers, we need to write the sections too... */
6584 num_sec
= elf_numsections (abfd
);
6585 for (count
= 1; count
< num_sec
; count
++)
6587 i_shdrp
[count
]->sh_name
6588 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6589 i_shdrp
[count
]->sh_name
);
6590 if (bed
->elf_backend_section_processing
)
6591 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6593 if (i_shdrp
[count
]->contents
)
6595 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6597 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6598 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6603 /* Write out the section header names. */
6604 t
= elf_tdata (abfd
);
6605 if (elf_shstrtab (abfd
) != NULL
6606 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6607 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6610 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6613 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6616 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6617 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6618 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6624 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6626 /* Hopefully this can be done just like an object file. */
6627 return _bfd_elf_write_object_contents (abfd
);
6630 /* Given a section, search the header to find them. */
6633 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6635 const struct elf_backend_data
*bed
;
6636 unsigned int sec_index
;
6638 if (elf_section_data (asect
) != NULL
6639 && elf_section_data (asect
)->this_idx
!= 0)
6640 return elf_section_data (asect
)->this_idx
;
6642 if (bfd_is_abs_section (asect
))
6643 sec_index
= SHN_ABS
;
6644 else if (bfd_is_com_section (asect
))
6645 sec_index
= SHN_COMMON
;
6646 else if (bfd_is_und_section (asect
))
6647 sec_index
= SHN_UNDEF
;
6649 sec_index
= SHN_BAD
;
6651 bed
= get_elf_backend_data (abfd
);
6652 if (bed
->elf_backend_section_from_bfd_section
)
6654 int retval
= sec_index
;
6656 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6660 if (sec_index
== SHN_BAD
)
6661 bfd_set_error (bfd_error_nonrepresentable_section
);
6666 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6670 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6672 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6674 flagword flags
= asym_ptr
->flags
;
6676 /* When gas creates relocations against local labels, it creates its
6677 own symbol for the section, but does put the symbol into the
6678 symbol chain, so udata is 0. When the linker is generating
6679 relocatable output, this section symbol may be for one of the
6680 input sections rather than the output section. */
6681 if (asym_ptr
->udata
.i
== 0
6682 && (flags
& BSF_SECTION_SYM
)
6683 && asym_ptr
->section
)
6688 sec
= asym_ptr
->section
;
6689 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6690 sec
= sec
->output_section
;
6691 if (sec
->owner
== abfd
6692 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6693 && elf_section_syms (abfd
)[indx
] != NULL
)
6694 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6697 idx
= asym_ptr
->udata
.i
;
6701 /* This case can occur when using --strip-symbol on a symbol
6702 which is used in a relocation entry. */
6704 /* xgettext:c-format */
6705 (_("%pB: symbol `%s' required but not present"),
6706 abfd
, bfd_asymbol_name (asym_ptr
));
6707 bfd_set_error (bfd_error_no_symbols
);
6714 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6715 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6723 /* Rewrite program header information. */
6726 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6728 Elf_Internal_Ehdr
*iehdr
;
6729 struct elf_segment_map
*map
;
6730 struct elf_segment_map
*map_first
;
6731 struct elf_segment_map
**pointer_to_map
;
6732 Elf_Internal_Phdr
*segment
;
6735 unsigned int num_segments
;
6736 bfd_boolean phdr_included
= FALSE
;
6737 bfd_boolean p_paddr_valid
;
6738 bfd_vma maxpagesize
;
6739 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6740 unsigned int phdr_adjust_num
= 0;
6741 const struct elf_backend_data
*bed
;
6743 bed
= get_elf_backend_data (ibfd
);
6744 iehdr
= elf_elfheader (ibfd
);
6747 pointer_to_map
= &map_first
;
6749 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6750 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6752 /* Returns the end address of the segment + 1. */
6753 #define SEGMENT_END(segment, start) \
6754 (start + (segment->p_memsz > segment->p_filesz \
6755 ? segment->p_memsz : segment->p_filesz))
6757 #define SECTION_SIZE(section, segment) \
6758 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6759 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6760 ? section->size : 0)
6762 /* Returns TRUE if the given section is contained within
6763 the given segment. VMA addresses are compared. */
6764 #define IS_CONTAINED_BY_VMA(section, segment) \
6765 (section->vma >= segment->p_vaddr \
6766 && (section->vma + SECTION_SIZE (section, segment) \
6767 <= (SEGMENT_END (segment, segment->p_vaddr))))
6769 /* Returns TRUE if the given section is contained within
6770 the given segment. LMA addresses are compared. */
6771 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6772 (section->lma >= base \
6773 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6774 && (section->lma + SECTION_SIZE (section, segment) \
6775 <= SEGMENT_END (segment, base)))
6777 /* Handle PT_NOTE segment. */
6778 #define IS_NOTE(p, s) \
6779 (p->p_type == PT_NOTE \
6780 && elf_section_type (s) == SHT_NOTE \
6781 && (bfd_vma) s->filepos >= p->p_offset \
6782 && ((bfd_vma) s->filepos + s->size \
6783 <= p->p_offset + p->p_filesz))
6785 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6787 #define IS_COREFILE_NOTE(p, s) \
6789 && bfd_get_format (ibfd) == bfd_core \
6793 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6794 linker, which generates a PT_INTERP section with p_vaddr and
6795 p_memsz set to 0. */
6796 #define IS_SOLARIS_PT_INTERP(p, s) \
6798 && p->p_paddr == 0 \
6799 && p->p_memsz == 0 \
6800 && p->p_filesz > 0 \
6801 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6803 && (bfd_vma) s->filepos >= p->p_offset \
6804 && ((bfd_vma) s->filepos + s->size \
6805 <= p->p_offset + p->p_filesz))
6807 /* Decide if the given section should be included in the given segment.
6808 A section will be included if:
6809 1. It is within the address space of the segment -- we use the LMA
6810 if that is set for the segment and the VMA otherwise,
6811 2. It is an allocated section or a NOTE section in a PT_NOTE
6813 3. There is an output section associated with it,
6814 4. The section has not already been allocated to a previous segment.
6815 5. PT_GNU_STACK segments do not include any sections.
6816 6. PT_TLS segment includes only SHF_TLS sections.
6817 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6818 8. PT_DYNAMIC should not contain empty sections at the beginning
6819 (with the possible exception of .dynamic). */
6820 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6821 ((((segment->p_paddr \
6822 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6823 : IS_CONTAINED_BY_VMA (section, segment)) \
6824 && (section->flags & SEC_ALLOC) != 0) \
6825 || IS_NOTE (segment, section)) \
6826 && segment->p_type != PT_GNU_STACK \
6827 && (segment->p_type != PT_TLS \
6828 || (section->flags & SEC_THREAD_LOCAL)) \
6829 && (segment->p_type == PT_LOAD \
6830 || segment->p_type == PT_TLS \
6831 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6832 && (segment->p_type != PT_DYNAMIC \
6833 || SECTION_SIZE (section, segment) > 0 \
6834 || (segment->p_paddr \
6835 ? segment->p_paddr != section->lma \
6836 : segment->p_vaddr != section->vma) \
6837 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6838 && (segment->p_type != PT_LOAD || !section->segment_mark))
6840 /* If the output section of a section in the input segment is NULL,
6841 it is removed from the corresponding output segment. */
6842 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6843 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6844 && section->output_section != NULL)
6846 /* Returns TRUE iff seg1 starts after the end of seg2. */
6847 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6848 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6850 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6851 their VMA address ranges and their LMA address ranges overlap.
6852 It is possible to have overlapping VMA ranges without overlapping LMA
6853 ranges. RedBoot images for example can have both .data and .bss mapped
6854 to the same VMA range, but with the .data section mapped to a different
6856 #define SEGMENT_OVERLAPS(seg1, seg2) \
6857 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6858 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6859 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6860 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6862 /* Initialise the segment mark field. */
6863 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6864 section
->segment_mark
= FALSE
;
6866 /* The Solaris linker creates program headers in which all the
6867 p_paddr fields are zero. When we try to objcopy or strip such a
6868 file, we get confused. Check for this case, and if we find it
6869 don't set the p_paddr_valid fields. */
6870 p_paddr_valid
= FALSE
;
6871 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6874 if (segment
->p_paddr
!= 0)
6876 p_paddr_valid
= TRUE
;
6880 /* Scan through the segments specified in the program header
6881 of the input BFD. For this first scan we look for overlaps
6882 in the loadable segments. These can be created by weird
6883 parameters to objcopy. Also, fix some solaris weirdness. */
6884 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6889 Elf_Internal_Phdr
*segment2
;
6891 if (segment
->p_type
== PT_INTERP
)
6892 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6893 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6895 /* Mininal change so that the normal section to segment
6896 assignment code will work. */
6897 segment
->p_vaddr
= section
->vma
;
6901 if (segment
->p_type
!= PT_LOAD
)
6903 /* Remove PT_GNU_RELRO segment. */
6904 if (segment
->p_type
== PT_GNU_RELRO
)
6905 segment
->p_type
= PT_NULL
;
6909 /* Determine if this segment overlaps any previous segments. */
6910 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6912 bfd_signed_vma extra_length
;
6914 if (segment2
->p_type
!= PT_LOAD
6915 || !SEGMENT_OVERLAPS (segment
, segment2
))
6918 /* Merge the two segments together. */
6919 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6921 /* Extend SEGMENT2 to include SEGMENT and then delete
6923 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6924 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6926 if (extra_length
> 0)
6928 segment2
->p_memsz
+= extra_length
;
6929 segment2
->p_filesz
+= extra_length
;
6932 segment
->p_type
= PT_NULL
;
6934 /* Since we have deleted P we must restart the outer loop. */
6936 segment
= elf_tdata (ibfd
)->phdr
;
6941 /* Extend SEGMENT to include SEGMENT2 and then delete
6943 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6944 - SEGMENT_END (segment
, segment
->p_vaddr
));
6946 if (extra_length
> 0)
6948 segment
->p_memsz
+= extra_length
;
6949 segment
->p_filesz
+= extra_length
;
6952 segment2
->p_type
= PT_NULL
;
6957 /* The second scan attempts to assign sections to segments. */
6958 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6962 unsigned int section_count
;
6963 asection
**sections
;
6964 asection
*output_section
;
6966 asection
*matching_lma
;
6967 asection
*suggested_lma
;
6970 asection
*first_section
;
6972 if (segment
->p_type
== PT_NULL
)
6975 first_section
= NULL
;
6976 /* Compute how many sections might be placed into this segment. */
6977 for (section
= ibfd
->sections
, section_count
= 0;
6979 section
= section
->next
)
6981 /* Find the first section in the input segment, which may be
6982 removed from the corresponding output segment. */
6983 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6985 if (first_section
== NULL
)
6986 first_section
= section
;
6987 if (section
->output_section
!= NULL
)
6992 /* Allocate a segment map big enough to contain
6993 all of the sections we have selected. */
6994 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
6995 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
6996 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7000 /* Initialise the fields of the segment map. Default to
7001 using the physical address of the segment in the input BFD. */
7003 map
->p_type
= segment
->p_type
;
7004 map
->p_flags
= segment
->p_flags
;
7005 map
->p_flags_valid
= 1;
7007 /* If the first section in the input segment is removed, there is
7008 no need to preserve segment physical address in the corresponding
7010 if (!first_section
|| first_section
->output_section
!= NULL
)
7012 map
->p_paddr
= segment
->p_paddr
;
7013 map
->p_paddr_valid
= p_paddr_valid
;
7016 /* Determine if this segment contains the ELF file header
7017 and if it contains the program headers themselves. */
7018 map
->includes_filehdr
= (segment
->p_offset
== 0
7019 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7020 map
->includes_phdrs
= 0;
7022 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7024 map
->includes_phdrs
=
7025 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7026 && (segment
->p_offset
+ segment
->p_filesz
7027 >= ((bfd_vma
) iehdr
->e_phoff
7028 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7030 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7031 phdr_included
= TRUE
;
7034 if (section_count
== 0)
7036 /* Special segments, such as the PT_PHDR segment, may contain
7037 no sections, but ordinary, loadable segments should contain
7038 something. They are allowed by the ELF spec however, so only
7039 a warning is produced.
7040 There is however the valid use case of embedded systems which
7041 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7042 flash memory with zeros. No warning is shown for that case. */
7043 if (segment
->p_type
== PT_LOAD
7044 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7045 /* xgettext:c-format */
7047 (_("%pB: warning: empty loadable segment detected"
7048 " at vaddr=%#" PRIx64
", is this intentional?"),
7049 ibfd
, (uint64_t) segment
->p_vaddr
);
7051 map
->p_vaddr_offset
= segment
->p_vaddr
;
7053 *pointer_to_map
= map
;
7054 pointer_to_map
= &map
->next
;
7059 /* Now scan the sections in the input BFD again and attempt
7060 to add their corresponding output sections to the segment map.
7061 The problem here is how to handle an output section which has
7062 been moved (ie had its LMA changed). There are four possibilities:
7064 1. None of the sections have been moved.
7065 In this case we can continue to use the segment LMA from the
7068 2. All of the sections have been moved by the same amount.
7069 In this case we can change the segment's LMA to match the LMA
7070 of the first section.
7072 3. Some of the sections have been moved, others have not.
7073 In this case those sections which have not been moved can be
7074 placed in the current segment which will have to have its size,
7075 and possibly its LMA changed, and a new segment or segments will
7076 have to be created to contain the other sections.
7078 4. The sections have been moved, but not by the same amount.
7079 In this case we can change the segment's LMA to match the LMA
7080 of the first section and we will have to create a new segment
7081 or segments to contain the other sections.
7083 In order to save time, we allocate an array to hold the section
7084 pointers that we are interested in. As these sections get assigned
7085 to a segment, they are removed from this array. */
7087 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
7088 if (sections
== NULL
)
7091 /* Step One: Scan for segment vs section LMA conflicts.
7092 Also add the sections to the section array allocated above.
7093 Also add the sections to the current segment. In the common
7094 case, where the sections have not been moved, this means that
7095 we have completely filled the segment, and there is nothing
7098 matching_lma
= NULL
;
7099 suggested_lma
= NULL
;
7101 for (section
= first_section
, j
= 0;
7103 section
= section
->next
)
7105 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
7107 output_section
= section
->output_section
;
7109 sections
[j
++] = section
;
7111 /* The Solaris native linker always sets p_paddr to 0.
7112 We try to catch that case here, and set it to the
7113 correct value. Note - some backends require that
7114 p_paddr be left as zero. */
7116 && segment
->p_vaddr
!= 0
7117 && !bed
->want_p_paddr_set_to_zero
7119 && output_section
->lma
!= 0
7120 && (align_power (segment
->p_vaddr
7121 + (map
->includes_filehdr
7122 ? iehdr
->e_ehsize
: 0)
7123 + (map
->includes_phdrs
7124 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7126 output_section
->alignment_power
)
7127 == output_section
->vma
))
7128 map
->p_paddr
= segment
->p_vaddr
;
7130 /* Match up the physical address of the segment with the
7131 LMA address of the output section. */
7132 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7133 || IS_COREFILE_NOTE (segment
, section
)
7134 || (bed
->want_p_paddr_set_to_zero
7135 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
7137 if (matching_lma
== NULL
7138 || output_section
->lma
< matching_lma
->lma
)
7139 matching_lma
= output_section
;
7141 /* We assume that if the section fits within the segment
7142 then it does not overlap any other section within that
7144 map
->sections
[isec
++] = output_section
;
7146 else if (suggested_lma
== NULL
)
7147 suggested_lma
= output_section
;
7149 if (j
== section_count
)
7154 BFD_ASSERT (j
== section_count
);
7156 /* Step Two: Adjust the physical address of the current segment,
7158 if (isec
== section_count
)
7160 /* All of the sections fitted within the segment as currently
7161 specified. This is the default case. Add the segment to
7162 the list of built segments and carry on to process the next
7163 program header in the input BFD. */
7164 map
->count
= section_count
;
7165 *pointer_to_map
= map
;
7166 pointer_to_map
= &map
->next
;
7169 && !bed
->want_p_paddr_set_to_zero
7170 && matching_lma
->lma
!= map
->p_paddr
7171 && !map
->includes_filehdr
7172 && !map
->includes_phdrs
)
7173 /* There is some padding before the first section in the
7174 segment. So, we must account for that in the output
7176 map
->p_vaddr_offset
= map
->p_paddr
- matching_lma
->lma
;
7183 /* Change the current segment's physical address to match
7184 the LMA of the first section that fitted, or if no
7185 section fitted, the first section. */
7186 if (matching_lma
== NULL
)
7187 matching_lma
= suggested_lma
;
7189 map
->p_paddr
= matching_lma
->lma
;
7191 /* Offset the segment physical address from the lma
7192 to allow for space taken up by elf headers. */
7193 if (map
->includes_phdrs
)
7195 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7197 /* iehdr->e_phnum is just an estimate of the number
7198 of program headers that we will need. Make a note
7199 here of the number we used and the segment we chose
7200 to hold these headers, so that we can adjust the
7201 offset when we know the correct value. */
7202 phdr_adjust_num
= iehdr
->e_phnum
;
7203 phdr_adjust_seg
= map
;
7206 if (map
->includes_filehdr
)
7208 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7209 map
->p_paddr
-= iehdr
->e_ehsize
;
7210 /* We've subtracted off the size of headers from the
7211 first section lma, but there may have been some
7212 alignment padding before that section too. Try to
7213 account for that by adjusting the segment lma down to
7214 the same alignment. */
7215 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7216 align
= segment
->p_align
;
7217 map
->p_paddr
&= -align
;
7221 /* Step Three: Loop over the sections again, this time assigning
7222 those that fit to the current segment and removing them from the
7223 sections array; but making sure not to leave large gaps. Once all
7224 possible sections have been assigned to the current segment it is
7225 added to the list of built segments and if sections still remain
7226 to be assigned, a new segment is constructed before repeating
7232 suggested_lma
= NULL
;
7234 /* Fill the current segment with sections that fit. */
7235 for (j
= 0; j
< section_count
; j
++)
7237 section
= sections
[j
];
7239 if (section
== NULL
)
7242 output_section
= section
->output_section
;
7244 BFD_ASSERT (output_section
!= NULL
);
7246 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7247 || IS_COREFILE_NOTE (segment
, section
))
7249 if (map
->count
== 0)
7251 /* If the first section in a segment does not start at
7252 the beginning of the segment, then something is
7254 if (align_power (map
->p_paddr
7255 + (map
->includes_filehdr
7256 ? iehdr
->e_ehsize
: 0)
7257 + (map
->includes_phdrs
7258 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7260 output_section
->alignment_power
)
7261 != output_section
->lma
)
7268 prev_sec
= map
->sections
[map
->count
- 1];
7270 /* If the gap between the end of the previous section
7271 and the start of this section is more than
7272 maxpagesize then we need to start a new segment. */
7273 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7275 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7276 || (prev_sec
->lma
+ prev_sec
->size
7277 > output_section
->lma
))
7279 if (suggested_lma
== NULL
)
7280 suggested_lma
= output_section
;
7286 map
->sections
[map
->count
++] = output_section
;
7289 if (segment
->p_type
== PT_LOAD
)
7290 section
->segment_mark
= TRUE
;
7292 else if (suggested_lma
== NULL
)
7293 suggested_lma
= output_section
;
7296 /* PR 23932. A corrupt input file may contain sections that cannot
7297 be assigned to any segment - because for example they have a
7298 negative size - or segments that do not contain any sections. */
7299 if (map
->count
== 0)
7301 bfd_set_error (bfd_error_bad_value
);
7306 /* Add the current segment to the list of built segments. */
7307 *pointer_to_map
= map
;
7308 pointer_to_map
= &map
->next
;
7310 if (isec
< section_count
)
7312 /* We still have not allocated all of the sections to
7313 segments. Create a new segment here, initialise it
7314 and carry on looping. */
7315 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7316 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
7317 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7324 /* Initialise the fields of the segment map. Set the physical
7325 physical address to the LMA of the first section that has
7326 not yet been assigned. */
7328 map
->p_type
= segment
->p_type
;
7329 map
->p_flags
= segment
->p_flags
;
7330 map
->p_flags_valid
= 1;
7331 map
->p_paddr
= suggested_lma
->lma
;
7332 map
->p_paddr_valid
= p_paddr_valid
;
7333 map
->includes_filehdr
= 0;
7334 map
->includes_phdrs
= 0;
7337 while (isec
< section_count
);
7342 elf_seg_map (obfd
) = map_first
;
7344 /* If we had to estimate the number of program headers that were
7345 going to be needed, then check our estimate now and adjust
7346 the offset if necessary. */
7347 if (phdr_adjust_seg
!= NULL
)
7351 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7354 if (count
> phdr_adjust_num
)
7355 phdr_adjust_seg
->p_paddr
7356 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7358 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7359 if (map
->p_type
== PT_PHDR
)
7362 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7363 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7370 #undef IS_CONTAINED_BY_VMA
7371 #undef IS_CONTAINED_BY_LMA
7373 #undef IS_COREFILE_NOTE
7374 #undef IS_SOLARIS_PT_INTERP
7375 #undef IS_SECTION_IN_INPUT_SEGMENT
7376 #undef INCLUDE_SECTION_IN_SEGMENT
7377 #undef SEGMENT_AFTER_SEGMENT
7378 #undef SEGMENT_OVERLAPS
7382 /* Copy ELF program header information. */
7385 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7387 Elf_Internal_Ehdr
*iehdr
;
7388 struct elf_segment_map
*map
;
7389 struct elf_segment_map
*map_first
;
7390 struct elf_segment_map
**pointer_to_map
;
7391 Elf_Internal_Phdr
*segment
;
7393 unsigned int num_segments
;
7394 bfd_boolean phdr_included
= FALSE
;
7395 bfd_boolean p_paddr_valid
;
7397 iehdr
= elf_elfheader (ibfd
);
7400 pointer_to_map
= &map_first
;
7402 /* If all the segment p_paddr fields are zero, don't set
7403 map->p_paddr_valid. */
7404 p_paddr_valid
= FALSE
;
7405 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7406 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7409 if (segment
->p_paddr
!= 0)
7411 p_paddr_valid
= TRUE
;
7415 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7420 unsigned int section_count
;
7422 Elf_Internal_Shdr
*this_hdr
;
7423 asection
*first_section
= NULL
;
7424 asection
*lowest_section
;
7425 bfd_boolean no_contents
= TRUE
;
7427 /* Compute how many sections are in this segment. */
7428 for (section
= ibfd
->sections
, section_count
= 0;
7430 section
= section
->next
)
7432 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7433 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7435 if (first_section
== NULL
)
7436 first_section
= section
;
7437 if (elf_section_type (section
) != SHT_NOBITS
)
7438 no_contents
= FALSE
;
7443 /* Allocate a segment map big enough to contain
7444 all of the sections we have selected. */
7445 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7446 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
7447 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7451 /* Initialize the fields of the output segment map with the
7454 map
->p_type
= segment
->p_type
;
7455 map
->p_flags
= segment
->p_flags
;
7456 map
->p_flags_valid
= 1;
7457 map
->p_paddr
= segment
->p_paddr
;
7458 map
->p_paddr_valid
= p_paddr_valid
;
7459 map
->p_align
= segment
->p_align
;
7460 map
->p_align_valid
= 1;
7461 map
->p_vaddr_offset
= 0;
7463 if (map
->p_type
== PT_GNU_RELRO
7464 || map
->p_type
== PT_GNU_STACK
)
7466 /* The PT_GNU_RELRO segment may contain the first a few
7467 bytes in the .got.plt section even if the whole .got.plt
7468 section isn't in the PT_GNU_RELRO segment. We won't
7469 change the size of the PT_GNU_RELRO segment.
7470 Similarly, PT_GNU_STACK size is significant on uclinux
7472 map
->p_size
= segment
->p_memsz
;
7473 map
->p_size_valid
= 1;
7476 /* Determine if this segment contains the ELF file header
7477 and if it contains the program headers themselves. */
7478 map
->includes_filehdr
= (segment
->p_offset
== 0
7479 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7481 map
->includes_phdrs
= 0;
7482 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7484 map
->includes_phdrs
=
7485 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7486 && (segment
->p_offset
+ segment
->p_filesz
7487 >= ((bfd_vma
) iehdr
->e_phoff
7488 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7490 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7491 phdr_included
= TRUE
;
7494 lowest_section
= NULL
;
7495 if (section_count
!= 0)
7497 unsigned int isec
= 0;
7499 for (section
= first_section
;
7501 section
= section
->next
)
7503 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7504 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7506 map
->sections
[isec
++] = section
->output_section
;
7507 if ((section
->flags
& SEC_ALLOC
) != 0)
7511 if (lowest_section
== NULL
7512 || section
->lma
< lowest_section
->lma
)
7513 lowest_section
= section
;
7515 /* Section lmas are set up from PT_LOAD header
7516 p_paddr in _bfd_elf_make_section_from_shdr.
7517 If this header has a p_paddr that disagrees
7518 with the section lma, flag the p_paddr as
7520 if ((section
->flags
& SEC_LOAD
) != 0)
7521 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7523 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7524 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7525 map
->p_paddr_valid
= FALSE
;
7527 if (isec
== section_count
)
7533 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7535 /* Try to keep the space used by the headers plus any
7536 padding fixed. If there are sections with file contents
7537 in this segment then the lowest sh_offset is the best
7538 guess. Otherwise the segment only has file contents for
7539 the headers, and p_filesz is the best guess. */
7541 map
->header_size
= segment
->p_filesz
;
7543 map
->header_size
= lowest_section
->filepos
;
7546 if (section_count
== 0)
7547 map
->p_vaddr_offset
= segment
->p_vaddr
;
7548 else if (!map
->includes_phdrs
7549 && !map
->includes_filehdr
7550 && map
->p_paddr_valid
)
7551 /* Account for padding before the first section. */
7552 map
->p_vaddr_offset
= (segment
->p_paddr
7553 - (lowest_section
? lowest_section
->lma
: 0));
7555 map
->count
= section_count
;
7556 *pointer_to_map
= map
;
7557 pointer_to_map
= &map
->next
;
7560 elf_seg_map (obfd
) = map_first
;
7564 /* Copy private BFD data. This copies or rewrites ELF program header
7568 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7570 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7571 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7574 if (elf_tdata (ibfd
)->phdr
== NULL
)
7577 if (ibfd
->xvec
== obfd
->xvec
)
7579 /* Check to see if any sections in the input BFD
7580 covered by ELF program header have changed. */
7581 Elf_Internal_Phdr
*segment
;
7582 asection
*section
, *osec
;
7583 unsigned int i
, num_segments
;
7584 Elf_Internal_Shdr
*this_hdr
;
7585 const struct elf_backend_data
*bed
;
7587 bed
= get_elf_backend_data (ibfd
);
7589 /* Regenerate the segment map if p_paddr is set to 0. */
7590 if (bed
->want_p_paddr_set_to_zero
)
7593 /* Initialize the segment mark field. */
7594 for (section
= obfd
->sections
; section
!= NULL
;
7595 section
= section
->next
)
7596 section
->segment_mark
= FALSE
;
7598 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7599 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7603 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7604 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7605 which severly confuses things, so always regenerate the segment
7606 map in this case. */
7607 if (segment
->p_paddr
== 0
7608 && segment
->p_memsz
== 0
7609 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7612 for (section
= ibfd
->sections
;
7613 section
!= NULL
; section
= section
->next
)
7615 /* We mark the output section so that we know it comes
7616 from the input BFD. */
7617 osec
= section
->output_section
;
7619 osec
->segment_mark
= TRUE
;
7621 /* Check if this section is covered by the segment. */
7622 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7623 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7625 /* FIXME: Check if its output section is changed or
7626 removed. What else do we need to check? */
7628 || section
->flags
!= osec
->flags
7629 || section
->lma
!= osec
->lma
7630 || section
->vma
!= osec
->vma
7631 || section
->size
!= osec
->size
7632 || section
->rawsize
!= osec
->rawsize
7633 || section
->alignment_power
!= osec
->alignment_power
)
7639 /* Check to see if any output section do not come from the
7641 for (section
= obfd
->sections
; section
!= NULL
;
7642 section
= section
->next
)
7644 if (!section
->segment_mark
)
7647 section
->segment_mark
= FALSE
;
7650 return copy_elf_program_header (ibfd
, obfd
);
7654 if (ibfd
->xvec
== obfd
->xvec
)
7656 /* When rewriting program header, set the output maxpagesize to
7657 the maximum alignment of input PT_LOAD segments. */
7658 Elf_Internal_Phdr
*segment
;
7660 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7661 bfd_vma maxpagesize
= 0;
7663 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7666 if (segment
->p_type
== PT_LOAD
7667 && maxpagesize
< segment
->p_align
)
7669 /* PR 17512: file: f17299af. */
7670 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7671 /* xgettext:c-format */
7672 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7673 PRIx64
" is too large"),
7674 ibfd
, (uint64_t) segment
->p_align
);
7676 maxpagesize
= segment
->p_align
;
7679 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7680 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7683 return rewrite_elf_program_header (ibfd
, obfd
);
7686 /* Initialize private output section information from input section. */
7689 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7693 struct bfd_link_info
*link_info
)
7696 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7697 bfd_boolean final_link
= (link_info
!= NULL
7698 && !bfd_link_relocatable (link_info
));
7700 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7701 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7704 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7706 /* For objcopy and relocatable link, don't copy the output ELF
7707 section type from input if the output BFD section flags have been
7708 set to something different. For a final link allow some flags
7709 that the linker clears to differ. */
7710 if (elf_section_type (osec
) == SHT_NULL
7711 && (osec
->flags
== isec
->flags
7713 && ((osec
->flags
^ isec
->flags
)
7714 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7715 elf_section_type (osec
) = elf_section_type (isec
);
7717 /* FIXME: Is this correct for all OS/PROC specific flags? */
7718 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7719 & (SHF_MASKOS
| SHF_MASKPROC
));
7721 /* Copy sh_info from input for mbind section. */
7722 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7723 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7724 elf_section_data (osec
)->this_hdr
.sh_info
7725 = elf_section_data (isec
)->this_hdr
.sh_info
;
7727 /* Set things up for objcopy and relocatable link. The output
7728 SHT_GROUP section will have its elf_next_in_group pointing back
7729 to the input group members. Ignore linker created group section.
7730 See elfNN_ia64_object_p in elfxx-ia64.c. */
7731 if ((link_info
== NULL
7732 || !link_info
->resolve_section_groups
)
7733 && (elf_sec_group (isec
) == NULL
7734 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7736 if (elf_section_flags (isec
) & SHF_GROUP
)
7737 elf_section_flags (osec
) |= SHF_GROUP
;
7738 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7739 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7742 /* If not decompress, preserve SHF_COMPRESSED. */
7743 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7744 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7747 ihdr
= &elf_section_data (isec
)->this_hdr
;
7749 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7750 don't use the output section of the linked-to section since it
7751 may be NULL at this point. */
7752 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7754 ohdr
= &elf_section_data (osec
)->this_hdr
;
7755 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7756 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7759 osec
->use_rela_p
= isec
->use_rela_p
;
7764 /* Copy private section information. This copies over the entsize
7765 field, and sometimes the info field. */
7768 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7773 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7775 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7776 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7779 ihdr
= &elf_section_data (isec
)->this_hdr
;
7780 ohdr
= &elf_section_data (osec
)->this_hdr
;
7782 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7784 if (ihdr
->sh_type
== SHT_SYMTAB
7785 || ihdr
->sh_type
== SHT_DYNSYM
7786 || ihdr
->sh_type
== SHT_GNU_verneed
7787 || ihdr
->sh_type
== SHT_GNU_verdef
)
7788 ohdr
->sh_info
= ihdr
->sh_info
;
7790 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7794 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7795 necessary if we are removing either the SHT_GROUP section or any of
7796 the group member sections. DISCARDED is the value that a section's
7797 output_section has if the section will be discarded, NULL when this
7798 function is called from objcopy, bfd_abs_section_ptr when called
7802 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7806 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7807 if (elf_section_type (isec
) == SHT_GROUP
)
7809 asection
*first
= elf_next_in_group (isec
);
7810 asection
*s
= first
;
7811 bfd_size_type removed
= 0;
7815 /* If this member section is being output but the
7816 SHT_GROUP section is not, then clear the group info
7817 set up by _bfd_elf_copy_private_section_data. */
7818 if (s
->output_section
!= discarded
7819 && isec
->output_section
== discarded
)
7821 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7822 elf_group_name (s
->output_section
) = NULL
;
7824 /* Conversely, if the member section is not being output
7825 but the SHT_GROUP section is, then adjust its size. */
7826 else if (s
->output_section
== discarded
7827 && isec
->output_section
!= discarded
)
7829 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7831 if (elf_sec
->rel
.hdr
!= NULL
7832 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7834 if (elf_sec
->rela
.hdr
!= NULL
7835 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7838 s
= elf_next_in_group (s
);
7844 if (discarded
!= NULL
)
7846 /* If we've been called for ld -r, then we need to
7847 adjust the input section size. */
7848 if (isec
->rawsize
== 0)
7849 isec
->rawsize
= isec
->size
;
7850 isec
->size
= isec
->rawsize
- removed
;
7851 if (isec
->size
<= 4)
7854 isec
->flags
|= SEC_EXCLUDE
;
7859 /* Adjust the output section size when called from
7861 isec
->output_section
->size
-= removed
;
7862 if (isec
->output_section
->size
<= 4)
7864 isec
->output_section
->size
= 0;
7865 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7874 /* Copy private header information. */
7877 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7879 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7880 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7883 /* Copy over private BFD data if it has not already been copied.
7884 This must be done here, rather than in the copy_private_bfd_data
7885 entry point, because the latter is called after the section
7886 contents have been set, which means that the program headers have
7887 already been worked out. */
7888 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7890 if (! copy_private_bfd_data (ibfd
, obfd
))
7894 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7897 /* Copy private symbol information. If this symbol is in a section
7898 which we did not map into a BFD section, try to map the section
7899 index correctly. We use special macro definitions for the mapped
7900 section indices; these definitions are interpreted by the
7901 swap_out_syms function. */
7903 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7904 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7905 #define MAP_STRTAB (SHN_HIOS + 3)
7906 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7907 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7910 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7915 elf_symbol_type
*isym
, *osym
;
7917 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7918 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7921 isym
= elf_symbol_from (ibfd
, isymarg
);
7922 osym
= elf_symbol_from (obfd
, osymarg
);
7925 && isym
->internal_elf_sym
.st_shndx
!= 0
7927 && bfd_is_abs_section (isym
->symbol
.section
))
7931 shndx
= isym
->internal_elf_sym
.st_shndx
;
7932 if (shndx
== elf_onesymtab (ibfd
))
7933 shndx
= MAP_ONESYMTAB
;
7934 else if (shndx
== elf_dynsymtab (ibfd
))
7935 shndx
= MAP_DYNSYMTAB
;
7936 else if (shndx
== elf_strtab_sec (ibfd
))
7938 else if (shndx
== elf_shstrtab_sec (ibfd
))
7939 shndx
= MAP_SHSTRTAB
;
7940 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7941 shndx
= MAP_SYM_SHNDX
;
7942 osym
->internal_elf_sym
.st_shndx
= shndx
;
7948 /* Swap out the symbols. */
7951 swap_out_syms (bfd
*abfd
,
7952 struct elf_strtab_hash
**sttp
,
7955 const struct elf_backend_data
*bed
;
7958 struct elf_strtab_hash
*stt
;
7959 Elf_Internal_Shdr
*symtab_hdr
;
7960 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7961 Elf_Internal_Shdr
*symstrtab_hdr
;
7962 struct elf_sym_strtab
*symstrtab
;
7963 bfd_byte
*outbound_syms
;
7964 bfd_byte
*outbound_shndx
;
7965 unsigned long outbound_syms_index
;
7966 unsigned long outbound_shndx_index
;
7968 unsigned int num_locals
;
7970 bfd_boolean name_local_sections
;
7972 if (!elf_map_symbols (abfd
, &num_locals
))
7975 /* Dump out the symtabs. */
7976 stt
= _bfd_elf_strtab_init ();
7980 bed
= get_elf_backend_data (abfd
);
7981 symcount
= bfd_get_symcount (abfd
);
7982 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7983 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7984 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7985 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7986 symtab_hdr
->sh_info
= num_locals
+ 1;
7987 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7989 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7990 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7992 /* Allocate buffer to swap out the .strtab section. */
7993 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc2 (symcount
+ 1,
7994 sizeof (*symstrtab
));
7995 if (symstrtab
== NULL
)
7997 _bfd_elf_strtab_free (stt
);
8001 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
8002 bed
->s
->sizeof_sym
);
8003 if (outbound_syms
== NULL
)
8006 _bfd_elf_strtab_free (stt
);
8010 symtab_hdr
->contents
= outbound_syms
;
8011 outbound_syms_index
= 0;
8013 outbound_shndx
= NULL
;
8014 outbound_shndx_index
= 0;
8016 if (elf_symtab_shndx_list (abfd
))
8018 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8019 if (symtab_shndx_hdr
->sh_name
!= 0)
8021 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
8022 outbound_shndx
= (bfd_byte
*)
8023 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
8024 if (outbound_shndx
== NULL
)
8027 symtab_shndx_hdr
->contents
= outbound_shndx
;
8028 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8029 symtab_shndx_hdr
->sh_size
= amt
;
8030 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8031 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8033 /* FIXME: What about any other headers in the list ? */
8036 /* Now generate the data (for "contents"). */
8038 /* Fill in zeroth symbol and swap it out. */
8039 Elf_Internal_Sym sym
;
8045 sym
.st_shndx
= SHN_UNDEF
;
8046 sym
.st_target_internal
= 0;
8047 symstrtab
[0].sym
= sym
;
8048 symstrtab
[0].dest_index
= outbound_syms_index
;
8049 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8050 outbound_syms_index
++;
8051 if (outbound_shndx
!= NULL
)
8052 outbound_shndx_index
++;
8056 = (bed
->elf_backend_name_local_section_symbols
8057 && bed
->elf_backend_name_local_section_symbols (abfd
));
8059 syms
= bfd_get_outsymbols (abfd
);
8060 for (idx
= 0; idx
< symcount
;)
8062 Elf_Internal_Sym sym
;
8063 bfd_vma value
= syms
[idx
]->value
;
8064 elf_symbol_type
*type_ptr
;
8065 flagword flags
= syms
[idx
]->flags
;
8068 if (!name_local_sections
8069 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8071 /* Local section symbols have no name. */
8072 sym
.st_name
= (unsigned long) -1;
8076 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8077 to get the final offset for st_name. */
8079 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8081 if (sym
.st_name
== (unsigned long) -1)
8085 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
8087 if ((flags
& BSF_SECTION_SYM
) == 0
8088 && bfd_is_com_section (syms
[idx
]->section
))
8090 /* ELF common symbols put the alignment into the `value' field,
8091 and the size into the `size' field. This is backwards from
8092 how BFD handles it, so reverse it here. */
8093 sym
.st_size
= value
;
8094 if (type_ptr
== NULL
8095 || type_ptr
->internal_elf_sym
.st_value
== 0)
8096 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8098 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8099 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8100 (abfd
, syms
[idx
]->section
);
8104 asection
*sec
= syms
[idx
]->section
;
8107 if (sec
->output_section
)
8109 value
+= sec
->output_offset
;
8110 sec
= sec
->output_section
;
8113 /* Don't add in the section vma for relocatable output. */
8114 if (! relocatable_p
)
8116 sym
.st_value
= value
;
8117 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8119 if (bfd_is_abs_section (sec
)
8121 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8123 /* This symbol is in a real ELF section which we did
8124 not create as a BFD section. Undo the mapping done
8125 by copy_private_symbol_data. */
8126 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8130 shndx
= elf_onesymtab (abfd
);
8133 shndx
= elf_dynsymtab (abfd
);
8136 shndx
= elf_strtab_sec (abfd
);
8139 shndx
= elf_shstrtab_sec (abfd
);
8142 if (elf_symtab_shndx_list (abfd
))
8143 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8152 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8154 if (shndx
== SHN_BAD
)
8158 /* Writing this would be a hell of a lot easier if
8159 we had some decent documentation on bfd, and
8160 knew what to expect of the library, and what to
8161 demand of applications. For example, it
8162 appears that `objcopy' might not set the
8163 section of a symbol to be a section that is
8164 actually in the output file. */
8165 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8167 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8168 if (shndx
== SHN_BAD
)
8170 /* xgettext:c-format */
8172 (_("unable to find equivalent output section"
8173 " for symbol '%s' from section '%s'"),
8174 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8176 bfd_set_error (bfd_error_invalid_operation
);
8182 sym
.st_shndx
= shndx
;
8185 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8187 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8188 type
= STT_GNU_IFUNC
;
8189 else if ((flags
& BSF_FUNCTION
) != 0)
8191 else if ((flags
& BSF_OBJECT
) != 0)
8193 else if ((flags
& BSF_RELC
) != 0)
8195 else if ((flags
& BSF_SRELC
) != 0)
8200 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8203 /* Processor-specific types. */
8204 if (type_ptr
!= NULL
8205 && bed
->elf_backend_get_symbol_type
)
8206 type
= ((*bed
->elf_backend_get_symbol_type
)
8207 (&type_ptr
->internal_elf_sym
, type
));
8209 if (flags
& BSF_SECTION_SYM
)
8211 if (flags
& BSF_GLOBAL
)
8212 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8214 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8216 else if (bfd_is_com_section (syms
[idx
]->section
))
8218 if (type
!= STT_TLS
)
8220 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8221 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8222 ? STT_COMMON
: STT_OBJECT
);
8224 type
= ((flags
& BSF_ELF_COMMON
) != 0
8225 ? STT_COMMON
: STT_OBJECT
);
8227 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8229 else if (bfd_is_und_section (syms
[idx
]->section
))
8230 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8234 else if (flags
& BSF_FILE
)
8235 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8238 int bind
= STB_LOCAL
;
8240 if (flags
& BSF_LOCAL
)
8242 else if (flags
& BSF_GNU_UNIQUE
)
8243 bind
= STB_GNU_UNIQUE
;
8244 else if (flags
& BSF_WEAK
)
8246 else if (flags
& BSF_GLOBAL
)
8249 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8252 if (type_ptr
!= NULL
)
8254 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8255 sym
.st_target_internal
8256 = type_ptr
->internal_elf_sym
.st_target_internal
;
8261 sym
.st_target_internal
= 0;
8265 symstrtab
[idx
].sym
= sym
;
8266 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8267 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8269 outbound_syms_index
++;
8270 if (outbound_shndx
!= NULL
)
8271 outbound_shndx_index
++;
8274 /* Finalize the .strtab section. */
8275 _bfd_elf_strtab_finalize (stt
);
8277 /* Swap out the .strtab section. */
8278 for (idx
= 0; idx
<= symcount
; idx
++)
8280 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8281 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8282 elfsym
->sym
.st_name
= 0;
8284 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8285 elfsym
->sym
.st_name
);
8286 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8288 + (elfsym
->dest_index
8289 * bed
->s
->sizeof_sym
)),
8291 + (elfsym
->destshndx_index
8292 * sizeof (Elf_External_Sym_Shndx
))));
8297 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8298 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8299 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8300 symstrtab_hdr
->sh_addr
= 0;
8301 symstrtab_hdr
->sh_entsize
= 0;
8302 symstrtab_hdr
->sh_link
= 0;
8303 symstrtab_hdr
->sh_info
= 0;
8304 symstrtab_hdr
->sh_addralign
= 1;
8309 /* Return the number of bytes required to hold the symtab vector.
8311 Note that we base it on the count plus 1, since we will null terminate
8312 the vector allocated based on this size. However, the ELF symbol table
8313 always has a dummy entry as symbol #0, so it ends up even. */
8316 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8318 bfd_size_type symcount
;
8320 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8322 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8323 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8325 bfd_set_error (bfd_error_file_too_big
);
8328 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8330 symtab_size
-= sizeof (asymbol
*);
8336 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8338 bfd_size_type symcount
;
8340 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8342 if (elf_dynsymtab (abfd
) == 0)
8344 bfd_set_error (bfd_error_invalid_operation
);
8348 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8349 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8351 bfd_set_error (bfd_error_file_too_big
);
8354 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8356 symtab_size
-= sizeof (asymbol
*);
8362 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8365 #if SIZEOF_LONG == SIZEOF_INT
8366 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8368 bfd_set_error (bfd_error_file_too_big
);
8372 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8375 /* Canonicalize the relocs. */
8378 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8385 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8387 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8390 tblptr
= section
->relocation
;
8391 for (i
= 0; i
< section
->reloc_count
; i
++)
8392 *relptr
++ = tblptr
++;
8396 return section
->reloc_count
;
8400 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8402 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8403 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8406 abfd
->symcount
= symcount
;
8411 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8412 asymbol
**allocation
)
8414 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8415 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8418 abfd
->dynsymcount
= symcount
;
8422 /* Return the size required for the dynamic reloc entries. Any loadable
8423 section that was actually installed in the BFD, and has type SHT_REL
8424 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8425 dynamic reloc section. */
8428 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8430 bfd_size_type count
;
8433 if (elf_dynsymtab (abfd
) == 0)
8435 bfd_set_error (bfd_error_invalid_operation
);
8440 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8441 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8442 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8443 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8445 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8446 if (count
> LONG_MAX
/ sizeof (arelent
*))
8448 bfd_set_error (bfd_error_file_too_big
);
8452 return count
* sizeof (arelent
*);
8455 /* Canonicalize the dynamic relocation entries. Note that we return the
8456 dynamic relocations as a single block, although they are actually
8457 associated with particular sections; the interface, which was
8458 designed for SunOS style shared libraries, expects that there is only
8459 one set of dynamic relocs. Any loadable section that was actually
8460 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8461 dynamic symbol table, is considered to be a dynamic reloc section. */
8464 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8468 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8472 if (elf_dynsymtab (abfd
) == 0)
8474 bfd_set_error (bfd_error_invalid_operation
);
8478 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8480 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8482 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8483 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8484 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8489 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8491 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8493 for (i
= 0; i
< count
; i
++)
8504 /* Read in the version information. */
8507 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8509 bfd_byte
*contents
= NULL
;
8510 unsigned int freeidx
= 0;
8512 if (elf_dynverref (abfd
) != 0)
8514 Elf_Internal_Shdr
*hdr
;
8515 Elf_External_Verneed
*everneed
;
8516 Elf_Internal_Verneed
*iverneed
;
8518 bfd_byte
*contents_end
;
8520 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8522 if (hdr
->sh_info
== 0
8523 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8525 error_return_bad_verref
:
8527 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8528 bfd_set_error (bfd_error_bad_value
);
8529 error_return_verref
:
8530 elf_tdata (abfd
)->verref
= NULL
;
8531 elf_tdata (abfd
)->cverrefs
= 0;
8535 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8536 if (filesize
> 0 && filesize
< hdr
->sh_size
)
8538 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8540 bfd_set_error (bfd_error_no_memory
);
8542 /* xgettext:c-format */
8543 (_("error: %pB version reference section is too large (%#" PRIx64
" bytes)"),
8544 abfd
, (uint64_t) hdr
->sh_size
);
8545 goto error_return_verref
;
8547 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8548 if (contents
== NULL
)
8549 goto error_return_verref
;
8551 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8552 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8553 goto error_return_verref
;
8555 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8556 bfd_alloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8558 if (elf_tdata (abfd
)->verref
== NULL
)
8559 goto error_return_verref
;
8561 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8562 == sizeof (Elf_External_Vernaux
));
8563 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8564 everneed
= (Elf_External_Verneed
*) contents
;
8565 iverneed
= elf_tdata (abfd
)->verref
;
8566 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8568 Elf_External_Vernaux
*evernaux
;
8569 Elf_Internal_Vernaux
*ivernaux
;
8572 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8574 iverneed
->vn_bfd
= abfd
;
8576 iverneed
->vn_filename
=
8577 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8579 if (iverneed
->vn_filename
== NULL
)
8580 goto error_return_bad_verref
;
8582 if (iverneed
->vn_cnt
== 0)
8583 iverneed
->vn_auxptr
= NULL
;
8586 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8587 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8588 sizeof (Elf_Internal_Vernaux
));
8589 if (iverneed
->vn_auxptr
== NULL
)
8590 goto error_return_verref
;
8593 if (iverneed
->vn_aux
8594 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8595 goto error_return_bad_verref
;
8597 evernaux
= ((Elf_External_Vernaux
*)
8598 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8599 ivernaux
= iverneed
->vn_auxptr
;
8600 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8602 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8604 ivernaux
->vna_nodename
=
8605 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8606 ivernaux
->vna_name
);
8607 if (ivernaux
->vna_nodename
== NULL
)
8608 goto error_return_bad_verref
;
8610 if (ivernaux
->vna_other
> freeidx
)
8611 freeidx
= ivernaux
->vna_other
;
8613 ivernaux
->vna_nextptr
= NULL
;
8614 if (ivernaux
->vna_next
== 0)
8616 iverneed
->vn_cnt
= j
+ 1;
8619 if (j
+ 1 < iverneed
->vn_cnt
)
8620 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8622 if (ivernaux
->vna_next
8623 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8624 goto error_return_bad_verref
;
8626 evernaux
= ((Elf_External_Vernaux
*)
8627 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8630 iverneed
->vn_nextref
= NULL
;
8631 if (iverneed
->vn_next
== 0)
8633 if (i
+ 1 < hdr
->sh_info
)
8634 iverneed
->vn_nextref
= iverneed
+ 1;
8636 if (iverneed
->vn_next
8637 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8638 goto error_return_bad_verref
;
8640 everneed
= ((Elf_External_Verneed
*)
8641 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8643 elf_tdata (abfd
)->cverrefs
= i
;
8649 if (elf_dynverdef (abfd
) != 0)
8651 Elf_Internal_Shdr
*hdr
;
8652 Elf_External_Verdef
*everdef
;
8653 Elf_Internal_Verdef
*iverdef
;
8654 Elf_Internal_Verdef
*iverdefarr
;
8655 Elf_Internal_Verdef iverdefmem
;
8657 unsigned int maxidx
;
8658 bfd_byte
*contents_end_def
, *contents_end_aux
;
8660 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8662 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8664 error_return_bad_verdef
:
8666 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8667 bfd_set_error (bfd_error_bad_value
);
8668 error_return_verdef
:
8669 elf_tdata (abfd
)->verdef
= NULL
;
8670 elf_tdata (abfd
)->cverdefs
= 0;
8674 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8675 if (contents
== NULL
)
8676 goto error_return_verdef
;
8677 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8678 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8679 goto error_return_verdef
;
8681 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8682 >= sizeof (Elf_External_Verdaux
));
8683 contents_end_def
= contents
+ hdr
->sh_size
8684 - sizeof (Elf_External_Verdef
);
8685 contents_end_aux
= contents
+ hdr
->sh_size
8686 - sizeof (Elf_External_Verdaux
);
8688 /* We know the number of entries in the section but not the maximum
8689 index. Therefore we have to run through all entries and find
8691 everdef
= (Elf_External_Verdef
*) contents
;
8693 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8695 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8697 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8698 goto error_return_bad_verdef
;
8699 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8700 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8702 if (iverdefmem
.vd_next
== 0)
8705 if (iverdefmem
.vd_next
8706 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8707 goto error_return_bad_verdef
;
8709 everdef
= ((Elf_External_Verdef
*)
8710 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8713 if (default_imported_symver
)
8715 if (freeidx
> maxidx
)
8721 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8722 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8723 if (elf_tdata (abfd
)->verdef
== NULL
)
8724 goto error_return_verdef
;
8726 elf_tdata (abfd
)->cverdefs
= maxidx
;
8728 everdef
= (Elf_External_Verdef
*) contents
;
8729 iverdefarr
= elf_tdata (abfd
)->verdef
;
8730 for (i
= 0; i
< hdr
->sh_info
; i
++)
8732 Elf_External_Verdaux
*everdaux
;
8733 Elf_Internal_Verdaux
*iverdaux
;
8736 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8738 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8739 goto error_return_bad_verdef
;
8741 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8742 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8744 iverdef
->vd_bfd
= abfd
;
8746 if (iverdef
->vd_cnt
== 0)
8747 iverdef
->vd_auxptr
= NULL
;
8750 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8751 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8752 sizeof (Elf_Internal_Verdaux
));
8753 if (iverdef
->vd_auxptr
== NULL
)
8754 goto error_return_verdef
;
8758 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8759 goto error_return_bad_verdef
;
8761 everdaux
= ((Elf_External_Verdaux
*)
8762 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8763 iverdaux
= iverdef
->vd_auxptr
;
8764 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8766 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8768 iverdaux
->vda_nodename
=
8769 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8770 iverdaux
->vda_name
);
8771 if (iverdaux
->vda_nodename
== NULL
)
8772 goto error_return_bad_verdef
;
8774 iverdaux
->vda_nextptr
= NULL
;
8775 if (iverdaux
->vda_next
== 0)
8777 iverdef
->vd_cnt
= j
+ 1;
8780 if (j
+ 1 < iverdef
->vd_cnt
)
8781 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8783 if (iverdaux
->vda_next
8784 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8785 goto error_return_bad_verdef
;
8787 everdaux
= ((Elf_External_Verdaux
*)
8788 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8791 iverdef
->vd_nodename
= NULL
;
8792 if (iverdef
->vd_cnt
)
8793 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8795 iverdef
->vd_nextdef
= NULL
;
8796 if (iverdef
->vd_next
== 0)
8798 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8799 iverdef
->vd_nextdef
= iverdef
+ 1;
8801 everdef
= ((Elf_External_Verdef
*)
8802 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8808 else if (default_imported_symver
)
8815 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8816 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8817 if (elf_tdata (abfd
)->verdef
== NULL
)
8820 elf_tdata (abfd
)->cverdefs
= freeidx
;
8823 /* Create a default version based on the soname. */
8824 if (default_imported_symver
)
8826 Elf_Internal_Verdef
*iverdef
;
8827 Elf_Internal_Verdaux
*iverdaux
;
8829 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8831 iverdef
->vd_version
= VER_DEF_CURRENT
;
8832 iverdef
->vd_flags
= 0;
8833 iverdef
->vd_ndx
= freeidx
;
8834 iverdef
->vd_cnt
= 1;
8836 iverdef
->vd_bfd
= abfd
;
8838 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8839 if (iverdef
->vd_nodename
== NULL
)
8840 goto error_return_verdef
;
8841 iverdef
->vd_nextdef
= NULL
;
8842 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8843 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8844 if (iverdef
->vd_auxptr
== NULL
)
8845 goto error_return_verdef
;
8847 iverdaux
= iverdef
->vd_auxptr
;
8848 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8854 if (contents
!= NULL
)
8860 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8862 elf_symbol_type
*newsym
;
8864 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
8867 newsym
->symbol
.the_bfd
= abfd
;
8868 return &newsym
->symbol
;
8872 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8876 bfd_symbol_info (symbol
, ret
);
8879 /* Return whether a symbol name implies a local symbol. Most targets
8880 use this function for the is_local_label_name entry point, but some
8884 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8887 /* Normal local symbols start with ``.L''. */
8888 if (name
[0] == '.' && name
[1] == 'L')
8891 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8892 DWARF debugging symbols starting with ``..''. */
8893 if (name
[0] == '.' && name
[1] == '.')
8896 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8897 emitting DWARF debugging output. I suspect this is actually a
8898 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8899 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8900 underscore to be emitted on some ELF targets). For ease of use,
8901 we treat such symbols as local. */
8902 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8905 /* Treat assembler generated fake symbols, dollar local labels and
8906 forward-backward labels (aka local labels) as locals.
8907 These labels have the form:
8909 L0^A.* (fake symbols)
8911 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8913 Versions which start with .L will have already been matched above,
8914 so we only need to match the rest. */
8915 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8917 bfd_boolean ret
= FALSE
;
8921 for (p
= name
+ 2; (c
= *p
); p
++)
8923 if (c
== 1 || c
== 2)
8925 if (c
== 1 && p
== name
+ 2)
8926 /* A fake symbol. */
8929 /* FIXME: We are being paranoid here and treating symbols like
8930 L0^Bfoo as if there were non-local, on the grounds that the
8931 assembler will never generate them. But can any symbol
8932 containing an ASCII value in the range 1-31 ever be anything
8933 other than some kind of local ? */
8950 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8951 asymbol
*symbol ATTRIBUTE_UNUSED
)
8958 _bfd_elf_set_arch_mach (bfd
*abfd
,
8959 enum bfd_architecture arch
,
8960 unsigned long machine
)
8962 /* If this isn't the right architecture for this backend, and this
8963 isn't the generic backend, fail. */
8964 if (arch
!= get_elf_backend_data (abfd
)->arch
8965 && arch
!= bfd_arch_unknown
8966 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8969 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8972 /* Find the nearest line to a particular section and offset,
8973 for error reporting. */
8976 _bfd_elf_find_nearest_line (bfd
*abfd
,
8980 const char **filename_ptr
,
8981 const char **functionname_ptr
,
8982 unsigned int *line_ptr
,
8983 unsigned int *discriminator_ptr
)
8987 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8988 filename_ptr
, functionname_ptr
,
8989 line_ptr
, discriminator_ptr
,
8990 dwarf_debug_sections
,
8991 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8992 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8993 filename_ptr
, functionname_ptr
,
8996 if (!*functionname_ptr
)
8997 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8998 *filename_ptr
? NULL
: filename_ptr
,
9003 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9004 &found
, filename_ptr
,
9005 functionname_ptr
, line_ptr
,
9006 &elf_tdata (abfd
)->line_info
))
9008 if (found
&& (*functionname_ptr
|| *line_ptr
))
9011 if (symbols
== NULL
)
9014 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9015 filename_ptr
, functionname_ptr
))
9022 /* Find the line for a symbol. */
9025 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9026 const char **filename_ptr
, unsigned int *line_ptr
)
9028 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9029 filename_ptr
, NULL
, line_ptr
, NULL
,
9030 dwarf_debug_sections
,
9031 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9034 /* After a call to bfd_find_nearest_line, successive calls to
9035 bfd_find_inliner_info can be used to get source information about
9036 each level of function inlining that terminated at the address
9037 passed to bfd_find_nearest_line. Currently this is only supported
9038 for DWARF2 with appropriate DWARF3 extensions. */
9041 _bfd_elf_find_inliner_info (bfd
*abfd
,
9042 const char **filename_ptr
,
9043 const char **functionname_ptr
,
9044 unsigned int *line_ptr
)
9047 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9048 functionname_ptr
, line_ptr
,
9049 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9054 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9056 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9057 int ret
= bed
->s
->sizeof_ehdr
;
9059 if (!bfd_link_relocatable (info
))
9061 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9063 if (phdr_size
== (bfd_size_type
) -1)
9065 struct elf_segment_map
*m
;
9068 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9069 phdr_size
+= bed
->s
->sizeof_phdr
;
9072 phdr_size
= get_program_header_size (abfd
, info
);
9075 elf_program_header_size (abfd
) = phdr_size
;
9083 _bfd_elf_set_section_contents (bfd
*abfd
,
9085 const void *location
,
9087 bfd_size_type count
)
9089 Elf_Internal_Shdr
*hdr
;
9092 if (! abfd
->output_has_begun
9093 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9099 hdr
= &elf_section_data (section
)->this_hdr
;
9100 if (hdr
->sh_offset
== (file_ptr
) -1)
9102 /* We must compress this section. Write output to the buffer. */
9103 unsigned char *contents
= hdr
->contents
;
9104 if ((offset
+ count
) > hdr
->sh_size
9105 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
9106 || contents
== NULL
)
9108 memcpy (contents
+ offset
, location
, count
);
9111 pos
= hdr
->sh_offset
+ offset
;
9112 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9113 || bfd_bwrite (location
, count
, abfd
) != count
)
9120 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9121 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9122 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9128 /* Try to convert a non-ELF reloc into an ELF one. */
9131 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9133 /* Check whether we really have an ELF howto. */
9135 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9137 bfd_reloc_code_real_type code
;
9138 reloc_howto_type
*howto
;
9140 /* Alien reloc: Try to determine its type to replace it with an
9141 equivalent ELF reloc. */
9143 if (areloc
->howto
->pc_relative
)
9145 switch (areloc
->howto
->bitsize
)
9148 code
= BFD_RELOC_8_PCREL
;
9151 code
= BFD_RELOC_12_PCREL
;
9154 code
= BFD_RELOC_16_PCREL
;
9157 code
= BFD_RELOC_24_PCREL
;
9160 code
= BFD_RELOC_32_PCREL
;
9163 code
= BFD_RELOC_64_PCREL
;
9169 howto
= bfd_reloc_type_lookup (abfd
, code
);
9171 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9173 if (howto
->pcrel_offset
)
9174 areloc
->addend
+= areloc
->address
;
9176 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9181 switch (areloc
->howto
->bitsize
)
9187 code
= BFD_RELOC_14
;
9190 code
= BFD_RELOC_16
;
9193 code
= BFD_RELOC_26
;
9196 code
= BFD_RELOC_32
;
9199 code
= BFD_RELOC_64
;
9205 howto
= bfd_reloc_type_lookup (abfd
, code
);
9209 areloc
->howto
= howto
;
9217 /* xgettext:c-format */
9218 _bfd_error_handler (_("%pB: %s unsupported"),
9219 abfd
, areloc
->howto
->name
);
9220 bfd_set_error (bfd_error_bad_value
);
9225 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9227 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9228 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9230 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9231 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9232 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9235 return _bfd_generic_close_and_cleanup (abfd
);
9238 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9239 in the relocation's offset. Thus we cannot allow any sort of sanity
9240 range-checking to interfere. There is nothing else to do in processing
9243 bfd_reloc_status_type
9244 _bfd_elf_rel_vtable_reloc_fn
9245 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9246 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9247 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9248 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9250 return bfd_reloc_ok
;
9253 /* Elf core file support. Much of this only works on native
9254 toolchains, since we rely on knowing the
9255 machine-dependent procfs structure in order to pick
9256 out details about the corefile. */
9258 #ifdef HAVE_SYS_PROCFS_H
9259 /* Needed for new procfs interface on sparc-solaris. */
9260 # define _STRUCTURED_PROC 1
9261 # include <sys/procfs.h>
9264 /* Return a PID that identifies a "thread" for threaded cores, or the
9265 PID of the main process for non-threaded cores. */
9268 elfcore_make_pid (bfd
*abfd
)
9272 pid
= elf_tdata (abfd
)->core
->lwpid
;
9274 pid
= elf_tdata (abfd
)->core
->pid
;
9279 /* If there isn't a section called NAME, make one, using
9280 data from SECT. Note, this function will generate a
9281 reference to NAME, so you shouldn't deallocate or
9285 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9289 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9292 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9296 sect2
->size
= sect
->size
;
9297 sect2
->filepos
= sect
->filepos
;
9298 sect2
->alignment_power
= sect
->alignment_power
;
9302 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9303 actually creates up to two pseudosections:
9304 - For the single-threaded case, a section named NAME, unless
9305 such a section already exists.
9306 - For the multi-threaded case, a section named "NAME/PID", where
9307 PID is elfcore_make_pid (abfd).
9308 Both pseudosections have identical contents. */
9310 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9316 char *threaded_name
;
9320 /* Build the section name. */
9322 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9323 len
= strlen (buf
) + 1;
9324 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9325 if (threaded_name
== NULL
)
9327 memcpy (threaded_name
, buf
, len
);
9329 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9334 sect
->filepos
= filepos
;
9335 sect
->alignment_power
= 2;
9337 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9341 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9344 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9350 sect
->size
= note
->descsz
- offs
;
9351 sect
->filepos
= note
->descpos
+ offs
;
9352 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9357 /* prstatus_t exists on:
9359 linux 2.[01] + glibc
9363 #if defined (HAVE_PRSTATUS_T)
9366 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9371 if (note
->descsz
== sizeof (prstatus_t
))
9375 size
= sizeof (prstat
.pr_reg
);
9376 offset
= offsetof (prstatus_t
, pr_reg
);
9377 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9379 /* Do not overwrite the core signal if it
9380 has already been set by another thread. */
9381 if (elf_tdata (abfd
)->core
->signal
== 0)
9382 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9383 if (elf_tdata (abfd
)->core
->pid
== 0)
9384 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9386 /* pr_who exists on:
9389 pr_who doesn't exist on:
9392 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9393 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9395 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9398 #if defined (HAVE_PRSTATUS32_T)
9399 else if (note
->descsz
== sizeof (prstatus32_t
))
9401 /* 64-bit host, 32-bit corefile */
9402 prstatus32_t prstat
;
9404 size
= sizeof (prstat
.pr_reg
);
9405 offset
= offsetof (prstatus32_t
, pr_reg
);
9406 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9408 /* Do not overwrite the core signal if it
9409 has already been set by another thread. */
9410 if (elf_tdata (abfd
)->core
->signal
== 0)
9411 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9412 if (elf_tdata (abfd
)->core
->pid
== 0)
9413 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9415 /* pr_who exists on:
9418 pr_who doesn't exist on:
9421 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9422 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9424 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9427 #endif /* HAVE_PRSTATUS32_T */
9430 /* Fail - we don't know how to handle any other
9431 note size (ie. data object type). */
9435 /* Make a ".reg/999" section and a ".reg" section. */
9436 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9437 size
, note
->descpos
+ offset
);
9439 #endif /* defined (HAVE_PRSTATUS_T) */
9441 /* Create a pseudosection containing the exact contents of NOTE. */
9443 elfcore_make_note_pseudosection (bfd
*abfd
,
9445 Elf_Internal_Note
*note
)
9447 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9448 note
->descsz
, note
->descpos
);
9451 /* There isn't a consistent prfpregset_t across platforms,
9452 but it doesn't matter, because we don't have to pick this
9453 data structure apart. */
9456 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9458 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9461 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9462 type of NT_PRXFPREG. Just include the whole note's contents
9466 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9468 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9471 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9472 with a note type of NT_X86_XSTATE. Just include the whole note's
9473 contents literally. */
9476 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9478 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9482 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9484 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9488 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9490 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9494 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9496 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9500 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9502 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9506 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9508 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9512 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9514 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9518 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9520 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9524 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9526 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9530 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9532 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9536 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9538 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9542 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9544 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9548 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9550 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9554 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9556 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9560 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9562 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9566 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9568 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9572 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9574 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9578 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9580 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9584 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9586 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9590 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9592 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9596 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9598 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9602 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9604 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9608 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9610 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9614 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9616 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9620 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9622 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9626 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9628 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9632 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9634 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9638 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9640 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9644 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9646 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9650 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9652 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9656 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9658 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9662 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9664 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9668 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9670 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9674 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9676 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9680 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9682 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9685 #if defined (HAVE_PRPSINFO_T)
9686 typedef prpsinfo_t elfcore_psinfo_t
;
9687 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9688 typedef prpsinfo32_t elfcore_psinfo32_t
;
9692 #if defined (HAVE_PSINFO_T)
9693 typedef psinfo_t elfcore_psinfo_t
;
9694 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9695 typedef psinfo32_t elfcore_psinfo32_t
;
9699 /* return a malloc'ed copy of a string at START which is at
9700 most MAX bytes long, possibly without a terminating '\0'.
9701 the copy will always have a terminating '\0'. */
9704 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9707 char *end
= (char *) memchr (start
, '\0', max
);
9715 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9719 memcpy (dups
, start
, len
);
9725 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9727 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9729 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9731 elfcore_psinfo_t psinfo
;
9733 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9735 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9736 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9738 elf_tdata (abfd
)->core
->program
9739 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9740 sizeof (psinfo
.pr_fname
));
9742 elf_tdata (abfd
)->core
->command
9743 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9744 sizeof (psinfo
.pr_psargs
));
9746 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9747 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9749 /* 64-bit host, 32-bit corefile */
9750 elfcore_psinfo32_t psinfo
;
9752 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9754 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9755 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9757 elf_tdata (abfd
)->core
->program
9758 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9759 sizeof (psinfo
.pr_fname
));
9761 elf_tdata (abfd
)->core
->command
9762 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9763 sizeof (psinfo
.pr_psargs
));
9769 /* Fail - we don't know how to handle any other
9770 note size (ie. data object type). */
9774 /* Note that for some reason, a spurious space is tacked
9775 onto the end of the args in some (at least one anyway)
9776 implementations, so strip it off if it exists. */
9779 char *command
= elf_tdata (abfd
)->core
->command
;
9780 int n
= strlen (command
);
9782 if (0 < n
&& command
[n
- 1] == ' ')
9783 command
[n
- 1] = '\0';
9788 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9790 #if defined (HAVE_PSTATUS_T)
9792 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9794 if (note
->descsz
== sizeof (pstatus_t
)
9795 #if defined (HAVE_PXSTATUS_T)
9796 || note
->descsz
== sizeof (pxstatus_t
)
9802 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9804 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9806 #if defined (HAVE_PSTATUS32_T)
9807 else if (note
->descsz
== sizeof (pstatus32_t
))
9809 /* 64-bit host, 32-bit corefile */
9812 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9814 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9817 /* Could grab some more details from the "representative"
9818 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9819 NT_LWPSTATUS note, presumably. */
9823 #endif /* defined (HAVE_PSTATUS_T) */
9825 #if defined (HAVE_LWPSTATUS_T)
9827 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9829 lwpstatus_t lwpstat
;
9835 if (note
->descsz
!= sizeof (lwpstat
)
9836 #if defined (HAVE_LWPXSTATUS_T)
9837 && note
->descsz
!= sizeof (lwpxstatus_t
)
9842 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9844 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9845 /* Do not overwrite the core signal if it has already been set by
9847 if (elf_tdata (abfd
)->core
->signal
== 0)
9848 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9850 /* Make a ".reg/999" section. */
9852 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9853 len
= strlen (buf
) + 1;
9854 name
= bfd_alloc (abfd
, len
);
9857 memcpy (name
, buf
, len
);
9859 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9863 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9864 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9865 sect
->filepos
= note
->descpos
9866 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9869 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9870 sect
->size
= sizeof (lwpstat
.pr_reg
);
9871 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9874 sect
->alignment_power
= 2;
9876 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9879 /* Make a ".reg2/999" section */
9881 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9882 len
= strlen (buf
) + 1;
9883 name
= bfd_alloc (abfd
, len
);
9886 memcpy (name
, buf
, len
);
9888 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9892 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9893 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9894 sect
->filepos
= note
->descpos
9895 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9898 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9899 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9900 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9903 sect
->alignment_power
= 2;
9905 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9907 #endif /* defined (HAVE_LWPSTATUS_T) */
9910 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9917 int is_active_thread
;
9920 if (note
->descsz
< 728)
9923 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9926 type
= bfd_get_32 (abfd
, note
->descdata
);
9930 case 1 /* NOTE_INFO_PROCESS */:
9931 /* FIXME: need to add ->core->command. */
9932 /* process_info.pid */
9933 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9934 /* process_info.signal */
9935 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9938 case 2 /* NOTE_INFO_THREAD */:
9939 /* Make a ".reg/999" section. */
9940 /* thread_info.tid */
9941 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9943 len
= strlen (buf
) + 1;
9944 name
= (char *) bfd_alloc (abfd
, len
);
9948 memcpy (name
, buf
, len
);
9950 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9954 /* sizeof (thread_info.thread_context) */
9956 /* offsetof (thread_info.thread_context) */
9957 sect
->filepos
= note
->descpos
+ 12;
9958 sect
->alignment_power
= 2;
9960 /* thread_info.is_active_thread */
9961 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9963 if (is_active_thread
)
9964 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9968 case 3 /* NOTE_INFO_MODULE */:
9969 /* Make a ".module/xxxxxxxx" section. */
9970 /* module_info.base_address */
9971 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9972 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9974 len
= strlen (buf
) + 1;
9975 name
= (char *) bfd_alloc (abfd
, len
);
9979 memcpy (name
, buf
, len
);
9981 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9986 sect
->size
= note
->descsz
;
9987 sect
->filepos
= note
->descpos
;
9988 sect
->alignment_power
= 2;
9999 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10001 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10003 switch (note
->type
)
10009 if (bed
->elf_backend_grok_prstatus
)
10010 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10012 #if defined (HAVE_PRSTATUS_T)
10013 return elfcore_grok_prstatus (abfd
, note
);
10018 #if defined (HAVE_PSTATUS_T)
10020 return elfcore_grok_pstatus (abfd
, note
);
10023 #if defined (HAVE_LWPSTATUS_T)
10025 return elfcore_grok_lwpstatus (abfd
, note
);
10028 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10029 return elfcore_grok_prfpreg (abfd
, note
);
10031 case NT_WIN32PSTATUS
:
10032 return elfcore_grok_win32pstatus (abfd
, note
);
10034 case NT_PRXFPREG
: /* Linux SSE extension */
10035 if (note
->namesz
== 6
10036 && strcmp (note
->namedata
, "LINUX") == 0)
10037 return elfcore_grok_prxfpreg (abfd
, note
);
10041 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10042 if (note
->namesz
== 6
10043 && strcmp (note
->namedata
, "LINUX") == 0)
10044 return elfcore_grok_xstatereg (abfd
, note
);
10049 if (note
->namesz
== 6
10050 && strcmp (note
->namedata
, "LINUX") == 0)
10051 return elfcore_grok_ppc_vmx (abfd
, note
);
10056 if (note
->namesz
== 6
10057 && strcmp (note
->namedata
, "LINUX") == 0)
10058 return elfcore_grok_ppc_vsx (abfd
, note
);
10063 if (note
->namesz
== 6
10064 && strcmp (note
->namedata
, "LINUX") == 0)
10065 return elfcore_grok_ppc_tar (abfd
, note
);
10070 if (note
->namesz
== 6
10071 && strcmp (note
->namedata
, "LINUX") == 0)
10072 return elfcore_grok_ppc_ppr (abfd
, note
);
10077 if (note
->namesz
== 6
10078 && strcmp (note
->namedata
, "LINUX") == 0)
10079 return elfcore_grok_ppc_dscr (abfd
, note
);
10084 if (note
->namesz
== 6
10085 && strcmp (note
->namedata
, "LINUX") == 0)
10086 return elfcore_grok_ppc_ebb (abfd
, note
);
10091 if (note
->namesz
== 6
10092 && strcmp (note
->namedata
, "LINUX") == 0)
10093 return elfcore_grok_ppc_pmu (abfd
, note
);
10097 case NT_PPC_TM_CGPR
:
10098 if (note
->namesz
== 6
10099 && strcmp (note
->namedata
, "LINUX") == 0)
10100 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10104 case NT_PPC_TM_CFPR
:
10105 if (note
->namesz
== 6
10106 && strcmp (note
->namedata
, "LINUX") == 0)
10107 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10111 case NT_PPC_TM_CVMX
:
10112 if (note
->namesz
== 6
10113 && strcmp (note
->namedata
, "LINUX") == 0)
10114 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10118 case NT_PPC_TM_CVSX
:
10119 if (note
->namesz
== 6
10120 && strcmp (note
->namedata
, "LINUX") == 0)
10121 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10125 case NT_PPC_TM_SPR
:
10126 if (note
->namesz
== 6
10127 && strcmp (note
->namedata
, "LINUX") == 0)
10128 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10132 case NT_PPC_TM_CTAR
:
10133 if (note
->namesz
== 6
10134 && strcmp (note
->namedata
, "LINUX") == 0)
10135 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10139 case NT_PPC_TM_CPPR
:
10140 if (note
->namesz
== 6
10141 && strcmp (note
->namedata
, "LINUX") == 0)
10142 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10146 case NT_PPC_TM_CDSCR
:
10147 if (note
->namesz
== 6
10148 && strcmp (note
->namedata
, "LINUX") == 0)
10149 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10153 case NT_S390_HIGH_GPRS
:
10154 if (note
->namesz
== 6
10155 && strcmp (note
->namedata
, "LINUX") == 0)
10156 return elfcore_grok_s390_high_gprs (abfd
, note
);
10160 case NT_S390_TIMER
:
10161 if (note
->namesz
== 6
10162 && strcmp (note
->namedata
, "LINUX") == 0)
10163 return elfcore_grok_s390_timer (abfd
, note
);
10167 case NT_S390_TODCMP
:
10168 if (note
->namesz
== 6
10169 && strcmp (note
->namedata
, "LINUX") == 0)
10170 return elfcore_grok_s390_todcmp (abfd
, note
);
10174 case NT_S390_TODPREG
:
10175 if (note
->namesz
== 6
10176 && strcmp (note
->namedata
, "LINUX") == 0)
10177 return elfcore_grok_s390_todpreg (abfd
, note
);
10182 if (note
->namesz
== 6
10183 && strcmp (note
->namedata
, "LINUX") == 0)
10184 return elfcore_grok_s390_ctrs (abfd
, note
);
10188 case NT_S390_PREFIX
:
10189 if (note
->namesz
== 6
10190 && strcmp (note
->namedata
, "LINUX") == 0)
10191 return elfcore_grok_s390_prefix (abfd
, note
);
10195 case NT_S390_LAST_BREAK
:
10196 if (note
->namesz
== 6
10197 && strcmp (note
->namedata
, "LINUX") == 0)
10198 return elfcore_grok_s390_last_break (abfd
, note
);
10202 case NT_S390_SYSTEM_CALL
:
10203 if (note
->namesz
== 6
10204 && strcmp (note
->namedata
, "LINUX") == 0)
10205 return elfcore_grok_s390_system_call (abfd
, note
);
10210 if (note
->namesz
== 6
10211 && strcmp (note
->namedata
, "LINUX") == 0)
10212 return elfcore_grok_s390_tdb (abfd
, note
);
10216 case NT_S390_VXRS_LOW
:
10217 if (note
->namesz
== 6
10218 && strcmp (note
->namedata
, "LINUX") == 0)
10219 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10223 case NT_S390_VXRS_HIGH
:
10224 if (note
->namesz
== 6
10225 && strcmp (note
->namedata
, "LINUX") == 0)
10226 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10230 case NT_S390_GS_CB
:
10231 if (note
->namesz
== 6
10232 && strcmp (note
->namedata
, "LINUX") == 0)
10233 return elfcore_grok_s390_gs_cb (abfd
, note
);
10237 case NT_S390_GS_BC
:
10238 if (note
->namesz
== 6
10239 && strcmp (note
->namedata
, "LINUX") == 0)
10240 return elfcore_grok_s390_gs_bc (abfd
, note
);
10245 if (note
->namesz
== 6
10246 && strcmp (note
->namedata
, "LINUX") == 0)
10247 return elfcore_grok_arm_vfp (abfd
, note
);
10252 if (note
->namesz
== 6
10253 && strcmp (note
->namedata
, "LINUX") == 0)
10254 return elfcore_grok_aarch_tls (abfd
, note
);
10258 case NT_ARM_HW_BREAK
:
10259 if (note
->namesz
== 6
10260 && strcmp (note
->namedata
, "LINUX") == 0)
10261 return elfcore_grok_aarch_hw_break (abfd
, note
);
10265 case NT_ARM_HW_WATCH
:
10266 if (note
->namesz
== 6
10267 && strcmp (note
->namedata
, "LINUX") == 0)
10268 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10273 if (note
->namesz
== 6
10274 && strcmp (note
->namedata
, "LINUX") == 0)
10275 return elfcore_grok_aarch_sve (abfd
, note
);
10279 case NT_ARM_PAC_MASK
:
10280 if (note
->namesz
== 6
10281 && strcmp (note
->namedata
, "LINUX") == 0)
10282 return elfcore_grok_aarch_pauth (abfd
, note
);
10288 if (bed
->elf_backend_grok_psinfo
)
10289 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10291 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10292 return elfcore_grok_psinfo (abfd
, note
);
10298 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10301 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10305 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10312 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10314 struct bfd_build_id
* build_id
;
10316 if (note
->descsz
== 0)
10319 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10320 if (build_id
== NULL
)
10323 build_id
->size
= note
->descsz
;
10324 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10325 abfd
->build_id
= build_id
;
10331 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10333 switch (note
->type
)
10338 case NT_GNU_PROPERTY_TYPE_0
:
10339 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10341 case NT_GNU_BUILD_ID
:
10342 return elfobj_grok_gnu_build_id (abfd
, note
);
10347 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10349 struct sdt_note
*cur
=
10350 (struct sdt_note
*) bfd_alloc (abfd
,
10351 sizeof (struct sdt_note
) + note
->descsz
);
10353 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10354 cur
->size
= (bfd_size_type
) note
->descsz
;
10355 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10357 elf_tdata (abfd
)->sdt_note_head
= cur
;
10363 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10365 switch (note
->type
)
10368 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10376 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10380 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10383 if (note
->descsz
< 108)
10388 if (note
->descsz
< 120)
10396 /* Check for version 1 in pr_version. */
10397 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10402 /* Skip over pr_psinfosz. */
10403 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10407 offset
+= 4; /* Padding before pr_psinfosz. */
10411 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10412 elf_tdata (abfd
)->core
->program
10413 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10416 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10417 elf_tdata (abfd
)->core
->command
10418 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10421 /* Padding before pr_pid. */
10424 /* The pr_pid field was added in version "1a". */
10425 if (note
->descsz
< offset
+ 4)
10428 elf_tdata (abfd
)->core
->pid
10429 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10435 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10441 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10442 Also compute minimum size of this note. */
10443 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10447 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10451 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10452 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10459 if (note
->descsz
< min_size
)
10462 /* Check for version 1 in pr_version. */
10463 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10466 /* Extract size of pr_reg from pr_gregsetsz. */
10467 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10468 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10470 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10475 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10479 /* Skip over pr_osreldate. */
10482 /* Read signal from pr_cursig. */
10483 if (elf_tdata (abfd
)->core
->signal
== 0)
10484 elf_tdata (abfd
)->core
->signal
10485 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10488 /* Read TID from pr_pid. */
10489 elf_tdata (abfd
)->core
->lwpid
10490 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10493 /* Padding before pr_reg. */
10494 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10497 /* Make sure that there is enough data remaining in the note. */
10498 if ((note
->descsz
- offset
) < size
)
10501 /* Make a ".reg/999" section and a ".reg" section. */
10502 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10503 size
, note
->descpos
+ offset
);
10507 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10509 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10511 switch (note
->type
)
10514 if (bed
->elf_backend_grok_freebsd_prstatus
)
10515 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10517 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10520 return elfcore_grok_prfpreg (abfd
, note
);
10523 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10525 case NT_FREEBSD_THRMISC
:
10526 if (note
->namesz
== 8)
10527 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10531 case NT_FREEBSD_PROCSTAT_PROC
:
10532 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10535 case NT_FREEBSD_PROCSTAT_FILES
:
10536 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10539 case NT_FREEBSD_PROCSTAT_VMMAP
:
10540 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10543 case NT_FREEBSD_PROCSTAT_AUXV
:
10544 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10546 case NT_X86_XSTATE
:
10547 if (note
->namesz
== 8)
10548 return elfcore_grok_xstatereg (abfd
, note
);
10552 case NT_FREEBSD_PTLWPINFO
:
10553 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10557 return elfcore_grok_arm_vfp (abfd
, note
);
10565 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10569 cp
= strchr (note
->namedata
, '@');
10572 *lwpidp
= atoi(cp
+ 1);
10579 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10581 if (note
->descsz
<= 0x7c + 31)
10584 /* Signal number at offset 0x08. */
10585 elf_tdata (abfd
)->core
->signal
10586 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10588 /* Process ID at offset 0x50. */
10589 elf_tdata (abfd
)->core
->pid
10590 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10592 /* Command name at 0x7c (max 32 bytes, including nul). */
10593 elf_tdata (abfd
)->core
->command
10594 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10596 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10601 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10605 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10606 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10608 switch (note
->type
)
10610 case NT_NETBSDCORE_PROCINFO
:
10611 /* NetBSD-specific core "procinfo". Note that we expect to
10612 find this note before any of the others, which is fine,
10613 since the kernel writes this note out first when it
10614 creates a core file. */
10615 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10616 #ifdef NT_NETBSDCORE_AUXV
10617 case NT_NETBSDCORE_AUXV
:
10618 /* NetBSD-specific Elf Auxiliary Vector data. */
10619 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10625 /* As of March 2017 there are no other machine-independent notes
10626 defined for NetBSD core files. If the note type is less
10627 than the start of the machine-dependent note types, we don't
10630 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10634 switch (bfd_get_arch (abfd
))
10636 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10637 PT_GETFPREGS == mach+2. */
10639 case bfd_arch_alpha
:
10640 case bfd_arch_sparc
:
10641 switch (note
->type
)
10643 case NT_NETBSDCORE_FIRSTMACH
+0:
10644 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10646 case NT_NETBSDCORE_FIRSTMACH
+2:
10647 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10653 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10654 There's also old PT___GETREGS40 == mach + 1 for old reg
10655 structure which lacks GBR. */
10658 switch (note
->type
)
10660 case NT_NETBSDCORE_FIRSTMACH
+3:
10661 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10663 case NT_NETBSDCORE_FIRSTMACH
+5:
10664 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10670 /* On all other arch's, PT_GETREGS == mach+1 and
10671 PT_GETFPREGS == mach+3. */
10674 switch (note
->type
)
10676 case NT_NETBSDCORE_FIRSTMACH
+1:
10677 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10679 case NT_NETBSDCORE_FIRSTMACH
+3:
10680 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10690 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10692 if (note
->descsz
<= 0x48 + 31)
10695 /* Signal number at offset 0x08. */
10696 elf_tdata (abfd
)->core
->signal
10697 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10699 /* Process ID at offset 0x20. */
10700 elf_tdata (abfd
)->core
->pid
10701 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10703 /* Command name at 0x48 (max 32 bytes, including nul). */
10704 elf_tdata (abfd
)->core
->command
10705 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10711 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10713 if (note
->type
== NT_OPENBSD_PROCINFO
)
10714 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10716 if (note
->type
== NT_OPENBSD_REGS
)
10717 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10719 if (note
->type
== NT_OPENBSD_FPREGS
)
10720 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10722 if (note
->type
== NT_OPENBSD_XFPREGS
)
10723 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10725 if (note
->type
== NT_OPENBSD_AUXV
)
10726 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10728 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10730 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10735 sect
->size
= note
->descsz
;
10736 sect
->filepos
= note
->descpos
;
10737 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10746 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10748 void *ddata
= note
->descdata
;
10755 if (note
->descsz
< 16)
10758 /* nto_procfs_status 'pid' field is at offset 0. */
10759 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10761 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10762 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10764 /* nto_procfs_status 'flags' field is at offset 8. */
10765 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10767 /* nto_procfs_status 'what' field is at offset 14. */
10768 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10770 elf_tdata (abfd
)->core
->signal
= sig
;
10771 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10774 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10775 do not come from signals so we make sure we set the current
10776 thread just in case. */
10777 if (flags
& 0x00000080)
10778 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10780 /* Make a ".qnx_core_status/%d" section. */
10781 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10783 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10786 strcpy (name
, buf
);
10788 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10792 sect
->size
= note
->descsz
;
10793 sect
->filepos
= note
->descpos
;
10794 sect
->alignment_power
= 2;
10796 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10800 elfcore_grok_nto_regs (bfd
*abfd
,
10801 Elf_Internal_Note
*note
,
10809 /* Make a "(base)/%d" section. */
10810 sprintf (buf
, "%s/%ld", base
, tid
);
10812 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10815 strcpy (name
, buf
);
10817 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10821 sect
->size
= note
->descsz
;
10822 sect
->filepos
= note
->descpos
;
10823 sect
->alignment_power
= 2;
10825 /* This is the current thread. */
10826 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10827 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10832 #define BFD_QNT_CORE_INFO 7
10833 #define BFD_QNT_CORE_STATUS 8
10834 #define BFD_QNT_CORE_GREG 9
10835 #define BFD_QNT_CORE_FPREG 10
10838 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10840 /* Every GREG section has a STATUS section before it. Store the
10841 tid from the previous call to pass down to the next gregs
10843 static long tid
= 1;
10845 switch (note
->type
)
10847 case BFD_QNT_CORE_INFO
:
10848 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10849 case BFD_QNT_CORE_STATUS
:
10850 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10851 case BFD_QNT_CORE_GREG
:
10852 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10853 case BFD_QNT_CORE_FPREG
:
10854 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10861 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10867 /* Use note name as section name. */
10868 len
= note
->namesz
;
10869 name
= (char *) bfd_alloc (abfd
, len
);
10872 memcpy (name
, note
->namedata
, len
);
10873 name
[len
- 1] = '\0';
10875 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10879 sect
->size
= note
->descsz
;
10880 sect
->filepos
= note
->descpos
;
10881 sect
->alignment_power
= 1;
10886 /* Function: elfcore_write_note
10889 buffer to hold note, and current size of buffer
10893 size of data for note
10895 Writes note to end of buffer. ELF64 notes are written exactly as
10896 for ELF32, despite the current (as of 2006) ELF gabi specifying
10897 that they ought to have 8-byte namesz and descsz field, and have
10898 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10901 Pointer to realloc'd buffer, *BUFSIZ updated. */
10904 elfcore_write_note (bfd
*abfd
,
10912 Elf_External_Note
*xnp
;
10919 namesz
= strlen (name
) + 1;
10921 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10923 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10926 dest
= buf
+ *bufsiz
;
10927 *bufsiz
+= newspace
;
10928 xnp
= (Elf_External_Note
*) dest
;
10929 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10930 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10931 H_PUT_32 (abfd
, type
, xnp
->type
);
10935 memcpy (dest
, name
, namesz
);
10943 memcpy (dest
, input
, size
);
10953 /* gcc-8 warns (*) on all the strncpy calls in this function about
10954 possible string truncation. The "truncation" is not a bug. We
10955 have an external representation of structs with fields that are not
10956 necessarily NULL terminated and corresponding internal
10957 representation fields that are one larger so that they can always
10958 be NULL terminated.
10959 gcc versions between 4.2 and 4.6 do not allow pragma control of
10960 diagnostics inside functions, giving a hard error if you try to use
10961 the finer control available with later versions.
10962 gcc prior to 4.2 warns about diagnostic push and pop.
10963 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10964 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10965 (*) Depending on your system header files! */
10966 #if GCC_VERSION >= 8000
10967 # pragma GCC diagnostic push
10968 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10971 elfcore_write_prpsinfo (bfd
*abfd
,
10975 const char *psargs
)
10977 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10979 if (bed
->elf_backend_write_core_note
!= NULL
)
10982 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10983 NT_PRPSINFO
, fname
, psargs
);
10988 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10989 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10990 if (bed
->s
->elfclass
== ELFCLASS32
)
10992 # if defined (HAVE_PSINFO32_T)
10994 int note_type
= NT_PSINFO
;
10997 int note_type
= NT_PRPSINFO
;
11000 memset (&data
, 0, sizeof (data
));
11001 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11002 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11003 return elfcore_write_note (abfd
, buf
, bufsiz
,
11004 "CORE", note_type
, &data
, sizeof (data
));
11009 # if defined (HAVE_PSINFO_T)
11011 int note_type
= NT_PSINFO
;
11014 int note_type
= NT_PRPSINFO
;
11017 memset (&data
, 0, sizeof (data
));
11018 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11019 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11020 return elfcore_write_note (abfd
, buf
, bufsiz
,
11021 "CORE", note_type
, &data
, sizeof (data
));
11023 #endif /* PSINFO_T or PRPSINFO_T */
11028 #if GCC_VERSION >= 8000
11029 # pragma GCC diagnostic pop
11033 elfcore_write_linux_prpsinfo32
11034 (bfd
*abfd
, char *buf
, int *bufsiz
,
11035 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11037 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11039 struct elf_external_linux_prpsinfo32_ugid16 data
;
11041 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11042 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11043 &data
, sizeof (data
));
11047 struct elf_external_linux_prpsinfo32_ugid32 data
;
11049 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11050 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11051 &data
, sizeof (data
));
11056 elfcore_write_linux_prpsinfo64
11057 (bfd
*abfd
, char *buf
, int *bufsiz
,
11058 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11060 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11062 struct elf_external_linux_prpsinfo64_ugid16 data
;
11064 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11065 return elfcore_write_note (abfd
, buf
, bufsiz
,
11066 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11070 struct elf_external_linux_prpsinfo64_ugid32 data
;
11072 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11073 return elfcore_write_note (abfd
, buf
, bufsiz
,
11074 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11079 elfcore_write_prstatus (bfd
*abfd
,
11086 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11088 if (bed
->elf_backend_write_core_note
!= NULL
)
11091 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11093 pid
, cursig
, gregs
);
11098 #if defined (HAVE_PRSTATUS_T)
11099 #if defined (HAVE_PRSTATUS32_T)
11100 if (bed
->s
->elfclass
== ELFCLASS32
)
11102 prstatus32_t prstat
;
11104 memset (&prstat
, 0, sizeof (prstat
));
11105 prstat
.pr_pid
= pid
;
11106 prstat
.pr_cursig
= cursig
;
11107 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11108 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11109 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11116 memset (&prstat
, 0, sizeof (prstat
));
11117 prstat
.pr_pid
= pid
;
11118 prstat
.pr_cursig
= cursig
;
11119 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11120 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11121 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11123 #endif /* HAVE_PRSTATUS_T */
11129 #if defined (HAVE_LWPSTATUS_T)
11131 elfcore_write_lwpstatus (bfd
*abfd
,
11138 lwpstatus_t lwpstat
;
11139 const char *note_name
= "CORE";
11141 memset (&lwpstat
, 0, sizeof (lwpstat
));
11142 lwpstat
.pr_lwpid
= pid
>> 16;
11143 lwpstat
.pr_cursig
= cursig
;
11144 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11145 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11146 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11147 #if !defined(gregs)
11148 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11149 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11151 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11152 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11155 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11156 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11158 #endif /* HAVE_LWPSTATUS_T */
11160 #if defined (HAVE_PSTATUS_T)
11162 elfcore_write_pstatus (bfd
*abfd
,
11166 int cursig ATTRIBUTE_UNUSED
,
11167 const void *gregs ATTRIBUTE_UNUSED
)
11169 const char *note_name
= "CORE";
11170 #if defined (HAVE_PSTATUS32_T)
11171 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11173 if (bed
->s
->elfclass
== ELFCLASS32
)
11177 memset (&pstat
, 0, sizeof (pstat
));
11178 pstat
.pr_pid
= pid
& 0xffff;
11179 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11180 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11188 memset (&pstat
, 0, sizeof (pstat
));
11189 pstat
.pr_pid
= pid
& 0xffff;
11190 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11191 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11195 #endif /* HAVE_PSTATUS_T */
11198 elfcore_write_prfpreg (bfd
*abfd
,
11201 const void *fpregs
,
11204 const char *note_name
= "CORE";
11205 return elfcore_write_note (abfd
, buf
, bufsiz
,
11206 note_name
, NT_FPREGSET
, fpregs
, size
);
11210 elfcore_write_prxfpreg (bfd
*abfd
,
11213 const void *xfpregs
,
11216 char *note_name
= "LINUX";
11217 return elfcore_write_note (abfd
, buf
, bufsiz
,
11218 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11222 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11223 const void *xfpregs
, int size
)
11226 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11227 note_name
= "FreeBSD";
11229 note_name
= "LINUX";
11230 return elfcore_write_note (abfd
, buf
, bufsiz
,
11231 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11235 elfcore_write_ppc_vmx (bfd
*abfd
,
11238 const void *ppc_vmx
,
11241 char *note_name
= "LINUX";
11242 return elfcore_write_note (abfd
, buf
, bufsiz
,
11243 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11247 elfcore_write_ppc_vsx (bfd
*abfd
,
11250 const void *ppc_vsx
,
11253 char *note_name
= "LINUX";
11254 return elfcore_write_note (abfd
, buf
, bufsiz
,
11255 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11259 elfcore_write_ppc_tar (bfd
*abfd
,
11262 const void *ppc_tar
,
11265 char *note_name
= "LINUX";
11266 return elfcore_write_note (abfd
, buf
, bufsiz
,
11267 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11271 elfcore_write_ppc_ppr (bfd
*abfd
,
11274 const void *ppc_ppr
,
11277 char *note_name
= "LINUX";
11278 return elfcore_write_note (abfd
, buf
, bufsiz
,
11279 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11283 elfcore_write_ppc_dscr (bfd
*abfd
,
11286 const void *ppc_dscr
,
11289 char *note_name
= "LINUX";
11290 return elfcore_write_note (abfd
, buf
, bufsiz
,
11291 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11295 elfcore_write_ppc_ebb (bfd
*abfd
,
11298 const void *ppc_ebb
,
11301 char *note_name
= "LINUX";
11302 return elfcore_write_note (abfd
, buf
, bufsiz
,
11303 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11307 elfcore_write_ppc_pmu (bfd
*abfd
,
11310 const void *ppc_pmu
,
11313 char *note_name
= "LINUX";
11314 return elfcore_write_note (abfd
, buf
, bufsiz
,
11315 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11319 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11322 const void *ppc_tm_cgpr
,
11325 char *note_name
= "LINUX";
11326 return elfcore_write_note (abfd
, buf
, bufsiz
,
11327 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11331 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11334 const void *ppc_tm_cfpr
,
11337 char *note_name
= "LINUX";
11338 return elfcore_write_note (abfd
, buf
, bufsiz
,
11339 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11343 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11346 const void *ppc_tm_cvmx
,
11349 char *note_name
= "LINUX";
11350 return elfcore_write_note (abfd
, buf
, bufsiz
,
11351 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11355 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11358 const void *ppc_tm_cvsx
,
11361 char *note_name
= "LINUX";
11362 return elfcore_write_note (abfd
, buf
, bufsiz
,
11363 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11367 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11370 const void *ppc_tm_spr
,
11373 char *note_name
= "LINUX";
11374 return elfcore_write_note (abfd
, buf
, bufsiz
,
11375 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11379 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11382 const void *ppc_tm_ctar
,
11385 char *note_name
= "LINUX";
11386 return elfcore_write_note (abfd
, buf
, bufsiz
,
11387 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11391 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11394 const void *ppc_tm_cppr
,
11397 char *note_name
= "LINUX";
11398 return elfcore_write_note (abfd
, buf
, bufsiz
,
11399 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11403 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11406 const void *ppc_tm_cdscr
,
11409 char *note_name
= "LINUX";
11410 return elfcore_write_note (abfd
, buf
, bufsiz
,
11411 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11415 elfcore_write_s390_high_gprs (bfd
*abfd
,
11418 const void *s390_high_gprs
,
11421 char *note_name
= "LINUX";
11422 return elfcore_write_note (abfd
, buf
, bufsiz
,
11423 note_name
, NT_S390_HIGH_GPRS
,
11424 s390_high_gprs
, size
);
11428 elfcore_write_s390_timer (bfd
*abfd
,
11431 const void *s390_timer
,
11434 char *note_name
= "LINUX";
11435 return elfcore_write_note (abfd
, buf
, bufsiz
,
11436 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11440 elfcore_write_s390_todcmp (bfd
*abfd
,
11443 const void *s390_todcmp
,
11446 char *note_name
= "LINUX";
11447 return elfcore_write_note (abfd
, buf
, bufsiz
,
11448 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11452 elfcore_write_s390_todpreg (bfd
*abfd
,
11455 const void *s390_todpreg
,
11458 char *note_name
= "LINUX";
11459 return elfcore_write_note (abfd
, buf
, bufsiz
,
11460 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11464 elfcore_write_s390_ctrs (bfd
*abfd
,
11467 const void *s390_ctrs
,
11470 char *note_name
= "LINUX";
11471 return elfcore_write_note (abfd
, buf
, bufsiz
,
11472 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11476 elfcore_write_s390_prefix (bfd
*abfd
,
11479 const void *s390_prefix
,
11482 char *note_name
= "LINUX";
11483 return elfcore_write_note (abfd
, buf
, bufsiz
,
11484 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11488 elfcore_write_s390_last_break (bfd
*abfd
,
11491 const void *s390_last_break
,
11494 char *note_name
= "LINUX";
11495 return elfcore_write_note (abfd
, buf
, bufsiz
,
11496 note_name
, NT_S390_LAST_BREAK
,
11497 s390_last_break
, size
);
11501 elfcore_write_s390_system_call (bfd
*abfd
,
11504 const void *s390_system_call
,
11507 char *note_name
= "LINUX";
11508 return elfcore_write_note (abfd
, buf
, bufsiz
,
11509 note_name
, NT_S390_SYSTEM_CALL
,
11510 s390_system_call
, size
);
11514 elfcore_write_s390_tdb (bfd
*abfd
,
11517 const void *s390_tdb
,
11520 char *note_name
= "LINUX";
11521 return elfcore_write_note (abfd
, buf
, bufsiz
,
11522 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11526 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11529 const void *s390_vxrs_low
,
11532 char *note_name
= "LINUX";
11533 return elfcore_write_note (abfd
, buf
, bufsiz
,
11534 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11538 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11541 const void *s390_vxrs_high
,
11544 char *note_name
= "LINUX";
11545 return elfcore_write_note (abfd
, buf
, bufsiz
,
11546 note_name
, NT_S390_VXRS_HIGH
,
11547 s390_vxrs_high
, size
);
11551 elfcore_write_s390_gs_cb (bfd
*abfd
,
11554 const void *s390_gs_cb
,
11557 char *note_name
= "LINUX";
11558 return elfcore_write_note (abfd
, buf
, bufsiz
,
11559 note_name
, NT_S390_GS_CB
,
11564 elfcore_write_s390_gs_bc (bfd
*abfd
,
11567 const void *s390_gs_bc
,
11570 char *note_name
= "LINUX";
11571 return elfcore_write_note (abfd
, buf
, bufsiz
,
11572 note_name
, NT_S390_GS_BC
,
11577 elfcore_write_arm_vfp (bfd
*abfd
,
11580 const void *arm_vfp
,
11583 char *note_name
= "LINUX";
11584 return elfcore_write_note (abfd
, buf
, bufsiz
,
11585 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11589 elfcore_write_aarch_tls (bfd
*abfd
,
11592 const void *aarch_tls
,
11595 char *note_name
= "LINUX";
11596 return elfcore_write_note (abfd
, buf
, bufsiz
,
11597 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11601 elfcore_write_aarch_hw_break (bfd
*abfd
,
11604 const void *aarch_hw_break
,
11607 char *note_name
= "LINUX";
11608 return elfcore_write_note (abfd
, buf
, bufsiz
,
11609 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11613 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11616 const void *aarch_hw_watch
,
11619 char *note_name
= "LINUX";
11620 return elfcore_write_note (abfd
, buf
, bufsiz
,
11621 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11625 elfcore_write_aarch_sve (bfd
*abfd
,
11628 const void *aarch_sve
,
11631 char *note_name
= "LINUX";
11632 return elfcore_write_note (abfd
, buf
, bufsiz
,
11633 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11637 elfcore_write_aarch_pauth (bfd
*abfd
,
11640 const void *aarch_pauth
,
11643 char *note_name
= "LINUX";
11644 return elfcore_write_note (abfd
, buf
, bufsiz
,
11645 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11649 elfcore_write_register_note (bfd
*abfd
,
11652 const char *section
,
11656 if (strcmp (section
, ".reg2") == 0)
11657 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11658 if (strcmp (section
, ".reg-xfp") == 0)
11659 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11660 if (strcmp (section
, ".reg-xstate") == 0)
11661 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11662 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11663 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11664 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11665 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11666 if (strcmp (section
, ".reg-ppc-tar") == 0)
11667 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11668 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11669 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11670 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11671 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11672 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11673 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11674 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11675 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11676 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11677 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11678 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11679 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11680 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11681 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11682 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11683 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11684 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11685 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11686 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11687 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11688 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11689 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11690 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11691 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11692 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11693 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11694 if (strcmp (section
, ".reg-s390-timer") == 0)
11695 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11696 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11697 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11698 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11699 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11700 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11701 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11702 if (strcmp (section
, ".reg-s390-prefix") == 0)
11703 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11704 if (strcmp (section
, ".reg-s390-last-break") == 0)
11705 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11706 if (strcmp (section
, ".reg-s390-system-call") == 0)
11707 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11708 if (strcmp (section
, ".reg-s390-tdb") == 0)
11709 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11710 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11711 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11712 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11713 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11714 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11715 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11716 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11717 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11718 if (strcmp (section
, ".reg-arm-vfp") == 0)
11719 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11720 if (strcmp (section
, ".reg-aarch-tls") == 0)
11721 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11722 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11723 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11724 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11725 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11726 if (strcmp (section
, ".reg-aarch-sve") == 0)
11727 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11728 if (strcmp (section
, ".reg-aarch-pauth") == 0)
11729 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
11734 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11739 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11740 gABI specifies that PT_NOTE alignment should be aligned to 4
11741 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11742 align is less than 4, we use 4 byte alignment. */
11745 if (align
!= 4 && align
!= 8)
11749 while (p
< buf
+ size
)
11751 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11752 Elf_Internal_Note in
;
11754 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11757 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11759 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11760 in
.namedata
= xnp
->name
;
11761 if (in
.namesz
> buf
- in
.namedata
+ size
)
11764 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11765 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11766 in
.descpos
= offset
+ (in
.descdata
- buf
);
11768 && (in
.descdata
>= buf
+ size
11769 || in
.descsz
> buf
- in
.descdata
+ size
))
11772 switch (bfd_get_format (abfd
))
11779 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11782 const char * string
;
11784 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11788 GROKER_ELEMENT ("", elfcore_grok_note
),
11789 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11790 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11791 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11792 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11793 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
11795 #undef GROKER_ELEMENT
11798 for (i
= ARRAY_SIZE (grokers
); i
--;)
11800 if (in
.namesz
>= grokers
[i
].len
11801 && strncmp (in
.namedata
, grokers
[i
].string
,
11802 grokers
[i
].len
) == 0)
11804 if (! grokers
[i
].func (abfd
, & in
))
11813 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
11815 if (! elfobj_grok_gnu_note (abfd
, &in
))
11818 else if (in
.namesz
== sizeof "stapsdt"
11819 && strcmp (in
.namedata
, "stapsdt") == 0)
11821 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
11827 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
11834 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
11839 if (size
== 0 || (size
+ 1) == 0)
11842 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
11845 buf
= (char *) bfd_malloc (size
+ 1);
11849 /* PR 17512: file: ec08f814
11850 0-termintate the buffer so that string searches will not overflow. */
11853 if (bfd_bread (buf
, size
, abfd
) != size
11854 || !elf_parse_notes (abfd
, buf
, size
, offset
, align
))
11864 /* Providing external access to the ELF program header table. */
11866 /* Return an upper bound on the number of bytes required to store a
11867 copy of ABFD's program header table entries. Return -1 if an error
11868 occurs; bfd_get_error will return an appropriate code. */
11871 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
11873 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11875 bfd_set_error (bfd_error_wrong_format
);
11879 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
11882 /* Copy ABFD's program header table entries to *PHDRS. The entries
11883 will be stored as an array of Elf_Internal_Phdr structures, as
11884 defined in include/elf/internal.h. To find out how large the
11885 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11887 Return the number of program header table entries read, or -1 if an
11888 error occurs; bfd_get_error will return an appropriate code. */
11891 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
11895 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11897 bfd_set_error (bfd_error_wrong_format
);
11901 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
11902 if (num_phdrs
!= 0)
11903 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
11904 num_phdrs
* sizeof (Elf_Internal_Phdr
));
11909 enum elf_reloc_type_class
11910 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11911 const asection
*rel_sec ATTRIBUTE_UNUSED
,
11912 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
11914 return reloc_class_normal
;
11917 /* For RELA architectures, return the relocation value for a
11918 relocation against a local symbol. */
11921 _bfd_elf_rela_local_sym (bfd
*abfd
,
11922 Elf_Internal_Sym
*sym
,
11924 Elf_Internal_Rela
*rel
)
11926 asection
*sec
= *psec
;
11927 bfd_vma relocation
;
11929 relocation
= (sec
->output_section
->vma
11930 + sec
->output_offset
11932 if ((sec
->flags
& SEC_MERGE
)
11933 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
11934 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11937 _bfd_merged_section_offset (abfd
, psec
,
11938 elf_section_data (sec
)->sec_info
,
11939 sym
->st_value
+ rel
->r_addend
);
11942 /* If we have changed the section, and our original section is
11943 marked with SEC_EXCLUDE, it means that the original
11944 SEC_MERGE section has been completely subsumed in some
11945 other SEC_MERGE section. In this case, we need to leave
11946 some info around for --emit-relocs. */
11947 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
11948 sec
->kept_section
= *psec
;
11951 rel
->r_addend
-= relocation
;
11952 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
11958 _bfd_elf_rel_local_sym (bfd
*abfd
,
11959 Elf_Internal_Sym
*sym
,
11963 asection
*sec
= *psec
;
11965 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
11966 return sym
->st_value
+ addend
;
11968 return _bfd_merged_section_offset (abfd
, psec
,
11969 elf_section_data (sec
)->sec_info
,
11970 sym
->st_value
+ addend
);
11973 /* Adjust an address within a section. Given OFFSET within SEC, return
11974 the new offset within the section, based upon changes made to the
11975 section. Returns -1 if the offset is now invalid.
11976 The offset (in abnd out) is in target sized bytes, however big a
11980 _bfd_elf_section_offset (bfd
*abfd
,
11981 struct bfd_link_info
*info
,
11985 switch (sec
->sec_info_type
)
11987 case SEC_INFO_TYPE_STABS
:
11988 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
11990 case SEC_INFO_TYPE_EH_FRAME
:
11991 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
11994 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
11996 /* Reverse the offset. */
11997 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11998 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12000 /* address_size and sec->size are in octets. Convert
12001 to bytes before subtracting the original offset. */
12002 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
12008 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12009 reconstruct an ELF file by reading the segments out of remote memory
12010 based on the ELF file header at EHDR_VMA and the ELF program headers it
12011 points to. If not null, *LOADBASEP is filled in with the difference
12012 between the VMAs from which the segments were read, and the VMAs the
12013 file headers (and hence BFD's idea of each section's VMA) put them at.
12015 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12016 remote memory at target address VMA into the local buffer at MYADDR; it
12017 should return zero on success or an `errno' code on failure. TEMPL must
12018 be a BFD for an ELF target with the word size and byte order found in
12019 the remote memory. */
12022 bfd_elf_bfd_from_remote_memory
12025 bfd_size_type size
,
12026 bfd_vma
*loadbasep
,
12027 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12029 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12030 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12034 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12035 long symcount ATTRIBUTE_UNUSED
,
12036 asymbol
**syms ATTRIBUTE_UNUSED
,
12041 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12044 const char *relplt_name
;
12045 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12049 Elf_Internal_Shdr
*hdr
;
12055 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12058 if (dynsymcount
<= 0)
12061 if (!bed
->plt_sym_val
)
12064 relplt_name
= bed
->relplt_name
;
12065 if (relplt_name
== NULL
)
12066 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12067 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12068 if (relplt
== NULL
)
12071 hdr
= &elf_section_data (relplt
)->this_hdr
;
12072 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12073 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12076 plt
= bfd_get_section_by_name (abfd
, ".plt");
12080 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12081 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12084 count
= relplt
->size
/ hdr
->sh_entsize
;
12085 size
= count
* sizeof (asymbol
);
12086 p
= relplt
->relocation
;
12087 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12089 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12090 if (p
->addend
!= 0)
12093 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12095 size
+= sizeof ("+0x") - 1 + 8;
12100 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12104 names
= (char *) (s
+ count
);
12105 p
= relplt
->relocation
;
12107 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12112 addr
= bed
->plt_sym_val (i
, plt
, p
);
12113 if (addr
== (bfd_vma
) -1)
12116 *s
= **p
->sym_ptr_ptr
;
12117 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12118 we are defining a symbol, ensure one of them is set. */
12119 if ((s
->flags
& BSF_LOCAL
) == 0)
12120 s
->flags
|= BSF_GLOBAL
;
12121 s
->flags
|= BSF_SYNTHETIC
;
12123 s
->value
= addr
- plt
->vma
;
12126 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12127 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12129 if (p
->addend
!= 0)
12133 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12134 names
+= sizeof ("+0x") - 1;
12135 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12136 for (a
= buf
; *a
== '0'; ++a
)
12139 memcpy (names
, a
, len
);
12142 memcpy (names
, "@plt", sizeof ("@plt"));
12143 names
+= sizeof ("@plt");
12150 /* It is only used by x86-64 so far.
12151 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12152 but current usage would allow all of _bfd_std_section to be zero. */
12153 static const asymbol lcomm_sym
12154 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12155 asection _bfd_elf_large_com_section
12156 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12157 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12160 _bfd_elf_post_process_headers (bfd
*abfd ATTRIBUTE_UNUSED
,
12161 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
12166 _bfd_elf_final_write_processing (bfd
*abfd
)
12168 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12170 i_ehdrp
= elf_elfheader (abfd
);
12172 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12173 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12175 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12176 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12177 STB_GNU_UNIQUE binding. */
12178 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12180 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12181 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12182 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12183 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12185 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12186 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12187 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12188 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12189 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12190 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12191 bfd_set_error (bfd_error_bad_value
);
12199 /* Return TRUE for ELF symbol types that represent functions.
12200 This is the default version of this function, which is sufficient for
12201 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12204 _bfd_elf_is_function_type (unsigned int type
)
12206 return (type
== STT_FUNC
12207 || type
== STT_GNU_IFUNC
);
12210 /* If the ELF symbol SYM might be a function in SEC, return the
12211 function size and set *CODE_OFF to the function's entry point,
12212 otherwise return zero. */
12215 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12218 bfd_size_type size
;
12220 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12221 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12222 || sym
->section
!= sec
)
12225 *code_off
= sym
->value
;
12227 if (!(sym
->flags
& BSF_SYNTHETIC
))
12228 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;