1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2022 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
)
199 const unsigned char *name
= (const unsigned char *) namearg
;
204 while ((ch
= *name
++) != '\0')
207 if ((g
= (h
& 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h
& 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
225 unsigned long h
= 5381;
228 while ((ch
= *name
++) != '\0')
229 h
= (h
<< 5) + h
+ ch
;
230 return h
& 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd
*abfd
,
238 enum elf_target_id object_id
)
240 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
241 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
242 if (abfd
->tdata
.any
== NULL
)
245 elf_object_id (abfd
) = object_id
;
246 if (abfd
->direction
!= read_direction
)
248 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
251 elf_tdata (abfd
)->o
= o
;
252 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
259 bfd_elf_make_object (bfd
*abfd
)
261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
262 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
267 bfd_elf_mkcorefile (bfd
*abfd
)
269 /* I think this can be done just like an object file. */
270 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
272 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
273 return elf_tdata (abfd
)->core
!= NULL
;
277 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
279 Elf_Internal_Shdr
**i_shdrp
;
280 bfd_byte
*shstrtab
= NULL
;
282 bfd_size_type shstrtabsize
;
284 i_shdrp
= elf_elfsections (abfd
);
286 || shindex
>= elf_numsections (abfd
)
287 || i_shdrp
[shindex
] == 0)
290 shstrtab
= i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize
+ 1 <= 1
300 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
301 || (shstrtab
= _bfd_alloc_and_read (abfd
, shstrtabsize
+ 1,
302 shstrtabsize
)) == NULL
)
304 /* Once we've failed to read it, make sure we don't keep
305 trying. Otherwise, we'll keep allocating space for
306 the string table over and over. */
307 i_shdrp
[shindex
]->sh_size
= 0;
310 shstrtab
[shstrtabsize
] = '\0';
311 i_shdrp
[shindex
]->contents
= shstrtab
;
313 return (char *) shstrtab
;
317 bfd_elf_string_from_elf_section (bfd
*abfd
,
318 unsigned int shindex
,
319 unsigned int strindex
)
321 Elf_Internal_Shdr
*hdr
;
326 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
329 hdr
= elf_elfsections (abfd
)[shindex
];
331 if (hdr
->contents
== NULL
)
333 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
335 /* PR 17512: file: f057ec89. */
336 /* xgettext:c-format */
337 _bfd_error_handler (_("%pB: attempt to load strings from"
338 " a non-string section (number %d)"),
343 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
348 /* PR 24273: The string section's contents may have already
349 been loaded elsewhere, eg because a corrupt file has the
350 string section index in the ELF header pointing at a group
351 section. So be paranoid, and test that the last byte of
352 the section is zero. */
353 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
357 if (strindex
>= hdr
->sh_size
)
359 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
361 /* xgettext:c-format */
362 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
363 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
364 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
366 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
370 return ((char *) hdr
->contents
) + strindex
;
373 /* Read and convert symbols to internal format.
374 SYMCOUNT specifies the number of symbols to read, starting from
375 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
376 are non-NULL, they are used to store the internal symbols, external
377 symbols, and symbol section index extensions, respectively.
378 Returns a pointer to the internal symbol buffer (malloced if necessary)
379 or NULL if there were no symbols or some kind of problem. */
382 bfd_elf_get_elf_syms (bfd
*ibfd
,
383 Elf_Internal_Shdr
*symtab_hdr
,
386 Elf_Internal_Sym
*intsym_buf
,
388 Elf_External_Sym_Shndx
*extshndx_buf
)
390 Elf_Internal_Shdr
*shndx_hdr
;
392 const bfd_byte
*esym
;
393 Elf_External_Sym_Shndx
*alloc_extshndx
;
394 Elf_External_Sym_Shndx
*shndx
;
395 Elf_Internal_Sym
*alloc_intsym
;
396 Elf_Internal_Sym
*isym
;
397 Elf_Internal_Sym
*isymend
;
398 const struct elf_backend_data
*bed
;
403 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
409 /* Normal syms might have section extension entries. */
411 if (elf_symtab_shndx_list (ibfd
) != NULL
)
413 elf_section_list
* entry
;
414 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
416 /* Find an index section that is linked to this symtab section. */
417 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
420 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
423 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
425 shndx_hdr
= & entry
->hdr
;
430 if (shndx_hdr
== NULL
)
432 if (symtab_hdr
== &elf_symtab_hdr (ibfd
))
433 /* Not really accurate, but this was how the old code used
435 shndx_hdr
= &elf_symtab_shndx_list (ibfd
)->hdr
;
436 /* Otherwise we do nothing. The assumption is that
437 the index table will not be needed. */
441 /* Read the symbols. */
443 alloc_extshndx
= NULL
;
445 bed
= get_elf_backend_data (ibfd
);
446 extsym_size
= bed
->s
->sizeof_sym
;
447 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
449 bfd_set_error (bfd_error_file_too_big
);
453 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
454 if (extsym_buf
== NULL
)
456 alloc_ext
= bfd_malloc (amt
);
457 extsym_buf
= alloc_ext
;
459 if (extsym_buf
== NULL
460 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
461 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
467 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
471 if (_bfd_mul_overflow (symcount
, sizeof (Elf_External_Sym_Shndx
), &amt
))
473 bfd_set_error (bfd_error_file_too_big
);
477 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
478 if (extshndx_buf
== NULL
)
480 alloc_extshndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
481 extshndx_buf
= alloc_extshndx
;
483 if (extshndx_buf
== NULL
484 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
485 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
492 if (intsym_buf
== NULL
)
494 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
496 bfd_set_error (bfd_error_file_too_big
);
499 alloc_intsym
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
500 intsym_buf
= alloc_intsym
;
501 if (intsym_buf
== NULL
)
505 /* Convert the symbols to internal form. */
506 isymend
= intsym_buf
+ symcount
;
507 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
508 shndx
= extshndx_buf
;
510 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
511 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
513 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
514 /* xgettext:c-format */
515 _bfd_error_handler (_("%pB symbol number %lu references"
516 " nonexistent SHT_SYMTAB_SHNDX section"),
517 ibfd
, (unsigned long) symoffset
);
525 free (alloc_extshndx
);
530 /* Look up a symbol name. */
532 bfd_elf_sym_name (bfd
*abfd
,
533 Elf_Internal_Shdr
*symtab_hdr
,
534 Elf_Internal_Sym
*isym
,
538 unsigned int iname
= isym
->st_name
;
539 unsigned int shindex
= symtab_hdr
->sh_link
;
541 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
542 /* Check for a bogus st_shndx to avoid crashing. */
543 && isym
->st_shndx
< elf_numsections (abfd
))
545 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
546 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
549 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
552 else if (sym_sec
&& *name
== '\0')
553 name
= bfd_section_name (sym_sec
);
558 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
559 sections. The first element is the flags, the rest are section
562 typedef union elf_internal_group
{
563 Elf_Internal_Shdr
*shdr
;
565 } Elf_Internal_Group
;
567 /* Return the name of the group signature symbol. Why isn't the
568 signature just a string? */
571 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
573 Elf_Internal_Shdr
*hdr
;
574 unsigned char esym
[sizeof (Elf64_External_Sym
)];
575 Elf_External_Sym_Shndx eshndx
;
576 Elf_Internal_Sym isym
;
578 /* First we need to ensure the symbol table is available. Make sure
579 that it is a symbol table section. */
580 if (ghdr
->sh_link
>= elf_numsections (abfd
))
582 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
583 if (hdr
->sh_type
!= SHT_SYMTAB
584 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
587 /* Go read the symbol. */
588 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
589 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
590 &isym
, esym
, &eshndx
) == NULL
)
593 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
596 /* Set next_in_group list pointer, and group name for NEWSECT. */
599 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
601 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
603 /* If num_group is zero, read in all SHT_GROUP sections. The count
604 is set to -1 if there are no SHT_GROUP sections. */
607 unsigned int i
, shnum
;
609 /* First count the number of groups. If we have a SHT_GROUP
610 section with just a flag word (ie. sh_size is 4), ignore it. */
611 shnum
= elf_numsections (abfd
);
614 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
615 ( (shdr)->sh_type == SHT_GROUP \
616 && (shdr)->sh_size >= minsize \
617 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
618 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
620 for (i
= 0; i
< shnum
; i
++)
622 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
624 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
630 num_group
= (unsigned) -1;
631 elf_tdata (abfd
)->num_group
= num_group
;
632 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
636 /* We keep a list of elf section headers for group sections,
637 so we can find them quickly. */
640 elf_tdata (abfd
)->num_group
= num_group
;
641 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
642 elf_tdata (abfd
)->group_sect_ptr
643 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
644 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
648 for (i
= 0; i
< shnum
; i
++)
650 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
652 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
655 Elf_Internal_Group
*dest
;
657 /* Make sure the group section has a BFD section
659 if (!bfd_section_from_shdr (abfd
, i
))
662 /* Add to list of sections. */
663 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
666 /* Read the raw contents. */
667 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
668 shdr
->contents
= NULL
;
669 if (_bfd_mul_overflow (shdr
->sh_size
,
670 sizeof (*dest
) / 4, &amt
)
671 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
673 = _bfd_alloc_and_read (abfd
, amt
, shdr
->sh_size
)))
676 /* xgettext:c-format */
677 (_("%pB: invalid size field in group section"
678 " header: %#" PRIx64
""),
679 abfd
, (uint64_t) shdr
->sh_size
);
680 bfd_set_error (bfd_error_bad_value
);
685 /* Translate raw contents, a flag word followed by an
686 array of elf section indices all in target byte order,
687 to the flag word followed by an array of elf section
689 src
= shdr
->contents
+ shdr
->sh_size
;
690 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
698 idx
= H_GET_32 (abfd
, src
);
699 if (src
== shdr
->contents
)
703 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
704 shdr
->bfd_section
->flags
705 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
710 dest
->shdr
= elf_elfsections (abfd
)[idx
];
711 /* PR binutils/23199: All sections in a
712 section group should be marked with
713 SHF_GROUP. But some tools generate
714 broken objects without SHF_GROUP. Fix
716 dest
->shdr
->sh_flags
|= SHF_GROUP
;
719 || dest
->shdr
->sh_type
== SHT_GROUP
)
722 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
730 /* PR 17510: Corrupt binaries might contain invalid groups. */
731 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
733 elf_tdata (abfd
)->num_group
= num_group
;
735 /* If all groups are invalid then fail. */
738 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
739 elf_tdata (abfd
)->num_group
= num_group
= -1;
741 (_("%pB: no valid group sections found"), abfd
);
742 bfd_set_error (bfd_error_bad_value
);
748 if (num_group
!= (unsigned) -1)
750 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
753 for (j
= 0; j
< num_group
; j
++)
755 /* Begin search from previous found group. */
756 unsigned i
= (j
+ search_offset
) % num_group
;
758 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
759 Elf_Internal_Group
*idx
;
765 idx
= (Elf_Internal_Group
*) shdr
->contents
;
766 if (idx
== NULL
|| shdr
->sh_size
< 4)
768 /* See PR 21957 for a reproducer. */
769 /* xgettext:c-format */
770 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
771 abfd
, shdr
->bfd_section
);
772 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
773 bfd_set_error (bfd_error_bad_value
);
776 n_elt
= shdr
->sh_size
/ 4;
778 /* Look through this group's sections to see if current
779 section is a member. */
781 if ((++idx
)->shdr
== hdr
)
785 /* We are a member of this group. Go looking through
786 other members to see if any others are linked via
788 idx
= (Elf_Internal_Group
*) shdr
->contents
;
789 n_elt
= shdr
->sh_size
/ 4;
791 if ((++idx
)->shdr
!= NULL
792 && (s
= idx
->shdr
->bfd_section
) != NULL
793 && elf_next_in_group (s
) != NULL
)
797 /* Snarf the group name from other member, and
798 insert current section in circular list. */
799 elf_group_name (newsect
) = elf_group_name (s
);
800 elf_next_in_group (newsect
) = elf_next_in_group (s
);
801 elf_next_in_group (s
) = newsect
;
807 gname
= group_signature (abfd
, shdr
);
810 elf_group_name (newsect
) = gname
;
812 /* Start a circular list with one element. */
813 elf_next_in_group (newsect
) = newsect
;
816 /* If the group section has been created, point to the
818 if (shdr
->bfd_section
!= NULL
)
819 elf_next_in_group (shdr
->bfd_section
) = newsect
;
821 elf_tdata (abfd
)->group_search_offset
= i
;
828 if (elf_group_name (newsect
) == NULL
)
830 /* xgettext:c-format */
831 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
833 /* PR 29532: Return true here, even though the group info has not been
834 read. Separate debug info files can have empty group sections, but
835 we do not want this to prevent them from being loaded as otherwise
836 GDB will not be able to use them. */
843 _bfd_elf_setup_sections (bfd
*abfd
)
846 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
850 /* Process SHF_LINK_ORDER. */
851 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
853 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
854 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
856 unsigned int elfsec
= this_hdr
->sh_link
;
857 /* An sh_link value of 0 is now allowed. It indicates that linked
858 to section has already been discarded, but that the current
859 section has been retained for some other reason. This linking
860 section is still a candidate for later garbage collection
864 elf_linked_to_section (s
) = NULL
;
868 asection
*linksec
= NULL
;
870 if (elfsec
< elf_numsections (abfd
))
872 this_hdr
= elf_elfsections (abfd
)[elfsec
];
873 linksec
= this_hdr
->bfd_section
;
877 Some strip/objcopy may leave an incorrect value in
878 sh_link. We don't want to proceed. */
882 /* xgettext:c-format */
883 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
884 s
->owner
, elfsec
, s
);
888 elf_linked_to_section (s
) = linksec
;
891 else if (this_hdr
->sh_type
== SHT_GROUP
892 && elf_next_in_group (s
) == NULL
)
895 /* xgettext:c-format */
896 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
897 abfd
, elf_section_data (s
)->this_idx
);
902 /* Process section groups. */
903 if (num_group
== (unsigned) -1)
906 for (i
= 0; i
< num_group
; i
++)
908 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
909 Elf_Internal_Group
*idx
;
912 /* PR binutils/18758: Beware of corrupt binaries with invalid
914 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
917 /* xgettext:c-format */
918 (_("%pB: section group entry number %u is corrupt"),
924 idx
= (Elf_Internal_Group
*) shdr
->contents
;
925 n_elt
= shdr
->sh_size
/ 4;
931 if (idx
->shdr
== NULL
)
933 else if (idx
->shdr
->bfd_section
)
934 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
935 else if (idx
->shdr
->sh_type
!= SHT_RELA
936 && idx
->shdr
->sh_type
!= SHT_REL
)
938 /* There are some unknown sections in the group. */
940 /* xgettext:c-format */
941 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
944 bfd_elf_string_from_elf_section (abfd
,
945 (elf_elfheader (abfd
)
958 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
960 return elf_next_in_group (sec
) != NULL
;
964 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
966 if (elf_sec_group (sec
) != NULL
)
967 return elf_group_name (sec
);
972 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
974 unsigned int len
= strlen (name
);
975 char *new_name
= bfd_alloc (abfd
, len
+ 2);
976 if (new_name
== NULL
)
980 memcpy (new_name
+ 2, name
+ 1, len
);
985 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
987 unsigned int len
= strlen (name
);
988 char *new_name
= bfd_alloc (abfd
, len
);
989 if (new_name
== NULL
)
992 memcpy (new_name
+ 1, name
+ 2, len
- 1);
996 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
1000 int16_t major_version
;
1001 int16_t minor_version
;
1002 unsigned char slim_object
;
1004 /* Flags is a private field that is not defined publicly. */
1008 /* Make a BFD section from an ELF section. We store a pointer to the
1009 BFD section in the bfd_section field of the header. */
1012 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1013 Elf_Internal_Shdr
*hdr
,
1019 const struct elf_backend_data
*bed
;
1020 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
1022 if (hdr
->bfd_section
!= NULL
)
1025 newsect
= bfd_make_section_anyway (abfd
, name
);
1026 if (newsect
== NULL
)
1029 hdr
->bfd_section
= newsect
;
1030 elf_section_data (newsect
)->this_hdr
= *hdr
;
1031 elf_section_data (newsect
)->this_idx
= shindex
;
1033 /* Always use the real type/flags. */
1034 elf_section_type (newsect
) = hdr
->sh_type
;
1035 elf_section_flags (newsect
) = hdr
->sh_flags
;
1037 newsect
->filepos
= hdr
->sh_offset
;
1039 flags
= SEC_NO_FLAGS
;
1040 if (hdr
->sh_type
!= SHT_NOBITS
)
1041 flags
|= SEC_HAS_CONTENTS
;
1042 if (hdr
->sh_type
== SHT_GROUP
)
1044 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1047 if (hdr
->sh_type
!= SHT_NOBITS
)
1050 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1051 flags
|= SEC_READONLY
;
1052 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1054 else if ((flags
& SEC_LOAD
) != 0)
1056 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1059 newsect
->entsize
= hdr
->sh_entsize
;
1061 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1062 flags
|= SEC_STRINGS
;
1063 if (hdr
->sh_flags
& SHF_GROUP
)
1064 if (!setup_group (abfd
, hdr
, newsect
))
1066 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1067 flags
|= SEC_THREAD_LOCAL
;
1068 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1069 flags
|= SEC_EXCLUDE
;
1071 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1073 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1074 but binutils as of 2019-07-23 did not set the EI_OSABI header
1077 case ELFOSABI_FREEBSD
:
1078 if ((hdr
->sh_flags
& SHF_GNU_RETAIN
) != 0)
1079 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_retain
;
1082 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1083 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1087 if ((flags
& SEC_ALLOC
) == 0)
1089 /* The debugging sections appear to be recognized only by name,
1090 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1091 if (name
[0] == '.')
1093 if (startswith (name
, ".debug")
1094 || startswith (name
, ".gnu.debuglto_.debug_")
1095 || startswith (name
, ".gnu.linkonce.wi.")
1096 || startswith (name
, ".zdebug"))
1097 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1098 else if (startswith (name
, GNU_BUILD_ATTRS_SECTION_NAME
)
1099 || startswith (name
, ".note.gnu"))
1101 flags
|= SEC_ELF_OCTETS
;
1104 else if (startswith (name
, ".line")
1105 || startswith (name
, ".stab")
1106 || strcmp (name
, ".gdb_index") == 0)
1107 flags
|= SEC_DEBUGGING
;
1111 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1112 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1113 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
1114 & -hdr
->sh_addralign
)))
1117 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1118 only link a single copy of the section. This is used to support
1119 g++. g++ will emit each template expansion in its own section.
1120 The symbols will be defined as weak, so that multiple definitions
1121 are permitted. The GNU linker extension is to actually discard
1122 all but one of the sections. */
1123 if (startswith (name
, ".gnu.linkonce")
1124 && elf_next_in_group (newsect
) == NULL
)
1125 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1127 if (!bfd_set_section_flags (newsect
, flags
))
1130 bed
= get_elf_backend_data (abfd
);
1131 if (bed
->elf_backend_section_flags
)
1132 if (!bed
->elf_backend_section_flags (hdr
))
1135 /* We do not parse the PT_NOTE segments as we are interested even in the
1136 separate debug info files which may have the segments offsets corrupted.
1137 PT_NOTEs from the core files are currently not parsed using BFD. */
1138 if (hdr
->sh_type
== SHT_NOTE
)
1142 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1145 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1146 hdr
->sh_offset
, hdr
->sh_addralign
);
1150 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1152 Elf_Internal_Phdr
*phdr
;
1153 unsigned int i
, nload
;
1155 /* Some ELF linkers produce binaries with all the program header
1156 p_paddr fields zero. If we have such a binary with more than
1157 one PT_LOAD header, then leave the section lma equal to vma
1158 so that we don't create sections with overlapping lma. */
1159 phdr
= elf_tdata (abfd
)->phdr
;
1160 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1161 if (phdr
->p_paddr
!= 0)
1163 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1165 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1168 phdr
= elf_tdata (abfd
)->phdr
;
1169 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1171 if (((phdr
->p_type
== PT_LOAD
1172 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1173 || phdr
->p_type
== PT_TLS
)
1174 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1176 if ((newsect
->flags
& SEC_LOAD
) == 0)
1177 newsect
->lma
= (phdr
->p_paddr
1178 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1180 /* We used to use the same adjustment for SEC_LOAD
1181 sections, but that doesn't work if the segment
1182 is packed with code from multiple VMAs.
1183 Instead we calculate the section LMA based on
1184 the segment LMA. It is assumed that the
1185 segment will contain sections with contiguous
1186 LMAs, even if the VMAs are not. */
1187 newsect
->lma
= (phdr
->p_paddr
1188 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1190 /* With contiguous segments, we can't tell from file
1191 offsets whether a section with zero size should
1192 be placed at the end of one segment or the
1193 beginning of the next. Decide based on vaddr. */
1194 if (hdr
->sh_addr
>= phdr
->p_vaddr
1195 && (hdr
->sh_addr
+ hdr
->sh_size
1196 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1202 /* Compress/decompress DWARF debug sections with names: .debug_* and
1203 .zdebug_*, after the section flags is set. */
1204 if ((newsect
->flags
& SEC_DEBUGGING
) != 0
1205 && (newsect
->flags
& SEC_HAS_CONTENTS
) != 0
1206 && ((name
[1] == 'd' && name
[6] == '_')
1207 || (name
[1] == 'z' && name
[7] == '_')))
1209 enum { nothing
, compress
, decompress
} action
= nothing
;
1210 int compression_header_size
;
1211 bfd_size_type uncompressed_size
;
1212 unsigned int uncompressed_align_power
;
1214 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1215 &compression_header_size
,
1217 &uncompressed_align_power
);
1220 /* Compressed section. Check if we should decompress. */
1221 if ((abfd
->flags
& BFD_DECOMPRESS
))
1222 action
= decompress
;
1225 /* Compress the uncompressed section or convert from/to .zdebug*
1226 section. Check if we should compress. */
1227 if (action
== nothing
)
1229 if (newsect
->size
!= 0
1230 && (abfd
->flags
& BFD_COMPRESS
)
1231 && compression_header_size
>= 0
1232 && uncompressed_size
> 0
1234 || ((compression_header_size
> 0)
1235 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1241 if (action
== compress
)
1243 if (!bfd_init_section_compress_status (abfd
, newsect
))
1246 /* xgettext:c-format */
1247 (_("%pB: unable to initialize compress status for section %s"),
1254 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1257 /* xgettext:c-format */
1258 (_("%pB: unable to initialize decompress status"
1264 if (newsect
->compress_status
== DECOMPRESS_SECTION_ZSTD
)
1267 /* xgettext:c-format */
1268 (_ ("%pB: section %s is compressed with zstd, but BFD "
1269 "is not built with zstd support"),
1271 newsect
->compress_status
= COMPRESS_SECTION_NONE
;
1277 if (abfd
->is_linker_input
)
1280 && (action
== decompress
1281 || (action
== compress
1282 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1284 /* Convert section name from .zdebug_* to .debug_* so
1285 that linker will consider this section as a debug
1287 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1288 if (new_name
== NULL
)
1290 bfd_rename_section (newsect
, new_name
);
1294 /* For objdump, don't rename the section. For objcopy, delay
1295 section rename to elf_fake_sections. */
1296 newsect
->flags
|= SEC_ELF_RENAME
;
1299 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1301 if (startswith (name
, ".gnu.lto_.lto."))
1303 struct lto_section lsection
;
1304 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1305 sizeof (struct lto_section
)))
1306 abfd
->lto_slim_object
= lsection
.slim_object
;
1312 const char *const bfd_elf_section_type_names
[] =
1314 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1315 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1316 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1319 /* ELF relocs are against symbols. If we are producing relocatable
1320 output, and the reloc is against an external symbol, and nothing
1321 has given us any additional addend, the resulting reloc will also
1322 be against the same symbol. In such a case, we don't want to
1323 change anything about the way the reloc is handled, since it will
1324 all be done at final link time. Rather than put special case code
1325 into bfd_perform_relocation, all the reloc types use this howto
1326 function, or should call this function for relocatable output. */
1328 bfd_reloc_status_type
1329 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1330 arelent
*reloc_entry
,
1332 void *data ATTRIBUTE_UNUSED
,
1333 asection
*input_section
,
1335 char **error_message ATTRIBUTE_UNUSED
)
1337 if (output_bfd
!= NULL
1338 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1339 && (! reloc_entry
->howto
->partial_inplace
1340 || reloc_entry
->addend
== 0))
1342 reloc_entry
->address
+= input_section
->output_offset
;
1343 return bfd_reloc_ok
;
1346 /* In some cases the relocation should be treated as output section
1347 relative, as when linking ELF DWARF into PE COFF. Many ELF
1348 targets lack section relative relocations and instead use
1349 ordinary absolute relocations for references between DWARF
1350 sections. That is arguably a bug in those targets but it happens
1351 to work for the usual case of linking to non-loaded ELF debug
1352 sections with VMAs forced to zero. PE COFF on the other hand
1353 doesn't allow a section VMA of zero. */
1354 if (output_bfd
== NULL
1355 && !reloc_entry
->howto
->pc_relative
1356 && (symbol
->section
->flags
& SEC_DEBUGGING
) != 0
1357 && (input_section
->flags
& SEC_DEBUGGING
) != 0)
1358 reloc_entry
->addend
-= symbol
->section
->output_section
->vma
;
1360 return bfd_reloc_continue
;
1363 /* Returns TRUE if section A matches section B.
1364 Names, addresses and links may be different, but everything else
1365 should be the same. */
1368 section_match (const Elf_Internal_Shdr
* a
,
1369 const Elf_Internal_Shdr
* b
)
1371 if (a
->sh_type
!= b
->sh_type
1372 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1373 || a
->sh_addralign
!= b
->sh_addralign
1374 || a
->sh_entsize
!= b
->sh_entsize
)
1376 if (a
->sh_type
== SHT_SYMTAB
1377 || a
->sh_type
== SHT_STRTAB
)
1379 return a
->sh_size
== b
->sh_size
;
1382 /* Find a section in OBFD that has the same characteristics
1383 as IHEADER. Return the index of this section or SHN_UNDEF if
1384 none can be found. Check's section HINT first, as this is likely
1385 to be the correct section. */
1388 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1389 const unsigned int hint
)
1391 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1394 BFD_ASSERT (iheader
!= NULL
);
1396 /* See PR 20922 for a reproducer of the NULL test. */
1397 if (hint
< elf_numsections (obfd
)
1398 && oheaders
[hint
] != NULL
1399 && section_match (oheaders
[hint
], iheader
))
1402 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1404 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1406 if (oheader
== NULL
)
1408 if (section_match (oheader
, iheader
))
1409 /* FIXME: Do we care if there is a potential for
1410 multiple matches ? */
1417 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1418 Processor specific section, based upon a matching input section.
1419 Returns TRUE upon success, FALSE otherwise. */
1422 copy_special_section_fields (const bfd
*ibfd
,
1424 const Elf_Internal_Shdr
*iheader
,
1425 Elf_Internal_Shdr
*oheader
,
1426 const unsigned int secnum
)
1428 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1429 const Elf_Internal_Shdr
**iheaders
1430 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1431 bool changed
= false;
1432 unsigned int sh_link
;
1434 if (oheader
->sh_type
== SHT_NOBITS
)
1436 /* This is a feature for objcopy --only-keep-debug:
1437 When a section's type is changed to NOBITS, we preserve
1438 the sh_link and sh_info fields so that they can be
1439 matched up with the original.
1441 Note: Strictly speaking these assignments are wrong.
1442 The sh_link and sh_info fields should point to the
1443 relevent sections in the output BFD, which may not be in
1444 the same location as they were in the input BFD. But
1445 the whole point of this action is to preserve the
1446 original values of the sh_link and sh_info fields, so
1447 that they can be matched up with the section headers in
1448 the original file. So strictly speaking we may be
1449 creating an invalid ELF file, but it is only for a file
1450 that just contains debug info and only for sections
1451 without any contents. */
1452 if (oheader
->sh_link
== 0)
1453 oheader
->sh_link
= iheader
->sh_link
;
1454 if (oheader
->sh_info
== 0)
1455 oheader
->sh_info
= iheader
->sh_info
;
1459 /* Allow the target a chance to decide how these fields should be set. */
1460 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1464 /* We have an iheader which might match oheader, and which has non-zero
1465 sh_info and/or sh_link fields. Attempt to follow those links and find
1466 the section in the output bfd which corresponds to the linked section
1467 in the input bfd. */
1468 if (iheader
->sh_link
!= SHN_UNDEF
)
1470 /* See PR 20931 for a reproducer. */
1471 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1474 /* xgettext:c-format */
1475 (_("%pB: invalid sh_link field (%d) in section number %d"),
1476 ibfd
, iheader
->sh_link
, secnum
);
1480 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1481 if (sh_link
!= SHN_UNDEF
)
1483 oheader
->sh_link
= sh_link
;
1487 /* FIXME: Should we install iheader->sh_link
1488 if we could not find a match ? */
1490 /* xgettext:c-format */
1491 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1494 if (iheader
->sh_info
)
1496 /* The sh_info field can hold arbitrary information, but if the
1497 SHF_LINK_INFO flag is set then it should be interpreted as a
1499 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1501 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1503 if (sh_link
!= SHN_UNDEF
)
1504 oheader
->sh_flags
|= SHF_INFO_LINK
;
1507 /* No idea what it means - just copy it. */
1508 sh_link
= iheader
->sh_info
;
1510 if (sh_link
!= SHN_UNDEF
)
1512 oheader
->sh_info
= sh_link
;
1517 /* xgettext:c-format */
1518 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1524 /* Copy the program header and other data from one object module to
1528 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1530 const Elf_Internal_Shdr
**iheaders
1531 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1532 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1533 const struct elf_backend_data
*bed
;
1536 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1537 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1540 if (!elf_flags_init (obfd
))
1542 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1543 elf_flags_init (obfd
) = true;
1546 elf_gp (obfd
) = elf_gp (ibfd
);
1548 /* Also copy the EI_OSABI field. */
1549 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1550 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1552 /* If set, copy the EI_ABIVERSION field. */
1553 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1554 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1555 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1557 /* Copy object attributes. */
1558 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1560 if (iheaders
== NULL
|| oheaders
== NULL
)
1563 bed
= get_elf_backend_data (obfd
);
1565 /* Possibly copy other fields in the section header. */
1566 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1569 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1571 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1572 because of a special case need for generating separate debug info
1573 files. See below for more details. */
1575 || (oheader
->sh_type
!= SHT_NOBITS
1576 && oheader
->sh_type
< SHT_LOOS
))
1579 /* Ignore empty sections, and sections whose
1580 fields have already been initialised. */
1581 if (oheader
->sh_size
== 0
1582 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1585 /* Scan for the matching section in the input bfd.
1586 First we try for a direct mapping between the input and
1588 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1590 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1592 if (iheader
== NULL
)
1595 if (oheader
->bfd_section
!= NULL
1596 && iheader
->bfd_section
!= NULL
1597 && iheader
->bfd_section
->output_section
!= NULL
1598 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1600 /* We have found a connection from the input section to
1601 the output section. Attempt to copy the header fields.
1602 If this fails then do not try any further sections -
1603 there should only be a one-to-one mapping between
1604 input and output. */
1605 if (!copy_special_section_fields (ibfd
, obfd
,
1606 iheader
, oheader
, i
))
1607 j
= elf_numsections (ibfd
);
1612 if (j
< elf_numsections (ibfd
))
1615 /* That failed. So try to deduce the corresponding input section.
1616 Unfortunately we cannot compare names as the output string table
1617 is empty, so instead we check size, address and type. */
1618 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1620 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1622 if (iheader
== NULL
)
1625 /* Try matching fields in the input section's header.
1626 Since --only-keep-debug turns all non-debug sections into
1627 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1629 if ((oheader
->sh_type
== SHT_NOBITS
1630 || iheader
->sh_type
== oheader
->sh_type
)
1631 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1632 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1633 && iheader
->sh_addralign
== oheader
->sh_addralign
1634 && iheader
->sh_entsize
== oheader
->sh_entsize
1635 && iheader
->sh_size
== oheader
->sh_size
1636 && iheader
->sh_addr
== oheader
->sh_addr
1637 && (iheader
->sh_info
!= oheader
->sh_info
1638 || iheader
->sh_link
!= oheader
->sh_link
))
1640 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1645 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1647 /* Final attempt. Call the backend copy function
1648 with a NULL input section. */
1649 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1658 get_segment_type (unsigned int p_type
)
1663 case PT_NULL
: pt
= "NULL"; break;
1664 case PT_LOAD
: pt
= "LOAD"; break;
1665 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1666 case PT_INTERP
: pt
= "INTERP"; break;
1667 case PT_NOTE
: pt
= "NOTE"; break;
1668 case PT_SHLIB
: pt
= "SHLIB"; break;
1669 case PT_PHDR
: pt
= "PHDR"; break;
1670 case PT_TLS
: pt
= "TLS"; break;
1671 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1672 case PT_GNU_STACK
: pt
= "STACK"; break;
1673 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1674 default: pt
= NULL
; break;
1679 /* Print out the program headers. */
1682 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1684 FILE *f
= (FILE *) farg
;
1685 Elf_Internal_Phdr
*p
;
1687 bfd_byte
*dynbuf
= NULL
;
1689 p
= elf_tdata (abfd
)->phdr
;
1694 fprintf (f
, _("\nProgram Header:\n"));
1695 c
= elf_elfheader (abfd
)->e_phnum
;
1696 for (i
= 0; i
< c
; i
++, p
++)
1698 const char *pt
= get_segment_type (p
->p_type
);
1703 sprintf (buf
, "0x%lx", p
->p_type
);
1706 fprintf (f
, "%8s off 0x", pt
);
1707 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1708 fprintf (f
, " vaddr 0x");
1709 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1710 fprintf (f
, " paddr 0x");
1711 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1712 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1713 fprintf (f
, " filesz 0x");
1714 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1715 fprintf (f
, " memsz 0x");
1716 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1717 fprintf (f
, " flags %c%c%c",
1718 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1719 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1720 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1721 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1722 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1727 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1730 unsigned int elfsec
;
1731 unsigned long shlink
;
1732 bfd_byte
*extdyn
, *extdynend
;
1734 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1736 fprintf (f
, _("\nDynamic Section:\n"));
1738 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1741 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1742 if (elfsec
== SHN_BAD
)
1744 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1746 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1747 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1749 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
1750 (size_t) (extdynend
- extdyn
) >= extdynsize
;
1751 extdyn
+= extdynsize
)
1753 Elf_Internal_Dyn dyn
;
1754 const char *name
= "";
1757 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1759 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1761 if (dyn
.d_tag
== DT_NULL
)
1768 if (bed
->elf_backend_get_target_dtag
)
1769 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1771 if (!strcmp (name
, ""))
1773 sprintf (ab
, "%#" PRIx64
, (uint64_t) dyn
.d_tag
);
1778 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1779 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1780 case DT_PLTGOT
: name
= "PLTGOT"; break;
1781 case DT_HASH
: name
= "HASH"; break;
1782 case DT_STRTAB
: name
= "STRTAB"; break;
1783 case DT_SYMTAB
: name
= "SYMTAB"; break;
1784 case DT_RELA
: name
= "RELA"; break;
1785 case DT_RELASZ
: name
= "RELASZ"; break;
1786 case DT_RELAENT
: name
= "RELAENT"; break;
1787 case DT_STRSZ
: name
= "STRSZ"; break;
1788 case DT_SYMENT
: name
= "SYMENT"; break;
1789 case DT_INIT
: name
= "INIT"; break;
1790 case DT_FINI
: name
= "FINI"; break;
1791 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1792 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1793 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1794 case DT_REL
: name
= "REL"; break;
1795 case DT_RELSZ
: name
= "RELSZ"; break;
1796 case DT_RELENT
: name
= "RELENT"; break;
1797 case DT_RELR
: name
= "RELR"; break;
1798 case DT_RELRSZ
: name
= "RELRSZ"; break;
1799 case DT_RELRENT
: name
= "RELRENT"; break;
1800 case DT_PLTREL
: name
= "PLTREL"; break;
1801 case DT_DEBUG
: name
= "DEBUG"; break;
1802 case DT_TEXTREL
: name
= "TEXTREL"; break;
1803 case DT_JMPREL
: name
= "JMPREL"; break;
1804 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1805 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1806 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1807 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1808 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1809 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1810 case DT_FLAGS
: name
= "FLAGS"; break;
1811 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1812 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1813 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1814 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1815 case DT_MOVEENT
: name
= "MOVEENT"; break;
1816 case DT_MOVESZ
: name
= "MOVESZ"; break;
1817 case DT_FEATURE
: name
= "FEATURE"; break;
1818 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1819 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1820 case DT_SYMINENT
: name
= "SYMINENT"; break;
1821 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1822 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1823 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1824 case DT_PLTPAD
: name
= "PLTPAD"; break;
1825 case DT_MOVETAB
: name
= "MOVETAB"; break;
1826 case DT_SYMINFO
: name
= "SYMINFO"; break;
1827 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1828 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1829 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1830 case DT_VERSYM
: name
= "VERSYM"; break;
1831 case DT_VERDEF
: name
= "VERDEF"; break;
1832 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1833 case DT_VERNEED
: name
= "VERNEED"; break;
1834 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1835 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1836 case DT_USED
: name
= "USED"; break;
1837 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1838 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1841 fprintf (f
, " %-20s ", name
);
1845 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1850 unsigned int tagv
= dyn
.d_un
.d_val
;
1852 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1855 fprintf (f
, "%s", string
);
1864 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1865 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1867 if (! _bfd_elf_slurp_version_tables (abfd
, false))
1871 if (elf_dynverdef (abfd
) != 0)
1873 Elf_Internal_Verdef
*t
;
1875 fprintf (f
, _("\nVersion definitions:\n"));
1876 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1878 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1879 t
->vd_flags
, t
->vd_hash
,
1880 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1881 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1883 Elf_Internal_Verdaux
*a
;
1886 for (a
= t
->vd_auxptr
->vda_nextptr
;
1890 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1896 if (elf_dynverref (abfd
) != 0)
1898 Elf_Internal_Verneed
*t
;
1900 fprintf (f
, _("\nVersion References:\n"));
1901 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1903 Elf_Internal_Vernaux
*a
;
1905 fprintf (f
, _(" required from %s:\n"),
1906 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1907 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1908 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1909 a
->vna_flags
, a
->vna_other
,
1910 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1921 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1922 and return symbol version for symbol version itself. */
1925 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1929 const char *version_string
= NULL
;
1930 if (elf_dynversym (abfd
) != 0
1931 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1933 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1935 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1936 vernum
&= VERSYM_VERSION
;
1939 version_string
= "";
1940 else if (vernum
== 1
1941 && (vernum
> elf_tdata (abfd
)->cverdefs
1942 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1944 version_string
= base_p
? "Base" : "";
1945 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1947 const char *nodename
1948 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1949 version_string
= "";
1952 || symbol
->name
== NULL
1953 || strcmp (symbol
->name
, nodename
) != 0)
1954 version_string
= nodename
;
1958 Elf_Internal_Verneed
*t
;
1960 version_string
= _("<corrupt>");
1961 for (t
= elf_tdata (abfd
)->verref
;
1965 Elf_Internal_Vernaux
*a
;
1967 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1969 if (a
->vna_other
== vernum
)
1972 version_string
= a
->vna_nodename
;
1979 return version_string
;
1982 /* Display ELF-specific fields of a symbol. */
1985 bfd_elf_print_symbol (bfd
*abfd
,
1988 bfd_print_symbol_type how
)
1990 FILE *file
= (FILE *) filep
;
1993 case bfd_print_symbol_name
:
1994 fprintf (file
, "%s", symbol
->name
);
1996 case bfd_print_symbol_more
:
1997 fprintf (file
, "elf ");
1998 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1999 fprintf (file
, " %x", symbol
->flags
);
2001 case bfd_print_symbol_all
:
2003 const char *section_name
;
2004 const char *name
= NULL
;
2005 const struct elf_backend_data
*bed
;
2006 unsigned char st_other
;
2008 const char *version_string
;
2011 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2013 bed
= get_elf_backend_data (abfd
);
2014 if (bed
->elf_backend_print_symbol_all
)
2015 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
2019 name
= symbol
->name
;
2020 bfd_print_symbol_vandf (abfd
, file
, symbol
);
2023 fprintf (file
, " %s\t", section_name
);
2024 /* Print the "other" value for a symbol. For common symbols,
2025 we've already printed the size; now print the alignment.
2026 For other symbols, we have no specified alignment, and
2027 we've printed the address; now print the size. */
2028 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
2029 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
2031 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
2032 bfd_fprintf_vma (abfd
, file
, val
);
2034 /* If we have version information, print it. */
2035 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2042 fprintf (file
, " %-11s", version_string
);
2047 fprintf (file
, " (%s)", version_string
);
2048 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2053 /* If the st_other field is not zero, print it. */
2054 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2059 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2060 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2061 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2063 /* Some other non-defined flags are also present, so print
2065 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2068 fprintf (file
, " %s", name
);
2074 /* ELF .o/exec file reading */
2076 /* Create a new bfd section from an ELF section header. */
2079 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2081 Elf_Internal_Shdr
*hdr
;
2082 Elf_Internal_Ehdr
*ehdr
;
2083 const struct elf_backend_data
*bed
;
2087 if (shindex
>= elf_numsections (abfd
))
2090 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2091 sh_link or sh_info. Detect this here, by refusing to load a
2092 section that we are already in the process of loading. */
2093 if (elf_tdata (abfd
)->being_created
[shindex
])
2096 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2099 elf_tdata (abfd
)->being_created
[shindex
] = true;
2101 hdr
= elf_elfsections (abfd
)[shindex
];
2102 ehdr
= elf_elfheader (abfd
);
2103 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2108 bed
= get_elf_backend_data (abfd
);
2109 switch (hdr
->sh_type
)
2112 /* Inactive section. Throw it away. */
2115 case SHT_PROGBITS
: /* Normal section with contents. */
2116 case SHT_NOBITS
: /* .bss section. */
2117 case SHT_HASH
: /* .hash section. */
2118 case SHT_NOTE
: /* .note section. */
2119 case SHT_INIT_ARRAY
: /* .init_array section. */
2120 case SHT_FINI_ARRAY
: /* .fini_array section. */
2121 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2122 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2123 case SHT_GNU_HASH
: /* .gnu.hash section. */
2124 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2127 case SHT_DYNAMIC
: /* Dynamic linking information. */
2128 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2131 if (hdr
->sh_link
> elf_numsections (abfd
))
2133 /* PR 10478: Accept Solaris binaries with a sh_link field
2134 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2135 switch (bfd_get_arch (abfd
))
2138 case bfd_arch_sparc
:
2139 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff)
2140 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff))
2142 /* Otherwise fall through. */
2147 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2149 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2151 Elf_Internal_Shdr
*dynsymhdr
;
2153 /* The shared libraries distributed with hpux11 have a bogus
2154 sh_link field for the ".dynamic" section. Find the
2155 string table for the ".dynsym" section instead. */
2156 if (elf_dynsymtab (abfd
) != 0)
2158 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2159 hdr
->sh_link
= dynsymhdr
->sh_link
;
2163 unsigned int i
, num_sec
;
2165 num_sec
= elf_numsections (abfd
);
2166 for (i
= 1; i
< num_sec
; i
++)
2168 dynsymhdr
= elf_elfsections (abfd
)[i
];
2169 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2171 hdr
->sh_link
= dynsymhdr
->sh_link
;
2179 case SHT_SYMTAB
: /* A symbol table. */
2180 if (elf_onesymtab (abfd
) == shindex
)
2183 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2186 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2188 if (hdr
->sh_size
!= 0)
2190 /* Some assemblers erroneously set sh_info to one with a
2191 zero sh_size. ld sees this as a global symbol count
2192 of (unsigned) -1. Fix it here. */
2197 /* PR 18854: A binary might contain more than one symbol table.
2198 Unusual, but possible. Warn, but continue. */
2199 if (elf_onesymtab (abfd
) != 0)
2202 /* xgettext:c-format */
2203 (_("%pB: warning: multiple symbol tables detected"
2204 " - ignoring the table in section %u"),
2208 elf_onesymtab (abfd
) = shindex
;
2209 elf_symtab_hdr (abfd
) = *hdr
;
2210 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2211 abfd
->flags
|= HAS_SYMS
;
2213 /* Sometimes a shared object will map in the symbol table. If
2214 SHF_ALLOC is set, and this is a shared object, then we also
2215 treat this section as a BFD section. We can not base the
2216 decision purely on SHF_ALLOC, because that flag is sometimes
2217 set in a relocatable object file, which would confuse the
2219 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2220 && (abfd
->flags
& DYNAMIC
) != 0
2221 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2225 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2226 can't read symbols without that section loaded as well. It
2227 is most likely specified by the next section header. */
2229 elf_section_list
* entry
;
2230 unsigned int i
, num_sec
;
2232 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2233 if (entry
->hdr
.sh_link
== shindex
)
2236 num_sec
= elf_numsections (abfd
);
2237 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2239 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2241 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2242 && hdr2
->sh_link
== shindex
)
2247 for (i
= 1; i
< shindex
; i
++)
2249 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2251 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2252 && hdr2
->sh_link
== shindex
)
2257 ret
= bfd_section_from_shdr (abfd
, i
);
2258 /* else FIXME: we have failed to find the symbol table.
2259 Should we issue an error? */
2263 case SHT_DYNSYM
: /* A dynamic symbol table. */
2264 if (elf_dynsymtab (abfd
) == shindex
)
2267 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2270 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2272 if (hdr
->sh_size
!= 0)
2275 /* Some linkers erroneously set sh_info to one with a
2276 zero sh_size. ld sees this as a global symbol count
2277 of (unsigned) -1. Fix it here. */
2282 /* PR 18854: A binary might contain more than one dynamic symbol table.
2283 Unusual, but possible. Warn, but continue. */
2284 if (elf_dynsymtab (abfd
) != 0)
2287 /* xgettext:c-format */
2288 (_("%pB: warning: multiple dynamic symbol tables detected"
2289 " - ignoring the table in section %u"),
2293 elf_dynsymtab (abfd
) = shindex
;
2294 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2295 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2296 abfd
->flags
|= HAS_SYMS
;
2298 /* Besides being a symbol table, we also treat this as a regular
2299 section, so that objcopy can handle it. */
2300 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2303 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2305 elf_section_list
* entry
;
2307 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2308 if (entry
->ndx
== shindex
)
2311 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2314 entry
->ndx
= shindex
;
2316 entry
->next
= elf_symtab_shndx_list (abfd
);
2317 elf_symtab_shndx_list (abfd
) = entry
;
2318 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2322 case SHT_STRTAB
: /* A string table. */
2323 if (hdr
->bfd_section
!= NULL
)
2326 if (ehdr
->e_shstrndx
== shindex
)
2328 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2329 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2333 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2336 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2337 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2341 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2344 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2345 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2346 elf_elfsections (abfd
)[shindex
] = hdr
;
2347 /* We also treat this as a regular section, so that objcopy
2349 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2354 /* If the string table isn't one of the above, then treat it as a
2355 regular section. We need to scan all the headers to be sure,
2356 just in case this strtab section appeared before the above. */
2357 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2359 unsigned int i
, num_sec
;
2361 num_sec
= elf_numsections (abfd
);
2362 for (i
= 1; i
< num_sec
; i
++)
2364 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2365 if (hdr2
->sh_link
== shindex
)
2367 /* Prevent endless recursion on broken objects. */
2370 if (! bfd_section_from_shdr (abfd
, i
))
2372 if (elf_onesymtab (abfd
) == i
)
2374 if (elf_dynsymtab (abfd
) == i
)
2375 goto dynsymtab_strtab
;
2379 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2385 /* *These* do a lot of work -- but build no sections! */
2387 asection
*target_sect
;
2388 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2389 unsigned int num_sec
= elf_numsections (abfd
);
2390 struct bfd_elf_section_data
*esdt
;
2393 if (hdr
->sh_type
== SHT_REL
)
2394 size
= bed
->s
->sizeof_rel
;
2395 else if (hdr
->sh_type
== SHT_RELA
)
2396 size
= bed
->s
->sizeof_rela
;
2398 size
= bed
->s
->arch_size
/ 8;
2399 if (hdr
->sh_entsize
!= size
)
2402 /* Check for a bogus link to avoid crashing. */
2403 if (hdr
->sh_link
>= num_sec
)
2406 /* xgettext:c-format */
2407 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2408 abfd
, hdr
->sh_link
, name
, shindex
);
2409 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2413 /* Get the symbol table. */
2414 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2415 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2416 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2419 /* If this is an alloc section in an executable or shared
2420 library, or the reloc section does not use the main symbol
2421 table we don't treat it as a reloc section. BFD can't
2422 adequately represent such a section, so at least for now,
2423 we don't try. We just present it as a normal section. We
2424 also can't use it as a reloc section if it points to the
2425 null section, an invalid section, another reloc section, or
2426 its sh_link points to the null section. */
2427 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2428 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2429 || hdr
->sh_type
== SHT_RELR
2430 || hdr
->sh_link
== SHN_UNDEF
2431 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2432 || hdr
->sh_info
== SHN_UNDEF
2433 || hdr
->sh_info
>= num_sec
2434 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2435 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2437 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2441 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2444 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2445 if (target_sect
== NULL
)
2448 esdt
= elf_section_data (target_sect
);
2449 if (hdr
->sh_type
== SHT_RELA
)
2450 p_hdr
= &esdt
->rela
.hdr
;
2452 p_hdr
= &esdt
->rel
.hdr
;
2454 /* PR 17512: file: 0b4f81b7.
2455 Also see PR 24456, for a file which deliberately has two reloc
2459 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2462 /* xgettext:c-format */
2463 (_("%pB: warning: secondary relocation section '%s' "
2464 "for section %pA found - ignoring"),
2465 abfd
, name
, target_sect
);
2468 esdt
->has_secondary_relocs
= true;
2472 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2477 elf_elfsections (abfd
)[shindex
] = hdr2
;
2478 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2479 * bed
->s
->int_rels_per_ext_rel
);
2480 target_sect
->flags
|= SEC_RELOC
;
2481 target_sect
->relocation
= NULL
;
2482 target_sect
->rel_filepos
= hdr
->sh_offset
;
2483 /* In the section to which the relocations apply, mark whether
2484 its relocations are of the REL or RELA variety. */
2485 if (hdr
->sh_size
!= 0)
2487 if (hdr
->sh_type
== SHT_RELA
)
2488 target_sect
->use_rela_p
= 1;
2490 abfd
->flags
|= HAS_RELOC
;
2494 case SHT_GNU_verdef
:
2495 if (hdr
->sh_info
!= 0)
2496 elf_dynverdef (abfd
) = shindex
;
2497 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2498 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2501 case SHT_GNU_versym
:
2502 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2505 elf_dynversym (abfd
) = shindex
;
2506 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2507 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2510 case SHT_GNU_verneed
:
2511 if (hdr
->sh_info
!= 0)
2512 elf_dynverref (abfd
) = shindex
;
2513 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2514 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2521 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2524 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2530 /* Possibly an attributes section. */
2531 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2532 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2534 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2536 _bfd_elf_parse_attributes (abfd
, hdr
);
2540 /* Check for any processor-specific section types. */
2541 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2544 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2546 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2547 /* FIXME: How to properly handle allocated section reserved
2548 for applications? */
2550 /* xgettext:c-format */
2551 (_("%pB: unknown type [%#x] section `%s'"),
2552 abfd
, hdr
->sh_type
, name
);
2555 /* Allow sections reserved for applications. */
2556 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2560 else if (hdr
->sh_type
>= SHT_LOPROC
2561 && hdr
->sh_type
<= SHT_HIPROC
)
2562 /* FIXME: We should handle this section. */
2564 /* xgettext:c-format */
2565 (_("%pB: unknown type [%#x] section `%s'"),
2566 abfd
, hdr
->sh_type
, name
);
2567 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2569 /* Unrecognised OS-specific sections. */
2570 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2571 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2572 required to correctly process the section and the file should
2573 be rejected with an error message. */
2575 /* xgettext:c-format */
2576 (_("%pB: unknown type [%#x] section `%s'"),
2577 abfd
, hdr
->sh_type
, name
);
2580 /* Otherwise it should be processed. */
2581 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2586 /* FIXME: We should handle this section. */
2588 /* xgettext:c-format */
2589 (_("%pB: unknown type [%#x] section `%s'"),
2590 abfd
, hdr
->sh_type
, name
);
2598 elf_tdata (abfd
)->being_created
[shindex
] = false;
2602 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2605 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2607 unsigned long r_symndx
)
2609 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2611 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2613 Elf_Internal_Shdr
*symtab_hdr
;
2614 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2615 Elf_External_Sym_Shndx eshndx
;
2617 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2618 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2619 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2622 if (cache
->abfd
!= abfd
)
2624 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2627 cache
->indx
[ent
] = r_symndx
;
2630 return &cache
->sym
[ent
];
2633 /* Given an ELF section number, retrieve the corresponding BFD
2637 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2639 if (sec_index
>= elf_numsections (abfd
))
2641 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2644 static const struct bfd_elf_special_section special_sections_b
[] =
2646 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2647 { NULL
, 0, 0, 0, 0 }
2650 static const struct bfd_elf_special_section special_sections_c
[] =
2652 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2653 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2654 { NULL
, 0, 0, 0, 0 }
2657 static const struct bfd_elf_special_section special_sections_d
[] =
2659 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2660 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2661 /* There are more DWARF sections than these, but they needn't be added here
2662 unless you have to cope with broken compilers that don't emit section
2663 attributes or you want to help the user writing assembler. */
2664 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2665 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2666 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2667 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2668 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2669 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2670 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2671 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2672 { NULL
, 0, 0, 0, 0 }
2675 static const struct bfd_elf_special_section special_sections_f
[] =
2677 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2678 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2679 { NULL
, 0 , 0, 0, 0 }
2682 static const struct bfd_elf_special_section special_sections_g
[] =
2684 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2685 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2686 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2687 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2688 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2689 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2690 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2691 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2692 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2693 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2694 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2695 { NULL
, 0, 0, 0, 0 }
2698 static const struct bfd_elf_special_section special_sections_h
[] =
2700 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2701 { NULL
, 0, 0, 0, 0 }
2704 static const struct bfd_elf_special_section special_sections_i
[] =
2706 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2707 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2708 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2709 { NULL
, 0, 0, 0, 0 }
2712 static const struct bfd_elf_special_section special_sections_l
[] =
2714 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2715 { NULL
, 0, 0, 0, 0 }
2718 static const struct bfd_elf_special_section special_sections_n
[] =
2720 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2721 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2722 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2723 { NULL
, 0, 0, 0, 0 }
2726 static const struct bfd_elf_special_section special_sections_p
[] =
2728 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2729 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2730 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2731 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2732 { NULL
, 0, 0, 0, 0 }
2735 static const struct bfd_elf_special_section special_sections_r
[] =
2737 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2738 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2739 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR
, SHF_ALLOC
},
2740 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2741 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2742 { NULL
, 0, 0, 0, 0 }
2745 static const struct bfd_elf_special_section special_sections_s
[] =
2747 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2748 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2749 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2750 /* See struct bfd_elf_special_section declaration for the semantics of
2751 this special case where .prefix_length != strlen (.prefix). */
2752 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2753 { NULL
, 0, 0, 0, 0 }
2756 static const struct bfd_elf_special_section special_sections_t
[] =
2758 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2759 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2760 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2761 { NULL
, 0, 0, 0, 0 }
2764 static const struct bfd_elf_special_section special_sections_z
[] =
2766 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2767 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2768 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2769 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2770 { NULL
, 0, 0, 0, 0 }
2773 static const struct bfd_elf_special_section
* const special_sections
[] =
2775 special_sections_b
, /* 'b' */
2776 special_sections_c
, /* 'c' */
2777 special_sections_d
, /* 'd' */
2779 special_sections_f
, /* 'f' */
2780 special_sections_g
, /* 'g' */
2781 special_sections_h
, /* 'h' */
2782 special_sections_i
, /* 'i' */
2785 special_sections_l
, /* 'l' */
2787 special_sections_n
, /* 'n' */
2789 special_sections_p
, /* 'p' */
2791 special_sections_r
, /* 'r' */
2792 special_sections_s
, /* 's' */
2793 special_sections_t
, /* 't' */
2799 special_sections_z
/* 'z' */
2802 const struct bfd_elf_special_section
*
2803 _bfd_elf_get_special_section (const char *name
,
2804 const struct bfd_elf_special_section
*spec
,
2810 len
= strlen (name
);
2812 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2815 int prefix_len
= spec
[i
].prefix_length
;
2817 if (len
< prefix_len
)
2819 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2822 suffix_len
= spec
[i
].suffix_length
;
2823 if (suffix_len
<= 0)
2825 if (name
[prefix_len
] != 0)
2827 if (suffix_len
== 0)
2829 if (name
[prefix_len
] != '.'
2830 && (suffix_len
== -2
2831 || (rela
&& spec
[i
].type
== SHT_REL
)))
2837 if (len
< prefix_len
+ suffix_len
)
2839 if (memcmp (name
+ len
- suffix_len
,
2840 spec
[i
].prefix
+ prefix_len
,
2850 const struct bfd_elf_special_section
*
2851 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2854 const struct bfd_elf_special_section
*spec
;
2855 const struct elf_backend_data
*bed
;
2857 /* See if this is one of the special sections. */
2858 if (sec
->name
== NULL
)
2861 bed
= get_elf_backend_data (abfd
);
2862 spec
= bed
->special_sections
;
2865 spec
= _bfd_elf_get_special_section (sec
->name
,
2866 bed
->special_sections
,
2872 if (sec
->name
[0] != '.')
2875 i
= sec
->name
[1] - 'b';
2876 if (i
< 0 || i
> 'z' - 'b')
2879 spec
= special_sections
[i
];
2884 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2888 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2890 struct bfd_elf_section_data
*sdata
;
2891 const struct elf_backend_data
*bed
;
2892 const struct bfd_elf_special_section
*ssect
;
2894 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2897 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2901 sec
->used_by_bfd
= sdata
;
2904 /* Indicate whether or not this section should use RELA relocations. */
2905 bed
= get_elf_backend_data (abfd
);
2906 sec
->use_rela_p
= bed
->default_use_rela_p
;
2908 /* Set up ELF section type and flags for newly created sections, if
2909 there is an ABI mandated section. */
2910 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2913 elf_section_type (sec
) = ssect
->type
;
2914 elf_section_flags (sec
) = ssect
->attr
;
2917 return _bfd_generic_new_section_hook (abfd
, sec
);
2920 /* Create a new bfd section from an ELF program header.
2922 Since program segments have no names, we generate a synthetic name
2923 of the form segment<NUM>, where NUM is generally the index in the
2924 program header table. For segments that are split (see below) we
2925 generate the names segment<NUM>a and segment<NUM>b.
2927 Note that some program segments may have a file size that is different than
2928 (less than) the memory size. All this means is that at execution the
2929 system must allocate the amount of memory specified by the memory size,
2930 but only initialize it with the first "file size" bytes read from the
2931 file. This would occur for example, with program segments consisting
2932 of combined data+bss.
2934 To handle the above situation, this routine generates TWO bfd sections
2935 for the single program segment. The first has the length specified by
2936 the file size of the segment, and the second has the length specified
2937 by the difference between the two sizes. In effect, the segment is split
2938 into its initialized and uninitialized parts. */
2941 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2942 Elf_Internal_Phdr
*hdr
,
2944 const char *type_name
)
2951 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
2953 split
= ((hdr
->p_memsz
> 0)
2954 && (hdr
->p_filesz
> 0)
2955 && (hdr
->p_memsz
> hdr
->p_filesz
));
2957 if (hdr
->p_filesz
> 0)
2959 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2960 len
= strlen (namebuf
) + 1;
2961 name
= (char *) bfd_alloc (abfd
, len
);
2964 memcpy (name
, namebuf
, len
);
2965 newsect
= bfd_make_section (abfd
, name
);
2966 if (newsect
== NULL
)
2968 newsect
->vma
= hdr
->p_vaddr
/ opb
;
2969 newsect
->lma
= hdr
->p_paddr
/ opb
;
2970 newsect
->size
= hdr
->p_filesz
;
2971 newsect
->filepos
= hdr
->p_offset
;
2972 newsect
->flags
|= SEC_HAS_CONTENTS
;
2973 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2974 if (hdr
->p_type
== PT_LOAD
)
2976 newsect
->flags
|= SEC_ALLOC
;
2977 newsect
->flags
|= SEC_LOAD
;
2978 if (hdr
->p_flags
& PF_X
)
2980 /* FIXME: all we known is that it has execute PERMISSION,
2982 newsect
->flags
|= SEC_CODE
;
2985 if (!(hdr
->p_flags
& PF_W
))
2987 newsect
->flags
|= SEC_READONLY
;
2991 if (hdr
->p_memsz
> hdr
->p_filesz
)
2995 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2996 len
= strlen (namebuf
) + 1;
2997 name
= (char *) bfd_alloc (abfd
, len
);
3000 memcpy (name
, namebuf
, len
);
3001 newsect
= bfd_make_section (abfd
, name
);
3002 if (newsect
== NULL
)
3004 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3005 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3006 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3007 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3008 align
= newsect
->vma
& -newsect
->vma
;
3009 if (align
== 0 || align
> hdr
->p_align
)
3010 align
= hdr
->p_align
;
3011 newsect
->alignment_power
= bfd_log2 (align
);
3012 if (hdr
->p_type
== PT_LOAD
)
3014 newsect
->flags
|= SEC_ALLOC
;
3015 if (hdr
->p_flags
& PF_X
)
3016 newsect
->flags
|= SEC_CODE
;
3018 if (!(hdr
->p_flags
& PF_W
))
3019 newsect
->flags
|= SEC_READONLY
;
3026 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3028 /* The return value is ignored. Build-ids are considered optional. */
3029 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3030 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3036 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3038 const struct elf_backend_data
*bed
;
3040 switch (hdr
->p_type
)
3043 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3046 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3048 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3049 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3053 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3056 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3059 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3061 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3067 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3070 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3072 case PT_GNU_EH_FRAME
:
3073 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3077 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3080 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3083 /* Check for any processor-specific program segment types. */
3084 bed
= get_elf_backend_data (abfd
);
3085 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3089 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3093 _bfd_elf_single_rel_hdr (asection
*sec
)
3095 if (elf_section_data (sec
)->rel
.hdr
)
3097 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3098 return elf_section_data (sec
)->rel
.hdr
;
3101 return elf_section_data (sec
)->rela
.hdr
;
3105 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3106 Elf_Internal_Shdr
*rel_hdr
,
3107 const char *sec_name
,
3110 char *name
= (char *) bfd_alloc (abfd
,
3111 sizeof ".rela" + strlen (sec_name
));
3115 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3117 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3119 if (rel_hdr
->sh_name
== (unsigned int) -1)
3125 /* Allocate and initialize a section-header for a new reloc section,
3126 containing relocations against ASECT. It is stored in RELDATA. If
3127 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3131 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3132 struct bfd_elf_section_reloc_data
*reldata
,
3133 const char *sec_name
,
3135 bool delay_st_name_p
)
3137 Elf_Internal_Shdr
*rel_hdr
;
3138 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3140 BFD_ASSERT (reldata
->hdr
== NULL
);
3141 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3142 reldata
->hdr
= rel_hdr
;
3144 if (delay_st_name_p
)
3145 rel_hdr
->sh_name
= (unsigned int) -1;
3146 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3149 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3150 rel_hdr
->sh_entsize
= (use_rela_p
3151 ? bed
->s
->sizeof_rela
3152 : bed
->s
->sizeof_rel
);
3153 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3154 rel_hdr
->sh_flags
= 0;
3155 rel_hdr
->sh_addr
= 0;
3156 rel_hdr
->sh_size
= 0;
3157 rel_hdr
->sh_offset
= 0;
3162 /* Return the default section type based on the passed in section flags. */
3165 bfd_elf_get_default_section_type (flagword flags
)
3167 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3168 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3170 return SHT_PROGBITS
;
3173 struct fake_section_arg
3175 struct bfd_link_info
*link_info
;
3179 /* Set up an ELF internal section header for a section. */
3182 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3184 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3185 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3186 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3187 Elf_Internal_Shdr
*this_hdr
;
3188 unsigned int sh_type
;
3189 const char *name
= asect
->name
;
3190 bool delay_st_name_p
= false;
3195 /* We already failed; just get out of the bfd_map_over_sections
3200 this_hdr
= &esd
->this_hdr
;
3204 /* ld: compress DWARF debug sections with names: .debug_*. */
3205 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3206 && (asect
->flags
& SEC_DEBUGGING
)
3210 /* Set SEC_ELF_COMPRESS to indicate this section should be
3212 asect
->flags
|= SEC_ELF_COMPRESS
;
3213 /* If this section will be compressed, delay adding section
3214 name to section name section after it is compressed in
3215 _bfd_elf_assign_file_positions_for_non_load. */
3216 delay_st_name_p
= true;
3219 else if ((asect
->flags
& SEC_ELF_RENAME
))
3221 /* objcopy: rename output DWARF debug section. */
3222 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3224 /* When we decompress or compress with SHF_COMPRESSED,
3225 convert section name from .zdebug_* to .debug_* if
3229 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3230 if (new_name
== NULL
)
3238 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3240 /* PR binutils/18087: Compression does not always make a
3241 section smaller. So only rename the section when
3242 compression has actually taken place. If input section
3243 name is .zdebug_*, we should never compress it again. */
3244 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3245 if (new_name
== NULL
)
3250 BFD_ASSERT (name
[1] != 'z');
3255 if (delay_st_name_p
)
3256 this_hdr
->sh_name
= (unsigned int) -1;
3260 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3262 if (this_hdr
->sh_name
== (unsigned int) -1)
3269 /* Don't clear sh_flags. Assembler may set additional bits. */
3271 if ((asect
->flags
& SEC_ALLOC
) != 0
3272 || asect
->user_set_vma
)
3273 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3275 this_hdr
->sh_addr
= 0;
3277 this_hdr
->sh_offset
= 0;
3278 this_hdr
->sh_size
= asect
->size
;
3279 this_hdr
->sh_link
= 0;
3280 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3281 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3284 /* xgettext:c-format */
3285 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3286 abfd
, asect
->alignment_power
, asect
);
3290 /* Set sh_addralign to the highest power of two given by alignment
3291 consistent with the section VMA. Linker scripts can force VMA. */
3292 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3293 this_hdr
->sh_addralign
= mask
& -mask
;
3294 /* The sh_entsize and sh_info fields may have been set already by
3295 copy_private_section_data. */
3297 this_hdr
->bfd_section
= asect
;
3298 this_hdr
->contents
= NULL
;
3300 /* If the section type is unspecified, we set it based on
3302 if (asect
->type
!= 0)
3303 sh_type
= asect
->type
;
3304 else if ((asect
->flags
& SEC_GROUP
) != 0)
3305 sh_type
= SHT_GROUP
;
3307 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3309 if (this_hdr
->sh_type
== SHT_NULL
)
3310 this_hdr
->sh_type
= sh_type
;
3311 else if (this_hdr
->sh_type
== SHT_NOBITS
3312 && sh_type
== SHT_PROGBITS
3313 && (asect
->flags
& SEC_ALLOC
) != 0)
3315 /* Warn if we are changing a NOBITS section to PROGBITS, but
3316 allow the link to proceed. This can happen when users link
3317 non-bss input sections to bss output sections, or emit data
3318 to a bss output section via a linker script. */
3320 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3321 this_hdr
->sh_type
= sh_type
;
3324 switch (this_hdr
->sh_type
)
3335 case SHT_INIT_ARRAY
:
3336 case SHT_FINI_ARRAY
:
3337 case SHT_PREINIT_ARRAY
:
3338 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3342 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3346 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3350 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3354 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3355 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3359 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3360 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3363 case SHT_GNU_versym
:
3364 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3367 case SHT_GNU_verdef
:
3368 this_hdr
->sh_entsize
= 0;
3369 /* objcopy or strip will copy over sh_info, but may not set
3370 cverdefs. The linker will set cverdefs, but sh_info will be
3372 if (this_hdr
->sh_info
== 0)
3373 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3375 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3376 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3379 case SHT_GNU_verneed
:
3380 this_hdr
->sh_entsize
= 0;
3381 /* objcopy or strip will copy over sh_info, but may not set
3382 cverrefs. The linker will set cverrefs, but sh_info will be
3384 if (this_hdr
->sh_info
== 0)
3385 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3387 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3388 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3392 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3396 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3400 if ((asect
->flags
& SEC_ALLOC
) != 0)
3401 this_hdr
->sh_flags
|= SHF_ALLOC
;
3402 if ((asect
->flags
& SEC_READONLY
) == 0)
3403 this_hdr
->sh_flags
|= SHF_WRITE
;
3404 if ((asect
->flags
& SEC_CODE
) != 0)
3405 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3406 if ((asect
->flags
& SEC_MERGE
) != 0)
3408 this_hdr
->sh_flags
|= SHF_MERGE
;
3409 this_hdr
->sh_entsize
= asect
->entsize
;
3411 if ((asect
->flags
& SEC_STRINGS
) != 0)
3412 this_hdr
->sh_flags
|= SHF_STRINGS
;
3413 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3414 this_hdr
->sh_flags
|= SHF_GROUP
;
3415 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3417 this_hdr
->sh_flags
|= SHF_TLS
;
3418 if (asect
->size
== 0
3419 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3421 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3423 this_hdr
->sh_size
= 0;
3426 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3427 if (this_hdr
->sh_size
!= 0)
3428 this_hdr
->sh_type
= SHT_NOBITS
;
3432 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3433 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3435 /* If the section has relocs, set up a section header for the
3436 SHT_REL[A] section. If two relocation sections are required for
3437 this section, it is up to the processor-specific back-end to
3438 create the other. */
3439 if ((asect
->flags
& SEC_RELOC
) != 0)
3441 /* When doing a relocatable link, create both REL and RELA sections if
3444 /* Do the normal setup if we wouldn't create any sections here. */
3445 && esd
->rel
.count
+ esd
->rela
.count
> 0
3446 && (bfd_link_relocatable (arg
->link_info
)
3447 || arg
->link_info
->emitrelocations
))
3449 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3450 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3451 false, delay_st_name_p
))
3456 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3457 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3458 true, delay_st_name_p
))
3464 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3466 ? &esd
->rela
: &esd
->rel
),
3476 /* Check for processor-specific section types. */
3477 sh_type
= this_hdr
->sh_type
;
3478 if (bed
->elf_backend_fake_sections
3479 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3485 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3487 /* Don't change the header type from NOBITS if we are being
3488 called for objcopy --only-keep-debug. */
3489 this_hdr
->sh_type
= sh_type
;
3493 /* Fill in the contents of a SHT_GROUP section. Called from
3494 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3495 when ELF targets use the generic linker, ld. Called for ld -r
3496 from bfd_elf_final_link. */
3499 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3501 bool *failedptr
= (bool *) failedptrarg
;
3502 asection
*elt
, *first
;
3506 /* Ignore linker created group section. See elfNN_ia64_object_p in
3508 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3513 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3515 unsigned long symindx
= 0;
3517 /* elf_group_id will have been set up by objcopy and the
3519 if (elf_group_id (sec
) != NULL
)
3520 symindx
= elf_group_id (sec
)->udata
.i
;
3524 /* If called from the assembler, swap_out_syms will have set up
3526 PR 25699: A corrupt input file could contain bogus group info. */
3527 if (sec
->index
>= elf_num_section_syms (abfd
)
3528 || elf_section_syms (abfd
)[sec
->index
] == NULL
)
3533 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3535 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3537 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3539 /* The ELF backend linker sets sh_info to -2 when the group
3540 signature symbol is global, and thus the index can't be
3541 set until all local symbols are output. */
3543 struct bfd_elf_section_data
*sec_data
;
3544 unsigned long symndx
;
3545 unsigned long extsymoff
;
3546 struct elf_link_hash_entry
*h
;
3548 /* The point of this little dance to the first SHF_GROUP section
3549 then back to the SHT_GROUP section is that this gets us to
3550 the SHT_GROUP in the input object. */
3551 igroup
= elf_sec_group (elf_next_in_group (sec
));
3552 sec_data
= elf_section_data (igroup
);
3553 symndx
= sec_data
->this_hdr
.sh_info
;
3555 if (!elf_bad_symtab (igroup
->owner
))
3557 Elf_Internal_Shdr
*symtab_hdr
;
3559 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3560 extsymoff
= symtab_hdr
->sh_info
;
3562 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3563 while (h
->root
.type
== bfd_link_hash_indirect
3564 || h
->root
.type
== bfd_link_hash_warning
)
3565 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3567 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3570 /* The contents won't be allocated for "ld -r" or objcopy. */
3572 if (sec
->contents
== NULL
)
3575 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3577 /* Arrange for the section to be written out. */
3578 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3579 if (sec
->contents
== NULL
)
3586 loc
= sec
->contents
+ sec
->size
;
3588 /* Get the pointer to the first section in the group that gas
3589 squirreled away here. objcopy arranges for this to be set to the
3590 start of the input section group. */
3591 first
= elt
= elf_next_in_group (sec
);
3593 /* First element is a flag word. Rest of section is elf section
3594 indices for all the sections of the group. Write them backwards
3595 just to keep the group in the same order as given in .section
3596 directives, not that it matters. */
3603 s
= s
->output_section
;
3605 && !bfd_is_abs_section (s
))
3607 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3608 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3610 if (elf_sec
->rel
.hdr
!= NULL
3612 || (input_elf_sec
->rel
.hdr
!= NULL
3613 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3615 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3617 if (loc
== sec
->contents
)
3619 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3621 if (elf_sec
->rela
.hdr
!= NULL
3623 || (input_elf_sec
->rela
.hdr
!= NULL
3624 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3626 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3628 if (loc
== sec
->contents
)
3630 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3633 if (loc
== sec
->contents
)
3635 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3637 elt
= elf_next_in_group (elt
);
3642 /* We should always get here with loc == sec->contents + 4, but it is
3643 possible to craft bogus SHT_GROUP sections that will cause segfaults
3644 in objcopy without checking loc here and in the loop above. */
3645 if (loc
== sec
->contents
)
3650 if (loc
!= sec
->contents
)
3653 memset (sec
->contents
+ 4, 0, loc
- sec
->contents
);
3654 loc
= sec
->contents
;
3658 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3661 /* Given NAME, the name of a relocation section stripped of its
3662 .rel/.rela prefix, return the section in ABFD to which the
3663 relocations apply. */
3666 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3668 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3669 section likely apply to .got.plt or .got section. */
3670 if (get_elf_backend_data (abfd
)->want_got_plt
3671 && strcmp (name
, ".plt") == 0)
3676 sec
= bfd_get_section_by_name (abfd
, name
);
3682 return bfd_get_section_by_name (abfd
, name
);
3685 /* Return the section to which RELOC_SEC applies. */
3688 elf_get_reloc_section (asection
*reloc_sec
)
3693 const struct elf_backend_data
*bed
;
3695 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3696 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3699 /* We look up the section the relocs apply to by name. */
3700 name
= reloc_sec
->name
;
3701 if (!startswith (name
, ".rel"))
3704 if (type
== SHT_RELA
&& *name
++ != 'a')
3707 abfd
= reloc_sec
->owner
;
3708 bed
= get_elf_backend_data (abfd
);
3709 return bed
->get_reloc_section (abfd
, name
);
3712 /* Assign all ELF section numbers. The dummy first section is handled here
3713 too. The link/info pointers for the standard section types are filled
3714 in here too, while we're at it. LINK_INFO will be 0 when arriving
3715 here for gas, objcopy, and when using the generic ELF linker. */
3718 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3720 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3722 unsigned int section_number
;
3723 Elf_Internal_Shdr
**i_shdrp
;
3724 struct bfd_elf_section_data
*d
;
3730 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3732 /* SHT_GROUP sections are in relocatable files only. */
3733 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3735 size_t reloc_count
= 0;
3737 /* Put SHT_GROUP sections first. */
3738 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3740 d
= elf_section_data (sec
);
3742 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3744 if (sec
->flags
& SEC_LINKER_CREATED
)
3746 /* Remove the linker created SHT_GROUP sections. */
3747 bfd_section_list_remove (abfd
, sec
);
3748 abfd
->section_count
--;
3751 d
->this_idx
= section_number
++;
3754 /* Count relocations. */
3755 reloc_count
+= sec
->reloc_count
;
3758 /* Set/clear HAS_RELOC depending on whether there are relocations. */
3759 if (reloc_count
== 0)
3760 abfd
->flags
&= ~HAS_RELOC
;
3762 abfd
->flags
|= HAS_RELOC
;
3765 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3767 d
= elf_section_data (sec
);
3769 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3770 d
->this_idx
= section_number
++;
3771 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3772 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3775 d
->rel
.idx
= section_number
++;
3776 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3777 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3784 d
->rela
.idx
= section_number
++;
3785 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3786 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3792 need_symtab
= (bfd_get_symcount (abfd
) > 0
3793 || (link_info
== NULL
3794 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3798 elf_onesymtab (abfd
) = section_number
++;
3799 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3800 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3802 elf_section_list
*entry
;
3804 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3806 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3807 entry
->ndx
= section_number
++;
3808 elf_symtab_shndx_list (abfd
) = entry
;
3810 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3811 ".symtab_shndx", false);
3812 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3815 elf_strtab_sec (abfd
) = section_number
++;
3816 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3819 elf_shstrtab_sec (abfd
) = section_number
++;
3820 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3821 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3823 if (section_number
>= SHN_LORESERVE
)
3825 /* xgettext:c-format */
3826 _bfd_error_handler (_("%pB: too many sections: %u"),
3827 abfd
, section_number
);
3831 elf_numsections (abfd
) = section_number
;
3832 elf_elfheader (abfd
)->e_shnum
= section_number
;
3834 /* Set up the list of section header pointers, in agreement with the
3836 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3837 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3838 if (i_shdrp
== NULL
)
3841 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3842 sizeof (Elf_Internal_Shdr
));
3843 if (i_shdrp
[0] == NULL
)
3845 bfd_release (abfd
, i_shdrp
);
3849 elf_elfsections (abfd
) = i_shdrp
;
3851 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3854 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3855 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3857 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3858 BFD_ASSERT (entry
!= NULL
);
3859 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3860 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3862 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3863 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3866 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3870 d
= elf_section_data (sec
);
3872 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3873 if (d
->rel
.idx
!= 0)
3874 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3875 if (d
->rela
.idx
!= 0)
3876 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3878 /* Fill in the sh_link and sh_info fields while we're at it. */
3880 /* sh_link of a reloc section is the section index of the symbol
3881 table. sh_info is the section index of the section to which
3882 the relocation entries apply. */
3883 if (d
->rel
.idx
!= 0)
3885 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3886 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3887 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3889 if (d
->rela
.idx
!= 0)
3891 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3892 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3893 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3896 /* We need to set up sh_link for SHF_LINK_ORDER. */
3897 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3899 s
= elf_linked_to_section (sec
);
3900 /* We can now have a NULL linked section pointer.
3901 This happens when the sh_link field is 0, which is done
3902 when a linked to section is discarded but the linking
3903 section has been retained for some reason. */
3906 /* Check discarded linkonce section. */
3907 if (discarded_section (s
))
3911 /* xgettext:c-format */
3912 (_("%pB: sh_link of section `%pA' points to"
3913 " discarded section `%pA' of `%pB'"),
3914 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3915 /* Point to the kept section if it has the same
3916 size as the discarded one. */
3917 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3920 bfd_set_error (bfd_error_bad_value
);
3925 /* Handle objcopy. */
3926 else if (s
->output_section
== NULL
)
3929 /* xgettext:c-format */
3930 (_("%pB: sh_link of section `%pA' points to"
3931 " removed section `%pA' of `%pB'"),
3932 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3933 bfd_set_error (bfd_error_bad_value
);
3936 s
= s
->output_section
;
3937 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3941 switch (d
->this_hdr
.sh_type
)
3945 /* A reloc section which we are treating as a normal BFD
3946 section. sh_link is the section index of the symbol
3947 table. sh_info is the section index of the section to
3948 which the relocation entries apply. We assume that an
3949 allocated reloc section uses the dynamic symbol table
3950 if there is one. Otherwise we guess the normal symbol
3951 table. FIXME: How can we be sure? */
3952 if (d
->this_hdr
.sh_link
== 0 && (sec
->flags
& SEC_ALLOC
) != 0)
3954 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3956 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3958 if (d
->this_hdr
.sh_link
== 0)
3959 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3961 s
= elf_get_reloc_section (sec
);
3964 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3965 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3970 /* We assume that a section named .stab*str is a stabs
3971 string section. We look for a section with the same name
3972 but without the trailing ``str'', and set its sh_link
3973 field to point to this section. */
3974 if (startswith (sec
->name
, ".stab")
3975 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3980 len
= strlen (sec
->name
);
3981 alc
= (char *) bfd_malloc (len
- 2);
3984 memcpy (alc
, sec
->name
, len
- 3);
3985 alc
[len
- 3] = '\0';
3986 s
= bfd_get_section_by_name (abfd
, alc
);
3990 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3992 /* This is a .stab section. */
3993 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
4000 case SHT_GNU_verneed
:
4001 case SHT_GNU_verdef
:
4002 /* sh_link is the section header index of the string table
4003 used for the dynamic entries, or the symbol table, or the
4005 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4007 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4010 case SHT_GNU_LIBLIST
:
4011 /* sh_link is the section header index of the prelink library
4012 list used for the dynamic entries, or the symbol table, or
4013 the version strings. */
4014 s
= bfd_get_section_by_name (abfd
, ((sec
->flags
& SEC_ALLOC
)
4015 ? ".dynstr" : ".gnu.libstr"));
4017 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4022 case SHT_GNU_versym
:
4023 /* sh_link is the section header index of the symbol table
4024 this hash table or version table is for. */
4025 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4027 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4031 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4035 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4036 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4037 debug section name from .debug_* to .zdebug_* if needed. */
4043 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4045 /* If the backend has a special mapping, use it. */
4046 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4047 if (bed
->elf_backend_sym_is_global
)
4048 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4050 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4051 || bfd_is_und_section (bfd_asymbol_section (sym
))
4052 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4055 /* Filter global symbols of ABFD to include in the import library. All
4056 SYMCOUNT symbols of ABFD can be examined from their pointers in
4057 SYMS. Pointers of symbols to keep should be stored contiguously at
4058 the beginning of that array.
4060 Returns the number of symbols to keep. */
4063 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4064 asymbol
**syms
, long symcount
)
4066 long src_count
, dst_count
= 0;
4068 for (src_count
= 0; src_count
< symcount
; src_count
++)
4070 asymbol
*sym
= syms
[src_count
];
4071 char *name
= (char *) bfd_asymbol_name (sym
);
4072 struct bfd_link_hash_entry
*h
;
4074 if (!sym_is_global (abfd
, sym
))
4077 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, false);
4080 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4082 if (h
->linker_def
|| h
->ldscript_def
)
4085 syms
[dst_count
++] = sym
;
4088 syms
[dst_count
] = NULL
;
4093 /* Don't output section symbols for sections that are not going to be
4094 output, that are duplicates or there is no BFD section. */
4097 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4099 elf_symbol_type
*type_ptr
;
4104 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4107 /* Ignore the section symbol if it isn't used. */
4108 if ((sym
->flags
& BSF_SECTION_SYM_USED
) == 0)
4111 if (sym
->section
== NULL
)
4114 type_ptr
= elf_symbol_from (sym
);
4115 return ((type_ptr
!= NULL
4116 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4117 && bfd_is_abs_section (sym
->section
))
4118 || !(sym
->section
->owner
== abfd
4119 || (sym
->section
->output_section
!= NULL
4120 && sym
->section
->output_section
->owner
== abfd
4121 && sym
->section
->output_offset
== 0)
4122 || bfd_is_abs_section (sym
->section
)));
4125 /* Map symbol from it's internal number to the external number, moving
4126 all local symbols to be at the head of the list. */
4129 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4131 unsigned int symcount
= bfd_get_symcount (abfd
);
4132 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4133 asymbol
**sect_syms
;
4134 unsigned int num_locals
= 0;
4135 unsigned int num_globals
= 0;
4136 unsigned int num_locals2
= 0;
4137 unsigned int num_globals2
= 0;
4138 unsigned int max_index
= 0;
4145 fprintf (stderr
, "elf_map_symbols\n");
4149 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4151 if (max_index
< asect
->index
)
4152 max_index
= asect
->index
;
4156 amt
= max_index
* sizeof (asymbol
*);
4157 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4158 if (sect_syms
== NULL
)
4160 elf_section_syms (abfd
) = sect_syms
;
4161 elf_num_section_syms (abfd
) = max_index
;
4163 /* Init sect_syms entries for any section symbols we have already
4164 decided to output. */
4165 for (idx
= 0; idx
< symcount
; idx
++)
4167 asymbol
*sym
= syms
[idx
];
4169 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4171 && !ignore_section_sym (abfd
, sym
)
4172 && !bfd_is_abs_section (sym
->section
))
4174 asection
*sec
= sym
->section
;
4176 if (sec
->owner
!= abfd
)
4177 sec
= sec
->output_section
;
4179 sect_syms
[sec
->index
] = syms
[idx
];
4183 /* Classify all of the symbols. */
4184 for (idx
= 0; idx
< symcount
; idx
++)
4186 if (sym_is_global (abfd
, syms
[idx
]))
4188 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4192 /* We will be adding a section symbol for each normal BFD section. Most
4193 sections will already have a section symbol in outsymbols, but
4194 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4195 at least in that case. */
4196 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4198 asymbol
*sym
= asect
->symbol
;
4199 /* Don't include ignored section symbols. */
4200 if (!ignore_section_sym (abfd
, sym
)
4201 && sect_syms
[asect
->index
] == NULL
)
4203 if (!sym_is_global (abfd
, asect
->symbol
))
4210 /* Now sort the symbols so the local symbols are first. */
4211 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4212 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4213 if (new_syms
== NULL
)
4216 for (idx
= 0; idx
< symcount
; idx
++)
4218 asymbol
*sym
= syms
[idx
];
4221 if (sym_is_global (abfd
, sym
))
4222 i
= num_locals
+ num_globals2
++;
4223 /* Don't include ignored section symbols. */
4224 else if (!ignore_section_sym (abfd
, sym
))
4229 sym
->udata
.i
= i
+ 1;
4231 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4233 asymbol
*sym
= asect
->symbol
;
4234 if (!ignore_section_sym (abfd
, sym
)
4235 && sect_syms
[asect
->index
] == NULL
)
4239 sect_syms
[asect
->index
] = sym
;
4240 if (!sym_is_global (abfd
, sym
))
4243 i
= num_locals
+ num_globals2
++;
4245 sym
->udata
.i
= i
+ 1;
4249 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4251 *pnum_locals
= num_locals
;
4255 /* Align to the maximum file alignment that could be required for any
4256 ELF data structure. */
4258 static inline file_ptr
4259 align_file_position (file_ptr off
, int align
)
4261 return (off
+ align
- 1) & ~(align
- 1);
4264 /* Assign a file position to a section, optionally aligning to the
4265 required section alignment. */
4268 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4272 if (align
&& i_shdrp
->sh_addralign
> 1)
4273 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
& -i_shdrp
->sh_addralign
);
4274 i_shdrp
->sh_offset
= offset
;
4275 if (i_shdrp
->bfd_section
!= NULL
)
4276 i_shdrp
->bfd_section
->filepos
= offset
;
4277 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4278 offset
+= i_shdrp
->sh_size
;
4282 /* Compute the file positions we are going to put the sections at, and
4283 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4284 is not NULL, this is being called by the ELF backend linker. */
4287 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4288 struct bfd_link_info
*link_info
)
4290 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4291 struct fake_section_arg fsargs
;
4293 struct elf_strtab_hash
*strtab
= NULL
;
4294 Elf_Internal_Shdr
*shstrtab_hdr
;
4297 if (abfd
->output_has_begun
)
4300 /* Do any elf backend specific processing first. */
4301 if (bed
->elf_backend_begin_write_processing
)
4302 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4304 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4307 fsargs
.failed
= false;
4308 fsargs
.link_info
= link_info
;
4309 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4313 if (!assign_section_numbers (abfd
, link_info
))
4316 /* The backend linker builds symbol table information itself. */
4317 need_symtab
= (link_info
== NULL
4318 && (bfd_get_symcount (abfd
) > 0
4319 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4323 /* Non-zero if doing a relocatable link. */
4324 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4326 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
, link_info
))
4331 if (link_info
== NULL
)
4333 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4338 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4339 /* sh_name was set in init_file_header. */
4340 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4341 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4342 shstrtab_hdr
->sh_addr
= 0;
4343 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4344 shstrtab_hdr
->sh_entsize
= 0;
4345 shstrtab_hdr
->sh_link
= 0;
4346 shstrtab_hdr
->sh_info
= 0;
4347 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4348 shstrtab_hdr
->sh_addralign
= 1;
4350 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4356 Elf_Internal_Shdr
*hdr
;
4358 off
= elf_next_file_pos (abfd
);
4360 hdr
= & elf_symtab_hdr (abfd
);
4361 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4363 if (elf_symtab_shndx_list (abfd
) != NULL
)
4365 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4366 if (hdr
->sh_size
!= 0)
4367 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4368 /* FIXME: What about other symtab_shndx sections in the list ? */
4371 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4372 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4374 elf_next_file_pos (abfd
) = off
;
4376 /* Now that we know where the .strtab section goes, write it
4378 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4379 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4381 _bfd_elf_strtab_free (strtab
);
4384 abfd
->output_has_begun
= true;
4389 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4390 function effectively returns whether --eh-frame-hdr is given on the
4391 command line. After size_dynamic_sections the result reflects
4392 whether .eh_frame_hdr will actually be output (sizing isn't done
4393 until ldemul_after_allocation). */
4396 elf_eh_frame_hdr (const struct bfd_link_info
*info
)
4398 if (info
!= NULL
&& is_elf_hash_table (info
->hash
))
4399 return elf_hash_table (info
)->eh_info
.hdr_sec
;
4403 /* Make an initial estimate of the size of the program header. If we
4404 get the number wrong here, we'll redo section placement. */
4406 static bfd_size_type
4407 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4411 const struct elf_backend_data
*bed
;
4413 /* Assume we will need exactly two PT_LOAD segments: one for text
4414 and one for data. */
4417 s
= bfd_get_section_by_name (abfd
, ".interp");
4418 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4420 /* If we have a loadable interpreter section, we need a
4421 PT_INTERP segment. In this case, assume we also need a
4422 PT_PHDR segment, although that may not be true for all
4427 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4429 /* We need a PT_DYNAMIC segment. */
4433 if (info
!= NULL
&& info
->relro
)
4435 /* We need a PT_GNU_RELRO segment. */
4439 if (elf_eh_frame_hdr (info
))
4441 /* We need a PT_GNU_EH_FRAME segment. */
4445 if (elf_stack_flags (abfd
))
4447 /* We need a PT_GNU_STACK segment. */
4451 s
= bfd_get_section_by_name (abfd
,
4452 NOTE_GNU_PROPERTY_SECTION_NAME
);
4453 if (s
!= NULL
&& s
->size
!= 0)
4455 /* We need a PT_GNU_PROPERTY segment. */
4459 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4461 if ((s
->flags
& SEC_LOAD
) != 0
4462 && elf_section_type (s
) == SHT_NOTE
)
4464 unsigned int alignment_power
;
4465 /* We need a PT_NOTE segment. */
4467 /* Try to create just one PT_NOTE segment for all adjacent
4468 loadable SHT_NOTE sections. gABI requires that within a
4469 PT_NOTE segment (and also inside of each SHT_NOTE section)
4470 each note should have the same alignment. So we check
4471 whether the sections are correctly aligned. */
4472 alignment_power
= s
->alignment_power
;
4473 while (s
->next
!= NULL
4474 && s
->next
->alignment_power
== alignment_power
4475 && (s
->next
->flags
& SEC_LOAD
) != 0
4476 && elf_section_type (s
->next
) == SHT_NOTE
)
4481 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4483 if (s
->flags
& SEC_THREAD_LOCAL
)
4485 /* We need a PT_TLS segment. */
4491 bed
= get_elf_backend_data (abfd
);
4493 if ((abfd
->flags
& D_PAGED
) != 0
4494 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4496 /* Add a PT_GNU_MBIND segment for each mbind section. */
4497 bfd_vma commonpagesize
;
4498 unsigned int page_align_power
;
4501 commonpagesize
= info
->commonpagesize
;
4503 commonpagesize
= bed
->commonpagesize
;
4504 page_align_power
= bfd_log2 (commonpagesize
);
4505 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4506 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4508 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4511 /* xgettext:c-format */
4512 (_("%pB: GNU_MBIND section `%pA' has invalid "
4513 "sh_info field: %d"),
4514 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4517 /* Align mbind section to page size. */
4518 if (s
->alignment_power
< page_align_power
)
4519 s
->alignment_power
= page_align_power
;
4524 /* Let the backend count up any program headers it might need. */
4525 if (bed
->elf_backend_additional_program_headers
)
4529 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4535 return segs
* bed
->s
->sizeof_phdr
;
4538 /* Find the segment that contains the output_section of section. */
4541 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4543 struct elf_segment_map
*m
;
4544 Elf_Internal_Phdr
*p
;
4546 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4552 for (i
= m
->count
- 1; i
>= 0; i
--)
4553 if (m
->sections
[i
] == section
)
4560 /* Create a mapping from a set of sections to a program segment. */
4562 static struct elf_segment_map
*
4563 make_mapping (bfd
*abfd
,
4564 asection
**sections
,
4569 struct elf_segment_map
*m
;
4574 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4575 amt
+= (to
- from
) * sizeof (asection
*);
4576 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4580 m
->p_type
= PT_LOAD
;
4581 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4582 m
->sections
[i
- from
] = *hdrpp
;
4583 m
->count
= to
- from
;
4585 if (from
== 0 && phdr
)
4587 /* Include the headers in the first PT_LOAD segment. */
4588 m
->includes_filehdr
= 1;
4589 m
->includes_phdrs
= 1;
4595 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4598 struct elf_segment_map
*
4599 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4601 struct elf_segment_map
*m
;
4603 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4604 sizeof (struct elf_segment_map
));
4608 m
->p_type
= PT_DYNAMIC
;
4610 m
->sections
[0] = dynsec
;
4615 /* Possibly add or remove segments from the segment map. */
4618 elf_modify_segment_map (bfd
*abfd
,
4619 struct bfd_link_info
*info
,
4620 bool remove_empty_load
)
4622 struct elf_segment_map
**m
;
4623 const struct elf_backend_data
*bed
;
4625 /* The placement algorithm assumes that non allocated sections are
4626 not in PT_LOAD segments. We ensure this here by removing such
4627 sections from the segment map. We also remove excluded
4628 sections. Finally, any PT_LOAD segment without sections is
4630 m
= &elf_seg_map (abfd
);
4633 unsigned int i
, new_count
;
4635 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4637 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4638 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4639 || (*m
)->p_type
!= PT_LOAD
))
4641 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4645 (*m
)->count
= new_count
;
4647 if (remove_empty_load
4648 && (*m
)->p_type
== PT_LOAD
4650 && !(*m
)->includes_phdrs
)
4656 bed
= get_elf_backend_data (abfd
);
4657 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4659 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4666 #define IS_TBSS(s) \
4667 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4669 /* Set up a mapping from BFD sections to program segments. Update
4670 NEED_LAYOUT if the section layout is changed. */
4673 _bfd_elf_map_sections_to_segments (bfd
*abfd
,
4674 struct bfd_link_info
*info
,
4678 struct elf_segment_map
*m
;
4679 asection
**sections
= NULL
;
4680 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4683 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4687 info
->user_phdrs
= !no_user_phdrs
;
4689 /* Size the relative relocations if DT_RELR is enabled. */
4690 if (info
->enable_dt_relr
4691 && need_layout
!= NULL
4692 && bed
->size_relative_relocs
4693 && !bed
->size_relative_relocs (info
, need_layout
))
4694 info
->callbacks
->einfo
4695 (_("%F%P: failed to size relative relocations\n"));
4698 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4702 struct elf_segment_map
*mfirst
;
4703 struct elf_segment_map
**pm
;
4706 unsigned int hdr_index
;
4707 bfd_vma maxpagesize
;
4709 bool phdr_in_segment
;
4712 unsigned int tls_count
= 0;
4713 asection
*first_tls
= NULL
;
4714 asection
*first_mbind
= NULL
;
4715 asection
*dynsec
, *eh_frame_hdr
;
4717 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4718 bfd_size_type phdr_size
; /* Octets/bytes. */
4719 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4721 /* Select the allocated sections, and sort them. */
4723 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4724 sections
= (asection
**) bfd_malloc (amt
);
4725 if (sections
== NULL
)
4728 /* Calculate top address, avoiding undefined behaviour of shift
4729 left operator when shift count is equal to size of type
4731 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4732 addr_mask
= (addr_mask
<< 1) + 1;
4735 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4737 if ((s
->flags
& SEC_ALLOC
) != 0)
4739 /* target_index is unused until bfd_elf_final_link
4740 starts output of section symbols. Use it to make
4742 s
->target_index
= i
;
4745 /* A wrapping section potentially clashes with header. */
4746 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4747 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4750 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4753 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4755 phdr_size
= elf_program_header_size (abfd
);
4756 if (phdr_size
== (bfd_size_type
) -1)
4757 phdr_size
= get_program_header_size (abfd
, info
);
4758 phdr_size
+= bed
->s
->sizeof_ehdr
;
4759 /* phdr_size is compared to LMA values which are in bytes. */
4762 maxpagesize
= info
->maxpagesize
;
4764 maxpagesize
= bed
->maxpagesize
;
4765 if (maxpagesize
== 0)
4767 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4769 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4770 >= (phdr_size
& (maxpagesize
- 1))))
4771 /* For compatibility with old scripts that may not be using
4772 SIZEOF_HEADERS, add headers when it looks like space has
4773 been left for them. */
4774 phdr_in_segment
= true;
4776 /* Build the mapping. */
4780 /* If we have a .interp section, then create a PT_PHDR segment for
4781 the program headers and a PT_INTERP segment for the .interp
4783 s
= bfd_get_section_by_name (abfd
, ".interp");
4784 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4786 amt
= sizeof (struct elf_segment_map
);
4787 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4791 m
->p_type
= PT_PHDR
;
4793 m
->p_flags_valid
= 1;
4794 m
->includes_phdrs
= 1;
4795 phdr_in_segment
= true;
4799 amt
= sizeof (struct elf_segment_map
);
4800 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4804 m
->p_type
= PT_INTERP
;
4812 /* Look through the sections. We put sections in the same program
4813 segment when the start of the second section can be placed within
4814 a few bytes of the end of the first section. */
4820 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4822 && (dynsec
->flags
& SEC_LOAD
) == 0)
4825 if ((abfd
->flags
& D_PAGED
) == 0)
4826 phdr_in_segment
= false;
4828 /* Deal with -Ttext or something similar such that the first section
4829 is not adjacent to the program headers. This is an
4830 approximation, since at this point we don't know exactly how many
4831 program headers we will need. */
4832 if (phdr_in_segment
&& count
> 0)
4834 bfd_vma phdr_lma
; /* Bytes. */
4835 bool separate_phdr
= false;
4837 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4839 && info
->separate_code
4840 && (sections
[0]->flags
& SEC_CODE
) != 0)
4842 /* If data sections should be separate from code and
4843 thus not executable, and the first section is
4844 executable then put the file and program headers in
4845 their own PT_LOAD. */
4846 separate_phdr
= true;
4847 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4848 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4850 /* The file and program headers are currently on the
4851 same page as the first section. Put them on the
4852 previous page if we can. */
4853 if (phdr_lma
>= maxpagesize
)
4854 phdr_lma
-= maxpagesize
;
4856 separate_phdr
= false;
4859 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4860 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4861 /* If file and program headers would be placed at the end
4862 of memory then it's probably better to omit them. */
4863 phdr_in_segment
= false;
4864 else if (phdr_lma
< wrap_to
)
4865 /* If a section wraps around to where we'll be placing
4866 file and program headers, then the headers will be
4868 phdr_in_segment
= false;
4869 else if (separate_phdr
)
4871 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4874 m
->p_paddr
= phdr_lma
* opb
;
4876 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4877 m
->p_paddr_valid
= 1;
4880 phdr_in_segment
= false;
4884 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4891 /* See if this section and the last one will fit in the same
4894 if (last_hdr
== NULL
)
4896 /* If we don't have a segment yet, then we don't need a new
4897 one (we build the last one after this loop). */
4898 new_segment
= false;
4900 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4902 /* If this section has a different relation between the
4903 virtual address and the load address, then we need a new
4907 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4908 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4910 /* If this section has a load address that makes it overlap
4911 the previous section, then we need a new segment. */
4914 else if ((abfd
->flags
& D_PAGED
) != 0
4915 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4916 == (hdr
->lma
& -maxpagesize
)))
4918 /* If we are demand paged then we can't map two disk
4919 pages onto the same memory page. */
4920 new_segment
= false;
4922 /* In the next test we have to be careful when last_hdr->lma is close
4923 to the end of the address space. If the aligned address wraps
4924 around to the start of the address space, then there are no more
4925 pages left in memory and it is OK to assume that the current
4926 section can be included in the current segment. */
4927 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4928 + maxpagesize
> last_hdr
->lma
)
4929 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4930 + maxpagesize
<= hdr
->lma
))
4932 /* If putting this section in this segment would force us to
4933 skip a page in the segment, then we need a new segment. */
4936 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4937 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4939 /* We don't want to put a loaded section after a
4940 nonloaded (ie. bss style) section in the same segment
4941 as that will force the non-loaded section to be loaded.
4942 Consider .tbss sections as loaded for this purpose. */
4945 else if ((abfd
->flags
& D_PAGED
) == 0)
4947 /* If the file is not demand paged, which means that we
4948 don't require the sections to be correctly aligned in the
4949 file, then there is no other reason for a new segment. */
4950 new_segment
= false;
4952 else if (info
!= NULL
4953 && info
->separate_code
4954 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4959 && (hdr
->flags
& SEC_READONLY
) == 0)
4961 /* We don't want to put a writable section in a read only
4967 /* Otherwise, we can use the same segment. */
4968 new_segment
= false;
4971 /* Allow interested parties a chance to override our decision. */
4972 if (last_hdr
!= NULL
4974 && info
->callbacks
->override_segment_assignment
!= NULL
)
4976 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4982 if ((hdr
->flags
& SEC_READONLY
) == 0)
4984 if ((hdr
->flags
& SEC_CODE
) != 0)
4987 /* .tbss sections effectively have zero size. */
4988 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4992 /* We need a new program segment. We must create a new program
4993 header holding all the sections from hdr_index until hdr. */
4995 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5002 if ((hdr
->flags
& SEC_READONLY
) == 0)
5007 if ((hdr
->flags
& SEC_CODE
) == 0)
5013 /* .tbss sections effectively have zero size. */
5014 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5016 phdr_in_segment
= false;
5019 /* Create a final PT_LOAD program segment, but not if it's just
5021 if (last_hdr
!= NULL
5022 && (i
- hdr_index
!= 1
5023 || !IS_TBSS (last_hdr
)))
5025 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5033 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5036 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5043 /* For each batch of consecutive loadable SHT_NOTE sections,
5044 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5045 because if we link together nonloadable .note sections and
5046 loadable .note sections, we will generate two .note sections
5047 in the output file. */
5048 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5050 if ((s
->flags
& SEC_LOAD
) != 0
5051 && elf_section_type (s
) == SHT_NOTE
)
5054 unsigned int alignment_power
= s
->alignment_power
;
5057 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5059 if (s2
->next
->alignment_power
== alignment_power
5060 && (s2
->next
->flags
& SEC_LOAD
) != 0
5061 && elf_section_type (s2
->next
) == SHT_NOTE
5062 && align_power (s2
->lma
+ s2
->size
/ opb
,
5069 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5070 amt
+= count
* sizeof (asection
*);
5071 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5075 m
->p_type
= PT_NOTE
;
5079 m
->sections
[m
->count
- count
--] = s
;
5080 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5083 m
->sections
[m
->count
- 1] = s
;
5084 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5088 if (s
->flags
& SEC_THREAD_LOCAL
)
5094 if (first_mbind
== NULL
5095 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5099 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5102 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5103 amt
+= tls_count
* sizeof (asection
*);
5104 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5109 m
->count
= tls_count
;
5110 /* Mandated PF_R. */
5112 m
->p_flags_valid
= 1;
5114 for (i
= 0; i
< tls_count
; ++i
)
5116 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5119 (_("%pB: TLS sections are not adjacent:"), abfd
);
5122 while (i
< tls_count
)
5124 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5126 _bfd_error_handler (_(" TLS: %pA"), s
);
5130 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5133 bfd_set_error (bfd_error_bad_value
);
5145 && (abfd
->flags
& D_PAGED
) != 0
5146 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5147 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5148 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5149 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5151 /* Mandated PF_R. */
5152 unsigned long p_flags
= PF_R
;
5153 if ((s
->flags
& SEC_READONLY
) == 0)
5155 if ((s
->flags
& SEC_CODE
) != 0)
5158 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5159 m
= bfd_zalloc (abfd
, amt
);
5163 m
->p_type
= (PT_GNU_MBIND_LO
5164 + elf_section_data (s
)->this_hdr
.sh_info
);
5166 m
->p_flags_valid
= 1;
5168 m
->p_flags
= p_flags
;
5174 s
= bfd_get_section_by_name (abfd
,
5175 NOTE_GNU_PROPERTY_SECTION_NAME
);
5176 if (s
!= NULL
&& s
->size
!= 0)
5178 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5179 m
= bfd_zalloc (abfd
, amt
);
5183 m
->p_type
= PT_GNU_PROPERTY
;
5185 m
->p_flags_valid
= 1;
5192 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5194 eh_frame_hdr
= elf_eh_frame_hdr (info
);
5195 if (eh_frame_hdr
!= NULL
5196 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5198 amt
= sizeof (struct elf_segment_map
);
5199 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5203 m
->p_type
= PT_GNU_EH_FRAME
;
5205 m
->sections
[0] = eh_frame_hdr
->output_section
;
5211 if (elf_stack_flags (abfd
))
5213 amt
= sizeof (struct elf_segment_map
);
5214 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5218 m
->p_type
= PT_GNU_STACK
;
5219 m
->p_flags
= elf_stack_flags (abfd
);
5220 m
->p_align
= bed
->stack_align
;
5221 m
->p_flags_valid
= 1;
5222 m
->p_align_valid
= m
->p_align
!= 0;
5223 if (info
->stacksize
> 0)
5225 m
->p_size
= info
->stacksize
;
5226 m
->p_size_valid
= 1;
5233 if (info
!= NULL
&& info
->relro
)
5235 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5237 if (m
->p_type
== PT_LOAD
5239 && m
->sections
[0]->vma
>= info
->relro_start
5240 && m
->sections
[0]->vma
< info
->relro_end
)
5243 while (--i
!= (unsigned) -1)
5245 if (m
->sections
[i
]->size
> 0
5246 && (m
->sections
[i
]->flags
& SEC_LOAD
) != 0
5247 && (m
->sections
[i
]->flags
& SEC_HAS_CONTENTS
) != 0)
5251 if (i
!= (unsigned) -1)
5256 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5259 amt
= sizeof (struct elf_segment_map
);
5260 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5264 m
->p_type
= PT_GNU_RELRO
;
5271 elf_seg_map (abfd
) = mfirst
;
5274 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5277 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5279 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5288 /* Sort sections by address. */
5291 elf_sort_sections (const void *arg1
, const void *arg2
)
5293 const asection
*sec1
= *(const asection
**) arg1
;
5294 const asection
*sec2
= *(const asection
**) arg2
;
5295 bfd_size_type size1
, size2
;
5297 /* Sort by LMA first, since this is the address used to
5298 place the section into a segment. */
5299 if (sec1
->lma
< sec2
->lma
)
5301 else if (sec1
->lma
> sec2
->lma
)
5304 /* Then sort by VMA. Normally the LMA and the VMA will be
5305 the same, and this will do nothing. */
5306 if (sec1
->vma
< sec2
->vma
)
5308 else if (sec1
->vma
> sec2
->vma
)
5311 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5313 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5321 else if (TOEND (sec2
))
5326 /* Sort by size, to put zero sized sections
5327 before others at the same address. */
5329 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5330 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5337 return sec1
->target_index
- sec2
->target_index
;
5340 /* This qsort comparison functions sorts PT_LOAD segments first and
5341 by p_paddr, for assign_file_positions_for_load_sections. */
5344 elf_sort_segments (const void *arg1
, const void *arg2
)
5346 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5347 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5349 if (m1
->p_type
!= m2
->p_type
)
5351 if (m1
->p_type
== PT_NULL
)
5353 if (m2
->p_type
== PT_NULL
)
5355 return m1
->p_type
< m2
->p_type
? -1 : 1;
5357 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5358 return m1
->includes_filehdr
? -1 : 1;
5359 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5360 return m1
->no_sort_lma
? -1 : 1;
5361 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5363 bfd_vma lma1
, lma2
; /* Octets. */
5365 if (m1
->p_paddr_valid
)
5367 else if (m1
->count
!= 0)
5369 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5371 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5374 if (m2
->p_paddr_valid
)
5376 else if (m2
->count
!= 0)
5378 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5380 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5383 return lma1
< lma2
? -1 : 1;
5385 if (m1
->idx
!= m2
->idx
)
5386 return m1
->idx
< m2
->idx
? -1 : 1;
5390 /* Ian Lance Taylor writes:
5392 We shouldn't be using % with a negative signed number. That's just
5393 not good. We have to make sure either that the number is not
5394 negative, or that the number has an unsigned type. When the types
5395 are all the same size they wind up as unsigned. When file_ptr is a
5396 larger signed type, the arithmetic winds up as signed long long,
5399 What we're trying to say here is something like ``increase OFF by
5400 the least amount that will cause it to be equal to the VMA modulo
5402 /* In other words, something like:
5404 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5405 off_offset = off % bed->maxpagesize;
5406 if (vma_offset < off_offset)
5407 adjustment = vma_offset + bed->maxpagesize - off_offset;
5409 adjustment = vma_offset - off_offset;
5411 which can be collapsed into the expression below. */
5414 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5416 /* PR binutils/16199: Handle an alignment of zero. */
5417 if (maxpagesize
== 0)
5419 return ((vma
- off
) % maxpagesize
);
5423 print_segment_map (const struct elf_segment_map
*m
)
5426 const char *pt
= get_segment_type (m
->p_type
);
5431 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5432 sprintf (buf
, "LOPROC+%7.7x",
5433 (unsigned int) (m
->p_type
- PT_LOPROC
));
5434 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5435 sprintf (buf
, "LOOS+%7.7x",
5436 (unsigned int) (m
->p_type
- PT_LOOS
));
5438 snprintf (buf
, sizeof (buf
), "%8.8x",
5439 (unsigned int) m
->p_type
);
5443 fprintf (stderr
, "%s:", pt
);
5444 for (j
= 0; j
< m
->count
; j
++)
5445 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5451 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5456 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5458 buf
= bfd_zmalloc (len
);
5461 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5466 /* Assign file positions to the sections based on the mapping from
5467 sections to segments. This function also sets up some fields in
5471 assign_file_positions_for_load_sections (bfd
*abfd
,
5472 struct bfd_link_info
*link_info
)
5474 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5475 struct elf_segment_map
*m
;
5476 struct elf_segment_map
*phdr_load_seg
;
5477 Elf_Internal_Phdr
*phdrs
;
5478 Elf_Internal_Phdr
*p
;
5479 file_ptr off
; /* Octets. */
5480 bfd_size_type maxpagesize
;
5481 unsigned int alloc
, actual
;
5483 struct elf_segment_map
**sorted_seg_map
;
5484 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5486 if (link_info
== NULL
5487 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
, NULL
))
5491 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5496 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5497 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5501 /* PR binutils/12467. */
5502 elf_elfheader (abfd
)->e_phoff
= 0;
5503 elf_elfheader (abfd
)->e_phentsize
= 0;
5506 elf_elfheader (abfd
)->e_phnum
= alloc
;
5508 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5511 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5515 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5516 BFD_ASSERT (elf_program_header_size (abfd
)
5517 == actual
* bed
->s
->sizeof_phdr
);
5518 BFD_ASSERT (actual
>= alloc
);
5523 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5527 /* We're writing the size in elf_program_header_size (abfd),
5528 see assign_file_positions_except_relocs, so make sure we have
5529 that amount allocated, with trailing space cleared.
5530 The variable alloc contains the computed need, while
5531 elf_program_header_size (abfd) contains the size used for the
5533 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5534 where the layout is forced to according to a larger size in the
5535 last iterations for the testcase ld-elf/header. */
5536 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5537 + alloc
* sizeof (*sorted_seg_map
)));
5538 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5539 elf_tdata (abfd
)->phdr
= phdrs
;
5543 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5545 sorted_seg_map
[j
] = m
;
5546 /* If elf_segment_map is not from map_sections_to_segments, the
5547 sections may not be correctly ordered. NOTE: sorting should
5548 not be done to the PT_NOTE section of a corefile, which may
5549 contain several pseudo-sections artificially created by bfd.
5550 Sorting these pseudo-sections breaks things badly. */
5552 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5553 && m
->p_type
== PT_NOTE
))
5555 for (i
= 0; i
< m
->count
; i
++)
5556 m
->sections
[i
]->target_index
= i
;
5557 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5562 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5566 if ((abfd
->flags
& D_PAGED
) != 0)
5568 if (link_info
!= NULL
)
5569 maxpagesize
= link_info
->maxpagesize
;
5571 maxpagesize
= bed
->maxpagesize
;
5574 /* Sections must map to file offsets past the ELF file header. */
5575 off
= bed
->s
->sizeof_ehdr
;
5576 /* And if one of the PT_LOAD headers doesn't include the program
5577 headers then we'll be mapping program headers in the usual
5578 position after the ELF file header. */
5579 phdr_load_seg
= NULL
;
5580 for (j
= 0; j
< alloc
; j
++)
5582 m
= sorted_seg_map
[j
];
5583 if (m
->p_type
!= PT_LOAD
)
5585 if (m
->includes_phdrs
)
5591 if (phdr_load_seg
== NULL
)
5592 off
+= actual
* bed
->s
->sizeof_phdr
;
5594 for (j
= 0; j
< alloc
; j
++)
5597 bfd_vma off_adjust
; /* Octets. */
5599 bfd_size_type p_align
;
5602 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5603 number of sections with contents contributing to both p_filesz
5604 and p_memsz, followed by a number of sections with no contents
5605 that just contribute to p_memsz. In this loop, OFF tracks next
5606 available file offset for PT_LOAD and PT_NOTE segments. */
5607 m
= sorted_seg_map
[j
];
5609 p
->p_type
= m
->p_type
;
5610 p
->p_flags
= m
->p_flags
;
5611 p_align
= bed
->p_align
;
5615 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5617 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5619 if (m
->p_paddr_valid
)
5620 p
->p_paddr
= m
->p_paddr
;
5621 else if (m
->count
== 0)
5624 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5626 if (p
->p_type
== PT_LOAD
5627 && (abfd
->flags
& D_PAGED
) != 0)
5629 /* p_align in demand paged PT_LOAD segments effectively stores
5630 the maximum page size. When copying an executable with
5631 objcopy, we set m->p_align from the input file. Use this
5632 value for maxpagesize rather than bed->maxpagesize, which
5633 may be different. Note that we use maxpagesize for PT_TLS
5634 segment alignment later in this function, so we are relying
5635 on at least one PT_LOAD segment appearing before a PT_TLS
5637 if (m
->p_align_valid
)
5638 maxpagesize
= m
->p_align
;
5639 else if (p_align
!= 0
5640 && (link_info
== NULL
5641 || !link_info
->maxpagesize_is_set
))
5642 /* Set p_align to the default p_align value while laying
5643 out segments aligning to the maximum page size or the
5644 largest section alignment. The run-time loader can
5645 align segments to the default p_align value or the
5646 maximum page size, depending on system page size. */
5649 p
->p_align
= maxpagesize
;
5651 else if (m
->p_align_valid
)
5652 p
->p_align
= m
->p_align
;
5653 else if (m
->count
== 0)
5654 p
->p_align
= 1 << bed
->s
->log_file_align
;
5656 if (m
== phdr_load_seg
)
5658 if (!m
->includes_filehdr
)
5660 off
+= actual
* bed
->s
->sizeof_phdr
;
5663 no_contents
= false;
5665 if (p
->p_type
== PT_LOAD
5668 bfd_size_type align
; /* Bytes. */
5669 unsigned int align_power
= 0;
5671 if (m
->p_align_valid
)
5675 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5677 unsigned int secalign
;
5679 secalign
= bfd_section_alignment (*secpp
);
5680 if (secalign
> align_power
)
5681 align_power
= secalign
;
5683 align
= (bfd_size_type
) 1 << align_power
;
5684 if (align
< maxpagesize
)
5686 /* If a section requires alignment higher than the
5687 default p_align value, don't set p_align to the
5688 default p_align value. */
5689 if (align
> p_align
)
5691 align
= maxpagesize
;
5695 /* If a section requires alignment higher than the
5696 maximum page size, set p_align to the section
5703 for (i
= 0; i
< m
->count
; i
++)
5704 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5705 /* If we aren't making room for this section, then
5706 it must be SHT_NOBITS regardless of what we've
5707 set via struct bfd_elf_special_section. */
5708 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5710 /* Find out whether this segment contains any loadable
5713 for (i
= 0; i
< m
->count
; i
++)
5714 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5716 no_contents
= false;
5720 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5722 /* Broken hardware and/or kernel require that files do not
5723 map the same page with different permissions on some hppa
5726 && (abfd
->flags
& D_PAGED
) != 0
5727 && bed
->no_page_alias
5728 && (off
& (maxpagesize
- 1)) != 0
5729 && ((off
& -maxpagesize
)
5730 == ((off
+ off_adjust
) & -maxpagesize
)))
5731 off_adjust
+= maxpagesize
;
5735 /* We shouldn't need to align the segment on disk since
5736 the segment doesn't need file space, but the gABI
5737 arguably requires the alignment and glibc ld.so
5738 checks it. So to comply with the alignment
5739 requirement but not waste file space, we adjust
5740 p_offset for just this segment. (OFF_ADJUST is
5741 subtracted from OFF later.) This may put p_offset
5742 past the end of file, but that shouldn't matter. */
5747 /* Make sure the .dynamic section is the first section in the
5748 PT_DYNAMIC segment. */
5749 else if (p
->p_type
== PT_DYNAMIC
5751 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5754 (_("%pB: The first section in the PT_DYNAMIC segment"
5755 " is not the .dynamic section"),
5757 bfd_set_error (bfd_error_bad_value
);
5760 /* Set the note section type to SHT_NOTE. */
5761 else if (p
->p_type
== PT_NOTE
)
5762 for (i
= 0; i
< m
->count
; i
++)
5763 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5765 if (m
->includes_filehdr
)
5767 if (!m
->p_flags_valid
)
5769 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5770 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5771 if (p
->p_type
== PT_LOAD
)
5775 if (p
->p_vaddr
< (bfd_vma
) off
5776 || (!m
->p_paddr_valid
5777 && p
->p_paddr
< (bfd_vma
) off
))
5780 (_("%pB: not enough room for program headers,"
5781 " try linking with -N"),
5783 bfd_set_error (bfd_error_bad_value
);
5787 if (!m
->p_paddr_valid
)
5791 else if (sorted_seg_map
[0]->includes_filehdr
)
5793 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5794 p
->p_vaddr
= filehdr
->p_vaddr
;
5795 if (!m
->p_paddr_valid
)
5796 p
->p_paddr
= filehdr
->p_paddr
;
5800 if (m
->includes_phdrs
)
5802 if (!m
->p_flags_valid
)
5804 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5805 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5806 if (!m
->includes_filehdr
)
5808 if (p
->p_type
== PT_LOAD
)
5810 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5813 p
->p_vaddr
-= off
- p
->p_offset
;
5814 if (!m
->p_paddr_valid
)
5815 p
->p_paddr
-= off
- p
->p_offset
;
5818 else if (phdr_load_seg
!= NULL
)
5820 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5821 bfd_vma phdr_off
= 0; /* Octets. */
5822 if (phdr_load_seg
->includes_filehdr
)
5823 phdr_off
= bed
->s
->sizeof_ehdr
;
5824 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5825 if (!m
->p_paddr_valid
)
5826 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5827 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5830 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5834 if (p
->p_type
== PT_LOAD
5835 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5837 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5842 /* Put meaningless p_offset for PT_LOAD segments
5843 without file contents somewhere within the first
5844 page, in an attempt to not point past EOF. */
5845 bfd_size_type align
= maxpagesize
;
5846 if (align
< p
->p_align
)
5850 p
->p_offset
= off
% align
;
5855 file_ptr adjust
; /* Octets. */
5857 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5859 p
->p_filesz
+= adjust
;
5860 p
->p_memsz
+= adjust
;
5864 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5865 maps. Set filepos for sections in PT_LOAD segments, and in
5866 core files, for sections in PT_NOTE segments.
5867 assign_file_positions_for_non_load_sections will set filepos
5868 for other sections and update p_filesz for other segments. */
5869 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5872 bfd_size_type align
;
5873 Elf_Internal_Shdr
*this_hdr
;
5876 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5877 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5879 if ((p
->p_type
== PT_LOAD
5880 || p
->p_type
== PT_TLS
)
5881 && (this_hdr
->sh_type
!= SHT_NOBITS
5882 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5883 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5884 || p
->p_type
== PT_TLS
))))
5886 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5887 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5888 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5889 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5893 || p_end
< p_start
))
5896 /* xgettext:c-format */
5897 (_("%pB: section %pA lma %#" PRIx64
5898 " adjusted to %#" PRIx64
),
5899 abfd
, sec
, (uint64_t) s_start
/ opb
,
5900 (uint64_t) p_end
/ opb
);
5902 sec
->lma
= p_end
/ opb
;
5904 p
->p_memsz
+= adjust
;
5906 if (p
->p_type
== PT_LOAD
)
5908 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5911 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5913 /* We have a PROGBITS section following NOBITS ones.
5914 Allocate file space for the NOBITS section(s) and
5916 adjust
= p
->p_memsz
- p
->p_filesz
;
5917 if (!write_zeros (abfd
, off
, adjust
))
5921 /* We only adjust sh_offset in SHT_NOBITS sections
5922 as would seem proper for their address when the
5923 section is first in the segment. sh_offset
5924 doesn't really have any significance for
5925 SHT_NOBITS anyway, apart from a notional position
5926 relative to other sections. Historically we
5927 didn't bother with adjusting sh_offset and some
5928 programs depend on it not being adjusted. See
5929 pr12921 and pr25662. */
5930 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
5933 if (this_hdr
->sh_type
== SHT_NOBITS
)
5934 off_adjust
+= adjust
;
5937 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5938 p
->p_filesz
+= adjust
;
5941 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5943 /* The section at i == 0 is the one that actually contains
5947 this_hdr
->sh_offset
= sec
->filepos
= off
;
5948 off
+= this_hdr
->sh_size
;
5949 p
->p_filesz
= this_hdr
->sh_size
;
5955 /* The rest are fake sections that shouldn't be written. */
5964 if (p
->p_type
== PT_LOAD
)
5966 this_hdr
->sh_offset
= sec
->filepos
= off
;
5967 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5968 off
+= this_hdr
->sh_size
;
5970 else if (this_hdr
->sh_type
== SHT_NOBITS
5971 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5972 && this_hdr
->sh_offset
== 0)
5974 /* This is a .tbss section that didn't get a PT_LOAD.
5975 (See _bfd_elf_map_sections_to_segments "Create a
5976 final PT_LOAD".) Set sh_offset to the value it
5977 would have if we had created a zero p_filesz and
5978 p_memsz PT_LOAD header for the section. This
5979 also makes the PT_TLS header have the same
5981 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5983 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5986 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5988 p
->p_filesz
+= this_hdr
->sh_size
;
5989 /* A load section without SHF_ALLOC is something like
5990 a note section in a PT_NOTE segment. These take
5991 file space but are not loaded into memory. */
5992 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5993 p
->p_memsz
+= this_hdr
->sh_size
;
5995 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5997 if (p
->p_type
== PT_TLS
)
5998 p
->p_memsz
+= this_hdr
->sh_size
;
6000 /* .tbss is special. It doesn't contribute to p_memsz of
6002 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
6003 p
->p_memsz
+= this_hdr
->sh_size
;
6006 if (align
> p
->p_align
6007 && !m
->p_align_valid
6008 && (p
->p_type
!= PT_LOAD
6009 || (abfd
->flags
& D_PAGED
) == 0))
6013 if (!m
->p_flags_valid
)
6016 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
6018 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
6025 /* PR ld/20815 - Check that the program header segment, if
6026 present, will be loaded into memory. */
6027 if (p
->p_type
== PT_PHDR
6028 && phdr_load_seg
== NULL
6029 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
6030 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
6032 /* The fix for this error is usually to edit the linker script being
6033 used and set up the program headers manually. Either that or
6034 leave room for the headers at the start of the SECTIONS. */
6035 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6036 " by LOAD segment"),
6038 if (link_info
== NULL
)
6040 /* Arrange for the linker to exit with an error, deleting
6041 the output file unless --noinhibit-exec is given. */
6042 link_info
->callbacks
->info ("%X");
6045 /* Check that all sections are in a PT_LOAD segment.
6046 Don't check funky gdb generated core files. */
6047 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
6049 bool check_vma
= true;
6051 for (i
= 1; i
< m
->count
; i
++)
6052 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
6053 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
6054 ->this_hdr
), p
) != 0
6055 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
6056 ->this_hdr
), p
) != 0)
6058 /* Looks like we have overlays packed into the segment. */
6063 for (i
= 0; i
< m
->count
; i
++)
6065 Elf_Internal_Shdr
*this_hdr
;
6068 sec
= m
->sections
[i
];
6069 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6070 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6071 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6074 /* xgettext:c-format */
6075 (_("%pB: section `%pA' can't be allocated in segment %d"),
6077 print_segment_map (m
);
6082 p
->p_align
= p_align
;
6086 elf_next_file_pos (abfd
) = off
;
6088 if (link_info
!= NULL
6089 && phdr_load_seg
!= NULL
6090 && phdr_load_seg
->includes_filehdr
)
6092 /* There is a segment that contains both the file headers and the
6093 program headers, so provide a symbol __ehdr_start pointing there.
6094 A program can use this to examine itself robustly. */
6096 struct elf_link_hash_entry
*hash
6097 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6098 false, false, true);
6099 /* If the symbol was referenced and not defined, define it. */
6101 && (hash
->root
.type
== bfd_link_hash_new
6102 || hash
->root
.type
== bfd_link_hash_undefined
6103 || hash
->root
.type
== bfd_link_hash_undefweak
6104 || hash
->root
.type
== bfd_link_hash_common
))
6107 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6109 if (phdr_load_seg
->count
!= 0)
6110 /* The segment contains sections, so use the first one. */
6111 s
= phdr_load_seg
->sections
[0];
6113 /* Use the first (i.e. lowest-addressed) section in any segment. */
6114 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6115 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6123 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6124 hash
->root
.u
.def
.section
= s
;
6128 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6129 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6132 hash
->root
.type
= bfd_link_hash_defined
;
6133 hash
->def_regular
= 1;
6141 /* Determine if a bfd is a debuginfo file. Unfortunately there
6142 is no defined method for detecting such files, so we have to
6143 use heuristics instead. */
6146 is_debuginfo_file (bfd
*abfd
)
6148 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6151 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6152 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6153 Elf_Internal_Shdr
**headerp
;
6155 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6157 Elf_Internal_Shdr
*header
= * headerp
;
6159 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6160 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6161 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6162 && header
->sh_type
!= SHT_NOBITS
6163 && header
->sh_type
!= SHT_NOTE
)
6170 /* Assign file positions for other sections, except for compressed debug
6171 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6174 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6175 struct bfd_link_info
*link_info
)
6177 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6178 Elf_Internal_Shdr
**i_shdrpp
;
6179 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6180 Elf_Internal_Phdr
*phdrs
;
6181 Elf_Internal_Phdr
*p
;
6182 struct elf_segment_map
*m
;
6184 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6185 bfd_vma maxpagesize
;
6187 if (link_info
!= NULL
)
6188 maxpagesize
= link_info
->maxpagesize
;
6190 maxpagesize
= bed
->maxpagesize
;
6191 i_shdrpp
= elf_elfsections (abfd
);
6192 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6193 off
= elf_next_file_pos (abfd
);
6194 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6196 Elf_Internal_Shdr
*hdr
;
6200 if (hdr
->bfd_section
!= NULL
6201 && (hdr
->bfd_section
->filepos
!= 0
6202 || (hdr
->sh_type
== SHT_NOBITS
6203 && hdr
->contents
== NULL
)))
6204 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6205 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6207 if (hdr
->sh_size
!= 0
6208 /* PR 24717 - debuginfo files are known to be not strictly
6209 compliant with the ELF standard. In particular they often
6210 have .note.gnu.property sections that are outside of any
6211 loadable segment. This is not a problem for such files,
6212 so do not warn about them. */
6213 && ! is_debuginfo_file (abfd
))
6215 /* xgettext:c-format */
6216 (_("%pB: warning: allocated section `%s' not in segment"),
6218 (hdr
->bfd_section
== NULL
6220 : hdr
->bfd_section
->name
));
6221 /* We don't need to page align empty sections. */
6222 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6223 align
= maxpagesize
;
6225 align
= hdr
->sh_addralign
& -hdr
->sh_addralign
;
6226 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
, align
);
6227 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6230 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6231 && hdr
->bfd_section
== NULL
)
6232 /* We don't know the offset of these sections yet:
6233 their size has not been decided. */
6234 || (hdr
->bfd_section
!= NULL
6235 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6236 || (bfd_section_is_ctf (hdr
->bfd_section
)
6237 && abfd
->is_linker_output
)))
6238 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6239 || (elf_symtab_shndx_list (abfd
) != NULL
6240 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6241 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6242 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6243 hdr
->sh_offset
= -1;
6245 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6247 elf_next_file_pos (abfd
) = off
;
6249 /* Now that we have set the section file positions, we can set up
6250 the file positions for the non PT_LOAD segments. */
6251 phdrs
= elf_tdata (abfd
)->phdr
;
6252 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6254 if (p
->p_type
== PT_GNU_RELRO
)
6256 bfd_vma start
, end
; /* Bytes. */
6259 if (link_info
!= NULL
)
6261 /* During linking the range of the RELRO segment is passed
6262 in link_info. Note that there may be padding between
6263 relro_start and the first RELRO section. */
6264 start
= link_info
->relro_start
;
6265 end
= link_info
->relro_end
;
6267 else if (m
->count
!= 0)
6269 if (!m
->p_size_valid
)
6271 start
= m
->sections
[0]->vma
;
6272 end
= start
+ m
->p_size
/ opb
;
6283 struct elf_segment_map
*lm
;
6284 const Elf_Internal_Phdr
*lp
;
6287 /* Find a LOAD segment containing a section in the RELRO
6289 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6291 lm
= lm
->next
, lp
++)
6293 if (lp
->p_type
== PT_LOAD
6295 && (lm
->sections
[lm
->count
- 1]->vma
6296 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6297 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6299 && lm
->sections
[0]->vma
< end
)
6305 /* Find the section starting the RELRO segment. */
6306 for (i
= 0; i
< lm
->count
; i
++)
6308 asection
*s
= lm
->sections
[i
];
6317 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6318 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6319 p
->p_offset
= lm
->sections
[i
]->filepos
;
6320 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6321 p
->p_filesz
= p
->p_memsz
;
6323 /* The RELRO segment typically ends a few bytes
6324 into .got.plt but other layouts are possible.
6325 In cases where the end does not match any
6326 loaded section (for instance is in file
6327 padding), trim p_filesz back to correspond to
6328 the end of loaded section contents. */
6329 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6330 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6332 /* Preserve the alignment and flags if they are
6333 valid. The gold linker generates RW/4 for
6334 the PT_GNU_RELRO section. It is better for
6335 objcopy/strip to honor these attributes
6336 otherwise gdb will choke when using separate
6338 if (!m
->p_align_valid
)
6340 if (!m
->p_flags_valid
)
6349 if (link_info
!= NULL
)
6351 (_("%pB: warning: unable to allocate any sections"
6352 " to PT_GNU_RELRO segment"),
6354 memset (p
, 0, sizeof *p
);
6357 else if (p
->p_type
== PT_GNU_STACK
)
6359 if (m
->p_size_valid
)
6360 p
->p_memsz
= m
->p_size
;
6362 else if (m
->count
!= 0)
6366 if (p
->p_type
!= PT_LOAD
6367 && (p
->p_type
!= PT_NOTE
6368 || bfd_get_format (abfd
) != bfd_core
))
6370 /* A user specified segment layout may include a PHDR
6371 segment that overlaps with a LOAD segment... */
6372 if (p
->p_type
== PT_PHDR
)
6378 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6380 /* PR 17512: file: 2195325e. */
6382 (_("%pB: error: non-load segment %d includes file header "
6383 "and/or program header"),
6384 abfd
, (int) (p
- phdrs
));
6389 p
->p_offset
= m
->sections
[0]->filepos
;
6390 for (i
= m
->count
; i
-- != 0;)
6392 asection
*sect
= m
->sections
[i
];
6393 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6394 if (hdr
->sh_type
!= SHT_NOBITS
)
6396 p
->p_filesz
= sect
->filepos
- p
->p_offset
+ hdr
->sh_size
;
6397 /* NB: p_memsz of the loadable PT_NOTE segment
6398 should be the same as p_filesz. */
6399 if (p
->p_type
== PT_NOTE
6400 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
6401 p
->p_memsz
= p
->p_filesz
;
6412 static elf_section_list
*
6413 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6415 for (;list
!= NULL
; list
= list
->next
)
6421 /* Work out the file positions of all the sections. This is called by
6422 _bfd_elf_compute_section_file_positions. All the section sizes and
6423 VMAs must be known before this is called.
6425 Reloc sections come in two flavours: Those processed specially as
6426 "side-channel" data attached to a section to which they apply, and
6427 those that bfd doesn't process as relocations. The latter sort are
6428 stored in a normal bfd section by bfd_section_from_shdr. We don't
6429 consider the former sort here, unless they form part of the loadable
6430 image. Reloc sections not assigned here (and compressed debugging
6431 sections and CTF sections which nothing else in the file can rely
6432 upon) will be handled later by assign_file_positions_for_relocs.
6434 We also don't set the positions of the .symtab and .strtab here. */
6437 assign_file_positions_except_relocs (bfd
*abfd
,
6438 struct bfd_link_info
*link_info
)
6440 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6441 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6442 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6445 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6446 && bfd_get_format (abfd
) != bfd_core
)
6448 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6449 unsigned int num_sec
= elf_numsections (abfd
);
6450 Elf_Internal_Shdr
**hdrpp
;
6454 /* Start after the ELF header. */
6455 off
= i_ehdrp
->e_ehsize
;
6457 /* We are not creating an executable, which means that we are
6458 not creating a program header, and that the actual order of
6459 the sections in the file is unimportant. */
6460 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6462 Elf_Internal_Shdr
*hdr
;
6465 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6466 && hdr
->bfd_section
== NULL
)
6467 /* Do not assign offsets for these sections yet: we don't know
6469 || (hdr
->bfd_section
!= NULL
6470 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6471 || (bfd_section_is_ctf (hdr
->bfd_section
)
6472 && abfd
->is_linker_output
)))
6473 || i
== elf_onesymtab (abfd
)
6474 || (elf_symtab_shndx_list (abfd
) != NULL
6475 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6476 || i
== elf_strtab_sec (abfd
)
6477 || i
== elf_shstrtab_sec (abfd
))
6479 hdr
->sh_offset
= -1;
6482 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6485 elf_next_file_pos (abfd
) = off
;
6486 elf_program_header_size (abfd
) = 0;
6490 /* Assign file positions for the loaded sections based on the
6491 assignment of sections to segments. */
6492 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6495 /* And for non-load sections. */
6496 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6500 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6503 /* Write out the program headers. */
6504 alloc
= i_ehdrp
->e_phnum
;
6507 if (link_info
!= NULL
&& ! link_info
->no_warn_rwx_segments
)
6509 /* Memory resident segments with non-zero size and RWX
6510 permissions are a security risk, so we generate a warning
6511 here if we are creating any. */
6514 for (i
= 0; i
< alloc
; i
++)
6516 const Elf_Internal_Phdr
* phdr
= tdata
->phdr
+ i
;
6518 if (phdr
->p_memsz
== 0)
6521 if (phdr
->p_type
== PT_TLS
&& (phdr
->p_flags
& PF_X
))
6522 _bfd_error_handler (_("warning: %pB has a TLS segment"
6523 " with execute permission"),
6525 else if (phdr
->p_type
== PT_LOAD
6526 && ((phdr
->p_flags
& (PF_R
| PF_W
| PF_X
))
6527 == (PF_R
| PF_W
| PF_X
)))
6528 _bfd_error_handler (_("warning: %pB has a LOAD segment"
6529 " with RWX permissions"),
6534 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6535 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6543 _bfd_elf_init_file_header (bfd
*abfd
,
6544 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6546 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6547 struct elf_strtab_hash
*shstrtab
;
6548 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6550 i_ehdrp
= elf_elfheader (abfd
);
6552 shstrtab
= _bfd_elf_strtab_init ();
6553 if (shstrtab
== NULL
)
6556 elf_shstrtab (abfd
) = shstrtab
;
6558 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6559 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6560 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6561 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6563 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6564 i_ehdrp
->e_ident
[EI_DATA
] =
6565 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6566 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6568 if ((abfd
->flags
& DYNAMIC
) != 0)
6569 i_ehdrp
->e_type
= ET_DYN
;
6570 else if ((abfd
->flags
& EXEC_P
) != 0)
6571 i_ehdrp
->e_type
= ET_EXEC
;
6572 else if (bfd_get_format (abfd
) == bfd_core
)
6573 i_ehdrp
->e_type
= ET_CORE
;
6575 i_ehdrp
->e_type
= ET_REL
;
6577 switch (bfd_get_arch (abfd
))
6579 case bfd_arch_unknown
:
6580 i_ehdrp
->e_machine
= EM_NONE
;
6583 /* There used to be a long list of cases here, each one setting
6584 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6585 in the corresponding bfd definition. To avoid duplication,
6586 the switch was removed. Machines that need special handling
6587 can generally do it in elf_backend_final_write_processing(),
6588 unless they need the information earlier than the final write.
6589 Such need can generally be supplied by replacing the tests for
6590 e_machine with the conditions used to determine it. */
6592 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6595 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6596 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6598 /* No program header, for now. */
6599 i_ehdrp
->e_phoff
= 0;
6600 i_ehdrp
->e_phentsize
= 0;
6601 i_ehdrp
->e_phnum
= 0;
6603 /* Each bfd section is section header entry. */
6604 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6605 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6607 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6608 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
6609 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6610 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
6611 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6612 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
6613 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6614 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6615 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6621 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6623 FIXME: We used to have code here to sort the PT_LOAD segments into
6624 ascending order, as per the ELF spec. But this breaks some programs,
6625 including the Linux kernel. But really either the spec should be
6626 changed or the programs updated. */
6629 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6631 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6633 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6634 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6635 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6636 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6637 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6639 /* Find the lowest p_vaddr in PT_LOAD segments. */
6640 bfd_vma p_vaddr
= (bfd_vma
) -1;
6641 for (; segment
< end_segment
; segment
++)
6642 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6643 p_vaddr
= segment
->p_vaddr
;
6645 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6646 segments is non-zero. */
6648 i_ehdrp
->e_type
= ET_EXEC
;
6653 /* Assign file positions for all the reloc sections which are not part
6654 of the loadable file image, and the file position of section headers. */
6657 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6660 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6661 Elf_Internal_Shdr
*shdrp
;
6662 Elf_Internal_Ehdr
*i_ehdrp
;
6663 const struct elf_backend_data
*bed
;
6665 off
= elf_next_file_pos (abfd
);
6667 shdrpp
= elf_elfsections (abfd
);
6668 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6669 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6672 if (shdrp
->sh_offset
== -1)
6674 asection
*sec
= shdrp
->bfd_section
;
6675 bool is_rel
= (shdrp
->sh_type
== SHT_REL
6676 || shdrp
->sh_type
== SHT_RELA
);
6677 bool is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6680 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6682 if (!is_rel
&& !is_ctf
)
6684 const char *name
= sec
->name
;
6685 struct bfd_elf_section_data
*d
;
6687 /* Compress DWARF debug sections. */
6688 if (!bfd_compress_section (abfd
, sec
,
6692 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6693 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6695 /* If section is compressed with zlib-gnu, convert
6696 section name from .debug_* to .zdebug_*. */
6698 = convert_debug_to_zdebug (abfd
, name
);
6699 if (new_name
== NULL
)
6703 /* Add section name to section name section. */
6704 if (shdrp
->sh_name
!= (unsigned int) -1)
6707 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6709 d
= elf_section_data (sec
);
6711 /* Add reloc section name to section name section. */
6713 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rel
.hdr
,
6717 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rela
.hdr
,
6721 /* Update section size and contents. */
6722 shdrp
->sh_size
= sec
->size
;
6723 shdrp
->contents
= sec
->contents
;
6724 shdrp
->bfd_section
->contents
= NULL
;
6728 /* Update section size and contents. */
6729 shdrp
->sh_size
= sec
->size
;
6730 shdrp
->contents
= sec
->contents
;
6733 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
,
6739 /* Place section name section after DWARF debug sections have been
6741 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6742 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6743 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6744 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
6746 /* Place the section headers. */
6747 i_ehdrp
= elf_elfheader (abfd
);
6748 bed
= get_elf_backend_data (abfd
);
6749 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6750 i_ehdrp
->e_shoff
= off
;
6751 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6752 elf_next_file_pos (abfd
) = off
;
6758 _bfd_elf_write_object_contents (bfd
*abfd
)
6760 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6761 Elf_Internal_Shdr
**i_shdrp
;
6763 unsigned int count
, num_sec
;
6764 struct elf_obj_tdata
*t
;
6766 if (! abfd
->output_has_begun
6767 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6769 /* Do not rewrite ELF data when the BFD has been opened for update.
6770 abfd->output_has_begun was set to TRUE on opening, so creation of
6771 new sections, and modification of existing section sizes was
6772 restricted. This means the ELF header, program headers and
6773 section headers can't have changed. If the contents of any
6774 sections has been modified, then those changes have already been
6775 written to the BFD. */
6776 else if (abfd
->direction
== both_direction
)
6778 BFD_ASSERT (abfd
->output_has_begun
);
6782 i_shdrp
= elf_elfsections (abfd
);
6785 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6789 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6792 /* After writing the headers, we need to write the sections too... */
6793 num_sec
= elf_numsections (abfd
);
6794 for (count
= 1; count
< num_sec
; count
++)
6796 i_shdrp
[count
]->sh_name
6797 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6798 i_shdrp
[count
]->sh_name
);
6799 if (bed
->elf_backend_section_processing
)
6800 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6802 if (i_shdrp
[count
]->contents
)
6804 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6806 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6807 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6812 /* Write out the section header names. */
6813 t
= elf_tdata (abfd
);
6814 if (elf_shstrtab (abfd
) != NULL
6815 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6816 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6819 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6822 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6825 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6826 if (t
->o
->build_id
.after_write_object_contents
!= NULL
6827 && !(*t
->o
->build_id
.after_write_object_contents
) (abfd
))
6829 if (t
->o
->package_metadata
.after_write_object_contents
!= NULL
6830 && !(*t
->o
->package_metadata
.after_write_object_contents
) (abfd
))
6837 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6839 /* Hopefully this can be done just like an object file. */
6840 return _bfd_elf_write_object_contents (abfd
);
6843 /* Given a section, search the header to find them. */
6846 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6848 const struct elf_backend_data
*bed
;
6849 unsigned int sec_index
;
6851 if (elf_section_data (asect
) != NULL
6852 && elf_section_data (asect
)->this_idx
!= 0)
6853 return elf_section_data (asect
)->this_idx
;
6855 if (bfd_is_abs_section (asect
))
6856 sec_index
= SHN_ABS
;
6857 else if (bfd_is_com_section (asect
))
6858 sec_index
= SHN_COMMON
;
6859 else if (bfd_is_und_section (asect
))
6860 sec_index
= SHN_UNDEF
;
6862 sec_index
= SHN_BAD
;
6864 bed
= get_elf_backend_data (abfd
);
6865 if (bed
->elf_backend_section_from_bfd_section
)
6867 int retval
= sec_index
;
6869 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6873 if (sec_index
== SHN_BAD
)
6874 bfd_set_error (bfd_error_nonrepresentable_section
);
6879 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6883 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6885 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6887 flagword flags
= asym_ptr
->flags
;
6889 /* When gas creates relocations against local labels, it creates its
6890 own symbol for the section, but does put the symbol into the
6891 symbol chain, so udata is 0. When the linker is generating
6892 relocatable output, this section symbol may be for one of the
6893 input sections rather than the output section. */
6894 if (asym_ptr
->udata
.i
== 0
6895 && (flags
& BSF_SECTION_SYM
)
6896 && asym_ptr
->section
)
6900 sec
= asym_ptr
->section
;
6901 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6902 sec
= sec
->output_section
;
6903 if (sec
->owner
== abfd
6904 && sec
->index
< elf_num_section_syms (abfd
)
6905 && elf_section_syms (abfd
)[sec
->index
] != NULL
)
6906 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
6909 idx
= asym_ptr
->udata
.i
;
6913 /* This case can occur when using --strip-symbol on a symbol
6914 which is used in a relocation entry. */
6916 /* xgettext:c-format */
6917 (_("%pB: symbol `%s' required but not present"),
6918 abfd
, bfd_asymbol_name (asym_ptr
));
6919 bfd_set_error (bfd_error_no_symbols
);
6926 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6927 " flags = 0x%.8x\n",
6928 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6936 static inline bfd_vma
6937 segment_size (Elf_Internal_Phdr
*segment
)
6939 return (segment
->p_memsz
> segment
->p_filesz
6940 ? segment
->p_memsz
: segment
->p_filesz
);
6944 /* Returns the end address of the segment + 1. */
6945 static inline bfd_vma
6946 segment_end (Elf_Internal_Phdr
*segment
, bfd_vma start
)
6948 return start
+ segment_size (segment
);
6951 static inline bfd_size_type
6952 section_size (asection
*section
, Elf_Internal_Phdr
*segment
)
6954 if ((section
->flags
& SEC_HAS_CONTENTS
) != 0
6955 || (section
->flags
& SEC_THREAD_LOCAL
) == 0
6956 || segment
->p_type
== PT_TLS
)
6957 return section
->size
;
6961 /* Returns TRUE if the given section is contained within the given
6962 segment. LMA addresses are compared against PADDR when
6963 bed->want_p_paddr_set_to_zero is false, VMA against VADDR when true. */
6965 is_contained_by (asection
*section
, Elf_Internal_Phdr
*segment
,
6966 bfd_vma paddr
, bfd_vma vaddr
, unsigned int opb
,
6967 const struct elf_backend_data
*bed
)
6969 bfd_vma seg_addr
= !bed
->want_p_paddr_set_to_zero
? paddr
: vaddr
;
6970 bfd_vma addr
= !bed
->want_p_paddr_set_to_zero
? section
->lma
: section
->vma
;
6972 if (_bfd_mul_overflow (addr
, opb
, &octet
))
6974 /* The third and fourth lines below are testing that the section end
6975 address is within the segment. It's written this way to avoid
6976 overflow. Add seg_addr + section_size to both sides of the
6977 inequality to make it obvious. */
6978 return (octet
>= seg_addr
6979 && segment_size (segment
) >= section_size (section
, segment
)
6980 && (octet
- seg_addr
6981 <= segment_size (segment
) - section_size (section
, segment
)));
6984 /* Handle PT_NOTE segment. */
6986 is_note (asection
*s
, Elf_Internal_Phdr
*p
)
6988 return (p
->p_type
== PT_NOTE
6989 && elf_section_type (s
) == SHT_NOTE
6990 && (ufile_ptr
) s
->filepos
>= p
->p_offset
6991 && p
->p_filesz
>= s
->size
6992 && (ufile_ptr
) s
->filepos
- p
->p_offset
<= p
->p_filesz
- s
->size
);
6995 /* Rewrite program header information. */
6998 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
, bfd_vma maxpagesize
)
7000 Elf_Internal_Ehdr
*iehdr
;
7001 struct elf_segment_map
*map
;
7002 struct elf_segment_map
*map_first
;
7003 struct elf_segment_map
**pointer_to_map
;
7004 Elf_Internal_Phdr
*segment
;
7007 unsigned int num_segments
;
7008 bool phdr_included
= false;
7010 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
7011 unsigned int phdr_adjust_num
= 0;
7012 const struct elf_backend_data
*bed
;
7013 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7015 bed
= get_elf_backend_data (ibfd
);
7016 iehdr
= elf_elfheader (ibfd
);
7019 pointer_to_map
= &map_first
;
7021 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7023 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7024 linker, which generates a PT_INTERP section with p_vaddr and
7025 p_memsz set to 0. */
7026 #define IS_SOLARIS_PT_INTERP(p, s) \
7028 && p->p_paddr == 0 \
7029 && p->p_memsz == 0 \
7030 && p->p_filesz > 0 \
7031 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7033 && (bfd_vma) s->filepos >= p->p_offset \
7034 && ((bfd_vma) s->filepos + s->size \
7035 <= p->p_offset + p->p_filesz))
7037 /* Decide if the given section should be included in the given segment.
7038 A section will be included if:
7039 1. It is within the address space of the segment -- we use the LMA
7040 if that is set for the segment and the VMA otherwise,
7041 2. It is an allocated section or a NOTE section in a PT_NOTE
7043 3. There is an output section associated with it,
7044 4. The section has not already been allocated to a previous segment.
7045 5. PT_GNU_STACK segments do not include any sections.
7046 6. PT_TLS segment includes only SHF_TLS sections.
7047 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7048 8. PT_DYNAMIC should not contain empty sections at the beginning
7049 (with the possible exception of .dynamic). */
7050 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7051 (((is_contained_by (section, segment, segment->p_paddr, \
7052 segment->p_vaddr, opb, bed) \
7053 && (section->flags & SEC_ALLOC) != 0) \
7054 || is_note (section, segment)) \
7055 && segment->p_type != PT_GNU_STACK \
7056 && (segment->p_type != PT_TLS \
7057 || (section->flags & SEC_THREAD_LOCAL)) \
7058 && (segment->p_type == PT_LOAD \
7059 || segment->p_type == PT_TLS \
7060 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7061 && (segment->p_type != PT_DYNAMIC \
7062 || section_size (section, segment) > 0 \
7063 || (segment->p_paddr \
7064 ? segment->p_paddr != section->lma * (opb) \
7065 : segment->p_vaddr != section->vma * (opb)) \
7066 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7067 && (segment->p_type != PT_LOAD || !section->segment_mark))
7069 /* If the output section of a section in the input segment is NULL,
7070 it is removed from the corresponding output segment. */
7071 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7072 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7073 && section->output_section != NULL)
7075 /* Returns TRUE iff seg1 starts after the end of seg2. */
7076 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7077 (seg1->field >= segment_end (seg2, seg2->field))
7079 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7080 their VMA address ranges and their LMA address ranges overlap.
7081 It is possible to have overlapping VMA ranges without overlapping LMA
7082 ranges. RedBoot images for example can have both .data and .bss mapped
7083 to the same VMA range, but with the .data section mapped to a different
7085 #define SEGMENT_OVERLAPS(seg1, seg2) \
7086 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7087 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7088 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7089 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7091 /* Initialise the segment mark field, and discard stupid alignment. */
7092 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
7094 asection
*o
= section
->output_section
;
7095 if (o
!= NULL
&& o
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
7096 o
->alignment_power
= 0;
7097 section
->segment_mark
= false;
7100 /* The Solaris linker creates program headers in which all the
7101 p_paddr fields are zero. When we try to objcopy or strip such a
7102 file, we get confused. Check for this case, and if we find it
7103 don't set the p_paddr_valid fields. */
7104 p_paddr_valid
= false;
7105 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7108 if (segment
->p_paddr
!= 0)
7110 p_paddr_valid
= true;
7114 /* Scan through the segments specified in the program header
7115 of the input BFD. For this first scan we look for overlaps
7116 in the loadable segments. These can be created by weird
7117 parameters to objcopy. Also, fix some solaris weirdness. */
7118 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7123 Elf_Internal_Phdr
*segment2
;
7125 if (segment
->p_type
== PT_INTERP
)
7126 for (section
= ibfd
->sections
; section
; section
= section
->next
)
7127 if (IS_SOLARIS_PT_INTERP (segment
, section
))
7129 /* Mininal change so that the normal section to segment
7130 assignment code will work. */
7131 segment
->p_vaddr
= section
->vma
* opb
;
7135 if (segment
->p_type
!= PT_LOAD
)
7137 /* Remove PT_GNU_RELRO segment. */
7138 if (segment
->p_type
== PT_GNU_RELRO
)
7139 segment
->p_type
= PT_NULL
;
7143 /* Determine if this segment overlaps any previous segments. */
7144 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7146 bfd_signed_vma extra_length
;
7148 if (segment2
->p_type
!= PT_LOAD
7149 || !SEGMENT_OVERLAPS (segment
, segment2
))
7152 /* Merge the two segments together. */
7153 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7155 /* Extend SEGMENT2 to include SEGMENT and then delete
7157 extra_length
= (segment_end (segment
, segment
->p_vaddr
)
7158 - segment_end (segment2
, segment2
->p_vaddr
));
7160 if (extra_length
> 0)
7162 segment2
->p_memsz
+= extra_length
;
7163 segment2
->p_filesz
+= extra_length
;
7166 segment
->p_type
= PT_NULL
;
7168 /* Since we have deleted P we must restart the outer loop. */
7170 segment
= elf_tdata (ibfd
)->phdr
;
7175 /* Extend SEGMENT to include SEGMENT2 and then delete
7177 extra_length
= (segment_end (segment2
, segment2
->p_vaddr
)
7178 - segment_end (segment
, segment
->p_vaddr
));
7180 if (extra_length
> 0)
7182 segment
->p_memsz
+= extra_length
;
7183 segment
->p_filesz
+= extra_length
;
7186 segment2
->p_type
= PT_NULL
;
7191 /* The second scan attempts to assign sections to segments. */
7192 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7196 unsigned int section_count
;
7197 asection
**sections
;
7198 asection
*output_section
;
7200 asection
*matching_lma
;
7201 asection
*suggested_lma
;
7204 asection
*first_section
;
7206 if (segment
->p_type
== PT_NULL
)
7209 first_section
= NULL
;
7210 /* Compute how many sections might be placed into this segment. */
7211 for (section
= ibfd
->sections
, section_count
= 0;
7213 section
= section
->next
)
7215 /* Find the first section in the input segment, which may be
7216 removed from the corresponding output segment. */
7217 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7219 if (first_section
== NULL
)
7220 first_section
= section
;
7221 if (section
->output_section
!= NULL
)
7226 /* Allocate a segment map big enough to contain
7227 all of the sections we have selected. */
7228 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7229 amt
+= section_count
* sizeof (asection
*);
7230 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7234 /* Initialise the fields of the segment map. Default to
7235 using the physical address of the segment in the input BFD. */
7237 map
->p_type
= segment
->p_type
;
7238 map
->p_flags
= segment
->p_flags
;
7239 map
->p_flags_valid
= 1;
7241 if (map
->p_type
== PT_LOAD
7242 && (ibfd
->flags
& D_PAGED
) != 0
7244 && segment
->p_align
> 1)
7246 map
->p_align
= segment
->p_align
;
7247 if (segment
->p_align
> maxpagesize
)
7248 map
->p_align
= maxpagesize
;
7249 map
->p_align_valid
= 1;
7252 /* If the first section in the input segment is removed, there is
7253 no need to preserve segment physical address in the corresponding
7255 if (!first_section
|| first_section
->output_section
!= NULL
)
7257 map
->p_paddr
= segment
->p_paddr
;
7258 map
->p_paddr_valid
= p_paddr_valid
;
7261 /* Determine if this segment contains the ELF file header
7262 and if it contains the program headers themselves. */
7263 map
->includes_filehdr
= (segment
->p_offset
== 0
7264 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7265 map
->includes_phdrs
= 0;
7267 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7269 map
->includes_phdrs
=
7270 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7271 && (segment
->p_offset
+ segment
->p_filesz
7272 >= ((bfd_vma
) iehdr
->e_phoff
7273 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7275 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7276 phdr_included
= true;
7279 if (section_count
== 0)
7281 /* Special segments, such as the PT_PHDR segment, may contain
7282 no sections, but ordinary, loadable segments should contain
7283 something. They are allowed by the ELF spec however, so only
7284 a warning is produced.
7285 There is however the valid use case of embedded systems which
7286 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7287 flash memory with zeros. No warning is shown for that case. */
7288 if (segment
->p_type
== PT_LOAD
7289 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7290 /* xgettext:c-format */
7292 (_("%pB: warning: empty loadable segment detected"
7293 " at vaddr=%#" PRIx64
", is this intentional?"),
7294 ibfd
, (uint64_t) segment
->p_vaddr
);
7296 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7298 *pointer_to_map
= map
;
7299 pointer_to_map
= &map
->next
;
7304 /* Now scan the sections in the input BFD again and attempt
7305 to add their corresponding output sections to the segment map.
7306 The problem here is how to handle an output section which has
7307 been moved (ie had its LMA changed). There are four possibilities:
7309 1. None of the sections have been moved.
7310 In this case we can continue to use the segment LMA from the
7313 2. All of the sections have been moved by the same amount.
7314 In this case we can change the segment's LMA to match the LMA
7315 of the first section.
7317 3. Some of the sections have been moved, others have not.
7318 In this case those sections which have not been moved can be
7319 placed in the current segment which will have to have its size,
7320 and possibly its LMA changed, and a new segment or segments will
7321 have to be created to contain the other sections.
7323 4. The sections have been moved, but not by the same amount.
7324 In this case we can change the segment's LMA to match the LMA
7325 of the first section and we will have to create a new segment
7326 or segments to contain the other sections.
7328 In order to save time, we allocate an array to hold the section
7329 pointers that we are interested in. As these sections get assigned
7330 to a segment, they are removed from this array. */
7332 amt
= section_count
* sizeof (asection
*);
7333 sections
= (asection
**) bfd_malloc (amt
);
7334 if (sections
== NULL
)
7337 /* Step One: Scan for segment vs section LMA conflicts.
7338 Also add the sections to the section array allocated above.
7339 Also add the sections to the current segment. In the common
7340 case, where the sections have not been moved, this means that
7341 we have completely filled the segment, and there is nothing
7344 matching_lma
= NULL
;
7345 suggested_lma
= NULL
;
7347 for (section
= first_section
, j
= 0;
7349 section
= section
->next
)
7351 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7353 output_section
= section
->output_section
;
7355 sections
[j
++] = section
;
7357 /* The Solaris native linker always sets p_paddr to 0.
7358 We try to catch that case here, and set it to the
7359 correct value. Note - some backends require that
7360 p_paddr be left as zero. */
7362 && segment
->p_vaddr
!= 0
7363 && !bed
->want_p_paddr_set_to_zero
7365 && output_section
->lma
!= 0
7366 && (align_power (segment
->p_vaddr
7367 + (map
->includes_filehdr
7368 ? iehdr
->e_ehsize
: 0)
7369 + (map
->includes_phdrs
7370 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7372 output_section
->alignment_power
* opb
)
7373 == (output_section
->vma
* opb
)))
7374 map
->p_paddr
= segment
->p_vaddr
;
7376 /* Match up the physical address of the segment with the
7377 LMA address of the output section. */
7378 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7379 map
->p_paddr
+ map
->p_vaddr_offset
, opb
, bed
)
7380 || is_note (section
, segment
))
7382 if (matching_lma
== NULL
7383 || output_section
->lma
< matching_lma
->lma
)
7384 matching_lma
= output_section
;
7386 /* We assume that if the section fits within the segment
7387 then it does not overlap any other section within that
7389 map
->sections
[isec
++] = output_section
;
7391 else if (suggested_lma
== NULL
)
7392 suggested_lma
= output_section
;
7394 if (j
== section_count
)
7399 BFD_ASSERT (j
== section_count
);
7401 /* Step Two: Adjust the physical address of the current segment,
7403 if (isec
== section_count
)
7405 /* All of the sections fitted within the segment as currently
7406 specified. This is the default case. Add the segment to
7407 the list of built segments and carry on to process the next
7408 program header in the input BFD. */
7409 map
->count
= section_count
;
7410 *pointer_to_map
= map
;
7411 pointer_to_map
= &map
->next
;
7414 && !bed
->want_p_paddr_set_to_zero
)
7416 bfd_vma hdr_size
= 0;
7417 if (map
->includes_filehdr
)
7418 hdr_size
= iehdr
->e_ehsize
;
7419 if (map
->includes_phdrs
)
7420 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7422 /* Account for padding before the first section in the
7424 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7425 - matching_lma
->lma
);
7433 /* Change the current segment's physical address to match
7434 the LMA of the first section that fitted, or if no
7435 section fitted, the first section. */
7436 if (matching_lma
== NULL
)
7437 matching_lma
= suggested_lma
;
7439 map
->p_paddr
= matching_lma
->lma
* opb
;
7441 /* Offset the segment physical address from the lma
7442 to allow for space taken up by elf headers. */
7443 if (map
->includes_phdrs
)
7445 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7447 /* iehdr->e_phnum is just an estimate of the number
7448 of program headers that we will need. Make a note
7449 here of the number we used and the segment we chose
7450 to hold these headers, so that we can adjust the
7451 offset when we know the correct value. */
7452 phdr_adjust_num
= iehdr
->e_phnum
;
7453 phdr_adjust_seg
= map
;
7456 if (map
->includes_filehdr
)
7458 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7459 map
->p_paddr
-= iehdr
->e_ehsize
;
7460 /* We've subtracted off the size of headers from the
7461 first section lma, but there may have been some
7462 alignment padding before that section too. Try to
7463 account for that by adjusting the segment lma down to
7464 the same alignment. */
7465 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7466 align
= segment
->p_align
;
7467 map
->p_paddr
&= -(align
* opb
);
7471 /* Step Three: Loop over the sections again, this time assigning
7472 those that fit to the current segment and removing them from the
7473 sections array; but making sure not to leave large gaps. Once all
7474 possible sections have been assigned to the current segment it is
7475 added to the list of built segments and if sections still remain
7476 to be assigned, a new segment is constructed before repeating
7482 suggested_lma
= NULL
;
7484 /* Fill the current segment with sections that fit. */
7485 for (j
= 0; j
< section_count
; j
++)
7487 section
= sections
[j
];
7489 if (section
== NULL
)
7492 output_section
= section
->output_section
;
7494 BFD_ASSERT (output_section
!= NULL
);
7496 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7497 map
->p_paddr
+ map
->p_vaddr_offset
, opb
, bed
)
7498 || is_note (section
, segment
))
7500 if (map
->count
== 0)
7502 /* If the first section in a segment does not start at
7503 the beginning of the segment, then something is
7505 if (align_power (map
->p_paddr
7506 + (map
->includes_filehdr
7507 ? iehdr
->e_ehsize
: 0)
7508 + (map
->includes_phdrs
7509 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7511 output_section
->alignment_power
* opb
)
7512 != output_section
->lma
* opb
)
7519 prev_sec
= map
->sections
[map
->count
- 1];
7521 /* If the gap between the end of the previous section
7522 and the start of this section is more than
7523 maxpagesize then we need to start a new segment. */
7524 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7526 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7527 || (prev_sec
->lma
+ prev_sec
->size
7528 > output_section
->lma
))
7530 if (suggested_lma
== NULL
)
7531 suggested_lma
= output_section
;
7537 map
->sections
[map
->count
++] = output_section
;
7540 if (segment
->p_type
== PT_LOAD
)
7541 section
->segment_mark
= true;
7543 else if (suggested_lma
== NULL
)
7544 suggested_lma
= output_section
;
7547 /* PR 23932. A corrupt input file may contain sections that cannot
7548 be assigned to any segment - because for example they have a
7549 negative size - or segments that do not contain any sections.
7550 But there are also valid reasons why a segment can be empty.
7551 So allow a count of zero. */
7553 /* Add the current segment to the list of built segments. */
7554 *pointer_to_map
= map
;
7555 pointer_to_map
= &map
->next
;
7557 if (isec
< section_count
)
7559 /* We still have not allocated all of the sections to
7560 segments. Create a new segment here, initialise it
7561 and carry on looping. */
7562 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7563 amt
+= section_count
* sizeof (asection
*);
7564 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7571 /* Initialise the fields of the segment map. Set the physical
7572 physical address to the LMA of the first section that has
7573 not yet been assigned. */
7575 map
->p_type
= segment
->p_type
;
7576 map
->p_flags
= segment
->p_flags
;
7577 map
->p_flags_valid
= 1;
7578 map
->p_paddr
= suggested_lma
->lma
* opb
;
7579 map
->p_paddr_valid
= p_paddr_valid
;
7580 map
->includes_filehdr
= 0;
7581 map
->includes_phdrs
= 0;
7586 bfd_set_error (bfd_error_sorry
);
7590 while (isec
< section_count
);
7595 elf_seg_map (obfd
) = map_first
;
7597 /* If we had to estimate the number of program headers that were
7598 going to be needed, then check our estimate now and adjust
7599 the offset if necessary. */
7600 if (phdr_adjust_seg
!= NULL
)
7604 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7607 if (count
> phdr_adjust_num
)
7608 phdr_adjust_seg
->p_paddr
7609 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7611 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7612 if (map
->p_type
== PT_PHDR
)
7615 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7616 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7621 #undef IS_SOLARIS_PT_INTERP
7622 #undef IS_SECTION_IN_INPUT_SEGMENT
7623 #undef INCLUDE_SECTION_IN_SEGMENT
7624 #undef SEGMENT_AFTER_SEGMENT
7625 #undef SEGMENT_OVERLAPS
7629 /* Return true if p_align in the ELF program header in ABFD is valid. */
7632 elf_is_p_align_valid (bfd
*abfd
)
7635 Elf_Internal_Phdr
*segment
;
7636 unsigned int num_segments
;
7637 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7638 bfd_size_type maxpagesize
= bed
->maxpagesize
;
7639 bfd_size_type p_align
= bed
->p_align
;
7641 /* Return true if the default p_align value isn't set or the maximum
7642 page size is the same as the minimum page size. */
7643 if (p_align
== 0 || maxpagesize
== bed
->minpagesize
)
7646 /* When the default p_align value is set, p_align may be set to the
7647 default p_align value while segments are aligned to the maximum
7648 page size. In this case, the input p_align will be ignored and
7649 the maximum page size will be used to align the output segments. */
7650 segment
= elf_tdata (abfd
)->phdr
;
7651 num_segments
= elf_elfheader (abfd
)->e_phnum
;
7652 for (i
= 0; i
< num_segments
; i
++, segment
++)
7653 if (segment
->p_type
== PT_LOAD
7654 && (segment
->p_align
!= p_align
7655 || vma_page_aligned_bias (segment
->p_vaddr
,
7663 /* Copy ELF program header information. */
7666 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7668 Elf_Internal_Ehdr
*iehdr
;
7669 struct elf_segment_map
*map
;
7670 struct elf_segment_map
*map_first
;
7671 struct elf_segment_map
**pointer_to_map
;
7672 Elf_Internal_Phdr
*segment
;
7674 unsigned int num_segments
;
7675 bool phdr_included
= false;
7677 bool p_palign_valid
;
7678 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7680 iehdr
= elf_elfheader (ibfd
);
7683 pointer_to_map
= &map_first
;
7685 /* If all the segment p_paddr fields are zero, don't set
7686 map->p_paddr_valid. */
7687 p_paddr_valid
= false;
7688 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7689 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7692 if (segment
->p_paddr
!= 0)
7694 p_paddr_valid
= true;
7698 p_palign_valid
= elf_is_p_align_valid (ibfd
);
7700 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7705 unsigned int section_count
;
7707 Elf_Internal_Shdr
*this_hdr
;
7708 asection
*first_section
= NULL
;
7709 asection
*lowest_section
;
7711 /* Compute how many sections are in this segment. */
7712 for (section
= ibfd
->sections
, section_count
= 0;
7714 section
= section
->next
)
7716 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7717 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7719 if (first_section
== NULL
)
7720 first_section
= section
;
7725 /* Allocate a segment map big enough to contain
7726 all of the sections we have selected. */
7727 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7728 amt
+= section_count
* sizeof (asection
*);
7729 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7733 /* Initialize the fields of the output segment map with the
7736 map
->p_type
= segment
->p_type
;
7737 map
->p_flags
= segment
->p_flags
;
7738 map
->p_flags_valid
= 1;
7739 map
->p_paddr
= segment
->p_paddr
;
7740 map
->p_paddr_valid
= p_paddr_valid
;
7741 map
->p_align
= segment
->p_align
;
7742 /* Keep p_align of PT_GNU_STACK for stack alignment. */
7743 map
->p_align_valid
= (map
->p_type
== PT_GNU_STACK
7745 map
->p_vaddr_offset
= 0;
7747 if (map
->p_type
== PT_GNU_RELRO
7748 || map
->p_type
== PT_GNU_STACK
)
7750 /* The PT_GNU_RELRO segment may contain the first a few
7751 bytes in the .got.plt section even if the whole .got.plt
7752 section isn't in the PT_GNU_RELRO segment. We won't
7753 change the size of the PT_GNU_RELRO segment.
7754 Similarly, PT_GNU_STACK size is significant on uclinux
7756 map
->p_size
= segment
->p_memsz
;
7757 map
->p_size_valid
= 1;
7760 /* Determine if this segment contains the ELF file header
7761 and if it contains the program headers themselves. */
7762 map
->includes_filehdr
= (segment
->p_offset
== 0
7763 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7765 map
->includes_phdrs
= 0;
7766 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7768 map
->includes_phdrs
=
7769 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7770 && (segment
->p_offset
+ segment
->p_filesz
7771 >= ((bfd_vma
) iehdr
->e_phoff
7772 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7774 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7775 phdr_included
= true;
7778 lowest_section
= NULL
;
7779 if (section_count
!= 0)
7781 unsigned int isec
= 0;
7783 for (section
= first_section
;
7785 section
= section
->next
)
7787 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7788 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7790 map
->sections
[isec
++] = section
->output_section
;
7791 if ((section
->flags
& SEC_ALLOC
) != 0)
7795 if (lowest_section
== NULL
7796 || section
->lma
< lowest_section
->lma
)
7797 lowest_section
= section
;
7799 /* Section lmas are set up from PT_LOAD header
7800 p_paddr in _bfd_elf_make_section_from_shdr.
7801 If this header has a p_paddr that disagrees
7802 with the section lma, flag the p_paddr as
7804 if ((section
->flags
& SEC_LOAD
) != 0)
7805 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7807 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7808 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7809 map
->p_paddr_valid
= false;
7811 if (isec
== section_count
)
7817 if (section_count
== 0)
7818 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7819 else if (map
->p_paddr_valid
)
7821 /* Account for padding before the first section in the segment. */
7822 bfd_vma hdr_size
= 0;
7823 if (map
->includes_filehdr
)
7824 hdr_size
= iehdr
->e_ehsize
;
7825 if (map
->includes_phdrs
)
7826 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7828 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7829 - (lowest_section
? lowest_section
->lma
: 0));
7832 map
->count
= section_count
;
7833 *pointer_to_map
= map
;
7834 pointer_to_map
= &map
->next
;
7837 elf_seg_map (obfd
) = map_first
;
7841 /* Copy private BFD data. This copies or rewrites ELF program header
7845 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7847 bfd_vma maxpagesize
;
7849 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7850 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7853 if (elf_tdata (ibfd
)->phdr
== NULL
)
7856 if (ibfd
->xvec
== obfd
->xvec
)
7858 /* Check to see if any sections in the input BFD
7859 covered by ELF program header have changed. */
7860 Elf_Internal_Phdr
*segment
;
7861 asection
*section
, *osec
;
7862 unsigned int i
, num_segments
;
7863 Elf_Internal_Shdr
*this_hdr
;
7864 const struct elf_backend_data
*bed
;
7866 bed
= get_elf_backend_data (ibfd
);
7868 /* Regenerate the segment map if p_paddr is set to 0. */
7869 if (bed
->want_p_paddr_set_to_zero
)
7872 /* Initialize the segment mark field. */
7873 for (section
= obfd
->sections
; section
!= NULL
;
7874 section
= section
->next
)
7875 section
->segment_mark
= false;
7877 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7878 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7882 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7883 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7884 which severly confuses things, so always regenerate the segment
7885 map in this case. */
7886 if (segment
->p_paddr
== 0
7887 && segment
->p_memsz
== 0
7888 && (segment
->p_type
== PT_INTERP
7889 || segment
->p_type
== PT_DYNAMIC
))
7892 for (section
= ibfd
->sections
;
7893 section
!= NULL
; section
= section
->next
)
7895 /* We mark the output section so that we know it comes
7896 from the input BFD. */
7897 osec
= section
->output_section
;
7899 osec
->segment_mark
= true;
7901 /* Check if this section is covered by the segment. */
7902 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7903 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7905 /* FIXME: Check if its output section is changed or
7906 removed. What else do we need to check? */
7908 || section
->flags
!= osec
->flags
7909 || section
->lma
!= osec
->lma
7910 || section
->vma
!= osec
->vma
7911 || section
->size
!= osec
->size
7912 || section
->rawsize
!= osec
->rawsize
7913 || section
->alignment_power
!= osec
->alignment_power
)
7919 /* Check to see if any output section do not come from the
7921 for (section
= obfd
->sections
; section
!= NULL
;
7922 section
= section
->next
)
7924 if (!section
->segment_mark
)
7927 section
->segment_mark
= false;
7930 return copy_elf_program_header (ibfd
, obfd
);
7935 if (ibfd
->xvec
== obfd
->xvec
)
7937 /* When rewriting program header, set the output maxpagesize to
7938 the maximum alignment of input PT_LOAD segments. */
7939 Elf_Internal_Phdr
*segment
;
7941 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7943 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7946 if (segment
->p_type
== PT_LOAD
7947 && maxpagesize
< segment
->p_align
)
7949 /* PR 17512: file: f17299af. */
7950 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7951 /* xgettext:c-format */
7952 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7953 PRIx64
" is too large"),
7954 ibfd
, (uint64_t) segment
->p_align
);
7956 maxpagesize
= segment
->p_align
;
7959 if (maxpagesize
== 0)
7960 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
7962 return rewrite_elf_program_header (ibfd
, obfd
, maxpagesize
);
7965 /* Initialize private output section information from input section. */
7968 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7972 struct bfd_link_info
*link_info
)
7975 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7976 bool final_link
= (link_info
!= NULL
7977 && !bfd_link_relocatable (link_info
));
7979 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7980 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7983 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7985 /* If this is a known ABI section, ELF section type and flags may
7986 have been set up when OSEC was created. For normal sections we
7987 allow the user to override the type and flags other than
7988 SHF_MASKOS and SHF_MASKPROC. */
7989 if (elf_section_type (osec
) == SHT_PROGBITS
7990 || elf_section_type (osec
) == SHT_NOTE
7991 || elf_section_type (osec
) == SHT_NOBITS
)
7992 elf_section_type (osec
) = SHT_NULL
;
7993 /* For objcopy and relocatable link, copy the ELF section type from
7994 the input file if the BFD section flags are the same. (If they
7995 are different the user may be doing something like
7996 "objcopy --set-section-flags .text=alloc,data".) For a final
7997 link allow some flags that the linker clears to differ. */
7998 if (elf_section_type (osec
) == SHT_NULL
7999 && (osec
->flags
== isec
->flags
8001 && ((osec
->flags
^ isec
->flags
)
8002 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
8003 elf_section_type (osec
) = elf_section_type (isec
);
8005 /* FIXME: Is this correct for all OS/PROC specific flags? */
8006 elf_section_flags (osec
) = (elf_section_flags (isec
)
8007 & (SHF_MASKOS
| SHF_MASKPROC
));
8009 /* Copy sh_info from input for mbind section. */
8010 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
8011 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
8012 elf_section_data (osec
)->this_hdr
.sh_info
8013 = elf_section_data (isec
)->this_hdr
.sh_info
;
8015 /* Set things up for objcopy and relocatable link. The output
8016 SHT_GROUP section will have its elf_next_in_group pointing back
8017 to the input group members. Ignore linker created group section.
8018 See elfNN_ia64_object_p in elfxx-ia64.c. */
8019 if ((link_info
== NULL
8020 || !link_info
->resolve_section_groups
)
8021 && (elf_sec_group (isec
) == NULL
8022 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
8024 if (elf_section_flags (isec
) & SHF_GROUP
)
8025 elf_section_flags (osec
) |= SHF_GROUP
;
8026 elf_next_in_group (osec
) = elf_next_in_group (isec
);
8027 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
8030 /* If not decompress, preserve SHF_COMPRESSED. */
8031 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
8032 elf_section_flags (osec
) |= (elf_section_flags (isec
)
8035 ihdr
= &elf_section_data (isec
)->this_hdr
;
8037 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8038 don't use the output section of the linked-to section since it
8039 may be NULL at this point. */
8040 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
8042 ohdr
= &elf_section_data (osec
)->this_hdr
;
8043 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
8044 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
8047 osec
->use_rela_p
= isec
->use_rela_p
;
8052 /* Copy private section information. This copies over the entsize
8053 field, and sometimes the info field. */
8056 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
8061 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8063 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8064 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8067 ihdr
= &elf_section_data (isec
)->this_hdr
;
8068 ohdr
= &elf_section_data (osec
)->this_hdr
;
8070 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
8072 if (ihdr
->sh_type
== SHT_SYMTAB
8073 || ihdr
->sh_type
== SHT_DYNSYM
8074 || ihdr
->sh_type
== SHT_GNU_verneed
8075 || ihdr
->sh_type
== SHT_GNU_verdef
)
8076 ohdr
->sh_info
= ihdr
->sh_info
;
8078 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
8082 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8083 necessary if we are removing either the SHT_GROUP section or any of
8084 the group member sections. DISCARDED is the value that a section's
8085 output_section has if the section will be discarded, NULL when this
8086 function is called from objcopy, bfd_abs_section_ptr when called
8090 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
8094 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
8095 if (elf_section_type (isec
) == SHT_GROUP
)
8097 asection
*first
= elf_next_in_group (isec
);
8098 asection
*s
= first
;
8099 bfd_size_type removed
= 0;
8103 /* If this member section is being output but the
8104 SHT_GROUP section is not, then clear the group info
8105 set up by _bfd_elf_copy_private_section_data. */
8106 if (s
->output_section
!= discarded
8107 && isec
->output_section
== discarded
)
8109 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
8110 elf_group_name (s
->output_section
) = NULL
;
8114 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
8115 if (s
->output_section
== discarded
8116 && isec
->output_section
!= discarded
)
8118 /* Conversely, if the member section is not being
8119 output but the SHT_GROUP section is, then adjust
8122 if (elf_sec
->rel
.hdr
!= NULL
8123 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8125 if (elf_sec
->rela
.hdr
!= NULL
8126 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8131 /* Also adjust for zero-sized relocation member
8133 if (elf_sec
->rel
.hdr
!= NULL
8134 && elf_sec
->rel
.hdr
->sh_size
== 0)
8136 if (elf_sec
->rela
.hdr
!= NULL
8137 && elf_sec
->rela
.hdr
->sh_size
== 0)
8141 s
= elf_next_in_group (s
);
8147 if (discarded
!= NULL
)
8149 /* If we've been called for ld -r, then we need to
8150 adjust the input section size. */
8151 if (isec
->rawsize
== 0)
8152 isec
->rawsize
= isec
->size
;
8153 isec
->size
= isec
->rawsize
- removed
;
8154 if (isec
->size
<= 4)
8157 isec
->flags
|= SEC_EXCLUDE
;
8160 else if (isec
->output_section
!= NULL
)
8162 /* Adjust the output section size when called from
8164 isec
->output_section
->size
-= removed
;
8165 if (isec
->output_section
->size
<= 4)
8167 isec
->output_section
->size
= 0;
8168 isec
->output_section
->flags
|= SEC_EXCLUDE
;
8177 /* Copy private header information. */
8180 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
8182 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8183 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8186 /* Copy over private BFD data if it has not already been copied.
8187 This must be done here, rather than in the copy_private_bfd_data
8188 entry point, because the latter is called after the section
8189 contents have been set, which means that the program headers have
8190 already been worked out. */
8191 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
8193 if (! copy_private_bfd_data (ibfd
, obfd
))
8197 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8200 /* Copy private symbol information. If this symbol is in a section
8201 which we did not map into a BFD section, try to map the section
8202 index correctly. We use special macro definitions for the mapped
8203 section indices; these definitions are interpreted by the
8204 swap_out_syms function. */
8206 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8207 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8208 #define MAP_STRTAB (SHN_HIOS + 3)
8209 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8210 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8213 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8218 elf_symbol_type
*isym
, *osym
;
8220 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8221 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8224 isym
= elf_symbol_from (isymarg
);
8225 osym
= elf_symbol_from (osymarg
);
8228 && isym
->internal_elf_sym
.st_shndx
!= 0
8230 && bfd_is_abs_section (isym
->symbol
.section
))
8234 shndx
= isym
->internal_elf_sym
.st_shndx
;
8235 if (shndx
== elf_onesymtab (ibfd
))
8236 shndx
= MAP_ONESYMTAB
;
8237 else if (shndx
== elf_dynsymtab (ibfd
))
8238 shndx
= MAP_DYNSYMTAB
;
8239 else if (shndx
== elf_strtab_sec (ibfd
))
8241 else if (shndx
== elf_shstrtab_sec (ibfd
))
8242 shndx
= MAP_SHSTRTAB
;
8243 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8244 shndx
= MAP_SYM_SHNDX
;
8245 osym
->internal_elf_sym
.st_shndx
= shndx
;
8251 /* Swap out the symbols. */
8254 swap_out_syms (bfd
*abfd
,
8255 struct elf_strtab_hash
**sttp
,
8257 struct bfd_link_info
*info
)
8259 const struct elf_backend_data
*bed
;
8260 unsigned int symcount
;
8262 struct elf_strtab_hash
*stt
;
8263 Elf_Internal_Shdr
*symtab_hdr
;
8264 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8265 Elf_Internal_Shdr
*symstrtab_hdr
;
8266 struct elf_sym_strtab
*symstrtab
;
8267 bfd_byte
*outbound_syms
;
8268 bfd_byte
*outbound_shndx
;
8269 unsigned long outbound_syms_index
;
8271 unsigned int num_locals
;
8273 bool name_local_sections
;
8275 if (!elf_map_symbols (abfd
, &num_locals
))
8278 /* Dump out the symtabs. */
8279 stt
= _bfd_elf_strtab_init ();
8283 bed
= get_elf_backend_data (abfd
);
8284 symcount
= bfd_get_symcount (abfd
);
8285 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8286 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8287 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8288 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8289 symtab_hdr
->sh_info
= num_locals
+ 1;
8290 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8292 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8293 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8295 /* Allocate buffer to swap out the .strtab section. */
8296 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8297 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8299 bfd_set_error (bfd_error_no_memory
);
8300 _bfd_elf_strtab_free (stt
);
8304 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8305 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8308 bfd_set_error (bfd_error_no_memory
);
8311 _bfd_elf_strtab_free (stt
);
8314 symtab_hdr
->contents
= outbound_syms
;
8315 outbound_syms_index
= 0;
8317 outbound_shndx
= NULL
;
8319 if (elf_symtab_shndx_list (abfd
))
8321 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8322 if (symtab_shndx_hdr
->sh_name
!= 0)
8324 if (_bfd_mul_overflow (symcount
+ 1,
8325 sizeof (Elf_External_Sym_Shndx
), &amt
))
8327 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8328 if (outbound_shndx
== NULL
)
8331 symtab_shndx_hdr
->contents
= outbound_shndx
;
8332 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8333 symtab_shndx_hdr
->sh_size
= amt
;
8334 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8335 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8337 /* FIXME: What about any other headers in the list ? */
8340 /* Now generate the data (for "contents"). */
8342 /* Fill in zeroth symbol and swap it out. */
8343 Elf_Internal_Sym sym
;
8349 sym
.st_shndx
= SHN_UNDEF
;
8350 sym
.st_target_internal
= 0;
8351 symstrtab
[0].sym
= sym
;
8352 symstrtab
[0].dest_index
= outbound_syms_index
;
8353 outbound_syms_index
++;
8357 = (bed
->elf_backend_name_local_section_symbols
8358 && bed
->elf_backend_name_local_section_symbols (abfd
));
8360 syms
= bfd_get_outsymbols (abfd
);
8361 for (idx
= 0; idx
< symcount
;)
8363 Elf_Internal_Sym sym
;
8364 bfd_vma value
= syms
[idx
]->value
;
8365 elf_symbol_type
*type_ptr
;
8366 flagword flags
= syms
[idx
]->flags
;
8369 if (!name_local_sections
8370 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8372 /* Local section symbols have no name. */
8373 sym
.st_name
= (unsigned long) -1;
8377 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8378 to get the final offset for st_name. */
8380 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8382 if (sym
.st_name
== (unsigned long) -1)
8386 type_ptr
= elf_symbol_from (syms
[idx
]);
8388 if ((flags
& BSF_SECTION_SYM
) == 0
8389 && bfd_is_com_section (syms
[idx
]->section
))
8391 /* ELF common symbols put the alignment into the `value' field,
8392 and the size into the `size' field. This is backwards from
8393 how BFD handles it, so reverse it here. */
8394 sym
.st_size
= value
;
8395 if (type_ptr
== NULL
8396 || type_ptr
->internal_elf_sym
.st_value
== 0)
8397 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8399 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8400 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8401 (abfd
, syms
[idx
]->section
);
8405 asection
*sec
= syms
[idx
]->section
;
8408 if (sec
->output_section
)
8410 value
+= sec
->output_offset
;
8411 sec
= sec
->output_section
;
8414 /* Don't add in the section vma for relocatable output. */
8415 if (! relocatable_p
)
8417 sym
.st_value
= value
;
8418 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8420 if (bfd_is_abs_section (sec
)
8422 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8424 /* This symbol is in a real ELF section which we did
8425 not create as a BFD section. Undo the mapping done
8426 by copy_private_symbol_data. */
8427 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8431 shndx
= elf_onesymtab (abfd
);
8434 shndx
= elf_dynsymtab (abfd
);
8437 shndx
= elf_strtab_sec (abfd
);
8440 shndx
= elf_shstrtab_sec (abfd
);
8443 if (elf_symtab_shndx_list (abfd
))
8444 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8451 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8453 if (bed
->symbol_section_index
)
8454 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8455 /* Otherwise just leave the index alone. */
8459 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8460 _bfd_error_handler (_("%pB: \
8461 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8470 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8472 if (shndx
== SHN_BAD
)
8476 /* Writing this would be a hell of a lot easier if
8477 we had some decent documentation on bfd, and
8478 knew what to expect of the library, and what to
8479 demand of applications. For example, it
8480 appears that `objcopy' might not set the
8481 section of a symbol to be a section that is
8482 actually in the output file. */
8483 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8485 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8486 if (shndx
== SHN_BAD
)
8488 /* xgettext:c-format */
8490 (_("unable to find equivalent output section"
8491 " for symbol '%s' from section '%s'"),
8492 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8494 bfd_set_error (bfd_error_invalid_operation
);
8500 sym
.st_shndx
= shndx
;
8503 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8505 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8506 type
= STT_GNU_IFUNC
;
8507 else if ((flags
& BSF_FUNCTION
) != 0)
8509 else if ((flags
& BSF_OBJECT
) != 0)
8511 else if ((flags
& BSF_RELC
) != 0)
8513 else if ((flags
& BSF_SRELC
) != 0)
8518 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8521 /* Processor-specific types. */
8522 if (type_ptr
!= NULL
8523 && bed
->elf_backend_get_symbol_type
)
8524 type
= ((*bed
->elf_backend_get_symbol_type
)
8525 (&type_ptr
->internal_elf_sym
, type
));
8527 if (flags
& BSF_SECTION_SYM
)
8529 if (flags
& BSF_GLOBAL
)
8530 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8532 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8534 else if (bfd_is_com_section (syms
[idx
]->section
))
8536 if (type
!= STT_TLS
)
8538 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8539 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8540 ? STT_COMMON
: STT_OBJECT
);
8542 type
= ((flags
& BSF_ELF_COMMON
) != 0
8543 ? STT_COMMON
: STT_OBJECT
);
8545 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8547 else if (bfd_is_und_section (syms
[idx
]->section
))
8548 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8552 else if (flags
& BSF_FILE
)
8553 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8556 int bind
= STB_LOCAL
;
8558 if (flags
& BSF_LOCAL
)
8560 else if (flags
& BSF_GNU_UNIQUE
)
8561 bind
= STB_GNU_UNIQUE
;
8562 else if (flags
& BSF_WEAK
)
8564 else if (flags
& BSF_GLOBAL
)
8567 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8570 if (type_ptr
!= NULL
)
8572 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8573 sym
.st_target_internal
8574 = type_ptr
->internal_elf_sym
.st_target_internal
;
8579 sym
.st_target_internal
= 0;
8583 symstrtab
[idx
].sym
= sym
;
8584 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8586 outbound_syms_index
++;
8589 /* Finalize the .strtab section. */
8590 _bfd_elf_strtab_finalize (stt
);
8592 /* Swap out the .strtab section. */
8593 for (idx
= 0; idx
<= symcount
; idx
++)
8595 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8596 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8597 elfsym
->sym
.st_name
= 0;
8599 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8600 elfsym
->sym
.st_name
);
8601 if (info
&& info
->callbacks
->ctf_new_symbol
)
8602 info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
8605 /* Inform the linker of the addition of this symbol. */
8607 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8609 + (elfsym
->dest_index
8610 * bed
->s
->sizeof_sym
)),
8611 NPTR_ADD (outbound_shndx
,
8613 * sizeof (Elf_External_Sym_Shndx
))));
8618 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8619 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8620 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8621 symstrtab_hdr
->sh_addr
= 0;
8622 symstrtab_hdr
->sh_entsize
= 0;
8623 symstrtab_hdr
->sh_link
= 0;
8624 symstrtab_hdr
->sh_info
= 0;
8625 symstrtab_hdr
->sh_addralign
= 1;
8630 /* Return the number of bytes required to hold the symtab vector.
8632 Note that we base it on the count plus 1, since we will null terminate
8633 the vector allocated based on this size. However, the ELF symbol table
8634 always has a dummy entry as symbol #0, so it ends up even. */
8637 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8639 bfd_size_type symcount
;
8641 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8643 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8644 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
8646 bfd_set_error (bfd_error_file_too_big
);
8649 symtab_size
= symcount
* (sizeof (asymbol
*));
8651 symtab_size
= sizeof (asymbol
*);
8652 else if (!bfd_write_p (abfd
))
8654 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8656 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
8658 bfd_set_error (bfd_error_file_truncated
);
8667 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8669 bfd_size_type symcount
;
8671 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8673 if (elf_dynsymtab (abfd
) == 0)
8675 bfd_set_error (bfd_error_invalid_operation
);
8679 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8680 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
8682 bfd_set_error (bfd_error_file_too_big
);
8685 symtab_size
= symcount
* (sizeof (asymbol
*));
8687 symtab_size
= sizeof (asymbol
*);
8688 else if (!bfd_write_p (abfd
))
8690 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8692 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
8694 bfd_set_error (bfd_error_file_truncated
);
8703 _bfd_elf_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
8705 if (asect
->reloc_count
!= 0 && !bfd_write_p (abfd
))
8707 /* Sanity check reloc section size. */
8708 struct bfd_elf_section_data
*d
= elf_section_data (asect
);
8709 Elf_Internal_Shdr
*rel_hdr
= &d
->this_hdr
;
8710 bfd_size_type ext_rel_size
= rel_hdr
->sh_size
;
8711 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8713 if (filesize
!= 0 && ext_rel_size
> filesize
)
8715 bfd_set_error (bfd_error_file_truncated
);
8720 #if SIZEOF_LONG == SIZEOF_INT
8721 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8723 bfd_set_error (bfd_error_file_too_big
);
8727 return (asect
->reloc_count
+ 1L) * sizeof (arelent
*);
8730 /* Canonicalize the relocs. */
8733 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8740 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8742 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
8745 tblptr
= section
->relocation
;
8746 for (i
= 0; i
< section
->reloc_count
; i
++)
8747 *relptr
++ = tblptr
++;
8751 return section
->reloc_count
;
8755 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8757 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8758 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, false);
8761 abfd
->symcount
= symcount
;
8766 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8767 asymbol
**allocation
)
8769 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8770 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, true);
8773 abfd
->dynsymcount
= symcount
;
8777 /* Return the size required for the dynamic reloc entries. Any loadable
8778 section that was actually installed in the BFD, and has type SHT_REL
8779 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8780 dynamic reloc section. */
8783 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8785 bfd_size_type count
, ext_rel_size
;
8788 if (elf_dynsymtab (abfd
) == 0)
8790 bfd_set_error (bfd_error_invalid_operation
);
8796 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8797 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8798 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8799 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8801 ext_rel_size
+= s
->size
;
8802 if (ext_rel_size
< s
->size
)
8804 bfd_set_error (bfd_error_file_truncated
);
8807 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8808 if (count
> LONG_MAX
/ sizeof (arelent
*))
8810 bfd_set_error (bfd_error_file_too_big
);
8814 if (count
> 1 && !bfd_write_p (abfd
))
8816 /* Sanity check reloc section sizes. */
8817 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8818 if (filesize
!= 0 && ext_rel_size
> filesize
)
8820 bfd_set_error (bfd_error_file_truncated
);
8824 return count
* sizeof (arelent
*);
8827 /* Canonicalize the dynamic relocation entries. Note that we return the
8828 dynamic relocations as a single block, although they are actually
8829 associated with particular sections; the interface, which was
8830 designed for SunOS style shared libraries, expects that there is only
8831 one set of dynamic relocs. Any loadable section that was actually
8832 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8833 dynamic symbol table, is considered to be a dynamic reloc section. */
8836 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8840 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
8844 if (elf_dynsymtab (abfd
) == 0)
8846 bfd_set_error (bfd_error_invalid_operation
);
8850 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8852 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8854 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8855 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8856 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8861 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
8863 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8865 for (i
= 0; i
< count
; i
++)
8876 /* Read in the version information. */
8879 _bfd_elf_slurp_version_tables (bfd
*abfd
, bool default_imported_symver
)
8881 bfd_byte
*contents
= NULL
;
8882 unsigned int freeidx
= 0;
8885 if (elf_dynverref (abfd
) != 0)
8887 Elf_Internal_Shdr
*hdr
;
8888 Elf_External_Verneed
*everneed
;
8889 Elf_Internal_Verneed
*iverneed
;
8891 bfd_byte
*contents_end
;
8893 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8895 if (hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8897 error_return_bad_verref
:
8899 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8900 bfd_set_error (bfd_error_bad_value
);
8901 error_return_verref
:
8902 elf_tdata (abfd
)->verref
= NULL
;
8903 elf_tdata (abfd
)->cverrefs
= 0;
8907 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8908 goto error_return_verref
;
8909 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8910 if (contents
== NULL
)
8911 goto error_return_verref
;
8913 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8915 bfd_set_error (bfd_error_file_too_big
);
8916 goto error_return_verref
;
8918 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8919 if (elf_tdata (abfd
)->verref
== NULL
)
8920 goto error_return_verref
;
8922 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8923 == sizeof (Elf_External_Vernaux
));
8924 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8925 everneed
= (Elf_External_Verneed
*) contents
;
8926 iverneed
= elf_tdata (abfd
)->verref
;
8927 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8929 Elf_External_Vernaux
*evernaux
;
8930 Elf_Internal_Vernaux
*ivernaux
;
8933 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8935 iverneed
->vn_bfd
= abfd
;
8937 iverneed
->vn_filename
=
8938 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8940 if (iverneed
->vn_filename
== NULL
)
8941 goto error_return_bad_verref
;
8943 if (iverneed
->vn_cnt
== 0)
8944 iverneed
->vn_auxptr
= NULL
;
8947 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8948 sizeof (Elf_Internal_Vernaux
), &amt
))
8950 bfd_set_error (bfd_error_file_too_big
);
8951 goto error_return_verref
;
8953 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8954 bfd_alloc (abfd
, amt
);
8955 if (iverneed
->vn_auxptr
== NULL
)
8956 goto error_return_verref
;
8959 if (iverneed
->vn_aux
8960 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8961 goto error_return_bad_verref
;
8963 evernaux
= ((Elf_External_Vernaux
*)
8964 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8965 ivernaux
= iverneed
->vn_auxptr
;
8966 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8968 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8970 ivernaux
->vna_nodename
=
8971 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8972 ivernaux
->vna_name
);
8973 if (ivernaux
->vna_nodename
== NULL
)
8974 goto error_return_bad_verref
;
8976 if (ivernaux
->vna_other
> freeidx
)
8977 freeidx
= ivernaux
->vna_other
;
8979 ivernaux
->vna_nextptr
= NULL
;
8980 if (ivernaux
->vna_next
== 0)
8982 iverneed
->vn_cnt
= j
+ 1;
8985 if (j
+ 1 < iverneed
->vn_cnt
)
8986 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8988 if (ivernaux
->vna_next
8989 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8990 goto error_return_bad_verref
;
8992 evernaux
= ((Elf_External_Vernaux
*)
8993 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8996 iverneed
->vn_nextref
= NULL
;
8997 if (iverneed
->vn_next
== 0)
8999 if (i
+ 1 < hdr
->sh_info
)
9000 iverneed
->vn_nextref
= iverneed
+ 1;
9002 if (iverneed
->vn_next
9003 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9004 goto error_return_bad_verref
;
9006 everneed
= ((Elf_External_Verneed
*)
9007 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
9009 elf_tdata (abfd
)->cverrefs
= i
;
9015 if (elf_dynverdef (abfd
) != 0)
9017 Elf_Internal_Shdr
*hdr
;
9018 Elf_External_Verdef
*everdef
;
9019 Elf_Internal_Verdef
*iverdef
;
9020 Elf_Internal_Verdef
*iverdefarr
;
9021 Elf_Internal_Verdef iverdefmem
;
9023 unsigned int maxidx
;
9024 bfd_byte
*contents_end_def
, *contents_end_aux
;
9026 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
9028 if (hdr
->sh_size
< sizeof (Elf_External_Verdef
))
9030 error_return_bad_verdef
:
9032 (_("%pB: .gnu.version_d invalid entry"), abfd
);
9033 bfd_set_error (bfd_error_bad_value
);
9034 error_return_verdef
:
9035 elf_tdata (abfd
)->verdef
= NULL
;
9036 elf_tdata (abfd
)->cverdefs
= 0;
9040 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9041 goto error_return_verdef
;
9042 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
9043 if (contents
== NULL
)
9044 goto error_return_verdef
;
9046 BFD_ASSERT (sizeof (Elf_External_Verdef
)
9047 >= sizeof (Elf_External_Verdaux
));
9048 contents_end_def
= contents
+ hdr
->sh_size
9049 - sizeof (Elf_External_Verdef
);
9050 contents_end_aux
= contents
+ hdr
->sh_size
9051 - sizeof (Elf_External_Verdaux
);
9053 /* We know the number of entries in the section but not the maximum
9054 index. Therefore we have to run through all entries and find
9056 everdef
= (Elf_External_Verdef
*) contents
;
9058 for (i
= 0; i
< hdr
->sh_info
; ++i
)
9060 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9062 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
9063 goto error_return_bad_verdef
;
9064 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
9065 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
9067 if (iverdefmem
.vd_next
== 0)
9070 if (iverdefmem
.vd_next
9071 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
9072 goto error_return_bad_verdef
;
9074 everdef
= ((Elf_External_Verdef
*)
9075 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
9078 if (default_imported_symver
)
9080 if (freeidx
> maxidx
)
9085 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9087 bfd_set_error (bfd_error_file_too_big
);
9088 goto error_return_verdef
;
9090 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9091 if (elf_tdata (abfd
)->verdef
== NULL
)
9092 goto error_return_verdef
;
9094 elf_tdata (abfd
)->cverdefs
= maxidx
;
9096 everdef
= (Elf_External_Verdef
*) contents
;
9097 iverdefarr
= elf_tdata (abfd
)->verdef
;
9098 for (i
= 0; i
< hdr
->sh_info
; i
++)
9100 Elf_External_Verdaux
*everdaux
;
9101 Elf_Internal_Verdaux
*iverdaux
;
9104 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9106 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
9107 goto error_return_bad_verdef
;
9109 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
9110 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
9112 iverdef
->vd_bfd
= abfd
;
9114 if (iverdef
->vd_cnt
== 0)
9115 iverdef
->vd_auxptr
= NULL
;
9118 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
9119 sizeof (Elf_Internal_Verdaux
), &amt
))
9121 bfd_set_error (bfd_error_file_too_big
);
9122 goto error_return_verdef
;
9124 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
9125 bfd_alloc (abfd
, amt
);
9126 if (iverdef
->vd_auxptr
== NULL
)
9127 goto error_return_verdef
;
9131 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
9132 goto error_return_bad_verdef
;
9134 everdaux
= ((Elf_External_Verdaux
*)
9135 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
9136 iverdaux
= iverdef
->vd_auxptr
;
9137 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
9139 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
9141 iverdaux
->vda_nodename
=
9142 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9143 iverdaux
->vda_name
);
9144 if (iverdaux
->vda_nodename
== NULL
)
9145 goto error_return_bad_verdef
;
9147 iverdaux
->vda_nextptr
= NULL
;
9148 if (iverdaux
->vda_next
== 0)
9150 iverdef
->vd_cnt
= j
+ 1;
9153 if (j
+ 1 < iverdef
->vd_cnt
)
9154 iverdaux
->vda_nextptr
= iverdaux
+ 1;
9156 if (iverdaux
->vda_next
9157 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
9158 goto error_return_bad_verdef
;
9160 everdaux
= ((Elf_External_Verdaux
*)
9161 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
9164 iverdef
->vd_nodename
= NULL
;
9165 if (iverdef
->vd_cnt
)
9166 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
9168 iverdef
->vd_nextdef
= NULL
;
9169 if (iverdef
->vd_next
== 0)
9171 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
9172 iverdef
->vd_nextdef
= iverdef
+ 1;
9174 everdef
= ((Elf_External_Verdef
*)
9175 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
9181 else if (default_imported_symver
)
9188 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9190 bfd_set_error (bfd_error_file_too_big
);
9193 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9194 if (elf_tdata (abfd
)->verdef
== NULL
)
9197 elf_tdata (abfd
)->cverdefs
= freeidx
;
9200 /* Create a default version based on the soname. */
9201 if (default_imported_symver
)
9203 Elf_Internal_Verdef
*iverdef
;
9204 Elf_Internal_Verdaux
*iverdaux
;
9206 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
9208 iverdef
->vd_version
= VER_DEF_CURRENT
;
9209 iverdef
->vd_flags
= 0;
9210 iverdef
->vd_ndx
= freeidx
;
9211 iverdef
->vd_cnt
= 1;
9213 iverdef
->vd_bfd
= abfd
;
9215 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
9216 if (iverdef
->vd_nodename
== NULL
)
9217 goto error_return_verdef
;
9218 iverdef
->vd_nextdef
= NULL
;
9219 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
9220 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
9221 if (iverdef
->vd_auxptr
== NULL
)
9222 goto error_return_verdef
;
9224 iverdaux
= iverdef
->vd_auxptr
;
9225 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9236 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9238 elf_symbol_type
*newsym
;
9240 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9243 newsym
->symbol
.the_bfd
= abfd
;
9244 return &newsym
->symbol
;
9248 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9252 bfd_symbol_info (symbol
, ret
);
9255 /* Return whether a symbol name implies a local symbol. Most targets
9256 use this function for the is_local_label_name entry point, but some
9260 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9263 /* Normal local symbols start with ``.L''. */
9264 if (name
[0] == '.' && name
[1] == 'L')
9267 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9268 DWARF debugging symbols starting with ``..''. */
9269 if (name
[0] == '.' && name
[1] == '.')
9272 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9273 emitting DWARF debugging output. I suspect this is actually a
9274 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9275 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9276 underscore to be emitted on some ELF targets). For ease of use,
9277 we treat such symbols as local. */
9278 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9281 /* Treat assembler generated fake symbols, dollar local labels and
9282 forward-backward labels (aka local labels) as locals.
9283 These labels have the form:
9285 L0^A.* (fake symbols)
9287 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9289 Versions which start with .L will have already been matched above,
9290 so we only need to match the rest. */
9291 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9297 for (p
= name
+ 2; (c
= *p
); p
++)
9299 if (c
== 1 || c
== 2)
9301 if (c
== 1 && p
== name
+ 2)
9302 /* A fake symbol. */
9305 /* FIXME: We are being paranoid here and treating symbols like
9306 L0^Bfoo as if there were non-local, on the grounds that the
9307 assembler will never generate them. But can any symbol
9308 containing an ASCII value in the range 1-31 ever be anything
9309 other than some kind of local ? */
9326 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9327 asymbol
*symbol ATTRIBUTE_UNUSED
)
9334 _bfd_elf_set_arch_mach (bfd
*abfd
,
9335 enum bfd_architecture arch
,
9336 unsigned long machine
)
9338 /* If this isn't the right architecture for this backend, and this
9339 isn't the generic backend, fail. */
9340 if (arch
!= get_elf_backend_data (abfd
)->arch
9341 && arch
!= bfd_arch_unknown
9342 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9345 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9348 /* Find the nearest line to a particular section and offset,
9349 for error reporting. */
9352 _bfd_elf_find_nearest_line (bfd
*abfd
,
9356 const char **filename_ptr
,
9357 const char **functionname_ptr
,
9358 unsigned int *line_ptr
,
9359 unsigned int *discriminator_ptr
)
9361 return _bfd_elf_find_nearest_line_with_alt (abfd
, NULL
, symbols
, section
,
9362 offset
, filename_ptr
,
9363 functionname_ptr
, line_ptr
,
9367 /* Find the nearest line to a particular section and offset,
9368 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9369 can be optionally specified. */
9372 _bfd_elf_find_nearest_line_with_alt (bfd
*abfd
,
9373 const char *alt_filename
,
9377 const char **filename_ptr
,
9378 const char **functionname_ptr
,
9379 unsigned int *line_ptr
,
9380 unsigned int *discriminator_ptr
)
9384 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd
, alt_filename
, symbols
, NULL
,
9385 section
, offset
, filename_ptr
,
9386 functionname_ptr
, line_ptr
,
9388 dwarf_debug_sections
,
9389 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9392 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9393 filename_ptr
, functionname_ptr
, line_ptr
))
9395 if (!*functionname_ptr
)
9396 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9397 *filename_ptr
? NULL
: filename_ptr
,
9402 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9403 &found
, filename_ptr
,
9404 functionname_ptr
, line_ptr
,
9405 &elf_tdata (abfd
)->line_info
))
9407 if (found
&& (*functionname_ptr
|| *line_ptr
))
9410 if (symbols
== NULL
)
9413 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9414 filename_ptr
, functionname_ptr
))
9421 /* Find the line for a symbol. */
9424 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9425 const char **filename_ptr
, unsigned int *line_ptr
)
9427 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9428 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9429 filename_ptr
, NULL
, line_ptr
, NULL
,
9430 dwarf_debug_sections
,
9431 &tdata
->dwarf2_find_line_info
);
9434 /* After a call to bfd_find_nearest_line, successive calls to
9435 bfd_find_inliner_info can be used to get source information about
9436 each level of function inlining that terminated at the address
9437 passed to bfd_find_nearest_line. Currently this is only supported
9438 for DWARF2 with appropriate DWARF3 extensions. */
9441 _bfd_elf_find_inliner_info (bfd
*abfd
,
9442 const char **filename_ptr
,
9443 const char **functionname_ptr
,
9444 unsigned int *line_ptr
)
9446 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9447 return _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9448 functionname_ptr
, line_ptr
,
9449 &tdata
->dwarf2_find_line_info
);
9453 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9455 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9456 int ret
= bed
->s
->sizeof_ehdr
;
9458 if (!bfd_link_relocatable (info
))
9460 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9462 if (phdr_size
== (bfd_size_type
) -1)
9464 struct elf_segment_map
*m
;
9467 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9468 phdr_size
+= bed
->s
->sizeof_phdr
;
9471 phdr_size
= get_program_header_size (abfd
, info
);
9474 elf_program_header_size (abfd
) = phdr_size
;
9482 _bfd_elf_set_section_contents (bfd
*abfd
,
9484 const void *location
,
9486 bfd_size_type count
)
9488 Elf_Internal_Shdr
*hdr
;
9490 if (! abfd
->output_has_begun
9491 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9497 hdr
= &elf_section_data (section
)->this_hdr
;
9498 if (hdr
->sh_offset
== (file_ptr
) -1)
9500 unsigned char *contents
;
9502 if (bfd_section_is_ctf (section
))
9503 /* Nothing to do with this section: the contents are generated
9507 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9510 (_("%pB:%pA: error: attempting to write"
9511 " into an unallocated compressed section"),
9513 bfd_set_error (bfd_error_invalid_operation
);
9517 if ((offset
+ count
) > hdr
->sh_size
)
9520 (_("%pB:%pA: error: attempting to write"
9521 " over the end of the section"),
9524 bfd_set_error (bfd_error_invalid_operation
);
9528 contents
= hdr
->contents
;
9529 if (contents
== NULL
)
9532 (_("%pB:%pA: error: attempting to write"
9533 " section into an empty buffer"),
9536 bfd_set_error (bfd_error_invalid_operation
);
9540 memcpy (contents
+ offset
, location
, count
);
9544 return _bfd_generic_set_section_contents (abfd
, section
,
9545 location
, offset
, count
);
9549 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9550 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9551 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9557 /* Try to convert a non-ELF reloc into an ELF one. */
9560 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9562 /* Check whether we really have an ELF howto. */
9564 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9566 bfd_reloc_code_real_type code
;
9567 reloc_howto_type
*howto
;
9569 /* Alien reloc: Try to determine its type to replace it with an
9570 equivalent ELF reloc. */
9572 if (areloc
->howto
->pc_relative
)
9574 switch (areloc
->howto
->bitsize
)
9577 code
= BFD_RELOC_8_PCREL
;
9580 code
= BFD_RELOC_12_PCREL
;
9583 code
= BFD_RELOC_16_PCREL
;
9586 code
= BFD_RELOC_24_PCREL
;
9589 code
= BFD_RELOC_32_PCREL
;
9592 code
= BFD_RELOC_64_PCREL
;
9598 howto
= bfd_reloc_type_lookup (abfd
, code
);
9600 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9602 if (howto
->pcrel_offset
)
9603 areloc
->addend
+= areloc
->address
;
9605 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9610 switch (areloc
->howto
->bitsize
)
9616 code
= BFD_RELOC_14
;
9619 code
= BFD_RELOC_16
;
9622 code
= BFD_RELOC_26
;
9625 code
= BFD_RELOC_32
;
9628 code
= BFD_RELOC_64
;
9634 howto
= bfd_reloc_type_lookup (abfd
, code
);
9638 areloc
->howto
= howto
;
9646 /* xgettext:c-format */
9647 _bfd_error_handler (_("%pB: %s unsupported"),
9648 abfd
, areloc
->howto
->name
);
9649 bfd_set_error (bfd_error_sorry
);
9654 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9656 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9658 && (bfd_get_format (abfd
) == bfd_object
9659 || bfd_get_format (abfd
) == bfd_core
))
9661 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9662 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9663 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9664 _bfd_stab_cleanup (abfd
, &tdata
->line_info
);
9667 return _bfd_generic_close_and_cleanup (abfd
);
9670 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9671 in the relocation's offset. Thus we cannot allow any sort of sanity
9672 range-checking to interfere. There is nothing else to do in processing
9675 bfd_reloc_status_type
9676 _bfd_elf_rel_vtable_reloc_fn
9677 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9678 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9679 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9680 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9682 return bfd_reloc_ok
;
9685 /* Elf core file support. Much of this only works on native
9686 toolchains, since we rely on knowing the
9687 machine-dependent procfs structure in order to pick
9688 out details about the corefile. */
9690 #ifdef HAVE_SYS_PROCFS_H
9691 # include <sys/procfs.h>
9694 /* Return a PID that identifies a "thread" for threaded cores, or the
9695 PID of the main process for non-threaded cores. */
9698 elfcore_make_pid (bfd
*abfd
)
9702 pid
= elf_tdata (abfd
)->core
->lwpid
;
9704 pid
= elf_tdata (abfd
)->core
->pid
;
9709 /* If there isn't a section called NAME, make one, using data from
9710 SECT. Note, this function will generate a reference to NAME, so
9711 you shouldn't deallocate or overwrite it. */
9714 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9718 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9721 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9725 sect2
->size
= sect
->size
;
9726 sect2
->filepos
= sect
->filepos
;
9727 sect2
->alignment_power
= sect
->alignment_power
;
9731 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9732 actually creates up to two pseudosections:
9733 - For the single-threaded case, a section named NAME, unless
9734 such a section already exists.
9735 - For the multi-threaded case, a section named "NAME/PID", where
9736 PID is elfcore_make_pid (abfd).
9737 Both pseudosections have identical contents. */
9739 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9745 char *threaded_name
;
9749 /* Build the section name. */
9751 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9752 len
= strlen (buf
) + 1;
9753 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9754 if (threaded_name
== NULL
)
9756 memcpy (threaded_name
, buf
, len
);
9758 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9763 sect
->filepos
= filepos
;
9764 sect
->alignment_power
= 2;
9766 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9770 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9773 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9779 sect
->size
= note
->descsz
- offs
;
9780 sect
->filepos
= note
->descpos
+ offs
;
9781 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9786 /* prstatus_t exists on:
9788 linux 2.[01] + glibc
9792 #if defined (HAVE_PRSTATUS_T)
9795 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9800 if (note
->descsz
== sizeof (prstatus_t
))
9804 size
= sizeof (prstat
.pr_reg
);
9805 offset
= offsetof (prstatus_t
, pr_reg
);
9806 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9808 /* Do not overwrite the core signal if it
9809 has already been set by another thread. */
9810 if (elf_tdata (abfd
)->core
->signal
== 0)
9811 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9812 if (elf_tdata (abfd
)->core
->pid
== 0)
9813 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9815 /* pr_who exists on:
9818 pr_who doesn't exist on:
9821 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9822 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9824 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9827 #if defined (HAVE_PRSTATUS32_T)
9828 else if (note
->descsz
== sizeof (prstatus32_t
))
9830 /* 64-bit host, 32-bit corefile */
9831 prstatus32_t prstat
;
9833 size
= sizeof (prstat
.pr_reg
);
9834 offset
= offsetof (prstatus32_t
, pr_reg
);
9835 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9837 /* Do not overwrite the core signal if it
9838 has already been set by another thread. */
9839 if (elf_tdata (abfd
)->core
->signal
== 0)
9840 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9841 if (elf_tdata (abfd
)->core
->pid
== 0)
9842 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9844 /* pr_who exists on:
9847 pr_who doesn't exist on:
9850 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9851 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9853 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9856 #endif /* HAVE_PRSTATUS32_T */
9859 /* Fail - we don't know how to handle any other
9860 note size (ie. data object type). */
9864 /* Make a ".reg/999" section and a ".reg" section. */
9865 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9866 size
, note
->descpos
+ offset
);
9868 #endif /* defined (HAVE_PRSTATUS_T) */
9870 /* Create a pseudosection containing the exact contents of NOTE. */
9872 elfcore_make_note_pseudosection (bfd
*abfd
,
9874 Elf_Internal_Note
*note
)
9876 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9877 note
->descsz
, note
->descpos
);
9880 /* There isn't a consistent prfpregset_t across platforms,
9881 but it doesn't matter, because we don't have to pick this
9882 data structure apart. */
9885 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9887 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9890 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9891 type of NT_PRXFPREG. Just include the whole note's contents
9895 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9897 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9900 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9901 with a note type of NT_X86_XSTATE. Just include the whole note's
9902 contents literally. */
9905 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9907 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9911 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9913 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9917 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9919 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9923 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9925 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9929 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9931 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9935 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9937 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9941 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9943 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9947 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9949 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9953 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9955 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9959 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9961 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9965 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9967 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9971 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9973 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9977 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9979 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9983 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9985 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9989 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9991 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9995 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9997 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
10001 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
10003 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
10007 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
10009 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
10013 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
10015 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
10019 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10021 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
10025 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
10027 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
10031 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
10033 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
10037 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10039 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
10043 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
10045 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
10049 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
10051 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
10055 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
10057 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
10061 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
10063 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
10067 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
10069 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
10073 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
10075 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
10079 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
10081 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
10085 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
10087 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
10091 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10093 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
10097 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
10099 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
10103 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
10105 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
10109 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
10111 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
10115 elfcore_grok_aarch_mte (bfd
*abfd
, Elf_Internal_Note
*note
)
10117 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-mte",
10122 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
10124 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
10127 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10128 successful otherwise, return FALSE. */
10131 elfcore_grok_riscv_csr (bfd
*abfd
, Elf_Internal_Note
*note
)
10133 return elfcore_make_note_pseudosection (abfd
, ".reg-riscv-csr", note
);
10136 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10137 successful otherwise, return FALSE. */
10140 elfcore_grok_gdb_tdesc (bfd
*abfd
, Elf_Internal_Note
*note
)
10142 return elfcore_make_note_pseudosection (abfd
, ".gdb-tdesc", note
);
10146 elfcore_grok_loongarch_cpucfg (bfd
*abfd
, Elf_Internal_Note
*note
)
10148 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-cpucfg", note
);
10152 elfcore_grok_loongarch_lbt (bfd
*abfd
, Elf_Internal_Note
*note
)
10154 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lbt", note
);
10158 elfcore_grok_loongarch_lsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10160 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lsx", note
);
10164 elfcore_grok_loongarch_lasx (bfd
*abfd
, Elf_Internal_Note
*note
)
10166 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lasx", note
);
10169 #if defined (HAVE_PRPSINFO_T)
10170 typedef prpsinfo_t elfcore_psinfo_t
;
10171 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10172 typedef prpsinfo32_t elfcore_psinfo32_t
;
10176 #if defined (HAVE_PSINFO_T)
10177 typedef psinfo_t elfcore_psinfo_t
;
10178 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10179 typedef psinfo32_t elfcore_psinfo32_t
;
10183 /* return a malloc'ed copy of a string at START which is at
10184 most MAX bytes long, possibly without a terminating '\0'.
10185 the copy will always have a terminating '\0'. */
10188 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
10191 char *end
= (char *) memchr (start
, '\0', max
);
10199 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
10203 memcpy (dups
, start
, len
);
10209 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10211 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10213 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
10215 elfcore_psinfo_t psinfo
;
10217 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10219 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10220 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10222 elf_tdata (abfd
)->core
->program
10223 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10224 sizeof (psinfo
.pr_fname
));
10226 elf_tdata (abfd
)->core
->command
10227 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10228 sizeof (psinfo
.pr_psargs
));
10230 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10231 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
10233 /* 64-bit host, 32-bit corefile */
10234 elfcore_psinfo32_t psinfo
;
10236 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10238 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10239 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10241 elf_tdata (abfd
)->core
->program
10242 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10243 sizeof (psinfo
.pr_fname
));
10245 elf_tdata (abfd
)->core
->command
10246 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10247 sizeof (psinfo
.pr_psargs
));
10253 /* Fail - we don't know how to handle any other
10254 note size (ie. data object type). */
10258 /* Note that for some reason, a spurious space is tacked
10259 onto the end of the args in some (at least one anyway)
10260 implementations, so strip it off if it exists. */
10263 char *command
= elf_tdata (abfd
)->core
->command
;
10264 int n
= strlen (command
);
10266 if (0 < n
&& command
[n
- 1] == ' ')
10267 command
[n
- 1] = '\0';
10272 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10274 #if defined (HAVE_PSTATUS_T)
10276 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10278 if (note
->descsz
== sizeof (pstatus_t
)
10279 #if defined (HAVE_PXSTATUS_T)
10280 || note
->descsz
== sizeof (pxstatus_t
)
10286 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10288 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10290 #if defined (HAVE_PSTATUS32_T)
10291 else if (note
->descsz
== sizeof (pstatus32_t
))
10293 /* 64-bit host, 32-bit corefile */
10296 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10298 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10301 /* Could grab some more details from the "representative"
10302 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10303 NT_LWPSTATUS note, presumably. */
10307 #endif /* defined (HAVE_PSTATUS_T) */
10309 #if defined (HAVE_LWPSTATUS_T)
10311 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10313 lwpstatus_t lwpstat
;
10319 if (note
->descsz
!= sizeof (lwpstat
)
10320 #if defined (HAVE_LWPXSTATUS_T)
10321 && note
->descsz
!= sizeof (lwpxstatus_t
)
10326 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10328 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10329 /* Do not overwrite the core signal if it has already been set by
10331 if (elf_tdata (abfd
)->core
->signal
== 0)
10332 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10334 /* Make a ".reg/999" section. */
10336 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10337 len
= strlen (buf
) + 1;
10338 name
= bfd_alloc (abfd
, len
);
10341 memcpy (name
, buf
, len
);
10343 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10347 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10348 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10349 sect
->filepos
= note
->descpos
10350 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10353 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10354 sect
->size
= sizeof (lwpstat
.pr_reg
);
10355 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10358 sect
->alignment_power
= 2;
10360 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10363 /* Make a ".reg2/999" section */
10365 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10366 len
= strlen (buf
) + 1;
10367 name
= bfd_alloc (abfd
, len
);
10370 memcpy (name
, buf
, len
);
10372 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10376 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10377 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10378 sect
->filepos
= note
->descpos
10379 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10382 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10383 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10384 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10387 sect
->alignment_power
= 2;
10389 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10391 #endif /* defined (HAVE_LWPSTATUS_T) */
10393 /* These constants, and the structure offsets used below, are defined by
10394 Cygwin's core_dump.h */
10395 #define NOTE_INFO_PROCESS 1
10396 #define NOTE_INFO_THREAD 2
10397 #define NOTE_INFO_MODULE 3
10398 #define NOTE_INFO_MODULE64 4
10401 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10406 unsigned int name_size
;
10409 int is_active_thread
;
10412 if (note
->descsz
< 4)
10415 if (! startswith (note
->namedata
, "win32"))
10418 type
= bfd_get_32 (abfd
, note
->descdata
);
10422 const char *type_name
;
10423 unsigned long min_size
;
10426 { "NOTE_INFO_PROCESS", 12 },
10427 { "NOTE_INFO_THREAD", 12 },
10428 { "NOTE_INFO_MODULE", 12 },
10429 { "NOTE_INFO_MODULE64", 16 },
10432 if (type
== 0 || type
> (sizeof(size_check
)/sizeof(size_check
[0])))
10435 if (note
->descsz
< size_check
[type
- 1].min_size
)
10437 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10439 abfd
, size_check
[type
- 1].type_name
, note
->descsz
);
10445 case NOTE_INFO_PROCESS
:
10446 /* FIXME: need to add ->core->command. */
10447 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10448 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10451 case NOTE_INFO_THREAD
:
10452 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10454 /* thread_info.tid */
10455 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 4));
10457 len
= strlen (buf
) + 1;
10458 name
= (char *) bfd_alloc (abfd
, len
);
10462 memcpy (name
, buf
, len
);
10464 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10468 /* sizeof (thread_info.thread_context) */
10469 sect
->size
= note
->descsz
- 12;
10470 /* offsetof (thread_info.thread_context) */
10471 sect
->filepos
= note
->descpos
+ 12;
10472 sect
->alignment_power
= 2;
10474 /* thread_info.is_active_thread */
10475 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10477 if (is_active_thread
)
10478 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10482 case NOTE_INFO_MODULE
:
10483 case NOTE_INFO_MODULE64
:
10484 /* Make a ".module/xxxxxxxx" section. */
10485 if (type
== NOTE_INFO_MODULE
)
10487 /* module_info.base_address */
10488 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10489 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10490 /* module_info.module_name_size */
10491 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10493 else /* NOTE_INFO_MODULE64 */
10495 /* module_info.base_address */
10496 base_addr
= bfd_get_64 (abfd
, note
->descdata
+ 4);
10497 sprintf (buf
, ".module/%016lx", (unsigned long) base_addr
);
10498 /* module_info.module_name_size */
10499 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10502 len
= strlen (buf
) + 1;
10503 name
= (char *) bfd_alloc (abfd
, len
);
10507 memcpy (name
, buf
, len
);
10509 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10514 if (note
->descsz
< 12 + name_size
)
10516 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10517 " is too small to contain a name of size %u"),
10518 abfd
, note
->descsz
, name_size
);
10522 sect
->size
= note
->descsz
;
10523 sect
->filepos
= note
->descpos
;
10524 sect
->alignment_power
= 2;
10535 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10537 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10539 switch (note
->type
)
10545 if (bed
->elf_backend_grok_prstatus
)
10546 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10548 #if defined (HAVE_PRSTATUS_T)
10549 return elfcore_grok_prstatus (abfd
, note
);
10554 #if defined (HAVE_PSTATUS_T)
10556 return elfcore_grok_pstatus (abfd
, note
);
10559 #if defined (HAVE_LWPSTATUS_T)
10561 return elfcore_grok_lwpstatus (abfd
, note
);
10564 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10565 return elfcore_grok_prfpreg (abfd
, note
);
10567 case NT_WIN32PSTATUS
:
10568 return elfcore_grok_win32pstatus (abfd
, note
);
10570 case NT_PRXFPREG
: /* Linux SSE extension */
10571 if (note
->namesz
== 6
10572 && strcmp (note
->namedata
, "LINUX") == 0)
10573 return elfcore_grok_prxfpreg (abfd
, note
);
10577 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10578 if (note
->namesz
== 6
10579 && strcmp (note
->namedata
, "LINUX") == 0)
10580 return elfcore_grok_xstatereg (abfd
, note
);
10585 if (note
->namesz
== 6
10586 && strcmp (note
->namedata
, "LINUX") == 0)
10587 return elfcore_grok_ppc_vmx (abfd
, note
);
10592 if (note
->namesz
== 6
10593 && strcmp (note
->namedata
, "LINUX") == 0)
10594 return elfcore_grok_ppc_vsx (abfd
, note
);
10599 if (note
->namesz
== 6
10600 && strcmp (note
->namedata
, "LINUX") == 0)
10601 return elfcore_grok_ppc_tar (abfd
, note
);
10606 if (note
->namesz
== 6
10607 && strcmp (note
->namedata
, "LINUX") == 0)
10608 return elfcore_grok_ppc_ppr (abfd
, note
);
10613 if (note
->namesz
== 6
10614 && strcmp (note
->namedata
, "LINUX") == 0)
10615 return elfcore_grok_ppc_dscr (abfd
, note
);
10620 if (note
->namesz
== 6
10621 && strcmp (note
->namedata
, "LINUX") == 0)
10622 return elfcore_grok_ppc_ebb (abfd
, note
);
10627 if (note
->namesz
== 6
10628 && strcmp (note
->namedata
, "LINUX") == 0)
10629 return elfcore_grok_ppc_pmu (abfd
, note
);
10633 case NT_PPC_TM_CGPR
:
10634 if (note
->namesz
== 6
10635 && strcmp (note
->namedata
, "LINUX") == 0)
10636 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10640 case NT_PPC_TM_CFPR
:
10641 if (note
->namesz
== 6
10642 && strcmp (note
->namedata
, "LINUX") == 0)
10643 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10647 case NT_PPC_TM_CVMX
:
10648 if (note
->namesz
== 6
10649 && strcmp (note
->namedata
, "LINUX") == 0)
10650 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10654 case NT_PPC_TM_CVSX
:
10655 if (note
->namesz
== 6
10656 && strcmp (note
->namedata
, "LINUX") == 0)
10657 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10661 case NT_PPC_TM_SPR
:
10662 if (note
->namesz
== 6
10663 && strcmp (note
->namedata
, "LINUX") == 0)
10664 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10668 case NT_PPC_TM_CTAR
:
10669 if (note
->namesz
== 6
10670 && strcmp (note
->namedata
, "LINUX") == 0)
10671 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10675 case NT_PPC_TM_CPPR
:
10676 if (note
->namesz
== 6
10677 && strcmp (note
->namedata
, "LINUX") == 0)
10678 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10682 case NT_PPC_TM_CDSCR
:
10683 if (note
->namesz
== 6
10684 && strcmp (note
->namedata
, "LINUX") == 0)
10685 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10689 case NT_S390_HIGH_GPRS
:
10690 if (note
->namesz
== 6
10691 && strcmp (note
->namedata
, "LINUX") == 0)
10692 return elfcore_grok_s390_high_gprs (abfd
, note
);
10696 case NT_S390_TIMER
:
10697 if (note
->namesz
== 6
10698 && strcmp (note
->namedata
, "LINUX") == 0)
10699 return elfcore_grok_s390_timer (abfd
, note
);
10703 case NT_S390_TODCMP
:
10704 if (note
->namesz
== 6
10705 && strcmp (note
->namedata
, "LINUX") == 0)
10706 return elfcore_grok_s390_todcmp (abfd
, note
);
10710 case NT_S390_TODPREG
:
10711 if (note
->namesz
== 6
10712 && strcmp (note
->namedata
, "LINUX") == 0)
10713 return elfcore_grok_s390_todpreg (abfd
, note
);
10718 if (note
->namesz
== 6
10719 && strcmp (note
->namedata
, "LINUX") == 0)
10720 return elfcore_grok_s390_ctrs (abfd
, note
);
10724 case NT_S390_PREFIX
:
10725 if (note
->namesz
== 6
10726 && strcmp (note
->namedata
, "LINUX") == 0)
10727 return elfcore_grok_s390_prefix (abfd
, note
);
10731 case NT_S390_LAST_BREAK
:
10732 if (note
->namesz
== 6
10733 && strcmp (note
->namedata
, "LINUX") == 0)
10734 return elfcore_grok_s390_last_break (abfd
, note
);
10738 case NT_S390_SYSTEM_CALL
:
10739 if (note
->namesz
== 6
10740 && strcmp (note
->namedata
, "LINUX") == 0)
10741 return elfcore_grok_s390_system_call (abfd
, note
);
10746 if (note
->namesz
== 6
10747 && strcmp (note
->namedata
, "LINUX") == 0)
10748 return elfcore_grok_s390_tdb (abfd
, note
);
10752 case NT_S390_VXRS_LOW
:
10753 if (note
->namesz
== 6
10754 && strcmp (note
->namedata
, "LINUX") == 0)
10755 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10759 case NT_S390_VXRS_HIGH
:
10760 if (note
->namesz
== 6
10761 && strcmp (note
->namedata
, "LINUX") == 0)
10762 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10766 case NT_S390_GS_CB
:
10767 if (note
->namesz
== 6
10768 && strcmp (note
->namedata
, "LINUX") == 0)
10769 return elfcore_grok_s390_gs_cb (abfd
, note
);
10773 case NT_S390_GS_BC
:
10774 if (note
->namesz
== 6
10775 && strcmp (note
->namedata
, "LINUX") == 0)
10776 return elfcore_grok_s390_gs_bc (abfd
, note
);
10781 if (note
->namesz
== 6
10782 && strcmp (note
->namedata
, "LINUX") == 0)
10783 return elfcore_grok_arc_v2 (abfd
, note
);
10788 if (note
->namesz
== 6
10789 && strcmp (note
->namedata
, "LINUX") == 0)
10790 return elfcore_grok_arm_vfp (abfd
, note
);
10795 if (note
->namesz
== 6
10796 && strcmp (note
->namedata
, "LINUX") == 0)
10797 return elfcore_grok_aarch_tls (abfd
, note
);
10801 case NT_ARM_HW_BREAK
:
10802 if (note
->namesz
== 6
10803 && strcmp (note
->namedata
, "LINUX") == 0)
10804 return elfcore_grok_aarch_hw_break (abfd
, note
);
10808 case NT_ARM_HW_WATCH
:
10809 if (note
->namesz
== 6
10810 && strcmp (note
->namedata
, "LINUX") == 0)
10811 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10816 if (note
->namesz
== 6
10817 && strcmp (note
->namedata
, "LINUX") == 0)
10818 return elfcore_grok_aarch_sve (abfd
, note
);
10822 case NT_ARM_PAC_MASK
:
10823 if (note
->namesz
== 6
10824 && strcmp (note
->namedata
, "LINUX") == 0)
10825 return elfcore_grok_aarch_pauth (abfd
, note
);
10829 case NT_ARM_TAGGED_ADDR_CTRL
:
10830 if (note
->namesz
== 6
10831 && strcmp (note
->namedata
, "LINUX") == 0)
10832 return elfcore_grok_aarch_mte (abfd
, note
);
10837 if (note
->namesz
== 4
10838 && strcmp (note
->namedata
, "GDB") == 0)
10839 return elfcore_grok_gdb_tdesc (abfd
, note
);
10844 if (note
->namesz
== 4
10845 && strcmp (note
->namedata
, "GDB") == 0)
10846 return elfcore_grok_riscv_csr (abfd
, note
);
10850 case NT_LARCH_CPUCFG
:
10851 if (note
->namesz
== 6
10852 && strcmp (note
->namedata
, "LINUX") == 0)
10853 return elfcore_grok_loongarch_cpucfg (abfd
, note
);
10858 if (note
->namesz
== 6
10859 && strcmp (note
->namedata
, "LINUX") == 0)
10860 return elfcore_grok_loongarch_lbt (abfd
, note
);
10865 if (note
->namesz
== 6
10866 && strcmp (note
->namedata
, "LINUX") == 0)
10867 return elfcore_grok_loongarch_lsx (abfd
, note
);
10871 case NT_LARCH_LASX
:
10872 if (note
->namesz
== 6
10873 && strcmp (note
->namedata
, "LINUX") == 0)
10874 return elfcore_grok_loongarch_lasx (abfd
, note
);
10880 if (bed
->elf_backend_grok_psinfo
)
10881 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10883 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10884 return elfcore_grok_psinfo (abfd
, note
);
10890 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10893 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10897 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10904 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10906 struct bfd_build_id
* build_id
;
10908 if (note
->descsz
== 0)
10911 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10912 if (build_id
== NULL
)
10915 build_id
->size
= note
->descsz
;
10916 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10917 abfd
->build_id
= build_id
;
10923 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10925 switch (note
->type
)
10930 case NT_GNU_PROPERTY_TYPE_0
:
10931 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10933 case NT_GNU_BUILD_ID
:
10934 return elfobj_grok_gnu_build_id (abfd
, note
);
10939 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10941 struct sdt_note
*cur
=
10942 (struct sdt_note
*) bfd_alloc (abfd
,
10943 sizeof (struct sdt_note
) + note
->descsz
);
10945 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10946 cur
->size
= (bfd_size_type
) note
->descsz
;
10947 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10949 elf_tdata (abfd
)->sdt_note_head
= cur
;
10955 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10957 switch (note
->type
)
10960 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10968 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10972 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10975 if (note
->descsz
< 108)
10980 if (note
->descsz
< 120)
10988 /* Check for version 1 in pr_version. */
10989 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10994 /* Skip over pr_psinfosz. */
10995 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10999 offset
+= 4; /* Padding before pr_psinfosz. */
11003 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11004 elf_tdata (abfd
)->core
->program
11005 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
11008 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11009 elf_tdata (abfd
)->core
->command
11010 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
11013 /* Padding before pr_pid. */
11016 /* The pr_pid field was added in version "1a". */
11017 if (note
->descsz
< offset
+ 4)
11020 elf_tdata (abfd
)->core
->pid
11021 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11027 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
11033 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11034 Also compute minimum size of this note. */
11035 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11039 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
11043 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
11044 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
11051 if (note
->descsz
< min_size
)
11054 /* Check for version 1 in pr_version. */
11055 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11058 /* Extract size of pr_reg from pr_gregsetsz. */
11059 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11060 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11062 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11067 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11071 /* Skip over pr_osreldate. */
11074 /* Read signal from pr_cursig. */
11075 if (elf_tdata (abfd
)->core
->signal
== 0)
11076 elf_tdata (abfd
)->core
->signal
11077 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11080 /* Read TID from pr_pid. */
11081 elf_tdata (abfd
)->core
->lwpid
11082 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11085 /* Padding before pr_reg. */
11086 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
11089 /* Make sure that there is enough data remaining in the note. */
11090 if ((note
->descsz
- offset
) < size
)
11093 /* Make a ".reg/999" section and a ".reg" section. */
11094 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
11095 size
, note
->descpos
+ offset
);
11099 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11101 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11103 switch (note
->type
)
11106 if (bed
->elf_backend_grok_freebsd_prstatus
)
11107 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
11109 return elfcore_grok_freebsd_prstatus (abfd
, note
);
11112 return elfcore_grok_prfpreg (abfd
, note
);
11115 return elfcore_grok_freebsd_psinfo (abfd
, note
);
11117 case NT_FREEBSD_THRMISC
:
11118 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
11120 case NT_FREEBSD_PROCSTAT_PROC
:
11121 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
11124 case NT_FREEBSD_PROCSTAT_FILES
:
11125 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
11128 case NT_FREEBSD_PROCSTAT_VMMAP
:
11129 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
11132 case NT_FREEBSD_PROCSTAT_AUXV
:
11133 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11135 case NT_FREEBSD_X86_SEGBASES
:
11136 return elfcore_make_note_pseudosection (abfd
, ".reg-x86-segbases", note
);
11138 case NT_X86_XSTATE
:
11139 return elfcore_grok_xstatereg (abfd
, note
);
11141 case NT_FREEBSD_PTLWPINFO
:
11142 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
11146 return elfcore_grok_aarch_tls (abfd
, note
);
11149 return elfcore_grok_arm_vfp (abfd
, note
);
11157 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
11161 cp
= strchr (note
->namedata
, '@');
11164 *lwpidp
= atoi(cp
+ 1);
11171 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11173 if (note
->descsz
<= 0x7c + 31)
11176 /* Signal number at offset 0x08. */
11177 elf_tdata (abfd
)->core
->signal
11178 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11180 /* Process ID at offset 0x50. */
11181 elf_tdata (abfd
)->core
->pid
11182 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
11184 /* Command name at 0x7c (max 32 bytes, including nul). */
11185 elf_tdata (abfd
)->core
->command
11186 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
11188 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
11193 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11197 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
11198 elf_tdata (abfd
)->core
->lwpid
= lwp
;
11200 switch (note
->type
)
11202 case NT_NETBSDCORE_PROCINFO
:
11203 /* NetBSD-specific core "procinfo". Note that we expect to
11204 find this note before any of the others, which is fine,
11205 since the kernel writes this note out first when it
11206 creates a core file. */
11207 return elfcore_grok_netbsd_procinfo (abfd
, note
);
11208 case NT_NETBSDCORE_AUXV
:
11209 /* NetBSD-specific Elf Auxiliary Vector data. */
11210 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11211 case NT_NETBSDCORE_LWPSTATUS
:
11212 return elfcore_make_note_pseudosection (abfd
,
11213 ".note.netbsdcore.lwpstatus",
11219 /* As of March 2020 there are no other machine-independent notes
11220 defined for NetBSD core files. If the note type is less
11221 than the start of the machine-dependent note types, we don't
11224 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
11228 switch (bfd_get_arch (abfd
))
11230 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11231 PT_GETFPREGS == mach+2. */
11233 case bfd_arch_aarch64
:
11234 case bfd_arch_alpha
:
11235 case bfd_arch_sparc
:
11236 switch (note
->type
)
11238 case NT_NETBSDCORE_FIRSTMACH
+0:
11239 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11241 case NT_NETBSDCORE_FIRSTMACH
+2:
11242 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11248 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11249 There's also old PT___GETREGS40 == mach + 1 for old reg
11250 structure which lacks GBR. */
11253 switch (note
->type
)
11255 case NT_NETBSDCORE_FIRSTMACH
+3:
11256 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11258 case NT_NETBSDCORE_FIRSTMACH
+5:
11259 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11265 /* On all other arch's, PT_GETREGS == mach+1 and
11266 PT_GETFPREGS == mach+3. */
11269 switch (note
->type
)
11271 case NT_NETBSDCORE_FIRSTMACH
+1:
11272 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11274 case NT_NETBSDCORE_FIRSTMACH
+3:
11275 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11285 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11287 if (note
->descsz
<= 0x48 + 31)
11290 /* Signal number at offset 0x08. */
11291 elf_tdata (abfd
)->core
->signal
11292 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11294 /* Process ID at offset 0x20. */
11295 elf_tdata (abfd
)->core
->pid
11296 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
11298 /* Command name at 0x48 (max 32 bytes, including nul). */
11299 elf_tdata (abfd
)->core
->command
11300 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
11305 /* Processes Solaris's process status note.
11306 sig_off ~ offsetof(prstatus_t, pr_cursig)
11307 pid_off ~ offsetof(prstatus_t, pr_pid)
11308 lwpid_off ~ offsetof(prstatus_t, pr_who)
11309 gregset_size ~ sizeof(gregset_t)
11310 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11313 elfcore_grok_solaris_prstatus (bfd
*abfd
, Elf_Internal_Note
* note
, int sig_off
,
11314 int pid_off
, int lwpid_off
, size_t gregset_size
,
11315 size_t gregset_offset
)
11317 asection
*sect
= NULL
;
11318 elf_tdata (abfd
)->core
->signal
11319 = bfd_get_16 (abfd
, note
->descdata
+ sig_off
);
11320 elf_tdata (abfd
)->core
->pid
11321 = bfd_get_32 (abfd
, note
->descdata
+ pid_off
);
11322 elf_tdata (abfd
)->core
->lwpid
11323 = bfd_get_32 (abfd
, note
->descdata
+ lwpid_off
);
11325 sect
= bfd_get_section_by_name (abfd
, ".reg");
11327 sect
->size
= gregset_size
;
11329 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11330 note
->descpos
+ gregset_offset
);
11333 /* Gets program and arguments from a core.
11334 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11335 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11338 elfcore_grok_solaris_info(bfd
*abfd
, Elf_Internal_Note
* note
,
11339 int prog_off
, int comm_off
)
11341 elf_tdata (abfd
)->core
->program
11342 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ prog_off
, 16);
11343 elf_tdata (abfd
)->core
->command
11344 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ comm_off
, 80);
11349 /* Processes Solaris's LWP status note.
11350 gregset_size ~ sizeof(gregset_t)
11351 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11352 fpregset_size ~ sizeof(fpregset_t)
11353 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11356 elfcore_grok_solaris_lwpstatus (bfd
*abfd
, Elf_Internal_Note
* note
,
11357 size_t gregset_size
, int gregset_off
,
11358 size_t fpregset_size
, int fpregset_off
)
11360 asection
*sect
= NULL
;
11361 char reg2_section_name
[16] = { 0 };
11363 (void) snprintf (reg2_section_name
, 16, "%s/%i", ".reg2",
11364 elf_tdata (abfd
)->core
->lwpid
);
11366 /* offsetof(lwpstatus_t, pr_lwpid) */
11367 elf_tdata (abfd
)->core
->lwpid
11368 = bfd_get_32 (abfd
, note
->descdata
+ 4);
11369 /* offsetof(lwpstatus_t, pr_cursig) */
11370 elf_tdata (abfd
)->core
->signal
11371 = bfd_get_16 (abfd
, note
->descdata
+ 12);
11373 sect
= bfd_get_section_by_name (abfd
, ".reg");
11375 sect
->size
= gregset_size
;
11376 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11377 note
->descpos
+ gregset_off
))
11380 sect
= bfd_get_section_by_name (abfd
, reg2_section_name
);
11383 sect
->size
= fpregset_size
;
11384 sect
->filepos
= note
->descpos
+ fpregset_off
;
11385 sect
->alignment_power
= 2;
11387 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg2", fpregset_size
,
11388 note
->descpos
+ fpregset_off
))
11395 elfcore_grok_solaris_note_impl (bfd
*abfd
, Elf_Internal_Note
*note
)
11400 /* core files are identified as 32- or 64-bit, SPARC or x86,
11401 by the size of the descsz which matches the sizeof()
11402 the type appropriate for that note type (e.g., prstatus_t for
11403 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11404 on Solaris. The core file bitness may differ from the bitness of
11405 gdb itself, so fixed values are used instead of sizeof().
11406 Appropriate fixed offsets are also used to obtain data from
11409 switch ((int) note
->type
)
11411 case SOLARIS_NT_PRSTATUS
:
11412 switch (note
->descsz
)
11414 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11415 return elfcore_grok_solaris_prstatus(abfd
, note
,
11416 136, 216, 308, 152, 356);
11417 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11418 return elfcore_grok_solaris_prstatus(abfd
, note
,
11419 264, 360, 520, 304, 600);
11420 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11421 return elfcore_grok_solaris_prstatus(abfd
, note
,
11422 136, 216, 308, 76, 356);
11423 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11424 return elfcore_grok_solaris_prstatus(abfd
, note
,
11425 264, 360, 520, 224, 600);
11430 case SOLARIS_NT_PSINFO
:
11431 case SOLARIS_NT_PRPSINFO
:
11432 switch (note
->descsz
)
11434 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11435 return elfcore_grok_solaris_info(abfd
, note
, 84, 100);
11436 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11437 return elfcore_grok_solaris_info(abfd
, note
, 120, 136);
11438 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11439 return elfcore_grok_solaris_info(abfd
, note
, 88, 104);
11440 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11441 return elfcore_grok_solaris_info(abfd
, note
, 136, 152);
11446 case SOLARIS_NT_LWPSTATUS
:
11447 switch (note
->descsz
)
11449 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11450 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
11451 152, 344, 400, 496);
11452 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11453 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
11454 304, 544, 544, 848);
11455 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11456 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
11457 76, 344, 380, 420);
11458 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11459 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
11460 224, 544, 528, 768);
11465 case SOLARIS_NT_LWPSINFO
:
11466 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11467 if (note
->descsz
== 128 || note
->descsz
== 152)
11468 elf_tdata (abfd
)->core
->lwpid
=
11469 bfd_get_32 (abfd
, note
->descdata
+ 4);
11479 /* For name starting with "CORE" this may be either a Solaris
11480 core file or a gdb-generated core file. Do Solaris-specific
11481 processing on selected note types first with
11482 elfcore_grok_solaris_note(), then process the note
11483 in elfcore_grok_note(). */
11486 elfcore_grok_solaris_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11488 if (!elfcore_grok_solaris_note_impl (abfd
, note
))
11491 return elfcore_grok_note (abfd
, note
);
11495 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11497 if (note
->type
== NT_OPENBSD_PROCINFO
)
11498 return elfcore_grok_openbsd_procinfo (abfd
, note
);
11500 if (note
->type
== NT_OPENBSD_REGS
)
11501 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11503 if (note
->type
== NT_OPENBSD_FPREGS
)
11504 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11506 if (note
->type
== NT_OPENBSD_XFPREGS
)
11507 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
11509 if (note
->type
== NT_OPENBSD_AUXV
)
11510 return elfcore_make_auxv_note_section (abfd
, note
, 0);
11512 if (note
->type
== NT_OPENBSD_WCOOKIE
)
11514 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
11519 sect
->size
= note
->descsz
;
11520 sect
->filepos
= note
->descpos
;
11521 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
11530 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
11532 void *ddata
= note
->descdata
;
11539 if (note
->descsz
< 16)
11542 /* nto_procfs_status 'pid' field is at offset 0. */
11543 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
11545 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11546 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
11548 /* nto_procfs_status 'flags' field is at offset 8. */
11549 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
11551 /* nto_procfs_status 'what' field is at offset 14. */
11552 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
11554 elf_tdata (abfd
)->core
->signal
= sig
;
11555 elf_tdata (abfd
)->core
->lwpid
= *tid
;
11558 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11559 do not come from signals so we make sure we set the current
11560 thread just in case. */
11561 if (flags
& 0x00000080)
11562 elf_tdata (abfd
)->core
->lwpid
= *tid
;
11564 /* Make a ".qnx_core_status/%d" section. */
11565 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
11567 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11570 strcpy (name
, buf
);
11572 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11576 sect
->size
= note
->descsz
;
11577 sect
->filepos
= note
->descpos
;
11578 sect
->alignment_power
= 2;
11580 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
11584 elfcore_grok_nto_regs (bfd
*abfd
,
11585 Elf_Internal_Note
*note
,
11593 /* Make a "(base)/%d" section. */
11594 sprintf (buf
, "%s/%ld", base
, tid
);
11596 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11599 strcpy (name
, buf
);
11601 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11605 sect
->size
= note
->descsz
;
11606 sect
->filepos
= note
->descpos
;
11607 sect
->alignment_power
= 2;
11609 /* This is the current thread. */
11610 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
11611 return elfcore_maybe_make_sect (abfd
, base
, sect
);
11616 #define BFD_QNT_CORE_INFO 7
11617 #define BFD_QNT_CORE_STATUS 8
11618 #define BFD_QNT_CORE_GREG 9
11619 #define BFD_QNT_CORE_FPREG 10
11622 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11624 /* Every GREG section has a STATUS section before it. Store the
11625 tid from the previous call to pass down to the next gregs
11627 static long tid
= 1;
11629 switch (note
->type
)
11631 case BFD_QNT_CORE_INFO
:
11632 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
11633 case BFD_QNT_CORE_STATUS
:
11634 return elfcore_grok_nto_status (abfd
, note
, &tid
);
11635 case BFD_QNT_CORE_GREG
:
11636 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
11637 case BFD_QNT_CORE_FPREG
:
11638 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
11645 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11651 /* Use note name as section name. */
11652 len
= note
->namesz
;
11653 name
= (char *) bfd_alloc (abfd
, len
);
11656 memcpy (name
, note
->namedata
, len
);
11657 name
[len
- 1] = '\0';
11659 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11663 sect
->size
= note
->descsz
;
11664 sect
->filepos
= note
->descpos
;
11665 sect
->alignment_power
= 1;
11670 /* Function: elfcore_write_note
11673 buffer to hold note, and current size of buffer
11677 size of data for note
11679 Writes note to end of buffer. ELF64 notes are written exactly as
11680 for ELF32, despite the current (as of 2006) ELF gabi specifying
11681 that they ought to have 8-byte namesz and descsz field, and have
11682 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11685 Pointer to realloc'd buffer, *BUFSIZ updated. */
11688 elfcore_write_note (bfd
*abfd
,
11696 Elf_External_Note
*xnp
;
11703 namesz
= strlen (name
) + 1;
11705 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11707 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11710 dest
= buf
+ *bufsiz
;
11711 *bufsiz
+= newspace
;
11712 xnp
= (Elf_External_Note
*) dest
;
11713 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11714 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11715 H_PUT_32 (abfd
, type
, xnp
->type
);
11719 memcpy (dest
, name
, namesz
);
11727 memcpy (dest
, input
, size
);
11737 /* gcc-8 warns (*) on all the strncpy calls in this function about
11738 possible string truncation. The "truncation" is not a bug. We
11739 have an external representation of structs with fields that are not
11740 necessarily NULL terminated and corresponding internal
11741 representation fields that are one larger so that they can always
11742 be NULL terminated.
11743 gcc versions between 4.2 and 4.6 do not allow pragma control of
11744 diagnostics inside functions, giving a hard error if you try to use
11745 the finer control available with later versions.
11746 gcc prior to 4.2 warns about diagnostic push and pop.
11747 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11748 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11749 (*) Depending on your system header files! */
11750 #if GCC_VERSION >= 8000
11751 # pragma GCC diagnostic push
11752 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11755 elfcore_write_prpsinfo (bfd
*abfd
,
11759 const char *psargs
)
11761 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11763 if (bed
->elf_backend_write_core_note
!= NULL
)
11766 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11767 NT_PRPSINFO
, fname
, psargs
);
11772 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11773 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11774 if (bed
->s
->elfclass
== ELFCLASS32
)
11776 # if defined (HAVE_PSINFO32_T)
11778 int note_type
= NT_PSINFO
;
11781 int note_type
= NT_PRPSINFO
;
11784 memset (&data
, 0, sizeof (data
));
11785 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11786 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11787 return elfcore_write_note (abfd
, buf
, bufsiz
,
11788 "CORE", note_type
, &data
, sizeof (data
));
11793 # if defined (HAVE_PSINFO_T)
11795 int note_type
= NT_PSINFO
;
11798 int note_type
= NT_PRPSINFO
;
11801 memset (&data
, 0, sizeof (data
));
11802 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11803 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11804 return elfcore_write_note (abfd
, buf
, bufsiz
,
11805 "CORE", note_type
, &data
, sizeof (data
));
11807 #endif /* PSINFO_T or PRPSINFO_T */
11812 #if GCC_VERSION >= 8000
11813 # pragma GCC diagnostic pop
11817 elfcore_write_linux_prpsinfo32
11818 (bfd
*abfd
, char *buf
, int *bufsiz
,
11819 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11821 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11823 struct elf_external_linux_prpsinfo32_ugid16 data
;
11825 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11826 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11827 &data
, sizeof (data
));
11831 struct elf_external_linux_prpsinfo32_ugid32 data
;
11833 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11834 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11835 &data
, sizeof (data
));
11840 elfcore_write_linux_prpsinfo64
11841 (bfd
*abfd
, char *buf
, int *bufsiz
,
11842 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11844 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11846 struct elf_external_linux_prpsinfo64_ugid16 data
;
11848 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11849 return elfcore_write_note (abfd
, buf
, bufsiz
,
11850 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11854 struct elf_external_linux_prpsinfo64_ugid32 data
;
11856 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11857 return elfcore_write_note (abfd
, buf
, bufsiz
,
11858 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11863 elfcore_write_prstatus (bfd
*abfd
,
11870 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11872 if (bed
->elf_backend_write_core_note
!= NULL
)
11875 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11877 pid
, cursig
, gregs
);
11882 #if defined (HAVE_PRSTATUS_T)
11883 #if defined (HAVE_PRSTATUS32_T)
11884 if (bed
->s
->elfclass
== ELFCLASS32
)
11886 prstatus32_t prstat
;
11888 memset (&prstat
, 0, sizeof (prstat
));
11889 prstat
.pr_pid
= pid
;
11890 prstat
.pr_cursig
= cursig
;
11891 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11892 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11893 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11900 memset (&prstat
, 0, sizeof (prstat
));
11901 prstat
.pr_pid
= pid
;
11902 prstat
.pr_cursig
= cursig
;
11903 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11904 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11905 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11907 #endif /* HAVE_PRSTATUS_T */
11913 #if defined (HAVE_LWPSTATUS_T)
11915 elfcore_write_lwpstatus (bfd
*abfd
,
11922 lwpstatus_t lwpstat
;
11923 const char *note_name
= "CORE";
11925 memset (&lwpstat
, 0, sizeof (lwpstat
));
11926 lwpstat
.pr_lwpid
= pid
>> 16;
11927 lwpstat
.pr_cursig
= cursig
;
11928 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11929 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11930 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11931 #if !defined(gregs)
11932 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11933 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11935 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11936 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11939 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11940 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11942 #endif /* HAVE_LWPSTATUS_T */
11944 #if defined (HAVE_PSTATUS_T)
11946 elfcore_write_pstatus (bfd
*abfd
,
11950 int cursig ATTRIBUTE_UNUSED
,
11951 const void *gregs ATTRIBUTE_UNUSED
)
11953 const char *note_name
= "CORE";
11954 #if defined (HAVE_PSTATUS32_T)
11955 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11957 if (bed
->s
->elfclass
== ELFCLASS32
)
11961 memset (&pstat
, 0, sizeof (pstat
));
11962 pstat
.pr_pid
= pid
& 0xffff;
11963 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11964 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11972 memset (&pstat
, 0, sizeof (pstat
));
11973 pstat
.pr_pid
= pid
& 0xffff;
11974 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11975 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11979 #endif /* HAVE_PSTATUS_T */
11982 elfcore_write_prfpreg (bfd
*abfd
,
11985 const void *fpregs
,
11988 const char *note_name
= "CORE";
11989 return elfcore_write_note (abfd
, buf
, bufsiz
,
11990 note_name
, NT_FPREGSET
, fpregs
, size
);
11994 elfcore_write_prxfpreg (bfd
*abfd
,
11997 const void *xfpregs
,
12000 char *note_name
= "LINUX";
12001 return elfcore_write_note (abfd
, buf
, bufsiz
,
12002 note_name
, NT_PRXFPREG
, xfpregs
, size
);
12006 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
12007 const void *xfpregs
, int size
)
12010 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
12011 note_name
= "FreeBSD";
12013 note_name
= "LINUX";
12014 return elfcore_write_note (abfd
, buf
, bufsiz
,
12015 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
12019 elfcore_write_x86_segbases (bfd
*abfd
, char *buf
, int *bufsiz
,
12020 const void *regs
, int size
)
12022 char *note_name
= "FreeBSD";
12023 return elfcore_write_note (abfd
, buf
, bufsiz
,
12024 note_name
, NT_FREEBSD_X86_SEGBASES
, regs
, size
);
12028 elfcore_write_ppc_vmx (bfd
*abfd
,
12031 const void *ppc_vmx
,
12034 char *note_name
= "LINUX";
12035 return elfcore_write_note (abfd
, buf
, bufsiz
,
12036 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
12040 elfcore_write_ppc_vsx (bfd
*abfd
,
12043 const void *ppc_vsx
,
12046 char *note_name
= "LINUX";
12047 return elfcore_write_note (abfd
, buf
, bufsiz
,
12048 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
12052 elfcore_write_ppc_tar (bfd
*abfd
,
12055 const void *ppc_tar
,
12058 char *note_name
= "LINUX";
12059 return elfcore_write_note (abfd
, buf
, bufsiz
,
12060 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
12064 elfcore_write_ppc_ppr (bfd
*abfd
,
12067 const void *ppc_ppr
,
12070 char *note_name
= "LINUX";
12071 return elfcore_write_note (abfd
, buf
, bufsiz
,
12072 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
12076 elfcore_write_ppc_dscr (bfd
*abfd
,
12079 const void *ppc_dscr
,
12082 char *note_name
= "LINUX";
12083 return elfcore_write_note (abfd
, buf
, bufsiz
,
12084 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
12088 elfcore_write_ppc_ebb (bfd
*abfd
,
12091 const void *ppc_ebb
,
12094 char *note_name
= "LINUX";
12095 return elfcore_write_note (abfd
, buf
, bufsiz
,
12096 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
12100 elfcore_write_ppc_pmu (bfd
*abfd
,
12103 const void *ppc_pmu
,
12106 char *note_name
= "LINUX";
12107 return elfcore_write_note (abfd
, buf
, bufsiz
,
12108 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
12112 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
12115 const void *ppc_tm_cgpr
,
12118 char *note_name
= "LINUX";
12119 return elfcore_write_note (abfd
, buf
, bufsiz
,
12120 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
12124 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
12127 const void *ppc_tm_cfpr
,
12130 char *note_name
= "LINUX";
12131 return elfcore_write_note (abfd
, buf
, bufsiz
,
12132 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
12136 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
12139 const void *ppc_tm_cvmx
,
12142 char *note_name
= "LINUX";
12143 return elfcore_write_note (abfd
, buf
, bufsiz
,
12144 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
12148 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
12151 const void *ppc_tm_cvsx
,
12154 char *note_name
= "LINUX";
12155 return elfcore_write_note (abfd
, buf
, bufsiz
,
12156 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
12160 elfcore_write_ppc_tm_spr (bfd
*abfd
,
12163 const void *ppc_tm_spr
,
12166 char *note_name
= "LINUX";
12167 return elfcore_write_note (abfd
, buf
, bufsiz
,
12168 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
12172 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
12175 const void *ppc_tm_ctar
,
12178 char *note_name
= "LINUX";
12179 return elfcore_write_note (abfd
, buf
, bufsiz
,
12180 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
12184 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
12187 const void *ppc_tm_cppr
,
12190 char *note_name
= "LINUX";
12191 return elfcore_write_note (abfd
, buf
, bufsiz
,
12192 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
12196 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
12199 const void *ppc_tm_cdscr
,
12202 char *note_name
= "LINUX";
12203 return elfcore_write_note (abfd
, buf
, bufsiz
,
12204 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
12208 elfcore_write_s390_high_gprs (bfd
*abfd
,
12211 const void *s390_high_gprs
,
12214 char *note_name
= "LINUX";
12215 return elfcore_write_note (abfd
, buf
, bufsiz
,
12216 note_name
, NT_S390_HIGH_GPRS
,
12217 s390_high_gprs
, size
);
12221 elfcore_write_s390_timer (bfd
*abfd
,
12224 const void *s390_timer
,
12227 char *note_name
= "LINUX";
12228 return elfcore_write_note (abfd
, buf
, bufsiz
,
12229 note_name
, NT_S390_TIMER
, s390_timer
, size
);
12233 elfcore_write_s390_todcmp (bfd
*abfd
,
12236 const void *s390_todcmp
,
12239 char *note_name
= "LINUX";
12240 return elfcore_write_note (abfd
, buf
, bufsiz
,
12241 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
12245 elfcore_write_s390_todpreg (bfd
*abfd
,
12248 const void *s390_todpreg
,
12251 char *note_name
= "LINUX";
12252 return elfcore_write_note (abfd
, buf
, bufsiz
,
12253 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
12257 elfcore_write_s390_ctrs (bfd
*abfd
,
12260 const void *s390_ctrs
,
12263 char *note_name
= "LINUX";
12264 return elfcore_write_note (abfd
, buf
, bufsiz
,
12265 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
12269 elfcore_write_s390_prefix (bfd
*abfd
,
12272 const void *s390_prefix
,
12275 char *note_name
= "LINUX";
12276 return elfcore_write_note (abfd
, buf
, bufsiz
,
12277 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
12281 elfcore_write_s390_last_break (bfd
*abfd
,
12284 const void *s390_last_break
,
12287 char *note_name
= "LINUX";
12288 return elfcore_write_note (abfd
, buf
, bufsiz
,
12289 note_name
, NT_S390_LAST_BREAK
,
12290 s390_last_break
, size
);
12294 elfcore_write_s390_system_call (bfd
*abfd
,
12297 const void *s390_system_call
,
12300 char *note_name
= "LINUX";
12301 return elfcore_write_note (abfd
, buf
, bufsiz
,
12302 note_name
, NT_S390_SYSTEM_CALL
,
12303 s390_system_call
, size
);
12307 elfcore_write_s390_tdb (bfd
*abfd
,
12310 const void *s390_tdb
,
12313 char *note_name
= "LINUX";
12314 return elfcore_write_note (abfd
, buf
, bufsiz
,
12315 note_name
, NT_S390_TDB
, s390_tdb
, size
);
12319 elfcore_write_s390_vxrs_low (bfd
*abfd
,
12322 const void *s390_vxrs_low
,
12325 char *note_name
= "LINUX";
12326 return elfcore_write_note (abfd
, buf
, bufsiz
,
12327 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
12331 elfcore_write_s390_vxrs_high (bfd
*abfd
,
12334 const void *s390_vxrs_high
,
12337 char *note_name
= "LINUX";
12338 return elfcore_write_note (abfd
, buf
, bufsiz
,
12339 note_name
, NT_S390_VXRS_HIGH
,
12340 s390_vxrs_high
, size
);
12344 elfcore_write_s390_gs_cb (bfd
*abfd
,
12347 const void *s390_gs_cb
,
12350 char *note_name
= "LINUX";
12351 return elfcore_write_note (abfd
, buf
, bufsiz
,
12352 note_name
, NT_S390_GS_CB
,
12357 elfcore_write_s390_gs_bc (bfd
*abfd
,
12360 const void *s390_gs_bc
,
12363 char *note_name
= "LINUX";
12364 return elfcore_write_note (abfd
, buf
, bufsiz
,
12365 note_name
, NT_S390_GS_BC
,
12370 elfcore_write_arm_vfp (bfd
*abfd
,
12373 const void *arm_vfp
,
12376 char *note_name
= "LINUX";
12377 return elfcore_write_note (abfd
, buf
, bufsiz
,
12378 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
12382 elfcore_write_aarch_tls (bfd
*abfd
,
12385 const void *aarch_tls
,
12388 char *note_name
= "LINUX";
12389 return elfcore_write_note (abfd
, buf
, bufsiz
,
12390 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
12394 elfcore_write_aarch_hw_break (bfd
*abfd
,
12397 const void *aarch_hw_break
,
12400 char *note_name
= "LINUX";
12401 return elfcore_write_note (abfd
, buf
, bufsiz
,
12402 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
12406 elfcore_write_aarch_hw_watch (bfd
*abfd
,
12409 const void *aarch_hw_watch
,
12412 char *note_name
= "LINUX";
12413 return elfcore_write_note (abfd
, buf
, bufsiz
,
12414 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
12418 elfcore_write_aarch_sve (bfd
*abfd
,
12421 const void *aarch_sve
,
12424 char *note_name
= "LINUX";
12425 return elfcore_write_note (abfd
, buf
, bufsiz
,
12426 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
12430 elfcore_write_aarch_pauth (bfd
*abfd
,
12433 const void *aarch_pauth
,
12436 char *note_name
= "LINUX";
12437 return elfcore_write_note (abfd
, buf
, bufsiz
,
12438 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
12442 elfcore_write_aarch_mte (bfd
*abfd
,
12445 const void *aarch_mte
,
12448 char *note_name
= "LINUX";
12449 return elfcore_write_note (abfd
, buf
, bufsiz
,
12450 note_name
, NT_ARM_TAGGED_ADDR_CTRL
,
12456 elfcore_write_arc_v2 (bfd
*abfd
,
12459 const void *arc_v2
,
12462 char *note_name
= "LINUX";
12463 return elfcore_write_note (abfd
, buf
, bufsiz
,
12464 note_name
, NT_ARC_V2
, arc_v2
, size
);
12468 elfcore_write_loongarch_cpucfg (bfd
*abfd
,
12471 const void *loongarch_cpucfg
,
12474 char *note_name
= "LINUX";
12475 return elfcore_write_note (abfd
, buf
, bufsiz
,
12476 note_name
, NT_LARCH_CPUCFG
,
12477 loongarch_cpucfg
, size
);
12481 elfcore_write_loongarch_lbt (bfd
*abfd
,
12484 const void *loongarch_lbt
,
12487 char *note_name
= "LINUX";
12488 return elfcore_write_note (abfd
, buf
, bufsiz
,
12489 note_name
, NT_LARCH_LBT
, loongarch_lbt
, size
);
12493 elfcore_write_loongarch_lsx (bfd
*abfd
,
12496 const void *loongarch_lsx
,
12499 char *note_name
= "LINUX";
12500 return elfcore_write_note (abfd
, buf
, bufsiz
,
12501 note_name
, NT_LARCH_LSX
, loongarch_lsx
, size
);
12505 elfcore_write_loongarch_lasx (bfd
*abfd
,
12508 const void *loongarch_lasx
,
12511 char *note_name
= "LINUX";
12512 return elfcore_write_note (abfd
, buf
, bufsiz
,
12513 note_name
, NT_LARCH_LASX
, loongarch_lasx
, size
);
12516 /* Write the buffer of csr values in CSRS (length SIZE) into the note
12517 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12518 written into. Return a pointer to the new start of the note buffer, to
12519 replace BUF which may no longer be valid. */
12522 elfcore_write_riscv_csr (bfd
*abfd
,
12528 const char *note_name
= "GDB";
12529 return elfcore_write_note (abfd
, buf
, bufsiz
,
12530 note_name
, NT_RISCV_CSR
, csrs
, size
);
12533 /* Write the target description (a string) pointed to by TDESC, length
12534 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12535 note is being written into. Return a pointer to the new start of the
12536 note buffer, to replace BUF which may no longer be valid. */
12539 elfcore_write_gdb_tdesc (bfd
*abfd
,
12545 const char *note_name
= "GDB";
12546 return elfcore_write_note (abfd
, buf
, bufsiz
,
12547 note_name
, NT_GDB_TDESC
, tdesc
, size
);
12551 elfcore_write_register_note (bfd
*abfd
,
12554 const char *section
,
12558 if (strcmp (section
, ".reg2") == 0)
12559 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
12560 if (strcmp (section
, ".reg-xfp") == 0)
12561 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
12562 if (strcmp (section
, ".reg-xstate") == 0)
12563 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
12564 if (strcmp (section
, ".reg-x86-segbases") == 0)
12565 return elfcore_write_x86_segbases (abfd
, buf
, bufsiz
, data
, size
);
12566 if (strcmp (section
, ".reg-ppc-vmx") == 0)
12567 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
12568 if (strcmp (section
, ".reg-ppc-vsx") == 0)
12569 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
12570 if (strcmp (section
, ".reg-ppc-tar") == 0)
12571 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
12572 if (strcmp (section
, ".reg-ppc-ppr") == 0)
12573 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
12574 if (strcmp (section
, ".reg-ppc-dscr") == 0)
12575 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
12576 if (strcmp (section
, ".reg-ppc-ebb") == 0)
12577 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
12578 if (strcmp (section
, ".reg-ppc-pmu") == 0)
12579 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
12580 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
12581 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
12582 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
12583 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
12584 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
12585 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
12586 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
12587 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
12588 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
12589 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
12590 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
12591 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
12592 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
12593 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
12594 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
12595 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
12596 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
12597 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
12598 if (strcmp (section
, ".reg-s390-timer") == 0)
12599 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
12600 if (strcmp (section
, ".reg-s390-todcmp") == 0)
12601 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
12602 if (strcmp (section
, ".reg-s390-todpreg") == 0)
12603 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
12604 if (strcmp (section
, ".reg-s390-ctrs") == 0)
12605 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
12606 if (strcmp (section
, ".reg-s390-prefix") == 0)
12607 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
12608 if (strcmp (section
, ".reg-s390-last-break") == 0)
12609 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
12610 if (strcmp (section
, ".reg-s390-system-call") == 0)
12611 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
12612 if (strcmp (section
, ".reg-s390-tdb") == 0)
12613 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
12614 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
12615 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
12616 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
12617 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
12618 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
12619 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
12620 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
12621 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
12622 if (strcmp (section
, ".reg-arm-vfp") == 0)
12623 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
12624 if (strcmp (section
, ".reg-aarch-tls") == 0)
12625 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
12626 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
12627 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
12628 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
12629 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
12630 if (strcmp (section
, ".reg-aarch-sve") == 0)
12631 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
12632 if (strcmp (section
, ".reg-aarch-pauth") == 0)
12633 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
12634 if (strcmp (section
, ".reg-aarch-mte") == 0)
12635 return elfcore_write_aarch_mte (abfd
, buf
, bufsiz
, data
, size
);
12636 if (strcmp (section
, ".reg-arc-v2") == 0)
12637 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
12638 if (strcmp (section
, ".gdb-tdesc") == 0)
12639 return elfcore_write_gdb_tdesc (abfd
, buf
, bufsiz
, data
, size
);
12640 if (strcmp (section
, ".reg-riscv-csr") == 0)
12641 return elfcore_write_riscv_csr (abfd
, buf
, bufsiz
, data
, size
);
12642 if (strcmp (section
, ".reg-loongarch-cpucfg") == 0)
12643 return elfcore_write_loongarch_cpucfg (abfd
, buf
, bufsiz
, data
, size
);
12644 if (strcmp (section
, ".reg-loongarch-lbt") == 0)
12645 return elfcore_write_loongarch_lbt (abfd
, buf
, bufsiz
, data
, size
);
12646 if (strcmp (section
, ".reg-loongarch-lsx") == 0)
12647 return elfcore_write_loongarch_lsx (abfd
, buf
, bufsiz
, data
, size
);
12648 if (strcmp (section
, ".reg-loongarch-lasx") == 0)
12649 return elfcore_write_loongarch_lasx (abfd
, buf
, bufsiz
, data
, size
);
12654 elfcore_write_file_note (bfd
*obfd
, char *note_data
, int *note_size
,
12655 const void *buf
, int bufsiz
)
12657 return elfcore_write_note (obfd
, note_data
, note_size
,
12658 "CORE", NT_FILE
, buf
, bufsiz
);
12662 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
12667 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12668 gABI specifies that PT_NOTE alignment should be aligned to 4
12669 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12670 align is less than 4, we use 4 byte alignment. */
12673 if (align
!= 4 && align
!= 8)
12677 while (p
< buf
+ size
)
12679 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
12680 Elf_Internal_Note in
;
12682 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
12685 in
.type
= H_GET_32 (abfd
, xnp
->type
);
12687 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
12688 in
.namedata
= xnp
->name
;
12689 if (in
.namesz
> buf
- in
.namedata
+ size
)
12692 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
12693 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
12694 in
.descpos
= offset
+ (in
.descdata
- buf
);
12696 && (in
.descdata
>= buf
+ size
12697 || in
.descsz
> buf
- in
.descdata
+ size
))
12700 switch (bfd_get_format (abfd
))
12707 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12710 const char * string
;
12712 bool (*func
) (bfd
*, Elf_Internal_Note
*);
12716 GROKER_ELEMENT ("", elfcore_grok_note
),
12717 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
12718 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
12719 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note
),
12720 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
12721 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
12722 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
),
12723 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note
)
12725 #undef GROKER_ELEMENT
12728 for (i
= ARRAY_SIZE (grokers
); i
--;)
12730 if (in
.namesz
>= grokers
[i
].len
12731 && strncmp (in
.namedata
, grokers
[i
].string
,
12732 grokers
[i
].len
) == 0)
12734 if (! grokers
[i
].func (abfd
, & in
))
12743 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
12745 if (! elfobj_grok_gnu_note (abfd
, &in
))
12748 else if (in
.namesz
== sizeof "stapsdt"
12749 && strcmp (in
.namedata
, "stapsdt") == 0)
12751 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
12757 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
12764 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
12769 if (size
== 0 || (size
+ 1) == 0)
12772 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
12775 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
12779 /* PR 17512: file: ec08f814
12780 0-termintate the buffer so that string searches will not overflow. */
12783 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
12793 /* Providing external access to the ELF program header table. */
12795 /* Return an upper bound on the number of bytes required to store a
12796 copy of ABFD's program header table entries. Return -1 if an error
12797 occurs; bfd_get_error will return an appropriate code. */
12800 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12802 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12804 bfd_set_error (bfd_error_wrong_format
);
12808 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12811 /* Copy ABFD's program header table entries to *PHDRS. The entries
12812 will be stored as an array of Elf_Internal_Phdr structures, as
12813 defined in include/elf/internal.h. To find out how large the
12814 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12816 Return the number of program header table entries read, or -1 if an
12817 error occurs; bfd_get_error will return an appropriate code. */
12820 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12824 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12826 bfd_set_error (bfd_error_wrong_format
);
12830 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12831 if (num_phdrs
!= 0)
12832 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12833 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12838 enum elf_reloc_type_class
12839 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12840 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12841 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12843 return reloc_class_normal
;
12846 /* For RELA architectures, return the relocation value for a
12847 relocation against a local symbol. */
12850 _bfd_elf_rela_local_sym (bfd
*abfd
,
12851 Elf_Internal_Sym
*sym
,
12853 Elf_Internal_Rela
*rel
)
12855 asection
*sec
= *psec
;
12856 bfd_vma relocation
;
12858 relocation
= (sec
->output_section
->vma
12859 + sec
->output_offset
12861 if ((sec
->flags
& SEC_MERGE
)
12862 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12863 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12866 _bfd_merged_section_offset (abfd
, psec
,
12867 elf_section_data (sec
)->sec_info
,
12868 sym
->st_value
+ rel
->r_addend
);
12871 /* If we have changed the section, and our original section is
12872 marked with SEC_EXCLUDE, it means that the original
12873 SEC_MERGE section has been completely subsumed in some
12874 other SEC_MERGE section. In this case, we need to leave
12875 some info around for --emit-relocs. */
12876 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12877 sec
->kept_section
= *psec
;
12880 rel
->r_addend
-= relocation
;
12881 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12887 _bfd_elf_rel_local_sym (bfd
*abfd
,
12888 Elf_Internal_Sym
*sym
,
12892 asection
*sec
= *psec
;
12894 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12895 return sym
->st_value
+ addend
;
12897 return _bfd_merged_section_offset (abfd
, psec
,
12898 elf_section_data (sec
)->sec_info
,
12899 sym
->st_value
+ addend
);
12902 /* Adjust an address within a section. Given OFFSET within SEC, return
12903 the new offset within the section, based upon changes made to the
12904 section. Returns -1 if the offset is now invalid.
12905 The offset (in abnd out) is in target sized bytes, however big a
12909 _bfd_elf_section_offset (bfd
*abfd
,
12910 struct bfd_link_info
*info
,
12914 switch (sec
->sec_info_type
)
12916 case SEC_INFO_TYPE_STABS
:
12917 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12919 case SEC_INFO_TYPE_EH_FRAME
:
12920 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12923 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12925 /* Reverse the offset. */
12926 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12927 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12929 /* address_size and sec->size are in octets. Convert
12930 to bytes before subtracting the original offset. */
12931 offset
= ((sec
->size
- address_size
)
12932 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12938 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12939 reconstruct an ELF file by reading the segments out of remote memory
12940 based on the ELF file header at EHDR_VMA and the ELF program headers it
12941 points to. If not null, *LOADBASEP is filled in with the difference
12942 between the VMAs from which the segments were read, and the VMAs the
12943 file headers (and hence BFD's idea of each section's VMA) put them at.
12945 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12946 remote memory at target address VMA into the local buffer at MYADDR; it
12947 should return zero on success or an `errno' code on failure. TEMPL must
12948 be a BFD for an ELF target with the word size and byte order found in
12949 the remote memory. */
12952 bfd_elf_bfd_from_remote_memory
12955 bfd_size_type size
,
12956 bfd_vma
*loadbasep
,
12957 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12959 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12960 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12964 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12965 long symcount ATTRIBUTE_UNUSED
,
12966 asymbol
**syms ATTRIBUTE_UNUSED
,
12971 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12974 const char *relplt_name
;
12975 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
12979 Elf_Internal_Shdr
*hdr
;
12985 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12988 if (dynsymcount
<= 0)
12991 if (!bed
->plt_sym_val
)
12994 relplt_name
= bed
->relplt_name
;
12995 if (relplt_name
== NULL
)
12996 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12997 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12998 if (relplt
== NULL
)
13001 hdr
= &elf_section_data (relplt
)->this_hdr
;
13002 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
13003 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
13006 plt
= bfd_get_section_by_name (abfd
, ".plt");
13010 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
13011 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, true))
13014 count
= relplt
->size
/ hdr
->sh_entsize
;
13015 size
= count
* sizeof (asymbol
);
13016 p
= relplt
->relocation
;
13017 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13019 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
13020 if (p
->addend
!= 0)
13023 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
13025 size
+= sizeof ("+0x") - 1 + 8;
13030 s
= *ret
= (asymbol
*) bfd_malloc (size
);
13034 names
= (char *) (s
+ count
);
13035 p
= relplt
->relocation
;
13037 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13042 addr
= bed
->plt_sym_val (i
, plt
, p
);
13043 if (addr
== (bfd_vma
) -1)
13046 *s
= **p
->sym_ptr_ptr
;
13047 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13048 we are defining a symbol, ensure one of them is set. */
13049 if ((s
->flags
& BSF_LOCAL
) == 0)
13050 s
->flags
|= BSF_GLOBAL
;
13051 s
->flags
|= BSF_SYNTHETIC
;
13053 s
->value
= addr
- plt
->vma
;
13056 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
13057 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
13059 if (p
->addend
!= 0)
13063 memcpy (names
, "+0x", sizeof ("+0x") - 1);
13064 names
+= sizeof ("+0x") - 1;
13065 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
13066 for (a
= buf
; *a
== '0'; ++a
)
13069 memcpy (names
, a
, len
);
13072 memcpy (names
, "@plt", sizeof ("@plt"));
13073 names
+= sizeof ("@plt");
13080 /* It is only used by x86-64 so far.
13081 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13082 but current usage would allow all of _bfd_std_section to be zero. */
13083 static const asymbol lcomm_sym
13084 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
13085 asection _bfd_elf_large_com_section
13086 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
13087 "LARGE_COMMON", 0, SEC_IS_COMMON
);
13090 _bfd_elf_final_write_processing (bfd
*abfd
)
13092 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
13094 i_ehdrp
= elf_elfheader (abfd
);
13096 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13097 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
13099 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13100 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13101 or STB_GNU_UNIQUE binding. */
13102 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
13104 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13105 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
13106 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
13107 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
13109 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
13110 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13111 "and FreeBSD targets"));
13112 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
13113 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13114 "only by GNU and FreeBSD targets"));
13115 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
13116 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13117 "only by GNU and FreeBSD targets"));
13118 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
13119 _bfd_error_handler (_("GNU_RETAIN section is supported "
13120 "only by GNU and FreeBSD targets"));
13121 bfd_set_error (bfd_error_sorry
);
13129 /* Return TRUE for ELF symbol types that represent functions.
13130 This is the default version of this function, which is sufficient for
13131 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13134 _bfd_elf_is_function_type (unsigned int type
)
13136 return (type
== STT_FUNC
13137 || type
== STT_GNU_IFUNC
);
13140 /* If the ELF symbol SYM might be a function in SEC, return the
13141 function size and set *CODE_OFF to the function's entry point,
13142 otherwise return zero. */
13145 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
13148 bfd_size_type size
;
13149 elf_symbol_type
* elf_sym
= (elf_symbol_type
*) sym
;
13151 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
13152 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
13153 || sym
->section
!= sec
)
13156 size
= (sym
->flags
& BSF_SYNTHETIC
) ? 0 : elf_sym
->internal_elf_sym
.st_size
;
13158 /* In theory we should check that the symbol's type satisfies
13159 _bfd_elf_is_function_type(), but there are some function-like
13160 symbols which would fail this test. (eg _start). Instead
13161 we check for hidden, local, notype symbols with zero size.
13162 This type of symbol is generated by the annobin plugin for gcc
13163 and clang, and should not be considered to be a function symbol. */
13165 && ((sym
->flags
& (BSF_SYNTHETIC
| BSF_LOCAL
)) == BSF_LOCAL
)
13166 && ELF_ST_TYPE (elf_sym
->internal_elf_sym
.st_info
) == STT_NOTYPE
13167 && ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
) == STV_HIDDEN
)
13170 *code_off
= sym
->value
;
13171 /* Do not return 0 for the function's size. */
13172 return size
? size
: 1;
13175 /* Set to non-zero to enable some debug messages. */
13176 #define DEBUG_SECONDARY_RELOCS 0
13178 /* An internal-to-the-bfd-library only section type
13179 used to indicate a cached secondary reloc section. */
13180 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13182 /* Create a BFD section to hold a secondary reloc section. */
13185 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
13186 Elf_Internal_Shdr
*hdr
,
13188 unsigned int shindex
)
13190 /* We only support RELA secondary relocs. */
13191 if (hdr
->sh_type
!= SHT_RELA
)
13194 #if DEBUG_SECONDARY_RELOCS
13195 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
13197 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
13198 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
13201 /* Read in any secondary relocs associated with SEC. */
13204 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
13206 asymbol
** symbols
,
13209 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
13211 bool result
= true;
13212 bfd_vma (*r_sym
) (bfd_vma
);
13214 #if BFD_DEFAULT_TARGET_SIZE > 32
13215 if (bfd_arch_bits_per_address (abfd
) != 32)
13216 r_sym
= elf64_r_sym
;
13219 r_sym
= elf32_r_sym
;
13221 if (!elf_section_data (sec
)->has_secondary_relocs
)
13224 /* Discover if there are any secondary reloc sections
13225 associated with SEC. */
13226 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
13228 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
13230 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
13231 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
13232 && (hdr
->sh_entsize
== ebd
->s
->sizeof_rel
13233 || hdr
->sh_entsize
== ebd
->s
->sizeof_rela
))
13235 bfd_byte
* native_relocs
;
13236 bfd_byte
* native_reloc
;
13237 arelent
* internal_relocs
;
13238 arelent
* internal_reloc
;
13240 unsigned int entsize
;
13241 unsigned int symcount
;
13242 unsigned int reloc_count
;
13245 if (ebd
->elf_info_to_howto
== NULL
)
13248 #if DEBUG_SECONDARY_RELOCS
13249 fprintf (stderr
, "read secondary relocs for %s from %s\n",
13250 sec
->name
, relsec
->name
);
13252 entsize
= hdr
->sh_entsize
;
13254 native_relocs
= bfd_malloc (hdr
->sh_size
);
13255 if (native_relocs
== NULL
)
13261 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
13262 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
13264 free (native_relocs
);
13265 bfd_set_error (bfd_error_file_too_big
);
13270 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
13271 if (internal_relocs
== NULL
)
13273 free (native_relocs
);
13278 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
13279 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
13282 free (native_relocs
);
13283 /* The internal_relocs will be freed when
13284 the memory for the bfd is released. */
13290 symcount
= bfd_get_dynamic_symcount (abfd
);
13292 symcount
= bfd_get_symcount (abfd
);
13294 for (i
= 0, internal_reloc
= internal_relocs
,
13295 native_reloc
= native_relocs
;
13297 i
++, internal_reloc
++, native_reloc
+= entsize
)
13300 Elf_Internal_Rela rela
;
13302 if (entsize
== ebd
->s
->sizeof_rel
)
13303 ebd
->s
->swap_reloc_in (abfd
, native_reloc
, & rela
);
13304 else /* entsize == ebd->s->sizeof_rela */
13305 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
13307 /* The address of an ELF reloc is section relative for an object
13308 file, and absolute for an executable file or shared library.
13309 The address of a normal BFD reloc is always section relative,
13310 and the address of a dynamic reloc is absolute.. */
13311 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
13312 internal_reloc
->address
= rela
.r_offset
;
13314 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
13316 if (r_sym (rela
.r_info
) == STN_UNDEF
)
13318 /* FIXME: This and the error case below mean that we
13319 have a symbol on relocs that is not elf_symbol_type. */
13320 internal_reloc
->sym_ptr_ptr
=
13321 bfd_abs_section_ptr
->symbol_ptr_ptr
;
13323 else if (r_sym (rela
.r_info
) > symcount
)
13326 /* xgettext:c-format */
13327 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
13328 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
13329 bfd_set_error (bfd_error_bad_value
);
13330 internal_reloc
->sym_ptr_ptr
=
13331 bfd_abs_section_ptr
->symbol_ptr_ptr
;
13338 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
13339 internal_reloc
->sym_ptr_ptr
= ps
;
13340 /* Make sure that this symbol is not removed by strip. */
13341 (*ps
)->flags
|= BSF_KEEP
;
13344 internal_reloc
->addend
= rela
.r_addend
;
13346 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
13347 if (! res
|| internal_reloc
->howto
== NULL
)
13349 #if DEBUG_SECONDARY_RELOCS
13351 "there is no howto associated with reloc %lx\n",
13358 free (native_relocs
);
13359 /* Store the internal relocs. */
13360 elf_section_data (relsec
)->sec_info
= internal_relocs
;
13367 /* Set the ELF section header fields of an output secondary reloc section. */
13370 _bfd_elf_copy_special_section_fields (const bfd
*ibfd ATTRIBUTE_UNUSED
,
13371 bfd
*obfd ATTRIBUTE_UNUSED
,
13372 const Elf_Internal_Shdr
*isection
,
13373 Elf_Internal_Shdr
*osection
)
13377 struct bfd_elf_section_data
* esd
;
13379 if (isection
== NULL
)
13382 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
13385 isec
= isection
->bfd_section
;
13389 osec
= osection
->bfd_section
;
13393 esd
= elf_section_data (osec
);
13394 BFD_ASSERT (esd
->sec_info
== NULL
);
13395 esd
->sec_info
= elf_section_data (isec
)->sec_info
;
13396 osection
->sh_type
= SHT_RELA
;
13397 osection
->sh_link
= elf_onesymtab (obfd
);
13398 if (osection
->sh_link
== 0)
13400 /* There is no symbol table - we are hosed... */
13402 /* xgettext:c-format */
13403 (_("%pB(%pA): link section cannot be set"
13404 " because the output file does not have a symbol table"),
13406 bfd_set_error (bfd_error_bad_value
);
13410 /* Find the output section that corresponds to the isection's
13412 if (isection
->sh_info
== 0
13413 || isection
->sh_info
>= elf_numsections (ibfd
))
13416 /* xgettext:c-format */
13417 (_("%pB(%pA): info section index is invalid"),
13419 bfd_set_error (bfd_error_bad_value
);
13423 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
13425 if (isection
== NULL
13426 || isection
->bfd_section
== NULL
13427 || isection
->bfd_section
->output_section
== NULL
)
13430 /* xgettext:c-format */
13431 (_("%pB(%pA): info section index cannot be set"
13432 " because the section is not in the output"),
13434 bfd_set_error (bfd_error_bad_value
);
13438 esd
= elf_section_data (isection
->bfd_section
->output_section
);
13439 BFD_ASSERT (esd
!= NULL
);
13440 osection
->sh_info
= esd
->this_idx
;
13441 esd
->has_secondary_relocs
= true;
13442 #if DEBUG_SECONDARY_RELOCS
13443 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
13444 osec
->name
, osection
->sh_link
, osection
->sh_info
);
13445 fprintf (stderr
, "mark section %s as having secondary relocs\n",
13446 bfd_section_name (isection
->bfd_section
->output_section
));
13452 /* Write out a secondary reloc section.
13454 FIXME: Currently this function can result in a serious performance penalty
13455 for files with secondary relocs and lots of sections. The proper way to
13456 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13457 relocs together and then to have this function just walk that chain. */
13460 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
13462 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
13463 bfd_vma addr_offset
;
13465 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
13466 bool result
= true;
13471 #if BFD_DEFAULT_TARGET_SIZE > 32
13472 if (bfd_arch_bits_per_address (abfd
) != 32)
13473 r_info
= elf64_r_info
;
13476 r_info
= elf32_r_info
;
13478 /* The address of an ELF reloc is section relative for an object
13479 file, and absolute for an executable file or shared library.
13480 The address of a BFD reloc is always section relative. */
13482 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
13483 addr_offset
= sec
->vma
;
13485 /* Discover if there are any secondary reloc sections
13486 associated with SEC. */
13487 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
13489 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
13490 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
13492 if (hdr
->sh_type
== SHT_RELA
13493 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
13495 asymbol
* last_sym
;
13497 size_t reloc_count
;
13499 bfd_size_type entsize
;
13500 arelent
* src_irel
;
13501 bfd_byte
* dst_rela
;
13503 if (hdr
->contents
!= NULL
)
13506 /* xgettext:c-format */
13507 (_("%pB(%pA): error: secondary reloc section processed twice"),
13509 bfd_set_error (bfd_error_bad_value
);
13514 entsize
= hdr
->sh_entsize
;
13518 /* xgettext:c-format */
13519 (_("%pB(%pA): error: secondary reloc section"
13520 " has zero sized entries"),
13522 bfd_set_error (bfd_error_bad_value
);
13526 else if (entsize
!= ebd
->s
->sizeof_rel
13527 && entsize
!= ebd
->s
->sizeof_rela
)
13530 /* xgettext:c-format */
13531 (_("%pB(%pA): error: secondary reloc section"
13532 " has non-standard sized entries"),
13534 bfd_set_error (bfd_error_bad_value
);
13539 reloc_count
= hdr
->sh_size
/ entsize
;
13540 hdr
->sh_size
= entsize
* reloc_count
;
13541 if (reloc_count
== 0)
13544 /* xgettext:c-format */
13545 (_("%pB(%pA): error: secondary reloc section is empty!"),
13547 bfd_set_error (bfd_error_bad_value
);
13552 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
13553 if (hdr
->contents
== NULL
)
13556 #if DEBUG_SECONDARY_RELOCS
13557 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
13558 reloc_count
, sec
->name
, relsec
->name
);
13562 dst_rela
= hdr
->contents
;
13563 src_irel
= (arelent
*) esd
->sec_info
;
13564 if (src_irel
== NULL
)
13567 /* xgettext:c-format */
13568 (_("%pB(%pA): error: internal relocs missing"
13569 " for secondary reloc section"),
13571 bfd_set_error (bfd_error_bad_value
);
13576 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= entsize
)
13578 Elf_Internal_Rela src_rela
;
13583 ptr
= src_irel
+ idx
;
13587 /* xgettext:c-format */
13588 (_("%pB(%pA): error: reloc table entry %zu is empty"),
13589 abfd
, relsec
, idx
);
13590 bfd_set_error (bfd_error_bad_value
);
13595 if (ptr
->sym_ptr_ptr
== NULL
)
13597 /* FIXME: Is this an error ? */
13602 sym
= *ptr
->sym_ptr_ptr
;
13604 if (sym
== last_sym
)
13608 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
13612 /* xgettext:c-format */
13613 (_("%pB(%pA): error: secondary reloc %zu"
13614 " references a missing symbol"),
13615 abfd
, relsec
, idx
);
13616 bfd_set_error (bfd_error_bad_value
);
13625 if (sym
->the_bfd
!= NULL
13626 && sym
->the_bfd
->xvec
!= abfd
->xvec
13627 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
13630 /* xgettext:c-format */
13631 (_("%pB(%pA): error: secondary reloc %zu"
13632 " references a deleted symbol"),
13633 abfd
, relsec
, idx
);
13634 bfd_set_error (bfd_error_bad_value
);
13640 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
13641 if (ptr
->howto
== NULL
)
13644 /* xgettext:c-format */
13645 (_("%pB(%pA): error: secondary reloc %zu"
13646 " is of an unknown type"),
13647 abfd
, relsec
, idx
);
13648 bfd_set_error (bfd_error_bad_value
);
13650 src_rela
.r_info
= r_info (0, 0);
13653 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
13654 src_rela
.r_addend
= ptr
->addend
;
13656 if (entsize
== ebd
->s
->sizeof_rel
)
13657 ebd
->s
->swap_reloc_out (abfd
, &src_rela
, dst_rela
);
13658 else /* entsize == ebd->s->sizeof_rela */
13659 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);