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_*,
1203 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1204 if ((newsect
->flags
& SEC_DEBUGGING
) != 0
1205 && (newsect
->flags
& SEC_HAS_CONTENTS
) != 0
1206 && (newsect
->flags
& SEC_ELF_OCTETS
) != 0)
1208 enum { nothing
, compress
, decompress
} action
= nothing
;
1209 int compression_header_size
;
1210 bfd_size_type uncompressed_size
;
1211 unsigned int uncompressed_align_power
;
1212 unsigned int ch_type
= 0;
1214 = bfd_is_section_compressed_info (abfd
, newsect
,
1215 &compression_header_size
,
1217 &uncompressed_align_power
,
1220 /* Should we decompress? */
1221 if ((abfd
->flags
& BFD_DECOMPRESS
) != 0 && compressed
)
1222 action
= decompress
;
1224 /* Should we compress? Or convert to a different compression? */
1225 else if ((abfd
->flags
& BFD_COMPRESS
) != 0
1226 && newsect
->size
!= 0
1227 && compression_header_size
>= 0
1228 && uncompressed_size
> 0)
1234 unsigned int new_ch_type
= 0;
1235 if ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0)
1236 new_ch_type
= ((abfd
->flags
& BFD_COMPRESS_ZSTD
) != 0
1237 ? ELFCOMPRESS_ZSTD
: ELFCOMPRESS_ZLIB
);
1238 if (new_ch_type
!= ch_type
)
1243 if (action
== compress
)
1245 if (!bfd_init_section_compress_status (abfd
, newsect
))
1248 /* xgettext:c-format */
1249 (_("%pB: unable to compress section %s"), abfd
, name
);
1253 else if (action
== decompress
)
1255 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1258 /* xgettext:c-format */
1259 (_("%pB: unable to decompress section %s"), abfd
, name
);
1263 if (newsect
->compress_status
== DECOMPRESS_SECTION_ZSTD
)
1266 /* xgettext:c-format */
1267 (_ ("%pB: section %s is compressed with zstd, but BFD "
1268 "is not built with zstd support"),
1270 newsect
->compress_status
= COMPRESS_SECTION_NONE
;
1276 if (action
!= nothing
)
1278 if (abfd
->is_linker_input
)
1281 && (action
== decompress
1282 || (action
== compress
1283 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1285 /* Convert section name from .zdebug_* to .debug_* so
1286 that linker will consider this section as a debug
1288 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1289 if (new_name
== NULL
)
1291 bfd_rename_section (newsect
, new_name
);
1295 /* For objdump, don't rename the section. For objcopy, delay
1296 section rename to elf_fake_sections. */
1297 newsect
->flags
|= SEC_ELF_RENAME
;
1301 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1303 if (startswith (name
, ".gnu.lto_.lto."))
1305 struct lto_section lsection
;
1306 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1307 sizeof (struct lto_section
)))
1308 abfd
->lto_slim_object
= lsection
.slim_object
;
1314 const char *const bfd_elf_section_type_names
[] =
1316 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1317 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1318 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1321 /* ELF relocs are against symbols. If we are producing relocatable
1322 output, and the reloc is against an external symbol, and nothing
1323 has given us any additional addend, the resulting reloc will also
1324 be against the same symbol. In such a case, we don't want to
1325 change anything about the way the reloc is handled, since it will
1326 all be done at final link time. Rather than put special case code
1327 into bfd_perform_relocation, all the reloc types use this howto
1328 function, or should call this function for relocatable output. */
1330 bfd_reloc_status_type
1331 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1332 arelent
*reloc_entry
,
1334 void *data ATTRIBUTE_UNUSED
,
1335 asection
*input_section
,
1337 char **error_message ATTRIBUTE_UNUSED
)
1339 if (output_bfd
!= NULL
1340 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1341 && (! reloc_entry
->howto
->partial_inplace
1342 || reloc_entry
->addend
== 0))
1344 reloc_entry
->address
+= input_section
->output_offset
;
1345 return bfd_reloc_ok
;
1348 /* In some cases the relocation should be treated as output section
1349 relative, as when linking ELF DWARF into PE COFF. Many ELF
1350 targets lack section relative relocations and instead use
1351 ordinary absolute relocations for references between DWARF
1352 sections. That is arguably a bug in those targets but it happens
1353 to work for the usual case of linking to non-loaded ELF debug
1354 sections with VMAs forced to zero. PE COFF on the other hand
1355 doesn't allow a section VMA of zero. */
1356 if (output_bfd
== NULL
1357 && !reloc_entry
->howto
->pc_relative
1358 && (symbol
->section
->flags
& SEC_DEBUGGING
) != 0
1359 && (input_section
->flags
& SEC_DEBUGGING
) != 0)
1360 reloc_entry
->addend
-= symbol
->section
->output_section
->vma
;
1362 return bfd_reloc_continue
;
1365 /* Returns TRUE if section A matches section B.
1366 Names, addresses and links may be different, but everything else
1367 should be the same. */
1370 section_match (const Elf_Internal_Shdr
* a
,
1371 const Elf_Internal_Shdr
* b
)
1373 if (a
->sh_type
!= b
->sh_type
1374 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1375 || a
->sh_addralign
!= b
->sh_addralign
1376 || a
->sh_entsize
!= b
->sh_entsize
)
1378 if (a
->sh_type
== SHT_SYMTAB
1379 || a
->sh_type
== SHT_STRTAB
)
1381 return a
->sh_size
== b
->sh_size
;
1384 /* Find a section in OBFD that has the same characteristics
1385 as IHEADER. Return the index of this section or SHN_UNDEF if
1386 none can be found. Check's section HINT first, as this is likely
1387 to be the correct section. */
1390 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1391 const unsigned int hint
)
1393 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1396 BFD_ASSERT (iheader
!= NULL
);
1398 /* See PR 20922 for a reproducer of the NULL test. */
1399 if (hint
< elf_numsections (obfd
)
1400 && oheaders
[hint
] != NULL
1401 && section_match (oheaders
[hint
], iheader
))
1404 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1406 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1408 if (oheader
== NULL
)
1410 if (section_match (oheader
, iheader
))
1411 /* FIXME: Do we care if there is a potential for
1412 multiple matches ? */
1419 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1420 Processor specific section, based upon a matching input section.
1421 Returns TRUE upon success, FALSE otherwise. */
1424 copy_special_section_fields (const bfd
*ibfd
,
1426 const Elf_Internal_Shdr
*iheader
,
1427 Elf_Internal_Shdr
*oheader
,
1428 const unsigned int secnum
)
1430 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1431 const Elf_Internal_Shdr
**iheaders
1432 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1433 bool changed
= false;
1434 unsigned int sh_link
;
1436 if (oheader
->sh_type
== SHT_NOBITS
)
1438 /* This is a feature for objcopy --only-keep-debug:
1439 When a section's type is changed to NOBITS, we preserve
1440 the sh_link and sh_info fields so that they can be
1441 matched up with the original.
1443 Note: Strictly speaking these assignments are wrong.
1444 The sh_link and sh_info fields should point to the
1445 relevent sections in the output BFD, which may not be in
1446 the same location as they were in the input BFD. But
1447 the whole point of this action is to preserve the
1448 original values of the sh_link and sh_info fields, so
1449 that they can be matched up with the section headers in
1450 the original file. So strictly speaking we may be
1451 creating an invalid ELF file, but it is only for a file
1452 that just contains debug info and only for sections
1453 without any contents. */
1454 if (oheader
->sh_link
== 0)
1455 oheader
->sh_link
= iheader
->sh_link
;
1456 if (oheader
->sh_info
== 0)
1457 oheader
->sh_info
= iheader
->sh_info
;
1461 /* Allow the target a chance to decide how these fields should be set. */
1462 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1466 /* We have an iheader which might match oheader, and which has non-zero
1467 sh_info and/or sh_link fields. Attempt to follow those links and find
1468 the section in the output bfd which corresponds to the linked section
1469 in the input bfd. */
1470 if (iheader
->sh_link
!= SHN_UNDEF
)
1472 /* See PR 20931 for a reproducer. */
1473 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1476 /* xgettext:c-format */
1477 (_("%pB: invalid sh_link field (%d) in section number %d"),
1478 ibfd
, iheader
->sh_link
, secnum
);
1482 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1483 if (sh_link
!= SHN_UNDEF
)
1485 oheader
->sh_link
= sh_link
;
1489 /* FIXME: Should we install iheader->sh_link
1490 if we could not find a match ? */
1492 /* xgettext:c-format */
1493 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1496 if (iheader
->sh_info
)
1498 /* The sh_info field can hold arbitrary information, but if the
1499 SHF_LINK_INFO flag is set then it should be interpreted as a
1501 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1503 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1505 if (sh_link
!= SHN_UNDEF
)
1506 oheader
->sh_flags
|= SHF_INFO_LINK
;
1509 /* No idea what it means - just copy it. */
1510 sh_link
= iheader
->sh_info
;
1512 if (sh_link
!= SHN_UNDEF
)
1514 oheader
->sh_info
= sh_link
;
1519 /* xgettext:c-format */
1520 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1526 /* Copy the program header and other data from one object module to
1530 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1532 const Elf_Internal_Shdr
**iheaders
1533 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1534 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1535 const struct elf_backend_data
*bed
;
1538 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1539 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1542 if (!elf_flags_init (obfd
))
1544 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1545 elf_flags_init (obfd
) = true;
1548 elf_gp (obfd
) = elf_gp (ibfd
);
1550 /* Also copy the EI_OSABI field. */
1551 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1552 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1554 /* If set, copy the EI_ABIVERSION field. */
1555 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1556 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1557 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1559 /* Copy object attributes. */
1560 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1562 if (iheaders
== NULL
|| oheaders
== NULL
)
1565 bed
= get_elf_backend_data (obfd
);
1567 /* Possibly copy other fields in the section header. */
1568 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1571 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1573 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1574 because of a special case need for generating separate debug info
1575 files. See below for more details. */
1577 || (oheader
->sh_type
!= SHT_NOBITS
1578 && oheader
->sh_type
< SHT_LOOS
))
1581 /* Ignore empty sections, and sections whose
1582 fields have already been initialised. */
1583 if (oheader
->sh_size
== 0
1584 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1587 /* Scan for the matching section in the input bfd.
1588 First we try for a direct mapping between the input and
1590 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1592 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1594 if (iheader
== NULL
)
1597 if (oheader
->bfd_section
!= NULL
1598 && iheader
->bfd_section
!= NULL
1599 && iheader
->bfd_section
->output_section
!= NULL
1600 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1602 /* We have found a connection from the input section to
1603 the output section. Attempt to copy the header fields.
1604 If this fails then do not try any further sections -
1605 there should only be a one-to-one mapping between
1606 input and output. */
1607 if (!copy_special_section_fields (ibfd
, obfd
,
1608 iheader
, oheader
, i
))
1609 j
= elf_numsections (ibfd
);
1614 if (j
< elf_numsections (ibfd
))
1617 /* That failed. So try to deduce the corresponding input section.
1618 Unfortunately we cannot compare names as the output string table
1619 is empty, so instead we check size, address and type. */
1620 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1622 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1624 if (iheader
== NULL
)
1627 /* Try matching fields in the input section's header.
1628 Since --only-keep-debug turns all non-debug sections into
1629 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1631 if ((oheader
->sh_type
== SHT_NOBITS
1632 || iheader
->sh_type
== oheader
->sh_type
)
1633 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1634 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1635 && iheader
->sh_addralign
== oheader
->sh_addralign
1636 && iheader
->sh_entsize
== oheader
->sh_entsize
1637 && iheader
->sh_size
== oheader
->sh_size
1638 && iheader
->sh_addr
== oheader
->sh_addr
1639 && (iheader
->sh_info
!= oheader
->sh_info
1640 || iheader
->sh_link
!= oheader
->sh_link
))
1642 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1647 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1649 /* Final attempt. Call the backend copy function
1650 with a NULL input section. */
1651 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1660 get_segment_type (unsigned int p_type
)
1665 case PT_NULL
: pt
= "NULL"; break;
1666 case PT_LOAD
: pt
= "LOAD"; break;
1667 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1668 case PT_INTERP
: pt
= "INTERP"; break;
1669 case PT_NOTE
: pt
= "NOTE"; break;
1670 case PT_SHLIB
: pt
= "SHLIB"; break;
1671 case PT_PHDR
: pt
= "PHDR"; break;
1672 case PT_TLS
: pt
= "TLS"; break;
1673 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1674 case PT_GNU_STACK
: pt
= "STACK"; break;
1675 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1676 case PT_GNU_SFRAME
: pt
= "SFRAME"; break;
1677 default: pt
= NULL
; break;
1682 /* Print out the program headers. */
1685 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1687 FILE *f
= (FILE *) farg
;
1688 Elf_Internal_Phdr
*p
;
1690 bfd_byte
*dynbuf
= NULL
;
1692 p
= elf_tdata (abfd
)->phdr
;
1697 fprintf (f
, _("\nProgram Header:\n"));
1698 c
= elf_elfheader (abfd
)->e_phnum
;
1699 for (i
= 0; i
< c
; i
++, p
++)
1701 const char *pt
= get_segment_type (p
->p_type
);
1706 sprintf (buf
, "0x%lx", p
->p_type
);
1709 fprintf (f
, "%8s off 0x", pt
);
1710 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1711 fprintf (f
, " vaddr 0x");
1712 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1713 fprintf (f
, " paddr 0x");
1714 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1715 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1716 fprintf (f
, " filesz 0x");
1717 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1718 fprintf (f
, " memsz 0x");
1719 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1720 fprintf (f
, " flags %c%c%c",
1721 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1722 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1723 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1724 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1725 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1730 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1733 unsigned int elfsec
;
1734 unsigned long shlink
;
1735 bfd_byte
*extdyn
, *extdynend
;
1737 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1739 fprintf (f
, _("\nDynamic Section:\n"));
1741 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1744 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1745 if (elfsec
== SHN_BAD
)
1747 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1749 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1750 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1752 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
1753 (size_t) (extdynend
- extdyn
) >= extdynsize
;
1754 extdyn
+= extdynsize
)
1756 Elf_Internal_Dyn dyn
;
1757 const char *name
= "";
1760 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1762 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1764 if (dyn
.d_tag
== DT_NULL
)
1771 if (bed
->elf_backend_get_target_dtag
)
1772 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1774 if (!strcmp (name
, ""))
1776 sprintf (ab
, "%#" PRIx64
, (uint64_t) dyn
.d_tag
);
1781 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1782 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1783 case DT_PLTGOT
: name
= "PLTGOT"; break;
1784 case DT_HASH
: name
= "HASH"; break;
1785 case DT_STRTAB
: name
= "STRTAB"; break;
1786 case DT_SYMTAB
: name
= "SYMTAB"; break;
1787 case DT_RELA
: name
= "RELA"; break;
1788 case DT_RELASZ
: name
= "RELASZ"; break;
1789 case DT_RELAENT
: name
= "RELAENT"; break;
1790 case DT_STRSZ
: name
= "STRSZ"; break;
1791 case DT_SYMENT
: name
= "SYMENT"; break;
1792 case DT_INIT
: name
= "INIT"; break;
1793 case DT_FINI
: name
= "FINI"; break;
1794 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1795 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1796 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1797 case DT_REL
: name
= "REL"; break;
1798 case DT_RELSZ
: name
= "RELSZ"; break;
1799 case DT_RELENT
: name
= "RELENT"; break;
1800 case DT_RELR
: name
= "RELR"; break;
1801 case DT_RELRSZ
: name
= "RELRSZ"; break;
1802 case DT_RELRENT
: name
= "RELRENT"; break;
1803 case DT_PLTREL
: name
= "PLTREL"; break;
1804 case DT_DEBUG
: name
= "DEBUG"; break;
1805 case DT_TEXTREL
: name
= "TEXTREL"; break;
1806 case DT_JMPREL
: name
= "JMPREL"; break;
1807 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1808 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1809 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1810 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1811 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1812 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1813 case DT_FLAGS
: name
= "FLAGS"; break;
1814 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1815 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1816 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1817 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1818 case DT_MOVEENT
: name
= "MOVEENT"; break;
1819 case DT_MOVESZ
: name
= "MOVESZ"; break;
1820 case DT_FEATURE
: name
= "FEATURE"; break;
1821 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1822 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1823 case DT_SYMINENT
: name
= "SYMINENT"; break;
1824 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1825 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1826 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1827 case DT_PLTPAD
: name
= "PLTPAD"; break;
1828 case DT_MOVETAB
: name
= "MOVETAB"; break;
1829 case DT_SYMINFO
: name
= "SYMINFO"; break;
1830 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1831 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1832 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1833 case DT_VERSYM
: name
= "VERSYM"; break;
1834 case DT_VERDEF
: name
= "VERDEF"; break;
1835 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1836 case DT_VERNEED
: name
= "VERNEED"; break;
1837 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1838 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1839 case DT_USED
: name
= "USED"; break;
1840 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1841 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1844 fprintf (f
, " %-20s ", name
);
1848 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1853 unsigned int tagv
= dyn
.d_un
.d_val
;
1855 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1858 fprintf (f
, "%s", string
);
1867 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1868 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1870 if (! _bfd_elf_slurp_version_tables (abfd
, false))
1874 if (elf_dynverdef (abfd
) != 0)
1876 Elf_Internal_Verdef
*t
;
1878 fprintf (f
, _("\nVersion definitions:\n"));
1879 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1881 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1882 t
->vd_flags
, t
->vd_hash
,
1883 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1884 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1886 Elf_Internal_Verdaux
*a
;
1889 for (a
= t
->vd_auxptr
->vda_nextptr
;
1893 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1899 if (elf_dynverref (abfd
) != 0)
1901 Elf_Internal_Verneed
*t
;
1903 fprintf (f
, _("\nVersion References:\n"));
1904 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1906 Elf_Internal_Vernaux
*a
;
1908 fprintf (f
, _(" required from %s:\n"),
1909 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1910 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1911 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1912 a
->vna_flags
, a
->vna_other
,
1913 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1924 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1925 and return symbol version for symbol version itself. */
1928 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1932 const char *version_string
= NULL
;
1933 if (elf_dynversym (abfd
) != 0
1934 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1936 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1938 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1939 vernum
&= VERSYM_VERSION
;
1942 version_string
= "";
1943 else if (vernum
== 1
1944 && (vernum
> elf_tdata (abfd
)->cverdefs
1945 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1947 version_string
= base_p
? "Base" : "";
1948 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1950 const char *nodename
1951 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1952 version_string
= "";
1955 || symbol
->name
== NULL
1956 || strcmp (symbol
->name
, nodename
) != 0)
1957 version_string
= nodename
;
1961 Elf_Internal_Verneed
*t
;
1963 version_string
= _("<corrupt>");
1964 for (t
= elf_tdata (abfd
)->verref
;
1968 Elf_Internal_Vernaux
*a
;
1970 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1972 if (a
->vna_other
== vernum
)
1975 version_string
= a
->vna_nodename
;
1982 return version_string
;
1985 /* Display ELF-specific fields of a symbol. */
1988 bfd_elf_print_symbol (bfd
*abfd
,
1991 bfd_print_symbol_type how
)
1993 FILE *file
= (FILE *) filep
;
1996 case bfd_print_symbol_name
:
1997 fprintf (file
, "%s", symbol
->name
);
1999 case bfd_print_symbol_more
:
2000 fprintf (file
, "elf ");
2001 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
2002 fprintf (file
, " %x", symbol
->flags
);
2004 case bfd_print_symbol_all
:
2006 const char *section_name
;
2007 const char *name
= NULL
;
2008 const struct elf_backend_data
*bed
;
2009 unsigned char st_other
;
2011 const char *version_string
;
2014 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2016 bed
= get_elf_backend_data (abfd
);
2017 if (bed
->elf_backend_print_symbol_all
)
2018 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
2022 name
= symbol
->name
;
2023 bfd_print_symbol_vandf (abfd
, file
, symbol
);
2026 fprintf (file
, " %s\t", section_name
);
2027 /* Print the "other" value for a symbol. For common symbols,
2028 we've already printed the size; now print the alignment.
2029 For other symbols, we have no specified alignment, and
2030 we've printed the address; now print the size. */
2031 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
2032 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
2034 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
2035 bfd_fprintf_vma (abfd
, file
, val
);
2037 /* If we have version information, print it. */
2038 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2045 fprintf (file
, " %-11s", version_string
);
2050 fprintf (file
, " (%s)", version_string
);
2051 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2056 /* If the st_other field is not zero, print it. */
2057 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2062 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2063 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2064 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2066 /* Some other non-defined flags are also present, so print
2068 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2071 fprintf (file
, " %s", name
);
2077 /* ELF .o/exec file reading */
2079 /* Create a new bfd section from an ELF section header. */
2082 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2084 Elf_Internal_Shdr
*hdr
;
2085 Elf_Internal_Ehdr
*ehdr
;
2086 const struct elf_backend_data
*bed
;
2090 if (shindex
>= elf_numsections (abfd
))
2093 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2094 sh_link or sh_info. Detect this here, by refusing to load a
2095 section that we are already in the process of loading. */
2096 if (elf_tdata (abfd
)->being_created
[shindex
])
2099 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2102 elf_tdata (abfd
)->being_created
[shindex
] = true;
2104 hdr
= elf_elfsections (abfd
)[shindex
];
2105 ehdr
= elf_elfheader (abfd
);
2106 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2111 bed
= get_elf_backend_data (abfd
);
2112 switch (hdr
->sh_type
)
2115 /* Inactive section. Throw it away. */
2118 case SHT_PROGBITS
: /* Normal section with contents. */
2119 case SHT_NOBITS
: /* .bss section. */
2120 case SHT_HASH
: /* .hash section. */
2121 case SHT_NOTE
: /* .note section. */
2122 case SHT_INIT_ARRAY
: /* .init_array section. */
2123 case SHT_FINI_ARRAY
: /* .fini_array section. */
2124 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2125 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2126 case SHT_GNU_HASH
: /* .gnu.hash section. */
2127 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2130 case SHT_DYNAMIC
: /* Dynamic linking information. */
2131 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2134 if (hdr
->sh_link
> elf_numsections (abfd
))
2136 /* PR 10478: Accept Solaris binaries with a sh_link field
2137 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2138 switch (bfd_get_arch (abfd
))
2141 case bfd_arch_sparc
:
2142 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff)
2143 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff))
2145 /* Otherwise fall through. */
2150 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2152 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2154 Elf_Internal_Shdr
*dynsymhdr
;
2156 /* The shared libraries distributed with hpux11 have a bogus
2157 sh_link field for the ".dynamic" section. Find the
2158 string table for the ".dynsym" section instead. */
2159 if (elf_dynsymtab (abfd
) != 0)
2161 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2162 hdr
->sh_link
= dynsymhdr
->sh_link
;
2166 unsigned int i
, num_sec
;
2168 num_sec
= elf_numsections (abfd
);
2169 for (i
= 1; i
< num_sec
; i
++)
2171 dynsymhdr
= elf_elfsections (abfd
)[i
];
2172 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2174 hdr
->sh_link
= dynsymhdr
->sh_link
;
2182 case SHT_SYMTAB
: /* A symbol table. */
2183 if (elf_onesymtab (abfd
) == shindex
)
2186 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2189 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2191 if (hdr
->sh_size
!= 0)
2193 /* Some assemblers erroneously set sh_info to one with a
2194 zero sh_size. ld sees this as a global symbol count
2195 of (unsigned) -1. Fix it here. */
2200 /* PR 18854: A binary might contain more than one symbol table.
2201 Unusual, but possible. Warn, but continue. */
2202 if (elf_onesymtab (abfd
) != 0)
2205 /* xgettext:c-format */
2206 (_("%pB: warning: multiple symbol tables detected"
2207 " - ignoring the table in section %u"),
2211 elf_onesymtab (abfd
) = shindex
;
2212 elf_symtab_hdr (abfd
) = *hdr
;
2213 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2214 abfd
->flags
|= HAS_SYMS
;
2216 /* Sometimes a shared object will map in the symbol table. If
2217 SHF_ALLOC is set, and this is a shared object, then we also
2218 treat this section as a BFD section. We can not base the
2219 decision purely on SHF_ALLOC, because that flag is sometimes
2220 set in a relocatable object file, which would confuse the
2222 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2223 && (abfd
->flags
& DYNAMIC
) != 0
2224 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2228 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2229 can't read symbols without that section loaded as well. It
2230 is most likely specified by the next section header. */
2232 elf_section_list
* entry
;
2233 unsigned int i
, num_sec
;
2235 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2236 if (entry
->hdr
.sh_link
== shindex
)
2239 num_sec
= elf_numsections (abfd
);
2240 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2242 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2244 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2245 && hdr2
->sh_link
== shindex
)
2250 for (i
= 1; i
< shindex
; i
++)
2252 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2254 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2255 && hdr2
->sh_link
== shindex
)
2260 ret
= bfd_section_from_shdr (abfd
, i
);
2261 /* else FIXME: we have failed to find the symbol table.
2262 Should we issue an error? */
2266 case SHT_DYNSYM
: /* A dynamic symbol table. */
2267 if (elf_dynsymtab (abfd
) == shindex
)
2270 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2273 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2275 if (hdr
->sh_size
!= 0)
2278 /* Some linkers erroneously set sh_info to one with a
2279 zero sh_size. ld sees this as a global symbol count
2280 of (unsigned) -1. Fix it here. */
2285 /* PR 18854: A binary might contain more than one dynamic symbol table.
2286 Unusual, but possible. Warn, but continue. */
2287 if (elf_dynsymtab (abfd
) != 0)
2290 /* xgettext:c-format */
2291 (_("%pB: warning: multiple dynamic symbol tables detected"
2292 " - ignoring the table in section %u"),
2296 elf_dynsymtab (abfd
) = shindex
;
2297 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2298 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2299 abfd
->flags
|= HAS_SYMS
;
2301 /* Besides being a symbol table, we also treat this as a regular
2302 section, so that objcopy can handle it. */
2303 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2306 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2308 elf_section_list
* entry
;
2310 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2311 if (entry
->ndx
== shindex
)
2314 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2317 entry
->ndx
= shindex
;
2319 entry
->next
= elf_symtab_shndx_list (abfd
);
2320 elf_symtab_shndx_list (abfd
) = entry
;
2321 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2325 case SHT_STRTAB
: /* A string table. */
2326 if (hdr
->bfd_section
!= NULL
)
2329 if (ehdr
->e_shstrndx
== shindex
)
2331 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2332 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2336 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2339 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2340 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2344 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2347 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2348 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2349 elf_elfsections (abfd
)[shindex
] = hdr
;
2350 /* We also treat this as a regular section, so that objcopy
2352 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2357 /* If the string table isn't one of the above, then treat it as a
2358 regular section. We need to scan all the headers to be sure,
2359 just in case this strtab section appeared before the above. */
2360 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2362 unsigned int i
, num_sec
;
2364 num_sec
= elf_numsections (abfd
);
2365 for (i
= 1; i
< num_sec
; i
++)
2367 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2368 if (hdr2
->sh_link
== shindex
)
2370 /* Prevent endless recursion on broken objects. */
2373 if (! bfd_section_from_shdr (abfd
, i
))
2375 if (elf_onesymtab (abfd
) == i
)
2377 if (elf_dynsymtab (abfd
) == i
)
2378 goto dynsymtab_strtab
;
2382 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2388 /* *These* do a lot of work -- but build no sections! */
2390 asection
*target_sect
;
2391 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2392 unsigned int num_sec
= elf_numsections (abfd
);
2393 struct bfd_elf_section_data
*esdt
;
2396 if (hdr
->sh_type
== SHT_REL
)
2397 size
= bed
->s
->sizeof_rel
;
2398 else if (hdr
->sh_type
== SHT_RELA
)
2399 size
= bed
->s
->sizeof_rela
;
2401 size
= bed
->s
->arch_size
/ 8;
2402 if (hdr
->sh_entsize
!= size
)
2405 /* Check for a bogus link to avoid crashing. */
2406 if (hdr
->sh_link
>= num_sec
)
2409 /* xgettext:c-format */
2410 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2411 abfd
, hdr
->sh_link
, name
, shindex
);
2412 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2416 /* Get the symbol table. */
2417 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2418 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2419 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2422 /* If this is an alloc section in an executable or shared
2423 library, or the reloc section does not use the main symbol
2424 table we don't treat it as a reloc section. BFD can't
2425 adequately represent such a section, so at least for now,
2426 we don't try. We just present it as a normal section. We
2427 also can't use it as a reloc section if it points to the
2428 null section, an invalid section, another reloc section, or
2429 its sh_link points to the null section. */
2430 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2431 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2432 || hdr
->sh_type
== SHT_RELR
2433 || hdr
->sh_link
== SHN_UNDEF
2434 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2435 || hdr
->sh_info
== SHN_UNDEF
2436 || hdr
->sh_info
>= num_sec
2437 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2438 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2440 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2444 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2447 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2448 if (target_sect
== NULL
)
2451 esdt
= elf_section_data (target_sect
);
2452 if (hdr
->sh_type
== SHT_RELA
)
2453 p_hdr
= &esdt
->rela
.hdr
;
2455 p_hdr
= &esdt
->rel
.hdr
;
2457 /* PR 17512: file: 0b4f81b7.
2458 Also see PR 24456, for a file which deliberately has two reloc
2462 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2465 /* xgettext:c-format */
2466 (_("%pB: warning: secondary relocation section '%s' "
2467 "for section %pA found - ignoring"),
2468 abfd
, name
, target_sect
);
2471 esdt
->has_secondary_relocs
= true;
2475 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2480 elf_elfsections (abfd
)[shindex
] = hdr2
;
2481 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2482 * bed
->s
->int_rels_per_ext_rel
);
2483 target_sect
->flags
|= SEC_RELOC
;
2484 target_sect
->relocation
= NULL
;
2485 target_sect
->rel_filepos
= hdr
->sh_offset
;
2486 /* In the section to which the relocations apply, mark whether
2487 its relocations are of the REL or RELA variety. */
2488 if (hdr
->sh_size
!= 0)
2490 if (hdr
->sh_type
== SHT_RELA
)
2491 target_sect
->use_rela_p
= 1;
2493 abfd
->flags
|= HAS_RELOC
;
2497 case SHT_GNU_verdef
:
2498 if (hdr
->sh_info
!= 0)
2499 elf_dynverdef (abfd
) = shindex
;
2500 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2501 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2504 case SHT_GNU_versym
:
2505 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2508 elf_dynversym (abfd
) = shindex
;
2509 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2510 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2513 case SHT_GNU_verneed
:
2514 if (hdr
->sh_info
!= 0)
2515 elf_dynverref (abfd
) = shindex
;
2516 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2517 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2524 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2527 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2533 /* Possibly an attributes section. */
2534 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2535 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2537 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2539 _bfd_elf_parse_attributes (abfd
, hdr
);
2543 /* Check for any processor-specific section types. */
2544 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2547 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2549 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2550 /* FIXME: How to properly handle allocated section reserved
2551 for applications? */
2553 /* xgettext:c-format */
2554 (_("%pB: unknown type [%#x] section `%s'"),
2555 abfd
, hdr
->sh_type
, name
);
2558 /* Allow sections reserved for applications. */
2559 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2563 else if (hdr
->sh_type
>= SHT_LOPROC
2564 && hdr
->sh_type
<= SHT_HIPROC
)
2565 /* FIXME: We should handle this section. */
2567 /* xgettext:c-format */
2568 (_("%pB: unknown type [%#x] section `%s'"),
2569 abfd
, hdr
->sh_type
, name
);
2570 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2572 /* Unrecognised OS-specific sections. */
2573 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2574 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2575 required to correctly process the section and the file should
2576 be rejected with an error message. */
2578 /* xgettext:c-format */
2579 (_("%pB: unknown type [%#x] section `%s'"),
2580 abfd
, hdr
->sh_type
, name
);
2583 /* Otherwise it should be processed. */
2584 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2589 /* FIXME: We should handle this section. */
2591 /* xgettext:c-format */
2592 (_("%pB: unknown type [%#x] section `%s'"),
2593 abfd
, hdr
->sh_type
, name
);
2601 elf_tdata (abfd
)->being_created
[shindex
] = false;
2605 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2608 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2610 unsigned long r_symndx
)
2612 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2614 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2616 Elf_Internal_Shdr
*symtab_hdr
;
2617 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2618 Elf_External_Sym_Shndx eshndx
;
2620 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2621 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2622 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2625 if (cache
->abfd
!= abfd
)
2627 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2630 cache
->indx
[ent
] = r_symndx
;
2633 return &cache
->sym
[ent
];
2636 /* Given an ELF section number, retrieve the corresponding BFD
2640 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2642 if (sec_index
>= elf_numsections (abfd
))
2644 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2647 static const struct bfd_elf_special_section special_sections_b
[] =
2649 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2650 { NULL
, 0, 0, 0, 0 }
2653 static const struct bfd_elf_special_section special_sections_c
[] =
2655 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2656 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2657 { NULL
, 0, 0, 0, 0 }
2660 static const struct bfd_elf_special_section special_sections_d
[] =
2662 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2663 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2664 /* There are more DWARF sections than these, but they needn't be added here
2665 unless you have to cope with broken compilers that don't emit section
2666 attributes or you want to help the user writing assembler. */
2667 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2668 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2669 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2670 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2671 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2672 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2673 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2674 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2675 { NULL
, 0, 0, 0, 0 }
2678 static const struct bfd_elf_special_section special_sections_f
[] =
2680 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2681 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2682 { NULL
, 0 , 0, 0, 0 }
2685 static const struct bfd_elf_special_section special_sections_g
[] =
2687 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2688 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2689 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2690 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2691 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2692 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2693 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2694 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2695 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2696 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2697 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2698 { NULL
, 0, 0, 0, 0 }
2701 static const struct bfd_elf_special_section special_sections_h
[] =
2703 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2704 { NULL
, 0, 0, 0, 0 }
2707 static const struct bfd_elf_special_section special_sections_i
[] =
2709 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2710 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2711 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2712 { NULL
, 0, 0, 0, 0 }
2715 static const struct bfd_elf_special_section special_sections_l
[] =
2717 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2718 { NULL
, 0, 0, 0, 0 }
2721 static const struct bfd_elf_special_section special_sections_n
[] =
2723 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2724 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2725 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2726 { NULL
, 0, 0, 0, 0 }
2729 static const struct bfd_elf_special_section special_sections_p
[] =
2731 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2732 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2733 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2734 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2735 { NULL
, 0, 0, 0, 0 }
2738 static const struct bfd_elf_special_section special_sections_r
[] =
2740 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2741 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2742 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR
, SHF_ALLOC
},
2743 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2744 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2745 { NULL
, 0, 0, 0, 0 }
2748 static const struct bfd_elf_special_section special_sections_s
[] =
2750 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2751 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2752 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2753 /* See struct bfd_elf_special_section declaration for the semantics of
2754 this special case where .prefix_length != strlen (.prefix). */
2755 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2756 { NULL
, 0, 0, 0, 0 }
2759 static const struct bfd_elf_special_section special_sections_t
[] =
2761 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2762 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2763 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2764 { NULL
, 0, 0, 0, 0 }
2767 static const struct bfd_elf_special_section special_sections_z
[] =
2769 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2770 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2771 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2772 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2773 { NULL
, 0, 0, 0, 0 }
2776 static const struct bfd_elf_special_section
* const special_sections
[] =
2778 special_sections_b
, /* 'b' */
2779 special_sections_c
, /* 'c' */
2780 special_sections_d
, /* 'd' */
2782 special_sections_f
, /* 'f' */
2783 special_sections_g
, /* 'g' */
2784 special_sections_h
, /* 'h' */
2785 special_sections_i
, /* 'i' */
2788 special_sections_l
, /* 'l' */
2790 special_sections_n
, /* 'n' */
2792 special_sections_p
, /* 'p' */
2794 special_sections_r
, /* 'r' */
2795 special_sections_s
, /* 's' */
2796 special_sections_t
, /* 't' */
2802 special_sections_z
/* 'z' */
2805 const struct bfd_elf_special_section
*
2806 _bfd_elf_get_special_section (const char *name
,
2807 const struct bfd_elf_special_section
*spec
,
2813 len
= strlen (name
);
2815 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2818 int prefix_len
= spec
[i
].prefix_length
;
2820 if (len
< prefix_len
)
2822 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2825 suffix_len
= spec
[i
].suffix_length
;
2826 if (suffix_len
<= 0)
2828 if (name
[prefix_len
] != 0)
2830 if (suffix_len
== 0)
2832 if (name
[prefix_len
] != '.'
2833 && (suffix_len
== -2
2834 || (rela
&& spec
[i
].type
== SHT_REL
)))
2840 if (len
< prefix_len
+ suffix_len
)
2842 if (memcmp (name
+ len
- suffix_len
,
2843 spec
[i
].prefix
+ prefix_len
,
2853 const struct bfd_elf_special_section
*
2854 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2857 const struct bfd_elf_special_section
*spec
;
2858 const struct elf_backend_data
*bed
;
2860 /* See if this is one of the special sections. */
2861 if (sec
->name
== NULL
)
2864 bed
= get_elf_backend_data (abfd
);
2865 spec
= bed
->special_sections
;
2868 spec
= _bfd_elf_get_special_section (sec
->name
,
2869 bed
->special_sections
,
2875 if (sec
->name
[0] != '.')
2878 i
= sec
->name
[1] - 'b';
2879 if (i
< 0 || i
> 'z' - 'b')
2882 spec
= special_sections
[i
];
2887 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2891 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2893 struct bfd_elf_section_data
*sdata
;
2894 const struct elf_backend_data
*bed
;
2895 const struct bfd_elf_special_section
*ssect
;
2897 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2900 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2904 sec
->used_by_bfd
= sdata
;
2907 /* Indicate whether or not this section should use RELA relocations. */
2908 bed
= get_elf_backend_data (abfd
);
2909 sec
->use_rela_p
= bed
->default_use_rela_p
;
2911 /* Set up ELF section type and flags for newly created sections, if
2912 there is an ABI mandated section. */
2913 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2916 elf_section_type (sec
) = ssect
->type
;
2917 elf_section_flags (sec
) = ssect
->attr
;
2920 return _bfd_generic_new_section_hook (abfd
, sec
);
2923 /* Create a new bfd section from an ELF program header.
2925 Since program segments have no names, we generate a synthetic name
2926 of the form segment<NUM>, where NUM is generally the index in the
2927 program header table. For segments that are split (see below) we
2928 generate the names segment<NUM>a and segment<NUM>b.
2930 Note that some program segments may have a file size that is different than
2931 (less than) the memory size. All this means is that at execution the
2932 system must allocate the amount of memory specified by the memory size,
2933 but only initialize it with the first "file size" bytes read from the
2934 file. This would occur for example, with program segments consisting
2935 of combined data+bss.
2937 To handle the above situation, this routine generates TWO bfd sections
2938 for the single program segment. The first has the length specified by
2939 the file size of the segment, and the second has the length specified
2940 by the difference between the two sizes. In effect, the segment is split
2941 into its initialized and uninitialized parts. */
2944 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2945 Elf_Internal_Phdr
*hdr
,
2947 const char *type_name
)
2954 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
2956 split
= ((hdr
->p_memsz
> 0)
2957 && (hdr
->p_filesz
> 0)
2958 && (hdr
->p_memsz
> hdr
->p_filesz
));
2960 if (hdr
->p_filesz
> 0)
2962 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2963 len
= strlen (namebuf
) + 1;
2964 name
= (char *) bfd_alloc (abfd
, len
);
2967 memcpy (name
, namebuf
, len
);
2968 newsect
= bfd_make_section (abfd
, name
);
2969 if (newsect
== NULL
)
2971 newsect
->vma
= hdr
->p_vaddr
/ opb
;
2972 newsect
->lma
= hdr
->p_paddr
/ opb
;
2973 newsect
->size
= hdr
->p_filesz
;
2974 newsect
->filepos
= hdr
->p_offset
;
2975 newsect
->flags
|= SEC_HAS_CONTENTS
;
2976 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2977 if (hdr
->p_type
== PT_LOAD
)
2979 newsect
->flags
|= SEC_ALLOC
;
2980 newsect
->flags
|= SEC_LOAD
;
2981 if (hdr
->p_flags
& PF_X
)
2983 /* FIXME: all we known is that it has execute PERMISSION,
2985 newsect
->flags
|= SEC_CODE
;
2988 if (!(hdr
->p_flags
& PF_W
))
2990 newsect
->flags
|= SEC_READONLY
;
2994 if (hdr
->p_memsz
> hdr
->p_filesz
)
2998 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2999 len
= strlen (namebuf
) + 1;
3000 name
= (char *) bfd_alloc (abfd
, len
);
3003 memcpy (name
, namebuf
, len
);
3004 newsect
= bfd_make_section (abfd
, name
);
3005 if (newsect
== NULL
)
3007 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3008 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3009 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3010 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3011 align
= newsect
->vma
& -newsect
->vma
;
3012 if (align
== 0 || align
> hdr
->p_align
)
3013 align
= hdr
->p_align
;
3014 newsect
->alignment_power
= bfd_log2 (align
);
3015 if (hdr
->p_type
== PT_LOAD
)
3017 newsect
->flags
|= SEC_ALLOC
;
3018 if (hdr
->p_flags
& PF_X
)
3019 newsect
->flags
|= SEC_CODE
;
3021 if (!(hdr
->p_flags
& PF_W
))
3022 newsect
->flags
|= SEC_READONLY
;
3029 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3031 /* The return value is ignored. Build-ids are considered optional. */
3032 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3033 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3039 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3041 const struct elf_backend_data
*bed
;
3043 switch (hdr
->p_type
)
3046 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3049 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3051 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3052 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3056 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3059 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3062 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3064 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3070 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3073 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3075 case PT_GNU_EH_FRAME
:
3076 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3080 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3083 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3086 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3090 /* Check for any processor-specific program segment types. */
3091 bed
= get_elf_backend_data (abfd
);
3092 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3096 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3100 _bfd_elf_single_rel_hdr (asection
*sec
)
3102 if (elf_section_data (sec
)->rel
.hdr
)
3104 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3105 return elf_section_data (sec
)->rel
.hdr
;
3108 return elf_section_data (sec
)->rela
.hdr
;
3112 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3113 Elf_Internal_Shdr
*rel_hdr
,
3114 const char *sec_name
,
3117 char *name
= (char *) bfd_alloc (abfd
,
3118 sizeof ".rela" + strlen (sec_name
));
3122 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3124 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3126 if (rel_hdr
->sh_name
== (unsigned int) -1)
3132 /* Allocate and initialize a section-header for a new reloc section,
3133 containing relocations against ASECT. It is stored in RELDATA. If
3134 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3138 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3139 struct bfd_elf_section_reloc_data
*reldata
,
3140 const char *sec_name
,
3142 bool delay_st_name_p
)
3144 Elf_Internal_Shdr
*rel_hdr
;
3145 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3147 BFD_ASSERT (reldata
->hdr
== NULL
);
3148 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3149 reldata
->hdr
= rel_hdr
;
3151 if (delay_st_name_p
)
3152 rel_hdr
->sh_name
= (unsigned int) -1;
3153 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3156 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3157 rel_hdr
->sh_entsize
= (use_rela_p
3158 ? bed
->s
->sizeof_rela
3159 : bed
->s
->sizeof_rel
);
3160 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3161 rel_hdr
->sh_flags
= 0;
3162 rel_hdr
->sh_addr
= 0;
3163 rel_hdr
->sh_size
= 0;
3164 rel_hdr
->sh_offset
= 0;
3169 /* Return the default section type based on the passed in section flags. */
3172 bfd_elf_get_default_section_type (flagword flags
)
3174 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3175 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3177 return SHT_PROGBITS
;
3180 struct fake_section_arg
3182 struct bfd_link_info
*link_info
;
3186 /* Set up an ELF internal section header for a section. */
3189 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3191 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3192 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3193 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3194 Elf_Internal_Shdr
*this_hdr
;
3195 unsigned int sh_type
;
3196 const char *name
= asect
->name
;
3197 bool delay_st_name_p
= false;
3202 /* We already failed; just get out of the bfd_map_over_sections
3207 this_hdr
= &esd
->this_hdr
;
3211 /* ld: compress DWARF debug sections with names: .debug_*. */
3212 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3213 && (asect
->flags
& SEC_DEBUGGING
)
3217 /* Set SEC_ELF_COMPRESS to indicate this section should be
3219 asect
->flags
|= SEC_ELF_COMPRESS
;
3220 /* If this section will be compressed, delay adding section
3221 name to section name section after it is compressed in
3222 _bfd_elf_assign_file_positions_for_non_load. */
3223 delay_st_name_p
= true;
3226 else if ((asect
->flags
& SEC_ELF_RENAME
))
3228 /* objcopy: rename output DWARF debug section. */
3229 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3231 /* When we decompress or compress with SHF_COMPRESSED,
3232 convert section name from .zdebug_* to .debug_* if
3236 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3237 if (new_name
== NULL
)
3245 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
3248 /* PR binutils/18087: Compression does not always make a
3249 section smaller. So only rename the section when
3250 compression has actually taken place. If input section
3251 name is .zdebug_*, we should never compress it again. */
3252 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3253 if (new_name
== NULL
)
3262 if (delay_st_name_p
)
3263 this_hdr
->sh_name
= (unsigned int) -1;
3267 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3269 if (this_hdr
->sh_name
== (unsigned int) -1)
3276 /* Don't clear sh_flags. Assembler may set additional bits. */
3278 if ((asect
->flags
& SEC_ALLOC
) != 0
3279 || asect
->user_set_vma
)
3280 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3282 this_hdr
->sh_addr
= 0;
3284 this_hdr
->sh_offset
= 0;
3285 this_hdr
->sh_size
= asect
->size
;
3286 this_hdr
->sh_link
= 0;
3287 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3288 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3291 /* xgettext:c-format */
3292 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3293 abfd
, asect
->alignment_power
, asect
);
3297 /* Set sh_addralign to the highest power of two given by alignment
3298 consistent with the section VMA. Linker scripts can force VMA. */
3299 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3300 this_hdr
->sh_addralign
= mask
& -mask
;
3301 /* The sh_entsize and sh_info fields may have been set already by
3302 copy_private_section_data. */
3304 this_hdr
->bfd_section
= asect
;
3305 this_hdr
->contents
= NULL
;
3307 /* If the section type is unspecified, we set it based on
3309 if (asect
->type
!= 0)
3310 sh_type
= asect
->type
;
3311 else if ((asect
->flags
& SEC_GROUP
) != 0)
3312 sh_type
= SHT_GROUP
;
3314 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3316 if (this_hdr
->sh_type
== SHT_NULL
)
3317 this_hdr
->sh_type
= sh_type
;
3318 else if (this_hdr
->sh_type
== SHT_NOBITS
3319 && sh_type
== SHT_PROGBITS
3320 && (asect
->flags
& SEC_ALLOC
) != 0)
3322 /* Warn if we are changing a NOBITS section to PROGBITS, but
3323 allow the link to proceed. This can happen when users link
3324 non-bss input sections to bss output sections, or emit data
3325 to a bss output section via a linker script. */
3327 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3328 this_hdr
->sh_type
= sh_type
;
3331 switch (this_hdr
->sh_type
)
3342 case SHT_INIT_ARRAY
:
3343 case SHT_FINI_ARRAY
:
3344 case SHT_PREINIT_ARRAY
:
3345 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3349 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3353 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3357 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3361 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3362 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3366 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3367 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3370 case SHT_GNU_versym
:
3371 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3374 case SHT_GNU_verdef
:
3375 this_hdr
->sh_entsize
= 0;
3376 /* objcopy or strip will copy over sh_info, but may not set
3377 cverdefs. The linker will set cverdefs, but sh_info will be
3379 if (this_hdr
->sh_info
== 0)
3380 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3382 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3383 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3386 case SHT_GNU_verneed
:
3387 this_hdr
->sh_entsize
= 0;
3388 /* objcopy or strip will copy over sh_info, but may not set
3389 cverrefs. The linker will set cverrefs, but sh_info will be
3391 if (this_hdr
->sh_info
== 0)
3392 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3394 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3395 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3399 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3403 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3407 if ((asect
->flags
& SEC_ALLOC
) != 0)
3408 this_hdr
->sh_flags
|= SHF_ALLOC
;
3409 if ((asect
->flags
& SEC_READONLY
) == 0)
3410 this_hdr
->sh_flags
|= SHF_WRITE
;
3411 if ((asect
->flags
& SEC_CODE
) != 0)
3412 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3413 if ((asect
->flags
& SEC_MERGE
) != 0)
3415 this_hdr
->sh_flags
|= SHF_MERGE
;
3416 this_hdr
->sh_entsize
= asect
->entsize
;
3418 if ((asect
->flags
& SEC_STRINGS
) != 0)
3419 this_hdr
->sh_flags
|= SHF_STRINGS
;
3420 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3421 this_hdr
->sh_flags
|= SHF_GROUP
;
3422 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3424 this_hdr
->sh_flags
|= SHF_TLS
;
3425 if (asect
->size
== 0
3426 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3428 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3430 this_hdr
->sh_size
= 0;
3433 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3434 if (this_hdr
->sh_size
!= 0)
3435 this_hdr
->sh_type
= SHT_NOBITS
;
3439 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3440 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3442 /* If the section has relocs, set up a section header for the
3443 SHT_REL[A] section. If two relocation sections are required for
3444 this section, it is up to the processor-specific back-end to
3445 create the other. */
3446 if ((asect
->flags
& SEC_RELOC
) != 0)
3448 /* When doing a relocatable link, create both REL and RELA sections if
3451 /* Do the normal setup if we wouldn't create any sections here. */
3452 && esd
->rel
.count
+ esd
->rela
.count
> 0
3453 && (bfd_link_relocatable (arg
->link_info
)
3454 || arg
->link_info
->emitrelocations
))
3456 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3457 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3458 false, delay_st_name_p
))
3463 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3464 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3465 true, delay_st_name_p
))
3471 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3473 ? &esd
->rela
: &esd
->rel
),
3483 /* Check for processor-specific section types. */
3484 sh_type
= this_hdr
->sh_type
;
3485 if (bed
->elf_backend_fake_sections
3486 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3492 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3494 /* Don't change the header type from NOBITS if we are being
3495 called for objcopy --only-keep-debug. */
3496 this_hdr
->sh_type
= sh_type
;
3500 /* Fill in the contents of a SHT_GROUP section. Called from
3501 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3502 when ELF targets use the generic linker, ld. Called for ld -r
3503 from bfd_elf_final_link. */
3506 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3508 bool *failedptr
= (bool *) failedptrarg
;
3509 asection
*elt
, *first
;
3513 /* Ignore linker created group section. See elfNN_ia64_object_p in
3515 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3520 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3522 unsigned long symindx
= 0;
3524 /* elf_group_id will have been set up by objcopy and the
3526 if (elf_group_id (sec
) != NULL
)
3527 symindx
= elf_group_id (sec
)->udata
.i
;
3531 /* If called from the assembler, swap_out_syms will have set up
3533 PR 25699: A corrupt input file could contain bogus group info. */
3534 if (sec
->index
>= elf_num_section_syms (abfd
)
3535 || elf_section_syms (abfd
)[sec
->index
] == NULL
)
3540 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3542 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3544 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3546 /* The ELF backend linker sets sh_info to -2 when the group
3547 signature symbol is global, and thus the index can't be
3548 set until all local symbols are output. */
3550 struct bfd_elf_section_data
*sec_data
;
3551 unsigned long symndx
;
3552 unsigned long extsymoff
;
3553 struct elf_link_hash_entry
*h
;
3555 /* The point of this little dance to the first SHF_GROUP section
3556 then back to the SHT_GROUP section is that this gets us to
3557 the SHT_GROUP in the input object. */
3558 igroup
= elf_sec_group (elf_next_in_group (sec
));
3559 sec_data
= elf_section_data (igroup
);
3560 symndx
= sec_data
->this_hdr
.sh_info
;
3562 if (!elf_bad_symtab (igroup
->owner
))
3564 Elf_Internal_Shdr
*symtab_hdr
;
3566 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3567 extsymoff
= symtab_hdr
->sh_info
;
3569 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3570 while (h
->root
.type
== bfd_link_hash_indirect
3571 || h
->root
.type
== bfd_link_hash_warning
)
3572 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3574 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3577 /* The contents won't be allocated for "ld -r" or objcopy. */
3579 if (sec
->contents
== NULL
)
3582 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3584 /* Arrange for the section to be written out. */
3585 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3586 if (sec
->contents
== NULL
)
3593 loc
= sec
->contents
+ sec
->size
;
3595 /* Get the pointer to the first section in the group that gas
3596 squirreled away here. objcopy arranges for this to be set to the
3597 start of the input section group. */
3598 first
= elt
= elf_next_in_group (sec
);
3600 /* First element is a flag word. Rest of section is elf section
3601 indices for all the sections of the group. Write them backwards
3602 just to keep the group in the same order as given in .section
3603 directives, not that it matters. */
3610 s
= s
->output_section
;
3612 && !bfd_is_abs_section (s
))
3614 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3615 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3617 if (elf_sec
->rel
.hdr
!= NULL
3619 || (input_elf_sec
->rel
.hdr
!= NULL
3620 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3622 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3624 if (loc
== sec
->contents
)
3626 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3628 if (elf_sec
->rela
.hdr
!= NULL
3630 || (input_elf_sec
->rela
.hdr
!= NULL
3631 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3633 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3635 if (loc
== sec
->contents
)
3637 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3640 if (loc
== sec
->contents
)
3642 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3644 elt
= elf_next_in_group (elt
);
3649 /* We should always get here with loc == sec->contents + 4, but it is
3650 possible to craft bogus SHT_GROUP sections that will cause segfaults
3651 in objcopy without checking loc here and in the loop above. */
3652 if (loc
== sec
->contents
)
3657 if (loc
!= sec
->contents
)
3660 memset (sec
->contents
+ 4, 0, loc
- sec
->contents
);
3661 loc
= sec
->contents
;
3665 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3668 /* Given NAME, the name of a relocation section stripped of its
3669 .rel/.rela prefix, return the section in ABFD to which the
3670 relocations apply. */
3673 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3675 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3676 section likely apply to .got.plt or .got section. */
3677 if (get_elf_backend_data (abfd
)->want_got_plt
3678 && strcmp (name
, ".plt") == 0)
3683 sec
= bfd_get_section_by_name (abfd
, name
);
3689 return bfd_get_section_by_name (abfd
, name
);
3692 /* Return the section to which RELOC_SEC applies. */
3695 elf_get_reloc_section (asection
*reloc_sec
)
3700 const struct elf_backend_data
*bed
;
3702 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3703 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3706 /* We look up the section the relocs apply to by name. */
3707 name
= reloc_sec
->name
;
3708 if (!startswith (name
, ".rel"))
3711 if (type
== SHT_RELA
&& *name
++ != 'a')
3714 abfd
= reloc_sec
->owner
;
3715 bed
= get_elf_backend_data (abfd
);
3716 return bed
->get_reloc_section (abfd
, name
);
3719 /* Assign all ELF section numbers. The dummy first section is handled here
3720 too. The link/info pointers for the standard section types are filled
3721 in here too, while we're at it. LINK_INFO will be 0 when arriving
3722 here for gas, objcopy, and when using the generic ELF linker. */
3725 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3727 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3729 unsigned int section_number
;
3730 Elf_Internal_Shdr
**i_shdrp
;
3731 struct bfd_elf_section_data
*d
;
3737 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3739 /* SHT_GROUP sections are in relocatable files only. */
3740 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3742 size_t reloc_count
= 0;
3744 /* Put SHT_GROUP sections first. */
3745 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3747 d
= elf_section_data (sec
);
3749 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3751 if (sec
->flags
& SEC_LINKER_CREATED
)
3753 /* Remove the linker created SHT_GROUP sections. */
3754 bfd_section_list_remove (abfd
, sec
);
3755 abfd
->section_count
--;
3758 d
->this_idx
= section_number
++;
3761 /* Count relocations. */
3762 reloc_count
+= sec
->reloc_count
;
3765 /* Set/clear HAS_RELOC depending on whether there are relocations. */
3766 if (reloc_count
== 0)
3767 abfd
->flags
&= ~HAS_RELOC
;
3769 abfd
->flags
|= HAS_RELOC
;
3772 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3774 d
= elf_section_data (sec
);
3776 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3777 d
->this_idx
= section_number
++;
3778 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3779 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3782 d
->rel
.idx
= section_number
++;
3783 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3784 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3791 d
->rela
.idx
= section_number
++;
3792 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3793 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3799 need_symtab
= (bfd_get_symcount (abfd
) > 0
3800 || (link_info
== NULL
3801 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3805 elf_onesymtab (abfd
) = section_number
++;
3806 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3807 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3809 elf_section_list
*entry
;
3811 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3813 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3814 entry
->ndx
= section_number
++;
3815 elf_symtab_shndx_list (abfd
) = entry
;
3817 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3818 ".symtab_shndx", false);
3819 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3822 elf_strtab_sec (abfd
) = section_number
++;
3823 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3826 elf_shstrtab_sec (abfd
) = section_number
++;
3827 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3828 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3830 if (section_number
>= SHN_LORESERVE
)
3832 /* xgettext:c-format */
3833 _bfd_error_handler (_("%pB: too many sections: %u"),
3834 abfd
, section_number
);
3838 elf_numsections (abfd
) = section_number
;
3839 elf_elfheader (abfd
)->e_shnum
= section_number
;
3841 /* Set up the list of section header pointers, in agreement with the
3843 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3844 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3845 if (i_shdrp
== NULL
)
3848 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3849 sizeof (Elf_Internal_Shdr
));
3850 if (i_shdrp
[0] == NULL
)
3852 bfd_release (abfd
, i_shdrp
);
3856 elf_elfsections (abfd
) = i_shdrp
;
3858 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3861 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3862 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3864 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3865 BFD_ASSERT (entry
!= NULL
);
3866 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3867 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3869 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3870 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3873 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3877 d
= elf_section_data (sec
);
3879 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3880 if (d
->rel
.idx
!= 0)
3881 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3882 if (d
->rela
.idx
!= 0)
3883 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3885 /* Fill in the sh_link and sh_info fields while we're at it. */
3887 /* sh_link of a reloc section is the section index of the symbol
3888 table. sh_info is the section index of the section to which
3889 the relocation entries apply. */
3890 if (d
->rel
.idx
!= 0)
3892 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3893 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3894 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3896 if (d
->rela
.idx
!= 0)
3898 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3899 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3900 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3903 /* We need to set up sh_link for SHF_LINK_ORDER. */
3904 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3906 s
= elf_linked_to_section (sec
);
3907 /* We can now have a NULL linked section pointer.
3908 This happens when the sh_link field is 0, which is done
3909 when a linked to section is discarded but the linking
3910 section has been retained for some reason. */
3913 /* Check discarded linkonce section. */
3914 if (discarded_section (s
))
3918 /* xgettext:c-format */
3919 (_("%pB: sh_link of section `%pA' points to"
3920 " discarded section `%pA' of `%pB'"),
3921 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3922 /* Point to the kept section if it has the same
3923 size as the discarded one. */
3924 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3927 bfd_set_error (bfd_error_bad_value
);
3932 /* Handle objcopy. */
3933 else if (s
->output_section
== NULL
)
3936 /* xgettext:c-format */
3937 (_("%pB: sh_link of section `%pA' points to"
3938 " removed section `%pA' of `%pB'"),
3939 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3940 bfd_set_error (bfd_error_bad_value
);
3943 s
= s
->output_section
;
3944 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3948 switch (d
->this_hdr
.sh_type
)
3952 /* A reloc section which we are treating as a normal BFD
3953 section. sh_link is the section index of the symbol
3954 table. sh_info is the section index of the section to
3955 which the relocation entries apply. We assume that an
3956 allocated reloc section uses the dynamic symbol table
3957 if there is one. Otherwise we guess the normal symbol
3958 table. FIXME: How can we be sure? */
3959 if (d
->this_hdr
.sh_link
== 0 && (sec
->flags
& SEC_ALLOC
) != 0)
3961 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3963 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3965 if (d
->this_hdr
.sh_link
== 0)
3966 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3968 s
= elf_get_reloc_section (sec
);
3971 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3972 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3977 /* We assume that a section named .stab*str is a stabs
3978 string section. We look for a section with the same name
3979 but without the trailing ``str'', and set its sh_link
3980 field to point to this section. */
3981 if (startswith (sec
->name
, ".stab")
3982 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3987 len
= strlen (sec
->name
);
3988 alc
= (char *) bfd_malloc (len
- 2);
3991 memcpy (alc
, sec
->name
, len
- 3);
3992 alc
[len
- 3] = '\0';
3993 s
= bfd_get_section_by_name (abfd
, alc
);
3997 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3999 /* This is a .stab section. */
4000 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
4007 case SHT_GNU_verneed
:
4008 case SHT_GNU_verdef
:
4009 /* sh_link is the section header index of the string table
4010 used for the dynamic entries, or the symbol table, or the
4012 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4014 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4017 case SHT_GNU_LIBLIST
:
4018 /* sh_link is the section header index of the prelink library
4019 list used for the dynamic entries, or the symbol table, or
4020 the version strings. */
4021 s
= bfd_get_section_by_name (abfd
, ((sec
->flags
& SEC_ALLOC
)
4022 ? ".dynstr" : ".gnu.libstr"));
4024 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4029 case SHT_GNU_versym
:
4030 /* sh_link is the section header index of the symbol table
4031 this hash table or version table is for. */
4032 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4034 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4038 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4042 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4043 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4044 debug section name from .debug_* to .zdebug_* if needed. */
4050 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4052 /* If the backend has a special mapping, use it. */
4053 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4054 if (bed
->elf_backend_sym_is_global
)
4055 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4057 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4058 || bfd_is_und_section (bfd_asymbol_section (sym
))
4059 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4062 /* Filter global symbols of ABFD to include in the import library. All
4063 SYMCOUNT symbols of ABFD can be examined from their pointers in
4064 SYMS. Pointers of symbols to keep should be stored contiguously at
4065 the beginning of that array.
4067 Returns the number of symbols to keep. */
4070 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4071 asymbol
**syms
, long symcount
)
4073 long src_count
, dst_count
= 0;
4075 for (src_count
= 0; src_count
< symcount
; src_count
++)
4077 asymbol
*sym
= syms
[src_count
];
4078 char *name
= (char *) bfd_asymbol_name (sym
);
4079 struct bfd_link_hash_entry
*h
;
4081 if (!sym_is_global (abfd
, sym
))
4084 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, false);
4087 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4089 if (h
->linker_def
|| h
->ldscript_def
)
4092 syms
[dst_count
++] = sym
;
4095 syms
[dst_count
] = NULL
;
4100 /* Don't output section symbols for sections that are not going to be
4101 output, that are duplicates or there is no BFD section. */
4104 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4106 elf_symbol_type
*type_ptr
;
4111 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4114 /* Ignore the section symbol if it isn't used. */
4115 if ((sym
->flags
& BSF_SECTION_SYM_USED
) == 0)
4118 if (sym
->section
== NULL
)
4121 type_ptr
= elf_symbol_from (sym
);
4122 return ((type_ptr
!= NULL
4123 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4124 && bfd_is_abs_section (sym
->section
))
4125 || !(sym
->section
->owner
== abfd
4126 || (sym
->section
->output_section
!= NULL
4127 && sym
->section
->output_section
->owner
== abfd
4128 && sym
->section
->output_offset
== 0)
4129 || bfd_is_abs_section (sym
->section
)));
4132 /* Map symbol from it's internal number to the external number, moving
4133 all local symbols to be at the head of the list. */
4136 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4138 unsigned int symcount
= bfd_get_symcount (abfd
);
4139 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4140 asymbol
**sect_syms
;
4141 unsigned int num_locals
= 0;
4142 unsigned int num_globals
= 0;
4143 unsigned int num_locals2
= 0;
4144 unsigned int num_globals2
= 0;
4145 unsigned int max_index
= 0;
4152 fprintf (stderr
, "elf_map_symbols\n");
4156 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4158 if (max_index
< asect
->index
)
4159 max_index
= asect
->index
;
4163 amt
= max_index
* sizeof (asymbol
*);
4164 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4165 if (sect_syms
== NULL
)
4167 elf_section_syms (abfd
) = sect_syms
;
4168 elf_num_section_syms (abfd
) = max_index
;
4170 /* Init sect_syms entries for any section symbols we have already
4171 decided to output. */
4172 for (idx
= 0; idx
< symcount
; idx
++)
4174 asymbol
*sym
= syms
[idx
];
4176 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4178 && !ignore_section_sym (abfd
, sym
)
4179 && !bfd_is_abs_section (sym
->section
))
4181 asection
*sec
= sym
->section
;
4183 if (sec
->owner
!= abfd
)
4184 sec
= sec
->output_section
;
4186 sect_syms
[sec
->index
] = syms
[idx
];
4190 /* Classify all of the symbols. */
4191 for (idx
= 0; idx
< symcount
; idx
++)
4193 if (sym_is_global (abfd
, syms
[idx
]))
4195 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4199 /* We will be adding a section symbol for each normal BFD section. Most
4200 sections will already have a section symbol in outsymbols, but
4201 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4202 at least in that case. */
4203 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4205 asymbol
*sym
= asect
->symbol
;
4206 /* Don't include ignored section symbols. */
4207 if (!ignore_section_sym (abfd
, sym
)
4208 && sect_syms
[asect
->index
] == NULL
)
4210 if (!sym_is_global (abfd
, asect
->symbol
))
4217 /* Now sort the symbols so the local symbols are first. */
4218 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4219 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4220 if (new_syms
== NULL
)
4223 for (idx
= 0; idx
< symcount
; idx
++)
4225 asymbol
*sym
= syms
[idx
];
4228 if (sym_is_global (abfd
, sym
))
4229 i
= num_locals
+ num_globals2
++;
4230 /* Don't include ignored section symbols. */
4231 else if (!ignore_section_sym (abfd
, sym
))
4236 sym
->udata
.i
= i
+ 1;
4238 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4240 asymbol
*sym
= asect
->symbol
;
4241 if (!ignore_section_sym (abfd
, sym
)
4242 && sect_syms
[asect
->index
] == NULL
)
4246 sect_syms
[asect
->index
] = sym
;
4247 if (!sym_is_global (abfd
, sym
))
4250 i
= num_locals
+ num_globals2
++;
4252 sym
->udata
.i
= i
+ 1;
4256 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4258 *pnum_locals
= num_locals
;
4262 /* Align to the maximum file alignment that could be required for any
4263 ELF data structure. */
4265 static inline file_ptr
4266 align_file_position (file_ptr off
, int align
)
4268 return (off
+ align
- 1) & ~(align
- 1);
4271 /* Assign a file position to a section, optionally aligning to the
4272 required section alignment. */
4275 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4279 if (align
&& i_shdrp
->sh_addralign
> 1)
4280 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
& -i_shdrp
->sh_addralign
);
4281 i_shdrp
->sh_offset
= offset
;
4282 if (i_shdrp
->bfd_section
!= NULL
)
4283 i_shdrp
->bfd_section
->filepos
= offset
;
4284 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4285 offset
+= i_shdrp
->sh_size
;
4289 /* Compute the file positions we are going to put the sections at, and
4290 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4291 is not NULL, this is being called by the ELF backend linker. */
4294 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4295 struct bfd_link_info
*link_info
)
4297 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4298 struct fake_section_arg fsargs
;
4300 struct elf_strtab_hash
*strtab
= NULL
;
4301 Elf_Internal_Shdr
*shstrtab_hdr
;
4304 if (abfd
->output_has_begun
)
4307 /* Do any elf backend specific processing first. */
4308 if (bed
->elf_backend_begin_write_processing
)
4309 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4311 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4314 fsargs
.failed
= false;
4315 fsargs
.link_info
= link_info
;
4316 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4320 if (!assign_section_numbers (abfd
, link_info
))
4323 /* The backend linker builds symbol table information itself. */
4324 need_symtab
= (link_info
== NULL
4325 && (bfd_get_symcount (abfd
) > 0
4326 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4330 /* Non-zero if doing a relocatable link. */
4331 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4333 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
, link_info
))
4338 if (link_info
== NULL
)
4340 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4345 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4346 /* sh_name was set in init_file_header. */
4347 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4348 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4349 shstrtab_hdr
->sh_addr
= 0;
4350 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4351 shstrtab_hdr
->sh_entsize
= 0;
4352 shstrtab_hdr
->sh_link
= 0;
4353 shstrtab_hdr
->sh_info
= 0;
4354 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4355 shstrtab_hdr
->sh_addralign
= 1;
4357 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4363 Elf_Internal_Shdr
*hdr
;
4365 off
= elf_next_file_pos (abfd
);
4367 hdr
= & elf_symtab_hdr (abfd
);
4368 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4370 if (elf_symtab_shndx_list (abfd
) != NULL
)
4372 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4373 if (hdr
->sh_size
!= 0)
4374 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4375 /* FIXME: What about other symtab_shndx sections in the list ? */
4378 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4379 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4381 elf_next_file_pos (abfd
) = off
;
4383 /* Now that we know where the .strtab section goes, write it
4385 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4386 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4388 _bfd_elf_strtab_free (strtab
);
4391 abfd
->output_has_begun
= true;
4396 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4397 function effectively returns whether --eh-frame-hdr is given on the
4398 command line. After size_dynamic_sections the result reflects
4399 whether .eh_frame_hdr will actually be output (sizing isn't done
4400 until ldemul_after_allocation). */
4403 elf_eh_frame_hdr (const struct bfd_link_info
*info
)
4405 if (info
!= NULL
&& is_elf_hash_table (info
->hash
))
4406 return elf_hash_table (info
)->eh_info
.hdr_sec
;
4410 /* Make an initial estimate of the size of the program header. If we
4411 get the number wrong here, we'll redo section placement. */
4413 static bfd_size_type
4414 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4418 const struct elf_backend_data
*bed
;
4420 /* Assume we will need exactly two PT_LOAD segments: one for text
4421 and one for data. */
4424 s
= bfd_get_section_by_name (abfd
, ".interp");
4425 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4427 /* If we have a loadable interpreter section, we need a
4428 PT_INTERP segment. In this case, assume we also need a
4429 PT_PHDR segment, although that may not be true for all
4434 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4436 /* We need a PT_DYNAMIC segment. */
4440 if (info
!= NULL
&& info
->relro
)
4442 /* We need a PT_GNU_RELRO segment. */
4446 if (elf_eh_frame_hdr (info
))
4448 /* We need a PT_GNU_EH_FRAME segment. */
4452 if (elf_stack_flags (abfd
))
4454 /* We need a PT_GNU_STACK segment. */
4458 if (elf_sframe (abfd
))
4460 /* We need a PT_GNU_SFRAME segment. */
4464 s
= bfd_get_section_by_name (abfd
,
4465 NOTE_GNU_PROPERTY_SECTION_NAME
);
4466 if (s
!= NULL
&& s
->size
!= 0)
4468 /* We need a PT_GNU_PROPERTY segment. */
4472 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4474 if ((s
->flags
& SEC_LOAD
) != 0
4475 && elf_section_type (s
) == SHT_NOTE
)
4477 unsigned int alignment_power
;
4478 /* We need a PT_NOTE segment. */
4480 /* Try to create just one PT_NOTE segment for all adjacent
4481 loadable SHT_NOTE sections. gABI requires that within a
4482 PT_NOTE segment (and also inside of each SHT_NOTE section)
4483 each note should have the same alignment. So we check
4484 whether the sections are correctly aligned. */
4485 alignment_power
= s
->alignment_power
;
4486 while (s
->next
!= NULL
4487 && s
->next
->alignment_power
== alignment_power
4488 && (s
->next
->flags
& SEC_LOAD
) != 0
4489 && elf_section_type (s
->next
) == SHT_NOTE
)
4494 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4496 if (s
->flags
& SEC_THREAD_LOCAL
)
4498 /* We need a PT_TLS segment. */
4504 bed
= get_elf_backend_data (abfd
);
4506 if ((abfd
->flags
& D_PAGED
) != 0
4507 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4509 /* Add a PT_GNU_MBIND segment for each mbind section. */
4510 bfd_vma commonpagesize
;
4511 unsigned int page_align_power
;
4514 commonpagesize
= info
->commonpagesize
;
4516 commonpagesize
= bed
->commonpagesize
;
4517 page_align_power
= bfd_log2 (commonpagesize
);
4518 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4519 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4521 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4524 /* xgettext:c-format */
4525 (_("%pB: GNU_MBIND section `%pA' has invalid "
4526 "sh_info field: %d"),
4527 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4530 /* Align mbind section to page size. */
4531 if (s
->alignment_power
< page_align_power
)
4532 s
->alignment_power
= page_align_power
;
4537 /* Let the backend count up any program headers it might need. */
4538 if (bed
->elf_backend_additional_program_headers
)
4542 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4548 return segs
* bed
->s
->sizeof_phdr
;
4551 /* Find the segment that contains the output_section of section. */
4554 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4556 struct elf_segment_map
*m
;
4557 Elf_Internal_Phdr
*p
;
4559 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4565 for (i
= m
->count
- 1; i
>= 0; i
--)
4566 if (m
->sections
[i
] == section
)
4573 /* Create a mapping from a set of sections to a program segment. */
4575 static struct elf_segment_map
*
4576 make_mapping (bfd
*abfd
,
4577 asection
**sections
,
4582 struct elf_segment_map
*m
;
4587 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4588 amt
+= (to
- from
) * sizeof (asection
*);
4589 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4593 m
->p_type
= PT_LOAD
;
4594 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4595 m
->sections
[i
- from
] = *hdrpp
;
4596 m
->count
= to
- from
;
4598 if (from
== 0 && phdr
)
4600 /* Include the headers in the first PT_LOAD segment. */
4601 m
->includes_filehdr
= 1;
4602 m
->includes_phdrs
= 1;
4608 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4611 struct elf_segment_map
*
4612 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4614 struct elf_segment_map
*m
;
4616 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4617 sizeof (struct elf_segment_map
));
4621 m
->p_type
= PT_DYNAMIC
;
4623 m
->sections
[0] = dynsec
;
4628 /* Possibly add or remove segments from the segment map. */
4631 elf_modify_segment_map (bfd
*abfd
,
4632 struct bfd_link_info
*info
,
4633 bool remove_empty_load
)
4635 struct elf_segment_map
**m
;
4636 const struct elf_backend_data
*bed
;
4638 /* The placement algorithm assumes that non allocated sections are
4639 not in PT_LOAD segments. We ensure this here by removing such
4640 sections from the segment map. We also remove excluded
4641 sections. Finally, any PT_LOAD segment without sections is
4643 m
= &elf_seg_map (abfd
);
4646 unsigned int i
, new_count
;
4648 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4650 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4651 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4652 || (*m
)->p_type
!= PT_LOAD
))
4654 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4658 (*m
)->count
= new_count
;
4660 if (remove_empty_load
4661 && (*m
)->p_type
== PT_LOAD
4663 && !(*m
)->includes_phdrs
)
4669 bed
= get_elf_backend_data (abfd
);
4670 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4672 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4679 #define IS_TBSS(s) \
4680 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4682 /* Set up a mapping from BFD sections to program segments. Update
4683 NEED_LAYOUT if the section layout is changed. */
4686 _bfd_elf_map_sections_to_segments (bfd
*abfd
,
4687 struct bfd_link_info
*info
,
4691 struct elf_segment_map
*m
;
4692 asection
**sections
= NULL
;
4693 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4696 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4700 info
->user_phdrs
= !no_user_phdrs
;
4702 /* Size the relative relocations if DT_RELR is enabled. */
4703 if (info
->enable_dt_relr
4704 && need_layout
!= NULL
4705 && bed
->size_relative_relocs
4706 && !bed
->size_relative_relocs (info
, need_layout
))
4707 info
->callbacks
->einfo
4708 (_("%F%P: failed to size relative relocations\n"));
4711 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4715 struct elf_segment_map
*mfirst
;
4716 struct elf_segment_map
**pm
;
4719 unsigned int hdr_index
;
4720 bfd_vma maxpagesize
;
4722 bool phdr_in_segment
;
4725 unsigned int tls_count
= 0;
4726 asection
*first_tls
= NULL
;
4727 asection
*first_mbind
= NULL
;
4728 asection
*dynsec
, *eh_frame_hdr
;
4731 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4732 bfd_size_type phdr_size
; /* Octets/bytes. */
4733 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4735 /* Select the allocated sections, and sort them. */
4737 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4738 sections
= (asection
**) bfd_malloc (amt
);
4739 if (sections
== NULL
)
4742 /* Calculate top address, avoiding undefined behaviour of shift
4743 left operator when shift count is equal to size of type
4745 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4746 addr_mask
= (addr_mask
<< 1) + 1;
4749 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4751 if ((s
->flags
& SEC_ALLOC
) != 0)
4753 /* target_index is unused until bfd_elf_final_link
4754 starts output of section symbols. Use it to make
4756 s
->target_index
= i
;
4759 /* A wrapping section potentially clashes with header. */
4760 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4761 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4764 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4767 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4769 phdr_size
= elf_program_header_size (abfd
);
4770 if (phdr_size
== (bfd_size_type
) -1)
4771 phdr_size
= get_program_header_size (abfd
, info
);
4772 phdr_size
+= bed
->s
->sizeof_ehdr
;
4773 /* phdr_size is compared to LMA values which are in bytes. */
4776 maxpagesize
= info
->maxpagesize
;
4778 maxpagesize
= bed
->maxpagesize
;
4779 if (maxpagesize
== 0)
4781 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4783 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4784 >= (phdr_size
& (maxpagesize
- 1))))
4785 /* For compatibility with old scripts that may not be using
4786 SIZEOF_HEADERS, add headers when it looks like space has
4787 been left for them. */
4788 phdr_in_segment
= true;
4790 /* Build the mapping. */
4794 /* If we have a .interp section, then create a PT_PHDR segment for
4795 the program headers and a PT_INTERP segment for the .interp
4797 s
= bfd_get_section_by_name (abfd
, ".interp");
4798 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4800 amt
= sizeof (struct elf_segment_map
);
4801 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4805 m
->p_type
= PT_PHDR
;
4807 m
->p_flags_valid
= 1;
4808 m
->includes_phdrs
= 1;
4809 phdr_in_segment
= true;
4813 amt
= sizeof (struct elf_segment_map
);
4814 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4818 m
->p_type
= PT_INTERP
;
4826 /* Look through the sections. We put sections in the same program
4827 segment when the start of the second section can be placed within
4828 a few bytes of the end of the first section. */
4834 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4836 && (dynsec
->flags
& SEC_LOAD
) == 0)
4839 if ((abfd
->flags
& D_PAGED
) == 0)
4840 phdr_in_segment
= false;
4842 /* Deal with -Ttext or something similar such that the first section
4843 is not adjacent to the program headers. This is an
4844 approximation, since at this point we don't know exactly how many
4845 program headers we will need. */
4846 if (phdr_in_segment
&& count
> 0)
4848 bfd_vma phdr_lma
; /* Bytes. */
4849 bool separate_phdr
= false;
4851 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4853 && info
->separate_code
4854 && (sections
[0]->flags
& SEC_CODE
) != 0)
4856 /* If data sections should be separate from code and
4857 thus not executable, and the first section is
4858 executable then put the file and program headers in
4859 their own PT_LOAD. */
4860 separate_phdr
= true;
4861 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4862 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4864 /* The file and program headers are currently on the
4865 same page as the first section. Put them on the
4866 previous page if we can. */
4867 if (phdr_lma
>= maxpagesize
)
4868 phdr_lma
-= maxpagesize
;
4870 separate_phdr
= false;
4873 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4874 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4875 /* If file and program headers would be placed at the end
4876 of memory then it's probably better to omit them. */
4877 phdr_in_segment
= false;
4878 else if (phdr_lma
< wrap_to
)
4879 /* If a section wraps around to where we'll be placing
4880 file and program headers, then the headers will be
4882 phdr_in_segment
= false;
4883 else if (separate_phdr
)
4885 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4888 m
->p_paddr
= phdr_lma
* opb
;
4890 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4891 m
->p_paddr_valid
= 1;
4894 phdr_in_segment
= false;
4898 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4905 /* See if this section and the last one will fit in the same
4908 if (last_hdr
== NULL
)
4910 /* If we don't have a segment yet, then we don't need a new
4911 one (we build the last one after this loop). */
4912 new_segment
= false;
4914 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4916 /* If this section has a different relation between the
4917 virtual address and the load address, then we need a new
4921 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4922 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4924 /* If this section has a load address that makes it overlap
4925 the previous section, then we need a new segment. */
4928 else if ((abfd
->flags
& D_PAGED
) != 0
4929 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4930 == (hdr
->lma
& -maxpagesize
)))
4932 /* If we are demand paged then we can't map two disk
4933 pages onto the same memory page. */
4934 new_segment
= false;
4936 /* In the next test we have to be careful when last_hdr->lma is close
4937 to the end of the address space. If the aligned address wraps
4938 around to the start of the address space, then there are no more
4939 pages left in memory and it is OK to assume that the current
4940 section can be included in the current segment. */
4941 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4942 + maxpagesize
> last_hdr
->lma
)
4943 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4944 + maxpagesize
<= hdr
->lma
))
4946 /* If putting this section in this segment would force us to
4947 skip a page in the segment, then we need a new segment. */
4950 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4951 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4953 /* We don't want to put a loaded section after a
4954 nonloaded (ie. bss style) section in the same segment
4955 as that will force the non-loaded section to be loaded.
4956 Consider .tbss sections as loaded for this purpose. */
4959 else if ((abfd
->flags
& D_PAGED
) == 0)
4961 /* If the file is not demand paged, which means that we
4962 don't require the sections to be correctly aligned in the
4963 file, then there is no other reason for a new segment. */
4964 new_segment
= false;
4966 else if (info
!= NULL
4967 && info
->separate_code
4968 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4973 && (hdr
->flags
& SEC_READONLY
) == 0)
4975 /* We don't want to put a writable section in a read only
4981 /* Otherwise, we can use the same segment. */
4982 new_segment
= false;
4985 /* Allow interested parties a chance to override our decision. */
4986 if (last_hdr
!= NULL
4988 && info
->callbacks
->override_segment_assignment
!= NULL
)
4990 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4996 if ((hdr
->flags
& SEC_READONLY
) == 0)
4998 if ((hdr
->flags
& SEC_CODE
) != 0)
5001 /* .tbss sections effectively have zero size. */
5002 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5006 /* We need a new program segment. We must create a new program
5007 header holding all the sections from hdr_index until hdr. */
5009 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5016 if ((hdr
->flags
& SEC_READONLY
) == 0)
5021 if ((hdr
->flags
& SEC_CODE
) == 0)
5027 /* .tbss sections effectively have zero size. */
5028 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5030 phdr_in_segment
= false;
5033 /* Create a final PT_LOAD program segment, but not if it's just
5035 if (last_hdr
!= NULL
5036 && (i
- hdr_index
!= 1
5037 || !IS_TBSS (last_hdr
)))
5039 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5047 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5050 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5057 /* For each batch of consecutive loadable SHT_NOTE sections,
5058 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5059 because if we link together nonloadable .note sections and
5060 loadable .note sections, we will generate two .note sections
5061 in the output file. */
5062 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5064 if ((s
->flags
& SEC_LOAD
) != 0
5065 && elf_section_type (s
) == SHT_NOTE
)
5068 unsigned int alignment_power
= s
->alignment_power
;
5071 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5073 if (s2
->next
->alignment_power
== alignment_power
5074 && (s2
->next
->flags
& SEC_LOAD
) != 0
5075 && elf_section_type (s2
->next
) == SHT_NOTE
5076 && align_power (s2
->lma
+ s2
->size
/ opb
,
5083 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5084 amt
+= count
* sizeof (asection
*);
5085 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5089 m
->p_type
= PT_NOTE
;
5093 m
->sections
[m
->count
- count
--] = s
;
5094 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5097 m
->sections
[m
->count
- 1] = s
;
5098 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5102 if (s
->flags
& SEC_THREAD_LOCAL
)
5108 if (first_mbind
== NULL
5109 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5113 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5116 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5117 amt
+= tls_count
* sizeof (asection
*);
5118 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5123 m
->count
= tls_count
;
5124 /* Mandated PF_R. */
5126 m
->p_flags_valid
= 1;
5128 for (i
= 0; i
< tls_count
; ++i
)
5130 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5133 (_("%pB: TLS sections are not adjacent:"), abfd
);
5136 while (i
< tls_count
)
5138 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5140 _bfd_error_handler (_(" TLS: %pA"), s
);
5144 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5147 bfd_set_error (bfd_error_bad_value
);
5159 && (abfd
->flags
& D_PAGED
) != 0
5160 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5161 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5162 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5163 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5165 /* Mandated PF_R. */
5166 unsigned long p_flags
= PF_R
;
5167 if ((s
->flags
& SEC_READONLY
) == 0)
5169 if ((s
->flags
& SEC_CODE
) != 0)
5172 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5173 m
= bfd_zalloc (abfd
, amt
);
5177 m
->p_type
= (PT_GNU_MBIND_LO
5178 + elf_section_data (s
)->this_hdr
.sh_info
);
5180 m
->p_flags_valid
= 1;
5182 m
->p_flags
= p_flags
;
5188 s
= bfd_get_section_by_name (abfd
,
5189 NOTE_GNU_PROPERTY_SECTION_NAME
);
5190 if (s
!= NULL
&& s
->size
!= 0)
5192 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5193 m
= bfd_zalloc (abfd
, amt
);
5197 m
->p_type
= PT_GNU_PROPERTY
;
5199 m
->p_flags_valid
= 1;
5206 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5208 eh_frame_hdr
= elf_eh_frame_hdr (info
);
5209 if (eh_frame_hdr
!= NULL
5210 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5212 amt
= sizeof (struct elf_segment_map
);
5213 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5217 m
->p_type
= PT_GNU_EH_FRAME
;
5219 m
->sections
[0] = eh_frame_hdr
->output_section
;
5225 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5227 sframe
= elf_sframe (abfd
);
5229 && (sframe
->output_section
->flags
& SEC_LOAD
) != 0
5230 && sframe
->size
!= 0)
5232 amt
= sizeof (struct elf_segment_map
);
5233 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5237 m
->p_type
= PT_GNU_SFRAME
;
5239 m
->sections
[0] = sframe
->output_section
;
5245 if (elf_stack_flags (abfd
))
5247 amt
= sizeof (struct elf_segment_map
);
5248 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5252 m
->p_type
= PT_GNU_STACK
;
5253 m
->p_flags
= elf_stack_flags (abfd
);
5254 m
->p_align
= bed
->stack_align
;
5255 m
->p_flags_valid
= 1;
5256 m
->p_align_valid
= m
->p_align
!= 0;
5257 if (info
->stacksize
> 0)
5259 m
->p_size
= info
->stacksize
;
5260 m
->p_size_valid
= 1;
5267 if (info
!= NULL
&& info
->relro
)
5269 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5271 if (m
->p_type
== PT_LOAD
5273 && m
->sections
[0]->vma
>= info
->relro_start
5274 && m
->sections
[0]->vma
< info
->relro_end
)
5277 while (--i
!= (unsigned) -1)
5279 if (m
->sections
[i
]->size
> 0
5280 && (m
->sections
[i
]->flags
& SEC_LOAD
) != 0
5281 && (m
->sections
[i
]->flags
& SEC_HAS_CONTENTS
) != 0)
5285 if (i
!= (unsigned) -1)
5290 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5293 amt
= sizeof (struct elf_segment_map
);
5294 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5298 m
->p_type
= PT_GNU_RELRO
;
5305 elf_seg_map (abfd
) = mfirst
;
5308 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5311 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5313 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5322 /* Sort sections by address. */
5325 elf_sort_sections (const void *arg1
, const void *arg2
)
5327 const asection
*sec1
= *(const asection
**) arg1
;
5328 const asection
*sec2
= *(const asection
**) arg2
;
5329 bfd_size_type size1
, size2
;
5331 /* Sort by LMA first, since this is the address used to
5332 place the section into a segment. */
5333 if (sec1
->lma
< sec2
->lma
)
5335 else if (sec1
->lma
> sec2
->lma
)
5338 /* Then sort by VMA. Normally the LMA and the VMA will be
5339 the same, and this will do nothing. */
5340 if (sec1
->vma
< sec2
->vma
)
5342 else if (sec1
->vma
> sec2
->vma
)
5345 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5347 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5355 else if (TOEND (sec2
))
5360 /* Sort by size, to put zero sized sections
5361 before others at the same address. */
5363 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5364 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5371 return sec1
->target_index
- sec2
->target_index
;
5374 /* This qsort comparison functions sorts PT_LOAD segments first and
5375 by p_paddr, for assign_file_positions_for_load_sections. */
5378 elf_sort_segments (const void *arg1
, const void *arg2
)
5380 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5381 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5383 if (m1
->p_type
!= m2
->p_type
)
5385 if (m1
->p_type
== PT_NULL
)
5387 if (m2
->p_type
== PT_NULL
)
5389 return m1
->p_type
< m2
->p_type
? -1 : 1;
5391 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5392 return m1
->includes_filehdr
? -1 : 1;
5393 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5394 return m1
->no_sort_lma
? -1 : 1;
5395 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5397 bfd_vma lma1
, lma2
; /* Octets. */
5399 if (m1
->p_paddr_valid
)
5401 else if (m1
->count
!= 0)
5403 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5405 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5408 if (m2
->p_paddr_valid
)
5410 else if (m2
->count
!= 0)
5412 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5414 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5417 return lma1
< lma2
? -1 : 1;
5419 if (m1
->idx
!= m2
->idx
)
5420 return m1
->idx
< m2
->idx
? -1 : 1;
5424 /* Ian Lance Taylor writes:
5426 We shouldn't be using % with a negative signed number. That's just
5427 not good. We have to make sure either that the number is not
5428 negative, or that the number has an unsigned type. When the types
5429 are all the same size they wind up as unsigned. When file_ptr is a
5430 larger signed type, the arithmetic winds up as signed long long,
5433 What we're trying to say here is something like ``increase OFF by
5434 the least amount that will cause it to be equal to the VMA modulo
5436 /* In other words, something like:
5438 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5439 off_offset = off % bed->maxpagesize;
5440 if (vma_offset < off_offset)
5441 adjustment = vma_offset + bed->maxpagesize - off_offset;
5443 adjustment = vma_offset - off_offset;
5445 which can be collapsed into the expression below. */
5448 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5450 /* PR binutils/16199: Handle an alignment of zero. */
5451 if (maxpagesize
== 0)
5453 return ((vma
- off
) % maxpagesize
);
5457 print_segment_map (const struct elf_segment_map
*m
)
5460 const char *pt
= get_segment_type (m
->p_type
);
5465 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5466 sprintf (buf
, "LOPROC+%7.7x",
5467 (unsigned int) (m
->p_type
- PT_LOPROC
));
5468 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5469 sprintf (buf
, "LOOS+%7.7x",
5470 (unsigned int) (m
->p_type
- PT_LOOS
));
5472 snprintf (buf
, sizeof (buf
), "%8.8x",
5473 (unsigned int) m
->p_type
);
5477 fprintf (stderr
, "%s:", pt
);
5478 for (j
= 0; j
< m
->count
; j
++)
5479 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5485 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5490 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5492 buf
= bfd_zmalloc (len
);
5495 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5500 /* Assign file positions to the sections based on the mapping from
5501 sections to segments. This function also sets up some fields in
5505 assign_file_positions_for_load_sections (bfd
*abfd
,
5506 struct bfd_link_info
*link_info
)
5508 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5509 struct elf_segment_map
*m
;
5510 struct elf_segment_map
*phdr_load_seg
;
5511 Elf_Internal_Phdr
*phdrs
;
5512 Elf_Internal_Phdr
*p
;
5513 file_ptr off
; /* Octets. */
5514 bfd_size_type maxpagesize
;
5515 unsigned int alloc
, actual
;
5517 struct elf_segment_map
**sorted_seg_map
;
5518 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5520 if (link_info
== NULL
5521 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
, NULL
))
5525 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5530 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5531 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5535 /* PR binutils/12467. */
5536 elf_elfheader (abfd
)->e_phoff
= 0;
5537 elf_elfheader (abfd
)->e_phentsize
= 0;
5540 elf_elfheader (abfd
)->e_phnum
= alloc
;
5542 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5545 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5549 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5550 BFD_ASSERT (elf_program_header_size (abfd
)
5551 == actual
* bed
->s
->sizeof_phdr
);
5552 BFD_ASSERT (actual
>= alloc
);
5557 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5561 /* We're writing the size in elf_program_header_size (abfd),
5562 see assign_file_positions_except_relocs, so make sure we have
5563 that amount allocated, with trailing space cleared.
5564 The variable alloc contains the computed need, while
5565 elf_program_header_size (abfd) contains the size used for the
5567 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5568 where the layout is forced to according to a larger size in the
5569 last iterations for the testcase ld-elf/header. */
5570 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5571 + alloc
* sizeof (*sorted_seg_map
)));
5572 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5573 elf_tdata (abfd
)->phdr
= phdrs
;
5577 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5579 sorted_seg_map
[j
] = m
;
5580 /* If elf_segment_map is not from map_sections_to_segments, the
5581 sections may not be correctly ordered. NOTE: sorting should
5582 not be done to the PT_NOTE section of a corefile, which may
5583 contain several pseudo-sections artificially created by bfd.
5584 Sorting these pseudo-sections breaks things badly. */
5586 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5587 && m
->p_type
== PT_NOTE
))
5589 for (i
= 0; i
< m
->count
; i
++)
5590 m
->sections
[i
]->target_index
= i
;
5591 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5596 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5600 if ((abfd
->flags
& D_PAGED
) != 0)
5602 if (link_info
!= NULL
)
5603 maxpagesize
= link_info
->maxpagesize
;
5605 maxpagesize
= bed
->maxpagesize
;
5608 /* Sections must map to file offsets past the ELF file header. */
5609 off
= bed
->s
->sizeof_ehdr
;
5610 /* And if one of the PT_LOAD headers doesn't include the program
5611 headers then we'll be mapping program headers in the usual
5612 position after the ELF file header. */
5613 phdr_load_seg
= NULL
;
5614 for (j
= 0; j
< alloc
; j
++)
5616 m
= sorted_seg_map
[j
];
5617 if (m
->p_type
!= PT_LOAD
)
5619 if (m
->includes_phdrs
)
5625 if (phdr_load_seg
== NULL
)
5626 off
+= actual
* bed
->s
->sizeof_phdr
;
5628 for (j
= 0; j
< alloc
; j
++)
5631 bfd_vma off_adjust
; /* Octets. */
5633 bfd_size_type p_align
;
5636 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5637 number of sections with contents contributing to both p_filesz
5638 and p_memsz, followed by a number of sections with no contents
5639 that just contribute to p_memsz. In this loop, OFF tracks next
5640 available file offset for PT_LOAD and PT_NOTE segments. */
5641 m
= sorted_seg_map
[j
];
5643 p
->p_type
= m
->p_type
;
5644 p
->p_flags
= m
->p_flags
;
5645 p_align
= bed
->p_align
;
5649 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5651 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5653 if (m
->p_paddr_valid
)
5654 p
->p_paddr
= m
->p_paddr
;
5655 else if (m
->count
== 0)
5658 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5660 if (p
->p_type
== PT_LOAD
5661 && (abfd
->flags
& D_PAGED
) != 0)
5663 /* p_align in demand paged PT_LOAD segments effectively stores
5664 the maximum page size. When copying an executable with
5665 objcopy, we set m->p_align from the input file. Use this
5666 value for maxpagesize rather than bed->maxpagesize, which
5667 may be different. Note that we use maxpagesize for PT_TLS
5668 segment alignment later in this function, so we are relying
5669 on at least one PT_LOAD segment appearing before a PT_TLS
5671 if (m
->p_align_valid
)
5672 maxpagesize
= m
->p_align
;
5673 else if (p_align
!= 0
5674 && (link_info
== NULL
5675 || !link_info
->maxpagesize_is_set
))
5676 /* Set p_align to the default p_align value while laying
5677 out segments aligning to the maximum page size or the
5678 largest section alignment. The run-time loader can
5679 align segments to the default p_align value or the
5680 maximum page size, depending on system page size. */
5683 p
->p_align
= maxpagesize
;
5685 else if (m
->p_align_valid
)
5686 p
->p_align
= m
->p_align
;
5687 else if (m
->count
== 0)
5688 p
->p_align
= 1 << bed
->s
->log_file_align
;
5690 if (m
== phdr_load_seg
)
5692 if (!m
->includes_filehdr
)
5694 off
+= actual
* bed
->s
->sizeof_phdr
;
5697 no_contents
= false;
5699 if (p
->p_type
== PT_LOAD
5702 bfd_size_type align
; /* Bytes. */
5703 unsigned int align_power
= 0;
5705 if (m
->p_align_valid
)
5709 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5711 unsigned int secalign
;
5713 secalign
= bfd_section_alignment (*secpp
);
5714 if (secalign
> align_power
)
5715 align_power
= secalign
;
5717 align
= (bfd_size_type
) 1 << align_power
;
5718 if (align
< maxpagesize
)
5720 /* If a section requires alignment higher than the
5721 default p_align value, don't set p_align to the
5722 default p_align value. */
5723 if (align
> p_align
)
5725 align
= maxpagesize
;
5729 /* If a section requires alignment higher than the
5730 maximum page size, set p_align to the section
5737 for (i
= 0; i
< m
->count
; i
++)
5738 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5739 /* If we aren't making room for this section, then
5740 it must be SHT_NOBITS regardless of what we've
5741 set via struct bfd_elf_special_section. */
5742 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5744 /* Find out whether this segment contains any loadable
5747 for (i
= 0; i
< m
->count
; i
++)
5748 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5750 no_contents
= false;
5754 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5756 /* Broken hardware and/or kernel require that files do not
5757 map the same page with different permissions on some hppa
5760 && (abfd
->flags
& D_PAGED
) != 0
5761 && bed
->no_page_alias
5762 && (off
& (maxpagesize
- 1)) != 0
5763 && ((off
& -maxpagesize
)
5764 == ((off
+ off_adjust
) & -maxpagesize
)))
5765 off_adjust
+= maxpagesize
;
5769 /* We shouldn't need to align the segment on disk since
5770 the segment doesn't need file space, but the gABI
5771 arguably requires the alignment and glibc ld.so
5772 checks it. So to comply with the alignment
5773 requirement but not waste file space, we adjust
5774 p_offset for just this segment. (OFF_ADJUST is
5775 subtracted from OFF later.) This may put p_offset
5776 past the end of file, but that shouldn't matter. */
5781 /* Make sure the .dynamic section is the first section in the
5782 PT_DYNAMIC segment. */
5783 else if (p
->p_type
== PT_DYNAMIC
5785 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5788 (_("%pB: The first section in the PT_DYNAMIC segment"
5789 " is not the .dynamic section"),
5791 bfd_set_error (bfd_error_bad_value
);
5794 /* Set the note section type to SHT_NOTE. */
5795 else if (p
->p_type
== PT_NOTE
)
5796 for (i
= 0; i
< m
->count
; i
++)
5797 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5799 if (m
->includes_filehdr
)
5801 if (!m
->p_flags_valid
)
5803 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5804 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5805 if (p
->p_type
== PT_LOAD
)
5809 if (p
->p_vaddr
< (bfd_vma
) off
5810 || (!m
->p_paddr_valid
5811 && p
->p_paddr
< (bfd_vma
) off
))
5814 (_("%pB: not enough room for program headers,"
5815 " try linking with -N"),
5817 bfd_set_error (bfd_error_bad_value
);
5821 if (!m
->p_paddr_valid
)
5825 else if (sorted_seg_map
[0]->includes_filehdr
)
5827 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5828 p
->p_vaddr
= filehdr
->p_vaddr
;
5829 if (!m
->p_paddr_valid
)
5830 p
->p_paddr
= filehdr
->p_paddr
;
5834 if (m
->includes_phdrs
)
5836 if (!m
->p_flags_valid
)
5838 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5839 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5840 if (!m
->includes_filehdr
)
5842 if (p
->p_type
== PT_LOAD
)
5844 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5847 p
->p_vaddr
-= off
- p
->p_offset
;
5848 if (!m
->p_paddr_valid
)
5849 p
->p_paddr
-= off
- p
->p_offset
;
5852 else if (phdr_load_seg
!= NULL
)
5854 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5855 bfd_vma phdr_off
= 0; /* Octets. */
5856 if (phdr_load_seg
->includes_filehdr
)
5857 phdr_off
= bed
->s
->sizeof_ehdr
;
5858 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5859 if (!m
->p_paddr_valid
)
5860 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5861 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5864 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5868 if (p
->p_type
== PT_LOAD
5869 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5871 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5876 /* Put meaningless p_offset for PT_LOAD segments
5877 without file contents somewhere within the first
5878 page, in an attempt to not point past EOF. */
5879 bfd_size_type align
= maxpagesize
;
5880 if (align
< p
->p_align
)
5884 p
->p_offset
= off
% align
;
5889 file_ptr adjust
; /* Octets. */
5891 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5893 p
->p_filesz
+= adjust
;
5894 p
->p_memsz
+= adjust
;
5898 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5899 maps. Set filepos for sections in PT_LOAD segments, and in
5900 core files, for sections in PT_NOTE segments.
5901 assign_file_positions_for_non_load_sections will set filepos
5902 for other sections and update p_filesz for other segments. */
5903 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5906 bfd_size_type align
;
5907 Elf_Internal_Shdr
*this_hdr
;
5910 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5911 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5913 if ((p
->p_type
== PT_LOAD
5914 || p
->p_type
== PT_TLS
)
5915 && (this_hdr
->sh_type
!= SHT_NOBITS
5916 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5917 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5918 || p
->p_type
== PT_TLS
))))
5920 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5921 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5922 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5923 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5927 || p_end
< p_start
))
5930 /* xgettext:c-format */
5931 (_("%pB: section %pA lma %#" PRIx64
5932 " adjusted to %#" PRIx64
),
5933 abfd
, sec
, (uint64_t) s_start
/ opb
,
5934 (uint64_t) p_end
/ opb
);
5936 sec
->lma
= p_end
/ opb
;
5938 p
->p_memsz
+= adjust
;
5940 if (p
->p_type
== PT_LOAD
)
5942 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5945 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5947 /* We have a PROGBITS section following NOBITS ones.
5948 Allocate file space for the NOBITS section(s) and
5950 adjust
= p
->p_memsz
- p
->p_filesz
;
5951 if (!write_zeros (abfd
, off
, adjust
))
5955 /* We only adjust sh_offset in SHT_NOBITS sections
5956 as would seem proper for their address when the
5957 section is first in the segment. sh_offset
5958 doesn't really have any significance for
5959 SHT_NOBITS anyway, apart from a notional position
5960 relative to other sections. Historically we
5961 didn't bother with adjusting sh_offset and some
5962 programs depend on it not being adjusted. See
5963 pr12921 and pr25662. */
5964 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
5967 if (this_hdr
->sh_type
== SHT_NOBITS
)
5968 off_adjust
+= adjust
;
5971 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5972 p
->p_filesz
+= adjust
;
5975 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5977 /* The section at i == 0 is the one that actually contains
5981 this_hdr
->sh_offset
= sec
->filepos
= off
;
5982 off
+= this_hdr
->sh_size
;
5983 p
->p_filesz
= this_hdr
->sh_size
;
5989 /* The rest are fake sections that shouldn't be written. */
5998 if (p
->p_type
== PT_LOAD
)
6000 this_hdr
->sh_offset
= sec
->filepos
= off
;
6001 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6002 off
+= this_hdr
->sh_size
;
6004 else if (this_hdr
->sh_type
== SHT_NOBITS
6005 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
6006 && this_hdr
->sh_offset
== 0)
6008 /* This is a .tbss section that didn't get a PT_LOAD.
6009 (See _bfd_elf_map_sections_to_segments "Create a
6010 final PT_LOAD".) Set sh_offset to the value it
6011 would have if we had created a zero p_filesz and
6012 p_memsz PT_LOAD header for the section. This
6013 also makes the PT_TLS header have the same
6015 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
6017 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
6020 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6022 p
->p_filesz
+= this_hdr
->sh_size
;
6023 /* A load section without SHF_ALLOC is something like
6024 a note section in a PT_NOTE segment. These take
6025 file space but are not loaded into memory. */
6026 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6027 p
->p_memsz
+= this_hdr
->sh_size
;
6029 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6031 if (p
->p_type
== PT_TLS
)
6032 p
->p_memsz
+= this_hdr
->sh_size
;
6034 /* .tbss is special. It doesn't contribute to p_memsz of
6036 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
6037 p
->p_memsz
+= this_hdr
->sh_size
;
6040 if (align
> p
->p_align
6041 && !m
->p_align_valid
6042 && (p
->p_type
!= PT_LOAD
6043 || (abfd
->flags
& D_PAGED
) == 0))
6047 if (!m
->p_flags_valid
)
6050 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
6052 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
6059 /* PR ld/20815 - Check that the program header segment, if
6060 present, will be loaded into memory. */
6061 if (p
->p_type
== PT_PHDR
6062 && phdr_load_seg
== NULL
6063 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
6064 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
6066 /* The fix for this error is usually to edit the linker script being
6067 used and set up the program headers manually. Either that or
6068 leave room for the headers at the start of the SECTIONS. */
6069 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6070 " by LOAD segment"),
6072 if (link_info
== NULL
)
6074 /* Arrange for the linker to exit with an error, deleting
6075 the output file unless --noinhibit-exec is given. */
6076 link_info
->callbacks
->info ("%X");
6079 /* Check that all sections are in a PT_LOAD segment.
6080 Don't check funky gdb generated core files. */
6081 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
6083 bool check_vma
= true;
6085 for (i
= 1; i
< m
->count
; i
++)
6086 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
6087 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
6088 ->this_hdr
), p
) != 0
6089 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
6090 ->this_hdr
), p
) != 0)
6092 /* Looks like we have overlays packed into the segment. */
6097 for (i
= 0; i
< m
->count
; i
++)
6099 Elf_Internal_Shdr
*this_hdr
;
6102 sec
= m
->sections
[i
];
6103 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6104 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6105 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6108 /* xgettext:c-format */
6109 (_("%pB: section `%pA' can't be allocated in segment %d"),
6111 print_segment_map (m
);
6116 p
->p_align
= p_align
;
6120 elf_next_file_pos (abfd
) = off
;
6122 if (link_info
!= NULL
6123 && phdr_load_seg
!= NULL
6124 && phdr_load_seg
->includes_filehdr
)
6126 /* There is a segment that contains both the file headers and the
6127 program headers, so provide a symbol __ehdr_start pointing there.
6128 A program can use this to examine itself robustly. */
6130 struct elf_link_hash_entry
*hash
6131 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6132 false, false, true);
6133 /* If the symbol was referenced and not defined, define it. */
6135 && (hash
->root
.type
== bfd_link_hash_new
6136 || hash
->root
.type
== bfd_link_hash_undefined
6137 || hash
->root
.type
== bfd_link_hash_undefweak
6138 || hash
->root
.type
== bfd_link_hash_common
))
6141 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6143 if (phdr_load_seg
->count
!= 0)
6144 /* The segment contains sections, so use the first one. */
6145 s
= phdr_load_seg
->sections
[0];
6147 /* Use the first (i.e. lowest-addressed) section in any segment. */
6148 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6149 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6157 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6158 hash
->root
.u
.def
.section
= s
;
6162 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6163 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6166 hash
->root
.type
= bfd_link_hash_defined
;
6167 hash
->def_regular
= 1;
6175 /* Determine if a bfd is a debuginfo file. Unfortunately there
6176 is no defined method for detecting such files, so we have to
6177 use heuristics instead. */
6180 is_debuginfo_file (bfd
*abfd
)
6182 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6185 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6186 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6187 Elf_Internal_Shdr
**headerp
;
6189 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6191 Elf_Internal_Shdr
*header
= * headerp
;
6193 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6194 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6195 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6196 && header
->sh_type
!= SHT_NOBITS
6197 && header
->sh_type
!= SHT_NOTE
)
6204 /* Assign file positions for other sections, except for compressed debug
6205 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6208 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6209 struct bfd_link_info
*link_info
)
6211 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6212 Elf_Internal_Shdr
**i_shdrpp
;
6213 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6214 Elf_Internal_Phdr
*phdrs
;
6215 Elf_Internal_Phdr
*p
;
6216 struct elf_segment_map
*m
;
6218 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6219 bfd_vma maxpagesize
;
6221 if (link_info
!= NULL
)
6222 maxpagesize
= link_info
->maxpagesize
;
6224 maxpagesize
= bed
->maxpagesize
;
6225 i_shdrpp
= elf_elfsections (abfd
);
6226 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6227 off
= elf_next_file_pos (abfd
);
6228 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6230 Elf_Internal_Shdr
*hdr
;
6234 if (hdr
->bfd_section
!= NULL
6235 && (hdr
->bfd_section
->filepos
!= 0
6236 || (hdr
->sh_type
== SHT_NOBITS
6237 && hdr
->contents
== NULL
)))
6238 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6239 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6241 if (hdr
->sh_size
!= 0
6242 /* PR 24717 - debuginfo files are known to be not strictly
6243 compliant with the ELF standard. In particular they often
6244 have .note.gnu.property sections that are outside of any
6245 loadable segment. This is not a problem for such files,
6246 so do not warn about them. */
6247 && ! is_debuginfo_file (abfd
))
6249 /* xgettext:c-format */
6250 (_("%pB: warning: allocated section `%s' not in segment"),
6252 (hdr
->bfd_section
== NULL
6254 : hdr
->bfd_section
->name
));
6255 /* We don't need to page align empty sections. */
6256 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6257 align
= maxpagesize
;
6259 align
= hdr
->sh_addralign
& -hdr
->sh_addralign
;
6260 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
, align
);
6261 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6264 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6265 && hdr
->bfd_section
== NULL
)
6266 /* We don't know the offset of these sections yet:
6267 their size has not been decided. */
6268 || (hdr
->bfd_section
!= NULL
6269 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6270 || (bfd_section_is_ctf (hdr
->bfd_section
)
6271 && abfd
->is_linker_output
)))
6272 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6273 || (elf_symtab_shndx_list (abfd
) != NULL
6274 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6275 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6276 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6277 hdr
->sh_offset
= -1;
6279 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6281 elf_next_file_pos (abfd
) = off
;
6283 /* Now that we have set the section file positions, we can set up
6284 the file positions for the non PT_LOAD segments. */
6285 phdrs
= elf_tdata (abfd
)->phdr
;
6286 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6288 if (p
->p_type
== PT_GNU_RELRO
)
6290 bfd_vma start
, end
; /* Bytes. */
6293 if (link_info
!= NULL
)
6295 /* During linking the range of the RELRO segment is passed
6296 in link_info. Note that there may be padding between
6297 relro_start and the first RELRO section. */
6298 start
= link_info
->relro_start
;
6299 end
= link_info
->relro_end
;
6301 else if (m
->count
!= 0)
6303 if (!m
->p_size_valid
)
6305 start
= m
->sections
[0]->vma
;
6306 end
= start
+ m
->p_size
/ opb
;
6317 struct elf_segment_map
*lm
;
6318 const Elf_Internal_Phdr
*lp
;
6321 /* Find a LOAD segment containing a section in the RELRO
6323 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6325 lm
= lm
->next
, lp
++)
6327 if (lp
->p_type
== PT_LOAD
6329 && (lm
->sections
[lm
->count
- 1]->vma
6330 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6331 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6333 && lm
->sections
[0]->vma
< end
)
6339 /* Find the section starting the RELRO segment. */
6340 for (i
= 0; i
< lm
->count
; i
++)
6342 asection
*s
= lm
->sections
[i
];
6351 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6352 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6353 p
->p_offset
= lm
->sections
[i
]->filepos
;
6354 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6355 p
->p_filesz
= p
->p_memsz
;
6357 /* The RELRO segment typically ends a few bytes
6358 into .got.plt but other layouts are possible.
6359 In cases where the end does not match any
6360 loaded section (for instance is in file
6361 padding), trim p_filesz back to correspond to
6362 the end of loaded section contents. */
6363 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6364 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6366 /* Preserve the alignment and flags if they are
6367 valid. The gold linker generates RW/4 for
6368 the PT_GNU_RELRO section. It is better for
6369 objcopy/strip to honor these attributes
6370 otherwise gdb will choke when using separate
6372 if (!m
->p_align_valid
)
6374 if (!m
->p_flags_valid
)
6383 if (link_info
!= NULL
)
6385 (_("%pB: warning: unable to allocate any sections"
6386 " to PT_GNU_RELRO segment"),
6388 memset (p
, 0, sizeof *p
);
6391 else if (p
->p_type
== PT_GNU_STACK
)
6393 if (m
->p_size_valid
)
6394 p
->p_memsz
= m
->p_size
;
6396 else if (m
->count
!= 0)
6400 if (p
->p_type
!= PT_LOAD
6401 && (p
->p_type
!= PT_NOTE
6402 || bfd_get_format (abfd
) != bfd_core
))
6404 /* A user specified segment layout may include a PHDR
6405 segment that overlaps with a LOAD segment... */
6406 if (p
->p_type
== PT_PHDR
)
6412 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6414 /* PR 17512: file: 2195325e. */
6416 (_("%pB: error: non-load segment %d includes file header "
6417 "and/or program header"),
6418 abfd
, (int) (p
- phdrs
));
6423 p
->p_offset
= m
->sections
[0]->filepos
;
6424 for (i
= m
->count
; i
-- != 0;)
6426 asection
*sect
= m
->sections
[i
];
6427 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6428 if (hdr
->sh_type
!= SHT_NOBITS
)
6430 p
->p_filesz
= sect
->filepos
- p
->p_offset
+ hdr
->sh_size
;
6431 /* NB: p_memsz of the loadable PT_NOTE segment
6432 should be the same as p_filesz. */
6433 if (p
->p_type
== PT_NOTE
6434 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
6435 p
->p_memsz
= p
->p_filesz
;
6446 static elf_section_list
*
6447 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6449 for (;list
!= NULL
; list
= list
->next
)
6455 /* Work out the file positions of all the sections. This is called by
6456 _bfd_elf_compute_section_file_positions. All the section sizes and
6457 VMAs must be known before this is called.
6459 Reloc sections come in two flavours: Those processed specially as
6460 "side-channel" data attached to a section to which they apply, and
6461 those that bfd doesn't process as relocations. The latter sort are
6462 stored in a normal bfd section by bfd_section_from_shdr. We don't
6463 consider the former sort here, unless they form part of the loadable
6464 image. Reloc sections not assigned here (and compressed debugging
6465 sections and CTF sections which nothing else in the file can rely
6466 upon) will be handled later by assign_file_positions_for_relocs.
6468 We also don't set the positions of the .symtab and .strtab here. */
6471 assign_file_positions_except_relocs (bfd
*abfd
,
6472 struct bfd_link_info
*link_info
)
6474 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6475 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6476 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6479 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6480 && bfd_get_format (abfd
) != bfd_core
)
6482 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6483 unsigned int num_sec
= elf_numsections (abfd
);
6484 Elf_Internal_Shdr
**hdrpp
;
6488 /* Start after the ELF header. */
6489 off
= i_ehdrp
->e_ehsize
;
6491 /* We are not creating an executable, which means that we are
6492 not creating a program header, and that the actual order of
6493 the sections in the file is unimportant. */
6494 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6496 Elf_Internal_Shdr
*hdr
;
6499 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6500 && hdr
->bfd_section
== NULL
)
6501 /* Do not assign offsets for these sections yet: we don't know
6503 || (hdr
->bfd_section
!= NULL
6504 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6505 || (bfd_section_is_ctf (hdr
->bfd_section
)
6506 && abfd
->is_linker_output
)))
6507 || i
== elf_onesymtab (abfd
)
6508 || (elf_symtab_shndx_list (abfd
) != NULL
6509 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6510 || i
== elf_strtab_sec (abfd
)
6511 || i
== elf_shstrtab_sec (abfd
))
6513 hdr
->sh_offset
= -1;
6516 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6519 elf_next_file_pos (abfd
) = off
;
6520 elf_program_header_size (abfd
) = 0;
6524 /* Assign file positions for the loaded sections based on the
6525 assignment of sections to segments. */
6526 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6529 /* And for non-load sections. */
6530 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6534 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6537 /* Write out the program headers. */
6538 alloc
= i_ehdrp
->e_phnum
;
6541 if (link_info
!= NULL
&& ! link_info
->no_warn_rwx_segments
)
6543 /* Memory resident segments with non-zero size and RWX
6544 permissions are a security risk, so we generate a warning
6545 here if we are creating any. */
6548 for (i
= 0; i
< alloc
; i
++)
6550 const Elf_Internal_Phdr
* phdr
= tdata
->phdr
+ i
;
6552 if (phdr
->p_memsz
== 0)
6555 if (phdr
->p_type
== PT_TLS
&& (phdr
->p_flags
& PF_X
))
6556 _bfd_error_handler (_("warning: %pB has a TLS segment"
6557 " with execute permission"),
6559 else if (phdr
->p_type
== PT_LOAD
6560 && ((phdr
->p_flags
& (PF_R
| PF_W
| PF_X
))
6561 == (PF_R
| PF_W
| PF_X
)))
6562 _bfd_error_handler (_("warning: %pB has a LOAD segment"
6563 " with RWX permissions"),
6568 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6569 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6577 _bfd_elf_init_file_header (bfd
*abfd
,
6578 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6580 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6581 struct elf_strtab_hash
*shstrtab
;
6582 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6584 i_ehdrp
= elf_elfheader (abfd
);
6586 shstrtab
= _bfd_elf_strtab_init ();
6587 if (shstrtab
== NULL
)
6590 elf_shstrtab (abfd
) = shstrtab
;
6592 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6593 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6594 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6595 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6597 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6598 i_ehdrp
->e_ident
[EI_DATA
] =
6599 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6600 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6602 if ((abfd
->flags
& DYNAMIC
) != 0)
6603 i_ehdrp
->e_type
= ET_DYN
;
6604 else if ((abfd
->flags
& EXEC_P
) != 0)
6605 i_ehdrp
->e_type
= ET_EXEC
;
6606 else if (bfd_get_format (abfd
) == bfd_core
)
6607 i_ehdrp
->e_type
= ET_CORE
;
6609 i_ehdrp
->e_type
= ET_REL
;
6611 switch (bfd_get_arch (abfd
))
6613 case bfd_arch_unknown
:
6614 i_ehdrp
->e_machine
= EM_NONE
;
6617 /* There used to be a long list of cases here, each one setting
6618 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6619 in the corresponding bfd definition. To avoid duplication,
6620 the switch was removed. Machines that need special handling
6621 can generally do it in elf_backend_final_write_processing(),
6622 unless they need the information earlier than the final write.
6623 Such need can generally be supplied by replacing the tests for
6624 e_machine with the conditions used to determine it. */
6626 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6629 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6630 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6632 /* No program header, for now. */
6633 i_ehdrp
->e_phoff
= 0;
6634 i_ehdrp
->e_phentsize
= 0;
6635 i_ehdrp
->e_phnum
= 0;
6637 /* Each bfd section is section header entry. */
6638 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6639 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6641 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6642 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
6643 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6644 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
6645 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6646 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
6647 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6648 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6649 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6655 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6657 FIXME: We used to have code here to sort the PT_LOAD segments into
6658 ascending order, as per the ELF spec. But this breaks some programs,
6659 including the Linux kernel. But really either the spec should be
6660 changed or the programs updated. */
6663 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6665 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6667 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6668 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6669 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6670 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6671 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6673 /* Find the lowest p_vaddr in PT_LOAD segments. */
6674 bfd_vma p_vaddr
= (bfd_vma
) -1;
6675 for (; segment
< end_segment
; segment
++)
6676 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6677 p_vaddr
= segment
->p_vaddr
;
6679 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6680 segments is non-zero. */
6682 i_ehdrp
->e_type
= ET_EXEC
;
6687 /* Assign file positions for all the reloc sections which are not part
6688 of the loadable file image, and the file position of section headers. */
6691 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6694 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6695 Elf_Internal_Shdr
*shdrp
;
6696 Elf_Internal_Ehdr
*i_ehdrp
;
6697 const struct elf_backend_data
*bed
;
6699 off
= elf_next_file_pos (abfd
);
6701 shdrpp
= elf_elfsections (abfd
);
6702 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6703 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6706 if (shdrp
->sh_offset
== -1)
6708 asection
*sec
= shdrp
->bfd_section
;
6709 bool is_rel
= (shdrp
->sh_type
== SHT_REL
6710 || shdrp
->sh_type
== SHT_RELA
);
6711 bool is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6714 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6716 if (!is_rel
&& !is_ctf
)
6718 const char *name
= sec
->name
;
6719 struct bfd_elf_section_data
*d
;
6721 /* Compress DWARF debug sections. */
6722 if (!bfd_compress_section (abfd
, sec
,
6726 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6727 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0
6730 /* If section is compressed with zlib-gnu, convert
6731 section name from .debug_* to .zdebug_*. */
6733 = convert_debug_to_zdebug (abfd
, name
);
6734 if (new_name
== NULL
)
6738 /* Add section name to section name section. */
6739 if (shdrp
->sh_name
!= (unsigned int) -1)
6742 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6744 d
= elf_section_data (sec
);
6746 /* Add reloc section name to section name section. */
6748 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rel
.hdr
,
6752 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rela
.hdr
,
6756 /* Update section size and contents. */
6757 shdrp
->sh_size
= sec
->size
;
6758 shdrp
->contents
= sec
->contents
;
6759 shdrp
->bfd_section
->contents
= NULL
;
6763 /* Update section size and contents. */
6764 shdrp
->sh_size
= sec
->size
;
6765 shdrp
->contents
= sec
->contents
;
6768 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
,
6774 /* Place section name section after DWARF debug sections have been
6776 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6777 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6778 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6779 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
6781 /* Place the section headers. */
6782 i_ehdrp
= elf_elfheader (abfd
);
6783 bed
= get_elf_backend_data (abfd
);
6784 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6785 i_ehdrp
->e_shoff
= off
;
6786 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6787 elf_next_file_pos (abfd
) = off
;
6793 _bfd_elf_write_object_contents (bfd
*abfd
)
6795 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6796 Elf_Internal_Shdr
**i_shdrp
;
6798 unsigned int count
, num_sec
;
6799 struct elf_obj_tdata
*t
;
6801 if (! abfd
->output_has_begun
6802 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6804 /* Do not rewrite ELF data when the BFD has been opened for update.
6805 abfd->output_has_begun was set to TRUE on opening, so creation of
6806 new sections, and modification of existing section sizes was
6807 restricted. This means the ELF header, program headers and
6808 section headers can't have changed. If the contents of any
6809 sections has been modified, then those changes have already been
6810 written to the BFD. */
6811 else if (abfd
->direction
== both_direction
)
6813 BFD_ASSERT (abfd
->output_has_begun
);
6817 i_shdrp
= elf_elfsections (abfd
);
6820 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6824 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6827 /* After writing the headers, we need to write the sections too... */
6828 num_sec
= elf_numsections (abfd
);
6829 for (count
= 1; count
< num_sec
; count
++)
6831 i_shdrp
[count
]->sh_name
6832 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6833 i_shdrp
[count
]->sh_name
);
6834 if (bed
->elf_backend_section_processing
)
6835 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6837 if (i_shdrp
[count
]->contents
)
6839 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6841 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6842 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6847 /* Write out the section header names. */
6848 t
= elf_tdata (abfd
);
6849 if (elf_shstrtab (abfd
) != NULL
6850 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6851 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6854 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6857 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6860 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6861 if (t
->o
->build_id
.after_write_object_contents
!= NULL
6862 && !(*t
->o
->build_id
.after_write_object_contents
) (abfd
))
6864 if (t
->o
->package_metadata
.after_write_object_contents
!= NULL
6865 && !(*t
->o
->package_metadata
.after_write_object_contents
) (abfd
))
6872 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6874 /* Hopefully this can be done just like an object file. */
6875 return _bfd_elf_write_object_contents (abfd
);
6878 /* Given a section, search the header to find them. */
6881 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6883 const struct elf_backend_data
*bed
;
6884 unsigned int sec_index
;
6886 if (elf_section_data (asect
) != NULL
6887 && elf_section_data (asect
)->this_idx
!= 0)
6888 return elf_section_data (asect
)->this_idx
;
6890 if (bfd_is_abs_section (asect
))
6891 sec_index
= SHN_ABS
;
6892 else if (bfd_is_com_section (asect
))
6893 sec_index
= SHN_COMMON
;
6894 else if (bfd_is_und_section (asect
))
6895 sec_index
= SHN_UNDEF
;
6897 sec_index
= SHN_BAD
;
6899 bed
= get_elf_backend_data (abfd
);
6900 if (bed
->elf_backend_section_from_bfd_section
)
6902 int retval
= sec_index
;
6904 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6908 if (sec_index
== SHN_BAD
)
6909 bfd_set_error (bfd_error_nonrepresentable_section
);
6914 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6918 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6920 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6922 flagword flags
= asym_ptr
->flags
;
6924 /* When gas creates relocations against local labels, it creates its
6925 own symbol for the section, but does put the symbol into the
6926 symbol chain, so udata is 0. When the linker is generating
6927 relocatable output, this section symbol may be for one of the
6928 input sections rather than the output section. */
6929 if (asym_ptr
->udata
.i
== 0
6930 && (flags
& BSF_SECTION_SYM
)
6931 && asym_ptr
->section
)
6935 sec
= asym_ptr
->section
;
6936 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6937 sec
= sec
->output_section
;
6938 if (sec
->owner
== abfd
6939 && sec
->index
< elf_num_section_syms (abfd
)
6940 && elf_section_syms (abfd
)[sec
->index
] != NULL
)
6941 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
6944 idx
= asym_ptr
->udata
.i
;
6948 /* This case can occur when using --strip-symbol on a symbol
6949 which is used in a relocation entry. */
6951 /* xgettext:c-format */
6952 (_("%pB: symbol `%s' required but not present"),
6953 abfd
, bfd_asymbol_name (asym_ptr
));
6954 bfd_set_error (bfd_error_no_symbols
);
6961 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6962 " flags = 0x%.8x\n",
6963 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6971 static inline bfd_vma
6972 segment_size (Elf_Internal_Phdr
*segment
)
6974 return (segment
->p_memsz
> segment
->p_filesz
6975 ? segment
->p_memsz
: segment
->p_filesz
);
6979 /* Returns the end address of the segment + 1. */
6980 static inline bfd_vma
6981 segment_end (Elf_Internal_Phdr
*segment
, bfd_vma start
)
6983 return start
+ segment_size (segment
);
6986 static inline bfd_size_type
6987 section_size (asection
*section
, Elf_Internal_Phdr
*segment
)
6989 if ((section
->flags
& SEC_HAS_CONTENTS
) != 0
6990 || (section
->flags
& SEC_THREAD_LOCAL
) == 0
6991 || segment
->p_type
== PT_TLS
)
6992 return section
->size
;
6996 /* Returns TRUE if the given section is contained within the given
6997 segment. LMA addresses are compared against PADDR when
6998 bed->want_p_paddr_set_to_zero is false, VMA against VADDR when true. */
7000 is_contained_by (asection
*section
, Elf_Internal_Phdr
*segment
,
7001 bfd_vma paddr
, bfd_vma vaddr
, unsigned int opb
,
7002 const struct elf_backend_data
*bed
)
7004 bfd_vma seg_addr
= !bed
->want_p_paddr_set_to_zero
? paddr
: vaddr
;
7005 bfd_vma addr
= !bed
->want_p_paddr_set_to_zero
? section
->lma
: section
->vma
;
7007 if (_bfd_mul_overflow (addr
, opb
, &octet
))
7009 /* The third and fourth lines below are testing that the section end
7010 address is within the segment. It's written this way to avoid
7011 overflow. Add seg_addr + section_size to both sides of the
7012 inequality to make it obvious. */
7013 return (octet
>= seg_addr
7014 && segment_size (segment
) >= section_size (section
, segment
)
7015 && (octet
- seg_addr
7016 <= segment_size (segment
) - section_size (section
, segment
)));
7019 /* Handle PT_NOTE segment. */
7021 is_note (asection
*s
, Elf_Internal_Phdr
*p
)
7023 return (p
->p_type
== PT_NOTE
7024 && elf_section_type (s
) == SHT_NOTE
7025 && (ufile_ptr
) s
->filepos
>= p
->p_offset
7026 && p
->p_filesz
>= s
->size
7027 && (ufile_ptr
) s
->filepos
- p
->p_offset
<= p
->p_filesz
- s
->size
);
7030 /* Rewrite program header information. */
7033 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
, bfd_vma maxpagesize
)
7035 Elf_Internal_Ehdr
*iehdr
;
7036 struct elf_segment_map
*map
;
7037 struct elf_segment_map
*map_first
;
7038 struct elf_segment_map
**pointer_to_map
;
7039 Elf_Internal_Phdr
*segment
;
7042 unsigned int num_segments
;
7043 bool phdr_included
= false;
7045 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
7046 unsigned int phdr_adjust_num
= 0;
7047 const struct elf_backend_data
*bed
;
7048 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7050 bed
= get_elf_backend_data (ibfd
);
7051 iehdr
= elf_elfheader (ibfd
);
7054 pointer_to_map
= &map_first
;
7056 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7058 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7059 linker, which generates a PT_INTERP section with p_vaddr and
7060 p_memsz set to 0. */
7061 #define IS_SOLARIS_PT_INTERP(p, s) \
7063 && p->p_paddr == 0 \
7064 && p->p_memsz == 0 \
7065 && p->p_filesz > 0 \
7066 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7068 && (bfd_vma) s->filepos >= p->p_offset \
7069 && ((bfd_vma) s->filepos + s->size \
7070 <= p->p_offset + p->p_filesz))
7072 /* Decide if the given section should be included in the given segment.
7073 A section will be included if:
7074 1. It is within the address space of the segment -- we use the LMA
7075 if that is set for the segment and the VMA otherwise,
7076 2. It is an allocated section or a NOTE section in a PT_NOTE
7078 3. There is an output section associated with it,
7079 4. The section has not already been allocated to a previous segment.
7080 5. PT_GNU_STACK segments do not include any sections.
7081 6. PT_TLS segment includes only SHF_TLS sections.
7082 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7083 8. PT_DYNAMIC should not contain empty sections at the beginning
7084 (with the possible exception of .dynamic). */
7085 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7086 (((is_contained_by (section, segment, segment->p_paddr, \
7087 segment->p_vaddr, opb, bed) \
7088 && (section->flags & SEC_ALLOC) != 0) \
7089 || is_note (section, segment)) \
7090 && segment->p_type != PT_GNU_STACK \
7091 && (segment->p_type != PT_TLS \
7092 || (section->flags & SEC_THREAD_LOCAL)) \
7093 && (segment->p_type == PT_LOAD \
7094 || segment->p_type == PT_TLS \
7095 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7096 && (segment->p_type != PT_DYNAMIC \
7097 || section_size (section, segment) > 0 \
7098 || (segment->p_paddr \
7099 ? segment->p_paddr != section->lma * (opb) \
7100 : segment->p_vaddr != section->vma * (opb)) \
7101 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7102 && (segment->p_type != PT_LOAD || !section->segment_mark))
7104 /* If the output section of a section in the input segment is NULL,
7105 it is removed from the corresponding output segment. */
7106 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7107 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7108 && section->output_section != NULL)
7110 /* Returns TRUE iff seg1 starts after the end of seg2. */
7111 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7112 (seg1->field >= segment_end (seg2, seg2->field))
7114 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7115 their VMA address ranges and their LMA address ranges overlap.
7116 It is possible to have overlapping VMA ranges without overlapping LMA
7117 ranges. RedBoot images for example can have both .data and .bss mapped
7118 to the same VMA range, but with the .data section mapped to a different
7120 #define SEGMENT_OVERLAPS(seg1, seg2) \
7121 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7122 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7123 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7124 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7126 /* Initialise the segment mark field, and discard stupid alignment. */
7127 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
7129 asection
*o
= section
->output_section
;
7130 if (o
!= NULL
&& o
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
7131 o
->alignment_power
= 0;
7132 section
->segment_mark
= false;
7135 /* The Solaris linker creates program headers in which all the
7136 p_paddr fields are zero. When we try to objcopy or strip such a
7137 file, we get confused. Check for this case, and if we find it
7138 don't set the p_paddr_valid fields. */
7139 p_paddr_valid
= false;
7140 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7143 if (segment
->p_paddr
!= 0)
7145 p_paddr_valid
= true;
7149 /* Scan through the segments specified in the program header
7150 of the input BFD. For this first scan we look for overlaps
7151 in the loadable segments. These can be created by weird
7152 parameters to objcopy. Also, fix some solaris weirdness. */
7153 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7158 Elf_Internal_Phdr
*segment2
;
7160 if (segment
->p_type
== PT_INTERP
)
7161 for (section
= ibfd
->sections
; section
; section
= section
->next
)
7162 if (IS_SOLARIS_PT_INTERP (segment
, section
))
7164 /* Mininal change so that the normal section to segment
7165 assignment code will work. */
7166 segment
->p_vaddr
= section
->vma
* opb
;
7170 if (segment
->p_type
!= PT_LOAD
)
7172 /* Remove PT_GNU_RELRO segment. */
7173 if (segment
->p_type
== PT_GNU_RELRO
)
7174 segment
->p_type
= PT_NULL
;
7178 /* Determine if this segment overlaps any previous segments. */
7179 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7181 bfd_signed_vma extra_length
;
7183 if (segment2
->p_type
!= PT_LOAD
7184 || !SEGMENT_OVERLAPS (segment
, segment2
))
7187 /* Merge the two segments together. */
7188 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7190 /* Extend SEGMENT2 to include SEGMENT and then delete
7192 extra_length
= (segment_end (segment
, segment
->p_vaddr
)
7193 - segment_end (segment2
, segment2
->p_vaddr
));
7195 if (extra_length
> 0)
7197 segment2
->p_memsz
+= extra_length
;
7198 segment2
->p_filesz
+= extra_length
;
7201 segment
->p_type
= PT_NULL
;
7203 /* Since we have deleted P we must restart the outer loop. */
7205 segment
= elf_tdata (ibfd
)->phdr
;
7210 /* Extend SEGMENT to include SEGMENT2 and then delete
7212 extra_length
= (segment_end (segment2
, segment2
->p_vaddr
)
7213 - segment_end (segment
, segment
->p_vaddr
));
7215 if (extra_length
> 0)
7217 segment
->p_memsz
+= extra_length
;
7218 segment
->p_filesz
+= extra_length
;
7221 segment2
->p_type
= PT_NULL
;
7226 /* The second scan attempts to assign sections to segments. */
7227 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7231 unsigned int section_count
;
7232 asection
**sections
;
7233 asection
*output_section
;
7235 asection
*matching_lma
;
7236 asection
*suggested_lma
;
7239 asection
*first_section
;
7241 if (segment
->p_type
== PT_NULL
)
7244 first_section
= NULL
;
7245 /* Compute how many sections might be placed into this segment. */
7246 for (section
= ibfd
->sections
, section_count
= 0;
7248 section
= section
->next
)
7250 /* Find the first section in the input segment, which may be
7251 removed from the corresponding output segment. */
7252 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7254 if (first_section
== NULL
)
7255 first_section
= section
;
7256 if (section
->output_section
!= NULL
)
7261 /* Allocate a segment map big enough to contain
7262 all of the sections we have selected. */
7263 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7264 amt
+= section_count
* sizeof (asection
*);
7265 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7269 /* Initialise the fields of the segment map. Default to
7270 using the physical address of the segment in the input BFD. */
7272 map
->p_type
= segment
->p_type
;
7273 map
->p_flags
= segment
->p_flags
;
7274 map
->p_flags_valid
= 1;
7276 if (map
->p_type
== PT_LOAD
7277 && (ibfd
->flags
& D_PAGED
) != 0
7279 && segment
->p_align
> 1)
7281 map
->p_align
= segment
->p_align
;
7282 if (segment
->p_align
> maxpagesize
)
7283 map
->p_align
= maxpagesize
;
7284 map
->p_align_valid
= 1;
7287 /* If the first section in the input segment is removed, there is
7288 no need to preserve segment physical address in the corresponding
7290 if (!first_section
|| first_section
->output_section
!= NULL
)
7292 map
->p_paddr
= segment
->p_paddr
;
7293 map
->p_paddr_valid
= p_paddr_valid
;
7296 /* Determine if this segment contains the ELF file header
7297 and if it contains the program headers themselves. */
7298 map
->includes_filehdr
= (segment
->p_offset
== 0
7299 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7300 map
->includes_phdrs
= 0;
7302 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7304 map
->includes_phdrs
=
7305 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7306 && (segment
->p_offset
+ segment
->p_filesz
7307 >= ((bfd_vma
) iehdr
->e_phoff
7308 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7310 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7311 phdr_included
= true;
7314 if (section_count
== 0)
7316 /* Special segments, such as the PT_PHDR segment, may contain
7317 no sections, but ordinary, loadable segments should contain
7318 something. They are allowed by the ELF spec however, so only
7319 a warning is produced.
7320 There is however the valid use case of embedded systems which
7321 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7322 flash memory with zeros. No warning is shown for that case. */
7323 if (segment
->p_type
== PT_LOAD
7324 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7325 /* xgettext:c-format */
7327 (_("%pB: warning: empty loadable segment detected"
7328 " at vaddr=%#" PRIx64
", is this intentional?"),
7329 ibfd
, (uint64_t) segment
->p_vaddr
);
7331 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7333 *pointer_to_map
= map
;
7334 pointer_to_map
= &map
->next
;
7339 /* Now scan the sections in the input BFD again and attempt
7340 to add their corresponding output sections to the segment map.
7341 The problem here is how to handle an output section which has
7342 been moved (ie had its LMA changed). There are four possibilities:
7344 1. None of the sections have been moved.
7345 In this case we can continue to use the segment LMA from the
7348 2. All of the sections have been moved by the same amount.
7349 In this case we can change the segment's LMA to match the LMA
7350 of the first section.
7352 3. Some of the sections have been moved, others have not.
7353 In this case those sections which have not been moved can be
7354 placed in the current segment which will have to have its size,
7355 and possibly its LMA changed, and a new segment or segments will
7356 have to be created to contain the other sections.
7358 4. The sections have been moved, but not by the same amount.
7359 In this case we can change the segment's LMA to match the LMA
7360 of the first section and we will have to create a new segment
7361 or segments to contain the other sections.
7363 In order to save time, we allocate an array to hold the section
7364 pointers that we are interested in. As these sections get assigned
7365 to a segment, they are removed from this array. */
7367 amt
= section_count
* sizeof (asection
*);
7368 sections
= (asection
**) bfd_malloc (amt
);
7369 if (sections
== NULL
)
7372 /* Step One: Scan for segment vs section LMA conflicts.
7373 Also add the sections to the section array allocated above.
7374 Also add the sections to the current segment. In the common
7375 case, where the sections have not been moved, this means that
7376 we have completely filled the segment, and there is nothing
7379 matching_lma
= NULL
;
7380 suggested_lma
= NULL
;
7382 for (section
= first_section
, j
= 0;
7384 section
= section
->next
)
7386 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7388 output_section
= section
->output_section
;
7390 sections
[j
++] = section
;
7392 /* The Solaris native linker always sets p_paddr to 0.
7393 We try to catch that case here, and set it to the
7394 correct value. Note - some backends require that
7395 p_paddr be left as zero. */
7397 && segment
->p_vaddr
!= 0
7398 && !bed
->want_p_paddr_set_to_zero
7400 && output_section
->lma
!= 0
7401 && (align_power (segment
->p_vaddr
7402 + (map
->includes_filehdr
7403 ? iehdr
->e_ehsize
: 0)
7404 + (map
->includes_phdrs
7405 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7407 output_section
->alignment_power
* opb
)
7408 == (output_section
->vma
* opb
)))
7409 map
->p_paddr
= segment
->p_vaddr
;
7411 /* Match up the physical address of the segment with the
7412 LMA address of the output section. */
7413 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7414 map
->p_paddr
+ map
->p_vaddr_offset
, opb
, bed
)
7415 || is_note (section
, segment
))
7417 if (matching_lma
== NULL
7418 || output_section
->lma
< matching_lma
->lma
)
7419 matching_lma
= output_section
;
7421 /* We assume that if the section fits within the segment
7422 then it does not overlap any other section within that
7424 map
->sections
[isec
++] = output_section
;
7426 else if (suggested_lma
== NULL
)
7427 suggested_lma
= output_section
;
7429 if (j
== section_count
)
7434 BFD_ASSERT (j
== section_count
);
7436 /* Step Two: Adjust the physical address of the current segment,
7438 if (isec
== section_count
)
7440 /* All of the sections fitted within the segment as currently
7441 specified. This is the default case. Add the segment to
7442 the list of built segments and carry on to process the next
7443 program header in the input BFD. */
7444 map
->count
= section_count
;
7445 *pointer_to_map
= map
;
7446 pointer_to_map
= &map
->next
;
7449 && !bed
->want_p_paddr_set_to_zero
)
7451 bfd_vma hdr_size
= 0;
7452 if (map
->includes_filehdr
)
7453 hdr_size
= iehdr
->e_ehsize
;
7454 if (map
->includes_phdrs
)
7455 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7457 /* Account for padding before the first section in the
7459 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7460 - matching_lma
->lma
);
7468 /* Change the current segment's physical address to match
7469 the LMA of the first section that fitted, or if no
7470 section fitted, the first section. */
7471 if (matching_lma
== NULL
)
7472 matching_lma
= suggested_lma
;
7474 map
->p_paddr
= matching_lma
->lma
* opb
;
7476 /* Offset the segment physical address from the lma
7477 to allow for space taken up by elf headers. */
7478 if (map
->includes_phdrs
)
7480 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7482 /* iehdr->e_phnum is just an estimate of the number
7483 of program headers that we will need. Make a note
7484 here of the number we used and the segment we chose
7485 to hold these headers, so that we can adjust the
7486 offset when we know the correct value. */
7487 phdr_adjust_num
= iehdr
->e_phnum
;
7488 phdr_adjust_seg
= map
;
7491 if (map
->includes_filehdr
)
7493 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7494 map
->p_paddr
-= iehdr
->e_ehsize
;
7495 /* We've subtracted off the size of headers from the
7496 first section lma, but there may have been some
7497 alignment padding before that section too. Try to
7498 account for that by adjusting the segment lma down to
7499 the same alignment. */
7500 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7501 align
= segment
->p_align
;
7502 map
->p_paddr
&= -(align
* opb
);
7506 /* Step Three: Loop over the sections again, this time assigning
7507 those that fit to the current segment and removing them from the
7508 sections array; but making sure not to leave large gaps. Once all
7509 possible sections have been assigned to the current segment it is
7510 added to the list of built segments and if sections still remain
7511 to be assigned, a new segment is constructed before repeating
7517 suggested_lma
= NULL
;
7519 /* Fill the current segment with sections that fit. */
7520 for (j
= 0; j
< section_count
; j
++)
7522 section
= sections
[j
];
7524 if (section
== NULL
)
7527 output_section
= section
->output_section
;
7529 BFD_ASSERT (output_section
!= NULL
);
7531 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7532 map
->p_paddr
+ map
->p_vaddr_offset
, opb
, bed
)
7533 || is_note (section
, segment
))
7535 if (map
->count
== 0)
7537 /* If the first section in a segment does not start at
7538 the beginning of the segment, then something is
7540 if (align_power (map
->p_paddr
7541 + (map
->includes_filehdr
7542 ? iehdr
->e_ehsize
: 0)
7543 + (map
->includes_phdrs
7544 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7546 output_section
->alignment_power
* opb
)
7547 != output_section
->lma
* opb
)
7554 prev_sec
= map
->sections
[map
->count
- 1];
7556 /* If the gap between the end of the previous section
7557 and the start of this section is more than
7558 maxpagesize then we need to start a new segment. */
7559 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7561 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7562 || (prev_sec
->lma
+ prev_sec
->size
7563 > output_section
->lma
))
7565 if (suggested_lma
== NULL
)
7566 suggested_lma
= output_section
;
7572 map
->sections
[map
->count
++] = output_section
;
7575 if (segment
->p_type
== PT_LOAD
)
7576 section
->segment_mark
= true;
7578 else if (suggested_lma
== NULL
)
7579 suggested_lma
= output_section
;
7582 /* PR 23932. A corrupt input file may contain sections that cannot
7583 be assigned to any segment - because for example they have a
7584 negative size - or segments that do not contain any sections.
7585 But there are also valid reasons why a segment can be empty.
7586 So allow a count of zero. */
7588 /* Add the current segment to the list of built segments. */
7589 *pointer_to_map
= map
;
7590 pointer_to_map
= &map
->next
;
7592 if (isec
< section_count
)
7594 /* We still have not allocated all of the sections to
7595 segments. Create a new segment here, initialise it
7596 and carry on looping. */
7597 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7598 amt
+= section_count
* sizeof (asection
*);
7599 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7606 /* Initialise the fields of the segment map. Set the physical
7607 physical address to the LMA of the first section that has
7608 not yet been assigned. */
7610 map
->p_type
= segment
->p_type
;
7611 map
->p_flags
= segment
->p_flags
;
7612 map
->p_flags_valid
= 1;
7613 map
->p_paddr
= suggested_lma
->lma
* opb
;
7614 map
->p_paddr_valid
= p_paddr_valid
;
7615 map
->includes_filehdr
= 0;
7616 map
->includes_phdrs
= 0;
7621 bfd_set_error (bfd_error_sorry
);
7625 while (isec
< section_count
);
7630 elf_seg_map (obfd
) = map_first
;
7632 /* If we had to estimate the number of program headers that were
7633 going to be needed, then check our estimate now and adjust
7634 the offset if necessary. */
7635 if (phdr_adjust_seg
!= NULL
)
7639 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7642 if (count
> phdr_adjust_num
)
7643 phdr_adjust_seg
->p_paddr
7644 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7646 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7647 if (map
->p_type
== PT_PHDR
)
7650 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7651 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7656 #undef IS_SOLARIS_PT_INTERP
7657 #undef IS_SECTION_IN_INPUT_SEGMENT
7658 #undef INCLUDE_SECTION_IN_SEGMENT
7659 #undef SEGMENT_AFTER_SEGMENT
7660 #undef SEGMENT_OVERLAPS
7664 /* Return true if p_align in the ELF program header in ABFD is valid. */
7667 elf_is_p_align_valid (bfd
*abfd
)
7670 Elf_Internal_Phdr
*segment
;
7671 unsigned int num_segments
;
7672 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7673 bfd_size_type maxpagesize
= bed
->maxpagesize
;
7674 bfd_size_type p_align
= bed
->p_align
;
7676 /* Return true if the default p_align value isn't set or the maximum
7677 page size is the same as the minimum page size. */
7678 if (p_align
== 0 || maxpagesize
== bed
->minpagesize
)
7681 /* When the default p_align value is set, p_align may be set to the
7682 default p_align value while segments are aligned to the maximum
7683 page size. In this case, the input p_align will be ignored and
7684 the maximum page size will be used to align the output segments. */
7685 segment
= elf_tdata (abfd
)->phdr
;
7686 num_segments
= elf_elfheader (abfd
)->e_phnum
;
7687 for (i
= 0; i
< num_segments
; i
++, segment
++)
7688 if (segment
->p_type
== PT_LOAD
7689 && (segment
->p_align
!= p_align
7690 || vma_page_aligned_bias (segment
->p_vaddr
,
7698 /* Copy ELF program header information. */
7701 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7703 Elf_Internal_Ehdr
*iehdr
;
7704 struct elf_segment_map
*map
;
7705 struct elf_segment_map
*map_first
;
7706 struct elf_segment_map
**pointer_to_map
;
7707 Elf_Internal_Phdr
*segment
;
7709 unsigned int num_segments
;
7710 bool phdr_included
= false;
7712 bool p_palign_valid
;
7713 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7715 iehdr
= elf_elfheader (ibfd
);
7718 pointer_to_map
= &map_first
;
7720 /* If all the segment p_paddr fields are zero, don't set
7721 map->p_paddr_valid. */
7722 p_paddr_valid
= false;
7723 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7724 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7727 if (segment
->p_paddr
!= 0)
7729 p_paddr_valid
= true;
7733 p_palign_valid
= elf_is_p_align_valid (ibfd
);
7735 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7740 unsigned int section_count
;
7742 Elf_Internal_Shdr
*this_hdr
;
7743 asection
*first_section
= NULL
;
7744 asection
*lowest_section
;
7746 /* Compute how many sections are in this segment. */
7747 for (section
= ibfd
->sections
, section_count
= 0;
7749 section
= section
->next
)
7751 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7752 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7754 if (first_section
== NULL
)
7755 first_section
= section
;
7760 /* Allocate a segment map big enough to contain
7761 all of the sections we have selected. */
7762 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7763 amt
+= section_count
* sizeof (asection
*);
7764 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7768 /* Initialize the fields of the output segment map with the
7771 map
->p_type
= segment
->p_type
;
7772 map
->p_flags
= segment
->p_flags
;
7773 map
->p_flags_valid
= 1;
7774 map
->p_paddr
= segment
->p_paddr
;
7775 map
->p_paddr_valid
= p_paddr_valid
;
7776 map
->p_align
= segment
->p_align
;
7777 /* Keep p_align of PT_GNU_STACK for stack alignment. */
7778 map
->p_align_valid
= (map
->p_type
== PT_GNU_STACK
7780 map
->p_vaddr_offset
= 0;
7782 if (map
->p_type
== PT_GNU_RELRO
7783 || map
->p_type
== PT_GNU_STACK
)
7785 /* The PT_GNU_RELRO segment may contain the first a few
7786 bytes in the .got.plt section even if the whole .got.plt
7787 section isn't in the PT_GNU_RELRO segment. We won't
7788 change the size of the PT_GNU_RELRO segment.
7789 Similarly, PT_GNU_STACK size is significant on uclinux
7791 map
->p_size
= segment
->p_memsz
;
7792 map
->p_size_valid
= 1;
7795 /* Determine if this segment contains the ELF file header
7796 and if it contains the program headers themselves. */
7797 map
->includes_filehdr
= (segment
->p_offset
== 0
7798 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7800 map
->includes_phdrs
= 0;
7801 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7803 map
->includes_phdrs
=
7804 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7805 && (segment
->p_offset
+ segment
->p_filesz
7806 >= ((bfd_vma
) iehdr
->e_phoff
7807 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7809 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7810 phdr_included
= true;
7813 lowest_section
= NULL
;
7814 if (section_count
!= 0)
7816 unsigned int isec
= 0;
7818 for (section
= first_section
;
7820 section
= section
->next
)
7822 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7823 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7825 map
->sections
[isec
++] = section
->output_section
;
7826 if ((section
->flags
& SEC_ALLOC
) != 0)
7830 if (lowest_section
== NULL
7831 || section
->lma
< lowest_section
->lma
)
7832 lowest_section
= section
;
7834 /* Section lmas are set up from PT_LOAD header
7835 p_paddr in _bfd_elf_make_section_from_shdr.
7836 If this header has a p_paddr that disagrees
7837 with the section lma, flag the p_paddr as
7839 if ((section
->flags
& SEC_LOAD
) != 0)
7840 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7842 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7843 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7844 map
->p_paddr_valid
= false;
7846 if (isec
== section_count
)
7852 if (section_count
== 0)
7853 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7854 else if (map
->p_paddr_valid
)
7856 /* Account for padding before the first section in the segment. */
7857 bfd_vma hdr_size
= 0;
7858 if (map
->includes_filehdr
)
7859 hdr_size
= iehdr
->e_ehsize
;
7860 if (map
->includes_phdrs
)
7861 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7863 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7864 - (lowest_section
? lowest_section
->lma
: 0));
7867 map
->count
= section_count
;
7868 *pointer_to_map
= map
;
7869 pointer_to_map
= &map
->next
;
7872 elf_seg_map (obfd
) = map_first
;
7876 /* Copy private BFD data. This copies or rewrites ELF program header
7880 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7882 bfd_vma maxpagesize
;
7884 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7885 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7888 if (elf_tdata (ibfd
)->phdr
== NULL
)
7891 if (ibfd
->xvec
== obfd
->xvec
)
7893 /* Check to see if any sections in the input BFD
7894 covered by ELF program header have changed. */
7895 Elf_Internal_Phdr
*segment
;
7896 asection
*section
, *osec
;
7897 unsigned int i
, num_segments
;
7898 Elf_Internal_Shdr
*this_hdr
;
7899 const struct elf_backend_data
*bed
;
7901 bed
= get_elf_backend_data (ibfd
);
7903 /* Regenerate the segment map if p_paddr is set to 0. */
7904 if (bed
->want_p_paddr_set_to_zero
)
7907 /* Initialize the segment mark field. */
7908 for (section
= obfd
->sections
; section
!= NULL
;
7909 section
= section
->next
)
7910 section
->segment_mark
= false;
7912 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7913 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7917 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7918 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7919 which severly confuses things, so always regenerate the segment
7920 map in this case. */
7921 if (segment
->p_paddr
== 0
7922 && segment
->p_memsz
== 0
7923 && (segment
->p_type
== PT_INTERP
7924 || segment
->p_type
== PT_DYNAMIC
))
7927 for (section
= ibfd
->sections
;
7928 section
!= NULL
; section
= section
->next
)
7930 /* We mark the output section so that we know it comes
7931 from the input BFD. */
7932 osec
= section
->output_section
;
7934 osec
->segment_mark
= true;
7936 /* Check if this section is covered by the segment. */
7937 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7938 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7940 /* FIXME: Check if its output section is changed or
7941 removed. What else do we need to check? */
7943 || section
->flags
!= osec
->flags
7944 || section
->lma
!= osec
->lma
7945 || section
->vma
!= osec
->vma
7946 || section
->size
!= osec
->size
7947 || section
->rawsize
!= osec
->rawsize
7948 || section
->alignment_power
!= osec
->alignment_power
)
7954 /* Check to see if any output section do not come from the
7956 for (section
= obfd
->sections
; section
!= NULL
;
7957 section
= section
->next
)
7959 if (!section
->segment_mark
)
7962 section
->segment_mark
= false;
7965 return copy_elf_program_header (ibfd
, obfd
);
7970 if (ibfd
->xvec
== obfd
->xvec
)
7972 /* When rewriting program header, set the output maxpagesize to
7973 the maximum alignment of input PT_LOAD segments. */
7974 Elf_Internal_Phdr
*segment
;
7976 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7978 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7981 if (segment
->p_type
== PT_LOAD
7982 && maxpagesize
< segment
->p_align
)
7984 /* PR 17512: file: f17299af. */
7985 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7986 /* xgettext:c-format */
7987 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7988 PRIx64
" is too large"),
7989 ibfd
, (uint64_t) segment
->p_align
);
7991 maxpagesize
= segment
->p_align
;
7994 if (maxpagesize
== 0)
7995 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
7997 return rewrite_elf_program_header (ibfd
, obfd
, maxpagesize
);
8000 /* Initialize private output section information from input section. */
8003 _bfd_elf_init_private_section_data (bfd
*ibfd
,
8007 struct bfd_link_info
*link_info
)
8010 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8011 bool final_link
= (link_info
!= NULL
8012 && !bfd_link_relocatable (link_info
));
8014 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8015 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8018 BFD_ASSERT (elf_section_data (osec
) != NULL
);
8020 /* If this is a known ABI section, ELF section type and flags may
8021 have been set up when OSEC was created. For normal sections we
8022 allow the user to override the type and flags other than
8023 SHF_MASKOS and SHF_MASKPROC. */
8024 if (elf_section_type (osec
) == SHT_PROGBITS
8025 || elf_section_type (osec
) == SHT_NOTE
8026 || elf_section_type (osec
) == SHT_NOBITS
)
8027 elf_section_type (osec
) = SHT_NULL
;
8028 /* For objcopy and relocatable link, copy the ELF section type from
8029 the input file if the BFD section flags are the same. (If they
8030 are different the user may be doing something like
8031 "objcopy --set-section-flags .text=alloc,data".) For a final
8032 link allow some flags that the linker clears to differ. */
8033 if (elf_section_type (osec
) == SHT_NULL
8034 && (osec
->flags
== isec
->flags
8036 && ((osec
->flags
^ isec
->flags
)
8037 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
8038 elf_section_type (osec
) = elf_section_type (isec
);
8040 /* FIXME: Is this correct for all OS/PROC specific flags? */
8041 elf_section_flags (osec
) = (elf_section_flags (isec
)
8042 & (SHF_MASKOS
| SHF_MASKPROC
));
8044 /* Copy sh_info from input for mbind section. */
8045 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
8046 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
8047 elf_section_data (osec
)->this_hdr
.sh_info
8048 = elf_section_data (isec
)->this_hdr
.sh_info
;
8050 /* Set things up for objcopy and relocatable link. The output
8051 SHT_GROUP section will have its elf_next_in_group pointing back
8052 to the input group members. Ignore linker created group section.
8053 See elfNN_ia64_object_p in elfxx-ia64.c. */
8054 if ((link_info
== NULL
8055 || !link_info
->resolve_section_groups
)
8056 && (elf_sec_group (isec
) == NULL
8057 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
8059 if (elf_section_flags (isec
) & SHF_GROUP
)
8060 elf_section_flags (osec
) |= SHF_GROUP
;
8061 elf_next_in_group (osec
) = elf_next_in_group (isec
);
8062 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
8065 /* If not decompress, preserve SHF_COMPRESSED. */
8066 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
8067 elf_section_flags (osec
) |= (elf_section_flags (isec
)
8070 ihdr
= &elf_section_data (isec
)->this_hdr
;
8072 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8073 don't use the output section of the linked-to section since it
8074 may be NULL at this point. */
8075 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
8077 ohdr
= &elf_section_data (osec
)->this_hdr
;
8078 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
8079 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
8082 osec
->use_rela_p
= isec
->use_rela_p
;
8087 /* Copy private section information. This copies over the entsize
8088 field, and sometimes the info field. */
8091 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
8096 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8098 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8099 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8102 ihdr
= &elf_section_data (isec
)->this_hdr
;
8103 ohdr
= &elf_section_data (osec
)->this_hdr
;
8105 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
8107 if (ihdr
->sh_type
== SHT_SYMTAB
8108 || ihdr
->sh_type
== SHT_DYNSYM
8109 || ihdr
->sh_type
== SHT_GNU_verneed
8110 || ihdr
->sh_type
== SHT_GNU_verdef
)
8111 ohdr
->sh_info
= ihdr
->sh_info
;
8113 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
8117 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8118 necessary if we are removing either the SHT_GROUP section or any of
8119 the group member sections. DISCARDED is the value that a section's
8120 output_section has if the section will be discarded, NULL when this
8121 function is called from objcopy, bfd_abs_section_ptr when called
8125 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
8129 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
8130 if (elf_section_type (isec
) == SHT_GROUP
)
8132 asection
*first
= elf_next_in_group (isec
);
8133 asection
*s
= first
;
8134 bfd_size_type removed
= 0;
8138 /* If this member section is being output but the
8139 SHT_GROUP section is not, then clear the group info
8140 set up by _bfd_elf_copy_private_section_data. */
8141 if (s
->output_section
!= discarded
8142 && isec
->output_section
== discarded
)
8144 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
8145 elf_group_name (s
->output_section
) = NULL
;
8149 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
8150 if (s
->output_section
== discarded
8151 && isec
->output_section
!= discarded
)
8153 /* Conversely, if the member section is not being
8154 output but the SHT_GROUP section is, then adjust
8157 if (elf_sec
->rel
.hdr
!= NULL
8158 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8160 if (elf_sec
->rela
.hdr
!= NULL
8161 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8166 /* Also adjust for zero-sized relocation member
8168 if (elf_sec
->rel
.hdr
!= NULL
8169 && elf_sec
->rel
.hdr
->sh_size
== 0)
8171 if (elf_sec
->rela
.hdr
!= NULL
8172 && elf_sec
->rela
.hdr
->sh_size
== 0)
8176 s
= elf_next_in_group (s
);
8182 if (discarded
!= NULL
)
8184 /* If we've been called for ld -r, then we need to
8185 adjust the input section size. */
8186 if (isec
->rawsize
== 0)
8187 isec
->rawsize
= isec
->size
;
8188 isec
->size
= isec
->rawsize
- removed
;
8189 if (isec
->size
<= 4)
8192 isec
->flags
|= SEC_EXCLUDE
;
8195 else if (isec
->output_section
!= NULL
)
8197 /* Adjust the output section size when called from
8199 isec
->output_section
->size
-= removed
;
8200 if (isec
->output_section
->size
<= 4)
8202 isec
->output_section
->size
= 0;
8203 isec
->output_section
->flags
|= SEC_EXCLUDE
;
8212 /* Copy private header information. */
8215 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
8217 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8218 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8221 /* Copy over private BFD data if it has not already been copied.
8222 This must be done here, rather than in the copy_private_bfd_data
8223 entry point, because the latter is called after the section
8224 contents have been set, which means that the program headers have
8225 already been worked out. */
8226 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
8228 if (! copy_private_bfd_data (ibfd
, obfd
))
8232 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8235 /* Copy private symbol information. If this symbol is in a section
8236 which we did not map into a BFD section, try to map the section
8237 index correctly. We use special macro definitions for the mapped
8238 section indices; these definitions are interpreted by the
8239 swap_out_syms function. */
8241 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8242 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8243 #define MAP_STRTAB (SHN_HIOS + 3)
8244 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8245 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8248 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8253 elf_symbol_type
*isym
, *osym
;
8255 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8256 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8259 isym
= elf_symbol_from (isymarg
);
8260 osym
= elf_symbol_from (osymarg
);
8263 && isym
->internal_elf_sym
.st_shndx
!= 0
8265 && bfd_is_abs_section (isym
->symbol
.section
))
8269 shndx
= isym
->internal_elf_sym
.st_shndx
;
8270 if (shndx
== elf_onesymtab (ibfd
))
8271 shndx
= MAP_ONESYMTAB
;
8272 else if (shndx
== elf_dynsymtab (ibfd
))
8273 shndx
= MAP_DYNSYMTAB
;
8274 else if (shndx
== elf_strtab_sec (ibfd
))
8276 else if (shndx
== elf_shstrtab_sec (ibfd
))
8277 shndx
= MAP_SHSTRTAB
;
8278 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8279 shndx
= MAP_SYM_SHNDX
;
8280 osym
->internal_elf_sym
.st_shndx
= shndx
;
8286 /* Swap out the symbols. */
8289 swap_out_syms (bfd
*abfd
,
8290 struct elf_strtab_hash
**sttp
,
8292 struct bfd_link_info
*info
)
8294 const struct elf_backend_data
*bed
;
8295 unsigned int symcount
;
8297 struct elf_strtab_hash
*stt
;
8298 Elf_Internal_Shdr
*symtab_hdr
;
8299 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8300 Elf_Internal_Shdr
*symstrtab_hdr
;
8301 struct elf_sym_strtab
*symstrtab
;
8302 bfd_byte
*outbound_syms
;
8303 bfd_byte
*outbound_shndx
;
8304 unsigned long outbound_syms_index
;
8306 unsigned int num_locals
;
8308 bool name_local_sections
;
8310 if (!elf_map_symbols (abfd
, &num_locals
))
8313 /* Dump out the symtabs. */
8314 stt
= _bfd_elf_strtab_init ();
8318 bed
= get_elf_backend_data (abfd
);
8319 symcount
= bfd_get_symcount (abfd
);
8320 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8321 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8322 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8323 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8324 symtab_hdr
->sh_info
= num_locals
+ 1;
8325 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8327 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8328 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8330 /* Allocate buffer to swap out the .strtab section. */
8331 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8332 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8334 bfd_set_error (bfd_error_no_memory
);
8335 _bfd_elf_strtab_free (stt
);
8339 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8340 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8343 bfd_set_error (bfd_error_no_memory
);
8346 _bfd_elf_strtab_free (stt
);
8349 symtab_hdr
->contents
= outbound_syms
;
8350 outbound_syms_index
= 0;
8352 outbound_shndx
= NULL
;
8354 if (elf_symtab_shndx_list (abfd
))
8356 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8357 if (symtab_shndx_hdr
->sh_name
!= 0)
8359 if (_bfd_mul_overflow (symcount
+ 1,
8360 sizeof (Elf_External_Sym_Shndx
), &amt
))
8362 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8363 if (outbound_shndx
== NULL
)
8366 symtab_shndx_hdr
->contents
= outbound_shndx
;
8367 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8368 symtab_shndx_hdr
->sh_size
= amt
;
8369 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8370 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8372 /* FIXME: What about any other headers in the list ? */
8375 /* Now generate the data (for "contents"). */
8377 /* Fill in zeroth symbol and swap it out. */
8378 Elf_Internal_Sym sym
;
8384 sym
.st_shndx
= SHN_UNDEF
;
8385 sym
.st_target_internal
= 0;
8386 symstrtab
[0].sym
= sym
;
8387 symstrtab
[0].dest_index
= outbound_syms_index
;
8388 outbound_syms_index
++;
8392 = (bed
->elf_backend_name_local_section_symbols
8393 && bed
->elf_backend_name_local_section_symbols (abfd
));
8395 syms
= bfd_get_outsymbols (abfd
);
8396 for (idx
= 0; idx
< symcount
;)
8398 Elf_Internal_Sym sym
;
8399 bfd_vma value
= syms
[idx
]->value
;
8400 elf_symbol_type
*type_ptr
;
8401 flagword flags
= syms
[idx
]->flags
;
8404 if (!name_local_sections
8405 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8407 /* Local section symbols have no name. */
8408 sym
.st_name
= (unsigned long) -1;
8412 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8413 to get the final offset for st_name. */
8415 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8417 if (sym
.st_name
== (unsigned long) -1)
8421 type_ptr
= elf_symbol_from (syms
[idx
]);
8423 if ((flags
& BSF_SECTION_SYM
) == 0
8424 && bfd_is_com_section (syms
[idx
]->section
))
8426 /* ELF common symbols put the alignment into the `value' field,
8427 and the size into the `size' field. This is backwards from
8428 how BFD handles it, so reverse it here. */
8429 sym
.st_size
= value
;
8430 if (type_ptr
== NULL
8431 || type_ptr
->internal_elf_sym
.st_value
== 0)
8432 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8434 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8435 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8436 (abfd
, syms
[idx
]->section
);
8440 asection
*sec
= syms
[idx
]->section
;
8443 if (sec
->output_section
)
8445 value
+= sec
->output_offset
;
8446 sec
= sec
->output_section
;
8449 /* Don't add in the section vma for relocatable output. */
8450 if (! relocatable_p
)
8452 sym
.st_value
= value
;
8453 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8455 if (bfd_is_abs_section (sec
)
8457 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8459 /* This symbol is in a real ELF section which we did
8460 not create as a BFD section. Undo the mapping done
8461 by copy_private_symbol_data. */
8462 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8466 shndx
= elf_onesymtab (abfd
);
8469 shndx
= elf_dynsymtab (abfd
);
8472 shndx
= elf_strtab_sec (abfd
);
8475 shndx
= elf_shstrtab_sec (abfd
);
8478 if (elf_symtab_shndx_list (abfd
))
8479 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8486 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8488 if (bed
->symbol_section_index
)
8489 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8490 /* Otherwise just leave the index alone. */
8494 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8495 _bfd_error_handler (_("%pB: \
8496 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8505 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8507 if (shndx
== SHN_BAD
)
8511 /* Writing this would be a hell of a lot easier if
8512 we had some decent documentation on bfd, and
8513 knew what to expect of the library, and what to
8514 demand of applications. For example, it
8515 appears that `objcopy' might not set the
8516 section of a symbol to be a section that is
8517 actually in the output file. */
8518 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8520 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8521 if (shndx
== SHN_BAD
)
8523 /* xgettext:c-format */
8525 (_("unable to find equivalent output section"
8526 " for symbol '%s' from section '%s'"),
8527 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8529 bfd_set_error (bfd_error_invalid_operation
);
8535 sym
.st_shndx
= shndx
;
8538 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8540 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8541 type
= STT_GNU_IFUNC
;
8542 else if ((flags
& BSF_FUNCTION
) != 0)
8544 else if ((flags
& BSF_OBJECT
) != 0)
8546 else if ((flags
& BSF_RELC
) != 0)
8548 else if ((flags
& BSF_SRELC
) != 0)
8553 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8556 /* Processor-specific types. */
8557 if (type_ptr
!= NULL
8558 && bed
->elf_backend_get_symbol_type
)
8559 type
= ((*bed
->elf_backend_get_symbol_type
)
8560 (&type_ptr
->internal_elf_sym
, type
));
8562 if (flags
& BSF_SECTION_SYM
)
8564 if (flags
& BSF_GLOBAL
)
8565 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8567 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8569 else if (bfd_is_com_section (syms
[idx
]->section
))
8571 if (type
!= STT_TLS
)
8573 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8574 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8575 ? STT_COMMON
: STT_OBJECT
);
8577 type
= ((flags
& BSF_ELF_COMMON
) != 0
8578 ? STT_COMMON
: STT_OBJECT
);
8580 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8582 else if (bfd_is_und_section (syms
[idx
]->section
))
8583 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8587 else if (flags
& BSF_FILE
)
8588 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8591 int bind
= STB_LOCAL
;
8593 if (flags
& BSF_LOCAL
)
8595 else if (flags
& BSF_GNU_UNIQUE
)
8596 bind
= STB_GNU_UNIQUE
;
8597 else if (flags
& BSF_WEAK
)
8599 else if (flags
& BSF_GLOBAL
)
8602 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8605 if (type_ptr
!= NULL
)
8607 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8608 sym
.st_target_internal
8609 = type_ptr
->internal_elf_sym
.st_target_internal
;
8614 sym
.st_target_internal
= 0;
8618 symstrtab
[idx
].sym
= sym
;
8619 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8621 outbound_syms_index
++;
8624 /* Finalize the .strtab section. */
8625 _bfd_elf_strtab_finalize (stt
);
8627 /* Swap out the .strtab section. */
8628 for (idx
= 0; idx
<= symcount
; idx
++)
8630 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8631 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8632 elfsym
->sym
.st_name
= 0;
8634 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8635 elfsym
->sym
.st_name
);
8636 if (info
&& info
->callbacks
->ctf_new_symbol
)
8637 info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
8640 /* Inform the linker of the addition of this symbol. */
8642 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8644 + (elfsym
->dest_index
8645 * bed
->s
->sizeof_sym
)),
8646 NPTR_ADD (outbound_shndx
,
8648 * sizeof (Elf_External_Sym_Shndx
))));
8653 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8654 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8655 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8656 symstrtab_hdr
->sh_addr
= 0;
8657 symstrtab_hdr
->sh_entsize
= 0;
8658 symstrtab_hdr
->sh_link
= 0;
8659 symstrtab_hdr
->sh_info
= 0;
8660 symstrtab_hdr
->sh_addralign
= 1;
8665 /* Return the number of bytes required to hold the symtab vector.
8667 Note that we base it on the count plus 1, since we will null terminate
8668 the vector allocated based on this size. However, the ELF symbol table
8669 always has a dummy entry as symbol #0, so it ends up even. */
8672 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8674 bfd_size_type symcount
;
8676 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8678 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8679 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
8681 bfd_set_error (bfd_error_file_too_big
);
8684 symtab_size
= symcount
* (sizeof (asymbol
*));
8686 symtab_size
= sizeof (asymbol
*);
8687 else if (!bfd_write_p (abfd
))
8689 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8691 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
8693 bfd_set_error (bfd_error_file_truncated
);
8702 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8704 bfd_size_type symcount
;
8706 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8708 if (elf_dynsymtab (abfd
) == 0)
8710 bfd_set_error (bfd_error_invalid_operation
);
8714 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8715 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
8717 bfd_set_error (bfd_error_file_too_big
);
8720 symtab_size
= symcount
* (sizeof (asymbol
*));
8722 symtab_size
= sizeof (asymbol
*);
8723 else if (!bfd_write_p (abfd
))
8725 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8727 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
8729 bfd_set_error (bfd_error_file_truncated
);
8738 _bfd_elf_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
8740 if (asect
->reloc_count
!= 0 && !bfd_write_p (abfd
))
8742 /* Sanity check reloc section size. */
8743 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8747 struct bfd_elf_section_data
*d
= elf_section_data (asect
);
8748 bfd_size_type rel_size
= d
->rel
.hdr
? d
->rel
.hdr
->sh_size
: 0;
8749 bfd_size_type rela_size
= d
->rela
.hdr
? d
->rela
.hdr
->sh_size
: 0;
8751 if (rel_size
+ rela_size
> filesize
8752 || rel_size
+ rela_size
< rel_size
)
8754 bfd_set_error (bfd_error_file_truncated
);
8760 #if SIZEOF_LONG == SIZEOF_INT
8761 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8763 bfd_set_error (bfd_error_file_too_big
);
8767 return (asect
->reloc_count
+ 1L) * sizeof (arelent
*);
8770 /* Canonicalize the relocs. */
8773 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8780 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8782 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
8785 tblptr
= section
->relocation
;
8786 for (i
= 0; i
< section
->reloc_count
; i
++)
8787 *relptr
++ = tblptr
++;
8791 return section
->reloc_count
;
8795 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8797 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8798 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, false);
8801 abfd
->symcount
= symcount
;
8806 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8807 asymbol
**allocation
)
8809 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8810 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, true);
8813 abfd
->dynsymcount
= symcount
;
8817 /* Return the size required for the dynamic reloc entries. Any loadable
8818 section that was actually installed in the BFD, and has type SHT_REL
8819 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8820 dynamic reloc section. */
8823 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8825 bfd_size_type count
, ext_rel_size
;
8828 if (elf_dynsymtab (abfd
) == 0)
8830 bfd_set_error (bfd_error_invalid_operation
);
8836 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8837 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8838 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8839 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8841 ext_rel_size
+= s
->size
;
8842 if (ext_rel_size
< s
->size
)
8844 bfd_set_error (bfd_error_file_truncated
);
8847 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8848 if (count
> LONG_MAX
/ sizeof (arelent
*))
8850 bfd_set_error (bfd_error_file_too_big
);
8854 if (count
> 1 && !bfd_write_p (abfd
))
8856 /* Sanity check reloc section sizes. */
8857 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8858 if (filesize
!= 0 && ext_rel_size
> filesize
)
8860 bfd_set_error (bfd_error_file_truncated
);
8864 return count
* sizeof (arelent
*);
8867 /* Canonicalize the dynamic relocation entries. Note that we return the
8868 dynamic relocations as a single block, although they are actually
8869 associated with particular sections; the interface, which was
8870 designed for SunOS style shared libraries, expects that there is only
8871 one set of dynamic relocs. Any loadable section that was actually
8872 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8873 dynamic symbol table, is considered to be a dynamic reloc section. */
8876 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8880 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
8884 if (elf_dynsymtab (abfd
) == 0)
8886 bfd_set_error (bfd_error_invalid_operation
);
8890 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8892 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8894 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8895 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8896 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8901 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
8903 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8905 for (i
= 0; i
< count
; i
++)
8916 /* Read in the version information. */
8919 _bfd_elf_slurp_version_tables (bfd
*abfd
, bool default_imported_symver
)
8921 bfd_byte
*contents
= NULL
;
8922 unsigned int freeidx
= 0;
8925 if (elf_dynverref (abfd
) != 0)
8927 Elf_Internal_Shdr
*hdr
;
8928 Elf_External_Verneed
*everneed
;
8929 Elf_Internal_Verneed
*iverneed
;
8931 bfd_byte
*contents_end
;
8933 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8935 if (hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8937 error_return_bad_verref
:
8939 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8940 bfd_set_error (bfd_error_bad_value
);
8941 error_return_verref
:
8942 elf_tdata (abfd
)->verref
= NULL
;
8943 elf_tdata (abfd
)->cverrefs
= 0;
8947 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8948 goto error_return_verref
;
8949 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8950 if (contents
== NULL
)
8951 goto error_return_verref
;
8953 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8955 bfd_set_error (bfd_error_file_too_big
);
8956 goto error_return_verref
;
8959 goto error_return_verref
;
8960 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
8961 if (elf_tdata (abfd
)->verref
== NULL
)
8962 goto error_return_verref
;
8964 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8965 == sizeof (Elf_External_Vernaux
));
8966 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8967 everneed
= (Elf_External_Verneed
*) contents
;
8968 iverneed
= elf_tdata (abfd
)->verref
;
8969 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8971 Elf_External_Vernaux
*evernaux
;
8972 Elf_Internal_Vernaux
*ivernaux
;
8975 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8977 iverneed
->vn_bfd
= abfd
;
8979 iverneed
->vn_filename
=
8980 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8982 if (iverneed
->vn_filename
== NULL
)
8983 goto error_return_bad_verref
;
8985 if (iverneed
->vn_cnt
== 0)
8986 iverneed
->vn_auxptr
= NULL
;
8989 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8990 sizeof (Elf_Internal_Vernaux
), &amt
))
8992 bfd_set_error (bfd_error_file_too_big
);
8993 goto error_return_verref
;
8995 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8996 bfd_alloc (abfd
, amt
);
8997 if (iverneed
->vn_auxptr
== NULL
)
8998 goto error_return_verref
;
9001 if (iverneed
->vn_aux
9002 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9003 goto error_return_bad_verref
;
9005 evernaux
= ((Elf_External_Vernaux
*)
9006 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
9007 ivernaux
= iverneed
->vn_auxptr
;
9008 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
9010 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
9012 ivernaux
->vna_nodename
=
9013 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9014 ivernaux
->vna_name
);
9015 if (ivernaux
->vna_nodename
== NULL
)
9016 goto error_return_bad_verref
;
9018 if (ivernaux
->vna_other
> freeidx
)
9019 freeidx
= ivernaux
->vna_other
;
9021 ivernaux
->vna_nextptr
= NULL
;
9022 if (ivernaux
->vna_next
== 0)
9024 iverneed
->vn_cnt
= j
+ 1;
9027 if (j
+ 1 < iverneed
->vn_cnt
)
9028 ivernaux
->vna_nextptr
= ivernaux
+ 1;
9030 if (ivernaux
->vna_next
9031 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
9032 goto error_return_bad_verref
;
9034 evernaux
= ((Elf_External_Vernaux
*)
9035 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
9038 iverneed
->vn_nextref
= NULL
;
9039 if (iverneed
->vn_next
== 0)
9041 if (i
+ 1 < hdr
->sh_info
)
9042 iverneed
->vn_nextref
= iverneed
+ 1;
9044 if (iverneed
->vn_next
9045 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9046 goto error_return_bad_verref
;
9048 everneed
= ((Elf_External_Verneed
*)
9049 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
9051 elf_tdata (abfd
)->cverrefs
= i
;
9057 if (elf_dynverdef (abfd
) != 0)
9059 Elf_Internal_Shdr
*hdr
;
9060 Elf_External_Verdef
*everdef
;
9061 Elf_Internal_Verdef
*iverdef
;
9062 Elf_Internal_Verdef
*iverdefarr
;
9063 Elf_Internal_Verdef iverdefmem
;
9065 unsigned int maxidx
;
9066 bfd_byte
*contents_end_def
, *contents_end_aux
;
9068 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
9070 if (hdr
->sh_size
< sizeof (Elf_External_Verdef
))
9072 error_return_bad_verdef
:
9074 (_("%pB: .gnu.version_d invalid entry"), abfd
);
9075 bfd_set_error (bfd_error_bad_value
);
9076 error_return_verdef
:
9077 elf_tdata (abfd
)->verdef
= NULL
;
9078 elf_tdata (abfd
)->cverdefs
= 0;
9082 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9083 goto error_return_verdef
;
9084 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
9085 if (contents
== NULL
)
9086 goto error_return_verdef
;
9088 BFD_ASSERT (sizeof (Elf_External_Verdef
)
9089 >= sizeof (Elf_External_Verdaux
));
9090 contents_end_def
= contents
+ hdr
->sh_size
9091 - sizeof (Elf_External_Verdef
);
9092 contents_end_aux
= contents
+ hdr
->sh_size
9093 - sizeof (Elf_External_Verdaux
);
9095 /* We know the number of entries in the section but not the maximum
9096 index. Therefore we have to run through all entries and find
9098 everdef
= (Elf_External_Verdef
*) contents
;
9100 for (i
= 0; i
< hdr
->sh_info
; ++i
)
9102 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9104 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
9105 goto error_return_bad_verdef
;
9106 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
9107 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
9109 if (iverdefmem
.vd_next
== 0)
9112 if (iverdefmem
.vd_next
9113 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
9114 goto error_return_bad_verdef
;
9116 everdef
= ((Elf_External_Verdef
*)
9117 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
9120 if (default_imported_symver
)
9122 if (freeidx
> maxidx
)
9127 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9129 bfd_set_error (bfd_error_file_too_big
);
9130 goto error_return_verdef
;
9132 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9133 if (elf_tdata (abfd
)->verdef
== NULL
)
9134 goto error_return_verdef
;
9136 elf_tdata (abfd
)->cverdefs
= maxidx
;
9138 everdef
= (Elf_External_Verdef
*) contents
;
9139 iverdefarr
= elf_tdata (abfd
)->verdef
;
9140 for (i
= 0; i
< hdr
->sh_info
; i
++)
9142 Elf_External_Verdaux
*everdaux
;
9143 Elf_Internal_Verdaux
*iverdaux
;
9146 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9148 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
9149 goto error_return_bad_verdef
;
9151 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
9152 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
9154 iverdef
->vd_bfd
= abfd
;
9156 if (iverdef
->vd_cnt
== 0)
9157 iverdef
->vd_auxptr
= NULL
;
9160 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
9161 sizeof (Elf_Internal_Verdaux
), &amt
))
9163 bfd_set_error (bfd_error_file_too_big
);
9164 goto error_return_verdef
;
9166 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
9167 bfd_alloc (abfd
, amt
);
9168 if (iverdef
->vd_auxptr
== NULL
)
9169 goto error_return_verdef
;
9173 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
9174 goto error_return_bad_verdef
;
9176 everdaux
= ((Elf_External_Verdaux
*)
9177 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
9178 iverdaux
= iverdef
->vd_auxptr
;
9179 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
9181 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
9183 iverdaux
->vda_nodename
=
9184 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9185 iverdaux
->vda_name
);
9186 if (iverdaux
->vda_nodename
== NULL
)
9187 goto error_return_bad_verdef
;
9189 iverdaux
->vda_nextptr
= NULL
;
9190 if (iverdaux
->vda_next
== 0)
9192 iverdef
->vd_cnt
= j
+ 1;
9195 if (j
+ 1 < iverdef
->vd_cnt
)
9196 iverdaux
->vda_nextptr
= iverdaux
+ 1;
9198 if (iverdaux
->vda_next
9199 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
9200 goto error_return_bad_verdef
;
9202 everdaux
= ((Elf_External_Verdaux
*)
9203 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
9206 iverdef
->vd_nodename
= NULL
;
9207 if (iverdef
->vd_cnt
)
9208 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
9210 iverdef
->vd_nextdef
= NULL
;
9211 if (iverdef
->vd_next
== 0)
9213 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
9214 iverdef
->vd_nextdef
= iverdef
+ 1;
9216 everdef
= ((Elf_External_Verdef
*)
9217 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
9223 else if (default_imported_symver
)
9230 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9232 bfd_set_error (bfd_error_file_too_big
);
9235 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9236 if (elf_tdata (abfd
)->verdef
== NULL
)
9239 elf_tdata (abfd
)->cverdefs
= freeidx
;
9242 /* Create a default version based on the soname. */
9243 if (default_imported_symver
)
9245 Elf_Internal_Verdef
*iverdef
;
9246 Elf_Internal_Verdaux
*iverdaux
;
9248 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
9250 iverdef
->vd_version
= VER_DEF_CURRENT
;
9251 iverdef
->vd_flags
= 0;
9252 iverdef
->vd_ndx
= freeidx
;
9253 iverdef
->vd_cnt
= 1;
9255 iverdef
->vd_bfd
= abfd
;
9257 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
9258 if (iverdef
->vd_nodename
== NULL
)
9259 goto error_return_verdef
;
9260 iverdef
->vd_nextdef
= NULL
;
9261 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
9262 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
9263 if (iverdef
->vd_auxptr
== NULL
)
9264 goto error_return_verdef
;
9266 iverdaux
= iverdef
->vd_auxptr
;
9267 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9278 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9280 elf_symbol_type
*newsym
;
9282 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9285 newsym
->symbol
.the_bfd
= abfd
;
9286 return &newsym
->symbol
;
9290 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9294 bfd_symbol_info (symbol
, ret
);
9297 /* Return whether a symbol name implies a local symbol. Most targets
9298 use this function for the is_local_label_name entry point, but some
9302 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9305 /* Normal local symbols start with ``.L''. */
9306 if (name
[0] == '.' && name
[1] == 'L')
9309 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9310 DWARF debugging symbols starting with ``..''. */
9311 if (name
[0] == '.' && name
[1] == '.')
9314 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9315 emitting DWARF debugging output. I suspect this is actually a
9316 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9317 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9318 underscore to be emitted on some ELF targets). For ease of use,
9319 we treat such symbols as local. */
9320 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9323 /* Treat assembler generated fake symbols, dollar local labels and
9324 forward-backward labels (aka local labels) as locals.
9325 These labels have the form:
9327 L0^A.* (fake symbols)
9329 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9331 Versions which start with .L will have already been matched above,
9332 so we only need to match the rest. */
9333 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9339 for (p
= name
+ 2; (c
= *p
); p
++)
9341 if (c
== 1 || c
== 2)
9343 if (c
== 1 && p
== name
+ 2)
9344 /* A fake symbol. */
9347 /* FIXME: We are being paranoid here and treating symbols like
9348 L0^Bfoo as if there were non-local, on the grounds that the
9349 assembler will never generate them. But can any symbol
9350 containing an ASCII value in the range 1-31 ever be anything
9351 other than some kind of local ? */
9368 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9369 asymbol
*symbol ATTRIBUTE_UNUSED
)
9376 _bfd_elf_set_arch_mach (bfd
*abfd
,
9377 enum bfd_architecture arch
,
9378 unsigned long machine
)
9380 /* If this isn't the right architecture for this backend, and this
9381 isn't the generic backend, fail. */
9382 if (arch
!= get_elf_backend_data (abfd
)->arch
9383 && arch
!= bfd_arch_unknown
9384 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9387 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9390 /* Find the nearest line to a particular section and offset,
9391 for error reporting. */
9394 _bfd_elf_find_nearest_line (bfd
*abfd
,
9398 const char **filename_ptr
,
9399 const char **functionname_ptr
,
9400 unsigned int *line_ptr
,
9401 unsigned int *discriminator_ptr
)
9403 return _bfd_elf_find_nearest_line_with_alt (abfd
, NULL
, symbols
, section
,
9404 offset
, filename_ptr
,
9405 functionname_ptr
, line_ptr
,
9409 /* Find the nearest line to a particular section and offset,
9410 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9411 can be optionally specified. */
9414 _bfd_elf_find_nearest_line_with_alt (bfd
*abfd
,
9415 const char *alt_filename
,
9419 const char **filename_ptr
,
9420 const char **functionname_ptr
,
9421 unsigned int *line_ptr
,
9422 unsigned int *discriminator_ptr
)
9426 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd
, alt_filename
, symbols
, NULL
,
9427 section
, offset
, filename_ptr
,
9428 functionname_ptr
, line_ptr
,
9430 dwarf_debug_sections
,
9431 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9434 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9435 filename_ptr
, functionname_ptr
, line_ptr
))
9437 if (!*functionname_ptr
)
9438 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9439 *filename_ptr
? NULL
: filename_ptr
,
9444 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9445 &found
, filename_ptr
,
9446 functionname_ptr
, line_ptr
,
9447 &elf_tdata (abfd
)->line_info
))
9449 if (found
&& (*functionname_ptr
|| *line_ptr
))
9452 if (symbols
== NULL
)
9455 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9456 filename_ptr
, functionname_ptr
))
9463 /* Find the line for a symbol. */
9466 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9467 const char **filename_ptr
, unsigned int *line_ptr
)
9469 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9470 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9471 filename_ptr
, NULL
, line_ptr
, NULL
,
9472 dwarf_debug_sections
,
9473 &tdata
->dwarf2_find_line_info
);
9476 /* After a call to bfd_find_nearest_line, successive calls to
9477 bfd_find_inliner_info can be used to get source information about
9478 each level of function inlining that terminated at the address
9479 passed to bfd_find_nearest_line. Currently this is only supported
9480 for DWARF2 with appropriate DWARF3 extensions. */
9483 _bfd_elf_find_inliner_info (bfd
*abfd
,
9484 const char **filename_ptr
,
9485 const char **functionname_ptr
,
9486 unsigned int *line_ptr
)
9488 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9489 return _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9490 functionname_ptr
, line_ptr
,
9491 &tdata
->dwarf2_find_line_info
);
9495 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9497 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9498 int ret
= bed
->s
->sizeof_ehdr
;
9500 if (!bfd_link_relocatable (info
))
9502 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9504 if (phdr_size
== (bfd_size_type
) -1)
9506 struct elf_segment_map
*m
;
9509 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9510 phdr_size
+= bed
->s
->sizeof_phdr
;
9513 phdr_size
= get_program_header_size (abfd
, info
);
9516 elf_program_header_size (abfd
) = phdr_size
;
9524 _bfd_elf_set_section_contents (bfd
*abfd
,
9526 const void *location
,
9528 bfd_size_type count
)
9530 Elf_Internal_Shdr
*hdr
;
9532 if (! abfd
->output_has_begun
9533 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9539 hdr
= &elf_section_data (section
)->this_hdr
;
9540 if (hdr
->sh_offset
== (file_ptr
) -1)
9542 unsigned char *contents
;
9544 if (bfd_section_is_ctf (section
))
9545 /* Nothing to do with this section: the contents are generated
9549 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9552 (_("%pB:%pA: error: attempting to write"
9553 " into an unallocated compressed section"),
9555 bfd_set_error (bfd_error_invalid_operation
);
9559 if ((offset
+ count
) > hdr
->sh_size
)
9562 (_("%pB:%pA: error: attempting to write"
9563 " over the end of the section"),
9566 bfd_set_error (bfd_error_invalid_operation
);
9570 contents
= hdr
->contents
;
9571 if (contents
== NULL
)
9574 (_("%pB:%pA: error: attempting to write"
9575 " section into an empty buffer"),
9578 bfd_set_error (bfd_error_invalid_operation
);
9582 memcpy (contents
+ offset
, location
, count
);
9586 return _bfd_generic_set_section_contents (abfd
, section
,
9587 location
, offset
, count
);
9591 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9592 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9593 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9599 /* Try to convert a non-ELF reloc into an ELF one. */
9602 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9604 /* Check whether we really have an ELF howto. */
9606 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9608 bfd_reloc_code_real_type code
;
9609 reloc_howto_type
*howto
;
9611 /* Alien reloc: Try to determine its type to replace it with an
9612 equivalent ELF reloc. */
9614 if (areloc
->howto
->pc_relative
)
9616 switch (areloc
->howto
->bitsize
)
9619 code
= BFD_RELOC_8_PCREL
;
9622 code
= BFD_RELOC_12_PCREL
;
9625 code
= BFD_RELOC_16_PCREL
;
9628 code
= BFD_RELOC_24_PCREL
;
9631 code
= BFD_RELOC_32_PCREL
;
9634 code
= BFD_RELOC_64_PCREL
;
9640 howto
= bfd_reloc_type_lookup (abfd
, code
);
9642 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9644 if (howto
->pcrel_offset
)
9645 areloc
->addend
+= areloc
->address
;
9647 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9652 switch (areloc
->howto
->bitsize
)
9658 code
= BFD_RELOC_14
;
9661 code
= BFD_RELOC_16
;
9664 code
= BFD_RELOC_26
;
9667 code
= BFD_RELOC_32
;
9670 code
= BFD_RELOC_64
;
9676 howto
= bfd_reloc_type_lookup (abfd
, code
);
9680 areloc
->howto
= howto
;
9688 /* xgettext:c-format */
9689 _bfd_error_handler (_("%pB: %s unsupported"),
9690 abfd
, areloc
->howto
->name
);
9691 bfd_set_error (bfd_error_sorry
);
9696 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9698 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9700 && (bfd_get_format (abfd
) == bfd_object
9701 || bfd_get_format (abfd
) == bfd_core
))
9703 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9704 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9705 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9706 _bfd_stab_cleanup (abfd
, &tdata
->line_info
);
9709 return _bfd_generic_close_and_cleanup (abfd
);
9712 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9713 in the relocation's offset. Thus we cannot allow any sort of sanity
9714 range-checking to interfere. There is nothing else to do in processing
9717 bfd_reloc_status_type
9718 _bfd_elf_rel_vtable_reloc_fn
9719 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9720 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9721 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9722 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9724 return bfd_reloc_ok
;
9727 /* Elf core file support. Much of this only works on native
9728 toolchains, since we rely on knowing the
9729 machine-dependent procfs structure in order to pick
9730 out details about the corefile. */
9732 #ifdef HAVE_SYS_PROCFS_H
9733 # include <sys/procfs.h>
9736 /* Return a PID that identifies a "thread" for threaded cores, or the
9737 PID of the main process for non-threaded cores. */
9740 elfcore_make_pid (bfd
*abfd
)
9744 pid
= elf_tdata (abfd
)->core
->lwpid
;
9746 pid
= elf_tdata (abfd
)->core
->pid
;
9751 /* If there isn't a section called NAME, make one, using data from
9752 SECT. Note, this function will generate a reference to NAME, so
9753 you shouldn't deallocate or overwrite it. */
9756 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9760 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9763 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9767 sect2
->size
= sect
->size
;
9768 sect2
->filepos
= sect
->filepos
;
9769 sect2
->alignment_power
= sect
->alignment_power
;
9773 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9774 actually creates up to two pseudosections:
9775 - For the single-threaded case, a section named NAME, unless
9776 such a section already exists.
9777 - For the multi-threaded case, a section named "NAME/PID", where
9778 PID is elfcore_make_pid (abfd).
9779 Both pseudosections have identical contents. */
9781 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9787 char *threaded_name
;
9791 /* Build the section name. */
9793 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9794 len
= strlen (buf
) + 1;
9795 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9796 if (threaded_name
== NULL
)
9798 memcpy (threaded_name
, buf
, len
);
9800 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9805 sect
->filepos
= filepos
;
9806 sect
->alignment_power
= 2;
9808 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9812 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9815 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9821 sect
->size
= note
->descsz
- offs
;
9822 sect
->filepos
= note
->descpos
+ offs
;
9823 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9828 /* prstatus_t exists on:
9830 linux 2.[01] + glibc
9834 #if defined (HAVE_PRSTATUS_T)
9837 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9842 if (note
->descsz
== sizeof (prstatus_t
))
9846 size
= sizeof (prstat
.pr_reg
);
9847 offset
= offsetof (prstatus_t
, pr_reg
);
9848 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9850 /* Do not overwrite the core signal if it
9851 has already been set by another thread. */
9852 if (elf_tdata (abfd
)->core
->signal
== 0)
9853 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9854 if (elf_tdata (abfd
)->core
->pid
== 0)
9855 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9857 /* pr_who exists on:
9860 pr_who doesn't exist on:
9863 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9864 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9866 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9869 #if defined (HAVE_PRSTATUS32_T)
9870 else if (note
->descsz
== sizeof (prstatus32_t
))
9872 /* 64-bit host, 32-bit corefile */
9873 prstatus32_t prstat
;
9875 size
= sizeof (prstat
.pr_reg
);
9876 offset
= offsetof (prstatus32_t
, pr_reg
);
9877 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9879 /* Do not overwrite the core signal if it
9880 has already been set by another thread. */
9881 if (elf_tdata (abfd
)->core
->signal
== 0)
9882 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9883 if (elf_tdata (abfd
)->core
->pid
== 0)
9884 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9886 /* pr_who exists on:
9889 pr_who doesn't exist on:
9892 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9893 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9895 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9898 #endif /* HAVE_PRSTATUS32_T */
9901 /* Fail - we don't know how to handle any other
9902 note size (ie. data object type). */
9906 /* Make a ".reg/999" section and a ".reg" section. */
9907 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9908 size
, note
->descpos
+ offset
);
9910 #endif /* defined (HAVE_PRSTATUS_T) */
9912 /* Create a pseudosection containing the exact contents of NOTE. */
9914 elfcore_make_note_pseudosection (bfd
*abfd
,
9916 Elf_Internal_Note
*note
)
9918 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9919 note
->descsz
, note
->descpos
);
9922 /* There isn't a consistent prfpregset_t across platforms,
9923 but it doesn't matter, because we don't have to pick this
9924 data structure apart. */
9927 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9929 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9932 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9933 type of NT_PRXFPREG. Just include the whole note's contents
9937 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9939 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9942 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9943 with a note type of NT_X86_XSTATE. Just include the whole note's
9944 contents literally. */
9947 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9949 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9953 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9955 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9959 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9961 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9965 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9967 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9971 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9973 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9977 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9979 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9983 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9985 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9989 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9991 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9995 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9997 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
10001 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10003 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
10007 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10009 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
10013 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10015 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
10019 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
10021 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
10025 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
10027 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
10031 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10033 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
10037 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10039 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
10043 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
10045 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
10049 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
10051 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
10055 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
10057 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
10061 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10063 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
10067 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
10069 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
10073 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
10075 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
10079 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10081 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
10085 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
10087 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
10091 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
10093 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
10097 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
10099 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
10103 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
10105 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
10109 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
10111 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
10115 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
10117 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
10121 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
10123 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
10127 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
10129 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
10133 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10135 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
10139 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
10141 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
10145 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
10147 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
10151 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
10153 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
10157 elfcore_grok_aarch_mte (bfd
*abfd
, Elf_Internal_Note
*note
)
10159 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-mte",
10164 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
10166 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
10169 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10170 successful otherwise, return FALSE. */
10173 elfcore_grok_riscv_csr (bfd
*abfd
, Elf_Internal_Note
*note
)
10175 return elfcore_make_note_pseudosection (abfd
, ".reg-riscv-csr", note
);
10178 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10179 successful otherwise, return FALSE. */
10182 elfcore_grok_gdb_tdesc (bfd
*abfd
, Elf_Internal_Note
*note
)
10184 return elfcore_make_note_pseudosection (abfd
, ".gdb-tdesc", note
);
10188 elfcore_grok_loongarch_cpucfg (bfd
*abfd
, Elf_Internal_Note
*note
)
10190 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-cpucfg", note
);
10194 elfcore_grok_loongarch_lbt (bfd
*abfd
, Elf_Internal_Note
*note
)
10196 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lbt", note
);
10200 elfcore_grok_loongarch_lsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10202 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lsx", note
);
10206 elfcore_grok_loongarch_lasx (bfd
*abfd
, Elf_Internal_Note
*note
)
10208 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lasx", note
);
10211 #if defined (HAVE_PRPSINFO_T)
10212 typedef prpsinfo_t elfcore_psinfo_t
;
10213 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10214 typedef prpsinfo32_t elfcore_psinfo32_t
;
10218 #if defined (HAVE_PSINFO_T)
10219 typedef psinfo_t elfcore_psinfo_t
;
10220 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10221 typedef psinfo32_t elfcore_psinfo32_t
;
10225 /* return a malloc'ed copy of a string at START which is at
10226 most MAX bytes long, possibly without a terminating '\0'.
10227 the copy will always have a terminating '\0'. */
10230 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
10233 char *end
= (char *) memchr (start
, '\0', max
);
10241 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
10245 memcpy (dups
, start
, len
);
10251 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10253 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10255 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
10257 elfcore_psinfo_t psinfo
;
10259 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10261 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10262 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10264 elf_tdata (abfd
)->core
->program
10265 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10266 sizeof (psinfo
.pr_fname
));
10268 elf_tdata (abfd
)->core
->command
10269 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10270 sizeof (psinfo
.pr_psargs
));
10272 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10273 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
10275 /* 64-bit host, 32-bit corefile */
10276 elfcore_psinfo32_t psinfo
;
10278 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10280 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10281 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10283 elf_tdata (abfd
)->core
->program
10284 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10285 sizeof (psinfo
.pr_fname
));
10287 elf_tdata (abfd
)->core
->command
10288 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10289 sizeof (psinfo
.pr_psargs
));
10295 /* Fail - we don't know how to handle any other
10296 note size (ie. data object type). */
10300 /* Note that for some reason, a spurious space is tacked
10301 onto the end of the args in some (at least one anyway)
10302 implementations, so strip it off if it exists. */
10305 char *command
= elf_tdata (abfd
)->core
->command
;
10306 int n
= strlen (command
);
10308 if (0 < n
&& command
[n
- 1] == ' ')
10309 command
[n
- 1] = '\0';
10314 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10316 #if defined (HAVE_PSTATUS_T)
10318 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10320 if (note
->descsz
== sizeof (pstatus_t
)
10321 #if defined (HAVE_PXSTATUS_T)
10322 || note
->descsz
== sizeof (pxstatus_t
)
10328 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10330 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10332 #if defined (HAVE_PSTATUS32_T)
10333 else if (note
->descsz
== sizeof (pstatus32_t
))
10335 /* 64-bit host, 32-bit corefile */
10338 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10340 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10343 /* Could grab some more details from the "representative"
10344 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10345 NT_LWPSTATUS note, presumably. */
10349 #endif /* defined (HAVE_PSTATUS_T) */
10351 #if defined (HAVE_LWPSTATUS_T)
10353 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10355 lwpstatus_t lwpstat
;
10361 if (note
->descsz
!= sizeof (lwpstat
)
10362 #if defined (HAVE_LWPXSTATUS_T)
10363 && note
->descsz
!= sizeof (lwpxstatus_t
)
10368 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10370 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10371 /* Do not overwrite the core signal if it has already been set by
10373 if (elf_tdata (abfd
)->core
->signal
== 0)
10374 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10376 /* Make a ".reg/999" section. */
10378 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10379 len
= strlen (buf
) + 1;
10380 name
= bfd_alloc (abfd
, len
);
10383 memcpy (name
, buf
, len
);
10385 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10389 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10390 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10391 sect
->filepos
= note
->descpos
10392 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10395 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10396 sect
->size
= sizeof (lwpstat
.pr_reg
);
10397 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10400 sect
->alignment_power
= 2;
10402 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10405 /* Make a ".reg2/999" section */
10407 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10408 len
= strlen (buf
) + 1;
10409 name
= bfd_alloc (abfd
, len
);
10412 memcpy (name
, buf
, len
);
10414 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10418 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10419 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10420 sect
->filepos
= note
->descpos
10421 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10424 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10425 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10426 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10429 sect
->alignment_power
= 2;
10431 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10433 #endif /* defined (HAVE_LWPSTATUS_T) */
10435 /* These constants, and the structure offsets used below, are defined by
10436 Cygwin's core_dump.h */
10437 #define NOTE_INFO_PROCESS 1
10438 #define NOTE_INFO_THREAD 2
10439 #define NOTE_INFO_MODULE 3
10440 #define NOTE_INFO_MODULE64 4
10443 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10448 unsigned int name_size
;
10451 int is_active_thread
;
10454 if (note
->descsz
< 4)
10457 if (! startswith (note
->namedata
, "win32"))
10460 type
= bfd_get_32 (abfd
, note
->descdata
);
10464 const char *type_name
;
10465 unsigned long min_size
;
10468 { "NOTE_INFO_PROCESS", 12 },
10469 { "NOTE_INFO_THREAD", 12 },
10470 { "NOTE_INFO_MODULE", 12 },
10471 { "NOTE_INFO_MODULE64", 16 },
10474 if (type
== 0 || type
> (sizeof(size_check
)/sizeof(size_check
[0])))
10477 if (note
->descsz
< size_check
[type
- 1].min_size
)
10479 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10481 abfd
, size_check
[type
- 1].type_name
, note
->descsz
);
10487 case NOTE_INFO_PROCESS
:
10488 /* FIXME: need to add ->core->command. */
10489 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10490 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10493 case NOTE_INFO_THREAD
:
10494 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10496 /* thread_info.tid */
10497 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 4));
10499 len
= strlen (buf
) + 1;
10500 name
= (char *) bfd_alloc (abfd
, len
);
10504 memcpy (name
, buf
, len
);
10506 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10510 /* sizeof (thread_info.thread_context) */
10511 sect
->size
= note
->descsz
- 12;
10512 /* offsetof (thread_info.thread_context) */
10513 sect
->filepos
= note
->descpos
+ 12;
10514 sect
->alignment_power
= 2;
10516 /* thread_info.is_active_thread */
10517 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10519 if (is_active_thread
)
10520 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10524 case NOTE_INFO_MODULE
:
10525 case NOTE_INFO_MODULE64
:
10526 /* Make a ".module/xxxxxxxx" section. */
10527 if (type
== NOTE_INFO_MODULE
)
10529 /* module_info.base_address */
10530 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10531 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10532 /* module_info.module_name_size */
10533 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10535 else /* NOTE_INFO_MODULE64 */
10537 /* module_info.base_address */
10538 base_addr
= bfd_get_64 (abfd
, note
->descdata
+ 4);
10539 sprintf (buf
, ".module/%016lx", (unsigned long) base_addr
);
10540 /* module_info.module_name_size */
10541 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10544 len
= strlen (buf
) + 1;
10545 name
= (char *) bfd_alloc (abfd
, len
);
10549 memcpy (name
, buf
, len
);
10551 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10556 if (note
->descsz
< 12 + name_size
)
10558 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10559 " is too small to contain a name of size %u"),
10560 abfd
, note
->descsz
, name_size
);
10564 sect
->size
= note
->descsz
;
10565 sect
->filepos
= note
->descpos
;
10566 sect
->alignment_power
= 2;
10577 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10579 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10581 switch (note
->type
)
10587 if (bed
->elf_backend_grok_prstatus
)
10588 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10590 #if defined (HAVE_PRSTATUS_T)
10591 return elfcore_grok_prstatus (abfd
, note
);
10596 #if defined (HAVE_PSTATUS_T)
10598 return elfcore_grok_pstatus (abfd
, note
);
10601 #if defined (HAVE_LWPSTATUS_T)
10603 return elfcore_grok_lwpstatus (abfd
, note
);
10606 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10607 return elfcore_grok_prfpreg (abfd
, note
);
10609 case NT_WIN32PSTATUS
:
10610 return elfcore_grok_win32pstatus (abfd
, note
);
10612 case NT_PRXFPREG
: /* Linux SSE extension */
10613 if (note
->namesz
== 6
10614 && strcmp (note
->namedata
, "LINUX") == 0)
10615 return elfcore_grok_prxfpreg (abfd
, note
);
10619 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10620 if (note
->namesz
== 6
10621 && strcmp (note
->namedata
, "LINUX") == 0)
10622 return elfcore_grok_xstatereg (abfd
, note
);
10627 if (note
->namesz
== 6
10628 && strcmp (note
->namedata
, "LINUX") == 0)
10629 return elfcore_grok_ppc_vmx (abfd
, note
);
10634 if (note
->namesz
== 6
10635 && strcmp (note
->namedata
, "LINUX") == 0)
10636 return elfcore_grok_ppc_vsx (abfd
, note
);
10641 if (note
->namesz
== 6
10642 && strcmp (note
->namedata
, "LINUX") == 0)
10643 return elfcore_grok_ppc_tar (abfd
, note
);
10648 if (note
->namesz
== 6
10649 && strcmp (note
->namedata
, "LINUX") == 0)
10650 return elfcore_grok_ppc_ppr (abfd
, note
);
10655 if (note
->namesz
== 6
10656 && strcmp (note
->namedata
, "LINUX") == 0)
10657 return elfcore_grok_ppc_dscr (abfd
, note
);
10662 if (note
->namesz
== 6
10663 && strcmp (note
->namedata
, "LINUX") == 0)
10664 return elfcore_grok_ppc_ebb (abfd
, note
);
10669 if (note
->namesz
== 6
10670 && strcmp (note
->namedata
, "LINUX") == 0)
10671 return elfcore_grok_ppc_pmu (abfd
, note
);
10675 case NT_PPC_TM_CGPR
:
10676 if (note
->namesz
== 6
10677 && strcmp (note
->namedata
, "LINUX") == 0)
10678 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10682 case NT_PPC_TM_CFPR
:
10683 if (note
->namesz
== 6
10684 && strcmp (note
->namedata
, "LINUX") == 0)
10685 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10689 case NT_PPC_TM_CVMX
:
10690 if (note
->namesz
== 6
10691 && strcmp (note
->namedata
, "LINUX") == 0)
10692 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10696 case NT_PPC_TM_CVSX
:
10697 if (note
->namesz
== 6
10698 && strcmp (note
->namedata
, "LINUX") == 0)
10699 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10703 case NT_PPC_TM_SPR
:
10704 if (note
->namesz
== 6
10705 && strcmp (note
->namedata
, "LINUX") == 0)
10706 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10710 case NT_PPC_TM_CTAR
:
10711 if (note
->namesz
== 6
10712 && strcmp (note
->namedata
, "LINUX") == 0)
10713 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10717 case NT_PPC_TM_CPPR
:
10718 if (note
->namesz
== 6
10719 && strcmp (note
->namedata
, "LINUX") == 0)
10720 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10724 case NT_PPC_TM_CDSCR
:
10725 if (note
->namesz
== 6
10726 && strcmp (note
->namedata
, "LINUX") == 0)
10727 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10731 case NT_S390_HIGH_GPRS
:
10732 if (note
->namesz
== 6
10733 && strcmp (note
->namedata
, "LINUX") == 0)
10734 return elfcore_grok_s390_high_gprs (abfd
, note
);
10738 case NT_S390_TIMER
:
10739 if (note
->namesz
== 6
10740 && strcmp (note
->namedata
, "LINUX") == 0)
10741 return elfcore_grok_s390_timer (abfd
, note
);
10745 case NT_S390_TODCMP
:
10746 if (note
->namesz
== 6
10747 && strcmp (note
->namedata
, "LINUX") == 0)
10748 return elfcore_grok_s390_todcmp (abfd
, note
);
10752 case NT_S390_TODPREG
:
10753 if (note
->namesz
== 6
10754 && strcmp (note
->namedata
, "LINUX") == 0)
10755 return elfcore_grok_s390_todpreg (abfd
, note
);
10760 if (note
->namesz
== 6
10761 && strcmp (note
->namedata
, "LINUX") == 0)
10762 return elfcore_grok_s390_ctrs (abfd
, note
);
10766 case NT_S390_PREFIX
:
10767 if (note
->namesz
== 6
10768 && strcmp (note
->namedata
, "LINUX") == 0)
10769 return elfcore_grok_s390_prefix (abfd
, note
);
10773 case NT_S390_LAST_BREAK
:
10774 if (note
->namesz
== 6
10775 && strcmp (note
->namedata
, "LINUX") == 0)
10776 return elfcore_grok_s390_last_break (abfd
, note
);
10780 case NT_S390_SYSTEM_CALL
:
10781 if (note
->namesz
== 6
10782 && strcmp (note
->namedata
, "LINUX") == 0)
10783 return elfcore_grok_s390_system_call (abfd
, note
);
10788 if (note
->namesz
== 6
10789 && strcmp (note
->namedata
, "LINUX") == 0)
10790 return elfcore_grok_s390_tdb (abfd
, note
);
10794 case NT_S390_VXRS_LOW
:
10795 if (note
->namesz
== 6
10796 && strcmp (note
->namedata
, "LINUX") == 0)
10797 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10801 case NT_S390_VXRS_HIGH
:
10802 if (note
->namesz
== 6
10803 && strcmp (note
->namedata
, "LINUX") == 0)
10804 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10808 case NT_S390_GS_CB
:
10809 if (note
->namesz
== 6
10810 && strcmp (note
->namedata
, "LINUX") == 0)
10811 return elfcore_grok_s390_gs_cb (abfd
, note
);
10815 case NT_S390_GS_BC
:
10816 if (note
->namesz
== 6
10817 && strcmp (note
->namedata
, "LINUX") == 0)
10818 return elfcore_grok_s390_gs_bc (abfd
, note
);
10823 if (note
->namesz
== 6
10824 && strcmp (note
->namedata
, "LINUX") == 0)
10825 return elfcore_grok_arc_v2 (abfd
, note
);
10830 if (note
->namesz
== 6
10831 && strcmp (note
->namedata
, "LINUX") == 0)
10832 return elfcore_grok_arm_vfp (abfd
, note
);
10837 if (note
->namesz
== 6
10838 && strcmp (note
->namedata
, "LINUX") == 0)
10839 return elfcore_grok_aarch_tls (abfd
, note
);
10843 case NT_ARM_HW_BREAK
:
10844 if (note
->namesz
== 6
10845 && strcmp (note
->namedata
, "LINUX") == 0)
10846 return elfcore_grok_aarch_hw_break (abfd
, note
);
10850 case NT_ARM_HW_WATCH
:
10851 if (note
->namesz
== 6
10852 && strcmp (note
->namedata
, "LINUX") == 0)
10853 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10858 if (note
->namesz
== 6
10859 && strcmp (note
->namedata
, "LINUX") == 0)
10860 return elfcore_grok_aarch_sve (abfd
, note
);
10864 case NT_ARM_PAC_MASK
:
10865 if (note
->namesz
== 6
10866 && strcmp (note
->namedata
, "LINUX") == 0)
10867 return elfcore_grok_aarch_pauth (abfd
, note
);
10871 case NT_ARM_TAGGED_ADDR_CTRL
:
10872 if (note
->namesz
== 6
10873 && strcmp (note
->namedata
, "LINUX") == 0)
10874 return elfcore_grok_aarch_mte (abfd
, note
);
10879 if (note
->namesz
== 4
10880 && strcmp (note
->namedata
, "GDB") == 0)
10881 return elfcore_grok_gdb_tdesc (abfd
, note
);
10886 if (note
->namesz
== 4
10887 && strcmp (note
->namedata
, "GDB") == 0)
10888 return elfcore_grok_riscv_csr (abfd
, note
);
10892 case NT_LARCH_CPUCFG
:
10893 if (note
->namesz
== 6
10894 && strcmp (note
->namedata
, "LINUX") == 0)
10895 return elfcore_grok_loongarch_cpucfg (abfd
, note
);
10900 if (note
->namesz
== 6
10901 && strcmp (note
->namedata
, "LINUX") == 0)
10902 return elfcore_grok_loongarch_lbt (abfd
, note
);
10907 if (note
->namesz
== 6
10908 && strcmp (note
->namedata
, "LINUX") == 0)
10909 return elfcore_grok_loongarch_lsx (abfd
, note
);
10913 case NT_LARCH_LASX
:
10914 if (note
->namesz
== 6
10915 && strcmp (note
->namedata
, "LINUX") == 0)
10916 return elfcore_grok_loongarch_lasx (abfd
, note
);
10922 if (bed
->elf_backend_grok_psinfo
)
10923 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10925 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10926 return elfcore_grok_psinfo (abfd
, note
);
10932 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10935 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10939 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10946 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10948 struct bfd_build_id
* build_id
;
10950 if (note
->descsz
== 0)
10953 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10954 if (build_id
== NULL
)
10957 build_id
->size
= note
->descsz
;
10958 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10959 abfd
->build_id
= build_id
;
10965 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10967 switch (note
->type
)
10972 case NT_GNU_PROPERTY_TYPE_0
:
10973 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10975 case NT_GNU_BUILD_ID
:
10976 return elfobj_grok_gnu_build_id (abfd
, note
);
10981 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10983 struct sdt_note
*cur
=
10984 (struct sdt_note
*) bfd_alloc (abfd
,
10985 sizeof (struct sdt_note
) + note
->descsz
);
10987 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10988 cur
->size
= (bfd_size_type
) note
->descsz
;
10989 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10991 elf_tdata (abfd
)->sdt_note_head
= cur
;
10997 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10999 switch (note
->type
)
11002 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
11010 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11014 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11017 if (note
->descsz
< 108)
11022 if (note
->descsz
< 120)
11030 /* Check for version 1 in pr_version. */
11031 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11036 /* Skip over pr_psinfosz. */
11037 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11041 offset
+= 4; /* Padding before pr_psinfosz. */
11045 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11046 elf_tdata (abfd
)->core
->program
11047 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
11050 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11051 elf_tdata (abfd
)->core
->command
11052 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
11055 /* Padding before pr_pid. */
11058 /* The pr_pid field was added in version "1a". */
11059 if (note
->descsz
< offset
+ 4)
11062 elf_tdata (abfd
)->core
->pid
11063 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11069 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
11075 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11076 Also compute minimum size of this note. */
11077 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11081 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
11085 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
11086 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
11093 if (note
->descsz
< min_size
)
11096 /* Check for version 1 in pr_version. */
11097 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11100 /* Extract size of pr_reg from pr_gregsetsz. */
11101 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11102 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11104 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11109 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11113 /* Skip over pr_osreldate. */
11116 /* Read signal from pr_cursig. */
11117 if (elf_tdata (abfd
)->core
->signal
== 0)
11118 elf_tdata (abfd
)->core
->signal
11119 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11122 /* Read TID from pr_pid. */
11123 elf_tdata (abfd
)->core
->lwpid
11124 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11127 /* Padding before pr_reg. */
11128 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
11131 /* Make sure that there is enough data remaining in the note. */
11132 if ((note
->descsz
- offset
) < size
)
11135 /* Make a ".reg/999" section and a ".reg" section. */
11136 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
11137 size
, note
->descpos
+ offset
);
11141 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11143 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11145 switch (note
->type
)
11148 if (bed
->elf_backend_grok_freebsd_prstatus
)
11149 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
11151 return elfcore_grok_freebsd_prstatus (abfd
, note
);
11154 return elfcore_grok_prfpreg (abfd
, note
);
11157 return elfcore_grok_freebsd_psinfo (abfd
, note
);
11159 case NT_FREEBSD_THRMISC
:
11160 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
11162 case NT_FREEBSD_PROCSTAT_PROC
:
11163 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
11166 case NT_FREEBSD_PROCSTAT_FILES
:
11167 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
11170 case NT_FREEBSD_PROCSTAT_VMMAP
:
11171 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
11174 case NT_FREEBSD_PROCSTAT_AUXV
:
11175 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11177 case NT_FREEBSD_X86_SEGBASES
:
11178 return elfcore_make_note_pseudosection (abfd
, ".reg-x86-segbases", note
);
11180 case NT_X86_XSTATE
:
11181 return elfcore_grok_xstatereg (abfd
, note
);
11183 case NT_FREEBSD_PTLWPINFO
:
11184 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
11188 return elfcore_grok_aarch_tls (abfd
, note
);
11191 return elfcore_grok_arm_vfp (abfd
, note
);
11199 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
11203 cp
= strchr (note
->namedata
, '@');
11206 *lwpidp
= atoi(cp
+ 1);
11213 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11215 if (note
->descsz
<= 0x7c + 31)
11218 /* Signal number at offset 0x08. */
11219 elf_tdata (abfd
)->core
->signal
11220 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11222 /* Process ID at offset 0x50. */
11223 elf_tdata (abfd
)->core
->pid
11224 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
11226 /* Command name at 0x7c (max 32 bytes, including nul). */
11227 elf_tdata (abfd
)->core
->command
11228 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
11230 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
11235 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11239 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
11240 elf_tdata (abfd
)->core
->lwpid
= lwp
;
11242 switch (note
->type
)
11244 case NT_NETBSDCORE_PROCINFO
:
11245 /* NetBSD-specific core "procinfo". Note that we expect to
11246 find this note before any of the others, which is fine,
11247 since the kernel writes this note out first when it
11248 creates a core file. */
11249 return elfcore_grok_netbsd_procinfo (abfd
, note
);
11250 case NT_NETBSDCORE_AUXV
:
11251 /* NetBSD-specific Elf Auxiliary Vector data. */
11252 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11253 case NT_NETBSDCORE_LWPSTATUS
:
11254 return elfcore_make_note_pseudosection (abfd
,
11255 ".note.netbsdcore.lwpstatus",
11261 /* As of March 2020 there are no other machine-independent notes
11262 defined for NetBSD core files. If the note type is less
11263 than the start of the machine-dependent note types, we don't
11266 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
11270 switch (bfd_get_arch (abfd
))
11272 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11273 PT_GETFPREGS == mach+2. */
11275 case bfd_arch_aarch64
:
11276 case bfd_arch_alpha
:
11277 case bfd_arch_sparc
:
11278 switch (note
->type
)
11280 case NT_NETBSDCORE_FIRSTMACH
+0:
11281 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11283 case NT_NETBSDCORE_FIRSTMACH
+2:
11284 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11290 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11291 There's also old PT___GETREGS40 == mach + 1 for old reg
11292 structure which lacks GBR. */
11295 switch (note
->type
)
11297 case NT_NETBSDCORE_FIRSTMACH
+3:
11298 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11300 case NT_NETBSDCORE_FIRSTMACH
+5:
11301 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11307 /* On all other arch's, PT_GETREGS == mach+1 and
11308 PT_GETFPREGS == mach+3. */
11311 switch (note
->type
)
11313 case NT_NETBSDCORE_FIRSTMACH
+1:
11314 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11316 case NT_NETBSDCORE_FIRSTMACH
+3:
11317 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11327 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11329 if (note
->descsz
<= 0x48 + 31)
11332 /* Signal number at offset 0x08. */
11333 elf_tdata (abfd
)->core
->signal
11334 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11336 /* Process ID at offset 0x20. */
11337 elf_tdata (abfd
)->core
->pid
11338 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
11340 /* Command name at 0x48 (max 32 bytes, including nul). */
11341 elf_tdata (abfd
)->core
->command
11342 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
11347 /* Processes Solaris's process status note.
11348 sig_off ~ offsetof(prstatus_t, pr_cursig)
11349 pid_off ~ offsetof(prstatus_t, pr_pid)
11350 lwpid_off ~ offsetof(prstatus_t, pr_who)
11351 gregset_size ~ sizeof(gregset_t)
11352 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11355 elfcore_grok_solaris_prstatus (bfd
*abfd
, Elf_Internal_Note
* note
, int sig_off
,
11356 int pid_off
, int lwpid_off
, size_t gregset_size
,
11357 size_t gregset_offset
)
11359 asection
*sect
= NULL
;
11360 elf_tdata (abfd
)->core
->signal
11361 = bfd_get_16 (abfd
, note
->descdata
+ sig_off
);
11362 elf_tdata (abfd
)->core
->pid
11363 = bfd_get_32 (abfd
, note
->descdata
+ pid_off
);
11364 elf_tdata (abfd
)->core
->lwpid
11365 = bfd_get_32 (abfd
, note
->descdata
+ lwpid_off
);
11367 sect
= bfd_get_section_by_name (abfd
, ".reg");
11369 sect
->size
= gregset_size
;
11371 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11372 note
->descpos
+ gregset_offset
);
11375 /* Gets program and arguments from a core.
11376 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11377 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11380 elfcore_grok_solaris_info(bfd
*abfd
, Elf_Internal_Note
* note
,
11381 int prog_off
, int comm_off
)
11383 elf_tdata (abfd
)->core
->program
11384 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ prog_off
, 16);
11385 elf_tdata (abfd
)->core
->command
11386 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ comm_off
, 80);
11391 /* Processes Solaris's LWP status note.
11392 gregset_size ~ sizeof(gregset_t)
11393 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11394 fpregset_size ~ sizeof(fpregset_t)
11395 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11398 elfcore_grok_solaris_lwpstatus (bfd
*abfd
, Elf_Internal_Note
* note
,
11399 size_t gregset_size
, int gregset_off
,
11400 size_t fpregset_size
, int fpregset_off
)
11402 asection
*sect
= NULL
;
11403 char reg2_section_name
[16] = { 0 };
11405 (void) snprintf (reg2_section_name
, 16, "%s/%i", ".reg2",
11406 elf_tdata (abfd
)->core
->lwpid
);
11408 /* offsetof(lwpstatus_t, pr_lwpid) */
11409 elf_tdata (abfd
)->core
->lwpid
11410 = bfd_get_32 (abfd
, note
->descdata
+ 4);
11411 /* offsetof(lwpstatus_t, pr_cursig) */
11412 elf_tdata (abfd
)->core
->signal
11413 = bfd_get_16 (abfd
, note
->descdata
+ 12);
11415 sect
= bfd_get_section_by_name (abfd
, ".reg");
11417 sect
->size
= gregset_size
;
11418 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11419 note
->descpos
+ gregset_off
))
11422 sect
= bfd_get_section_by_name (abfd
, reg2_section_name
);
11425 sect
->size
= fpregset_size
;
11426 sect
->filepos
= note
->descpos
+ fpregset_off
;
11427 sect
->alignment_power
= 2;
11429 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg2", fpregset_size
,
11430 note
->descpos
+ fpregset_off
))
11437 elfcore_grok_solaris_note_impl (bfd
*abfd
, Elf_Internal_Note
*note
)
11442 /* core files are identified as 32- or 64-bit, SPARC or x86,
11443 by the size of the descsz which matches the sizeof()
11444 the type appropriate for that note type (e.g., prstatus_t for
11445 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11446 on Solaris. The core file bitness may differ from the bitness of
11447 gdb itself, so fixed values are used instead of sizeof().
11448 Appropriate fixed offsets are also used to obtain data from
11451 switch ((int) note
->type
)
11453 case SOLARIS_NT_PRSTATUS
:
11454 switch (note
->descsz
)
11456 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11457 return elfcore_grok_solaris_prstatus(abfd
, note
,
11458 136, 216, 308, 152, 356);
11459 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11460 return elfcore_grok_solaris_prstatus(abfd
, note
,
11461 264, 360, 520, 304, 600);
11462 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11463 return elfcore_grok_solaris_prstatus(abfd
, note
,
11464 136, 216, 308, 76, 356);
11465 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11466 return elfcore_grok_solaris_prstatus(abfd
, note
,
11467 264, 360, 520, 224, 600);
11472 case SOLARIS_NT_PSINFO
:
11473 case SOLARIS_NT_PRPSINFO
:
11474 switch (note
->descsz
)
11476 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11477 return elfcore_grok_solaris_info(abfd
, note
, 84, 100);
11478 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11479 return elfcore_grok_solaris_info(abfd
, note
, 120, 136);
11480 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11481 return elfcore_grok_solaris_info(abfd
, note
, 88, 104);
11482 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11483 return elfcore_grok_solaris_info(abfd
, note
, 136, 152);
11488 case SOLARIS_NT_LWPSTATUS
:
11489 switch (note
->descsz
)
11491 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11492 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
11493 152, 344, 400, 496);
11494 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11495 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
11496 304, 544, 544, 848);
11497 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11498 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
11499 76, 344, 380, 420);
11500 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11501 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
11502 224, 544, 528, 768);
11507 case SOLARIS_NT_LWPSINFO
:
11508 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11509 if (note
->descsz
== 128 || note
->descsz
== 152)
11510 elf_tdata (abfd
)->core
->lwpid
=
11511 bfd_get_32 (abfd
, note
->descdata
+ 4);
11521 /* For name starting with "CORE" this may be either a Solaris
11522 core file or a gdb-generated core file. Do Solaris-specific
11523 processing on selected note types first with
11524 elfcore_grok_solaris_note(), then process the note
11525 in elfcore_grok_note(). */
11528 elfcore_grok_solaris_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11530 if (!elfcore_grok_solaris_note_impl (abfd
, note
))
11533 return elfcore_grok_note (abfd
, note
);
11537 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11539 if (note
->type
== NT_OPENBSD_PROCINFO
)
11540 return elfcore_grok_openbsd_procinfo (abfd
, note
);
11542 if (note
->type
== NT_OPENBSD_REGS
)
11543 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11545 if (note
->type
== NT_OPENBSD_FPREGS
)
11546 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11548 if (note
->type
== NT_OPENBSD_XFPREGS
)
11549 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
11551 if (note
->type
== NT_OPENBSD_AUXV
)
11552 return elfcore_make_auxv_note_section (abfd
, note
, 0);
11554 if (note
->type
== NT_OPENBSD_WCOOKIE
)
11556 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
11561 sect
->size
= note
->descsz
;
11562 sect
->filepos
= note
->descpos
;
11563 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
11572 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
11574 void *ddata
= note
->descdata
;
11581 if (note
->descsz
< 16)
11584 /* nto_procfs_status 'pid' field is at offset 0. */
11585 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
11587 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11588 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
11590 /* nto_procfs_status 'flags' field is at offset 8. */
11591 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
11593 /* nto_procfs_status 'what' field is at offset 14. */
11594 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
11596 elf_tdata (abfd
)->core
->signal
= sig
;
11597 elf_tdata (abfd
)->core
->lwpid
= *tid
;
11600 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11601 do not come from signals so we make sure we set the current
11602 thread just in case. */
11603 if (flags
& 0x00000080)
11604 elf_tdata (abfd
)->core
->lwpid
= *tid
;
11606 /* Make a ".qnx_core_status/%d" section. */
11607 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
11609 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11612 strcpy (name
, buf
);
11614 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11618 sect
->size
= note
->descsz
;
11619 sect
->filepos
= note
->descpos
;
11620 sect
->alignment_power
= 2;
11622 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
11626 elfcore_grok_nto_regs (bfd
*abfd
,
11627 Elf_Internal_Note
*note
,
11635 /* Make a "(base)/%d" section. */
11636 sprintf (buf
, "%s/%ld", base
, tid
);
11638 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11641 strcpy (name
, buf
);
11643 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11647 sect
->size
= note
->descsz
;
11648 sect
->filepos
= note
->descpos
;
11649 sect
->alignment_power
= 2;
11651 /* This is the current thread. */
11652 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
11653 return elfcore_maybe_make_sect (abfd
, base
, sect
);
11658 #define BFD_QNT_CORE_INFO 7
11659 #define BFD_QNT_CORE_STATUS 8
11660 #define BFD_QNT_CORE_GREG 9
11661 #define BFD_QNT_CORE_FPREG 10
11664 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11666 /* Every GREG section has a STATUS section before it. Store the
11667 tid from the previous call to pass down to the next gregs
11669 static long tid
= 1;
11671 switch (note
->type
)
11673 case BFD_QNT_CORE_INFO
:
11674 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
11675 case BFD_QNT_CORE_STATUS
:
11676 return elfcore_grok_nto_status (abfd
, note
, &tid
);
11677 case BFD_QNT_CORE_GREG
:
11678 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
11679 case BFD_QNT_CORE_FPREG
:
11680 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
11687 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11693 /* Use note name as section name. */
11694 len
= note
->namesz
;
11695 name
= (char *) bfd_alloc (abfd
, len
);
11698 memcpy (name
, note
->namedata
, len
);
11699 name
[len
- 1] = '\0';
11701 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11705 sect
->size
= note
->descsz
;
11706 sect
->filepos
= note
->descpos
;
11707 sect
->alignment_power
= 1;
11712 /* Function: elfcore_write_note
11715 buffer to hold note, and current size of buffer
11719 size of data for note
11721 Writes note to end of buffer. ELF64 notes are written exactly as
11722 for ELF32, despite the current (as of 2006) ELF gabi specifying
11723 that they ought to have 8-byte namesz and descsz field, and have
11724 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11727 Pointer to realloc'd buffer, *BUFSIZ updated. */
11730 elfcore_write_note (bfd
*abfd
,
11738 Elf_External_Note
*xnp
;
11745 namesz
= strlen (name
) + 1;
11747 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11749 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11752 dest
= buf
+ *bufsiz
;
11753 *bufsiz
+= newspace
;
11754 xnp
= (Elf_External_Note
*) dest
;
11755 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11756 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11757 H_PUT_32 (abfd
, type
, xnp
->type
);
11761 memcpy (dest
, name
, namesz
);
11769 memcpy (dest
, input
, size
);
11779 /* gcc-8 warns (*) on all the strncpy calls in this function about
11780 possible string truncation. The "truncation" is not a bug. We
11781 have an external representation of structs with fields that are not
11782 necessarily NULL terminated and corresponding internal
11783 representation fields that are one larger so that they can always
11784 be NULL terminated.
11785 gcc versions between 4.2 and 4.6 do not allow pragma control of
11786 diagnostics inside functions, giving a hard error if you try to use
11787 the finer control available with later versions.
11788 gcc prior to 4.2 warns about diagnostic push and pop.
11789 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11790 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11791 (*) Depending on your system header files! */
11792 #if GCC_VERSION >= 8000
11793 # pragma GCC diagnostic push
11794 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11797 elfcore_write_prpsinfo (bfd
*abfd
,
11801 const char *psargs
)
11803 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11805 if (bed
->elf_backend_write_core_note
!= NULL
)
11808 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11809 NT_PRPSINFO
, fname
, psargs
);
11814 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11815 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11816 if (bed
->s
->elfclass
== ELFCLASS32
)
11818 # if defined (HAVE_PSINFO32_T)
11820 int note_type
= NT_PSINFO
;
11823 int note_type
= NT_PRPSINFO
;
11826 memset (&data
, 0, sizeof (data
));
11827 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11828 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11829 return elfcore_write_note (abfd
, buf
, bufsiz
,
11830 "CORE", note_type
, &data
, sizeof (data
));
11835 # if defined (HAVE_PSINFO_T)
11837 int note_type
= NT_PSINFO
;
11840 int note_type
= NT_PRPSINFO
;
11843 memset (&data
, 0, sizeof (data
));
11844 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11845 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11846 return elfcore_write_note (abfd
, buf
, bufsiz
,
11847 "CORE", note_type
, &data
, sizeof (data
));
11849 #endif /* PSINFO_T or PRPSINFO_T */
11854 #if GCC_VERSION >= 8000
11855 # pragma GCC diagnostic pop
11859 elfcore_write_linux_prpsinfo32
11860 (bfd
*abfd
, char *buf
, int *bufsiz
,
11861 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11863 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11865 struct elf_external_linux_prpsinfo32_ugid16 data
;
11867 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11868 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11869 &data
, sizeof (data
));
11873 struct elf_external_linux_prpsinfo32_ugid32 data
;
11875 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11876 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11877 &data
, sizeof (data
));
11882 elfcore_write_linux_prpsinfo64
11883 (bfd
*abfd
, char *buf
, int *bufsiz
,
11884 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11886 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11888 struct elf_external_linux_prpsinfo64_ugid16 data
;
11890 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11891 return elfcore_write_note (abfd
, buf
, bufsiz
,
11892 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11896 struct elf_external_linux_prpsinfo64_ugid32 data
;
11898 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11899 return elfcore_write_note (abfd
, buf
, bufsiz
,
11900 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11905 elfcore_write_prstatus (bfd
*abfd
,
11912 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11914 if (bed
->elf_backend_write_core_note
!= NULL
)
11917 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11919 pid
, cursig
, gregs
);
11924 #if defined (HAVE_PRSTATUS_T)
11925 #if defined (HAVE_PRSTATUS32_T)
11926 if (bed
->s
->elfclass
== ELFCLASS32
)
11928 prstatus32_t prstat
;
11930 memset (&prstat
, 0, sizeof (prstat
));
11931 prstat
.pr_pid
= pid
;
11932 prstat
.pr_cursig
= cursig
;
11933 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11934 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11935 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11942 memset (&prstat
, 0, sizeof (prstat
));
11943 prstat
.pr_pid
= pid
;
11944 prstat
.pr_cursig
= cursig
;
11945 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11946 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11947 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11949 #endif /* HAVE_PRSTATUS_T */
11955 #if defined (HAVE_LWPSTATUS_T)
11957 elfcore_write_lwpstatus (bfd
*abfd
,
11964 lwpstatus_t lwpstat
;
11965 const char *note_name
= "CORE";
11967 memset (&lwpstat
, 0, sizeof (lwpstat
));
11968 lwpstat
.pr_lwpid
= pid
>> 16;
11969 lwpstat
.pr_cursig
= cursig
;
11970 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11971 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11972 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11973 #if !defined(gregs)
11974 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11975 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11977 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11978 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11981 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11982 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11984 #endif /* HAVE_LWPSTATUS_T */
11986 #if defined (HAVE_PSTATUS_T)
11988 elfcore_write_pstatus (bfd
*abfd
,
11992 int cursig ATTRIBUTE_UNUSED
,
11993 const void *gregs ATTRIBUTE_UNUSED
)
11995 const char *note_name
= "CORE";
11996 #if defined (HAVE_PSTATUS32_T)
11997 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11999 if (bed
->s
->elfclass
== ELFCLASS32
)
12003 memset (&pstat
, 0, sizeof (pstat
));
12004 pstat
.pr_pid
= pid
& 0xffff;
12005 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12006 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12014 memset (&pstat
, 0, sizeof (pstat
));
12015 pstat
.pr_pid
= pid
& 0xffff;
12016 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12017 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12021 #endif /* HAVE_PSTATUS_T */
12024 elfcore_write_prfpreg (bfd
*abfd
,
12027 const void *fpregs
,
12030 const char *note_name
= "CORE";
12031 return elfcore_write_note (abfd
, buf
, bufsiz
,
12032 note_name
, NT_FPREGSET
, fpregs
, size
);
12036 elfcore_write_prxfpreg (bfd
*abfd
,
12039 const void *xfpregs
,
12042 char *note_name
= "LINUX";
12043 return elfcore_write_note (abfd
, buf
, bufsiz
,
12044 note_name
, NT_PRXFPREG
, xfpregs
, size
);
12048 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
12049 const void *xfpregs
, int size
)
12052 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
12053 note_name
= "FreeBSD";
12055 note_name
= "LINUX";
12056 return elfcore_write_note (abfd
, buf
, bufsiz
,
12057 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
12061 elfcore_write_x86_segbases (bfd
*abfd
, char *buf
, int *bufsiz
,
12062 const void *regs
, int size
)
12064 char *note_name
= "FreeBSD";
12065 return elfcore_write_note (abfd
, buf
, bufsiz
,
12066 note_name
, NT_FREEBSD_X86_SEGBASES
, regs
, size
);
12070 elfcore_write_ppc_vmx (bfd
*abfd
,
12073 const void *ppc_vmx
,
12076 char *note_name
= "LINUX";
12077 return elfcore_write_note (abfd
, buf
, bufsiz
,
12078 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
12082 elfcore_write_ppc_vsx (bfd
*abfd
,
12085 const void *ppc_vsx
,
12088 char *note_name
= "LINUX";
12089 return elfcore_write_note (abfd
, buf
, bufsiz
,
12090 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
12094 elfcore_write_ppc_tar (bfd
*abfd
,
12097 const void *ppc_tar
,
12100 char *note_name
= "LINUX";
12101 return elfcore_write_note (abfd
, buf
, bufsiz
,
12102 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
12106 elfcore_write_ppc_ppr (bfd
*abfd
,
12109 const void *ppc_ppr
,
12112 char *note_name
= "LINUX";
12113 return elfcore_write_note (abfd
, buf
, bufsiz
,
12114 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
12118 elfcore_write_ppc_dscr (bfd
*abfd
,
12121 const void *ppc_dscr
,
12124 char *note_name
= "LINUX";
12125 return elfcore_write_note (abfd
, buf
, bufsiz
,
12126 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
12130 elfcore_write_ppc_ebb (bfd
*abfd
,
12133 const void *ppc_ebb
,
12136 char *note_name
= "LINUX";
12137 return elfcore_write_note (abfd
, buf
, bufsiz
,
12138 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
12142 elfcore_write_ppc_pmu (bfd
*abfd
,
12145 const void *ppc_pmu
,
12148 char *note_name
= "LINUX";
12149 return elfcore_write_note (abfd
, buf
, bufsiz
,
12150 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
12154 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
12157 const void *ppc_tm_cgpr
,
12160 char *note_name
= "LINUX";
12161 return elfcore_write_note (abfd
, buf
, bufsiz
,
12162 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
12166 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
12169 const void *ppc_tm_cfpr
,
12172 char *note_name
= "LINUX";
12173 return elfcore_write_note (abfd
, buf
, bufsiz
,
12174 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
12178 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
12181 const void *ppc_tm_cvmx
,
12184 char *note_name
= "LINUX";
12185 return elfcore_write_note (abfd
, buf
, bufsiz
,
12186 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
12190 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
12193 const void *ppc_tm_cvsx
,
12196 char *note_name
= "LINUX";
12197 return elfcore_write_note (abfd
, buf
, bufsiz
,
12198 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
12202 elfcore_write_ppc_tm_spr (bfd
*abfd
,
12205 const void *ppc_tm_spr
,
12208 char *note_name
= "LINUX";
12209 return elfcore_write_note (abfd
, buf
, bufsiz
,
12210 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
12214 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
12217 const void *ppc_tm_ctar
,
12220 char *note_name
= "LINUX";
12221 return elfcore_write_note (abfd
, buf
, bufsiz
,
12222 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
12226 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
12229 const void *ppc_tm_cppr
,
12232 char *note_name
= "LINUX";
12233 return elfcore_write_note (abfd
, buf
, bufsiz
,
12234 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
12238 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
12241 const void *ppc_tm_cdscr
,
12244 char *note_name
= "LINUX";
12245 return elfcore_write_note (abfd
, buf
, bufsiz
,
12246 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
12250 elfcore_write_s390_high_gprs (bfd
*abfd
,
12253 const void *s390_high_gprs
,
12256 char *note_name
= "LINUX";
12257 return elfcore_write_note (abfd
, buf
, bufsiz
,
12258 note_name
, NT_S390_HIGH_GPRS
,
12259 s390_high_gprs
, size
);
12263 elfcore_write_s390_timer (bfd
*abfd
,
12266 const void *s390_timer
,
12269 char *note_name
= "LINUX";
12270 return elfcore_write_note (abfd
, buf
, bufsiz
,
12271 note_name
, NT_S390_TIMER
, s390_timer
, size
);
12275 elfcore_write_s390_todcmp (bfd
*abfd
,
12278 const void *s390_todcmp
,
12281 char *note_name
= "LINUX";
12282 return elfcore_write_note (abfd
, buf
, bufsiz
,
12283 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
12287 elfcore_write_s390_todpreg (bfd
*abfd
,
12290 const void *s390_todpreg
,
12293 char *note_name
= "LINUX";
12294 return elfcore_write_note (abfd
, buf
, bufsiz
,
12295 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
12299 elfcore_write_s390_ctrs (bfd
*abfd
,
12302 const void *s390_ctrs
,
12305 char *note_name
= "LINUX";
12306 return elfcore_write_note (abfd
, buf
, bufsiz
,
12307 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
12311 elfcore_write_s390_prefix (bfd
*abfd
,
12314 const void *s390_prefix
,
12317 char *note_name
= "LINUX";
12318 return elfcore_write_note (abfd
, buf
, bufsiz
,
12319 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
12323 elfcore_write_s390_last_break (bfd
*abfd
,
12326 const void *s390_last_break
,
12329 char *note_name
= "LINUX";
12330 return elfcore_write_note (abfd
, buf
, bufsiz
,
12331 note_name
, NT_S390_LAST_BREAK
,
12332 s390_last_break
, size
);
12336 elfcore_write_s390_system_call (bfd
*abfd
,
12339 const void *s390_system_call
,
12342 char *note_name
= "LINUX";
12343 return elfcore_write_note (abfd
, buf
, bufsiz
,
12344 note_name
, NT_S390_SYSTEM_CALL
,
12345 s390_system_call
, size
);
12349 elfcore_write_s390_tdb (bfd
*abfd
,
12352 const void *s390_tdb
,
12355 char *note_name
= "LINUX";
12356 return elfcore_write_note (abfd
, buf
, bufsiz
,
12357 note_name
, NT_S390_TDB
, s390_tdb
, size
);
12361 elfcore_write_s390_vxrs_low (bfd
*abfd
,
12364 const void *s390_vxrs_low
,
12367 char *note_name
= "LINUX";
12368 return elfcore_write_note (abfd
, buf
, bufsiz
,
12369 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
12373 elfcore_write_s390_vxrs_high (bfd
*abfd
,
12376 const void *s390_vxrs_high
,
12379 char *note_name
= "LINUX";
12380 return elfcore_write_note (abfd
, buf
, bufsiz
,
12381 note_name
, NT_S390_VXRS_HIGH
,
12382 s390_vxrs_high
, size
);
12386 elfcore_write_s390_gs_cb (bfd
*abfd
,
12389 const void *s390_gs_cb
,
12392 char *note_name
= "LINUX";
12393 return elfcore_write_note (abfd
, buf
, bufsiz
,
12394 note_name
, NT_S390_GS_CB
,
12399 elfcore_write_s390_gs_bc (bfd
*abfd
,
12402 const void *s390_gs_bc
,
12405 char *note_name
= "LINUX";
12406 return elfcore_write_note (abfd
, buf
, bufsiz
,
12407 note_name
, NT_S390_GS_BC
,
12412 elfcore_write_arm_vfp (bfd
*abfd
,
12415 const void *arm_vfp
,
12418 char *note_name
= "LINUX";
12419 return elfcore_write_note (abfd
, buf
, bufsiz
,
12420 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
12424 elfcore_write_aarch_tls (bfd
*abfd
,
12427 const void *aarch_tls
,
12430 char *note_name
= "LINUX";
12431 return elfcore_write_note (abfd
, buf
, bufsiz
,
12432 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
12436 elfcore_write_aarch_hw_break (bfd
*abfd
,
12439 const void *aarch_hw_break
,
12442 char *note_name
= "LINUX";
12443 return elfcore_write_note (abfd
, buf
, bufsiz
,
12444 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
12448 elfcore_write_aarch_hw_watch (bfd
*abfd
,
12451 const void *aarch_hw_watch
,
12454 char *note_name
= "LINUX";
12455 return elfcore_write_note (abfd
, buf
, bufsiz
,
12456 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
12460 elfcore_write_aarch_sve (bfd
*abfd
,
12463 const void *aarch_sve
,
12466 char *note_name
= "LINUX";
12467 return elfcore_write_note (abfd
, buf
, bufsiz
,
12468 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
12472 elfcore_write_aarch_pauth (bfd
*abfd
,
12475 const void *aarch_pauth
,
12478 char *note_name
= "LINUX";
12479 return elfcore_write_note (abfd
, buf
, bufsiz
,
12480 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
12484 elfcore_write_aarch_mte (bfd
*abfd
,
12487 const void *aarch_mte
,
12490 char *note_name
= "LINUX";
12491 return elfcore_write_note (abfd
, buf
, bufsiz
,
12492 note_name
, NT_ARM_TAGGED_ADDR_CTRL
,
12498 elfcore_write_arc_v2 (bfd
*abfd
,
12501 const void *arc_v2
,
12504 char *note_name
= "LINUX";
12505 return elfcore_write_note (abfd
, buf
, bufsiz
,
12506 note_name
, NT_ARC_V2
, arc_v2
, size
);
12510 elfcore_write_loongarch_cpucfg (bfd
*abfd
,
12513 const void *loongarch_cpucfg
,
12516 char *note_name
= "LINUX";
12517 return elfcore_write_note (abfd
, buf
, bufsiz
,
12518 note_name
, NT_LARCH_CPUCFG
,
12519 loongarch_cpucfg
, size
);
12523 elfcore_write_loongarch_lbt (bfd
*abfd
,
12526 const void *loongarch_lbt
,
12529 char *note_name
= "LINUX";
12530 return elfcore_write_note (abfd
, buf
, bufsiz
,
12531 note_name
, NT_LARCH_LBT
, loongarch_lbt
, size
);
12535 elfcore_write_loongarch_lsx (bfd
*abfd
,
12538 const void *loongarch_lsx
,
12541 char *note_name
= "LINUX";
12542 return elfcore_write_note (abfd
, buf
, bufsiz
,
12543 note_name
, NT_LARCH_LSX
, loongarch_lsx
, size
);
12547 elfcore_write_loongarch_lasx (bfd
*abfd
,
12550 const void *loongarch_lasx
,
12553 char *note_name
= "LINUX";
12554 return elfcore_write_note (abfd
, buf
, bufsiz
,
12555 note_name
, NT_LARCH_LASX
, loongarch_lasx
, size
);
12558 /* Write the buffer of csr values in CSRS (length SIZE) into the note
12559 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12560 written into. Return a pointer to the new start of the note buffer, to
12561 replace BUF which may no longer be valid. */
12564 elfcore_write_riscv_csr (bfd
*abfd
,
12570 const char *note_name
= "GDB";
12571 return elfcore_write_note (abfd
, buf
, bufsiz
,
12572 note_name
, NT_RISCV_CSR
, csrs
, size
);
12575 /* Write the target description (a string) pointed to by TDESC, length
12576 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12577 note is being written into. Return a pointer to the new start of the
12578 note buffer, to replace BUF which may no longer be valid. */
12581 elfcore_write_gdb_tdesc (bfd
*abfd
,
12587 const char *note_name
= "GDB";
12588 return elfcore_write_note (abfd
, buf
, bufsiz
,
12589 note_name
, NT_GDB_TDESC
, tdesc
, size
);
12593 elfcore_write_register_note (bfd
*abfd
,
12596 const char *section
,
12600 if (strcmp (section
, ".reg2") == 0)
12601 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
12602 if (strcmp (section
, ".reg-xfp") == 0)
12603 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
12604 if (strcmp (section
, ".reg-xstate") == 0)
12605 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
12606 if (strcmp (section
, ".reg-x86-segbases") == 0)
12607 return elfcore_write_x86_segbases (abfd
, buf
, bufsiz
, data
, size
);
12608 if (strcmp (section
, ".reg-ppc-vmx") == 0)
12609 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
12610 if (strcmp (section
, ".reg-ppc-vsx") == 0)
12611 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
12612 if (strcmp (section
, ".reg-ppc-tar") == 0)
12613 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
12614 if (strcmp (section
, ".reg-ppc-ppr") == 0)
12615 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
12616 if (strcmp (section
, ".reg-ppc-dscr") == 0)
12617 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
12618 if (strcmp (section
, ".reg-ppc-ebb") == 0)
12619 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
12620 if (strcmp (section
, ".reg-ppc-pmu") == 0)
12621 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
12622 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
12623 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
12624 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
12625 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
12626 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
12627 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
12628 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
12629 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
12630 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
12631 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
12632 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
12633 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
12634 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
12635 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
12636 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
12637 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
12638 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
12639 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
12640 if (strcmp (section
, ".reg-s390-timer") == 0)
12641 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
12642 if (strcmp (section
, ".reg-s390-todcmp") == 0)
12643 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
12644 if (strcmp (section
, ".reg-s390-todpreg") == 0)
12645 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
12646 if (strcmp (section
, ".reg-s390-ctrs") == 0)
12647 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
12648 if (strcmp (section
, ".reg-s390-prefix") == 0)
12649 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
12650 if (strcmp (section
, ".reg-s390-last-break") == 0)
12651 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
12652 if (strcmp (section
, ".reg-s390-system-call") == 0)
12653 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
12654 if (strcmp (section
, ".reg-s390-tdb") == 0)
12655 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
12656 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
12657 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
12658 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
12659 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
12660 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
12661 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
12662 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
12663 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
12664 if (strcmp (section
, ".reg-arm-vfp") == 0)
12665 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
12666 if (strcmp (section
, ".reg-aarch-tls") == 0)
12667 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
12668 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
12669 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
12670 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
12671 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
12672 if (strcmp (section
, ".reg-aarch-sve") == 0)
12673 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
12674 if (strcmp (section
, ".reg-aarch-pauth") == 0)
12675 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
12676 if (strcmp (section
, ".reg-aarch-mte") == 0)
12677 return elfcore_write_aarch_mte (abfd
, buf
, bufsiz
, data
, size
);
12678 if (strcmp (section
, ".reg-arc-v2") == 0)
12679 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
12680 if (strcmp (section
, ".gdb-tdesc") == 0)
12681 return elfcore_write_gdb_tdesc (abfd
, buf
, bufsiz
, data
, size
);
12682 if (strcmp (section
, ".reg-riscv-csr") == 0)
12683 return elfcore_write_riscv_csr (abfd
, buf
, bufsiz
, data
, size
);
12684 if (strcmp (section
, ".reg-loongarch-cpucfg") == 0)
12685 return elfcore_write_loongarch_cpucfg (abfd
, buf
, bufsiz
, data
, size
);
12686 if (strcmp (section
, ".reg-loongarch-lbt") == 0)
12687 return elfcore_write_loongarch_lbt (abfd
, buf
, bufsiz
, data
, size
);
12688 if (strcmp (section
, ".reg-loongarch-lsx") == 0)
12689 return elfcore_write_loongarch_lsx (abfd
, buf
, bufsiz
, data
, size
);
12690 if (strcmp (section
, ".reg-loongarch-lasx") == 0)
12691 return elfcore_write_loongarch_lasx (abfd
, buf
, bufsiz
, data
, size
);
12696 elfcore_write_file_note (bfd
*obfd
, char *note_data
, int *note_size
,
12697 const void *buf
, int bufsiz
)
12699 return elfcore_write_note (obfd
, note_data
, note_size
,
12700 "CORE", NT_FILE
, buf
, bufsiz
);
12704 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
12709 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12710 gABI specifies that PT_NOTE alignment should be aligned to 4
12711 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12712 align is less than 4, we use 4 byte alignment. */
12715 if (align
!= 4 && align
!= 8)
12719 while (p
< buf
+ size
)
12721 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
12722 Elf_Internal_Note in
;
12724 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
12727 in
.type
= H_GET_32 (abfd
, xnp
->type
);
12729 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
12730 in
.namedata
= xnp
->name
;
12731 if (in
.namesz
> buf
- in
.namedata
+ size
)
12734 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
12735 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
12736 in
.descpos
= offset
+ (in
.descdata
- buf
);
12738 && (in
.descdata
>= buf
+ size
12739 || in
.descsz
> buf
- in
.descdata
+ size
))
12742 switch (bfd_get_format (abfd
))
12749 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12752 const char * string
;
12754 bool (*func
) (bfd
*, Elf_Internal_Note
*);
12758 GROKER_ELEMENT ("", elfcore_grok_note
),
12759 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
12760 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
12761 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note
),
12762 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
12763 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
12764 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
),
12765 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note
)
12767 #undef GROKER_ELEMENT
12770 for (i
= ARRAY_SIZE (grokers
); i
--;)
12772 if (in
.namesz
>= grokers
[i
].len
12773 && strncmp (in
.namedata
, grokers
[i
].string
,
12774 grokers
[i
].len
) == 0)
12776 if (! grokers
[i
].func (abfd
, & in
))
12785 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
12787 if (! elfobj_grok_gnu_note (abfd
, &in
))
12790 else if (in
.namesz
== sizeof "stapsdt"
12791 && strcmp (in
.namedata
, "stapsdt") == 0)
12793 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
12799 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
12806 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
12811 if (size
== 0 || (size
+ 1) == 0)
12814 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
12817 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
12821 /* PR 17512: file: ec08f814
12822 0-termintate the buffer so that string searches will not overflow. */
12825 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
12835 /* Providing external access to the ELF program header table. */
12837 /* Return an upper bound on the number of bytes required to store a
12838 copy of ABFD's program header table entries. Return -1 if an error
12839 occurs; bfd_get_error will return an appropriate code. */
12842 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12844 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12846 bfd_set_error (bfd_error_wrong_format
);
12850 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12853 /* Copy ABFD's program header table entries to *PHDRS. The entries
12854 will be stored as an array of Elf_Internal_Phdr structures, as
12855 defined in include/elf/internal.h. To find out how large the
12856 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12858 Return the number of program header table entries read, or -1 if an
12859 error occurs; bfd_get_error will return an appropriate code. */
12862 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12866 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12868 bfd_set_error (bfd_error_wrong_format
);
12872 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12873 if (num_phdrs
!= 0)
12874 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12875 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12880 enum elf_reloc_type_class
12881 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12882 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12883 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12885 return reloc_class_normal
;
12888 /* For RELA architectures, return the relocation value for a
12889 relocation against a local symbol. */
12892 _bfd_elf_rela_local_sym (bfd
*abfd
,
12893 Elf_Internal_Sym
*sym
,
12895 Elf_Internal_Rela
*rel
)
12897 asection
*sec
= *psec
;
12898 bfd_vma relocation
;
12900 relocation
= (sec
->output_section
->vma
12901 + sec
->output_offset
12903 if ((sec
->flags
& SEC_MERGE
)
12904 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12905 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12908 _bfd_merged_section_offset (abfd
, psec
,
12909 elf_section_data (sec
)->sec_info
,
12910 sym
->st_value
+ rel
->r_addend
);
12913 /* If we have changed the section, and our original section is
12914 marked with SEC_EXCLUDE, it means that the original
12915 SEC_MERGE section has been completely subsumed in some
12916 other SEC_MERGE section. In this case, we need to leave
12917 some info around for --emit-relocs. */
12918 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12919 sec
->kept_section
= *psec
;
12922 rel
->r_addend
-= relocation
;
12923 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12929 _bfd_elf_rel_local_sym (bfd
*abfd
,
12930 Elf_Internal_Sym
*sym
,
12934 asection
*sec
= *psec
;
12936 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12937 return sym
->st_value
+ addend
;
12939 return _bfd_merged_section_offset (abfd
, psec
,
12940 elf_section_data (sec
)->sec_info
,
12941 sym
->st_value
+ addend
);
12944 /* Adjust an address within a section. Given OFFSET within SEC, return
12945 the new offset within the section, based upon changes made to the
12946 section. Returns -1 if the offset is now invalid.
12947 The offset (in abnd out) is in target sized bytes, however big a
12951 _bfd_elf_section_offset (bfd
*abfd
,
12952 struct bfd_link_info
*info
,
12956 switch (sec
->sec_info_type
)
12958 case SEC_INFO_TYPE_STABS
:
12959 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12961 case SEC_INFO_TYPE_EH_FRAME
:
12962 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12965 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12967 /* Reverse the offset. */
12968 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12969 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12971 /* address_size and sec->size are in octets. Convert
12972 to bytes before subtracting the original offset. */
12973 offset
= ((sec
->size
- address_size
)
12974 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12980 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12981 reconstruct an ELF file by reading the segments out of remote memory
12982 based on the ELF file header at EHDR_VMA and the ELF program headers it
12983 points to. If not null, *LOADBASEP is filled in with the difference
12984 between the VMAs from which the segments were read, and the VMAs the
12985 file headers (and hence BFD's idea of each section's VMA) put them at.
12987 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12988 remote memory at target address VMA into the local buffer at MYADDR; it
12989 should return zero on success or an `errno' code on failure. TEMPL must
12990 be a BFD for an ELF target with the word size and byte order found in
12991 the remote memory. */
12994 bfd_elf_bfd_from_remote_memory
12997 bfd_size_type size
,
12998 bfd_vma
*loadbasep
,
12999 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
13001 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
13002 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
13006 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
13007 long symcount ATTRIBUTE_UNUSED
,
13008 asymbol
**syms ATTRIBUTE_UNUSED
,
13013 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13016 const char *relplt_name
;
13017 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
13021 Elf_Internal_Shdr
*hdr
;
13027 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
13030 if (dynsymcount
<= 0)
13033 if (!bed
->plt_sym_val
)
13036 relplt_name
= bed
->relplt_name
;
13037 if (relplt_name
== NULL
)
13038 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
13039 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
13040 if (relplt
== NULL
)
13043 hdr
= &elf_section_data (relplt
)->this_hdr
;
13044 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
13045 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
13048 plt
= bfd_get_section_by_name (abfd
, ".plt");
13052 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
13053 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, true))
13056 count
= relplt
->size
/ hdr
->sh_entsize
;
13057 size
= count
* sizeof (asymbol
);
13058 p
= relplt
->relocation
;
13059 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13061 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
13062 if (p
->addend
!= 0)
13065 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
13067 size
+= sizeof ("+0x") - 1 + 8;
13072 s
= *ret
= (asymbol
*) bfd_malloc (size
);
13076 names
= (char *) (s
+ count
);
13077 p
= relplt
->relocation
;
13079 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13084 addr
= bed
->plt_sym_val (i
, plt
, p
);
13085 if (addr
== (bfd_vma
) -1)
13088 *s
= **p
->sym_ptr_ptr
;
13089 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13090 we are defining a symbol, ensure one of them is set. */
13091 if ((s
->flags
& BSF_LOCAL
) == 0)
13092 s
->flags
|= BSF_GLOBAL
;
13093 s
->flags
|= BSF_SYNTHETIC
;
13095 s
->value
= addr
- plt
->vma
;
13098 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
13099 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
13101 if (p
->addend
!= 0)
13105 memcpy (names
, "+0x", sizeof ("+0x") - 1);
13106 names
+= sizeof ("+0x") - 1;
13107 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
13108 for (a
= buf
; *a
== '0'; ++a
)
13111 memcpy (names
, a
, len
);
13114 memcpy (names
, "@plt", sizeof ("@plt"));
13115 names
+= sizeof ("@plt");
13122 /* It is only used by x86-64 so far.
13123 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13124 but current usage would allow all of _bfd_std_section to be zero. */
13125 static const asymbol lcomm_sym
13126 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
13127 asection _bfd_elf_large_com_section
13128 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
13129 "LARGE_COMMON", 0, SEC_IS_COMMON
);
13132 _bfd_elf_final_write_processing (bfd
*abfd
)
13134 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
13136 i_ehdrp
= elf_elfheader (abfd
);
13138 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13139 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
13141 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13142 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13143 or STB_GNU_UNIQUE binding. */
13144 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
13146 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13147 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
13148 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
13149 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
13151 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
13152 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13153 "and FreeBSD targets"));
13154 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
13155 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13156 "only by GNU and FreeBSD targets"));
13157 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
13158 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13159 "only by GNU and FreeBSD targets"));
13160 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
13161 _bfd_error_handler (_("GNU_RETAIN section is supported "
13162 "only by GNU and FreeBSD targets"));
13163 bfd_set_error (bfd_error_sorry
);
13171 /* Return TRUE for ELF symbol types that represent functions.
13172 This is the default version of this function, which is sufficient for
13173 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13176 _bfd_elf_is_function_type (unsigned int type
)
13178 return (type
== STT_FUNC
13179 || type
== STT_GNU_IFUNC
);
13182 /* If the ELF symbol SYM might be a function in SEC, return the
13183 function size and set *CODE_OFF to the function's entry point,
13184 otherwise return zero. */
13187 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
13190 bfd_size_type size
;
13191 elf_symbol_type
* elf_sym
= (elf_symbol_type
*) sym
;
13193 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
13194 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
13195 || sym
->section
!= sec
)
13198 size
= (sym
->flags
& BSF_SYNTHETIC
) ? 0 : elf_sym
->internal_elf_sym
.st_size
;
13200 /* In theory we should check that the symbol's type satisfies
13201 _bfd_elf_is_function_type(), but there are some function-like
13202 symbols which would fail this test. (eg _start). Instead
13203 we check for hidden, local, notype symbols with zero size.
13204 This type of symbol is generated by the annobin plugin for gcc
13205 and clang, and should not be considered to be a function symbol. */
13207 && ((sym
->flags
& (BSF_SYNTHETIC
| BSF_LOCAL
)) == BSF_LOCAL
)
13208 && ELF_ST_TYPE (elf_sym
->internal_elf_sym
.st_info
) == STT_NOTYPE
13209 && ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
) == STV_HIDDEN
)
13212 *code_off
= sym
->value
;
13213 /* Do not return 0 for the function's size. */
13214 return size
? size
: 1;
13217 /* Set to non-zero to enable some debug messages. */
13218 #define DEBUG_SECONDARY_RELOCS 0
13220 /* An internal-to-the-bfd-library only section type
13221 used to indicate a cached secondary reloc section. */
13222 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13224 /* Create a BFD section to hold a secondary reloc section. */
13227 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
13228 Elf_Internal_Shdr
*hdr
,
13230 unsigned int shindex
)
13232 /* We only support RELA secondary relocs. */
13233 if (hdr
->sh_type
!= SHT_RELA
)
13236 #if DEBUG_SECONDARY_RELOCS
13237 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
13239 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
13240 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
13243 /* Read in any secondary relocs associated with SEC. */
13246 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
13248 asymbol
** symbols
,
13251 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
13253 bool result
= true;
13254 bfd_vma (*r_sym
) (bfd_vma
);
13256 #if BFD_DEFAULT_TARGET_SIZE > 32
13257 if (bfd_arch_bits_per_address (abfd
) != 32)
13258 r_sym
= elf64_r_sym
;
13261 r_sym
= elf32_r_sym
;
13263 if (!elf_section_data (sec
)->has_secondary_relocs
)
13266 /* Discover if there are any secondary reloc sections
13267 associated with SEC. */
13268 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
13270 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
13272 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
13273 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
13274 && (hdr
->sh_entsize
== ebd
->s
->sizeof_rel
13275 || hdr
->sh_entsize
== ebd
->s
->sizeof_rela
))
13277 bfd_byte
* native_relocs
;
13278 bfd_byte
* native_reloc
;
13279 arelent
* internal_relocs
;
13280 arelent
* internal_reloc
;
13282 unsigned int entsize
;
13283 unsigned int symcount
;
13284 unsigned int reloc_count
;
13287 if (ebd
->elf_info_to_howto
== NULL
)
13290 #if DEBUG_SECONDARY_RELOCS
13291 fprintf (stderr
, "read secondary relocs for %s from %s\n",
13292 sec
->name
, relsec
->name
);
13294 entsize
= hdr
->sh_entsize
;
13296 native_relocs
= bfd_malloc (hdr
->sh_size
);
13297 if (native_relocs
== NULL
)
13303 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
13304 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
13306 free (native_relocs
);
13307 bfd_set_error (bfd_error_file_too_big
);
13312 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
13313 if (internal_relocs
== NULL
)
13315 free (native_relocs
);
13320 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
13321 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
13324 free (native_relocs
);
13325 /* The internal_relocs will be freed when
13326 the memory for the bfd is released. */
13332 symcount
= bfd_get_dynamic_symcount (abfd
);
13334 symcount
= bfd_get_symcount (abfd
);
13336 for (i
= 0, internal_reloc
= internal_relocs
,
13337 native_reloc
= native_relocs
;
13339 i
++, internal_reloc
++, native_reloc
+= entsize
)
13342 Elf_Internal_Rela rela
;
13344 if (entsize
== ebd
->s
->sizeof_rel
)
13345 ebd
->s
->swap_reloc_in (abfd
, native_reloc
, & rela
);
13346 else /* entsize == ebd->s->sizeof_rela */
13347 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
13349 /* The address of an ELF reloc is section relative for an object
13350 file, and absolute for an executable file or shared library.
13351 The address of a normal BFD reloc is always section relative,
13352 and the address of a dynamic reloc is absolute.. */
13353 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
13354 internal_reloc
->address
= rela
.r_offset
;
13356 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
13358 if (r_sym (rela
.r_info
) == STN_UNDEF
)
13360 /* FIXME: This and the error case below mean that we
13361 have a symbol on relocs that is not elf_symbol_type. */
13362 internal_reloc
->sym_ptr_ptr
=
13363 bfd_abs_section_ptr
->symbol_ptr_ptr
;
13365 else if (r_sym (rela
.r_info
) > symcount
)
13368 /* xgettext:c-format */
13369 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
13370 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
13371 bfd_set_error (bfd_error_bad_value
);
13372 internal_reloc
->sym_ptr_ptr
=
13373 bfd_abs_section_ptr
->symbol_ptr_ptr
;
13380 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
13381 internal_reloc
->sym_ptr_ptr
= ps
;
13382 /* Make sure that this symbol is not removed by strip. */
13383 (*ps
)->flags
|= BSF_KEEP
;
13386 internal_reloc
->addend
= rela
.r_addend
;
13388 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
13389 if (! res
|| internal_reloc
->howto
== NULL
)
13391 #if DEBUG_SECONDARY_RELOCS
13393 "there is no howto associated with reloc %lx\n",
13400 free (native_relocs
);
13401 /* Store the internal relocs. */
13402 elf_section_data (relsec
)->sec_info
= internal_relocs
;
13409 /* Set the ELF section header fields of an output secondary reloc section. */
13412 _bfd_elf_copy_special_section_fields (const bfd
*ibfd ATTRIBUTE_UNUSED
,
13413 bfd
*obfd ATTRIBUTE_UNUSED
,
13414 const Elf_Internal_Shdr
*isection
,
13415 Elf_Internal_Shdr
*osection
)
13419 struct bfd_elf_section_data
* esd
;
13421 if (isection
== NULL
)
13424 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
13427 isec
= isection
->bfd_section
;
13431 osec
= osection
->bfd_section
;
13435 esd
= elf_section_data (osec
);
13436 BFD_ASSERT (esd
->sec_info
== NULL
);
13437 esd
->sec_info
= elf_section_data (isec
)->sec_info
;
13438 osection
->sh_type
= SHT_RELA
;
13439 osection
->sh_link
= elf_onesymtab (obfd
);
13440 if (osection
->sh_link
== 0)
13442 /* There is no symbol table - we are hosed... */
13444 /* xgettext:c-format */
13445 (_("%pB(%pA): link section cannot be set"
13446 " because the output file does not have a symbol table"),
13448 bfd_set_error (bfd_error_bad_value
);
13452 /* Find the output section that corresponds to the isection's
13454 if (isection
->sh_info
== 0
13455 || isection
->sh_info
>= elf_numsections (ibfd
))
13458 /* xgettext:c-format */
13459 (_("%pB(%pA): info section index is invalid"),
13461 bfd_set_error (bfd_error_bad_value
);
13465 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
13467 if (isection
== NULL
13468 || isection
->bfd_section
== NULL
13469 || isection
->bfd_section
->output_section
== NULL
)
13472 /* xgettext:c-format */
13473 (_("%pB(%pA): info section index cannot be set"
13474 " because the section is not in the output"),
13476 bfd_set_error (bfd_error_bad_value
);
13480 esd
= elf_section_data (isection
->bfd_section
->output_section
);
13481 BFD_ASSERT (esd
!= NULL
);
13482 osection
->sh_info
= esd
->this_idx
;
13483 esd
->has_secondary_relocs
= true;
13484 #if DEBUG_SECONDARY_RELOCS
13485 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
13486 osec
->name
, osection
->sh_link
, osection
->sh_info
);
13487 fprintf (stderr
, "mark section %s as having secondary relocs\n",
13488 bfd_section_name (isection
->bfd_section
->output_section
));
13494 /* Write out a secondary reloc section.
13496 FIXME: Currently this function can result in a serious performance penalty
13497 for files with secondary relocs and lots of sections. The proper way to
13498 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13499 relocs together and then to have this function just walk that chain. */
13502 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
13504 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
13505 bfd_vma addr_offset
;
13507 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
13508 bool result
= true;
13513 #if BFD_DEFAULT_TARGET_SIZE > 32
13514 if (bfd_arch_bits_per_address (abfd
) != 32)
13515 r_info
= elf64_r_info
;
13518 r_info
= elf32_r_info
;
13520 /* The address of an ELF reloc is section relative for an object
13521 file, and absolute for an executable file or shared library.
13522 The address of a BFD reloc is always section relative. */
13524 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
13525 addr_offset
= sec
->vma
;
13527 /* Discover if there are any secondary reloc sections
13528 associated with SEC. */
13529 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
13531 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
13532 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
13534 if (hdr
->sh_type
== SHT_RELA
13535 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
13537 asymbol
* last_sym
;
13539 size_t reloc_count
;
13541 bfd_size_type entsize
;
13542 arelent
* src_irel
;
13543 bfd_byte
* dst_rela
;
13545 if (hdr
->contents
!= NULL
)
13548 /* xgettext:c-format */
13549 (_("%pB(%pA): error: secondary reloc section processed twice"),
13551 bfd_set_error (bfd_error_bad_value
);
13556 entsize
= hdr
->sh_entsize
;
13560 /* xgettext:c-format */
13561 (_("%pB(%pA): error: secondary reloc section"
13562 " has zero sized entries"),
13564 bfd_set_error (bfd_error_bad_value
);
13568 else if (entsize
!= ebd
->s
->sizeof_rel
13569 && entsize
!= ebd
->s
->sizeof_rela
)
13572 /* xgettext:c-format */
13573 (_("%pB(%pA): error: secondary reloc section"
13574 " has non-standard sized entries"),
13576 bfd_set_error (bfd_error_bad_value
);
13581 reloc_count
= hdr
->sh_size
/ entsize
;
13582 hdr
->sh_size
= entsize
* reloc_count
;
13583 if (reloc_count
== 0)
13586 /* xgettext:c-format */
13587 (_("%pB(%pA): error: secondary reloc section is empty!"),
13589 bfd_set_error (bfd_error_bad_value
);
13594 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
13595 if (hdr
->contents
== NULL
)
13598 #if DEBUG_SECONDARY_RELOCS
13599 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
13600 reloc_count
, sec
->name
, relsec
->name
);
13604 dst_rela
= hdr
->contents
;
13605 src_irel
= (arelent
*) esd
->sec_info
;
13606 if (src_irel
== NULL
)
13609 /* xgettext:c-format */
13610 (_("%pB(%pA): error: internal relocs missing"
13611 " for secondary reloc section"),
13613 bfd_set_error (bfd_error_bad_value
);
13618 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= entsize
)
13620 Elf_Internal_Rela src_rela
;
13625 ptr
= src_irel
+ idx
;
13629 /* xgettext:c-format */
13630 (_("%pB(%pA): error: reloc table entry %zu is empty"),
13631 abfd
, relsec
, idx
);
13632 bfd_set_error (bfd_error_bad_value
);
13637 if (ptr
->sym_ptr_ptr
== NULL
)
13639 /* FIXME: Is this an error ? */
13644 sym
= *ptr
->sym_ptr_ptr
;
13646 if (sym
== last_sym
)
13650 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
13654 /* xgettext:c-format */
13655 (_("%pB(%pA): error: secondary reloc %zu"
13656 " references a missing symbol"),
13657 abfd
, relsec
, idx
);
13658 bfd_set_error (bfd_error_bad_value
);
13667 if (sym
->the_bfd
!= NULL
13668 && sym
->the_bfd
->xvec
!= abfd
->xvec
13669 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
13672 /* xgettext:c-format */
13673 (_("%pB(%pA): error: secondary reloc %zu"
13674 " references a deleted symbol"),
13675 abfd
, relsec
, idx
);
13676 bfd_set_error (bfd_error_bad_value
);
13682 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
13683 if (ptr
->howto
== NULL
)
13686 /* xgettext:c-format */
13687 (_("%pB(%pA): error: secondary reloc %zu"
13688 " is of an unknown type"),
13689 abfd
, relsec
, idx
);
13690 bfd_set_error (bfd_error_bad_value
);
13692 src_rela
.r_info
= r_info (0, 0);
13695 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
13696 src_rela
.r_addend
= ptr
->addend
;
13698 if (entsize
== ebd
->s
->sizeof_rel
)
13699 ebd
->s
->swap_reloc_out (abfd
, &src_rela
, dst_rela
);
13700 else /* entsize == ebd->s->sizeof_rela */
13701 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);