1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2024 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 to OBJECT_ID. */
227 bfd_elf_allocate_object (bfd
*abfd
,
229 enum elf_target_id object_id
)
231 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
232 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
233 if (abfd
->tdata
.any
== NULL
)
236 elf_object_id (abfd
) = object_id
;
237 if (abfd
->direction
!= read_direction
)
239 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
242 elf_tdata (abfd
)->o
= o
;
243 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
250 bfd_elf_make_object (bfd
*abfd
)
252 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
253 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
258 bfd_elf_mkcorefile (bfd
*abfd
)
260 /* I think this can be done just like an object file. */
261 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
263 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
264 return elf_tdata (abfd
)->core
!= NULL
;
268 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
270 Elf_Internal_Shdr
**i_shdrp
;
271 bfd_byte
*shstrtab
= NULL
;
273 bfd_size_type shstrtabsize
;
275 i_shdrp
= elf_elfsections (abfd
);
277 || shindex
>= elf_numsections (abfd
)
278 || i_shdrp
[shindex
] == 0)
281 shstrtab
= i_shdrp
[shindex
]->contents
;
282 if (shstrtab
== NULL
)
284 /* No cached one, attempt to read, and cache what we read. */
285 offset
= i_shdrp
[shindex
]->sh_offset
;
286 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize
+ 1 <= 1
291 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
293 = _bfd_mmap_readonly_persistent (abfd
, shstrtabsize
)) == NULL
)
295 /* Once we've failed to read it, make sure we don't keep
296 trying. Otherwise, we'll keep allocating space for
297 the string table over and over. */
298 i_shdrp
[shindex
]->sh_size
= 0;
300 else if (shstrtab
[shstrtabsize
- 1] != '\0')
302 /* It is an error if a string table isn't terminated. */
304 /* xgettext:c-format */
305 (_("%pB(%pA): string table is corrupt"),
306 abfd
, i_shdrp
[shindex
]->bfd_section
);
309 i_shdrp
[shindex
]->contents
= shstrtab
;
311 return (char *) shstrtab
;
315 bfd_elf_string_from_elf_section (bfd
*abfd
,
316 unsigned int shindex
,
317 unsigned int strindex
)
319 Elf_Internal_Shdr
*hdr
;
324 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
327 hdr
= elf_elfsections (abfd
)[shindex
];
329 if (hdr
->contents
== NULL
)
331 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
333 /* PR 17512: file: f057ec89. */
334 /* xgettext:c-format */
335 _bfd_error_handler (_("%pB: attempt to load strings from"
336 " a non-string section (number %d)"),
341 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
346 /* PR 24273: The string section's contents may have already
347 been loaded elsewhere, eg because a corrupt file has the
348 string section index in the ELF header pointing at a group
349 section. So be paranoid, and test that the last byte of
350 the section is zero. */
351 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
355 if (strindex
>= hdr
->sh_size
)
357 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
359 /* xgettext:c-format */
360 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
361 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
362 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
364 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
368 return ((char *) hdr
->contents
) + strindex
;
371 /* Read and convert symbols to internal format.
372 SYMCOUNT specifies the number of symbols to read, starting from
373 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
374 are non-NULL, they are used to store the internal symbols, external
375 symbols, and symbol section index extensions, respectively.
376 Returns a pointer to the internal symbol buffer (malloced if necessary)
377 or NULL if there were no symbols or some kind of problem. */
380 bfd_elf_get_elf_syms (bfd
*ibfd
,
381 Elf_Internal_Shdr
*symtab_hdr
,
384 Elf_Internal_Sym
*intsym_buf
,
386 Elf_External_Sym_Shndx
*extshndx_buf
)
388 Elf_Internal_Shdr
*shndx_hdr
;
390 const bfd_byte
*esym
;
391 Elf_External_Sym_Shndx
*alloc_extshndx
;
392 Elf_External_Sym_Shndx
*shndx
;
393 Elf_Internal_Sym
*alloc_intsym
;
394 Elf_Internal_Sym
*isym
;
395 Elf_Internal_Sym
*isymend
;
396 const struct elf_backend_data
*bed
;
401 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
407 if (elf_use_dt_symtab_p (ibfd
))
409 /* Use dynamic symbol table. */
410 if (elf_tdata (ibfd
)->dt_symtab_count
!= symcount
+ symoffset
)
412 bfd_set_error (bfd_error_invalid_operation
);
415 return elf_tdata (ibfd
)->dt_symtab
+ symoffset
;
418 /* Normal syms might have section extension entries. */
420 if (elf_symtab_shndx_list (ibfd
) != NULL
)
422 elf_section_list
* entry
;
423 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
425 /* Find an index section that is linked to this symtab section. */
426 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
429 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
432 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
434 shndx_hdr
= & entry
->hdr
;
439 if (shndx_hdr
== NULL
)
441 if (symtab_hdr
== &elf_symtab_hdr (ibfd
))
442 /* Not really accurate, but this was how the old code used
444 shndx_hdr
= &elf_symtab_shndx_list (ibfd
)->hdr
;
445 /* Otherwise we do nothing. The assumption is that
446 the index table will not be needed. */
450 /* Read the symbols. */
452 alloc_extshndx
= NULL
;
454 bed
= get_elf_backend_data (ibfd
);
455 extsym_size
= bed
->s
->sizeof_sym
;
456 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
458 bfd_set_error (bfd_error_file_too_big
);
461 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
462 size_t alloc_ext_size
= amt
;
463 if (bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
464 || !_bfd_mmap_read_temporary (&extsym_buf
, &alloc_ext_size
,
465 &alloc_ext
, ibfd
, false))
471 size_t alloc_extshndx_size
= 0;
472 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
476 if (_bfd_mul_overflow (symcount
, sizeof (Elf_External_Sym_Shndx
), &amt
))
478 bfd_set_error (bfd_error_file_too_big
);
482 alloc_extshndx_size
= amt
;
483 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
484 if (bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
485 || !_bfd_mmap_read_temporary ((void **) &extshndx_buf
,
486 &alloc_extshndx_size
,
487 (void **) &alloc_extshndx
,
495 if (intsym_buf
== NULL
)
497 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
499 bfd_set_error (bfd_error_file_too_big
);
502 alloc_intsym
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
503 intsym_buf
= alloc_intsym
;
504 if (intsym_buf
== NULL
)
508 /* Convert the symbols to internal form. */
509 isymend
= intsym_buf
+ symcount
;
510 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
511 shndx
= extshndx_buf
;
513 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
514 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
516 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
517 /* xgettext:c-format */
518 _bfd_error_handler (_("%pB symbol number %lu references"
519 " nonexistent SHT_SYMTAB_SHNDX section"),
520 ibfd
, (unsigned long) symoffset
);
527 _bfd_munmap_readonly_temporary (alloc_extshndx
, alloc_extshndx_size
);
529 _bfd_munmap_readonly_temporary (alloc_ext
, alloc_ext_size
);
534 /* Look up a symbol name. */
536 bfd_elf_sym_name (bfd
*abfd
,
537 Elf_Internal_Shdr
*symtab_hdr
,
538 Elf_Internal_Sym
*isym
,
542 unsigned int iname
= isym
->st_name
;
543 unsigned int shindex
= symtab_hdr
->sh_link
;
545 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
546 /* Check for a bogus st_shndx to avoid crashing. */
547 && isym
->st_shndx
< elf_numsections (abfd
))
549 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
550 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
553 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
556 else if (sym_sec
&& *name
== '\0')
557 name
= bfd_section_name (sym_sec
);
562 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
563 sections. The first element is the flags, the rest are section
566 typedef union elf_internal_group
{
567 Elf_Internal_Shdr
*shdr
;
569 } Elf_Internal_Group
;
571 /* Return the name of the group signature symbol. Why isn't the
572 signature just a string? */
575 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
577 Elf_Internal_Shdr
*hdr
;
578 unsigned char esym
[sizeof (Elf64_External_Sym
)];
579 Elf_External_Sym_Shndx eshndx
;
580 Elf_Internal_Sym isym
;
582 /* First we need to ensure the symbol table is available. Make sure
583 that it is a symbol table section. */
584 if (ghdr
->sh_link
>= elf_numsections (abfd
))
586 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
587 if (hdr
->sh_type
!= SHT_SYMTAB
588 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
591 /* Go read the symbol. */
592 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
593 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
594 &isym
, esym
, &eshndx
) == NULL
)
597 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
600 /* Set next_in_group list pointer, and group name for NEWSECT. */
603 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
605 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
607 /* If num_group is zero, read in all SHT_GROUP sections. The count
608 is set to -1 if there are no SHT_GROUP sections. */
611 unsigned int i
, shnum
;
613 /* First count the number of groups. If we have a SHT_GROUP
614 section with just a flag word (ie. sh_size is 4), ignore it. */
615 shnum
= elf_numsections (abfd
);
618 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
619 ( (shdr)->sh_type == SHT_GROUP \
620 && (shdr)->sh_size >= minsize \
621 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
622 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
624 for (i
= 0; i
< shnum
; i
++)
626 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
628 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
634 num_group
= (unsigned) -1;
635 elf_tdata (abfd
)->num_group
= num_group
;
636 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
640 /* We keep a list of elf section headers for group sections,
641 so we can find them quickly. */
644 elf_tdata (abfd
)->num_group
= num_group
;
645 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
646 elf_tdata (abfd
)->group_sect_ptr
647 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
648 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
652 for (i
= 0; i
< shnum
; i
++)
654 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
656 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
659 Elf_Internal_Group
*dest
;
661 /* Make sure the group section has a BFD section
663 if (!bfd_section_from_shdr (abfd
, i
))
666 /* Add to list of sections. */
667 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
670 /* Read the raw contents. */
671 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
672 shdr
->contents
= NULL
;
673 if (_bfd_mul_overflow (shdr
->sh_size
,
674 sizeof (*dest
) / 4, &amt
)
675 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
677 = _bfd_alloc_and_read (abfd
, amt
, shdr
->sh_size
)))
680 /* xgettext:c-format */
681 (_("%pB: invalid size field in group section"
682 " header: %#" PRIx64
""),
683 abfd
, (uint64_t) shdr
->sh_size
);
684 bfd_set_error (bfd_error_bad_value
);
689 /* Translate raw contents, a flag word followed by an
690 array of elf section indices all in target byte order,
691 to the flag word followed by an array of elf section
693 src
= shdr
->contents
+ shdr
->sh_size
;
694 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
702 idx
= H_GET_32 (abfd
, src
);
703 if (src
== shdr
->contents
)
707 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
708 shdr
->bfd_section
->flags
709 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
714 dest
->shdr
= elf_elfsections (abfd
)[idx
];
715 /* PR binutils/23199: All sections in a
716 section group should be marked with
717 SHF_GROUP. But some tools generate
718 broken objects without SHF_GROUP. Fix
720 dest
->shdr
->sh_flags
|= SHF_GROUP
;
723 || dest
->shdr
->sh_type
== SHT_GROUP
)
726 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
734 /* PR 17510: Corrupt binaries might contain invalid groups. */
735 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
737 elf_tdata (abfd
)->num_group
= num_group
;
739 /* If all groups are invalid then fail. */
742 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
743 elf_tdata (abfd
)->num_group
= num_group
= -1;
745 (_("%pB: no valid group sections found"), abfd
);
746 bfd_set_error (bfd_error_bad_value
);
752 if (num_group
!= (unsigned) -1)
754 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
757 for (j
= 0; j
< num_group
; j
++)
759 /* Begin search from previous found group. */
760 unsigned i
= (j
+ search_offset
) % num_group
;
762 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
763 Elf_Internal_Group
*idx
;
769 idx
= (Elf_Internal_Group
*) shdr
->contents
;
770 if (idx
== NULL
|| shdr
->sh_size
< 4)
772 /* See PR 21957 for a reproducer. */
773 /* xgettext:c-format */
774 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
775 abfd
, shdr
->bfd_section
);
776 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
777 bfd_set_error (bfd_error_bad_value
);
780 n_elt
= shdr
->sh_size
/ 4;
782 /* Look through this group's sections to see if current
783 section is a member. */
785 if ((++idx
)->shdr
== hdr
)
789 /* We are a member of this group. Go looking through
790 other members to see if any others are linked via
792 idx
= (Elf_Internal_Group
*) shdr
->contents
;
793 n_elt
= shdr
->sh_size
/ 4;
795 if ((++idx
)->shdr
!= NULL
796 && (s
= idx
->shdr
->bfd_section
) != NULL
797 && elf_next_in_group (s
) != NULL
)
801 /* Snarf the group name from other member, and
802 insert current section in circular list. */
803 elf_group_name (newsect
) = elf_group_name (s
);
804 elf_next_in_group (newsect
) = elf_next_in_group (s
);
805 elf_next_in_group (s
) = newsect
;
811 gname
= group_signature (abfd
, shdr
);
814 elf_group_name (newsect
) = gname
;
816 /* Start a circular list with one element. */
817 elf_next_in_group (newsect
) = newsect
;
820 /* If the group section has been created, point to the
822 if (shdr
->bfd_section
!= NULL
)
823 elf_next_in_group (shdr
->bfd_section
) = newsect
;
825 elf_tdata (abfd
)->group_search_offset
= i
;
832 if (elf_group_name (newsect
) == NULL
)
834 /* xgettext:c-format */
835 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
837 /* PR 29532: Return true here, even though the group info has not been
838 read. Separate debug info files can have empty group sections, but
839 we do not want this to prevent them from being loaded as otherwise
840 GDB will not be able to use them. */
847 _bfd_elf_setup_sections (bfd
*abfd
)
850 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
854 /* Process SHF_LINK_ORDER. */
855 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
857 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
858 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
860 unsigned int elfsec
= this_hdr
->sh_link
;
861 /* An sh_link value of 0 is now allowed. It indicates that linked
862 to section has already been discarded, but that the current
863 section has been retained for some other reason. This linking
864 section is still a candidate for later garbage collection
868 elf_linked_to_section (s
) = NULL
;
872 asection
*linksec
= NULL
;
874 if (elfsec
< elf_numsections (abfd
))
876 this_hdr
= elf_elfsections (abfd
)[elfsec
];
877 linksec
= this_hdr
->bfd_section
;
881 Some strip/objcopy may leave an incorrect value in
882 sh_link. We don't want to proceed. */
886 /* xgettext:c-format */
887 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
888 s
->owner
, elfsec
, s
);
892 elf_linked_to_section (s
) = linksec
;
895 else if (this_hdr
->sh_type
== SHT_GROUP
896 && elf_next_in_group (s
) == NULL
)
899 /* xgettext:c-format */
900 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
901 abfd
, elf_section_data (s
)->this_idx
);
906 /* Process section groups. */
907 if (num_group
== (unsigned) -1)
910 for (i
= 0; i
< num_group
; i
++)
912 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
913 Elf_Internal_Group
*idx
;
916 /* PR binutils/18758: Beware of corrupt binaries with invalid
918 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
921 /* xgettext:c-format */
922 (_("%pB: section group entry number %u is corrupt"),
928 idx
= (Elf_Internal_Group
*) shdr
->contents
;
929 n_elt
= shdr
->sh_size
/ 4;
935 if (idx
->shdr
== NULL
)
937 else if (idx
->shdr
->bfd_section
)
938 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
939 else if (idx
->shdr
->sh_type
!= SHT_RELA
940 && idx
->shdr
->sh_type
!= SHT_REL
)
942 /* There are some unknown sections in the group. */
944 /* xgettext:c-format */
945 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
948 bfd_elf_string_from_elf_section (abfd
,
949 (elf_elfheader (abfd
)
962 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
964 return elf_next_in_group (sec
) != NULL
;
968 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
970 if (elf_sec_group (sec
) != NULL
)
971 return elf_group_name (sec
);
975 /* Make a BFD section from an ELF section. We store a pointer to the
976 BFD section in the bfd_section field of the header. */
979 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
980 Elf_Internal_Shdr
*hdr
,
986 const struct elf_backend_data
*bed
;
987 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
989 if (hdr
->bfd_section
!= NULL
)
992 newsect
= bfd_make_section_anyway (abfd
, name
);
996 hdr
->bfd_section
= newsect
;
997 elf_section_data (newsect
)->this_hdr
= *hdr
;
998 elf_section_data (newsect
)->this_idx
= shindex
;
1000 /* Always use the real type/flags. */
1001 elf_section_type (newsect
) = hdr
->sh_type
;
1002 elf_section_flags (newsect
) = hdr
->sh_flags
;
1004 newsect
->filepos
= hdr
->sh_offset
;
1006 flags
= SEC_NO_FLAGS
;
1007 if (hdr
->sh_type
!= SHT_NOBITS
)
1008 flags
|= SEC_HAS_CONTENTS
;
1009 if (hdr
->sh_type
== SHT_GROUP
)
1011 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1014 if (hdr
->sh_type
!= SHT_NOBITS
)
1017 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1018 flags
|= SEC_READONLY
;
1019 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1021 else if ((flags
& SEC_LOAD
) != 0)
1023 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1026 newsect
->entsize
= hdr
->sh_entsize
;
1028 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1029 flags
|= SEC_STRINGS
;
1030 if (hdr
->sh_flags
& SHF_GROUP
)
1031 if (!setup_group (abfd
, hdr
, newsect
))
1033 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1034 flags
|= SEC_THREAD_LOCAL
;
1035 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1036 flags
|= SEC_EXCLUDE
;
1038 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1040 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1041 but binutils as of 2019-07-23 did not set the EI_OSABI header
1044 case ELFOSABI_FREEBSD
:
1045 if ((hdr
->sh_flags
& SHF_GNU_RETAIN
) != 0)
1046 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_retain
;
1049 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1050 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1054 if ((flags
& SEC_ALLOC
) == 0)
1056 /* The debugging sections appear to be recognized only by name,
1057 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1058 if (name
[0] == '.')
1060 if (startswith (name
, ".debug")
1061 || startswith (name
, ".gnu.debuglto_.debug_")
1062 || startswith (name
, ".gnu.linkonce.wi.")
1063 || startswith (name
, ".zdebug"))
1064 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1065 else if (startswith (name
, GNU_BUILD_ATTRS_SECTION_NAME
)
1066 || startswith (name
, ".note.gnu"))
1068 flags
|= SEC_ELF_OCTETS
;
1071 else if (startswith (name
, ".line")
1072 || startswith (name
, ".stab")
1073 || strcmp (name
, ".gdb_index") == 0)
1074 flags
|= SEC_DEBUGGING
;
1078 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1079 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1080 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
1081 & -hdr
->sh_addralign
)))
1084 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1085 only link a single copy of the section. This is used to support
1086 g++. g++ will emit each template expansion in its own section.
1087 The symbols will be defined as weak, so that multiple definitions
1088 are permitted. The GNU linker extension is to actually discard
1089 all but one of the sections. */
1090 if (startswith (name
, ".gnu.linkonce")
1091 && elf_next_in_group (newsect
) == NULL
)
1092 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1094 if (!bfd_set_section_flags (newsect
, flags
))
1097 bed
= get_elf_backend_data (abfd
);
1098 if (bed
->elf_backend_section_flags
)
1099 if (!bed
->elf_backend_section_flags (hdr
))
1102 /* We do not parse the PT_NOTE segments as we are interested even in the
1103 separate debug info files which may have the segments offsets corrupted.
1104 PT_NOTEs from the core files are currently not parsed using BFD. */
1105 if (hdr
->sh_type
== SHT_NOTE
&& hdr
->sh_size
!= 0)
1109 if (!_bfd_elf_mmap_section_contents (abfd
, newsect
, &contents
))
1112 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1113 hdr
->sh_offset
, hdr
->sh_addralign
);
1114 _bfd_elf_munmap_section_contents (newsect
, contents
);
1117 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1119 Elf_Internal_Phdr
*phdr
;
1120 unsigned int i
, nload
;
1122 /* Some ELF linkers produce binaries with all the program header
1123 p_paddr fields zero. If we have such a binary with more than
1124 one PT_LOAD header, then leave the section lma equal to vma
1125 so that we don't create sections with overlapping lma. */
1126 phdr
= elf_tdata (abfd
)->phdr
;
1127 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1128 if (phdr
->p_paddr
!= 0)
1130 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1132 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1135 phdr
= elf_tdata (abfd
)->phdr
;
1136 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1138 if (((phdr
->p_type
== PT_LOAD
1139 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1140 || phdr
->p_type
== PT_TLS
)
1141 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1143 if ((newsect
->flags
& SEC_LOAD
) == 0)
1144 newsect
->lma
= (phdr
->p_paddr
1145 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1147 /* We used to use the same adjustment for SEC_LOAD
1148 sections, but that doesn't work if the segment
1149 is packed with code from multiple VMAs.
1150 Instead we calculate the section LMA based on
1151 the segment LMA. It is assumed that the
1152 segment will contain sections with contiguous
1153 LMAs, even if the VMAs are not. */
1154 newsect
->lma
= (phdr
->p_paddr
1155 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1157 /* With contiguous segments, we can't tell from file
1158 offsets whether a section with zero size should
1159 be placed at the end of one segment or the
1160 beginning of the next. Decide based on vaddr. */
1161 if (hdr
->sh_addr
>= phdr
->p_vaddr
1162 && (hdr
->sh_addr
+ hdr
->sh_size
1163 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1169 /* Compress/decompress DWARF debug sections with names: .debug_*,
1170 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1171 if ((newsect
->flags
& SEC_DEBUGGING
) != 0
1172 && (newsect
->flags
& SEC_HAS_CONTENTS
) != 0
1173 && (newsect
->flags
& SEC_ELF_OCTETS
) != 0)
1175 enum { nothing
, compress
, decompress
} action
= nothing
;
1176 int compression_header_size
;
1177 bfd_size_type uncompressed_size
;
1178 unsigned int uncompressed_align_power
;
1179 enum compression_type ch_type
= ch_none
;
1181 = bfd_is_section_compressed_info (abfd
, newsect
,
1182 &compression_header_size
,
1184 &uncompressed_align_power
,
1187 /* Should we decompress? */
1188 if ((abfd
->flags
& BFD_DECOMPRESS
) != 0 && compressed
)
1189 action
= decompress
;
1191 /* Should we compress? Or convert to a different compression? */
1192 else if ((abfd
->flags
& BFD_COMPRESS
) != 0
1193 && newsect
->size
!= 0
1194 && compression_header_size
>= 0
1195 && uncompressed_size
> 0)
1201 enum compression_type new_ch_type
= ch_none
;
1202 if ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0)
1203 new_ch_type
= ((abfd
->flags
& BFD_COMPRESS_ZSTD
) != 0
1204 ? ch_compress_zstd
: ch_compress_zlib
);
1205 if (new_ch_type
!= ch_type
)
1210 if (action
== compress
)
1212 if (!bfd_init_section_compress_status (abfd
, newsect
))
1215 /* xgettext:c-format */
1216 (_("%pB: unable to compress section %s"), abfd
, name
);
1220 else if (action
== decompress
)
1222 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1225 /* xgettext:c-format */
1226 (_("%pB: unable to decompress section %s"), abfd
, name
);
1230 if (newsect
->compress_status
== DECOMPRESS_SECTION_ZSTD
)
1233 /* xgettext:c-format */
1234 (_ ("%pB: section %s is compressed with zstd, but BFD "
1235 "is not built with zstd support"),
1237 newsect
->compress_status
= COMPRESS_SECTION_NONE
;
1241 if (abfd
->is_linker_input
1244 /* Rename section from .zdebug_* to .debug_* so that ld
1245 scripts will see this section as a debug section. */
1246 char *new_name
= bfd_zdebug_name_to_debug (abfd
, name
);
1247 if (new_name
== NULL
)
1249 bfd_rename_section (newsect
, new_name
);
1257 const char *const bfd_elf_section_type_names
[] =
1259 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1260 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1261 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1264 /* ELF relocs are against symbols. If we are producing relocatable
1265 output, and the reloc is against an external symbol, and nothing
1266 has given us any additional addend, the resulting reloc will also
1267 be against the same symbol. In such a case, we don't want to
1268 change anything about the way the reloc is handled, since it will
1269 all be done at final link time. Rather than put special case code
1270 into bfd_perform_relocation, all the reloc types use this howto
1271 function, or should call this function for relocatable output. */
1273 bfd_reloc_status_type
1274 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1275 arelent
*reloc_entry
,
1277 void *data ATTRIBUTE_UNUSED
,
1278 asection
*input_section
,
1280 char **error_message ATTRIBUTE_UNUSED
)
1282 if (output_bfd
!= NULL
1283 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1284 && (! reloc_entry
->howto
->partial_inplace
1285 || reloc_entry
->addend
== 0))
1287 reloc_entry
->address
+= input_section
->output_offset
;
1288 return bfd_reloc_ok
;
1291 /* In some cases the relocation should be treated as output section
1292 relative, as when linking ELF DWARF into PE COFF. Many ELF
1293 targets lack section relative relocations and instead use
1294 ordinary absolute relocations for references between DWARF
1295 sections. That is arguably a bug in those targets but it happens
1296 to work for the usual case of linking to non-loaded ELF debug
1297 sections with VMAs forced to zero. PE COFF on the other hand
1298 doesn't allow a section VMA of zero. */
1299 if (output_bfd
== NULL
1300 && !reloc_entry
->howto
->pc_relative
1301 && (symbol
->section
->flags
& SEC_DEBUGGING
) != 0
1302 && (input_section
->flags
& SEC_DEBUGGING
) != 0)
1303 reloc_entry
->addend
-= symbol
->section
->output_section
->vma
;
1305 return bfd_reloc_continue
;
1308 /* Returns TRUE if section A matches section B.
1309 Names, addresses and links may be different, but everything else
1310 should be the same. */
1313 section_match (const Elf_Internal_Shdr
* a
,
1314 const Elf_Internal_Shdr
* b
)
1316 if (a
->sh_type
!= b
->sh_type
1317 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1318 || a
->sh_addralign
!= b
->sh_addralign
1319 || a
->sh_entsize
!= b
->sh_entsize
)
1321 if (a
->sh_type
== SHT_SYMTAB
1322 || a
->sh_type
== SHT_STRTAB
)
1324 return a
->sh_size
== b
->sh_size
;
1327 /* Find a section in OBFD that has the same characteristics
1328 as IHEADER. Return the index of this section or SHN_UNDEF if
1329 none can be found. Check's section HINT first, as this is likely
1330 to be the correct section. */
1333 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1334 const unsigned int hint
)
1336 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1339 BFD_ASSERT (iheader
!= NULL
);
1341 /* See PR 20922 for a reproducer of the NULL test. */
1342 if (hint
< elf_numsections (obfd
)
1343 && oheaders
[hint
] != NULL
1344 && section_match (oheaders
[hint
], iheader
))
1347 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1349 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1351 if (oheader
== NULL
)
1353 if (section_match (oheader
, iheader
))
1354 /* FIXME: Do we care if there is a potential for
1355 multiple matches ? */
1362 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1363 Processor specific section, based upon a matching input section.
1364 Returns TRUE upon success, FALSE otherwise. */
1367 copy_special_section_fields (const bfd
*ibfd
,
1369 const Elf_Internal_Shdr
*iheader
,
1370 Elf_Internal_Shdr
*oheader
,
1371 const unsigned int secnum
)
1373 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1374 const Elf_Internal_Shdr
**iheaders
1375 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1376 bool changed
= false;
1377 unsigned int sh_link
;
1379 if (oheader
->sh_type
== SHT_NOBITS
)
1381 /* This is a feature for objcopy --only-keep-debug:
1382 When a section's type is changed to NOBITS, we preserve
1383 the sh_link and sh_info fields so that they can be
1384 matched up with the original.
1386 Note: Strictly speaking these assignments are wrong.
1387 The sh_link and sh_info fields should point to the
1388 relevent sections in the output BFD, which may not be in
1389 the same location as they were in the input BFD. But
1390 the whole point of this action is to preserve the
1391 original values of the sh_link and sh_info fields, so
1392 that they can be matched up with the section headers in
1393 the original file. So strictly speaking we may be
1394 creating an invalid ELF file, but it is only for a file
1395 that just contains debug info and only for sections
1396 without any contents. */
1397 if (oheader
->sh_link
== 0)
1398 oheader
->sh_link
= iheader
->sh_link
;
1399 if (oheader
->sh_info
== 0)
1400 oheader
->sh_info
= iheader
->sh_info
;
1404 /* Allow the target a chance to decide how these fields should be set. */
1405 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1409 /* We have an iheader which might match oheader, and which has non-zero
1410 sh_info and/or sh_link fields. Attempt to follow those links and find
1411 the section in the output bfd which corresponds to the linked section
1412 in the input bfd. */
1413 if (iheader
->sh_link
!= SHN_UNDEF
)
1415 /* See PR 20931 for a reproducer. */
1416 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1419 /* xgettext:c-format */
1420 (_("%pB: invalid sh_link field (%d) in section number %d"),
1421 ibfd
, iheader
->sh_link
, secnum
);
1425 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1426 if (sh_link
!= SHN_UNDEF
)
1428 oheader
->sh_link
= sh_link
;
1432 /* FIXME: Should we install iheader->sh_link
1433 if we could not find a match ? */
1435 /* xgettext:c-format */
1436 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1439 if (iheader
->sh_info
)
1441 /* The sh_info field can hold arbitrary information, but if the
1442 SHF_LINK_INFO flag is set then it should be interpreted as a
1444 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1446 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1448 if (sh_link
!= SHN_UNDEF
)
1449 oheader
->sh_flags
|= SHF_INFO_LINK
;
1452 /* No idea what it means - just copy it. */
1453 sh_link
= iheader
->sh_info
;
1455 if (sh_link
!= SHN_UNDEF
)
1457 oheader
->sh_info
= sh_link
;
1462 /* xgettext:c-format */
1463 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1469 /* Copy the program header and other data from one object module to
1473 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1475 const Elf_Internal_Shdr
**iheaders
1476 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1477 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1478 const struct elf_backend_data
*bed
;
1481 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1482 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1485 if (!elf_flags_init (obfd
))
1487 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1488 elf_flags_init (obfd
) = true;
1491 elf_gp (obfd
) = elf_gp (ibfd
);
1493 /* Also copy the EI_OSABI field. */
1494 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1495 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1497 /* If set, copy the EI_ABIVERSION field. */
1498 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1499 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1500 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1502 /* Copy object attributes. */
1503 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1505 if (iheaders
== NULL
|| oheaders
== NULL
)
1508 bed
= get_elf_backend_data (obfd
);
1510 /* Possibly copy other fields in the section header. */
1511 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1514 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1516 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1517 because of a special case need for generating separate debug info
1518 files. See below for more details. */
1520 || (oheader
->sh_type
!= SHT_NOBITS
1521 && oheader
->sh_type
< SHT_LOOS
))
1524 /* Ignore empty sections, and sections whose
1525 fields have already been initialised. */
1526 if (oheader
->sh_size
== 0
1527 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1530 /* Scan for the matching section in the input bfd.
1531 First we try for a direct mapping between the input and
1533 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1535 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1537 if (iheader
== NULL
)
1540 if (oheader
->bfd_section
!= NULL
1541 && iheader
->bfd_section
!= NULL
1542 && iheader
->bfd_section
->output_section
!= NULL
1543 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1545 /* We have found a connection from the input section to
1546 the output section. Attempt to copy the header fields.
1547 If this fails then do not try any further sections -
1548 there should only be a one-to-one mapping between
1549 input and output. */
1550 if (!copy_special_section_fields (ibfd
, obfd
,
1551 iheader
, oheader
, i
))
1552 j
= elf_numsections (ibfd
);
1557 if (j
< elf_numsections (ibfd
))
1560 /* That failed. So try to deduce the corresponding input section.
1561 Unfortunately we cannot compare names as the output string table
1562 is empty, so instead we check size, address and type. */
1563 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1565 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1567 if (iheader
== NULL
)
1570 /* Try matching fields in the input section's header.
1571 Since --only-keep-debug turns all non-debug sections into
1572 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1574 if ((oheader
->sh_type
== SHT_NOBITS
1575 || iheader
->sh_type
== oheader
->sh_type
)
1576 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1577 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1578 && iheader
->sh_addralign
== oheader
->sh_addralign
1579 && iheader
->sh_entsize
== oheader
->sh_entsize
1580 && iheader
->sh_size
== oheader
->sh_size
1581 && iheader
->sh_addr
== oheader
->sh_addr
1582 && (iheader
->sh_info
!= oheader
->sh_info
1583 || iheader
->sh_link
!= oheader
->sh_link
))
1585 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1590 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1592 /* Final attempt. Call the backend copy function
1593 with a NULL input section. */
1594 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1603 get_segment_type (unsigned int p_type
)
1608 case PT_NULL
: pt
= "NULL"; break;
1609 case PT_LOAD
: pt
= "LOAD"; break;
1610 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1611 case PT_INTERP
: pt
= "INTERP"; break;
1612 case PT_NOTE
: pt
= "NOTE"; break;
1613 case PT_SHLIB
: pt
= "SHLIB"; break;
1614 case PT_PHDR
: pt
= "PHDR"; break;
1615 case PT_TLS
: pt
= "TLS"; break;
1616 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1617 case PT_GNU_STACK
: pt
= "STACK"; break;
1618 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1619 case PT_GNU_SFRAME
: pt
= "SFRAME"; break;
1620 default: pt
= NULL
; break;
1625 /* Print out the program headers. */
1628 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1630 FILE *f
= (FILE *) farg
;
1631 Elf_Internal_Phdr
*p
;
1633 bfd_byte
*dynbuf
= NULL
;
1635 p
= elf_tdata (abfd
)->phdr
;
1640 fprintf (f
, _("\nProgram Header:\n"));
1641 c
= elf_elfheader (abfd
)->e_phnum
;
1642 for (i
= 0; i
< c
; i
++, p
++)
1644 const char *pt
= get_segment_type (p
->p_type
);
1649 sprintf (buf
, "0x%lx", p
->p_type
);
1652 fprintf (f
, "%8s off 0x", pt
);
1653 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1654 fprintf (f
, " vaddr 0x");
1655 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1656 fprintf (f
, " paddr 0x");
1657 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1658 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1659 fprintf (f
, " filesz 0x");
1660 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1661 fprintf (f
, " memsz 0x");
1662 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1663 fprintf (f
, " flags %c%c%c",
1664 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1665 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1666 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1667 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1668 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1673 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1674 if (s
!= NULL
&& (s
->flags
& SEC_HAS_CONTENTS
) != 0)
1676 unsigned int elfsec
;
1677 unsigned long shlink
;
1678 bfd_byte
*extdyn
, *extdynend
;
1680 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1682 fprintf (f
, _("\nDynamic Section:\n"));
1684 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
1687 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1688 if (elfsec
== SHN_BAD
)
1690 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1692 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1693 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1695 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
1696 (size_t) (extdynend
- extdyn
) >= extdynsize
;
1697 extdyn
+= extdynsize
)
1699 Elf_Internal_Dyn dyn
;
1700 const char *name
= "";
1703 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1705 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1707 if (dyn
.d_tag
== DT_NULL
)
1714 if (bed
->elf_backend_get_target_dtag
)
1715 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1717 if (!strcmp (name
, ""))
1719 sprintf (ab
, "%#" PRIx64
, (uint64_t) dyn
.d_tag
);
1724 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1725 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1726 case DT_PLTGOT
: name
= "PLTGOT"; break;
1727 case DT_HASH
: name
= "HASH"; break;
1728 case DT_STRTAB
: name
= "STRTAB"; break;
1729 case DT_SYMTAB
: name
= "SYMTAB"; break;
1730 case DT_RELA
: name
= "RELA"; break;
1731 case DT_RELASZ
: name
= "RELASZ"; break;
1732 case DT_RELAENT
: name
= "RELAENT"; break;
1733 case DT_STRSZ
: name
= "STRSZ"; break;
1734 case DT_SYMENT
: name
= "SYMENT"; break;
1735 case DT_INIT
: name
= "INIT"; break;
1736 case DT_FINI
: name
= "FINI"; break;
1737 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1738 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1739 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1740 case DT_REL
: name
= "REL"; break;
1741 case DT_RELSZ
: name
= "RELSZ"; break;
1742 case DT_RELENT
: name
= "RELENT"; break;
1743 case DT_RELR
: name
= "RELR"; break;
1744 case DT_RELRSZ
: name
= "RELRSZ"; break;
1745 case DT_RELRENT
: name
= "RELRENT"; break;
1746 case DT_PLTREL
: name
= "PLTREL"; break;
1747 case DT_DEBUG
: name
= "DEBUG"; break;
1748 case DT_TEXTREL
: name
= "TEXTREL"; break;
1749 case DT_JMPREL
: name
= "JMPREL"; break;
1750 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1751 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1752 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1753 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1754 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1755 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1756 case DT_FLAGS
: name
= "FLAGS"; break;
1757 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1758 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1759 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1760 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1761 case DT_MOVEENT
: name
= "MOVEENT"; break;
1762 case DT_MOVESZ
: name
= "MOVESZ"; break;
1763 case DT_FEATURE
: name
= "FEATURE"; break;
1764 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1765 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1766 case DT_SYMINENT
: name
= "SYMINENT"; break;
1767 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1768 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1769 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1770 case DT_PLTPAD
: name
= "PLTPAD"; break;
1771 case DT_MOVETAB
: name
= "MOVETAB"; break;
1772 case DT_SYMINFO
: name
= "SYMINFO"; break;
1773 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1774 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1775 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1776 case DT_VERSYM
: name
= "VERSYM"; break;
1777 case DT_VERDEF
: name
= "VERDEF"; break;
1778 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1779 case DT_VERNEED
: name
= "VERNEED"; break;
1780 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1781 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1782 case DT_USED
: name
= "USED"; break;
1783 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1784 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1787 fprintf (f
, " %-20s ", name
);
1791 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1796 unsigned int tagv
= dyn
.d_un
.d_val
;
1798 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1801 fprintf (f
, "%s", string
);
1806 _bfd_elf_munmap_section_contents (s
, dynbuf
);
1810 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1811 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1813 if (! _bfd_elf_slurp_version_tables (abfd
, false))
1817 if (elf_dynverdef (abfd
) != 0)
1819 Elf_Internal_Verdef
*t
;
1821 fprintf (f
, _("\nVersion definitions:\n"));
1822 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1824 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1825 t
->vd_flags
, t
->vd_hash
,
1826 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1827 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1829 Elf_Internal_Verdaux
*a
;
1832 for (a
= t
->vd_auxptr
->vda_nextptr
;
1836 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1842 if (elf_dynverref (abfd
) != 0)
1844 Elf_Internal_Verneed
*t
;
1846 fprintf (f
, _("\nVersion References:\n"));
1847 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1849 Elf_Internal_Vernaux
*a
;
1851 fprintf (f
, _(" required from %s:\n"),
1852 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1853 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1854 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1855 a
->vna_flags
, a
->vna_other
,
1856 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1863 _bfd_elf_munmap_section_contents (s
, dynbuf
);
1867 /* Find the file offset corresponding to VMA by using the program
1871 offset_from_vma (Elf_Internal_Phdr
*phdrs
, size_t phnum
, bfd_vma vma
,
1872 size_t size
, size_t *max_size_p
)
1874 Elf_Internal_Phdr
*seg
;
1877 for (seg
= phdrs
, i
= 0; i
< phnum
; ++seg
, ++i
)
1878 if (seg
->p_type
== PT_LOAD
1879 && vma
>= (seg
->p_vaddr
& -seg
->p_align
)
1880 && vma
+ size
<= seg
->p_vaddr
+ seg
->p_filesz
)
1883 *max_size_p
= seg
->p_vaddr
+ seg
->p_filesz
- vma
;
1884 return vma
- seg
->p_vaddr
+ seg
->p_offset
;
1889 bfd_set_error (bfd_error_invalid_operation
);
1890 return (file_ptr
) -1;
1893 /* Convert hash table to internal form. */
1896 get_hash_table_data (bfd
*abfd
, bfd_size_type number
,
1897 unsigned int ent_size
, bfd_size_type filesize
)
1899 unsigned char *e_data
= NULL
;
1900 bfd_vma
*i_data
= NULL
;
1903 size_t e_data_size ATTRIBUTE_UNUSED
;
1905 if (ent_size
!= 4 && ent_size
!= 8)
1908 if ((size_t) number
!= number
)
1910 bfd_set_error (bfd_error_file_too_big
);
1914 size
= ent_size
* number
;
1915 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1916 attempting to allocate memory when the read is bound to fail. */
1918 || number
>= ~(size_t) 0 / ent_size
1919 || number
>= ~(size_t) 0 / sizeof (*i_data
))
1921 bfd_set_error (bfd_error_file_too_big
);
1925 e_data
= _bfd_mmap_readonly_temporary (abfd
, size
, &e_data_addr
,
1930 i_data
= (bfd_vma
*) bfd_malloc (number
* sizeof (*i_data
));
1939 i_data
[number
] = bfd_get_32 (abfd
, e_data
+ number
* ent_size
);
1942 i_data
[number
] = bfd_get_64 (abfd
, e_data
+ number
* ent_size
);
1944 _bfd_munmap_readonly_temporary (e_data_addr
, e_data_size
);
1948 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1949 support DT_MIPS_XHASH? */
1950 #define DT_MIPS_XHASH 0x70000036
1952 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1955 _bfd_elf_get_dynamic_symbols (bfd
*abfd
, Elf_Internal_Phdr
*phdr
,
1956 Elf_Internal_Phdr
*phdrs
, size_t phnum
,
1957 bfd_size_type filesize
)
1959 bfd_byte
*extdyn
, *extdynend
;
1961 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1962 bool (*swap_symbol_in
) (bfd
*, const void *, const void *,
1963 Elf_Internal_Sym
*);
1964 Elf_Internal_Dyn dyn
;
1965 bfd_vma dt_hash
= 0;
1966 bfd_vma dt_gnu_hash
= 0;
1967 bfd_vma dt_mips_xhash
= 0;
1968 bfd_vma dt_strtab
= 0;
1969 bfd_vma dt_symtab
= 0;
1970 size_t dt_strsz
= 0;
1971 bfd_vma dt_versym
= 0;
1972 bfd_vma dt_verdef
= 0;
1973 bfd_vma dt_verneed
= 0;
1974 bfd_byte
*dynbuf
= NULL
;
1975 char *strbuf
= NULL
;
1976 bfd_vma
*gnubuckets
= NULL
;
1977 bfd_vma
*gnuchains
= NULL
;
1978 bfd_vma
*mipsxlat
= NULL
;
1979 file_ptr saved_filepos
, filepos
;
1982 bfd_byte
*esymbuf
= NULL
, *esym
;
1983 bfd_size_type symcount
;
1984 Elf_Internal_Sym
*isymbuf
= NULL
;
1985 Elf_Internal_Sym
*isym
, *isymend
;
1986 bfd_byte
*versym
= NULL
;
1987 bfd_byte
*verdef
= NULL
;
1988 bfd_byte
*verneed
= NULL
;
1989 size_t verdef_size
= 0;
1990 size_t verneed_size
= 0;
1992 const struct elf_backend_data
*bed
;
1993 void *dynbuf_addr
= NULL
;
1994 void *esymbuf_addr
= NULL
;
1995 size_t dynbuf_size
= 0;
1996 size_t esymbuf_size
= 0;
1998 /* Return TRUE if symbol table is bad. */
1999 if (elf_bad_symtab (abfd
))
2002 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
2003 if (elf_tdata (abfd
)->dt_strtab
!= NULL
)
2006 bed
= get_elf_backend_data (abfd
);
2008 /* Save file position for elf_object_p. */
2009 saved_filepos
= bfd_tell (abfd
);
2011 if (bfd_seek (abfd
, phdr
->p_offset
, SEEK_SET
) != 0)
2014 dynbuf_size
= phdr
->p_filesz
;
2015 dynbuf
= _bfd_mmap_readonly_temporary (abfd
, dynbuf_size
,
2016 &dynbuf_addr
, &dynbuf_size
);
2020 extsym_size
= bed
->s
->sizeof_sym
;
2021 extdynsize
= bed
->s
->sizeof_dyn
;
2022 swap_dyn_in
= bed
->s
->swap_dyn_in
;
2025 if (phdr
->p_filesz
< extdynsize
)
2027 extdynend
= extdyn
+ phdr
->p_filesz
;
2028 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
2030 swap_dyn_in (abfd
, extdyn
, &dyn
);
2032 if (dyn
.d_tag
== DT_NULL
)
2038 dt_hash
= dyn
.d_un
.d_val
;
2041 if (bed
->elf_machine_code
!= EM_MIPS
2042 && bed
->elf_machine_code
!= EM_MIPS_RS3_LE
)
2043 dt_gnu_hash
= dyn
.d_un
.d_val
;
2046 dt_strtab
= dyn
.d_un
.d_val
;
2049 dt_symtab
= dyn
.d_un
.d_val
;
2052 dt_strsz
= dyn
.d_un
.d_val
;
2055 if (dyn
.d_un
.d_val
!= extsym_size
)
2059 dt_versym
= dyn
.d_un
.d_val
;
2062 dt_verdef
= dyn
.d_un
.d_val
;
2065 dt_verneed
= dyn
.d_un
.d_val
;
2068 if (dyn
.d_tag
== DT_MIPS_XHASH
2069 && (bed
->elf_machine_code
== EM_MIPS
2070 || bed
->elf_machine_code
== EM_MIPS_RS3_LE
))
2072 dt_gnu_hash
= dyn
.d_un
.d_val
;
2073 dt_mips_xhash
= dyn
.d_un
.d_val
;
2079 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
2081 if ((!dt_hash
&& !dt_gnu_hash
)
2087 /* Get dynamic string table. */
2088 filepos
= offset_from_vma (phdrs
, phnum
, dt_strtab
, dt_strsz
, NULL
);
2089 if (filepos
== (file_ptr
) -1
2090 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2093 /* Dynamic string table must be valid until ABFD is closed. */
2094 strbuf
= (char *) _bfd_mmap_readonly_persistent (abfd
, dt_strsz
);
2097 if (strbuf
[dt_strsz
- 1] != 0)
2099 /* It is an error if a string table is't terminated. */
2101 /* xgettext:c-format */
2102 (_("%pB: DT_STRTAB table is corrupt"), abfd
);
2106 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
2107 DT_HASH since it is simpler than DT_GNU_HASH. */
2110 unsigned char nb
[16];
2111 unsigned int hash_ent_size
;
2113 switch (bed
->elf_machine_code
)
2118 if (bed
->s
->elfclass
== ELFCLASS64
)
2129 filepos
= offset_from_vma (phdrs
, phnum
, dt_hash
, sizeof (nb
),
2131 if (filepos
== (file_ptr
) -1
2132 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
2133 || bfd_read (nb
, 2 * hash_ent_size
, abfd
) != 2 * hash_ent_size
)
2136 /* The number of dynamic symbol table entries equals the number
2138 if (hash_ent_size
== 8)
2139 symcount
= bfd_get_64 (abfd
, nb
+ hash_ent_size
);
2141 symcount
= bfd_get_32 (abfd
, nb
+ hash_ent_size
);
2145 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2146 bindings are in hash table. Since in dynamic symbol table,
2147 all symbols with STB_LOCAL binding are placed before symbols
2148 with other bindings and all undefined symbols are placed
2149 before defined ones, the highest symbol index in DT_GNU_HASH
2150 is the highest dynamic symbol table index. */
2151 unsigned char nb
[16];
2152 bfd_vma ngnubuckets
;
2154 size_t i
, ngnuchains
;
2155 bfd_vma maxchain
= 0xffffffff, bitmaskwords
;
2156 bfd_vma buckets_vma
;
2158 filepos
= offset_from_vma (phdrs
, phnum
, dt_gnu_hash
,
2160 if (filepos
== (file_ptr
) -1
2161 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
2162 || bfd_read (nb
, sizeof (nb
), abfd
) != sizeof (nb
))
2165 ngnubuckets
= bfd_get_32 (abfd
, nb
);
2166 gnusymidx
= bfd_get_32 (abfd
, nb
+ 4);
2167 bitmaskwords
= bfd_get_32 (abfd
, nb
+ 8);
2168 buckets_vma
= dt_gnu_hash
+ 16;
2169 if (bed
->s
->elfclass
== ELFCLASS32
)
2170 buckets_vma
+= bitmaskwords
* 4;
2172 buckets_vma
+= bitmaskwords
* 8;
2173 filepos
= offset_from_vma (phdrs
, phnum
, buckets_vma
, 4, NULL
);
2174 if (filepos
== (file_ptr
) -1
2175 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2178 gnubuckets
= get_hash_table_data (abfd
, ngnubuckets
, 4, filesize
);
2179 if (gnubuckets
== NULL
)
2182 for (i
= 0; i
< ngnubuckets
; i
++)
2183 if (gnubuckets
[i
] != 0)
2185 if (gnubuckets
[i
] < gnusymidx
)
2188 if (maxchain
== 0xffffffff || gnubuckets
[i
] > maxchain
)
2189 maxchain
= gnubuckets
[i
];
2192 if (maxchain
== 0xffffffff)
2195 goto empty_gnu_hash
;
2198 maxchain
-= gnusymidx
;
2199 filepos
= offset_from_vma (phdrs
, phnum
,
2201 4 * (ngnubuckets
+ maxchain
)),
2203 if (filepos
== (file_ptr
) -1
2204 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2209 if (bfd_read (nb
, 4, abfd
) != 4)
2215 while ((bfd_get_32 (abfd
, nb
) & 1) == 0);
2217 filepos
= offset_from_vma (phdrs
, phnum
,
2218 (buckets_vma
+ 4 * ngnubuckets
),
2220 if (filepos
== (file_ptr
) -1
2221 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2224 gnuchains
= get_hash_table_data (abfd
, maxchain
, 4, filesize
);
2225 if (gnuchains
== NULL
)
2227 ngnuchains
= maxchain
;
2231 filepos
= offset_from_vma (phdrs
, phnum
,
2233 + 4 * (ngnubuckets
+ maxchain
)),
2235 if (filepos
== (file_ptr
) -1
2236 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2239 mipsxlat
= get_hash_table_data (abfd
, maxchain
, 4, filesize
);
2240 if (mipsxlat
== NULL
)
2245 for (i
= 0; i
< ngnubuckets
; ++i
)
2246 if (gnubuckets
[i
] != 0)
2248 bfd_vma si
= gnubuckets
[i
];
2249 bfd_vma off
= si
- gnusymidx
;
2254 if (mipsxlat
[off
] >= symcount
)
2255 symcount
= mipsxlat
[off
] + 1;
2264 while (off
< ngnuchains
&& (gnuchains
[off
++] & 1) == 0);
2268 /* Swap in dynamic symbol table. */
2269 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
2271 bfd_set_error (bfd_error_file_too_big
);
2275 filepos
= offset_from_vma (phdrs
, phnum
, dt_symtab
, amt
, NULL
);
2276 if (filepos
== (file_ptr
) -1
2277 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2280 esymbuf
= _bfd_mmap_readonly_temporary (abfd
, esymbuf_size
,
2283 if (esymbuf
== NULL
)
2286 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
2288 bfd_set_error (bfd_error_file_too_big
);
2292 /* Dynamic symbol table must be valid until ABFD is closed. */
2293 isymbuf
= (Elf_Internal_Sym
*) bfd_alloc (abfd
, amt
);
2294 if (isymbuf
== NULL
)
2297 swap_symbol_in
= bed
->s
->swap_symbol_in
;
2299 /* Convert the symbols to internal form. */
2300 isymend
= isymbuf
+ symcount
;
2301 for (esym
= esymbuf
, isym
= isymbuf
;
2303 esym
+= extsym_size
, isym
++)
2304 if (!swap_symbol_in (abfd
, esym
, NULL
, isym
)
2305 || isym
->st_name
>= dt_strsz
)
2307 bfd_set_error (bfd_error_invalid_operation
);
2313 /* Swap in DT_VERSYM. */
2314 if (_bfd_mul_overflow (symcount
, 2, &amt
))
2316 bfd_set_error (bfd_error_file_too_big
);
2320 filepos
= offset_from_vma (phdrs
, phnum
, dt_versym
, amt
, NULL
);
2321 if (filepos
== (file_ptr
) -1
2322 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2325 /* DT_VERSYM info must be valid until ABFD is closed. */
2326 versym
= _bfd_mmap_readonly_persistent (abfd
, amt
);
2330 /* Read in DT_VERDEF. */
2331 filepos
= offset_from_vma (phdrs
, phnum
, dt_verdef
,
2333 if (filepos
== (file_ptr
) -1
2334 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2337 /* DT_VERDEF info must be valid until ABFD is closed. */
2338 verdef
= _bfd_mmap_readonly_persistent (abfd
, verdef_size
);
2343 /* Read in DT_VERNEED. */
2344 filepos
= offset_from_vma (phdrs
, phnum
, dt_verneed
,
2346 if (filepos
== (file_ptr
) -1
2347 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2350 /* DT_VERNEED info must be valid until ABFD is closed. */
2351 verneed
= _bfd_mmap_readonly_persistent (abfd
, verneed_size
);
2356 elf_tdata (abfd
)->dt_strtab
= strbuf
;
2357 elf_tdata (abfd
)->dt_strsz
= dt_strsz
;
2358 elf_tdata (abfd
)->dt_symtab
= isymbuf
;
2359 elf_tdata (abfd
)->dt_symtab_count
= symcount
;
2360 elf_tdata (abfd
)->dt_versym
= versym
;
2361 elf_tdata (abfd
)->dt_verdef
= verdef
;
2362 elf_tdata (abfd
)->dt_verneed
= verneed
;
2363 elf_tdata (abfd
)->dt_verdef_count
2364 = verdef_size
/ sizeof (Elf_External_Verdef
);
2365 elf_tdata (abfd
)->dt_verneed_count
2366 = verneed_size
/ sizeof (Elf_External_Verneed
);
2371 /* Restore file position for elf_object_p. */
2372 if (bfd_seek (abfd
, saved_filepos
, SEEK_SET
) != 0)
2374 _bfd_munmap_readonly_temporary (dynbuf_addr
, dynbuf_size
);
2375 _bfd_munmap_readonly_temporary (esymbuf_addr
, esymbuf_size
);
2382 /* Reconstruct section from dynamic symbol. */
2385 _bfd_elf_get_section_from_dynamic_symbol (bfd
*abfd
,
2386 Elf_Internal_Sym
*isym
)
2391 if (!elf_use_dt_symtab_p (abfd
))
2394 flags
= SEC_ALLOC
| SEC_LOAD
;
2395 switch (ELF_ST_TYPE (isym
->st_info
))
2399 sec
= bfd_get_section_by_name (abfd
, ".text");
2401 sec
= bfd_make_section_with_flags (abfd
,
2406 sec
= bfd_com_section_ptr
;
2409 sec
= bfd_get_section_by_name (abfd
, ".data");
2411 sec
= bfd_make_section_with_flags (abfd
,
2416 sec
= bfd_get_section_by_name (abfd
, ".tdata");
2418 sec
= bfd_make_section_with_flags (abfd
,
2422 | SEC_THREAD_LOCAL
));
2425 sec
= bfd_abs_section_ptr
;
2432 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2433 and return symbol version for symbol version itself. */
2436 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
2440 const char *version_string
= NULL
;
2441 if ((elf_dynversym (abfd
) != 0
2442 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
2443 || (elf_tdata (abfd
)->dt_versym
!= NULL
2444 && (elf_tdata (abfd
)->dt_verdef
!= NULL
2445 || elf_tdata (abfd
)->dt_verneed
!= NULL
)))
2447 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
2449 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
2450 vernum
&= VERSYM_VERSION
;
2453 version_string
= "";
2454 else if (vernum
== 1
2455 && (vernum
> elf_tdata (abfd
)->cverdefs
2456 || (elf_tdata (abfd
)->verdef
[0].vd_flags
2458 version_string
= base_p
? "Base" : "";
2459 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
2461 const char *nodename
2462 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
2463 version_string
= "";
2466 || symbol
->name
== NULL
2467 || strcmp (symbol
->name
, nodename
) != 0)
2468 version_string
= nodename
;
2472 Elf_Internal_Verneed
*t
;
2474 version_string
= _("<corrupt>");
2475 for (t
= elf_tdata (abfd
)->verref
;
2479 Elf_Internal_Vernaux
*a
;
2481 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2483 if (a
->vna_other
== vernum
)
2486 version_string
= a
->vna_nodename
;
2493 return version_string
;
2496 /* Display ELF-specific fields of a symbol. */
2499 bfd_elf_print_symbol (bfd
*abfd
,
2502 bfd_print_symbol_type how
)
2504 FILE *file
= (FILE *) filep
;
2507 case bfd_print_symbol_name
:
2508 fprintf (file
, "%s", symbol
->name
);
2510 case bfd_print_symbol_more
:
2511 fprintf (file
, "elf ");
2512 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
2513 fprintf (file
, " %x", symbol
->flags
);
2515 case bfd_print_symbol_all
:
2517 const char *section_name
;
2518 const char *name
= NULL
;
2519 const struct elf_backend_data
*bed
;
2520 unsigned char st_other
;
2522 const char *version_string
;
2525 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2527 bed
= get_elf_backend_data (abfd
);
2528 if (bed
->elf_backend_print_symbol_all
)
2529 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
2533 name
= symbol
->name
;
2534 bfd_print_symbol_vandf (abfd
, file
, symbol
);
2537 fprintf (file
, " %s\t", section_name
);
2538 /* Print the "other" value for a symbol. For common symbols,
2539 we've already printed the size; now print the alignment.
2540 For other symbols, we have no specified alignment, and
2541 we've printed the address; now print the size. */
2542 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
2543 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
2545 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
2546 bfd_fprintf_vma (abfd
, file
, val
);
2548 /* If we have version information, print it. */
2549 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2556 fprintf (file
, " %-11s", version_string
);
2561 fprintf (file
, " (%s)", version_string
);
2562 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2567 /* If the st_other field is not zero, print it. */
2568 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2573 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2574 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2575 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2577 /* Some other non-defined flags are also present, so print
2579 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2582 fprintf (file
, " %s", name
);
2588 /* ELF .o/exec file reading */
2590 /* Create a new bfd section from an ELF section header. */
2593 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2595 Elf_Internal_Shdr
*hdr
;
2596 Elf_Internal_Ehdr
*ehdr
;
2597 const struct elf_backend_data
*bed
;
2601 if (shindex
>= elf_numsections (abfd
))
2604 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2605 sh_link or sh_info. Detect this here, by refusing to load a
2606 section that we are already in the process of loading. */
2607 if (elf_tdata (abfd
)->being_created
[shindex
])
2610 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2613 elf_tdata (abfd
)->being_created
[shindex
] = true;
2615 hdr
= elf_elfsections (abfd
)[shindex
];
2616 ehdr
= elf_elfheader (abfd
);
2617 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2622 bed
= get_elf_backend_data (abfd
);
2623 switch (hdr
->sh_type
)
2626 /* Inactive section. Throw it away. */
2629 case SHT_PROGBITS
: /* Normal section with contents. */
2630 case SHT_NOBITS
: /* .bss section. */
2631 case SHT_HASH
: /* .hash section. */
2632 case SHT_NOTE
: /* .note section. */
2633 case SHT_INIT_ARRAY
: /* .init_array section. */
2634 case SHT_FINI_ARRAY
: /* .fini_array section. */
2635 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2636 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2637 case SHT_GNU_HASH
: /* .gnu.hash section. */
2638 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2641 case SHT_DYNAMIC
: /* Dynamic linking information. */
2642 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2645 if (hdr
->sh_link
> elf_numsections (abfd
))
2647 /* PR 10478: Accept Solaris binaries with a sh_link field
2648 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2649 switch (bfd_get_arch (abfd
))
2652 case bfd_arch_sparc
:
2653 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff)
2654 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff))
2656 /* Otherwise fall through. */
2661 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2663 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2665 Elf_Internal_Shdr
*dynsymhdr
;
2667 /* The shared libraries distributed with hpux11 have a bogus
2668 sh_link field for the ".dynamic" section. Find the
2669 string table for the ".dynsym" section instead. */
2670 if (elf_dynsymtab (abfd
) != 0)
2672 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2673 hdr
->sh_link
= dynsymhdr
->sh_link
;
2677 unsigned int i
, num_sec
;
2679 num_sec
= elf_numsections (abfd
);
2680 for (i
= 1; i
< num_sec
; i
++)
2682 dynsymhdr
= elf_elfsections (abfd
)[i
];
2683 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2685 hdr
->sh_link
= dynsymhdr
->sh_link
;
2693 case SHT_SYMTAB
: /* A symbol table. */
2694 if (elf_onesymtab (abfd
) == shindex
)
2697 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2700 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2702 if (hdr
->sh_size
!= 0)
2704 /* Some assemblers erroneously set sh_info to one with a
2705 zero sh_size. ld sees this as a global symbol count
2706 of (unsigned) -1. Fix it here. */
2711 /* PR 18854: A binary might contain more than one symbol table.
2712 Unusual, but possible. Warn, but continue. */
2713 if (elf_onesymtab (abfd
) != 0)
2716 /* xgettext:c-format */
2717 (_("%pB: warning: multiple symbol tables detected"
2718 " - ignoring the table in section %u"),
2722 elf_onesymtab (abfd
) = shindex
;
2723 elf_symtab_hdr (abfd
) = *hdr
;
2724 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2725 abfd
->flags
|= HAS_SYMS
;
2727 /* Sometimes a shared object will map in the symbol table. If
2728 SHF_ALLOC is set, and this is a shared object, then we also
2729 treat this section as a BFD section. We can not base the
2730 decision purely on SHF_ALLOC, because that flag is sometimes
2731 set in a relocatable object file, which would confuse the
2733 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2734 && (abfd
->flags
& DYNAMIC
) != 0
2735 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2739 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2740 can't read symbols without that section loaded as well. It
2741 is most likely specified by the next section header. */
2743 elf_section_list
* entry
;
2744 unsigned int i
, num_sec
;
2746 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2747 if (entry
->hdr
.sh_link
== shindex
)
2750 num_sec
= elf_numsections (abfd
);
2751 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2753 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2755 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2756 && hdr2
->sh_link
== shindex
)
2761 for (i
= 1; i
< shindex
; i
++)
2763 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2765 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2766 && hdr2
->sh_link
== shindex
)
2771 ret
= bfd_section_from_shdr (abfd
, i
);
2772 /* else FIXME: we have failed to find the symbol table.
2773 Should we issue an error? */
2777 case SHT_DYNSYM
: /* A dynamic symbol table. */
2778 if (elf_dynsymtab (abfd
) == shindex
)
2781 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2784 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2786 if (hdr
->sh_size
!= 0)
2789 /* Some linkers erroneously set sh_info to one with a
2790 zero sh_size. ld sees this as a global symbol count
2791 of (unsigned) -1. Fix it here. */
2796 /* PR 18854: A binary might contain more than one dynamic symbol table.
2797 Unusual, but possible. Warn, but continue. */
2798 if (elf_dynsymtab (abfd
) != 0)
2801 /* xgettext:c-format */
2802 (_("%pB: warning: multiple dynamic symbol tables detected"
2803 " - ignoring the table in section %u"),
2807 elf_dynsymtab (abfd
) = shindex
;
2808 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2809 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2810 abfd
->flags
|= HAS_SYMS
;
2812 /* Besides being a symbol table, we also treat this as a regular
2813 section, so that objcopy can handle it. */
2814 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2817 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2819 elf_section_list
* entry
;
2821 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2822 if (entry
->ndx
== shindex
)
2825 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2828 entry
->ndx
= shindex
;
2830 entry
->next
= elf_symtab_shndx_list (abfd
);
2831 elf_symtab_shndx_list (abfd
) = entry
;
2832 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2836 case SHT_STRTAB
: /* A string table. */
2837 if (hdr
->bfd_section
!= NULL
)
2840 if (ehdr
->e_shstrndx
== shindex
)
2842 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2843 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2847 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2850 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2851 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2855 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2858 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2859 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2860 elf_elfsections (abfd
)[shindex
] = hdr
;
2861 /* We also treat this as a regular section, so that objcopy
2863 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2868 /* If the string table isn't one of the above, then treat it as a
2869 regular section. We need to scan all the headers to be sure,
2870 just in case this strtab section appeared before the above. */
2871 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2873 unsigned int i
, num_sec
;
2875 num_sec
= elf_numsections (abfd
);
2876 for (i
= 1; i
< num_sec
; i
++)
2878 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2879 if (hdr2
->sh_link
== shindex
)
2881 /* Prevent endless recursion on broken objects. */
2884 if (! bfd_section_from_shdr (abfd
, i
))
2886 if (elf_onesymtab (abfd
) == i
)
2888 if (elf_dynsymtab (abfd
) == i
)
2889 goto dynsymtab_strtab
;
2893 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2899 /* *These* do a lot of work -- but build no sections! */
2901 asection
*target_sect
;
2902 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2903 unsigned int num_sec
= elf_numsections (abfd
);
2904 struct bfd_elf_section_data
*esdt
;
2907 if (hdr
->sh_type
== SHT_REL
)
2908 size
= bed
->s
->sizeof_rel
;
2909 else if (hdr
->sh_type
== SHT_RELA
)
2910 size
= bed
->s
->sizeof_rela
;
2912 size
= bed
->s
->arch_size
/ 8;
2913 if (hdr
->sh_entsize
!= size
)
2916 /* Check for a bogus link to avoid crashing. */
2917 if (hdr
->sh_link
>= num_sec
)
2920 /* xgettext:c-format */
2921 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2922 abfd
, hdr
->sh_link
, name
, shindex
);
2923 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2927 /* Get the symbol table. */
2928 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2929 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2930 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2933 /* If this is an alloc section in an executable or shared
2934 library, or the reloc section does not use the main symbol
2935 table we don't treat it as a reloc section. BFD can't
2936 adequately represent such a section, so at least for now,
2937 we don't try. We just present it as a normal section. We
2938 also can't use it as a reloc section if it points to the
2939 null section, an invalid section, another reloc section, or
2940 its sh_link points to the null section. */
2941 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2942 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2943 || (hdr
->sh_flags
& SHF_COMPRESSED
) != 0
2944 || hdr
->sh_type
== SHT_RELR
2945 || hdr
->sh_link
== SHN_UNDEF
2946 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2947 || hdr
->sh_info
== SHN_UNDEF
2948 || hdr
->sh_info
>= num_sec
2949 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2950 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2952 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2956 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2959 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2960 if (target_sect
== NULL
)
2963 esdt
= elf_section_data (target_sect
);
2964 if (hdr
->sh_type
== SHT_RELA
)
2965 p_hdr
= &esdt
->rela
.hdr
;
2967 p_hdr
= &esdt
->rel
.hdr
;
2969 /* PR 17512: file: 0b4f81b7.
2970 Also see PR 24456, for a file which deliberately has two reloc
2974 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2977 /* xgettext:c-format */
2978 (_("%pB: warning: secondary relocation section '%s' "
2979 "for section %pA found - ignoring"),
2980 abfd
, name
, target_sect
);
2983 esdt
->has_secondary_relocs
= true;
2987 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2992 elf_elfsections (abfd
)[shindex
] = hdr2
;
2993 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2994 * bed
->s
->int_rels_per_ext_rel
);
2995 target_sect
->flags
|= SEC_RELOC
;
2996 target_sect
->relocation
= NULL
;
2997 target_sect
->rel_filepos
= hdr
->sh_offset
;
2998 /* In the section to which the relocations apply, mark whether
2999 its relocations are of the REL or RELA variety. */
3000 if (hdr
->sh_size
!= 0)
3002 if (hdr
->sh_type
== SHT_RELA
)
3003 target_sect
->use_rela_p
= 1;
3005 abfd
->flags
|= HAS_RELOC
;
3009 case SHT_GNU_verdef
:
3010 if (hdr
->sh_info
!= 0)
3011 elf_dynverdef (abfd
) = shindex
;
3012 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
3013 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3016 case SHT_GNU_versym
:
3017 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
3020 elf_dynversym (abfd
) = shindex
;
3021 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
3022 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3025 case SHT_GNU_verneed
:
3026 if (hdr
->sh_info
!= 0)
3027 elf_dynverref (abfd
) = shindex
;
3028 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
3029 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3036 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
3039 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3045 /* Possibly an attributes section. */
3046 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
3047 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
3049 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3051 _bfd_elf_parse_attributes (abfd
, hdr
);
3055 /* Check for any processor-specific section types. */
3056 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
3059 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
3061 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3062 /* FIXME: How to properly handle allocated section reserved
3063 for applications? */
3065 /* xgettext:c-format */
3066 (_("%pB: unknown type [%#x] section `%s'"),
3067 abfd
, hdr
->sh_type
, name
);
3070 /* Allow sections reserved for applications. */
3071 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3075 else if (hdr
->sh_type
>= SHT_LOPROC
3076 && hdr
->sh_type
<= SHT_HIPROC
)
3077 /* FIXME: We should handle this section. */
3079 /* xgettext:c-format */
3080 (_("%pB: unknown type [%#x] section `%s'"),
3081 abfd
, hdr
->sh_type
, name
);
3082 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
3084 /* Unrecognised OS-specific sections. */
3085 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
3086 /* SHF_OS_NONCONFORMING indicates that special knowledge is
3087 required to correctly process the section and the file should
3088 be rejected with an error message. */
3090 /* xgettext:c-format */
3091 (_("%pB: unknown type [%#x] section `%s'"),
3092 abfd
, hdr
->sh_type
, name
);
3095 /* Otherwise it should be processed. */
3096 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3101 /* FIXME: We should handle this section. */
3103 /* xgettext:c-format */
3104 (_("%pB: unknown type [%#x] section `%s'"),
3105 abfd
, hdr
->sh_type
, name
);
3113 elf_tdata (abfd
)->being_created
[shindex
] = false;
3117 /* Return the local symbol specified by ABFD, R_SYMNDX. */
3120 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
3122 unsigned long r_symndx
)
3124 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
3126 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
3128 Elf_Internal_Shdr
*symtab_hdr
;
3129 unsigned char esym
[sizeof (Elf64_External_Sym
)];
3130 Elf_External_Sym_Shndx eshndx
;
3132 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3133 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
3134 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
3137 if (cache
->abfd
!= abfd
)
3139 memset (cache
->indx
, -1, sizeof (cache
->indx
));
3142 cache
->indx
[ent
] = r_symndx
;
3145 return &cache
->sym
[ent
];
3148 /* Given an ELF section number, retrieve the corresponding BFD
3152 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
3154 if (sec_index
>= elf_numsections (abfd
))
3156 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
3159 static const struct bfd_elf_special_section special_sections_b
[] =
3161 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3162 { NULL
, 0, 0, 0, 0 }
3165 static const struct bfd_elf_special_section special_sections_c
[] =
3167 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
3168 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
3169 { NULL
, 0, 0, 0, 0 }
3172 static const struct bfd_elf_special_section special_sections_d
[] =
3174 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3175 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3176 /* There are more DWARF sections than these, but they needn't be added here
3177 unless you have to cope with broken compilers that don't emit section
3178 attributes or you want to help the user writing assembler. */
3179 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
3180 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
3181 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
3182 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
3183 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
3184 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
3185 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
3186 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
3187 { NULL
, 0, 0, 0, 0 }
3190 static const struct bfd_elf_special_section special_sections_f
[] =
3192 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3193 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3194 { NULL
, 0 , 0, 0, 0 }
3197 static const struct bfd_elf_special_section special_sections_g
[] =
3199 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3200 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3201 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3202 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
3203 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3204 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
3205 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
3206 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
3207 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
3208 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
3209 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
3210 { NULL
, 0, 0, 0, 0 }
3213 static const struct bfd_elf_special_section special_sections_h
[] =
3215 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
3216 { NULL
, 0, 0, 0, 0 }
3219 static const struct bfd_elf_special_section special_sections_i
[] =
3221 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3222 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3223 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
3224 { NULL
, 0, 0, 0, 0 }
3227 static const struct bfd_elf_special_section special_sections_l
[] =
3229 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
3230 { NULL
, 0, 0, 0, 0 }
3233 static const struct bfd_elf_special_section special_sections_n
[] =
3235 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3236 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
3237 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
3238 { NULL
, 0, 0, 0, 0 }
3241 static const struct bfd_elf_special_section special_sections_p
[] =
3243 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3244 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3245 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3246 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3247 { NULL
, 0, 0, 0, 0 }
3250 static const struct bfd_elf_special_section special_sections_r
[] =
3252 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
3253 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
3254 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR
, SHF_ALLOC
},
3255 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
3256 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
3257 { NULL
, 0, 0, 0, 0 }
3260 static const struct bfd_elf_special_section special_sections_s
[] =
3262 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
3263 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
3264 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
3265 /* See struct bfd_elf_special_section declaration for the semantics of
3266 this special case where .prefix_length != strlen (.prefix). */
3267 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
3268 { NULL
, 0, 0, 0, 0 }
3271 static const struct bfd_elf_special_section special_sections_t
[] =
3273 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3274 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
3275 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
3276 { NULL
, 0, 0, 0, 0 }
3279 static const struct bfd_elf_special_section special_sections_z
[] =
3281 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
3282 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
3283 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
3284 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
3285 { NULL
, 0, 0, 0, 0 }
3288 static const struct bfd_elf_special_section
* const special_sections
[] =
3290 special_sections_b
, /* 'b' */
3291 special_sections_c
, /* 'c' */
3292 special_sections_d
, /* 'd' */
3294 special_sections_f
, /* 'f' */
3295 special_sections_g
, /* 'g' */
3296 special_sections_h
, /* 'h' */
3297 special_sections_i
, /* 'i' */
3300 special_sections_l
, /* 'l' */
3302 special_sections_n
, /* 'n' */
3304 special_sections_p
, /* 'p' */
3306 special_sections_r
, /* 'r' */
3307 special_sections_s
, /* 's' */
3308 special_sections_t
, /* 't' */
3314 special_sections_z
/* 'z' */
3317 const struct bfd_elf_special_section
*
3318 _bfd_elf_get_special_section (const char *name
,
3319 const struct bfd_elf_special_section
*spec
,
3325 len
= strlen (name
);
3327 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
3330 int prefix_len
= spec
[i
].prefix_length
;
3332 if (len
< prefix_len
)
3334 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
3337 suffix_len
= spec
[i
].suffix_length
;
3338 if (suffix_len
<= 0)
3340 if (name
[prefix_len
] != 0)
3342 if (suffix_len
== 0)
3344 if (name
[prefix_len
] != '.'
3345 && (suffix_len
== -2
3346 || (rela
&& spec
[i
].type
== SHT_REL
)))
3352 if (len
< prefix_len
+ suffix_len
)
3354 if (memcmp (name
+ len
- suffix_len
,
3355 spec
[i
].prefix
+ prefix_len
,
3365 const struct bfd_elf_special_section
*
3366 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
3369 const struct bfd_elf_special_section
*spec
;
3370 const struct elf_backend_data
*bed
;
3372 /* See if this is one of the special sections. */
3373 if (sec
->name
== NULL
)
3376 bed
= get_elf_backend_data (abfd
);
3377 spec
= bed
->special_sections
;
3380 spec
= _bfd_elf_get_special_section (sec
->name
,
3381 bed
->special_sections
,
3387 if (sec
->name
[0] != '.')
3390 i
= sec
->name
[1] - 'b';
3391 if (i
< 0 || i
> 'z' - 'b')
3394 spec
= special_sections
[i
];
3399 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
3403 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
3405 struct bfd_elf_section_data
*sdata
;
3406 const struct elf_backend_data
*bed
;
3407 const struct bfd_elf_special_section
*ssect
;
3409 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
3412 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
3416 sec
->used_by_bfd
= sdata
;
3419 /* Indicate whether or not this section should use RELA relocations. */
3420 bed
= get_elf_backend_data (abfd
);
3421 sec
->use_rela_p
= bed
->default_use_rela_p
;
3423 /* Set up ELF section type and flags for newly created sections, if
3424 there is an ABI mandated section. */
3425 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
3428 elf_section_type (sec
) = ssect
->type
;
3429 elf_section_flags (sec
) = ssect
->attr
;
3432 return _bfd_generic_new_section_hook (abfd
, sec
);
3435 /* Create a new bfd section from an ELF program header.
3437 Since program segments have no names, we generate a synthetic name
3438 of the form segment<NUM>, where NUM is generally the index in the
3439 program header table. For segments that are split (see below) we
3440 generate the names segment<NUM>a and segment<NUM>b.
3442 Note that some program segments may have a file size that is different than
3443 (less than) the memory size. All this means is that at execution the
3444 system must allocate the amount of memory specified by the memory size,
3445 but only initialize it with the first "file size" bytes read from the
3446 file. This would occur for example, with program segments consisting
3447 of combined data+bss.
3449 To handle the above situation, this routine generates TWO bfd sections
3450 for the single program segment. The first has the length specified by
3451 the file size of the segment, and the second has the length specified
3452 by the difference between the two sizes. In effect, the segment is split
3453 into its initialized and uninitialized parts. */
3456 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
3457 Elf_Internal_Phdr
*hdr
,
3459 const char *type_name
)
3466 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
3468 split
= ((hdr
->p_memsz
> 0)
3469 && (hdr
->p_filesz
> 0)
3470 && (hdr
->p_memsz
> hdr
->p_filesz
));
3472 if (hdr
->p_filesz
> 0)
3474 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
3475 len
= strlen (namebuf
) + 1;
3476 name
= (char *) bfd_alloc (abfd
, len
);
3479 memcpy (name
, namebuf
, len
);
3480 newsect
= bfd_make_section (abfd
, name
);
3481 if (newsect
== NULL
)
3483 newsect
->vma
= hdr
->p_vaddr
/ opb
;
3484 newsect
->lma
= hdr
->p_paddr
/ opb
;
3485 newsect
->size
= hdr
->p_filesz
;
3486 newsect
->filepos
= hdr
->p_offset
;
3487 newsect
->flags
|= SEC_HAS_CONTENTS
;
3488 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
3489 if (hdr
->p_type
== PT_LOAD
)
3491 newsect
->flags
|= SEC_ALLOC
;
3492 newsect
->flags
|= SEC_LOAD
;
3493 if (hdr
->p_flags
& PF_X
)
3495 /* FIXME: all we known is that it has execute PERMISSION,
3497 newsect
->flags
|= SEC_CODE
;
3500 if (!(hdr
->p_flags
& PF_W
))
3502 newsect
->flags
|= SEC_READONLY
;
3506 if (hdr
->p_memsz
> hdr
->p_filesz
)
3510 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3511 len
= strlen (namebuf
) + 1;
3512 name
= (char *) bfd_alloc (abfd
, len
);
3515 memcpy (name
, namebuf
, len
);
3516 newsect
= bfd_make_section (abfd
, name
);
3517 if (newsect
== NULL
)
3519 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3520 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3521 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3522 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3523 align
= newsect
->vma
& -newsect
->vma
;
3524 if (align
== 0 || align
> hdr
->p_align
)
3525 align
= hdr
->p_align
;
3526 newsect
->alignment_power
= bfd_log2 (align
);
3527 if (hdr
->p_type
== PT_LOAD
)
3529 newsect
->flags
|= SEC_ALLOC
;
3530 if (hdr
->p_flags
& PF_X
)
3531 newsect
->flags
|= SEC_CODE
;
3533 if (!(hdr
->p_flags
& PF_W
))
3534 newsect
->flags
|= SEC_READONLY
;
3541 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3543 /* The return value is ignored. Build-ids are considered optional. */
3544 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3545 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3551 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3553 const struct elf_backend_data
*bed
;
3555 switch (hdr
->p_type
)
3558 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3561 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3563 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3564 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3568 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3571 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3574 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3576 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3582 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3585 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3587 case PT_GNU_EH_FRAME
:
3588 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3592 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3595 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3598 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3602 /* Check for any processor-specific program segment types. */
3603 bed
= get_elf_backend_data (abfd
);
3604 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3608 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3612 _bfd_elf_single_rel_hdr (asection
*sec
)
3614 if (elf_section_data (sec
)->rel
.hdr
)
3616 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3617 return elf_section_data (sec
)->rel
.hdr
;
3620 return elf_section_data (sec
)->rela
.hdr
;
3624 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3625 Elf_Internal_Shdr
*rel_hdr
,
3626 const char *sec_name
,
3629 char *name
= (char *) bfd_alloc (abfd
,
3630 sizeof ".rela" + strlen (sec_name
));
3634 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3636 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3638 if (rel_hdr
->sh_name
== (unsigned int) -1)
3644 /* Allocate and initialize a section-header for a new reloc section,
3645 containing relocations against ASECT. It is stored in RELDATA. If
3646 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3650 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3651 struct bfd_elf_section_reloc_data
*reldata
,
3652 const char *sec_name
,
3654 bool delay_st_name_p
)
3656 Elf_Internal_Shdr
*rel_hdr
;
3657 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3659 BFD_ASSERT (reldata
->hdr
== NULL
);
3660 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3661 if (rel_hdr
== NULL
)
3663 reldata
->hdr
= rel_hdr
;
3665 if (delay_st_name_p
)
3666 rel_hdr
->sh_name
= (unsigned int) -1;
3667 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3670 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3671 rel_hdr
->sh_entsize
= (use_rela_p
3672 ? bed
->s
->sizeof_rela
3673 : bed
->s
->sizeof_rel
);
3674 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3675 rel_hdr
->sh_flags
= 0;
3676 rel_hdr
->sh_addr
= 0;
3677 rel_hdr
->sh_size
= 0;
3678 rel_hdr
->sh_offset
= 0;
3683 /* Return the default section type based on the passed in section flags. */
3686 bfd_elf_get_default_section_type (flagword flags
)
3688 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3689 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3691 return SHT_PROGBITS
;
3694 struct fake_section_arg
3696 struct bfd_link_info
*link_info
;
3700 /* Set up an ELF internal section header for a section. */
3703 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3705 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3706 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3707 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3708 Elf_Internal_Shdr
*this_hdr
;
3709 unsigned int sh_type
;
3710 const char *name
= asect
->name
;
3711 bool delay_st_name_p
= false;
3716 /* We already failed; just get out of the bfd_map_over_sections
3721 this_hdr
= &esd
->this_hdr
;
3723 /* ld: compress DWARF debug sections with names: .debug_*. */
3725 && (abfd
->flags
& BFD_COMPRESS
) != 0
3726 && (asect
->flags
& SEC_DEBUGGING
) != 0
3730 /* If this section will be compressed, delay adding section
3731 name to section name section after it is compressed in
3732 _bfd_elf_assign_file_positions_for_non_load. */
3733 delay_st_name_p
= true;
3736 if (delay_st_name_p
)
3737 this_hdr
->sh_name
= (unsigned int) -1;
3741 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3743 if (this_hdr
->sh_name
== (unsigned int) -1)
3750 /* Don't clear sh_flags. Assembler may set additional bits. */
3752 if ((asect
->flags
& SEC_ALLOC
) != 0
3753 || asect
->user_set_vma
)
3754 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3756 this_hdr
->sh_addr
= 0;
3758 this_hdr
->sh_offset
= 0;
3759 this_hdr
->sh_size
= asect
->size
;
3760 this_hdr
->sh_link
= 0;
3761 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3762 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3765 /* xgettext:c-format */
3766 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3767 abfd
, asect
->alignment_power
, asect
);
3771 /* Set sh_addralign to the highest power of two given by alignment
3772 consistent with the section VMA. Linker scripts can force VMA. */
3773 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3774 this_hdr
->sh_addralign
= mask
& -mask
;
3775 /* The sh_entsize and sh_info fields may have been set already by
3776 copy_private_section_data. */
3778 this_hdr
->bfd_section
= asect
;
3779 this_hdr
->contents
= NULL
;
3781 /* If the section type is unspecified, we set it based on
3783 if (asect
->type
!= 0)
3784 sh_type
= asect
->type
;
3785 else if ((asect
->flags
& SEC_GROUP
) != 0)
3786 sh_type
= SHT_GROUP
;
3788 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3790 if (this_hdr
->sh_type
== SHT_NULL
)
3791 this_hdr
->sh_type
= sh_type
;
3792 else if (this_hdr
->sh_type
== SHT_NOBITS
3793 && sh_type
== SHT_PROGBITS
3794 && (asect
->flags
& SEC_ALLOC
) != 0)
3796 /* Warn if we are changing a NOBITS section to PROGBITS, but
3797 allow the link to proceed. This can happen when users link
3798 non-bss input sections to bss output sections, or emit data
3799 to a bss output section via a linker script. */
3801 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3802 this_hdr
->sh_type
= sh_type
;
3805 switch (this_hdr
->sh_type
)
3816 case SHT_INIT_ARRAY
:
3817 case SHT_FINI_ARRAY
:
3818 case SHT_PREINIT_ARRAY
:
3819 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3823 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3827 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3831 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3835 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3836 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3840 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3841 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3844 case SHT_GNU_versym
:
3845 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3848 case SHT_GNU_verdef
:
3849 this_hdr
->sh_entsize
= 0;
3850 /* objcopy or strip will copy over sh_info, but may not set
3851 cverdefs. The linker will set cverdefs, but sh_info will be
3853 if (this_hdr
->sh_info
== 0)
3854 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3856 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3857 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3860 case SHT_GNU_verneed
:
3861 this_hdr
->sh_entsize
= 0;
3862 /* objcopy or strip will copy over sh_info, but may not set
3863 cverrefs. The linker will set cverrefs, but sh_info will be
3865 if (this_hdr
->sh_info
== 0)
3866 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3868 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3869 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3873 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3877 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3881 if ((asect
->flags
& SEC_ALLOC
) != 0)
3882 this_hdr
->sh_flags
|= SHF_ALLOC
;
3883 if ((asect
->flags
& SEC_READONLY
) == 0)
3884 this_hdr
->sh_flags
|= SHF_WRITE
;
3885 if ((asect
->flags
& SEC_CODE
) != 0)
3886 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3887 if ((asect
->flags
& SEC_MERGE
) != 0)
3889 this_hdr
->sh_flags
|= SHF_MERGE
;
3890 this_hdr
->sh_entsize
= asect
->entsize
;
3892 if ((asect
->flags
& SEC_STRINGS
) != 0)
3893 this_hdr
->sh_flags
|= SHF_STRINGS
;
3894 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3895 this_hdr
->sh_flags
|= SHF_GROUP
;
3896 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3898 this_hdr
->sh_flags
|= SHF_TLS
;
3899 if (asect
->size
== 0
3900 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3902 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3904 this_hdr
->sh_size
= 0;
3907 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3908 if (this_hdr
->sh_size
!= 0)
3909 this_hdr
->sh_type
= SHT_NOBITS
;
3913 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3914 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3916 /* If the section has relocs, set up a section header for the
3917 SHT_REL[A] section. If two relocation sections are required for
3918 this section, it is up to the processor-specific back-end to
3919 create the other. */
3920 if ((asect
->flags
& SEC_RELOC
) != 0)
3922 /* When doing a relocatable link, create both REL and RELA sections if
3925 /* Do the normal setup if we wouldn't create any sections here. */
3926 && esd
->rel
.count
+ esd
->rela
.count
> 0
3927 && (bfd_link_relocatable (arg
->link_info
)
3928 || arg
->link_info
->emitrelocations
))
3930 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3931 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3932 false, delay_st_name_p
))
3937 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3938 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3939 true, delay_st_name_p
))
3945 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3947 ? &esd
->rela
: &esd
->rel
),
3957 /* Check for processor-specific section types. */
3958 sh_type
= this_hdr
->sh_type
;
3959 if (bed
->elf_backend_fake_sections
3960 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3966 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3968 /* Don't change the header type from NOBITS if we are being
3969 called for objcopy --only-keep-debug. */
3970 this_hdr
->sh_type
= sh_type
;
3974 /* Fill in the contents of a SHT_GROUP section. Called from
3975 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3976 when ELF targets use the generic linker, ld. Called for ld -r
3977 from bfd_elf_final_link. */
3980 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3982 bool *failedptr
= (bool *) failedptrarg
;
3983 asection
*elt
, *first
;
3987 /* Ignore linker created group section. See elfNN_ia64_object_p in
3989 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3994 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3996 unsigned long symindx
= 0;
3998 /* elf_group_id will have been set up by objcopy and the
4000 if (elf_group_id (sec
) != NULL
)
4001 symindx
= elf_group_id (sec
)->udata
.i
;
4005 /* If called from the assembler, swap_out_syms will have set up
4007 PR 25699: A corrupt input file could contain bogus group info. */
4008 if (sec
->index
>= elf_num_section_syms (abfd
)
4009 || elf_section_syms (abfd
)[sec
->index
] == NULL
)
4014 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
4016 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
4018 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
4020 /* The ELF backend linker sets sh_info to -2 when the group
4021 signature symbol is global, and thus the index can't be
4022 set until all local symbols are output. */
4024 struct bfd_elf_section_data
*sec_data
;
4025 unsigned long symndx
;
4026 unsigned long extsymoff
;
4027 struct elf_link_hash_entry
*h
;
4029 /* The point of this little dance to the first SHF_GROUP section
4030 then back to the SHT_GROUP section is that this gets us to
4031 the SHT_GROUP in the input object. */
4032 igroup
= elf_sec_group (elf_next_in_group (sec
));
4033 sec_data
= elf_section_data (igroup
);
4034 symndx
= sec_data
->this_hdr
.sh_info
;
4036 if (!elf_bad_symtab (igroup
->owner
))
4038 Elf_Internal_Shdr
*symtab_hdr
;
4040 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
4041 extsymoff
= symtab_hdr
->sh_info
;
4043 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
4044 while (h
->root
.type
== bfd_link_hash_indirect
4045 || h
->root
.type
== bfd_link_hash_warning
)
4046 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4048 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
4051 /* The contents won't be allocated for "ld -r" or objcopy. */
4053 if (sec
->contents
== NULL
)
4056 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
4058 /* Arrange for the section to be written out. */
4059 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
4060 if (sec
->contents
== NULL
)
4067 loc
= sec
->contents
+ sec
->size
;
4069 /* Get the pointer to the first section in the group that gas
4070 squirreled away here. objcopy arranges for this to be set to the
4071 start of the input section group. */
4072 first
= elt
= elf_next_in_group (sec
);
4074 /* First element is a flag word. Rest of section is elf section
4075 indices for all the sections of the group. Write them backwards
4076 just to keep the group in the same order as given in .section
4077 directives, not that it matters. */
4084 s
= s
->output_section
;
4086 && !bfd_is_abs_section (s
))
4088 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
4089 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
4091 if (elf_sec
->rel
.hdr
!= NULL
4093 || (input_elf_sec
->rel
.hdr
!= NULL
4094 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
4096 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
4098 if (loc
== sec
->contents
)
4100 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
4102 if (elf_sec
->rela
.hdr
!= NULL
4104 || (input_elf_sec
->rela
.hdr
!= NULL
4105 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
4107 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
4109 if (loc
== sec
->contents
)
4111 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
4114 if (loc
== sec
->contents
)
4116 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
4118 elt
= elf_next_in_group (elt
);
4123 /* We should always get here with loc == sec->contents + 4, but it is
4124 possible to craft bogus SHT_GROUP sections that will cause segfaults
4125 in objcopy without checking loc here and in the loop above. */
4126 if (loc
== sec
->contents
)
4131 if (loc
!= sec
->contents
)
4134 memset (sec
->contents
+ 4, 0, loc
- sec
->contents
);
4135 loc
= sec
->contents
;
4139 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
4142 /* Given NAME, the name of a relocation section stripped of its
4143 .rel/.rela prefix, return the section in ABFD to which the
4144 relocations apply. */
4147 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
4149 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4150 section likely apply to .got.plt or .got section. */
4151 if (get_elf_backend_data (abfd
)->want_got_plt
4152 && strcmp (name
, ".plt") == 0)
4157 sec
= bfd_get_section_by_name (abfd
, name
);
4163 return bfd_get_section_by_name (abfd
, name
);
4166 /* Return the section to which RELOC_SEC applies. */
4169 elf_get_reloc_section (asection
*reloc_sec
)
4174 const struct elf_backend_data
*bed
;
4176 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
4177 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
4180 /* We look up the section the relocs apply to by name. */
4181 name
= reloc_sec
->name
;
4182 if (!startswith (name
, ".rel"))
4185 if (type
== SHT_RELA
&& *name
++ != 'a')
4188 abfd
= reloc_sec
->owner
;
4189 bed
= get_elf_backend_data (abfd
);
4190 return bed
->get_reloc_section (abfd
, name
);
4193 /* Assign all ELF section numbers. The dummy first section is handled here
4194 too. The link/info pointers for the standard section types are filled
4195 in here too, while we're at it. LINK_INFO will be 0 when arriving
4196 here for gas, objcopy, and when using the generic ELF linker. */
4199 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
4201 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
4203 unsigned int section_number
;
4204 Elf_Internal_Shdr
**i_shdrp
;
4205 struct bfd_elf_section_data
*d
;
4211 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
4213 /* SHT_GROUP sections are in relocatable files only. */
4214 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
4216 size_t reloc_count
= 0;
4218 /* Put SHT_GROUP sections first. */
4219 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4221 d
= elf_section_data (sec
);
4223 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
4225 if (sec
->flags
& SEC_LINKER_CREATED
)
4227 /* Remove the linker created SHT_GROUP sections. */
4228 bfd_section_list_remove (abfd
, sec
);
4229 abfd
->section_count
--;
4232 d
->this_idx
= section_number
++;
4235 /* Count relocations. */
4236 reloc_count
+= sec
->reloc_count
;
4239 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4240 if (reloc_count
== 0)
4241 abfd
->flags
&= ~HAS_RELOC
;
4243 abfd
->flags
|= HAS_RELOC
;
4246 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4248 d
= elf_section_data (sec
);
4250 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
4251 d
->this_idx
= section_number
++;
4252 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
4253 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
4256 d
->rel
.idx
= section_number
++;
4257 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
4258 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
4265 d
->rela
.idx
= section_number
++;
4266 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
4267 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
4273 need_symtab
= (bfd_get_symcount (abfd
) > 0
4274 || (link_info
== NULL
4275 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4279 elf_onesymtab (abfd
) = section_number
++;
4280 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
4281 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
4283 elf_section_list
*entry
;
4285 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
4287 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
4288 entry
->ndx
= section_number
++;
4289 elf_symtab_shndx_list (abfd
) = entry
;
4291 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
4292 ".symtab_shndx", false);
4293 if (entry
->hdr
.sh_name
== (unsigned int) -1)
4296 elf_strtab_sec (abfd
) = section_number
++;
4297 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
4300 elf_shstrtab_sec (abfd
) = section_number
++;
4301 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
4302 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
4304 if (section_number
>= SHN_LORESERVE
)
4306 /* xgettext:c-format */
4307 _bfd_error_handler (_("%pB: too many sections: %u"),
4308 abfd
, section_number
);
4312 elf_numsections (abfd
) = section_number
;
4313 elf_elfheader (abfd
)->e_shnum
= section_number
;
4315 /* Set up the list of section header pointers, in agreement with the
4317 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
4318 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
4319 if (i_shdrp
== NULL
)
4322 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
4323 sizeof (Elf_Internal_Shdr
));
4324 if (i_shdrp
[0] == NULL
)
4326 bfd_release (abfd
, i_shdrp
);
4330 elf_elfsections (abfd
) = i_shdrp
;
4332 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
4335 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
4336 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
4338 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
4339 BFD_ASSERT (entry
!= NULL
);
4340 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
4341 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
4343 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
4344 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
4347 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4351 d
= elf_section_data (sec
);
4353 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
4354 if (d
->rel
.idx
!= 0)
4355 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
4356 if (d
->rela
.idx
!= 0)
4357 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
4359 /* Fill in the sh_link and sh_info fields while we're at it. */
4361 /* sh_link of a reloc section is the section index of the symbol
4362 table. sh_info is the section index of the section to which
4363 the relocation entries apply. */
4364 if (d
->rel
.idx
!= 0)
4366 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
4367 d
->rel
.hdr
->sh_info
= d
->this_idx
;
4368 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
4370 if (d
->rela
.idx
!= 0)
4372 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
4373 d
->rela
.hdr
->sh_info
= d
->this_idx
;
4374 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
4377 /* We need to set up sh_link for SHF_LINK_ORDER. */
4378 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
4380 s
= elf_linked_to_section (sec
);
4381 /* We can now have a NULL linked section pointer.
4382 This happens when the sh_link field is 0, which is done
4383 when a linked to section is discarded but the linking
4384 section has been retained for some reason. */
4387 /* Check discarded linkonce section. */
4388 if (discarded_section (s
))
4392 /* xgettext:c-format */
4393 (_("%pB: sh_link of section `%pA' points to"
4394 " discarded section `%pA' of `%pB'"),
4395 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
4396 /* Point to the kept section if it has the same
4397 size as the discarded one. */
4398 kept
= _bfd_elf_check_kept_section (s
, link_info
);
4401 bfd_set_error (bfd_error_bad_value
);
4406 /* Handle objcopy. */
4407 else if (s
->output_section
== NULL
)
4410 /* xgettext:c-format */
4411 (_("%pB: sh_link of section `%pA' points to"
4412 " removed section `%pA' of `%pB'"),
4413 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
4414 bfd_set_error (bfd_error_bad_value
);
4417 s
= s
->output_section
;
4418 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4422 switch (d
->this_hdr
.sh_type
)
4426 /* sh_link is the section index of the symbol table.
4427 sh_info is the section index of the section to which the
4428 relocation entries apply. */
4429 if (d
->this_hdr
.sh_link
== 0)
4431 /* FIXME maybe: If this is a reloc section which we are
4432 treating as a normal section then we likely should
4433 not be assuming its sh_link is .dynsym or .symtab. */
4434 if ((sec
->flags
& SEC_ALLOC
) != 0)
4436 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4438 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4441 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4444 s
= elf_get_reloc_section (sec
);
4447 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
4448 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
4453 /* We assume that a section named .stab*str is a stabs
4454 string section. We look for a section with the same name
4455 but without the trailing ``str'', and set its sh_link
4456 field to point to this section. */
4457 if (startswith (sec
->name
, ".stab")
4458 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
4463 len
= strlen (sec
->name
);
4464 alc
= (char *) bfd_malloc (len
- 2);
4467 memcpy (alc
, sec
->name
, len
- 3);
4468 alc
[len
- 3] = '\0';
4469 s
= bfd_get_section_by_name (abfd
, alc
);
4473 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
4475 /* This is a .stab section. */
4476 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
4483 case SHT_GNU_verneed
:
4484 case SHT_GNU_verdef
:
4485 /* sh_link is the section header index of the string table
4486 used for the dynamic entries, or the symbol table, or the
4488 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4490 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4493 case SHT_GNU_LIBLIST
:
4494 /* sh_link is the section header index of the prelink library
4495 list used for the dynamic entries, or the symbol table, or
4496 the version strings. */
4497 s
= bfd_get_section_by_name (abfd
, ((sec
->flags
& SEC_ALLOC
)
4498 ? ".dynstr" : ".gnu.libstr"));
4500 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4505 case SHT_GNU_versym
:
4506 /* sh_link is the section header index of the symbol table
4507 this hash table or version table is for. */
4508 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4510 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4514 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4518 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4519 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4520 debug section name from .debug_* to .zdebug_* if needed. */
4526 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4528 /* If the backend has a special mapping, use it. */
4529 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4530 if (bed
->elf_backend_sym_is_global
)
4531 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4533 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4534 || bfd_is_und_section (bfd_asymbol_section (sym
))
4535 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4538 /* Filter global symbols of ABFD to include in the import library. All
4539 SYMCOUNT symbols of ABFD can be examined from their pointers in
4540 SYMS. Pointers of symbols to keep should be stored contiguously at
4541 the beginning of that array.
4543 Returns the number of symbols to keep. */
4546 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4547 asymbol
**syms
, long symcount
)
4549 long src_count
, dst_count
= 0;
4551 for (src_count
= 0; src_count
< symcount
; src_count
++)
4553 asymbol
*sym
= syms
[src_count
];
4554 char *name
= (char *) bfd_asymbol_name (sym
);
4555 struct bfd_link_hash_entry
*h
;
4557 if (!sym_is_global (abfd
, sym
))
4560 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, false);
4563 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4565 if (h
->linker_def
|| h
->ldscript_def
)
4568 syms
[dst_count
++] = sym
;
4571 syms
[dst_count
] = NULL
;
4576 /* Don't output symbols for sections that are not going to be output,
4577 that are duplicates or there is no BFD section. */
4580 ignore_sym (asymbol
*sym
)
4585 if (sym
->section
== NULL
)
4588 if ((sym
->flags
& BSF_SECTION_SYM
) != 0)
4590 if ((sym
->flags
& BSF_SECTION_SYM_USED
) == 0)
4592 /* With ld -r on generic elf targets it is possible to have
4593 multiple section symbols in the output for a given section.
4594 We'd like to get rid of all but the first one. This drops
4595 them if the first input section is non-zero size, but fails
4596 to do so if the first input section is zero sized. */
4597 if (sym
->section
->output_offset
!= 0)
4601 return discarded_section (sym
->section
);
4604 /* Map symbol from it's internal number to the external number, moving
4605 all local symbols to be at the head of the list. */
4608 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4610 unsigned int symcount
= bfd_get_symcount (abfd
);
4611 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4612 asymbol
**sect_syms
;
4613 unsigned int num_locals
= 0;
4614 unsigned int num_globals
= 0;
4615 unsigned int max_index
= 0;
4622 fprintf (stderr
, "elf_map_symbols\n");
4626 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4628 if (max_index
< asect
->index
)
4629 max_index
= asect
->index
;
4633 amt
= max_index
* sizeof (asymbol
*);
4634 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4635 if (sect_syms
== NULL
)
4637 elf_section_syms (abfd
) = sect_syms
;
4638 elf_num_section_syms (abfd
) = max_index
;
4640 /* Init sect_syms entries for any section symbols we have already
4641 decided to output. */
4642 for (idx
= 0; idx
< symcount
; idx
++)
4644 asymbol
*sym
= syms
[idx
];
4646 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4648 && !ignore_sym (sym
)
4649 && !bfd_is_abs_section (sym
->section
))
4651 asection
*sec
= sym
->section
;
4653 if (sec
->owner
!= abfd
)
4654 sec
= sec
->output_section
;
4656 sect_syms
[sec
->index
] = syms
[idx
];
4660 /* Classify all of the symbols. */
4661 for (idx
= 0; idx
< symcount
; idx
++)
4663 if (ignore_sym (syms
[idx
]))
4665 if (sym_is_global (abfd
, syms
[idx
]))
4671 /* We will be adding a section symbol for each normal BFD section. Most
4672 sections will already have a section symbol in outsymbols, but
4673 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4674 at least in that case. */
4675 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4677 asymbol
*sym
= asect
->symbol
;
4678 /* Don't include ignored section symbols. */
4679 if (!ignore_sym (sym
)
4680 && sect_syms
[asect
->index
] == NULL
)
4682 if (sym_is_global (abfd
, asect
->symbol
))
4689 /* Now sort the symbols so the local symbols are first. */
4690 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4691 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4692 if (new_syms
== NULL
)
4695 unsigned int num_globals2
= 0;
4696 unsigned int num_locals2
= 0;
4697 for (idx
= 0; idx
< symcount
; idx
++)
4699 asymbol
*sym
= syms
[idx
];
4702 if (ignore_sym (sym
))
4705 if (sym_is_global (abfd
, sym
))
4706 i
= num_locals
+ num_globals2
++;
4710 sym
->udata
.i
= i
+ 1;
4712 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4714 asymbol
*sym
= asect
->symbol
;
4715 if (!ignore_sym (sym
)
4716 && sect_syms
[asect
->index
] == NULL
)
4720 sect_syms
[asect
->index
] = sym
;
4721 if (sym_is_global (abfd
, sym
))
4722 i
= num_locals
+ num_globals2
++;
4726 sym
->udata
.i
= i
+ 1;
4730 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4732 *pnum_locals
= num_locals
;
4736 /* Align to the maximum file alignment that could be required for any
4737 ELF data structure. */
4739 static inline file_ptr
4740 align_file_position (file_ptr off
, int align
)
4742 return (off
+ align
- 1) & ~(align
- 1);
4745 /* Assign a file position to a section, optionally aligning to the
4746 required section alignment. */
4749 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4753 if (align
&& i_shdrp
->sh_addralign
> 1)
4754 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
& -i_shdrp
->sh_addralign
);
4755 i_shdrp
->sh_offset
= offset
;
4756 if (i_shdrp
->bfd_section
!= NULL
)
4757 i_shdrp
->bfd_section
->filepos
= offset
;
4758 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4759 offset
+= i_shdrp
->sh_size
;
4763 /* Compute the file positions we are going to put the sections at, and
4764 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4765 is not NULL, this is being called by the ELF backend linker. */
4768 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4769 struct bfd_link_info
*link_info
)
4771 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4772 struct fake_section_arg fsargs
;
4774 struct elf_strtab_hash
*strtab
= NULL
;
4775 Elf_Internal_Shdr
*shstrtab_hdr
;
4778 if (abfd
->output_has_begun
)
4781 /* Do any elf backend specific processing first. */
4782 if (bed
->elf_backend_begin_write_processing
)
4783 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4785 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4788 fsargs
.failed
= false;
4789 fsargs
.link_info
= link_info
;
4790 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4794 if (!assign_section_numbers (abfd
, link_info
))
4797 /* The backend linker builds symbol table information itself. */
4798 need_symtab
= (link_info
== NULL
4799 && (bfd_get_symcount (abfd
) > 0
4800 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4804 /* Non-zero if doing a relocatable link. */
4805 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4807 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
, link_info
))
4812 if (link_info
== NULL
)
4814 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4816 goto err_free_strtab
;
4819 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4820 /* sh_name was set in init_file_header. */
4821 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4822 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4823 shstrtab_hdr
->sh_addr
= 0;
4824 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4825 shstrtab_hdr
->sh_entsize
= 0;
4826 shstrtab_hdr
->sh_link
= 0;
4827 shstrtab_hdr
->sh_info
= 0;
4828 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4829 shstrtab_hdr
->sh_addralign
= 1;
4831 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4832 goto err_free_strtab
;
4837 Elf_Internal_Shdr
*hdr
;
4839 off
= elf_next_file_pos (abfd
);
4841 hdr
= & elf_symtab_hdr (abfd
);
4842 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4844 if (elf_symtab_shndx_list (abfd
) != NULL
)
4846 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4847 if (hdr
->sh_size
!= 0)
4848 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4849 /* FIXME: What about other symtab_shndx sections in the list ? */
4852 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4853 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4855 elf_next_file_pos (abfd
) = off
;
4857 /* Now that we know where the .strtab section goes, write it
4859 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4860 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4861 goto err_free_strtab
;
4862 _bfd_elf_strtab_free (strtab
);
4865 abfd
->output_has_begun
= true;
4870 _bfd_elf_strtab_free (strtab
);
4874 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4875 function effectively returns whether --eh-frame-hdr is given on the
4876 command line. After size_dynamic_sections the result reflects
4877 whether .eh_frame_hdr will actually be output (sizing isn't done
4878 until ldemul_after_allocation). */
4881 elf_eh_frame_hdr (const struct bfd_link_info
*info
)
4883 if (info
!= NULL
&& is_elf_hash_table (info
->hash
))
4884 return elf_hash_table (info
)->eh_info
.hdr_sec
;
4888 /* Make an initial estimate of the size of the program header. If we
4889 get the number wrong here, we'll redo section placement. */
4891 static bfd_size_type
4892 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4896 const struct elf_backend_data
*bed
;
4898 /* Assume we will need exactly two PT_LOAD segments: one for text
4899 and one for data. */
4902 s
= bfd_get_section_by_name (abfd
, ".interp");
4903 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4905 /* If we have a loadable interpreter section, we need a
4906 PT_INTERP segment. In this case, assume we also need a
4907 PT_PHDR segment, although that may not be true for all
4912 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4914 /* We need a PT_DYNAMIC segment. */
4918 if (info
!= NULL
&& info
->relro
)
4920 /* We need a PT_GNU_RELRO segment. */
4924 if (elf_eh_frame_hdr (info
))
4926 /* We need a PT_GNU_EH_FRAME segment. */
4930 if (elf_stack_flags (abfd
))
4932 /* We need a PT_GNU_STACK segment. */
4936 if (elf_sframe (abfd
))
4938 /* We need a PT_GNU_SFRAME segment. */
4942 s
= bfd_get_section_by_name (abfd
,
4943 NOTE_GNU_PROPERTY_SECTION_NAME
);
4944 if (s
!= NULL
&& s
->size
!= 0)
4946 /* We need a PT_GNU_PROPERTY segment. */
4950 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4952 if ((s
->flags
& SEC_LOAD
) != 0
4953 && elf_section_type (s
) == SHT_NOTE
)
4955 unsigned int alignment_power
;
4956 /* We need a PT_NOTE segment. */
4958 /* Try to create just one PT_NOTE segment for all adjacent
4959 loadable SHT_NOTE sections. gABI requires that within a
4960 PT_NOTE segment (and also inside of each SHT_NOTE section)
4961 each note should have the same alignment. So we check
4962 whether the sections are correctly aligned. */
4963 alignment_power
= s
->alignment_power
;
4964 while (s
->next
!= NULL
4965 && s
->next
->alignment_power
== alignment_power
4966 && (s
->next
->flags
& SEC_LOAD
) != 0
4967 && elf_section_type (s
->next
) == SHT_NOTE
)
4972 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4974 if (s
->flags
& SEC_THREAD_LOCAL
)
4976 /* We need a PT_TLS segment. */
4982 bed
= get_elf_backend_data (abfd
);
4984 if ((abfd
->flags
& D_PAGED
) != 0
4985 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4987 /* Add a PT_GNU_MBIND segment for each mbind section. */
4988 bfd_vma commonpagesize
;
4989 unsigned int page_align_power
;
4992 commonpagesize
= info
->commonpagesize
;
4994 commonpagesize
= bed
->commonpagesize
;
4995 page_align_power
= bfd_log2 (commonpagesize
);
4996 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4997 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4999 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
5002 /* xgettext:c-format */
5003 (_("%pB: GNU_MBIND section `%pA' has invalid "
5004 "sh_info field: %d"),
5005 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
5008 /* Align mbind section to page size. */
5009 if (s
->alignment_power
< page_align_power
)
5010 s
->alignment_power
= page_align_power
;
5015 /* Let the backend count up any program headers it might need. */
5016 if (bed
->elf_backend_additional_program_headers
)
5020 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
5026 return segs
* bed
->s
->sizeof_phdr
;
5029 /* Find the segment that contains the output_section of section. */
5032 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
5034 struct elf_segment_map
*m
;
5035 Elf_Internal_Phdr
*p
;
5037 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
5043 for (i
= m
->count
- 1; i
>= 0; i
--)
5044 if (m
->sections
[i
] == section
)
5051 /* Create a mapping from a set of sections to a program segment. */
5053 static struct elf_segment_map
*
5054 make_mapping (bfd
*abfd
,
5055 asection
**sections
,
5060 struct elf_segment_map
*m
;
5065 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5066 amt
+= (to
- from
) * sizeof (asection
*);
5067 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5071 m
->p_type
= PT_LOAD
;
5072 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
5073 m
->sections
[i
- from
] = *hdrpp
;
5074 m
->count
= to
- from
;
5076 if (from
== 0 && phdr
)
5078 /* Include the headers in the first PT_LOAD segment. */
5079 m
->includes_filehdr
= 1;
5080 m
->includes_phdrs
= 1;
5086 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
5089 struct elf_segment_map
*
5090 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
5092 struct elf_segment_map
*m
;
5094 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
5095 sizeof (struct elf_segment_map
));
5099 m
->p_type
= PT_DYNAMIC
;
5101 m
->sections
[0] = dynsec
;
5106 /* Possibly add or remove segments from the segment map. */
5109 elf_modify_segment_map (bfd
*abfd
,
5110 struct bfd_link_info
*info
,
5111 bool remove_empty_load
)
5113 struct elf_segment_map
**m
;
5114 const struct elf_backend_data
*bed
;
5116 /* The placement algorithm assumes that non allocated sections are
5117 not in PT_LOAD segments. We ensure this here by removing such
5118 sections from the segment map. We also remove excluded
5119 sections. Finally, any PT_LOAD segment without sections is
5121 m
= &elf_seg_map (abfd
);
5124 unsigned int i
, new_count
;
5126 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
5128 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
5129 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
5130 || (*m
)->p_type
!= PT_LOAD
))
5132 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
5136 (*m
)->count
= new_count
;
5138 if (remove_empty_load
5139 && (*m
)->p_type
== PT_LOAD
5141 && !(*m
)->includes_phdrs
)
5147 bed
= get_elf_backend_data (abfd
);
5148 if (bed
->elf_backend_modify_segment_map
!= NULL
)
5150 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
5157 #define IS_TBSS(s) \
5158 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5160 /* Set up a mapping from BFD sections to program segments. Update
5161 NEED_LAYOUT if the section layout is changed. */
5164 _bfd_elf_map_sections_to_segments (bfd
*abfd
,
5165 struct bfd_link_info
*info
,
5169 struct elf_segment_map
*m
;
5170 asection
**sections
= NULL
;
5171 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5174 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
5178 info
->user_phdrs
= !no_user_phdrs
;
5180 /* Size the relative relocations if DT_RELR is enabled. */
5181 if (info
->enable_dt_relr
5182 && need_layout
!= NULL
5183 && bed
->size_relative_relocs
5184 && !bed
->size_relative_relocs (info
, need_layout
))
5185 info
->callbacks
->einfo
5186 (_("%F%P: failed to size relative relocations\n"));
5189 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
5193 struct elf_segment_map
*mfirst
;
5194 struct elf_segment_map
**pm
;
5197 unsigned int hdr_index
;
5198 bfd_vma maxpagesize
;
5200 bool phdr_in_segment
;
5203 unsigned int tls_count
= 0;
5204 asection
*first_tls
= NULL
;
5205 asection
*first_mbind
= NULL
;
5206 asection
*dynsec
, *eh_frame_hdr
;
5209 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
5210 bfd_size_type phdr_size
; /* Octets/bytes. */
5211 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5213 /* Select the allocated sections, and sort them. */
5215 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
5216 sections
= (asection
**) bfd_malloc (amt
);
5217 if (sections
== NULL
)
5220 /* Calculate top address, avoiding undefined behaviour of shift
5221 left operator when shift count is equal to size of type
5223 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
5224 addr_mask
= (addr_mask
<< 1) + 1;
5227 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5229 if ((s
->flags
& SEC_ALLOC
) != 0)
5231 /* target_index is unused until bfd_elf_final_link
5232 starts output of section symbols. Use it to make
5234 s
->target_index
= i
;
5237 /* A wrapping section potentially clashes with header. */
5238 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
5239 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
5242 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
5245 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
5247 phdr_size
= elf_program_header_size (abfd
);
5248 if (phdr_size
== (bfd_size_type
) -1)
5249 phdr_size
= get_program_header_size (abfd
, info
);
5250 phdr_size
+= bed
->s
->sizeof_ehdr
;
5251 /* phdr_size is compared to LMA values which are in bytes. */
5254 maxpagesize
= info
->maxpagesize
;
5256 maxpagesize
= bed
->maxpagesize
;
5257 if (maxpagesize
== 0)
5259 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
5261 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
5262 >= (phdr_size
& (maxpagesize
- 1))))
5263 /* For compatibility with old scripts that may not be using
5264 SIZEOF_HEADERS, add headers when it looks like space has
5265 been left for them. */
5266 phdr_in_segment
= true;
5268 /* Build the mapping. */
5272 /* If we have a .interp section, then create a PT_PHDR segment for
5273 the program headers and a PT_INTERP segment for the .interp
5275 s
= bfd_get_section_by_name (abfd
, ".interp");
5276 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
5278 amt
= sizeof (struct elf_segment_map
);
5279 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5283 m
->p_type
= PT_PHDR
;
5285 m
->p_flags_valid
= 1;
5286 m
->includes_phdrs
= 1;
5287 phdr_in_segment
= true;
5291 amt
= sizeof (struct elf_segment_map
);
5292 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5296 m
->p_type
= PT_INTERP
;
5304 /* Look through the sections. We put sections in the same program
5305 segment when the start of the second section can be placed within
5306 a few bytes of the end of the first section. */
5312 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
5314 && (dynsec
->flags
& SEC_LOAD
) == 0)
5317 if ((abfd
->flags
& D_PAGED
) == 0)
5318 phdr_in_segment
= false;
5320 /* Deal with -Ttext or something similar such that the first section
5321 is not adjacent to the program headers. This is an
5322 approximation, since at this point we don't know exactly how many
5323 program headers we will need. */
5324 if (phdr_in_segment
&& count
> 0)
5326 bfd_vma phdr_lma
; /* Bytes. */
5327 bool separate_phdr
= false;
5329 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
5331 && info
->separate_code
5332 && (sections
[0]->flags
& SEC_CODE
) != 0)
5334 /* If data sections should be separate from code and
5335 thus not executable, and the first section is
5336 executable then put the file and program headers in
5337 their own PT_LOAD. */
5338 separate_phdr
= true;
5339 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
5340 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
5342 /* The file and program headers are currently on the
5343 same page as the first section. Put them on the
5344 previous page if we can. */
5345 if (phdr_lma
>= maxpagesize
)
5346 phdr_lma
-= maxpagesize
;
5348 separate_phdr
= false;
5351 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
5352 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
5353 /* If file and program headers would be placed at the end
5354 of memory then it's probably better to omit them. */
5355 phdr_in_segment
= false;
5356 else if (phdr_lma
< wrap_to
)
5357 /* If a section wraps around to where we'll be placing
5358 file and program headers, then the headers will be
5360 phdr_in_segment
= false;
5361 else if (separate_phdr
)
5363 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
5366 m
->p_paddr
= phdr_lma
* opb
;
5368 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
5369 m
->p_paddr_valid
= 1;
5372 phdr_in_segment
= false;
5376 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
5383 /* See if this section and the last one will fit in the same
5386 if (last_hdr
== NULL
)
5388 /* If we don't have a segment yet, then we don't need a new
5389 one (we build the last one after this loop). */
5390 new_segment
= false;
5392 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
5394 /* If this section has a different relation between the
5395 virtual address and the load address, then we need a new
5399 else if (hdr
->lma
< last_hdr
->lma
+ last_size
5400 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
5402 /* If this section has a load address that makes it overlap
5403 the previous section, then we need a new segment. */
5406 else if ((abfd
->flags
& D_PAGED
) != 0
5407 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
5408 == (hdr
->lma
& -maxpagesize
)))
5410 /* If we are demand paged then we can't map two disk
5411 pages onto the same memory page. */
5412 new_segment
= false;
5414 /* In the next test we have to be careful when last_hdr->lma is close
5415 to the end of the address space. If the aligned address wraps
5416 around to the start of the address space, then there are no more
5417 pages left in memory and it is OK to assume that the current
5418 section can be included in the current segment. */
5419 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
5420 + maxpagesize
> last_hdr
->lma
)
5421 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
5422 + maxpagesize
<= hdr
->lma
))
5424 /* If putting this section in this segment would force us to
5425 skip a page in the segment, then we need a new segment. */
5428 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
5429 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
5431 /* We don't want to put a loaded section after a
5432 nonloaded (ie. bss style) section in the same segment
5433 as that will force the non-loaded section to be loaded.
5434 Consider .tbss sections as loaded for this purpose. */
5437 else if ((abfd
->flags
& D_PAGED
) == 0)
5439 /* If the file is not demand paged, which means that we
5440 don't require the sections to be correctly aligned in the
5441 file, then there is no other reason for a new segment. */
5442 new_segment
= false;
5444 else if (info
!= NULL
5445 && info
->separate_code
5446 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
5451 && (hdr
->flags
& SEC_READONLY
) == 0)
5453 /* We don't want to put a writable section in a read only
5459 /* Otherwise, we can use the same segment. */
5460 new_segment
= false;
5463 /* Allow interested parties a chance to override our decision. */
5464 if (last_hdr
!= NULL
5466 && info
->callbacks
->override_segment_assignment
!= NULL
)
5468 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
5474 if ((hdr
->flags
& SEC_READONLY
) == 0)
5476 if ((hdr
->flags
& SEC_CODE
) != 0)
5479 /* .tbss sections effectively have zero size. */
5480 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5484 /* We need a new program segment. We must create a new program
5485 header holding all the sections from hdr_index until hdr. */
5487 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5494 if ((hdr
->flags
& SEC_READONLY
) == 0)
5499 if ((hdr
->flags
& SEC_CODE
) == 0)
5505 /* .tbss sections effectively have zero size. */
5506 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5508 phdr_in_segment
= false;
5511 /* Create a final PT_LOAD program segment, but not if it's just
5513 if (last_hdr
!= NULL
5514 && (i
- hdr_index
!= 1
5515 || !IS_TBSS (last_hdr
)))
5517 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5525 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5528 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5535 /* For each batch of consecutive loadable SHT_NOTE sections,
5536 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5537 because if we link together nonloadable .note sections and
5538 loadable .note sections, we will generate two .note sections
5539 in the output file. */
5540 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5542 if ((s
->flags
& SEC_LOAD
) != 0
5543 && elf_section_type (s
) == SHT_NOTE
)
5546 unsigned int alignment_power
= s
->alignment_power
;
5549 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5551 if (s2
->next
->alignment_power
== alignment_power
5552 && (s2
->next
->flags
& SEC_LOAD
) != 0
5553 && elf_section_type (s2
->next
) == SHT_NOTE
5554 && align_power (s2
->lma
+ s2
->size
/ opb
,
5561 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5562 amt
+= count
* sizeof (asection
*);
5563 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5567 m
->p_type
= PT_NOTE
;
5571 m
->sections
[m
->count
- count
--] = s
;
5572 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5575 m
->sections
[m
->count
- 1] = s
;
5576 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5580 if (s
->flags
& SEC_THREAD_LOCAL
)
5586 if (first_mbind
== NULL
5587 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5591 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5594 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5595 amt
+= tls_count
* sizeof (asection
*);
5596 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5601 m
->count
= tls_count
;
5602 /* Mandated PF_R. */
5604 m
->p_flags_valid
= 1;
5606 for (i
= 0; i
< tls_count
; ++i
)
5608 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5611 (_("%pB: TLS sections are not adjacent:"), abfd
);
5614 while (i
< tls_count
)
5616 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5618 _bfd_error_handler (_(" TLS: %pA"), s
);
5622 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5625 bfd_set_error (bfd_error_bad_value
);
5637 && (abfd
->flags
& D_PAGED
) != 0
5638 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5639 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5640 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5641 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5643 /* Mandated PF_R. */
5644 unsigned long p_flags
= PF_R
;
5645 if ((s
->flags
& SEC_READONLY
) == 0)
5647 if ((s
->flags
& SEC_CODE
) != 0)
5650 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5651 m
= bfd_zalloc (abfd
, amt
);
5655 m
->p_type
= (PT_GNU_MBIND_LO
5656 + elf_section_data (s
)->this_hdr
.sh_info
);
5658 m
->p_flags_valid
= 1;
5660 m
->p_flags
= p_flags
;
5666 s
= bfd_get_section_by_name (abfd
,
5667 NOTE_GNU_PROPERTY_SECTION_NAME
);
5668 if (s
!= NULL
&& s
->size
!= 0)
5670 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5671 m
= bfd_zalloc (abfd
, amt
);
5675 m
->p_type
= PT_GNU_PROPERTY
;
5677 m
->p_flags_valid
= 1;
5684 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5686 eh_frame_hdr
= elf_eh_frame_hdr (info
);
5687 if (eh_frame_hdr
!= NULL
5688 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5690 amt
= sizeof (struct elf_segment_map
);
5691 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5695 m
->p_type
= PT_GNU_EH_FRAME
;
5697 m
->sections
[0] = eh_frame_hdr
->output_section
;
5703 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5705 sframe
= elf_sframe (abfd
);
5707 && (sframe
->output_section
->flags
& SEC_LOAD
) != 0
5708 && sframe
->size
!= 0)
5710 amt
= sizeof (struct elf_segment_map
);
5711 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5715 m
->p_type
= PT_GNU_SFRAME
;
5717 m
->sections
[0] = sframe
->output_section
;
5723 if (elf_stack_flags (abfd
))
5725 amt
= sizeof (struct elf_segment_map
);
5726 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5730 m
->p_type
= PT_GNU_STACK
;
5731 m
->p_flags
= elf_stack_flags (abfd
);
5732 m
->p_align
= bed
->stack_align
;
5733 m
->p_flags_valid
= 1;
5734 m
->p_align_valid
= m
->p_align
!= 0;
5735 if (info
->stacksize
> 0)
5737 m
->p_size
= info
->stacksize
;
5738 m
->p_size_valid
= 1;
5745 if (info
!= NULL
&& info
->relro
)
5747 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5749 if (m
->p_type
== PT_LOAD
5751 && m
->sections
[0]->vma
>= info
->relro_start
5752 && m
->sections
[0]->vma
< info
->relro_end
)
5755 while (--i
!= (unsigned) -1)
5757 if (m
->sections
[i
]->size
> 0
5758 && (m
->sections
[i
]->flags
& SEC_LOAD
) != 0
5759 && (m
->sections
[i
]->flags
& SEC_HAS_CONTENTS
) != 0)
5763 if (i
!= (unsigned) -1)
5768 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5771 amt
= sizeof (struct elf_segment_map
);
5772 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5776 m
->p_type
= PT_GNU_RELRO
;
5783 elf_seg_map (abfd
) = mfirst
;
5786 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
|| info
== NULL
))
5789 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5791 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5800 /* Sort sections by address. */
5803 elf_sort_sections (const void *arg1
, const void *arg2
)
5805 const asection
*sec1
= *(const asection
**) arg1
;
5806 const asection
*sec2
= *(const asection
**) arg2
;
5807 bfd_size_type size1
, size2
;
5809 /* Sort by LMA first, since this is the address used to
5810 place the section into a segment. */
5811 if (sec1
->lma
< sec2
->lma
)
5813 else if (sec1
->lma
> sec2
->lma
)
5816 /* Then sort by VMA. Normally the LMA and the VMA will be
5817 the same, and this will do nothing. */
5818 if (sec1
->vma
< sec2
->vma
)
5820 else if (sec1
->vma
> sec2
->vma
)
5823 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5825 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5833 else if (TOEND (sec2
))
5838 /* Sort by size, to put zero sized sections
5839 before others at the same address. */
5841 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5842 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5849 return sec1
->target_index
- sec2
->target_index
;
5852 /* This qsort comparison functions sorts PT_LOAD segments first and
5853 by p_paddr, for assign_file_positions_for_load_sections. */
5856 elf_sort_segments (const void *arg1
, const void *arg2
)
5858 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5859 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5861 if (m1
->p_type
!= m2
->p_type
)
5863 if (m1
->p_type
== PT_NULL
)
5865 if (m2
->p_type
== PT_NULL
)
5867 return m1
->p_type
< m2
->p_type
? -1 : 1;
5869 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5870 return m1
->includes_filehdr
? -1 : 1;
5871 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5872 return m1
->no_sort_lma
? -1 : 1;
5873 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5875 bfd_vma lma1
, lma2
; /* Octets. */
5877 if (m1
->p_paddr_valid
)
5879 else if (m1
->count
!= 0)
5881 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5883 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5886 if (m2
->p_paddr_valid
)
5888 else if (m2
->count
!= 0)
5890 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5892 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5895 return lma1
< lma2
? -1 : 1;
5897 if (m1
->idx
!= m2
->idx
)
5898 return m1
->idx
< m2
->idx
? -1 : 1;
5902 /* Ian Lance Taylor writes:
5904 We shouldn't be using % with a negative signed number. That's just
5905 not good. We have to make sure either that the number is not
5906 negative, or that the number has an unsigned type. When the types
5907 are all the same size they wind up as unsigned. When file_ptr is a
5908 larger signed type, the arithmetic winds up as signed long long,
5911 What we're trying to say here is something like ``increase OFF by
5912 the least amount that will cause it to be equal to the VMA modulo
5914 /* In other words, something like:
5916 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5917 off_offset = off % bed->maxpagesize;
5918 if (vma_offset < off_offset)
5919 adjustment = vma_offset + bed->maxpagesize - off_offset;
5921 adjustment = vma_offset - off_offset;
5923 which can be collapsed into the expression below. */
5926 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5928 /* PR binutils/16199: Handle an alignment of zero. */
5929 if (maxpagesize
== 0)
5931 return ((vma
- off
) % maxpagesize
);
5935 print_segment_map (const struct elf_segment_map
*m
)
5938 const char *pt
= get_segment_type (m
->p_type
);
5943 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5944 sprintf (buf
, "LOPROC+%7.7x",
5945 (unsigned int) (m
->p_type
- PT_LOPROC
));
5946 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5947 sprintf (buf
, "LOOS+%7.7x",
5948 (unsigned int) (m
->p_type
- PT_LOOS
));
5950 snprintf (buf
, sizeof (buf
), "%8.8x",
5951 (unsigned int) m
->p_type
);
5955 fprintf (stderr
, "%s:", pt
);
5956 for (j
= 0; j
< m
->count
; j
++)
5957 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5962 /* Assign file positions to the sections based on the mapping from
5963 sections to segments. This function also sets up some fields in
5967 assign_file_positions_for_load_sections (bfd
*abfd
,
5968 struct bfd_link_info
*link_info
)
5970 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5971 struct elf_segment_map
*m
;
5972 struct elf_segment_map
*phdr_load_seg
;
5973 Elf_Internal_Phdr
*phdrs
;
5974 Elf_Internal_Phdr
*p
;
5975 file_ptr off
; /* Octets. */
5976 bfd_size_type maxpagesize
;
5977 unsigned int alloc
, actual
;
5979 struct elf_segment_map
**sorted_seg_map
;
5980 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5982 if (link_info
== NULL
5983 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
, NULL
))
5987 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5992 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5993 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5997 /* PR binutils/12467. */
5998 elf_elfheader (abfd
)->e_phoff
= 0;
5999 elf_elfheader (abfd
)->e_phentsize
= 0;
6002 elf_elfheader (abfd
)->e_phnum
= alloc
;
6004 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
6007 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
6011 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
6012 BFD_ASSERT (elf_program_header_size (abfd
)
6013 == actual
* bed
->s
->sizeof_phdr
);
6014 BFD_ASSERT (actual
>= alloc
);
6019 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
6023 /* We're writing the size in elf_program_header_size (abfd),
6024 see assign_file_positions_except_relocs, so make sure we have
6025 that amount allocated, with trailing space cleared.
6026 The variable alloc contains the computed need, while
6027 elf_program_header_size (abfd) contains the size used for the
6029 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6030 where the layout is forced to according to a larger size in the
6031 last iterations for the testcase ld-elf/header. */
6032 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
6033 + alloc
* sizeof (*sorted_seg_map
)));
6034 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
6035 elf_tdata (abfd
)->phdr
= phdrs
;
6039 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
6041 sorted_seg_map
[j
] = m
;
6042 /* If elf_segment_map is not from map_sections_to_segments, the
6043 sections may not be correctly ordered. NOTE: sorting should
6044 not be done to the PT_NOTE section of a corefile, which may
6045 contain several pseudo-sections artificially created by bfd.
6046 Sorting these pseudo-sections breaks things badly. */
6048 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
6049 && m
->p_type
== PT_NOTE
))
6051 for (i
= 0; i
< m
->count
; i
++)
6052 m
->sections
[i
]->target_index
= i
;
6053 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
6058 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
6062 if ((abfd
->flags
& D_PAGED
) != 0)
6064 if (link_info
!= NULL
)
6065 maxpagesize
= link_info
->maxpagesize
;
6067 maxpagesize
= bed
->maxpagesize
;
6070 /* Sections must map to file offsets past the ELF file header. */
6071 off
= bed
->s
->sizeof_ehdr
;
6072 /* And if one of the PT_LOAD headers doesn't include the program
6073 headers then we'll be mapping program headers in the usual
6074 position after the ELF file header. */
6075 phdr_load_seg
= NULL
;
6076 for (j
= 0; j
< alloc
; j
++)
6078 m
= sorted_seg_map
[j
];
6079 if (m
->p_type
!= PT_LOAD
)
6081 if (m
->includes_phdrs
)
6087 if (phdr_load_seg
== NULL
)
6088 off
+= actual
* bed
->s
->sizeof_phdr
;
6090 for (j
= 0; j
< alloc
; j
++)
6093 bfd_vma off_adjust
; /* Octets. */
6095 bfd_size_type p_align
;
6098 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6099 number of sections with contents contributing to both p_filesz
6100 and p_memsz, followed by a number of sections with no contents
6101 that just contribute to p_memsz. In this loop, OFF tracks next
6102 available file offset for PT_LOAD and PT_NOTE segments. */
6103 m
= sorted_seg_map
[j
];
6105 p
->p_type
= m
->p_type
;
6106 p
->p_flags
= m
->p_flags
;
6107 p_align
= bed
->p_align
;
6111 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
6113 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
6115 if (m
->p_paddr_valid
)
6116 p
->p_paddr
= m
->p_paddr
;
6117 else if (m
->count
== 0)
6120 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
6122 if (p
->p_type
== PT_LOAD
6123 && (abfd
->flags
& D_PAGED
) != 0)
6125 /* p_align in demand paged PT_LOAD segments effectively stores
6126 the maximum page size. When copying an executable with
6127 objcopy, we set m->p_align from the input file. Use this
6128 value for maxpagesize rather than bed->maxpagesize, which
6129 may be different. Note that we use maxpagesize for PT_TLS
6130 segment alignment later in this function, so we are relying
6131 on at least one PT_LOAD segment appearing before a PT_TLS
6133 if (m
->p_align_valid
)
6134 maxpagesize
= m
->p_align
;
6135 else if (p_align
!= 0
6136 && (link_info
== NULL
6137 || !link_info
->maxpagesize_is_set
))
6138 /* Set p_align to the default p_align value while laying
6139 out segments aligning to the maximum page size or the
6140 largest section alignment. The run-time loader can
6141 align segments to the default p_align value or the
6142 maximum page size, depending on system page size. */
6145 p
->p_align
= maxpagesize
;
6147 else if (m
->p_align_valid
)
6148 p
->p_align
= m
->p_align
;
6149 else if (m
->count
== 0)
6150 p
->p_align
= 1 << bed
->s
->log_file_align
;
6152 if (m
== phdr_load_seg
)
6154 if (!m
->includes_filehdr
)
6156 off
+= actual
* bed
->s
->sizeof_phdr
;
6159 no_contents
= false;
6161 if (p
->p_type
== PT_LOAD
6164 bfd_size_type align
; /* Bytes. */
6165 unsigned int align_power
= 0;
6167 if (m
->p_align_valid
)
6171 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
6173 unsigned int secalign
;
6175 secalign
= bfd_section_alignment (*secpp
);
6176 if (secalign
> align_power
)
6177 align_power
= secalign
;
6179 align
= (bfd_size_type
) 1 << align_power
;
6180 if (align
< maxpagesize
)
6182 /* If a section requires alignment higher than the
6183 default p_align value, don't set p_align to the
6184 default p_align value. */
6185 if (align
> p_align
)
6187 align
= maxpagesize
;
6191 /* If a section requires alignment higher than the
6192 maximum page size, set p_align to the section
6199 for (i
= 0; i
< m
->count
; i
++)
6200 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
6201 /* If we aren't making room for this section, then
6202 it must be SHT_NOBITS regardless of what we've
6203 set via struct bfd_elf_special_section. */
6204 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
6206 /* Find out whether this segment contains any loadable
6209 for (i
= 0; i
< m
->count
; i
++)
6210 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
6212 no_contents
= false;
6216 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
6218 /* Broken hardware and/or kernel require that files do not
6219 map the same page with different permissions on some hppa
6222 && (abfd
->flags
& D_PAGED
) != 0
6223 && bed
->no_page_alias
6224 && (off
& (maxpagesize
- 1)) != 0
6225 && ((off
& -maxpagesize
)
6226 == ((off
+ off_adjust
) & -maxpagesize
)))
6227 off_adjust
+= maxpagesize
;
6231 /* We shouldn't need to align the segment on disk since
6232 the segment doesn't need file space, but the gABI
6233 arguably requires the alignment and glibc ld.so
6234 checks it. So to comply with the alignment
6235 requirement but not waste file space, we adjust
6236 p_offset for just this segment. (OFF_ADJUST is
6237 subtracted from OFF later.) This may put p_offset
6238 past the end of file, but that shouldn't matter. */
6243 /* Make sure the .dynamic section is the first section in the
6244 PT_DYNAMIC segment. */
6245 else if (p
->p_type
== PT_DYNAMIC
6247 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
6250 (_("%pB: The first section in the PT_DYNAMIC segment"
6251 " is not the .dynamic section"),
6253 bfd_set_error (bfd_error_bad_value
);
6256 /* Set the note section type to SHT_NOTE. */
6257 else if (p
->p_type
== PT_NOTE
)
6258 for (i
= 0; i
< m
->count
; i
++)
6259 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
6261 if (m
->includes_filehdr
)
6263 if (!m
->p_flags_valid
)
6265 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
6266 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
6267 if (p
->p_type
== PT_LOAD
)
6271 if (p
->p_vaddr
< (bfd_vma
) off
6272 || (!m
->p_paddr_valid
6273 && p
->p_paddr
< (bfd_vma
) off
))
6276 (_("%pB: not enough room for program headers,"
6277 " try linking with -N"),
6279 bfd_set_error (bfd_error_bad_value
);
6283 if (!m
->p_paddr_valid
)
6287 else if (sorted_seg_map
[0]->includes_filehdr
)
6289 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
6290 p
->p_vaddr
= filehdr
->p_vaddr
;
6291 if (!m
->p_paddr_valid
)
6292 p
->p_paddr
= filehdr
->p_paddr
;
6296 if (m
->includes_phdrs
)
6298 if (!m
->p_flags_valid
)
6300 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
6301 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
6302 if (!m
->includes_filehdr
)
6304 if (p
->p_type
== PT_LOAD
)
6306 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
6309 p
->p_vaddr
-= off
- p
->p_offset
;
6310 if (!m
->p_paddr_valid
)
6311 p
->p_paddr
-= off
- p
->p_offset
;
6314 else if (phdr_load_seg
!= NULL
)
6316 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
6317 bfd_vma phdr_off
= 0; /* Octets. */
6318 if (phdr_load_seg
->includes_filehdr
)
6319 phdr_off
= bed
->s
->sizeof_ehdr
;
6320 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
6321 if (!m
->p_paddr_valid
)
6322 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
6323 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
6326 p
->p_offset
= bed
->s
->sizeof_ehdr
;
6330 if (p
->p_type
== PT_LOAD
6331 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
6333 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
6338 /* Put meaningless p_offset for PT_LOAD segments
6339 without file contents somewhere within the first
6340 page, in an attempt to not point past EOF. */
6341 bfd_size_type align
= maxpagesize
;
6342 if (align
< p
->p_align
)
6346 p
->p_offset
= off
% align
;
6351 file_ptr adjust
; /* Octets. */
6353 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
6355 p
->p_filesz
+= adjust
;
6356 p
->p_memsz
+= adjust
;
6360 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6361 maps. Set filepos for sections in PT_LOAD segments, and in
6362 core files, for sections in PT_NOTE segments.
6363 assign_file_positions_for_non_load_sections will set filepos
6364 for other sections and update p_filesz for other segments. */
6365 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
6368 bfd_size_type align
;
6369 Elf_Internal_Shdr
*this_hdr
;
6372 this_hdr
= &elf_section_data (sec
)->this_hdr
;
6373 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
6375 if ((p
->p_type
== PT_LOAD
6376 || p
->p_type
== PT_TLS
)
6377 && (this_hdr
->sh_type
!= SHT_NOBITS
6378 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
6379 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
6380 || p
->p_type
== PT_TLS
))))
6382 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
6383 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
6384 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
6385 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
6389 || p_end
< p_start
))
6392 /* xgettext:c-format */
6393 (_("%pB: section %pA lma %#" PRIx64
6394 " adjusted to %#" PRIx64
),
6395 abfd
, sec
, (uint64_t) s_start
/ opb
,
6396 (uint64_t) p_end
/ opb
);
6398 sec
->lma
= p_end
/ opb
;
6400 p
->p_memsz
+= adjust
;
6402 if (p
->p_type
== PT_LOAD
)
6404 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6407 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
6409 /* We have a PROGBITS section following NOBITS ones.
6410 Allocate file space for the NOBITS section(s).
6411 We don't need to write out the zeros, posix
6412 fseek past the end of data already written
6413 followed by a write at that location is
6414 guaranteed to result in zeros being read
6416 adjust
= p
->p_memsz
- p
->p_filesz
;
6419 /* We only adjust sh_offset in SHT_NOBITS sections
6420 as would seem proper for their address when the
6421 section is first in the segment. sh_offset
6422 doesn't really have any significance for
6423 SHT_NOBITS anyway, apart from a notional position
6424 relative to other sections. Historically we
6425 didn't bother with adjusting sh_offset and some
6426 programs depend on it not being adjusted. See
6427 pr12921 and pr25662. */
6428 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
6431 if (this_hdr
->sh_type
== SHT_NOBITS
)
6432 off_adjust
+= adjust
;
6435 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6436 p
->p_filesz
+= adjust
;
6439 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
6441 /* The section at i == 0 is the one that actually contains
6445 this_hdr
->sh_offset
= sec
->filepos
= off
;
6446 off
+= this_hdr
->sh_size
;
6447 p
->p_filesz
= this_hdr
->sh_size
;
6453 /* The rest are fake sections that shouldn't be written. */
6462 if (p
->p_type
== PT_LOAD
)
6464 this_hdr
->sh_offset
= sec
->filepos
= off
;
6465 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6466 off
+= this_hdr
->sh_size
;
6468 else if (this_hdr
->sh_type
== SHT_NOBITS
6469 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
6470 && this_hdr
->sh_offset
== 0)
6472 /* This is a .tbss section that didn't get a PT_LOAD.
6473 (See _bfd_elf_map_sections_to_segments "Create a
6474 final PT_LOAD".) Set sh_offset to the value it
6475 would have if we had created a zero p_filesz and
6476 p_memsz PT_LOAD header for the section. This
6477 also makes the PT_TLS header have the same
6479 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
6481 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
6484 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6486 p
->p_filesz
+= this_hdr
->sh_size
;
6487 /* A load section without SHF_ALLOC is something like
6488 a note section in a PT_NOTE segment. These take
6489 file space but are not loaded into memory. */
6490 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6491 p
->p_memsz
+= this_hdr
->sh_size
;
6493 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6495 if (p
->p_type
== PT_TLS
)
6496 p
->p_memsz
+= this_hdr
->sh_size
;
6498 /* .tbss is special. It doesn't contribute to p_memsz of
6500 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
6501 p
->p_memsz
+= this_hdr
->sh_size
;
6504 if (align
> p
->p_align
6505 && !m
->p_align_valid
6506 && (p
->p_type
!= PT_LOAD
6507 || (abfd
->flags
& D_PAGED
) == 0))
6511 if (!m
->p_flags_valid
)
6514 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
6516 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
6523 /* PR ld/20815 - Check that the program header segment, if
6524 present, will be loaded into memory. */
6525 if (p
->p_type
== PT_PHDR
6526 && phdr_load_seg
== NULL
6527 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
6528 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
6530 /* The fix for this error is usually to edit the linker script being
6531 used and set up the program headers manually. Either that or
6532 leave room for the headers at the start of the SECTIONS. */
6533 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6534 " by LOAD segment"),
6536 if (link_info
== NULL
)
6538 /* Arrange for the linker to exit with an error, deleting
6539 the output file unless --noinhibit-exec is given. */
6540 link_info
->callbacks
->info ("%X");
6543 /* Check that all sections are in a PT_LOAD segment.
6544 Don't check funky gdb generated core files. */
6545 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
6547 bool check_vma
= true;
6549 for (i
= 1; i
< m
->count
; i
++)
6550 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
6551 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
6552 ->this_hdr
), p
) != 0
6553 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
6554 ->this_hdr
), p
) != 0)
6556 /* Looks like we have overlays packed into the segment. */
6561 for (i
= 0; i
< m
->count
; i
++)
6563 Elf_Internal_Shdr
*this_hdr
;
6566 sec
= m
->sections
[i
];
6567 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6568 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6569 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6572 /* xgettext:c-format */
6573 (_("%pB: section `%pA' can't be allocated in segment %d"),
6575 print_segment_map (m
);
6580 p
->p_align
= p_align
;
6584 elf_next_file_pos (abfd
) = off
;
6586 if (link_info
!= NULL
6587 && phdr_load_seg
!= NULL
6588 && phdr_load_seg
->includes_filehdr
)
6590 /* There is a segment that contains both the file headers and the
6591 program headers, so provide a symbol __ehdr_start pointing there.
6592 A program can use this to examine itself robustly. */
6594 struct elf_link_hash_entry
*hash
6595 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6596 false, false, true);
6597 /* If the symbol was referenced and not defined, define it. */
6599 && (hash
->root
.type
== bfd_link_hash_new
6600 || hash
->root
.type
== bfd_link_hash_undefined
6601 || hash
->root
.type
== bfd_link_hash_undefweak
6602 || hash
->root
.type
== bfd_link_hash_common
))
6605 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6607 if (phdr_load_seg
->count
!= 0)
6608 /* The segment contains sections, so use the first one. */
6609 s
= phdr_load_seg
->sections
[0];
6611 /* Use the first (i.e. lowest-addressed) section in any segment. */
6612 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6613 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6621 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6622 hash
->root
.u
.def
.section
= s
;
6626 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6627 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6630 hash
->root
.type
= bfd_link_hash_defined
;
6631 hash
->def_regular
= 1;
6639 /* Determine if a bfd is a debuginfo file. Unfortunately there
6640 is no defined method for detecting such files, so we have to
6641 use heuristics instead. */
6644 is_debuginfo_file (bfd
*abfd
)
6646 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6649 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6650 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6651 Elf_Internal_Shdr
**headerp
;
6653 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6655 Elf_Internal_Shdr
*header
= * headerp
;
6657 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6658 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6659 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6660 && header
->sh_type
!= SHT_NOBITS
6661 && header
->sh_type
!= SHT_NOTE
)
6668 /* Assign file positions for other sections, except for compressed debug
6669 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6672 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6673 struct bfd_link_info
*link_info
)
6675 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6676 Elf_Internal_Shdr
**i_shdrpp
;
6677 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6678 Elf_Internal_Phdr
*phdrs
;
6679 Elf_Internal_Phdr
*p
;
6680 struct elf_segment_map
*m
;
6682 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6683 bfd_vma maxpagesize
;
6685 if (link_info
!= NULL
)
6686 maxpagesize
= link_info
->maxpagesize
;
6688 maxpagesize
= bed
->maxpagesize
;
6689 i_shdrpp
= elf_elfsections (abfd
);
6690 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6691 off
= elf_next_file_pos (abfd
);
6692 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6694 Elf_Internal_Shdr
*hdr
;
6698 if (hdr
->bfd_section
!= NULL
6699 && (hdr
->bfd_section
->filepos
!= 0
6700 || (hdr
->sh_type
== SHT_NOBITS
6701 && hdr
->contents
== NULL
)))
6702 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6703 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6705 if (hdr
->sh_size
!= 0
6706 /* PR 24717 - debuginfo files are known to be not strictly
6707 compliant with the ELF standard. In particular they often
6708 have .note.gnu.property sections that are outside of any
6709 loadable segment. This is not a problem for such files,
6710 so do not warn about them. */
6711 && ! is_debuginfo_file (abfd
))
6713 /* xgettext:c-format */
6714 (_("%pB: warning: allocated section `%s' not in segment"),
6716 (hdr
->bfd_section
== NULL
6718 : hdr
->bfd_section
->name
));
6719 /* We don't need to page align empty sections. */
6720 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6721 align
= maxpagesize
;
6723 align
= hdr
->sh_addralign
& -hdr
->sh_addralign
;
6724 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
, align
);
6725 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6728 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6729 && hdr
->bfd_section
== NULL
)
6730 /* We don't know the offset of these sections yet:
6731 their size has not been decided. */
6732 || (abfd
->is_linker_output
6733 && hdr
->bfd_section
!= NULL
6734 && (hdr
->sh_name
== -1u
6735 || bfd_section_is_ctf (hdr
->bfd_section
)))
6736 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6737 || (elf_symtab_shndx_list (abfd
) != NULL
6738 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6739 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6740 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6741 hdr
->sh_offset
= -1;
6743 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6745 elf_next_file_pos (abfd
) = off
;
6747 /* Now that we have set the section file positions, we can set up
6748 the file positions for the non PT_LOAD segments. */
6749 phdrs
= elf_tdata (abfd
)->phdr
;
6750 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6752 if (p
->p_type
== PT_GNU_RELRO
)
6754 bfd_vma start
, end
; /* Bytes. */
6757 if (link_info
!= NULL
)
6759 /* During linking the range of the RELRO segment is passed
6760 in link_info. Note that there may be padding between
6761 relro_start and the first RELRO section. */
6762 start
= link_info
->relro_start
;
6763 end
= link_info
->relro_end
;
6765 else if (m
->count
!= 0)
6767 if (!m
->p_size_valid
)
6769 start
= m
->sections
[0]->vma
;
6770 end
= start
+ m
->p_size
/ opb
;
6781 struct elf_segment_map
*lm
;
6782 const Elf_Internal_Phdr
*lp
;
6785 /* Find a LOAD segment containing a section in the RELRO
6787 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6789 lm
= lm
->next
, lp
++)
6791 if (lp
->p_type
== PT_LOAD
6793 && (lm
->sections
[lm
->count
- 1]->vma
6794 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6795 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6797 && lm
->sections
[0]->vma
< end
)
6803 /* Find the section starting the RELRO segment. */
6804 for (i
= 0; i
< lm
->count
; i
++)
6806 asection
*s
= lm
->sections
[i
];
6815 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6816 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6817 p
->p_offset
= lm
->sections
[i
]->filepos
;
6818 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6819 p
->p_filesz
= p
->p_memsz
;
6821 /* The RELRO segment typically ends a few bytes
6822 into .got.plt but other layouts are possible.
6823 In cases where the end does not match any
6824 loaded section (for instance is in file
6825 padding), trim p_filesz back to correspond to
6826 the end of loaded section contents. */
6827 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6828 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6830 /* Preserve the alignment and flags if they are
6831 valid. The gold linker generates RW/4 for
6832 the PT_GNU_RELRO section. It is better for
6833 objcopy/strip to honor these attributes
6834 otherwise gdb will choke when using separate
6836 if (!m
->p_align_valid
)
6838 if (!m
->p_flags_valid
)
6847 if (link_info
!= NULL
)
6849 (_("%pB: warning: unable to allocate any sections"
6850 " to PT_GNU_RELRO segment"),
6852 memset (p
, 0, sizeof *p
);
6855 else if (p
->p_type
== PT_GNU_STACK
)
6857 if (m
->p_size_valid
)
6858 p
->p_memsz
= m
->p_size
;
6860 else if (m
->count
!= 0)
6864 if (p
->p_type
!= PT_LOAD
6865 && (p
->p_type
!= PT_NOTE
6866 || bfd_get_format (abfd
) != bfd_core
))
6868 /* A user specified segment layout may include a PHDR
6869 segment that overlaps with a LOAD segment... */
6870 if (p
->p_type
== PT_PHDR
)
6876 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6878 /* PR 17512: file: 2195325e. */
6880 (_("%pB: error: non-load segment %d includes file header "
6881 "and/or program header"),
6882 abfd
, (int) (p
- phdrs
));
6887 p
->p_offset
= m
->sections
[0]->filepos
;
6888 for (i
= m
->count
; i
-- != 0;)
6890 asection
*sect
= m
->sections
[i
];
6891 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6892 if (hdr
->sh_type
!= SHT_NOBITS
)
6894 p
->p_filesz
= sect
->filepos
- p
->p_offset
+ hdr
->sh_size
;
6895 /* NB: p_memsz of the loadable PT_NOTE segment
6896 should be the same as p_filesz. */
6897 if (p
->p_type
== PT_NOTE
6898 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
6899 p
->p_memsz
= p
->p_filesz
;
6910 static elf_section_list
*
6911 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6913 for (;list
!= NULL
; list
= list
->next
)
6919 /* Work out the file positions of all the sections. This is called by
6920 _bfd_elf_compute_section_file_positions. All the section sizes and
6921 VMAs must be known before this is called.
6923 Reloc sections come in two flavours: Those processed specially as
6924 "side-channel" data attached to a section to which they apply, and
6925 those that bfd doesn't process as relocations. The latter sort are
6926 stored in a normal bfd section by bfd_section_from_shdr. We don't
6927 consider the former sort here, unless they form part of the loadable
6928 image. Reloc sections not assigned here (and compressed debugging
6929 sections and CTF sections which nothing else in the file can rely
6930 upon) will be handled later by assign_file_positions_for_relocs.
6932 We also don't set the positions of the .symtab and .strtab here. */
6935 assign_file_positions_except_relocs (bfd
*abfd
,
6936 struct bfd_link_info
*link_info
)
6938 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6939 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6940 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6943 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6944 && bfd_get_format (abfd
) != bfd_core
)
6946 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6947 unsigned int num_sec
= elf_numsections (abfd
);
6948 Elf_Internal_Shdr
**hdrpp
;
6952 /* Start after the ELF header. */
6953 off
= i_ehdrp
->e_ehsize
;
6955 /* We are not creating an executable, which means that we are
6956 not creating a program header, and that the actual order of
6957 the sections in the file is unimportant. */
6958 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6960 Elf_Internal_Shdr
*hdr
;
6963 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6964 && hdr
->bfd_section
== NULL
)
6965 /* Do not assign offsets for these sections yet: we don't know
6967 || (abfd
->is_linker_output
6968 && hdr
->bfd_section
!= NULL
6969 && (hdr
->sh_name
== -1u
6970 || bfd_section_is_ctf (hdr
->bfd_section
)))
6971 || i
== elf_onesymtab (abfd
)
6972 || (elf_symtab_shndx_list (abfd
) != NULL
6973 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6974 || i
== elf_strtab_sec (abfd
)
6975 || i
== elf_shstrtab_sec (abfd
))
6977 hdr
->sh_offset
= -1;
6980 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6983 elf_next_file_pos (abfd
) = off
;
6984 elf_program_header_size (abfd
) = 0;
6988 /* Assign file positions for the loaded sections based on the
6989 assignment of sections to segments. */
6990 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6993 /* And for non-load sections. */
6994 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6998 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
7001 /* Write out the program headers. */
7002 alloc
= i_ehdrp
->e_phnum
;
7005 if (link_info
!= NULL
&& ! link_info
->no_warn_rwx_segments
)
7007 bool warned_tls
= false;
7008 bool warned_rwx
= false;
7010 /* Memory resident segments with non-zero size and RWX
7011 permissions are a security risk, so we generate a warning
7012 here if we are creating any. */
7015 for (i
= 0; i
< alloc
; i
++)
7017 const Elf_Internal_Phdr
* phdr
= tdata
->phdr
+ i
;
7019 if (phdr
->p_memsz
== 0)
7023 && phdr
->p_type
== PT_TLS
7024 && (phdr
->p_flags
& PF_X
))
7026 if (link_info
->warn_is_error_for_rwx_segments
)
7028 _bfd_error_handler (_("\
7029 error: %pB has a TLS segment with execute permission"),
7034 _bfd_error_handler (_("\
7035 warning: %pB has a TLS segment with execute permission"),
7042 else if (! warned_rwx
7043 && phdr
->p_type
== PT_LOAD
7044 && ((phdr
->p_flags
& (PF_R
| PF_W
| PF_X
))
7045 == (PF_R
| PF_W
| PF_X
)))
7047 if (link_info
->warn_is_error_for_rwx_segments
)
7049 _bfd_error_handler (_("\
7050 error: %pB has a LOAD segment with RWX permissions"),
7055 _bfd_error_handler (_("\
7056 warning: %pB has a LOAD segment with RWX permissions"),
7066 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
7067 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
7075 _bfd_elf_init_file_header (bfd
*abfd
,
7076 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
7078 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
7079 struct elf_strtab_hash
*shstrtab
;
7080 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7082 i_ehdrp
= elf_elfheader (abfd
);
7084 shstrtab
= _bfd_elf_strtab_init ();
7085 if (shstrtab
== NULL
)
7088 elf_shstrtab (abfd
) = shstrtab
;
7090 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
7091 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
7092 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
7093 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
7095 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
7096 i_ehdrp
->e_ident
[EI_DATA
] =
7097 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
7098 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
7100 if ((abfd
->flags
& DYNAMIC
) != 0)
7101 i_ehdrp
->e_type
= ET_DYN
;
7102 else if ((abfd
->flags
& EXEC_P
) != 0)
7103 i_ehdrp
->e_type
= ET_EXEC
;
7104 else if (bfd_get_format (abfd
) == bfd_core
)
7105 i_ehdrp
->e_type
= ET_CORE
;
7107 i_ehdrp
->e_type
= ET_REL
;
7109 switch (bfd_get_arch (abfd
))
7111 case bfd_arch_unknown
:
7112 i_ehdrp
->e_machine
= EM_NONE
;
7115 /* There used to be a long list of cases here, each one setting
7116 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7117 in the corresponding bfd definition. To avoid duplication,
7118 the switch was removed. Machines that need special handling
7119 can generally do it in elf_backend_final_write_processing(),
7120 unless they need the information earlier than the final write.
7121 Such need can generally be supplied by replacing the tests for
7122 e_machine with the conditions used to determine it. */
7124 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
7127 i_ehdrp
->e_version
= bed
->s
->ev_current
;
7128 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
7130 /* No program header, for now. */
7131 i_ehdrp
->e_phoff
= 0;
7132 i_ehdrp
->e_phentsize
= 0;
7133 i_ehdrp
->e_phnum
= 0;
7135 /* Each bfd section is section header entry. */
7136 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
7137 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
7139 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
7140 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
7141 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
7142 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
7143 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
7144 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
7145 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
7146 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
7147 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
7153 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7155 FIXME: We used to have code here to sort the PT_LOAD segments into
7156 ascending order, as per the ELF spec. But this breaks some programs,
7157 including the Linux kernel. But really either the spec should be
7158 changed or the programs updated. */
7161 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
7163 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
7165 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
7166 unsigned int num_segments
= i_ehdrp
->e_phnum
;
7167 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
7168 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
7169 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
7171 /* Find the lowest p_vaddr in PT_LOAD segments. */
7172 bfd_vma p_vaddr
= (bfd_vma
) -1;
7173 for (; segment
< end_segment
; segment
++)
7174 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
7175 p_vaddr
= segment
->p_vaddr
;
7177 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7178 segments is non-zero. */
7180 i_ehdrp
->e_type
= ET_EXEC
;
7185 /* Assign file positions for all the reloc sections which are not part
7186 of the loadable file image, and the file position of section headers. */
7189 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
7192 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
7193 Elf_Internal_Shdr
*shdrp
;
7194 Elf_Internal_Ehdr
*i_ehdrp
;
7195 const struct elf_backend_data
*bed
;
7197 /* Skip non-load sections without section header. */
7198 if ((abfd
->flags
& BFD_NO_SECTION_HEADER
) != 0)
7201 off
= elf_next_file_pos (abfd
);
7203 shdrpp
= elf_elfsections (abfd
);
7204 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
7205 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
7208 if (shdrp
->sh_offset
== -1)
7210 asection
*sec
= shdrp
->bfd_section
;
7212 || shdrp
->sh_type
== SHT_REL
7213 || shdrp
->sh_type
== SHT_RELA
)
7215 else if (bfd_section_is_ctf (sec
))
7217 /* Update section size and contents. */
7218 shdrp
->sh_size
= sec
->size
;
7219 shdrp
->contents
= sec
->contents
;
7221 else if (shdrp
->sh_name
== -1u)
7223 const char *name
= sec
->name
;
7224 struct bfd_elf_section_data
*d
;
7226 /* Compress DWARF debug sections. */
7227 if (!bfd_compress_section (abfd
, sec
, shdrp
->contents
))
7230 if (sec
->compress_status
== COMPRESS_SECTION_DONE
7231 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0
7234 /* If section is compressed with zlib-gnu, convert
7235 section name from .debug_* to .zdebug_*. */
7236 char *new_name
= bfd_debug_name_to_zdebug (abfd
, name
);
7237 if (new_name
== NULL
)
7241 /* Add section name to section name section. */
7243 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
7245 d
= elf_section_data (sec
);
7247 /* Add reloc section name to section name section. */
7249 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rel
.hdr
,
7253 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rela
.hdr
,
7257 /* Update section size and contents. */
7258 shdrp
->sh_size
= sec
->size
;
7259 shdrp
->contents
= sec
->contents
;
7260 sec
->contents
= NULL
;
7263 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
7267 /* Place section name section after DWARF debug sections have been
7269 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
7270 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
7271 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
7272 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
7274 /* Place the section headers. */
7275 i_ehdrp
= elf_elfheader (abfd
);
7276 bed
= get_elf_backend_data (abfd
);
7277 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
7278 i_ehdrp
->e_shoff
= off
;
7279 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
7280 elf_next_file_pos (abfd
) = off
;
7286 _bfd_elf_write_object_contents (bfd
*abfd
)
7288 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7289 Elf_Internal_Shdr
**i_shdrp
;
7291 unsigned int count
, num_sec
;
7292 struct elf_obj_tdata
*t
;
7294 if (! abfd
->output_has_begun
7295 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7297 /* Do not rewrite ELF data when the BFD has been opened for update.
7298 abfd->output_has_begun was set to TRUE on opening, so creation of
7299 new sections, and modification of existing section sizes was
7300 restricted. This means the ELF header, program headers and
7301 section headers can't have changed. If the contents of any
7302 sections has been modified, then those changes have already been
7303 written to the BFD. */
7304 else if (abfd
->direction
== both_direction
)
7306 BFD_ASSERT (abfd
->output_has_begun
);
7310 i_shdrp
= elf_elfsections (abfd
);
7313 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
7317 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
7320 /* After writing the headers, we need to write the sections too... */
7321 num_sec
= elf_numsections (abfd
);
7322 for (count
= 1; count
< num_sec
; count
++)
7324 /* Don't set the sh_name field without section header. */
7325 if ((abfd
->flags
& BFD_NO_SECTION_HEADER
) == 0)
7326 i_shdrp
[count
]->sh_name
7327 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
7328 i_shdrp
[count
]->sh_name
);
7329 if (bed
->elf_backend_section_processing
)
7330 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
7332 if (i_shdrp
[count
]->contents
)
7334 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
7336 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
7337 || bfd_write (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
7342 /* Write out the section header names. */
7343 t
= elf_tdata (abfd
);
7344 if (elf_shstrtab (abfd
) != NULL
7345 && t
->shstrtab_hdr
.sh_offset
!= -1
7346 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
7347 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
7350 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
7353 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
7356 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7357 if (t
->o
->build_id
.after_write_object_contents
!= NULL
7358 && !(*t
->o
->build_id
.after_write_object_contents
) (abfd
))
7360 if (t
->o
->package_metadata
.after_write_object_contents
!= NULL
7361 && !(*t
->o
->package_metadata
.after_write_object_contents
) (abfd
))
7368 _bfd_elf_write_corefile_contents (bfd
*abfd
)
7370 /* Hopefully this can be done just like an object file. */
7371 return _bfd_elf_write_object_contents (abfd
);
7374 /* Given a section, search the header to find them. */
7377 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
7379 const struct elf_backend_data
*bed
;
7380 unsigned int sec_index
;
7382 if (elf_section_data (asect
) != NULL
7383 && elf_section_data (asect
)->this_idx
!= 0)
7384 return elf_section_data (asect
)->this_idx
;
7386 if (bfd_is_abs_section (asect
))
7387 sec_index
= SHN_ABS
;
7388 else if (bfd_is_com_section (asect
))
7389 sec_index
= SHN_COMMON
;
7390 else if (bfd_is_und_section (asect
))
7391 sec_index
= SHN_UNDEF
;
7393 sec_index
= SHN_BAD
;
7395 bed
= get_elf_backend_data (abfd
);
7396 if (bed
->elf_backend_section_from_bfd_section
)
7398 int retval
= sec_index
;
7400 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
7404 if (sec_index
== SHN_BAD
)
7405 bfd_set_error (bfd_error_nonrepresentable_section
);
7410 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7414 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
7416 asymbol
*asym_ptr
= *asym_ptr_ptr
;
7418 flagword flags
= asym_ptr
->flags
;
7420 /* When gas creates relocations against local labels, it creates its
7421 own symbol for the section, but does put the symbol into the
7422 symbol chain, so udata is 0. When the linker is generating
7423 relocatable output, this section symbol may be for one of the
7424 input sections rather than the output section. */
7425 if (asym_ptr
->udata
.i
== 0
7426 && (flags
& BSF_SECTION_SYM
)
7427 && asym_ptr
->section
)
7431 sec
= asym_ptr
->section
;
7432 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
7433 sec
= sec
->output_section
;
7434 if (sec
->owner
== abfd
7435 && sec
->index
< elf_num_section_syms (abfd
)
7436 && elf_section_syms (abfd
)[sec
->index
] != NULL
)
7437 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
7440 idx
= asym_ptr
->udata
.i
;
7444 /* This case can occur when using --strip-symbol on a symbol
7445 which is used in a relocation entry. */
7447 /* xgettext:c-format */
7448 (_("%pB: symbol `%s' required but not present"),
7449 abfd
, bfd_asymbol_name (asym_ptr
));
7450 bfd_set_error (bfd_error_no_symbols
);
7457 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7458 " flags = 0x%.8x\n",
7459 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
7467 static inline bfd_vma
7468 segment_size (Elf_Internal_Phdr
*segment
)
7470 return (segment
->p_memsz
> segment
->p_filesz
7471 ? segment
->p_memsz
: segment
->p_filesz
);
7475 /* Returns the end address of the segment + 1. */
7476 static inline bfd_vma
7477 segment_end (Elf_Internal_Phdr
*segment
, bfd_vma start
)
7479 return start
+ segment_size (segment
);
7482 static inline bfd_size_type
7483 section_size (asection
*section
, Elf_Internal_Phdr
*segment
)
7485 if ((section
->flags
& SEC_HAS_CONTENTS
) != 0
7486 || (section
->flags
& SEC_THREAD_LOCAL
) == 0
7487 || segment
->p_type
== PT_TLS
)
7488 return section
->size
;
7492 /* Returns TRUE if the given section is contained within the given
7493 segment. LMA addresses are compared against PADDR when
7494 USE_VADDR is false, VMA against VADDR when true. */
7496 is_contained_by (asection
*section
, Elf_Internal_Phdr
*segment
,
7497 bfd_vma paddr
, bfd_vma vaddr
, unsigned int opb
,
7500 bfd_vma seg_addr
= !use_vaddr
? paddr
: vaddr
;
7501 bfd_vma addr
= !use_vaddr
? section
->lma
: section
->vma
;
7503 if (_bfd_mul_overflow (addr
, opb
, &octet
))
7505 /* The third and fourth lines below are testing that the section end
7506 address is within the segment. It's written this way to avoid
7507 overflow. Add seg_addr + section_size to both sides of the
7508 inequality to make it obvious. */
7509 return (octet
>= seg_addr
7510 && segment_size (segment
) >= section_size (section
, segment
)
7511 && (octet
- seg_addr
7512 <= segment_size (segment
) - section_size (section
, segment
)));
7515 /* Handle PT_NOTE segment. */
7517 is_note (asection
*s
, Elf_Internal_Phdr
*p
)
7519 return (p
->p_type
== PT_NOTE
7520 && elf_section_type (s
) == SHT_NOTE
7521 && (ufile_ptr
) s
->filepos
>= p
->p_offset
7522 && p
->p_filesz
>= s
->size
7523 && (ufile_ptr
) s
->filepos
- p
->p_offset
<= p
->p_filesz
- s
->size
);
7526 /* Rewrite program header information. */
7529 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
, bfd_vma maxpagesize
)
7531 Elf_Internal_Ehdr
*iehdr
;
7532 struct elf_segment_map
*map
;
7533 struct elf_segment_map
*map_first
;
7534 struct elf_segment_map
**pointer_to_map
;
7535 Elf_Internal_Phdr
*segment
;
7538 unsigned int num_segments
;
7539 bool phdr_included
= false;
7541 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
7542 unsigned int phdr_adjust_num
= 0;
7543 const struct elf_backend_data
*bed
;
7544 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7546 bed
= get_elf_backend_data (ibfd
);
7547 iehdr
= elf_elfheader (ibfd
);
7550 pointer_to_map
= &map_first
;
7552 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7554 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7555 linker, which generates a PT_INTERP section with p_vaddr and
7556 p_memsz set to 0. */
7557 #define IS_SOLARIS_PT_INTERP(p, s) \
7559 && p->p_paddr == 0 \
7560 && p->p_memsz == 0 \
7561 && p->p_filesz > 0 \
7562 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7564 && (bfd_vma) s->filepos >= p->p_offset \
7565 && ((bfd_vma) s->filepos + s->size \
7566 <= p->p_offset + p->p_filesz))
7568 /* Decide if the given section should be included in the given segment.
7569 A section will be included if:
7570 1. It is within the address space of the segment -- we use the LMA
7571 if that is set for the segment and the VMA otherwise,
7572 2. It is an allocated section or a NOTE section in a PT_NOTE
7574 3. There is an output section associated with it,
7575 4. The section has not already been allocated to a previous segment.
7576 5. PT_GNU_STACK segments do not include any sections.
7577 6. PT_TLS segment includes only SHF_TLS sections.
7578 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7579 8. PT_DYNAMIC should not contain empty sections at the beginning
7580 (with the possible exception of .dynamic). */
7581 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7582 (((is_contained_by (section, segment, segment->p_paddr, \
7583 segment->p_vaddr, opb, !paddr_valid) \
7584 && (section->flags & SEC_ALLOC) != 0) \
7585 || is_note (section, segment)) \
7586 && segment->p_type != PT_GNU_STACK \
7587 && (segment->p_type != PT_TLS \
7588 || (section->flags & SEC_THREAD_LOCAL)) \
7589 && (segment->p_type == PT_LOAD \
7590 || segment->p_type == PT_TLS \
7591 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7592 && (segment->p_type != PT_DYNAMIC \
7593 || section_size (section, segment) > 0 \
7594 || (segment->p_paddr \
7595 ? segment->p_paddr != section->lma * (opb) \
7596 : segment->p_vaddr != section->vma * (opb)) \
7597 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7598 && (segment->p_type != PT_LOAD || !section->segment_mark))
7600 /* If the output section of a section in the input segment is NULL,
7601 it is removed from the corresponding output segment. */
7602 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \
7603 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7604 && section->output_section != NULL)
7606 /* Returns TRUE iff seg1 starts after the end of seg2. */
7607 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7608 (seg1->field >= segment_end (seg2, seg2->field))
7610 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7611 their VMA address ranges and their LMA address ranges overlap.
7612 It is possible to have overlapping VMA ranges without overlapping LMA
7613 ranges. RedBoot images for example can have both .data and .bss mapped
7614 to the same VMA range, but with the .data section mapped to a different
7616 #define SEGMENT_OVERLAPS(seg1, seg2) \
7617 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7618 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7619 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7620 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7622 /* Initialise the segment mark field, and discard stupid alignment. */
7623 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
7625 asection
*o
= section
->output_section
;
7626 if (o
!= NULL
&& o
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
7627 o
->alignment_power
= 0;
7628 section
->segment_mark
= false;
7631 /* The Solaris linker creates program headers in which all the
7632 p_paddr fields are zero. When we try to objcopy or strip such a
7633 file, we get confused. Check for this case, and if we find it
7634 don't set the p_paddr_valid fields. */
7635 p_paddr_valid
= false;
7636 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7639 if (segment
->p_paddr
!= 0)
7641 p_paddr_valid
= true;
7645 /* Scan through the segments specified in the program header
7646 of the input BFD. For this first scan we look for overlaps
7647 in the loadable segments. These can be created by weird
7648 parameters to objcopy. Also, fix some solaris weirdness. */
7649 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7654 Elf_Internal_Phdr
*segment2
;
7656 if (segment
->p_type
== PT_INTERP
)
7657 for (section
= ibfd
->sections
; section
; section
= section
->next
)
7658 if (IS_SOLARIS_PT_INTERP (segment
, section
))
7660 /* Mininal change so that the normal section to segment
7661 assignment code will work. */
7662 segment
->p_vaddr
= section
->vma
* opb
;
7666 if (segment
->p_type
!= PT_LOAD
)
7668 /* Remove PT_GNU_RELRO segment. */
7669 if (segment
->p_type
== PT_GNU_RELRO
)
7670 segment
->p_type
= PT_NULL
;
7674 /* Determine if this segment overlaps any previous segments. */
7675 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7677 bfd_signed_vma extra_length
;
7679 if (segment2
->p_type
!= PT_LOAD
7680 || !SEGMENT_OVERLAPS (segment
, segment2
))
7683 /* Merge the two segments together. */
7684 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7686 /* Extend SEGMENT2 to include SEGMENT and then delete
7688 extra_length
= (segment_end (segment
, segment
->p_vaddr
)
7689 - segment_end (segment2
, segment2
->p_vaddr
));
7691 if (extra_length
> 0)
7693 segment2
->p_memsz
+= extra_length
;
7694 segment2
->p_filesz
+= extra_length
;
7697 segment
->p_type
= PT_NULL
;
7699 /* Since we have deleted P we must restart the outer loop. */
7701 segment
= elf_tdata (ibfd
)->phdr
;
7706 /* Extend SEGMENT to include SEGMENT2 and then delete
7708 extra_length
= (segment_end (segment2
, segment2
->p_vaddr
)
7709 - segment_end (segment
, segment
->p_vaddr
));
7711 if (extra_length
> 0)
7713 segment
->p_memsz
+= extra_length
;
7714 segment
->p_filesz
+= extra_length
;
7717 segment2
->p_type
= PT_NULL
;
7722 /* The second scan attempts to assign sections to segments. */
7723 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7727 unsigned int section_count
;
7728 asection
**sections
;
7729 asection
*output_section
;
7731 asection
*matching_lma
;
7732 asection
*suggested_lma
;
7735 asection
*first_section
;
7737 if (segment
->p_type
== PT_NULL
)
7740 first_section
= NULL
;
7741 /* Compute how many sections might be placed into this segment. */
7742 for (section
= ibfd
->sections
, section_count
= 0;
7744 section
= section
->next
)
7746 /* Find the first section in the input segment, which may be
7747 removed from the corresponding output segment. */
7748 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, opb
, p_paddr_valid
))
7750 if (first_section
== NULL
)
7751 first_section
= section
;
7752 if (section
->output_section
!= NULL
)
7757 /* Allocate a segment map big enough to contain
7758 all of the sections we have selected. */
7759 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7760 amt
+= section_count
* sizeof (asection
*);
7761 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7765 /* Initialise the fields of the segment map. Default to
7766 using the physical address of the segment in the input BFD. */
7768 map
->p_type
= segment
->p_type
;
7769 map
->p_flags
= segment
->p_flags
;
7770 map
->p_flags_valid
= 1;
7772 if (map
->p_type
== PT_LOAD
7773 && (ibfd
->flags
& D_PAGED
) != 0
7775 && segment
->p_align
> 1)
7777 map
->p_align
= segment
->p_align
;
7778 if (segment
->p_align
> maxpagesize
)
7779 map
->p_align
= maxpagesize
;
7780 map
->p_align_valid
= 1;
7783 /* If the first section in the input segment is removed, there is
7784 no need to preserve segment physical address in the corresponding
7786 if (!first_section
|| first_section
->output_section
!= NULL
)
7788 map
->p_paddr
= segment
->p_paddr
;
7789 map
->p_paddr_valid
= p_paddr_valid
;
7792 /* Determine if this segment contains the ELF file header
7793 and if it contains the program headers themselves. */
7794 map
->includes_filehdr
= (segment
->p_offset
== 0
7795 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7796 map
->includes_phdrs
= 0;
7798 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7800 map
->includes_phdrs
=
7801 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7802 && (segment
->p_offset
+ segment
->p_filesz
7803 >= ((bfd_vma
) iehdr
->e_phoff
7804 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7806 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7807 phdr_included
= true;
7810 if (section_count
== 0)
7812 /* Special segments, such as the PT_PHDR segment, may contain
7813 no sections, but ordinary, loadable segments should contain
7814 something. They are allowed by the ELF spec however, so only
7815 a warning is produced.
7816 Don't warn if an empty PT_LOAD contains the program headers.
7817 There is however the valid use case of embedded systems which
7818 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7819 flash memory with zeros. No warning is shown for that case. */
7820 if (segment
->p_type
== PT_LOAD
7821 && !map
->includes_phdrs
7822 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7823 /* xgettext:c-format */
7825 (_("%pB: warning: empty loadable segment detected"
7826 " at vaddr=%#" PRIx64
", is this intentional?"),
7827 ibfd
, (uint64_t) segment
->p_vaddr
);
7829 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7831 *pointer_to_map
= map
;
7832 pointer_to_map
= &map
->next
;
7837 /* Now scan the sections in the input BFD again and attempt
7838 to add their corresponding output sections to the segment map.
7839 The problem here is how to handle an output section which has
7840 been moved (ie had its LMA changed). There are four possibilities:
7842 1. None of the sections have been moved.
7843 In this case we can continue to use the segment LMA from the
7846 2. All of the sections have been moved by the same amount.
7847 In this case we can change the segment's LMA to match the LMA
7848 of the first section.
7850 3. Some of the sections have been moved, others have not.
7851 In this case those sections which have not been moved can be
7852 placed in the current segment which will have to have its size,
7853 and possibly its LMA changed, and a new segment or segments will
7854 have to be created to contain the other sections.
7856 4. The sections have been moved, but not by the same amount.
7857 In this case we can change the segment's LMA to match the LMA
7858 of the first section and we will have to create a new segment
7859 or segments to contain the other sections.
7861 In order to save time, we allocate an array to hold the section
7862 pointers that we are interested in. As these sections get assigned
7863 to a segment, they are removed from this array. */
7865 amt
= section_count
* sizeof (asection
*);
7866 sections
= (asection
**) bfd_malloc (amt
);
7867 if (sections
== NULL
)
7870 /* Step One: Scan for segment vs section LMA conflicts.
7871 Also add the sections to the section array allocated above.
7872 Also add the sections to the current segment. In the common
7873 case, where the sections have not been moved, this means that
7874 we have completely filled the segment, and there is nothing
7877 matching_lma
= NULL
;
7878 suggested_lma
= NULL
;
7880 for (section
= first_section
, j
= 0;
7882 section
= section
->next
)
7884 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, opb
, p_paddr_valid
))
7886 output_section
= section
->output_section
;
7888 sections
[j
++] = section
;
7890 /* The Solaris native linker always sets p_paddr to 0.
7891 We try to catch that case here, and set it to the
7892 correct value. Note - some backends require that
7893 p_paddr be left as zero. */
7895 && segment
->p_vaddr
!= 0
7896 && !bed
->want_p_paddr_set_to_zero
7898 && output_section
->lma
!= 0
7899 && (align_power (segment
->p_vaddr
7900 + (map
->includes_filehdr
7901 ? iehdr
->e_ehsize
: 0)
7902 + (map
->includes_phdrs
7903 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7905 output_section
->alignment_power
* opb
)
7906 == (output_section
->vma
* opb
)))
7907 map
->p_paddr
= segment
->p_vaddr
;
7909 /* Match up the physical address of the segment with the
7910 LMA address of the output section. */
7911 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7913 || is_note (section
, segment
))
7915 if (matching_lma
== NULL
7916 || output_section
->lma
< matching_lma
->lma
)
7917 matching_lma
= output_section
;
7919 /* We assume that if the section fits within the segment
7920 then it does not overlap any other section within that
7922 map
->sections
[isec
++] = output_section
;
7924 else if (suggested_lma
== NULL
)
7925 suggested_lma
= output_section
;
7927 if (j
== section_count
)
7932 BFD_ASSERT (j
== section_count
);
7934 /* Step Two: Adjust the physical address of the current segment,
7936 if (isec
== section_count
)
7938 /* All of the sections fitted within the segment as currently
7939 specified. This is the default case. Add the segment to
7940 the list of built segments and carry on to process the next
7941 program header in the input BFD. */
7942 map
->count
= section_count
;
7943 *pointer_to_map
= map
;
7944 pointer_to_map
= &map
->next
;
7947 && !bed
->want_p_paddr_set_to_zero
)
7949 bfd_vma hdr_size
= 0;
7950 if (map
->includes_filehdr
)
7951 hdr_size
= iehdr
->e_ehsize
;
7952 if (map
->includes_phdrs
)
7953 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7955 /* Account for padding before the first section in the
7957 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7958 - matching_lma
->lma
);
7966 /* Change the current segment's physical address to match
7967 the LMA of the first section that fitted, or if no
7968 section fitted, the first section. */
7969 if (matching_lma
== NULL
)
7970 matching_lma
= suggested_lma
;
7972 map
->p_paddr
= matching_lma
->lma
* opb
;
7974 /* Offset the segment physical address from the lma
7975 to allow for space taken up by elf headers. */
7976 if (map
->includes_phdrs
)
7978 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7980 /* iehdr->e_phnum is just an estimate of the number
7981 of program headers that we will need. Make a note
7982 here of the number we used and the segment we chose
7983 to hold these headers, so that we can adjust the
7984 offset when we know the correct value. */
7985 phdr_adjust_num
= iehdr
->e_phnum
;
7986 phdr_adjust_seg
= map
;
7989 if (map
->includes_filehdr
)
7991 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7992 map
->p_paddr
-= iehdr
->e_ehsize
;
7993 /* We've subtracted off the size of headers from the
7994 first section lma, but there may have been some
7995 alignment padding before that section too. Try to
7996 account for that by adjusting the segment lma down to
7997 the same alignment. */
7998 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7999 align
= segment
->p_align
;
8000 map
->p_paddr
&= -(align
* opb
);
8004 /* Step Three: Loop over the sections again, this time assigning
8005 those that fit to the current segment and removing them from the
8006 sections array; but making sure not to leave large gaps. Once all
8007 possible sections have been assigned to the current segment it is
8008 added to the list of built segments and if sections still remain
8009 to be assigned, a new segment is constructed before repeating
8015 suggested_lma
= NULL
;
8017 /* Fill the current segment with sections that fit. */
8018 for (j
= 0; j
< section_count
; j
++)
8020 section
= sections
[j
];
8022 if (section
== NULL
)
8025 output_section
= section
->output_section
;
8027 BFD_ASSERT (output_section
!= NULL
);
8029 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
8031 || is_note (section
, segment
))
8033 if (map
->count
== 0)
8035 /* If the first section in a segment does not start at
8036 the beginning of the segment, then something is
8038 if (align_power (map
->p_paddr
8039 + (map
->includes_filehdr
8040 ? iehdr
->e_ehsize
: 0)
8041 + (map
->includes_phdrs
8042 ? iehdr
->e_phnum
* iehdr
->e_phentsize
8044 output_section
->alignment_power
* opb
)
8045 != output_section
->lma
* opb
)
8052 prev_sec
= map
->sections
[map
->count
- 1];
8054 /* If the gap between the end of the previous section
8055 and the start of this section is more than
8056 maxpagesize then we need to start a new segment. */
8057 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
8059 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
8060 || (prev_sec
->lma
+ prev_sec
->size
8061 > output_section
->lma
))
8063 if (suggested_lma
== NULL
)
8064 suggested_lma
= output_section
;
8070 map
->sections
[map
->count
++] = output_section
;
8073 if (segment
->p_type
== PT_LOAD
)
8074 section
->segment_mark
= true;
8076 else if (suggested_lma
== NULL
)
8077 suggested_lma
= output_section
;
8080 /* PR 23932. A corrupt input file may contain sections that cannot
8081 be assigned to any segment - because for example they have a
8082 negative size - or segments that do not contain any sections.
8083 But there are also valid reasons why a segment can be empty.
8084 So allow a count of zero. */
8086 /* Add the current segment to the list of built segments. */
8087 *pointer_to_map
= map
;
8088 pointer_to_map
= &map
->next
;
8090 if (isec
< section_count
)
8092 /* We still have not allocated all of the sections to
8093 segments. Create a new segment here, initialise it
8094 and carry on looping. */
8095 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
8096 amt
+= section_count
* sizeof (asection
*);
8097 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
8104 /* Initialise the fields of the segment map. Set the physical
8105 physical address to the LMA of the first section that has
8106 not yet been assigned. */
8108 map
->p_type
= segment
->p_type
;
8109 map
->p_flags
= segment
->p_flags
;
8110 map
->p_flags_valid
= 1;
8111 map
->p_paddr
= suggested_lma
->lma
* opb
;
8112 map
->p_paddr_valid
= p_paddr_valid
;
8113 map
->includes_filehdr
= 0;
8114 map
->includes_phdrs
= 0;
8119 bfd_set_error (bfd_error_sorry
);
8123 while (isec
< section_count
);
8128 elf_seg_map (obfd
) = map_first
;
8130 /* If we had to estimate the number of program headers that were
8131 going to be needed, then check our estimate now and adjust
8132 the offset if necessary. */
8133 if (phdr_adjust_seg
!= NULL
)
8137 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
8140 if (count
> phdr_adjust_num
)
8141 phdr_adjust_seg
->p_paddr
8142 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
8144 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
8145 if (map
->p_type
== PT_PHDR
)
8148 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
8149 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
8154 #undef IS_SOLARIS_PT_INTERP
8155 #undef IS_SECTION_IN_INPUT_SEGMENT
8156 #undef INCLUDE_SECTION_IN_SEGMENT
8157 #undef SEGMENT_AFTER_SEGMENT
8158 #undef SEGMENT_OVERLAPS
8162 /* Return true if p_align in the ELF program header in ABFD is valid. */
8165 elf_is_p_align_valid (bfd
*abfd
)
8168 Elf_Internal_Phdr
*segment
;
8169 unsigned int num_segments
;
8170 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8171 bfd_size_type maxpagesize
= bed
->maxpagesize
;
8172 bfd_size_type p_align
= bed
->p_align
;
8174 /* Return true if the default p_align value isn't set or the maximum
8175 page size is the same as the minimum page size. */
8176 if (p_align
== 0 || maxpagesize
== bed
->minpagesize
)
8179 /* When the default p_align value is set, p_align may be set to the
8180 default p_align value while segments are aligned to the maximum
8181 page size. In this case, the input p_align will be ignored and
8182 the maximum page size will be used to align the output segments. */
8183 segment
= elf_tdata (abfd
)->phdr
;
8184 num_segments
= elf_elfheader (abfd
)->e_phnum
;
8185 for (i
= 0; i
< num_segments
; i
++, segment
++)
8186 if (segment
->p_type
== PT_LOAD
8187 && (segment
->p_align
!= p_align
8188 || vma_page_aligned_bias (segment
->p_vaddr
,
8196 /* Copy ELF program header information. */
8199 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
8201 Elf_Internal_Ehdr
*iehdr
;
8202 struct elf_segment_map
*map
;
8203 struct elf_segment_map
*map_first
;
8204 struct elf_segment_map
**pointer_to_map
;
8205 Elf_Internal_Phdr
*segment
;
8207 unsigned int num_segments
;
8208 bool phdr_included
= false;
8210 bool p_palign_valid
;
8211 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
8213 iehdr
= elf_elfheader (ibfd
);
8216 pointer_to_map
= &map_first
;
8218 /* If all the segment p_paddr fields are zero, don't set
8219 map->p_paddr_valid. */
8220 p_paddr_valid
= false;
8221 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8222 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8225 if (segment
->p_paddr
!= 0)
8227 p_paddr_valid
= true;
8231 p_palign_valid
= elf_is_p_align_valid (ibfd
);
8233 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8238 unsigned int section_count
;
8240 Elf_Internal_Shdr
*this_hdr
;
8241 asection
*first_section
= NULL
;
8242 asection
*lowest_section
;
8244 /* Compute how many sections are in this segment. */
8245 for (section
= ibfd
->sections
, section_count
= 0;
8247 section
= section
->next
)
8249 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8250 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8252 if (first_section
== NULL
)
8253 first_section
= section
;
8258 /* Allocate a segment map big enough to contain
8259 all of the sections we have selected. */
8260 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
8261 amt
+= section_count
* sizeof (asection
*);
8262 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
8266 /* Initialize the fields of the output segment map with the
8269 map
->p_type
= segment
->p_type
;
8270 map
->p_flags
= segment
->p_flags
;
8271 map
->p_flags_valid
= 1;
8272 map
->p_paddr
= segment
->p_paddr
;
8273 map
->p_paddr_valid
= p_paddr_valid
;
8274 map
->p_align
= segment
->p_align
;
8275 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8276 map
->p_align_valid
= (map
->p_type
== PT_GNU_STACK
8278 map
->p_vaddr_offset
= 0;
8280 if (map
->p_type
== PT_GNU_RELRO
8281 || map
->p_type
== PT_GNU_STACK
)
8283 /* The PT_GNU_RELRO segment may contain the first a few
8284 bytes in the .got.plt section even if the whole .got.plt
8285 section isn't in the PT_GNU_RELRO segment. We won't
8286 change the size of the PT_GNU_RELRO segment.
8287 Similarly, PT_GNU_STACK size is significant on uclinux
8289 map
->p_size
= segment
->p_memsz
;
8290 map
->p_size_valid
= 1;
8293 /* Determine if this segment contains the ELF file header
8294 and if it contains the program headers themselves. */
8295 map
->includes_filehdr
= (segment
->p_offset
== 0
8296 && segment
->p_filesz
>= iehdr
->e_ehsize
);
8298 map
->includes_phdrs
= 0;
8299 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
8301 map
->includes_phdrs
=
8302 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
8303 && (segment
->p_offset
+ segment
->p_filesz
8304 >= ((bfd_vma
) iehdr
->e_phoff
8305 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
8307 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
8308 phdr_included
= true;
8311 lowest_section
= NULL
;
8312 if (section_count
!= 0)
8314 unsigned int isec
= 0;
8316 for (section
= first_section
;
8318 section
= section
->next
)
8320 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8321 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8323 map
->sections
[isec
++] = section
->output_section
;
8324 if ((section
->flags
& SEC_ALLOC
) != 0)
8328 if (lowest_section
== NULL
8329 || section
->lma
< lowest_section
->lma
)
8330 lowest_section
= section
;
8332 /* Section lmas are set up from PT_LOAD header
8333 p_paddr in _bfd_elf_make_section_from_shdr.
8334 If this header has a p_paddr that disagrees
8335 with the section lma, flag the p_paddr as
8337 if ((section
->flags
& SEC_LOAD
) != 0)
8338 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
8340 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
8341 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
8342 map
->p_paddr_valid
= false;
8344 if (isec
== section_count
)
8350 if (section_count
== 0)
8351 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
8352 else if (map
->p_paddr_valid
)
8354 /* Account for padding before the first section in the segment. */
8355 bfd_vma hdr_size
= 0;
8356 if (map
->includes_filehdr
)
8357 hdr_size
= iehdr
->e_ehsize
;
8358 if (map
->includes_phdrs
)
8359 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
8361 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
8362 - (lowest_section
? lowest_section
->lma
: 0));
8365 map
->count
= section_count
;
8366 *pointer_to_map
= map
;
8367 pointer_to_map
= &map
->next
;
8370 elf_seg_map (obfd
) = map_first
;
8374 /* Copy private BFD data. This copies or rewrites ELF program header
8378 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
8380 bfd_vma maxpagesize
;
8382 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8383 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8386 if (elf_tdata (ibfd
)->phdr
== NULL
)
8389 if (ibfd
->xvec
== obfd
->xvec
)
8391 /* Check to see if any sections in the input BFD
8392 covered by ELF program header have changed. */
8393 Elf_Internal_Phdr
*segment
;
8397 unsigned int i
, num_segments
;
8398 Elf_Internal_Shdr
*this_hdr
;
8399 const struct elf_backend_data
*bed
;
8401 bed
= get_elf_backend_data (ibfd
);
8403 /* Regenerate the segment map if p_paddr is set to 0. */
8404 if (bed
->want_p_paddr_set_to_zero
)
8407 /* Initialize the segment mark field. */
8408 for (section
= obfd
->sections
; section
!= NULL
;
8409 section
= section
->next
)
8410 section
->segment_mark
= false;
8412 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8413 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8417 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8418 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8419 which severly confuses things, so always regenerate the segment
8420 map in this case. */
8421 if (segment
->p_paddr
== 0
8422 && segment
->p_memsz
== 0
8423 && (segment
->p_type
== PT_INTERP
8424 || segment
->p_type
== PT_DYNAMIC
))
8427 for (section
= ibfd
->sections
, prev
= NULL
;
8428 section
!= NULL
; section
= section
->next
)
8430 /* We mark the output section so that we know it comes
8431 from the input BFD. */
8432 osec
= section
->output_section
;
8434 osec
->segment_mark
= true;
8436 /* Check if this section is covered by the segment. */
8437 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8438 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8440 /* FIXME: Check if its output section is changed or
8441 removed. What else do we need to check? */
8443 || section
->flags
!= osec
->flags
8444 || section
->lma
!= osec
->lma
8445 || section
->vma
!= osec
->vma
8446 || section
->size
!= osec
->size
8447 || section
->rawsize
!= osec
->rawsize
8448 || section
->alignment_power
!= osec
->alignment_power
)
8451 /* PR 31450: If this is an allocated section then make sure
8452 that this section's vma to lma relationship is the same
8453 as previous (allocated) section's. */
8455 && section
->flags
& SEC_ALLOC
8456 && section
->lma
- section
->vma
!= prev
->lma
- prev
->vma
)
8459 if (section
->flags
& SEC_ALLOC
)
8465 /* Check to see if any output section do not come from the
8467 for (section
= obfd
->sections
; section
!= NULL
;
8468 section
= section
->next
)
8470 if (!section
->segment_mark
)
8473 section
->segment_mark
= false;
8476 return copy_elf_program_header (ibfd
, obfd
);
8481 if (ibfd
->xvec
== obfd
->xvec
)
8483 /* When rewriting program header, set the output maxpagesize to
8484 the maximum alignment of input PT_LOAD segments. */
8485 Elf_Internal_Phdr
*segment
;
8487 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8489 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8492 if (segment
->p_type
== PT_LOAD
8493 && maxpagesize
< segment
->p_align
)
8495 /* PR 17512: file: f17299af. */
8496 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
8497 /* xgettext:c-format */
8498 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8499 PRIx64
" is too large"),
8500 ibfd
, (uint64_t) segment
->p_align
);
8502 maxpagesize
= segment
->p_align
;
8505 if (maxpagesize
== 0)
8506 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
8508 return rewrite_elf_program_header (ibfd
, obfd
, maxpagesize
);
8511 /* Initialize private output section information from input section. */
8514 _bfd_elf_init_private_section_data (bfd
*ibfd
,
8518 struct bfd_link_info
*link_info
)
8521 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8522 bool final_link
= (link_info
!= NULL
8523 && !bfd_link_relocatable (link_info
));
8525 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8526 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8529 BFD_ASSERT (elf_section_data (osec
) != NULL
);
8531 /* If this is a known ABI section, ELF section type and flags may
8532 have been set up when OSEC was created. For normal sections we
8533 allow the user to override the type and flags other than
8534 SHF_MASKOS and SHF_MASKPROC. */
8535 if (elf_section_type (osec
) == SHT_PROGBITS
8536 || elf_section_type (osec
) == SHT_NOTE
8537 || elf_section_type (osec
) == SHT_NOBITS
)
8538 elf_section_type (osec
) = SHT_NULL
;
8539 /* For objcopy and relocatable link, copy the ELF section type from
8540 the input file if the BFD section flags are the same. (If they
8541 are different the user may be doing something like
8542 "objcopy --set-section-flags .text=alloc,data".) For a final
8543 link allow some flags that the linker clears to differ. */
8544 if (elf_section_type (osec
) == SHT_NULL
8545 && (osec
->flags
== isec
->flags
8547 && ((osec
->flags
^ isec
->flags
)
8548 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
8549 elf_section_type (osec
) = elf_section_type (isec
);
8551 /* FIXME: Is this correct for all OS/PROC specific flags? */
8552 elf_section_flags (osec
) = (elf_section_flags (isec
)
8553 & (SHF_MASKOS
| SHF_MASKPROC
));
8555 /* Copy sh_info from input for mbind section. */
8556 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
8557 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
8558 elf_section_data (osec
)->this_hdr
.sh_info
8559 = elf_section_data (isec
)->this_hdr
.sh_info
;
8561 /* Set things up for objcopy and relocatable link. The output
8562 SHT_GROUP section will have its elf_next_in_group pointing back
8563 to the input group members. Ignore linker created group section.
8564 See elfNN_ia64_object_p in elfxx-ia64.c. */
8565 if ((link_info
== NULL
8566 || !link_info
->resolve_section_groups
)
8567 && (elf_sec_group (isec
) == NULL
8568 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
8570 if (elf_section_flags (isec
) & SHF_GROUP
)
8571 elf_section_flags (osec
) |= SHF_GROUP
;
8572 elf_next_in_group (osec
) = elf_next_in_group (isec
);
8573 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
8576 /* If not decompress, preserve SHF_COMPRESSED. */
8577 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
8578 elf_section_flags (osec
) |= (elf_section_flags (isec
)
8581 ihdr
= &elf_section_data (isec
)->this_hdr
;
8583 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8584 don't use the output section of the linked-to section since it
8585 may be NULL at this point. */
8586 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
8588 ohdr
= &elf_section_data (osec
)->this_hdr
;
8589 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
8590 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
8593 osec
->use_rela_p
= isec
->use_rela_p
;
8598 /* Copy private section information. This copies over the entsize
8599 field, and sometimes the info field. */
8602 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
8607 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8609 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8610 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8613 ihdr
= &elf_section_data (isec
)->this_hdr
;
8614 ohdr
= &elf_section_data (osec
)->this_hdr
;
8616 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
8618 if (ihdr
->sh_type
== SHT_SYMTAB
8619 || ihdr
->sh_type
== SHT_DYNSYM
8620 || ihdr
->sh_type
== SHT_GNU_verneed
8621 || ihdr
->sh_type
== SHT_GNU_verdef
)
8622 ohdr
->sh_info
= ihdr
->sh_info
;
8624 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
8628 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8629 necessary if we are removing either the SHT_GROUP section or any of
8630 the group member sections. DISCARDED is the value that a section's
8631 output_section has if the section will be discarded, NULL when this
8632 function is called from objcopy, bfd_abs_section_ptr when called
8636 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
8640 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
8641 if (elf_section_type (isec
) == SHT_GROUP
)
8643 asection
*first
= elf_next_in_group (isec
);
8644 asection
*s
= first
;
8645 bfd_size_type removed
= 0;
8649 /* If this member section is being output but the
8650 SHT_GROUP section is not, then clear the group info
8651 set up by _bfd_elf_copy_private_section_data. */
8652 if (s
->output_section
!= discarded
8653 && isec
->output_section
== discarded
)
8655 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
8656 elf_group_name (s
->output_section
) = NULL
;
8660 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
8661 if (s
->output_section
== discarded
8662 && isec
->output_section
!= discarded
)
8664 /* Conversely, if the member section is not being
8665 output but the SHT_GROUP section is, then adjust
8668 if (elf_sec
->rel
.hdr
!= NULL
8669 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8671 if (elf_sec
->rela
.hdr
!= NULL
8672 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8677 /* Also adjust for zero-sized relocation member
8679 if (elf_sec
->rel
.hdr
!= NULL
8680 && elf_sec
->rel
.hdr
->sh_size
== 0)
8682 if (elf_sec
->rela
.hdr
!= NULL
8683 && elf_sec
->rela
.hdr
->sh_size
== 0)
8687 s
= elf_next_in_group (s
);
8693 if (discarded
!= NULL
)
8695 /* If we've been called for ld -r, then we need to
8696 adjust the input section size. */
8697 if (isec
->rawsize
== 0)
8698 isec
->rawsize
= isec
->size
;
8699 isec
->size
= isec
->rawsize
- removed
;
8700 if (isec
->size
<= 4)
8703 isec
->flags
|= SEC_EXCLUDE
;
8706 else if (isec
->output_section
!= NULL
)
8708 /* Adjust the output section size when called from
8710 isec
->output_section
->size
-= removed
;
8711 if (isec
->output_section
->size
<= 4)
8713 isec
->output_section
->size
= 0;
8714 isec
->output_section
->flags
|= SEC_EXCLUDE
;
8723 /* Copy private header information. */
8726 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
8728 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8729 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8732 /* Copy over private BFD data if it has not already been copied.
8733 This must be done here, rather than in the copy_private_bfd_data
8734 entry point, because the latter is called after the section
8735 contents have been set, which means that the program headers have
8736 already been worked out. */
8737 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
8739 if (! copy_private_bfd_data (ibfd
, obfd
))
8743 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8746 /* Copy private symbol information. If this symbol is in a section
8747 which we did not map into a BFD section, try to map the section
8748 index correctly. We use special macro definitions for the mapped
8749 section indices; these definitions are interpreted by the
8750 swap_out_syms function. */
8752 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8753 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8754 #define MAP_STRTAB (SHN_HIOS + 3)
8755 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8756 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8759 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8764 elf_symbol_type
*isym
, *osym
;
8766 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8767 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8770 isym
= elf_symbol_from (isymarg
);
8771 osym
= elf_symbol_from (osymarg
);
8774 && isym
->internal_elf_sym
.st_shndx
!= 0
8776 && bfd_is_abs_section (isym
->symbol
.section
))
8780 shndx
= isym
->internal_elf_sym
.st_shndx
;
8781 if (shndx
== elf_onesymtab (ibfd
))
8782 shndx
= MAP_ONESYMTAB
;
8783 else if (shndx
== elf_dynsymtab (ibfd
))
8784 shndx
= MAP_DYNSYMTAB
;
8785 else if (shndx
== elf_elfsections (ibfd
)[elf_onesymtab (ibfd
)]->sh_link
)
8787 else if (shndx
== elf_elfheader (ibfd
)->e_shstrndx
)
8788 shndx
= MAP_SHSTRTAB
;
8789 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8790 shndx
= MAP_SYM_SHNDX
;
8791 osym
->internal_elf_sym
.st_shndx
= shndx
;
8797 /* Swap out the symbols. */
8800 swap_out_syms (bfd
*abfd
,
8801 struct elf_strtab_hash
**sttp
,
8803 struct bfd_link_info
*info
)
8805 const struct elf_backend_data
*bed
;
8806 unsigned int symcount
;
8808 struct elf_strtab_hash
*stt
;
8809 Elf_Internal_Shdr
*symtab_hdr
;
8810 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8811 Elf_Internal_Shdr
*symstrtab_hdr
;
8812 struct elf_sym_strtab
*symstrtab
;
8813 bfd_byte
*outbound_syms
;
8814 bfd_byte
*outbound_shndx
;
8815 unsigned long outbound_syms_index
;
8817 unsigned int num_locals
;
8819 bool name_local_sections
;
8821 if (!elf_map_symbols (abfd
, &num_locals
))
8824 /* Dump out the symtabs. */
8825 stt
= _bfd_elf_strtab_init ();
8829 bed
= get_elf_backend_data (abfd
);
8830 symcount
= bfd_get_symcount (abfd
);
8831 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8832 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8833 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8834 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8835 symtab_hdr
->sh_info
= num_locals
+ 1;
8836 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8838 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8839 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8841 /* Allocate buffer to swap out the .strtab section. */
8842 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8843 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8845 bfd_set_error (bfd_error_no_memory
);
8846 _bfd_elf_strtab_free (stt
);
8850 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8851 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8854 bfd_set_error (bfd_error_no_memory
);
8857 _bfd_elf_strtab_free (stt
);
8860 symtab_hdr
->contents
= outbound_syms
;
8861 outbound_syms_index
= 0;
8863 outbound_shndx
= NULL
;
8865 if (elf_symtab_shndx_list (abfd
))
8867 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8868 if (symtab_shndx_hdr
->sh_name
!= 0)
8870 if (_bfd_mul_overflow (symcount
+ 1,
8871 sizeof (Elf_External_Sym_Shndx
), &amt
))
8873 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8874 if (outbound_shndx
== NULL
)
8877 symtab_shndx_hdr
->contents
= outbound_shndx
;
8878 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8879 symtab_shndx_hdr
->sh_size
= amt
;
8880 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8881 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8883 /* FIXME: What about any other headers in the list ? */
8886 /* Now generate the data (for "contents"). */
8888 /* Fill in zeroth symbol and swap it out. */
8889 Elf_Internal_Sym sym
;
8895 sym
.st_shndx
= SHN_UNDEF
;
8896 sym
.st_target_internal
= 0;
8897 symstrtab
[outbound_syms_index
].sym
= sym
;
8898 symstrtab
[outbound_syms_index
].dest_index
= outbound_syms_index
;
8899 outbound_syms_index
++;
8903 = (bed
->elf_backend_name_local_section_symbols
8904 && bed
->elf_backend_name_local_section_symbols (abfd
));
8906 syms
= bfd_get_outsymbols (abfd
);
8907 for (idx
= 0; idx
< symcount
; idx
++)
8909 Elf_Internal_Sym sym
;
8911 flagword flags
= syms
[idx
]->flags
;
8912 if (!name_local_sections
8913 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8915 /* Local section symbols have no name. */
8916 sym
.st_name
= (unsigned long) -1;
8920 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8921 to get the final offset for st_name. */
8923 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8925 if (sym
.st_name
== (unsigned long) -1)
8929 bfd_vma value
= syms
[idx
]->value
;
8930 elf_symbol_type
*type_ptr
= elf_symbol_from (syms
[idx
]);
8931 asection
*sec
= syms
[idx
]->section
;
8933 if ((flags
& BSF_SECTION_SYM
) == 0 && bfd_is_com_section (sec
))
8935 /* ELF common symbols put the alignment into the `value' field,
8936 and the size into the `size' field. This is backwards from
8937 how BFD handles it, so reverse it here. */
8938 sym
.st_size
= value
;
8939 if (type_ptr
== NULL
8940 || type_ptr
->internal_elf_sym
.st_value
== 0)
8941 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8943 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8944 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8950 if (sec
->output_section
)
8952 value
+= sec
->output_offset
;
8953 sec
= sec
->output_section
;
8956 /* Don't add in the section vma for relocatable output. */
8957 if (! relocatable_p
)
8959 sym
.st_value
= value
;
8960 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8962 if (bfd_is_abs_section (sec
)
8964 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8966 /* This symbol is in a real ELF section which we did
8967 not create as a BFD section. Undo the mapping done
8968 by copy_private_symbol_data. */
8969 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8973 shndx
= elf_onesymtab (abfd
);
8976 shndx
= elf_dynsymtab (abfd
);
8979 shndx
= elf_strtab_sec (abfd
);
8982 shndx
= elf_shstrtab_sec (abfd
);
8985 if (elf_symtab_shndx_list (abfd
))
8986 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8993 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8995 if (bed
->symbol_section_index
)
8996 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8997 /* Otherwise just leave the index alone. */
9001 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
9002 _bfd_error_handler (_("%pB: \
9003 Unable to handle section index %x in ELF symbol. Using ABS instead."),
9012 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
9014 if (shndx
== SHN_BAD
)
9018 /* Writing this would be a hell of a lot easier if
9019 we had some decent documentation on bfd, and
9020 knew what to expect of the library, and what to
9021 demand of applications. For example, it
9022 appears that `objcopy' might not set the
9023 section of a symbol to be a section that is
9024 actually in the output file. */
9025 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
9027 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
9028 if (shndx
== SHN_BAD
)
9030 /* xgettext:c-format */
9032 (_("unable to find equivalent output section"
9033 " for symbol '%s' from section '%s'"),
9034 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
9036 bfd_set_error (bfd_error_invalid_operation
);
9042 sym
.st_shndx
= shndx
;
9046 if ((flags
& BSF_THREAD_LOCAL
) != 0)
9048 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
9049 type
= STT_GNU_IFUNC
;
9050 else if ((flags
& BSF_FUNCTION
) != 0)
9052 else if ((flags
& BSF_OBJECT
) != 0)
9054 else if ((flags
& BSF_RELC
) != 0)
9056 else if ((flags
& BSF_SRELC
) != 0)
9061 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
9064 /* Processor-specific types. */
9065 if (type_ptr
!= NULL
9066 && bed
->elf_backend_get_symbol_type
)
9067 type
= ((*bed
->elf_backend_get_symbol_type
)
9068 (&type_ptr
->internal_elf_sym
, type
));
9070 if (flags
& BSF_SECTION_SYM
)
9072 if (flags
& BSF_GLOBAL
)
9073 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
9075 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9077 else if (bfd_is_com_section (syms
[idx
]->section
))
9079 if (type
!= STT_TLS
)
9081 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
9082 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
9083 ? STT_COMMON
: STT_OBJECT
);
9085 type
= ((flags
& BSF_ELF_COMMON
) != 0
9086 ? STT_COMMON
: STT_OBJECT
);
9088 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9090 else if (bfd_is_und_section (syms
[idx
]->section
))
9091 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
9095 else if (flags
& BSF_FILE
)
9096 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9099 int bind
= STB_LOCAL
;
9101 if (flags
& BSF_LOCAL
)
9103 else if (flags
& BSF_GNU_UNIQUE
)
9104 bind
= STB_GNU_UNIQUE
;
9105 else if (flags
& BSF_WEAK
)
9107 else if (flags
& BSF_GLOBAL
)
9110 sym
.st_info
= ELF_ST_INFO (bind
, type
);
9113 if (type_ptr
!= NULL
)
9115 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
9116 sym
.st_target_internal
9117 = type_ptr
->internal_elf_sym
.st_target_internal
;
9122 sym
.st_target_internal
= 0;
9125 symstrtab
[outbound_syms_index
].sym
= sym
;
9126 symstrtab
[outbound_syms_index
].dest_index
= outbound_syms_index
;
9127 outbound_syms_index
++;
9130 /* Finalize the .strtab section. */
9131 _bfd_elf_strtab_finalize (stt
);
9133 /* Swap out the .strtab section. */
9134 for (idx
= 0; idx
< outbound_syms_index
; idx
++)
9136 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
9137 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9138 elfsym
->sym
.st_name
= 0;
9140 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
9141 elfsym
->sym
.st_name
);
9142 if (info
&& info
->callbacks
->ctf_new_symbol
)
9143 info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
9146 /* Inform the linker of the addition of this symbol. */
9148 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
9150 + (elfsym
->dest_index
9151 * bed
->s
->sizeof_sym
)),
9152 NPTR_ADD (outbound_shndx
,
9154 * sizeof (Elf_External_Sym_Shndx
))));
9159 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
9160 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9161 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
9162 symstrtab_hdr
->sh_addr
= 0;
9163 symstrtab_hdr
->sh_entsize
= 0;
9164 symstrtab_hdr
->sh_link
= 0;
9165 symstrtab_hdr
->sh_info
= 0;
9166 symstrtab_hdr
->sh_addralign
= 1;
9171 /* Return the number of bytes required to hold the symtab vector.
9173 Note that we base it on the count plus 1, since we will null terminate
9174 the vector allocated based on this size. However, the ELF symbol table
9175 always has a dummy entry as symbol #0, so it ends up even. */
9178 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
9180 bfd_size_type symcount
;
9182 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9184 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
9185 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
9187 bfd_set_error (bfd_error_file_too_big
);
9190 symtab_size
= symcount
* (sizeof (asymbol
*));
9192 symtab_size
= sizeof (asymbol
*);
9193 else if (!bfd_write_p (abfd
))
9195 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9197 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
9199 bfd_set_error (bfd_error_file_truncated
);
9208 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
9210 bfd_size_type symcount
;
9212 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
9214 if (elf_dynsymtab (abfd
) == 0)
9216 /* Check if there is dynamic symbol table. */
9217 symcount
= elf_tdata (abfd
)->dt_symtab_count
;
9219 goto compute_symtab_size
;
9221 bfd_set_error (bfd_error_invalid_operation
);
9225 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
9226 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
9228 bfd_set_error (bfd_error_file_too_big
);
9232 compute_symtab_size
:
9233 symtab_size
= symcount
* (sizeof (asymbol
*));
9235 symtab_size
= sizeof (asymbol
*);
9236 else if (!bfd_write_p (abfd
))
9238 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9240 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
9242 bfd_set_error (bfd_error_file_truncated
);
9251 _bfd_elf_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
9253 if (asect
->reloc_count
!= 0 && !bfd_write_p (abfd
))
9255 /* Sanity check reloc section size. */
9256 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9260 struct bfd_elf_section_data
*d
= elf_section_data (asect
);
9261 bfd_size_type rel_size
= d
->rel
.hdr
? d
->rel
.hdr
->sh_size
: 0;
9262 bfd_size_type rela_size
= d
->rela
.hdr
? d
->rela
.hdr
->sh_size
: 0;
9264 if (rel_size
+ rela_size
> filesize
9265 || rel_size
+ rela_size
< rel_size
)
9267 bfd_set_error (bfd_error_file_truncated
);
9273 #if SIZEOF_LONG == SIZEOF_INT
9274 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
9276 bfd_set_error (bfd_error_file_too_big
);
9280 return (asect
->reloc_count
+ 1L) * sizeof (arelent
*);
9283 /* Canonicalize the relocs. */
9286 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
9293 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9295 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
9298 tblptr
= section
->relocation
;
9299 for (i
= 0; i
< section
->reloc_count
; i
++)
9300 *relptr
++ = tblptr
++;
9304 return section
->reloc_count
;
9308 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
9310 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9311 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, false);
9314 abfd
->symcount
= symcount
;
9319 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
9320 asymbol
**allocation
)
9322 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9323 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, true);
9326 abfd
->dynsymcount
= symcount
;
9330 /* Return the size required for the dynamic reloc entries. Any loadable
9331 section that was actually installed in the BFD, and has type SHT_REL
9332 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9333 dynamic reloc section. */
9336 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
9338 bfd_size_type count
, ext_rel_size
;
9341 if (elf_dynsymtab (abfd
) == 0)
9343 bfd_set_error (bfd_error_invalid_operation
);
9349 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9350 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
9351 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
9352 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
)
9353 && (elf_section_data (s
)->this_hdr
.sh_flags
& SHF_COMPRESSED
) == 0)
9355 ext_rel_size
+= elf_section_data (s
)->this_hdr
.sh_size
;
9356 if (ext_rel_size
< elf_section_data (s
)->this_hdr
.sh_size
)
9358 bfd_set_error (bfd_error_file_truncated
);
9361 count
+= NUM_SHDR_ENTRIES (&elf_section_data (s
)->this_hdr
);
9362 if (count
> LONG_MAX
/ sizeof (arelent
*))
9364 bfd_set_error (bfd_error_file_too_big
);
9368 if (count
> 1 && !bfd_write_p (abfd
))
9370 /* Sanity check reloc section sizes. */
9371 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9372 if (filesize
!= 0 && ext_rel_size
> filesize
)
9374 bfd_set_error (bfd_error_file_truncated
);
9378 return count
* sizeof (arelent
*);
9381 /* Canonicalize the dynamic relocation entries. Note that we return the
9382 dynamic relocations as a single block, although they are actually
9383 associated with particular sections; the interface, which was
9384 designed for SunOS style shared libraries, expects that there is only
9385 one set of dynamic relocs. Any loadable section that was actually
9386 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9387 dynamic symbol table, is considered to be a dynamic reloc section. */
9390 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
9394 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
9398 if (elf_dynsymtab (abfd
) == 0)
9400 bfd_set_error (bfd_error_invalid_operation
);
9404 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9406 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9408 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
9409 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
9410 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
)
9411 && (elf_section_data (s
)->this_hdr
.sh_flags
& SHF_COMPRESSED
) == 0)
9416 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
9418 count
= NUM_SHDR_ENTRIES (&elf_section_data (s
)->this_hdr
);
9420 for (i
= 0; i
< count
; i
++)
9431 /* Read in the version information. */
9434 _bfd_elf_slurp_version_tables (bfd
*abfd
, bool default_imported_symver
)
9436 bfd_byte
*contents
= NULL
;
9437 unsigned int freeidx
= 0;
9439 void *contents_addr
= NULL
;
9440 size_t contents_size
= 0;
9442 if (elf_dynverref (abfd
) != 0 || elf_tdata (abfd
)->dt_verneed
!= NULL
)
9444 Elf_Internal_Shdr
*hdr
;
9445 Elf_External_Verneed
*everneed
;
9446 Elf_Internal_Verneed
*iverneed
;
9448 bfd_byte
*contents_end
;
9449 size_t verneed_count
;
9450 size_t verneed_size
;
9452 if (elf_tdata (abfd
)->dt_verneed
!= NULL
)
9455 contents
= elf_tdata (abfd
)->dt_verneed
;
9456 verneed_count
= elf_tdata (abfd
)->dt_verneed_count
;
9457 verneed_size
= verneed_count
* sizeof (Elf_External_Verneed
);
9461 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
9463 if (hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
9465 error_return_bad_verref
:
9467 (_("%pB: .gnu.version_r invalid entry"), abfd
);
9468 bfd_set_error (bfd_error_bad_value
);
9469 error_return_verref
:
9470 elf_tdata (abfd
)->verref
= NULL
;
9471 elf_tdata (abfd
)->cverrefs
= 0;
9475 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9476 goto error_return_verref
;
9477 contents_size
= hdr
->sh_size
;
9478 contents
= _bfd_mmap_readonly_temporary (abfd
, contents_size
,
9481 if (contents
== NULL
)
9482 goto error_return_verref
;
9484 verneed_size
= hdr
->sh_size
;
9485 verneed_count
= hdr
->sh_info
;
9488 if (_bfd_mul_overflow (verneed_count
,
9489 sizeof (Elf_Internal_Verneed
), &amt
))
9491 bfd_set_error (bfd_error_file_too_big
);
9492 goto error_return_verref
;
9495 goto error_return_verref
;
9496 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
9497 if (elf_tdata (abfd
)->verref
== NULL
)
9498 goto error_return_verref
;
9500 BFD_ASSERT (sizeof (Elf_External_Verneed
)
9501 == sizeof (Elf_External_Vernaux
));
9502 contents_end
= (contents
+ verneed_size
9503 - sizeof (Elf_External_Verneed
));
9504 everneed
= (Elf_External_Verneed
*) contents
;
9505 iverneed
= elf_tdata (abfd
)->verref
;
9506 for (i
= 0; i
< verneed_count
; i
++, iverneed
++)
9508 Elf_External_Vernaux
*evernaux
;
9509 Elf_Internal_Vernaux
*ivernaux
;
9512 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
9514 iverneed
->vn_bfd
= abfd
;
9516 if (elf_use_dt_symtab_p (abfd
))
9518 if (iverneed
->vn_file
< elf_tdata (abfd
)->dt_strsz
)
9519 iverneed
->vn_filename
9520 = elf_tdata (abfd
)->dt_strtab
+ iverneed
->vn_file
;
9522 iverneed
->vn_filename
= NULL
;
9524 else if (hdr
== NULL
)
9525 goto error_return_bad_verref
;
9527 iverneed
->vn_filename
9528 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9530 if (iverneed
->vn_filename
== NULL
)
9531 goto error_return_bad_verref
;
9533 if (iverneed
->vn_cnt
== 0)
9534 iverneed
->vn_auxptr
= NULL
;
9537 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
9538 sizeof (Elf_Internal_Vernaux
), &amt
))
9540 bfd_set_error (bfd_error_file_too_big
);
9541 goto error_return_verref
;
9543 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
9544 bfd_alloc (abfd
, amt
);
9545 if (iverneed
->vn_auxptr
== NULL
)
9546 goto error_return_verref
;
9549 if (iverneed
->vn_aux
9550 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9551 goto error_return_bad_verref
;
9553 evernaux
= ((Elf_External_Vernaux
*)
9554 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
9555 ivernaux
= iverneed
->vn_auxptr
;
9556 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
9558 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
9560 if (elf_use_dt_symtab_p (abfd
))
9562 if (ivernaux
->vna_name
< elf_tdata (abfd
)->dt_strsz
)
9563 ivernaux
->vna_nodename
9564 = elf_tdata (abfd
)->dt_strtab
+ ivernaux
->vna_name
;
9566 ivernaux
->vna_nodename
= NULL
;
9568 else if (hdr
== NULL
)
9569 goto error_return_bad_verref
;
9571 ivernaux
->vna_nodename
9572 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9573 ivernaux
->vna_name
);
9574 if (ivernaux
->vna_nodename
== NULL
)
9575 goto error_return_bad_verref
;
9577 if (ivernaux
->vna_other
> freeidx
)
9578 freeidx
= ivernaux
->vna_other
;
9580 ivernaux
->vna_nextptr
= NULL
;
9581 if (ivernaux
->vna_next
== 0)
9583 iverneed
->vn_cnt
= j
+ 1;
9586 if (j
+ 1 < iverneed
->vn_cnt
)
9587 ivernaux
->vna_nextptr
= ivernaux
+ 1;
9589 if (ivernaux
->vna_next
9590 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
9591 goto error_return_bad_verref
;
9593 evernaux
= ((Elf_External_Vernaux
*)
9594 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
9597 iverneed
->vn_nextref
= NULL
;
9598 if (iverneed
->vn_next
== 0)
9600 if (hdr
!= NULL
&& (i
+ 1 < hdr
->sh_info
))
9601 iverneed
->vn_nextref
= iverneed
+ 1;
9603 if (iverneed
->vn_next
9604 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9605 goto error_return_bad_verref
;
9607 everneed
= ((Elf_External_Verneed
*)
9608 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
9610 elf_tdata (abfd
)->cverrefs
= i
;
9612 if (contents
!= elf_tdata (abfd
)->dt_verneed
)
9613 _bfd_munmap_readonly_temporary (contents_addr
, contents_size
);
9615 contents_addr
= NULL
;
9618 if (elf_dynverdef (abfd
) != 0 || elf_tdata (abfd
)->dt_verdef
!= NULL
)
9620 Elf_Internal_Shdr
*hdr
;
9621 Elf_External_Verdef
*everdef
;
9622 Elf_Internal_Verdef
*iverdef
;
9623 Elf_Internal_Verdef
*iverdefarr
;
9624 Elf_Internal_Verdef iverdefmem
;
9626 unsigned int maxidx
;
9627 bfd_byte
*contents_end_def
, *contents_end_aux
;
9628 size_t verdef_count
;
9631 if (elf_tdata (abfd
)->dt_verdef
!= NULL
)
9634 contents
= elf_tdata (abfd
)->dt_verdef
;
9635 verdef_count
= elf_tdata (abfd
)->dt_verdef_count
;
9636 verdef_size
= verdef_count
* sizeof (Elf_External_Verdef
);
9640 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
9642 if (hdr
->sh_size
< sizeof (Elf_External_Verdef
))
9644 error_return_bad_verdef
:
9646 (_("%pB: .gnu.version_d invalid entry"), abfd
);
9647 bfd_set_error (bfd_error_bad_value
);
9648 error_return_verdef
:
9649 elf_tdata (abfd
)->verdef
= NULL
;
9650 elf_tdata (abfd
)->cverdefs
= 0;
9654 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9655 goto error_return_verdef
;
9656 contents_size
= hdr
->sh_size
;
9657 contents
= _bfd_mmap_readonly_temporary (abfd
, contents_size
,
9660 if (contents
== NULL
)
9661 goto error_return_verdef
;
9663 BFD_ASSERT (sizeof (Elf_External_Verdef
)
9664 >= sizeof (Elf_External_Verdaux
));
9666 verdef_count
= hdr
->sh_info
;
9667 verdef_size
= hdr
->sh_size
;
9670 contents_end_def
= (contents
+ verdef_size
9671 - sizeof (Elf_External_Verdef
));
9672 contents_end_aux
= (contents
+ verdef_size
9673 - sizeof (Elf_External_Verdaux
));
9675 /* We know the number of entries in the section but not the maximum
9676 index. Therefore we have to run through all entries and find
9678 everdef
= (Elf_External_Verdef
*) contents
;
9680 for (i
= 0; i
< verdef_count
; ++i
)
9682 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9684 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
9685 goto error_return_bad_verdef
;
9686 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
9687 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
9689 if (iverdefmem
.vd_next
== 0)
9692 if (iverdefmem
.vd_next
9693 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
9694 goto error_return_bad_verdef
;
9696 everdef
= ((Elf_External_Verdef
*)
9697 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
9700 if (default_imported_symver
)
9702 if (freeidx
> maxidx
)
9707 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9709 bfd_set_error (bfd_error_file_too_big
);
9710 goto error_return_verdef
;
9714 goto error_return_verdef
;
9715 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9716 if (elf_tdata (abfd
)->verdef
== NULL
)
9717 goto error_return_verdef
;
9719 elf_tdata (abfd
)->cverdefs
= maxidx
;
9721 everdef
= (Elf_External_Verdef
*) contents
;
9722 iverdefarr
= elf_tdata (abfd
)->verdef
;
9723 for (i
= 0; i
< verdef_count
; ++i
)
9725 Elf_External_Verdaux
*everdaux
;
9726 Elf_Internal_Verdaux
*iverdaux
;
9729 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9731 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
9732 goto error_return_bad_verdef
;
9734 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
9735 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
9737 iverdef
->vd_bfd
= abfd
;
9739 if (iverdef
->vd_cnt
== 0)
9740 iverdef
->vd_auxptr
= NULL
;
9743 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
9744 sizeof (Elf_Internal_Verdaux
), &amt
))
9746 bfd_set_error (bfd_error_file_too_big
);
9747 goto error_return_verdef
;
9749 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
9750 bfd_alloc (abfd
, amt
);
9751 if (iverdef
->vd_auxptr
== NULL
)
9752 goto error_return_verdef
;
9756 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
9757 goto error_return_bad_verdef
;
9759 everdaux
= ((Elf_External_Verdaux
*)
9760 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
9761 iverdaux
= iverdef
->vd_auxptr
;
9762 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
9764 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
9766 if (elf_use_dt_symtab_p (abfd
))
9768 if (iverdaux
->vda_name
< elf_tdata (abfd
)->dt_strsz
)
9769 iverdaux
->vda_nodename
9770 = elf_tdata (abfd
)->dt_strtab
+ iverdaux
->vda_name
;
9772 iverdaux
->vda_nodename
= NULL
;
9775 iverdaux
->vda_nodename
9776 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9777 iverdaux
->vda_name
);
9778 if (iverdaux
->vda_nodename
== NULL
)
9779 goto error_return_bad_verdef
;
9781 iverdaux
->vda_nextptr
= NULL
;
9782 if (iverdaux
->vda_next
== 0)
9784 iverdef
->vd_cnt
= j
+ 1;
9787 if (j
+ 1 < iverdef
->vd_cnt
)
9788 iverdaux
->vda_nextptr
= iverdaux
+ 1;
9790 if (iverdaux
->vda_next
9791 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
9792 goto error_return_bad_verdef
;
9794 everdaux
= ((Elf_External_Verdaux
*)
9795 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
9798 iverdef
->vd_nodename
= NULL
;
9799 if (iverdef
->vd_cnt
)
9800 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
9802 iverdef
->vd_nextdef
= NULL
;
9803 if (iverdef
->vd_next
== 0)
9805 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
9806 iverdef
->vd_nextdef
= iverdef
+ 1;
9808 everdef
= ((Elf_External_Verdef
*)
9809 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
9812 if (contents
!= elf_tdata (abfd
)->dt_verdef
)
9813 _bfd_munmap_readonly_temporary (contents_addr
, contents_size
);
9815 contents_addr
= NULL
;
9817 else if (default_imported_symver
)
9824 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9826 bfd_set_error (bfd_error_file_too_big
);
9831 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9832 if (elf_tdata (abfd
)->verdef
== NULL
)
9835 elf_tdata (abfd
)->cverdefs
= freeidx
;
9838 /* Create a default version based on the soname. */
9839 if (default_imported_symver
)
9841 Elf_Internal_Verdef
*iverdef
;
9842 Elf_Internal_Verdaux
*iverdaux
;
9844 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
9846 iverdef
->vd_version
= VER_DEF_CURRENT
;
9847 iverdef
->vd_flags
= 0;
9848 iverdef
->vd_ndx
= freeidx
;
9849 iverdef
->vd_cnt
= 1;
9851 iverdef
->vd_bfd
= abfd
;
9853 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
9854 if (iverdef
->vd_nodename
== NULL
)
9855 goto error_return_verdef
;
9856 iverdef
->vd_nextdef
= NULL
;
9857 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
9858 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
9859 if (iverdef
->vd_auxptr
== NULL
)
9860 goto error_return_verdef
;
9862 iverdaux
= iverdef
->vd_auxptr
;
9863 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9869 if (contents
!= elf_tdata (abfd
)->dt_verneed
9870 && contents
!= elf_tdata (abfd
)->dt_verdef
)
9871 _bfd_munmap_readonly_temporary (contents_addr
, contents_size
);
9876 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9878 elf_symbol_type
*newsym
;
9880 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9883 newsym
->symbol
.the_bfd
= abfd
;
9884 return &newsym
->symbol
;
9888 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9892 bfd_symbol_info (symbol
, ret
);
9895 /* Return whether a symbol name implies a local symbol. Most targets
9896 use this function for the is_local_label_name entry point, but some
9900 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9903 /* Normal local symbols start with ``.L''. */
9904 if (name
[0] == '.' && name
[1] == 'L')
9907 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9908 DWARF debugging symbols starting with ``..''. */
9909 if (name
[0] == '.' && name
[1] == '.')
9912 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9913 emitting DWARF debugging output. I suspect this is actually a
9914 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9915 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9916 underscore to be emitted on some ELF targets). For ease of use,
9917 we treat such symbols as local. */
9918 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9921 /* Treat assembler generated fake symbols, dollar local labels and
9922 forward-backward labels (aka local labels) as locals.
9923 These labels have the form:
9925 L0^A.* (fake symbols)
9927 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9929 Versions which start with .L will have already been matched above,
9930 so we only need to match the rest. */
9931 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9937 for (p
= name
+ 2; (c
= *p
); p
++)
9939 if (c
== 1 || c
== 2)
9941 if (c
== 1 && p
== name
+ 2)
9942 /* A fake symbol. */
9945 /* FIXME: We are being paranoid here and treating symbols like
9946 L0^Bfoo as if there were non-local, on the grounds that the
9947 assembler will never generate them. But can any symbol
9948 containing an ASCII value in the range 1-31 ever be anything
9949 other than some kind of local ? */
9966 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9967 asymbol
*symbol ATTRIBUTE_UNUSED
)
9974 _bfd_elf_set_arch_mach (bfd
*abfd
,
9975 enum bfd_architecture arch
,
9976 unsigned long machine
)
9978 /* If this isn't the right architecture for this backend, and this
9979 isn't the generic backend, fail. */
9980 if (arch
!= get_elf_backend_data (abfd
)->arch
9981 && arch
!= bfd_arch_unknown
9982 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9985 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9988 /* Find the nearest line to a particular section and offset,
9989 for error reporting. */
9992 _bfd_elf_find_nearest_line (bfd
*abfd
,
9996 const char **filename_ptr
,
9997 const char **functionname_ptr
,
9998 unsigned int *line_ptr
,
9999 unsigned int *discriminator_ptr
)
10001 return _bfd_elf_find_nearest_line_with_alt (abfd
, NULL
, symbols
, section
,
10002 offset
, filename_ptr
,
10003 functionname_ptr
, line_ptr
,
10004 discriminator_ptr
);
10007 /* Find the nearest line to a particular section and offset,
10008 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
10009 can be optionally specified. */
10012 _bfd_elf_find_nearest_line_with_alt (bfd
*abfd
,
10013 const char *alt_filename
,
10017 const char **filename_ptr
,
10018 const char **functionname_ptr
,
10019 unsigned int *line_ptr
,
10020 unsigned int *discriminator_ptr
)
10024 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd
, alt_filename
, symbols
, NULL
,
10025 section
, offset
, filename_ptr
,
10026 functionname_ptr
, line_ptr
,
10028 dwarf_debug_sections
,
10029 &elf_tdata (abfd
)->dwarf2_find_line_info
))
10032 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
10033 filename_ptr
, functionname_ptr
, line_ptr
))
10035 if (!*functionname_ptr
)
10036 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
10037 *filename_ptr
? NULL
: filename_ptr
,
10042 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
10043 &found
, filename_ptr
,
10044 functionname_ptr
, line_ptr
,
10045 &elf_tdata (abfd
)->line_info
))
10047 if (found
&& (*functionname_ptr
|| *line_ptr
))
10050 if (symbols
== NULL
)
10053 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
10054 filename_ptr
, functionname_ptr
))
10061 /* Find the line for a symbol. */
10064 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
10065 const char **filename_ptr
, unsigned int *line_ptr
)
10067 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
10068 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
10069 filename_ptr
, NULL
, line_ptr
, NULL
,
10070 dwarf_debug_sections
,
10071 &tdata
->dwarf2_find_line_info
);
10074 /* After a call to bfd_find_nearest_line, successive calls to
10075 bfd_find_inliner_info can be used to get source information about
10076 each level of function inlining that terminated at the address
10077 passed to bfd_find_nearest_line. Currently this is only supported
10078 for DWARF2 with appropriate DWARF3 extensions. */
10081 _bfd_elf_find_inliner_info (bfd
*abfd
,
10082 const char **filename_ptr
,
10083 const char **functionname_ptr
,
10084 unsigned int *line_ptr
)
10086 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
10087 return _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
10088 functionname_ptr
, line_ptr
,
10089 &tdata
->dwarf2_find_line_info
);
10093 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
10095 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10096 int ret
= bed
->s
->sizeof_ehdr
;
10098 if (!bfd_link_relocatable (info
))
10100 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
10102 if (phdr_size
== (bfd_size_type
) -1)
10104 struct elf_segment_map
*m
;
10107 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
10108 phdr_size
+= bed
->s
->sizeof_phdr
;
10110 if (phdr_size
== 0)
10111 phdr_size
= get_program_header_size (abfd
, info
);
10114 elf_program_header_size (abfd
) = phdr_size
;
10122 _bfd_elf_set_section_contents (bfd
*abfd
,
10124 const void *location
,
10126 bfd_size_type count
)
10128 Elf_Internal_Shdr
*hdr
;
10130 if (! abfd
->output_has_begun
10131 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
10137 hdr
= &elf_section_data (section
)->this_hdr
;
10138 if (hdr
->sh_offset
== (file_ptr
) -1)
10140 unsigned char *contents
;
10142 if (bfd_section_is_ctf (section
))
10143 /* Nothing to do with this section: the contents are generated
10147 if ((offset
+ count
) > hdr
->sh_size
)
10150 (_("%pB:%pA: error: attempting to write"
10151 " over the end of the section"),
10154 bfd_set_error (bfd_error_invalid_operation
);
10158 contents
= hdr
->contents
;
10159 if (contents
== NULL
)
10162 (_("%pB:%pA: error: attempting to write"
10163 " section into an empty buffer"),
10166 bfd_set_error (bfd_error_invalid_operation
);
10170 memcpy (contents
+ offset
, location
, count
);
10174 return _bfd_generic_set_section_contents (abfd
, section
,
10175 location
, offset
, count
);
10179 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
10180 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
10181 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
10187 /* Try to convert a non-ELF reloc into an ELF one. */
10190 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
10192 /* Check whether we really have an ELF howto. */
10194 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
10196 bfd_reloc_code_real_type code
;
10197 reloc_howto_type
*howto
;
10199 /* Alien reloc: Try to determine its type to replace it with an
10200 equivalent ELF reloc. */
10202 if (areloc
->howto
->pc_relative
)
10204 switch (areloc
->howto
->bitsize
)
10207 code
= BFD_RELOC_8_PCREL
;
10210 code
= BFD_RELOC_12_PCREL
;
10213 code
= BFD_RELOC_16_PCREL
;
10216 code
= BFD_RELOC_24_PCREL
;
10219 code
= BFD_RELOC_32_PCREL
;
10222 code
= BFD_RELOC_64_PCREL
;
10228 howto
= bfd_reloc_type_lookup (abfd
, code
);
10230 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
10232 if (howto
->pcrel_offset
)
10233 areloc
->addend
+= areloc
->address
;
10235 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
10240 switch (areloc
->howto
->bitsize
)
10243 code
= BFD_RELOC_8
;
10246 code
= BFD_RELOC_14
;
10249 code
= BFD_RELOC_16
;
10252 code
= BFD_RELOC_26
;
10255 code
= BFD_RELOC_32
;
10258 code
= BFD_RELOC_64
;
10264 howto
= bfd_reloc_type_lookup (abfd
, code
);
10268 areloc
->howto
= howto
;
10276 /* xgettext:c-format */
10277 _bfd_error_handler (_("%pB: %s unsupported"),
10278 abfd
, areloc
->howto
->name
);
10279 bfd_set_error (bfd_error_sorry
);
10284 _bfd_elf_free_cached_info (bfd
*abfd
)
10286 struct elf_obj_tdata
*tdata
;
10288 if ((bfd_get_format (abfd
) == bfd_object
10289 || bfd_get_format (abfd
) == bfd_core
)
10290 && (tdata
= elf_tdata (abfd
)) != NULL
)
10292 if (tdata
->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
10293 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
10294 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
10295 _bfd_dwarf1_cleanup_debug_info (abfd
, &tdata
->dwarf1_find_line_info
);
10296 _bfd_stab_cleanup (abfd
, &tdata
->line_info
);
10299 return _bfd_generic_bfd_free_cached_info (abfd
);
10302 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10303 in the relocation's offset. Thus we cannot allow any sort of sanity
10304 range-checking to interfere. There is nothing else to do in processing
10307 bfd_reloc_status_type
10308 _bfd_elf_rel_vtable_reloc_fn
10309 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
10310 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
10311 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
10312 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
10314 return bfd_reloc_ok
;
10317 /* Elf core file support. Much of this only works on native
10318 toolchains, since we rely on knowing the
10319 machine-dependent procfs structure in order to pick
10320 out details about the corefile. */
10322 #ifdef HAVE_SYS_PROCFS_H
10323 # include <sys/procfs.h>
10326 /* Return a PID that identifies a "thread" for threaded cores, or the
10327 PID of the main process for non-threaded cores. */
10330 elfcore_make_pid (bfd
*abfd
)
10334 pid
= elf_tdata (abfd
)->core
->lwpid
;
10336 pid
= elf_tdata (abfd
)->core
->pid
;
10341 /* If there isn't a section called NAME, make one, using data from
10342 SECT. Note, this function will generate a reference to NAME, so
10343 you shouldn't deallocate or overwrite it. */
10346 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
10350 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
10353 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
10357 sect2
->size
= sect
->size
;
10358 sect2
->filepos
= sect
->filepos
;
10359 sect2
->alignment_power
= sect
->alignment_power
;
10363 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10364 actually creates up to two pseudosections:
10365 - For the single-threaded case, a section named NAME, unless
10366 such a section already exists.
10367 - For the multi-threaded case, a section named "NAME/PID", where
10368 PID is elfcore_make_pid (abfd).
10369 Both pseudosections have identical contents. */
10371 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
10377 char *threaded_name
;
10381 /* Build the section name. */
10383 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
10384 len
= strlen (buf
) + 1;
10385 threaded_name
= (char *) bfd_alloc (abfd
, len
);
10386 if (threaded_name
== NULL
)
10388 memcpy (threaded_name
, buf
, len
);
10390 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
10395 sect
->filepos
= filepos
;
10396 sect
->alignment_power
= 2;
10398 return elfcore_maybe_make_sect (abfd
, name
, sect
);
10402 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
10405 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10411 sect
->size
= note
->descsz
- offs
;
10412 sect
->filepos
= note
->descpos
+ offs
;
10413 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10418 /* prstatus_t exists on:
10420 linux 2.[01] + glibc
10424 #if defined (HAVE_PRSTATUS_T)
10427 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10432 if (note
->descsz
== sizeof (prstatus_t
))
10436 size
= sizeof (prstat
.pr_reg
);
10437 offset
= offsetof (prstatus_t
, pr_reg
);
10438 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
10440 /* Do not overwrite the core signal if it
10441 has already been set by another thread. */
10442 if (elf_tdata (abfd
)->core
->signal
== 0)
10443 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
10444 if (elf_tdata (abfd
)->core
->pid
== 0)
10445 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
10447 /* pr_who exists on:
10450 pr_who doesn't exist on:
10453 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10454 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
10456 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
10459 #if defined (HAVE_PRSTATUS32_T)
10460 else if (note
->descsz
== sizeof (prstatus32_t
))
10462 /* 64-bit host, 32-bit corefile */
10463 prstatus32_t prstat
;
10465 size
= sizeof (prstat
.pr_reg
);
10466 offset
= offsetof (prstatus32_t
, pr_reg
);
10467 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
10469 /* Do not overwrite the core signal if it
10470 has already been set by another thread. */
10471 if (elf_tdata (abfd
)->core
->signal
== 0)
10472 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
10473 if (elf_tdata (abfd
)->core
->pid
== 0)
10474 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
10476 /* pr_who exists on:
10479 pr_who doesn't exist on:
10482 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10483 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
10485 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
10488 #endif /* HAVE_PRSTATUS32_T */
10491 /* Fail - we don't know how to handle any other
10492 note size (ie. data object type). */
10496 /* Make a ".reg/999" section and a ".reg" section. */
10497 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10498 size
, note
->descpos
+ offset
);
10500 #endif /* defined (HAVE_PRSTATUS_T) */
10502 /* Create a pseudosection containing the exact contents of NOTE. */
10504 elfcore_make_note_pseudosection (bfd
*abfd
,
10506 Elf_Internal_Note
*note
)
10508 return _bfd_elfcore_make_pseudosection (abfd
, name
,
10509 note
->descsz
, note
->descpos
);
10512 /* There isn't a consistent prfpregset_t across platforms,
10513 but it doesn't matter, because we don't have to pick this
10514 data structure apart. */
10517 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10519 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10522 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10523 type of NT_PRXFPREG. Just include the whole note's contents
10527 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10529 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10532 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10533 with a note type of NT_X86_XSTATE. Just include the whole note's
10534 contents literally. */
10537 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
10539 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
10543 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10545 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
10549 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10551 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
10555 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
10557 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
10561 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10563 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
10567 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10569 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
10573 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
10575 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
10579 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
10581 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
10585 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10587 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
10591 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10593 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
10597 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10599 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
10603 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10605 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
10609 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
10611 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
10615 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
10617 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
10621 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10623 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
10627 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10629 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
10633 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
10635 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
10639 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
10641 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
10645 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
10647 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
10651 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10653 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
10657 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
10659 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
10663 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
10665 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
10669 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10671 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
10675 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
10677 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
10681 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
10683 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
10687 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
10689 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
10693 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
10695 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
10699 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
10701 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
10705 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
10707 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
10711 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
10713 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
10717 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
10719 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
10723 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10725 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
10729 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
10731 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
10735 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
10737 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
10741 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
10743 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
10747 elfcore_grok_aarch_mte (bfd
*abfd
, Elf_Internal_Note
*note
)
10749 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-mte",
10754 elfcore_grok_aarch_ssve (bfd
*abfd
, Elf_Internal_Note
*note
)
10756 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-ssve", note
);
10760 elfcore_grok_aarch_za (bfd
*abfd
, Elf_Internal_Note
*note
)
10762 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-za", note
);
10765 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10766 successful, otherwise return FALSE. */
10769 elfcore_grok_aarch_zt (bfd
*abfd
, Elf_Internal_Note
*note
)
10771 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-zt", note
);
10775 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
10777 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
10780 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10781 successful otherwise, return FALSE. */
10784 elfcore_grok_riscv_csr (bfd
*abfd
, Elf_Internal_Note
*note
)
10786 return elfcore_make_note_pseudosection (abfd
, ".reg-riscv-csr", note
);
10789 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10790 successful otherwise, return FALSE. */
10793 elfcore_grok_gdb_tdesc (bfd
*abfd
, Elf_Internal_Note
*note
)
10795 return elfcore_make_note_pseudosection (abfd
, ".gdb-tdesc", note
);
10799 elfcore_grok_loongarch_cpucfg (bfd
*abfd
, Elf_Internal_Note
*note
)
10801 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-cpucfg", note
);
10805 elfcore_grok_loongarch_lbt (bfd
*abfd
, Elf_Internal_Note
*note
)
10807 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lbt", note
);
10811 elfcore_grok_loongarch_lsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10813 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lsx", note
);
10817 elfcore_grok_loongarch_lasx (bfd
*abfd
, Elf_Internal_Note
*note
)
10819 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lasx", note
);
10822 #if defined (HAVE_PRPSINFO_T)
10823 typedef prpsinfo_t elfcore_psinfo_t
;
10824 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10825 typedef prpsinfo32_t elfcore_psinfo32_t
;
10829 #if defined (HAVE_PSINFO_T)
10830 typedef psinfo_t elfcore_psinfo_t
;
10831 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10832 typedef psinfo32_t elfcore_psinfo32_t
;
10836 /* return a malloc'ed copy of a string at START which is at
10837 most MAX bytes long, possibly without a terminating '\0'.
10838 the copy will always have a terminating '\0'. */
10841 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
10844 char *end
= (char *) memchr (start
, '\0', max
);
10852 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
10856 memcpy (dups
, start
, len
);
10862 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10864 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10866 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
10868 elfcore_psinfo_t psinfo
;
10870 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10872 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10873 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10875 elf_tdata (abfd
)->core
->program
10876 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10877 sizeof (psinfo
.pr_fname
));
10879 elf_tdata (abfd
)->core
->command
10880 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10881 sizeof (psinfo
.pr_psargs
));
10883 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10884 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
10886 /* 64-bit host, 32-bit corefile */
10887 elfcore_psinfo32_t psinfo
;
10889 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10891 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10892 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10894 elf_tdata (abfd
)->core
->program
10895 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10896 sizeof (psinfo
.pr_fname
));
10898 elf_tdata (abfd
)->core
->command
10899 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10900 sizeof (psinfo
.pr_psargs
));
10906 /* Fail - we don't know how to handle any other
10907 note size (ie. data object type). */
10911 /* Note that for some reason, a spurious space is tacked
10912 onto the end of the args in some (at least one anyway)
10913 implementations, so strip it off if it exists. */
10916 char *command
= elf_tdata (abfd
)->core
->command
;
10917 int n
= strlen (command
);
10919 if (0 < n
&& command
[n
- 1] == ' ')
10920 command
[n
- 1] = '\0';
10925 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10927 #if defined (HAVE_PSTATUS_T)
10929 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10931 if (note
->descsz
== sizeof (pstatus_t
)
10932 #if defined (HAVE_PXSTATUS_T)
10933 || note
->descsz
== sizeof (pxstatus_t
)
10939 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10941 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10943 #if defined (HAVE_PSTATUS32_T)
10944 else if (note
->descsz
== sizeof (pstatus32_t
))
10946 /* 64-bit host, 32-bit corefile */
10949 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10951 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10954 /* Could grab some more details from the "representative"
10955 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10956 NT_LWPSTATUS note, presumably. */
10960 #endif /* defined (HAVE_PSTATUS_T) */
10962 #if defined (HAVE_LWPSTATUS_T)
10964 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10966 lwpstatus_t lwpstat
;
10972 if (note
->descsz
!= sizeof (lwpstat
)
10973 #if defined (HAVE_LWPXSTATUS_T)
10974 && note
->descsz
!= sizeof (lwpxstatus_t
)
10979 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10981 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10982 /* Do not overwrite the core signal if it has already been set by
10984 if (elf_tdata (abfd
)->core
->signal
== 0)
10985 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10987 /* Make a ".reg/999" section. */
10989 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10990 len
= strlen (buf
) + 1;
10991 name
= bfd_alloc (abfd
, len
);
10994 memcpy (name
, buf
, len
);
10996 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11000 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11001 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
11002 sect
->filepos
= note
->descpos
11003 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
11006 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11007 sect
->size
= sizeof (lwpstat
.pr_reg
);
11008 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
11011 sect
->alignment_power
= 2;
11013 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
11016 /* Make a ".reg2/999" section */
11018 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
11019 len
= strlen (buf
) + 1;
11020 name
= bfd_alloc (abfd
, len
);
11023 memcpy (name
, buf
, len
);
11025 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11029 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11030 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
11031 sect
->filepos
= note
->descpos
11032 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
11035 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11036 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
11037 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
11040 sect
->alignment_power
= 2;
11042 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
11044 #endif /* defined (HAVE_LWPSTATUS_T) */
11046 /* These constants, and the structure offsets used below, are defined by
11047 Cygwin's core_dump.h */
11048 #define NOTE_INFO_PROCESS 1
11049 #define NOTE_INFO_THREAD 2
11050 #define NOTE_INFO_MODULE 3
11051 #define NOTE_INFO_MODULE64 4
11054 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
11059 unsigned int name_size
;
11062 int is_active_thread
;
11065 if (note
->descsz
< 4)
11068 if (! startswith (note
->namedata
, "win32"))
11071 type
= bfd_get_32 (abfd
, note
->descdata
);
11075 const char *type_name
;
11076 unsigned long min_size
;
11079 { "NOTE_INFO_PROCESS", 12 },
11080 { "NOTE_INFO_THREAD", 12 },
11081 { "NOTE_INFO_MODULE", 12 },
11082 { "NOTE_INFO_MODULE64", 16 },
11085 if (type
== 0 || type
> (sizeof(size_check
)/sizeof(size_check
[0])))
11088 if (note
->descsz
< size_check
[type
- 1].min_size
)
11090 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11092 abfd
, size_check
[type
- 1].type_name
, note
->descsz
);
11098 case NOTE_INFO_PROCESS
:
11099 /* FIXME: need to add ->core->command. */
11100 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 4);
11101 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11104 case NOTE_INFO_THREAD
:
11105 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11107 /* thread_info.tid */
11108 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 4));
11110 len
= strlen (buf
) + 1;
11111 name
= (char *) bfd_alloc (abfd
, len
);
11115 memcpy (name
, buf
, len
);
11117 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11121 /* sizeof (thread_info.thread_context) */
11122 sect
->size
= note
->descsz
- 12;
11123 /* offsetof (thread_info.thread_context) */
11124 sect
->filepos
= note
->descpos
+ 12;
11125 sect
->alignment_power
= 2;
11127 /* thread_info.is_active_thread */
11128 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11130 if (is_active_thread
)
11131 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
11135 case NOTE_INFO_MODULE
:
11136 case NOTE_INFO_MODULE64
:
11137 /* Make a ".module/xxxxxxxx" section. */
11138 if (type
== NOTE_INFO_MODULE
)
11140 /* module_info.base_address */
11141 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
11142 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
11143 /* module_info.module_name_size */
11144 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11146 else /* NOTE_INFO_MODULE64 */
11148 /* module_info.base_address */
11149 base_addr
= bfd_get_64 (abfd
, note
->descdata
+ 4);
11150 sprintf (buf
, ".module/%016lx", (unsigned long) base_addr
);
11151 /* module_info.module_name_size */
11152 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 12);
11155 len
= strlen (buf
) + 1;
11156 name
= (char *) bfd_alloc (abfd
, len
);
11160 memcpy (name
, buf
, len
);
11162 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11167 if (note
->descsz
< 12 + name_size
)
11169 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11170 " is too small to contain a name of size %u"),
11171 abfd
, note
->descsz
, name_size
);
11175 sect
->size
= note
->descsz
;
11176 sect
->filepos
= note
->descpos
;
11177 sect
->alignment_power
= 2;
11188 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11190 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11192 switch (note
->type
)
11198 if (bed
->elf_backend_grok_prstatus
)
11199 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
11201 #if defined (HAVE_PRSTATUS_T)
11202 return elfcore_grok_prstatus (abfd
, note
);
11207 #if defined (HAVE_PSTATUS_T)
11209 return elfcore_grok_pstatus (abfd
, note
);
11212 #if defined (HAVE_LWPSTATUS_T)
11214 return elfcore_grok_lwpstatus (abfd
, note
);
11217 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
11218 return elfcore_grok_prfpreg (abfd
, note
);
11220 case NT_WIN32PSTATUS
:
11221 return elfcore_grok_win32pstatus (abfd
, note
);
11223 case NT_PRXFPREG
: /* Linux SSE extension */
11224 if (note
->namesz
== 6
11225 && strcmp (note
->namedata
, "LINUX") == 0)
11226 return elfcore_grok_prxfpreg (abfd
, note
);
11230 case NT_X86_XSTATE
: /* Linux XSAVE extension */
11231 if (note
->namesz
== 6
11232 && strcmp (note
->namedata
, "LINUX") == 0)
11233 return elfcore_grok_xstatereg (abfd
, note
);
11238 if (note
->namesz
== 6
11239 && strcmp (note
->namedata
, "LINUX") == 0)
11240 return elfcore_grok_ppc_vmx (abfd
, note
);
11245 if (note
->namesz
== 6
11246 && strcmp (note
->namedata
, "LINUX") == 0)
11247 return elfcore_grok_ppc_vsx (abfd
, note
);
11252 if (note
->namesz
== 6
11253 && strcmp (note
->namedata
, "LINUX") == 0)
11254 return elfcore_grok_ppc_tar (abfd
, note
);
11259 if (note
->namesz
== 6
11260 && strcmp (note
->namedata
, "LINUX") == 0)
11261 return elfcore_grok_ppc_ppr (abfd
, note
);
11266 if (note
->namesz
== 6
11267 && strcmp (note
->namedata
, "LINUX") == 0)
11268 return elfcore_grok_ppc_dscr (abfd
, note
);
11273 if (note
->namesz
== 6
11274 && strcmp (note
->namedata
, "LINUX") == 0)
11275 return elfcore_grok_ppc_ebb (abfd
, note
);
11280 if (note
->namesz
== 6
11281 && strcmp (note
->namedata
, "LINUX") == 0)
11282 return elfcore_grok_ppc_pmu (abfd
, note
);
11286 case NT_PPC_TM_CGPR
:
11287 if (note
->namesz
== 6
11288 && strcmp (note
->namedata
, "LINUX") == 0)
11289 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
11293 case NT_PPC_TM_CFPR
:
11294 if (note
->namesz
== 6
11295 && strcmp (note
->namedata
, "LINUX") == 0)
11296 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
11300 case NT_PPC_TM_CVMX
:
11301 if (note
->namesz
== 6
11302 && strcmp (note
->namedata
, "LINUX") == 0)
11303 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
11307 case NT_PPC_TM_CVSX
:
11308 if (note
->namesz
== 6
11309 && strcmp (note
->namedata
, "LINUX") == 0)
11310 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
11314 case NT_PPC_TM_SPR
:
11315 if (note
->namesz
== 6
11316 && strcmp (note
->namedata
, "LINUX") == 0)
11317 return elfcore_grok_ppc_tm_spr (abfd
, note
);
11321 case NT_PPC_TM_CTAR
:
11322 if (note
->namesz
== 6
11323 && strcmp (note
->namedata
, "LINUX") == 0)
11324 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
11328 case NT_PPC_TM_CPPR
:
11329 if (note
->namesz
== 6
11330 && strcmp (note
->namedata
, "LINUX") == 0)
11331 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
11335 case NT_PPC_TM_CDSCR
:
11336 if (note
->namesz
== 6
11337 && strcmp (note
->namedata
, "LINUX") == 0)
11338 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
11342 case NT_S390_HIGH_GPRS
:
11343 if (note
->namesz
== 6
11344 && strcmp (note
->namedata
, "LINUX") == 0)
11345 return elfcore_grok_s390_high_gprs (abfd
, note
);
11349 case NT_S390_TIMER
:
11350 if (note
->namesz
== 6
11351 && strcmp (note
->namedata
, "LINUX") == 0)
11352 return elfcore_grok_s390_timer (abfd
, note
);
11356 case NT_S390_TODCMP
:
11357 if (note
->namesz
== 6
11358 && strcmp (note
->namedata
, "LINUX") == 0)
11359 return elfcore_grok_s390_todcmp (abfd
, note
);
11363 case NT_S390_TODPREG
:
11364 if (note
->namesz
== 6
11365 && strcmp (note
->namedata
, "LINUX") == 0)
11366 return elfcore_grok_s390_todpreg (abfd
, note
);
11371 if (note
->namesz
== 6
11372 && strcmp (note
->namedata
, "LINUX") == 0)
11373 return elfcore_grok_s390_ctrs (abfd
, note
);
11377 case NT_S390_PREFIX
:
11378 if (note
->namesz
== 6
11379 && strcmp (note
->namedata
, "LINUX") == 0)
11380 return elfcore_grok_s390_prefix (abfd
, note
);
11384 case NT_S390_LAST_BREAK
:
11385 if (note
->namesz
== 6
11386 && strcmp (note
->namedata
, "LINUX") == 0)
11387 return elfcore_grok_s390_last_break (abfd
, note
);
11391 case NT_S390_SYSTEM_CALL
:
11392 if (note
->namesz
== 6
11393 && strcmp (note
->namedata
, "LINUX") == 0)
11394 return elfcore_grok_s390_system_call (abfd
, note
);
11399 if (note
->namesz
== 6
11400 && strcmp (note
->namedata
, "LINUX") == 0)
11401 return elfcore_grok_s390_tdb (abfd
, note
);
11405 case NT_S390_VXRS_LOW
:
11406 if (note
->namesz
== 6
11407 && strcmp (note
->namedata
, "LINUX") == 0)
11408 return elfcore_grok_s390_vxrs_low (abfd
, note
);
11412 case NT_S390_VXRS_HIGH
:
11413 if (note
->namesz
== 6
11414 && strcmp (note
->namedata
, "LINUX") == 0)
11415 return elfcore_grok_s390_vxrs_high (abfd
, note
);
11419 case NT_S390_GS_CB
:
11420 if (note
->namesz
== 6
11421 && strcmp (note
->namedata
, "LINUX") == 0)
11422 return elfcore_grok_s390_gs_cb (abfd
, note
);
11426 case NT_S390_GS_BC
:
11427 if (note
->namesz
== 6
11428 && strcmp (note
->namedata
, "LINUX") == 0)
11429 return elfcore_grok_s390_gs_bc (abfd
, note
);
11434 if (note
->namesz
== 6
11435 && strcmp (note
->namedata
, "LINUX") == 0)
11436 return elfcore_grok_arc_v2 (abfd
, note
);
11441 if (note
->namesz
== 6
11442 && strcmp (note
->namedata
, "LINUX") == 0)
11443 return elfcore_grok_arm_vfp (abfd
, note
);
11448 if (note
->namesz
== 6
11449 && strcmp (note
->namedata
, "LINUX") == 0)
11450 return elfcore_grok_aarch_tls (abfd
, note
);
11454 case NT_ARM_HW_BREAK
:
11455 if (note
->namesz
== 6
11456 && strcmp (note
->namedata
, "LINUX") == 0)
11457 return elfcore_grok_aarch_hw_break (abfd
, note
);
11461 case NT_ARM_HW_WATCH
:
11462 if (note
->namesz
== 6
11463 && strcmp (note
->namedata
, "LINUX") == 0)
11464 return elfcore_grok_aarch_hw_watch (abfd
, note
);
11469 if (note
->namesz
== 6
11470 && strcmp (note
->namedata
, "LINUX") == 0)
11471 return elfcore_grok_aarch_sve (abfd
, note
);
11475 case NT_ARM_PAC_MASK
:
11476 if (note
->namesz
== 6
11477 && strcmp (note
->namedata
, "LINUX") == 0)
11478 return elfcore_grok_aarch_pauth (abfd
, note
);
11482 case NT_ARM_TAGGED_ADDR_CTRL
:
11483 if (note
->namesz
== 6
11484 && strcmp (note
->namedata
, "LINUX") == 0)
11485 return elfcore_grok_aarch_mte (abfd
, note
);
11490 if (note
->namesz
== 6
11491 && strcmp (note
->namedata
, "LINUX") == 0)
11492 return elfcore_grok_aarch_ssve (abfd
, note
);
11497 if (note
->namesz
== 6
11498 && strcmp (note
->namedata
, "LINUX") == 0)
11499 return elfcore_grok_aarch_za (abfd
, note
);
11504 if (note
->namesz
== 6
11505 && strcmp (note
->namedata
, "LINUX") == 0)
11506 return elfcore_grok_aarch_zt (abfd
, note
);
11511 if (note
->namesz
== 4
11512 && strcmp (note
->namedata
, "GDB") == 0)
11513 return elfcore_grok_gdb_tdesc (abfd
, note
);
11518 if (note
->namesz
== 4
11519 && strcmp (note
->namedata
, "GDB") == 0)
11520 return elfcore_grok_riscv_csr (abfd
, note
);
11524 case NT_LARCH_CPUCFG
:
11525 if (note
->namesz
== 6
11526 && strcmp (note
->namedata
, "LINUX") == 0)
11527 return elfcore_grok_loongarch_cpucfg (abfd
, note
);
11532 if (note
->namesz
== 6
11533 && strcmp (note
->namedata
, "LINUX") == 0)
11534 return elfcore_grok_loongarch_lbt (abfd
, note
);
11539 if (note
->namesz
== 6
11540 && strcmp (note
->namedata
, "LINUX") == 0)
11541 return elfcore_grok_loongarch_lsx (abfd
, note
);
11545 case NT_LARCH_LASX
:
11546 if (note
->namesz
== 6
11547 && strcmp (note
->namedata
, "LINUX") == 0)
11548 return elfcore_grok_loongarch_lasx (abfd
, note
);
11554 if (bed
->elf_backend_grok_psinfo
)
11555 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
11557 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11558 return elfcore_grok_psinfo (abfd
, note
);
11564 return elfcore_make_auxv_note_section (abfd
, note
, 0);
11567 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
11571 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
11578 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
11580 struct bfd_build_id
* build_id
;
11582 if (note
->descsz
== 0)
11585 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
11586 if (build_id
== NULL
)
11589 build_id
->size
= note
->descsz
;
11590 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
11591 abfd
->build_id
= build_id
;
11597 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11599 switch (note
->type
)
11604 case NT_GNU_PROPERTY_TYPE_0
:
11605 return _bfd_elf_parse_gnu_properties (abfd
, note
);
11607 case NT_GNU_BUILD_ID
:
11608 return elfobj_grok_gnu_build_id (abfd
, note
);
11613 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
11615 struct sdt_note
*cur
=
11616 (struct sdt_note
*) bfd_alloc (abfd
,
11617 sizeof (struct sdt_note
) + note
->descsz
);
11619 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
11620 cur
->size
= (bfd_size_type
) note
->descsz
;
11621 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
11623 elf_tdata (abfd
)->sdt_note_head
= cur
;
11629 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11631 switch (note
->type
)
11634 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
11642 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11646 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11649 if (note
->descsz
< 108)
11654 if (note
->descsz
< 120)
11662 /* Check for version 1 in pr_version. */
11663 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11668 /* Skip over pr_psinfosz. */
11669 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11673 offset
+= 4; /* Padding before pr_psinfosz. */
11677 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11678 elf_tdata (abfd
)->core
->program
11679 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
11682 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11683 elf_tdata (abfd
)->core
->command
11684 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
11687 /* Padding before pr_pid. */
11690 /* The pr_pid field was added in version "1a". */
11691 if (note
->descsz
< offset
+ 4)
11694 elf_tdata (abfd
)->core
->pid
11695 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11701 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
11707 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11708 Also compute minimum size of this note. */
11709 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11713 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
11717 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
11718 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
11725 if (note
->descsz
< min_size
)
11728 /* Check for version 1 in pr_version. */
11729 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11732 /* Extract size of pr_reg from pr_gregsetsz. */
11733 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11734 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11736 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11741 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11745 /* Skip over pr_osreldate. */
11748 /* Read signal from pr_cursig. */
11749 if (elf_tdata (abfd
)->core
->signal
== 0)
11750 elf_tdata (abfd
)->core
->signal
11751 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11754 /* Read TID from pr_pid. */
11755 elf_tdata (abfd
)->core
->lwpid
11756 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11759 /* Padding before pr_reg. */
11760 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
11763 /* Make sure that there is enough data remaining in the note. */
11764 if ((note
->descsz
- offset
) < size
)
11767 /* Make a ".reg/999" section and a ".reg" section. */
11768 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
11769 size
, note
->descpos
+ offset
);
11773 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11775 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11777 switch (note
->type
)
11780 if (bed
->elf_backend_grok_freebsd_prstatus
)
11781 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
11783 return elfcore_grok_freebsd_prstatus (abfd
, note
);
11786 return elfcore_grok_prfpreg (abfd
, note
);
11789 return elfcore_grok_freebsd_psinfo (abfd
, note
);
11791 case NT_FREEBSD_THRMISC
:
11792 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
11794 case NT_FREEBSD_PROCSTAT_PROC
:
11795 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
11798 case NT_FREEBSD_PROCSTAT_FILES
:
11799 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
11802 case NT_FREEBSD_PROCSTAT_VMMAP
:
11803 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
11806 case NT_FREEBSD_PROCSTAT_AUXV
:
11807 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11809 case NT_FREEBSD_X86_SEGBASES
:
11810 return elfcore_make_note_pseudosection (abfd
, ".reg-x86-segbases", note
);
11812 case NT_X86_XSTATE
:
11813 return elfcore_grok_xstatereg (abfd
, note
);
11815 case NT_FREEBSD_PTLWPINFO
:
11816 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
11820 return elfcore_grok_aarch_tls (abfd
, note
);
11823 return elfcore_grok_arm_vfp (abfd
, note
);
11831 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
11835 cp
= strchr (note
->namedata
, '@');
11838 *lwpidp
= atoi(cp
+ 1);
11845 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11847 if (note
->descsz
<= 0x7c + 31)
11850 /* Signal number at offset 0x08. */
11851 elf_tdata (abfd
)->core
->signal
11852 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11854 /* Process ID at offset 0x50. */
11855 elf_tdata (abfd
)->core
->pid
11856 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
11858 /* Command name at 0x7c (max 32 bytes, including nul). */
11859 elf_tdata (abfd
)->core
->command
11860 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
11862 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
11867 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11871 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
11872 elf_tdata (abfd
)->core
->lwpid
= lwp
;
11874 switch (note
->type
)
11876 case NT_NETBSDCORE_PROCINFO
:
11877 /* NetBSD-specific core "procinfo". Note that we expect to
11878 find this note before any of the others, which is fine,
11879 since the kernel writes this note out first when it
11880 creates a core file. */
11881 return elfcore_grok_netbsd_procinfo (abfd
, note
);
11882 case NT_NETBSDCORE_AUXV
:
11883 /* NetBSD-specific Elf Auxiliary Vector data. */
11884 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11885 case NT_NETBSDCORE_LWPSTATUS
:
11886 return elfcore_make_note_pseudosection (abfd
,
11887 ".note.netbsdcore.lwpstatus",
11893 /* As of March 2020 there are no other machine-independent notes
11894 defined for NetBSD core files. If the note type is less
11895 than the start of the machine-dependent note types, we don't
11898 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
11902 switch (bfd_get_arch (abfd
))
11904 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11905 PT_GETFPREGS == mach+2. */
11907 case bfd_arch_aarch64
:
11908 case bfd_arch_alpha
:
11909 case bfd_arch_sparc
:
11910 switch (note
->type
)
11912 case NT_NETBSDCORE_FIRSTMACH
+0:
11913 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11915 case NT_NETBSDCORE_FIRSTMACH
+2:
11916 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11922 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11923 There's also old PT___GETREGS40 == mach + 1 for old reg
11924 structure which lacks GBR. */
11927 switch (note
->type
)
11929 case NT_NETBSDCORE_FIRSTMACH
+3:
11930 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11932 case NT_NETBSDCORE_FIRSTMACH
+5:
11933 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11939 /* On all other arch's, PT_GETREGS == mach+1 and
11940 PT_GETFPREGS == mach+3. */
11943 switch (note
->type
)
11945 case NT_NETBSDCORE_FIRSTMACH
+1:
11946 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11948 case NT_NETBSDCORE_FIRSTMACH
+3:
11949 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11959 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11961 if (note
->descsz
<= 0x48 + 31)
11964 /* Signal number at offset 0x08. */
11965 elf_tdata (abfd
)->core
->signal
11966 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11968 /* Process ID at offset 0x20. */
11969 elf_tdata (abfd
)->core
->pid
11970 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
11972 /* Command name at 0x48 (max 32 bytes, including nul). */
11973 elf_tdata (abfd
)->core
->command
11974 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
11979 /* Processes Solaris's process status note.
11980 sig_off ~ offsetof(prstatus_t, pr_cursig)
11981 pid_off ~ offsetof(prstatus_t, pr_pid)
11982 lwpid_off ~ offsetof(prstatus_t, pr_who)
11983 gregset_size ~ sizeof(gregset_t)
11984 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11987 elfcore_grok_solaris_prstatus (bfd
*abfd
, Elf_Internal_Note
* note
, int sig_off
,
11988 int pid_off
, int lwpid_off
, size_t gregset_size
,
11989 size_t gregset_offset
)
11991 asection
*sect
= NULL
;
11992 elf_tdata (abfd
)->core
->signal
11993 = bfd_get_16 (abfd
, note
->descdata
+ sig_off
);
11994 elf_tdata (abfd
)->core
->pid
11995 = bfd_get_32 (abfd
, note
->descdata
+ pid_off
);
11996 elf_tdata (abfd
)->core
->lwpid
11997 = bfd_get_32 (abfd
, note
->descdata
+ lwpid_off
);
11999 sect
= bfd_get_section_by_name (abfd
, ".reg");
12001 sect
->size
= gregset_size
;
12003 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
12004 note
->descpos
+ gregset_offset
);
12007 /* Gets program and arguments from a core.
12008 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
12009 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
12012 elfcore_grok_solaris_info(bfd
*abfd
, Elf_Internal_Note
* note
,
12013 int prog_off
, int comm_off
)
12015 elf_tdata (abfd
)->core
->program
12016 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ prog_off
, 16);
12017 elf_tdata (abfd
)->core
->command
12018 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ comm_off
, 80);
12023 /* Processes Solaris's LWP status note.
12024 gregset_size ~ sizeof(gregset_t)
12025 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
12026 fpregset_size ~ sizeof(fpregset_t)
12027 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
12030 elfcore_grok_solaris_lwpstatus (bfd
*abfd
, Elf_Internal_Note
* note
,
12031 size_t gregset_size
, int gregset_off
,
12032 size_t fpregset_size
, int fpregset_off
)
12034 asection
*sect
= NULL
;
12035 char reg2_section_name
[16] = { 0 };
12037 (void) snprintf (reg2_section_name
, 16, "%s/%i", ".reg2",
12038 elf_tdata (abfd
)->core
->lwpid
);
12040 /* offsetof(lwpstatus_t, pr_lwpid) */
12041 elf_tdata (abfd
)->core
->lwpid
12042 = bfd_get_32 (abfd
, note
->descdata
+ 4);
12043 /* offsetof(lwpstatus_t, pr_cursig) */
12044 elf_tdata (abfd
)->core
->signal
12045 = bfd_get_16 (abfd
, note
->descdata
+ 12);
12047 sect
= bfd_get_section_by_name (abfd
, ".reg");
12049 sect
->size
= gregset_size
;
12050 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
12051 note
->descpos
+ gregset_off
))
12054 sect
= bfd_get_section_by_name (abfd
, reg2_section_name
);
12057 sect
->size
= fpregset_size
;
12058 sect
->filepos
= note
->descpos
+ fpregset_off
;
12059 sect
->alignment_power
= 2;
12061 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg2", fpregset_size
,
12062 note
->descpos
+ fpregset_off
))
12069 elfcore_grok_solaris_note_impl (bfd
*abfd
, Elf_Internal_Note
*note
)
12074 /* core files are identified as 32- or 64-bit, SPARC or x86,
12075 by the size of the descsz which matches the sizeof()
12076 the type appropriate for that note type (e.g., prstatus_t for
12077 SOLARIS_NT_PRSTATUS) for the corresponding architecture
12078 on Solaris. The core file bitness may differ from the bitness of
12079 gdb itself, so fixed values are used instead of sizeof().
12080 Appropriate fixed offsets are also used to obtain data from
12083 switch ((int) note
->type
)
12085 case SOLARIS_NT_PRSTATUS
:
12086 switch (note
->descsz
)
12088 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12089 return elfcore_grok_solaris_prstatus(abfd
, note
,
12090 136, 216, 308, 152, 356);
12091 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12092 return elfcore_grok_solaris_prstatus(abfd
, note
,
12093 264, 360, 520, 304, 600);
12094 case 432: /* sizeof(prstatus_t) Intel 32-bit */
12095 return elfcore_grok_solaris_prstatus(abfd
, note
,
12096 136, 216, 308, 76, 356);
12097 case 824: /* sizeof(prstatus_t) Intel 64-bit */
12098 return elfcore_grok_solaris_prstatus(abfd
, note
,
12099 264, 360, 520, 224, 600);
12104 case SOLARIS_NT_PSINFO
:
12105 case SOLARIS_NT_PRPSINFO
:
12106 switch (note
->descsz
)
12108 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12109 return elfcore_grok_solaris_info(abfd
, note
, 84, 100);
12110 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12111 return elfcore_grok_solaris_info(abfd
, note
, 120, 136);
12112 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12113 return elfcore_grok_solaris_info(abfd
, note
, 88, 104);
12114 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12115 return elfcore_grok_solaris_info(abfd
, note
, 136, 152);
12120 case SOLARIS_NT_LWPSTATUS
:
12121 switch (note
->descsz
)
12123 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12124 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12125 152, 344, 400, 496);
12126 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12127 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12128 304, 544, 544, 848);
12129 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12130 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12131 76, 344, 380, 420);
12132 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12133 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12134 224, 544, 528, 768);
12139 case SOLARIS_NT_LWPSINFO
:
12140 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12141 if (note
->descsz
== 128 || note
->descsz
== 152)
12142 elf_tdata (abfd
)->core
->lwpid
=
12143 bfd_get_32 (abfd
, note
->descdata
+ 4);
12153 /* For name starting with "CORE" this may be either a Solaris
12154 core file or a gdb-generated core file. Do Solaris-specific
12155 processing on selected note types first with
12156 elfcore_grok_solaris_note(), then process the note
12157 in elfcore_grok_note(). */
12160 elfcore_grok_solaris_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12162 if (!elfcore_grok_solaris_note_impl (abfd
, note
))
12165 return elfcore_grok_note (abfd
, note
);
12169 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12171 if (note
->type
== NT_OPENBSD_PROCINFO
)
12172 return elfcore_grok_openbsd_procinfo (abfd
, note
);
12174 if (note
->type
== NT_OPENBSD_REGS
)
12175 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
12177 if (note
->type
== NT_OPENBSD_FPREGS
)
12178 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
12180 if (note
->type
== NT_OPENBSD_XFPREGS
)
12181 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
12183 if (note
->type
== NT_OPENBSD_AUXV
)
12184 return elfcore_make_auxv_note_section (abfd
, note
, 0);
12186 if (note
->type
== NT_OPENBSD_WCOOKIE
)
12188 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
12193 sect
->size
= note
->descsz
;
12194 sect
->filepos
= note
->descpos
;
12195 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
12204 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
12206 void *ddata
= note
->descdata
;
12213 if (note
->descsz
< 16)
12216 /* nto_procfs_status 'pid' field is at offset 0. */
12217 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
12219 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12220 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
12222 /* nto_procfs_status 'flags' field is at offset 8. */
12223 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
12225 /* nto_procfs_status 'what' field is at offset 14. */
12226 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
12228 elf_tdata (abfd
)->core
->signal
= sig
;
12229 elf_tdata (abfd
)->core
->lwpid
= *tid
;
12232 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12233 do not come from signals so we make sure we set the current
12234 thread just in case. */
12235 if (flags
& 0x00000080)
12236 elf_tdata (abfd
)->core
->lwpid
= *tid
;
12238 /* Make a ".qnx_core_status/%d" section. */
12239 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
12241 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
12244 strcpy (name
, buf
);
12246 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12250 sect
->size
= note
->descsz
;
12251 sect
->filepos
= note
->descpos
;
12252 sect
->alignment_power
= 2;
12254 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
12258 elfcore_grok_nto_regs (bfd
*abfd
,
12259 Elf_Internal_Note
*note
,
12267 /* Make a "(base)/%d" section. */
12268 sprintf (buf
, "%s/%ld", base
, tid
);
12270 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
12273 strcpy (name
, buf
);
12275 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12279 sect
->size
= note
->descsz
;
12280 sect
->filepos
= note
->descpos
;
12281 sect
->alignment_power
= 2;
12283 /* This is the current thread. */
12284 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
12285 return elfcore_maybe_make_sect (abfd
, base
, sect
);
12291 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12293 /* Every GREG section has a STATUS section before it. Store the
12294 tid from the previous call to pass down to the next gregs
12296 static long tid
= 1;
12298 switch (note
->type
)
12300 case QNT_CORE_INFO
:
12301 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
12302 case QNT_CORE_STATUS
:
12303 return elfcore_grok_nto_status (abfd
, note
, &tid
);
12304 case QNT_CORE_GREG
:
12305 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
12306 case QNT_CORE_FPREG
:
12307 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
12314 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12320 /* Use note name as section name. */
12321 len
= note
->namesz
;
12322 name
= (char *) bfd_alloc (abfd
, len
);
12325 memcpy (name
, note
->namedata
, len
);
12326 name
[len
- 1] = '\0';
12328 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12332 sect
->size
= note
->descsz
;
12333 sect
->filepos
= note
->descpos
;
12334 sect
->alignment_power
= 1;
12339 /* Function: elfcore_write_note
12342 buffer to hold note, and current size of buffer
12346 size of data for note
12348 Writes note to end of buffer. ELF64 notes are written exactly as
12349 for ELF32, despite the current (as of 2006) ELF gabi specifying
12350 that they ought to have 8-byte namesz and descsz field, and have
12351 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12354 Pointer to realloc'd buffer, *BUFSIZ updated. */
12357 elfcore_write_note (bfd
*abfd
,
12365 Elf_External_Note
*xnp
;
12372 namesz
= strlen (name
) + 1;
12374 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
12376 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
12379 dest
= buf
+ *bufsiz
;
12380 *bufsiz
+= newspace
;
12381 xnp
= (Elf_External_Note
*) dest
;
12382 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
12383 H_PUT_32 (abfd
, size
, xnp
->descsz
);
12384 H_PUT_32 (abfd
, type
, xnp
->type
);
12388 memcpy (dest
, name
, namesz
);
12396 memcpy (dest
, input
, size
);
12406 /* gcc-8 warns (*) on all the strncpy calls in this function about
12407 possible string truncation. The "truncation" is not a bug. We
12408 have an external representation of structs with fields that are not
12409 necessarily NULL terminated and corresponding internal
12410 representation fields that are one larger so that they can always
12411 be NULL terminated.
12412 gcc versions between 4.2 and 4.6 do not allow pragma control of
12413 diagnostics inside functions, giving a hard error if you try to use
12414 the finer control available with later versions.
12415 gcc prior to 4.2 warns about diagnostic push and pop.
12416 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12417 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12418 (*) Depending on your system header files! */
12419 #if GCC_VERSION >= 8000
12420 # pragma GCC diagnostic push
12421 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12424 elfcore_write_prpsinfo (bfd
*abfd
,
12428 const char *psargs
)
12430 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12432 if (bed
->elf_backend_write_core_note
!= NULL
)
12435 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
12436 NT_PRPSINFO
, fname
, psargs
);
12441 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12442 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12443 if (bed
->s
->elfclass
== ELFCLASS32
)
12445 # if defined (HAVE_PSINFO32_T)
12447 int note_type
= NT_PSINFO
;
12450 int note_type
= NT_PRPSINFO
;
12453 memset (&data
, 0, sizeof (data
));
12454 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
12455 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
12456 return elfcore_write_note (abfd
, buf
, bufsiz
,
12457 "CORE", note_type
, &data
, sizeof (data
));
12462 # if defined (HAVE_PSINFO_T)
12464 int note_type
= NT_PSINFO
;
12467 int note_type
= NT_PRPSINFO
;
12470 memset (&data
, 0, sizeof (data
));
12471 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
12472 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
12473 return elfcore_write_note (abfd
, buf
, bufsiz
,
12474 "CORE", note_type
, &data
, sizeof (data
));
12476 #endif /* PSINFO_T or PRPSINFO_T */
12481 #if GCC_VERSION >= 8000
12482 # pragma GCC diagnostic pop
12486 elfcore_write_linux_prpsinfo32
12487 (bfd
*abfd
, char *buf
, int *bufsiz
,
12488 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
12490 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
12492 struct elf_external_linux_prpsinfo32_ugid16 data
;
12494 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
12495 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
12496 &data
, sizeof (data
));
12500 struct elf_external_linux_prpsinfo32_ugid32 data
;
12502 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
12503 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
12504 &data
, sizeof (data
));
12509 elfcore_write_linux_prpsinfo64
12510 (bfd
*abfd
, char *buf
, int *bufsiz
,
12511 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
12513 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
12515 struct elf_external_linux_prpsinfo64_ugid16 data
;
12517 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
12518 return elfcore_write_note (abfd
, buf
, bufsiz
,
12519 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
12523 struct elf_external_linux_prpsinfo64_ugid32 data
;
12525 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
12526 return elfcore_write_note (abfd
, buf
, bufsiz
,
12527 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
12532 elfcore_write_prstatus (bfd
*abfd
,
12539 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12541 if (bed
->elf_backend_write_core_note
!= NULL
)
12544 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
12546 pid
, cursig
, gregs
);
12551 #if defined (HAVE_PRSTATUS_T)
12552 #if defined (HAVE_PRSTATUS32_T)
12553 if (bed
->s
->elfclass
== ELFCLASS32
)
12555 prstatus32_t prstat
;
12557 memset (&prstat
, 0, sizeof (prstat
));
12558 prstat
.pr_pid
= pid
;
12559 prstat
.pr_cursig
= cursig
;
12560 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
12561 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
12562 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
12569 memset (&prstat
, 0, sizeof (prstat
));
12570 prstat
.pr_pid
= pid
;
12571 prstat
.pr_cursig
= cursig
;
12572 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
12573 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
12574 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
12576 #endif /* HAVE_PRSTATUS_T */
12582 #if defined (HAVE_LWPSTATUS_T)
12584 elfcore_write_lwpstatus (bfd
*abfd
,
12591 lwpstatus_t lwpstat
;
12592 const char *note_name
= "CORE";
12594 memset (&lwpstat
, 0, sizeof (lwpstat
));
12595 lwpstat
.pr_lwpid
= pid
>> 16;
12596 lwpstat
.pr_cursig
= cursig
;
12597 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12598 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
12599 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12600 #if !defined(gregs)
12601 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
12602 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
12604 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
12605 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
12608 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12609 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
12611 #endif /* HAVE_LWPSTATUS_T */
12613 #if defined (HAVE_PSTATUS_T)
12615 elfcore_write_pstatus (bfd
*abfd
,
12619 int cursig ATTRIBUTE_UNUSED
,
12620 const void *gregs ATTRIBUTE_UNUSED
)
12622 const char *note_name
= "CORE";
12623 #if defined (HAVE_PSTATUS32_T)
12624 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12626 if (bed
->s
->elfclass
== ELFCLASS32
)
12630 memset (&pstat
, 0, sizeof (pstat
));
12631 pstat
.pr_pid
= pid
& 0xffff;
12632 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12633 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12641 memset (&pstat
, 0, sizeof (pstat
));
12642 pstat
.pr_pid
= pid
& 0xffff;
12643 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12644 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12648 #endif /* HAVE_PSTATUS_T */
12651 elfcore_write_prfpreg (bfd
*abfd
,
12654 const void *fpregs
,
12657 const char *note_name
= "CORE";
12658 return elfcore_write_note (abfd
, buf
, bufsiz
,
12659 note_name
, NT_FPREGSET
, fpregs
, size
);
12663 elfcore_write_prxfpreg (bfd
*abfd
,
12666 const void *xfpregs
,
12669 char *note_name
= "LINUX";
12670 return elfcore_write_note (abfd
, buf
, bufsiz
,
12671 note_name
, NT_PRXFPREG
, xfpregs
, size
);
12675 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
12676 const void *xfpregs
, int size
)
12679 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
12680 note_name
= "FreeBSD";
12682 note_name
= "LINUX";
12683 return elfcore_write_note (abfd
, buf
, bufsiz
,
12684 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
12688 elfcore_write_x86_segbases (bfd
*abfd
, char *buf
, int *bufsiz
,
12689 const void *regs
, int size
)
12691 char *note_name
= "FreeBSD";
12692 return elfcore_write_note (abfd
, buf
, bufsiz
,
12693 note_name
, NT_FREEBSD_X86_SEGBASES
, regs
, size
);
12697 elfcore_write_ppc_vmx (bfd
*abfd
,
12700 const void *ppc_vmx
,
12703 char *note_name
= "LINUX";
12704 return elfcore_write_note (abfd
, buf
, bufsiz
,
12705 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
12709 elfcore_write_ppc_vsx (bfd
*abfd
,
12712 const void *ppc_vsx
,
12715 char *note_name
= "LINUX";
12716 return elfcore_write_note (abfd
, buf
, bufsiz
,
12717 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
12721 elfcore_write_ppc_tar (bfd
*abfd
,
12724 const void *ppc_tar
,
12727 char *note_name
= "LINUX";
12728 return elfcore_write_note (abfd
, buf
, bufsiz
,
12729 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
12733 elfcore_write_ppc_ppr (bfd
*abfd
,
12736 const void *ppc_ppr
,
12739 char *note_name
= "LINUX";
12740 return elfcore_write_note (abfd
, buf
, bufsiz
,
12741 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
12745 elfcore_write_ppc_dscr (bfd
*abfd
,
12748 const void *ppc_dscr
,
12751 char *note_name
= "LINUX";
12752 return elfcore_write_note (abfd
, buf
, bufsiz
,
12753 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
12757 elfcore_write_ppc_ebb (bfd
*abfd
,
12760 const void *ppc_ebb
,
12763 char *note_name
= "LINUX";
12764 return elfcore_write_note (abfd
, buf
, bufsiz
,
12765 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
12769 elfcore_write_ppc_pmu (bfd
*abfd
,
12772 const void *ppc_pmu
,
12775 char *note_name
= "LINUX";
12776 return elfcore_write_note (abfd
, buf
, bufsiz
,
12777 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
12781 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
12784 const void *ppc_tm_cgpr
,
12787 char *note_name
= "LINUX";
12788 return elfcore_write_note (abfd
, buf
, bufsiz
,
12789 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
12793 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
12796 const void *ppc_tm_cfpr
,
12799 char *note_name
= "LINUX";
12800 return elfcore_write_note (abfd
, buf
, bufsiz
,
12801 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
12805 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
12808 const void *ppc_tm_cvmx
,
12811 char *note_name
= "LINUX";
12812 return elfcore_write_note (abfd
, buf
, bufsiz
,
12813 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
12817 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
12820 const void *ppc_tm_cvsx
,
12823 char *note_name
= "LINUX";
12824 return elfcore_write_note (abfd
, buf
, bufsiz
,
12825 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
12829 elfcore_write_ppc_tm_spr (bfd
*abfd
,
12832 const void *ppc_tm_spr
,
12835 char *note_name
= "LINUX";
12836 return elfcore_write_note (abfd
, buf
, bufsiz
,
12837 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
12841 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
12844 const void *ppc_tm_ctar
,
12847 char *note_name
= "LINUX";
12848 return elfcore_write_note (abfd
, buf
, bufsiz
,
12849 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
12853 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
12856 const void *ppc_tm_cppr
,
12859 char *note_name
= "LINUX";
12860 return elfcore_write_note (abfd
, buf
, bufsiz
,
12861 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
12865 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
12868 const void *ppc_tm_cdscr
,
12871 char *note_name
= "LINUX";
12872 return elfcore_write_note (abfd
, buf
, bufsiz
,
12873 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
12877 elfcore_write_s390_high_gprs (bfd
*abfd
,
12880 const void *s390_high_gprs
,
12883 char *note_name
= "LINUX";
12884 return elfcore_write_note (abfd
, buf
, bufsiz
,
12885 note_name
, NT_S390_HIGH_GPRS
,
12886 s390_high_gprs
, size
);
12890 elfcore_write_s390_timer (bfd
*abfd
,
12893 const void *s390_timer
,
12896 char *note_name
= "LINUX";
12897 return elfcore_write_note (abfd
, buf
, bufsiz
,
12898 note_name
, NT_S390_TIMER
, s390_timer
, size
);
12902 elfcore_write_s390_todcmp (bfd
*abfd
,
12905 const void *s390_todcmp
,
12908 char *note_name
= "LINUX";
12909 return elfcore_write_note (abfd
, buf
, bufsiz
,
12910 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
12914 elfcore_write_s390_todpreg (bfd
*abfd
,
12917 const void *s390_todpreg
,
12920 char *note_name
= "LINUX";
12921 return elfcore_write_note (abfd
, buf
, bufsiz
,
12922 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
12926 elfcore_write_s390_ctrs (bfd
*abfd
,
12929 const void *s390_ctrs
,
12932 char *note_name
= "LINUX";
12933 return elfcore_write_note (abfd
, buf
, bufsiz
,
12934 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
12938 elfcore_write_s390_prefix (bfd
*abfd
,
12941 const void *s390_prefix
,
12944 char *note_name
= "LINUX";
12945 return elfcore_write_note (abfd
, buf
, bufsiz
,
12946 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
12950 elfcore_write_s390_last_break (bfd
*abfd
,
12953 const void *s390_last_break
,
12956 char *note_name
= "LINUX";
12957 return elfcore_write_note (abfd
, buf
, bufsiz
,
12958 note_name
, NT_S390_LAST_BREAK
,
12959 s390_last_break
, size
);
12963 elfcore_write_s390_system_call (bfd
*abfd
,
12966 const void *s390_system_call
,
12969 char *note_name
= "LINUX";
12970 return elfcore_write_note (abfd
, buf
, bufsiz
,
12971 note_name
, NT_S390_SYSTEM_CALL
,
12972 s390_system_call
, size
);
12976 elfcore_write_s390_tdb (bfd
*abfd
,
12979 const void *s390_tdb
,
12982 char *note_name
= "LINUX";
12983 return elfcore_write_note (abfd
, buf
, bufsiz
,
12984 note_name
, NT_S390_TDB
, s390_tdb
, size
);
12988 elfcore_write_s390_vxrs_low (bfd
*abfd
,
12991 const void *s390_vxrs_low
,
12994 char *note_name
= "LINUX";
12995 return elfcore_write_note (abfd
, buf
, bufsiz
,
12996 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
13000 elfcore_write_s390_vxrs_high (bfd
*abfd
,
13003 const void *s390_vxrs_high
,
13006 char *note_name
= "LINUX";
13007 return elfcore_write_note (abfd
, buf
, bufsiz
,
13008 note_name
, NT_S390_VXRS_HIGH
,
13009 s390_vxrs_high
, size
);
13013 elfcore_write_s390_gs_cb (bfd
*abfd
,
13016 const void *s390_gs_cb
,
13019 char *note_name
= "LINUX";
13020 return elfcore_write_note (abfd
, buf
, bufsiz
,
13021 note_name
, NT_S390_GS_CB
,
13026 elfcore_write_s390_gs_bc (bfd
*abfd
,
13029 const void *s390_gs_bc
,
13032 char *note_name
= "LINUX";
13033 return elfcore_write_note (abfd
, buf
, bufsiz
,
13034 note_name
, NT_S390_GS_BC
,
13039 elfcore_write_arm_vfp (bfd
*abfd
,
13042 const void *arm_vfp
,
13045 char *note_name
= "LINUX";
13046 return elfcore_write_note (abfd
, buf
, bufsiz
,
13047 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
13051 elfcore_write_aarch_tls (bfd
*abfd
,
13054 const void *aarch_tls
,
13057 char *note_name
= "LINUX";
13058 return elfcore_write_note (abfd
, buf
, bufsiz
,
13059 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
13063 elfcore_write_aarch_hw_break (bfd
*abfd
,
13066 const void *aarch_hw_break
,
13069 char *note_name
= "LINUX";
13070 return elfcore_write_note (abfd
, buf
, bufsiz
,
13071 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
13075 elfcore_write_aarch_hw_watch (bfd
*abfd
,
13078 const void *aarch_hw_watch
,
13081 char *note_name
= "LINUX";
13082 return elfcore_write_note (abfd
, buf
, bufsiz
,
13083 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
13087 elfcore_write_aarch_sve (bfd
*abfd
,
13090 const void *aarch_sve
,
13093 char *note_name
= "LINUX";
13094 return elfcore_write_note (abfd
, buf
, bufsiz
,
13095 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
13099 elfcore_write_aarch_pauth (bfd
*abfd
,
13102 const void *aarch_pauth
,
13105 char *note_name
= "LINUX";
13106 return elfcore_write_note (abfd
, buf
, bufsiz
,
13107 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
13111 elfcore_write_aarch_mte (bfd
*abfd
,
13114 const void *aarch_mte
,
13117 char *note_name
= "LINUX";
13118 return elfcore_write_note (abfd
, buf
, bufsiz
,
13119 note_name
, NT_ARM_TAGGED_ADDR_CTRL
,
13125 elfcore_write_aarch_ssve (bfd
*abfd
,
13128 const void *aarch_ssve
,
13131 char *note_name
= "LINUX";
13132 return elfcore_write_note (abfd
, buf
, bufsiz
,
13133 note_name
, NT_ARM_SSVE
,
13139 elfcore_write_aarch_za (bfd
*abfd
,
13142 const void *aarch_za
,
13145 char *note_name
= "LINUX";
13146 return elfcore_write_note (abfd
, buf
, bufsiz
,
13147 note_name
, NT_ARM_ZA
,
13152 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13153 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13154 written into. Return a pointer to the new start of the note buffer, to
13155 replace BUF which may no longer be valid. */
13158 elfcore_write_aarch_zt (bfd
*abfd
,
13161 const void *aarch_zt
,
13164 char *note_name
= "LINUX";
13165 return elfcore_write_note (abfd
, buf
, bufsiz
,
13166 note_name
, NT_ARM_ZT
,
13172 elfcore_write_arc_v2 (bfd
*abfd
,
13175 const void *arc_v2
,
13178 char *note_name
= "LINUX";
13179 return elfcore_write_note (abfd
, buf
, bufsiz
,
13180 note_name
, NT_ARC_V2
, arc_v2
, size
);
13184 elfcore_write_loongarch_cpucfg (bfd
*abfd
,
13187 const void *loongarch_cpucfg
,
13190 char *note_name
= "LINUX";
13191 return elfcore_write_note (abfd
, buf
, bufsiz
,
13192 note_name
, NT_LARCH_CPUCFG
,
13193 loongarch_cpucfg
, size
);
13197 elfcore_write_loongarch_lbt (bfd
*abfd
,
13200 const void *loongarch_lbt
,
13203 char *note_name
= "LINUX";
13204 return elfcore_write_note (abfd
, buf
, bufsiz
,
13205 note_name
, NT_LARCH_LBT
, loongarch_lbt
, size
);
13209 elfcore_write_loongarch_lsx (bfd
*abfd
,
13212 const void *loongarch_lsx
,
13215 char *note_name
= "LINUX";
13216 return elfcore_write_note (abfd
, buf
, bufsiz
,
13217 note_name
, NT_LARCH_LSX
, loongarch_lsx
, size
);
13221 elfcore_write_loongarch_lasx (bfd
*abfd
,
13224 const void *loongarch_lasx
,
13227 char *note_name
= "LINUX";
13228 return elfcore_write_note (abfd
, buf
, bufsiz
,
13229 note_name
, NT_LARCH_LASX
, loongarch_lasx
, size
);
13232 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13233 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13234 written into. Return a pointer to the new start of the note buffer, to
13235 replace BUF which may no longer be valid. */
13238 elfcore_write_riscv_csr (bfd
*abfd
,
13244 const char *note_name
= "GDB";
13245 return elfcore_write_note (abfd
, buf
, bufsiz
,
13246 note_name
, NT_RISCV_CSR
, csrs
, size
);
13249 /* Write the target description (a string) pointed to by TDESC, length
13250 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13251 note is being written into. Return a pointer to the new start of the
13252 note buffer, to replace BUF which may no longer be valid. */
13255 elfcore_write_gdb_tdesc (bfd
*abfd
,
13261 const char *note_name
= "GDB";
13262 return elfcore_write_note (abfd
, buf
, bufsiz
,
13263 note_name
, NT_GDB_TDESC
, tdesc
, size
);
13267 elfcore_write_register_note (bfd
*abfd
,
13270 const char *section
,
13274 if (strcmp (section
, ".reg2") == 0)
13275 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
13276 if (strcmp (section
, ".reg-xfp") == 0)
13277 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
13278 if (strcmp (section
, ".reg-xstate") == 0)
13279 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
13280 if (strcmp (section
, ".reg-x86-segbases") == 0)
13281 return elfcore_write_x86_segbases (abfd
, buf
, bufsiz
, data
, size
);
13282 if (strcmp (section
, ".reg-ppc-vmx") == 0)
13283 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
13284 if (strcmp (section
, ".reg-ppc-vsx") == 0)
13285 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
13286 if (strcmp (section
, ".reg-ppc-tar") == 0)
13287 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
13288 if (strcmp (section
, ".reg-ppc-ppr") == 0)
13289 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
13290 if (strcmp (section
, ".reg-ppc-dscr") == 0)
13291 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
13292 if (strcmp (section
, ".reg-ppc-ebb") == 0)
13293 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
13294 if (strcmp (section
, ".reg-ppc-pmu") == 0)
13295 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
13296 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
13297 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
13298 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
13299 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
13300 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
13301 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
13302 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
13303 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
13304 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
13305 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
13306 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
13307 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
13308 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
13309 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
13310 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
13311 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
13312 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
13313 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
13314 if (strcmp (section
, ".reg-s390-timer") == 0)
13315 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
13316 if (strcmp (section
, ".reg-s390-todcmp") == 0)
13317 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
13318 if (strcmp (section
, ".reg-s390-todpreg") == 0)
13319 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
13320 if (strcmp (section
, ".reg-s390-ctrs") == 0)
13321 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
13322 if (strcmp (section
, ".reg-s390-prefix") == 0)
13323 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
13324 if (strcmp (section
, ".reg-s390-last-break") == 0)
13325 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
13326 if (strcmp (section
, ".reg-s390-system-call") == 0)
13327 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
13328 if (strcmp (section
, ".reg-s390-tdb") == 0)
13329 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
13330 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
13331 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
13332 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
13333 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
13334 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
13335 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
13336 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
13337 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
13338 if (strcmp (section
, ".reg-arm-vfp") == 0)
13339 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
13340 if (strcmp (section
, ".reg-aarch-tls") == 0)
13341 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
13342 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
13343 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
13344 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
13345 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
13346 if (strcmp (section
, ".reg-aarch-sve") == 0)
13347 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
13348 if (strcmp (section
, ".reg-aarch-pauth") == 0)
13349 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
13350 if (strcmp (section
, ".reg-aarch-mte") == 0)
13351 return elfcore_write_aarch_mte (abfd
, buf
, bufsiz
, data
, size
);
13352 if (strcmp (section
, ".reg-aarch-ssve") == 0)
13353 return elfcore_write_aarch_ssve (abfd
, buf
, bufsiz
, data
, size
);
13354 if (strcmp (section
, ".reg-aarch-za") == 0)
13355 return elfcore_write_aarch_za (abfd
, buf
, bufsiz
, data
, size
);
13356 if (strcmp (section
, ".reg-aarch-zt") == 0)
13357 return elfcore_write_aarch_zt (abfd
, buf
, bufsiz
, data
, size
);
13358 if (strcmp (section
, ".reg-arc-v2") == 0)
13359 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
13360 if (strcmp (section
, ".gdb-tdesc") == 0)
13361 return elfcore_write_gdb_tdesc (abfd
, buf
, bufsiz
, data
, size
);
13362 if (strcmp (section
, ".reg-riscv-csr") == 0)
13363 return elfcore_write_riscv_csr (abfd
, buf
, bufsiz
, data
, size
);
13364 if (strcmp (section
, ".reg-loongarch-cpucfg") == 0)
13365 return elfcore_write_loongarch_cpucfg (abfd
, buf
, bufsiz
, data
, size
);
13366 if (strcmp (section
, ".reg-loongarch-lbt") == 0)
13367 return elfcore_write_loongarch_lbt (abfd
, buf
, bufsiz
, data
, size
);
13368 if (strcmp (section
, ".reg-loongarch-lsx") == 0)
13369 return elfcore_write_loongarch_lsx (abfd
, buf
, bufsiz
, data
, size
);
13370 if (strcmp (section
, ".reg-loongarch-lasx") == 0)
13371 return elfcore_write_loongarch_lasx (abfd
, buf
, bufsiz
, data
, size
);
13376 elfcore_write_file_note (bfd
*obfd
, char *note_data
, int *note_size
,
13377 const void *buf
, int bufsiz
)
13379 return elfcore_write_note (obfd
, note_data
, note_size
,
13380 "CORE", NT_FILE
, buf
, bufsiz
);
13384 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
13389 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13390 gABI specifies that PT_NOTE alignment should be aligned to 4
13391 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13392 align is less than 4, we use 4 byte alignment. */
13395 if (align
!= 4 && align
!= 8)
13399 while (p
< buf
+ size
)
13401 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
13402 Elf_Internal_Note in
;
13404 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
13407 in
.type
= H_GET_32 (abfd
, xnp
->type
);
13409 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
13410 in
.namedata
= xnp
->name
;
13411 if (in
.namesz
> buf
- in
.namedata
+ size
)
13414 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
13415 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
13416 in
.descpos
= offset
+ (in
.descdata
- buf
);
13418 && (in
.descdata
>= buf
+ size
13419 || in
.descsz
> buf
- in
.descdata
+ size
))
13422 switch (bfd_get_format (abfd
))
13429 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13432 const char * string
;
13434 bool (*func
) (bfd
*, Elf_Internal_Note
*);
13438 GROKER_ELEMENT ("", elfcore_grok_note
),
13439 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
13440 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
13441 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note
),
13442 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
13443 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
13444 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
),
13445 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note
)
13447 #undef GROKER_ELEMENT
13450 for (i
= ARRAY_SIZE (grokers
); i
--;)
13452 if (in
.namesz
>= grokers
[i
].len
13453 && strncmp (in
.namedata
, grokers
[i
].string
,
13454 grokers
[i
].len
) == 0)
13456 if (! grokers
[i
].func (abfd
, & in
))
13465 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
13467 if (! elfobj_grok_gnu_note (abfd
, &in
))
13470 else if (in
.namesz
== sizeof "stapsdt"
13471 && strcmp (in
.namedata
, "stapsdt") == 0)
13473 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
13479 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
13486 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
13491 if (size
== 0 || (size
+ 1) == 0)
13494 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
13497 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
13501 /* PR 17512: file: ec08f814
13502 0-termintate the buffer so that string searches will not overflow. */
13505 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
13515 /* Providing external access to the ELF program header table. */
13517 /* Return an upper bound on the number of bytes required to store a
13518 copy of ABFD's program header table entries. Return -1 if an error
13519 occurs; bfd_get_error will return an appropriate code. */
13522 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
13524 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
13526 bfd_set_error (bfd_error_wrong_format
);
13530 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
13533 /* Copy ABFD's program header table entries to *PHDRS. The entries
13534 will be stored as an array of Elf_Internal_Phdr structures, as
13535 defined in include/elf/internal.h. To find out how large the
13536 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13538 Return the number of program header table entries read, or -1 if an
13539 error occurs; bfd_get_error will return an appropriate code. */
13542 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
13546 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
13548 bfd_set_error (bfd_error_wrong_format
);
13552 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
13553 if (num_phdrs
!= 0)
13554 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
13555 num_phdrs
* sizeof (Elf_Internal_Phdr
));
13560 enum elf_reloc_type_class
13561 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13562 const asection
*rel_sec ATTRIBUTE_UNUSED
,
13563 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
13565 return reloc_class_normal
;
13568 /* For RELA architectures, return the relocation value for a
13569 relocation against a local symbol. */
13572 _bfd_elf_rela_local_sym (bfd
*abfd
,
13573 Elf_Internal_Sym
*sym
,
13575 Elf_Internal_Rela
*rel
)
13577 asection
*sec
= *psec
;
13578 bfd_vma relocation
;
13580 relocation
= (sec
->output_section
->vma
13581 + sec
->output_offset
13583 if ((sec
->flags
& SEC_MERGE
)
13584 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
13585 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
13588 _bfd_merged_section_offset (abfd
, psec
,
13589 elf_section_data (sec
)->sec_info
,
13590 sym
->st_value
+ rel
->r_addend
);
13593 /* If we have changed the section, and our original section is
13594 marked with SEC_EXCLUDE, it means that the original
13595 SEC_MERGE section has been completely subsumed in some
13596 other SEC_MERGE section. In this case, we need to leave
13597 some info around for --emit-relocs. */
13598 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
13599 sec
->kept_section
= *psec
;
13602 rel
->r_addend
-= relocation
;
13603 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
13609 _bfd_elf_rel_local_sym (bfd
*abfd
,
13610 Elf_Internal_Sym
*sym
,
13614 asection
*sec
= *psec
;
13616 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
13617 return sym
->st_value
+ addend
;
13619 return _bfd_merged_section_offset (abfd
, psec
,
13620 elf_section_data (sec
)->sec_info
,
13621 sym
->st_value
+ addend
);
13624 /* Adjust an address within a section. Given OFFSET within SEC, return
13625 the new offset within the section, based upon changes made to the
13626 section. Returns -1 if the offset is now invalid.
13627 The offset (in abnd out) is in target sized bytes, however big a
13631 _bfd_elf_section_offset (bfd
*abfd
,
13632 struct bfd_link_info
*info
,
13636 switch (sec
->sec_info_type
)
13638 case SEC_INFO_TYPE_STABS
:
13639 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
13641 case SEC_INFO_TYPE_EH_FRAME
:
13642 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
13645 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
13647 /* Reverse the offset. */
13648 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13649 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
13651 /* address_size and sec->size are in octets. Convert
13652 to bytes before subtracting the original offset. */
13653 offset
= ((sec
->size
- address_size
)
13654 / bfd_octets_per_byte (abfd
, sec
) - offset
);
13661 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
13662 long symcount ATTRIBUTE_UNUSED
,
13663 asymbol
**syms ATTRIBUTE_UNUSED
,
13668 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13671 const char *relplt_name
;
13672 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
13676 Elf_Internal_Shdr
*hdr
;
13682 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
13685 if (dynsymcount
<= 0)
13688 if (!bed
->plt_sym_val
)
13691 relplt_name
= bed
->relplt_name
;
13692 if (relplt_name
== NULL
)
13693 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
13694 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
13695 if (relplt
== NULL
)
13698 hdr
= &elf_section_data (relplt
)->this_hdr
;
13699 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
13700 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
13703 plt
= bfd_get_section_by_name (abfd
, ".plt");
13707 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
13708 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, true))
13711 count
= NUM_SHDR_ENTRIES (hdr
);
13712 size
= count
* sizeof (asymbol
);
13713 p
= relplt
->relocation
;
13714 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13716 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
13717 if (p
->addend
!= 0)
13720 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
13722 size
+= sizeof ("+0x") - 1 + 8;
13727 s
= *ret
= (asymbol
*) bfd_malloc (size
);
13731 names
= (char *) (s
+ count
);
13732 p
= relplt
->relocation
;
13734 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13739 addr
= bed
->plt_sym_val (i
, plt
, p
);
13740 if (addr
== (bfd_vma
) -1)
13743 *s
= **p
->sym_ptr_ptr
;
13744 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13745 we are defining a symbol, ensure one of them is set. */
13746 if ((s
->flags
& BSF_LOCAL
) == 0)
13747 s
->flags
|= BSF_GLOBAL
;
13748 s
->flags
|= BSF_SYNTHETIC
;
13750 s
->value
= addr
- plt
->vma
;
13753 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
13754 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
13756 if (p
->addend
!= 0)
13760 memcpy (names
, "+0x", sizeof ("+0x") - 1);
13761 names
+= sizeof ("+0x") - 1;
13762 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
13763 for (a
= buf
; *a
== '0'; ++a
)
13766 memcpy (names
, a
, len
);
13769 memcpy (names
, "@plt", sizeof ("@plt"));
13770 names
+= sizeof ("@plt");
13777 /* It is only used by x86-64 so far.
13778 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13779 but current usage would allow all of _bfd_std_section to be zero. */
13780 static const asymbol lcomm_sym
13781 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
13782 asection _bfd_elf_large_com_section
13783 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
13784 "LARGE_COMMON", 0, SEC_IS_COMMON
);
13787 _bfd_elf_final_write_processing (bfd
*abfd
)
13789 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
13791 i_ehdrp
= elf_elfheader (abfd
);
13793 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13794 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
13796 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13797 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13798 or STB_GNU_UNIQUE binding. */
13799 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
13801 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13802 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
13803 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
13804 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
13806 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
13807 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13808 "and FreeBSD targets"));
13809 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
13810 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13811 "only by GNU and FreeBSD targets"));
13812 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
13813 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13814 "only by GNU and FreeBSD targets"));
13815 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
13816 _bfd_error_handler (_("GNU_RETAIN section is supported "
13817 "only by GNU and FreeBSD targets"));
13818 bfd_set_error (bfd_error_sorry
);
13826 /* Return TRUE for ELF symbol types that represent functions.
13827 This is the default version of this function, which is sufficient for
13828 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13831 _bfd_elf_is_function_type (unsigned int type
)
13833 return (type
== STT_FUNC
13834 || type
== STT_GNU_IFUNC
);
13837 /* If the ELF symbol SYM might be a function in SEC, return the
13838 function size and set *CODE_OFF to the function's entry point,
13839 otherwise return zero. */
13842 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
13845 bfd_size_type size
;
13846 elf_symbol_type
* elf_sym
= (elf_symbol_type
*) sym
;
13848 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
13849 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
13850 || sym
->section
!= sec
)
13853 size
= (sym
->flags
& BSF_SYNTHETIC
) ? 0 : elf_sym
->internal_elf_sym
.st_size
;
13855 /* In theory we should check that the symbol's type satisfies
13856 _bfd_elf_is_function_type(), but there are some function-like
13857 symbols which would fail this test. (eg _start). Instead
13858 we check for hidden, local, notype symbols with zero size.
13859 This type of symbol is generated by the annobin plugin for gcc
13860 and clang, and should not be considered to be a function symbol. */
13862 && ((sym
->flags
& (BSF_SYNTHETIC
| BSF_LOCAL
)) == BSF_LOCAL
)
13863 && ELF_ST_TYPE (elf_sym
->internal_elf_sym
.st_info
) == STT_NOTYPE
13864 && ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
) == STV_HIDDEN
)
13867 *code_off
= sym
->value
;
13868 /* Do not return 0 for the function's size. */
13869 return size
? size
: 1;
13872 /* Set to non-zero to enable some debug messages. */
13873 #define DEBUG_SECONDARY_RELOCS 0
13875 /* An internal-to-the-bfd-library only section type
13876 used to indicate a cached secondary reloc section. */
13877 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13879 /* Create a BFD section to hold a secondary reloc section. */
13882 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
13883 Elf_Internal_Shdr
*hdr
,
13885 unsigned int shindex
)
13887 /* We only support RELA secondary relocs. */
13888 if (hdr
->sh_type
!= SHT_RELA
)
13891 #if DEBUG_SECONDARY_RELOCS
13892 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
13894 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
13895 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
13898 /* Read in any secondary relocs associated with SEC. */
13901 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
13903 asymbol
** symbols
,
13906 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
13908 bool result
= true;
13909 bfd_vma (*r_sym
) (bfd_vma
);
13910 ufile_ptr filesize
;
13912 #if BFD_DEFAULT_TARGET_SIZE > 32
13913 if (bfd_arch_bits_per_address (abfd
) != 32)
13914 r_sym
= elf64_r_sym
;
13917 r_sym
= elf32_r_sym
;
13919 if (!elf_section_data (sec
)->has_secondary_relocs
)
13922 /* Discover if there are any secondary reloc sections
13923 associated with SEC. */
13924 filesize
= bfd_get_file_size (abfd
);
13925 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
13927 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
13929 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
13930 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
13931 && (hdr
->sh_entsize
== ebd
->s
->sizeof_rel
13932 || hdr
->sh_entsize
== ebd
->s
->sizeof_rela
))
13934 bfd_byte
* native_relocs
;
13935 bfd_byte
* native_reloc
;
13936 arelent
* internal_relocs
;
13937 arelent
* internal_reloc
;
13939 unsigned int entsize
;
13940 unsigned int symcount
;
13941 bfd_size_type reloc_count
;
13944 if (ebd
->elf_info_to_howto
== NULL
)
13947 #if DEBUG_SECONDARY_RELOCS
13948 fprintf (stderr
, "read secondary relocs for %s from %s\n",
13949 sec
->name
, relsec
->name
);
13951 entsize
= hdr
->sh_entsize
;
13954 && ((ufile_ptr
) hdr
->sh_offset
> filesize
13955 || hdr
->sh_size
> filesize
- hdr
->sh_offset
))
13957 bfd_set_error (bfd_error_file_truncated
);
13962 native_relocs
= bfd_malloc (hdr
->sh_size
);
13963 if (native_relocs
== NULL
)
13969 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
13970 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
13972 free (native_relocs
);
13973 bfd_set_error (bfd_error_file_too_big
);
13978 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
13979 if (internal_relocs
== NULL
)
13981 free (native_relocs
);
13986 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
13987 || bfd_read (native_relocs
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
13989 free (native_relocs
);
13990 /* The internal_relocs will be freed when
13991 the memory for the bfd is released. */
13997 symcount
= bfd_get_dynamic_symcount (abfd
);
13999 symcount
= bfd_get_symcount (abfd
);
14001 for (i
= 0, internal_reloc
= internal_relocs
,
14002 native_reloc
= native_relocs
;
14004 i
++, internal_reloc
++, native_reloc
+= entsize
)
14007 Elf_Internal_Rela rela
;
14009 if (entsize
== ebd
->s
->sizeof_rel
)
14010 ebd
->s
->swap_reloc_in (abfd
, native_reloc
, & rela
);
14011 else /* entsize == ebd->s->sizeof_rela */
14012 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
14014 /* The address of an ELF reloc is section relative for an object
14015 file, and absolute for an executable file or shared library.
14016 The address of a normal BFD reloc is always section relative,
14017 and the address of a dynamic reloc is absolute.. */
14018 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
14019 internal_reloc
->address
= rela
.r_offset
;
14021 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
14023 if (r_sym (rela
.r_info
) == STN_UNDEF
)
14025 /* FIXME: This and the error case below mean that we
14026 have a symbol on relocs that is not elf_symbol_type. */
14027 internal_reloc
->sym_ptr_ptr
=
14028 bfd_abs_section_ptr
->symbol_ptr_ptr
;
14030 else if (r_sym (rela
.r_info
) > symcount
)
14033 /* xgettext:c-format */
14034 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
14035 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
14036 bfd_set_error (bfd_error_bad_value
);
14037 internal_reloc
->sym_ptr_ptr
=
14038 bfd_abs_section_ptr
->symbol_ptr_ptr
;
14045 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
14046 internal_reloc
->sym_ptr_ptr
= ps
;
14047 /* Make sure that this symbol is not removed by strip. */
14048 (*ps
)->flags
|= BSF_KEEP
;
14051 internal_reloc
->addend
= rela
.r_addend
;
14053 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
14054 if (! res
|| internal_reloc
->howto
== NULL
)
14056 #if DEBUG_SECONDARY_RELOCS
14058 "there is no howto associated with reloc %lx\n",
14065 free (native_relocs
);
14066 /* Store the internal relocs. */
14067 elf_section_data (relsec
)->sec_info
= internal_relocs
;
14074 /* Set the ELF section header fields of an output secondary reloc section. */
14077 _bfd_elf_copy_special_section_fields (const bfd
*ibfd ATTRIBUTE_UNUSED
,
14078 bfd
*obfd ATTRIBUTE_UNUSED
,
14079 const Elf_Internal_Shdr
*isection
,
14080 Elf_Internal_Shdr
*osection
)
14084 struct bfd_elf_section_data
* esd
;
14086 if (isection
== NULL
)
14089 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
14092 isec
= isection
->bfd_section
;
14096 osec
= osection
->bfd_section
;
14100 esd
= elf_section_data (osec
);
14101 BFD_ASSERT (esd
->sec_info
== NULL
);
14102 esd
->sec_info
= elf_section_data (isec
)->sec_info
;
14103 osection
->sh_type
= SHT_RELA
;
14104 osection
->sh_link
= elf_onesymtab (obfd
);
14105 if (osection
->sh_link
== 0)
14107 /* There is no symbol table - we are hosed... */
14109 /* xgettext:c-format */
14110 (_("%pB(%pA): link section cannot be set"
14111 " because the output file does not have a symbol table"),
14113 bfd_set_error (bfd_error_bad_value
);
14117 /* Find the output section that corresponds to the isection's
14119 if (isection
->sh_info
== 0
14120 || isection
->sh_info
>= elf_numsections (ibfd
))
14123 /* xgettext:c-format */
14124 (_("%pB(%pA): info section index is invalid"),
14126 bfd_set_error (bfd_error_bad_value
);
14130 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
14132 if (isection
== NULL
14133 || isection
->bfd_section
== NULL
14134 || isection
->bfd_section
->output_section
== NULL
)
14137 /* xgettext:c-format */
14138 (_("%pB(%pA): info section index cannot be set"
14139 " because the section is not in the output"),
14141 bfd_set_error (bfd_error_bad_value
);
14145 esd
= elf_section_data (isection
->bfd_section
->output_section
);
14146 BFD_ASSERT (esd
!= NULL
);
14147 osection
->sh_info
= esd
->this_idx
;
14148 esd
->has_secondary_relocs
= true;
14149 #if DEBUG_SECONDARY_RELOCS
14150 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
14151 osec
->name
, osection
->sh_link
, osection
->sh_info
);
14152 fprintf (stderr
, "mark section %s as having secondary relocs\n",
14153 bfd_section_name (isection
->bfd_section
->output_section
));
14159 /* Write out a secondary reloc section.
14161 FIXME: Currently this function can result in a serious performance penalty
14162 for files with secondary relocs and lots of sections. The proper way to
14163 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14164 relocs together and then to have this function just walk that chain. */
14167 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
14169 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
14170 bfd_vma addr_offset
;
14172 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
14173 bool result
= true;
14178 #if BFD_DEFAULT_TARGET_SIZE > 32
14179 if (bfd_arch_bits_per_address (abfd
) != 32)
14180 r_info
= elf64_r_info
;
14183 r_info
= elf32_r_info
;
14185 /* The address of an ELF reloc is section relative for an object
14186 file, and absolute for an executable file or shared library.
14187 The address of a BFD reloc is always section relative. */
14189 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
14190 addr_offset
= sec
->vma
;
14192 /* Discover if there are any secondary reloc sections
14193 associated with SEC. */
14194 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
14196 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
14197 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
14199 if (hdr
->sh_type
== SHT_RELA
14200 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
14202 asymbol
* last_sym
;
14204 size_t reloc_count
;
14206 bfd_size_type entsize
;
14207 arelent
* src_irel
;
14208 bfd_byte
* dst_rela
;
14210 if (hdr
->contents
!= NULL
)
14213 /* xgettext:c-format */
14214 (_("%pB(%pA): error: secondary reloc section processed twice"),
14216 bfd_set_error (bfd_error_bad_value
);
14221 entsize
= hdr
->sh_entsize
;
14225 /* xgettext:c-format */
14226 (_("%pB(%pA): error: secondary reloc section"
14227 " has zero sized entries"),
14229 bfd_set_error (bfd_error_bad_value
);
14233 else if (entsize
!= ebd
->s
->sizeof_rel
14234 && entsize
!= ebd
->s
->sizeof_rela
)
14237 /* xgettext:c-format */
14238 (_("%pB(%pA): error: secondary reloc section"
14239 " has non-standard sized entries"),
14241 bfd_set_error (bfd_error_bad_value
);
14246 reloc_count
= hdr
->sh_size
/ entsize
;
14247 hdr
->sh_size
= entsize
* reloc_count
;
14248 if (reloc_count
== 0)
14251 /* xgettext:c-format */
14252 (_("%pB(%pA): error: secondary reloc section is empty!"),
14254 bfd_set_error (bfd_error_bad_value
);
14259 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
14260 if (hdr
->contents
== NULL
)
14263 #if DEBUG_SECONDARY_RELOCS
14264 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
14265 reloc_count
, sec
->name
, relsec
->name
);
14269 dst_rela
= hdr
->contents
;
14270 src_irel
= (arelent
*) esd
->sec_info
;
14271 if (src_irel
== NULL
)
14274 /* xgettext:c-format */
14275 (_("%pB(%pA): error: internal relocs missing"
14276 " for secondary reloc section"),
14278 bfd_set_error (bfd_error_bad_value
);
14283 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= entsize
)
14285 Elf_Internal_Rela src_rela
;
14290 ptr
= src_irel
+ idx
;
14294 /* xgettext:c-format */
14295 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14296 abfd
, relsec
, idx
);
14297 bfd_set_error (bfd_error_bad_value
);
14302 if (ptr
->sym_ptr_ptr
== NULL
)
14304 /* FIXME: Is this an error ? */
14309 sym
= *ptr
->sym_ptr_ptr
;
14311 if (sym
== last_sym
)
14315 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
14319 /* xgettext:c-format */
14320 (_("%pB(%pA): error: secondary reloc %zu"
14321 " references a missing symbol"),
14322 abfd
, relsec
, idx
);
14323 bfd_set_error (bfd_error_bad_value
);
14332 if (sym
->the_bfd
!= NULL
14333 && sym
->the_bfd
->xvec
!= abfd
->xvec
14334 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
14337 /* xgettext:c-format */
14338 (_("%pB(%pA): error: secondary reloc %zu"
14339 " references a deleted symbol"),
14340 abfd
, relsec
, idx
);
14341 bfd_set_error (bfd_error_bad_value
);
14347 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
14348 if (ptr
->howto
== NULL
)
14351 /* xgettext:c-format */
14352 (_("%pB(%pA): error: secondary reloc %zu"
14353 " is of an unknown type"),
14354 abfd
, relsec
, idx
);
14355 bfd_set_error (bfd_error_bad_value
);
14357 src_rela
.r_info
= r_info (0, 0);
14360 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
14361 src_rela
.r_addend
= ptr
->addend
;
14363 if (entsize
== ebd
->s
->sizeof_rel
)
14364 ebd
->s
->swap_reloc_out (abfd
, &src_rela
, dst_rela
);
14365 else /* entsize == ebd->s->sizeof_rela */
14366 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);
14374 /* Mmap in section contents. If FINAL_LINK is false, set *BUF to NULL
14375 before calling bfd_get_full_section_contents. */
14378 elf_mmap_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**buf
,
14382 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14384 && sec
->compress_status
== COMPRESS_SECTION_NONE
14385 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14387 /* Use mmap only if section size >= the minimum mmap section
14389 size_t readsz
= bfd_get_section_limit_octets (abfd
, sec
);
14390 size_t allocsz
= bfd_get_section_alloc_size (abfd
, sec
);
14391 if (readsz
== allocsz
&& readsz
>= _bfd_minimum_mmap_size
)
14393 if (sec
->contents
!= NULL
)
14395 if (!sec
->mmapped_p
)
14397 *buf
= sec
->contents
;
14400 if (sec
->mmapped_p
)
14402 sec
->mmapped_p
= 1;
14404 /* Never use the preallocated buffer if mmapp is used. */
14409 /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is
14410 true. If FINAL_LINK is false, *BUF is set to the preallocated
14411 buffer if USE_MMAP is undefined and *BUF is set to NULL if
14412 USE_MMAP is defined. */
14415 bool ret
= bfd_get_full_section_contents (abfd
, sec
, buf
);
14416 if (ret
&& sec
->mmapped_p
)
14417 *buf
= sec
->contents
;
14421 /* Mmap in section contents. */
14424 _bfd_elf_mmap_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**buf
)
14426 return elf_mmap_section_contents (abfd
, sec
, buf
, false);
14429 /* Mmap in the full section contents for the final link. */
14432 _bfd_elf_link_mmap_section_contents (bfd
*abfd
, sec_ptr sec
,
14435 return elf_mmap_section_contents (abfd
, sec
, buf
, true);
14438 /* Munmap section contents. */
14441 _bfd_elf_munmap_section_contents (asection
*sec ATTRIBUTE_UNUSED
,
14444 /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14445 CONTENTS may be NULL. */
14446 if (contents
== NULL
)
14450 if (sec
->mmapped_p
)
14452 /* _bfd_elf_mmap_section_contents may return the previously
14453 mapped section contents. Munmap the section contents only
14454 if they haven't been cached. */
14455 if (elf_section_data (sec
)->this_hdr
.contents
== contents
)
14458 /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14459 malloced, CONTENTS_ADDR is set to NULL. */
14460 if (elf_section_data (sec
)->contents_addr
!= NULL
)
14462 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14463 if (munmap (elf_section_data (sec
)->contents_addr
,
14464 elf_section_data (sec
)->contents_size
) != 0)
14466 sec
->mmapped_p
= 0;
14467 sec
->contents
= NULL
;
14468 elf_section_data (sec
)->contents_addr
= NULL
;
14469 elf_section_data (sec
)->contents_size
= 0;
14478 /* Munmap the full section contents for the final link. */
14481 _bfd_elf_link_munmap_section_contents (asection
*sec ATTRIBUTE_UNUSED
)
14484 if (sec
->mmapped_p
&& elf_section_data (sec
)->contents_addr
!= NULL
)
14486 /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14487 malloced, CONTENTS_ADDR is set to NULL. */
14488 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14489 if (munmap (elf_section_data (sec
)->contents_addr
,
14490 elf_section_data (sec
)->contents_size
) != 0)
14492 sec
->mmapped_p
= 0;
14493 sec
->contents
= NULL
;
14494 elf_section_data (sec
)->contents_addr
= NULL
;
14495 elf_section_data (sec
)->contents_size
= 0;