1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2025 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 bool assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bool swap_out_syms (bfd
*, struct elf_strtab_hash
**, int,
55 struct bfd_link_info
*);
56 static bool elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
57 file_ptr offset
, size_t align
);
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
201 for (const unsigned char *name
= (const unsigned char *) namearg
;
204 h
= (h
<< 4) + *name
;
205 h
^= (h
>> 24) & 0xf0;
207 return h
& 0x0fffffff;
210 /* DT_GNU_HASH hash function. Do not change this function; you will
211 cause invalid hash tables to be generated. */
214 bfd_elf_gnu_hash (const char *namearg
)
218 for (const unsigned char *name
= (const unsigned char *) namearg
;
220 h
= (h
<< 5) + h
+ *name
;
224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 the object_id field of an elf_obj_tdata field set. */
227 bfd_elf_allocate_object (bfd
*abfd
,
230 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
231 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
232 if (abfd
->tdata
.any
== NULL
)
235 elf_object_id (abfd
) = get_elf_backend_data (abfd
)->target_id
;
236 if (abfd
->direction
!= read_direction
)
238 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
241 elf_tdata (abfd
)->o
= o
;
242 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
249 bfd_elf_make_object (bfd
*abfd
)
251 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
));
255 bfd_elf_mkcorefile (bfd
*abfd
)
257 /* I think this can be done just like an object file. */
258 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
260 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
261 return elf_tdata (abfd
)->core
!= NULL
;
265 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
267 Elf_Internal_Shdr
**i_shdrp
;
268 bfd_byte
*shstrtab
= NULL
;
270 bfd_size_type shstrtabsize
;
272 i_shdrp
= elf_elfsections (abfd
);
274 || shindex
>= elf_numsections (abfd
)
275 || i_shdrp
[shindex
] == 0)
278 shstrtab
= i_shdrp
[shindex
]->contents
;
279 if (shstrtab
== NULL
)
281 /* No cached one, attempt to read, and cache what we read. */
282 offset
= i_shdrp
[shindex
]->sh_offset
;
283 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
285 if (shstrtabsize
== 0
286 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
287 || (shstrtab
= _bfd_mmap_persistent (abfd
, shstrtabsize
)) == NULL
)
289 /* Once we've failed to read it, make sure we don't keep
290 trying. Otherwise, we'll keep allocating space for
291 the string table over and over. */
292 i_shdrp
[shindex
]->sh_size
= 0;
294 else if (shstrtab
[shstrtabsize
- 1] != 0)
296 /* It is an error if a string table isn't terminated. */
298 /* xgettext:c-format */
299 (_("%pB: string table [%u] is corrupt"), abfd
, shindex
);
300 shstrtab
[shstrtabsize
- 1] = 0;
302 i_shdrp
[shindex
]->contents
= shstrtab
;
304 return (char *) shstrtab
;
308 bfd_elf_string_from_elf_section (bfd
*abfd
,
309 unsigned int shindex
,
310 unsigned int strindex
)
312 Elf_Internal_Shdr
*hdr
;
317 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
320 hdr
= elf_elfsections (abfd
)[shindex
];
322 if (hdr
->contents
== NULL
)
324 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
326 /* PR 17512: file: f057ec89. */
327 /* xgettext:c-format */
328 _bfd_error_handler (_("%pB: attempt to load strings from"
329 " a non-string section (number %d)"),
334 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
339 /* PR 24273: The string section's contents may have already
340 been loaded elsewhere, eg because a corrupt file has the
341 string section index in the ELF header pointing at a group
342 section. So be paranoid, and test that the last byte of
343 the section is zero. */
344 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
348 if (strindex
>= hdr
->sh_size
)
350 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
352 /* xgettext:c-format */
353 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
354 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
355 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
357 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
361 return ((char *) hdr
->contents
) + strindex
;
364 /* Read and convert symbols to internal format.
365 SYMCOUNT specifies the number of symbols to read, starting from
366 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367 are non-NULL, they are used to store the internal symbols, external
368 symbols, and symbol section index extensions, respectively.
369 Returns a pointer to the internal symbol buffer (malloced if necessary)
370 or NULL if there were no symbols or some kind of problem. */
373 bfd_elf_get_elf_syms (bfd
*ibfd
,
374 Elf_Internal_Shdr
*symtab_hdr
,
377 Elf_Internal_Sym
*intsym_buf
,
379 Elf_External_Sym_Shndx
*extshndx_buf
)
381 Elf_Internal_Shdr
*shndx_hdr
;
383 const bfd_byte
*esym
;
384 Elf_External_Sym_Shndx
*alloc_extshndx
;
385 Elf_External_Sym_Shndx
*shndx
;
386 Elf_Internal_Sym
*alloc_intsym
;
387 Elf_Internal_Sym
*isym
;
388 Elf_Internal_Sym
*isymend
;
389 const struct elf_backend_data
*bed
;
394 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
400 if (elf_use_dt_symtab_p (ibfd
))
402 /* Use dynamic symbol table. */
403 if (elf_tdata (ibfd
)->dt_symtab_count
!= symcount
+ symoffset
)
405 bfd_set_error (bfd_error_invalid_operation
);
408 return elf_tdata (ibfd
)->dt_symtab
+ symoffset
;
411 /* Normal syms might have section extension entries. */
413 if (elf_symtab_shndx_list (ibfd
) != NULL
)
415 elf_section_list
* entry
;
416 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
418 /* Find an index section that is linked to this symtab section. */
419 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
422 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
425 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
427 shndx_hdr
= & entry
->hdr
;
432 if (shndx_hdr
== NULL
)
434 if (symtab_hdr
== &elf_symtab_hdr (ibfd
))
435 /* Not really accurate, but this was how the old code used
437 shndx_hdr
= &elf_symtab_shndx_list (ibfd
)->hdr
;
438 /* Otherwise we do nothing. The assumption is that
439 the index table will not be needed. */
443 /* Read the symbols. */
445 alloc_extshndx
= NULL
;
447 bed
= get_elf_backend_data (ibfd
);
448 extsym_size
= bed
->s
->sizeof_sym
;
449 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
451 bfd_set_error (bfd_error_file_too_big
);
454 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
455 size_t alloc_ext_size
= amt
;
456 if (bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
457 || !_bfd_mmap_read_temporary (&extsym_buf
, &alloc_ext_size
,
458 &alloc_ext
, ibfd
, false))
464 size_t alloc_extshndx_size
= 0;
465 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
469 if (_bfd_mul_overflow (symcount
, sizeof (Elf_External_Sym_Shndx
), &amt
))
471 bfd_set_error (bfd_error_file_too_big
);
475 alloc_extshndx_size
= amt
;
476 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
477 if (bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
478 || !_bfd_mmap_read_temporary ((void **) &extshndx_buf
,
479 &alloc_extshndx_size
,
480 (void **) &alloc_extshndx
,
488 if (intsym_buf
== NULL
)
490 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
492 bfd_set_error (bfd_error_file_too_big
);
495 alloc_intsym
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
496 intsym_buf
= alloc_intsym
;
497 if (intsym_buf
== NULL
)
501 /* Convert the symbols to internal form. */
502 isymend
= intsym_buf
+ symcount
;
503 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
504 shndx
= extshndx_buf
;
506 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
508 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
510 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
511 /* xgettext:c-format */
512 _bfd_error_handler (_("%pB symbol number %lu references"
513 " nonexistent SHT_SYMTAB_SHNDX section"),
514 ibfd
, (unsigned long) symoffset
);
520 /* PR 33019: Do not accept unsupported binding values - they will
521 likely cause problems later on. */
522 int bind
= ELF_ST_BIND (isym
->st_info
);
523 if (bind
> STB_WEAK
&& bind
< STB_LOOS
)
525 /* xgettext:c-format */
526 _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
527 ibfd
, (unsigned long) (isym
- intsym_buf
), bind
);
533 /* Paranoia: Also refuse to accept the only undefined symbol type: 7. */
534 int t
= ELF_ST_TYPE (isym
->st_info
);
537 /* xgettext:c-format */
538 _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
539 ibfd
, (unsigned long) (isym
- intsym_buf
), t
);
547 _bfd_munmap_temporary (alloc_extshndx
, alloc_extshndx_size
);
549 _bfd_munmap_temporary (alloc_ext
, alloc_ext_size
);
554 /* Look up a symbol name. */
556 bfd_elf_sym_name_raw (bfd
*abfd
,
557 Elf_Internal_Shdr
*symtab_hdr
,
558 Elf_Internal_Sym
*isym
)
560 unsigned int iname
= isym
->st_name
;
561 unsigned int shindex
= symtab_hdr
->sh_link
;
563 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
564 /* Check for a bogus st_shndx to avoid crashing. */
565 && isym
->st_shndx
< elf_numsections (abfd
))
567 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
568 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
571 return bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
575 bfd_elf_sym_name (bfd
*abfd
,
576 Elf_Internal_Shdr
*symtab_hdr
,
577 Elf_Internal_Sym
*isym
,
580 const char *name
= bfd_elf_sym_name_raw (abfd
, symtab_hdr
, isym
);
582 name
= bfd_symbol_error_name
;
583 else if (sym_sec
&& *name
== '\0')
584 name
= bfd_section_name (sym_sec
);
589 /* Return the name of the group signature symbol. Why isn't the
590 signature just a string? */
593 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
595 Elf_Internal_Shdr
*hdr
;
596 unsigned char esym
[sizeof (Elf64_External_Sym
)];
597 Elf_External_Sym_Shndx eshndx
;
598 Elf_Internal_Sym isym
;
600 /* First we need to ensure the symbol table is available. Make sure
601 that it is a symbol table section. */
602 if (ghdr
->sh_link
>= elf_numsections (abfd
))
604 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
605 if (hdr
->sh_type
!= SHT_SYMTAB
606 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
609 /* Go read the symbol. */
610 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
611 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
612 &isym
, esym
, &eshndx
) == NULL
)
615 return bfd_elf_sym_name_raw (abfd
, hdr
, &isym
);
619 is_valid_group_section_header (Elf_Internal_Shdr
*shdr
, size_t minsize
)
621 return (shdr
->sh_size
>= minsize
622 && shdr
->sh_entsize
== GRP_ENTRY_SIZE
623 && shdr
->sh_size
% GRP_ENTRY_SIZE
== 0
624 && shdr
->bfd_section
!= NULL
);
628 /* Set next_in_group, sec_group list pointers, and group names. */
631 process_sht_group_entries (bfd
*abfd
,
632 Elf_Internal_Shdr
*ghdr
, unsigned int gidx
)
634 unsigned char *contents
;
636 /* Read the raw contents. */
637 if (!bfd_malloc_and_get_section (abfd
, ghdr
->bfd_section
, &contents
))
640 /* xgettext:c-format */
641 (_("%pB: could not read contents of group [%u]"), abfd
, gidx
);
645 asection
*last_elt
= NULL
;
646 const char *gname
= NULL
;
647 unsigned char *p
= contents
+ ghdr
->sh_size
;
651 Elf_Internal_Shdr
*shdr
;
655 idx
= H_GET_32 (abfd
, p
);
658 if ((idx
& GRP_COMDAT
) != 0)
659 ghdr
->bfd_section
->flags
660 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
665 || idx
>= elf_numsections (abfd
)
666 || (shdr
= elf_elfsections (abfd
)[idx
])->sh_type
== SHT_GROUP
667 || ((elt
= shdr
->bfd_section
) != NULL
668 && elf_sec_group (elt
) != NULL
669 && elf_sec_group (elt
) != ghdr
->bfd_section
))
672 (_("%pB: invalid entry (%#x) in group [%u]"),
677 /* PR binutils/23199: According to the ELF gABI all sections in
678 a group must be marked with SHF_GROUP, but some tools
679 generate broken objects. Fix them up here. */
680 shdr
->sh_flags
|= SHF_GROUP
;
684 if (shdr
->sh_type
!= SHT_RELA
&& shdr
->sh_type
!= SHT_REL
)
686 const char *name
= bfd_elf_string_from_elf_section
687 (abfd
, elf_elfheader (abfd
)->e_shstrndx
, shdr
->sh_name
);
690 /* xgettext:c-format */
691 (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
692 abfd
, shdr
->sh_type
, name
, gidx
);
697 /* Don't try to add a section to elf_next_in_group list twice. */
698 if (elf_sec_group (elt
) != NULL
)
701 if (last_elt
== NULL
)
703 /* Start a circular list with one element.
704 It will be in reverse order to match what gas does. */
705 elf_next_in_group (elt
) = elt
;
706 /* Point the group section to it. */
707 elf_next_in_group (ghdr
->bfd_section
) = elt
;
708 gname
= group_signature (abfd
, ghdr
);
717 elf_next_in_group (elt
) = elf_next_in_group (last_elt
);
718 elf_next_in_group (last_elt
) = elt
;
721 elf_group_name (elt
) = gname
;
722 elf_sec_group (elt
) = ghdr
->bfd_section
;
730 _bfd_elf_setup_sections (bfd
*abfd
)
734 /* Process SHF_LINK_ORDER. */
735 for (asection
*s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
737 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
738 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
740 unsigned int elfsec
= this_hdr
->sh_link
;
741 /* An sh_link value of 0 is now allowed. It indicates that linked
742 to section has already been discarded, but that the current
743 section has been retained for some other reason. This linking
744 section is still a candidate for later garbage collection
748 elf_linked_to_section (s
) = NULL
;
752 asection
*linksec
= NULL
;
754 if (elfsec
< elf_numsections (abfd
))
756 this_hdr
= elf_elfsections (abfd
)[elfsec
];
757 linksec
= this_hdr
->bfd_section
;
761 Some strip/objcopy may leave an incorrect value in
762 sh_link. We don't want to proceed. */
766 /* xgettext:c-format */
767 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
768 s
->owner
, elfsec
, s
);
772 elf_linked_to_section (s
) = linksec
;
777 /* Process section groups. */
778 for (unsigned int i
= 1; i
< elf_numsections (abfd
); i
++)
780 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
782 if (shdr
&& shdr
->sh_type
== SHT_GROUP
)
784 if (is_valid_group_section_header (shdr
, GRP_ENTRY_SIZE
))
786 if (shdr
->sh_size
>= 2 * GRP_ENTRY_SIZE
787 && !process_sht_group_entries (abfd
, shdr
, i
))
792 /* PR binutils/18758: Beware of corrupt binaries with
793 invalid group data. */
795 /* xgettext:c-format */
796 (_("%pB: section group entry number %u is corrupt"), abfd
, i
);
806 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
808 return elf_next_in_group (sec
) != NULL
;
812 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
814 if (elf_sec_group (sec
) != NULL
)
815 return elf_group_name (sec
);
819 /* Make a BFD section from an ELF section. We store a pointer to the
820 BFD section in the bfd_section field of the header. */
823 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
824 Elf_Internal_Shdr
*hdr
,
830 const struct elf_backend_data
*bed
;
831 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
833 if (hdr
->bfd_section
!= NULL
)
836 newsect
= bfd_make_section_anyway (abfd
, name
);
840 hdr
->bfd_section
= newsect
;
841 elf_section_data (newsect
)->this_hdr
= *hdr
;
842 elf_section_data (newsect
)->this_idx
= shindex
;
844 /* Always use the real type/flags. */
845 elf_section_type (newsect
) = hdr
->sh_type
;
846 elf_section_flags (newsect
) = hdr
->sh_flags
;
848 newsect
->filepos
= hdr
->sh_offset
;
850 flags
= SEC_NO_FLAGS
;
851 if (hdr
->sh_type
!= SHT_NOBITS
)
852 flags
|= SEC_HAS_CONTENTS
;
853 if (hdr
->sh_type
== SHT_GROUP
)
855 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
858 if (hdr
->sh_type
!= SHT_NOBITS
)
861 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
862 flags
|= SEC_READONLY
;
863 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
865 else if ((flags
& SEC_LOAD
) != 0)
867 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
870 newsect
->entsize
= hdr
->sh_entsize
;
872 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
874 flags
|= SEC_STRINGS
;
875 newsect
->entsize
= hdr
->sh_entsize
;
877 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
878 flags
|= SEC_THREAD_LOCAL
;
879 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
880 flags
|= SEC_EXCLUDE
;
882 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
884 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
885 but binutils as of 2019-07-23 did not set the EI_OSABI header
888 case ELFOSABI_FREEBSD
:
889 if ((hdr
->sh_flags
& SHF_GNU_RETAIN
) != 0)
890 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_retain
;
893 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
894 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
898 if ((flags
& SEC_ALLOC
) == 0)
900 /* The debugging sections appear to be recognized only by name,
901 not any sort of flag. Their SEC_ALLOC bits are cleared. */
904 if (startswith (name
, ".debug")
905 || startswith (name
, ".gnu.debuglto_.debug_")
906 || startswith (name
, ".gnu.linkonce.wi.")
907 || startswith (name
, ".zdebug"))
908 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
909 else if (startswith (name
, GNU_BUILD_ATTRS_SECTION_NAME
)
910 || startswith (name
, ".note.gnu"))
912 flags
|= SEC_ELF_OCTETS
;
915 else if (startswith (name
, ".line")
916 || startswith (name
, ".stab")
917 || strcmp (name
, ".gdb_index") == 0)
918 flags
|= SEC_DEBUGGING
;
922 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
923 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
924 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
925 & -hdr
->sh_addralign
)))
928 /* As a GNU extension, if the name begins with .gnu.linkonce, we
929 only link a single copy of the section. This is used to support
930 g++. g++ will emit each template expansion in its own section.
931 The symbols will be defined as weak, so that multiple definitions
932 are permitted. The GNU linker extension is to actually discard
933 all but one of the sections. */
934 if (startswith (name
, ".gnu.linkonce")
935 && elf_next_in_group (newsect
) == NULL
)
936 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
938 if (!bfd_set_section_flags (newsect
, flags
))
941 bed
= get_elf_backend_data (abfd
);
942 if (bed
->elf_backend_section_flags
)
943 if (!bed
->elf_backend_section_flags (hdr
))
946 /* We do not parse the PT_NOTE segments as we are interested even in the
947 separate debug info files which may have the segments offsets corrupted.
948 PT_NOTEs from the core files are currently not parsed using BFD. */
949 if (hdr
->sh_type
== SHT_NOTE
&& hdr
->sh_size
!= 0)
953 if (!_bfd_elf_mmap_section_contents (abfd
, newsect
, &contents
))
956 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
957 hdr
->sh_offset
, hdr
->sh_addralign
);
958 _bfd_elf_munmap_section_contents (newsect
, contents
);
961 if ((newsect
->flags
& SEC_ALLOC
) != 0)
963 Elf_Internal_Phdr
*phdr
;
964 unsigned int i
, nload
;
966 /* Some ELF linkers produce binaries with all the program header
967 p_paddr fields zero. If we have such a binary with more than
968 one PT_LOAD header, then leave the section lma equal to vma
969 so that we don't create sections with overlapping lma. */
970 phdr
= elf_tdata (abfd
)->phdr
;
971 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
972 if (phdr
->p_paddr
!= 0)
974 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
976 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
979 phdr
= elf_tdata (abfd
)->phdr
;
980 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
982 if (((phdr
->p_type
== PT_LOAD
983 && (hdr
->sh_flags
& SHF_TLS
) == 0)
984 || phdr
->p_type
== PT_TLS
)
985 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
987 if ((newsect
->flags
& SEC_LOAD
) == 0)
988 newsect
->lma
= (phdr
->p_paddr
989 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
991 /* We used to use the same adjustment for SEC_LOAD
992 sections, but that doesn't work if the segment
993 is packed with code from multiple VMAs.
994 Instead we calculate the section LMA based on
995 the segment LMA. It is assumed that the
996 segment will contain sections with contiguous
997 LMAs, even if the VMAs are not. */
998 newsect
->lma
= (phdr
->p_paddr
999 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1001 /* With contiguous segments, we can't tell from file
1002 offsets whether a section with zero size should
1003 be placed at the end of one segment or the
1004 beginning of the next. Decide based on vaddr. */
1005 if (hdr
->sh_addr
>= phdr
->p_vaddr
1006 && (hdr
->sh_addr
+ hdr
->sh_size
1007 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1013 /* Compress/decompress DWARF debug sections with names: .debug_*,
1014 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1015 if ((newsect
->flags
& SEC_DEBUGGING
) != 0
1016 && (newsect
->flags
& SEC_HAS_CONTENTS
) != 0
1017 && (newsect
->flags
& SEC_ELF_OCTETS
) != 0)
1019 enum { nothing
, compress
, decompress
} action
= nothing
;
1020 int compression_header_size
;
1021 bfd_size_type uncompressed_size
;
1022 unsigned int uncompressed_align_power
;
1023 enum compression_type ch_type
= ch_none
;
1025 = bfd_is_section_compressed_info (abfd
, newsect
,
1026 &compression_header_size
,
1028 &uncompressed_align_power
,
1031 /* Should we decompress? */
1032 if ((abfd
->flags
& BFD_DECOMPRESS
) != 0 && compressed
)
1033 action
= decompress
;
1035 /* Should we compress? Or convert to a different compression? */
1036 else if ((abfd
->flags
& BFD_COMPRESS
) != 0
1037 && newsect
->size
!= 0
1038 && compression_header_size
>= 0
1039 && uncompressed_size
> 0)
1045 enum compression_type new_ch_type
= ch_none
;
1046 if ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0)
1047 new_ch_type
= ((abfd
->flags
& BFD_COMPRESS_ZSTD
) != 0
1048 ? ch_compress_zstd
: ch_compress_zlib
);
1049 if (new_ch_type
!= ch_type
)
1054 if (action
== compress
)
1056 if (!bfd_init_section_compress_status (abfd
, newsect
))
1059 /* xgettext:c-format */
1060 (_("%pB: unable to compress section %s"), abfd
, name
);
1064 else if (action
== decompress
)
1066 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1069 /* xgettext:c-format */
1070 (_("%pB: unable to decompress section %s"), abfd
, name
);
1074 if (newsect
->compress_status
== DECOMPRESS_SECTION_ZSTD
)
1077 /* xgettext:c-format */
1078 (_ ("%pB: section %s is compressed with zstd, but BFD "
1079 "is not built with zstd support"),
1081 newsect
->compress_status
= COMPRESS_SECTION_NONE
;
1085 if (abfd
->is_linker_input
1088 /* Rename section from .zdebug_* to .debug_* so that ld
1089 scripts will see this section as a debug section. */
1090 char *new_name
= bfd_zdebug_name_to_debug (abfd
, name
);
1091 if (new_name
== NULL
)
1093 bfd_rename_section (newsect
, new_name
);
1101 const char *const bfd_elf_section_type_names
[] =
1103 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1104 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1105 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1108 /* ELF relocs are against symbols. If we are producing relocatable
1109 output, and the reloc is against an external symbol, and nothing
1110 has given us any additional addend, the resulting reloc will also
1111 be against the same symbol. In such a case, we don't want to
1112 change anything about the way the reloc is handled, since it will
1113 all be done at final link time. Rather than put special case code
1114 into bfd_perform_relocation, all the reloc types use this howto
1115 function, or should call this function for relocatable output. */
1117 bfd_reloc_status_type
1118 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1119 arelent
*reloc_entry
,
1121 void *data ATTRIBUTE_UNUSED
,
1122 asection
*input_section
,
1124 char **error_message ATTRIBUTE_UNUSED
)
1126 if (output_bfd
!= NULL
1127 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1128 && (! reloc_entry
->howto
->partial_inplace
1129 || reloc_entry
->addend
== 0))
1131 reloc_entry
->address
+= input_section
->output_offset
;
1132 return bfd_reloc_ok
;
1135 /* In some cases the relocation should be treated as output section
1136 relative, as when linking ELF DWARF into PE COFF. Many ELF
1137 targets lack section relative relocations and instead use
1138 ordinary absolute relocations for references between DWARF
1139 sections. That is arguably a bug in those targets but it happens
1140 to work for the usual case of linking to non-loaded ELF debug
1141 sections with VMAs forced to zero. PE COFF on the other hand
1142 doesn't allow a section VMA of zero. */
1143 if (output_bfd
== NULL
1144 && !reloc_entry
->howto
->pc_relative
1145 && (symbol
->section
->flags
& SEC_DEBUGGING
) != 0
1146 && (input_section
->flags
& SEC_DEBUGGING
) != 0)
1147 reloc_entry
->addend
-= symbol
->section
->output_section
->vma
;
1149 return bfd_reloc_continue
;
1152 /* Returns TRUE if section A matches section B.
1153 Names, addresses and links may be different, but everything else
1154 should be the same. */
1157 section_match (const Elf_Internal_Shdr
* a
,
1158 const Elf_Internal_Shdr
* b
)
1160 if (a
->sh_type
!= b
->sh_type
1161 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1162 || a
->sh_addralign
!= b
->sh_addralign
1163 || a
->sh_entsize
!= b
->sh_entsize
)
1165 if (a
->sh_type
== SHT_SYMTAB
1166 || a
->sh_type
== SHT_STRTAB
)
1168 return a
->sh_size
== b
->sh_size
;
1171 /* Find a section in OBFD that has the same characteristics
1172 as IHEADER. Return the index of this section or SHN_UNDEF if
1173 none can be found. Check's section HINT first, as this is likely
1174 to be the correct section. */
1177 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1178 const unsigned int hint
)
1180 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1183 BFD_ASSERT (iheader
!= NULL
);
1185 /* See PR 20922 for a reproducer of the NULL test. */
1186 if (hint
< elf_numsections (obfd
)
1187 && oheaders
[hint
] != NULL
1188 && section_match (oheaders
[hint
], iheader
))
1191 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1193 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1195 if (oheader
== NULL
)
1197 if (section_match (oheader
, iheader
))
1198 /* FIXME: Do we care if there is a potential for
1199 multiple matches ? */
1206 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1207 Processor specific section, based upon a matching input section.
1208 Returns TRUE upon success, FALSE otherwise. */
1211 copy_special_section_fields (const bfd
*ibfd
,
1213 const Elf_Internal_Shdr
*iheader
,
1214 Elf_Internal_Shdr
*oheader
,
1215 const unsigned int secnum
)
1217 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1218 const Elf_Internal_Shdr
**iheaders
1219 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1220 bool changed
= false;
1221 unsigned int sh_link
;
1223 if (oheader
->sh_type
== SHT_NOBITS
)
1225 /* This is a feature for objcopy --only-keep-debug:
1226 When a section's type is changed to NOBITS, we preserve
1227 the sh_link and sh_info fields so that they can be
1228 matched up with the original.
1230 Note: Strictly speaking these assignments are wrong.
1231 The sh_link and sh_info fields should point to the
1232 relevent sections in the output BFD, which may not be in
1233 the same location as they were in the input BFD. But
1234 the whole point of this action is to preserve the
1235 original values of the sh_link and sh_info fields, so
1236 that they can be matched up with the section headers in
1237 the original file. So strictly speaking we may be
1238 creating an invalid ELF file, but it is only for a file
1239 that just contains debug info and only for sections
1240 without any contents. */
1241 if (oheader
->sh_link
== 0)
1242 oheader
->sh_link
= iheader
->sh_link
;
1243 if (oheader
->sh_info
== 0)
1244 oheader
->sh_info
= iheader
->sh_info
;
1248 /* Allow the target a chance to decide how these fields should be set. */
1249 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1253 /* We have an iheader which might match oheader, and which has non-zero
1254 sh_info and/or sh_link fields. Attempt to follow those links and find
1255 the section in the output bfd which corresponds to the linked section
1256 in the input bfd. */
1257 if (iheader
->sh_link
!= SHN_UNDEF
)
1259 /* See PR 20931 for a reproducer. */
1260 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1263 /* xgettext:c-format */
1264 (_("%pB: invalid sh_link field (%d) in section number %d"),
1265 ibfd
, iheader
->sh_link
, secnum
);
1269 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1270 if (sh_link
!= SHN_UNDEF
)
1272 oheader
->sh_link
= sh_link
;
1276 /* FIXME: Should we install iheader->sh_link
1277 if we could not find a match ? */
1279 /* xgettext:c-format */
1280 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1283 if (iheader
->sh_info
)
1285 /* The sh_info field can hold arbitrary information, but if the
1286 SHF_LINK_INFO flag is set then it should be interpreted as a
1288 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1290 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1292 if (sh_link
!= SHN_UNDEF
)
1293 oheader
->sh_flags
|= SHF_INFO_LINK
;
1296 /* No idea what it means - just copy it. */
1297 sh_link
= iheader
->sh_info
;
1299 if (sh_link
!= SHN_UNDEF
)
1301 oheader
->sh_info
= sh_link
;
1306 /* xgettext:c-format */
1307 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1313 /* Copy the program header and other data from one object module to
1317 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1319 const Elf_Internal_Shdr
**iheaders
1320 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1321 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1322 const struct elf_backend_data
*bed
;
1325 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1326 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1329 if (!elf_flags_init (obfd
))
1331 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1332 elf_flags_init (obfd
) = true;
1335 elf_gp (obfd
) = elf_gp (ibfd
);
1337 /* Also copy the EI_OSABI field. */
1338 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1339 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1341 /* If set, copy the EI_ABIVERSION field. */
1342 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1343 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1344 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1346 /* Copy object attributes. */
1347 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1349 if (iheaders
== NULL
|| oheaders
== NULL
)
1352 bed
= get_elf_backend_data (obfd
);
1354 /* Possibly copy other fields in the section header. */
1355 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1358 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1360 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1361 because of a special case need for generating separate debug info
1362 files. See below for more details. */
1364 || (oheader
->sh_type
!= SHT_NOBITS
1365 && oheader
->sh_type
< SHT_LOOS
))
1368 /* Ignore empty sections, and sections whose
1369 fields have already been initialised. */
1370 if (oheader
->sh_size
== 0
1371 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1374 /* Scan for the matching section in the input bfd.
1375 First we try for a direct mapping between the input and
1377 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1379 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1381 if (iheader
== NULL
)
1384 if (oheader
->bfd_section
!= NULL
1385 && iheader
->bfd_section
!= NULL
1386 && iheader
->bfd_section
->output_section
!= NULL
1387 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1389 /* We have found a connection from the input section to
1390 the output section. Attempt to copy the header fields.
1391 If this fails then do not try any further sections -
1392 there should only be a one-to-one mapping between
1393 input and output. */
1394 if (!copy_special_section_fields (ibfd
, obfd
,
1395 iheader
, oheader
, i
))
1396 j
= elf_numsections (ibfd
);
1401 if (j
< elf_numsections (ibfd
))
1404 /* That failed. So try to deduce the corresponding input section.
1405 Unfortunately we cannot compare names as the output string table
1406 is empty, so instead we check size, address and type. */
1407 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1409 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1411 if (iheader
== NULL
)
1414 /* Try matching fields in the input section's header.
1415 Since --only-keep-debug turns all non-debug sections into
1416 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1418 if ((oheader
->sh_type
== SHT_NOBITS
1419 || iheader
->sh_type
== oheader
->sh_type
)
1420 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1421 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1422 && iheader
->sh_addralign
== oheader
->sh_addralign
1423 && iheader
->sh_entsize
== oheader
->sh_entsize
1424 && iheader
->sh_size
== oheader
->sh_size
1425 && iheader
->sh_addr
== oheader
->sh_addr
1426 && (iheader
->sh_info
!= oheader
->sh_info
1427 || iheader
->sh_link
!= oheader
->sh_link
))
1429 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1434 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1436 /* Final attempt. Call the backend copy function
1437 with a NULL input section. */
1438 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1447 get_segment_type (unsigned int p_type
)
1452 case PT_NULL
: pt
= "NULL"; break;
1453 case PT_LOAD
: pt
= "LOAD"; break;
1454 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1455 case PT_INTERP
: pt
= "INTERP"; break;
1456 case PT_NOTE
: pt
= "NOTE"; break;
1457 case PT_SHLIB
: pt
= "SHLIB"; break;
1458 case PT_PHDR
: pt
= "PHDR"; break;
1459 case PT_TLS
: pt
= "TLS"; break;
1460 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1461 case PT_GNU_STACK
: pt
= "STACK"; break;
1462 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1463 case PT_GNU_SFRAME
: pt
= "SFRAME"; break;
1464 default: pt
= NULL
; break;
1469 /* Print out the program headers. */
1472 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1474 FILE *f
= (FILE *) farg
;
1475 Elf_Internal_Phdr
*p
;
1477 bfd_byte
*dynbuf
= NULL
;
1479 p
= elf_tdata (abfd
)->phdr
;
1484 fprintf (f
, _("\nProgram Header:\n"));
1485 c
= elf_elfheader (abfd
)->e_phnum
;
1486 for (i
= 0; i
< c
; i
++, p
++)
1488 const char *pt
= get_segment_type (p
->p_type
);
1493 sprintf (buf
, "0x%lx", p
->p_type
);
1496 fprintf (f
, "%8s off 0x", pt
);
1497 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1498 fprintf (f
, " vaddr 0x");
1499 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1500 fprintf (f
, " paddr 0x");
1501 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1502 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1503 fprintf (f
, " filesz 0x");
1504 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1505 fprintf (f
, " memsz 0x");
1506 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1507 fprintf (f
, " flags %c%c%c",
1508 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1509 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1510 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1511 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1512 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1517 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1518 if (s
!= NULL
&& (s
->flags
& SEC_HAS_CONTENTS
) != 0)
1520 unsigned int elfsec
;
1521 unsigned long shlink
;
1522 bfd_byte
*extdyn
, *extdynend
;
1524 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1526 fprintf (f
, _("\nDynamic Section:\n"));
1528 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
1531 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1532 if (elfsec
== SHN_BAD
)
1534 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1536 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1537 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1539 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
1540 (size_t) (extdynend
- extdyn
) >= extdynsize
;
1541 extdyn
+= extdynsize
)
1543 Elf_Internal_Dyn dyn
;
1544 const char *name
= "";
1547 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1549 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1551 if (dyn
.d_tag
== DT_NULL
)
1558 if (bed
->elf_backend_get_target_dtag
)
1559 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1561 if (!strcmp (name
, ""))
1563 sprintf (ab
, "%#" PRIx64
, (uint64_t) dyn
.d_tag
);
1568 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1569 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1570 case DT_PLTGOT
: name
= "PLTGOT"; break;
1571 case DT_HASH
: name
= "HASH"; break;
1572 case DT_STRTAB
: name
= "STRTAB"; break;
1573 case DT_SYMTAB
: name
= "SYMTAB"; break;
1574 case DT_RELA
: name
= "RELA"; break;
1575 case DT_RELASZ
: name
= "RELASZ"; break;
1576 case DT_RELAENT
: name
= "RELAENT"; break;
1577 case DT_STRSZ
: name
= "STRSZ"; break;
1578 case DT_SYMENT
: name
= "SYMENT"; break;
1579 case DT_INIT
: name
= "INIT"; break;
1580 case DT_FINI
: name
= "FINI"; break;
1581 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1582 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1583 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1584 case DT_REL
: name
= "REL"; break;
1585 case DT_RELSZ
: name
= "RELSZ"; break;
1586 case DT_RELENT
: name
= "RELENT"; break;
1587 case DT_RELR
: name
= "RELR"; break;
1588 case DT_RELRSZ
: name
= "RELRSZ"; break;
1589 case DT_RELRENT
: name
= "RELRENT"; break;
1590 case DT_PLTREL
: name
= "PLTREL"; break;
1591 case DT_DEBUG
: name
= "DEBUG"; break;
1592 case DT_TEXTREL
: name
= "TEXTREL"; break;
1593 case DT_JMPREL
: name
= "JMPREL"; break;
1594 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1595 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1596 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1597 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1598 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1599 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1600 case DT_FLAGS
: name
= "FLAGS"; break;
1601 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1602 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1603 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1604 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1605 case DT_MOVEENT
: name
= "MOVEENT"; break;
1606 case DT_MOVESZ
: name
= "MOVESZ"; break;
1607 case DT_FEATURE
: name
= "FEATURE"; break;
1608 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1609 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1610 case DT_SYMINENT
: name
= "SYMINENT"; break;
1611 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1612 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1613 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1614 case DT_PLTPAD
: name
= "PLTPAD"; break;
1615 case DT_MOVETAB
: name
= "MOVETAB"; break;
1616 case DT_SYMINFO
: name
= "SYMINFO"; break;
1617 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1618 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1619 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1620 case DT_VERSYM
: name
= "VERSYM"; break;
1621 case DT_VERDEF
: name
= "VERDEF"; break;
1622 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1623 case DT_VERNEED
: name
= "VERNEED"; break;
1624 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1625 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1626 case DT_USED
: name
= "USED"; break;
1627 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1628 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1631 fprintf (f
, " %-20s ", name
);
1635 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1640 unsigned int tagv
= dyn
.d_un
.d_val
;
1642 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1645 fprintf (f
, "%s", string
);
1650 _bfd_elf_munmap_section_contents (s
, dynbuf
);
1654 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1655 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1657 if (! _bfd_elf_slurp_version_tables (abfd
, false))
1661 if (elf_dynverdef (abfd
) != 0)
1663 Elf_Internal_Verdef
*t
;
1665 fprintf (f
, _("\nVersion definitions:\n"));
1666 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1668 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1669 t
->vd_flags
, t
->vd_hash
,
1670 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1671 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1673 Elf_Internal_Verdaux
*a
;
1676 for (a
= t
->vd_auxptr
->vda_nextptr
;
1680 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1686 if (elf_dynverref (abfd
) != 0)
1688 Elf_Internal_Verneed
*t
;
1690 fprintf (f
, _("\nVersion References:\n"));
1691 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1693 Elf_Internal_Vernaux
*a
;
1695 fprintf (f
, _(" required from %s:\n"),
1696 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1697 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1698 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1699 a
->vna_flags
, a
->vna_other
,
1700 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1707 _bfd_elf_munmap_section_contents (s
, dynbuf
);
1711 /* Find the file offset corresponding to VMA by using the program
1715 offset_from_vma (Elf_Internal_Phdr
*phdrs
, size_t phnum
, bfd_vma vma
,
1716 size_t size
, size_t *max_size_p
)
1718 Elf_Internal_Phdr
*seg
;
1721 for (seg
= phdrs
, i
= 0; i
< phnum
; ++seg
, ++i
)
1722 if (seg
->p_type
== PT_LOAD
1723 && vma
>= (seg
->p_vaddr
& -seg
->p_align
)
1724 && vma
+ size
<= seg
->p_vaddr
+ seg
->p_filesz
)
1727 *max_size_p
= seg
->p_vaddr
+ seg
->p_filesz
- vma
;
1728 return vma
- seg
->p_vaddr
+ seg
->p_offset
;
1733 bfd_set_error (bfd_error_invalid_operation
);
1734 return (file_ptr
) -1;
1737 /* Convert hash table to internal form. */
1740 get_hash_table_data (bfd
*abfd
, bfd_size_type number
,
1741 unsigned int ent_size
, bfd_size_type filesize
)
1743 unsigned char *e_data
= NULL
;
1744 bfd_vma
*i_data
= NULL
;
1747 size_t e_data_size ATTRIBUTE_UNUSED
;
1749 if (ent_size
!= 4 && ent_size
!= 8)
1752 if ((size_t) number
!= number
)
1754 bfd_set_error (bfd_error_file_too_big
);
1758 size
= ent_size
* number
;
1759 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1760 attempting to allocate memory when the read is bound to fail. */
1762 || number
>= ~(size_t) 0 / ent_size
1763 || number
>= ~(size_t) 0 / sizeof (*i_data
))
1765 bfd_set_error (bfd_error_file_too_big
);
1769 e_data
= _bfd_mmap_temporary (abfd
, size
, &e_data_addr
, &e_data_size
);
1773 i_data
= (bfd_vma
*) bfd_malloc (number
* sizeof (*i_data
));
1776 _bfd_munmap_temporary (e_data_addr
, e_data_size
);
1782 i_data
[number
] = bfd_get_32 (abfd
, e_data
+ number
* ent_size
);
1785 i_data
[number
] = bfd_get_64 (abfd
, e_data
+ number
* ent_size
);
1787 _bfd_munmap_temporary (e_data_addr
, e_data_size
);
1791 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1792 support DT_MIPS_XHASH? */
1793 #define DT_MIPS_XHASH 0x70000036
1795 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1798 _bfd_elf_get_dynamic_symbols (bfd
*abfd
, Elf_Internal_Phdr
*phdr
,
1799 Elf_Internal_Phdr
*phdrs
, size_t phnum
,
1800 bfd_size_type filesize
)
1802 bfd_byte
*extdyn
, *extdynend
;
1804 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1805 bool (*swap_symbol_in
) (bfd
*, const void *, const void *,
1806 Elf_Internal_Sym
*);
1807 Elf_Internal_Dyn dyn
;
1808 bfd_vma dt_hash
= 0;
1809 bfd_vma dt_gnu_hash
= 0;
1810 bfd_vma dt_mips_xhash
= 0;
1811 bfd_vma dt_strtab
= 0;
1812 bfd_vma dt_symtab
= 0;
1813 size_t dt_strsz
= 0;
1814 bfd_vma dt_versym
= 0;
1815 bfd_vma dt_verdef
= 0;
1816 bfd_vma dt_verneed
= 0;
1817 bfd_byte
*dynbuf
= NULL
;
1818 char *strbuf
= NULL
;
1819 bfd_vma
*gnubuckets
= NULL
;
1820 bfd_vma
*gnuchains
= NULL
;
1821 bfd_vma
*mipsxlat
= NULL
;
1822 file_ptr saved_filepos
, filepos
;
1825 bfd_byte
*esymbuf
= NULL
, *esym
;
1826 bfd_size_type symcount
;
1827 Elf_Internal_Sym
*isymbuf
= NULL
;
1828 Elf_Internal_Sym
*isym
, *isymend
;
1829 bfd_byte
*versym
= NULL
;
1830 bfd_byte
*verdef
= NULL
;
1831 bfd_byte
*verneed
= NULL
;
1832 size_t verdef_size
= 0;
1833 size_t verneed_size
= 0;
1835 const struct elf_backend_data
*bed
;
1836 void *dynbuf_addr
= NULL
;
1837 void *esymbuf_addr
= NULL
;
1838 size_t dynbuf_size
= 0;
1839 size_t esymbuf_size
= 0;
1841 /* Return TRUE if symbol table is bad. */
1842 if (elf_bad_symtab (abfd
))
1845 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
1846 if (elf_tdata (abfd
)->dt_strtab
!= NULL
)
1849 bed
= get_elf_backend_data (abfd
);
1851 /* Save file position for elf_object_p. */
1852 saved_filepos
= bfd_tell (abfd
);
1854 if (bfd_seek (abfd
, phdr
->p_offset
, SEEK_SET
) != 0)
1857 dynbuf_size
= phdr
->p_filesz
;
1858 dynbuf
= _bfd_mmap_temporary (abfd
, dynbuf_size
, &dynbuf_addr
, &dynbuf_size
);
1862 extsym_size
= bed
->s
->sizeof_sym
;
1863 extdynsize
= bed
->s
->sizeof_dyn
;
1864 swap_dyn_in
= bed
->s
->swap_dyn_in
;
1867 if (phdr
->p_filesz
< extdynsize
)
1869 extdynend
= extdyn
+ phdr
->p_filesz
;
1870 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1872 swap_dyn_in (abfd
, extdyn
, &dyn
);
1874 if (dyn
.d_tag
== DT_NULL
)
1880 dt_hash
= dyn
.d_un
.d_val
;
1883 if (bed
->elf_machine_code
!= EM_MIPS
1884 && bed
->elf_machine_code
!= EM_MIPS_RS3_LE
)
1885 dt_gnu_hash
= dyn
.d_un
.d_val
;
1888 dt_strtab
= dyn
.d_un
.d_val
;
1891 dt_symtab
= dyn
.d_un
.d_val
;
1894 dt_strsz
= dyn
.d_un
.d_val
;
1897 if (dyn
.d_un
.d_val
!= extsym_size
)
1901 dt_versym
= dyn
.d_un
.d_val
;
1904 dt_verdef
= dyn
.d_un
.d_val
;
1907 dt_verneed
= dyn
.d_un
.d_val
;
1910 if (dyn
.d_tag
== DT_MIPS_XHASH
1911 && (bed
->elf_machine_code
== EM_MIPS
1912 || bed
->elf_machine_code
== EM_MIPS_RS3_LE
))
1914 dt_gnu_hash
= dyn
.d_un
.d_val
;
1915 dt_mips_xhash
= dyn
.d_un
.d_val
;
1921 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1923 if ((!dt_hash
&& !dt_gnu_hash
)
1929 /* Get dynamic string table. */
1930 filepos
= offset_from_vma (phdrs
, phnum
, dt_strtab
, dt_strsz
, NULL
);
1931 if (filepos
== (file_ptr
) -1
1932 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
1935 /* Dynamic string table must be valid until ABFD is closed. */
1936 strbuf
= (char *) _bfd_mmap_persistent (abfd
, dt_strsz
);
1939 if (strbuf
[dt_strsz
- 1] != 0)
1941 /* It is an error if a string table is't terminated. */
1943 /* xgettext:c-format */
1944 (_("%pB: DT_STRTAB table is corrupt"), abfd
);
1945 strbuf
[dt_strsz
- 1] = 0;
1948 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
1949 DT_HASH since it is simpler than DT_GNU_HASH. */
1952 unsigned char nb
[16];
1953 unsigned int hash_ent_size
;
1955 switch (bed
->elf_machine_code
)
1960 if (bed
->s
->elfclass
== ELFCLASS64
)
1971 filepos
= offset_from_vma (phdrs
, phnum
, dt_hash
, 2 * hash_ent_size
,
1973 if (filepos
== (file_ptr
) -1
1974 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
1975 || bfd_read (nb
, 2 * hash_ent_size
, abfd
) != 2 * hash_ent_size
)
1978 /* The number of dynamic symbol table entries equals the number
1980 if (hash_ent_size
== 8)
1981 symcount
= bfd_get_64 (abfd
, nb
+ hash_ent_size
);
1983 symcount
= bfd_get_32 (abfd
, nb
+ hash_ent_size
);
1987 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
1988 bindings are in hash table. Since in dynamic symbol table,
1989 all symbols with STB_LOCAL binding are placed before symbols
1990 with other bindings and all undefined symbols are placed
1991 before defined ones, the highest symbol index in DT_GNU_HASH
1992 is the highest dynamic symbol table index. */
1993 unsigned char nb
[16];
1994 bfd_vma ngnubuckets
;
1996 size_t i
, ngnuchains
;
1997 bfd_vma maxchain
= 0xffffffff, bitmaskwords
;
1998 bfd_vma buckets_vma
;
2000 filepos
= offset_from_vma (phdrs
, phnum
, dt_gnu_hash
,
2002 if (filepos
== (file_ptr
) -1
2003 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
2004 || bfd_read (nb
, sizeof (nb
), abfd
) != sizeof (nb
))
2007 ngnubuckets
= bfd_get_32 (abfd
, nb
);
2008 gnusymidx
= bfd_get_32 (abfd
, nb
+ 4);
2009 bitmaskwords
= bfd_get_32 (abfd
, nb
+ 8);
2010 buckets_vma
= dt_gnu_hash
+ 16;
2011 if (bed
->s
->elfclass
== ELFCLASS32
)
2012 buckets_vma
+= bitmaskwords
* 4;
2014 buckets_vma
+= bitmaskwords
* 8;
2015 filepos
= offset_from_vma (phdrs
, phnum
, buckets_vma
, 4, NULL
);
2016 if (filepos
== (file_ptr
) -1
2017 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2020 gnubuckets
= get_hash_table_data (abfd
, ngnubuckets
, 4, filesize
);
2021 if (gnubuckets
== NULL
)
2024 for (i
= 0; i
< ngnubuckets
; i
++)
2025 if (gnubuckets
[i
] != 0)
2027 if (gnubuckets
[i
] < gnusymidx
)
2030 if (maxchain
== 0xffffffff || gnubuckets
[i
] > maxchain
)
2031 maxchain
= gnubuckets
[i
];
2034 if (maxchain
== 0xffffffff)
2037 goto empty_gnu_hash
;
2040 maxchain
-= gnusymidx
;
2041 filepos
= offset_from_vma (phdrs
, phnum
,
2042 buckets_vma
+ 4 * (ngnubuckets
+ maxchain
),
2044 if (filepos
== (file_ptr
) -1
2045 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2050 if (bfd_read (nb
, 4, abfd
) != 4)
2056 while ((bfd_get_32 (abfd
, nb
) & 1) == 0);
2058 filepos
= offset_from_vma (phdrs
, phnum
,
2059 buckets_vma
+ 4 * ngnubuckets
,
2061 if (filepos
== (file_ptr
) -1
2062 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2065 gnuchains
= get_hash_table_data (abfd
, maxchain
, 4, filesize
);
2066 if (gnuchains
== NULL
)
2068 ngnuchains
= maxchain
;
2072 filepos
= offset_from_vma (phdrs
, phnum
,
2073 buckets_vma
+ 4 * (ngnubuckets
+ maxchain
),
2075 if (filepos
== (file_ptr
) -1
2076 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2079 mipsxlat
= get_hash_table_data (abfd
, maxchain
, 4, filesize
);
2080 if (mipsxlat
== NULL
)
2085 for (i
= 0; i
< ngnubuckets
; ++i
)
2086 if (gnubuckets
[i
] != 0)
2088 bfd_vma si
= gnubuckets
[i
];
2089 bfd_vma off
= si
- gnusymidx
;
2094 if (mipsxlat
[off
] >= symcount
)
2095 symcount
= mipsxlat
[off
] + 1;
2104 while (off
< ngnuchains
&& (gnuchains
[off
++] & 1) == 0);
2108 /* Swap in dynamic symbol table. */
2109 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
2111 bfd_set_error (bfd_error_file_too_big
);
2115 filepos
= offset_from_vma (phdrs
, phnum
, dt_symtab
, amt
, NULL
);
2116 if (filepos
== (file_ptr
) -1
2117 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2120 esymbuf
= _bfd_mmap_temporary (abfd
, esymbuf_size
,
2121 &esymbuf_addr
, &esymbuf_size
);
2122 if (esymbuf
== NULL
)
2125 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
2127 bfd_set_error (bfd_error_file_too_big
);
2131 /* Dynamic symbol table must be valid until ABFD is closed. */
2132 isymbuf
= (Elf_Internal_Sym
*) bfd_alloc (abfd
, amt
);
2133 if (isymbuf
== NULL
)
2136 swap_symbol_in
= bed
->s
->swap_symbol_in
;
2138 /* Convert the symbols to internal form. */
2139 isymend
= isymbuf
+ symcount
;
2140 for (esym
= esymbuf
, isym
= isymbuf
;
2142 esym
+= extsym_size
, isym
++)
2143 if (!swap_symbol_in (abfd
, esym
, NULL
, isym
)
2144 || isym
->st_name
>= dt_strsz
)
2146 bfd_set_error (bfd_error_invalid_operation
);
2152 /* Swap in DT_VERSYM. */
2153 if (_bfd_mul_overflow (symcount
, 2, &amt
))
2155 bfd_set_error (bfd_error_file_too_big
);
2159 filepos
= offset_from_vma (phdrs
, phnum
, dt_versym
, amt
, NULL
);
2160 if (filepos
== (file_ptr
) -1
2161 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2164 /* DT_VERSYM info must be valid until ABFD is closed. */
2165 versym
= _bfd_mmap_persistent (abfd
, amt
);
2169 /* Read in DT_VERDEF. */
2170 filepos
= offset_from_vma (phdrs
, phnum
, dt_verdef
,
2172 if (filepos
== (file_ptr
) -1
2173 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2176 /* DT_VERDEF info must be valid until ABFD is closed. */
2177 verdef
= _bfd_mmap_persistent (abfd
, verdef_size
);
2182 /* Read in DT_VERNEED. */
2183 filepos
= offset_from_vma (phdrs
, phnum
, dt_verneed
,
2185 if (filepos
== (file_ptr
) -1
2186 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2189 /* DT_VERNEED info must be valid until ABFD is closed. */
2190 verneed
= _bfd_mmap_persistent (abfd
, verneed_size
);
2195 elf_tdata (abfd
)->dt_strtab
= strbuf
;
2196 elf_tdata (abfd
)->dt_strsz
= dt_strsz
;
2197 elf_tdata (abfd
)->dt_symtab
= isymbuf
;
2198 elf_tdata (abfd
)->dt_symtab_count
= symcount
;
2199 elf_tdata (abfd
)->dt_versym
= versym
;
2200 elf_tdata (abfd
)->dt_verdef
= verdef
;
2201 elf_tdata (abfd
)->dt_verneed
= verneed
;
2202 elf_tdata (abfd
)->dt_verdef_count
2203 = verdef_size
/ sizeof (Elf_External_Verdef
);
2204 elf_tdata (abfd
)->dt_verneed_count
2205 = verneed_size
/ sizeof (Elf_External_Verneed
);
2210 /* Restore file position for elf_object_p. */
2211 if (bfd_seek (abfd
, saved_filepos
, SEEK_SET
) != 0)
2213 _bfd_munmap_temporary (dynbuf_addr
, dynbuf_size
);
2214 _bfd_munmap_temporary (esymbuf_addr
, esymbuf_size
);
2221 /* Reconstruct section from dynamic symbol. */
2224 _bfd_elf_get_section_from_dynamic_symbol (bfd
*abfd
,
2225 Elf_Internal_Sym
*isym
)
2230 if (!elf_use_dt_symtab_p (abfd
))
2233 flags
= SEC_ALLOC
| SEC_LOAD
;
2234 switch (ELF_ST_TYPE (isym
->st_info
))
2238 sec
= bfd_get_section_by_name (abfd
, ".text");
2240 sec
= bfd_make_section_with_flags (abfd
,
2245 sec
= bfd_com_section_ptr
;
2248 sec
= bfd_get_section_by_name (abfd
, ".data");
2250 sec
= bfd_make_section_with_flags (abfd
,
2255 sec
= bfd_get_section_by_name (abfd
, ".tdata");
2257 sec
= bfd_make_section_with_flags (abfd
,
2261 | SEC_THREAD_LOCAL
));
2264 sec
= bfd_abs_section_ptr
;
2271 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2272 and return symbol version for symbol version itself. */
2275 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
2279 const char *version_string
= NULL
;
2280 if ((elf_dynversym (abfd
) != 0
2281 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
2282 || (elf_tdata (abfd
)->dt_versym
!= NULL
2283 && (elf_tdata (abfd
)->dt_verdef
!= NULL
2284 || elf_tdata (abfd
)->dt_verneed
!= NULL
)))
2286 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
2288 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
2289 vernum
&= VERSYM_VERSION
;
2292 version_string
= "";
2293 else if (vernum
== 1
2294 && (vernum
> elf_tdata (abfd
)->cverdefs
2295 || (elf_tdata (abfd
)->verdef
[0].vd_flags
2297 version_string
= base_p
? "Base" : "";
2298 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
2300 const char *nodename
2301 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
2302 version_string
= "";
2305 || symbol
->name
== NULL
2306 || strcmp (symbol
->name
, nodename
) != 0)
2307 version_string
= nodename
;
2311 Elf_Internal_Verneed
*t
;
2313 version_string
= _("<corrupt>");
2314 for (t
= elf_tdata (abfd
)->verref
;
2318 Elf_Internal_Vernaux
*a
;
2320 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2322 if (a
->vna_other
== vernum
)
2325 version_string
= a
->vna_nodename
;
2332 return version_string
;
2335 /* Display ELF-specific fields of a symbol. */
2338 bfd_elf_print_symbol (bfd
*abfd
,
2341 bfd_print_symbol_type how
)
2343 FILE *file
= (FILE *) filep
;
2344 const char *symname
= (symbol
->name
!= bfd_symbol_error_name
2345 ? symbol
->name
: _("<corrupt>"));
2349 case bfd_print_symbol_name
:
2350 fprintf (file
, "%s", symname
);
2352 case bfd_print_symbol_more
:
2353 fprintf (file
, "elf ");
2354 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
2355 fprintf (file
, " %x", symbol
->flags
);
2357 case bfd_print_symbol_all
:
2359 const char *section_name
;
2360 const char *name
= NULL
;
2361 const struct elf_backend_data
*bed
;
2362 unsigned char st_other
;
2364 const char *version_string
;
2367 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2369 bed
= get_elf_backend_data (abfd
);
2370 if (bed
->elf_backend_print_symbol_all
)
2371 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
2376 bfd_print_symbol_vandf (abfd
, file
, symbol
);
2378 fprintf (file
, " %s\t", section_name
);
2379 /* Print the "other" value for a symbol. For common symbols,
2380 we've already printed the size; now print the alignment.
2381 For other symbols, we have no specified alignment, and
2382 we've printed the address; now print the size. */
2383 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
2384 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
2386 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
2387 bfd_fprintf_vma (abfd
, file
, val
);
2389 /* If we have version information, print it. */
2390 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2397 fprintf (file
, " %-11s", version_string
);
2402 fprintf (file
, " (%s)", version_string
);
2403 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2408 /* If the st_other field is not zero, print it. */
2409 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2414 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2415 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2416 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2418 /* Some other non-defined flags are also present, so print
2420 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2423 fprintf (file
, " %s", symname
);
2429 /* ELF .o/exec file reading */
2431 /* Create a new bfd section from an ELF section header. */
2434 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2436 Elf_Internal_Shdr
*hdr
;
2437 Elf_Internal_Ehdr
*ehdr
;
2438 const struct elf_backend_data
*bed
;
2442 if (shindex
>= elf_numsections (abfd
))
2445 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2446 sh_link or sh_info. Detect this here, by refusing to load a
2447 section that we are already in the process of loading. */
2448 if (elf_tdata (abfd
)->being_created
[shindex
])
2451 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2454 elf_tdata (abfd
)->being_created
[shindex
] = true;
2456 hdr
= elf_elfsections (abfd
)[shindex
];
2457 ehdr
= elf_elfheader (abfd
);
2458 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2463 bed
= get_elf_backend_data (abfd
);
2464 switch (hdr
->sh_type
)
2467 /* Inactive section. Throw it away. */
2470 case SHT_PROGBITS
: /* Normal section with contents. */
2471 case SHT_NOBITS
: /* .bss section. */
2472 case SHT_HASH
: /* .hash section. */
2473 case SHT_NOTE
: /* .note section. */
2474 case SHT_INIT_ARRAY
: /* .init_array section. */
2475 case SHT_FINI_ARRAY
: /* .fini_array section. */
2476 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2477 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2478 case SHT_GNU_HASH
: /* .gnu.hash section. */
2479 case SHT_GNU_SFRAME
: /* .sframe section. */
2480 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2483 case SHT_DYNAMIC
: /* Dynamic linking information. */
2484 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2487 if (hdr
->sh_link
> elf_numsections (abfd
))
2489 /* PR 10478: Accept Solaris binaries with a sh_link field
2490 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2491 switch (bfd_get_arch (abfd
))
2494 case bfd_arch_sparc
:
2495 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff)
2496 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff))
2498 /* Otherwise fall through. */
2503 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2505 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2507 Elf_Internal_Shdr
*dynsymhdr
;
2509 /* The shared libraries distributed with hpux11 have a bogus
2510 sh_link field for the ".dynamic" section. Find the
2511 string table for the ".dynsym" section instead. */
2512 if (elf_dynsymtab (abfd
) != 0)
2514 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2515 hdr
->sh_link
= dynsymhdr
->sh_link
;
2519 unsigned int i
, num_sec
;
2521 num_sec
= elf_numsections (abfd
);
2522 for (i
= 1; i
< num_sec
; i
++)
2524 dynsymhdr
= elf_elfsections (abfd
)[i
];
2525 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2527 hdr
->sh_link
= dynsymhdr
->sh_link
;
2535 case SHT_SYMTAB
: /* A symbol table. */
2536 if (elf_onesymtab (abfd
) == shindex
)
2539 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2542 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2544 if (hdr
->sh_size
!= 0)
2546 /* Some assemblers erroneously set sh_info to one with a
2547 zero sh_size. ld sees this as a global symbol count
2548 of (unsigned) -1. Fix it here. */
2553 /* PR 18854: A binary might contain more than one symbol table.
2554 Unusual, but possible. Warn, but continue. */
2555 if (elf_onesymtab (abfd
) != 0)
2558 /* xgettext:c-format */
2559 (_("%pB: warning: multiple symbol tables detected"
2560 " - ignoring the table in section %u"),
2564 elf_onesymtab (abfd
) = shindex
;
2565 elf_symtab_hdr (abfd
) = *hdr
;
2566 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2567 abfd
->flags
|= HAS_SYMS
;
2569 /* Sometimes a shared object will map in the symbol table. If
2570 SHF_ALLOC is set, and this is a shared object, then we also
2571 treat this section as a BFD section. We can not base the
2572 decision purely on SHF_ALLOC, because that flag is sometimes
2573 set in a relocatable object file, which would confuse the
2575 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2576 && (abfd
->flags
& DYNAMIC
) != 0
2577 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2581 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2582 can't read symbols without that section loaded as well. It
2583 is most likely specified by the next section header. */
2585 elf_section_list
* entry
;
2586 unsigned int i
, num_sec
;
2588 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2589 if (entry
->hdr
.sh_link
== shindex
)
2592 num_sec
= elf_numsections (abfd
);
2593 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2595 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2597 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2598 && hdr2
->sh_link
== shindex
)
2603 for (i
= 1; i
< shindex
; i
++)
2605 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2607 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2608 && hdr2
->sh_link
== shindex
)
2613 ret
= bfd_section_from_shdr (abfd
, i
);
2614 /* else FIXME: we have failed to find the symbol table.
2615 Should we issue an error? */
2619 case SHT_DYNSYM
: /* A dynamic symbol table. */
2620 if (elf_dynsymtab (abfd
) == shindex
)
2623 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2626 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2628 if (hdr
->sh_size
!= 0)
2631 /* Some linkers erroneously set sh_info to one with a
2632 zero sh_size. ld sees this as a global symbol count
2633 of (unsigned) -1. Fix it here. */
2638 /* PR 18854: A binary might contain more than one dynamic symbol table.
2639 Unusual, but possible. Warn, but continue. */
2640 if (elf_dynsymtab (abfd
) != 0)
2643 /* xgettext:c-format */
2644 (_("%pB: warning: multiple dynamic symbol tables detected"
2645 " - ignoring the table in section %u"),
2649 elf_dynsymtab (abfd
) = shindex
;
2650 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2651 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2652 abfd
->flags
|= HAS_SYMS
;
2654 /* Besides being a symbol table, we also treat this as a regular
2655 section, so that objcopy can handle it. */
2656 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2659 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2661 elf_section_list
* entry
;
2663 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2664 if (entry
->ndx
== shindex
)
2667 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2670 entry
->ndx
= shindex
;
2672 entry
->next
= elf_symtab_shndx_list (abfd
);
2673 elf_symtab_shndx_list (abfd
) = entry
;
2674 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2678 case SHT_STRTAB
: /* A string table. */
2679 if (hdr
->bfd_section
!= NULL
)
2682 if (ehdr
->e_shstrndx
== shindex
)
2684 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2685 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2689 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2692 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2693 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2697 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2700 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2701 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2702 elf_elfsections (abfd
)[shindex
] = hdr
;
2703 /* We also treat this as a regular section, so that objcopy
2705 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2710 /* If the string table isn't one of the above, then treat it as a
2711 regular section. We need to scan all the headers to be sure,
2712 just in case this strtab section appeared before the above. */
2713 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2715 unsigned int i
, num_sec
;
2717 num_sec
= elf_numsections (abfd
);
2718 for (i
= 1; i
< num_sec
; i
++)
2720 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2721 if (hdr2
->sh_link
== shindex
)
2723 /* Prevent endless recursion on broken objects. */
2726 if (! bfd_section_from_shdr (abfd
, i
))
2728 if (elf_onesymtab (abfd
) == i
)
2730 if (elf_dynsymtab (abfd
) == i
)
2731 goto dynsymtab_strtab
;
2735 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2741 /* *These* do a lot of work -- but build no sections! */
2743 asection
*target_sect
;
2744 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2745 unsigned int num_sec
= elf_numsections (abfd
);
2746 struct bfd_elf_section_data
*esdt
;
2749 if (hdr
->sh_type
== SHT_REL
)
2750 size
= bed
->s
->sizeof_rel
;
2751 else if (hdr
->sh_type
== SHT_RELA
)
2752 size
= bed
->s
->sizeof_rela
;
2754 size
= bed
->s
->arch_size
/ 8;
2755 if (hdr
->sh_entsize
!= size
)
2758 /* Check for a bogus link to avoid crashing. */
2759 if (hdr
->sh_link
>= num_sec
)
2762 /* xgettext:c-format */
2763 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2764 abfd
, hdr
->sh_link
, name
, shindex
);
2765 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2769 /* Get the symbol table. */
2770 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2771 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2772 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2775 /* If this is an alloc section in an executable or shared
2776 library, or the reloc section does not use the main symbol
2777 table we don't treat it as a reloc section. BFD can't
2778 adequately represent such a section, so at least for now,
2779 we don't try. We just present it as a normal section. We
2780 also can't use it as a reloc section if it points to the
2781 null section, an invalid section, another reloc section, or
2782 its sh_link points to the null section. */
2783 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2784 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2785 || (hdr
->sh_flags
& SHF_COMPRESSED
) != 0
2786 || hdr
->sh_type
== SHT_RELR
2787 || hdr
->sh_link
== SHN_UNDEF
2788 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2789 || hdr
->sh_info
== SHN_UNDEF
2790 || hdr
->sh_info
>= num_sec
2791 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2792 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2794 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2798 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2801 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2802 if (target_sect
== NULL
)
2805 esdt
= elf_section_data (target_sect
);
2806 if (hdr
->sh_type
== SHT_RELA
)
2807 p_hdr
= &esdt
->rela
.hdr
;
2809 p_hdr
= &esdt
->rel
.hdr
;
2811 /* PR 17512: file: 0b4f81b7.
2812 Also see PR 24456, for a file which deliberately has two reloc
2816 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2819 /* xgettext:c-format */
2820 (_("%pB: warning: secondary relocation section '%s' "
2821 "for section %pA found - ignoring"),
2822 abfd
, name
, target_sect
);
2825 esdt
->has_secondary_relocs
= true;
2829 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2834 elf_elfsections (abfd
)[shindex
] = hdr2
;
2835 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2836 * bed
->s
->int_rels_per_ext_rel
);
2837 target_sect
->flags
|= SEC_RELOC
;
2838 target_sect
->relocation
= NULL
;
2839 target_sect
->rel_filepos
= hdr
->sh_offset
;
2840 /* In the section to which the relocations apply, mark whether
2841 its relocations are of the REL or RELA variety. */
2842 if (hdr
->sh_size
!= 0)
2844 if (hdr
->sh_type
== SHT_RELA
)
2845 target_sect
->use_rela_p
= 1;
2847 abfd
->flags
|= HAS_RELOC
;
2851 case SHT_GNU_verdef
:
2852 if (hdr
->sh_info
!= 0)
2853 elf_dynverdef (abfd
) = shindex
;
2854 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2855 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2858 case SHT_GNU_versym
:
2859 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2862 elf_dynversym (abfd
) = shindex
;
2863 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2864 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2867 case SHT_GNU_verneed
:
2868 if (hdr
->sh_info
!= 0)
2869 elf_dynverref (abfd
) = shindex
;
2870 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2871 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2878 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2884 /* Possibly an attributes section. */
2885 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2886 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2888 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2890 _bfd_elf_parse_attributes (abfd
, hdr
);
2894 /* Check for any processor-specific section types. */
2895 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2898 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2900 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2901 /* FIXME: How to properly handle allocated section reserved
2902 for applications? */
2904 /* xgettext:c-format */
2905 (_("%pB: unknown type [%#x] section `%s'"),
2906 abfd
, hdr
->sh_type
, name
);
2909 /* Allow sections reserved for applications. */
2910 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2914 else if (hdr
->sh_type
>= SHT_LOPROC
2915 && hdr
->sh_type
<= SHT_HIPROC
)
2916 /* FIXME: We should handle this section. */
2918 /* xgettext:c-format */
2919 (_("%pB: unknown type [%#x] section `%s'"),
2920 abfd
, hdr
->sh_type
, name
);
2921 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2923 /* Unrecognised OS-specific sections. */
2924 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2925 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2926 required to correctly process the section and the file should
2927 be rejected with an error message. */
2929 /* xgettext:c-format */
2930 (_("%pB: unknown type [%#x] section `%s'"),
2931 abfd
, hdr
->sh_type
, name
);
2934 /* Otherwise it should be processed. */
2935 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2940 /* FIXME: We should handle this section. */
2942 /* xgettext:c-format */
2943 (_("%pB: unknown type [%#x] section `%s'"),
2944 abfd
, hdr
->sh_type
, name
);
2952 elf_tdata (abfd
)->being_created
[shindex
] = false;
2956 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2959 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2961 unsigned long r_symndx
)
2963 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2965 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2967 Elf_Internal_Shdr
*symtab_hdr
;
2968 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2969 Elf_External_Sym_Shndx eshndx
;
2971 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2972 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2973 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2976 if (cache
->abfd
!= abfd
)
2978 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2981 cache
->indx
[ent
] = r_symndx
;
2984 return &cache
->sym
[ent
];
2987 /* Given an ELF section number, retrieve the corresponding BFD
2991 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2993 if (sec_index
>= elf_numsections (abfd
))
2995 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2998 static const struct bfd_elf_special_section special_sections_b
[] =
3000 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3001 { NULL
, 0, 0, 0, 0 }
3004 static const struct bfd_elf_special_section special_sections_c
[] =
3006 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
3007 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
3008 { NULL
, 0, 0, 0, 0 }
3011 static const struct bfd_elf_special_section special_sections_d
[] =
3013 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3014 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3015 /* There are more DWARF sections than these, but they needn't be added here
3016 unless you have to cope with broken compilers that don't emit section
3017 attributes or you want to help the user writing assembler. */
3018 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
3019 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
3020 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
3021 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
3022 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
3023 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
3024 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
3025 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
3026 { NULL
, 0, 0, 0, 0 }
3029 static const struct bfd_elf_special_section special_sections_f
[] =
3031 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3032 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3033 { NULL
, 0 , 0, 0, 0 }
3036 static const struct bfd_elf_special_section special_sections_g
[] =
3038 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3039 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3040 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3041 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
3042 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3043 { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY
, SHF_EXCLUDE
},
3044 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
3045 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
3046 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
3047 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
3048 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
3049 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
3050 { NULL
, 0, 0, 0, 0 }
3053 static const struct bfd_elf_special_section special_sections_h
[] =
3055 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
3056 { NULL
, 0, 0, 0, 0 }
3059 static const struct bfd_elf_special_section special_sections_i
[] =
3061 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3062 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3063 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
3064 { NULL
, 0, 0, 0, 0 }
3067 static const struct bfd_elf_special_section special_sections_l
[] =
3069 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
3070 { NULL
, 0, 0, 0, 0 }
3073 static const struct bfd_elf_special_section special_sections_n
[] =
3075 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3076 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
3077 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
3078 { NULL
, 0, 0, 0, 0 }
3081 static const struct bfd_elf_special_section special_sections_p
[] =
3083 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3084 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3085 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3086 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3087 { NULL
, 0, 0, 0, 0 }
3090 static const struct bfd_elf_special_section special_sections_r
[] =
3092 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
3093 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
3094 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR
, SHF_ALLOC
},
3095 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
3096 /* .relro_padding is generated by lld. It should not be confused with a
3097 reloc containing section, because otherwise elf_fake_sections() will
3098 set the entsize to 8, which may not be an actual multiple of the
3100 Note - this entry must appear before the ".rel" entry below. */
3101 { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
},
3102 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
3103 { NULL
, 0, 0, 0, 0 }
3106 static const struct bfd_elf_special_section special_sections_s
[] =
3108 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
3109 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
3110 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
3111 /* See struct bfd_elf_special_section declaration for the semantics of
3112 this special case where .prefix_length != strlen (.prefix). */
3113 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
3114 { NULL
, 0, 0, 0, 0 }
3117 static const struct bfd_elf_special_section special_sections_t
[] =
3119 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3120 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
3121 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
3122 { NULL
, 0, 0, 0, 0 }
3125 static const struct bfd_elf_special_section special_sections_z
[] =
3127 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
3128 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
3129 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
3130 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
3131 { NULL
, 0, 0, 0, 0 }
3134 static const struct bfd_elf_special_section
* const special_sections
[] =
3136 special_sections_b
, /* 'b' */
3137 special_sections_c
, /* 'c' */
3138 special_sections_d
, /* 'd' */
3140 special_sections_f
, /* 'f' */
3141 special_sections_g
, /* 'g' */
3142 special_sections_h
, /* 'h' */
3143 special_sections_i
, /* 'i' */
3146 special_sections_l
, /* 'l' */
3148 special_sections_n
, /* 'n' */
3150 special_sections_p
, /* 'p' */
3152 special_sections_r
, /* 'r' */
3153 special_sections_s
, /* 's' */
3154 special_sections_t
, /* 't' */
3160 special_sections_z
/* 'z' */
3163 const struct bfd_elf_special_section
*
3164 _bfd_elf_get_special_section (const char *name
,
3165 const struct bfd_elf_special_section
*spec
,
3171 len
= strlen (name
);
3173 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
3176 int prefix_len
= spec
[i
].prefix_length
;
3178 if (len
< prefix_len
)
3180 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
3183 suffix_len
= spec
[i
].suffix_length
;
3184 if (suffix_len
<= 0)
3186 if (name
[prefix_len
] != 0)
3188 if (suffix_len
== 0)
3190 if (name
[prefix_len
] != '.'
3191 && (suffix_len
== -2
3192 || (rela
&& spec
[i
].type
== SHT_REL
)))
3198 if (len
< prefix_len
+ suffix_len
)
3200 if (memcmp (name
+ len
- suffix_len
,
3201 spec
[i
].prefix
+ prefix_len
,
3211 const struct bfd_elf_special_section
*
3212 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
3215 const struct bfd_elf_special_section
*spec
;
3216 const struct elf_backend_data
*bed
;
3218 /* See if this is one of the special sections. */
3219 if (sec
->name
== NULL
)
3222 bed
= get_elf_backend_data (abfd
);
3223 spec
= bed
->special_sections
;
3226 spec
= _bfd_elf_get_special_section (sec
->name
,
3227 bed
->special_sections
,
3233 if (sec
->name
[0] != '.')
3236 i
= sec
->name
[1] - 'b';
3237 if (i
< 0 || i
> 'z' - 'b')
3240 spec
= special_sections
[i
];
3245 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
3249 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
3251 struct bfd_elf_section_data
*sdata
;
3252 const struct elf_backend_data
*bed
;
3253 const struct bfd_elf_special_section
*ssect
;
3255 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
3258 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
3262 sec
->used_by_bfd
= sdata
;
3265 /* Indicate whether or not this section should use RELA relocations. */
3266 bed
= get_elf_backend_data (abfd
);
3267 sec
->use_rela_p
= bed
->default_use_rela_p
;
3269 /* Set up ELF section type and flags for newly created sections, if
3270 there is an ABI mandated section. */
3271 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
3274 elf_section_type (sec
) = ssect
->type
;
3275 elf_section_flags (sec
) = ssect
->attr
;
3278 return _bfd_generic_new_section_hook (abfd
, sec
);
3281 /* Create a new bfd section from an ELF program header.
3283 Since program segments have no names, we generate a synthetic name
3284 of the form segment<NUM>, where NUM is generally the index in the
3285 program header table. For segments that are split (see below) we
3286 generate the names segment<NUM>a and segment<NUM>b.
3288 Note that some program segments may have a file size that is different than
3289 (less than) the memory size. All this means is that at execution the
3290 system must allocate the amount of memory specified by the memory size,
3291 but only initialize it with the first "file size" bytes read from the
3292 file. This would occur for example, with program segments consisting
3293 of combined data+bss.
3295 To handle the above situation, this routine generates TWO bfd sections
3296 for the single program segment. The first has the length specified by
3297 the file size of the segment, and the second has the length specified
3298 by the difference between the two sizes. In effect, the segment is split
3299 into its initialized and uninitialized parts. */
3302 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
3303 Elf_Internal_Phdr
*hdr
,
3305 const char *type_name
)
3312 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
3314 split
= ((hdr
->p_memsz
> 0)
3315 && (hdr
->p_filesz
> 0)
3316 && (hdr
->p_memsz
> hdr
->p_filesz
));
3318 if (hdr
->p_filesz
> 0)
3320 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
3321 len
= strlen (namebuf
) + 1;
3322 name
= (char *) bfd_alloc (abfd
, len
);
3325 memcpy (name
, namebuf
, len
);
3326 newsect
= bfd_make_section (abfd
, name
);
3327 if (newsect
== NULL
)
3329 newsect
->vma
= hdr
->p_vaddr
/ opb
;
3330 newsect
->lma
= hdr
->p_paddr
/ opb
;
3331 newsect
->size
= hdr
->p_filesz
;
3332 newsect
->filepos
= hdr
->p_offset
;
3333 newsect
->flags
|= SEC_HAS_CONTENTS
;
3334 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
3335 if (hdr
->p_type
== PT_LOAD
)
3337 newsect
->flags
|= SEC_ALLOC
;
3338 newsect
->flags
|= SEC_LOAD
;
3339 if (hdr
->p_flags
& PF_X
)
3341 /* FIXME: all we known is that it has execute PERMISSION,
3343 newsect
->flags
|= SEC_CODE
;
3346 if (!(hdr
->p_flags
& PF_W
))
3348 newsect
->flags
|= SEC_READONLY
;
3352 if (hdr
->p_memsz
> hdr
->p_filesz
)
3356 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3357 len
= strlen (namebuf
) + 1;
3358 name
= (char *) bfd_alloc (abfd
, len
);
3361 memcpy (name
, namebuf
, len
);
3362 newsect
= bfd_make_section (abfd
, name
);
3363 if (newsect
== NULL
)
3365 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3366 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3367 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3368 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3369 align
= newsect
->vma
& -newsect
->vma
;
3370 if (align
== 0 || align
> hdr
->p_align
)
3371 align
= hdr
->p_align
;
3372 newsect
->alignment_power
= bfd_log2 (align
);
3373 if (hdr
->p_type
== PT_LOAD
)
3375 newsect
->flags
|= SEC_ALLOC
;
3376 if (hdr
->p_flags
& PF_X
)
3377 newsect
->flags
|= SEC_CODE
;
3379 if (!(hdr
->p_flags
& PF_W
))
3380 newsect
->flags
|= SEC_READONLY
;
3387 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3389 /* The return value is ignored. Build-ids are considered optional. */
3390 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3391 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3397 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3399 const struct elf_backend_data
*bed
;
3401 switch (hdr
->p_type
)
3404 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3407 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3409 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3410 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3414 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3417 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3420 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3422 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3428 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3431 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3433 case PT_GNU_EH_FRAME
:
3434 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3438 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3441 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3444 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3448 /* Check for any processor-specific program segment types. */
3449 bed
= get_elf_backend_data (abfd
);
3450 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3454 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3458 _bfd_elf_single_rel_hdr (asection
*sec
)
3460 if (elf_section_data (sec
)->rel
.hdr
)
3462 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3463 return elf_section_data (sec
)->rel
.hdr
;
3466 return elf_section_data (sec
)->rela
.hdr
;
3470 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3471 Elf_Internal_Shdr
*rel_hdr
,
3472 const char *sec_name
,
3475 char *name
= (char *) bfd_alloc (abfd
,
3476 sizeof ".rela" + strlen (sec_name
));
3480 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3482 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3484 if (rel_hdr
->sh_name
== (unsigned int) -1)
3490 /* Allocate and initialize a section-header for a new reloc section,
3491 containing relocations against ASECT. It is stored in RELDATA. If
3492 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3496 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3497 struct bfd_elf_section_reloc_data
*reldata
,
3498 const char *sec_name
,
3500 bool delay_sh_name_p
)
3502 Elf_Internal_Shdr
*rel_hdr
;
3503 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3505 BFD_ASSERT (reldata
->hdr
== NULL
);
3506 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3507 if (rel_hdr
== NULL
)
3509 reldata
->hdr
= rel_hdr
;
3511 if (delay_sh_name_p
)
3512 rel_hdr
->sh_name
= (unsigned int) -1;
3513 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3516 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3517 rel_hdr
->sh_entsize
= (use_rela_p
3518 ? bed
->s
->sizeof_rela
3519 : bed
->s
->sizeof_rel
);
3520 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3521 rel_hdr
->sh_flags
= 0;
3522 rel_hdr
->sh_addr
= 0;
3523 rel_hdr
->sh_size
= 0;
3524 rel_hdr
->sh_offset
= 0;
3529 /* Return the default section type based on the passed in section flags. */
3532 bfd_elf_get_default_section_type (flagword flags
)
3534 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3535 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3537 return SHT_PROGBITS
;
3540 struct fake_section_arg
3542 struct bfd_link_info
*link_info
;
3546 /* Set up an ELF internal section header for a section. */
3549 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3551 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3552 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3553 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3554 Elf_Internal_Shdr
*this_hdr
;
3555 unsigned int sh_type
;
3556 const char *name
= asect
->name
;
3557 bool delay_sh_name_p
= false;
3562 /* We already failed; just get out of the bfd_map_over_sections
3567 this_hdr
= &esd
->this_hdr
;
3569 /* ld: compress DWARF debug sections with names: .debug_*. */
3571 && (abfd
->flags
& BFD_COMPRESS
) != 0
3572 && (asect
->flags
& SEC_DEBUGGING
) != 0
3573 && (asect
->flags
& SEC_ALLOC
) == 0
3574 && (asect
->flags
& SEC_HAS_CONTENTS
) != 0
3578 /* If this section will be compressed, delay adding section
3579 name to section name section after it is compressed in
3580 _bfd_elf_assign_file_positions_for_non_load. */
3581 delay_sh_name_p
= true;
3584 if (delay_sh_name_p
)
3585 this_hdr
->sh_name
= (unsigned int) -1;
3589 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3591 if (this_hdr
->sh_name
== (unsigned int) -1)
3598 /* Don't clear sh_flags. Assembler may set additional bits. */
3600 if ((asect
->flags
& SEC_ALLOC
) != 0
3601 || asect
->user_set_vma
)
3602 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3604 this_hdr
->sh_addr
= 0;
3606 this_hdr
->sh_offset
= 0;
3607 this_hdr
->sh_size
= asect
->size
;
3608 this_hdr
->sh_link
= 0;
3609 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3610 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3613 /* xgettext:c-format */
3614 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3615 abfd
, asect
->alignment_power
, asect
);
3619 /* Set sh_addralign to the highest power of two given by alignment
3620 consistent with the section VMA. Linker scripts can force VMA. */
3621 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3622 this_hdr
->sh_addralign
= mask
& -mask
;
3623 /* The sh_entsize and sh_info fields may have been set already by
3624 copy_private_section_data. */
3626 this_hdr
->bfd_section
= asect
;
3627 this_hdr
->contents
= NULL
;
3629 /* If the section type is unspecified, we set it based on
3631 if (asect
->type
!= 0)
3632 sh_type
= asect
->type
;
3633 else if ((asect
->flags
& SEC_GROUP
) != 0)
3634 sh_type
= SHT_GROUP
;
3636 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3638 if (this_hdr
->sh_type
== SHT_NULL
)
3639 this_hdr
->sh_type
= sh_type
;
3640 else if (this_hdr
->sh_type
== SHT_NOBITS
3641 && sh_type
== SHT_PROGBITS
3642 && (asect
->flags
& SEC_ALLOC
) != 0)
3644 /* Warn if we are changing a NOBITS section to PROGBITS, but
3645 allow the link to proceed. This can happen when users link
3646 non-bss input sections to bss output sections, or emit data
3647 to a bss output section via a linker script. */
3649 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3650 this_hdr
->sh_type
= sh_type
;
3653 switch (this_hdr
->sh_type
)
3664 case SHT_INIT_ARRAY
:
3665 case SHT_FINI_ARRAY
:
3666 case SHT_PREINIT_ARRAY
:
3667 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3671 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3675 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3679 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3683 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3684 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3688 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3689 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3692 case SHT_GNU_versym
:
3693 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3696 case SHT_GNU_verdef
:
3697 this_hdr
->sh_entsize
= 0;
3698 /* objcopy or strip will copy over sh_info, but may not set
3699 cverdefs. The linker will set cverdefs, but sh_info will be
3701 if (this_hdr
->sh_info
== 0)
3702 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3704 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3705 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3708 case SHT_GNU_verneed
:
3709 this_hdr
->sh_entsize
= 0;
3710 /* objcopy or strip will copy over sh_info, but may not set
3711 cverrefs. The linker will set cverrefs, but sh_info will be
3713 if (this_hdr
->sh_info
== 0)
3714 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3716 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3717 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3721 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3725 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3729 if ((asect
->flags
& SEC_ALLOC
) != 0)
3730 this_hdr
->sh_flags
|= SHF_ALLOC
;
3731 if ((asect
->flags
& SEC_READONLY
) == 0)
3732 this_hdr
->sh_flags
|= SHF_WRITE
;
3733 if ((asect
->flags
& SEC_CODE
) != 0)
3734 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3735 if ((asect
->flags
& SEC_MERGE
) != 0)
3737 this_hdr
->sh_flags
|= SHF_MERGE
;
3738 this_hdr
->sh_entsize
= asect
->entsize
;
3740 if ((asect
->flags
& SEC_STRINGS
) != 0)
3742 this_hdr
->sh_flags
|= SHF_STRINGS
;
3743 this_hdr
->sh_entsize
= asect
->entsize
;
3745 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3746 this_hdr
->sh_flags
|= SHF_GROUP
;
3747 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3749 this_hdr
->sh_flags
|= SHF_TLS
;
3750 if (asect
->size
== 0
3751 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3753 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3755 this_hdr
->sh_size
= 0;
3758 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3759 if (this_hdr
->sh_size
!= 0)
3760 this_hdr
->sh_type
= SHT_NOBITS
;
3764 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3765 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3767 /* If the section has relocs, set up a section header for the
3768 SHT_REL[A] section. If two relocation sections are required for
3769 this section, it is up to the processor-specific back-end to
3770 create the other. */
3771 if ((asect
->flags
& SEC_RELOC
) != 0)
3773 /* When doing a relocatable link, create both REL and RELA sections if
3776 /* Do the normal setup if we wouldn't create any sections here. */
3777 && esd
->rel
.count
+ esd
->rela
.count
> 0
3778 && (bfd_link_relocatable (arg
->link_info
)
3779 || arg
->link_info
->emitrelocations
))
3781 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3782 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3783 false, delay_sh_name_p
))
3788 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3789 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3790 true, delay_sh_name_p
))
3796 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3798 ? &esd
->rela
: &esd
->rel
),
3808 /* Check for processor-specific section types. */
3809 sh_type
= this_hdr
->sh_type
;
3810 if (bed
->elf_backend_fake_sections
3811 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3817 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3819 /* Don't change the header type from NOBITS if we are being
3820 called for objcopy --only-keep-debug. */
3821 this_hdr
->sh_type
= sh_type
;
3825 /* Fill in the contents of a SHT_GROUP section. Called from
3826 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3827 when ELF targets use the generic linker, ld. Called for ld -r
3828 from bfd_elf_final_link. */
3831 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3833 bool *failedptr
= (bool *) failedptrarg
;
3834 asection
*elt
, *first
;
3838 /* Ignore linker created group section. See elfNN_ia64_object_p in
3840 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3845 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3847 unsigned long symindx
= 0;
3849 /* elf_group_id will have been set up by objcopy and the
3851 if (elf_group_id (sec
) != NULL
)
3852 symindx
= elf_group_id (sec
)->udata
.i
;
3856 /* If called from the assembler, swap_out_syms will have set up
3858 PR 25699: A corrupt input file could contain bogus group info. */
3859 if (sec
->index
>= elf_num_section_syms (abfd
)
3860 || elf_section_syms (abfd
)[sec
->index
] == NULL
)
3865 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3867 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3869 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3871 /* The ELF backend linker sets sh_info to -2 when the group
3872 signature symbol is global, and thus the index can't be
3873 set until all local symbols are output. */
3875 struct bfd_elf_section_data
*sec_data
;
3876 unsigned long symndx
;
3877 unsigned long extsymoff
;
3878 struct elf_link_hash_entry
*h
;
3880 /* The point of this little dance to the first SHF_GROUP section
3881 then back to the SHT_GROUP section is that this gets us to
3882 the SHT_GROUP in the input object. */
3883 igroup
= elf_sec_group (elf_next_in_group (sec
));
3884 sec_data
= elf_section_data (igroup
);
3885 symndx
= sec_data
->this_hdr
.sh_info
;
3887 if (!elf_bad_symtab (igroup
->owner
))
3889 Elf_Internal_Shdr
*symtab_hdr
;
3891 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3892 extsymoff
= symtab_hdr
->sh_info
;
3894 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3895 while (h
->root
.type
== bfd_link_hash_indirect
3896 || h
->root
.type
== bfd_link_hash_warning
)
3897 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3899 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3902 /* The contents won't be allocated for "ld -r" or objcopy. */
3904 if (sec
->contents
== NULL
)
3907 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3909 /* Arrange for the section to be written out. */
3910 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3911 if (sec
->contents
== NULL
)
3919 loc
= sec
->contents
+ sec
->size
;
3921 /* Get the pointer to the first section in the group that gas
3922 squirreled away here. objcopy arranges for this to be set to the
3923 start of the input section group. */
3924 first
= elt
= elf_next_in_group (sec
);
3926 /* First element is a flag word. Rest of section is elf section
3927 indices for all the sections of the group. Write them backwards
3928 just to keep the group in the same order as given in .section
3929 directives, not that it matters. */
3936 s
= s
->output_section
;
3938 && !bfd_is_abs_section (s
))
3940 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3941 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3943 if (elf_sec
->rel
.hdr
!= NULL
3945 || (input_elf_sec
->rel
.hdr
!= NULL
3946 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3948 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3950 if (loc
== sec
->contents
)
3952 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3954 if (elf_sec
->rela
.hdr
!= NULL
3956 || (input_elf_sec
->rela
.hdr
!= NULL
3957 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3959 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3961 if (loc
== sec
->contents
)
3963 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3966 if (loc
== sec
->contents
)
3968 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3970 elt
= elf_next_in_group (elt
);
3975 /* We should always get here with loc == sec->contents + 4. Return
3976 an error for bogus SHT_GROUP sections. */
3978 if (loc
!= sec
->contents
)
3980 /* xgettext:c-format */
3981 _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
3983 bfd_set_error (bfd_error_bad_value
);
3988 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3991 /* Given NAME, the name of a relocation section stripped of its
3992 .rel/.rela prefix, return the section in ABFD to which the
3993 relocations apply. */
3996 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3998 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3999 section likely apply to .got.plt or .got section. */
4000 if (get_elf_backend_data (abfd
)->want_got_plt
4001 && strcmp (name
, ".plt") == 0)
4006 sec
= bfd_get_section_by_name (abfd
, name
);
4012 return bfd_get_section_by_name (abfd
, name
);
4015 /* Return the section to which RELOC_SEC applies. */
4018 elf_get_reloc_section (asection
*reloc_sec
)
4023 const struct elf_backend_data
*bed
;
4025 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
4026 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
4029 /* We look up the section the relocs apply to by name. */
4030 name
= reloc_sec
->name
;
4031 if (!startswith (name
, ".rel"))
4034 if (type
== SHT_RELA
&& *name
++ != 'a')
4037 abfd
= reloc_sec
->owner
;
4038 bed
= get_elf_backend_data (abfd
);
4039 return bed
->get_reloc_section (abfd
, name
);
4042 /* Assign all ELF section numbers. The dummy first section is handled here
4043 too. The link/info pointers for the standard section types are filled
4044 in here too, while we're at it. LINK_INFO will be 0 when arriving
4045 here for gas, objcopy, and when using the generic ELF linker. */
4048 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
4050 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
4052 unsigned int section_number
;
4053 Elf_Internal_Shdr
**i_shdrp
;
4054 struct bfd_elf_section_data
*d
;
4060 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
4062 /* SHT_GROUP sections are in relocatable files only. */
4063 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
4065 size_t reloc_count
= 0;
4067 /* Put SHT_GROUP sections first. */
4068 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4070 d
= elf_section_data (sec
);
4072 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
4074 if (sec
->flags
& SEC_LINKER_CREATED
)
4076 /* Remove the linker created SHT_GROUP sections. */
4077 bfd_section_list_remove (abfd
, sec
);
4078 abfd
->section_count
--;
4081 d
->this_idx
= section_number
++;
4084 /* Count relocations. */
4085 reloc_count
+= sec
->reloc_count
;
4088 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4089 if (reloc_count
== 0)
4090 abfd
->flags
&= ~HAS_RELOC
;
4092 abfd
->flags
|= HAS_RELOC
;
4095 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4097 d
= elf_section_data (sec
);
4099 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
4100 d
->this_idx
= section_number
++;
4101 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
4102 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
4105 d
->rel
.idx
= section_number
++;
4106 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
4107 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
4114 d
->rela
.idx
= section_number
++;
4115 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
4116 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
4122 need_symtab
= (bfd_get_symcount (abfd
) > 0
4123 || (link_info
== NULL
4124 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4128 elf_onesymtab (abfd
) = section_number
++;
4129 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
4130 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
4132 elf_section_list
*entry
;
4134 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
4136 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
4137 entry
->ndx
= section_number
++;
4138 elf_symtab_shndx_list (abfd
) = entry
;
4140 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
4141 ".symtab_shndx", false);
4142 if (entry
->hdr
.sh_name
== (unsigned int) -1)
4145 elf_strtab_sec (abfd
) = section_number
++;
4146 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
4149 elf_shstrtab_sec (abfd
) = section_number
++;
4150 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
4151 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
4153 if (section_number
>= SHN_LORESERVE
)
4155 /* xgettext:c-format */
4156 _bfd_error_handler (_("%pB: too many sections: %u"),
4157 abfd
, section_number
);
4161 elf_numsections (abfd
) = section_number
;
4162 elf_elfheader (abfd
)->e_shnum
= section_number
;
4164 /* Set up the list of section header pointers, in agreement with the
4166 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
4167 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
4168 if (i_shdrp
== NULL
)
4171 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
4172 sizeof (Elf_Internal_Shdr
));
4173 if (i_shdrp
[0] == NULL
)
4175 bfd_release (abfd
, i_shdrp
);
4179 elf_elfsections (abfd
) = i_shdrp
;
4181 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
4184 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
4185 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
4187 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
4188 BFD_ASSERT (entry
!= NULL
);
4189 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
4190 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
4192 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
4193 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
4196 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4200 d
= elf_section_data (sec
);
4202 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
4203 if (d
->rel
.idx
!= 0)
4204 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
4205 if (d
->rela
.idx
!= 0)
4206 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
4208 /* Fill in the sh_link and sh_info fields while we're at it. */
4210 /* sh_link of a reloc section is the section index of the symbol
4211 table. sh_info is the section index of the section to which
4212 the relocation entries apply. */
4213 if (d
->rel
.idx
!= 0)
4215 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
4216 d
->rel
.hdr
->sh_info
= d
->this_idx
;
4217 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
4219 if (d
->rela
.idx
!= 0)
4221 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
4222 d
->rela
.hdr
->sh_info
= d
->this_idx
;
4223 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
4226 /* We need to set up sh_link for SHF_LINK_ORDER. */
4227 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
4229 s
= elf_linked_to_section (sec
);
4230 /* We can now have a NULL linked section pointer.
4231 This happens when the sh_link field is 0, which is done
4232 when a linked to section is discarded but the linking
4233 section has been retained for some reason. */
4236 /* Check discarded linkonce section. */
4237 if (discarded_section (s
))
4241 /* xgettext:c-format */
4242 (_("%pB: sh_link of section `%pA' points to"
4243 " discarded section `%pA' of `%pB'"),
4244 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
4245 /* Point to the kept section if it has the same
4246 size as the discarded one. */
4247 kept
= _bfd_elf_check_kept_section (s
, link_info
);
4250 bfd_set_error (bfd_error_bad_value
);
4255 /* Handle objcopy. */
4256 else if (s
->output_section
== NULL
)
4259 /* xgettext:c-format */
4260 (_("%pB: sh_link of section `%pA' points to"
4261 " removed section `%pA' of `%pB'"),
4262 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
4263 bfd_set_error (bfd_error_bad_value
);
4266 s
= s
->output_section
;
4267 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4271 switch (d
->this_hdr
.sh_type
)
4275 /* sh_link is the section index of the symbol table.
4276 sh_info is the section index of the section to which the
4277 relocation entries apply. */
4278 if (d
->this_hdr
.sh_link
== 0)
4280 /* FIXME maybe: If this is a reloc section which we are
4281 treating as a normal section then we likely should
4282 not be assuming its sh_link is .dynsym or .symtab. */
4283 if ((sec
->flags
& SEC_ALLOC
) != 0)
4285 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4287 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4290 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4293 s
= elf_get_reloc_section (sec
);
4296 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
4297 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
4302 /* We assume that a section named .stab*str is a stabs
4303 string section. We look for a section with the same name
4304 but without the trailing ``str'', and set its sh_link
4305 field to point to this section. */
4306 if (startswith (sec
->name
, ".stab")
4307 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
4312 len
= strlen (sec
->name
);
4313 alc
= (char *) bfd_malloc (len
- 2);
4316 memcpy (alc
, sec
->name
, len
- 3);
4317 alc
[len
- 3] = '\0';
4318 s
= bfd_get_section_by_name (abfd
, alc
);
4322 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
4324 /* This is a .stab section. */
4325 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
4332 case SHT_GNU_verneed
:
4333 case SHT_GNU_verdef
:
4334 /* sh_link is the section header index of the string table
4335 used for the dynamic entries, or the symbol table, or the
4337 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4339 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4342 case SHT_GNU_LIBLIST
:
4343 /* sh_link is the section header index of the prelink library
4344 list used for the dynamic entries, or the symbol table, or
4345 the version strings. */
4346 s
= bfd_get_section_by_name (abfd
, ((sec
->flags
& SEC_ALLOC
)
4347 ? ".dynstr" : ".gnu.libstr"));
4349 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4354 case SHT_GNU_versym
:
4355 /* sh_link is the section header index of the symbol table
4356 this hash table or version table is for. */
4357 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4359 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4363 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4367 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4368 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4369 debug section name from .debug_* to .zdebug_* if needed. */
4375 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4377 /* If the backend has a special mapping, use it. */
4378 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4379 if (bed
->elf_backend_sym_is_global
)
4380 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4382 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4383 || bfd_is_und_section (bfd_asymbol_section (sym
))
4384 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4387 /* Filter global symbols of ABFD to include in the import library. All
4388 SYMCOUNT symbols of ABFD can be examined from their pointers in
4389 SYMS. Pointers of symbols to keep should be stored contiguously at
4390 the beginning of that array.
4392 Returns the number of symbols to keep. */
4395 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4396 asymbol
**syms
, long symcount
)
4398 long src_count
, dst_count
= 0;
4400 for (src_count
= 0; src_count
< symcount
; src_count
++)
4402 asymbol
*sym
= syms
[src_count
];
4403 char *name
= (char *) bfd_asymbol_name (sym
);
4404 struct bfd_link_hash_entry
*h
;
4406 if (!sym_is_global (abfd
, sym
))
4409 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, false);
4412 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4414 if (h
->linker_def
|| h
->ldscript_def
)
4417 syms
[dst_count
++] = sym
;
4420 syms
[dst_count
] = NULL
;
4425 /* Don't output symbols for sections that are not going to be output,
4426 that are duplicates or there is no BFD section. */
4429 ignore_sym (asymbol
*sym
)
4434 if (sym
->section
== NULL
)
4437 if ((sym
->flags
& BSF_SECTION_SYM
) != 0)
4439 if ((sym
->flags
& BSF_SECTION_SYM_USED
) == 0)
4441 /* With ld -r on generic elf targets it is possible to have
4442 multiple section symbols in the output for a given section.
4443 We'd like to get rid of all but the first one. This drops
4444 them if the first input section is non-zero size, but fails
4445 to do so if the first input section is zero sized. */
4446 if (sym
->section
->output_offset
!= 0)
4450 return discarded_section (sym
->section
);
4453 /* Map symbol from it's internal number to the external number, moving
4454 all local symbols to be at the head of the list. */
4457 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4459 unsigned int symcount
= bfd_get_symcount (abfd
);
4460 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4461 asymbol
**sect_syms
;
4462 unsigned int num_locals
= 0;
4463 unsigned int num_globals
= 0;
4464 unsigned int max_index
= 0;
4471 fprintf (stderr
, "elf_map_symbols\n");
4475 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4477 if (max_index
< asect
->index
)
4478 max_index
= asect
->index
;
4482 amt
= max_index
* sizeof (asymbol
*);
4483 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4484 if (sect_syms
== NULL
)
4486 elf_section_syms (abfd
) = sect_syms
;
4487 elf_num_section_syms (abfd
) = max_index
;
4489 /* Init sect_syms entries for any section symbols we have already
4490 decided to output. */
4491 for (idx
= 0; idx
< symcount
; idx
++)
4493 asymbol
*sym
= syms
[idx
];
4495 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4497 && !ignore_sym (sym
)
4498 && !bfd_is_abs_section (sym
->section
))
4500 asection
*sec
= sym
->section
;
4502 if (sec
->owner
!= abfd
)
4504 sec
= sec
->output_section
;
4509 sect_syms
[sec
->index
] = syms
[idx
];
4513 /* Classify all of the symbols. */
4514 for (idx
= 0; idx
< symcount
; idx
++)
4516 if (ignore_sym (syms
[idx
]))
4518 if (sym_is_global (abfd
, syms
[idx
]))
4524 /* We will be adding a section symbol for each normal BFD section. Most
4525 sections will already have a section symbol in outsymbols, but
4526 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4527 at least in that case. */
4528 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4530 asymbol
*sym
= asect
->symbol
;
4531 /* Don't include ignored section symbols. */
4532 if (!ignore_sym (sym
)
4533 && sect_syms
[asect
->index
] == NULL
)
4535 if (sym_is_global (abfd
, asect
->symbol
))
4542 /* Now sort the symbols so the local symbols are first. */
4543 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4544 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4545 if (new_syms
== NULL
)
4548 unsigned int num_globals2
= 0;
4549 unsigned int num_locals2
= 0;
4550 for (idx
= 0; idx
< symcount
; idx
++)
4552 asymbol
*sym
= syms
[idx
];
4555 if (ignore_sym (sym
))
4558 if (sym_is_global (abfd
, sym
))
4559 i
= num_locals
+ num_globals2
++;
4563 sym
->udata
.i
= i
+ 1;
4565 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4567 asymbol
*sym
= asect
->symbol
;
4568 if (!ignore_sym (sym
)
4569 && sect_syms
[asect
->index
] == NULL
)
4573 sect_syms
[asect
->index
] = sym
;
4574 if (sym_is_global (abfd
, sym
))
4575 i
= num_locals
+ num_globals2
++;
4579 sym
->udata
.i
= i
+ 1;
4583 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4585 *pnum_locals
= num_locals
;
4589 /* Assign a file position to a section, optionally aligning to the
4590 required section alignment. */
4593 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4596 unsigned char log_file_align
)
4598 if (i_shdrp
->sh_addralign
> 1)
4600 file_ptr salign
= i_shdrp
->sh_addralign
& -i_shdrp
->sh_addralign
;
4603 offset
= BFD_ALIGN (offset
, salign
);
4604 else if (log_file_align
)
4606 /* Heuristic: Cap alignment at log_file_align. */
4607 file_ptr falign
= 1u << log_file_align
;
4609 offset
= BFD_ALIGN (offset
, salign
< falign
? salign
: falign
);
4612 i_shdrp
->sh_offset
= offset
;
4613 if (i_shdrp
->bfd_section
!= NULL
)
4614 i_shdrp
->bfd_section
->filepos
= offset
;
4615 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4616 offset
+= i_shdrp
->sh_size
;
4620 /* Compute the file positions we are going to put the sections at, and
4621 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4622 is not NULL, this is being called by the ELF backend linker. */
4625 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4626 struct bfd_link_info
*link_info
)
4628 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4629 struct fake_section_arg fsargs
;
4631 struct elf_strtab_hash
*strtab
= NULL
;
4632 Elf_Internal_Shdr
*shstrtab_hdr
;
4635 if (abfd
->output_has_begun
)
4638 /* Do any elf backend specific processing first. */
4639 if (bed
->elf_backend_begin_write_processing
)
4640 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4642 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4645 fsargs
.failed
= false;
4646 fsargs
.link_info
= link_info
;
4647 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4651 if (!assign_section_numbers (abfd
, link_info
))
4654 /* The backend linker builds symbol table information itself. */
4655 need_symtab
= (link_info
== NULL
4656 && (bfd_get_symcount (abfd
) > 0
4657 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4661 /* Non-zero if doing a relocatable link. */
4662 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4664 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
, link_info
))
4669 if (link_info
== NULL
)
4671 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4673 goto err_free_strtab
;
4676 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4677 /* sh_name was set in init_file_header. */
4678 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4679 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4680 shstrtab_hdr
->sh_addr
= 0;
4681 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4682 shstrtab_hdr
->sh_entsize
= 0;
4683 shstrtab_hdr
->sh_link
= 0;
4684 shstrtab_hdr
->sh_info
= 0;
4685 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4686 shstrtab_hdr
->sh_addralign
= 1;
4688 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4689 goto err_free_strtab
;
4694 Elf_Internal_Shdr
*hdr
;
4696 off
= elf_next_file_pos (abfd
);
4698 hdr
= & elf_symtab_hdr (abfd
);
4699 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true, 0);
4701 if (elf_symtab_shndx_list (abfd
) != NULL
)
4703 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4704 if (hdr
->sh_size
!= 0)
4705 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true, 0);
4706 /* FIXME: What about other symtab_shndx sections in the list ? */
4709 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4710 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true, 0);
4712 elf_next_file_pos (abfd
) = off
;
4714 /* Now that we know where the .strtab section goes, write it
4716 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4717 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4718 goto err_free_strtab
;
4719 _bfd_elf_strtab_free (strtab
);
4722 abfd
->output_has_begun
= true;
4727 _bfd_elf_strtab_free (strtab
);
4731 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4732 function effectively returns whether --eh-frame-hdr is given on the
4733 command line. After size_dynamic_sections the result reflects
4734 whether .eh_frame_hdr will actually be output (sizing isn't done
4735 until ldemul_after_allocation). */
4738 elf_eh_frame_hdr (const struct bfd_link_info
*info
)
4740 if (info
!= NULL
&& is_elf_hash_table (info
->hash
))
4741 return elf_hash_table (info
)->eh_info
.hdr_sec
;
4745 /* Make an initial estimate of the size of the program header. If we
4746 get the number wrong here, we'll redo section placement. */
4748 static bfd_size_type
4749 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4753 const struct elf_backend_data
*bed
;
4755 /* Assume we will need exactly two PT_LOAD segments: one for text
4756 and one for data. */
4759 s
= bfd_get_section_by_name (abfd
, ".interp");
4760 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4762 /* If we have a loadable interpreter section, we need a
4763 PT_INTERP segment. In this case, assume we also need a
4764 PT_PHDR segment, although that may not be true for all
4769 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4771 /* We need a PT_DYNAMIC segment. */
4775 if (info
!= NULL
&& info
->relro
)
4777 /* We need a PT_GNU_RELRO segment. */
4781 if (elf_eh_frame_hdr (info
))
4783 /* We need a PT_GNU_EH_FRAME segment. */
4787 if (elf_stack_flags (abfd
))
4789 /* We need a PT_GNU_STACK segment. */
4793 if (elf_sframe (abfd
))
4795 /* We need a PT_GNU_SFRAME segment. */
4799 s
= bfd_get_section_by_name (abfd
,
4800 NOTE_GNU_PROPERTY_SECTION_NAME
);
4801 if (s
!= NULL
&& s
->size
!= 0)
4803 /* We need a PT_GNU_PROPERTY segment. */
4807 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4809 if ((s
->flags
& SEC_LOAD
) != 0
4810 && elf_section_type (s
) == SHT_NOTE
)
4812 unsigned int alignment_power
;
4813 /* We need a PT_NOTE segment. */
4815 /* Try to create just one PT_NOTE segment for all adjacent
4816 loadable SHT_NOTE sections. gABI requires that within a
4817 PT_NOTE segment (and also inside of each SHT_NOTE section)
4818 each note should have the same alignment. So we check
4819 whether the sections are correctly aligned. */
4820 alignment_power
= s
->alignment_power
;
4821 while (s
->next
!= NULL
4822 && s
->next
->alignment_power
== alignment_power
4823 && (s
->next
->flags
& SEC_LOAD
) != 0
4824 && elf_section_type (s
->next
) == SHT_NOTE
)
4829 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4831 if (s
->flags
& SEC_THREAD_LOCAL
)
4833 /* We need a PT_TLS segment. */
4839 bed
= get_elf_backend_data (abfd
);
4841 if ((abfd
->flags
& D_PAGED
) != 0
4842 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4844 /* Add a PT_GNU_MBIND segment for each mbind section. */
4845 bfd_vma commonpagesize
;
4846 unsigned int page_align_power
;
4849 commonpagesize
= info
->commonpagesize
;
4851 commonpagesize
= bed
->commonpagesize
;
4852 page_align_power
= bfd_log2 (commonpagesize
);
4853 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4854 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4856 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4859 /* xgettext:c-format */
4860 (_("%pB: GNU_MBIND section `%pA' has invalid "
4861 "sh_info field: %d"),
4862 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4865 /* Align mbind section to page size. */
4866 if (s
->alignment_power
< page_align_power
)
4867 s
->alignment_power
= page_align_power
;
4872 /* Let the backend count up any program headers it might need. */
4873 if (bed
->elf_backend_additional_program_headers
)
4877 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4883 return segs
* bed
->s
->sizeof_phdr
;
4886 /* Find the segment that contains the output_section of section. */
4889 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4891 struct elf_segment_map
*m
;
4892 Elf_Internal_Phdr
*p
;
4894 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4900 for (i
= m
->count
- 1; i
>= 0; i
--)
4901 if (m
->sections
[i
] == section
)
4908 /* Create a mapping from a set of sections to a program segment. */
4910 static struct elf_segment_map
*
4911 make_mapping (bfd
*abfd
,
4912 asection
**sections
,
4917 struct elf_segment_map
*m
;
4922 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4923 amt
+= (to
- from
) * sizeof (asection
*);
4924 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4928 m
->p_type
= PT_LOAD
;
4929 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4930 m
->sections
[i
- from
] = *hdrpp
;
4931 m
->count
= to
- from
;
4933 if (from
== 0 && phdr
)
4935 /* Include the headers in the first PT_LOAD segment. */
4936 m
->includes_filehdr
= 1;
4937 m
->includes_phdrs
= 1;
4943 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4946 struct elf_segment_map
*
4947 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4949 struct elf_segment_map
*m
;
4951 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4952 sizeof (struct elf_segment_map
));
4956 m
->p_type
= PT_DYNAMIC
;
4958 m
->sections
[0] = dynsec
;
4963 /* Possibly add or remove segments from the segment map. */
4966 elf_modify_segment_map (bfd
*abfd
,
4967 struct bfd_link_info
*info
,
4968 bool remove_empty_load
)
4970 struct elf_segment_map
**m
;
4971 const struct elf_backend_data
*bed
;
4973 /* The placement algorithm assumes that non allocated sections are
4974 not in PT_LOAD segments. We ensure this here by removing such
4975 sections from the segment map. We also remove excluded
4976 sections. Finally, any PT_LOAD segment without sections is
4978 m
= &elf_seg_map (abfd
);
4981 unsigned int i
, new_count
;
4983 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4985 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4986 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4987 || (*m
)->p_type
!= PT_LOAD
))
4989 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4993 (*m
)->count
= new_count
;
4995 if (remove_empty_load
4996 && (*m
)->p_type
== PT_LOAD
4998 && !(*m
)->includes_phdrs
)
5004 bed
= get_elf_backend_data (abfd
);
5005 if (bed
->elf_backend_modify_segment_map
!= NULL
)
5007 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
5014 #define IS_TBSS(s) \
5015 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5017 /* Set up a mapping from BFD sections to program segments. Update
5018 NEED_LAYOUT if the section layout is changed. */
5021 _bfd_elf_map_sections_to_segments (bfd
*abfd
,
5022 struct bfd_link_info
*info
,
5026 struct elf_segment_map
*m
;
5027 asection
**sections
= NULL
;
5028 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5031 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
5035 info
->user_phdrs
= !no_user_phdrs
;
5037 /* Size the relative relocations if DT_RELR is enabled. */
5038 if (info
->enable_dt_relr
5039 && need_layout
!= NULL
5040 && bed
->size_relative_relocs
5041 && !bed
->size_relative_relocs (info
, need_layout
))
5042 info
->callbacks
->fatal
5043 (_("%P: failed to size relative relocations\n"));
5046 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
5050 struct elf_segment_map
*mfirst
;
5051 struct elf_segment_map
**pm
;
5054 unsigned int hdr_index
;
5055 bfd_vma maxpagesize
;
5057 bool phdr_in_segment
;
5060 unsigned int tls_count
= 0;
5061 asection
*first_tls
= NULL
;
5062 asection
*first_mbind
= NULL
;
5063 asection
*dynsec
, *eh_frame_hdr
;
5066 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
5067 bfd_size_type phdr_size
; /* Octets/bytes. */
5068 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5070 /* Select the allocated sections, and sort them. */
5072 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
5073 sections
= (asection
**) bfd_malloc (amt
);
5074 if (sections
== NULL
)
5077 /* Calculate top address, avoiding undefined behaviour of shift
5078 left operator when shift count is equal to size of type
5080 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
5081 addr_mask
= (addr_mask
<< 1) + 1;
5084 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5086 if ((s
->flags
& SEC_ALLOC
) != 0)
5088 /* target_index is unused until bfd_elf_final_link
5089 starts output of section symbols. Use it to make
5091 s
->target_index
= i
;
5094 /* A wrapping section potentially clashes with header. */
5095 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
5096 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
5099 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
5102 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
5104 phdr_size
= elf_program_header_size (abfd
);
5105 if (phdr_size
== (bfd_size_type
) -1)
5106 phdr_size
= get_program_header_size (abfd
, info
);
5107 phdr_size
+= bed
->s
->sizeof_ehdr
;
5108 /* phdr_size is compared to LMA values which are in bytes. */
5111 maxpagesize
= info
->maxpagesize
;
5113 maxpagesize
= bed
->maxpagesize
;
5114 if (maxpagesize
== 0)
5116 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
5118 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
5119 >= (phdr_size
& (maxpagesize
- 1))))
5120 /* For compatibility with old scripts that may not be using
5121 SIZEOF_HEADERS, add headers when it looks like space has
5122 been left for them. */
5123 phdr_in_segment
= true;
5125 /* Build the mapping. */
5129 /* If we have a .interp section, then create a PT_PHDR segment for
5130 the program headers and a PT_INTERP segment for the .interp
5132 s
= bfd_get_section_by_name (abfd
, ".interp");
5133 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
5135 amt
= sizeof (struct elf_segment_map
);
5136 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5140 m
->p_type
= PT_PHDR
;
5142 m
->p_flags_valid
= 1;
5143 m
->includes_phdrs
= 1;
5144 phdr_in_segment
= true;
5148 amt
= sizeof (struct elf_segment_map
);
5149 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5153 m
->p_type
= PT_INTERP
;
5161 /* Look through the sections. We put sections in the same program
5162 segment when the start of the second section can be placed within
5163 a few bytes of the end of the first section. */
5169 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
5171 && (dynsec
->flags
& SEC_LOAD
) == 0)
5174 if ((abfd
->flags
& D_PAGED
) == 0)
5175 phdr_in_segment
= false;
5177 /* Deal with -Ttext or something similar such that the first section
5178 is not adjacent to the program headers. This is an
5179 approximation, since at this point we don't know exactly how many
5180 program headers we will need. */
5181 if (phdr_in_segment
&& count
> 0)
5183 bfd_vma phdr_lma
; /* Bytes. */
5184 bool separate_phdr
= false;
5186 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
5188 && info
->separate_code
5189 && (sections
[0]->flags
& SEC_CODE
) != 0)
5191 /* If data sections should be separate from code and
5192 thus not executable, and the first section is
5193 executable then put the file and program headers in
5194 their own PT_LOAD. */
5195 if (!info
->one_rosegment
)
5196 separate_phdr
= true;
5198 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
5199 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
5201 /* The file and program headers are currently on the
5202 same page as the first section. Put them on the
5203 previous page if we can. */
5204 if (phdr_lma
>= maxpagesize
)
5205 phdr_lma
-= maxpagesize
;
5207 separate_phdr
= false;
5210 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
5211 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
5212 /* If file and program headers would be placed at the end
5213 of memory then it's probably better to omit them. */
5214 phdr_in_segment
= false;
5215 else if (phdr_lma
< wrap_to
)
5216 /* If a section wraps around to where we'll be placing
5217 file and program headers, then the headers will be
5219 phdr_in_segment
= false;
5220 else if (separate_phdr
)
5222 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
5225 m
->p_paddr
= phdr_lma
* opb
;
5227 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
5228 m
->p_paddr_valid
= 1;
5231 phdr_in_segment
= false;
5235 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
5242 /* See if this section and the last one will fit in the same
5245 if (last_hdr
== NULL
)
5247 /* If we don't have a segment yet, then we don't need a new
5248 one (we build the last one after this loop). */
5249 new_segment
= false;
5251 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
5253 /* If this section has a different relation between the
5254 virtual address and the load address, then we need a new
5258 else if (hdr
->lma
< last_hdr
->lma
+ last_size
5259 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
5261 /* If this section has a load address that makes it overlap
5262 the previous section, then we need a new segment. */
5265 else if ((abfd
->flags
& D_PAGED
) != 0
5266 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
5267 == (hdr
->lma
& -maxpagesize
)))
5269 /* If we are demand paged then we can't map two disk
5270 pages onto the same memory page. */
5271 new_segment
= false;
5273 /* In the next test we have to be careful when last_hdr->lma is close
5274 to the end of the address space. If the aligned address wraps
5275 around to the start of the address space, then there are no more
5276 pages left in memory and it is OK to assume that the current
5277 section can be included in the current segment. */
5278 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
5279 + maxpagesize
> last_hdr
->lma
)
5280 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
5281 + maxpagesize
<= hdr
->lma
))
5283 /* If putting this section in this segment would force us to
5284 skip a page in the segment, then we need a new segment. */
5287 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
5288 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
5290 /* We don't want to put a loaded section after a
5291 nonloaded (ie. bss style) section in the same segment
5292 as that will force the non-loaded section to be loaded.
5293 Consider .tbss sections as loaded for this purpose. */
5296 else if ((abfd
->flags
& D_PAGED
) == 0)
5298 /* If the file is not demand paged, which means that we
5299 don't require the sections to be correctly aligned in the
5300 file, then there is no other reason for a new segment. */
5301 new_segment
= false;
5303 else if (info
!= NULL
5304 && info
->separate_code
5305 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
5310 && (hdr
->flags
& SEC_READONLY
) == 0)
5312 /* We don't want to put a writable section in a read only
5318 /* Otherwise, we can use the same segment. */
5319 new_segment
= false;
5322 /* Allow interested parties a chance to override our decision. */
5323 if (last_hdr
!= NULL
5325 && info
->callbacks
->override_segment_assignment
!= NULL
)
5327 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
5333 if ((hdr
->flags
& SEC_READONLY
) == 0)
5335 if ((hdr
->flags
& SEC_CODE
) != 0)
5338 /* .tbss sections effectively have zero size. */
5339 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5343 /* We need a new program segment. We must create a new program
5344 header holding all the sections from hdr_index until hdr. */
5346 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5353 if ((hdr
->flags
& SEC_READONLY
) == 0)
5358 if ((hdr
->flags
& SEC_CODE
) == 0)
5364 /* .tbss sections effectively have zero size. */
5365 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5367 phdr_in_segment
= false;
5370 /* Create a final PT_LOAD program segment, but not if it's just
5372 if (last_hdr
!= NULL
5373 && (i
- hdr_index
!= 1
5374 || !IS_TBSS (last_hdr
)))
5376 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5384 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5387 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5394 /* For each batch of consecutive loadable SHT_NOTE sections,
5395 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5396 because if we link together nonloadable .note sections and
5397 loadable .note sections, we will generate two .note sections
5398 in the output file. */
5399 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5401 if ((s
->flags
& SEC_LOAD
) != 0
5402 && elf_section_type (s
) == SHT_NOTE
)
5405 unsigned int alignment_power
= s
->alignment_power
;
5408 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5410 if (s2
->next
->alignment_power
== alignment_power
5411 && (s2
->next
->flags
& SEC_LOAD
) != 0
5412 && elf_section_type (s2
->next
) == SHT_NOTE
5413 && align_power (s2
->lma
+ s2
->size
/ opb
,
5420 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5421 amt
+= count
* sizeof (asection
*);
5422 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5426 m
->p_type
= PT_NOTE
;
5430 m
->sections
[m
->count
- count
--] = s
;
5431 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5434 m
->sections
[m
->count
- 1] = s
;
5435 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5439 if (s
->flags
& SEC_THREAD_LOCAL
)
5445 if (first_mbind
== NULL
5446 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5450 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5453 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5454 amt
+= tls_count
* sizeof (asection
*);
5455 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5460 m
->count
= tls_count
;
5461 /* Mandated PF_R. */
5463 m
->p_flags_valid
= 1;
5465 for (i
= 0; i
< tls_count
; ++i
)
5467 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5470 (_("%pB: TLS sections are not adjacent:"), abfd
);
5473 while (i
< tls_count
)
5475 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5477 _bfd_error_handler (_(" TLS: %pA"), s
);
5481 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5484 bfd_set_error (bfd_error_bad_value
);
5496 && (abfd
->flags
& D_PAGED
) != 0
5497 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5498 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5499 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5500 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5502 /* Mandated PF_R. */
5503 unsigned long p_flags
= PF_R
;
5504 if ((s
->flags
& SEC_READONLY
) == 0)
5506 if ((s
->flags
& SEC_CODE
) != 0)
5509 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5510 m
= bfd_zalloc (abfd
, amt
);
5514 m
->p_type
= (PT_GNU_MBIND_LO
5515 + elf_section_data (s
)->this_hdr
.sh_info
);
5517 m
->p_flags_valid
= 1;
5519 m
->p_flags
= p_flags
;
5525 s
= bfd_get_section_by_name (abfd
,
5526 NOTE_GNU_PROPERTY_SECTION_NAME
);
5527 if (s
!= NULL
&& s
->size
!= 0)
5529 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5530 m
= bfd_zalloc (abfd
, amt
);
5534 m
->p_type
= PT_GNU_PROPERTY
;
5536 m
->p_flags_valid
= 1;
5543 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5545 eh_frame_hdr
= elf_eh_frame_hdr (info
);
5546 if (eh_frame_hdr
!= NULL
5547 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5549 amt
= sizeof (struct elf_segment_map
);
5550 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5554 m
->p_type
= PT_GNU_EH_FRAME
;
5556 m
->sections
[0] = eh_frame_hdr
->output_section
;
5562 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5564 sframe
= elf_sframe (abfd
);
5566 && (sframe
->output_section
->flags
& SEC_LOAD
) != 0
5567 && sframe
->size
!= 0)
5569 amt
= sizeof (struct elf_segment_map
);
5570 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5574 m
->p_type
= PT_GNU_SFRAME
;
5576 m
->sections
[0] = sframe
->output_section
;
5582 if (elf_stack_flags (abfd
))
5584 amt
= sizeof (struct elf_segment_map
);
5585 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5589 m
->p_type
= PT_GNU_STACK
;
5590 m
->p_flags
= elf_stack_flags (abfd
);
5591 m
->p_align
= bed
->stack_align
;
5592 m
->p_flags_valid
= 1;
5593 m
->p_align_valid
= m
->p_align
!= 0;
5594 if (info
->stacksize
> 0)
5596 m
->p_size
= info
->stacksize
;
5597 m
->p_size_valid
= 1;
5604 if (info
!= NULL
&& info
->relro
)
5606 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5608 if (m
->p_type
== PT_LOAD
5610 && m
->sections
[0]->vma
>= info
->relro_start
5611 && m
->sections
[0]->vma
< info
->relro_end
)
5614 while (--i
!= (unsigned) -1)
5616 if (m
->sections
[i
]->size
> 0
5617 && (m
->sections
[i
]->flags
& SEC_LOAD
) != 0
5618 && (m
->sections
[i
]->flags
& SEC_HAS_CONTENTS
) != 0)
5622 if (i
!= (unsigned) -1)
5627 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5630 amt
= sizeof (struct elf_segment_map
);
5631 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5635 m
->p_type
= PT_GNU_RELRO
;
5642 elf_seg_map (abfd
) = mfirst
;
5645 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
|| info
== NULL
))
5648 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5650 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5659 /* Sort sections by address. */
5662 elf_sort_sections (const void *arg1
, const void *arg2
)
5664 const asection
*sec1
= *(const asection
**) arg1
;
5665 const asection
*sec2
= *(const asection
**) arg2
;
5666 bfd_size_type size1
, size2
;
5668 /* Sort by LMA first, since this is the address used to
5669 place the section into a segment. */
5670 if (sec1
->lma
< sec2
->lma
)
5672 else if (sec1
->lma
> sec2
->lma
)
5675 /* Then sort by VMA. Normally the LMA and the VMA will be
5676 the same, and this will do nothing. */
5677 if (sec1
->vma
< sec2
->vma
)
5679 else if (sec1
->vma
> sec2
->vma
)
5682 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5684 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5692 else if (TOEND (sec2
))
5697 /* Sort by size, to put zero sized sections
5698 before others at the same address. */
5700 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5701 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5708 return sec1
->target_index
- sec2
->target_index
;
5711 /* This qsort comparison functions sorts PT_LOAD segments first and
5712 by p_paddr, for assign_file_positions_for_load_sections. */
5715 elf_sort_segments (const void *arg1
, const void *arg2
)
5717 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5718 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5720 if (m1
->p_type
!= m2
->p_type
)
5722 if (m1
->p_type
== PT_NULL
)
5724 if (m2
->p_type
== PT_NULL
)
5726 return m1
->p_type
< m2
->p_type
? -1 : 1;
5728 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5729 return m1
->includes_filehdr
? -1 : 1;
5730 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5731 return m1
->no_sort_lma
? -1 : 1;
5732 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5734 bfd_vma lma1
, lma2
; /* Octets. */
5736 if (m1
->p_paddr_valid
)
5738 else if (m1
->count
!= 0)
5740 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5742 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5745 if (m2
->p_paddr_valid
)
5747 else if (m2
->count
!= 0)
5749 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5751 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5754 return lma1
< lma2
? -1 : 1;
5756 if (m1
->idx
!= m2
->idx
)
5757 return m1
->idx
< m2
->idx
? -1 : 1;
5761 /* Ian Lance Taylor writes:
5763 We shouldn't be using % with a negative signed number. That's just
5764 not good. We have to make sure either that the number is not
5765 negative, or that the number has an unsigned type. When the types
5766 are all the same size they wind up as unsigned. When file_ptr is a
5767 larger signed type, the arithmetic winds up as signed long long,
5770 What we're trying to say here is something like ``increase OFF by
5771 the least amount that will cause it to be equal to the VMA modulo
5773 /* In other words, something like:
5775 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5776 off_offset = off % bed->maxpagesize;
5777 if (vma_offset < off_offset)
5778 adjustment = vma_offset + bed->maxpagesize - off_offset;
5780 adjustment = vma_offset - off_offset;
5782 which can be collapsed into the expression below. */
5785 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5787 /* PR binutils/16199: Handle an alignment of zero. */
5788 if (maxpagesize
== 0)
5790 return ((vma
- off
) % maxpagesize
);
5794 print_segment_map (const struct elf_segment_map
*m
)
5797 const char *pt
= get_segment_type (m
->p_type
);
5802 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5803 sprintf (buf
, "LOPROC+%7.7x",
5804 (unsigned int) (m
->p_type
- PT_LOPROC
));
5805 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5806 sprintf (buf
, "LOOS+%7.7x",
5807 (unsigned int) (m
->p_type
- PT_LOOS
));
5809 snprintf (buf
, sizeof (buf
), "%8.8x",
5810 (unsigned int) m
->p_type
);
5814 fprintf (stderr
, "%s:", pt
);
5815 for (j
= 0; j
< m
->count
; j
++)
5816 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5821 /* Assign file positions to the sections based on the mapping from
5822 sections to segments. This function also sets up some fields in
5826 assign_file_positions_for_load_sections (bfd
*abfd
,
5827 struct bfd_link_info
*link_info
)
5829 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5830 struct elf_segment_map
*m
;
5831 struct elf_segment_map
*phdr_load_seg
;
5832 Elf_Internal_Phdr
*phdrs
;
5833 Elf_Internal_Phdr
*p
;
5834 file_ptr off
; /* Octets. */
5835 bfd_size_type maxpagesize
;
5836 unsigned int alloc
, actual
;
5838 struct elf_segment_map
**sorted_seg_map
;
5839 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5841 if (link_info
== NULL
5842 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
, NULL
))
5846 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5851 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5852 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5856 /* PR binutils/12467. */
5857 elf_elfheader (abfd
)->e_phoff
= 0;
5858 elf_elfheader (abfd
)->e_phentsize
= 0;
5861 elf_elfheader (abfd
)->e_phnum
= alloc
;
5863 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5866 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5870 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5871 BFD_ASSERT (elf_program_header_size (abfd
)
5872 == actual
* bed
->s
->sizeof_phdr
);
5873 BFD_ASSERT (actual
>= alloc
);
5878 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5882 /* We're writing the size in elf_program_header_size (abfd),
5883 see assign_file_positions_except_relocs, so make sure we have
5884 that amount allocated, with trailing space cleared.
5885 The variable alloc contains the computed need, while
5886 elf_program_header_size (abfd) contains the size used for the
5888 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5889 where the layout is forced to according to a larger size in the
5890 last iterations for the testcase ld-elf/header. */
5891 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5892 + alloc
* sizeof (*sorted_seg_map
)));
5893 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5894 elf_tdata (abfd
)->phdr
= phdrs
;
5898 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5900 sorted_seg_map
[j
] = m
;
5901 /* If elf_segment_map is not from map_sections_to_segments, the
5902 sections may not be correctly ordered. NOTE: sorting should
5903 not be done to the PT_NOTE section of a corefile, which may
5904 contain several pseudo-sections artificially created by bfd.
5905 Sorting these pseudo-sections breaks things badly. */
5907 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5908 && m
->p_type
== PT_NOTE
))
5910 for (i
= 0; i
< m
->count
; i
++)
5911 m
->sections
[i
]->target_index
= i
;
5912 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5917 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5921 if ((abfd
->flags
& D_PAGED
) != 0)
5923 if (link_info
!= NULL
)
5924 maxpagesize
= link_info
->maxpagesize
;
5926 maxpagesize
= bed
->maxpagesize
;
5929 /* Sections must map to file offsets past the ELF file header. */
5930 off
= bed
->s
->sizeof_ehdr
;
5931 /* And if one of the PT_LOAD headers doesn't include the program
5932 headers then we'll be mapping program headers in the usual
5933 position after the ELF file header. */
5934 phdr_load_seg
= NULL
;
5935 for (j
= 0; j
< alloc
; j
++)
5937 m
= sorted_seg_map
[j
];
5938 if (m
->p_type
!= PT_LOAD
)
5940 if (m
->includes_phdrs
)
5946 if (phdr_load_seg
== NULL
)
5947 off
+= actual
* bed
->s
->sizeof_phdr
;
5949 for (j
= 0; j
< alloc
; j
++)
5952 bfd_vma off_adjust
; /* Octets. */
5954 bfd_size_type align_pagesize
;
5956 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5957 number of sections with contents contributing to both p_filesz
5958 and p_memsz, followed by a number of sections with no contents
5959 that just contribute to p_memsz. In this loop, OFF tracks next
5960 available file offset for PT_LOAD and PT_NOTE segments. */
5961 m
= sorted_seg_map
[j
];
5963 p
->p_type
= m
->p_type
;
5964 p
->p_flags
= m
->p_flags
;
5967 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5969 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5971 if (m
->p_paddr_valid
)
5972 p
->p_paddr
= m
->p_paddr
;
5973 else if (m
->count
== 0)
5976 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5979 if (p
->p_type
== PT_LOAD
5980 && (abfd
->flags
& D_PAGED
) != 0)
5982 /* p_align in demand paged PT_LOAD segments effectively stores
5983 the maximum page size. When copying an executable with
5984 objcopy, we set m->p_align from the input file. Use this
5985 value for maxpagesize rather than bed->maxpagesize, which
5986 may be different. Note that we use maxpagesize for PT_TLS
5987 segment alignment later in this function, so we are relying
5988 on at least one PT_LOAD segment appearing before a PT_TLS
5990 if (m
->p_align_valid
)
5991 maxpagesize
= m
->p_align
;
5992 else if (bed
->p_align
!= 0
5993 && (link_info
== NULL
5994 || !link_info
->maxpagesize_is_set
))
5995 /* We will lay out this binary using maxpagesize but set
5996 p->p_align later to the possibly smaller bed->p_align.
5997 The run-time loader will then be able to load this
5998 binary when the system page size is maxpagesize, but if
5999 the system page size is smaller can use p->p_align.
6000 In either case p->p_align will be increased if
6001 necessary to match section alignment. */
6002 align_pagesize
= bed
->p_align
;
6004 p
->p_align
= maxpagesize
;
6006 else if (m
->p_align_valid
)
6007 p
->p_align
= m
->p_align
;
6008 else if (m
->count
== 0)
6009 p
->p_align
= 1 << bed
->s
->log_file_align
;
6011 if (m
== phdr_load_seg
)
6012 off
+= actual
* bed
->s
->sizeof_phdr
;
6014 no_contents
= false;
6016 if (p
->p_type
== PT_LOAD
6019 bfd_size_type align
; /* Bytes. */
6020 unsigned int align_power
= 0;
6022 if (m
->p_align_valid
)
6026 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
6028 unsigned int secalign
;
6030 secalign
= bfd_section_alignment (*secpp
);
6031 if (secalign
> align_power
)
6032 align_power
= secalign
;
6034 align
= (bfd_size_type
) 1 << align_power
;
6035 /* If a section requires alignment higher than the
6036 minimum p_align value, don't reduce a maxpagesize
6037 p->p_align set earlier in this function. */
6038 if (align
> bed
->p_align
)
6040 if (align
< maxpagesize
)
6041 align
= maxpagesize
;
6044 /* If a section requires alignment higher than the
6045 maximum page size, set p_align to the section
6047 if ((abfd
->flags
& D_PAGED
) != 0)
6052 for (i
= 0; i
< m
->count
; i
++)
6053 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
6054 /* If we aren't making room for this section, then
6055 it must be SHT_NOBITS regardless of what we've
6056 set via struct bfd_elf_special_section. */
6057 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
6059 /* Find out whether this segment contains any loadable
6062 for (i
= 0; i
< m
->count
; i
++)
6063 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
6065 no_contents
= false;
6069 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
6071 /* Broken hardware and/or kernel require that files do not
6072 map the same page with different permissions on some hppa
6075 && (abfd
->flags
& D_PAGED
) != 0
6076 && bed
->no_page_alias
6077 && (off
& (maxpagesize
- 1)) != 0
6078 && ((off
& -maxpagesize
)
6079 == ((off
+ off_adjust
) & -maxpagesize
)))
6080 off_adjust
+= maxpagesize
;
6084 /* We shouldn't need to align the segment on disk since
6085 the segment doesn't need file space, but the gABI
6086 arguably requires the alignment and glibc ld.so
6087 checks it. So to comply with the alignment
6088 requirement but not waste file space, we adjust
6089 p_offset for just this segment. (OFF_ADJUST is
6090 subtracted from OFF later.) This may put p_offset
6091 past the end of file, but that shouldn't matter. */
6096 /* Make sure the .dynamic section is the first section in the
6097 PT_DYNAMIC segment. */
6098 else if (p
->p_type
== PT_DYNAMIC
6100 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
6103 (_("%pB: The first section in the PT_DYNAMIC segment"
6104 " is not the .dynamic section"),
6106 bfd_set_error (bfd_error_bad_value
);
6109 /* Set the note section type to SHT_NOTE. */
6110 else if (p
->p_type
== PT_NOTE
)
6111 for (i
= 0; i
< m
->count
; i
++)
6112 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
6114 if (m
->includes_filehdr
)
6116 if (!m
->p_flags_valid
)
6118 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
6119 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
6120 if (p
->p_type
== PT_LOAD
)
6124 if (p
->p_vaddr
< (bfd_vma
) off
6125 || (!m
->p_paddr_valid
6126 && p
->p_paddr
< (bfd_vma
) off
))
6129 (_("%pB: not enough room for program headers,"
6130 " try linking with -N"),
6132 bfd_set_error (bfd_error_bad_value
);
6136 if (!m
->p_paddr_valid
)
6140 else if (sorted_seg_map
[0]->includes_filehdr
)
6142 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
6143 p
->p_vaddr
= filehdr
->p_vaddr
;
6144 if (!m
->p_paddr_valid
)
6145 p
->p_paddr
= filehdr
->p_paddr
;
6149 if (m
->includes_phdrs
)
6151 if (!m
->p_flags_valid
)
6153 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
6154 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
6155 if (!m
->includes_filehdr
)
6157 if (p
->p_type
== PT_LOAD
)
6159 p
->p_offset
= off
- actual
* bed
->s
->sizeof_phdr
;
6160 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
6163 p
->p_vaddr
-= off
- p
->p_offset
;
6164 if (!m
->p_paddr_valid
)
6165 p
->p_paddr
-= off
- p
->p_offset
;
6168 else if (phdr_load_seg
!= NULL
)
6170 /* Also set PT_PHDR to match phdr_load_seg. We've
6171 sorted segments so that phdr_load_seg will
6172 already be set by the code immediately above. */
6173 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
6174 bfd_vma phdr_off
= 0; /* Octets. */
6175 if (phdr_load_seg
->includes_filehdr
)
6176 phdr_off
= bed
->s
->sizeof_ehdr
;
6177 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
6178 if (!m
->p_paddr_valid
)
6179 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
6180 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
6183 p
->p_offset
= bed
->s
->sizeof_ehdr
;
6187 if (p
->p_type
== PT_LOAD
6188 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
6190 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
6195 /* Put meaningless p_offset for PT_LOAD segments
6196 without file contents somewhere within the first
6197 page, in an attempt to not point past EOF. */
6198 bfd_size_type align
= maxpagesize
;
6199 if (align
< p
->p_align
)
6203 /* Avoid p_offset of zero, which might be wrongly
6204 interpreted as the segment being the first one,
6205 containing the file header. PR32763. */
6206 p
->p_offset
= (off
+ align
- 1) % align
+ 1;
6211 file_ptr adjust
; /* Octets. */
6213 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
6215 p
->p_filesz
+= adjust
;
6216 p
->p_memsz
+= adjust
;
6221 p
->p_align
= align_pagesize
;
6223 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6224 maps. Set filepos for sections in PT_LOAD segments, and in
6225 core files, for sections in PT_NOTE segments.
6226 assign_file_positions_for_non_load_sections will set filepos
6227 for other sections and update p_filesz for other segments. */
6228 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
6231 bfd_size_type align
;
6232 Elf_Internal_Shdr
*this_hdr
;
6235 this_hdr
= &elf_section_data (sec
)->this_hdr
;
6236 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
6238 if ((p
->p_type
== PT_LOAD
6239 || p
->p_type
== PT_TLS
)
6240 && (this_hdr
->sh_type
!= SHT_NOBITS
6241 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
6242 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
6243 || p
->p_type
== PT_TLS
))))
6245 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
6246 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
6247 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
6248 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
6252 || p_end
< p_start
))
6255 /* xgettext:c-format */
6256 (_("%pB: section %pA lma %#" PRIx64
6257 " adjusted to %#" PRIx64
),
6258 abfd
, sec
, (uint64_t) s_start
/ opb
,
6259 (uint64_t) p_end
/ opb
);
6261 sec
->lma
= p_end
/ opb
;
6263 p
->p_memsz
+= adjust
;
6265 if (p
->p_type
== PT_LOAD
)
6267 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6270 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
6272 /* We have a PROGBITS section following NOBITS ones.
6273 Allocate file space for the NOBITS section(s).
6274 We don't need to write out the zeros, posix
6275 fseek past the end of data already written
6276 followed by a write at that location is
6277 guaranteed to result in zeros being read
6279 adjust
= p
->p_memsz
- p
->p_filesz
;
6282 /* We only adjust sh_offset in SHT_NOBITS sections
6283 as would seem proper for their address when the
6284 section is first in the segment. sh_offset
6285 doesn't really have any significance for
6286 SHT_NOBITS anyway, apart from a notional position
6287 relative to other sections. Historically we
6288 didn't bother with adjusting sh_offset and some
6289 programs depend on it not being adjusted. See
6290 pr12921 and pr25662. */
6291 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
6294 if (this_hdr
->sh_type
== SHT_NOBITS
)
6295 off_adjust
+= adjust
;
6298 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6299 p
->p_filesz
+= adjust
;
6302 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
6304 /* The section at i == 0 is the one that actually contains
6308 this_hdr
->sh_offset
= sec
->filepos
= off
;
6309 off
+= this_hdr
->sh_size
;
6310 p
->p_filesz
= this_hdr
->sh_size
;
6316 /* The rest are fake sections that shouldn't be written. */
6325 if (this_hdr
->sh_type
== SHT_NOBITS
6326 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
6327 && this_hdr
->sh_offset
== 0)
6329 /* Set sh_offset for .tbss sections to their nominal
6330 offset after aligning. They are not loaded from
6331 disk so the value doesn't really matter, except
6332 when the .tbss section is the first one in a
6333 PT_TLS segment. In that case it sets the
6334 p_offset for the PT_TLS segment, which according
6335 to the ELF gABI ought to satisfy
6336 p_offset % p_align == p_vaddr % p_align. */
6337 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
6339 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
6341 else if (p
->p_type
== PT_LOAD
)
6343 this_hdr
->sh_offset
= sec
->filepos
= off
;
6344 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6345 off
+= this_hdr
->sh_size
;
6348 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6350 p
->p_filesz
+= this_hdr
->sh_size
;
6351 /* A load section without SHF_ALLOC is something like
6352 a note section in a PT_NOTE segment. These take
6353 file space but are not loaded into memory. */
6354 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6355 p
->p_memsz
+= this_hdr
->sh_size
;
6357 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6359 if (p
->p_type
== PT_TLS
)
6360 p
->p_memsz
+= this_hdr
->sh_size
;
6362 /* .tbss is special. It doesn't contribute to p_memsz of
6364 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
6365 p
->p_memsz
+= this_hdr
->sh_size
;
6368 if (align
> p
->p_align
6369 && !m
->p_align_valid
6370 && (p
->p_type
!= PT_LOAD
6371 || (abfd
->flags
& D_PAGED
) == 0))
6375 if (!m
->p_flags_valid
)
6378 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
6380 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
6387 /* PR ld/20815 - Check that the program header segment, if
6388 present, will be loaded into memory. */
6389 if (p
->p_type
== PT_PHDR
6390 && phdr_load_seg
== NULL
6391 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
6392 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
6394 /* The fix for this error is usually to edit the linker script being
6395 used and set up the program headers manually. Either that or
6396 leave room for the headers at the start of the SECTIONS. */
6397 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6398 " by LOAD segment"),
6400 if (link_info
== NULL
)
6402 /* Arrange for the linker to exit with an error, deleting
6403 the output file unless --noinhibit-exec is given. */
6404 link_info
->callbacks
->info ("%X");
6407 /* Check that all sections are in a PT_LOAD segment.
6408 Don't check funky gdb generated core files. */
6409 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
6411 bool check_vma
= true;
6413 for (i
= 1; i
< m
->count
; i
++)
6414 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
6415 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
6416 ->this_hdr
), p
) != 0
6417 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
6418 ->this_hdr
), p
) != 0)
6420 /* Looks like we have overlays packed into the segment. */
6425 for (i
= 0; i
< m
->count
; i
++)
6427 Elf_Internal_Shdr
*this_hdr
;
6430 sec
= m
->sections
[i
];
6431 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6432 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6433 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6436 /* xgettext:c-format */
6437 (_("%pB: section `%pA' can't be allocated in segment %u"),
6439 print_segment_map (m
);
6445 elf_next_file_pos (abfd
) = off
;
6447 if (link_info
!= NULL
6448 && phdr_load_seg
!= NULL
6449 && phdr_load_seg
->includes_filehdr
)
6451 /* There is a segment that contains both the file headers and the
6452 program headers, so provide a symbol __ehdr_start pointing there.
6453 A program can use this to examine itself robustly. */
6455 struct elf_link_hash_table
*htab
= elf_hash_table (link_info
);
6456 struct elf_link_hash_entry
*hash
= htab
->hehdr_start
;
6458 /* If the symbol was referenced and not defined, define it. */
6460 && (hash
->root
.type
== bfd_link_hash_new
6461 || hash
->root
.type
== bfd_link_hash_undefined
6462 || hash
->root
.type
== bfd_link_hash_undefweak
6463 || hash
->root
.type
== bfd_link_hash_common
))
6466 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6468 if (phdr_load_seg
->count
!= 0)
6469 /* The segment contains sections, so use the first one. */
6470 s
= phdr_load_seg
->sections
[0];
6472 /* Use the first (i.e. lowest-addressed) section in any segment. */
6473 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6474 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6482 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6483 hash
->root
.u
.def
.section
= s
;
6487 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6488 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6491 hash
->root
.type
= bfd_link_hash_defined
;
6492 hash
->def_regular
= 1;
6500 /* Determine if a bfd is a debuginfo file. Unfortunately there
6501 is no defined method for detecting such files, so we have to
6502 use heuristics instead. */
6505 is_debuginfo_file (bfd
*abfd
)
6507 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6510 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6511 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6512 Elf_Internal_Shdr
**headerp
;
6514 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6516 Elf_Internal_Shdr
*header
= * headerp
;
6518 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6519 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6520 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6521 && header
->sh_type
!= SHT_NOBITS
6522 && header
->sh_type
!= SHT_NOTE
)
6529 /* Assign file positions for other sections, except for compressed debug
6530 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6533 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6534 struct bfd_link_info
*link_info
)
6536 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6537 Elf_Internal_Shdr
**i_shdrpp
;
6538 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6539 Elf_Internal_Phdr
*phdrs
;
6540 Elf_Internal_Phdr
*p
;
6541 struct elf_segment_map
*m
;
6543 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6544 bfd_vma maxpagesize
;
6546 if (link_info
!= NULL
)
6547 maxpagesize
= link_info
->maxpagesize
;
6549 maxpagesize
= bed
->maxpagesize
;
6550 i_shdrpp
= elf_elfsections (abfd
);
6551 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6552 off
= elf_next_file_pos (abfd
);
6553 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6555 Elf_Internal_Shdr
*hdr
;
6559 if (hdr
->bfd_section
!= NULL
6560 && (hdr
->bfd_section
->filepos
!= 0
6561 || (hdr
->sh_type
== SHT_NOBITS
6562 && hdr
->contents
== NULL
)))
6563 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6564 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6566 if (hdr
->sh_size
!= 0
6567 /* PR 24717 - debuginfo files are known to be not strictly
6568 compliant with the ELF standard. In particular they often
6569 have .note.gnu.property sections that are outside of any
6570 loadable segment. This is not a problem for such files,
6571 so do not warn about them. */
6572 && ! is_debuginfo_file (abfd
))
6574 /* xgettext:c-format */
6575 (_("%pB: warning: allocated section `%s' not in segment"),
6577 (hdr
->bfd_section
== NULL
6579 : hdr
->bfd_section
->name
));
6580 /* We don't need to page align empty sections. */
6581 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6582 align
= maxpagesize
;
6584 align
= hdr
->sh_addralign
& -hdr
->sh_addralign
;
6585 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
, align
);
6586 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, false,
6587 bed
->s
->log_file_align
);
6589 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6590 && hdr
->bfd_section
== NULL
)
6591 /* We don't know the offset of these sections yet:
6592 their size has not been decided. */
6593 || (abfd
->is_linker_output
6594 && hdr
->bfd_section
!= NULL
6595 && (hdr
->sh_name
== -1u
6596 || bfd_section_is_ctf (hdr
->bfd_section
)))
6597 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6598 || (elf_symtab_shndx_list (abfd
) != NULL
6599 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6600 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6601 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6602 hdr
->sh_offset
= -1;
6604 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true, 0);
6606 elf_next_file_pos (abfd
) = off
;
6608 /* Now that we have set the section file positions, we can set up
6609 the file positions for the non PT_LOAD segments. */
6610 phdrs
= elf_tdata (abfd
)->phdr
;
6611 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6613 if (p
->p_type
== PT_GNU_RELRO
)
6615 bfd_vma start
, end
; /* Bytes. */
6618 if (link_info
!= NULL
)
6620 /* During linking the range of the RELRO segment is passed
6621 in link_info. Note that there may be padding between
6622 relro_start and the first RELRO section. */
6623 start
= link_info
->relro_start
;
6624 end
= link_info
->relro_end
;
6626 else if (m
->count
!= 0)
6628 if (!m
->p_size_valid
)
6630 start
= m
->sections
[0]->vma
;
6631 end
= start
+ m
->p_size
/ opb
;
6642 struct elf_segment_map
*lm
;
6643 const Elf_Internal_Phdr
*lp
;
6646 /* Find a LOAD segment containing a section in the RELRO
6648 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6650 lm
= lm
->next
, lp
++)
6652 if (lp
->p_type
== PT_LOAD
6654 && (lm
->sections
[lm
->count
- 1]->vma
6655 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6656 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6658 && lm
->sections
[0]->vma
< end
)
6664 /* Find the section starting the RELRO segment. */
6665 for (i
= 0; i
< lm
->count
; i
++)
6667 asection
*s
= lm
->sections
[i
];
6676 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6677 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6678 p
->p_offset
= lm
->sections
[i
]->filepos
;
6679 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6680 p
->p_filesz
= p
->p_memsz
;
6682 /* The RELRO segment typically ends a few bytes
6683 into .got.plt but other layouts are possible.
6684 In cases where the end does not match any
6685 loaded section (for instance is in file
6686 padding), trim p_filesz back to correspond to
6687 the end of loaded section contents. */
6688 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6689 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6691 /* Preserve the alignment and flags if they are
6692 valid. The gold linker generates RW/4 for
6693 the PT_GNU_RELRO section. It is better for
6694 objcopy/strip to honor these attributes
6695 otherwise gdb will choke when using separate
6697 if (!m
->p_align_valid
)
6699 if (!m
->p_flags_valid
)
6708 if (link_info
!= NULL
)
6710 (_("%pB: warning: unable to allocate any sections"
6711 " to PT_GNU_RELRO segment"),
6713 memset (p
, 0, sizeof *p
);
6716 else if (p
->p_type
== PT_GNU_STACK
)
6718 if (m
->p_size_valid
)
6719 p
->p_memsz
= m
->p_size
;
6721 else if (m
->count
!= 0)
6725 if (p
->p_type
!= PT_LOAD
6726 && (p
->p_type
!= PT_NOTE
6727 || bfd_get_format (abfd
) != bfd_core
))
6729 /* A user specified segment layout may include a PHDR
6730 segment that overlaps with a LOAD segment... */
6731 if (p
->p_type
== PT_PHDR
)
6737 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6739 /* PR 17512: file: 2195325e. */
6741 (_("%pB: error: non-load segment %d includes file header "
6742 "and/or program header"),
6743 abfd
, (int) (p
- phdrs
));
6748 p
->p_offset
= m
->sections
[0]->filepos
;
6749 for (i
= m
->count
; i
-- != 0;)
6751 asection
*sect
= m
->sections
[i
];
6752 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6753 if (hdr
->sh_type
!= SHT_NOBITS
)
6755 p
->p_filesz
= sect
->filepos
- p
->p_offset
+ hdr
->sh_size
;
6756 /* NB: p_memsz of the loadable PT_NOTE segment
6757 should be the same as p_filesz. */
6758 if (p
->p_type
== PT_NOTE
6759 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
6760 p
->p_memsz
= p
->p_filesz
;
6771 static elf_section_list
*
6772 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6774 for (;list
!= NULL
; list
= list
->next
)
6780 /* Work out the file positions of all the sections. This is called by
6781 _bfd_elf_compute_section_file_positions. All the section sizes and
6782 VMAs must be known before this is called.
6784 Reloc sections come in two flavours: Those processed specially as
6785 "side-channel" data attached to a section to which they apply, and
6786 those that bfd doesn't process as relocations. The latter sort are
6787 stored in a normal bfd section by bfd_section_from_shdr. We don't
6788 consider the former sort here, unless they form part of the loadable
6789 image. Reloc sections not assigned here (and compressed debugging
6790 sections and CTF sections which nothing else in the file can rely
6791 upon) will be handled later by assign_file_positions_for_relocs.
6793 We also don't set the positions of the .symtab and .strtab here. */
6796 assign_file_positions_except_relocs (bfd
*abfd
,
6797 struct bfd_link_info
*link_info
)
6799 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6800 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6801 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6804 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6805 && bfd_get_format (abfd
) != bfd_core
)
6807 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6808 unsigned int num_sec
= elf_numsections (abfd
);
6809 Elf_Internal_Shdr
**hdrpp
;
6813 /* Start after the ELF header. */
6814 off
= i_ehdrp
->e_ehsize
;
6816 /* We are not creating an executable, which means that we are
6817 not creating a program header, and that the actual order of
6818 the sections in the file is unimportant. */
6819 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6821 Elf_Internal_Shdr
*hdr
;
6824 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6825 && hdr
->bfd_section
== NULL
)
6826 /* Do not assign offsets for these sections yet: we don't know
6828 || (abfd
->is_linker_output
6829 && hdr
->bfd_section
!= NULL
6830 && (hdr
->sh_name
== -1u
6831 || bfd_section_is_ctf (hdr
->bfd_section
)))
6832 || i
== elf_onesymtab (abfd
)
6833 || (elf_symtab_shndx_list (abfd
) != NULL
6834 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6835 || i
== elf_strtab_sec (abfd
)
6836 || i
== elf_shstrtab_sec (abfd
))
6838 hdr
->sh_offset
= -1;
6841 /* There shouldn't be a need to effect "capped" file alignment here,
6842 yet at least the Linux kernel's modpost utility was found to be
6843 unhappy without. While the issue was addressed there, let's be
6844 kind for at least the foreseeable future ... */
6845 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, false,
6846 bed
->s
->log_file_align
);
6849 elf_next_file_pos (abfd
) = off
;
6850 elf_program_header_size (abfd
) = 0;
6854 /* Assign file positions for the loaded sections based on the
6855 assignment of sections to segments. */
6856 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6859 /* And for non-load sections. */
6860 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6864 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6867 /* Write out the program headers. */
6868 alloc
= i_ehdrp
->e_phnum
;
6871 if (link_info
!= NULL
&& ! link_info
->no_warn_rwx_segments
)
6873 bool warned_tls
= false;
6874 bool warned_rwx
= false;
6876 /* Memory resident segments with non-zero size and RWX
6877 permissions are a security risk, so we generate a warning
6878 here if we are creating any. */
6881 for (i
= 0; i
< alloc
; i
++)
6883 const Elf_Internal_Phdr
* phdr
= tdata
->phdr
+ i
;
6885 if (phdr
->p_memsz
== 0)
6889 && phdr
->p_type
== PT_TLS
6890 && (phdr
->p_flags
& PF_X
))
6892 if (link_info
->warn_is_error_for_rwx_segments
)
6894 _bfd_error_handler (_("\
6895 error: %pB has a TLS segment with execute permission"),
6900 _bfd_error_handler (_("\
6901 warning: %pB has a TLS segment with execute permission"),
6908 else if (! warned_rwx
6909 && phdr
->p_type
== PT_LOAD
6910 && ((phdr
->p_flags
& (PF_R
| PF_W
| PF_X
))
6911 == (PF_R
| PF_W
| PF_X
)))
6913 if (link_info
->warn_is_error_for_rwx_segments
)
6915 _bfd_error_handler (_("\
6916 error: %pB has a LOAD segment with RWX permissions"),
6921 _bfd_error_handler (_("\
6922 warning: %pB has a LOAD segment with RWX permissions"),
6932 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6933 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6941 _bfd_elf_init_file_header (bfd
*abfd
,
6942 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6944 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6945 struct elf_strtab_hash
*shstrtab
;
6946 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6948 i_ehdrp
= elf_elfheader (abfd
);
6950 shstrtab
= _bfd_elf_strtab_init ();
6951 if (shstrtab
== NULL
)
6954 elf_shstrtab (abfd
) = shstrtab
;
6956 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6957 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6958 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6959 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6961 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6962 i_ehdrp
->e_ident
[EI_DATA
] =
6963 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6964 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6966 if ((abfd
->flags
& DYNAMIC
) != 0)
6967 i_ehdrp
->e_type
= ET_DYN
;
6968 else if ((abfd
->flags
& EXEC_P
) != 0)
6969 i_ehdrp
->e_type
= ET_EXEC
;
6970 else if (bfd_get_format (abfd
) == bfd_core
)
6971 i_ehdrp
->e_type
= ET_CORE
;
6973 i_ehdrp
->e_type
= ET_REL
;
6975 switch (bfd_get_arch (abfd
))
6977 case bfd_arch_unknown
:
6978 i_ehdrp
->e_machine
= EM_NONE
;
6981 /* There used to be a long list of cases here, each one setting
6982 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6983 in the corresponding bfd definition. To avoid duplication,
6984 the switch was removed. Machines that need special handling
6985 can generally do it in elf_backend_final_write_processing(),
6986 unless they need the information earlier than the final write.
6987 Such need can generally be supplied by replacing the tests for
6988 e_machine with the conditions used to determine it. */
6990 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6993 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6994 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6996 /* No program header, for now. */
6997 i_ehdrp
->e_phoff
= 0;
6998 i_ehdrp
->e_phentsize
= 0;
6999 i_ehdrp
->e_phnum
= 0;
7001 /* Each bfd section is section header entry. */
7002 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
7003 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
7005 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
7006 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
7007 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
7008 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
7009 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
7010 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
7011 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
7012 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
7013 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
7019 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7021 FIXME: We used to have code here to sort the PT_LOAD segments into
7022 ascending order, as per the ELF spec. But this breaks some programs,
7023 including the Linux kernel. But really either the spec should be
7024 changed or the programs updated. */
7027 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
7029 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
7031 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
7032 unsigned int num_segments
= i_ehdrp
->e_phnum
;
7033 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
7034 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
7035 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
7037 /* Find the lowest p_vaddr in PT_LOAD segments. */
7038 bfd_vma p_vaddr
= (bfd_vma
) -1;
7039 for (; segment
< end_segment
; segment
++)
7040 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
7041 p_vaddr
= segment
->p_vaddr
;
7043 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7044 segments is non-zero. */
7046 i_ehdrp
->e_type
= ET_EXEC
;
7051 /* Assign file positions for all the reloc sections which are not part
7052 of the loadable file image, and the file position of section headers. */
7055 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
7058 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
7059 Elf_Internal_Shdr
*shdrp
;
7060 Elf_Internal_Ehdr
*i_ehdrp
;
7061 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7063 /* Skip non-load sections without section header. */
7064 if ((abfd
->flags
& BFD_NO_SECTION_HEADER
) != 0)
7067 off
= elf_next_file_pos (abfd
);
7069 shdrpp
= elf_elfsections (abfd
);
7070 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
7071 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
7074 if (shdrp
->sh_offset
== -1)
7076 asection
*sec
= shdrp
->bfd_section
;
7078 || shdrp
->sh_type
== SHT_REL
7079 || shdrp
->sh_type
== SHT_RELA
)
7081 else if (bfd_section_is_ctf (sec
))
7083 /* Update section size and contents. */
7084 shdrp
->sh_size
= sec
->size
;
7085 shdrp
->contents
= sec
->contents
;
7087 else if (shdrp
->sh_name
== -1u)
7089 const char *name
= sec
->name
;
7090 struct bfd_elf_section_data
*d
;
7092 /* Compress DWARF debug sections. */
7093 if (!bfd_compress_section (abfd
, sec
, shdrp
->contents
))
7096 if (sec
->compress_status
== COMPRESS_SECTION_DONE
7097 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0
7100 /* If section is compressed with zlib-gnu, convert
7101 section name from .debug_* to .zdebug_*. */
7102 char *new_name
= bfd_debug_name_to_zdebug (abfd
, name
);
7103 if (new_name
== NULL
)
7107 /* Add section name to section name section. */
7109 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
7111 d
= elf_section_data (sec
);
7113 /* Add reloc section name to section name section. */
7115 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rel
.hdr
,
7119 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rela
.hdr
,
7123 /* Update section size and contents. */
7124 shdrp
->sh_size
= sec
->size
;
7125 shdrp
->contents
= sec
->contents
;
7126 sec
->contents
= NULL
;
7129 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
,
7130 (abfd
->flags
& (EXEC_P
| DYNAMIC
))
7131 || bfd_get_format (abfd
) == bfd_core
,
7132 bed
->s
->log_file_align
);
7136 /* Place section name section after DWARF debug sections have been
7138 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
7139 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
7140 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
7141 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true, 0);
7143 /* Place the section headers. */
7144 i_ehdrp
= elf_elfheader (abfd
);
7145 off
= BFD_ALIGN (off
, 1u << bed
->s
->log_file_align
);
7146 i_ehdrp
->e_shoff
= off
;
7147 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
7148 elf_next_file_pos (abfd
) = off
;
7154 _bfd_elf_write_object_contents (bfd
*abfd
)
7156 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7157 Elf_Internal_Shdr
**i_shdrp
;
7159 unsigned int count
, num_sec
;
7160 struct elf_obj_tdata
*t
;
7162 if (! abfd
->output_has_begun
7163 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7165 /* Do not rewrite ELF data when the BFD has been opened for update.
7166 abfd->output_has_begun was set to TRUE on opening, so creation of
7167 new sections, and modification of existing section sizes was
7168 restricted. This means the ELF header, program headers and
7169 section headers can't have changed. If the contents of any
7170 sections has been modified, then those changes have already been
7171 written to the BFD. */
7172 else if (abfd
->direction
== both_direction
)
7174 BFD_ASSERT (abfd
->output_has_begun
);
7178 i_shdrp
= elf_elfsections (abfd
);
7181 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
7185 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
7188 /* After writing the headers, we need to write the sections too... */
7189 num_sec
= elf_numsections (abfd
);
7190 for (count
= 1; count
< num_sec
; count
++)
7192 /* Don't set the sh_name field without section header. */
7193 if ((abfd
->flags
& BFD_NO_SECTION_HEADER
) == 0)
7194 i_shdrp
[count
]->sh_name
7195 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
7196 i_shdrp
[count
]->sh_name
);
7197 if (bed
->elf_backend_section_processing
)
7198 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
7200 if (i_shdrp
[count
]->contents
)
7202 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
7204 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
7205 || bfd_write (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
7210 /* Write out the section header names. */
7211 t
= elf_tdata (abfd
);
7212 if (elf_shstrtab (abfd
) != NULL
7213 && t
->shstrtab_hdr
.sh_offset
!= -1
7214 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
7215 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
7218 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
7221 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
7224 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7225 if (t
->o
->build_id
.after_write_object_contents
!= NULL
7226 && !(*t
->o
->build_id
.after_write_object_contents
) (abfd
))
7228 if (t
->o
->package_metadata
.after_write_object_contents
!= NULL
7229 && !(*t
->o
->package_metadata
.after_write_object_contents
) (abfd
))
7236 _bfd_elf_write_corefile_contents (bfd
*abfd
)
7238 /* Hopefully this can be done just like an object file. */
7239 return _bfd_elf_write_object_contents (abfd
);
7242 /* Given a section, search the header to find them. */
7245 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
7247 const struct elf_backend_data
*bed
;
7248 unsigned int sec_index
;
7250 if (elf_section_data (asect
) != NULL
7251 && elf_section_data (asect
)->this_idx
!= 0)
7252 return elf_section_data (asect
)->this_idx
;
7254 if (bfd_is_abs_section (asect
))
7255 sec_index
= SHN_ABS
;
7256 else if (bfd_is_com_section (asect
))
7257 sec_index
= SHN_COMMON
;
7258 else if (bfd_is_und_section (asect
))
7259 sec_index
= SHN_UNDEF
;
7261 sec_index
= SHN_BAD
;
7263 bed
= get_elf_backend_data (abfd
);
7264 if (bed
->elf_backend_section_from_bfd_section
)
7266 int retval
= sec_index
;
7268 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
7272 if (sec_index
== SHN_BAD
)
7273 bfd_set_error (bfd_error_nonrepresentable_section
);
7278 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7282 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
7284 asymbol
*asym_ptr
= *asym_ptr_ptr
;
7286 flagword flags
= asym_ptr
->flags
;
7288 /* When gas creates relocations against local labels, it creates its
7289 own symbol for the section, but does put the symbol into the
7290 symbol chain, so udata is 0. When the linker is generating
7291 relocatable output, this section symbol may be for one of the
7292 input sections rather than the output section. */
7293 if (asym_ptr
->udata
.i
== 0
7294 && (flags
& BSF_SECTION_SYM
)
7295 && asym_ptr
->section
)
7299 sec
= asym_ptr
->section
;
7300 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
7301 sec
= sec
->output_section
;
7302 if (sec
->owner
== abfd
7303 && sec
->index
< elf_num_section_syms (abfd
)
7304 && elf_section_syms (abfd
)[sec
->index
] != NULL
)
7305 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
7308 idx
= asym_ptr
->udata
.i
;
7312 /* This case can occur when using --strip-symbol on a symbol
7313 which is used in a relocation entry. */
7315 /* xgettext:c-format */
7316 (_("%pB: symbol `%s' required but not present"),
7317 abfd
, bfd_asymbol_name (asym_ptr
));
7318 bfd_set_error (bfd_error_no_symbols
);
7325 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7326 " flags = 0x%.8x\n",
7327 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
7335 static inline bfd_vma
7336 segment_size (Elf_Internal_Phdr
*segment
)
7338 return (segment
->p_memsz
> segment
->p_filesz
7339 ? segment
->p_memsz
: segment
->p_filesz
);
7343 /* Returns the end address of the segment + 1. */
7344 static inline bfd_vma
7345 segment_end (Elf_Internal_Phdr
*segment
, bfd_vma start
)
7347 return start
+ segment_size (segment
);
7350 static inline bfd_size_type
7351 section_size (asection
*section
, Elf_Internal_Phdr
*segment
)
7353 if ((section
->flags
& SEC_HAS_CONTENTS
) != 0
7354 || (section
->flags
& SEC_THREAD_LOCAL
) == 0
7355 || segment
->p_type
== PT_TLS
)
7356 return section
->size
;
7360 /* Returns TRUE if the given section is contained within the given
7361 segment. LMA addresses are compared against PADDR when
7362 USE_VADDR is false, VMA against VADDR when true. */
7364 is_contained_by (asection
*section
, Elf_Internal_Phdr
*segment
,
7365 bfd_vma paddr
, bfd_vma vaddr
, unsigned int opb
,
7368 bfd_vma seg_addr
= !use_vaddr
? paddr
: vaddr
;
7369 bfd_vma addr
= !use_vaddr
? section
->lma
: section
->vma
;
7371 if (_bfd_mul_overflow (addr
, opb
, &octet
))
7373 /* The third and fourth lines below are testing that the section end
7374 address is within the segment. It's written this way to avoid
7375 overflow. Add seg_addr + section_size to both sides of the
7376 inequality to make it obvious. */
7377 return (octet
>= seg_addr
7378 && segment_size (segment
) >= section_size (section
, segment
)
7379 && (octet
- seg_addr
7380 <= segment_size (segment
) - section_size (section
, segment
)));
7383 /* Handle PT_NOTE segment. */
7385 is_note (asection
*s
, Elf_Internal_Phdr
*p
)
7387 return (p
->p_type
== PT_NOTE
7388 && elf_section_type (s
) == SHT_NOTE
7389 && (ufile_ptr
) s
->filepos
>= p
->p_offset
7390 && p
->p_filesz
>= s
->size
7391 && (ufile_ptr
) s
->filepos
- p
->p_offset
<= p
->p_filesz
- s
->size
);
7394 /* Rewrite program header information. */
7397 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
, bfd_vma maxpagesize
)
7399 Elf_Internal_Ehdr
*iehdr
;
7400 struct elf_segment_map
*map
;
7401 struct elf_segment_map
*map_first
;
7402 struct elf_segment_map
**pointer_to_map
;
7403 Elf_Internal_Phdr
*segment
;
7406 unsigned int num_segments
;
7407 bool phdr_included
= false;
7409 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
7410 unsigned int phdr_adjust_num
= 0;
7411 const struct elf_backend_data
*bed
;
7412 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7414 bed
= get_elf_backend_data (ibfd
);
7415 iehdr
= elf_elfheader (ibfd
);
7418 pointer_to_map
= &map_first
;
7420 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7422 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7423 linker, which generates a PT_INTERP section with p_vaddr and
7424 p_memsz set to 0. */
7425 #define IS_SOLARIS_PT_INTERP(p, s) \
7427 && p->p_paddr == 0 \
7428 && p->p_memsz == 0 \
7429 && p->p_filesz > 0 \
7430 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7432 && (bfd_vma) s->filepos >= p->p_offset \
7433 && ((bfd_vma) s->filepos + s->size \
7434 <= p->p_offset + p->p_filesz))
7436 /* Decide if the given section should be included in the given segment.
7437 A section will be included if:
7438 1. It is within the address space of the segment -- we use the LMA
7439 if that is set for the segment and the VMA otherwise,
7440 2. It is an allocated section or a NOTE section in a PT_NOTE
7442 3. There is an output section associated with it,
7443 4. The section has not already been allocated to a previous segment.
7444 5. PT_GNU_STACK segments do not include any sections.
7445 6. PT_TLS segment includes only SHF_TLS sections.
7446 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7447 8. PT_DYNAMIC should not contain empty sections at the beginning
7448 (with the possible exception of .dynamic). */
7449 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7450 (((is_contained_by (section, segment, segment->p_paddr, \
7451 segment->p_vaddr, opb, !paddr_valid) \
7452 && (section->flags & SEC_ALLOC) != 0) \
7453 || is_note (section, segment)) \
7454 && segment->p_type != PT_GNU_STACK \
7455 && (segment->p_type != PT_TLS \
7456 || (section->flags & SEC_THREAD_LOCAL)) \
7457 && (segment->p_type == PT_LOAD \
7458 || segment->p_type == PT_TLS \
7459 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7460 && (segment->p_type != PT_DYNAMIC \
7461 || section_size (section, segment) > 0 \
7462 || (segment->p_paddr \
7463 ? segment->p_paddr != section->lma * (opb) \
7464 : segment->p_vaddr != section->vma * (opb)) \
7465 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7466 && (segment->p_type != PT_LOAD || !section->segment_mark))
7468 /* If the output section of a section in the input segment is NULL,
7469 it is removed from the corresponding output segment. */
7470 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \
7471 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7472 && section->output_section != NULL)
7474 /* Returns TRUE iff seg1 starts after the end of seg2. */
7475 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7476 (seg1->field >= segment_end (seg2, seg2->field))
7478 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7479 their VMA address ranges and their LMA address ranges overlap.
7480 It is possible to have overlapping VMA ranges without overlapping LMA
7481 ranges. RedBoot images for example can have both .data and .bss mapped
7482 to the same VMA range, but with the .data section mapped to a different
7484 #define SEGMENT_OVERLAPS(seg1, seg2) \
7485 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7486 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7487 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7488 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7490 /* Initialise the segment mark field, and discard stupid alignment. */
7491 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
7493 asection
*o
= section
->output_section
;
7494 if (o
!= NULL
&& o
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
7495 o
->alignment_power
= 0;
7496 section
->segment_mark
= false;
7499 /* The Solaris linker creates program headers in which all the
7500 p_paddr fields are zero. When we try to objcopy or strip such a
7501 file, we get confused. Check for this case, and if we find it
7502 don't set the p_paddr_valid fields. */
7503 p_paddr_valid
= false;
7504 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7507 if (segment
->p_paddr
!= 0)
7509 p_paddr_valid
= true;
7513 /* Scan through the segments specified in the program header
7514 of the input BFD. For this first scan we look for overlaps
7515 in the loadable segments. These can be created by weird
7516 parameters to objcopy. Also, fix some solaris weirdness. */
7517 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7522 Elf_Internal_Phdr
*segment2
;
7524 if (segment
->p_type
== PT_INTERP
)
7525 for (section
= ibfd
->sections
; section
; section
= section
->next
)
7526 if (IS_SOLARIS_PT_INTERP (segment
, section
))
7528 /* Mininal change so that the normal section to segment
7529 assignment code will work. */
7530 segment
->p_vaddr
= section
->vma
* opb
;
7534 if (segment
->p_type
!= PT_LOAD
)
7536 /* Remove PT_GNU_RELRO segment. */
7537 if (segment
->p_type
== PT_GNU_RELRO
)
7538 segment
->p_type
= PT_NULL
;
7542 /* Determine if this segment overlaps any previous segments. */
7543 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7545 bfd_signed_vma extra_length
;
7547 if (segment2
->p_type
!= PT_LOAD
7548 || !SEGMENT_OVERLAPS (segment
, segment2
))
7551 /* Merge the two segments together. */
7552 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7554 /* Extend SEGMENT2 to include SEGMENT and then delete
7556 extra_length
= (segment_end (segment
, segment
->p_vaddr
)
7557 - segment_end (segment2
, segment2
->p_vaddr
));
7559 if (extra_length
> 0)
7561 segment2
->p_memsz
+= extra_length
;
7562 segment2
->p_filesz
+= extra_length
;
7565 segment
->p_type
= PT_NULL
;
7567 /* Since we have deleted P we must restart the outer loop. */
7569 segment
= elf_tdata (ibfd
)->phdr
;
7574 /* Extend SEGMENT to include SEGMENT2 and then delete
7576 extra_length
= (segment_end (segment2
, segment2
->p_vaddr
)
7577 - segment_end (segment
, segment
->p_vaddr
));
7579 if (extra_length
> 0)
7581 segment
->p_memsz
+= extra_length
;
7582 segment
->p_filesz
+= extra_length
;
7585 segment2
->p_type
= PT_NULL
;
7590 /* The second scan attempts to assign sections to segments. */
7591 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7595 unsigned int section_count
;
7596 asection
**sections
;
7597 asection
*output_section
;
7599 asection
*matching_lma
;
7600 asection
*suggested_lma
;
7603 asection
*first_section
;
7605 if (segment
->p_type
== PT_NULL
)
7608 first_section
= NULL
;
7609 /* Compute how many sections might be placed into this segment. */
7610 for (section
= ibfd
->sections
, section_count
= 0;
7612 section
= section
->next
)
7614 /* Find the first section in the input segment, which may be
7615 removed from the corresponding output segment. */
7616 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, opb
, p_paddr_valid
))
7618 if (first_section
== NULL
)
7619 first_section
= section
;
7620 if (section
->output_section
!= NULL
)
7625 /* Allocate a segment map big enough to contain
7626 all of the sections we have selected. */
7627 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7628 amt
+= section_count
* sizeof (asection
*);
7629 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7633 /* Initialise the fields of the segment map. Default to
7634 using the physical address of the segment in the input BFD. */
7636 map
->p_type
= segment
->p_type
;
7637 map
->p_flags
= segment
->p_flags
;
7638 map
->p_flags_valid
= 1;
7640 if (map
->p_type
== PT_LOAD
7641 && (ibfd
->flags
& D_PAGED
) != 0
7643 && segment
->p_align
> 1)
7645 map
->p_align
= segment
->p_align
;
7646 if (segment
->p_align
> maxpagesize
)
7647 map
->p_align
= maxpagesize
;
7648 map
->p_align_valid
= 1;
7651 /* If the first section in the input segment is removed, there is
7652 no need to preserve segment physical address in the corresponding
7654 if (!first_section
|| first_section
->output_section
!= NULL
)
7656 map
->p_paddr
= segment
->p_paddr
;
7657 map
->p_paddr_valid
= p_paddr_valid
;
7660 /* Determine if this segment contains the ELF file header
7661 and if it contains the program headers themselves. */
7662 map
->includes_filehdr
= (segment
->p_offset
== 0
7663 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7664 map
->includes_phdrs
= 0;
7666 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7668 map
->includes_phdrs
=
7669 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7670 && (segment
->p_offset
+ segment
->p_filesz
7671 >= ((bfd_vma
) iehdr
->e_phoff
7672 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7674 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7675 phdr_included
= true;
7678 if (section_count
== 0)
7680 /* Special segments, such as the PT_PHDR segment, may contain
7681 no sections, but ordinary, loadable segments should contain
7682 something. They are allowed by the ELF spec however, so only
7683 a warning is produced.
7684 Don't warn if an empty PT_LOAD contains the program headers.
7685 There is however the valid use case of embedded systems which
7686 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7687 flash memory with zeros. No warning is shown for that case. */
7688 if (segment
->p_type
== PT_LOAD
7689 && !map
->includes_phdrs
7690 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7691 /* xgettext:c-format */
7693 (_("%pB: warning: empty loadable segment detected"
7694 " at vaddr=%#" PRIx64
", is this intentional?"),
7695 ibfd
, (uint64_t) segment
->p_vaddr
);
7697 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7699 *pointer_to_map
= map
;
7700 pointer_to_map
= &map
->next
;
7705 /* Now scan the sections in the input BFD again and attempt
7706 to add their corresponding output sections to the segment map.
7707 The problem here is how to handle an output section which has
7708 been moved (ie had its LMA changed). There are four possibilities:
7710 1. None of the sections have been moved.
7711 In this case we can continue to use the segment LMA from the
7714 2. All of the sections have been moved by the same amount.
7715 In this case we can change the segment's LMA to match the LMA
7716 of the first section.
7718 3. Some of the sections have been moved, others have not.
7719 In this case those sections which have not been moved can be
7720 placed in the current segment which will have to have its size,
7721 and possibly its LMA changed, and a new segment or segments will
7722 have to be created to contain the other sections.
7724 4. The sections have been moved, but not by the same amount.
7725 In this case we can change the segment's LMA to match the LMA
7726 of the first section and we will have to create a new segment
7727 or segments to contain the other sections.
7729 In order to save time, we allocate an array to hold the section
7730 pointers that we are interested in. As these sections get assigned
7731 to a segment, they are removed from this array. */
7733 amt
= section_count
* sizeof (asection
*);
7734 sections
= (asection
**) bfd_malloc (amt
);
7735 if (sections
== NULL
)
7738 /* Step One: Scan for segment vs section LMA conflicts.
7739 Also add the sections to the section array allocated above.
7740 Also add the sections to the current segment. In the common
7741 case, where the sections have not been moved, this means that
7742 we have completely filled the segment, and there is nothing
7745 matching_lma
= NULL
;
7746 suggested_lma
= NULL
;
7748 for (section
= first_section
, j
= 0;
7750 section
= section
->next
)
7752 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, opb
, p_paddr_valid
))
7754 output_section
= section
->output_section
;
7756 sections
[j
++] = section
;
7758 /* The Solaris native linker always sets p_paddr to 0.
7759 We try to catch that case here, and set it to the
7760 correct value. Note - some backends require that
7761 p_paddr be left as zero. */
7763 && segment
->p_vaddr
!= 0
7764 && !bed
->want_p_paddr_set_to_zero
7766 && output_section
->lma
!= 0
7767 && (align_power (segment
->p_vaddr
7768 + (map
->includes_filehdr
7769 ? iehdr
->e_ehsize
: 0)
7770 + (map
->includes_phdrs
7771 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7773 output_section
->alignment_power
* opb
)
7774 == (output_section
->vma
* opb
)))
7775 map
->p_paddr
= segment
->p_vaddr
;
7777 /* Match up the physical address of the segment with the
7778 LMA address of the output section. */
7779 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7781 || is_note (section
, segment
))
7783 if (matching_lma
== NULL
7784 || output_section
->lma
< matching_lma
->lma
)
7785 matching_lma
= output_section
;
7787 /* We assume that if the section fits within the segment
7788 then it does not overlap any other section within that
7790 map
->sections
[isec
++] = output_section
;
7792 else if (suggested_lma
== NULL
)
7793 suggested_lma
= output_section
;
7795 if (j
== section_count
)
7800 BFD_ASSERT (j
== section_count
);
7802 /* Step Two: Adjust the physical address of the current segment,
7804 if (isec
== section_count
)
7806 /* All of the sections fitted within the segment as currently
7807 specified. This is the default case. Add the segment to
7808 the list of built segments and carry on to process the next
7809 program header in the input BFD. */
7810 map
->count
= section_count
;
7811 *pointer_to_map
= map
;
7812 pointer_to_map
= &map
->next
;
7815 && !bed
->want_p_paddr_set_to_zero
)
7817 bfd_vma hdr_size
= 0;
7818 if (map
->includes_filehdr
)
7819 hdr_size
= iehdr
->e_ehsize
;
7820 if (map
->includes_phdrs
)
7821 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7823 /* Account for padding before the first section in the
7825 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7826 - matching_lma
->lma
);
7834 /* Change the current segment's physical address to match
7835 the LMA of the first section that fitted, or if no
7836 section fitted, the first section. */
7837 if (matching_lma
== NULL
)
7838 matching_lma
= suggested_lma
;
7840 map
->p_paddr
= matching_lma
->lma
* opb
;
7842 /* Offset the segment physical address from the lma
7843 to allow for space taken up by elf headers. */
7844 if (map
->includes_phdrs
)
7846 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7848 /* iehdr->e_phnum is just an estimate of the number
7849 of program headers that we will need. Make a note
7850 here of the number we used and the segment we chose
7851 to hold these headers, so that we can adjust the
7852 offset when we know the correct value. */
7853 phdr_adjust_num
= iehdr
->e_phnum
;
7854 phdr_adjust_seg
= map
;
7857 if (map
->includes_filehdr
)
7859 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7860 map
->p_paddr
-= iehdr
->e_ehsize
;
7861 /* We've subtracted off the size of headers from the
7862 first section lma, but there may have been some
7863 alignment padding before that section too. Try to
7864 account for that by adjusting the segment lma down to
7865 the same alignment. */
7866 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7867 align
= segment
->p_align
;
7868 map
->p_paddr
&= -(align
* opb
);
7872 /* Step Three: Loop over the sections again, this time assigning
7873 those that fit to the current segment and removing them from the
7874 sections array; but making sure not to leave large gaps. Once all
7875 possible sections have been assigned to the current segment it is
7876 added to the list of built segments and if sections still remain
7877 to be assigned, a new segment is constructed before repeating
7883 suggested_lma
= NULL
;
7885 /* Fill the current segment with sections that fit. */
7886 for (j
= 0; j
< section_count
; j
++)
7888 section
= sections
[j
];
7890 if (section
== NULL
)
7893 output_section
= section
->output_section
;
7895 BFD_ASSERT (output_section
!= NULL
);
7897 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7899 || is_note (section
, segment
))
7901 if (map
->count
== 0)
7903 /* If the first section in a segment does not start at
7904 the beginning of the segment, then something is
7906 if (align_power (map
->p_paddr
7907 + (map
->includes_filehdr
7908 ? iehdr
->e_ehsize
: 0)
7909 + (map
->includes_phdrs
7910 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7912 output_section
->alignment_power
* opb
)
7913 != output_section
->lma
* opb
)
7920 prev_sec
= map
->sections
[map
->count
- 1];
7922 /* If the gap between the end of the previous section
7923 and the start of this section is more than
7924 maxpagesize then we need to start a new segment. */
7925 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7927 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7928 || (prev_sec
->lma
+ prev_sec
->size
7929 > output_section
->lma
))
7931 if (suggested_lma
== NULL
)
7932 suggested_lma
= output_section
;
7938 map
->sections
[map
->count
++] = output_section
;
7941 if (segment
->p_type
== PT_LOAD
)
7942 section
->segment_mark
= true;
7944 else if (suggested_lma
== NULL
)
7945 suggested_lma
= output_section
;
7948 /* PR 23932. A corrupt input file may contain sections that cannot
7949 be assigned to any segment - because for example they have a
7950 negative size - or segments that do not contain any sections.
7951 But there are also valid reasons why a segment can be empty.
7952 So allow a count of zero. */
7954 /* Add the current segment to the list of built segments. */
7955 *pointer_to_map
= map
;
7956 pointer_to_map
= &map
->next
;
7958 if (isec
< section_count
)
7960 /* We still have not allocated all of the sections to
7961 segments. Create a new segment here, initialise it
7962 and carry on looping. */
7963 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7964 amt
+= section_count
* sizeof (asection
*);
7965 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7972 /* Initialise the fields of the segment map. Set the physical
7973 physical address to the LMA of the first section that has
7974 not yet been assigned. */
7976 map
->p_type
= segment
->p_type
;
7977 map
->p_flags
= segment
->p_flags
;
7978 map
->p_flags_valid
= 1;
7979 map
->p_paddr
= suggested_lma
->lma
* opb
;
7980 map
->p_paddr_valid
= p_paddr_valid
;
7981 map
->includes_filehdr
= 0;
7982 map
->includes_phdrs
= 0;
7987 bfd_set_error (bfd_error_sorry
);
7991 while (isec
< section_count
);
7996 elf_seg_map (obfd
) = map_first
;
7998 /* If we had to estimate the number of program headers that were
7999 going to be needed, then check our estimate now and adjust
8000 the offset if necessary. */
8001 if (phdr_adjust_seg
!= NULL
)
8005 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
8008 if (count
> phdr_adjust_num
)
8009 phdr_adjust_seg
->p_paddr
8010 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
8012 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
8013 if (map
->p_type
== PT_PHDR
)
8016 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
8017 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
8022 #undef IS_SOLARIS_PT_INTERP
8023 #undef IS_SECTION_IN_INPUT_SEGMENT
8024 #undef INCLUDE_SECTION_IN_SEGMENT
8025 #undef SEGMENT_AFTER_SEGMENT
8026 #undef SEGMENT_OVERLAPS
8030 /* Return true if p_align in the ELF program header in ABFD is valid. */
8033 elf_is_p_align_valid (bfd
*abfd
)
8036 Elf_Internal_Phdr
*segment
;
8037 unsigned int num_segments
;
8038 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8039 bfd_size_type maxpagesize
= bed
->maxpagesize
;
8040 bfd_size_type p_align
= bed
->p_align
;
8042 /* Return true if the default p_align value isn't set or the maximum
8043 page size is the same as the minimum page size. */
8044 if (p_align
== 0 || maxpagesize
== bed
->minpagesize
)
8047 /* When the default p_align value is set, p_align may be set to the
8048 default p_align value while segments are aligned to the maximum
8049 page size. In this case, the input p_align will be ignored and
8050 the maximum page size will be used to align the output segments. */
8051 segment
= elf_tdata (abfd
)->phdr
;
8052 num_segments
= elf_elfheader (abfd
)->e_phnum
;
8053 for (i
= 0; i
< num_segments
; i
++, segment
++)
8054 if (segment
->p_type
== PT_LOAD
8055 && (segment
->p_align
!= p_align
8056 || vma_page_aligned_bias (segment
->p_vaddr
,
8064 /* Copy ELF program header information. */
8067 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
8069 Elf_Internal_Ehdr
*iehdr
;
8070 struct elf_segment_map
*map
;
8071 struct elf_segment_map
*map_first
;
8072 struct elf_segment_map
**pointer_to_map
;
8073 Elf_Internal_Phdr
*segment
;
8075 unsigned int num_segments
;
8076 bool phdr_included
= false;
8078 bool p_palign_valid
;
8079 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
8081 iehdr
= elf_elfheader (ibfd
);
8084 pointer_to_map
= &map_first
;
8086 /* If all the segment p_paddr fields are zero, don't set
8087 map->p_paddr_valid. */
8088 p_paddr_valid
= false;
8089 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8090 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8093 if (segment
->p_paddr
!= 0)
8095 p_paddr_valid
= true;
8099 p_palign_valid
= elf_is_p_align_valid (ibfd
);
8101 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8106 unsigned int section_count
;
8108 Elf_Internal_Shdr
*this_hdr
;
8109 asection
*first_section
= NULL
;
8110 asection
*lowest_section
;
8112 /* Compute how many sections are in this segment. */
8113 for (section
= ibfd
->sections
, section_count
= 0;
8115 section
= section
->next
)
8117 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8118 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8120 if (first_section
== NULL
)
8121 first_section
= section
;
8126 /* Allocate a segment map big enough to contain
8127 all of the sections we have selected. */
8128 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
8129 amt
+= section_count
* sizeof (asection
*);
8130 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
8134 /* Initialize the fields of the output segment map with the
8137 map
->p_type
= segment
->p_type
;
8138 map
->p_flags
= segment
->p_flags
;
8139 map
->p_flags_valid
= 1;
8140 map
->p_paddr
= segment
->p_paddr
;
8141 map
->p_paddr_valid
= p_paddr_valid
;
8142 map
->p_align
= segment
->p_align
;
8143 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8144 map
->p_align_valid
= (map
->p_type
== PT_GNU_STACK
8146 map
->p_vaddr_offset
= 0;
8148 if (map
->p_type
== PT_GNU_RELRO
8149 || map
->p_type
== PT_GNU_STACK
)
8151 /* The PT_GNU_RELRO segment may contain the first a few
8152 bytes in the .got.plt section even if the whole .got.plt
8153 section isn't in the PT_GNU_RELRO segment. We won't
8154 change the size of the PT_GNU_RELRO segment.
8155 Similarly, PT_GNU_STACK size is significant on uclinux
8157 map
->p_size
= segment
->p_memsz
;
8158 map
->p_size_valid
= 1;
8161 /* Determine if this segment contains the ELF file header
8162 and if it contains the program headers themselves. */
8163 map
->includes_filehdr
= (segment
->p_offset
== 0
8164 && segment
->p_filesz
>= iehdr
->e_ehsize
);
8166 map
->includes_phdrs
= 0;
8167 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
8169 map
->includes_phdrs
=
8170 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
8171 && (segment
->p_offset
+ segment
->p_filesz
8172 >= ((bfd_vma
) iehdr
->e_phoff
8173 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
8175 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
8176 phdr_included
= true;
8179 lowest_section
= NULL
;
8180 if (section_count
!= 0)
8182 unsigned int isec
= 0;
8184 for (section
= first_section
;
8186 section
= section
->next
)
8188 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8189 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8191 map
->sections
[isec
++] = section
->output_section
;
8192 if ((section
->flags
& SEC_ALLOC
) != 0)
8196 if (lowest_section
== NULL
8197 || section
->lma
< lowest_section
->lma
)
8198 lowest_section
= section
;
8200 /* Section lmas are set up from PT_LOAD header
8201 p_paddr in _bfd_elf_make_section_from_shdr.
8202 If this header has a p_paddr that disagrees
8203 with the section lma, flag the p_paddr as
8205 if ((section
->flags
& SEC_LOAD
) != 0)
8206 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
8208 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
8209 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
8210 map
->p_paddr_valid
= false;
8212 if (isec
== section_count
)
8218 if (section_count
== 0)
8219 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
8220 else if (map
->p_paddr_valid
)
8222 /* Account for padding before the first section in the segment. */
8223 bfd_vma hdr_size
= 0;
8224 if (map
->includes_filehdr
)
8225 hdr_size
= iehdr
->e_ehsize
;
8226 if (map
->includes_phdrs
)
8227 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
8229 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
8230 - (lowest_section
? lowest_section
->lma
: 0));
8233 map
->count
= section_count
;
8234 *pointer_to_map
= map
;
8235 pointer_to_map
= &map
->next
;
8238 elf_seg_map (obfd
) = map_first
;
8242 /* Copy private BFD data. This copies or rewrites ELF program header
8246 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
8248 bfd_vma maxpagesize
;
8250 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8251 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8254 if (elf_tdata (ibfd
)->phdr
== NULL
)
8257 if (ibfd
->xvec
== obfd
->xvec
)
8259 /* Check to see if any sections in the input BFD
8260 covered by ELF program header have changed. */
8261 Elf_Internal_Phdr
*segment
;
8265 unsigned int i
, num_segments
;
8266 Elf_Internal_Shdr
*this_hdr
;
8267 const struct elf_backend_data
*bed
;
8269 bed
= get_elf_backend_data (ibfd
);
8271 /* Regenerate the segment map if p_paddr is set to 0. */
8272 if (bed
->want_p_paddr_set_to_zero
)
8275 /* Initialize the segment mark field. */
8276 for (section
= obfd
->sections
; section
!= NULL
;
8277 section
= section
->next
)
8278 section
->segment_mark
= false;
8280 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8281 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8285 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8286 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8287 which severly confuses things, so always regenerate the segment
8288 map in this case. */
8289 if (segment
->p_paddr
== 0
8290 && segment
->p_memsz
== 0
8291 && (segment
->p_type
== PT_INTERP
8292 || segment
->p_type
== PT_DYNAMIC
))
8295 for (section
= ibfd
->sections
, prev
= NULL
;
8296 section
!= NULL
; section
= section
->next
)
8298 /* We mark the output section so that we know it comes
8299 from the input BFD. */
8300 osec
= section
->output_section
;
8302 osec
->segment_mark
= true;
8304 /* Check if this section is covered by the segment. */
8305 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8306 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8308 /* FIXME: Check if its output section is changed or
8309 removed. What else do we need to check? */
8311 || section
->flags
!= osec
->flags
8312 || section
->lma
!= osec
->lma
8313 || section
->vma
!= osec
->vma
8314 || section
->size
!= osec
->size
8315 || section
->rawsize
!= osec
->rawsize
8316 || section
->alignment_power
!= osec
->alignment_power
)
8319 /* PR 31450: If this is an allocated section then make sure
8320 that this section's vma to lma relationship is the same
8321 as previous (allocated) section's. */
8323 && section
->flags
& SEC_ALLOC
8324 && section
->lma
- section
->vma
!= prev
->lma
- prev
->vma
)
8327 if (section
->flags
& SEC_ALLOC
)
8333 /* Check to see if any output section do not come from the
8335 for (section
= obfd
->sections
; section
!= NULL
;
8336 section
= section
->next
)
8338 if (!section
->segment_mark
)
8341 section
->segment_mark
= false;
8344 return copy_elf_program_header (ibfd
, obfd
);
8349 if (ibfd
->xvec
== obfd
->xvec
)
8351 /* When rewriting program header, set the output maxpagesize to
8352 the maximum alignment of input PT_LOAD segments. */
8353 Elf_Internal_Phdr
*segment
;
8355 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8357 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8360 if (segment
->p_type
== PT_LOAD
8361 && maxpagesize
< segment
->p_align
)
8363 /* PR 17512: file: f17299af. */
8364 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
8365 /* xgettext:c-format */
8366 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8367 PRIx64
" is too large"),
8368 ibfd
, (uint64_t) segment
->p_align
);
8370 maxpagesize
= segment
->p_align
;
8373 if (maxpagesize
== 0)
8374 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
8376 return rewrite_elf_program_header (ibfd
, obfd
, maxpagesize
);
8379 /* Copy private section information from input to output. This function
8380 is called both by objcopy where LINK_INFO is NULL, and ld where
8381 LINK_INFO is non-NULL in the usual case but NULL for a special case
8382 when dealing with LTO IR or cloning output sections. */
8385 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
8389 struct bfd_link_info
*link_info
)
8391 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8392 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8395 Elf_Internal_Shdr
*ihdr
= &elf_section_data (isec
)->this_hdr
;
8396 Elf_Internal_Shdr
*ohdr
= &elf_section_data (osec
)->this_hdr
;
8397 if (link_info
== NULL
)
8399 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
8401 if (ihdr
->sh_type
== SHT_SYMTAB
8402 || ihdr
->sh_type
== SHT_DYNSYM
8403 || ihdr
->sh_type
== SHT_GNU_verneed
8404 || ihdr
->sh_type
== SHT_GNU_verdef
)
8405 ohdr
->sh_info
= ihdr
->sh_info
;
8408 /* If this is a known ABI section, ELF section type and flags may
8409 have been set up when OSEC was created. For normal sections we
8410 allow the user to override the type and flags other than
8411 SHF_MASKOS and SHF_MASKPROC. */
8412 if (elf_section_type (osec
) == SHT_PROGBITS
8413 || elf_section_type (osec
) == SHT_NOTE
8414 || elf_section_type (osec
) == SHT_NOBITS
)
8415 elf_section_type (osec
) = SHT_NULL
;
8417 /* For objcopy and relocatable link, copy the ELF section type from
8418 the input file if the BFD section flags are the same. (If they
8419 are different the user may be doing something like
8420 "objcopy --set-section-flags .text=alloc,data".) For a final
8421 link allow some flags that the linker clears to differ. */
8422 bool final_link
= (link_info
!= NULL
8423 && !bfd_link_relocatable (link_info
));
8424 if (elf_section_type (osec
) == SHT_NULL
8425 && (osec
->flags
== isec
->flags
8427 && ((osec
->flags
^ isec
->flags
)
8428 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
8429 elf_section_type (osec
) = elf_section_type (isec
);
8431 /* FIXME: Is this correct for all OS/PROC specific flags? */
8432 elf_section_flags (osec
) = (elf_section_flags (isec
)
8433 & (SHF_MASKOS
| SHF_MASKPROC
));
8435 /* Copy sh_info from input for mbind section. */
8436 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
8437 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
8438 elf_section_data (osec
)->this_hdr
.sh_info
8439 = elf_section_data (isec
)->this_hdr
.sh_info
;
8441 /* Set things up for objcopy and relocatable link. The output
8442 SHT_GROUP section will have its elf_next_in_group pointing back
8443 to the input group members. Ignore linker created group section.
8444 See elfNN_ia64_object_p in elfxx-ia64.c. */
8445 if ((link_info
== NULL
8446 || !link_info
->resolve_section_groups
)
8447 && (elf_sec_group (isec
) == NULL
8448 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
8450 if (elf_section_flags (isec
) & SHF_GROUP
)
8451 elf_section_flags (osec
) |= SHF_GROUP
;
8452 elf_next_in_group (osec
) = elf_next_in_group (isec
);
8453 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
8456 /* If not decompress, preserve SHF_COMPRESSED. */
8457 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
8458 elf_section_flags (osec
) |= (elf_section_flags (isec
)
8461 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8462 don't use the output section of the linked-to section since it
8463 may be NULL at this point. */
8464 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
8466 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
8467 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
8470 osec
->use_rela_p
= isec
->use_rela_p
;
8475 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8476 necessary if we are removing either the SHT_GROUP section or any of
8477 the group member sections. DISCARDED is the value that a section's
8478 output_section has if the section will be discarded, NULL when this
8479 function is called from objcopy, bfd_abs_section_ptr when called
8483 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
8487 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
8488 if (elf_section_type (isec
) == SHT_GROUP
)
8490 asection
*first
= elf_next_in_group (isec
);
8491 asection
*s
= first
;
8492 bfd_size_type removed
= 0;
8496 /* If this member section is being output but the
8497 SHT_GROUP section is not, then clear the group info
8498 set up by _bfd_elf_copy_private_section_data. */
8499 if (s
->output_section
!= discarded
8500 && isec
->output_section
== discarded
)
8502 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
8503 elf_group_name (s
->output_section
) = NULL
;
8507 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
8508 if (s
->output_section
== discarded
8509 && isec
->output_section
!= discarded
)
8511 /* Conversely, if the member section is not being
8512 output but the SHT_GROUP section is, then adjust
8515 if (elf_sec
->rel
.hdr
!= NULL
8516 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8518 if (elf_sec
->rela
.hdr
!= NULL
8519 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8524 /* Also adjust for zero-sized relocation member
8526 if (elf_sec
->rel
.hdr
!= NULL
8527 && elf_sec
->rel
.hdr
->sh_size
== 0)
8529 if (elf_sec
->rela
.hdr
!= NULL
8530 && elf_sec
->rela
.hdr
->sh_size
== 0)
8534 s
= elf_next_in_group (s
);
8540 if (discarded
!= NULL
)
8542 /* If we've been called for ld -r, then we need to
8543 adjust the input section size. */
8544 if (isec
->rawsize
== 0)
8545 isec
->rawsize
= isec
->size
;
8546 isec
->size
= isec
->rawsize
- removed
;
8547 if (isec
->size
<= 4)
8550 isec
->flags
|= SEC_EXCLUDE
;
8553 else if (isec
->output_section
!= NULL
)
8555 /* Adjust the output section size when called from
8557 isec
->output_section
->size
-= removed
;
8558 if (isec
->output_section
->size
<= 4)
8560 isec
->output_section
->size
= 0;
8561 isec
->output_section
->flags
|= SEC_EXCLUDE
;
8570 /* Copy private header information. */
8573 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
8575 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8576 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8579 /* Copy over private BFD data if it has not already been copied.
8580 This must be done here, rather than in the copy_private_bfd_data
8581 entry point, because the latter is called after the section
8582 contents have been set, which means that the program headers have
8583 already been worked out. */
8584 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
8586 if (! copy_private_bfd_data (ibfd
, obfd
))
8590 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8593 /* Copy private symbol information. If this symbol is in a section
8594 which we did not map into a BFD section, try to map the section
8595 index correctly. We use special macro definitions for the mapped
8596 section indices; these definitions are interpreted by the
8597 swap_out_syms function. */
8599 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8600 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8601 #define MAP_STRTAB (SHN_HIOS + 3)
8602 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8603 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8606 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8611 elf_symbol_type
*isym
, *osym
;
8613 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8614 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8617 isym
= elf_symbol_from (isymarg
);
8618 osym
= elf_symbol_from (osymarg
);
8621 && isym
->internal_elf_sym
.st_shndx
!= 0
8623 && bfd_is_abs_section (isym
->symbol
.section
))
8627 shndx
= isym
->internal_elf_sym
.st_shndx
;
8628 if (shndx
== elf_onesymtab (ibfd
))
8629 shndx
= MAP_ONESYMTAB
;
8630 else if (shndx
== elf_dynsymtab (ibfd
))
8631 shndx
= MAP_DYNSYMTAB
;
8632 else if (shndx
== elf_elfsections (ibfd
)[elf_onesymtab (ibfd
)]->sh_link
)
8634 else if (shndx
== elf_elfheader (ibfd
)->e_shstrndx
)
8635 shndx
= MAP_SHSTRTAB
;
8636 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8637 shndx
= MAP_SYM_SHNDX
;
8638 osym
->internal_elf_sym
.st_shndx
= shndx
;
8644 /* Swap out the symbols. */
8647 swap_out_syms (bfd
*abfd
,
8648 struct elf_strtab_hash
**sttp
,
8650 struct bfd_link_info
*info
)
8652 const struct elf_backend_data
*bed
;
8653 unsigned int symcount
;
8655 struct elf_strtab_hash
*stt
;
8656 Elf_Internal_Shdr
*symtab_hdr
;
8657 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8658 Elf_Internal_Shdr
*symstrtab_hdr
;
8659 struct elf_sym_strtab
*symstrtab
;
8660 bfd_byte
*outbound_syms
;
8661 bfd_byte
*outbound_shndx
;
8662 unsigned long outbound_syms_index
;
8664 unsigned int num_locals
;
8666 bool name_local_sections
;
8668 if (!elf_map_symbols (abfd
, &num_locals
))
8671 /* Dump out the symtabs. */
8672 stt
= _bfd_elf_strtab_init ();
8676 bed
= get_elf_backend_data (abfd
);
8677 symcount
= bfd_get_symcount (abfd
);
8678 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8679 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8680 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8681 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8682 symtab_hdr
->sh_info
= num_locals
+ 1;
8683 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8685 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8686 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8688 /* Allocate buffer to swap out the .strtab section. */
8689 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8690 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8692 bfd_set_error (bfd_error_no_memory
);
8693 _bfd_elf_strtab_free (stt
);
8697 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8698 || (outbound_syms
= bfd_malloc (amt
)) == NULL
)
8701 bfd_set_error (bfd_error_no_memory
);
8704 _bfd_elf_strtab_free (stt
);
8707 symtab_hdr
->contents
= outbound_syms
;
8708 outbound_syms_index
= 0;
8710 outbound_shndx
= NULL
;
8712 if (elf_symtab_shndx_list (abfd
))
8714 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8715 if (symtab_shndx_hdr
->sh_name
!= 0)
8717 if (_bfd_mul_overflow (symcount
+ 1,
8718 sizeof (Elf_External_Sym_Shndx
), &amt
))
8720 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8721 if (outbound_shndx
== NULL
)
8724 symtab_shndx_hdr
->contents
= outbound_shndx
;
8725 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8726 symtab_shndx_hdr
->sh_size
= amt
;
8727 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8728 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8730 /* FIXME: What about any other headers in the list ? */
8733 /* Now generate the data (for "contents"). */
8735 /* Fill in zeroth symbol and swap it out. */
8736 Elf_Internal_Sym sym
;
8742 sym
.st_shndx
= SHN_UNDEF
;
8743 sym
.st_target_internal
= 0;
8744 symstrtab
[outbound_syms_index
].sym
= sym
;
8745 symstrtab
[outbound_syms_index
].dest_index
= outbound_syms_index
;
8746 outbound_syms_index
++;
8750 = (bed
->elf_backend_name_local_section_symbols
8751 && bed
->elf_backend_name_local_section_symbols (abfd
));
8753 syms
= bfd_get_outsymbols (abfd
);
8754 for (idx
= 0; idx
< symcount
; idx
++)
8756 Elf_Internal_Sym sym
;
8758 flagword flags
= syms
[idx
]->flags
;
8759 if (!name_local_sections
8760 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8762 /* Local section symbols have no name. */
8767 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8768 to get the final offset for st_name. */
8769 size_t stridx
= _bfd_elf_strtab_add (stt
, syms
[idx
]->name
, false);
8770 if (stridx
== (size_t) -1)
8772 sym
.st_name
= stridx
;
8775 bfd_vma value
= syms
[idx
]->value
;
8776 elf_symbol_type
*type_ptr
= elf_symbol_from (syms
[idx
]);
8777 asection
*sec
= syms
[idx
]->section
;
8779 if ((flags
& BSF_SECTION_SYM
) == 0 && bfd_is_com_section (sec
))
8781 /* ELF common symbols put the alignment into the `value' field,
8782 and the size into the `size' field. This is backwards from
8783 how BFD handles it, so reverse it here. */
8784 sym
.st_size
= value
;
8785 if (type_ptr
== NULL
8786 || type_ptr
->internal_elf_sym
.st_value
== 0)
8787 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8789 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8790 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8796 if (sec
->output_section
)
8798 value
+= sec
->output_offset
;
8799 sec
= sec
->output_section
;
8802 /* Don't add in the section vma for relocatable output. */
8803 if (! relocatable_p
)
8805 sym
.st_value
= value
;
8806 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8808 if (bfd_is_abs_section (sec
)
8810 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8812 /* This symbol is in a real ELF section which we did
8813 not create as a BFD section. Undo the mapping done
8814 by copy_private_symbol_data. */
8815 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8819 shndx
= elf_onesymtab (abfd
);
8822 shndx
= elf_dynsymtab (abfd
);
8825 shndx
= elf_strtab_sec (abfd
);
8828 shndx
= elf_shstrtab_sec (abfd
);
8831 if (elf_symtab_shndx_list (abfd
))
8832 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8839 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8841 if (bed
->symbol_section_index
)
8842 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8843 /* Otherwise just leave the index alone. */
8847 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8848 _bfd_error_handler (_("%pB: \
8849 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8858 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8860 if (shndx
== SHN_BAD
)
8864 /* Writing this would be a hell of a lot easier if
8865 we had some decent documentation on bfd, and
8866 knew what to expect of the library, and what to
8867 demand of applications. For example, it
8868 appears that `objcopy' might not set the
8869 section of a symbol to be a section that is
8870 actually in the output file. */
8871 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8873 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8874 if (shndx
== SHN_BAD
)
8876 /* xgettext:c-format */
8878 (_("unable to find equivalent output section"
8879 " for symbol '%s' from section '%s'"),
8880 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8882 bfd_set_error (bfd_error_invalid_operation
);
8888 sym
.st_shndx
= shndx
;
8892 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8894 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8895 type
= STT_GNU_IFUNC
;
8896 else if ((flags
& BSF_FUNCTION
) != 0)
8898 else if ((flags
& BSF_OBJECT
) != 0)
8900 else if ((flags
& BSF_RELC
) != 0)
8902 else if ((flags
& BSF_SRELC
) != 0)
8907 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8910 /* Processor-specific types. */
8911 if (type_ptr
!= NULL
8912 && bed
->elf_backend_get_symbol_type
)
8913 type
= ((*bed
->elf_backend_get_symbol_type
)
8914 (&type_ptr
->internal_elf_sym
, type
));
8916 if (flags
& BSF_SECTION_SYM
)
8918 if (flags
& BSF_GLOBAL
)
8919 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8921 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8923 else if (bfd_is_com_section (syms
[idx
]->section
))
8925 if (type
!= STT_TLS
)
8927 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8928 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8929 ? STT_COMMON
: STT_OBJECT
);
8931 type
= ((flags
& BSF_ELF_COMMON
) != 0
8932 ? STT_COMMON
: STT_OBJECT
);
8934 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8936 else if (bfd_is_und_section (syms
[idx
]->section
))
8937 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8941 else if (flags
& BSF_FILE
)
8942 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8945 int bind
= STB_LOCAL
;
8947 if (flags
& BSF_LOCAL
)
8949 else if (flags
& BSF_GNU_UNIQUE
)
8950 bind
= STB_GNU_UNIQUE
;
8951 else if (flags
& BSF_WEAK
)
8953 else if (flags
& BSF_GLOBAL
)
8956 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8959 if (type_ptr
!= NULL
)
8961 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8962 sym
.st_target_internal
8963 = type_ptr
->internal_elf_sym
.st_target_internal
;
8968 sym
.st_target_internal
= 0;
8971 symstrtab
[outbound_syms_index
].sym
= sym
;
8972 symstrtab
[outbound_syms_index
].dest_index
= outbound_syms_index
;
8973 outbound_syms_index
++;
8976 /* Finalize the .strtab section. */
8977 _bfd_elf_strtab_finalize (stt
);
8979 /* Swap out the .strtab section. */
8980 for (idx
= 0; idx
< outbound_syms_index
; idx
++)
8982 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8983 if (elfsym
->sym
.st_name
!= 0)
8984 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8985 elfsym
->sym
.st_name
);
8986 if (info
&& info
->callbacks
->ctf_new_symbol
)
8987 info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
8990 /* Inform the linker of the addition of this symbol. */
8992 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8994 + (elfsym
->dest_index
8995 * bed
->s
->sizeof_sym
)),
8996 NPTR_ADD (outbound_shndx
,
8998 * sizeof (Elf_External_Sym_Shndx
))));
9003 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
9004 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9005 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
9006 symstrtab_hdr
->sh_addr
= 0;
9007 symstrtab_hdr
->sh_entsize
= 0;
9008 symstrtab_hdr
->sh_link
= 0;
9009 symstrtab_hdr
->sh_info
= 0;
9010 symstrtab_hdr
->sh_addralign
= 1;
9015 /* Return the number of bytes required to hold the symtab vector.
9017 Note that we base it on the count plus 1, since we will null terminate
9018 the vector allocated based on this size. However, the ELF symbol table
9019 always has a dummy entry as symbol #0, so it ends up even. */
9022 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
9024 bfd_size_type symcount
;
9026 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9028 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
9029 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
9031 bfd_set_error (bfd_error_file_too_big
);
9034 symtab_size
= symcount
* (sizeof (asymbol
*));
9036 symtab_size
= sizeof (asymbol
*);
9037 else if (!bfd_write_p (abfd
))
9039 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9041 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
9043 bfd_set_error (bfd_error_file_truncated
);
9052 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
9054 bfd_size_type symcount
;
9056 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
9058 if (elf_dynsymtab (abfd
) == 0)
9060 /* Check if there is dynamic symbol table. */
9061 symcount
= elf_tdata (abfd
)->dt_symtab_count
;
9063 goto compute_symtab_size
;
9065 bfd_set_error (bfd_error_invalid_operation
);
9069 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
9070 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
9072 bfd_set_error (bfd_error_file_too_big
);
9076 compute_symtab_size
:
9077 symtab_size
= symcount
* (sizeof (asymbol
*));
9079 symtab_size
= sizeof (asymbol
*);
9080 else if (!bfd_write_p (abfd
))
9082 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9084 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
9086 bfd_set_error (bfd_error_file_truncated
);
9095 _bfd_elf_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
9097 if (asect
->reloc_count
!= 0 && !bfd_write_p (abfd
))
9099 /* Sanity check reloc section size. */
9100 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9104 struct bfd_elf_section_data
*d
= elf_section_data (asect
);
9105 bfd_size_type rel_size
= d
->rel
.hdr
? d
->rel
.hdr
->sh_size
: 0;
9106 bfd_size_type rela_size
= d
->rela
.hdr
? d
->rela
.hdr
->sh_size
: 0;
9108 if (rel_size
+ rela_size
> filesize
9109 || rel_size
+ rela_size
< rel_size
)
9111 bfd_set_error (bfd_error_file_truncated
);
9117 #if SIZEOF_LONG == SIZEOF_INT
9118 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
9120 bfd_set_error (bfd_error_file_too_big
);
9124 return (asect
->reloc_count
+ 1L) * sizeof (arelent
*);
9127 /* Canonicalize the relocs. */
9130 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
9137 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9139 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
9142 tblptr
= section
->relocation
;
9143 for (i
= 0; i
< section
->reloc_count
; i
++)
9144 *relptr
++ = tblptr
++;
9148 return section
->reloc_count
;
9152 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
9154 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9155 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, false);
9158 abfd
->symcount
= symcount
;
9163 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
9164 asymbol
**allocation
)
9166 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9167 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, true);
9170 abfd
->dynsymcount
= symcount
;
9174 /* Return the size required for the dynamic reloc entries. Any loadable
9175 section that was actually installed in the BFD, and has type SHT_REL
9176 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9177 dynamic reloc section. */
9180 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
9182 bfd_size_type count
, ext_rel_size
;
9185 if (elf_dynsymtab (abfd
) == 0)
9187 bfd_set_error (bfd_error_invalid_operation
);
9193 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9194 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
9195 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
9196 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
)
9197 && (elf_section_data (s
)->this_hdr
.sh_flags
& SHF_COMPRESSED
) == 0)
9199 ext_rel_size
+= elf_section_data (s
)->this_hdr
.sh_size
;
9200 if (ext_rel_size
< elf_section_data (s
)->this_hdr
.sh_size
)
9202 bfd_set_error (bfd_error_file_truncated
);
9205 count
+= NUM_SHDR_ENTRIES (&elf_section_data (s
)->this_hdr
);
9206 if (count
> LONG_MAX
/ sizeof (arelent
*))
9208 bfd_set_error (bfd_error_file_too_big
);
9212 if (count
> 1 && !bfd_write_p (abfd
))
9214 /* Sanity check reloc section sizes. */
9215 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9216 if (filesize
!= 0 && ext_rel_size
> filesize
)
9218 bfd_set_error (bfd_error_file_truncated
);
9222 return count
* sizeof (arelent
*);
9225 /* Canonicalize the dynamic relocation entries. Note that we return the
9226 dynamic relocations as a single block, although they are actually
9227 associated with particular sections; the interface, which was
9228 designed for SunOS style shared libraries, expects that there is only
9229 one set of dynamic relocs. Any loadable section that was actually
9230 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9231 dynamic symbol table, is considered to be a dynamic reloc section. */
9234 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
9238 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
9242 if (elf_dynsymtab (abfd
) == 0)
9244 bfd_set_error (bfd_error_invalid_operation
);
9248 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9250 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9252 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
9253 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
9254 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
)
9255 && (elf_section_data (s
)->this_hdr
.sh_flags
& SHF_COMPRESSED
) == 0)
9260 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
9262 count
= NUM_SHDR_ENTRIES (&elf_section_data (s
)->this_hdr
);
9264 for (i
= 0; i
< count
; i
++)
9275 /* Read in the version information. */
9278 _bfd_elf_slurp_version_tables (bfd
*abfd
, bool default_imported_symver
)
9280 bfd_byte
*contents
= NULL
;
9281 unsigned int freeidx
= 0;
9283 void *contents_addr
= NULL
;
9284 size_t contents_size
= 0;
9286 if (elf_dynverref (abfd
) != 0 || elf_tdata (abfd
)->dt_verneed
!= NULL
)
9288 Elf_Internal_Shdr
*hdr
;
9289 Elf_External_Verneed
*everneed
;
9290 Elf_Internal_Verneed
*iverneed
;
9292 bfd_byte
*contents_end
;
9293 size_t verneed_count
;
9294 size_t verneed_size
;
9296 if (elf_tdata (abfd
)->dt_verneed
!= NULL
)
9299 contents
= elf_tdata (abfd
)->dt_verneed
;
9300 verneed_count
= elf_tdata (abfd
)->dt_verneed_count
;
9301 verneed_size
= verneed_count
* sizeof (Elf_External_Verneed
);
9305 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
9307 if (hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
9309 error_return_bad_verref
:
9311 (_("%pB: .gnu.version_r invalid entry"), abfd
);
9312 bfd_set_error (bfd_error_bad_value
);
9313 error_return_verref
:
9314 elf_tdata (abfd
)->verref
= NULL
;
9315 elf_tdata (abfd
)->cverrefs
= 0;
9319 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9320 goto error_return_verref
;
9321 contents_size
= hdr
->sh_size
;
9322 contents
= _bfd_mmap_temporary (abfd
, contents_size
,
9323 &contents_addr
, &contents_size
);
9324 if (contents
== NULL
)
9325 goto error_return_verref
;
9327 verneed_size
= hdr
->sh_size
;
9328 verneed_count
= hdr
->sh_info
;
9331 if (_bfd_mul_overflow (verneed_count
,
9332 sizeof (Elf_Internal_Verneed
), &amt
))
9334 bfd_set_error (bfd_error_file_too_big
);
9335 goto error_return_verref
;
9338 goto error_return_verref
;
9339 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
9340 if (elf_tdata (abfd
)->verref
== NULL
)
9341 goto error_return_verref
;
9343 BFD_ASSERT (sizeof (Elf_External_Verneed
)
9344 == sizeof (Elf_External_Vernaux
));
9345 contents_end
= (contents
+ verneed_size
9346 - sizeof (Elf_External_Verneed
));
9347 everneed
= (Elf_External_Verneed
*) contents
;
9348 iverneed
= elf_tdata (abfd
)->verref
;
9349 for (i
= 0; i
< verneed_count
; i
++, iverneed
++)
9351 Elf_External_Vernaux
*evernaux
;
9352 Elf_Internal_Vernaux
*ivernaux
;
9355 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
9357 iverneed
->vn_bfd
= abfd
;
9359 if (elf_use_dt_symtab_p (abfd
))
9361 if (iverneed
->vn_file
< elf_tdata (abfd
)->dt_strsz
)
9362 iverneed
->vn_filename
9363 = elf_tdata (abfd
)->dt_strtab
+ iverneed
->vn_file
;
9365 iverneed
->vn_filename
= NULL
;
9367 else if (hdr
== NULL
)
9368 goto error_return_bad_verref
;
9370 iverneed
->vn_filename
9371 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9373 if (iverneed
->vn_filename
== NULL
)
9374 goto error_return_bad_verref
;
9376 if (iverneed
->vn_cnt
== 0)
9377 iverneed
->vn_auxptr
= NULL
;
9380 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
9381 sizeof (Elf_Internal_Vernaux
), &amt
))
9383 bfd_set_error (bfd_error_file_too_big
);
9384 goto error_return_verref
;
9386 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
9387 bfd_alloc (abfd
, amt
);
9388 if (iverneed
->vn_auxptr
== NULL
)
9389 goto error_return_verref
;
9392 if (iverneed
->vn_aux
9393 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9394 goto error_return_bad_verref
;
9396 evernaux
= ((Elf_External_Vernaux
*)
9397 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
9398 ivernaux
= iverneed
->vn_auxptr
;
9399 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
9401 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
9403 if (elf_use_dt_symtab_p (abfd
))
9405 if (ivernaux
->vna_name
< elf_tdata (abfd
)->dt_strsz
)
9406 ivernaux
->vna_nodename
9407 = elf_tdata (abfd
)->dt_strtab
+ ivernaux
->vna_name
;
9409 ivernaux
->vna_nodename
= NULL
;
9411 else if (hdr
== NULL
)
9412 goto error_return_bad_verref
;
9414 ivernaux
->vna_nodename
9415 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9416 ivernaux
->vna_name
);
9417 if (ivernaux
->vna_nodename
== NULL
)
9418 goto error_return_bad_verref
;
9420 if (ivernaux
->vna_other
> freeidx
)
9421 freeidx
= ivernaux
->vna_other
;
9423 ivernaux
->vna_nextptr
= NULL
;
9424 if (ivernaux
->vna_next
== 0)
9426 iverneed
->vn_cnt
= j
+ 1;
9429 if (j
+ 1 < iverneed
->vn_cnt
)
9430 ivernaux
->vna_nextptr
= ivernaux
+ 1;
9432 if (ivernaux
->vna_next
9433 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
9434 goto error_return_bad_verref
;
9436 evernaux
= ((Elf_External_Vernaux
*)
9437 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
9440 iverneed
->vn_nextref
= NULL
;
9441 if (iverneed
->vn_next
== 0)
9443 if (hdr
!= NULL
&& (i
+ 1 < hdr
->sh_info
))
9444 iverneed
->vn_nextref
= iverneed
+ 1;
9446 if (iverneed
->vn_next
9447 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9448 goto error_return_bad_verref
;
9450 everneed
= ((Elf_External_Verneed
*)
9451 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
9453 elf_tdata (abfd
)->cverrefs
= i
;
9455 if (contents
!= elf_tdata (abfd
)->dt_verneed
)
9456 _bfd_munmap_temporary (contents_addr
, contents_size
);
9458 contents_addr
= NULL
;
9461 if (elf_dynverdef (abfd
) != 0 || elf_tdata (abfd
)->dt_verdef
!= NULL
)
9463 Elf_Internal_Shdr
*hdr
;
9464 Elf_External_Verdef
*everdef
;
9465 Elf_Internal_Verdef
*iverdef
;
9466 Elf_Internal_Verdef
*iverdefarr
;
9467 Elf_Internal_Verdef iverdefmem
;
9469 unsigned int maxidx
;
9470 bfd_byte
*contents_end_def
, *contents_end_aux
;
9471 size_t verdef_count
;
9474 if (elf_tdata (abfd
)->dt_verdef
!= NULL
)
9477 contents
= elf_tdata (abfd
)->dt_verdef
;
9478 verdef_count
= elf_tdata (abfd
)->dt_verdef_count
;
9479 verdef_size
= verdef_count
* sizeof (Elf_External_Verdef
);
9483 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
9485 if (hdr
->sh_size
< sizeof (Elf_External_Verdef
))
9487 error_return_bad_verdef
:
9489 (_("%pB: .gnu.version_d invalid entry"), abfd
);
9490 bfd_set_error (bfd_error_bad_value
);
9491 error_return_verdef
:
9492 elf_tdata (abfd
)->verdef
= NULL
;
9493 elf_tdata (abfd
)->cverdefs
= 0;
9497 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9498 goto error_return_verdef
;
9499 contents_size
= hdr
->sh_size
;
9500 contents
= _bfd_mmap_temporary (abfd
, contents_size
,
9501 &contents_addr
, &contents_size
);
9502 if (contents
== NULL
)
9503 goto error_return_verdef
;
9505 BFD_ASSERT (sizeof (Elf_External_Verdef
)
9506 >= sizeof (Elf_External_Verdaux
));
9508 verdef_count
= hdr
->sh_info
;
9509 verdef_size
= hdr
->sh_size
;
9512 contents_end_def
= (contents
+ verdef_size
9513 - sizeof (Elf_External_Verdef
));
9514 contents_end_aux
= (contents
+ verdef_size
9515 - sizeof (Elf_External_Verdaux
));
9517 /* We know the number of entries in the section but not the maximum
9518 index. Therefore we have to run through all entries and find
9520 everdef
= (Elf_External_Verdef
*) contents
;
9522 for (i
= 0; i
< verdef_count
; ++i
)
9524 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9526 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
9527 goto error_return_bad_verdef
;
9528 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
9529 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
9531 if (iverdefmem
.vd_next
== 0)
9534 if (iverdefmem
.vd_next
9535 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
9536 goto error_return_bad_verdef
;
9538 everdef
= ((Elf_External_Verdef
*)
9539 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
9542 if (default_imported_symver
)
9544 if (freeidx
> maxidx
)
9549 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9551 bfd_set_error (bfd_error_file_too_big
);
9552 goto error_return_verdef
;
9556 goto error_return_verdef
;
9557 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9558 if (elf_tdata (abfd
)->verdef
== NULL
)
9559 goto error_return_verdef
;
9561 elf_tdata (abfd
)->cverdefs
= maxidx
;
9563 everdef
= (Elf_External_Verdef
*) contents
;
9564 iverdefarr
= elf_tdata (abfd
)->verdef
;
9565 for (i
= 0; i
< verdef_count
; ++i
)
9567 Elf_External_Verdaux
*everdaux
;
9568 Elf_Internal_Verdaux
*iverdaux
;
9571 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9573 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
9574 goto error_return_bad_verdef
;
9576 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
9577 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
9579 iverdef
->vd_bfd
= abfd
;
9581 if (iverdef
->vd_cnt
== 0)
9582 iverdef
->vd_auxptr
= NULL
;
9585 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
9586 sizeof (Elf_Internal_Verdaux
), &amt
))
9588 bfd_set_error (bfd_error_file_too_big
);
9589 goto error_return_verdef
;
9591 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
9592 bfd_alloc (abfd
, amt
);
9593 if (iverdef
->vd_auxptr
== NULL
)
9594 goto error_return_verdef
;
9598 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
9599 goto error_return_bad_verdef
;
9601 everdaux
= ((Elf_External_Verdaux
*)
9602 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
9603 iverdaux
= iverdef
->vd_auxptr
;
9604 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
9606 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
9608 if (elf_use_dt_symtab_p (abfd
))
9610 if (iverdaux
->vda_name
< elf_tdata (abfd
)->dt_strsz
)
9611 iverdaux
->vda_nodename
9612 = elf_tdata (abfd
)->dt_strtab
+ iverdaux
->vda_name
;
9614 iverdaux
->vda_nodename
= NULL
;
9617 iverdaux
->vda_nodename
9618 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9619 iverdaux
->vda_name
);
9620 if (iverdaux
->vda_nodename
== NULL
)
9621 goto error_return_bad_verdef
;
9623 iverdaux
->vda_nextptr
= NULL
;
9624 if (iverdaux
->vda_next
== 0)
9626 iverdef
->vd_cnt
= j
+ 1;
9629 if (j
+ 1 < iverdef
->vd_cnt
)
9630 iverdaux
->vda_nextptr
= iverdaux
+ 1;
9632 if (iverdaux
->vda_next
9633 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
9634 goto error_return_bad_verdef
;
9636 everdaux
= ((Elf_External_Verdaux
*)
9637 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
9640 iverdef
->vd_nodename
= NULL
;
9641 if (iverdef
->vd_cnt
)
9642 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
9644 iverdef
->vd_nextdef
= NULL
;
9645 if (iverdef
->vd_next
== 0)
9647 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
9648 iverdef
->vd_nextdef
= iverdef
+ 1;
9650 everdef
= ((Elf_External_Verdef
*)
9651 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
9654 if (contents
!= elf_tdata (abfd
)->dt_verdef
)
9655 _bfd_munmap_temporary (contents_addr
, contents_size
);
9657 contents_addr
= NULL
;
9659 else if (default_imported_symver
)
9666 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9668 bfd_set_error (bfd_error_file_too_big
);
9673 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9674 if (elf_tdata (abfd
)->verdef
== NULL
)
9677 elf_tdata (abfd
)->cverdefs
= freeidx
;
9680 /* Create a default version based on the soname. */
9681 if (default_imported_symver
)
9683 Elf_Internal_Verdef
*iverdef
;
9684 Elf_Internal_Verdaux
*iverdaux
;
9686 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
9688 iverdef
->vd_version
= VER_DEF_CURRENT
;
9689 iverdef
->vd_flags
= 0;
9690 iverdef
->vd_ndx
= freeidx
;
9691 iverdef
->vd_cnt
= 1;
9693 iverdef
->vd_bfd
= abfd
;
9695 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
9696 if (iverdef
->vd_nodename
== NULL
)
9697 goto error_return_verdef
;
9698 iverdef
->vd_nextdef
= NULL
;
9699 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
9700 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
9701 if (iverdef
->vd_auxptr
== NULL
)
9702 goto error_return_verdef
;
9704 iverdaux
= iverdef
->vd_auxptr
;
9705 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9711 if (contents
!= elf_tdata (abfd
)->dt_verneed
9712 && contents
!= elf_tdata (abfd
)->dt_verdef
)
9713 _bfd_munmap_temporary (contents_addr
, contents_size
);
9718 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9720 elf_symbol_type
*newsym
;
9722 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9725 newsym
->symbol
.the_bfd
= abfd
;
9726 return &newsym
->symbol
;
9730 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9734 bfd_symbol_info (symbol
, ret
);
9737 /* Return whether a symbol name implies a local symbol. Most targets
9738 use this function for the is_local_label_name entry point, but some
9742 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9745 /* Normal local symbols start with ``.L''. */
9746 if (name
[0] == '.' && name
[1] == 'L')
9749 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9750 DWARF debugging symbols starting with ``..''. */
9751 if (name
[0] == '.' && name
[1] == '.')
9754 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9755 emitting DWARF debugging output. I suspect this is actually a
9756 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9757 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9758 underscore to be emitted on some ELF targets). For ease of use,
9759 we treat such symbols as local. */
9760 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9763 /* Treat assembler generated fake symbols, dollar local labels and
9764 forward-backward labels (aka local labels) as locals.
9765 These labels have the form:
9767 L0^A.* (fake symbols)
9769 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9771 Versions which start with .L will have already been matched above,
9772 so we only need to match the rest. */
9773 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9779 for (p
= name
+ 2; (c
= *p
); p
++)
9781 if (c
== 1 || c
== 2)
9783 if (c
== 1 && p
== name
+ 2)
9784 /* A fake symbol. */
9787 /* FIXME: We are being paranoid here and treating symbols like
9788 L0^Bfoo as if there were non-local, on the grounds that the
9789 assembler will never generate them. But can any symbol
9790 containing an ASCII value in the range 1-31 ever be anything
9791 other than some kind of local ? */
9808 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9809 asymbol
*symbol ATTRIBUTE_UNUSED
)
9816 _bfd_elf_set_arch_mach (bfd
*abfd
,
9817 enum bfd_architecture arch
,
9818 unsigned long machine
)
9820 /* If this isn't the right architecture for this backend, and this
9821 isn't the generic backend, fail. */
9822 if (arch
!= get_elf_backend_data (abfd
)->arch
9823 && arch
!= bfd_arch_unknown
9824 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9827 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9830 /* Find the nearest line to a particular section and offset,
9831 for error reporting. */
9834 _bfd_elf_find_nearest_line (bfd
*abfd
,
9838 const char **filename_ptr
,
9839 const char **functionname_ptr
,
9840 unsigned int *line_ptr
,
9841 unsigned int *discriminator_ptr
)
9843 return _bfd_elf_find_nearest_line_with_alt (abfd
, NULL
, symbols
, section
,
9844 offset
, filename_ptr
,
9845 functionname_ptr
, line_ptr
,
9849 /* Find the nearest line to a particular section and offset,
9850 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9851 can be optionally specified. */
9854 _bfd_elf_find_nearest_line_with_alt (bfd
*abfd
,
9855 const char *alt_filename
,
9859 const char **filename_ptr
,
9860 const char **functionname_ptr
,
9861 unsigned int *line_ptr
,
9862 unsigned int *discriminator_ptr
)
9866 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd
, alt_filename
, symbols
, NULL
,
9867 section
, offset
, filename_ptr
,
9868 functionname_ptr
, line_ptr
,
9870 dwarf_debug_sections
,
9871 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9874 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9875 filename_ptr
, functionname_ptr
, line_ptr
))
9877 if (!*functionname_ptr
)
9878 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9879 *filename_ptr
? NULL
: filename_ptr
,
9884 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9885 &found
, filename_ptr
,
9886 functionname_ptr
, line_ptr
,
9887 &elf_tdata (abfd
)->line_info
))
9889 if (found
&& (*functionname_ptr
|| *line_ptr
))
9892 if (symbols
== NULL
)
9895 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9896 filename_ptr
, functionname_ptr
))
9903 /* Find the line for a symbol. */
9906 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9907 const char **filename_ptr
, unsigned int *line_ptr
)
9909 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9910 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9911 filename_ptr
, NULL
, line_ptr
, NULL
,
9912 dwarf_debug_sections
,
9913 &tdata
->dwarf2_find_line_info
);
9916 /* After a call to bfd_find_nearest_line, successive calls to
9917 bfd_find_inliner_info can be used to get source information about
9918 each level of function inlining that terminated at the address
9919 passed to bfd_find_nearest_line. Currently this is only supported
9920 for DWARF2 with appropriate DWARF3 extensions. */
9923 _bfd_elf_find_inliner_info (bfd
*abfd
,
9924 const char **filename_ptr
,
9925 const char **functionname_ptr
,
9926 unsigned int *line_ptr
)
9928 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9929 return _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9930 functionname_ptr
, line_ptr
,
9931 &tdata
->dwarf2_find_line_info
);
9935 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9937 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9938 int ret
= bed
->s
->sizeof_ehdr
;
9940 if (!bfd_link_relocatable (info
))
9942 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9944 if (phdr_size
== (bfd_size_type
) -1)
9946 struct elf_segment_map
*m
;
9949 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9950 phdr_size
+= bed
->s
->sizeof_phdr
;
9953 phdr_size
= get_program_header_size (abfd
, info
);
9956 elf_program_header_size (abfd
) = phdr_size
;
9964 _bfd_elf_set_section_contents (bfd
*abfd
,
9966 const void *location
,
9968 bfd_size_type count
)
9970 Elf_Internal_Shdr
*hdr
;
9972 if (! abfd
->output_has_begun
9973 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9979 hdr
= &elf_section_data (section
)->this_hdr
;
9980 if (hdr
->sh_offset
== (file_ptr
) -1)
9982 unsigned char *contents
;
9984 if (bfd_section_is_ctf (section
))
9985 /* Nothing to do with this section: the contents are generated
9989 if ((offset
+ count
) > hdr
->sh_size
)
9992 (_("%pB:%pA: error: attempting to write"
9993 " over the end of the section"),
9996 bfd_set_error (bfd_error_invalid_operation
);
10000 contents
= hdr
->contents
;
10001 if (contents
== NULL
)
10004 (_("%pB:%pA: error: attempting to write"
10005 " section into an empty buffer"),
10008 bfd_set_error (bfd_error_invalid_operation
);
10012 memcpy (contents
+ offset
, location
, count
);
10016 return _bfd_generic_set_section_contents (abfd
, section
,
10017 location
, offset
, count
);
10021 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
10022 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
10023 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
10029 /* Try to convert a non-ELF reloc into an ELF one. */
10032 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
10034 /* Check whether we really have an ELF howto. */
10036 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
10038 bfd_reloc_code_real_type code
;
10039 reloc_howto_type
*howto
;
10041 /* Alien reloc: Try to determine its type to replace it with an
10042 equivalent ELF reloc. */
10044 if (areloc
->howto
->pc_relative
)
10046 switch (areloc
->howto
->bitsize
)
10049 code
= BFD_RELOC_8_PCREL
;
10052 code
= BFD_RELOC_12_PCREL
;
10055 code
= BFD_RELOC_16_PCREL
;
10058 code
= BFD_RELOC_24_PCREL
;
10061 code
= BFD_RELOC_32_PCREL
;
10064 code
= BFD_RELOC_64_PCREL
;
10070 howto
= bfd_reloc_type_lookup (abfd
, code
);
10072 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
10074 if (howto
->pcrel_offset
)
10075 areloc
->addend
+= areloc
->address
;
10077 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
10082 switch (areloc
->howto
->bitsize
)
10085 code
= BFD_RELOC_8
;
10088 code
= BFD_RELOC_14
;
10091 code
= BFD_RELOC_16
;
10094 code
= BFD_RELOC_26
;
10097 code
= BFD_RELOC_32
;
10100 code
= BFD_RELOC_64
;
10106 howto
= bfd_reloc_type_lookup (abfd
, code
);
10110 areloc
->howto
= howto
;
10118 /* xgettext:c-format */
10119 _bfd_error_handler (_("%pB: %s unsupported"),
10120 abfd
, areloc
->howto
->name
);
10121 bfd_set_error (bfd_error_sorry
);
10126 _bfd_elf_free_cached_info (bfd
*abfd
)
10128 struct elf_obj_tdata
*tdata
;
10130 if ((bfd_get_format (abfd
) == bfd_object
10131 || bfd_get_format (abfd
) == bfd_core
)
10132 && (tdata
= elf_tdata (abfd
)) != NULL
)
10134 if (tdata
->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
10135 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
10136 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
10137 _bfd_dwarf1_cleanup_debug_info (abfd
, &tdata
->dwarf1_find_line_info
);
10138 _bfd_stab_cleanup (abfd
, &tdata
->line_info
);
10139 for (asection
*sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
10141 _bfd_elf_munmap_section_contents (sec
, sec
->contents
);
10144 free (elf_section_data (sec
)->this_hdr
.contents
);
10145 elf_section_data (sec
)->this_hdr
.contents
= NULL
;
10147 free (elf_section_data (sec
)->relocs
);
10148 elf_section_data (sec
)->relocs
= NULL
;
10149 if (sec
->sec_info_type
== SEC_INFO_TYPE_EH_FRAME
)
10151 struct eh_frame_sec_info
*sec_info
10152 = elf_section_data (sec
)->sec_info
;
10153 free (sec_info
->cies
);
10156 free (tdata
->symtab_hdr
.contents
);
10157 tdata
->symtab_hdr
.contents
= NULL
;
10160 return _bfd_generic_bfd_free_cached_info (abfd
);
10163 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10164 in the relocation's offset. Thus we cannot allow any sort of sanity
10165 range-checking to interfere. There is nothing else to do in processing
10168 bfd_reloc_status_type
10169 _bfd_elf_rel_vtable_reloc_fn
10170 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
10171 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
10172 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
10173 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
10175 return bfd_reloc_ok
;
10178 /* Elf core file support. Much of this only works on native
10179 toolchains, since we rely on knowing the
10180 machine-dependent procfs structure in order to pick
10181 out details about the corefile. */
10183 #ifdef HAVE_SYS_PROCFS_H
10184 # include <sys/procfs.h>
10187 /* Return a PID that identifies a "thread" for threaded cores, or the
10188 PID of the main process for non-threaded cores. */
10191 elfcore_make_pid (bfd
*abfd
)
10195 pid
= elf_tdata (abfd
)->core
->lwpid
;
10197 pid
= elf_tdata (abfd
)->core
->pid
;
10202 /* If there isn't a section called NAME, make one, using data from
10203 SECT. Note, this function will generate a reference to NAME, so
10204 you shouldn't deallocate or overwrite it. */
10207 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
10211 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
10214 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
10218 sect2
->size
= sect
->size
;
10219 sect2
->filepos
= sect
->filepos
;
10220 sect2
->alignment_power
= sect
->alignment_power
;
10224 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10225 actually creates up to two pseudosections:
10226 - For the single-threaded case, a section named NAME, unless
10227 such a section already exists.
10228 - For the multi-threaded case, a section named "NAME/PID", where
10229 PID is elfcore_make_pid (abfd).
10230 Both pseudosections have identical contents. */
10232 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
10238 char *threaded_name
;
10242 /* Build the section name. */
10244 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
10245 len
= strlen (buf
) + 1;
10246 threaded_name
= (char *) bfd_alloc (abfd
, len
);
10247 if (threaded_name
== NULL
)
10249 memcpy (threaded_name
, buf
, len
);
10251 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
10256 sect
->filepos
= filepos
;
10257 sect
->alignment_power
= 2;
10259 return elfcore_maybe_make_sect (abfd
, name
, sect
);
10263 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
10266 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10272 sect
->size
= note
->descsz
- offs
;
10273 sect
->filepos
= note
->descpos
+ offs
;
10274 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10279 /* prstatus_t exists on:
10281 linux 2.[01] + glibc
10285 #if defined (HAVE_PRSTATUS_T)
10288 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10293 if (note
->descsz
== sizeof (prstatus_t
))
10297 size
= sizeof (prstat
.pr_reg
);
10298 offset
= offsetof (prstatus_t
, pr_reg
);
10299 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
10301 /* Do not overwrite the core signal if it
10302 has already been set by another thread. */
10303 if (elf_tdata (abfd
)->core
->signal
== 0)
10304 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
10305 if (elf_tdata (abfd
)->core
->pid
== 0)
10306 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
10308 /* pr_who exists on:
10311 pr_who doesn't exist on:
10314 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10315 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
10317 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
10320 #if defined (HAVE_PRSTATUS32_T)
10321 else if (note
->descsz
== sizeof (prstatus32_t
))
10323 /* 64-bit host, 32-bit corefile */
10324 prstatus32_t prstat
;
10326 size
= sizeof (prstat
.pr_reg
);
10327 offset
= offsetof (prstatus32_t
, pr_reg
);
10328 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
10330 /* Do not overwrite the core signal if it
10331 has already been set by another thread. */
10332 if (elf_tdata (abfd
)->core
->signal
== 0)
10333 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
10334 if (elf_tdata (abfd
)->core
->pid
== 0)
10335 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
10337 /* pr_who exists on:
10340 pr_who doesn't exist on:
10343 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10344 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
10346 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
10349 #endif /* HAVE_PRSTATUS32_T */
10352 /* Fail - we don't know how to handle any other
10353 note size (ie. data object type). */
10357 /* Make a ".reg/999" section and a ".reg" section. */
10358 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10359 size
, note
->descpos
+ offset
);
10361 #endif /* defined (HAVE_PRSTATUS_T) */
10363 /* Create a pseudosection containing the exact contents of NOTE. */
10365 elfcore_make_note_pseudosection (bfd
*abfd
,
10367 Elf_Internal_Note
*note
)
10369 return _bfd_elfcore_make_pseudosection (abfd
, name
,
10370 note
->descsz
, note
->descpos
);
10373 /* There isn't a consistent prfpregset_t across platforms,
10374 but it doesn't matter, because we don't have to pick this
10375 data structure apart. */
10378 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10380 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10383 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10384 type of NT_PRXFPREG. Just include the whole note's contents
10388 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10390 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10393 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10394 with a note type of NT_X86_XSTATE. Just include the whole note's
10395 contents literally. */
10398 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
10400 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
10404 elfcore_grok_sspreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10406 return elfcore_make_note_pseudosection (abfd
, ".reg-ssp", note
);
10410 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10412 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
10416 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10418 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
10422 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
10424 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
10428 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10430 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
10434 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10436 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
10440 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
10442 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
10446 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
10448 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
10452 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10454 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
10458 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10460 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
10464 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10466 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
10470 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10472 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
10476 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
10478 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
10482 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
10484 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
10488 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10490 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
10494 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10496 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
10500 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
10502 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
10506 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
10508 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
10512 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
10514 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
10518 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10520 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
10524 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
10526 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
10530 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
10532 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
10536 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10538 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
10542 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
10544 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
10548 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
10550 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
10554 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
10556 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
10560 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
10562 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
10566 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
10568 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
10572 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
10574 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
10578 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
10580 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
10584 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
10586 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
10590 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10592 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
10596 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
10598 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
10602 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
10604 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
10608 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
10610 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
10614 elfcore_grok_aarch_mte (bfd
*abfd
, Elf_Internal_Note
*note
)
10616 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-mte",
10621 elfcore_grok_aarch_ssve (bfd
*abfd
, Elf_Internal_Note
*note
)
10623 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-ssve", note
);
10627 elfcore_grok_aarch_za (bfd
*abfd
, Elf_Internal_Note
*note
)
10629 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-za", note
);
10632 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10633 successful, otherwise return FALSE. */
10636 elfcore_grok_aarch_zt (bfd
*abfd
, Elf_Internal_Note
*note
)
10638 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-zt", note
);
10641 /* Convert NOTE into a bfd_section called ".reg-aarch-gcs". Return TRUE if
10642 successful, otherwise return FALSE. */
10645 elfcore_grok_aarch_gcs (bfd
*abfd
, Elf_Internal_Note
*note
)
10647 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-gcs", note
);
10651 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
10653 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
10656 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10657 successful otherwise, return FALSE. */
10660 elfcore_grok_riscv_csr (bfd
*abfd
, Elf_Internal_Note
*note
)
10662 return elfcore_make_note_pseudosection (abfd
, ".reg-riscv-csr", note
);
10665 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10666 successful otherwise, return FALSE. */
10669 elfcore_grok_gdb_tdesc (bfd
*abfd
, Elf_Internal_Note
*note
)
10671 return elfcore_make_note_pseudosection (abfd
, ".gdb-tdesc", note
);
10675 elfcore_grok_loongarch_cpucfg (bfd
*abfd
, Elf_Internal_Note
*note
)
10677 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-cpucfg", note
);
10681 elfcore_grok_loongarch_lbt (bfd
*abfd
, Elf_Internal_Note
*note
)
10683 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lbt", note
);
10687 elfcore_grok_loongarch_lsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10689 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lsx", note
);
10693 elfcore_grok_loongarch_lasx (bfd
*abfd
, Elf_Internal_Note
*note
)
10695 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lasx", note
);
10698 #if defined (HAVE_PRPSINFO_T)
10699 typedef prpsinfo_t elfcore_psinfo_t
;
10700 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10701 typedef prpsinfo32_t elfcore_psinfo32_t
;
10705 #if defined (HAVE_PSINFO_T)
10706 typedef psinfo_t elfcore_psinfo_t
;
10707 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10708 typedef psinfo32_t elfcore_psinfo32_t
;
10712 /* return a malloc'ed copy of a string at START which is at
10713 most MAX bytes long, possibly without a terminating '\0'.
10714 the copy will always have a terminating '\0'. */
10717 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
10720 char *end
= (char *) memchr (start
, '\0', max
);
10728 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
10732 memcpy (dups
, start
, len
);
10738 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10740 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10742 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
10744 elfcore_psinfo_t psinfo
;
10746 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10748 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10749 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10751 elf_tdata (abfd
)->core
->program
10752 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10753 sizeof (psinfo
.pr_fname
));
10755 elf_tdata (abfd
)->core
->command
10756 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10757 sizeof (psinfo
.pr_psargs
));
10759 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10760 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
10762 /* 64-bit host, 32-bit corefile */
10763 elfcore_psinfo32_t psinfo
;
10765 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10767 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10768 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10770 elf_tdata (abfd
)->core
->program
10771 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10772 sizeof (psinfo
.pr_fname
));
10774 elf_tdata (abfd
)->core
->command
10775 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10776 sizeof (psinfo
.pr_psargs
));
10782 /* Fail - we don't know how to handle any other
10783 note size (ie. data object type). */
10787 /* Note that for some reason, a spurious space is tacked
10788 onto the end of the args in some (at least one anyway)
10789 implementations, so strip it off if it exists. */
10792 char *command
= elf_tdata (abfd
)->core
->command
;
10793 int n
= strlen (command
);
10795 if (0 < n
&& command
[n
- 1] == ' ')
10796 command
[n
- 1] = '\0';
10801 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10803 #if defined (HAVE_PSTATUS_T)
10805 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10807 if (note
->descsz
== sizeof (pstatus_t
)
10808 #if defined (HAVE_PXSTATUS_T)
10809 || note
->descsz
== sizeof (pxstatus_t
)
10815 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10817 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10819 #if defined (HAVE_PSTATUS32_T)
10820 else if (note
->descsz
== sizeof (pstatus32_t
))
10822 /* 64-bit host, 32-bit corefile */
10825 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10827 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10830 /* Could grab some more details from the "representative"
10831 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10832 NT_LWPSTATUS note, presumably. */
10836 #endif /* defined (HAVE_PSTATUS_T) */
10838 #if defined (HAVE_LWPSTATUS_T)
10840 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10842 lwpstatus_t lwpstat
;
10848 if (note
->descsz
!= sizeof (lwpstat
)
10849 #if defined (HAVE_LWPXSTATUS_T)
10850 && note
->descsz
!= sizeof (lwpxstatus_t
)
10855 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10857 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10858 /* Do not overwrite the core signal if it has already been set by
10860 if (elf_tdata (abfd
)->core
->signal
== 0)
10861 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10863 /* Make a ".reg/999" section. */
10865 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10866 len
= strlen (buf
) + 1;
10867 name
= bfd_alloc (abfd
, len
);
10870 memcpy (name
, buf
, len
);
10872 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10876 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10877 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10878 sect
->filepos
= note
->descpos
10879 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10882 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10883 sect
->size
= sizeof (lwpstat
.pr_reg
);
10884 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10887 sect
->alignment_power
= 2;
10889 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10892 /* Make a ".reg2/999" section */
10894 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10895 len
= strlen (buf
) + 1;
10896 name
= bfd_alloc (abfd
, len
);
10899 memcpy (name
, buf
, len
);
10901 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10905 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10906 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10907 sect
->filepos
= note
->descpos
10908 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10911 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10912 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10913 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10916 sect
->alignment_power
= 2;
10918 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10920 #endif /* defined (HAVE_LWPSTATUS_T) */
10922 /* These constants, and the structure offsets used below, are defined by
10923 Cygwin's core_dump.h */
10924 #define NOTE_INFO_PROCESS 1
10925 #define NOTE_INFO_THREAD 2
10926 #define NOTE_INFO_MODULE 3
10927 #define NOTE_INFO_MODULE64 4
10930 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10935 unsigned int name_size
;
10938 int is_active_thread
;
10941 if (note
->descsz
< 4)
10944 if (! startswith (note
->namedata
, "win32"))
10947 type
= bfd_get_32 (abfd
, note
->descdata
);
10951 const char *type_name
;
10952 unsigned long min_size
;
10955 { "NOTE_INFO_PROCESS", 12 },
10956 { "NOTE_INFO_THREAD", 12 },
10957 { "NOTE_INFO_MODULE", 12 },
10958 { "NOTE_INFO_MODULE64", 16 },
10961 if (type
== 0 || type
> (sizeof(size_check
)/sizeof(size_check
[0])))
10964 if (note
->descsz
< size_check
[type
- 1].min_size
)
10966 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10968 abfd
, size_check
[type
- 1].type_name
, note
->descsz
);
10974 case NOTE_INFO_PROCESS
:
10975 /* FIXME: need to add ->core->command. */
10976 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10977 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10980 case NOTE_INFO_THREAD
:
10981 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10983 /* thread_info.tid */
10984 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 4));
10986 len
= strlen (buf
) + 1;
10987 name
= (char *) bfd_alloc (abfd
, len
);
10991 memcpy (name
, buf
, len
);
10993 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10997 /* sizeof (thread_info.thread_context) */
10998 sect
->size
= note
->descsz
- 12;
10999 /* offsetof (thread_info.thread_context) */
11000 sect
->filepos
= note
->descpos
+ 12;
11001 sect
->alignment_power
= 2;
11003 /* thread_info.is_active_thread */
11004 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11006 if (is_active_thread
)
11007 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
11011 case NOTE_INFO_MODULE
:
11012 case NOTE_INFO_MODULE64
:
11013 /* Make a ".module/xxxxxxxx" section. */
11014 if (type
== NOTE_INFO_MODULE
)
11016 /* module_info.base_address */
11017 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
11018 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
11019 /* module_info.module_name_size */
11020 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11022 else /* NOTE_INFO_MODULE64 */
11024 /* module_info.base_address */
11025 base_addr
= bfd_get_64 (abfd
, note
->descdata
+ 4);
11026 sprintf (buf
, ".module/%016lx", (unsigned long) base_addr
);
11027 /* module_info.module_name_size */
11028 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 12);
11031 len
= strlen (buf
) + 1;
11032 name
= (char *) bfd_alloc (abfd
, len
);
11036 memcpy (name
, buf
, len
);
11038 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11043 if (note
->descsz
< 12 + name_size
)
11045 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11046 " is too small to contain a name of size %u"),
11047 abfd
, note
->descsz
, name_size
);
11051 sect
->size
= note
->descsz
;
11052 sect
->filepos
= note
->descpos
;
11053 sect
->alignment_power
= 2;
11064 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11066 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11068 switch (note
->type
)
11074 if (bed
->elf_backend_grok_prstatus
)
11075 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
11077 #if defined (HAVE_PRSTATUS_T)
11078 return elfcore_grok_prstatus (abfd
, note
);
11083 #if defined (HAVE_PSTATUS_T)
11085 return elfcore_grok_pstatus (abfd
, note
);
11088 #if defined (HAVE_LWPSTATUS_T)
11090 return elfcore_grok_lwpstatus (abfd
, note
);
11093 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
11094 return elfcore_grok_prfpreg (abfd
, note
);
11096 case NT_WIN32PSTATUS
:
11097 return elfcore_grok_win32pstatus (abfd
, note
);
11099 case NT_PRXFPREG
: /* Linux SSE extension */
11100 if (note
->namesz
== 6
11101 && strcmp (note
->namedata
, "LINUX") == 0)
11102 return elfcore_grok_prxfpreg (abfd
, note
);
11106 case NT_X86_XSTATE
: /* Linux XSAVE extension */
11107 if (note
->namesz
== 6
11108 && strcmp (note
->namedata
, "LINUX") == 0)
11109 return elfcore_grok_xstatereg (abfd
, note
);
11113 case NT_X86_SHSTK
: /* Linux CET extension. */
11114 if (note
->namesz
== 6
11115 && strcmp (note
->namedata
, "LINUX") == 0)
11116 return elfcore_grok_sspreg (abfd
, note
);
11121 if (note
->namesz
== 6
11122 && strcmp (note
->namedata
, "LINUX") == 0)
11123 return elfcore_grok_ppc_vmx (abfd
, note
);
11128 if (note
->namesz
== 6
11129 && strcmp (note
->namedata
, "LINUX") == 0)
11130 return elfcore_grok_ppc_vsx (abfd
, note
);
11135 if (note
->namesz
== 6
11136 && strcmp (note
->namedata
, "LINUX") == 0)
11137 return elfcore_grok_ppc_tar (abfd
, note
);
11142 if (note
->namesz
== 6
11143 && strcmp (note
->namedata
, "LINUX") == 0)
11144 return elfcore_grok_ppc_ppr (abfd
, note
);
11149 if (note
->namesz
== 6
11150 && strcmp (note
->namedata
, "LINUX") == 0)
11151 return elfcore_grok_ppc_dscr (abfd
, note
);
11156 if (note
->namesz
== 6
11157 && strcmp (note
->namedata
, "LINUX") == 0)
11158 return elfcore_grok_ppc_ebb (abfd
, note
);
11163 if (note
->namesz
== 6
11164 && strcmp (note
->namedata
, "LINUX") == 0)
11165 return elfcore_grok_ppc_pmu (abfd
, note
);
11169 case NT_PPC_TM_CGPR
:
11170 if (note
->namesz
== 6
11171 && strcmp (note
->namedata
, "LINUX") == 0)
11172 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
11176 case NT_PPC_TM_CFPR
:
11177 if (note
->namesz
== 6
11178 && strcmp (note
->namedata
, "LINUX") == 0)
11179 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
11183 case NT_PPC_TM_CVMX
:
11184 if (note
->namesz
== 6
11185 && strcmp (note
->namedata
, "LINUX") == 0)
11186 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
11190 case NT_PPC_TM_CVSX
:
11191 if (note
->namesz
== 6
11192 && strcmp (note
->namedata
, "LINUX") == 0)
11193 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
11197 case NT_PPC_TM_SPR
:
11198 if (note
->namesz
== 6
11199 && strcmp (note
->namedata
, "LINUX") == 0)
11200 return elfcore_grok_ppc_tm_spr (abfd
, note
);
11204 case NT_PPC_TM_CTAR
:
11205 if (note
->namesz
== 6
11206 && strcmp (note
->namedata
, "LINUX") == 0)
11207 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
11211 case NT_PPC_TM_CPPR
:
11212 if (note
->namesz
== 6
11213 && strcmp (note
->namedata
, "LINUX") == 0)
11214 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
11218 case NT_PPC_TM_CDSCR
:
11219 if (note
->namesz
== 6
11220 && strcmp (note
->namedata
, "LINUX") == 0)
11221 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
11225 case NT_S390_HIGH_GPRS
:
11226 if (note
->namesz
== 6
11227 && strcmp (note
->namedata
, "LINUX") == 0)
11228 return elfcore_grok_s390_high_gprs (abfd
, note
);
11232 case NT_S390_TIMER
:
11233 if (note
->namesz
== 6
11234 && strcmp (note
->namedata
, "LINUX") == 0)
11235 return elfcore_grok_s390_timer (abfd
, note
);
11239 case NT_S390_TODCMP
:
11240 if (note
->namesz
== 6
11241 && strcmp (note
->namedata
, "LINUX") == 0)
11242 return elfcore_grok_s390_todcmp (abfd
, note
);
11246 case NT_S390_TODPREG
:
11247 if (note
->namesz
== 6
11248 && strcmp (note
->namedata
, "LINUX") == 0)
11249 return elfcore_grok_s390_todpreg (abfd
, note
);
11254 if (note
->namesz
== 6
11255 && strcmp (note
->namedata
, "LINUX") == 0)
11256 return elfcore_grok_s390_ctrs (abfd
, note
);
11260 case NT_S390_PREFIX
:
11261 if (note
->namesz
== 6
11262 && strcmp (note
->namedata
, "LINUX") == 0)
11263 return elfcore_grok_s390_prefix (abfd
, note
);
11267 case NT_S390_LAST_BREAK
:
11268 if (note
->namesz
== 6
11269 && strcmp (note
->namedata
, "LINUX") == 0)
11270 return elfcore_grok_s390_last_break (abfd
, note
);
11274 case NT_S390_SYSTEM_CALL
:
11275 if (note
->namesz
== 6
11276 && strcmp (note
->namedata
, "LINUX") == 0)
11277 return elfcore_grok_s390_system_call (abfd
, note
);
11282 if (note
->namesz
== 6
11283 && strcmp (note
->namedata
, "LINUX") == 0)
11284 return elfcore_grok_s390_tdb (abfd
, note
);
11288 case NT_S390_VXRS_LOW
:
11289 if (note
->namesz
== 6
11290 && strcmp (note
->namedata
, "LINUX") == 0)
11291 return elfcore_grok_s390_vxrs_low (abfd
, note
);
11295 case NT_S390_VXRS_HIGH
:
11296 if (note
->namesz
== 6
11297 && strcmp (note
->namedata
, "LINUX") == 0)
11298 return elfcore_grok_s390_vxrs_high (abfd
, note
);
11302 case NT_S390_GS_CB
:
11303 if (note
->namesz
== 6
11304 && strcmp (note
->namedata
, "LINUX") == 0)
11305 return elfcore_grok_s390_gs_cb (abfd
, note
);
11309 case NT_S390_GS_BC
:
11310 if (note
->namesz
== 6
11311 && strcmp (note
->namedata
, "LINUX") == 0)
11312 return elfcore_grok_s390_gs_bc (abfd
, note
);
11317 if (note
->namesz
== 6
11318 && strcmp (note
->namedata
, "LINUX") == 0)
11319 return elfcore_grok_arc_v2 (abfd
, note
);
11324 if (note
->namesz
== 6
11325 && strcmp (note
->namedata
, "LINUX") == 0)
11326 return elfcore_grok_arm_vfp (abfd
, note
);
11331 if (note
->namesz
== 6
11332 && strcmp (note
->namedata
, "LINUX") == 0)
11333 return elfcore_grok_aarch_tls (abfd
, note
);
11337 case NT_ARM_HW_BREAK
:
11338 if (note
->namesz
== 6
11339 && strcmp (note
->namedata
, "LINUX") == 0)
11340 return elfcore_grok_aarch_hw_break (abfd
, note
);
11344 case NT_ARM_HW_WATCH
:
11345 if (note
->namesz
== 6
11346 && strcmp (note
->namedata
, "LINUX") == 0)
11347 return elfcore_grok_aarch_hw_watch (abfd
, note
);
11352 if (note
->namesz
== 6
11353 && strcmp (note
->namedata
, "LINUX") == 0)
11354 return elfcore_grok_aarch_sve (abfd
, note
);
11358 case NT_ARM_PAC_MASK
:
11359 if (note
->namesz
== 6
11360 && strcmp (note
->namedata
, "LINUX") == 0)
11361 return elfcore_grok_aarch_pauth (abfd
, note
);
11365 case NT_ARM_TAGGED_ADDR_CTRL
:
11366 if (note
->namesz
== 6
11367 && strcmp (note
->namedata
, "LINUX") == 0)
11368 return elfcore_grok_aarch_mte (abfd
, note
);
11373 if (note
->namesz
== 6
11374 && strcmp (note
->namedata
, "LINUX") == 0)
11375 return elfcore_grok_aarch_ssve (abfd
, note
);
11380 if (note
->namesz
== 6
11381 && strcmp (note
->namedata
, "LINUX") == 0)
11382 return elfcore_grok_aarch_za (abfd
, note
);
11387 if (note
->namesz
== 6
11388 && strcmp (note
->namedata
, "LINUX") == 0)
11389 return elfcore_grok_aarch_zt (abfd
, note
);
11394 if (note
->namesz
== 6 && strcmp (note
->namedata
, "LINUX") == 0)
11395 return elfcore_grok_aarch_gcs (abfd
, note
);
11400 if (note
->namesz
== 4
11401 && strcmp (note
->namedata
, "GDB") == 0)
11402 return elfcore_grok_gdb_tdesc (abfd
, note
);
11407 if (note
->namesz
== 4
11408 && strcmp (note
->namedata
, "GDB") == 0)
11409 return elfcore_grok_riscv_csr (abfd
, note
);
11413 case NT_LARCH_CPUCFG
:
11414 if (note
->namesz
== 6
11415 && strcmp (note
->namedata
, "LINUX") == 0)
11416 return elfcore_grok_loongarch_cpucfg (abfd
, note
);
11421 if (note
->namesz
== 6
11422 && strcmp (note
->namedata
, "LINUX") == 0)
11423 return elfcore_grok_loongarch_lbt (abfd
, note
);
11428 if (note
->namesz
== 6
11429 && strcmp (note
->namedata
, "LINUX") == 0)
11430 return elfcore_grok_loongarch_lsx (abfd
, note
);
11434 case NT_LARCH_LASX
:
11435 if (note
->namesz
== 6
11436 && strcmp (note
->namedata
, "LINUX") == 0)
11437 return elfcore_grok_loongarch_lasx (abfd
, note
);
11443 if (bed
->elf_backend_grok_psinfo
)
11444 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
11446 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11447 return elfcore_grok_psinfo (abfd
, note
);
11453 return elfcore_make_auxv_note_section (abfd
, note
, 0);
11456 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
11460 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
11467 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
11469 struct bfd_build_id
* build_id
;
11471 if (note
->descsz
== 0)
11474 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
11475 if (build_id
== NULL
)
11478 build_id
->size
= note
->descsz
;
11479 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
11480 abfd
->build_id
= build_id
;
11486 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11488 switch (note
->type
)
11493 case NT_GNU_PROPERTY_TYPE_0
:
11494 return _bfd_elf_parse_gnu_properties (abfd
, note
);
11496 case NT_GNU_BUILD_ID
:
11497 return elfobj_grok_gnu_build_id (abfd
, note
);
11502 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
11504 struct sdt_note
*cur
=
11505 (struct sdt_note
*) bfd_alloc (abfd
,
11506 sizeof (struct sdt_note
) + note
->descsz
);
11508 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
11509 cur
->size
= (bfd_size_type
) note
->descsz
;
11510 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
11512 elf_tdata (abfd
)->sdt_note_head
= cur
;
11518 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11520 switch (note
->type
)
11523 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
11531 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11535 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11538 if (note
->descsz
< 108)
11543 if (note
->descsz
< 120)
11551 /* Check for version 1 in pr_version. */
11552 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11557 /* Skip over pr_psinfosz. */
11558 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11562 offset
+= 4; /* Padding before pr_psinfosz. */
11566 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11567 elf_tdata (abfd
)->core
->program
11568 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
11571 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11572 elf_tdata (abfd
)->core
->command
11573 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
11576 /* Padding before pr_pid. */
11579 /* The pr_pid field was added in version "1a". */
11580 if (note
->descsz
< offset
+ 4)
11583 elf_tdata (abfd
)->core
->pid
11584 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11590 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
11596 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11597 Also compute minimum size of this note. */
11598 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11602 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
11606 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
11607 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
11614 if (note
->descsz
< min_size
)
11617 /* Check for version 1 in pr_version. */
11618 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11621 /* Extract size of pr_reg from pr_gregsetsz. */
11622 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11623 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11625 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11630 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11634 /* Skip over pr_osreldate. */
11637 /* Read signal from pr_cursig. */
11638 if (elf_tdata (abfd
)->core
->signal
== 0)
11639 elf_tdata (abfd
)->core
->signal
11640 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11643 /* Read TID from pr_pid. */
11644 elf_tdata (abfd
)->core
->lwpid
11645 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11648 /* Padding before pr_reg. */
11649 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
11652 /* Make sure that there is enough data remaining in the note. */
11653 if ((note
->descsz
- offset
) < size
)
11656 /* Make a ".reg/999" section and a ".reg" section. */
11657 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
11658 size
, note
->descpos
+ offset
);
11662 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11664 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11666 switch (note
->type
)
11669 if (bed
->elf_backend_grok_freebsd_prstatus
)
11670 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
11672 return elfcore_grok_freebsd_prstatus (abfd
, note
);
11675 return elfcore_grok_prfpreg (abfd
, note
);
11678 return elfcore_grok_freebsd_psinfo (abfd
, note
);
11680 case NT_FREEBSD_THRMISC
:
11681 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
11683 case NT_FREEBSD_PROCSTAT_PROC
:
11684 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
11687 case NT_FREEBSD_PROCSTAT_FILES
:
11688 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
11691 case NT_FREEBSD_PROCSTAT_VMMAP
:
11692 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
11695 case NT_FREEBSD_PROCSTAT_AUXV
:
11696 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11698 case NT_FREEBSD_X86_SEGBASES
:
11699 return elfcore_make_note_pseudosection (abfd
, ".reg-x86-segbases", note
);
11701 case NT_X86_XSTATE
:
11702 return elfcore_grok_xstatereg (abfd
, note
);
11704 case NT_FREEBSD_PTLWPINFO
:
11705 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
11709 return elfcore_grok_aarch_tls (abfd
, note
);
11712 return elfcore_grok_arm_vfp (abfd
, note
);
11720 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
11724 cp
= strchr (note
->namedata
, '@');
11727 *lwpidp
= atoi(cp
+ 1);
11734 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11736 if (note
->descsz
<= 0x7c + 31)
11739 /* Signal number at offset 0x08. */
11740 elf_tdata (abfd
)->core
->signal
11741 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11743 /* Process ID at offset 0x50. */
11744 elf_tdata (abfd
)->core
->pid
11745 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
11747 /* Command name at 0x7c (max 32 bytes, including nul). */
11748 elf_tdata (abfd
)->core
->command
11749 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
11751 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
11756 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11760 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
11761 elf_tdata (abfd
)->core
->lwpid
= lwp
;
11763 switch (note
->type
)
11765 case NT_NETBSDCORE_PROCINFO
:
11766 /* NetBSD-specific core "procinfo". Note that we expect to
11767 find this note before any of the others, which is fine,
11768 since the kernel writes this note out first when it
11769 creates a core file. */
11770 return elfcore_grok_netbsd_procinfo (abfd
, note
);
11771 case NT_NETBSDCORE_AUXV
:
11772 /* NetBSD-specific Elf Auxiliary Vector data. */
11773 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11774 case NT_NETBSDCORE_LWPSTATUS
:
11775 return elfcore_make_note_pseudosection (abfd
,
11776 ".note.netbsdcore.lwpstatus",
11782 /* As of March 2020 there are no other machine-independent notes
11783 defined for NetBSD core files. If the note type is less
11784 than the start of the machine-dependent note types, we don't
11787 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
11791 switch (bfd_get_arch (abfd
))
11793 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11794 PT_GETFPREGS == mach+2. */
11796 case bfd_arch_aarch64
:
11797 case bfd_arch_alpha
:
11798 case bfd_arch_sparc
:
11799 switch (note
->type
)
11801 case NT_NETBSDCORE_FIRSTMACH
+0:
11802 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11804 case NT_NETBSDCORE_FIRSTMACH
+2:
11805 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11811 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11812 There's also old PT___GETREGS40 == mach + 1 for old reg
11813 structure which lacks GBR. */
11816 switch (note
->type
)
11818 case NT_NETBSDCORE_FIRSTMACH
+3:
11819 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11821 case NT_NETBSDCORE_FIRSTMACH
+5:
11822 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11828 /* On all other arch's, PT_GETREGS == mach+1 and
11829 PT_GETFPREGS == mach+3. */
11832 switch (note
->type
)
11834 case NT_NETBSDCORE_FIRSTMACH
+1:
11835 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11837 case NT_NETBSDCORE_FIRSTMACH
+3:
11838 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11848 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11850 if (note
->descsz
<= 0x48 + 31)
11853 /* Signal number at offset 0x08. */
11854 elf_tdata (abfd
)->core
->signal
11855 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11857 /* Process ID at offset 0x20. */
11858 elf_tdata (abfd
)->core
->pid
11859 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
11861 /* Command name at 0x48 (max 32 bytes, including nul). */
11862 elf_tdata (abfd
)->core
->command
11863 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
11868 /* Processes Solaris's process status note.
11869 sig_off ~ offsetof(prstatus_t, pr_cursig)
11870 pid_off ~ offsetof(prstatus_t, pr_pid)
11871 lwpid_off ~ offsetof(prstatus_t, pr_who)
11872 gregset_size ~ sizeof(gregset_t)
11873 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11876 elfcore_grok_solaris_prstatus (bfd
*abfd
, Elf_Internal_Note
* note
, int sig_off
,
11877 int pid_off
, int lwpid_off
, size_t gregset_size
,
11878 size_t gregset_offset
)
11880 asection
*sect
= NULL
;
11881 elf_tdata (abfd
)->core
->signal
11882 = bfd_get_16 (abfd
, note
->descdata
+ sig_off
);
11883 elf_tdata (abfd
)->core
->pid
11884 = bfd_get_32 (abfd
, note
->descdata
+ pid_off
);
11885 elf_tdata (abfd
)->core
->lwpid
11886 = bfd_get_32 (abfd
, note
->descdata
+ lwpid_off
);
11888 sect
= bfd_get_section_by_name (abfd
, ".reg");
11890 sect
->size
= gregset_size
;
11892 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11893 note
->descpos
+ gregset_offset
);
11896 /* Gets program and arguments from a core.
11897 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11898 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11901 elfcore_grok_solaris_info(bfd
*abfd
, Elf_Internal_Note
* note
,
11902 int prog_off
, int comm_off
)
11904 elf_tdata (abfd
)->core
->program
11905 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ prog_off
, 16);
11906 elf_tdata (abfd
)->core
->command
11907 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ comm_off
, 80);
11912 /* Processes Solaris's LWP status note.
11913 gregset_size ~ sizeof(gregset_t)
11914 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11915 fpregset_size ~ sizeof(fpregset_t)
11916 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11919 elfcore_grok_solaris_lwpstatus (bfd
*abfd
, Elf_Internal_Note
* note
,
11920 size_t gregset_size
, int gregset_off
,
11921 size_t fpregset_size
, int fpregset_off
)
11923 asection
*sect
= NULL
;
11924 char reg2_section_name
[16] = { 0 };
11926 (void) snprintf (reg2_section_name
, 16, "%s/%i", ".reg2",
11927 elf_tdata (abfd
)->core
->lwpid
);
11929 /* offsetof(lwpstatus_t, pr_lwpid) */
11930 elf_tdata (abfd
)->core
->lwpid
11931 = bfd_get_32 (abfd
, note
->descdata
+ 4);
11932 /* offsetof(lwpstatus_t, pr_cursig) */
11933 elf_tdata (abfd
)->core
->signal
11934 = bfd_get_16 (abfd
, note
->descdata
+ 12);
11936 sect
= bfd_get_section_by_name (abfd
, ".reg");
11938 sect
->size
= gregset_size
;
11939 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11940 note
->descpos
+ gregset_off
))
11943 sect
= bfd_get_section_by_name (abfd
, reg2_section_name
);
11946 sect
->size
= fpregset_size
;
11947 sect
->filepos
= note
->descpos
+ fpregset_off
;
11948 sect
->alignment_power
= 2;
11950 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg2", fpregset_size
,
11951 note
->descpos
+ fpregset_off
))
11958 elfcore_grok_solaris_note_impl (bfd
*abfd
, Elf_Internal_Note
*note
)
11963 /* core files are identified as 32- or 64-bit, SPARC or x86,
11964 by the size of the descsz which matches the sizeof()
11965 the type appropriate for that note type (e.g., prstatus_t for
11966 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11967 on Solaris. The core file bitness may differ from the bitness of
11968 gdb itself, so fixed values are used instead of sizeof().
11969 Appropriate fixed offsets are also used to obtain data from
11972 switch ((int) note
->type
)
11974 case SOLARIS_NT_PRSTATUS
:
11975 switch (note
->descsz
)
11977 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11978 return elfcore_grok_solaris_prstatus(abfd
, note
,
11979 136, 216, 308, 152, 356);
11980 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11981 return elfcore_grok_solaris_prstatus(abfd
, note
,
11982 264, 360, 520, 304, 600);
11983 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11984 return elfcore_grok_solaris_prstatus(abfd
, note
,
11985 136, 216, 308, 76, 356);
11986 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11987 return elfcore_grok_solaris_prstatus(abfd
, note
,
11988 264, 360, 520, 224, 600);
11993 case SOLARIS_NT_PSINFO
:
11994 case SOLARIS_NT_PRPSINFO
:
11995 switch (note
->descsz
)
11997 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11998 return elfcore_grok_solaris_info(abfd
, note
, 84, 100);
11999 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12000 return elfcore_grok_solaris_info(abfd
, note
, 120, 136);
12001 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12002 return elfcore_grok_solaris_info(abfd
, note
, 88, 104);
12003 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12004 return elfcore_grok_solaris_info(abfd
, note
, 136, 152);
12009 case SOLARIS_NT_LWPSTATUS
:
12010 switch (note
->descsz
)
12012 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12013 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12014 152, 344, 400, 496);
12015 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12016 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12017 304, 544, 544, 848);
12018 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12019 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12020 76, 344, 380, 420);
12021 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12022 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12023 224, 544, 528, 768);
12028 case SOLARIS_NT_LWPSINFO
:
12029 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12030 if (note
->descsz
== 128 || note
->descsz
== 152)
12031 elf_tdata (abfd
)->core
->lwpid
=
12032 bfd_get_32 (abfd
, note
->descdata
+ 4);
12042 /* For name starting with "CORE" this may be either a Solaris
12043 core file or a gdb-generated core file. Do Solaris-specific
12044 processing on selected note types first with
12045 elfcore_grok_solaris_note(), then process the note
12046 in elfcore_grok_note(). */
12049 elfcore_grok_solaris_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12051 if (!elfcore_grok_solaris_note_impl (abfd
, note
))
12054 return elfcore_grok_note (abfd
, note
);
12058 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12060 if (note
->type
== NT_OPENBSD_PROCINFO
)
12061 return elfcore_grok_openbsd_procinfo (abfd
, note
);
12063 if (note
->type
== NT_OPENBSD_REGS
)
12064 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
12066 if (note
->type
== NT_OPENBSD_FPREGS
)
12067 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
12069 if (note
->type
== NT_OPENBSD_XFPREGS
)
12070 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
12072 if (note
->type
== NT_OPENBSD_AUXV
)
12073 return elfcore_make_auxv_note_section (abfd
, note
, 0);
12075 if (note
->type
== NT_OPENBSD_WCOOKIE
)
12077 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
12082 sect
->size
= note
->descsz
;
12083 sect
->filepos
= note
->descpos
;
12084 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
12093 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
12095 void *ddata
= note
->descdata
;
12102 if (note
->descsz
< 16)
12105 /* nto_procfs_status 'pid' field is at offset 0. */
12106 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
12108 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12109 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
12111 /* nto_procfs_status 'flags' field is at offset 8. */
12112 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
12114 /* nto_procfs_status 'what' field is at offset 14. */
12115 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
12117 elf_tdata (abfd
)->core
->signal
= sig
;
12118 elf_tdata (abfd
)->core
->lwpid
= *tid
;
12121 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12122 do not come from signals so we make sure we set the current
12123 thread just in case. */
12124 if (flags
& 0x00000080)
12125 elf_tdata (abfd
)->core
->lwpid
= *tid
;
12127 /* Make a ".qnx_core_status/%d" section. */
12128 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
12130 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
12133 strcpy (name
, buf
);
12135 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12139 sect
->size
= note
->descsz
;
12140 sect
->filepos
= note
->descpos
;
12141 sect
->alignment_power
= 2;
12143 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
12147 elfcore_grok_nto_regs (bfd
*abfd
,
12148 Elf_Internal_Note
*note
,
12156 /* Make a "(base)/%d" section. */
12157 sprintf (buf
, "%s/%ld", base
, tid
);
12159 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
12162 strcpy (name
, buf
);
12164 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12168 sect
->size
= note
->descsz
;
12169 sect
->filepos
= note
->descpos
;
12170 sect
->alignment_power
= 2;
12172 /* This is the current thread. */
12173 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
12174 return elfcore_maybe_make_sect (abfd
, base
, sect
);
12180 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12182 /* Every GREG section has a STATUS section before it. Store the
12183 tid from the previous call to pass down to the next gregs
12185 static long tid
= 1;
12187 switch (note
->type
)
12189 case QNT_CORE_INFO
:
12190 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
12191 case QNT_CORE_STATUS
:
12192 return elfcore_grok_nto_status (abfd
, note
, &tid
);
12193 case QNT_CORE_GREG
:
12194 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
12195 case QNT_CORE_FPREG
:
12196 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
12203 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12209 /* Use note name as section name. */
12210 len
= note
->namesz
;
12211 name
= (char *) bfd_alloc (abfd
, len
);
12214 memcpy (name
, note
->namedata
, len
);
12215 name
[len
- 1] = '\0';
12217 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12221 sect
->size
= note
->descsz
;
12222 sect
->filepos
= note
->descpos
;
12223 sect
->alignment_power
= 1;
12228 /* Function: elfcore_write_note
12231 buffer to hold note, and current size of buffer
12235 size of data for note
12237 Writes note to end of buffer. ELF64 notes are written exactly as
12238 for ELF32, despite the current (as of 2006) ELF gabi specifying
12239 that they ought to have 8-byte namesz and descsz field, and have
12240 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12243 Pointer to realloc'd buffer, *BUFSIZ updated. */
12246 elfcore_write_note (bfd
*abfd
,
12254 Elf_External_Note
*xnp
;
12261 namesz
= strlen (name
) + 1;
12263 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
12265 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
12268 dest
= buf
+ *bufsiz
;
12269 *bufsiz
+= newspace
;
12270 xnp
= (Elf_External_Note
*) dest
;
12271 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
12272 H_PUT_32 (abfd
, size
, xnp
->descsz
);
12273 H_PUT_32 (abfd
, type
, xnp
->type
);
12277 memcpy (dest
, name
, namesz
);
12285 memcpy (dest
, input
, size
);
12295 /* gcc-8 warns (*) on all the strncpy calls in this function about
12296 possible string truncation. The "truncation" is not a bug. We
12297 have an external representation of structs with fields that are not
12298 necessarily NULL terminated and corresponding internal
12299 representation fields that are one larger so that they can always
12300 be NULL terminated.
12301 gcc versions between 4.2 and 4.6 do not allow pragma control of
12302 diagnostics inside functions, giving a hard error if you try to use
12303 the finer control available with later versions.
12304 gcc prior to 4.2 warns about diagnostic push and pop.
12305 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12306 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12307 (*) Depending on your system header files! */
12308 #if GCC_VERSION >= 8000
12309 # pragma GCC diagnostic push
12310 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12313 elfcore_write_prpsinfo (bfd
*abfd
,
12317 const char *psargs
)
12319 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12321 if (bed
->elf_backend_write_core_note
!= NULL
)
12324 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
12325 NT_PRPSINFO
, fname
, psargs
);
12330 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12331 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12332 if (bed
->s
->elfclass
== ELFCLASS32
)
12334 # if defined (HAVE_PSINFO32_T)
12336 int note_type
= NT_PSINFO
;
12339 int note_type
= NT_PRPSINFO
;
12342 memset (&data
, 0, sizeof (data
));
12343 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
12344 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
12345 return elfcore_write_note (abfd
, buf
, bufsiz
,
12346 "CORE", note_type
, &data
, sizeof (data
));
12351 # if defined (HAVE_PSINFO_T)
12353 int note_type
= NT_PSINFO
;
12356 int note_type
= NT_PRPSINFO
;
12359 memset (&data
, 0, sizeof (data
));
12360 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
12361 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
12362 return elfcore_write_note (abfd
, buf
, bufsiz
,
12363 "CORE", note_type
, &data
, sizeof (data
));
12365 #endif /* PSINFO_T or PRPSINFO_T */
12370 #if GCC_VERSION >= 8000
12371 # pragma GCC diagnostic pop
12375 elfcore_write_linux_prpsinfo32
12376 (bfd
*abfd
, char *buf
, int *bufsiz
,
12377 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
12379 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
12381 struct elf_external_linux_prpsinfo32_ugid16 data
;
12383 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
12384 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
12385 &data
, sizeof (data
));
12389 struct elf_external_linux_prpsinfo32_ugid32 data
;
12391 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
12392 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
12393 &data
, sizeof (data
));
12398 elfcore_write_linux_prpsinfo64
12399 (bfd
*abfd
, char *buf
, int *bufsiz
,
12400 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
12402 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
12404 struct elf_external_linux_prpsinfo64_ugid16 data
;
12406 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
12407 return elfcore_write_note (abfd
, buf
, bufsiz
,
12408 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
12412 struct elf_external_linux_prpsinfo64_ugid32 data
;
12414 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
12415 return elfcore_write_note (abfd
, buf
, bufsiz
,
12416 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
12421 elfcore_write_prstatus (bfd
*abfd
,
12428 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12430 if (bed
->elf_backend_write_core_note
!= NULL
)
12433 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
12435 pid
, cursig
, gregs
);
12440 #if defined (HAVE_PRSTATUS_T)
12441 #if defined (HAVE_PRSTATUS32_T)
12442 if (bed
->s
->elfclass
== ELFCLASS32
)
12444 prstatus32_t prstat
;
12446 memset (&prstat
, 0, sizeof (prstat
));
12447 prstat
.pr_pid
= pid
;
12448 prstat
.pr_cursig
= cursig
;
12449 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
12450 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
12451 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
12458 memset (&prstat
, 0, sizeof (prstat
));
12459 prstat
.pr_pid
= pid
;
12460 prstat
.pr_cursig
= cursig
;
12461 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
12462 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
12463 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
12465 #endif /* HAVE_PRSTATUS_T */
12471 #if defined (HAVE_LWPSTATUS_T)
12473 elfcore_write_lwpstatus (bfd
*abfd
,
12480 lwpstatus_t lwpstat
;
12481 const char *note_name
= "CORE";
12483 memset (&lwpstat
, 0, sizeof (lwpstat
));
12484 lwpstat
.pr_lwpid
= pid
>> 16;
12485 lwpstat
.pr_cursig
= cursig
;
12486 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12487 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
12488 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12489 #if !defined(gregs)
12490 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
12491 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
12493 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
12494 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
12497 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12498 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
12500 #endif /* HAVE_LWPSTATUS_T */
12502 #if defined (HAVE_PSTATUS_T)
12504 elfcore_write_pstatus (bfd
*abfd
,
12508 int cursig ATTRIBUTE_UNUSED
,
12509 const void *gregs ATTRIBUTE_UNUSED
)
12511 const char *note_name
= "CORE";
12512 #if defined (HAVE_PSTATUS32_T)
12513 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12515 if (bed
->s
->elfclass
== ELFCLASS32
)
12519 memset (&pstat
, 0, sizeof (pstat
));
12520 pstat
.pr_pid
= pid
& 0xffff;
12521 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12522 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12530 memset (&pstat
, 0, sizeof (pstat
));
12531 pstat
.pr_pid
= pid
& 0xffff;
12532 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12533 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12537 #endif /* HAVE_PSTATUS_T */
12540 elfcore_write_prfpreg (bfd
*abfd
,
12543 const void *fpregs
,
12546 const char *note_name
= "CORE";
12547 return elfcore_write_note (abfd
, buf
, bufsiz
,
12548 note_name
, NT_FPREGSET
, fpregs
, size
);
12552 elfcore_write_prxfpreg (bfd
*abfd
,
12555 const void *xfpregs
,
12558 char *note_name
= "LINUX";
12559 return elfcore_write_note (abfd
, buf
, bufsiz
,
12560 note_name
, NT_PRXFPREG
, xfpregs
, size
);
12564 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
12565 const void *xfpregs
, int size
)
12568 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
12569 note_name
= "FreeBSD";
12571 note_name
= "LINUX";
12572 return elfcore_write_note (abfd
, buf
, bufsiz
,
12573 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
12577 elfcore_write_sspreg (bfd
*abfd
, char *buf
, int *bufsiz
,
12578 const void *ssp
, int size
)
12580 const char *note_name
= "LINUX";
12581 return elfcore_write_note (abfd
, buf
, bufsiz
,
12582 note_name
, NT_X86_SHSTK
, ssp
, size
);
12586 elfcore_write_x86_segbases (bfd
*abfd
, char *buf
, int *bufsiz
,
12587 const void *regs
, int size
)
12589 char *note_name
= "FreeBSD";
12590 return elfcore_write_note (abfd
, buf
, bufsiz
,
12591 note_name
, NT_FREEBSD_X86_SEGBASES
, regs
, size
);
12595 elfcore_write_ppc_vmx (bfd
*abfd
,
12598 const void *ppc_vmx
,
12601 char *note_name
= "LINUX";
12602 return elfcore_write_note (abfd
, buf
, bufsiz
,
12603 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
12607 elfcore_write_ppc_vsx (bfd
*abfd
,
12610 const void *ppc_vsx
,
12613 char *note_name
= "LINUX";
12614 return elfcore_write_note (abfd
, buf
, bufsiz
,
12615 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
12619 elfcore_write_ppc_tar (bfd
*abfd
,
12622 const void *ppc_tar
,
12625 char *note_name
= "LINUX";
12626 return elfcore_write_note (abfd
, buf
, bufsiz
,
12627 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
12631 elfcore_write_ppc_ppr (bfd
*abfd
,
12634 const void *ppc_ppr
,
12637 char *note_name
= "LINUX";
12638 return elfcore_write_note (abfd
, buf
, bufsiz
,
12639 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
12643 elfcore_write_ppc_dscr (bfd
*abfd
,
12646 const void *ppc_dscr
,
12649 char *note_name
= "LINUX";
12650 return elfcore_write_note (abfd
, buf
, bufsiz
,
12651 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
12655 elfcore_write_ppc_ebb (bfd
*abfd
,
12658 const void *ppc_ebb
,
12661 char *note_name
= "LINUX";
12662 return elfcore_write_note (abfd
, buf
, bufsiz
,
12663 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
12667 elfcore_write_ppc_pmu (bfd
*abfd
,
12670 const void *ppc_pmu
,
12673 char *note_name
= "LINUX";
12674 return elfcore_write_note (abfd
, buf
, bufsiz
,
12675 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
12679 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
12682 const void *ppc_tm_cgpr
,
12685 char *note_name
= "LINUX";
12686 return elfcore_write_note (abfd
, buf
, bufsiz
,
12687 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
12691 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
12694 const void *ppc_tm_cfpr
,
12697 char *note_name
= "LINUX";
12698 return elfcore_write_note (abfd
, buf
, bufsiz
,
12699 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
12703 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
12706 const void *ppc_tm_cvmx
,
12709 char *note_name
= "LINUX";
12710 return elfcore_write_note (abfd
, buf
, bufsiz
,
12711 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
12715 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
12718 const void *ppc_tm_cvsx
,
12721 char *note_name
= "LINUX";
12722 return elfcore_write_note (abfd
, buf
, bufsiz
,
12723 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
12727 elfcore_write_ppc_tm_spr (bfd
*abfd
,
12730 const void *ppc_tm_spr
,
12733 char *note_name
= "LINUX";
12734 return elfcore_write_note (abfd
, buf
, bufsiz
,
12735 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
12739 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
12742 const void *ppc_tm_ctar
,
12745 char *note_name
= "LINUX";
12746 return elfcore_write_note (abfd
, buf
, bufsiz
,
12747 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
12751 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
12754 const void *ppc_tm_cppr
,
12757 char *note_name
= "LINUX";
12758 return elfcore_write_note (abfd
, buf
, bufsiz
,
12759 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
12763 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
12766 const void *ppc_tm_cdscr
,
12769 char *note_name
= "LINUX";
12770 return elfcore_write_note (abfd
, buf
, bufsiz
,
12771 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
12775 elfcore_write_s390_high_gprs (bfd
*abfd
,
12778 const void *s390_high_gprs
,
12781 char *note_name
= "LINUX";
12782 return elfcore_write_note (abfd
, buf
, bufsiz
,
12783 note_name
, NT_S390_HIGH_GPRS
,
12784 s390_high_gprs
, size
);
12788 elfcore_write_s390_timer (bfd
*abfd
,
12791 const void *s390_timer
,
12794 char *note_name
= "LINUX";
12795 return elfcore_write_note (abfd
, buf
, bufsiz
,
12796 note_name
, NT_S390_TIMER
, s390_timer
, size
);
12800 elfcore_write_s390_todcmp (bfd
*abfd
,
12803 const void *s390_todcmp
,
12806 char *note_name
= "LINUX";
12807 return elfcore_write_note (abfd
, buf
, bufsiz
,
12808 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
12812 elfcore_write_s390_todpreg (bfd
*abfd
,
12815 const void *s390_todpreg
,
12818 char *note_name
= "LINUX";
12819 return elfcore_write_note (abfd
, buf
, bufsiz
,
12820 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
12824 elfcore_write_s390_ctrs (bfd
*abfd
,
12827 const void *s390_ctrs
,
12830 char *note_name
= "LINUX";
12831 return elfcore_write_note (abfd
, buf
, bufsiz
,
12832 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
12836 elfcore_write_s390_prefix (bfd
*abfd
,
12839 const void *s390_prefix
,
12842 char *note_name
= "LINUX";
12843 return elfcore_write_note (abfd
, buf
, bufsiz
,
12844 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
12848 elfcore_write_s390_last_break (bfd
*abfd
,
12851 const void *s390_last_break
,
12854 char *note_name
= "LINUX";
12855 return elfcore_write_note (abfd
, buf
, bufsiz
,
12856 note_name
, NT_S390_LAST_BREAK
,
12857 s390_last_break
, size
);
12861 elfcore_write_s390_system_call (bfd
*abfd
,
12864 const void *s390_system_call
,
12867 char *note_name
= "LINUX";
12868 return elfcore_write_note (abfd
, buf
, bufsiz
,
12869 note_name
, NT_S390_SYSTEM_CALL
,
12870 s390_system_call
, size
);
12874 elfcore_write_s390_tdb (bfd
*abfd
,
12877 const void *s390_tdb
,
12880 char *note_name
= "LINUX";
12881 return elfcore_write_note (abfd
, buf
, bufsiz
,
12882 note_name
, NT_S390_TDB
, s390_tdb
, size
);
12886 elfcore_write_s390_vxrs_low (bfd
*abfd
,
12889 const void *s390_vxrs_low
,
12892 char *note_name
= "LINUX";
12893 return elfcore_write_note (abfd
, buf
, bufsiz
,
12894 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
12898 elfcore_write_s390_vxrs_high (bfd
*abfd
,
12901 const void *s390_vxrs_high
,
12904 char *note_name
= "LINUX";
12905 return elfcore_write_note (abfd
, buf
, bufsiz
,
12906 note_name
, NT_S390_VXRS_HIGH
,
12907 s390_vxrs_high
, size
);
12911 elfcore_write_s390_gs_cb (bfd
*abfd
,
12914 const void *s390_gs_cb
,
12917 char *note_name
= "LINUX";
12918 return elfcore_write_note (abfd
, buf
, bufsiz
,
12919 note_name
, NT_S390_GS_CB
,
12924 elfcore_write_s390_gs_bc (bfd
*abfd
,
12927 const void *s390_gs_bc
,
12930 char *note_name
= "LINUX";
12931 return elfcore_write_note (abfd
, buf
, bufsiz
,
12932 note_name
, NT_S390_GS_BC
,
12937 elfcore_write_arm_vfp (bfd
*abfd
,
12940 const void *arm_vfp
,
12943 char *note_name
= "LINUX";
12944 return elfcore_write_note (abfd
, buf
, bufsiz
,
12945 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
12949 elfcore_write_aarch_tls (bfd
*abfd
,
12952 const void *aarch_tls
,
12955 char *note_name
= "LINUX";
12956 return elfcore_write_note (abfd
, buf
, bufsiz
,
12957 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
12961 elfcore_write_aarch_hw_break (bfd
*abfd
,
12964 const void *aarch_hw_break
,
12967 char *note_name
= "LINUX";
12968 return elfcore_write_note (abfd
, buf
, bufsiz
,
12969 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
12973 elfcore_write_aarch_hw_watch (bfd
*abfd
,
12976 const void *aarch_hw_watch
,
12979 char *note_name
= "LINUX";
12980 return elfcore_write_note (abfd
, buf
, bufsiz
,
12981 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
12985 elfcore_write_aarch_sve (bfd
*abfd
,
12988 const void *aarch_sve
,
12991 char *note_name
= "LINUX";
12992 return elfcore_write_note (abfd
, buf
, bufsiz
,
12993 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
12997 elfcore_write_aarch_pauth (bfd
*abfd
,
13000 const void *aarch_pauth
,
13003 char *note_name
= "LINUX";
13004 return elfcore_write_note (abfd
, buf
, bufsiz
,
13005 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
13009 elfcore_write_aarch_mte (bfd
*abfd
,
13012 const void *aarch_mte
,
13015 char *note_name
= "LINUX";
13016 return elfcore_write_note (abfd
, buf
, bufsiz
,
13017 note_name
, NT_ARM_TAGGED_ADDR_CTRL
,
13023 elfcore_write_aarch_ssve (bfd
*abfd
,
13026 const void *aarch_ssve
,
13029 char *note_name
= "LINUX";
13030 return elfcore_write_note (abfd
, buf
, bufsiz
,
13031 note_name
, NT_ARM_SSVE
,
13037 elfcore_write_aarch_za (bfd
*abfd
,
13040 const void *aarch_za
,
13043 char *note_name
= "LINUX";
13044 return elfcore_write_note (abfd
, buf
, bufsiz
,
13045 note_name
, NT_ARM_ZA
,
13050 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13051 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13052 written into. Return a pointer to the new start of the note buffer, to
13053 replace BUF which may no longer be valid. */
13056 elfcore_write_aarch_zt (bfd
*abfd
,
13059 const void *aarch_zt
,
13062 char *note_name
= "LINUX";
13063 return elfcore_write_note (abfd
, buf
, bufsiz
,
13064 note_name
, NT_ARM_ZT
,
13069 /* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
13070 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13071 written into. Return a pointer to the new start of the note buffer, to
13072 replace BUF which may no longer be valid. */
13075 elfcore_write_aarch_gcs (bfd
*abfd
, char *buf
, int *bufsiz
,
13076 const void *aarch_gcs
, int size
)
13078 const char *note_name
= "LINUX";
13079 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
, NT_ARM_GCS
,
13084 elfcore_write_arc_v2 (bfd
*abfd
,
13087 const void *arc_v2
,
13090 char *note_name
= "LINUX";
13091 return elfcore_write_note (abfd
, buf
, bufsiz
,
13092 note_name
, NT_ARC_V2
, arc_v2
, size
);
13096 elfcore_write_loongarch_cpucfg (bfd
*abfd
,
13099 const void *loongarch_cpucfg
,
13102 char *note_name
= "LINUX";
13103 return elfcore_write_note (abfd
, buf
, bufsiz
,
13104 note_name
, NT_LARCH_CPUCFG
,
13105 loongarch_cpucfg
, size
);
13109 elfcore_write_loongarch_lbt (bfd
*abfd
,
13112 const void *loongarch_lbt
,
13115 char *note_name
= "LINUX";
13116 return elfcore_write_note (abfd
, buf
, bufsiz
,
13117 note_name
, NT_LARCH_LBT
, loongarch_lbt
, size
);
13121 elfcore_write_loongarch_lsx (bfd
*abfd
,
13124 const void *loongarch_lsx
,
13127 char *note_name
= "LINUX";
13128 return elfcore_write_note (abfd
, buf
, bufsiz
,
13129 note_name
, NT_LARCH_LSX
, loongarch_lsx
, size
);
13133 elfcore_write_loongarch_lasx (bfd
*abfd
,
13136 const void *loongarch_lasx
,
13139 char *note_name
= "LINUX";
13140 return elfcore_write_note (abfd
, buf
, bufsiz
,
13141 note_name
, NT_LARCH_LASX
, loongarch_lasx
, size
);
13144 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13145 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13146 written into. Return a pointer to the new start of the note buffer, to
13147 replace BUF which may no longer be valid. */
13150 elfcore_write_riscv_csr (bfd
*abfd
,
13156 const char *note_name
= "GDB";
13157 return elfcore_write_note (abfd
, buf
, bufsiz
,
13158 note_name
, NT_RISCV_CSR
, csrs
, size
);
13161 /* Write the target description (a string) pointed to by TDESC, length
13162 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13163 note is being written into. Return a pointer to the new start of the
13164 note buffer, to replace BUF which may no longer be valid. */
13167 elfcore_write_gdb_tdesc (bfd
*abfd
,
13173 const char *note_name
= "GDB";
13174 return elfcore_write_note (abfd
, buf
, bufsiz
,
13175 note_name
, NT_GDB_TDESC
, tdesc
, size
);
13179 elfcore_write_register_note (bfd
*abfd
,
13182 const char *section
,
13186 if (strcmp (section
, ".reg2") == 0)
13187 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
13188 if (strcmp (section
, ".reg-xfp") == 0)
13189 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
13190 if (strcmp (section
, ".reg-xstate") == 0)
13191 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
13192 if (strcmp (section
, ".reg-x86-segbases") == 0)
13193 return elfcore_write_x86_segbases (abfd
, buf
, bufsiz
, data
, size
);
13194 if (strcmp (section
, ".reg-ssp") == 0)
13195 return elfcore_write_sspreg (abfd
, buf
, bufsiz
, data
, size
);
13196 if (strcmp (section
, ".reg-ppc-vmx") == 0)
13197 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
13198 if (strcmp (section
, ".reg-ppc-vsx") == 0)
13199 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
13200 if (strcmp (section
, ".reg-ppc-tar") == 0)
13201 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
13202 if (strcmp (section
, ".reg-ppc-ppr") == 0)
13203 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
13204 if (strcmp (section
, ".reg-ppc-dscr") == 0)
13205 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
13206 if (strcmp (section
, ".reg-ppc-ebb") == 0)
13207 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
13208 if (strcmp (section
, ".reg-ppc-pmu") == 0)
13209 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
13210 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
13211 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
13212 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
13213 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
13214 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
13215 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
13216 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
13217 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
13218 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
13219 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
13220 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
13221 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
13222 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
13223 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
13224 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
13225 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
13226 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
13227 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
13228 if (strcmp (section
, ".reg-s390-timer") == 0)
13229 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
13230 if (strcmp (section
, ".reg-s390-todcmp") == 0)
13231 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
13232 if (strcmp (section
, ".reg-s390-todpreg") == 0)
13233 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
13234 if (strcmp (section
, ".reg-s390-ctrs") == 0)
13235 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
13236 if (strcmp (section
, ".reg-s390-prefix") == 0)
13237 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
13238 if (strcmp (section
, ".reg-s390-last-break") == 0)
13239 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
13240 if (strcmp (section
, ".reg-s390-system-call") == 0)
13241 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
13242 if (strcmp (section
, ".reg-s390-tdb") == 0)
13243 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
13244 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
13245 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
13246 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
13247 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
13248 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
13249 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
13250 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
13251 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
13252 if (strcmp (section
, ".reg-arm-vfp") == 0)
13253 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
13254 if (strcmp (section
, ".reg-aarch-tls") == 0)
13255 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
13256 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
13257 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
13258 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
13259 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
13260 if (strcmp (section
, ".reg-aarch-sve") == 0)
13261 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
13262 if (strcmp (section
, ".reg-aarch-pauth") == 0)
13263 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
13264 if (strcmp (section
, ".reg-aarch-mte") == 0)
13265 return elfcore_write_aarch_mte (abfd
, buf
, bufsiz
, data
, size
);
13266 if (strcmp (section
, ".reg-aarch-ssve") == 0)
13267 return elfcore_write_aarch_ssve (abfd
, buf
, bufsiz
, data
, size
);
13268 if (strcmp (section
, ".reg-aarch-za") == 0)
13269 return elfcore_write_aarch_za (abfd
, buf
, bufsiz
, data
, size
);
13270 if (strcmp (section
, ".reg-aarch-zt") == 0)
13271 return elfcore_write_aarch_zt (abfd
, buf
, bufsiz
, data
, size
);
13272 if (strcmp (section
, ".reg-aarch-gcs") == 0)
13273 return elfcore_write_aarch_gcs (abfd
, buf
, bufsiz
, data
, size
);
13274 if (strcmp (section
, ".reg-arc-v2") == 0)
13275 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
13276 if (strcmp (section
, ".gdb-tdesc") == 0)
13277 return elfcore_write_gdb_tdesc (abfd
, buf
, bufsiz
, data
, size
);
13278 if (strcmp (section
, ".reg-riscv-csr") == 0)
13279 return elfcore_write_riscv_csr (abfd
, buf
, bufsiz
, data
, size
);
13280 if (strcmp (section
, ".reg-loongarch-cpucfg") == 0)
13281 return elfcore_write_loongarch_cpucfg (abfd
, buf
, bufsiz
, data
, size
);
13282 if (strcmp (section
, ".reg-loongarch-lbt") == 0)
13283 return elfcore_write_loongarch_lbt (abfd
, buf
, bufsiz
, data
, size
);
13284 if (strcmp (section
, ".reg-loongarch-lsx") == 0)
13285 return elfcore_write_loongarch_lsx (abfd
, buf
, bufsiz
, data
, size
);
13286 if (strcmp (section
, ".reg-loongarch-lasx") == 0)
13287 return elfcore_write_loongarch_lasx (abfd
, buf
, bufsiz
, data
, size
);
13292 elfcore_write_file_note (bfd
*obfd
, char *note_data
, int *note_size
,
13293 const void *buf
, int bufsiz
)
13295 return elfcore_write_note (obfd
, note_data
, note_size
,
13296 "CORE", NT_FILE
, buf
, bufsiz
);
13300 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
13305 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13306 gABI specifies that PT_NOTE alignment should be aligned to 4
13307 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13308 align is less than 4, we use 4 byte alignment. */
13311 if (align
!= 4 && align
!= 8)
13315 while (p
< buf
+ size
)
13317 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
13318 Elf_Internal_Note in
;
13320 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
13323 in
.type
= H_GET_32 (abfd
, xnp
->type
);
13325 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
13326 in
.namedata
= xnp
->name
;
13327 if (in
.namesz
> buf
- in
.namedata
+ size
)
13330 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
13331 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
13332 in
.descpos
= offset
+ (in
.descdata
- buf
);
13334 && (in
.descdata
>= buf
+ size
13335 || in
.descsz
> buf
- in
.descdata
+ size
))
13338 switch (bfd_get_format (abfd
))
13345 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13348 const char * string
;
13350 bool (*func
) (bfd
*, Elf_Internal_Note
*);
13354 GROKER_ELEMENT ("", elfcore_grok_note
),
13355 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
13356 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
13357 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note
),
13358 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
13359 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
13360 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
),
13361 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note
)
13363 #undef GROKER_ELEMENT
13366 for (i
= ARRAY_SIZE (grokers
); i
--;)
13368 if (in
.namesz
>= grokers
[i
].len
13369 && strncmp (in
.namedata
, grokers
[i
].string
,
13370 grokers
[i
].len
) == 0)
13372 if (! grokers
[i
].func (abfd
, & in
))
13381 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
13383 if (! elfobj_grok_gnu_note (abfd
, &in
))
13386 else if (in
.namesz
== sizeof "stapsdt"
13387 && strcmp (in
.namedata
, "stapsdt") == 0)
13389 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
13395 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
13402 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
13407 if (size
== 0 || (size
+ 1) == 0)
13410 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
13413 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
13417 /* PR 17512: file: ec08f814
13418 0-termintate the buffer so that string searches will not overflow. */
13421 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
13431 /* Providing external access to the ELF program header table. */
13433 /* Return an upper bound on the number of bytes required to store a
13434 copy of ABFD's program header table entries. Return -1 if an error
13435 occurs; bfd_get_error will return an appropriate code. */
13438 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
13440 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
13442 bfd_set_error (bfd_error_wrong_format
);
13446 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
13449 /* Copy ABFD's program header table entries to *PHDRS. The entries
13450 will be stored as an array of Elf_Internal_Phdr structures, as
13451 defined in include/elf/internal.h. To find out how large the
13452 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13454 Return the number of program header table entries read, or -1 if an
13455 error occurs; bfd_get_error will return an appropriate code. */
13458 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
13462 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
13464 bfd_set_error (bfd_error_wrong_format
);
13468 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
13469 if (num_phdrs
!= 0)
13470 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
13471 num_phdrs
* sizeof (Elf_Internal_Phdr
));
13476 enum elf_reloc_type_class
13477 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13478 const asection
*rel_sec ATTRIBUTE_UNUSED
,
13479 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
13481 return reloc_class_normal
;
13484 /* For RELA architectures, return the relocation value for a
13485 relocation against a local symbol. */
13488 _bfd_elf_rela_local_sym (bfd
*abfd
,
13489 Elf_Internal_Sym
*sym
,
13491 Elf_Internal_Rela
*rel
)
13493 asection
*sec
= *psec
;
13494 bfd_vma relocation
;
13496 relocation
= (sec
->output_section
->vma
13497 + sec
->output_offset
13499 if ((sec
->flags
& SEC_MERGE
)
13500 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
13501 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
13504 _bfd_merged_section_offset (abfd
, psec
,
13505 elf_section_data (sec
)->sec_info
,
13506 sym
->st_value
+ rel
->r_addend
);
13509 /* If we have changed the section, and our original section is
13510 marked with SEC_EXCLUDE, it means that the original
13511 SEC_MERGE section has been completely subsumed in some
13512 other SEC_MERGE section. In this case, we need to leave
13513 some info around for --emit-relocs. */
13514 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
13515 sec
->kept_section
= *psec
;
13518 rel
->r_addend
-= relocation
;
13519 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
13525 _bfd_elf_rel_local_sym (bfd
*abfd
,
13526 Elf_Internal_Sym
*sym
,
13530 asection
*sec
= *psec
;
13532 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
13533 return sym
->st_value
+ addend
;
13535 return _bfd_merged_section_offset (abfd
, psec
,
13536 elf_section_data (sec
)->sec_info
,
13537 sym
->st_value
+ addend
);
13540 /* Adjust an address within a section. Given OFFSET within SEC, return
13541 the new offset within the section, based upon changes made to the
13542 section. Returns -1 if the offset is now invalid.
13543 The offset (in abnd out) is in target sized bytes, however big a
13547 _bfd_elf_section_offset (bfd
*abfd
,
13548 struct bfd_link_info
*info
,
13552 switch (sec
->sec_info_type
)
13554 case SEC_INFO_TYPE_STABS
:
13555 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
13557 case SEC_INFO_TYPE_EH_FRAME
:
13558 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
13560 case SEC_INFO_TYPE_SFRAME
:
13561 return _bfd_elf_sframe_section_offset (abfd
, info
, sec
, offset
);
13564 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
13566 /* Reverse the offset. */
13567 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13568 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
13570 /* address_size and sec->size are in octets. Convert
13571 to bytes before subtracting the original offset. */
13572 offset
= ((sec
->size
- address_size
)
13573 / bfd_octets_per_byte (abfd
, sec
) - offset
);
13580 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
13581 long symcount ATTRIBUTE_UNUSED
,
13582 asymbol
**syms ATTRIBUTE_UNUSED
,
13587 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13590 const char *relplt_name
;
13591 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
13595 Elf_Internal_Shdr
*hdr
;
13601 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
13604 if (dynsymcount
<= 0)
13607 if (!bed
->plt_sym_val
)
13610 relplt_name
= bed
->relplt_name
;
13611 if (relplt_name
== NULL
)
13612 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
13613 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
13614 if (relplt
== NULL
)
13617 hdr
= &elf_section_data (relplt
)->this_hdr
;
13618 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
13619 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
13622 plt
= bfd_get_section_by_name (abfd
, ".plt");
13626 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
13627 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, true))
13630 count
= NUM_SHDR_ENTRIES (hdr
);
13631 size
= count
* sizeof (asymbol
);
13632 p
= relplt
->relocation
;
13633 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13635 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
13636 if (p
->addend
!= 0)
13639 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
13641 size
+= sizeof ("+0x") - 1 + 8;
13646 s
= *ret
= (asymbol
*) bfd_malloc (size
);
13650 names
= (char *) (s
+ count
);
13651 p
= relplt
->relocation
;
13653 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13658 addr
= bed
->plt_sym_val (i
, plt
, p
);
13659 if (addr
== (bfd_vma
) -1)
13662 *s
= **p
->sym_ptr_ptr
;
13663 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13664 we are defining a symbol, ensure one of them is set. */
13665 if ((s
->flags
& BSF_LOCAL
) == 0)
13666 s
->flags
|= BSF_GLOBAL
;
13667 s
->flags
|= BSF_SYNTHETIC
;
13669 s
->value
= addr
- plt
->vma
;
13672 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
13673 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
13675 if (p
->addend
!= 0)
13679 memcpy (names
, "+0x", sizeof ("+0x") - 1);
13680 names
+= sizeof ("+0x") - 1;
13681 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
13682 for (a
= buf
; *a
== '0'; ++a
)
13685 memcpy (names
, a
, len
);
13688 memcpy (names
, "@plt", sizeof ("@plt"));
13689 names
+= sizeof ("@plt");
13696 /* It is only used by x86-64 so far.
13697 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13698 but current usage would allow all of _bfd_std_section to be zero. */
13699 static const asymbol lcomm_sym
13700 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
13701 asection _bfd_elf_large_com_section
13702 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
13703 "LARGE_COMMON", 0, SEC_IS_COMMON
);
13706 _bfd_elf_final_write_processing (bfd
*abfd
)
13708 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
13710 i_ehdrp
= elf_elfheader (abfd
);
13712 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13713 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
13715 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13716 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13717 or STB_GNU_UNIQUE binding. */
13718 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
13720 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13721 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
13722 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
13723 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
13725 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
13726 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13727 "and FreeBSD targets"));
13728 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
13729 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13730 "only by GNU and FreeBSD targets"));
13731 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
13732 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13733 "only by GNU and FreeBSD targets"));
13734 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
13735 _bfd_error_handler (_("GNU_RETAIN section is supported "
13736 "only by GNU and FreeBSD targets"));
13737 bfd_set_error (bfd_error_sorry
);
13745 /* Return TRUE for ELF symbol types that represent functions.
13746 This is the default version of this function, which is sufficient for
13747 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13750 _bfd_elf_is_function_type (unsigned int type
)
13752 return (type
== STT_FUNC
13753 || type
== STT_GNU_IFUNC
);
13756 /* If the ELF symbol SYM might be a function in SEC, return the
13757 function size and set *CODE_OFF to the function's entry point,
13758 otherwise return zero. */
13761 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
13764 bfd_size_type size
;
13765 elf_symbol_type
* elf_sym
= (elf_symbol_type
*) sym
;
13767 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
13768 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
13769 || sym
->section
!= sec
)
13772 size
= (sym
->flags
& BSF_SYNTHETIC
) ? 0 : elf_sym
->internal_elf_sym
.st_size
;
13774 /* In theory we should check that the symbol's type satisfies
13775 _bfd_elf_is_function_type(), but there are some function-like
13776 symbols which would fail this test. (eg _start). Instead
13777 we check for hidden, local, notype symbols with zero size.
13778 This type of symbol is generated by the annobin plugin for gcc
13779 and clang, and should not be considered to be a function symbol. */
13781 && ((sym
->flags
& (BSF_SYNTHETIC
| BSF_LOCAL
)) == BSF_LOCAL
)
13782 && ELF_ST_TYPE (elf_sym
->internal_elf_sym
.st_info
) == STT_NOTYPE
13783 && ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
) == STV_HIDDEN
)
13786 *code_off
= sym
->value
;
13787 /* Do not return 0 for the function's size. */
13788 return size
? size
: 1;
13791 /* Set to non-zero to enable some debug messages. */
13792 #define DEBUG_SECONDARY_RELOCS 0
13794 /* An internal-to-the-bfd-library only section type
13795 used to indicate a cached secondary reloc section. */
13796 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13798 /* Create a BFD section to hold a secondary reloc section. */
13801 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
13802 Elf_Internal_Shdr
*hdr
,
13804 unsigned int shindex
)
13806 /* We only support RELA secondary relocs. */
13807 if (hdr
->sh_type
!= SHT_RELA
)
13810 #if DEBUG_SECONDARY_RELOCS
13811 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
13813 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
13814 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
13817 /* Read in any secondary relocs associated with SEC. */
13820 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
13822 asymbol
** symbols
,
13825 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
13827 bool result
= true;
13828 bfd_vma (*r_sym
) (bfd_vma
);
13829 ufile_ptr filesize
;
13831 #if BFD_DEFAULT_TARGET_SIZE > 32
13832 if (bfd_arch_bits_per_address (abfd
) != 32)
13833 r_sym
= elf64_r_sym
;
13836 r_sym
= elf32_r_sym
;
13838 if (!elf_section_data (sec
)->has_secondary_relocs
)
13841 /* Discover if there are any secondary reloc sections
13842 associated with SEC. */
13843 filesize
= bfd_get_file_size (abfd
);
13844 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
13846 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
13848 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
13849 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
13850 && (hdr
->sh_entsize
== ebd
->s
->sizeof_rel
13851 || hdr
->sh_entsize
== ebd
->s
->sizeof_rela
))
13853 bfd_byte
* native_relocs
;
13854 bfd_byte
* native_reloc
;
13855 arelent
* internal_relocs
;
13856 arelent
* internal_reloc
;
13858 unsigned int entsize
;
13859 unsigned int symcount
;
13860 bfd_size_type reloc_count
;
13863 if (ebd
->elf_info_to_howto
== NULL
)
13866 #if DEBUG_SECONDARY_RELOCS
13867 fprintf (stderr
, "read secondary relocs for %s from %s\n",
13868 sec
->name
, relsec
->name
);
13870 entsize
= hdr
->sh_entsize
;
13873 && ((ufile_ptr
) hdr
->sh_offset
> filesize
13874 || hdr
->sh_size
> filesize
- hdr
->sh_offset
))
13876 bfd_set_error (bfd_error_file_truncated
);
13881 native_relocs
= bfd_malloc (hdr
->sh_size
);
13882 if (native_relocs
== NULL
)
13888 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
13889 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
13891 free (native_relocs
);
13892 bfd_set_error (bfd_error_file_too_big
);
13897 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
13898 if (internal_relocs
== NULL
)
13900 free (native_relocs
);
13905 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
13906 || bfd_read (native_relocs
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
13908 free (native_relocs
);
13909 /* The internal_relocs will be freed when
13910 the memory for the bfd is released. */
13916 symcount
= bfd_get_dynamic_symcount (abfd
);
13918 symcount
= bfd_get_symcount (abfd
);
13920 for (i
= 0, internal_reloc
= internal_relocs
,
13921 native_reloc
= native_relocs
;
13923 i
++, internal_reloc
++, native_reloc
+= entsize
)
13926 Elf_Internal_Rela rela
;
13928 if (entsize
== ebd
->s
->sizeof_rel
)
13929 ebd
->s
->swap_reloc_in (abfd
, native_reloc
, & rela
);
13930 else /* entsize == ebd->s->sizeof_rela */
13931 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
13933 /* The address of an ELF reloc is section relative for an object
13934 file, and absolute for an executable file or shared library.
13935 The address of a normal BFD reloc is always section relative,
13936 and the address of a dynamic reloc is absolute.. */
13937 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
13938 internal_reloc
->address
= rela
.r_offset
;
13940 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
13942 if (r_sym (rela
.r_info
) == STN_UNDEF
)
13944 /* FIXME: This and the error case below mean that we
13945 have a symbol on relocs that is not elf_symbol_type. */
13946 internal_reloc
->sym_ptr_ptr
= &bfd_abs_section_ptr
->symbol
;
13948 else if (r_sym (rela
.r_info
) > symcount
)
13951 /* xgettext:c-format */
13952 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13953 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
13954 bfd_set_error (bfd_error_bad_value
);
13955 internal_reloc
->sym_ptr_ptr
= &bfd_abs_section_ptr
->symbol
;
13962 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
13963 internal_reloc
->sym_ptr_ptr
= ps
;
13964 /* Make sure that this symbol is not removed by strip. */
13965 (*ps
)->flags
|= BSF_KEEP
;
13968 internal_reloc
->addend
= rela
.r_addend
;
13970 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
13971 if (! res
|| internal_reloc
->howto
== NULL
)
13973 #if DEBUG_SECONDARY_RELOCS
13975 "there is no howto associated with reloc %lx\n",
13982 free (native_relocs
);
13983 /* Store the internal relocs. */
13984 elf_section_data (relsec
)->sec_info
= internal_relocs
;
13991 /* Set the ELF section header fields of an output secondary reloc section. */
13994 _bfd_elf_copy_special_section_fields (const bfd
*ibfd ATTRIBUTE_UNUSED
,
13995 bfd
*obfd ATTRIBUTE_UNUSED
,
13996 const Elf_Internal_Shdr
*isection
,
13997 Elf_Internal_Shdr
*osection
)
14001 struct bfd_elf_section_data
* esd
;
14003 if (isection
== NULL
)
14006 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
14009 isec
= isection
->bfd_section
;
14013 osec
= osection
->bfd_section
;
14017 esd
= elf_section_data (osec
);
14018 BFD_ASSERT (esd
->sec_info
== NULL
);
14019 esd
->sec_info
= elf_section_data (isec
)->sec_info
;
14020 osection
->sh_type
= SHT_RELA
;
14021 osection
->sh_link
= elf_onesymtab (obfd
);
14022 if (osection
->sh_link
== 0)
14024 /* There is no symbol table - we are hosed... */
14026 /* xgettext:c-format */
14027 (_("%pB(%pA): link section cannot be set"
14028 " because the output file does not have a symbol table"),
14030 bfd_set_error (bfd_error_bad_value
);
14034 /* Find the output section that corresponds to the isection's
14036 if (isection
->sh_info
== 0
14037 || isection
->sh_info
>= elf_numsections (ibfd
))
14040 /* xgettext:c-format */
14041 (_("%pB(%pA): info section index is invalid"),
14043 bfd_set_error (bfd_error_bad_value
);
14047 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
14049 if (isection
== NULL
14050 || isection
->bfd_section
== NULL
14051 || isection
->bfd_section
->output_section
== NULL
)
14054 /* xgettext:c-format */
14055 (_("%pB(%pA): info section index cannot be set"
14056 " because the section is not in the output"),
14058 bfd_set_error (bfd_error_bad_value
);
14062 esd
= elf_section_data (isection
->bfd_section
->output_section
);
14063 BFD_ASSERT (esd
!= NULL
);
14064 osection
->sh_info
= esd
->this_idx
;
14065 esd
->has_secondary_relocs
= true;
14066 #if DEBUG_SECONDARY_RELOCS
14067 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
14068 osec
->name
, osection
->sh_link
, osection
->sh_info
);
14069 fprintf (stderr
, "mark section %s as having secondary relocs\n",
14070 bfd_section_name (isection
->bfd_section
->output_section
));
14076 /* Write out a secondary reloc section.
14078 FIXME: Currently this function can result in a serious performance penalty
14079 for files with secondary relocs and lots of sections. The proper way to
14080 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14081 relocs together and then to have this function just walk that chain. */
14084 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
14086 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
14087 bfd_vma addr_offset
;
14089 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
14090 bool result
= true;
14095 #if BFD_DEFAULT_TARGET_SIZE > 32
14096 if (bfd_arch_bits_per_address (abfd
) != 32)
14097 r_info
= elf64_r_info
;
14100 r_info
= elf32_r_info
;
14102 /* The address of an ELF reloc is section relative for an object
14103 file, and absolute for an executable file or shared library.
14104 The address of a BFD reloc is always section relative. */
14106 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
14107 addr_offset
= sec
->vma
;
14109 /* Discover if there are any secondary reloc sections
14110 associated with SEC. */
14111 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
14113 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
14114 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
14116 if (hdr
->sh_type
== SHT_RELA
14117 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
14119 asymbol
* last_sym
;
14121 size_t reloc_count
;
14123 bfd_size_type entsize
;
14124 arelent
* src_irel
;
14125 bfd_byte
* dst_rela
;
14127 if (hdr
->contents
!= NULL
)
14130 /* xgettext:c-format */
14131 (_("%pB(%pA): error: secondary reloc section processed twice"),
14133 bfd_set_error (bfd_error_bad_value
);
14138 entsize
= hdr
->sh_entsize
;
14142 /* xgettext:c-format */
14143 (_("%pB(%pA): error: secondary reloc section"
14144 " has zero sized entries"),
14146 bfd_set_error (bfd_error_bad_value
);
14150 else if (entsize
!= ebd
->s
->sizeof_rel
14151 && entsize
!= ebd
->s
->sizeof_rela
)
14154 /* xgettext:c-format */
14155 (_("%pB(%pA): error: secondary reloc section"
14156 " has non-standard sized entries"),
14158 bfd_set_error (bfd_error_bad_value
);
14163 reloc_count
= hdr
->sh_size
/ entsize
;
14164 hdr
->sh_size
= entsize
* reloc_count
;
14165 if (reloc_count
== 0)
14168 /* xgettext:c-format */
14169 (_("%pB(%pA): error: secondary reloc section is empty!"),
14171 bfd_set_error (bfd_error_bad_value
);
14176 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
14177 if (hdr
->contents
== NULL
)
14179 relsec
->alloced
= 1;
14181 #if DEBUG_SECONDARY_RELOCS
14182 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
14183 reloc_count
, sec
->name
, relsec
->name
);
14187 dst_rela
= hdr
->contents
;
14188 src_irel
= (arelent
*) esd
->sec_info
;
14189 if (src_irel
== NULL
)
14192 /* xgettext:c-format */
14193 (_("%pB(%pA): error: internal relocs missing"
14194 " for secondary reloc section"),
14196 bfd_set_error (bfd_error_bad_value
);
14201 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= entsize
)
14203 Elf_Internal_Rela src_rela
;
14208 ptr
= src_irel
+ idx
;
14212 /* xgettext:c-format */
14213 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14214 abfd
, relsec
, idx
);
14215 bfd_set_error (bfd_error_bad_value
);
14220 if (ptr
->sym_ptr_ptr
== NULL
)
14222 /* FIXME: Is this an error ? */
14227 sym
= *ptr
->sym_ptr_ptr
;
14229 if (sym
== last_sym
)
14233 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
14237 /* xgettext:c-format */
14238 (_("%pB(%pA): error: secondary reloc %zu"
14239 " references a missing symbol"),
14240 abfd
, relsec
, idx
);
14241 bfd_set_error (bfd_error_bad_value
);
14250 if (sym
->the_bfd
!= NULL
14251 && sym
->the_bfd
->xvec
!= abfd
->xvec
14252 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
14255 /* xgettext:c-format */
14256 (_("%pB(%pA): error: secondary reloc %zu"
14257 " references a deleted symbol"),
14258 abfd
, relsec
, idx
);
14259 bfd_set_error (bfd_error_bad_value
);
14265 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
14266 if (ptr
->howto
== NULL
)
14269 /* xgettext:c-format */
14270 (_("%pB(%pA): error: secondary reloc %zu"
14271 " is of an unknown type"),
14272 abfd
, relsec
, idx
);
14273 bfd_set_error (bfd_error_bad_value
);
14275 src_rela
.r_info
= r_info (0, 0);
14278 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
14279 src_rela
.r_addend
= ptr
->addend
;
14281 if (entsize
== ebd
->s
->sizeof_rel
)
14282 ebd
->s
->swap_reloc_out (abfd
, &src_rela
, dst_rela
);
14283 else /* entsize == ebd->s->sizeof_rela */
14284 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);
14292 /* Mmap in section contents. */
14295 elf_mmap_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**buf
)
14298 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14300 && sec
->compress_status
== COMPRESS_SECTION_NONE
14301 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14303 /* Use mmap only if section size >= the minimum mmap section
14305 size_t readsz
= bfd_get_section_limit_octets (abfd
, sec
);
14306 size_t allocsz
= bfd_get_section_alloc_size (abfd
, sec
);
14307 if (readsz
== allocsz
&& readsz
>= _bfd_minimum_mmap_size
)
14309 if (sec
->contents
!= NULL
)
14311 if (!sec
->mmapped_p
)
14313 *buf
= sec
->contents
;
14316 if (sec
->mmapped_p
)
14318 sec
->mmapped_p
= 1;
14320 /* We can't use the final link preallocated buffer for mmap. */
14325 /* FIXME: We should not get here if sec->alloced is set. */
14326 bool ret
= bfd_get_full_section_contents (abfd
, sec
, buf
);
14327 if (ret
&& sec
->mmapped_p
)
14328 *buf
= sec
->contents
;
14332 /* Mmap in section contents. */
14335 _bfd_elf_mmap_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**buf
)
14338 return elf_mmap_section_contents (abfd
, sec
, buf
);
14341 /* Mmap in the full section contents for the final link. */
14344 _bfd_elf_link_mmap_section_contents (bfd
*abfd
, sec_ptr sec
,
14347 return elf_mmap_section_contents (abfd
, sec
, buf
);
14350 /* Munmap section contents. */
14353 _bfd_elf_munmap_section_contents (asection
*sec
, void *contents
)
14355 /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14356 CONTENTS may be NULL. */
14357 if (contents
== NULL
)
14361 /* What a tangled web we weave with section contents.
14362 FIXME: We shouldn't need to test anything but sec->alloced
14363 here, but there are cases where a buffer is allocated for a
14364 section but then another buffer is malloc'd anyway. eg.
14365 trace through ld-elf/eh4 testcase on x86_64. */
14366 && (sec
->contents
== contents
14367 || elf_section_data (sec
)->this_hdr
.contents
== contents
))
14370 /* Don't leave pointers to data we are about to munmap or free. */
14371 if (sec
->contents
== contents
)
14372 sec
->contents
= NULL
;
14373 if (elf_section_data (sec
)->this_hdr
.contents
== contents
)
14374 elf_section_data (sec
)->this_hdr
.contents
= NULL
;
14377 if (sec
->mmapped_p
)
14379 /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14380 malloced, CONTENTS_ADDR is set to NULL. */
14381 if (elf_section_data (sec
)->contents_addr
!= NULL
)
14383 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14384 if (munmap (elf_section_data (sec
)->contents_addr
,
14385 elf_section_data (sec
)->contents_size
) != 0)
14387 sec
->mmapped_p
= 0;
14388 elf_section_data (sec
)->contents_addr
= NULL
;
14389 elf_section_data (sec
)->contents_size
= 0;
14398 /* Munmap the full section contents for the final link. */
14401 _bfd_elf_link_munmap_section_contents (asection
*sec ATTRIBUTE_UNUSED
)
14404 if (sec
->mmapped_p
&& elf_section_data (sec
)->contents_addr
!= NULL
)
14406 /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14407 malloced, CONTENTS_ADDR is set to NULL. */
14408 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14409 if (munmap (elf_section_data (sec
)->contents_addr
,
14410 elf_section_data (sec
)->contents_size
) != 0)
14412 sec
->mmapped_p
= 0;
14413 sec
->contents
= NULL
;
14414 elf_section_data (sec
)->this_hdr
.contents
= NULL
;
14415 elf_section_data (sec
)->contents_addr
= NULL
;
14416 elf_section_data (sec
)->contents_size
= 0;