1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2023 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bool assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bool swap_out_syms (bfd
*, struct elf_strtab_hash
**, int,
55 struct bfd_link_info
*);
56 static bool elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
57 file_ptr offset
, size_t align
);
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
201 for (const unsigned char *name
= (const unsigned char *) namearg
;
204 h
= (h
<< 4) + *name
;
205 h
^= (h
>> 24) & 0xf0;
207 return h
& 0x0fffffff;
210 /* DT_GNU_HASH hash function. Do not change this function; you will
211 cause invalid hash tables to be generated. */
214 bfd_elf_gnu_hash (const char *namearg
)
218 for (const unsigned char *name
= (const unsigned char *) namearg
;
220 h
= (h
<< 5) + h
+ *name
;
224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
227 bfd_elf_allocate_object (bfd
*abfd
,
229 enum elf_target_id object_id
)
231 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
232 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
233 if (abfd
->tdata
.any
== NULL
)
236 elf_object_id (abfd
) = object_id
;
237 if (abfd
->direction
!= read_direction
)
239 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
242 elf_tdata (abfd
)->o
= o
;
243 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
250 bfd_elf_make_object (bfd
*abfd
)
252 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
253 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
258 bfd_elf_mkcorefile (bfd
*abfd
)
260 /* I think this can be done just like an object file. */
261 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
263 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
264 return elf_tdata (abfd
)->core
!= NULL
;
268 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
270 Elf_Internal_Shdr
**i_shdrp
;
271 bfd_byte
*shstrtab
= NULL
;
273 bfd_size_type shstrtabsize
;
275 i_shdrp
= elf_elfsections (abfd
);
277 || shindex
>= elf_numsections (abfd
)
278 || i_shdrp
[shindex
] == 0)
281 shstrtab
= i_shdrp
[shindex
]->contents
;
282 if (shstrtab
== NULL
)
284 /* No cached one, attempt to read, and cache what we read. */
285 offset
= i_shdrp
[shindex
]->sh_offset
;
286 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize
+ 1 <= 1
291 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
292 || (shstrtab
= _bfd_alloc_and_read (abfd
, shstrtabsize
+ 1,
293 shstrtabsize
)) == NULL
)
295 /* Once we've failed to read it, make sure we don't keep
296 trying. Otherwise, we'll keep allocating space for
297 the string table over and over. */
298 i_shdrp
[shindex
]->sh_size
= 0;
301 shstrtab
[shstrtabsize
] = '\0';
302 i_shdrp
[shindex
]->contents
= shstrtab
;
304 return (char *) shstrtab
;
308 bfd_elf_string_from_elf_section (bfd
*abfd
,
309 unsigned int shindex
,
310 unsigned int strindex
)
312 Elf_Internal_Shdr
*hdr
;
317 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
320 hdr
= elf_elfsections (abfd
)[shindex
];
322 if (hdr
->contents
== NULL
)
324 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
326 /* PR 17512: file: f057ec89. */
327 /* xgettext:c-format */
328 _bfd_error_handler (_("%pB: attempt to load strings from"
329 " a non-string section (number %d)"),
334 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
339 /* PR 24273: The string section's contents may have already
340 been loaded elsewhere, eg because a corrupt file has the
341 string section index in the ELF header pointing at a group
342 section. So be paranoid, and test that the last byte of
343 the section is zero. */
344 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
348 if (strindex
>= hdr
->sh_size
)
350 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
352 /* xgettext:c-format */
353 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
354 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
355 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
357 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
361 return ((char *) hdr
->contents
) + strindex
;
364 /* Read and convert symbols to internal format.
365 SYMCOUNT specifies the number of symbols to read, starting from
366 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367 are non-NULL, they are used to store the internal symbols, external
368 symbols, and symbol section index extensions, respectively.
369 Returns a pointer to the internal symbol buffer (malloced if necessary)
370 or NULL if there were no symbols or some kind of problem. */
373 bfd_elf_get_elf_syms (bfd
*ibfd
,
374 Elf_Internal_Shdr
*symtab_hdr
,
377 Elf_Internal_Sym
*intsym_buf
,
379 Elf_External_Sym_Shndx
*extshndx_buf
)
381 Elf_Internal_Shdr
*shndx_hdr
;
383 const bfd_byte
*esym
;
384 Elf_External_Sym_Shndx
*alloc_extshndx
;
385 Elf_External_Sym_Shndx
*shndx
;
386 Elf_Internal_Sym
*alloc_intsym
;
387 Elf_Internal_Sym
*isym
;
388 Elf_Internal_Sym
*isymend
;
389 const struct elf_backend_data
*bed
;
394 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
400 if (elf_use_dt_symtab_p (ibfd
))
402 /* Use dynamic symbol table. */
403 if (elf_tdata (ibfd
)->dt_symtab_count
!= symcount
+ symoffset
)
405 bfd_set_error (bfd_error_invalid_operation
);
408 return elf_tdata (ibfd
)->dt_symtab
+ symoffset
;
411 /* Normal syms might have section extension entries. */
413 if (elf_symtab_shndx_list (ibfd
) != NULL
)
415 elf_section_list
* entry
;
416 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
418 /* Find an index section that is linked to this symtab section. */
419 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
422 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
425 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
427 shndx_hdr
= & entry
->hdr
;
432 if (shndx_hdr
== NULL
)
434 if (symtab_hdr
== &elf_symtab_hdr (ibfd
))
435 /* Not really accurate, but this was how the old code used
437 shndx_hdr
= &elf_symtab_shndx_list (ibfd
)->hdr
;
438 /* Otherwise we do nothing. The assumption is that
439 the index table will not be needed. */
443 /* Read the symbols. */
445 alloc_extshndx
= NULL
;
447 bed
= get_elf_backend_data (ibfd
);
448 extsym_size
= bed
->s
->sizeof_sym
;
449 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
451 bfd_set_error (bfd_error_file_too_big
);
455 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
456 if (extsym_buf
== NULL
)
458 alloc_ext
= bfd_malloc (amt
);
459 extsym_buf
= alloc_ext
;
461 if (extsym_buf
== NULL
462 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
463 || bfd_read (extsym_buf
, amt
, ibfd
) != amt
)
469 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
473 if (_bfd_mul_overflow (symcount
, sizeof (Elf_External_Sym_Shndx
), &amt
))
475 bfd_set_error (bfd_error_file_too_big
);
479 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
480 if (extshndx_buf
== NULL
)
482 alloc_extshndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
483 extshndx_buf
= alloc_extshndx
;
485 if (extshndx_buf
== NULL
486 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
487 || bfd_read (extshndx_buf
, amt
, ibfd
) != amt
)
494 if (intsym_buf
== NULL
)
496 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
498 bfd_set_error (bfd_error_file_too_big
);
501 alloc_intsym
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
502 intsym_buf
= alloc_intsym
;
503 if (intsym_buf
== NULL
)
507 /* Convert the symbols to internal form. */
508 isymend
= intsym_buf
+ symcount
;
509 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
510 shndx
= extshndx_buf
;
512 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
513 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
515 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
516 /* xgettext:c-format */
517 _bfd_error_handler (_("%pB symbol number %lu references"
518 " nonexistent SHT_SYMTAB_SHNDX section"),
519 ibfd
, (unsigned long) symoffset
);
527 free (alloc_extshndx
);
532 /* Look up a symbol name. */
534 bfd_elf_sym_name (bfd
*abfd
,
535 Elf_Internal_Shdr
*symtab_hdr
,
536 Elf_Internal_Sym
*isym
,
540 unsigned int iname
= isym
->st_name
;
541 unsigned int shindex
= symtab_hdr
->sh_link
;
543 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
544 /* Check for a bogus st_shndx to avoid crashing. */
545 && isym
->st_shndx
< elf_numsections (abfd
))
547 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
548 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
551 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
554 else if (sym_sec
&& *name
== '\0')
555 name
= bfd_section_name (sym_sec
);
560 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
561 sections. The first element is the flags, the rest are section
564 typedef union elf_internal_group
{
565 Elf_Internal_Shdr
*shdr
;
567 } Elf_Internal_Group
;
569 /* Return the name of the group signature symbol. Why isn't the
570 signature just a string? */
573 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
575 Elf_Internal_Shdr
*hdr
;
576 unsigned char esym
[sizeof (Elf64_External_Sym
)];
577 Elf_External_Sym_Shndx eshndx
;
578 Elf_Internal_Sym isym
;
580 /* First we need to ensure the symbol table is available. Make sure
581 that it is a symbol table section. */
582 if (ghdr
->sh_link
>= elf_numsections (abfd
))
584 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
585 if (hdr
->sh_type
!= SHT_SYMTAB
586 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
589 /* Go read the symbol. */
590 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
591 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
592 &isym
, esym
, &eshndx
) == NULL
)
595 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
598 /* Set next_in_group list pointer, and group name for NEWSECT. */
601 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
603 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
605 /* If num_group is zero, read in all SHT_GROUP sections. The count
606 is set to -1 if there are no SHT_GROUP sections. */
609 unsigned int i
, shnum
;
611 /* First count the number of groups. If we have a SHT_GROUP
612 section with just a flag word (ie. sh_size is 4), ignore it. */
613 shnum
= elf_numsections (abfd
);
616 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
617 ( (shdr)->sh_type == SHT_GROUP \
618 && (shdr)->sh_size >= minsize \
619 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
620 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
622 for (i
= 0; i
< shnum
; i
++)
624 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
626 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
632 num_group
= (unsigned) -1;
633 elf_tdata (abfd
)->num_group
= num_group
;
634 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
638 /* We keep a list of elf section headers for group sections,
639 so we can find them quickly. */
642 elf_tdata (abfd
)->num_group
= num_group
;
643 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
644 elf_tdata (abfd
)->group_sect_ptr
645 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
646 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
650 for (i
= 0; i
< shnum
; i
++)
652 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
654 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
657 Elf_Internal_Group
*dest
;
659 /* Make sure the group section has a BFD section
661 if (!bfd_section_from_shdr (abfd
, i
))
664 /* Add to list of sections. */
665 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
668 /* Read the raw contents. */
669 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
670 shdr
->contents
= NULL
;
671 if (_bfd_mul_overflow (shdr
->sh_size
,
672 sizeof (*dest
) / 4, &amt
)
673 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
675 = _bfd_alloc_and_read (abfd
, amt
, shdr
->sh_size
)))
678 /* xgettext:c-format */
679 (_("%pB: invalid size field in group section"
680 " header: %#" PRIx64
""),
681 abfd
, (uint64_t) shdr
->sh_size
);
682 bfd_set_error (bfd_error_bad_value
);
687 /* Translate raw contents, a flag word followed by an
688 array of elf section indices all in target byte order,
689 to the flag word followed by an array of elf section
691 src
= shdr
->contents
+ shdr
->sh_size
;
692 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
700 idx
= H_GET_32 (abfd
, src
);
701 if (src
== shdr
->contents
)
705 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
706 shdr
->bfd_section
->flags
707 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
712 dest
->shdr
= elf_elfsections (abfd
)[idx
];
713 /* PR binutils/23199: All sections in a
714 section group should be marked with
715 SHF_GROUP. But some tools generate
716 broken objects without SHF_GROUP. Fix
718 dest
->shdr
->sh_flags
|= SHF_GROUP
;
721 || dest
->shdr
->sh_type
== SHT_GROUP
)
724 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
732 /* PR 17510: Corrupt binaries might contain invalid groups. */
733 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
735 elf_tdata (abfd
)->num_group
= num_group
;
737 /* If all groups are invalid then fail. */
740 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
741 elf_tdata (abfd
)->num_group
= num_group
= -1;
743 (_("%pB: no valid group sections found"), abfd
);
744 bfd_set_error (bfd_error_bad_value
);
750 if (num_group
!= (unsigned) -1)
752 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
755 for (j
= 0; j
< num_group
; j
++)
757 /* Begin search from previous found group. */
758 unsigned i
= (j
+ search_offset
) % num_group
;
760 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
761 Elf_Internal_Group
*idx
;
767 idx
= (Elf_Internal_Group
*) shdr
->contents
;
768 if (idx
== NULL
|| shdr
->sh_size
< 4)
770 /* See PR 21957 for a reproducer. */
771 /* xgettext:c-format */
772 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
773 abfd
, shdr
->bfd_section
);
774 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
775 bfd_set_error (bfd_error_bad_value
);
778 n_elt
= shdr
->sh_size
/ 4;
780 /* Look through this group's sections to see if current
781 section is a member. */
783 if ((++idx
)->shdr
== hdr
)
787 /* We are a member of this group. Go looking through
788 other members to see if any others are linked via
790 idx
= (Elf_Internal_Group
*) shdr
->contents
;
791 n_elt
= shdr
->sh_size
/ 4;
793 if ((++idx
)->shdr
!= NULL
794 && (s
= idx
->shdr
->bfd_section
) != NULL
795 && elf_next_in_group (s
) != NULL
)
799 /* Snarf the group name from other member, and
800 insert current section in circular list. */
801 elf_group_name (newsect
) = elf_group_name (s
);
802 elf_next_in_group (newsect
) = elf_next_in_group (s
);
803 elf_next_in_group (s
) = newsect
;
809 gname
= group_signature (abfd
, shdr
);
812 elf_group_name (newsect
) = gname
;
814 /* Start a circular list with one element. */
815 elf_next_in_group (newsect
) = newsect
;
818 /* If the group section has been created, point to the
820 if (shdr
->bfd_section
!= NULL
)
821 elf_next_in_group (shdr
->bfd_section
) = newsect
;
823 elf_tdata (abfd
)->group_search_offset
= i
;
830 if (elf_group_name (newsect
) == NULL
)
832 /* xgettext:c-format */
833 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
835 /* PR 29532: Return true here, even though the group info has not been
836 read. Separate debug info files can have empty group sections, but
837 we do not want this to prevent them from being loaded as otherwise
838 GDB will not be able to use them. */
845 _bfd_elf_setup_sections (bfd
*abfd
)
848 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
852 /* Process SHF_LINK_ORDER. */
853 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
855 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
856 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
858 unsigned int elfsec
= this_hdr
->sh_link
;
859 /* An sh_link value of 0 is now allowed. It indicates that linked
860 to section has already been discarded, but that the current
861 section has been retained for some other reason. This linking
862 section is still a candidate for later garbage collection
866 elf_linked_to_section (s
) = NULL
;
870 asection
*linksec
= NULL
;
872 if (elfsec
< elf_numsections (abfd
))
874 this_hdr
= elf_elfsections (abfd
)[elfsec
];
875 linksec
= this_hdr
->bfd_section
;
879 Some strip/objcopy may leave an incorrect value in
880 sh_link. We don't want to proceed. */
884 /* xgettext:c-format */
885 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
886 s
->owner
, elfsec
, s
);
890 elf_linked_to_section (s
) = linksec
;
893 else if (this_hdr
->sh_type
== SHT_GROUP
894 && elf_next_in_group (s
) == NULL
)
897 /* xgettext:c-format */
898 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
899 abfd
, elf_section_data (s
)->this_idx
);
904 /* Process section groups. */
905 if (num_group
== (unsigned) -1)
908 for (i
= 0; i
< num_group
; i
++)
910 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
911 Elf_Internal_Group
*idx
;
914 /* PR binutils/18758: Beware of corrupt binaries with invalid
916 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
919 /* xgettext:c-format */
920 (_("%pB: section group entry number %u is corrupt"),
926 idx
= (Elf_Internal_Group
*) shdr
->contents
;
927 n_elt
= shdr
->sh_size
/ 4;
933 if (idx
->shdr
== NULL
)
935 else if (idx
->shdr
->bfd_section
)
936 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
937 else if (idx
->shdr
->sh_type
!= SHT_RELA
938 && idx
->shdr
->sh_type
!= SHT_REL
)
940 /* There are some unknown sections in the group. */
942 /* xgettext:c-format */
943 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
946 bfd_elf_string_from_elf_section (abfd
,
947 (elf_elfheader (abfd
)
960 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
962 return elf_next_in_group (sec
) != NULL
;
966 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
968 if (elf_sec_group (sec
) != NULL
)
969 return elf_group_name (sec
);
973 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
977 int16_t major_version
;
978 int16_t minor_version
;
979 unsigned char slim_object
;
981 /* Flags is a private field that is not defined publicly. */
985 /* Make a BFD section from an ELF section. We store a pointer to the
986 BFD section in the bfd_section field of the header. */
989 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
990 Elf_Internal_Shdr
*hdr
,
996 const struct elf_backend_data
*bed
;
997 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
999 if (hdr
->bfd_section
!= NULL
)
1002 newsect
= bfd_make_section_anyway (abfd
, name
);
1003 if (newsect
== NULL
)
1006 hdr
->bfd_section
= newsect
;
1007 elf_section_data (newsect
)->this_hdr
= *hdr
;
1008 elf_section_data (newsect
)->this_idx
= shindex
;
1010 /* Always use the real type/flags. */
1011 elf_section_type (newsect
) = hdr
->sh_type
;
1012 elf_section_flags (newsect
) = hdr
->sh_flags
;
1014 newsect
->filepos
= hdr
->sh_offset
;
1016 flags
= SEC_NO_FLAGS
;
1017 if (hdr
->sh_type
!= SHT_NOBITS
)
1018 flags
|= SEC_HAS_CONTENTS
;
1019 if (hdr
->sh_type
== SHT_GROUP
)
1021 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1024 if (hdr
->sh_type
!= SHT_NOBITS
)
1027 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1028 flags
|= SEC_READONLY
;
1029 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1031 else if ((flags
& SEC_LOAD
) != 0)
1033 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1036 newsect
->entsize
= hdr
->sh_entsize
;
1038 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1039 flags
|= SEC_STRINGS
;
1040 if (hdr
->sh_flags
& SHF_GROUP
)
1041 if (!setup_group (abfd
, hdr
, newsect
))
1043 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1044 flags
|= SEC_THREAD_LOCAL
;
1045 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1046 flags
|= SEC_EXCLUDE
;
1048 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1050 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1051 but binutils as of 2019-07-23 did not set the EI_OSABI header
1054 case ELFOSABI_FREEBSD
:
1055 if ((hdr
->sh_flags
& SHF_GNU_RETAIN
) != 0)
1056 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_retain
;
1059 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1060 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1064 if ((flags
& SEC_ALLOC
) == 0)
1066 /* The debugging sections appear to be recognized only by name,
1067 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1068 if (name
[0] == '.')
1070 if (startswith (name
, ".debug")
1071 || startswith (name
, ".gnu.debuglto_.debug_")
1072 || startswith (name
, ".gnu.linkonce.wi.")
1073 || startswith (name
, ".zdebug"))
1074 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1075 else if (startswith (name
, GNU_BUILD_ATTRS_SECTION_NAME
)
1076 || startswith (name
, ".note.gnu"))
1078 flags
|= SEC_ELF_OCTETS
;
1081 else if (startswith (name
, ".line")
1082 || startswith (name
, ".stab")
1083 || strcmp (name
, ".gdb_index") == 0)
1084 flags
|= SEC_DEBUGGING
;
1088 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1089 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1090 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
1091 & -hdr
->sh_addralign
)))
1094 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1095 only link a single copy of the section. This is used to support
1096 g++. g++ will emit each template expansion in its own section.
1097 The symbols will be defined as weak, so that multiple definitions
1098 are permitted. The GNU linker extension is to actually discard
1099 all but one of the sections. */
1100 if (startswith (name
, ".gnu.linkonce")
1101 && elf_next_in_group (newsect
) == NULL
)
1102 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1104 if (!bfd_set_section_flags (newsect
, flags
))
1107 bed
= get_elf_backend_data (abfd
);
1108 if (bed
->elf_backend_section_flags
)
1109 if (!bed
->elf_backend_section_flags (hdr
))
1112 /* We do not parse the PT_NOTE segments as we are interested even in the
1113 separate debug info files which may have the segments offsets corrupted.
1114 PT_NOTEs from the core files are currently not parsed using BFD. */
1115 if (hdr
->sh_type
== SHT_NOTE
&& hdr
->sh_size
!= 0)
1119 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1122 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1123 hdr
->sh_offset
, hdr
->sh_addralign
);
1127 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1129 Elf_Internal_Phdr
*phdr
;
1130 unsigned int i
, nload
;
1132 /* Some ELF linkers produce binaries with all the program header
1133 p_paddr fields zero. If we have such a binary with more than
1134 one PT_LOAD header, then leave the section lma equal to vma
1135 so that we don't create sections with overlapping lma. */
1136 phdr
= elf_tdata (abfd
)->phdr
;
1137 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1138 if (phdr
->p_paddr
!= 0)
1140 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1142 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1145 phdr
= elf_tdata (abfd
)->phdr
;
1146 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1148 if (((phdr
->p_type
== PT_LOAD
1149 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1150 || phdr
->p_type
== PT_TLS
)
1151 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1153 if ((newsect
->flags
& SEC_LOAD
) == 0)
1154 newsect
->lma
= (phdr
->p_paddr
1155 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1157 /* We used to use the same adjustment for SEC_LOAD
1158 sections, but that doesn't work if the segment
1159 is packed with code from multiple VMAs.
1160 Instead we calculate the section LMA based on
1161 the segment LMA. It is assumed that the
1162 segment will contain sections with contiguous
1163 LMAs, even if the VMAs are not. */
1164 newsect
->lma
= (phdr
->p_paddr
1165 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1167 /* With contiguous segments, we can't tell from file
1168 offsets whether a section with zero size should
1169 be placed at the end of one segment or the
1170 beginning of the next. Decide based on vaddr. */
1171 if (hdr
->sh_addr
>= phdr
->p_vaddr
1172 && (hdr
->sh_addr
+ hdr
->sh_size
1173 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1179 /* Compress/decompress DWARF debug sections with names: .debug_*,
1180 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1181 if ((newsect
->flags
& SEC_DEBUGGING
) != 0
1182 && (newsect
->flags
& SEC_HAS_CONTENTS
) != 0
1183 && (newsect
->flags
& SEC_ELF_OCTETS
) != 0)
1185 enum { nothing
, compress
, decompress
} action
= nothing
;
1186 int compression_header_size
;
1187 bfd_size_type uncompressed_size
;
1188 unsigned int uncompressed_align_power
;
1189 enum compression_type ch_type
= ch_none
;
1191 = bfd_is_section_compressed_info (abfd
, newsect
,
1192 &compression_header_size
,
1194 &uncompressed_align_power
,
1197 /* Should we decompress? */
1198 if ((abfd
->flags
& BFD_DECOMPRESS
) != 0 && compressed
)
1199 action
= decompress
;
1201 /* Should we compress? Or convert to a different compression? */
1202 else if ((abfd
->flags
& BFD_COMPRESS
) != 0
1203 && newsect
->size
!= 0
1204 && compression_header_size
>= 0
1205 && uncompressed_size
> 0)
1211 enum compression_type new_ch_type
= ch_none
;
1212 if ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0)
1213 new_ch_type
= ((abfd
->flags
& BFD_COMPRESS_ZSTD
) != 0
1214 ? ch_compress_zstd
: ch_compress_zlib
);
1215 if (new_ch_type
!= ch_type
)
1220 if (action
== compress
)
1222 if (!bfd_init_section_compress_status (abfd
, newsect
))
1225 /* xgettext:c-format */
1226 (_("%pB: unable to compress section %s"), abfd
, name
);
1230 else if (action
== decompress
)
1232 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1235 /* xgettext:c-format */
1236 (_("%pB: unable to decompress section %s"), abfd
, name
);
1240 if (newsect
->compress_status
== DECOMPRESS_SECTION_ZSTD
)
1243 /* xgettext:c-format */
1244 (_ ("%pB: section %s is compressed with zstd, but BFD "
1245 "is not built with zstd support"),
1247 newsect
->compress_status
= COMPRESS_SECTION_NONE
;
1251 if (abfd
->is_linker_input
1254 /* Rename section from .zdebug_* to .debug_* so that ld
1255 scripts will see this section as a debug section. */
1256 char *new_name
= bfd_zdebug_name_to_debug (abfd
, name
);
1257 if (new_name
== NULL
)
1259 bfd_rename_section (newsect
, new_name
);
1264 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1266 if (startswith (name
, ".gnu.lto_.lto."))
1268 struct lto_section lsection
;
1269 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1270 sizeof (struct lto_section
)))
1271 abfd
->lto_slim_object
= lsection
.slim_object
;
1277 const char *const bfd_elf_section_type_names
[] =
1279 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1280 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1281 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1284 /* ELF relocs are against symbols. If we are producing relocatable
1285 output, and the reloc is against an external symbol, and nothing
1286 has given us any additional addend, the resulting reloc will also
1287 be against the same symbol. In such a case, we don't want to
1288 change anything about the way the reloc is handled, since it will
1289 all be done at final link time. Rather than put special case code
1290 into bfd_perform_relocation, all the reloc types use this howto
1291 function, or should call this function for relocatable output. */
1293 bfd_reloc_status_type
1294 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1295 arelent
*reloc_entry
,
1297 void *data ATTRIBUTE_UNUSED
,
1298 asection
*input_section
,
1300 char **error_message ATTRIBUTE_UNUSED
)
1302 if (output_bfd
!= NULL
1303 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1304 && (! reloc_entry
->howto
->partial_inplace
1305 || reloc_entry
->addend
== 0))
1307 reloc_entry
->address
+= input_section
->output_offset
;
1308 return bfd_reloc_ok
;
1311 /* In some cases the relocation should be treated as output section
1312 relative, as when linking ELF DWARF into PE COFF. Many ELF
1313 targets lack section relative relocations and instead use
1314 ordinary absolute relocations for references between DWARF
1315 sections. That is arguably a bug in those targets but it happens
1316 to work for the usual case of linking to non-loaded ELF debug
1317 sections with VMAs forced to zero. PE COFF on the other hand
1318 doesn't allow a section VMA of zero. */
1319 if (output_bfd
== NULL
1320 && !reloc_entry
->howto
->pc_relative
1321 && (symbol
->section
->flags
& SEC_DEBUGGING
) != 0
1322 && (input_section
->flags
& SEC_DEBUGGING
) != 0)
1323 reloc_entry
->addend
-= symbol
->section
->output_section
->vma
;
1325 return bfd_reloc_continue
;
1328 /* Returns TRUE if section A matches section B.
1329 Names, addresses and links may be different, but everything else
1330 should be the same. */
1333 section_match (const Elf_Internal_Shdr
* a
,
1334 const Elf_Internal_Shdr
* b
)
1336 if (a
->sh_type
!= b
->sh_type
1337 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1338 || a
->sh_addralign
!= b
->sh_addralign
1339 || a
->sh_entsize
!= b
->sh_entsize
)
1341 if (a
->sh_type
== SHT_SYMTAB
1342 || a
->sh_type
== SHT_STRTAB
)
1344 return a
->sh_size
== b
->sh_size
;
1347 /* Find a section in OBFD that has the same characteristics
1348 as IHEADER. Return the index of this section or SHN_UNDEF if
1349 none can be found. Check's section HINT first, as this is likely
1350 to be the correct section. */
1353 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1354 const unsigned int hint
)
1356 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1359 BFD_ASSERT (iheader
!= NULL
);
1361 /* See PR 20922 for a reproducer of the NULL test. */
1362 if (hint
< elf_numsections (obfd
)
1363 && oheaders
[hint
] != NULL
1364 && section_match (oheaders
[hint
], iheader
))
1367 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1369 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1371 if (oheader
== NULL
)
1373 if (section_match (oheader
, iheader
))
1374 /* FIXME: Do we care if there is a potential for
1375 multiple matches ? */
1382 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1383 Processor specific section, based upon a matching input section.
1384 Returns TRUE upon success, FALSE otherwise. */
1387 copy_special_section_fields (const bfd
*ibfd
,
1389 const Elf_Internal_Shdr
*iheader
,
1390 Elf_Internal_Shdr
*oheader
,
1391 const unsigned int secnum
)
1393 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1394 const Elf_Internal_Shdr
**iheaders
1395 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1396 bool changed
= false;
1397 unsigned int sh_link
;
1399 if (oheader
->sh_type
== SHT_NOBITS
)
1401 /* This is a feature for objcopy --only-keep-debug:
1402 When a section's type is changed to NOBITS, we preserve
1403 the sh_link and sh_info fields so that they can be
1404 matched up with the original.
1406 Note: Strictly speaking these assignments are wrong.
1407 The sh_link and sh_info fields should point to the
1408 relevent sections in the output BFD, which may not be in
1409 the same location as they were in the input BFD. But
1410 the whole point of this action is to preserve the
1411 original values of the sh_link and sh_info fields, so
1412 that they can be matched up with the section headers in
1413 the original file. So strictly speaking we may be
1414 creating an invalid ELF file, but it is only for a file
1415 that just contains debug info and only for sections
1416 without any contents. */
1417 if (oheader
->sh_link
== 0)
1418 oheader
->sh_link
= iheader
->sh_link
;
1419 if (oheader
->sh_info
== 0)
1420 oheader
->sh_info
= iheader
->sh_info
;
1424 /* Allow the target a chance to decide how these fields should be set. */
1425 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1429 /* We have an iheader which might match oheader, and which has non-zero
1430 sh_info and/or sh_link fields. Attempt to follow those links and find
1431 the section in the output bfd which corresponds to the linked section
1432 in the input bfd. */
1433 if (iheader
->sh_link
!= SHN_UNDEF
)
1435 /* See PR 20931 for a reproducer. */
1436 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1439 /* xgettext:c-format */
1440 (_("%pB: invalid sh_link field (%d) in section number %d"),
1441 ibfd
, iheader
->sh_link
, secnum
);
1445 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1446 if (sh_link
!= SHN_UNDEF
)
1448 oheader
->sh_link
= sh_link
;
1452 /* FIXME: Should we install iheader->sh_link
1453 if we could not find a match ? */
1455 /* xgettext:c-format */
1456 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1459 if (iheader
->sh_info
)
1461 /* The sh_info field can hold arbitrary information, but if the
1462 SHF_LINK_INFO flag is set then it should be interpreted as a
1464 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1466 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1468 if (sh_link
!= SHN_UNDEF
)
1469 oheader
->sh_flags
|= SHF_INFO_LINK
;
1472 /* No idea what it means - just copy it. */
1473 sh_link
= iheader
->sh_info
;
1475 if (sh_link
!= SHN_UNDEF
)
1477 oheader
->sh_info
= sh_link
;
1482 /* xgettext:c-format */
1483 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1489 /* Copy the program header and other data from one object module to
1493 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1495 const Elf_Internal_Shdr
**iheaders
1496 = (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1497 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1498 const struct elf_backend_data
*bed
;
1501 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1502 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1505 if (!elf_flags_init (obfd
))
1507 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1508 elf_flags_init (obfd
) = true;
1511 elf_gp (obfd
) = elf_gp (ibfd
);
1513 /* Also copy the EI_OSABI field. */
1514 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1515 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1517 /* If set, copy the EI_ABIVERSION field. */
1518 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1519 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1520 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1522 /* Copy object attributes. */
1523 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1525 if (iheaders
== NULL
|| oheaders
== NULL
)
1528 bed
= get_elf_backend_data (obfd
);
1530 /* Possibly copy other fields in the section header. */
1531 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1534 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1536 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1537 because of a special case need for generating separate debug info
1538 files. See below for more details. */
1540 || (oheader
->sh_type
!= SHT_NOBITS
1541 && oheader
->sh_type
< SHT_LOOS
))
1544 /* Ignore empty sections, and sections whose
1545 fields have already been initialised. */
1546 if (oheader
->sh_size
== 0
1547 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1550 /* Scan for the matching section in the input bfd.
1551 First we try for a direct mapping between the input and
1553 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1555 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1557 if (iheader
== NULL
)
1560 if (oheader
->bfd_section
!= NULL
1561 && iheader
->bfd_section
!= NULL
1562 && iheader
->bfd_section
->output_section
!= NULL
1563 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1565 /* We have found a connection from the input section to
1566 the output section. Attempt to copy the header fields.
1567 If this fails then do not try any further sections -
1568 there should only be a one-to-one mapping between
1569 input and output. */
1570 if (!copy_special_section_fields (ibfd
, obfd
,
1571 iheader
, oheader
, i
))
1572 j
= elf_numsections (ibfd
);
1577 if (j
< elf_numsections (ibfd
))
1580 /* That failed. So try to deduce the corresponding input section.
1581 Unfortunately we cannot compare names as the output string table
1582 is empty, so instead we check size, address and type. */
1583 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1585 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1587 if (iheader
== NULL
)
1590 /* Try matching fields in the input section's header.
1591 Since --only-keep-debug turns all non-debug sections into
1592 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1594 if ((oheader
->sh_type
== SHT_NOBITS
1595 || iheader
->sh_type
== oheader
->sh_type
)
1596 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1597 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1598 && iheader
->sh_addralign
== oheader
->sh_addralign
1599 && iheader
->sh_entsize
== oheader
->sh_entsize
1600 && iheader
->sh_size
== oheader
->sh_size
1601 && iheader
->sh_addr
== oheader
->sh_addr
1602 && (iheader
->sh_info
!= oheader
->sh_info
1603 || iheader
->sh_link
!= oheader
->sh_link
))
1605 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1610 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1612 /* Final attempt. Call the backend copy function
1613 with a NULL input section. */
1614 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1623 get_segment_type (unsigned int p_type
)
1628 case PT_NULL
: pt
= "NULL"; break;
1629 case PT_LOAD
: pt
= "LOAD"; break;
1630 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1631 case PT_INTERP
: pt
= "INTERP"; break;
1632 case PT_NOTE
: pt
= "NOTE"; break;
1633 case PT_SHLIB
: pt
= "SHLIB"; break;
1634 case PT_PHDR
: pt
= "PHDR"; break;
1635 case PT_TLS
: pt
= "TLS"; break;
1636 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1637 case PT_GNU_STACK
: pt
= "STACK"; break;
1638 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1639 case PT_GNU_SFRAME
: pt
= "SFRAME"; break;
1640 default: pt
= NULL
; break;
1645 /* Print out the program headers. */
1648 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1650 FILE *f
= (FILE *) farg
;
1651 Elf_Internal_Phdr
*p
;
1653 bfd_byte
*dynbuf
= NULL
;
1655 p
= elf_tdata (abfd
)->phdr
;
1660 fprintf (f
, _("\nProgram Header:\n"));
1661 c
= elf_elfheader (abfd
)->e_phnum
;
1662 for (i
= 0; i
< c
; i
++, p
++)
1664 const char *pt
= get_segment_type (p
->p_type
);
1669 sprintf (buf
, "0x%lx", p
->p_type
);
1672 fprintf (f
, "%8s off 0x", pt
);
1673 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1674 fprintf (f
, " vaddr 0x");
1675 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1676 fprintf (f
, " paddr 0x");
1677 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1678 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1679 fprintf (f
, " filesz 0x");
1680 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1681 fprintf (f
, " memsz 0x");
1682 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1683 fprintf (f
, " flags %c%c%c",
1684 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1685 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1686 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1687 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1688 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1693 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1694 if (s
!= NULL
&& (s
->flags
& SEC_HAS_CONTENTS
) != 0)
1696 unsigned int elfsec
;
1697 unsigned long shlink
;
1698 bfd_byte
*extdyn
, *extdynend
;
1700 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1702 fprintf (f
, _("\nDynamic Section:\n"));
1704 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1707 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1708 if (elfsec
== SHN_BAD
)
1710 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1712 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1713 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1715 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
1716 (size_t) (extdynend
- extdyn
) >= extdynsize
;
1717 extdyn
+= extdynsize
)
1719 Elf_Internal_Dyn dyn
;
1720 const char *name
= "";
1723 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1725 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1727 if (dyn
.d_tag
== DT_NULL
)
1734 if (bed
->elf_backend_get_target_dtag
)
1735 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1737 if (!strcmp (name
, ""))
1739 sprintf (ab
, "%#" PRIx64
, (uint64_t) dyn
.d_tag
);
1744 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1745 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1746 case DT_PLTGOT
: name
= "PLTGOT"; break;
1747 case DT_HASH
: name
= "HASH"; break;
1748 case DT_STRTAB
: name
= "STRTAB"; break;
1749 case DT_SYMTAB
: name
= "SYMTAB"; break;
1750 case DT_RELA
: name
= "RELA"; break;
1751 case DT_RELASZ
: name
= "RELASZ"; break;
1752 case DT_RELAENT
: name
= "RELAENT"; break;
1753 case DT_STRSZ
: name
= "STRSZ"; break;
1754 case DT_SYMENT
: name
= "SYMENT"; break;
1755 case DT_INIT
: name
= "INIT"; break;
1756 case DT_FINI
: name
= "FINI"; break;
1757 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1758 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1759 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1760 case DT_REL
: name
= "REL"; break;
1761 case DT_RELSZ
: name
= "RELSZ"; break;
1762 case DT_RELENT
: name
= "RELENT"; break;
1763 case DT_RELR
: name
= "RELR"; break;
1764 case DT_RELRSZ
: name
= "RELRSZ"; break;
1765 case DT_RELRENT
: name
= "RELRENT"; break;
1766 case DT_PLTREL
: name
= "PLTREL"; break;
1767 case DT_DEBUG
: name
= "DEBUG"; break;
1768 case DT_TEXTREL
: name
= "TEXTREL"; break;
1769 case DT_JMPREL
: name
= "JMPREL"; break;
1770 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1771 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1772 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1773 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1774 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1775 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1776 case DT_FLAGS
: name
= "FLAGS"; break;
1777 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1778 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1779 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1780 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1781 case DT_MOVEENT
: name
= "MOVEENT"; break;
1782 case DT_MOVESZ
: name
= "MOVESZ"; break;
1783 case DT_FEATURE
: name
= "FEATURE"; break;
1784 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1785 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1786 case DT_SYMINENT
: name
= "SYMINENT"; break;
1787 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1788 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1789 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1790 case DT_PLTPAD
: name
= "PLTPAD"; break;
1791 case DT_MOVETAB
: name
= "MOVETAB"; break;
1792 case DT_SYMINFO
: name
= "SYMINFO"; break;
1793 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1794 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1795 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1796 case DT_VERSYM
: name
= "VERSYM"; break;
1797 case DT_VERDEF
: name
= "VERDEF"; break;
1798 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1799 case DT_VERNEED
: name
= "VERNEED"; break;
1800 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1801 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1802 case DT_USED
: name
= "USED"; break;
1803 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1804 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1807 fprintf (f
, " %-20s ", name
);
1811 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1816 unsigned int tagv
= dyn
.d_un
.d_val
;
1818 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1821 fprintf (f
, "%s", string
);
1830 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1831 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1833 if (! _bfd_elf_slurp_version_tables (abfd
, false))
1837 if (elf_dynverdef (abfd
) != 0)
1839 Elf_Internal_Verdef
*t
;
1841 fprintf (f
, _("\nVersion definitions:\n"));
1842 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1844 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1845 t
->vd_flags
, t
->vd_hash
,
1846 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1847 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1849 Elf_Internal_Verdaux
*a
;
1852 for (a
= t
->vd_auxptr
->vda_nextptr
;
1856 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1862 if (elf_dynverref (abfd
) != 0)
1864 Elf_Internal_Verneed
*t
;
1866 fprintf (f
, _("\nVersion References:\n"));
1867 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1869 Elf_Internal_Vernaux
*a
;
1871 fprintf (f
, _(" required from %s:\n"),
1872 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1873 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1874 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1875 a
->vna_flags
, a
->vna_other
,
1876 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1887 /* Find the file offset corresponding to VMA by using the program
1891 offset_from_vma (Elf_Internal_Phdr
*phdrs
, size_t phnum
, bfd_vma vma
,
1892 size_t size
, size_t *max_size_p
)
1894 Elf_Internal_Phdr
*seg
;
1897 for (seg
= phdrs
, i
= 0; i
< phnum
; ++seg
, ++i
)
1898 if (seg
->p_type
== PT_LOAD
1899 && vma
>= (seg
->p_vaddr
& -seg
->p_align
)
1900 && vma
+ size
<= seg
->p_vaddr
+ seg
->p_filesz
)
1903 *max_size_p
= seg
->p_vaddr
+ seg
->p_filesz
- vma
;
1904 return vma
- seg
->p_vaddr
+ seg
->p_offset
;
1909 bfd_set_error (bfd_error_invalid_operation
);
1910 return (file_ptr
) -1;
1913 /* Convert hash table to internal form. */
1916 get_hash_table_data (bfd
*abfd
, bfd_size_type number
,
1917 unsigned int ent_size
, bfd_size_type filesize
)
1919 unsigned char *e_data
= NULL
;
1920 bfd_vma
*i_data
= NULL
;
1923 if (ent_size
!= 4 && ent_size
!= 8)
1926 if ((size_t) number
!= number
)
1928 bfd_set_error (bfd_error_file_too_big
);
1932 size
= ent_size
* number
;
1933 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1934 attempting to allocate memory when the read is bound to fail. */
1936 || number
>= ~(size_t) 0 / ent_size
1937 || number
>= ~(size_t) 0 / sizeof (*i_data
))
1939 bfd_set_error (bfd_error_file_too_big
);
1943 e_data
= _bfd_malloc_and_read (abfd
, size
, size
);
1947 i_data
= (bfd_vma
*) bfd_malloc (number
* sizeof (*i_data
));
1956 i_data
[number
] = bfd_get_32 (abfd
, e_data
+ number
* ent_size
);
1959 i_data
[number
] = bfd_get_64 (abfd
, e_data
+ number
* ent_size
);
1965 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1966 support DT_MIPS_XHASH? */
1967 #define DT_MIPS_XHASH 0x70000036
1969 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1972 _bfd_elf_get_dynamic_symbols (bfd
*abfd
, Elf_Internal_Phdr
*phdr
,
1973 Elf_Internal_Phdr
*phdrs
, size_t phnum
,
1974 bfd_size_type filesize
)
1976 bfd_byte
*extdyn
, *extdynend
;
1978 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1979 bool (*swap_symbol_in
) (bfd
*, const void *, const void *,
1980 Elf_Internal_Sym
*);
1981 Elf_Internal_Dyn dyn
;
1982 bfd_vma dt_hash
= 0;
1983 bfd_vma dt_gnu_hash
= 0;
1984 bfd_vma dt_mips_xhash
= 0;
1985 bfd_vma dt_strtab
= 0;
1986 bfd_vma dt_symtab
= 0;
1987 size_t dt_strsz
= 0;
1988 bfd_vma dt_versym
= 0;
1989 bfd_vma dt_verdef
= 0;
1990 bfd_vma dt_verneed
= 0;
1991 bfd_byte
*dynbuf
= NULL
;
1992 char *strbuf
= NULL
;
1993 bfd_vma
*gnubuckets
= NULL
;
1994 bfd_vma
*gnuchains
= NULL
;
1995 bfd_vma
*mipsxlat
= NULL
;
1996 file_ptr saved_filepos
, filepos
;
1999 bfd_byte
*esymbuf
= NULL
, *esym
;
2000 bfd_size_type symcount
;
2001 Elf_Internal_Sym
*isymbuf
= NULL
;
2002 Elf_Internal_Sym
*isym
, *isymend
;
2003 bfd_byte
*versym
= NULL
;
2004 bfd_byte
*verdef
= NULL
;
2005 bfd_byte
*verneed
= NULL
;
2006 size_t verdef_size
= 0;
2007 size_t verneed_size
= 0;
2009 const struct elf_backend_data
*bed
;
2011 /* Return TRUE if symbol table is bad. */
2012 if (elf_bad_symtab (abfd
))
2015 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
2016 if (elf_tdata (abfd
)->dt_strtab
!= NULL
)
2019 bed
= get_elf_backend_data (abfd
);
2021 /* Save file position for elf_object_p. */
2022 saved_filepos
= bfd_tell (abfd
);
2024 if (bfd_seek (abfd
, phdr
->p_offset
, SEEK_SET
) != 0)
2027 dynbuf
= _bfd_malloc_and_read (abfd
, phdr
->p_filesz
, phdr
->p_filesz
);
2031 extsym_size
= bed
->s
->sizeof_sym
;
2032 extdynsize
= bed
->s
->sizeof_dyn
;
2033 swap_dyn_in
= bed
->s
->swap_dyn_in
;
2036 if (phdr
->p_filesz
< extdynsize
)
2038 extdynend
= extdyn
+ phdr
->p_filesz
;
2039 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
2041 swap_dyn_in (abfd
, extdyn
, &dyn
);
2043 if (dyn
.d_tag
== DT_NULL
)
2049 dt_hash
= dyn
.d_un
.d_val
;
2052 if (bed
->elf_machine_code
!= EM_MIPS
2053 && bed
->elf_machine_code
!= EM_MIPS_RS3_LE
)
2054 dt_gnu_hash
= dyn
.d_un
.d_val
;
2057 dt_strtab
= dyn
.d_un
.d_val
;
2060 dt_symtab
= dyn
.d_un
.d_val
;
2063 dt_strsz
= dyn
.d_un
.d_val
;
2066 if (dyn
.d_un
.d_val
!= extsym_size
)
2070 dt_versym
= dyn
.d_un
.d_val
;
2073 dt_verdef
= dyn
.d_un
.d_val
;
2076 dt_verneed
= dyn
.d_un
.d_val
;
2079 if (dyn
.d_tag
== DT_MIPS_XHASH
2080 && (bed
->elf_machine_code
== EM_MIPS
2081 || bed
->elf_machine_code
== EM_MIPS_RS3_LE
))
2083 dt_gnu_hash
= dyn
.d_un
.d_val
;
2084 dt_mips_xhash
= dyn
.d_un
.d_val
;
2090 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
2092 if ((!dt_hash
&& !dt_gnu_hash
)
2098 /* Get dynamic string table. */
2099 filepos
= offset_from_vma (phdrs
, phnum
, dt_strtab
, dt_strsz
, NULL
);
2100 if (filepos
== (file_ptr
) -1
2101 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2104 /* Dynamic string table must be valid until ABFD is closed. */
2105 strbuf
= (char *) _bfd_alloc_and_read (abfd
, dt_strsz
, dt_strsz
);
2109 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
2110 DT_HASH since it is simpler than DT_GNU_HASH. */
2113 unsigned char nb
[16];
2114 unsigned int hash_ent_size
;
2116 switch (bed
->elf_machine_code
)
2121 if (bed
->s
->elfclass
== ELFCLASS64
)
2132 filepos
= offset_from_vma (phdrs
, phnum
, dt_hash
, sizeof (nb
),
2134 if (filepos
== (file_ptr
) -1
2135 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
2136 || bfd_read (nb
, 2 * hash_ent_size
, abfd
) != 2 * hash_ent_size
)
2139 /* The number of dynamic symbol table entries equals the number
2141 if (hash_ent_size
== 8)
2142 symcount
= bfd_get_64 (abfd
, nb
+ hash_ent_size
);
2144 symcount
= bfd_get_32 (abfd
, nb
+ hash_ent_size
);
2148 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2149 bindings are in hash table. Since in dynamic symbol table,
2150 all symbols with STB_LOCAL binding are placed before symbols
2151 with other bindings and all undefined symbols are placed
2152 before defined ones, the highest symbol index in DT_GNU_HASH
2153 is the highest dynamic symbol table index. */
2154 unsigned char nb
[16];
2155 bfd_vma ngnubuckets
;
2157 size_t i
, ngnuchains
;
2158 bfd_vma maxchain
= 0xffffffff, bitmaskwords
;
2159 bfd_vma buckets_vma
;
2161 filepos
= offset_from_vma (phdrs
, phnum
, dt_gnu_hash
,
2163 if (filepos
== (file_ptr
) -1
2164 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
2165 || bfd_read (nb
, sizeof (nb
), abfd
) != sizeof (nb
))
2168 ngnubuckets
= bfd_get_32 (abfd
, nb
);
2169 gnusymidx
= bfd_get_32 (abfd
, nb
+ 4);
2170 bitmaskwords
= bfd_get_32 (abfd
, nb
+ 8);
2171 buckets_vma
= dt_gnu_hash
+ 16;
2172 if (bed
->s
->elfclass
== ELFCLASS32
)
2173 buckets_vma
+= bitmaskwords
* 4;
2175 buckets_vma
+= bitmaskwords
* 8;
2176 filepos
= offset_from_vma (phdrs
, phnum
, buckets_vma
, 4, NULL
);
2177 if (filepos
== (file_ptr
) -1
2178 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2181 gnubuckets
= get_hash_table_data (abfd
, ngnubuckets
, 4, filesize
);
2182 if (gnubuckets
== NULL
)
2185 for (i
= 0; i
< ngnubuckets
; i
++)
2186 if (gnubuckets
[i
] != 0)
2188 if (gnubuckets
[i
] < gnusymidx
)
2191 if (maxchain
== 0xffffffff || gnubuckets
[i
] > maxchain
)
2192 maxchain
= gnubuckets
[i
];
2195 if (maxchain
== 0xffffffff)
2198 goto empty_gnu_hash
;
2201 maxchain
-= gnusymidx
;
2202 filepos
= offset_from_vma (phdrs
, phnum
,
2204 4 * (ngnubuckets
+ maxchain
)),
2206 if (filepos
== (file_ptr
) -1
2207 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2212 if (bfd_read (nb
, 4, abfd
) != 4)
2218 while ((bfd_get_32 (abfd
, nb
) & 1) == 0);
2220 filepos
= offset_from_vma (phdrs
, phnum
,
2221 (buckets_vma
+ 4 * ngnubuckets
),
2223 if (filepos
== (file_ptr
) -1
2224 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2227 gnuchains
= get_hash_table_data (abfd
, maxchain
, 4, filesize
);
2228 if (gnubuckets
== NULL
)
2230 ngnuchains
= maxchain
;
2234 filepos
= offset_from_vma (phdrs
, phnum
,
2236 + 4 * (ngnubuckets
+ maxchain
)),
2238 if (filepos
== (file_ptr
) -1
2239 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2242 mipsxlat
= get_hash_table_data (abfd
, maxchain
, 4, filesize
);
2243 if (mipsxlat
== NULL
)
2248 for (i
= 0; i
< ngnubuckets
; ++i
)
2249 if (gnubuckets
[i
] != 0)
2251 bfd_vma si
= gnubuckets
[i
];
2252 bfd_vma off
= si
- gnusymidx
;
2257 if (mipsxlat
[off
] >= symcount
)
2258 symcount
= mipsxlat
[off
] + 1;
2267 while (off
< ngnuchains
&& (gnuchains
[off
++] & 1) == 0);
2271 /* Swap in dynamic symbol table. */
2272 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
2274 bfd_set_error (bfd_error_file_too_big
);
2278 filepos
= offset_from_vma (phdrs
, phnum
, dt_symtab
, amt
, NULL
);
2279 if (filepos
== (file_ptr
) -1
2280 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2282 esymbuf
= _bfd_malloc_and_read (abfd
, amt
, amt
);
2283 if (esymbuf
== NULL
)
2286 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
2288 bfd_set_error (bfd_error_file_too_big
);
2292 /* Dynamic symbol table must be valid until ABFD is closed. */
2293 isymbuf
= (Elf_Internal_Sym
*) bfd_alloc (abfd
, amt
);
2294 if (isymbuf
== NULL
)
2297 swap_symbol_in
= bed
->s
->swap_symbol_in
;
2299 /* Convert the symbols to internal form. */
2300 isymend
= isymbuf
+ symcount
;
2301 for (esym
= esymbuf
, isym
= isymbuf
;
2303 esym
+= extsym_size
, isym
++)
2304 if (!swap_symbol_in (abfd
, esym
, NULL
, isym
)
2305 || isym
->st_name
>= dt_strsz
)
2307 bfd_set_error (bfd_error_invalid_operation
);
2313 /* Swap in DT_VERSYM. */
2314 if (_bfd_mul_overflow (symcount
, 2, &amt
))
2316 bfd_set_error (bfd_error_file_too_big
);
2320 filepos
= offset_from_vma (phdrs
, phnum
, dt_versym
, amt
, NULL
);
2321 if (filepos
== (file_ptr
) -1
2322 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2325 /* DT_VERSYM info must be valid until ABFD is closed. */
2326 versym
= _bfd_alloc_and_read (abfd
, amt
, amt
);
2330 /* Read in DT_VERDEF. */
2331 filepos
= offset_from_vma (phdrs
, phnum
, dt_verdef
,
2333 if (filepos
== (file_ptr
) -1
2334 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2337 /* DT_VERDEF info must be valid until ABFD is closed. */
2338 verdef
= _bfd_alloc_and_read (abfd
, verdef_size
,
2344 /* Read in DT_VERNEED. */
2345 filepos
= offset_from_vma (phdrs
, phnum
, dt_verneed
,
2347 if (filepos
== (file_ptr
) -1
2348 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2351 /* DT_VERNEED info must be valid until ABFD is closed. */
2352 verneed
= _bfd_alloc_and_read (abfd
, verneed_size
,
2358 elf_tdata (abfd
)->dt_strtab
= strbuf
;
2359 elf_tdata (abfd
)->dt_symtab
= isymbuf
;
2360 elf_tdata (abfd
)->dt_symtab_count
= symcount
;
2361 elf_tdata (abfd
)->dt_versym
= versym
;
2362 elf_tdata (abfd
)->dt_verdef
= verdef
;
2363 elf_tdata (abfd
)->dt_verneed
= verneed
;
2364 elf_tdata (abfd
)->dt_verdef_count
2365 = verdef_size
/ sizeof (Elf_External_Verdef
);
2366 elf_tdata (abfd
)->dt_verneed_count
2367 = verneed_size
/ sizeof (Elf_External_Verneed
);
2372 /* Restore file position for elf_object_p. */
2373 if (bfd_seek (abfd
, saved_filepos
, SEEK_SET
) != 0)
2383 /* Reconstruct section from dynamic symbol. */
2386 _bfd_elf_get_section_from_dynamic_symbol (bfd
*abfd
,
2387 Elf_Internal_Sym
*isym
)
2392 if (!elf_use_dt_symtab_p (abfd
))
2395 flags
= SEC_ALLOC
| SEC_LOAD
;
2396 switch (ELF_ST_TYPE (isym
->st_info
))
2400 sec
= bfd_get_section_by_name (abfd
, ".text");
2402 sec
= bfd_make_section_with_flags (abfd
,
2407 sec
= bfd_com_section_ptr
;
2410 sec
= bfd_get_section_by_name (abfd
, ".data");
2412 sec
= bfd_make_section_with_flags (abfd
,
2417 sec
= bfd_get_section_by_name (abfd
, ".tdata");
2419 sec
= bfd_make_section_with_flags (abfd
,
2423 | SEC_THREAD_LOCAL
));
2426 sec
= bfd_abs_section_ptr
;
2433 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2434 and return symbol version for symbol version itself. */
2437 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
2441 const char *version_string
= NULL
;
2442 if ((elf_dynversym (abfd
) != 0
2443 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
2444 || (elf_tdata (abfd
)->dt_versym
!= NULL
2445 && (elf_tdata (abfd
)->dt_verdef
!= NULL
2446 || elf_tdata (abfd
)->dt_verneed
!= NULL
)))
2448 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
2450 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
2451 vernum
&= VERSYM_VERSION
;
2454 version_string
= "";
2455 else if (vernum
== 1
2456 && (vernum
> elf_tdata (abfd
)->cverdefs
2457 || (elf_tdata (abfd
)->verdef
[0].vd_flags
2459 version_string
= base_p
? "Base" : "";
2460 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
2462 const char *nodename
2463 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
2464 version_string
= "";
2467 || symbol
->name
== NULL
2468 || strcmp (symbol
->name
, nodename
) != 0)
2469 version_string
= nodename
;
2473 Elf_Internal_Verneed
*t
;
2475 version_string
= _("<corrupt>");
2476 for (t
= elf_tdata (abfd
)->verref
;
2480 Elf_Internal_Vernaux
*a
;
2482 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2484 if (a
->vna_other
== vernum
)
2487 version_string
= a
->vna_nodename
;
2494 return version_string
;
2497 /* Display ELF-specific fields of a symbol. */
2500 bfd_elf_print_symbol (bfd
*abfd
,
2503 bfd_print_symbol_type how
)
2505 FILE *file
= (FILE *) filep
;
2508 case bfd_print_symbol_name
:
2509 fprintf (file
, "%s", symbol
->name
);
2511 case bfd_print_symbol_more
:
2512 fprintf (file
, "elf ");
2513 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
2514 fprintf (file
, " %x", symbol
->flags
);
2516 case bfd_print_symbol_all
:
2518 const char *section_name
;
2519 const char *name
= NULL
;
2520 const struct elf_backend_data
*bed
;
2521 unsigned char st_other
;
2523 const char *version_string
;
2526 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2528 bed
= get_elf_backend_data (abfd
);
2529 if (bed
->elf_backend_print_symbol_all
)
2530 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
2534 name
= symbol
->name
;
2535 bfd_print_symbol_vandf (abfd
, file
, symbol
);
2538 fprintf (file
, " %s\t", section_name
);
2539 /* Print the "other" value for a symbol. For common symbols,
2540 we've already printed the size; now print the alignment.
2541 For other symbols, we have no specified alignment, and
2542 we've printed the address; now print the size. */
2543 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
2544 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
2546 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
2547 bfd_fprintf_vma (abfd
, file
, val
);
2549 /* If we have version information, print it. */
2550 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2557 fprintf (file
, " %-11s", version_string
);
2562 fprintf (file
, " (%s)", version_string
);
2563 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2568 /* If the st_other field is not zero, print it. */
2569 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2574 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2575 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2576 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2578 /* Some other non-defined flags are also present, so print
2580 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2583 fprintf (file
, " %s", name
);
2589 /* ELF .o/exec file reading */
2591 /* Create a new bfd section from an ELF section header. */
2594 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2596 Elf_Internal_Shdr
*hdr
;
2597 Elf_Internal_Ehdr
*ehdr
;
2598 const struct elf_backend_data
*bed
;
2602 if (shindex
>= elf_numsections (abfd
))
2605 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2606 sh_link or sh_info. Detect this here, by refusing to load a
2607 section that we are already in the process of loading. */
2608 if (elf_tdata (abfd
)->being_created
[shindex
])
2611 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2614 elf_tdata (abfd
)->being_created
[shindex
] = true;
2616 hdr
= elf_elfsections (abfd
)[shindex
];
2617 ehdr
= elf_elfheader (abfd
);
2618 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2623 bed
= get_elf_backend_data (abfd
);
2624 switch (hdr
->sh_type
)
2627 /* Inactive section. Throw it away. */
2630 case SHT_PROGBITS
: /* Normal section with contents. */
2631 case SHT_NOBITS
: /* .bss section. */
2632 case SHT_HASH
: /* .hash section. */
2633 case SHT_NOTE
: /* .note section. */
2634 case SHT_INIT_ARRAY
: /* .init_array section. */
2635 case SHT_FINI_ARRAY
: /* .fini_array section. */
2636 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2637 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2638 case SHT_GNU_HASH
: /* .gnu.hash section. */
2639 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2642 case SHT_DYNAMIC
: /* Dynamic linking information. */
2643 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2646 if (hdr
->sh_link
> elf_numsections (abfd
))
2648 /* PR 10478: Accept Solaris binaries with a sh_link field
2649 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2650 switch (bfd_get_arch (abfd
))
2653 case bfd_arch_sparc
:
2654 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff)
2655 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff))
2657 /* Otherwise fall through. */
2662 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2664 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2666 Elf_Internal_Shdr
*dynsymhdr
;
2668 /* The shared libraries distributed with hpux11 have a bogus
2669 sh_link field for the ".dynamic" section. Find the
2670 string table for the ".dynsym" section instead. */
2671 if (elf_dynsymtab (abfd
) != 0)
2673 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2674 hdr
->sh_link
= dynsymhdr
->sh_link
;
2678 unsigned int i
, num_sec
;
2680 num_sec
= elf_numsections (abfd
);
2681 for (i
= 1; i
< num_sec
; i
++)
2683 dynsymhdr
= elf_elfsections (abfd
)[i
];
2684 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2686 hdr
->sh_link
= dynsymhdr
->sh_link
;
2694 case SHT_SYMTAB
: /* A symbol table. */
2695 if (elf_onesymtab (abfd
) == shindex
)
2698 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2701 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2703 if (hdr
->sh_size
!= 0)
2705 /* Some assemblers erroneously set sh_info to one with a
2706 zero sh_size. ld sees this as a global symbol count
2707 of (unsigned) -1. Fix it here. */
2712 /* PR 18854: A binary might contain more than one symbol table.
2713 Unusual, but possible. Warn, but continue. */
2714 if (elf_onesymtab (abfd
) != 0)
2717 /* xgettext:c-format */
2718 (_("%pB: warning: multiple symbol tables detected"
2719 " - ignoring the table in section %u"),
2723 elf_onesymtab (abfd
) = shindex
;
2724 elf_symtab_hdr (abfd
) = *hdr
;
2725 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2726 abfd
->flags
|= HAS_SYMS
;
2728 /* Sometimes a shared object will map in the symbol table. If
2729 SHF_ALLOC is set, and this is a shared object, then we also
2730 treat this section as a BFD section. We can not base the
2731 decision purely on SHF_ALLOC, because that flag is sometimes
2732 set in a relocatable object file, which would confuse the
2734 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2735 && (abfd
->flags
& DYNAMIC
) != 0
2736 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2740 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2741 can't read symbols without that section loaded as well. It
2742 is most likely specified by the next section header. */
2744 elf_section_list
* entry
;
2745 unsigned int i
, num_sec
;
2747 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2748 if (entry
->hdr
.sh_link
== shindex
)
2751 num_sec
= elf_numsections (abfd
);
2752 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2754 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2756 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2757 && hdr2
->sh_link
== shindex
)
2762 for (i
= 1; i
< shindex
; i
++)
2764 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2766 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2767 && hdr2
->sh_link
== shindex
)
2772 ret
= bfd_section_from_shdr (abfd
, i
);
2773 /* else FIXME: we have failed to find the symbol table.
2774 Should we issue an error? */
2778 case SHT_DYNSYM
: /* A dynamic symbol table. */
2779 if (elf_dynsymtab (abfd
) == shindex
)
2782 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2785 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2787 if (hdr
->sh_size
!= 0)
2790 /* Some linkers erroneously set sh_info to one with a
2791 zero sh_size. ld sees this as a global symbol count
2792 of (unsigned) -1. Fix it here. */
2797 /* PR 18854: A binary might contain more than one dynamic symbol table.
2798 Unusual, but possible. Warn, but continue. */
2799 if (elf_dynsymtab (abfd
) != 0)
2802 /* xgettext:c-format */
2803 (_("%pB: warning: multiple dynamic symbol tables detected"
2804 " - ignoring the table in section %u"),
2808 elf_dynsymtab (abfd
) = shindex
;
2809 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2810 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2811 abfd
->flags
|= HAS_SYMS
;
2813 /* Besides being a symbol table, we also treat this as a regular
2814 section, so that objcopy can handle it. */
2815 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2818 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2820 elf_section_list
* entry
;
2822 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2823 if (entry
->ndx
== shindex
)
2826 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2829 entry
->ndx
= shindex
;
2831 entry
->next
= elf_symtab_shndx_list (abfd
);
2832 elf_symtab_shndx_list (abfd
) = entry
;
2833 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2837 case SHT_STRTAB
: /* A string table. */
2838 if (hdr
->bfd_section
!= NULL
)
2841 if (ehdr
->e_shstrndx
== shindex
)
2843 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2844 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2848 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2851 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2852 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2856 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2859 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2860 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2861 elf_elfsections (abfd
)[shindex
] = hdr
;
2862 /* We also treat this as a regular section, so that objcopy
2864 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2869 /* If the string table isn't one of the above, then treat it as a
2870 regular section. We need to scan all the headers to be sure,
2871 just in case this strtab section appeared before the above. */
2872 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2874 unsigned int i
, num_sec
;
2876 num_sec
= elf_numsections (abfd
);
2877 for (i
= 1; i
< num_sec
; i
++)
2879 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2880 if (hdr2
->sh_link
== shindex
)
2882 /* Prevent endless recursion on broken objects. */
2885 if (! bfd_section_from_shdr (abfd
, i
))
2887 if (elf_onesymtab (abfd
) == i
)
2889 if (elf_dynsymtab (abfd
) == i
)
2890 goto dynsymtab_strtab
;
2894 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2900 /* *These* do a lot of work -- but build no sections! */
2902 asection
*target_sect
;
2903 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2904 unsigned int num_sec
= elf_numsections (abfd
);
2905 struct bfd_elf_section_data
*esdt
;
2908 if (hdr
->sh_type
== SHT_REL
)
2909 size
= bed
->s
->sizeof_rel
;
2910 else if (hdr
->sh_type
== SHT_RELA
)
2911 size
= bed
->s
->sizeof_rela
;
2913 size
= bed
->s
->arch_size
/ 8;
2914 if (hdr
->sh_entsize
!= size
)
2917 /* Check for a bogus link to avoid crashing. */
2918 if (hdr
->sh_link
>= num_sec
)
2921 /* xgettext:c-format */
2922 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2923 abfd
, hdr
->sh_link
, name
, shindex
);
2924 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2928 /* Get the symbol table. */
2929 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2930 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2931 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2934 /* If this is an alloc section in an executable or shared
2935 library, or the reloc section does not use the main symbol
2936 table we don't treat it as a reloc section. BFD can't
2937 adequately represent such a section, so at least for now,
2938 we don't try. We just present it as a normal section. We
2939 also can't use it as a reloc section if it points to the
2940 null section, an invalid section, another reloc section, or
2941 its sh_link points to the null section. */
2942 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2943 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2944 || (hdr
->sh_flags
& SHF_COMPRESSED
) != 0
2945 || hdr
->sh_type
== SHT_RELR
2946 || hdr
->sh_link
== SHN_UNDEF
2947 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2948 || hdr
->sh_info
== SHN_UNDEF
2949 || hdr
->sh_info
>= num_sec
2950 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2951 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2953 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2957 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2960 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2961 if (target_sect
== NULL
)
2964 esdt
= elf_section_data (target_sect
);
2965 if (hdr
->sh_type
== SHT_RELA
)
2966 p_hdr
= &esdt
->rela
.hdr
;
2968 p_hdr
= &esdt
->rel
.hdr
;
2970 /* PR 17512: file: 0b4f81b7.
2971 Also see PR 24456, for a file which deliberately has two reloc
2975 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2978 /* xgettext:c-format */
2979 (_("%pB: warning: secondary relocation section '%s' "
2980 "for section %pA found - ignoring"),
2981 abfd
, name
, target_sect
);
2984 esdt
->has_secondary_relocs
= true;
2988 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2993 elf_elfsections (abfd
)[shindex
] = hdr2
;
2994 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2995 * bed
->s
->int_rels_per_ext_rel
);
2996 target_sect
->flags
|= SEC_RELOC
;
2997 target_sect
->relocation
= NULL
;
2998 target_sect
->rel_filepos
= hdr
->sh_offset
;
2999 /* In the section to which the relocations apply, mark whether
3000 its relocations are of the REL or RELA variety. */
3001 if (hdr
->sh_size
!= 0)
3003 if (hdr
->sh_type
== SHT_RELA
)
3004 target_sect
->use_rela_p
= 1;
3006 abfd
->flags
|= HAS_RELOC
;
3010 case SHT_GNU_verdef
:
3011 if (hdr
->sh_info
!= 0)
3012 elf_dynverdef (abfd
) = shindex
;
3013 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
3014 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3017 case SHT_GNU_versym
:
3018 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
3021 elf_dynversym (abfd
) = shindex
;
3022 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
3023 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3026 case SHT_GNU_verneed
:
3027 if (hdr
->sh_info
!= 0)
3028 elf_dynverref (abfd
) = shindex
;
3029 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
3030 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3037 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
3040 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3046 /* Possibly an attributes section. */
3047 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
3048 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
3050 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3052 _bfd_elf_parse_attributes (abfd
, hdr
);
3056 /* Check for any processor-specific section types. */
3057 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
3060 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
3062 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3063 /* FIXME: How to properly handle allocated section reserved
3064 for applications? */
3066 /* xgettext:c-format */
3067 (_("%pB: unknown type [%#x] section `%s'"),
3068 abfd
, hdr
->sh_type
, name
);
3071 /* Allow sections reserved for applications. */
3072 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3076 else if (hdr
->sh_type
>= SHT_LOPROC
3077 && hdr
->sh_type
<= SHT_HIPROC
)
3078 /* FIXME: We should handle this section. */
3080 /* xgettext:c-format */
3081 (_("%pB: unknown type [%#x] section `%s'"),
3082 abfd
, hdr
->sh_type
, name
);
3083 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
3085 /* Unrecognised OS-specific sections. */
3086 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
3087 /* SHF_OS_NONCONFORMING indicates that special knowledge is
3088 required to correctly process the section and the file should
3089 be rejected with an error message. */
3091 /* xgettext:c-format */
3092 (_("%pB: unknown type [%#x] section `%s'"),
3093 abfd
, hdr
->sh_type
, name
);
3096 /* Otherwise it should be processed. */
3097 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3102 /* FIXME: We should handle this section. */
3104 /* xgettext:c-format */
3105 (_("%pB: unknown type [%#x] section `%s'"),
3106 abfd
, hdr
->sh_type
, name
);
3114 elf_tdata (abfd
)->being_created
[shindex
] = false;
3118 /* Return the local symbol specified by ABFD, R_SYMNDX. */
3121 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
3123 unsigned long r_symndx
)
3125 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
3127 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
3129 Elf_Internal_Shdr
*symtab_hdr
;
3130 unsigned char esym
[sizeof (Elf64_External_Sym
)];
3131 Elf_External_Sym_Shndx eshndx
;
3133 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3134 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
3135 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
3138 if (cache
->abfd
!= abfd
)
3140 memset (cache
->indx
, -1, sizeof (cache
->indx
));
3143 cache
->indx
[ent
] = r_symndx
;
3146 return &cache
->sym
[ent
];
3149 /* Given an ELF section number, retrieve the corresponding BFD
3153 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
3155 if (sec_index
>= elf_numsections (abfd
))
3157 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
3160 static const struct bfd_elf_special_section special_sections_b
[] =
3162 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3163 { NULL
, 0, 0, 0, 0 }
3166 static const struct bfd_elf_special_section special_sections_c
[] =
3168 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
3169 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
3170 { NULL
, 0, 0, 0, 0 }
3173 static const struct bfd_elf_special_section special_sections_d
[] =
3175 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3176 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3177 /* There are more DWARF sections than these, but they needn't be added here
3178 unless you have to cope with broken compilers that don't emit section
3179 attributes or you want to help the user writing assembler. */
3180 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
3181 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
3182 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
3183 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
3184 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
3185 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
3186 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
3187 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
3188 { NULL
, 0, 0, 0, 0 }
3191 static const struct bfd_elf_special_section special_sections_f
[] =
3193 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3194 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3195 { NULL
, 0 , 0, 0, 0 }
3198 static const struct bfd_elf_special_section special_sections_g
[] =
3200 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3201 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3202 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3203 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
3204 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3205 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
3206 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
3207 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
3208 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
3209 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
3210 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
3211 { NULL
, 0, 0, 0, 0 }
3214 static const struct bfd_elf_special_section special_sections_h
[] =
3216 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
3217 { NULL
, 0, 0, 0, 0 }
3220 static const struct bfd_elf_special_section special_sections_i
[] =
3222 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3223 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3224 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
3225 { NULL
, 0, 0, 0, 0 }
3228 static const struct bfd_elf_special_section special_sections_l
[] =
3230 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
3231 { NULL
, 0, 0, 0, 0 }
3234 static const struct bfd_elf_special_section special_sections_n
[] =
3236 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3237 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
3238 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
3239 { NULL
, 0, 0, 0, 0 }
3242 static const struct bfd_elf_special_section special_sections_p
[] =
3244 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3245 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3246 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3247 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3248 { NULL
, 0, 0, 0, 0 }
3251 static const struct bfd_elf_special_section special_sections_r
[] =
3253 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
3254 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
3255 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR
, SHF_ALLOC
},
3256 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
3257 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
3258 { NULL
, 0, 0, 0, 0 }
3261 static const struct bfd_elf_special_section special_sections_s
[] =
3263 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
3264 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
3265 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
3266 /* See struct bfd_elf_special_section declaration for the semantics of
3267 this special case where .prefix_length != strlen (.prefix). */
3268 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
3269 { NULL
, 0, 0, 0, 0 }
3272 static const struct bfd_elf_special_section special_sections_t
[] =
3274 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3275 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
3276 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
3277 { NULL
, 0, 0, 0, 0 }
3280 static const struct bfd_elf_special_section special_sections_z
[] =
3282 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
3283 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
3284 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
3285 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
3286 { NULL
, 0, 0, 0, 0 }
3289 static const struct bfd_elf_special_section
* const special_sections
[] =
3291 special_sections_b
, /* 'b' */
3292 special_sections_c
, /* 'c' */
3293 special_sections_d
, /* 'd' */
3295 special_sections_f
, /* 'f' */
3296 special_sections_g
, /* 'g' */
3297 special_sections_h
, /* 'h' */
3298 special_sections_i
, /* 'i' */
3301 special_sections_l
, /* 'l' */
3303 special_sections_n
, /* 'n' */
3305 special_sections_p
, /* 'p' */
3307 special_sections_r
, /* 'r' */
3308 special_sections_s
, /* 's' */
3309 special_sections_t
, /* 't' */
3315 special_sections_z
/* 'z' */
3318 const struct bfd_elf_special_section
*
3319 _bfd_elf_get_special_section (const char *name
,
3320 const struct bfd_elf_special_section
*spec
,
3326 len
= strlen (name
);
3328 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
3331 int prefix_len
= spec
[i
].prefix_length
;
3333 if (len
< prefix_len
)
3335 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
3338 suffix_len
= spec
[i
].suffix_length
;
3339 if (suffix_len
<= 0)
3341 if (name
[prefix_len
] != 0)
3343 if (suffix_len
== 0)
3345 if (name
[prefix_len
] != '.'
3346 && (suffix_len
== -2
3347 || (rela
&& spec
[i
].type
== SHT_REL
)))
3353 if (len
< prefix_len
+ suffix_len
)
3355 if (memcmp (name
+ len
- suffix_len
,
3356 spec
[i
].prefix
+ prefix_len
,
3366 const struct bfd_elf_special_section
*
3367 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
3370 const struct bfd_elf_special_section
*spec
;
3371 const struct elf_backend_data
*bed
;
3373 /* See if this is one of the special sections. */
3374 if (sec
->name
== NULL
)
3377 bed
= get_elf_backend_data (abfd
);
3378 spec
= bed
->special_sections
;
3381 spec
= _bfd_elf_get_special_section (sec
->name
,
3382 bed
->special_sections
,
3388 if (sec
->name
[0] != '.')
3391 i
= sec
->name
[1] - 'b';
3392 if (i
< 0 || i
> 'z' - 'b')
3395 spec
= special_sections
[i
];
3400 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
3404 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
3406 struct bfd_elf_section_data
*sdata
;
3407 const struct elf_backend_data
*bed
;
3408 const struct bfd_elf_special_section
*ssect
;
3410 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
3413 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
3417 sec
->used_by_bfd
= sdata
;
3420 /* Indicate whether or not this section should use RELA relocations. */
3421 bed
= get_elf_backend_data (abfd
);
3422 sec
->use_rela_p
= bed
->default_use_rela_p
;
3424 /* Set up ELF section type and flags for newly created sections, if
3425 there is an ABI mandated section. */
3426 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
3429 elf_section_type (sec
) = ssect
->type
;
3430 elf_section_flags (sec
) = ssect
->attr
;
3433 return _bfd_generic_new_section_hook (abfd
, sec
);
3436 /* Create a new bfd section from an ELF program header.
3438 Since program segments have no names, we generate a synthetic name
3439 of the form segment<NUM>, where NUM is generally the index in the
3440 program header table. For segments that are split (see below) we
3441 generate the names segment<NUM>a and segment<NUM>b.
3443 Note that some program segments may have a file size that is different than
3444 (less than) the memory size. All this means is that at execution the
3445 system must allocate the amount of memory specified by the memory size,
3446 but only initialize it with the first "file size" bytes read from the
3447 file. This would occur for example, with program segments consisting
3448 of combined data+bss.
3450 To handle the above situation, this routine generates TWO bfd sections
3451 for the single program segment. The first has the length specified by
3452 the file size of the segment, and the second has the length specified
3453 by the difference between the two sizes. In effect, the segment is split
3454 into its initialized and uninitialized parts. */
3457 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
3458 Elf_Internal_Phdr
*hdr
,
3460 const char *type_name
)
3467 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
3469 split
= ((hdr
->p_memsz
> 0)
3470 && (hdr
->p_filesz
> 0)
3471 && (hdr
->p_memsz
> hdr
->p_filesz
));
3473 if (hdr
->p_filesz
> 0)
3475 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
3476 len
= strlen (namebuf
) + 1;
3477 name
= (char *) bfd_alloc (abfd
, len
);
3480 memcpy (name
, namebuf
, len
);
3481 newsect
= bfd_make_section (abfd
, name
);
3482 if (newsect
== NULL
)
3484 newsect
->vma
= hdr
->p_vaddr
/ opb
;
3485 newsect
->lma
= hdr
->p_paddr
/ opb
;
3486 newsect
->size
= hdr
->p_filesz
;
3487 newsect
->filepos
= hdr
->p_offset
;
3488 newsect
->flags
|= SEC_HAS_CONTENTS
;
3489 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
3490 if (hdr
->p_type
== PT_LOAD
)
3492 newsect
->flags
|= SEC_ALLOC
;
3493 newsect
->flags
|= SEC_LOAD
;
3494 if (hdr
->p_flags
& PF_X
)
3496 /* FIXME: all we known is that it has execute PERMISSION,
3498 newsect
->flags
|= SEC_CODE
;
3501 if (!(hdr
->p_flags
& PF_W
))
3503 newsect
->flags
|= SEC_READONLY
;
3507 if (hdr
->p_memsz
> hdr
->p_filesz
)
3511 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3512 len
= strlen (namebuf
) + 1;
3513 name
= (char *) bfd_alloc (abfd
, len
);
3516 memcpy (name
, namebuf
, len
);
3517 newsect
= bfd_make_section (abfd
, name
);
3518 if (newsect
== NULL
)
3520 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3521 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3522 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3523 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3524 align
= newsect
->vma
& -newsect
->vma
;
3525 if (align
== 0 || align
> hdr
->p_align
)
3526 align
= hdr
->p_align
;
3527 newsect
->alignment_power
= bfd_log2 (align
);
3528 if (hdr
->p_type
== PT_LOAD
)
3530 newsect
->flags
|= SEC_ALLOC
;
3531 if (hdr
->p_flags
& PF_X
)
3532 newsect
->flags
|= SEC_CODE
;
3534 if (!(hdr
->p_flags
& PF_W
))
3535 newsect
->flags
|= SEC_READONLY
;
3542 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3544 /* The return value is ignored. Build-ids are considered optional. */
3545 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3546 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3552 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3554 const struct elf_backend_data
*bed
;
3556 switch (hdr
->p_type
)
3559 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3562 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3564 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3565 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3569 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3572 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3575 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3577 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3583 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3586 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3588 case PT_GNU_EH_FRAME
:
3589 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3593 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3596 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3599 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3603 /* Check for any processor-specific program segment types. */
3604 bed
= get_elf_backend_data (abfd
);
3605 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3609 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3613 _bfd_elf_single_rel_hdr (asection
*sec
)
3615 if (elf_section_data (sec
)->rel
.hdr
)
3617 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3618 return elf_section_data (sec
)->rel
.hdr
;
3621 return elf_section_data (sec
)->rela
.hdr
;
3625 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3626 Elf_Internal_Shdr
*rel_hdr
,
3627 const char *sec_name
,
3630 char *name
= (char *) bfd_alloc (abfd
,
3631 sizeof ".rela" + strlen (sec_name
));
3635 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3637 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3639 if (rel_hdr
->sh_name
== (unsigned int) -1)
3645 /* Allocate and initialize a section-header for a new reloc section,
3646 containing relocations against ASECT. It is stored in RELDATA. If
3647 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3651 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3652 struct bfd_elf_section_reloc_data
*reldata
,
3653 const char *sec_name
,
3655 bool delay_st_name_p
)
3657 Elf_Internal_Shdr
*rel_hdr
;
3658 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3660 BFD_ASSERT (reldata
->hdr
== NULL
);
3661 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3662 reldata
->hdr
= rel_hdr
;
3664 if (delay_st_name_p
)
3665 rel_hdr
->sh_name
= (unsigned int) -1;
3666 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3669 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3670 rel_hdr
->sh_entsize
= (use_rela_p
3671 ? bed
->s
->sizeof_rela
3672 : bed
->s
->sizeof_rel
);
3673 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3674 rel_hdr
->sh_flags
= 0;
3675 rel_hdr
->sh_addr
= 0;
3676 rel_hdr
->sh_size
= 0;
3677 rel_hdr
->sh_offset
= 0;
3682 /* Return the default section type based on the passed in section flags. */
3685 bfd_elf_get_default_section_type (flagword flags
)
3687 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3688 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3690 return SHT_PROGBITS
;
3693 struct fake_section_arg
3695 struct bfd_link_info
*link_info
;
3699 /* Set up an ELF internal section header for a section. */
3702 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3704 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3705 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3706 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3707 Elf_Internal_Shdr
*this_hdr
;
3708 unsigned int sh_type
;
3709 const char *name
= asect
->name
;
3710 bool delay_st_name_p
= false;
3715 /* We already failed; just get out of the bfd_map_over_sections
3720 this_hdr
= &esd
->this_hdr
;
3722 /* ld: compress DWARF debug sections with names: .debug_*. */
3724 && (abfd
->flags
& BFD_COMPRESS
) != 0
3725 && (asect
->flags
& SEC_DEBUGGING
) != 0
3729 /* If this section will be compressed, delay adding section
3730 name to section name section after it is compressed in
3731 _bfd_elf_assign_file_positions_for_non_load. */
3732 delay_st_name_p
= true;
3735 if (delay_st_name_p
)
3736 this_hdr
->sh_name
= (unsigned int) -1;
3740 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3742 if (this_hdr
->sh_name
== (unsigned int) -1)
3749 /* Don't clear sh_flags. Assembler may set additional bits. */
3751 if ((asect
->flags
& SEC_ALLOC
) != 0
3752 || asect
->user_set_vma
)
3753 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3755 this_hdr
->sh_addr
= 0;
3757 this_hdr
->sh_offset
= 0;
3758 this_hdr
->sh_size
= asect
->size
;
3759 this_hdr
->sh_link
= 0;
3760 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3761 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3764 /* xgettext:c-format */
3765 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3766 abfd
, asect
->alignment_power
, asect
);
3770 /* Set sh_addralign to the highest power of two given by alignment
3771 consistent with the section VMA. Linker scripts can force VMA. */
3772 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3773 this_hdr
->sh_addralign
= mask
& -mask
;
3774 /* The sh_entsize and sh_info fields may have been set already by
3775 copy_private_section_data. */
3777 this_hdr
->bfd_section
= asect
;
3778 this_hdr
->contents
= NULL
;
3780 /* If the section type is unspecified, we set it based on
3782 if (asect
->type
!= 0)
3783 sh_type
= asect
->type
;
3784 else if ((asect
->flags
& SEC_GROUP
) != 0)
3785 sh_type
= SHT_GROUP
;
3787 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3789 if (this_hdr
->sh_type
== SHT_NULL
)
3790 this_hdr
->sh_type
= sh_type
;
3791 else if (this_hdr
->sh_type
== SHT_NOBITS
3792 && sh_type
== SHT_PROGBITS
3793 && (asect
->flags
& SEC_ALLOC
) != 0)
3795 /* Warn if we are changing a NOBITS section to PROGBITS, but
3796 allow the link to proceed. This can happen when users link
3797 non-bss input sections to bss output sections, or emit data
3798 to a bss output section via a linker script. */
3800 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3801 this_hdr
->sh_type
= sh_type
;
3804 switch (this_hdr
->sh_type
)
3815 case SHT_INIT_ARRAY
:
3816 case SHT_FINI_ARRAY
:
3817 case SHT_PREINIT_ARRAY
:
3818 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3822 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3826 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3830 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3834 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3835 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3839 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3840 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3843 case SHT_GNU_versym
:
3844 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3847 case SHT_GNU_verdef
:
3848 this_hdr
->sh_entsize
= 0;
3849 /* objcopy or strip will copy over sh_info, but may not set
3850 cverdefs. The linker will set cverdefs, but sh_info will be
3852 if (this_hdr
->sh_info
== 0)
3853 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3855 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3856 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3859 case SHT_GNU_verneed
:
3860 this_hdr
->sh_entsize
= 0;
3861 /* objcopy or strip will copy over sh_info, but may not set
3862 cverrefs. The linker will set cverrefs, but sh_info will be
3864 if (this_hdr
->sh_info
== 0)
3865 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3867 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3868 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3872 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3876 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3880 if ((asect
->flags
& SEC_ALLOC
) != 0)
3881 this_hdr
->sh_flags
|= SHF_ALLOC
;
3882 if ((asect
->flags
& SEC_READONLY
) == 0)
3883 this_hdr
->sh_flags
|= SHF_WRITE
;
3884 if ((asect
->flags
& SEC_CODE
) != 0)
3885 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3886 if ((asect
->flags
& SEC_MERGE
) != 0)
3888 this_hdr
->sh_flags
|= SHF_MERGE
;
3889 this_hdr
->sh_entsize
= asect
->entsize
;
3891 if ((asect
->flags
& SEC_STRINGS
) != 0)
3892 this_hdr
->sh_flags
|= SHF_STRINGS
;
3893 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3894 this_hdr
->sh_flags
|= SHF_GROUP
;
3895 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3897 this_hdr
->sh_flags
|= SHF_TLS
;
3898 if (asect
->size
== 0
3899 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3901 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3903 this_hdr
->sh_size
= 0;
3906 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3907 if (this_hdr
->sh_size
!= 0)
3908 this_hdr
->sh_type
= SHT_NOBITS
;
3912 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3913 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3915 /* If the section has relocs, set up a section header for the
3916 SHT_REL[A] section. If two relocation sections are required for
3917 this section, it is up to the processor-specific back-end to
3918 create the other. */
3919 if ((asect
->flags
& SEC_RELOC
) != 0)
3921 /* When doing a relocatable link, create both REL and RELA sections if
3924 /* Do the normal setup if we wouldn't create any sections here. */
3925 && esd
->rel
.count
+ esd
->rela
.count
> 0
3926 && (bfd_link_relocatable (arg
->link_info
)
3927 || arg
->link_info
->emitrelocations
))
3929 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3930 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3931 false, delay_st_name_p
))
3936 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3937 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3938 true, delay_st_name_p
))
3944 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3946 ? &esd
->rela
: &esd
->rel
),
3956 /* Check for processor-specific section types. */
3957 sh_type
= this_hdr
->sh_type
;
3958 if (bed
->elf_backend_fake_sections
3959 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3965 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3967 /* Don't change the header type from NOBITS if we are being
3968 called for objcopy --only-keep-debug. */
3969 this_hdr
->sh_type
= sh_type
;
3973 /* Fill in the contents of a SHT_GROUP section. Called from
3974 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3975 when ELF targets use the generic linker, ld. Called for ld -r
3976 from bfd_elf_final_link. */
3979 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3981 bool *failedptr
= (bool *) failedptrarg
;
3982 asection
*elt
, *first
;
3986 /* Ignore linker created group section. See elfNN_ia64_object_p in
3988 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3993 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3995 unsigned long symindx
= 0;
3997 /* elf_group_id will have been set up by objcopy and the
3999 if (elf_group_id (sec
) != NULL
)
4000 symindx
= elf_group_id (sec
)->udata
.i
;
4004 /* If called from the assembler, swap_out_syms will have set up
4006 PR 25699: A corrupt input file could contain bogus group info. */
4007 if (sec
->index
>= elf_num_section_syms (abfd
)
4008 || elf_section_syms (abfd
)[sec
->index
] == NULL
)
4013 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
4015 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
4017 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
4019 /* The ELF backend linker sets sh_info to -2 when the group
4020 signature symbol is global, and thus the index can't be
4021 set until all local symbols are output. */
4023 struct bfd_elf_section_data
*sec_data
;
4024 unsigned long symndx
;
4025 unsigned long extsymoff
;
4026 struct elf_link_hash_entry
*h
;
4028 /* The point of this little dance to the first SHF_GROUP section
4029 then back to the SHT_GROUP section is that this gets us to
4030 the SHT_GROUP in the input object. */
4031 igroup
= elf_sec_group (elf_next_in_group (sec
));
4032 sec_data
= elf_section_data (igroup
);
4033 symndx
= sec_data
->this_hdr
.sh_info
;
4035 if (!elf_bad_symtab (igroup
->owner
))
4037 Elf_Internal_Shdr
*symtab_hdr
;
4039 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
4040 extsymoff
= symtab_hdr
->sh_info
;
4042 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
4043 while (h
->root
.type
== bfd_link_hash_indirect
4044 || h
->root
.type
== bfd_link_hash_warning
)
4045 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4047 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
4050 /* The contents won't be allocated for "ld -r" or objcopy. */
4052 if (sec
->contents
== NULL
)
4055 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
4057 /* Arrange for the section to be written out. */
4058 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
4059 if (sec
->contents
== NULL
)
4066 loc
= sec
->contents
+ sec
->size
;
4068 /* Get the pointer to the first section in the group that gas
4069 squirreled away here. objcopy arranges for this to be set to the
4070 start of the input section group. */
4071 first
= elt
= elf_next_in_group (sec
);
4073 /* First element is a flag word. Rest of section is elf section
4074 indices for all the sections of the group. Write them backwards
4075 just to keep the group in the same order as given in .section
4076 directives, not that it matters. */
4083 s
= s
->output_section
;
4085 && !bfd_is_abs_section (s
))
4087 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
4088 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
4090 if (elf_sec
->rel
.hdr
!= NULL
4092 || (input_elf_sec
->rel
.hdr
!= NULL
4093 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
4095 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
4097 if (loc
== sec
->contents
)
4099 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
4101 if (elf_sec
->rela
.hdr
!= NULL
4103 || (input_elf_sec
->rela
.hdr
!= NULL
4104 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
4106 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
4108 if (loc
== sec
->contents
)
4110 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
4113 if (loc
== sec
->contents
)
4115 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
4117 elt
= elf_next_in_group (elt
);
4122 /* We should always get here with loc == sec->contents + 4, but it is
4123 possible to craft bogus SHT_GROUP sections that will cause segfaults
4124 in objcopy without checking loc here and in the loop above. */
4125 if (loc
== sec
->contents
)
4130 if (loc
!= sec
->contents
)
4133 memset (sec
->contents
+ 4, 0, loc
- sec
->contents
);
4134 loc
= sec
->contents
;
4138 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
4141 /* Given NAME, the name of a relocation section stripped of its
4142 .rel/.rela prefix, return the section in ABFD to which the
4143 relocations apply. */
4146 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
4148 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4149 section likely apply to .got.plt or .got section. */
4150 if (get_elf_backend_data (abfd
)->want_got_plt
4151 && strcmp (name
, ".plt") == 0)
4156 sec
= bfd_get_section_by_name (abfd
, name
);
4162 return bfd_get_section_by_name (abfd
, name
);
4165 /* Return the section to which RELOC_SEC applies. */
4168 elf_get_reloc_section (asection
*reloc_sec
)
4173 const struct elf_backend_data
*bed
;
4175 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
4176 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
4179 /* We look up the section the relocs apply to by name. */
4180 name
= reloc_sec
->name
;
4181 if (!startswith (name
, ".rel"))
4184 if (type
== SHT_RELA
&& *name
++ != 'a')
4187 abfd
= reloc_sec
->owner
;
4188 bed
= get_elf_backend_data (abfd
);
4189 return bed
->get_reloc_section (abfd
, name
);
4192 /* Assign all ELF section numbers. The dummy first section is handled here
4193 too. The link/info pointers for the standard section types are filled
4194 in here too, while we're at it. LINK_INFO will be 0 when arriving
4195 here for gas, objcopy, and when using the generic ELF linker. */
4198 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
4200 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
4202 unsigned int section_number
;
4203 Elf_Internal_Shdr
**i_shdrp
;
4204 struct bfd_elf_section_data
*d
;
4210 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
4212 /* SHT_GROUP sections are in relocatable files only. */
4213 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
4215 size_t reloc_count
= 0;
4217 /* Put SHT_GROUP sections first. */
4218 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4220 d
= elf_section_data (sec
);
4222 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
4224 if (sec
->flags
& SEC_LINKER_CREATED
)
4226 /* Remove the linker created SHT_GROUP sections. */
4227 bfd_section_list_remove (abfd
, sec
);
4228 abfd
->section_count
--;
4231 d
->this_idx
= section_number
++;
4234 /* Count relocations. */
4235 reloc_count
+= sec
->reloc_count
;
4238 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4239 if (reloc_count
== 0)
4240 abfd
->flags
&= ~HAS_RELOC
;
4242 abfd
->flags
|= HAS_RELOC
;
4245 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4247 d
= elf_section_data (sec
);
4249 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
4250 d
->this_idx
= section_number
++;
4251 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
4252 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
4255 d
->rel
.idx
= section_number
++;
4256 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
4257 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
4264 d
->rela
.idx
= section_number
++;
4265 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
4266 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
4272 need_symtab
= (bfd_get_symcount (abfd
) > 0
4273 || (link_info
== NULL
4274 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4278 elf_onesymtab (abfd
) = section_number
++;
4279 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
4280 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
4282 elf_section_list
*entry
;
4284 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
4286 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
4287 entry
->ndx
= section_number
++;
4288 elf_symtab_shndx_list (abfd
) = entry
;
4290 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
4291 ".symtab_shndx", false);
4292 if (entry
->hdr
.sh_name
== (unsigned int) -1)
4295 elf_strtab_sec (abfd
) = section_number
++;
4296 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
4299 elf_shstrtab_sec (abfd
) = section_number
++;
4300 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
4301 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
4303 if (section_number
>= SHN_LORESERVE
)
4305 /* xgettext:c-format */
4306 _bfd_error_handler (_("%pB: too many sections: %u"),
4307 abfd
, section_number
);
4311 elf_numsections (abfd
) = section_number
;
4312 elf_elfheader (abfd
)->e_shnum
= section_number
;
4314 /* Set up the list of section header pointers, in agreement with the
4316 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
4317 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
4318 if (i_shdrp
== NULL
)
4321 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
4322 sizeof (Elf_Internal_Shdr
));
4323 if (i_shdrp
[0] == NULL
)
4325 bfd_release (abfd
, i_shdrp
);
4329 elf_elfsections (abfd
) = i_shdrp
;
4331 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
4334 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
4335 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
4337 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
4338 BFD_ASSERT (entry
!= NULL
);
4339 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
4340 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
4342 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
4343 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
4346 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4350 d
= elf_section_data (sec
);
4352 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
4353 if (d
->rel
.idx
!= 0)
4354 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
4355 if (d
->rela
.idx
!= 0)
4356 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
4358 /* Fill in the sh_link and sh_info fields while we're at it. */
4360 /* sh_link of a reloc section is the section index of the symbol
4361 table. sh_info is the section index of the section to which
4362 the relocation entries apply. */
4363 if (d
->rel
.idx
!= 0)
4365 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
4366 d
->rel
.hdr
->sh_info
= d
->this_idx
;
4367 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
4369 if (d
->rela
.idx
!= 0)
4371 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
4372 d
->rela
.hdr
->sh_info
= d
->this_idx
;
4373 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
4376 /* We need to set up sh_link for SHF_LINK_ORDER. */
4377 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
4379 s
= elf_linked_to_section (sec
);
4380 /* We can now have a NULL linked section pointer.
4381 This happens when the sh_link field is 0, which is done
4382 when a linked to section is discarded but the linking
4383 section has been retained for some reason. */
4386 /* Check discarded linkonce section. */
4387 if (discarded_section (s
))
4391 /* xgettext:c-format */
4392 (_("%pB: sh_link of section `%pA' points to"
4393 " discarded section `%pA' of `%pB'"),
4394 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
4395 /* Point to the kept section if it has the same
4396 size as the discarded one. */
4397 kept
= _bfd_elf_check_kept_section (s
, link_info
);
4400 bfd_set_error (bfd_error_bad_value
);
4405 /* Handle objcopy. */
4406 else if (s
->output_section
== NULL
)
4409 /* xgettext:c-format */
4410 (_("%pB: sh_link of section `%pA' points to"
4411 " removed section `%pA' of `%pB'"),
4412 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
4413 bfd_set_error (bfd_error_bad_value
);
4416 s
= s
->output_section
;
4417 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4421 switch (d
->this_hdr
.sh_type
)
4425 /* sh_link is the section index of the symbol table.
4426 sh_info is the section index of the section to which the
4427 relocation entries apply. */
4428 if (d
->this_hdr
.sh_link
== 0)
4430 /* FIXME maybe: If this is a reloc section which we are
4431 treating as a normal section then we likely should
4432 not be assuming its sh_link is .dynsym or .symtab. */
4433 if ((sec
->flags
& SEC_ALLOC
) != 0)
4435 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4437 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4440 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4443 s
= elf_get_reloc_section (sec
);
4446 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
4447 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
4452 /* We assume that a section named .stab*str is a stabs
4453 string section. We look for a section with the same name
4454 but without the trailing ``str'', and set its sh_link
4455 field to point to this section. */
4456 if (startswith (sec
->name
, ".stab")
4457 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
4462 len
= strlen (sec
->name
);
4463 alc
= (char *) bfd_malloc (len
- 2);
4466 memcpy (alc
, sec
->name
, len
- 3);
4467 alc
[len
- 3] = '\0';
4468 s
= bfd_get_section_by_name (abfd
, alc
);
4472 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
4474 /* This is a .stab section. */
4475 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
4482 case SHT_GNU_verneed
:
4483 case SHT_GNU_verdef
:
4484 /* sh_link is the section header index of the string table
4485 used for the dynamic entries, or the symbol table, or the
4487 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4489 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4492 case SHT_GNU_LIBLIST
:
4493 /* sh_link is the section header index of the prelink library
4494 list used for the dynamic entries, or the symbol table, or
4495 the version strings. */
4496 s
= bfd_get_section_by_name (abfd
, ((sec
->flags
& SEC_ALLOC
)
4497 ? ".dynstr" : ".gnu.libstr"));
4499 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4504 case SHT_GNU_versym
:
4505 /* sh_link is the section header index of the symbol table
4506 this hash table or version table is for. */
4507 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4509 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4513 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4517 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4518 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4519 debug section name from .debug_* to .zdebug_* if needed. */
4525 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4527 /* If the backend has a special mapping, use it. */
4528 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4529 if (bed
->elf_backend_sym_is_global
)
4530 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4532 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4533 || bfd_is_und_section (bfd_asymbol_section (sym
))
4534 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4537 /* Filter global symbols of ABFD to include in the import library. All
4538 SYMCOUNT symbols of ABFD can be examined from their pointers in
4539 SYMS. Pointers of symbols to keep should be stored contiguously at
4540 the beginning of that array.
4542 Returns the number of symbols to keep. */
4545 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4546 asymbol
**syms
, long symcount
)
4548 long src_count
, dst_count
= 0;
4550 for (src_count
= 0; src_count
< symcount
; src_count
++)
4552 asymbol
*sym
= syms
[src_count
];
4553 char *name
= (char *) bfd_asymbol_name (sym
);
4554 struct bfd_link_hash_entry
*h
;
4556 if (!sym_is_global (abfd
, sym
))
4559 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, false);
4562 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4564 if (h
->linker_def
|| h
->ldscript_def
)
4567 syms
[dst_count
++] = sym
;
4570 syms
[dst_count
] = NULL
;
4575 /* Don't output section symbols for sections that are not going to be
4576 output, that are duplicates or there is no BFD section. */
4579 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4581 elf_symbol_type
*type_ptr
;
4586 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4589 /* Ignore the section symbol if it isn't used. */
4590 if ((sym
->flags
& BSF_SECTION_SYM_USED
) == 0)
4593 if (sym
->section
== NULL
)
4596 type_ptr
= elf_symbol_from (sym
);
4597 return ((type_ptr
!= NULL
4598 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4599 && bfd_is_abs_section (sym
->section
))
4600 || !(sym
->section
->owner
== abfd
4601 || (sym
->section
->output_section
!= NULL
4602 && sym
->section
->output_section
->owner
== abfd
4603 && sym
->section
->output_offset
== 0)
4604 || bfd_is_abs_section (sym
->section
)));
4607 /* Map symbol from it's internal number to the external number, moving
4608 all local symbols to be at the head of the list. */
4611 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4613 unsigned int symcount
= bfd_get_symcount (abfd
);
4614 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4615 asymbol
**sect_syms
;
4616 unsigned int num_locals
= 0;
4617 unsigned int num_globals
= 0;
4618 unsigned int num_locals2
= 0;
4619 unsigned int num_globals2
= 0;
4620 unsigned int max_index
= 0;
4627 fprintf (stderr
, "elf_map_symbols\n");
4631 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4633 if (max_index
< asect
->index
)
4634 max_index
= asect
->index
;
4638 amt
= max_index
* sizeof (asymbol
*);
4639 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4640 if (sect_syms
== NULL
)
4642 elf_section_syms (abfd
) = sect_syms
;
4643 elf_num_section_syms (abfd
) = max_index
;
4645 /* Init sect_syms entries for any section symbols we have already
4646 decided to output. */
4647 for (idx
= 0; idx
< symcount
; idx
++)
4649 asymbol
*sym
= syms
[idx
];
4651 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4653 && !ignore_section_sym (abfd
, sym
)
4654 && !bfd_is_abs_section (sym
->section
))
4656 asection
*sec
= sym
->section
;
4658 if (sec
->owner
!= abfd
)
4659 sec
= sec
->output_section
;
4661 sect_syms
[sec
->index
] = syms
[idx
];
4665 /* Classify all of the symbols. */
4666 for (idx
= 0; idx
< symcount
; idx
++)
4668 if (sym_is_global (abfd
, syms
[idx
]))
4670 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4674 /* We will be adding a section symbol for each normal BFD section. Most
4675 sections will already have a section symbol in outsymbols, but
4676 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4677 at least in that case. */
4678 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4680 asymbol
*sym
= asect
->symbol
;
4681 /* Don't include ignored section symbols. */
4682 if (!ignore_section_sym (abfd
, sym
)
4683 && sect_syms
[asect
->index
] == NULL
)
4685 if (!sym_is_global (abfd
, asect
->symbol
))
4692 /* Now sort the symbols so the local symbols are first. */
4693 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4694 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4695 if (new_syms
== NULL
)
4698 for (idx
= 0; idx
< symcount
; idx
++)
4700 asymbol
*sym
= syms
[idx
];
4703 if (sym_is_global (abfd
, sym
))
4704 i
= num_locals
+ num_globals2
++;
4705 /* Don't include ignored section symbols. */
4706 else if (!ignore_section_sym (abfd
, sym
))
4711 sym
->udata
.i
= i
+ 1;
4713 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4715 asymbol
*sym
= asect
->symbol
;
4716 if (!ignore_section_sym (abfd
, sym
)
4717 && sect_syms
[asect
->index
] == NULL
)
4721 sect_syms
[asect
->index
] = sym
;
4722 if (!sym_is_global (abfd
, sym
))
4725 i
= num_locals
+ num_globals2
++;
4727 sym
->udata
.i
= i
+ 1;
4731 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4733 *pnum_locals
= num_locals
;
4737 /* Align to the maximum file alignment that could be required for any
4738 ELF data structure. */
4740 static inline file_ptr
4741 align_file_position (file_ptr off
, int align
)
4743 return (off
+ align
- 1) & ~(align
- 1);
4746 /* Assign a file position to a section, optionally aligning to the
4747 required section alignment. */
4750 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4754 if (align
&& i_shdrp
->sh_addralign
> 1)
4755 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
& -i_shdrp
->sh_addralign
);
4756 i_shdrp
->sh_offset
= offset
;
4757 if (i_shdrp
->bfd_section
!= NULL
)
4758 i_shdrp
->bfd_section
->filepos
= offset
;
4759 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4760 offset
+= i_shdrp
->sh_size
;
4764 /* Compute the file positions we are going to put the sections at, and
4765 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4766 is not NULL, this is being called by the ELF backend linker. */
4769 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4770 struct bfd_link_info
*link_info
)
4772 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4773 struct fake_section_arg fsargs
;
4775 struct elf_strtab_hash
*strtab
= NULL
;
4776 Elf_Internal_Shdr
*shstrtab_hdr
;
4779 if (abfd
->output_has_begun
)
4782 /* Do any elf backend specific processing first. */
4783 if (bed
->elf_backend_begin_write_processing
)
4784 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4786 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4789 fsargs
.failed
= false;
4790 fsargs
.link_info
= link_info
;
4791 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4795 if (!assign_section_numbers (abfd
, link_info
))
4798 /* The backend linker builds symbol table information itself. */
4799 need_symtab
= (link_info
== NULL
4800 && (bfd_get_symcount (abfd
) > 0
4801 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4805 /* Non-zero if doing a relocatable link. */
4806 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4808 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
, link_info
))
4813 if (link_info
== NULL
)
4815 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4817 goto err_free_strtab
;
4820 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4821 /* sh_name was set in init_file_header. */
4822 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4823 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4824 shstrtab_hdr
->sh_addr
= 0;
4825 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4826 shstrtab_hdr
->sh_entsize
= 0;
4827 shstrtab_hdr
->sh_link
= 0;
4828 shstrtab_hdr
->sh_info
= 0;
4829 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4830 shstrtab_hdr
->sh_addralign
= 1;
4832 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4833 goto err_free_strtab
;
4838 Elf_Internal_Shdr
*hdr
;
4840 off
= elf_next_file_pos (abfd
);
4842 hdr
= & elf_symtab_hdr (abfd
);
4843 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4845 if (elf_symtab_shndx_list (abfd
) != NULL
)
4847 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4848 if (hdr
->sh_size
!= 0)
4849 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4850 /* FIXME: What about other symtab_shndx sections in the list ? */
4853 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4854 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4856 elf_next_file_pos (abfd
) = off
;
4858 /* Now that we know where the .strtab section goes, write it
4860 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4861 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4862 goto err_free_strtab
;
4863 _bfd_elf_strtab_free (strtab
);
4866 abfd
->output_has_begun
= true;
4871 _bfd_elf_strtab_free (strtab
);
4875 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4876 function effectively returns whether --eh-frame-hdr is given on the
4877 command line. After size_dynamic_sections the result reflects
4878 whether .eh_frame_hdr will actually be output (sizing isn't done
4879 until ldemul_after_allocation). */
4882 elf_eh_frame_hdr (const struct bfd_link_info
*info
)
4884 if (info
!= NULL
&& is_elf_hash_table (info
->hash
))
4885 return elf_hash_table (info
)->eh_info
.hdr_sec
;
4889 /* Make an initial estimate of the size of the program header. If we
4890 get the number wrong here, we'll redo section placement. */
4892 static bfd_size_type
4893 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4897 const struct elf_backend_data
*bed
;
4899 /* Assume we will need exactly two PT_LOAD segments: one for text
4900 and one for data. */
4903 s
= bfd_get_section_by_name (abfd
, ".interp");
4904 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4906 /* If we have a loadable interpreter section, we need a
4907 PT_INTERP segment. In this case, assume we also need a
4908 PT_PHDR segment, although that may not be true for all
4913 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4915 /* We need a PT_DYNAMIC segment. */
4919 if (info
!= NULL
&& info
->relro
)
4921 /* We need a PT_GNU_RELRO segment. */
4925 if (elf_eh_frame_hdr (info
))
4927 /* We need a PT_GNU_EH_FRAME segment. */
4931 if (elf_stack_flags (abfd
))
4933 /* We need a PT_GNU_STACK segment. */
4937 if (elf_sframe (abfd
))
4939 /* We need a PT_GNU_SFRAME segment. */
4943 s
= bfd_get_section_by_name (abfd
,
4944 NOTE_GNU_PROPERTY_SECTION_NAME
);
4945 if (s
!= NULL
&& s
->size
!= 0)
4947 /* We need a PT_GNU_PROPERTY segment. */
4951 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4953 if ((s
->flags
& SEC_LOAD
) != 0
4954 && elf_section_type (s
) == SHT_NOTE
)
4956 unsigned int alignment_power
;
4957 /* We need a PT_NOTE segment. */
4959 /* Try to create just one PT_NOTE segment for all adjacent
4960 loadable SHT_NOTE sections. gABI requires that within a
4961 PT_NOTE segment (and also inside of each SHT_NOTE section)
4962 each note should have the same alignment. So we check
4963 whether the sections are correctly aligned. */
4964 alignment_power
= s
->alignment_power
;
4965 while (s
->next
!= NULL
4966 && s
->next
->alignment_power
== alignment_power
4967 && (s
->next
->flags
& SEC_LOAD
) != 0
4968 && elf_section_type (s
->next
) == SHT_NOTE
)
4973 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4975 if (s
->flags
& SEC_THREAD_LOCAL
)
4977 /* We need a PT_TLS segment. */
4983 bed
= get_elf_backend_data (abfd
);
4985 if ((abfd
->flags
& D_PAGED
) != 0
4986 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4988 /* Add a PT_GNU_MBIND segment for each mbind section. */
4989 bfd_vma commonpagesize
;
4990 unsigned int page_align_power
;
4993 commonpagesize
= info
->commonpagesize
;
4995 commonpagesize
= bed
->commonpagesize
;
4996 page_align_power
= bfd_log2 (commonpagesize
);
4997 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4998 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
5000 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
5003 /* xgettext:c-format */
5004 (_("%pB: GNU_MBIND section `%pA' has invalid "
5005 "sh_info field: %d"),
5006 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
5009 /* Align mbind section to page size. */
5010 if (s
->alignment_power
< page_align_power
)
5011 s
->alignment_power
= page_align_power
;
5016 /* Let the backend count up any program headers it might need. */
5017 if (bed
->elf_backend_additional_program_headers
)
5021 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
5027 return segs
* bed
->s
->sizeof_phdr
;
5030 /* Find the segment that contains the output_section of section. */
5033 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
5035 struct elf_segment_map
*m
;
5036 Elf_Internal_Phdr
*p
;
5038 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
5044 for (i
= m
->count
- 1; i
>= 0; i
--)
5045 if (m
->sections
[i
] == section
)
5052 /* Create a mapping from a set of sections to a program segment. */
5054 static struct elf_segment_map
*
5055 make_mapping (bfd
*abfd
,
5056 asection
**sections
,
5061 struct elf_segment_map
*m
;
5066 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5067 amt
+= (to
- from
) * sizeof (asection
*);
5068 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5072 m
->p_type
= PT_LOAD
;
5073 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
5074 m
->sections
[i
- from
] = *hdrpp
;
5075 m
->count
= to
- from
;
5077 if (from
== 0 && phdr
)
5079 /* Include the headers in the first PT_LOAD segment. */
5080 m
->includes_filehdr
= 1;
5081 m
->includes_phdrs
= 1;
5087 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
5090 struct elf_segment_map
*
5091 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
5093 struct elf_segment_map
*m
;
5095 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
5096 sizeof (struct elf_segment_map
));
5100 m
->p_type
= PT_DYNAMIC
;
5102 m
->sections
[0] = dynsec
;
5107 /* Possibly add or remove segments from the segment map. */
5110 elf_modify_segment_map (bfd
*abfd
,
5111 struct bfd_link_info
*info
,
5112 bool remove_empty_load
)
5114 struct elf_segment_map
**m
;
5115 const struct elf_backend_data
*bed
;
5117 /* The placement algorithm assumes that non allocated sections are
5118 not in PT_LOAD segments. We ensure this here by removing such
5119 sections from the segment map. We also remove excluded
5120 sections. Finally, any PT_LOAD segment without sections is
5122 m
= &elf_seg_map (abfd
);
5125 unsigned int i
, new_count
;
5127 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
5129 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
5130 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
5131 || (*m
)->p_type
!= PT_LOAD
))
5133 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
5137 (*m
)->count
= new_count
;
5139 if (remove_empty_load
5140 && (*m
)->p_type
== PT_LOAD
5142 && !(*m
)->includes_phdrs
)
5148 bed
= get_elf_backend_data (abfd
);
5149 if (bed
->elf_backend_modify_segment_map
!= NULL
)
5151 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
5158 #define IS_TBSS(s) \
5159 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5161 /* Set up a mapping from BFD sections to program segments. Update
5162 NEED_LAYOUT if the section layout is changed. */
5165 _bfd_elf_map_sections_to_segments (bfd
*abfd
,
5166 struct bfd_link_info
*info
,
5170 struct elf_segment_map
*m
;
5171 asection
**sections
= NULL
;
5172 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5175 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
5179 info
->user_phdrs
= !no_user_phdrs
;
5181 /* Size the relative relocations if DT_RELR is enabled. */
5182 if (info
->enable_dt_relr
5183 && need_layout
!= NULL
5184 && bed
->size_relative_relocs
5185 && !bed
->size_relative_relocs (info
, need_layout
))
5186 info
->callbacks
->einfo
5187 (_("%F%P: failed to size relative relocations\n"));
5190 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
5194 struct elf_segment_map
*mfirst
;
5195 struct elf_segment_map
**pm
;
5198 unsigned int hdr_index
;
5199 bfd_vma maxpagesize
;
5201 bool phdr_in_segment
;
5204 unsigned int tls_count
= 0;
5205 asection
*first_tls
= NULL
;
5206 asection
*first_mbind
= NULL
;
5207 asection
*dynsec
, *eh_frame_hdr
;
5210 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
5211 bfd_size_type phdr_size
; /* Octets/bytes. */
5212 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5214 /* Select the allocated sections, and sort them. */
5216 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
5217 sections
= (asection
**) bfd_malloc (amt
);
5218 if (sections
== NULL
)
5221 /* Calculate top address, avoiding undefined behaviour of shift
5222 left operator when shift count is equal to size of type
5224 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
5225 addr_mask
= (addr_mask
<< 1) + 1;
5228 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5230 if ((s
->flags
& SEC_ALLOC
) != 0)
5232 /* target_index is unused until bfd_elf_final_link
5233 starts output of section symbols. Use it to make
5235 s
->target_index
= i
;
5238 /* A wrapping section potentially clashes with header. */
5239 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
5240 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
5243 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
5246 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
5248 phdr_size
= elf_program_header_size (abfd
);
5249 if (phdr_size
== (bfd_size_type
) -1)
5250 phdr_size
= get_program_header_size (abfd
, info
);
5251 phdr_size
+= bed
->s
->sizeof_ehdr
;
5252 /* phdr_size is compared to LMA values which are in bytes. */
5255 maxpagesize
= info
->maxpagesize
;
5257 maxpagesize
= bed
->maxpagesize
;
5258 if (maxpagesize
== 0)
5260 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
5262 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
5263 >= (phdr_size
& (maxpagesize
- 1))))
5264 /* For compatibility with old scripts that may not be using
5265 SIZEOF_HEADERS, add headers when it looks like space has
5266 been left for them. */
5267 phdr_in_segment
= true;
5269 /* Build the mapping. */
5273 /* If we have a .interp section, then create a PT_PHDR segment for
5274 the program headers and a PT_INTERP segment for the .interp
5276 s
= bfd_get_section_by_name (abfd
, ".interp");
5277 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
5279 amt
= sizeof (struct elf_segment_map
);
5280 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5284 m
->p_type
= PT_PHDR
;
5286 m
->p_flags_valid
= 1;
5287 m
->includes_phdrs
= 1;
5288 phdr_in_segment
= true;
5292 amt
= sizeof (struct elf_segment_map
);
5293 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5297 m
->p_type
= PT_INTERP
;
5305 /* Look through the sections. We put sections in the same program
5306 segment when the start of the second section can be placed within
5307 a few bytes of the end of the first section. */
5313 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
5315 && (dynsec
->flags
& SEC_LOAD
) == 0)
5318 if ((abfd
->flags
& D_PAGED
) == 0)
5319 phdr_in_segment
= false;
5321 /* Deal with -Ttext or something similar such that the first section
5322 is not adjacent to the program headers. This is an
5323 approximation, since at this point we don't know exactly how many
5324 program headers we will need. */
5325 if (phdr_in_segment
&& count
> 0)
5327 bfd_vma phdr_lma
; /* Bytes. */
5328 bool separate_phdr
= false;
5330 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
5332 && info
->separate_code
5333 && (sections
[0]->flags
& SEC_CODE
) != 0)
5335 /* If data sections should be separate from code and
5336 thus not executable, and the first section is
5337 executable then put the file and program headers in
5338 their own PT_LOAD. */
5339 separate_phdr
= true;
5340 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
5341 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
5343 /* The file and program headers are currently on the
5344 same page as the first section. Put them on the
5345 previous page if we can. */
5346 if (phdr_lma
>= maxpagesize
)
5347 phdr_lma
-= maxpagesize
;
5349 separate_phdr
= false;
5352 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
5353 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
5354 /* If file and program headers would be placed at the end
5355 of memory then it's probably better to omit them. */
5356 phdr_in_segment
= false;
5357 else if (phdr_lma
< wrap_to
)
5358 /* If a section wraps around to where we'll be placing
5359 file and program headers, then the headers will be
5361 phdr_in_segment
= false;
5362 else if (separate_phdr
)
5364 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
5367 m
->p_paddr
= phdr_lma
* opb
;
5369 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
5370 m
->p_paddr_valid
= 1;
5373 phdr_in_segment
= false;
5377 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
5384 /* See if this section and the last one will fit in the same
5387 if (last_hdr
== NULL
)
5389 /* If we don't have a segment yet, then we don't need a new
5390 one (we build the last one after this loop). */
5391 new_segment
= false;
5393 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
5395 /* If this section has a different relation between the
5396 virtual address and the load address, then we need a new
5400 else if (hdr
->lma
< last_hdr
->lma
+ last_size
5401 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
5403 /* If this section has a load address that makes it overlap
5404 the previous section, then we need a new segment. */
5407 else if ((abfd
->flags
& D_PAGED
) != 0
5408 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
5409 == (hdr
->lma
& -maxpagesize
)))
5411 /* If we are demand paged then we can't map two disk
5412 pages onto the same memory page. */
5413 new_segment
= false;
5415 /* In the next test we have to be careful when last_hdr->lma is close
5416 to the end of the address space. If the aligned address wraps
5417 around to the start of the address space, then there are no more
5418 pages left in memory and it is OK to assume that the current
5419 section can be included in the current segment. */
5420 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
5421 + maxpagesize
> last_hdr
->lma
)
5422 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
5423 + maxpagesize
<= hdr
->lma
))
5425 /* If putting this section in this segment would force us to
5426 skip a page in the segment, then we need a new segment. */
5429 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
5430 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
5432 /* We don't want to put a loaded section after a
5433 nonloaded (ie. bss style) section in the same segment
5434 as that will force the non-loaded section to be loaded.
5435 Consider .tbss sections as loaded for this purpose. */
5438 else if ((abfd
->flags
& D_PAGED
) == 0)
5440 /* If the file is not demand paged, which means that we
5441 don't require the sections to be correctly aligned in the
5442 file, then there is no other reason for a new segment. */
5443 new_segment
= false;
5445 else if (info
!= NULL
5446 && info
->separate_code
5447 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
5452 && (hdr
->flags
& SEC_READONLY
) == 0)
5454 /* We don't want to put a writable section in a read only
5460 /* Otherwise, we can use the same segment. */
5461 new_segment
= false;
5464 /* Allow interested parties a chance to override our decision. */
5465 if (last_hdr
!= NULL
5467 && info
->callbacks
->override_segment_assignment
!= NULL
)
5469 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
5475 if ((hdr
->flags
& SEC_READONLY
) == 0)
5477 if ((hdr
->flags
& SEC_CODE
) != 0)
5480 /* .tbss sections effectively have zero size. */
5481 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5485 /* We need a new program segment. We must create a new program
5486 header holding all the sections from hdr_index until hdr. */
5488 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5495 if ((hdr
->flags
& SEC_READONLY
) == 0)
5500 if ((hdr
->flags
& SEC_CODE
) == 0)
5506 /* .tbss sections effectively have zero size. */
5507 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5509 phdr_in_segment
= false;
5512 /* Create a final PT_LOAD program segment, but not if it's just
5514 if (last_hdr
!= NULL
5515 && (i
- hdr_index
!= 1
5516 || !IS_TBSS (last_hdr
)))
5518 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5526 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5529 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5536 /* For each batch of consecutive loadable SHT_NOTE sections,
5537 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5538 because if we link together nonloadable .note sections and
5539 loadable .note sections, we will generate two .note sections
5540 in the output file. */
5541 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5543 if ((s
->flags
& SEC_LOAD
) != 0
5544 && elf_section_type (s
) == SHT_NOTE
)
5547 unsigned int alignment_power
= s
->alignment_power
;
5550 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5552 if (s2
->next
->alignment_power
== alignment_power
5553 && (s2
->next
->flags
& SEC_LOAD
) != 0
5554 && elf_section_type (s2
->next
) == SHT_NOTE
5555 && align_power (s2
->lma
+ s2
->size
/ opb
,
5562 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5563 amt
+= count
* sizeof (asection
*);
5564 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5568 m
->p_type
= PT_NOTE
;
5572 m
->sections
[m
->count
- count
--] = s
;
5573 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5576 m
->sections
[m
->count
- 1] = s
;
5577 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5581 if (s
->flags
& SEC_THREAD_LOCAL
)
5587 if (first_mbind
== NULL
5588 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5592 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5595 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5596 amt
+= tls_count
* sizeof (asection
*);
5597 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5602 m
->count
= tls_count
;
5603 /* Mandated PF_R. */
5605 m
->p_flags_valid
= 1;
5607 for (i
= 0; i
< tls_count
; ++i
)
5609 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5612 (_("%pB: TLS sections are not adjacent:"), abfd
);
5615 while (i
< tls_count
)
5617 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5619 _bfd_error_handler (_(" TLS: %pA"), s
);
5623 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5626 bfd_set_error (bfd_error_bad_value
);
5638 && (abfd
->flags
& D_PAGED
) != 0
5639 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5640 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5641 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5642 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5644 /* Mandated PF_R. */
5645 unsigned long p_flags
= PF_R
;
5646 if ((s
->flags
& SEC_READONLY
) == 0)
5648 if ((s
->flags
& SEC_CODE
) != 0)
5651 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5652 m
= bfd_zalloc (abfd
, amt
);
5656 m
->p_type
= (PT_GNU_MBIND_LO
5657 + elf_section_data (s
)->this_hdr
.sh_info
);
5659 m
->p_flags_valid
= 1;
5661 m
->p_flags
= p_flags
;
5667 s
= bfd_get_section_by_name (abfd
,
5668 NOTE_GNU_PROPERTY_SECTION_NAME
);
5669 if (s
!= NULL
&& s
->size
!= 0)
5671 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5672 m
= bfd_zalloc (abfd
, amt
);
5676 m
->p_type
= PT_GNU_PROPERTY
;
5678 m
->p_flags_valid
= 1;
5685 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5687 eh_frame_hdr
= elf_eh_frame_hdr (info
);
5688 if (eh_frame_hdr
!= NULL
5689 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5691 amt
= sizeof (struct elf_segment_map
);
5692 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5696 m
->p_type
= PT_GNU_EH_FRAME
;
5698 m
->sections
[0] = eh_frame_hdr
->output_section
;
5704 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5706 sframe
= elf_sframe (abfd
);
5708 && (sframe
->output_section
->flags
& SEC_LOAD
) != 0
5709 && sframe
->size
!= 0)
5711 amt
= sizeof (struct elf_segment_map
);
5712 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5716 m
->p_type
= PT_GNU_SFRAME
;
5718 m
->sections
[0] = sframe
->output_section
;
5724 if (elf_stack_flags (abfd
))
5726 amt
= sizeof (struct elf_segment_map
);
5727 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5731 m
->p_type
= PT_GNU_STACK
;
5732 m
->p_flags
= elf_stack_flags (abfd
);
5733 m
->p_align
= bed
->stack_align
;
5734 m
->p_flags_valid
= 1;
5735 m
->p_align_valid
= m
->p_align
!= 0;
5736 if (info
->stacksize
> 0)
5738 m
->p_size
= info
->stacksize
;
5739 m
->p_size_valid
= 1;
5746 if (info
!= NULL
&& info
->relro
)
5748 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5750 if (m
->p_type
== PT_LOAD
5752 && m
->sections
[0]->vma
>= info
->relro_start
5753 && m
->sections
[0]->vma
< info
->relro_end
)
5756 while (--i
!= (unsigned) -1)
5758 if (m
->sections
[i
]->size
> 0
5759 && (m
->sections
[i
]->flags
& SEC_LOAD
) != 0
5760 && (m
->sections
[i
]->flags
& SEC_HAS_CONTENTS
) != 0)
5764 if (i
!= (unsigned) -1)
5769 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5772 amt
= sizeof (struct elf_segment_map
);
5773 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5777 m
->p_type
= PT_GNU_RELRO
;
5784 elf_seg_map (abfd
) = mfirst
;
5787 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5790 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5792 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5801 /* Sort sections by address. */
5804 elf_sort_sections (const void *arg1
, const void *arg2
)
5806 const asection
*sec1
= *(const asection
**) arg1
;
5807 const asection
*sec2
= *(const asection
**) arg2
;
5808 bfd_size_type size1
, size2
;
5810 /* Sort by LMA first, since this is the address used to
5811 place the section into a segment. */
5812 if (sec1
->lma
< sec2
->lma
)
5814 else if (sec1
->lma
> sec2
->lma
)
5817 /* Then sort by VMA. Normally the LMA and the VMA will be
5818 the same, and this will do nothing. */
5819 if (sec1
->vma
< sec2
->vma
)
5821 else if (sec1
->vma
> sec2
->vma
)
5824 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5826 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5834 else if (TOEND (sec2
))
5839 /* Sort by size, to put zero sized sections
5840 before others at the same address. */
5842 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5843 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5850 return sec1
->target_index
- sec2
->target_index
;
5853 /* This qsort comparison functions sorts PT_LOAD segments first and
5854 by p_paddr, for assign_file_positions_for_load_sections. */
5857 elf_sort_segments (const void *arg1
, const void *arg2
)
5859 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5860 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5862 if (m1
->p_type
!= m2
->p_type
)
5864 if (m1
->p_type
== PT_NULL
)
5866 if (m2
->p_type
== PT_NULL
)
5868 return m1
->p_type
< m2
->p_type
? -1 : 1;
5870 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5871 return m1
->includes_filehdr
? -1 : 1;
5872 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5873 return m1
->no_sort_lma
? -1 : 1;
5874 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5876 bfd_vma lma1
, lma2
; /* Octets. */
5878 if (m1
->p_paddr_valid
)
5880 else if (m1
->count
!= 0)
5882 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5884 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5887 if (m2
->p_paddr_valid
)
5889 else if (m2
->count
!= 0)
5891 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5893 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5896 return lma1
< lma2
? -1 : 1;
5898 if (m1
->idx
!= m2
->idx
)
5899 return m1
->idx
< m2
->idx
? -1 : 1;
5903 /* Ian Lance Taylor writes:
5905 We shouldn't be using % with a negative signed number. That's just
5906 not good. We have to make sure either that the number is not
5907 negative, or that the number has an unsigned type. When the types
5908 are all the same size they wind up as unsigned. When file_ptr is a
5909 larger signed type, the arithmetic winds up as signed long long,
5912 What we're trying to say here is something like ``increase OFF by
5913 the least amount that will cause it to be equal to the VMA modulo
5915 /* In other words, something like:
5917 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5918 off_offset = off % bed->maxpagesize;
5919 if (vma_offset < off_offset)
5920 adjustment = vma_offset + bed->maxpagesize - off_offset;
5922 adjustment = vma_offset - off_offset;
5924 which can be collapsed into the expression below. */
5927 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5929 /* PR binutils/16199: Handle an alignment of zero. */
5930 if (maxpagesize
== 0)
5932 return ((vma
- off
) % maxpagesize
);
5936 print_segment_map (const struct elf_segment_map
*m
)
5939 const char *pt
= get_segment_type (m
->p_type
);
5944 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5945 sprintf (buf
, "LOPROC+%7.7x",
5946 (unsigned int) (m
->p_type
- PT_LOPROC
));
5947 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5948 sprintf (buf
, "LOOS+%7.7x",
5949 (unsigned int) (m
->p_type
- PT_LOOS
));
5951 snprintf (buf
, sizeof (buf
), "%8.8x",
5952 (unsigned int) m
->p_type
);
5956 fprintf (stderr
, "%s:", pt
);
5957 for (j
= 0; j
< m
->count
; j
++)
5958 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5963 /* Assign file positions to the sections based on the mapping from
5964 sections to segments. This function also sets up some fields in
5968 assign_file_positions_for_load_sections (bfd
*abfd
,
5969 struct bfd_link_info
*link_info
)
5971 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5972 struct elf_segment_map
*m
;
5973 struct elf_segment_map
*phdr_load_seg
;
5974 Elf_Internal_Phdr
*phdrs
;
5975 Elf_Internal_Phdr
*p
;
5976 file_ptr off
; /* Octets. */
5977 bfd_size_type maxpagesize
;
5978 unsigned int alloc
, actual
;
5980 struct elf_segment_map
**sorted_seg_map
;
5981 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5983 if (link_info
== NULL
5984 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
, NULL
))
5988 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5993 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5994 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5998 /* PR binutils/12467. */
5999 elf_elfheader (abfd
)->e_phoff
= 0;
6000 elf_elfheader (abfd
)->e_phentsize
= 0;
6003 elf_elfheader (abfd
)->e_phnum
= alloc
;
6005 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
6008 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
6012 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
6013 BFD_ASSERT (elf_program_header_size (abfd
)
6014 == actual
* bed
->s
->sizeof_phdr
);
6015 BFD_ASSERT (actual
>= alloc
);
6020 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
6024 /* We're writing the size in elf_program_header_size (abfd),
6025 see assign_file_positions_except_relocs, so make sure we have
6026 that amount allocated, with trailing space cleared.
6027 The variable alloc contains the computed need, while
6028 elf_program_header_size (abfd) contains the size used for the
6030 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6031 where the layout is forced to according to a larger size in the
6032 last iterations for the testcase ld-elf/header. */
6033 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
6034 + alloc
* sizeof (*sorted_seg_map
)));
6035 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
6036 elf_tdata (abfd
)->phdr
= phdrs
;
6040 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
6042 sorted_seg_map
[j
] = m
;
6043 /* If elf_segment_map is not from map_sections_to_segments, the
6044 sections may not be correctly ordered. NOTE: sorting should
6045 not be done to the PT_NOTE section of a corefile, which may
6046 contain several pseudo-sections artificially created by bfd.
6047 Sorting these pseudo-sections breaks things badly. */
6049 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
6050 && m
->p_type
== PT_NOTE
))
6052 for (i
= 0; i
< m
->count
; i
++)
6053 m
->sections
[i
]->target_index
= i
;
6054 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
6059 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
6063 if ((abfd
->flags
& D_PAGED
) != 0)
6065 if (link_info
!= NULL
)
6066 maxpagesize
= link_info
->maxpagesize
;
6068 maxpagesize
= bed
->maxpagesize
;
6071 /* Sections must map to file offsets past the ELF file header. */
6072 off
= bed
->s
->sizeof_ehdr
;
6073 /* And if one of the PT_LOAD headers doesn't include the program
6074 headers then we'll be mapping program headers in the usual
6075 position after the ELF file header. */
6076 phdr_load_seg
= NULL
;
6077 for (j
= 0; j
< alloc
; j
++)
6079 m
= sorted_seg_map
[j
];
6080 if (m
->p_type
!= PT_LOAD
)
6082 if (m
->includes_phdrs
)
6088 if (phdr_load_seg
== NULL
)
6089 off
+= actual
* bed
->s
->sizeof_phdr
;
6091 for (j
= 0; j
< alloc
; j
++)
6094 bfd_vma off_adjust
; /* Octets. */
6096 bfd_size_type p_align
;
6099 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6100 number of sections with contents contributing to both p_filesz
6101 and p_memsz, followed by a number of sections with no contents
6102 that just contribute to p_memsz. In this loop, OFF tracks next
6103 available file offset for PT_LOAD and PT_NOTE segments. */
6104 m
= sorted_seg_map
[j
];
6106 p
->p_type
= m
->p_type
;
6107 p
->p_flags
= m
->p_flags
;
6108 p_align
= bed
->p_align
;
6112 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
6114 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
6116 if (m
->p_paddr_valid
)
6117 p
->p_paddr
= m
->p_paddr
;
6118 else if (m
->count
== 0)
6121 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
6123 if (p
->p_type
== PT_LOAD
6124 && (abfd
->flags
& D_PAGED
) != 0)
6126 /* p_align in demand paged PT_LOAD segments effectively stores
6127 the maximum page size. When copying an executable with
6128 objcopy, we set m->p_align from the input file. Use this
6129 value for maxpagesize rather than bed->maxpagesize, which
6130 may be different. Note that we use maxpagesize for PT_TLS
6131 segment alignment later in this function, so we are relying
6132 on at least one PT_LOAD segment appearing before a PT_TLS
6134 if (m
->p_align_valid
)
6135 maxpagesize
= m
->p_align
;
6136 else if (p_align
!= 0
6137 && (link_info
== NULL
6138 || !link_info
->maxpagesize_is_set
))
6139 /* Set p_align to the default p_align value while laying
6140 out segments aligning to the maximum page size or the
6141 largest section alignment. The run-time loader can
6142 align segments to the default p_align value or the
6143 maximum page size, depending on system page size. */
6146 p
->p_align
= maxpagesize
;
6148 else if (m
->p_align_valid
)
6149 p
->p_align
= m
->p_align
;
6150 else if (m
->count
== 0)
6151 p
->p_align
= 1 << bed
->s
->log_file_align
;
6153 if (m
== phdr_load_seg
)
6155 if (!m
->includes_filehdr
)
6157 off
+= actual
* bed
->s
->sizeof_phdr
;
6160 no_contents
= false;
6162 if (p
->p_type
== PT_LOAD
6165 bfd_size_type align
; /* Bytes. */
6166 unsigned int align_power
= 0;
6168 if (m
->p_align_valid
)
6172 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
6174 unsigned int secalign
;
6176 secalign
= bfd_section_alignment (*secpp
);
6177 if (secalign
> align_power
)
6178 align_power
= secalign
;
6180 align
= (bfd_size_type
) 1 << align_power
;
6181 if (align
< maxpagesize
)
6183 /* If a section requires alignment higher than the
6184 default p_align value, don't set p_align to the
6185 default p_align value. */
6186 if (align
> p_align
)
6188 align
= maxpagesize
;
6192 /* If a section requires alignment higher than the
6193 maximum page size, set p_align to the section
6200 for (i
= 0; i
< m
->count
; i
++)
6201 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
6202 /* If we aren't making room for this section, then
6203 it must be SHT_NOBITS regardless of what we've
6204 set via struct bfd_elf_special_section. */
6205 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
6207 /* Find out whether this segment contains any loadable
6210 for (i
= 0; i
< m
->count
; i
++)
6211 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
6213 no_contents
= false;
6217 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
6219 /* Broken hardware and/or kernel require that files do not
6220 map the same page with different permissions on some hppa
6223 && (abfd
->flags
& D_PAGED
) != 0
6224 && bed
->no_page_alias
6225 && (off
& (maxpagesize
- 1)) != 0
6226 && ((off
& -maxpagesize
)
6227 == ((off
+ off_adjust
) & -maxpagesize
)))
6228 off_adjust
+= maxpagesize
;
6232 /* We shouldn't need to align the segment on disk since
6233 the segment doesn't need file space, but the gABI
6234 arguably requires the alignment and glibc ld.so
6235 checks it. So to comply with the alignment
6236 requirement but not waste file space, we adjust
6237 p_offset for just this segment. (OFF_ADJUST is
6238 subtracted from OFF later.) This may put p_offset
6239 past the end of file, but that shouldn't matter. */
6244 /* Make sure the .dynamic section is the first section in the
6245 PT_DYNAMIC segment. */
6246 else if (p
->p_type
== PT_DYNAMIC
6248 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
6251 (_("%pB: The first section in the PT_DYNAMIC segment"
6252 " is not the .dynamic section"),
6254 bfd_set_error (bfd_error_bad_value
);
6257 /* Set the note section type to SHT_NOTE. */
6258 else if (p
->p_type
== PT_NOTE
)
6259 for (i
= 0; i
< m
->count
; i
++)
6260 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
6262 if (m
->includes_filehdr
)
6264 if (!m
->p_flags_valid
)
6266 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
6267 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
6268 if (p
->p_type
== PT_LOAD
)
6272 if (p
->p_vaddr
< (bfd_vma
) off
6273 || (!m
->p_paddr_valid
6274 && p
->p_paddr
< (bfd_vma
) off
))
6277 (_("%pB: not enough room for program headers,"
6278 " try linking with -N"),
6280 bfd_set_error (bfd_error_bad_value
);
6284 if (!m
->p_paddr_valid
)
6288 else if (sorted_seg_map
[0]->includes_filehdr
)
6290 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
6291 p
->p_vaddr
= filehdr
->p_vaddr
;
6292 if (!m
->p_paddr_valid
)
6293 p
->p_paddr
= filehdr
->p_paddr
;
6297 if (m
->includes_phdrs
)
6299 if (!m
->p_flags_valid
)
6301 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
6302 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
6303 if (!m
->includes_filehdr
)
6305 if (p
->p_type
== PT_LOAD
)
6307 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
6310 p
->p_vaddr
-= off
- p
->p_offset
;
6311 if (!m
->p_paddr_valid
)
6312 p
->p_paddr
-= off
- p
->p_offset
;
6315 else if (phdr_load_seg
!= NULL
)
6317 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
6318 bfd_vma phdr_off
= 0; /* Octets. */
6319 if (phdr_load_seg
->includes_filehdr
)
6320 phdr_off
= bed
->s
->sizeof_ehdr
;
6321 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
6322 if (!m
->p_paddr_valid
)
6323 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
6324 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
6327 p
->p_offset
= bed
->s
->sizeof_ehdr
;
6331 if (p
->p_type
== PT_LOAD
6332 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
6334 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
6339 /* Put meaningless p_offset for PT_LOAD segments
6340 without file contents somewhere within the first
6341 page, in an attempt to not point past EOF. */
6342 bfd_size_type align
= maxpagesize
;
6343 if (align
< p
->p_align
)
6347 p
->p_offset
= off
% align
;
6352 file_ptr adjust
; /* Octets. */
6354 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
6356 p
->p_filesz
+= adjust
;
6357 p
->p_memsz
+= adjust
;
6361 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6362 maps. Set filepos for sections in PT_LOAD segments, and in
6363 core files, for sections in PT_NOTE segments.
6364 assign_file_positions_for_non_load_sections will set filepos
6365 for other sections and update p_filesz for other segments. */
6366 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
6369 bfd_size_type align
;
6370 Elf_Internal_Shdr
*this_hdr
;
6373 this_hdr
= &elf_section_data (sec
)->this_hdr
;
6374 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
6376 if ((p
->p_type
== PT_LOAD
6377 || p
->p_type
== PT_TLS
)
6378 && (this_hdr
->sh_type
!= SHT_NOBITS
6379 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
6380 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
6381 || p
->p_type
== PT_TLS
))))
6383 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
6384 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
6385 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
6386 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
6390 || p_end
< p_start
))
6393 /* xgettext:c-format */
6394 (_("%pB: section %pA lma %#" PRIx64
6395 " adjusted to %#" PRIx64
),
6396 abfd
, sec
, (uint64_t) s_start
/ opb
,
6397 (uint64_t) p_end
/ opb
);
6399 sec
->lma
= p_end
/ opb
;
6401 p
->p_memsz
+= adjust
;
6403 if (p
->p_type
== PT_LOAD
)
6405 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6408 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
6410 /* We have a PROGBITS section following NOBITS ones.
6411 Allocate file space for the NOBITS section(s).
6412 We don't need to write out the zeros, posix
6413 fseek past the end of data already written
6414 followed by a write at that location is
6415 guaranteed to result in zeros being read
6417 adjust
= p
->p_memsz
- p
->p_filesz
;
6420 /* We only adjust sh_offset in SHT_NOBITS sections
6421 as would seem proper for their address when the
6422 section is first in the segment. sh_offset
6423 doesn't really have any significance for
6424 SHT_NOBITS anyway, apart from a notional position
6425 relative to other sections. Historically we
6426 didn't bother with adjusting sh_offset and some
6427 programs depend on it not being adjusted. See
6428 pr12921 and pr25662. */
6429 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
6432 if (this_hdr
->sh_type
== SHT_NOBITS
)
6433 off_adjust
+= adjust
;
6436 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6437 p
->p_filesz
+= adjust
;
6440 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
6442 /* The section at i == 0 is the one that actually contains
6446 this_hdr
->sh_offset
= sec
->filepos
= off
;
6447 off
+= this_hdr
->sh_size
;
6448 p
->p_filesz
= this_hdr
->sh_size
;
6454 /* The rest are fake sections that shouldn't be written. */
6463 if (p
->p_type
== PT_LOAD
)
6465 this_hdr
->sh_offset
= sec
->filepos
= off
;
6466 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6467 off
+= this_hdr
->sh_size
;
6469 else if (this_hdr
->sh_type
== SHT_NOBITS
6470 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
6471 && this_hdr
->sh_offset
== 0)
6473 /* This is a .tbss section that didn't get a PT_LOAD.
6474 (See _bfd_elf_map_sections_to_segments "Create a
6475 final PT_LOAD".) Set sh_offset to the value it
6476 would have if we had created a zero p_filesz and
6477 p_memsz PT_LOAD header for the section. This
6478 also makes the PT_TLS header have the same
6480 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
6482 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
6485 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6487 p
->p_filesz
+= this_hdr
->sh_size
;
6488 /* A load section without SHF_ALLOC is something like
6489 a note section in a PT_NOTE segment. These take
6490 file space but are not loaded into memory. */
6491 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6492 p
->p_memsz
+= this_hdr
->sh_size
;
6494 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6496 if (p
->p_type
== PT_TLS
)
6497 p
->p_memsz
+= this_hdr
->sh_size
;
6499 /* .tbss is special. It doesn't contribute to p_memsz of
6501 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
6502 p
->p_memsz
+= this_hdr
->sh_size
;
6505 if (align
> p
->p_align
6506 && !m
->p_align_valid
6507 && (p
->p_type
!= PT_LOAD
6508 || (abfd
->flags
& D_PAGED
) == 0))
6512 if (!m
->p_flags_valid
)
6515 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
6517 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
6524 /* PR ld/20815 - Check that the program header segment, if
6525 present, will be loaded into memory. */
6526 if (p
->p_type
== PT_PHDR
6527 && phdr_load_seg
== NULL
6528 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
6529 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
6531 /* The fix for this error is usually to edit the linker script being
6532 used and set up the program headers manually. Either that or
6533 leave room for the headers at the start of the SECTIONS. */
6534 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6535 " by LOAD segment"),
6537 if (link_info
== NULL
)
6539 /* Arrange for the linker to exit with an error, deleting
6540 the output file unless --noinhibit-exec is given. */
6541 link_info
->callbacks
->info ("%X");
6544 /* Check that all sections are in a PT_LOAD segment.
6545 Don't check funky gdb generated core files. */
6546 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
6548 bool check_vma
= true;
6550 for (i
= 1; i
< m
->count
; i
++)
6551 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
6552 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
6553 ->this_hdr
), p
) != 0
6554 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
6555 ->this_hdr
), p
) != 0)
6557 /* Looks like we have overlays packed into the segment. */
6562 for (i
= 0; i
< m
->count
; i
++)
6564 Elf_Internal_Shdr
*this_hdr
;
6567 sec
= m
->sections
[i
];
6568 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6569 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6570 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6573 /* xgettext:c-format */
6574 (_("%pB: section `%pA' can't be allocated in segment %d"),
6576 print_segment_map (m
);
6581 p
->p_align
= p_align
;
6585 elf_next_file_pos (abfd
) = off
;
6587 if (link_info
!= NULL
6588 && phdr_load_seg
!= NULL
6589 && phdr_load_seg
->includes_filehdr
)
6591 /* There is a segment that contains both the file headers and the
6592 program headers, so provide a symbol __ehdr_start pointing there.
6593 A program can use this to examine itself robustly. */
6595 struct elf_link_hash_entry
*hash
6596 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6597 false, false, true);
6598 /* If the symbol was referenced and not defined, define it. */
6600 && (hash
->root
.type
== bfd_link_hash_new
6601 || hash
->root
.type
== bfd_link_hash_undefined
6602 || hash
->root
.type
== bfd_link_hash_undefweak
6603 || hash
->root
.type
== bfd_link_hash_common
))
6606 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6608 if (phdr_load_seg
->count
!= 0)
6609 /* The segment contains sections, so use the first one. */
6610 s
= phdr_load_seg
->sections
[0];
6612 /* Use the first (i.e. lowest-addressed) section in any segment. */
6613 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6614 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6622 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6623 hash
->root
.u
.def
.section
= s
;
6627 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6628 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6631 hash
->root
.type
= bfd_link_hash_defined
;
6632 hash
->def_regular
= 1;
6640 /* Determine if a bfd is a debuginfo file. Unfortunately there
6641 is no defined method for detecting such files, so we have to
6642 use heuristics instead. */
6645 is_debuginfo_file (bfd
*abfd
)
6647 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6650 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6651 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6652 Elf_Internal_Shdr
**headerp
;
6654 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6656 Elf_Internal_Shdr
*header
= * headerp
;
6658 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6659 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6660 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6661 && header
->sh_type
!= SHT_NOBITS
6662 && header
->sh_type
!= SHT_NOTE
)
6669 /* Assign file positions for other sections, except for compressed debug
6670 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6673 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6674 struct bfd_link_info
*link_info
)
6676 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6677 Elf_Internal_Shdr
**i_shdrpp
;
6678 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6679 Elf_Internal_Phdr
*phdrs
;
6680 Elf_Internal_Phdr
*p
;
6681 struct elf_segment_map
*m
;
6683 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6684 bfd_vma maxpagesize
;
6686 if (link_info
!= NULL
)
6687 maxpagesize
= link_info
->maxpagesize
;
6689 maxpagesize
= bed
->maxpagesize
;
6690 i_shdrpp
= elf_elfsections (abfd
);
6691 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6692 off
= elf_next_file_pos (abfd
);
6693 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6695 Elf_Internal_Shdr
*hdr
;
6699 if (hdr
->bfd_section
!= NULL
6700 && (hdr
->bfd_section
->filepos
!= 0
6701 || (hdr
->sh_type
== SHT_NOBITS
6702 && hdr
->contents
== NULL
)))
6703 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6704 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6706 if (hdr
->sh_size
!= 0
6707 /* PR 24717 - debuginfo files are known to be not strictly
6708 compliant with the ELF standard. In particular they often
6709 have .note.gnu.property sections that are outside of any
6710 loadable segment. This is not a problem for such files,
6711 so do not warn about them. */
6712 && ! is_debuginfo_file (abfd
))
6714 /* xgettext:c-format */
6715 (_("%pB: warning: allocated section `%s' not in segment"),
6717 (hdr
->bfd_section
== NULL
6719 : hdr
->bfd_section
->name
));
6720 /* We don't need to page align empty sections. */
6721 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6722 align
= maxpagesize
;
6724 align
= hdr
->sh_addralign
& -hdr
->sh_addralign
;
6725 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
, align
);
6726 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6729 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6730 && hdr
->bfd_section
== NULL
)
6731 /* We don't know the offset of these sections yet:
6732 their size has not been decided. */
6733 || (abfd
->is_linker_output
6734 && hdr
->bfd_section
!= NULL
6735 && (hdr
->sh_name
== -1u
6736 || bfd_section_is_ctf (hdr
->bfd_section
)))
6737 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6738 || (elf_symtab_shndx_list (abfd
) != NULL
6739 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6740 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6741 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6742 hdr
->sh_offset
= -1;
6744 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6746 elf_next_file_pos (abfd
) = off
;
6748 /* Now that we have set the section file positions, we can set up
6749 the file positions for the non PT_LOAD segments. */
6750 phdrs
= elf_tdata (abfd
)->phdr
;
6751 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6753 if (p
->p_type
== PT_GNU_RELRO
)
6755 bfd_vma start
, end
; /* Bytes. */
6758 if (link_info
!= NULL
)
6760 /* During linking the range of the RELRO segment is passed
6761 in link_info. Note that there may be padding between
6762 relro_start and the first RELRO section. */
6763 start
= link_info
->relro_start
;
6764 end
= link_info
->relro_end
;
6766 else if (m
->count
!= 0)
6768 if (!m
->p_size_valid
)
6770 start
= m
->sections
[0]->vma
;
6771 end
= start
+ m
->p_size
/ opb
;
6782 struct elf_segment_map
*lm
;
6783 const Elf_Internal_Phdr
*lp
;
6786 /* Find a LOAD segment containing a section in the RELRO
6788 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6790 lm
= lm
->next
, lp
++)
6792 if (lp
->p_type
== PT_LOAD
6794 && (lm
->sections
[lm
->count
- 1]->vma
6795 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6796 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6798 && lm
->sections
[0]->vma
< end
)
6804 /* Find the section starting the RELRO segment. */
6805 for (i
= 0; i
< lm
->count
; i
++)
6807 asection
*s
= lm
->sections
[i
];
6816 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6817 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6818 p
->p_offset
= lm
->sections
[i
]->filepos
;
6819 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6820 p
->p_filesz
= p
->p_memsz
;
6822 /* The RELRO segment typically ends a few bytes
6823 into .got.plt but other layouts are possible.
6824 In cases where the end does not match any
6825 loaded section (for instance is in file
6826 padding), trim p_filesz back to correspond to
6827 the end of loaded section contents. */
6828 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6829 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6831 /* Preserve the alignment and flags if they are
6832 valid. The gold linker generates RW/4 for
6833 the PT_GNU_RELRO section. It is better for
6834 objcopy/strip to honor these attributes
6835 otherwise gdb will choke when using separate
6837 if (!m
->p_align_valid
)
6839 if (!m
->p_flags_valid
)
6848 if (link_info
!= NULL
)
6850 (_("%pB: warning: unable to allocate any sections"
6851 " to PT_GNU_RELRO segment"),
6853 memset (p
, 0, sizeof *p
);
6856 else if (p
->p_type
== PT_GNU_STACK
)
6858 if (m
->p_size_valid
)
6859 p
->p_memsz
= m
->p_size
;
6861 else if (m
->count
!= 0)
6865 if (p
->p_type
!= PT_LOAD
6866 && (p
->p_type
!= PT_NOTE
6867 || bfd_get_format (abfd
) != bfd_core
))
6869 /* A user specified segment layout may include a PHDR
6870 segment that overlaps with a LOAD segment... */
6871 if (p
->p_type
== PT_PHDR
)
6877 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6879 /* PR 17512: file: 2195325e. */
6881 (_("%pB: error: non-load segment %d includes file header "
6882 "and/or program header"),
6883 abfd
, (int) (p
- phdrs
));
6888 p
->p_offset
= m
->sections
[0]->filepos
;
6889 for (i
= m
->count
; i
-- != 0;)
6891 asection
*sect
= m
->sections
[i
];
6892 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6893 if (hdr
->sh_type
!= SHT_NOBITS
)
6895 p
->p_filesz
= sect
->filepos
- p
->p_offset
+ hdr
->sh_size
;
6896 /* NB: p_memsz of the loadable PT_NOTE segment
6897 should be the same as p_filesz. */
6898 if (p
->p_type
== PT_NOTE
6899 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
6900 p
->p_memsz
= p
->p_filesz
;
6911 static elf_section_list
*
6912 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6914 for (;list
!= NULL
; list
= list
->next
)
6920 /* Work out the file positions of all the sections. This is called by
6921 _bfd_elf_compute_section_file_positions. All the section sizes and
6922 VMAs must be known before this is called.
6924 Reloc sections come in two flavours: Those processed specially as
6925 "side-channel" data attached to a section to which they apply, and
6926 those that bfd doesn't process as relocations. The latter sort are
6927 stored in a normal bfd section by bfd_section_from_shdr. We don't
6928 consider the former sort here, unless they form part of the loadable
6929 image. Reloc sections not assigned here (and compressed debugging
6930 sections and CTF sections which nothing else in the file can rely
6931 upon) will be handled later by assign_file_positions_for_relocs.
6933 We also don't set the positions of the .symtab and .strtab here. */
6936 assign_file_positions_except_relocs (bfd
*abfd
,
6937 struct bfd_link_info
*link_info
)
6939 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6940 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6941 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6944 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6945 && bfd_get_format (abfd
) != bfd_core
)
6947 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6948 unsigned int num_sec
= elf_numsections (abfd
);
6949 Elf_Internal_Shdr
**hdrpp
;
6953 /* Start after the ELF header. */
6954 off
= i_ehdrp
->e_ehsize
;
6956 /* We are not creating an executable, which means that we are
6957 not creating a program header, and that the actual order of
6958 the sections in the file is unimportant. */
6959 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6961 Elf_Internal_Shdr
*hdr
;
6964 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6965 && hdr
->bfd_section
== NULL
)
6966 /* Do not assign offsets for these sections yet: we don't know
6968 || (abfd
->is_linker_output
6969 && hdr
->bfd_section
!= NULL
6970 && (hdr
->sh_name
== -1u
6971 || bfd_section_is_ctf (hdr
->bfd_section
)))
6972 || i
== elf_onesymtab (abfd
)
6973 || (elf_symtab_shndx_list (abfd
) != NULL
6974 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6975 || i
== elf_strtab_sec (abfd
)
6976 || i
== elf_shstrtab_sec (abfd
))
6978 hdr
->sh_offset
= -1;
6981 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6984 elf_next_file_pos (abfd
) = off
;
6985 elf_program_header_size (abfd
) = 0;
6989 /* Assign file positions for the loaded sections based on the
6990 assignment of sections to segments. */
6991 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6994 /* And for non-load sections. */
6995 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6999 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
7002 /* Write out the program headers. */
7003 alloc
= i_ehdrp
->e_phnum
;
7006 if (link_info
!= NULL
&& ! link_info
->no_warn_rwx_segments
)
7008 /* Memory resident segments with non-zero size and RWX
7009 permissions are a security risk, so we generate a warning
7010 here if we are creating any. */
7013 for (i
= 0; i
< alloc
; i
++)
7015 const Elf_Internal_Phdr
* phdr
= tdata
->phdr
+ i
;
7017 if (phdr
->p_memsz
== 0)
7020 if (phdr
->p_type
== PT_TLS
&& (phdr
->p_flags
& PF_X
))
7021 _bfd_error_handler (_("warning: %pB has a TLS segment"
7022 " with execute permission"),
7024 else if (phdr
->p_type
== PT_LOAD
7025 && ((phdr
->p_flags
& (PF_R
| PF_W
| PF_X
))
7026 == (PF_R
| PF_W
| PF_X
)))
7027 _bfd_error_handler (_("warning: %pB has a LOAD segment"
7028 " with RWX permissions"),
7033 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
7034 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
7042 _bfd_elf_init_file_header (bfd
*abfd
,
7043 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
7045 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
7046 struct elf_strtab_hash
*shstrtab
;
7047 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7049 i_ehdrp
= elf_elfheader (abfd
);
7051 shstrtab
= _bfd_elf_strtab_init ();
7052 if (shstrtab
== NULL
)
7055 elf_shstrtab (abfd
) = shstrtab
;
7057 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
7058 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
7059 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
7060 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
7062 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
7063 i_ehdrp
->e_ident
[EI_DATA
] =
7064 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
7065 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
7067 if ((abfd
->flags
& DYNAMIC
) != 0)
7068 i_ehdrp
->e_type
= ET_DYN
;
7069 else if ((abfd
->flags
& EXEC_P
) != 0)
7070 i_ehdrp
->e_type
= ET_EXEC
;
7071 else if (bfd_get_format (abfd
) == bfd_core
)
7072 i_ehdrp
->e_type
= ET_CORE
;
7074 i_ehdrp
->e_type
= ET_REL
;
7076 switch (bfd_get_arch (abfd
))
7078 case bfd_arch_unknown
:
7079 i_ehdrp
->e_machine
= EM_NONE
;
7082 /* There used to be a long list of cases here, each one setting
7083 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7084 in the corresponding bfd definition. To avoid duplication,
7085 the switch was removed. Machines that need special handling
7086 can generally do it in elf_backend_final_write_processing(),
7087 unless they need the information earlier than the final write.
7088 Such need can generally be supplied by replacing the tests for
7089 e_machine with the conditions used to determine it. */
7091 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
7094 i_ehdrp
->e_version
= bed
->s
->ev_current
;
7095 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
7097 /* No program header, for now. */
7098 i_ehdrp
->e_phoff
= 0;
7099 i_ehdrp
->e_phentsize
= 0;
7100 i_ehdrp
->e_phnum
= 0;
7102 /* Each bfd section is section header entry. */
7103 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
7104 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
7106 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
7107 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
7108 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
7109 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
7110 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
7111 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
7112 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
7113 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
7114 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
7120 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7122 FIXME: We used to have code here to sort the PT_LOAD segments into
7123 ascending order, as per the ELF spec. But this breaks some programs,
7124 including the Linux kernel. But really either the spec should be
7125 changed or the programs updated. */
7128 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
7130 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
7132 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
7133 unsigned int num_segments
= i_ehdrp
->e_phnum
;
7134 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
7135 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
7136 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
7138 /* Find the lowest p_vaddr in PT_LOAD segments. */
7139 bfd_vma p_vaddr
= (bfd_vma
) -1;
7140 for (; segment
< end_segment
; segment
++)
7141 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
7142 p_vaddr
= segment
->p_vaddr
;
7144 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7145 segments is non-zero. */
7147 i_ehdrp
->e_type
= ET_EXEC
;
7152 /* Assign file positions for all the reloc sections which are not part
7153 of the loadable file image, and the file position of section headers. */
7156 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
7159 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
7160 Elf_Internal_Shdr
*shdrp
;
7161 Elf_Internal_Ehdr
*i_ehdrp
;
7162 const struct elf_backend_data
*bed
;
7164 /* Skip non-load sections without section header. */
7165 if ((abfd
->flags
& BFD_NO_SECTION_HEADER
) != 0)
7168 off
= elf_next_file_pos (abfd
);
7170 shdrpp
= elf_elfsections (abfd
);
7171 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
7172 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
7175 if (shdrp
->sh_offset
== -1)
7177 asection
*sec
= shdrp
->bfd_section
;
7179 || shdrp
->sh_type
== SHT_REL
7180 || shdrp
->sh_type
== SHT_RELA
)
7182 else if (bfd_section_is_ctf (sec
))
7184 /* Update section size and contents. */
7185 shdrp
->sh_size
= sec
->size
;
7186 shdrp
->contents
= sec
->contents
;
7188 else if (shdrp
->sh_name
== -1u)
7190 const char *name
= sec
->name
;
7191 struct bfd_elf_section_data
*d
;
7193 /* Compress DWARF debug sections. */
7194 if (!bfd_compress_section (abfd
, sec
, shdrp
->contents
))
7197 if (sec
->compress_status
== COMPRESS_SECTION_DONE
7198 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0
7201 /* If section is compressed with zlib-gnu, convert
7202 section name from .debug_* to .zdebug_*. */
7203 char *new_name
= bfd_debug_name_to_zdebug (abfd
, name
);
7204 if (new_name
== NULL
)
7208 /* Add section name to section name section. */
7210 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
7212 d
= elf_section_data (sec
);
7214 /* Add reloc section name to section name section. */
7216 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rel
.hdr
,
7220 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rela
.hdr
,
7224 /* Update section size and contents. */
7225 shdrp
->sh_size
= sec
->size
;
7226 shdrp
->contents
= sec
->contents
;
7227 sec
->contents
= NULL
;
7230 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
7234 /* Place section name section after DWARF debug sections have been
7236 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
7237 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
7238 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
7239 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
7241 /* Place the section headers. */
7242 i_ehdrp
= elf_elfheader (abfd
);
7243 bed
= get_elf_backend_data (abfd
);
7244 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
7245 i_ehdrp
->e_shoff
= off
;
7246 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
7247 elf_next_file_pos (abfd
) = off
;
7253 _bfd_elf_write_object_contents (bfd
*abfd
)
7255 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7256 Elf_Internal_Shdr
**i_shdrp
;
7258 unsigned int count
, num_sec
;
7259 struct elf_obj_tdata
*t
;
7261 if (! abfd
->output_has_begun
7262 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7264 /* Do not rewrite ELF data when the BFD has been opened for update.
7265 abfd->output_has_begun was set to TRUE on opening, so creation of
7266 new sections, and modification of existing section sizes was
7267 restricted. This means the ELF header, program headers and
7268 section headers can't have changed. If the contents of any
7269 sections has been modified, then those changes have already been
7270 written to the BFD. */
7271 else if (abfd
->direction
== both_direction
)
7273 BFD_ASSERT (abfd
->output_has_begun
);
7277 i_shdrp
= elf_elfsections (abfd
);
7280 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
7284 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
7287 /* After writing the headers, we need to write the sections too... */
7288 num_sec
= elf_numsections (abfd
);
7289 for (count
= 1; count
< num_sec
; count
++)
7291 /* Don't set the sh_name field without section header. */
7292 if ((abfd
->flags
& BFD_NO_SECTION_HEADER
) == 0)
7293 i_shdrp
[count
]->sh_name
7294 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
7295 i_shdrp
[count
]->sh_name
);
7296 if (bed
->elf_backend_section_processing
)
7297 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
7299 if (i_shdrp
[count
]->contents
)
7301 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
7303 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
7304 || bfd_write (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
7309 /* Write out the section header names. */
7310 t
= elf_tdata (abfd
);
7311 if (elf_shstrtab (abfd
) != NULL
7312 && t
->shstrtab_hdr
.sh_offset
!= -1
7313 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
7314 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
7317 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
7320 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
7323 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7324 if (t
->o
->build_id
.after_write_object_contents
!= NULL
7325 && !(*t
->o
->build_id
.after_write_object_contents
) (abfd
))
7327 if (t
->o
->package_metadata
.after_write_object_contents
!= NULL
7328 && !(*t
->o
->package_metadata
.after_write_object_contents
) (abfd
))
7335 _bfd_elf_write_corefile_contents (bfd
*abfd
)
7337 /* Hopefully this can be done just like an object file. */
7338 return _bfd_elf_write_object_contents (abfd
);
7341 /* Given a section, search the header to find them. */
7344 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
7346 const struct elf_backend_data
*bed
;
7347 unsigned int sec_index
;
7349 if (elf_section_data (asect
) != NULL
7350 && elf_section_data (asect
)->this_idx
!= 0)
7351 return elf_section_data (asect
)->this_idx
;
7353 if (bfd_is_abs_section (asect
))
7354 sec_index
= SHN_ABS
;
7355 else if (bfd_is_com_section (asect
))
7356 sec_index
= SHN_COMMON
;
7357 else if (bfd_is_und_section (asect
))
7358 sec_index
= SHN_UNDEF
;
7360 sec_index
= SHN_BAD
;
7362 bed
= get_elf_backend_data (abfd
);
7363 if (bed
->elf_backend_section_from_bfd_section
)
7365 int retval
= sec_index
;
7367 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
7371 if (sec_index
== SHN_BAD
)
7372 bfd_set_error (bfd_error_nonrepresentable_section
);
7377 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7381 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
7383 asymbol
*asym_ptr
= *asym_ptr_ptr
;
7385 flagword flags
= asym_ptr
->flags
;
7387 /* When gas creates relocations against local labels, it creates its
7388 own symbol for the section, but does put the symbol into the
7389 symbol chain, so udata is 0. When the linker is generating
7390 relocatable output, this section symbol may be for one of the
7391 input sections rather than the output section. */
7392 if (asym_ptr
->udata
.i
== 0
7393 && (flags
& BSF_SECTION_SYM
)
7394 && asym_ptr
->section
)
7398 sec
= asym_ptr
->section
;
7399 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
7400 sec
= sec
->output_section
;
7401 if (sec
->owner
== abfd
7402 && sec
->index
< elf_num_section_syms (abfd
)
7403 && elf_section_syms (abfd
)[sec
->index
] != NULL
)
7404 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
7407 idx
= asym_ptr
->udata
.i
;
7411 /* This case can occur when using --strip-symbol on a symbol
7412 which is used in a relocation entry. */
7414 /* xgettext:c-format */
7415 (_("%pB: symbol `%s' required but not present"),
7416 abfd
, bfd_asymbol_name (asym_ptr
));
7417 bfd_set_error (bfd_error_no_symbols
);
7424 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7425 " flags = 0x%.8x\n",
7426 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
7434 static inline bfd_vma
7435 segment_size (Elf_Internal_Phdr
*segment
)
7437 return (segment
->p_memsz
> segment
->p_filesz
7438 ? segment
->p_memsz
: segment
->p_filesz
);
7442 /* Returns the end address of the segment + 1. */
7443 static inline bfd_vma
7444 segment_end (Elf_Internal_Phdr
*segment
, bfd_vma start
)
7446 return start
+ segment_size (segment
);
7449 static inline bfd_size_type
7450 section_size (asection
*section
, Elf_Internal_Phdr
*segment
)
7452 if ((section
->flags
& SEC_HAS_CONTENTS
) != 0
7453 || (section
->flags
& SEC_THREAD_LOCAL
) == 0
7454 || segment
->p_type
== PT_TLS
)
7455 return section
->size
;
7459 /* Returns TRUE if the given section is contained within the given
7460 segment. LMA addresses are compared against PADDR when
7461 USE_VADDR is false, VMA against VADDR when true. */
7463 is_contained_by (asection
*section
, Elf_Internal_Phdr
*segment
,
7464 bfd_vma paddr
, bfd_vma vaddr
, unsigned int opb
,
7467 bfd_vma seg_addr
= !use_vaddr
? paddr
: vaddr
;
7468 bfd_vma addr
= !use_vaddr
? section
->lma
: section
->vma
;
7470 if (_bfd_mul_overflow (addr
, opb
, &octet
))
7472 /* The third and fourth lines below are testing that the section end
7473 address is within the segment. It's written this way to avoid
7474 overflow. Add seg_addr + section_size to both sides of the
7475 inequality to make it obvious. */
7476 return (octet
>= seg_addr
7477 && segment_size (segment
) >= section_size (section
, segment
)
7478 && (octet
- seg_addr
7479 <= segment_size (segment
) - section_size (section
, segment
)));
7482 /* Handle PT_NOTE segment. */
7484 is_note (asection
*s
, Elf_Internal_Phdr
*p
)
7486 return (p
->p_type
== PT_NOTE
7487 && elf_section_type (s
) == SHT_NOTE
7488 && (ufile_ptr
) s
->filepos
>= p
->p_offset
7489 && p
->p_filesz
>= s
->size
7490 && (ufile_ptr
) s
->filepos
- p
->p_offset
<= p
->p_filesz
- s
->size
);
7493 /* Rewrite program header information. */
7496 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
, bfd_vma maxpagesize
)
7498 Elf_Internal_Ehdr
*iehdr
;
7499 struct elf_segment_map
*map
;
7500 struct elf_segment_map
*map_first
;
7501 struct elf_segment_map
**pointer_to_map
;
7502 Elf_Internal_Phdr
*segment
;
7505 unsigned int num_segments
;
7506 bool phdr_included
= false;
7508 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
7509 unsigned int phdr_adjust_num
= 0;
7510 const struct elf_backend_data
*bed
;
7511 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7513 bed
= get_elf_backend_data (ibfd
);
7514 iehdr
= elf_elfheader (ibfd
);
7517 pointer_to_map
= &map_first
;
7519 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7521 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7522 linker, which generates a PT_INTERP section with p_vaddr and
7523 p_memsz set to 0. */
7524 #define IS_SOLARIS_PT_INTERP(p, s) \
7526 && p->p_paddr == 0 \
7527 && p->p_memsz == 0 \
7528 && p->p_filesz > 0 \
7529 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7531 && (bfd_vma) s->filepos >= p->p_offset \
7532 && ((bfd_vma) s->filepos + s->size \
7533 <= p->p_offset + p->p_filesz))
7535 /* Decide if the given section should be included in the given segment.
7536 A section will be included if:
7537 1. It is within the address space of the segment -- we use the LMA
7538 if that is set for the segment and the VMA otherwise,
7539 2. It is an allocated section or a NOTE section in a PT_NOTE
7541 3. There is an output section associated with it,
7542 4. The section has not already been allocated to a previous segment.
7543 5. PT_GNU_STACK segments do not include any sections.
7544 6. PT_TLS segment includes only SHF_TLS sections.
7545 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7546 8. PT_DYNAMIC should not contain empty sections at the beginning
7547 (with the possible exception of .dynamic). */
7548 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7549 (((is_contained_by (section, segment, segment->p_paddr, \
7550 segment->p_vaddr, opb, \
7551 bed->want_p_paddr_set_to_zero) \
7552 && (section->flags & SEC_ALLOC) != 0) \
7553 || is_note (section, segment)) \
7554 && segment->p_type != PT_GNU_STACK \
7555 && (segment->p_type != PT_TLS \
7556 || (section->flags & SEC_THREAD_LOCAL)) \
7557 && (segment->p_type == PT_LOAD \
7558 || segment->p_type == PT_TLS \
7559 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7560 && (segment->p_type != PT_DYNAMIC \
7561 || section_size (section, segment) > 0 \
7562 || (segment->p_paddr \
7563 ? segment->p_paddr != section->lma * (opb) \
7564 : segment->p_vaddr != section->vma * (opb)) \
7565 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7566 && (segment->p_type != PT_LOAD || !section->segment_mark))
7568 /* If the output section of a section in the input segment is NULL,
7569 it is removed from the corresponding output segment. */
7570 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7571 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7572 && section->output_section != NULL)
7574 /* Returns TRUE iff seg1 starts after the end of seg2. */
7575 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7576 (seg1->field >= segment_end (seg2, seg2->field))
7578 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7579 their VMA address ranges and their LMA address ranges overlap.
7580 It is possible to have overlapping VMA ranges without overlapping LMA
7581 ranges. RedBoot images for example can have both .data and .bss mapped
7582 to the same VMA range, but with the .data section mapped to a different
7584 #define SEGMENT_OVERLAPS(seg1, seg2) \
7585 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7586 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7587 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7588 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7590 /* Initialise the segment mark field, and discard stupid alignment. */
7591 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
7593 asection
*o
= section
->output_section
;
7594 if (o
!= NULL
&& o
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
7595 o
->alignment_power
= 0;
7596 section
->segment_mark
= false;
7599 /* The Solaris linker creates program headers in which all the
7600 p_paddr fields are zero. When we try to objcopy or strip such a
7601 file, we get confused. Check for this case, and if we find it
7602 don't set the p_paddr_valid fields. */
7603 p_paddr_valid
= false;
7604 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7607 if (segment
->p_paddr
!= 0)
7609 p_paddr_valid
= true;
7613 /* Scan through the segments specified in the program header
7614 of the input BFD. For this first scan we look for overlaps
7615 in the loadable segments. These can be created by weird
7616 parameters to objcopy. Also, fix some solaris weirdness. */
7617 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7622 Elf_Internal_Phdr
*segment2
;
7624 if (segment
->p_type
== PT_INTERP
)
7625 for (section
= ibfd
->sections
; section
; section
= section
->next
)
7626 if (IS_SOLARIS_PT_INTERP (segment
, section
))
7628 /* Mininal change so that the normal section to segment
7629 assignment code will work. */
7630 segment
->p_vaddr
= section
->vma
* opb
;
7634 if (segment
->p_type
!= PT_LOAD
)
7636 /* Remove PT_GNU_RELRO segment. */
7637 if (segment
->p_type
== PT_GNU_RELRO
)
7638 segment
->p_type
= PT_NULL
;
7642 /* Determine if this segment overlaps any previous segments. */
7643 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7645 bfd_signed_vma extra_length
;
7647 if (segment2
->p_type
!= PT_LOAD
7648 || !SEGMENT_OVERLAPS (segment
, segment2
))
7651 /* Merge the two segments together. */
7652 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7654 /* Extend SEGMENT2 to include SEGMENT and then delete
7656 extra_length
= (segment_end (segment
, segment
->p_vaddr
)
7657 - segment_end (segment2
, segment2
->p_vaddr
));
7659 if (extra_length
> 0)
7661 segment2
->p_memsz
+= extra_length
;
7662 segment2
->p_filesz
+= extra_length
;
7665 segment
->p_type
= PT_NULL
;
7667 /* Since we have deleted P we must restart the outer loop. */
7669 segment
= elf_tdata (ibfd
)->phdr
;
7674 /* Extend SEGMENT to include SEGMENT2 and then delete
7676 extra_length
= (segment_end (segment2
, segment2
->p_vaddr
)
7677 - segment_end (segment
, segment
->p_vaddr
));
7679 if (extra_length
> 0)
7681 segment
->p_memsz
+= extra_length
;
7682 segment
->p_filesz
+= extra_length
;
7685 segment2
->p_type
= PT_NULL
;
7690 /* The second scan attempts to assign sections to segments. */
7691 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7695 unsigned int section_count
;
7696 asection
**sections
;
7697 asection
*output_section
;
7699 asection
*matching_lma
;
7700 asection
*suggested_lma
;
7703 asection
*first_section
;
7705 if (segment
->p_type
== PT_NULL
)
7708 first_section
= NULL
;
7709 /* Compute how many sections might be placed into this segment. */
7710 for (section
= ibfd
->sections
, section_count
= 0;
7712 section
= section
->next
)
7714 /* Find the first section in the input segment, which may be
7715 removed from the corresponding output segment. */
7716 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7718 if (first_section
== NULL
)
7719 first_section
= section
;
7720 if (section
->output_section
!= NULL
)
7725 /* Allocate a segment map big enough to contain
7726 all of the sections we have selected. */
7727 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7728 amt
+= section_count
* sizeof (asection
*);
7729 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7733 /* Initialise the fields of the segment map. Default to
7734 using the physical address of the segment in the input BFD. */
7736 map
->p_type
= segment
->p_type
;
7737 map
->p_flags
= segment
->p_flags
;
7738 map
->p_flags_valid
= 1;
7740 if (map
->p_type
== PT_LOAD
7741 && (ibfd
->flags
& D_PAGED
) != 0
7743 && segment
->p_align
> 1)
7745 map
->p_align
= segment
->p_align
;
7746 if (segment
->p_align
> maxpagesize
)
7747 map
->p_align
= maxpagesize
;
7748 map
->p_align_valid
= 1;
7751 /* If the first section in the input segment is removed, there is
7752 no need to preserve segment physical address in the corresponding
7754 if (!first_section
|| first_section
->output_section
!= NULL
)
7756 map
->p_paddr
= segment
->p_paddr
;
7757 map
->p_paddr_valid
= p_paddr_valid
;
7760 /* Determine if this segment contains the ELF file header
7761 and if it contains the program headers themselves. */
7762 map
->includes_filehdr
= (segment
->p_offset
== 0
7763 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7764 map
->includes_phdrs
= 0;
7766 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7768 map
->includes_phdrs
=
7769 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7770 && (segment
->p_offset
+ segment
->p_filesz
7771 >= ((bfd_vma
) iehdr
->e_phoff
7772 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7774 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7775 phdr_included
= true;
7778 if (section_count
== 0)
7780 /* Special segments, such as the PT_PHDR segment, may contain
7781 no sections, but ordinary, loadable segments should contain
7782 something. They are allowed by the ELF spec however, so only
7783 a warning is produced.
7784 Don't warn if an empty PT_LOAD contains the program headers.
7785 There is however the valid use case of embedded systems which
7786 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7787 flash memory with zeros. No warning is shown for that case. */
7788 if (segment
->p_type
== PT_LOAD
7789 && !map
->includes_phdrs
7790 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7791 /* xgettext:c-format */
7793 (_("%pB: warning: empty loadable segment detected"
7794 " at vaddr=%#" PRIx64
", is this intentional?"),
7795 ibfd
, (uint64_t) segment
->p_vaddr
);
7797 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7799 *pointer_to_map
= map
;
7800 pointer_to_map
= &map
->next
;
7805 /* Now scan the sections in the input BFD again and attempt
7806 to add their corresponding output sections to the segment map.
7807 The problem here is how to handle an output section which has
7808 been moved (ie had its LMA changed). There are four possibilities:
7810 1. None of the sections have been moved.
7811 In this case we can continue to use the segment LMA from the
7814 2. All of the sections have been moved by the same amount.
7815 In this case we can change the segment's LMA to match the LMA
7816 of the first section.
7818 3. Some of the sections have been moved, others have not.
7819 In this case those sections which have not been moved can be
7820 placed in the current segment which will have to have its size,
7821 and possibly its LMA changed, and a new segment or segments will
7822 have to be created to contain the other sections.
7824 4. The sections have been moved, but not by the same amount.
7825 In this case we can change the segment's LMA to match the LMA
7826 of the first section and we will have to create a new segment
7827 or segments to contain the other sections.
7829 In order to save time, we allocate an array to hold the section
7830 pointers that we are interested in. As these sections get assigned
7831 to a segment, they are removed from this array. */
7833 amt
= section_count
* sizeof (asection
*);
7834 sections
= (asection
**) bfd_malloc (amt
);
7835 if (sections
== NULL
)
7838 /* Step One: Scan for segment vs section LMA conflicts.
7839 Also add the sections to the section array allocated above.
7840 Also add the sections to the current segment. In the common
7841 case, where the sections have not been moved, this means that
7842 we have completely filled the segment, and there is nothing
7845 matching_lma
= NULL
;
7846 suggested_lma
= NULL
;
7848 for (section
= first_section
, j
= 0;
7850 section
= section
->next
)
7852 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7854 output_section
= section
->output_section
;
7856 sections
[j
++] = section
;
7858 /* The Solaris native linker always sets p_paddr to 0.
7859 We try to catch that case here, and set it to the
7860 correct value. Note - some backends require that
7861 p_paddr be left as zero. */
7863 && segment
->p_vaddr
!= 0
7864 && !bed
->want_p_paddr_set_to_zero
7866 && output_section
->lma
!= 0
7867 && (align_power (segment
->p_vaddr
7868 + (map
->includes_filehdr
7869 ? iehdr
->e_ehsize
: 0)
7870 + (map
->includes_phdrs
7871 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7873 output_section
->alignment_power
* opb
)
7874 == (output_section
->vma
* opb
)))
7875 map
->p_paddr
= segment
->p_vaddr
;
7877 /* Match up the physical address of the segment with the
7878 LMA address of the output section. */
7879 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7881 || is_note (section
, segment
))
7883 if (matching_lma
== NULL
7884 || output_section
->lma
< matching_lma
->lma
)
7885 matching_lma
= output_section
;
7887 /* We assume that if the section fits within the segment
7888 then it does not overlap any other section within that
7890 map
->sections
[isec
++] = output_section
;
7892 else if (suggested_lma
== NULL
)
7893 suggested_lma
= output_section
;
7895 if (j
== section_count
)
7900 BFD_ASSERT (j
== section_count
);
7902 /* Step Two: Adjust the physical address of the current segment,
7904 if (isec
== section_count
)
7906 /* All of the sections fitted within the segment as currently
7907 specified. This is the default case. Add the segment to
7908 the list of built segments and carry on to process the next
7909 program header in the input BFD. */
7910 map
->count
= section_count
;
7911 *pointer_to_map
= map
;
7912 pointer_to_map
= &map
->next
;
7915 && !bed
->want_p_paddr_set_to_zero
)
7917 bfd_vma hdr_size
= 0;
7918 if (map
->includes_filehdr
)
7919 hdr_size
= iehdr
->e_ehsize
;
7920 if (map
->includes_phdrs
)
7921 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7923 /* Account for padding before the first section in the
7925 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7926 - matching_lma
->lma
);
7934 /* Change the current segment's physical address to match
7935 the LMA of the first section that fitted, or if no
7936 section fitted, the first section. */
7937 if (matching_lma
== NULL
)
7938 matching_lma
= suggested_lma
;
7940 map
->p_paddr
= matching_lma
->lma
* opb
;
7942 /* Offset the segment physical address from the lma
7943 to allow for space taken up by elf headers. */
7944 if (map
->includes_phdrs
)
7946 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7948 /* iehdr->e_phnum is just an estimate of the number
7949 of program headers that we will need. Make a note
7950 here of the number we used and the segment we chose
7951 to hold these headers, so that we can adjust the
7952 offset when we know the correct value. */
7953 phdr_adjust_num
= iehdr
->e_phnum
;
7954 phdr_adjust_seg
= map
;
7957 if (map
->includes_filehdr
)
7959 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7960 map
->p_paddr
-= iehdr
->e_ehsize
;
7961 /* We've subtracted off the size of headers from the
7962 first section lma, but there may have been some
7963 alignment padding before that section too. Try to
7964 account for that by adjusting the segment lma down to
7965 the same alignment. */
7966 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7967 align
= segment
->p_align
;
7968 map
->p_paddr
&= -(align
* opb
);
7972 /* Step Three: Loop over the sections again, this time assigning
7973 those that fit to the current segment and removing them from the
7974 sections array; but making sure not to leave large gaps. Once all
7975 possible sections have been assigned to the current segment it is
7976 added to the list of built segments and if sections still remain
7977 to be assigned, a new segment is constructed before repeating
7983 suggested_lma
= NULL
;
7985 /* Fill the current segment with sections that fit. */
7986 for (j
= 0; j
< section_count
; j
++)
7988 section
= sections
[j
];
7990 if (section
== NULL
)
7993 output_section
= section
->output_section
;
7995 BFD_ASSERT (output_section
!= NULL
);
7997 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7999 || is_note (section
, segment
))
8001 if (map
->count
== 0)
8003 /* If the first section in a segment does not start at
8004 the beginning of the segment, then something is
8006 if (align_power (map
->p_paddr
8007 + (map
->includes_filehdr
8008 ? iehdr
->e_ehsize
: 0)
8009 + (map
->includes_phdrs
8010 ? iehdr
->e_phnum
* iehdr
->e_phentsize
8012 output_section
->alignment_power
* opb
)
8013 != output_section
->lma
* opb
)
8020 prev_sec
= map
->sections
[map
->count
- 1];
8022 /* If the gap between the end of the previous section
8023 and the start of this section is more than
8024 maxpagesize then we need to start a new segment. */
8025 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
8027 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
8028 || (prev_sec
->lma
+ prev_sec
->size
8029 > output_section
->lma
))
8031 if (suggested_lma
== NULL
)
8032 suggested_lma
= output_section
;
8038 map
->sections
[map
->count
++] = output_section
;
8041 if (segment
->p_type
== PT_LOAD
)
8042 section
->segment_mark
= true;
8044 else if (suggested_lma
== NULL
)
8045 suggested_lma
= output_section
;
8048 /* PR 23932. A corrupt input file may contain sections that cannot
8049 be assigned to any segment - because for example they have a
8050 negative size - or segments that do not contain any sections.
8051 But there are also valid reasons why a segment can be empty.
8052 So allow a count of zero. */
8054 /* Add the current segment to the list of built segments. */
8055 *pointer_to_map
= map
;
8056 pointer_to_map
= &map
->next
;
8058 if (isec
< section_count
)
8060 /* We still have not allocated all of the sections to
8061 segments. Create a new segment here, initialise it
8062 and carry on looping. */
8063 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
8064 amt
+= section_count
* sizeof (asection
*);
8065 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
8072 /* Initialise the fields of the segment map. Set the physical
8073 physical address to the LMA of the first section that has
8074 not yet been assigned. */
8076 map
->p_type
= segment
->p_type
;
8077 map
->p_flags
= segment
->p_flags
;
8078 map
->p_flags_valid
= 1;
8079 map
->p_paddr
= suggested_lma
->lma
* opb
;
8080 map
->p_paddr_valid
= p_paddr_valid
;
8081 map
->includes_filehdr
= 0;
8082 map
->includes_phdrs
= 0;
8087 bfd_set_error (bfd_error_sorry
);
8091 while (isec
< section_count
);
8096 elf_seg_map (obfd
) = map_first
;
8098 /* If we had to estimate the number of program headers that were
8099 going to be needed, then check our estimate now and adjust
8100 the offset if necessary. */
8101 if (phdr_adjust_seg
!= NULL
)
8105 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
8108 if (count
> phdr_adjust_num
)
8109 phdr_adjust_seg
->p_paddr
8110 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
8112 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
8113 if (map
->p_type
== PT_PHDR
)
8116 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
8117 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
8122 #undef IS_SOLARIS_PT_INTERP
8123 #undef IS_SECTION_IN_INPUT_SEGMENT
8124 #undef INCLUDE_SECTION_IN_SEGMENT
8125 #undef SEGMENT_AFTER_SEGMENT
8126 #undef SEGMENT_OVERLAPS
8130 /* Return true if p_align in the ELF program header in ABFD is valid. */
8133 elf_is_p_align_valid (bfd
*abfd
)
8136 Elf_Internal_Phdr
*segment
;
8137 unsigned int num_segments
;
8138 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8139 bfd_size_type maxpagesize
= bed
->maxpagesize
;
8140 bfd_size_type p_align
= bed
->p_align
;
8142 /* Return true if the default p_align value isn't set or the maximum
8143 page size is the same as the minimum page size. */
8144 if (p_align
== 0 || maxpagesize
== bed
->minpagesize
)
8147 /* When the default p_align value is set, p_align may be set to the
8148 default p_align value while segments are aligned to the maximum
8149 page size. In this case, the input p_align will be ignored and
8150 the maximum page size will be used to align the output segments. */
8151 segment
= elf_tdata (abfd
)->phdr
;
8152 num_segments
= elf_elfheader (abfd
)->e_phnum
;
8153 for (i
= 0; i
< num_segments
; i
++, segment
++)
8154 if (segment
->p_type
== PT_LOAD
8155 && (segment
->p_align
!= p_align
8156 || vma_page_aligned_bias (segment
->p_vaddr
,
8164 /* Copy ELF program header information. */
8167 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
8169 Elf_Internal_Ehdr
*iehdr
;
8170 struct elf_segment_map
*map
;
8171 struct elf_segment_map
*map_first
;
8172 struct elf_segment_map
**pointer_to_map
;
8173 Elf_Internal_Phdr
*segment
;
8175 unsigned int num_segments
;
8176 bool phdr_included
= false;
8178 bool p_palign_valid
;
8179 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
8181 iehdr
= elf_elfheader (ibfd
);
8184 pointer_to_map
= &map_first
;
8186 /* If all the segment p_paddr fields are zero, don't set
8187 map->p_paddr_valid. */
8188 p_paddr_valid
= false;
8189 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8190 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8193 if (segment
->p_paddr
!= 0)
8195 p_paddr_valid
= true;
8199 p_palign_valid
= elf_is_p_align_valid (ibfd
);
8201 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8206 unsigned int section_count
;
8208 Elf_Internal_Shdr
*this_hdr
;
8209 asection
*first_section
= NULL
;
8210 asection
*lowest_section
;
8212 /* Compute how many sections are in this segment. */
8213 for (section
= ibfd
->sections
, section_count
= 0;
8215 section
= section
->next
)
8217 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8218 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8220 if (first_section
== NULL
)
8221 first_section
= section
;
8226 /* Allocate a segment map big enough to contain
8227 all of the sections we have selected. */
8228 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
8229 amt
+= section_count
* sizeof (asection
*);
8230 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
8234 /* Initialize the fields of the output segment map with the
8237 map
->p_type
= segment
->p_type
;
8238 map
->p_flags
= segment
->p_flags
;
8239 map
->p_flags_valid
= 1;
8240 map
->p_paddr
= segment
->p_paddr
;
8241 map
->p_paddr_valid
= p_paddr_valid
;
8242 map
->p_align
= segment
->p_align
;
8243 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8244 map
->p_align_valid
= (map
->p_type
== PT_GNU_STACK
8246 map
->p_vaddr_offset
= 0;
8248 if (map
->p_type
== PT_GNU_RELRO
8249 || map
->p_type
== PT_GNU_STACK
)
8251 /* The PT_GNU_RELRO segment may contain the first a few
8252 bytes in the .got.plt section even if the whole .got.plt
8253 section isn't in the PT_GNU_RELRO segment. We won't
8254 change the size of the PT_GNU_RELRO segment.
8255 Similarly, PT_GNU_STACK size is significant on uclinux
8257 map
->p_size
= segment
->p_memsz
;
8258 map
->p_size_valid
= 1;
8261 /* Determine if this segment contains the ELF file header
8262 and if it contains the program headers themselves. */
8263 map
->includes_filehdr
= (segment
->p_offset
== 0
8264 && segment
->p_filesz
>= iehdr
->e_ehsize
);
8266 map
->includes_phdrs
= 0;
8267 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
8269 map
->includes_phdrs
=
8270 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
8271 && (segment
->p_offset
+ segment
->p_filesz
8272 >= ((bfd_vma
) iehdr
->e_phoff
8273 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
8275 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
8276 phdr_included
= true;
8279 lowest_section
= NULL
;
8280 if (section_count
!= 0)
8282 unsigned int isec
= 0;
8284 for (section
= first_section
;
8286 section
= section
->next
)
8288 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8289 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8291 map
->sections
[isec
++] = section
->output_section
;
8292 if ((section
->flags
& SEC_ALLOC
) != 0)
8296 if (lowest_section
== NULL
8297 || section
->lma
< lowest_section
->lma
)
8298 lowest_section
= section
;
8300 /* Section lmas are set up from PT_LOAD header
8301 p_paddr in _bfd_elf_make_section_from_shdr.
8302 If this header has a p_paddr that disagrees
8303 with the section lma, flag the p_paddr as
8305 if ((section
->flags
& SEC_LOAD
) != 0)
8306 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
8308 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
8309 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
8310 map
->p_paddr_valid
= false;
8312 if (isec
== section_count
)
8318 if (section_count
== 0)
8319 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
8320 else if (map
->p_paddr_valid
)
8322 /* Account for padding before the first section in the segment. */
8323 bfd_vma hdr_size
= 0;
8324 if (map
->includes_filehdr
)
8325 hdr_size
= iehdr
->e_ehsize
;
8326 if (map
->includes_phdrs
)
8327 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
8329 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
8330 - (lowest_section
? lowest_section
->lma
: 0));
8333 map
->count
= section_count
;
8334 *pointer_to_map
= map
;
8335 pointer_to_map
= &map
->next
;
8338 elf_seg_map (obfd
) = map_first
;
8342 /* Copy private BFD data. This copies or rewrites ELF program header
8346 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
8348 bfd_vma maxpagesize
;
8350 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8351 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8354 if (elf_tdata (ibfd
)->phdr
== NULL
)
8357 if (ibfd
->xvec
== obfd
->xvec
)
8359 /* Check to see if any sections in the input BFD
8360 covered by ELF program header have changed. */
8361 Elf_Internal_Phdr
*segment
;
8362 asection
*section
, *osec
;
8363 unsigned int i
, num_segments
;
8364 Elf_Internal_Shdr
*this_hdr
;
8365 const struct elf_backend_data
*bed
;
8367 bed
= get_elf_backend_data (ibfd
);
8369 /* Regenerate the segment map if p_paddr is set to 0. */
8370 if (bed
->want_p_paddr_set_to_zero
)
8373 /* Initialize the segment mark field. */
8374 for (section
= obfd
->sections
; section
!= NULL
;
8375 section
= section
->next
)
8376 section
->segment_mark
= false;
8378 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8379 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8383 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8384 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8385 which severly confuses things, so always regenerate the segment
8386 map in this case. */
8387 if (segment
->p_paddr
== 0
8388 && segment
->p_memsz
== 0
8389 && (segment
->p_type
== PT_INTERP
8390 || segment
->p_type
== PT_DYNAMIC
))
8393 for (section
= ibfd
->sections
;
8394 section
!= NULL
; section
= section
->next
)
8396 /* We mark the output section so that we know it comes
8397 from the input BFD. */
8398 osec
= section
->output_section
;
8400 osec
->segment_mark
= true;
8402 /* Check if this section is covered by the segment. */
8403 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8404 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8406 /* FIXME: Check if its output section is changed or
8407 removed. What else do we need to check? */
8409 || section
->flags
!= osec
->flags
8410 || section
->lma
!= osec
->lma
8411 || section
->vma
!= osec
->vma
8412 || section
->size
!= osec
->size
8413 || section
->rawsize
!= osec
->rawsize
8414 || section
->alignment_power
!= osec
->alignment_power
)
8420 /* Check to see if any output section do not come from the
8422 for (section
= obfd
->sections
; section
!= NULL
;
8423 section
= section
->next
)
8425 if (!section
->segment_mark
)
8428 section
->segment_mark
= false;
8431 return copy_elf_program_header (ibfd
, obfd
);
8436 if (ibfd
->xvec
== obfd
->xvec
)
8438 /* When rewriting program header, set the output maxpagesize to
8439 the maximum alignment of input PT_LOAD segments. */
8440 Elf_Internal_Phdr
*segment
;
8442 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8444 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8447 if (segment
->p_type
== PT_LOAD
8448 && maxpagesize
< segment
->p_align
)
8450 /* PR 17512: file: f17299af. */
8451 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
8452 /* xgettext:c-format */
8453 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8454 PRIx64
" is too large"),
8455 ibfd
, (uint64_t) segment
->p_align
);
8457 maxpagesize
= segment
->p_align
;
8460 if (maxpagesize
== 0)
8461 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
8463 return rewrite_elf_program_header (ibfd
, obfd
, maxpagesize
);
8466 /* Initialize private output section information from input section. */
8469 _bfd_elf_init_private_section_data (bfd
*ibfd
,
8473 struct bfd_link_info
*link_info
)
8476 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8477 bool final_link
= (link_info
!= NULL
8478 && !bfd_link_relocatable (link_info
));
8480 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8481 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8484 BFD_ASSERT (elf_section_data (osec
) != NULL
);
8486 /* If this is a known ABI section, ELF section type and flags may
8487 have been set up when OSEC was created. For normal sections we
8488 allow the user to override the type and flags other than
8489 SHF_MASKOS and SHF_MASKPROC. */
8490 if (elf_section_type (osec
) == SHT_PROGBITS
8491 || elf_section_type (osec
) == SHT_NOTE
8492 || elf_section_type (osec
) == SHT_NOBITS
)
8493 elf_section_type (osec
) = SHT_NULL
;
8494 /* For objcopy and relocatable link, copy the ELF section type from
8495 the input file if the BFD section flags are the same. (If they
8496 are different the user may be doing something like
8497 "objcopy --set-section-flags .text=alloc,data".) For a final
8498 link allow some flags that the linker clears to differ. */
8499 if (elf_section_type (osec
) == SHT_NULL
8500 && (osec
->flags
== isec
->flags
8502 && ((osec
->flags
^ isec
->flags
)
8503 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
8504 elf_section_type (osec
) = elf_section_type (isec
);
8506 /* FIXME: Is this correct for all OS/PROC specific flags? */
8507 elf_section_flags (osec
) = (elf_section_flags (isec
)
8508 & (SHF_MASKOS
| SHF_MASKPROC
));
8510 /* Copy sh_info from input for mbind section. */
8511 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
8512 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
8513 elf_section_data (osec
)->this_hdr
.sh_info
8514 = elf_section_data (isec
)->this_hdr
.sh_info
;
8516 /* Set things up for objcopy and relocatable link. The output
8517 SHT_GROUP section will have its elf_next_in_group pointing back
8518 to the input group members. Ignore linker created group section.
8519 See elfNN_ia64_object_p in elfxx-ia64.c. */
8520 if ((link_info
== NULL
8521 || !link_info
->resolve_section_groups
)
8522 && (elf_sec_group (isec
) == NULL
8523 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
8525 if (elf_section_flags (isec
) & SHF_GROUP
)
8526 elf_section_flags (osec
) |= SHF_GROUP
;
8527 elf_next_in_group (osec
) = elf_next_in_group (isec
);
8528 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
8531 /* If not decompress, preserve SHF_COMPRESSED. */
8532 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
8533 elf_section_flags (osec
) |= (elf_section_flags (isec
)
8536 ihdr
= &elf_section_data (isec
)->this_hdr
;
8538 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8539 don't use the output section of the linked-to section since it
8540 may be NULL at this point. */
8541 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
8543 ohdr
= &elf_section_data (osec
)->this_hdr
;
8544 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
8545 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
8548 osec
->use_rela_p
= isec
->use_rela_p
;
8553 /* Copy private section information. This copies over the entsize
8554 field, and sometimes the info field. */
8557 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
8562 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8564 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8565 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8568 ihdr
= &elf_section_data (isec
)->this_hdr
;
8569 ohdr
= &elf_section_data (osec
)->this_hdr
;
8571 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
8573 if (ihdr
->sh_type
== SHT_SYMTAB
8574 || ihdr
->sh_type
== SHT_DYNSYM
8575 || ihdr
->sh_type
== SHT_GNU_verneed
8576 || ihdr
->sh_type
== SHT_GNU_verdef
)
8577 ohdr
->sh_info
= ihdr
->sh_info
;
8579 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
8583 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8584 necessary if we are removing either the SHT_GROUP section or any of
8585 the group member sections. DISCARDED is the value that a section's
8586 output_section has if the section will be discarded, NULL when this
8587 function is called from objcopy, bfd_abs_section_ptr when called
8591 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
8595 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
8596 if (elf_section_type (isec
) == SHT_GROUP
)
8598 asection
*first
= elf_next_in_group (isec
);
8599 asection
*s
= first
;
8600 bfd_size_type removed
= 0;
8604 /* If this member section is being output but the
8605 SHT_GROUP section is not, then clear the group info
8606 set up by _bfd_elf_copy_private_section_data. */
8607 if (s
->output_section
!= discarded
8608 && isec
->output_section
== discarded
)
8610 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
8611 elf_group_name (s
->output_section
) = NULL
;
8615 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
8616 if (s
->output_section
== discarded
8617 && isec
->output_section
!= discarded
)
8619 /* Conversely, if the member section is not being
8620 output but the SHT_GROUP section is, then adjust
8623 if (elf_sec
->rel
.hdr
!= NULL
8624 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8626 if (elf_sec
->rela
.hdr
!= NULL
8627 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8632 /* Also adjust for zero-sized relocation member
8634 if (elf_sec
->rel
.hdr
!= NULL
8635 && elf_sec
->rel
.hdr
->sh_size
== 0)
8637 if (elf_sec
->rela
.hdr
!= NULL
8638 && elf_sec
->rela
.hdr
->sh_size
== 0)
8642 s
= elf_next_in_group (s
);
8648 if (discarded
!= NULL
)
8650 /* If we've been called for ld -r, then we need to
8651 adjust the input section size. */
8652 if (isec
->rawsize
== 0)
8653 isec
->rawsize
= isec
->size
;
8654 isec
->size
= isec
->rawsize
- removed
;
8655 if (isec
->size
<= 4)
8658 isec
->flags
|= SEC_EXCLUDE
;
8661 else if (isec
->output_section
!= NULL
)
8663 /* Adjust the output section size when called from
8665 isec
->output_section
->size
-= removed
;
8666 if (isec
->output_section
->size
<= 4)
8668 isec
->output_section
->size
= 0;
8669 isec
->output_section
->flags
|= SEC_EXCLUDE
;
8678 /* Copy private header information. */
8681 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
8683 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8684 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8687 /* Copy over private BFD data if it has not already been copied.
8688 This must be done here, rather than in the copy_private_bfd_data
8689 entry point, because the latter is called after the section
8690 contents have been set, which means that the program headers have
8691 already been worked out. */
8692 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
8694 if (! copy_private_bfd_data (ibfd
, obfd
))
8698 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8701 /* Copy private symbol information. If this symbol is in a section
8702 which we did not map into a BFD section, try to map the section
8703 index correctly. We use special macro definitions for the mapped
8704 section indices; these definitions are interpreted by the
8705 swap_out_syms function. */
8707 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8708 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8709 #define MAP_STRTAB (SHN_HIOS + 3)
8710 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8711 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8714 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8719 elf_symbol_type
*isym
, *osym
;
8721 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8722 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8725 isym
= elf_symbol_from (isymarg
);
8726 osym
= elf_symbol_from (osymarg
);
8729 && isym
->internal_elf_sym
.st_shndx
!= 0
8731 && bfd_is_abs_section (isym
->symbol
.section
))
8735 shndx
= isym
->internal_elf_sym
.st_shndx
;
8736 if (shndx
== elf_onesymtab (ibfd
))
8737 shndx
= MAP_ONESYMTAB
;
8738 else if (shndx
== elf_dynsymtab (ibfd
))
8739 shndx
= MAP_DYNSYMTAB
;
8740 else if (shndx
== elf_strtab_sec (ibfd
))
8742 else if (shndx
== elf_shstrtab_sec (ibfd
))
8743 shndx
= MAP_SHSTRTAB
;
8744 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8745 shndx
= MAP_SYM_SHNDX
;
8746 osym
->internal_elf_sym
.st_shndx
= shndx
;
8752 /* Swap out the symbols. */
8755 swap_out_syms (bfd
*abfd
,
8756 struct elf_strtab_hash
**sttp
,
8758 struct bfd_link_info
*info
)
8760 const struct elf_backend_data
*bed
;
8761 unsigned int symcount
;
8763 struct elf_strtab_hash
*stt
;
8764 Elf_Internal_Shdr
*symtab_hdr
;
8765 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8766 Elf_Internal_Shdr
*symstrtab_hdr
;
8767 struct elf_sym_strtab
*symstrtab
;
8768 bfd_byte
*outbound_syms
;
8769 bfd_byte
*outbound_shndx
;
8770 unsigned long outbound_syms_index
;
8772 unsigned int num_locals
;
8774 bool name_local_sections
;
8776 if (!elf_map_symbols (abfd
, &num_locals
))
8779 /* Dump out the symtabs. */
8780 stt
= _bfd_elf_strtab_init ();
8784 bed
= get_elf_backend_data (abfd
);
8785 symcount
= bfd_get_symcount (abfd
);
8786 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8787 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8788 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8789 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8790 symtab_hdr
->sh_info
= num_locals
+ 1;
8791 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8793 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8794 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8796 /* Allocate buffer to swap out the .strtab section. */
8797 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8798 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8800 bfd_set_error (bfd_error_no_memory
);
8801 _bfd_elf_strtab_free (stt
);
8805 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8806 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8809 bfd_set_error (bfd_error_no_memory
);
8812 _bfd_elf_strtab_free (stt
);
8815 symtab_hdr
->contents
= outbound_syms
;
8816 outbound_syms_index
= 0;
8818 outbound_shndx
= NULL
;
8820 if (elf_symtab_shndx_list (abfd
))
8822 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8823 if (symtab_shndx_hdr
->sh_name
!= 0)
8825 if (_bfd_mul_overflow (symcount
+ 1,
8826 sizeof (Elf_External_Sym_Shndx
), &amt
))
8828 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8829 if (outbound_shndx
== NULL
)
8832 symtab_shndx_hdr
->contents
= outbound_shndx
;
8833 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8834 symtab_shndx_hdr
->sh_size
= amt
;
8835 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8836 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8838 /* FIXME: What about any other headers in the list ? */
8841 /* Now generate the data (for "contents"). */
8843 /* Fill in zeroth symbol and swap it out. */
8844 Elf_Internal_Sym sym
;
8850 sym
.st_shndx
= SHN_UNDEF
;
8851 sym
.st_target_internal
= 0;
8852 symstrtab
[0].sym
= sym
;
8853 symstrtab
[0].dest_index
= outbound_syms_index
;
8854 outbound_syms_index
++;
8858 = (bed
->elf_backend_name_local_section_symbols
8859 && bed
->elf_backend_name_local_section_symbols (abfd
));
8861 syms
= bfd_get_outsymbols (abfd
);
8862 for (idx
= 0; idx
< symcount
;)
8864 Elf_Internal_Sym sym
;
8865 bfd_vma value
= syms
[idx
]->value
;
8866 elf_symbol_type
*type_ptr
;
8867 flagword flags
= syms
[idx
]->flags
;
8870 if (!name_local_sections
8871 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8873 /* Local section symbols have no name. */
8874 sym
.st_name
= (unsigned long) -1;
8878 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8879 to get the final offset for st_name. */
8881 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8883 if (sym
.st_name
== (unsigned long) -1)
8887 type_ptr
= elf_symbol_from (syms
[idx
]);
8889 if ((flags
& BSF_SECTION_SYM
) == 0
8890 && bfd_is_com_section (syms
[idx
]->section
))
8892 /* ELF common symbols put the alignment into the `value' field,
8893 and the size into the `size' field. This is backwards from
8894 how BFD handles it, so reverse it here. */
8895 sym
.st_size
= value
;
8896 if (type_ptr
== NULL
8897 || type_ptr
->internal_elf_sym
.st_value
== 0)
8898 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8900 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8901 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8902 (abfd
, syms
[idx
]->section
);
8906 asection
*sec
= syms
[idx
]->section
;
8909 if (sec
->output_section
)
8911 value
+= sec
->output_offset
;
8912 sec
= sec
->output_section
;
8915 /* Don't add in the section vma for relocatable output. */
8916 if (! relocatable_p
)
8918 sym
.st_value
= value
;
8919 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8921 if (bfd_is_abs_section (sec
)
8923 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8925 /* This symbol is in a real ELF section which we did
8926 not create as a BFD section. Undo the mapping done
8927 by copy_private_symbol_data. */
8928 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8932 shndx
= elf_onesymtab (abfd
);
8935 shndx
= elf_dynsymtab (abfd
);
8938 shndx
= elf_strtab_sec (abfd
);
8941 shndx
= elf_shstrtab_sec (abfd
);
8944 if (elf_symtab_shndx_list (abfd
))
8945 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8952 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8954 if (bed
->symbol_section_index
)
8955 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8956 /* Otherwise just leave the index alone. */
8960 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8961 _bfd_error_handler (_("%pB: \
8962 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8971 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8973 if (shndx
== SHN_BAD
)
8977 /* Writing this would be a hell of a lot easier if
8978 we had some decent documentation on bfd, and
8979 knew what to expect of the library, and what to
8980 demand of applications. For example, it
8981 appears that `objcopy' might not set the
8982 section of a symbol to be a section that is
8983 actually in the output file. */
8984 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8986 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8987 if (shndx
== SHN_BAD
)
8989 /* xgettext:c-format */
8991 (_("unable to find equivalent output section"
8992 " for symbol '%s' from section '%s'"),
8993 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8995 bfd_set_error (bfd_error_invalid_operation
);
9001 sym
.st_shndx
= shndx
;
9004 if ((flags
& BSF_THREAD_LOCAL
) != 0)
9006 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
9007 type
= STT_GNU_IFUNC
;
9008 else if ((flags
& BSF_FUNCTION
) != 0)
9010 else if ((flags
& BSF_OBJECT
) != 0)
9012 else if ((flags
& BSF_RELC
) != 0)
9014 else if ((flags
& BSF_SRELC
) != 0)
9019 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
9022 /* Processor-specific types. */
9023 if (type_ptr
!= NULL
9024 && bed
->elf_backend_get_symbol_type
)
9025 type
= ((*bed
->elf_backend_get_symbol_type
)
9026 (&type_ptr
->internal_elf_sym
, type
));
9028 if (flags
& BSF_SECTION_SYM
)
9030 if (flags
& BSF_GLOBAL
)
9031 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
9033 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9035 else if (bfd_is_com_section (syms
[idx
]->section
))
9037 if (type
!= STT_TLS
)
9039 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
9040 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
9041 ? STT_COMMON
: STT_OBJECT
);
9043 type
= ((flags
& BSF_ELF_COMMON
) != 0
9044 ? STT_COMMON
: STT_OBJECT
);
9046 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9048 else if (bfd_is_und_section (syms
[idx
]->section
))
9049 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
9053 else if (flags
& BSF_FILE
)
9054 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9057 int bind
= STB_LOCAL
;
9059 if (flags
& BSF_LOCAL
)
9061 else if (flags
& BSF_GNU_UNIQUE
)
9062 bind
= STB_GNU_UNIQUE
;
9063 else if (flags
& BSF_WEAK
)
9065 else if (flags
& BSF_GLOBAL
)
9068 sym
.st_info
= ELF_ST_INFO (bind
, type
);
9071 if (type_ptr
!= NULL
)
9073 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
9074 sym
.st_target_internal
9075 = type_ptr
->internal_elf_sym
.st_target_internal
;
9080 sym
.st_target_internal
= 0;
9084 symstrtab
[idx
].sym
= sym
;
9085 symstrtab
[idx
].dest_index
= outbound_syms_index
;
9087 outbound_syms_index
++;
9090 /* Finalize the .strtab section. */
9091 _bfd_elf_strtab_finalize (stt
);
9093 /* Swap out the .strtab section. */
9094 for (idx
= 0; idx
<= symcount
; idx
++)
9096 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
9097 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9098 elfsym
->sym
.st_name
= 0;
9100 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
9101 elfsym
->sym
.st_name
);
9102 if (info
&& info
->callbacks
->ctf_new_symbol
)
9103 info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
9106 /* Inform the linker of the addition of this symbol. */
9108 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
9110 + (elfsym
->dest_index
9111 * bed
->s
->sizeof_sym
)),
9112 NPTR_ADD (outbound_shndx
,
9114 * sizeof (Elf_External_Sym_Shndx
))));
9119 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
9120 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9121 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
9122 symstrtab_hdr
->sh_addr
= 0;
9123 symstrtab_hdr
->sh_entsize
= 0;
9124 symstrtab_hdr
->sh_link
= 0;
9125 symstrtab_hdr
->sh_info
= 0;
9126 symstrtab_hdr
->sh_addralign
= 1;
9131 /* Return the number of bytes required to hold the symtab vector.
9133 Note that we base it on the count plus 1, since we will null terminate
9134 the vector allocated based on this size. However, the ELF symbol table
9135 always has a dummy entry as symbol #0, so it ends up even. */
9138 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
9140 bfd_size_type symcount
;
9142 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9144 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
9145 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
9147 bfd_set_error (bfd_error_file_too_big
);
9150 symtab_size
= symcount
* (sizeof (asymbol
*));
9152 symtab_size
= sizeof (asymbol
*);
9153 else if (!bfd_write_p (abfd
))
9155 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9157 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
9159 bfd_set_error (bfd_error_file_truncated
);
9168 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
9170 bfd_size_type symcount
;
9172 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
9174 if (elf_dynsymtab (abfd
) == 0)
9176 /* Check if there is dynamic symbol table. */
9177 symcount
= elf_tdata (abfd
)->dt_symtab_count
;
9179 goto compute_symtab_size
;
9181 bfd_set_error (bfd_error_invalid_operation
);
9185 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
9186 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
9188 bfd_set_error (bfd_error_file_too_big
);
9192 compute_symtab_size
:
9193 symtab_size
= symcount
* (sizeof (asymbol
*));
9195 symtab_size
= sizeof (asymbol
*);
9196 else if (!bfd_write_p (abfd
))
9198 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9200 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
9202 bfd_set_error (bfd_error_file_truncated
);
9211 _bfd_elf_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
9213 if (asect
->reloc_count
!= 0 && !bfd_write_p (abfd
))
9215 /* Sanity check reloc section size. */
9216 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9220 struct bfd_elf_section_data
*d
= elf_section_data (asect
);
9221 bfd_size_type rel_size
= d
->rel
.hdr
? d
->rel
.hdr
->sh_size
: 0;
9222 bfd_size_type rela_size
= d
->rela
.hdr
? d
->rela
.hdr
->sh_size
: 0;
9224 if (rel_size
+ rela_size
> filesize
9225 || rel_size
+ rela_size
< rel_size
)
9227 bfd_set_error (bfd_error_file_truncated
);
9233 #if SIZEOF_LONG == SIZEOF_INT
9234 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
9236 bfd_set_error (bfd_error_file_too_big
);
9240 return (asect
->reloc_count
+ 1L) * sizeof (arelent
*);
9243 /* Canonicalize the relocs. */
9246 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
9253 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9255 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
9258 tblptr
= section
->relocation
;
9259 for (i
= 0; i
< section
->reloc_count
; i
++)
9260 *relptr
++ = tblptr
++;
9264 return section
->reloc_count
;
9268 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
9270 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9271 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, false);
9274 abfd
->symcount
= symcount
;
9279 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
9280 asymbol
**allocation
)
9282 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9283 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, true);
9286 abfd
->dynsymcount
= symcount
;
9290 /* Return the size required for the dynamic reloc entries. Any loadable
9291 section that was actually installed in the BFD, and has type SHT_REL
9292 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9293 dynamic reloc section. */
9296 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
9298 bfd_size_type count
, ext_rel_size
;
9301 if (elf_dynsymtab (abfd
) == 0)
9303 bfd_set_error (bfd_error_invalid_operation
);
9309 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9310 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
9311 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
9312 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
)
9313 && (elf_section_data (s
)->this_hdr
.sh_flags
& SHF_COMPRESSED
) == 0)
9315 ext_rel_size
+= elf_section_data (s
)->this_hdr
.sh_size
;
9316 if (ext_rel_size
< elf_section_data (s
)->this_hdr
.sh_size
)
9318 bfd_set_error (bfd_error_file_truncated
);
9321 count
+= NUM_SHDR_ENTRIES (&elf_section_data (s
)->this_hdr
);
9322 if (count
> LONG_MAX
/ sizeof (arelent
*))
9324 bfd_set_error (bfd_error_file_too_big
);
9328 if (count
> 1 && !bfd_write_p (abfd
))
9330 /* Sanity check reloc section sizes. */
9331 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9332 if (filesize
!= 0 && ext_rel_size
> filesize
)
9334 bfd_set_error (bfd_error_file_truncated
);
9338 return count
* sizeof (arelent
*);
9341 /* Canonicalize the dynamic relocation entries. Note that we return the
9342 dynamic relocations as a single block, although they are actually
9343 associated with particular sections; the interface, which was
9344 designed for SunOS style shared libraries, expects that there is only
9345 one set of dynamic relocs. Any loadable section that was actually
9346 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9347 dynamic symbol table, is considered to be a dynamic reloc section. */
9350 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
9354 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
9358 if (elf_dynsymtab (abfd
) == 0)
9360 bfd_set_error (bfd_error_invalid_operation
);
9364 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9366 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9368 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
9369 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
9370 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
)
9371 && (elf_section_data (s
)->this_hdr
.sh_flags
& SHF_COMPRESSED
) == 0)
9376 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
9378 count
= NUM_SHDR_ENTRIES (&elf_section_data (s
)->this_hdr
);
9380 for (i
= 0; i
< count
; i
++)
9391 /* Read in the version information. */
9394 _bfd_elf_slurp_version_tables (bfd
*abfd
, bool default_imported_symver
)
9396 bfd_byte
*contents
= NULL
;
9397 unsigned int freeidx
= 0;
9400 if (elf_dynverref (abfd
) != 0 || elf_tdata (abfd
)->dt_verneed
!= NULL
)
9402 Elf_Internal_Shdr
*hdr
;
9403 Elf_External_Verneed
*everneed
;
9404 Elf_Internal_Verneed
*iverneed
;
9406 bfd_byte
*contents_end
;
9407 size_t verneed_count
;
9408 size_t verneed_size
;
9410 if (elf_tdata (abfd
)->dt_verneed
!= NULL
)
9413 contents
= elf_tdata (abfd
)->dt_verneed
;
9414 verneed_count
= elf_tdata (abfd
)->dt_verneed_count
;
9415 verneed_size
= verneed_count
* sizeof (Elf_External_Verneed
);
9419 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
9421 if (hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
9423 error_return_bad_verref
:
9425 (_("%pB: .gnu.version_r invalid entry"), abfd
);
9426 bfd_set_error (bfd_error_bad_value
);
9427 error_return_verref
:
9428 elf_tdata (abfd
)->verref
= NULL
;
9429 elf_tdata (abfd
)->cverrefs
= 0;
9433 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9434 goto error_return_verref
;
9435 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
9436 if (contents
== NULL
)
9437 goto error_return_verref
;
9439 verneed_size
= hdr
->sh_size
;
9440 verneed_count
= hdr
->sh_info
;
9443 if (_bfd_mul_overflow (verneed_count
,
9444 sizeof (Elf_Internal_Verneed
), &amt
))
9446 bfd_set_error (bfd_error_file_too_big
);
9447 goto error_return_verref
;
9450 goto error_return_verref
;
9451 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
9452 if (elf_tdata (abfd
)->verref
== NULL
)
9453 goto error_return_verref
;
9455 BFD_ASSERT (sizeof (Elf_External_Verneed
)
9456 == sizeof (Elf_External_Vernaux
));
9457 contents_end
= (contents
+ verneed_size
9458 - sizeof (Elf_External_Verneed
));
9459 everneed
= (Elf_External_Verneed
*) contents
;
9460 iverneed
= elf_tdata (abfd
)->verref
;
9461 for (i
= 0; i
< verneed_count
; i
++, iverneed
++)
9463 Elf_External_Vernaux
*evernaux
;
9464 Elf_Internal_Vernaux
*ivernaux
;
9467 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
9469 iverneed
->vn_bfd
= abfd
;
9471 if (elf_use_dt_symtab_p (abfd
))
9472 iverneed
->vn_filename
9473 = elf_tdata (abfd
)->dt_strtab
+ iverneed
->vn_file
;
9475 iverneed
->vn_filename
9476 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9478 if (iverneed
->vn_filename
== NULL
)
9479 goto error_return_bad_verref
;
9481 if (iverneed
->vn_cnt
== 0)
9482 iverneed
->vn_auxptr
= NULL
;
9485 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
9486 sizeof (Elf_Internal_Vernaux
), &amt
))
9488 bfd_set_error (bfd_error_file_too_big
);
9489 goto error_return_verref
;
9491 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
9492 bfd_alloc (abfd
, amt
);
9493 if (iverneed
->vn_auxptr
== NULL
)
9494 goto error_return_verref
;
9497 if (iverneed
->vn_aux
9498 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9499 goto error_return_bad_verref
;
9501 evernaux
= ((Elf_External_Vernaux
*)
9502 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
9503 ivernaux
= iverneed
->vn_auxptr
;
9504 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
9506 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
9508 if (elf_use_dt_symtab_p (abfd
))
9509 ivernaux
->vna_nodename
9510 = elf_tdata (abfd
)->dt_strtab
+ ivernaux
->vna_name
;
9512 ivernaux
->vna_nodename
9513 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9514 ivernaux
->vna_name
);
9515 if (ivernaux
->vna_nodename
== NULL
)
9516 goto error_return_bad_verref
;
9518 if (ivernaux
->vna_other
> freeidx
)
9519 freeidx
= ivernaux
->vna_other
;
9521 ivernaux
->vna_nextptr
= NULL
;
9522 if (ivernaux
->vna_next
== 0)
9524 iverneed
->vn_cnt
= j
+ 1;
9527 if (j
+ 1 < iverneed
->vn_cnt
)
9528 ivernaux
->vna_nextptr
= ivernaux
+ 1;
9530 if (ivernaux
->vna_next
9531 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
9532 goto error_return_bad_verref
;
9534 evernaux
= ((Elf_External_Vernaux
*)
9535 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
9538 iverneed
->vn_nextref
= NULL
;
9539 if (iverneed
->vn_next
== 0)
9541 if (i
+ 1 < hdr
->sh_info
)
9542 iverneed
->vn_nextref
= iverneed
+ 1;
9544 if (iverneed
->vn_next
9545 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9546 goto error_return_bad_verref
;
9548 everneed
= ((Elf_External_Verneed
*)
9549 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
9551 elf_tdata (abfd
)->cverrefs
= i
;
9553 if (elf_tdata (abfd
)->dt_verneed
== NULL
)
9558 if (elf_dynverdef (abfd
) != 0 || elf_tdata (abfd
)->dt_verdef
!= NULL
)
9560 Elf_Internal_Shdr
*hdr
;
9561 Elf_External_Verdef
*everdef
;
9562 Elf_Internal_Verdef
*iverdef
;
9563 Elf_Internal_Verdef
*iverdefarr
;
9564 Elf_Internal_Verdef iverdefmem
;
9566 unsigned int maxidx
;
9567 bfd_byte
*contents_end_def
, *contents_end_aux
;
9568 size_t verdef_count
;
9571 if (elf_tdata (abfd
)->dt_verdef
!= NULL
)
9574 contents
= elf_tdata (abfd
)->dt_verdef
;
9575 verdef_count
= elf_tdata (abfd
)->dt_verdef_count
;
9576 verdef_size
= verdef_count
* sizeof (Elf_External_Verdef
);
9580 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
9582 if (hdr
->sh_size
< sizeof (Elf_External_Verdef
))
9584 error_return_bad_verdef
:
9586 (_("%pB: .gnu.version_d invalid entry"), abfd
);
9587 bfd_set_error (bfd_error_bad_value
);
9588 error_return_verdef
:
9589 elf_tdata (abfd
)->verdef
= NULL
;
9590 elf_tdata (abfd
)->cverdefs
= 0;
9594 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9595 goto error_return_verdef
;
9596 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
9597 if (contents
== NULL
)
9598 goto error_return_verdef
;
9600 BFD_ASSERT (sizeof (Elf_External_Verdef
)
9601 >= sizeof (Elf_External_Verdaux
));
9603 verdef_count
= hdr
->sh_info
;
9604 verdef_size
= hdr
->sh_size
;
9607 contents_end_def
= (contents
+ verdef_size
9608 - sizeof (Elf_External_Verdef
));
9609 contents_end_aux
= (contents
+ verdef_size
9610 - sizeof (Elf_External_Verdaux
));
9612 /* We know the number of entries in the section but not the maximum
9613 index. Therefore we have to run through all entries and find
9615 everdef
= (Elf_External_Verdef
*) contents
;
9617 for (i
= 0; i
< verdef_count
; ++i
)
9619 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9621 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
9622 goto error_return_bad_verdef
;
9623 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
9624 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
9626 if (iverdefmem
.vd_next
== 0)
9629 if (iverdefmem
.vd_next
9630 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
9631 goto error_return_bad_verdef
;
9633 everdef
= ((Elf_External_Verdef
*)
9634 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
9637 if (default_imported_symver
)
9639 if (freeidx
> maxidx
)
9644 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9646 bfd_set_error (bfd_error_file_too_big
);
9647 goto error_return_verdef
;
9651 goto error_return_verdef
;
9652 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9653 if (elf_tdata (abfd
)->verdef
== NULL
)
9654 goto error_return_verdef
;
9656 elf_tdata (abfd
)->cverdefs
= maxidx
;
9658 everdef
= (Elf_External_Verdef
*) contents
;
9659 iverdefarr
= elf_tdata (abfd
)->verdef
;
9660 for (i
= 0; i
< verdef_count
; ++i
)
9662 Elf_External_Verdaux
*everdaux
;
9663 Elf_Internal_Verdaux
*iverdaux
;
9666 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9668 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
9669 goto error_return_bad_verdef
;
9671 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
9672 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
9674 iverdef
->vd_bfd
= abfd
;
9676 if (iverdef
->vd_cnt
== 0)
9677 iverdef
->vd_auxptr
= NULL
;
9680 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
9681 sizeof (Elf_Internal_Verdaux
), &amt
))
9683 bfd_set_error (bfd_error_file_too_big
);
9684 goto error_return_verdef
;
9686 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
9687 bfd_alloc (abfd
, amt
);
9688 if (iverdef
->vd_auxptr
== NULL
)
9689 goto error_return_verdef
;
9693 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
9694 goto error_return_bad_verdef
;
9696 everdaux
= ((Elf_External_Verdaux
*)
9697 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
9698 iverdaux
= iverdef
->vd_auxptr
;
9699 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
9701 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
9703 if (elf_use_dt_symtab_p (abfd
))
9704 iverdaux
->vda_nodename
9705 = elf_tdata (abfd
)->dt_strtab
+ iverdaux
->vda_name
;
9707 iverdaux
->vda_nodename
9708 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9709 iverdaux
->vda_name
);
9710 if (iverdaux
->vda_nodename
== NULL
)
9711 goto error_return_bad_verdef
;
9713 iverdaux
->vda_nextptr
= NULL
;
9714 if (iverdaux
->vda_next
== 0)
9716 iverdef
->vd_cnt
= j
+ 1;
9719 if (j
+ 1 < iverdef
->vd_cnt
)
9720 iverdaux
->vda_nextptr
= iverdaux
+ 1;
9722 if (iverdaux
->vda_next
9723 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
9724 goto error_return_bad_verdef
;
9726 everdaux
= ((Elf_External_Verdaux
*)
9727 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
9730 iverdef
->vd_nodename
= NULL
;
9731 if (iverdef
->vd_cnt
)
9732 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
9734 iverdef
->vd_nextdef
= NULL
;
9735 if (iverdef
->vd_next
== 0)
9737 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
9738 iverdef
->vd_nextdef
= iverdef
+ 1;
9740 everdef
= ((Elf_External_Verdef
*)
9741 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
9744 if (elf_tdata (abfd
)->dt_verdef
== NULL
)
9748 else if (default_imported_symver
)
9755 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9757 bfd_set_error (bfd_error_file_too_big
);
9762 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9763 if (elf_tdata (abfd
)->verdef
== NULL
)
9766 elf_tdata (abfd
)->cverdefs
= freeidx
;
9769 /* Create a default version based on the soname. */
9770 if (default_imported_symver
)
9772 Elf_Internal_Verdef
*iverdef
;
9773 Elf_Internal_Verdaux
*iverdaux
;
9775 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
9777 iverdef
->vd_version
= VER_DEF_CURRENT
;
9778 iverdef
->vd_flags
= 0;
9779 iverdef
->vd_ndx
= freeidx
;
9780 iverdef
->vd_cnt
= 1;
9782 iverdef
->vd_bfd
= abfd
;
9784 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
9785 if (iverdef
->vd_nodename
== NULL
)
9786 goto error_return_verdef
;
9787 iverdef
->vd_nextdef
= NULL
;
9788 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
9789 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
9790 if (iverdef
->vd_auxptr
== NULL
)
9791 goto error_return_verdef
;
9793 iverdaux
= iverdef
->vd_auxptr
;
9794 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9805 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9807 elf_symbol_type
*newsym
;
9809 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9812 newsym
->symbol
.the_bfd
= abfd
;
9813 return &newsym
->symbol
;
9817 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9821 bfd_symbol_info (symbol
, ret
);
9824 /* Return whether a symbol name implies a local symbol. Most targets
9825 use this function for the is_local_label_name entry point, but some
9829 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9832 /* Normal local symbols start with ``.L''. */
9833 if (name
[0] == '.' && name
[1] == 'L')
9836 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9837 DWARF debugging symbols starting with ``..''. */
9838 if (name
[0] == '.' && name
[1] == '.')
9841 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9842 emitting DWARF debugging output. I suspect this is actually a
9843 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9844 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9845 underscore to be emitted on some ELF targets). For ease of use,
9846 we treat such symbols as local. */
9847 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9850 /* Treat assembler generated fake symbols, dollar local labels and
9851 forward-backward labels (aka local labels) as locals.
9852 These labels have the form:
9854 L0^A.* (fake symbols)
9856 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9858 Versions which start with .L will have already been matched above,
9859 so we only need to match the rest. */
9860 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9866 for (p
= name
+ 2; (c
= *p
); p
++)
9868 if (c
== 1 || c
== 2)
9870 if (c
== 1 && p
== name
+ 2)
9871 /* A fake symbol. */
9874 /* FIXME: We are being paranoid here and treating symbols like
9875 L0^Bfoo as if there were non-local, on the grounds that the
9876 assembler will never generate them. But can any symbol
9877 containing an ASCII value in the range 1-31 ever be anything
9878 other than some kind of local ? */
9895 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9896 asymbol
*symbol ATTRIBUTE_UNUSED
)
9903 _bfd_elf_set_arch_mach (bfd
*abfd
,
9904 enum bfd_architecture arch
,
9905 unsigned long machine
)
9907 /* If this isn't the right architecture for this backend, and this
9908 isn't the generic backend, fail. */
9909 if (arch
!= get_elf_backend_data (abfd
)->arch
9910 && arch
!= bfd_arch_unknown
9911 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9914 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9917 /* Find the nearest line to a particular section and offset,
9918 for error reporting. */
9921 _bfd_elf_find_nearest_line (bfd
*abfd
,
9925 const char **filename_ptr
,
9926 const char **functionname_ptr
,
9927 unsigned int *line_ptr
,
9928 unsigned int *discriminator_ptr
)
9930 return _bfd_elf_find_nearest_line_with_alt (abfd
, NULL
, symbols
, section
,
9931 offset
, filename_ptr
,
9932 functionname_ptr
, line_ptr
,
9936 /* Find the nearest line to a particular section and offset,
9937 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9938 can be optionally specified. */
9941 _bfd_elf_find_nearest_line_with_alt (bfd
*abfd
,
9942 const char *alt_filename
,
9946 const char **filename_ptr
,
9947 const char **functionname_ptr
,
9948 unsigned int *line_ptr
,
9949 unsigned int *discriminator_ptr
)
9953 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd
, alt_filename
, symbols
, NULL
,
9954 section
, offset
, filename_ptr
,
9955 functionname_ptr
, line_ptr
,
9957 dwarf_debug_sections
,
9958 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9961 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9962 filename_ptr
, functionname_ptr
, line_ptr
))
9964 if (!*functionname_ptr
)
9965 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9966 *filename_ptr
? NULL
: filename_ptr
,
9971 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9972 &found
, filename_ptr
,
9973 functionname_ptr
, line_ptr
,
9974 &elf_tdata (abfd
)->line_info
))
9976 if (found
&& (*functionname_ptr
|| *line_ptr
))
9979 if (symbols
== NULL
)
9982 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9983 filename_ptr
, functionname_ptr
))
9990 /* Find the line for a symbol. */
9993 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9994 const char **filename_ptr
, unsigned int *line_ptr
)
9996 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9997 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9998 filename_ptr
, NULL
, line_ptr
, NULL
,
9999 dwarf_debug_sections
,
10000 &tdata
->dwarf2_find_line_info
);
10003 /* After a call to bfd_find_nearest_line, successive calls to
10004 bfd_find_inliner_info can be used to get source information about
10005 each level of function inlining that terminated at the address
10006 passed to bfd_find_nearest_line. Currently this is only supported
10007 for DWARF2 with appropriate DWARF3 extensions. */
10010 _bfd_elf_find_inliner_info (bfd
*abfd
,
10011 const char **filename_ptr
,
10012 const char **functionname_ptr
,
10013 unsigned int *line_ptr
)
10015 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
10016 return _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
10017 functionname_ptr
, line_ptr
,
10018 &tdata
->dwarf2_find_line_info
);
10022 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
10024 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10025 int ret
= bed
->s
->sizeof_ehdr
;
10027 if (!bfd_link_relocatable (info
))
10029 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
10031 if (phdr_size
== (bfd_size_type
) -1)
10033 struct elf_segment_map
*m
;
10036 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
10037 phdr_size
+= bed
->s
->sizeof_phdr
;
10039 if (phdr_size
== 0)
10040 phdr_size
= get_program_header_size (abfd
, info
);
10043 elf_program_header_size (abfd
) = phdr_size
;
10051 _bfd_elf_set_section_contents (bfd
*abfd
,
10053 const void *location
,
10055 bfd_size_type count
)
10057 Elf_Internal_Shdr
*hdr
;
10059 if (! abfd
->output_has_begun
10060 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
10066 hdr
= &elf_section_data (section
)->this_hdr
;
10067 if (hdr
->sh_offset
== (file_ptr
) -1)
10069 unsigned char *contents
;
10071 if (bfd_section_is_ctf (section
))
10072 /* Nothing to do with this section: the contents are generated
10076 if ((offset
+ count
) > hdr
->sh_size
)
10079 (_("%pB:%pA: error: attempting to write"
10080 " over the end of the section"),
10083 bfd_set_error (bfd_error_invalid_operation
);
10087 contents
= hdr
->contents
;
10088 if (contents
== NULL
)
10091 (_("%pB:%pA: error: attempting to write"
10092 " section into an empty buffer"),
10095 bfd_set_error (bfd_error_invalid_operation
);
10099 memcpy (contents
+ offset
, location
, count
);
10103 return _bfd_generic_set_section_contents (abfd
, section
,
10104 location
, offset
, count
);
10108 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
10109 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
10110 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
10116 /* Try to convert a non-ELF reloc into an ELF one. */
10119 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
10121 /* Check whether we really have an ELF howto. */
10123 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
10125 bfd_reloc_code_real_type code
;
10126 reloc_howto_type
*howto
;
10128 /* Alien reloc: Try to determine its type to replace it with an
10129 equivalent ELF reloc. */
10131 if (areloc
->howto
->pc_relative
)
10133 switch (areloc
->howto
->bitsize
)
10136 code
= BFD_RELOC_8_PCREL
;
10139 code
= BFD_RELOC_12_PCREL
;
10142 code
= BFD_RELOC_16_PCREL
;
10145 code
= BFD_RELOC_24_PCREL
;
10148 code
= BFD_RELOC_32_PCREL
;
10151 code
= BFD_RELOC_64_PCREL
;
10157 howto
= bfd_reloc_type_lookup (abfd
, code
);
10159 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
10161 if (howto
->pcrel_offset
)
10162 areloc
->addend
+= areloc
->address
;
10164 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
10169 switch (areloc
->howto
->bitsize
)
10172 code
= BFD_RELOC_8
;
10175 code
= BFD_RELOC_14
;
10178 code
= BFD_RELOC_16
;
10181 code
= BFD_RELOC_26
;
10184 code
= BFD_RELOC_32
;
10187 code
= BFD_RELOC_64
;
10193 howto
= bfd_reloc_type_lookup (abfd
, code
);
10197 areloc
->howto
= howto
;
10205 /* xgettext:c-format */
10206 _bfd_error_handler (_("%pB: %s unsupported"),
10207 abfd
, areloc
->howto
->name
);
10208 bfd_set_error (bfd_error_sorry
);
10213 _bfd_elf_free_cached_info (bfd
*abfd
)
10215 struct elf_obj_tdata
*tdata
;
10217 if ((bfd_get_format (abfd
) == bfd_object
10218 || bfd_get_format (abfd
) == bfd_core
)
10219 && (tdata
= elf_tdata (abfd
)) != NULL
)
10221 if (tdata
->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
10222 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
10223 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
10224 _bfd_dwarf1_cleanup_debug_info (abfd
, &tdata
->dwarf1_find_line_info
);
10225 _bfd_stab_cleanup (abfd
, &tdata
->line_info
);
10228 return _bfd_generic_bfd_free_cached_info (abfd
);
10231 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10232 in the relocation's offset. Thus we cannot allow any sort of sanity
10233 range-checking to interfere. There is nothing else to do in processing
10236 bfd_reloc_status_type
10237 _bfd_elf_rel_vtable_reloc_fn
10238 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
10239 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
10240 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
10241 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
10243 return bfd_reloc_ok
;
10246 /* Elf core file support. Much of this only works on native
10247 toolchains, since we rely on knowing the
10248 machine-dependent procfs structure in order to pick
10249 out details about the corefile. */
10251 #ifdef HAVE_SYS_PROCFS_H
10252 # include <sys/procfs.h>
10255 /* Return a PID that identifies a "thread" for threaded cores, or the
10256 PID of the main process for non-threaded cores. */
10259 elfcore_make_pid (bfd
*abfd
)
10263 pid
= elf_tdata (abfd
)->core
->lwpid
;
10265 pid
= elf_tdata (abfd
)->core
->pid
;
10270 /* If there isn't a section called NAME, make one, using data from
10271 SECT. Note, this function will generate a reference to NAME, so
10272 you shouldn't deallocate or overwrite it. */
10275 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
10279 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
10282 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
10286 sect2
->size
= sect
->size
;
10287 sect2
->filepos
= sect
->filepos
;
10288 sect2
->alignment_power
= sect
->alignment_power
;
10292 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10293 actually creates up to two pseudosections:
10294 - For the single-threaded case, a section named NAME, unless
10295 such a section already exists.
10296 - For the multi-threaded case, a section named "NAME/PID", where
10297 PID is elfcore_make_pid (abfd).
10298 Both pseudosections have identical contents. */
10300 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
10306 char *threaded_name
;
10310 /* Build the section name. */
10312 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
10313 len
= strlen (buf
) + 1;
10314 threaded_name
= (char *) bfd_alloc (abfd
, len
);
10315 if (threaded_name
== NULL
)
10317 memcpy (threaded_name
, buf
, len
);
10319 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
10324 sect
->filepos
= filepos
;
10325 sect
->alignment_power
= 2;
10327 return elfcore_maybe_make_sect (abfd
, name
, sect
);
10331 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
10334 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10340 sect
->size
= note
->descsz
- offs
;
10341 sect
->filepos
= note
->descpos
+ offs
;
10342 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10347 /* prstatus_t exists on:
10349 linux 2.[01] + glibc
10353 #if defined (HAVE_PRSTATUS_T)
10356 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10361 if (note
->descsz
== sizeof (prstatus_t
))
10365 size
= sizeof (prstat
.pr_reg
);
10366 offset
= offsetof (prstatus_t
, pr_reg
);
10367 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
10369 /* Do not overwrite the core signal if it
10370 has already been set by another thread. */
10371 if (elf_tdata (abfd
)->core
->signal
== 0)
10372 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
10373 if (elf_tdata (abfd
)->core
->pid
== 0)
10374 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
10376 /* pr_who exists on:
10379 pr_who doesn't exist on:
10382 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10383 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
10385 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
10388 #if defined (HAVE_PRSTATUS32_T)
10389 else if (note
->descsz
== sizeof (prstatus32_t
))
10391 /* 64-bit host, 32-bit corefile */
10392 prstatus32_t prstat
;
10394 size
= sizeof (prstat
.pr_reg
);
10395 offset
= offsetof (prstatus32_t
, pr_reg
);
10396 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
10398 /* Do not overwrite the core signal if it
10399 has already been set by another thread. */
10400 if (elf_tdata (abfd
)->core
->signal
== 0)
10401 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
10402 if (elf_tdata (abfd
)->core
->pid
== 0)
10403 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
10405 /* pr_who exists on:
10408 pr_who doesn't exist on:
10411 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10412 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
10414 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
10417 #endif /* HAVE_PRSTATUS32_T */
10420 /* Fail - we don't know how to handle any other
10421 note size (ie. data object type). */
10425 /* Make a ".reg/999" section and a ".reg" section. */
10426 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10427 size
, note
->descpos
+ offset
);
10429 #endif /* defined (HAVE_PRSTATUS_T) */
10431 /* Create a pseudosection containing the exact contents of NOTE. */
10433 elfcore_make_note_pseudosection (bfd
*abfd
,
10435 Elf_Internal_Note
*note
)
10437 return _bfd_elfcore_make_pseudosection (abfd
, name
,
10438 note
->descsz
, note
->descpos
);
10441 /* There isn't a consistent prfpregset_t across platforms,
10442 but it doesn't matter, because we don't have to pick this
10443 data structure apart. */
10446 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10448 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10451 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10452 type of NT_PRXFPREG. Just include the whole note's contents
10456 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10458 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10461 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10462 with a note type of NT_X86_XSTATE. Just include the whole note's
10463 contents literally. */
10466 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
10468 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
10472 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10474 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
10478 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10480 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
10484 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
10486 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
10490 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10492 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
10496 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10498 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
10502 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
10504 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
10508 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
10510 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
10514 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10516 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
10520 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10522 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
10526 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10528 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
10532 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10534 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
10538 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
10540 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
10544 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
10546 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
10550 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10552 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
10556 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10558 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
10562 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
10564 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
10568 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
10570 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
10574 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
10576 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
10580 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10582 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
10586 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
10588 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
10592 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
10594 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
10598 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10600 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
10604 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
10606 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
10610 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
10612 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
10616 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
10618 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
10622 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
10624 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
10628 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
10630 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
10634 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
10636 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
10640 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
10642 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
10646 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
10648 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
10652 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10654 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
10658 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
10660 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
10664 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
10666 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
10670 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
10672 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
10676 elfcore_grok_aarch_mte (bfd
*abfd
, Elf_Internal_Note
*note
)
10678 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-mte",
10683 elfcore_grok_aarch_ssve (bfd
*abfd
, Elf_Internal_Note
*note
)
10685 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-ssve", note
);
10689 elfcore_grok_aarch_za (bfd
*abfd
, Elf_Internal_Note
*note
)
10691 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-za", note
);
10694 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10695 successful, otherwise return FALSE. */
10698 elfcore_grok_aarch_zt (bfd
*abfd
, Elf_Internal_Note
*note
)
10700 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-zt", note
);
10704 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
10706 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
10709 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10710 successful otherwise, return FALSE. */
10713 elfcore_grok_riscv_csr (bfd
*abfd
, Elf_Internal_Note
*note
)
10715 return elfcore_make_note_pseudosection (abfd
, ".reg-riscv-csr", note
);
10718 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10719 successful otherwise, return FALSE. */
10722 elfcore_grok_gdb_tdesc (bfd
*abfd
, Elf_Internal_Note
*note
)
10724 return elfcore_make_note_pseudosection (abfd
, ".gdb-tdesc", note
);
10728 elfcore_grok_loongarch_cpucfg (bfd
*abfd
, Elf_Internal_Note
*note
)
10730 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-cpucfg", note
);
10734 elfcore_grok_loongarch_lbt (bfd
*abfd
, Elf_Internal_Note
*note
)
10736 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lbt", note
);
10740 elfcore_grok_loongarch_lsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10742 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lsx", note
);
10746 elfcore_grok_loongarch_lasx (bfd
*abfd
, Elf_Internal_Note
*note
)
10748 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lasx", note
);
10751 #if defined (HAVE_PRPSINFO_T)
10752 typedef prpsinfo_t elfcore_psinfo_t
;
10753 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10754 typedef prpsinfo32_t elfcore_psinfo32_t
;
10758 #if defined (HAVE_PSINFO_T)
10759 typedef psinfo_t elfcore_psinfo_t
;
10760 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10761 typedef psinfo32_t elfcore_psinfo32_t
;
10765 /* return a malloc'ed copy of a string at START which is at
10766 most MAX bytes long, possibly without a terminating '\0'.
10767 the copy will always have a terminating '\0'. */
10770 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
10773 char *end
= (char *) memchr (start
, '\0', max
);
10781 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
10785 memcpy (dups
, start
, len
);
10791 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10793 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10795 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
10797 elfcore_psinfo_t psinfo
;
10799 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10801 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10802 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10804 elf_tdata (abfd
)->core
->program
10805 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10806 sizeof (psinfo
.pr_fname
));
10808 elf_tdata (abfd
)->core
->command
10809 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10810 sizeof (psinfo
.pr_psargs
));
10812 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10813 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
10815 /* 64-bit host, 32-bit corefile */
10816 elfcore_psinfo32_t psinfo
;
10818 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10820 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10821 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10823 elf_tdata (abfd
)->core
->program
10824 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10825 sizeof (psinfo
.pr_fname
));
10827 elf_tdata (abfd
)->core
->command
10828 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10829 sizeof (psinfo
.pr_psargs
));
10835 /* Fail - we don't know how to handle any other
10836 note size (ie. data object type). */
10840 /* Note that for some reason, a spurious space is tacked
10841 onto the end of the args in some (at least one anyway)
10842 implementations, so strip it off if it exists. */
10845 char *command
= elf_tdata (abfd
)->core
->command
;
10846 int n
= strlen (command
);
10848 if (0 < n
&& command
[n
- 1] == ' ')
10849 command
[n
- 1] = '\0';
10854 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10856 #if defined (HAVE_PSTATUS_T)
10858 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10860 if (note
->descsz
== sizeof (pstatus_t
)
10861 #if defined (HAVE_PXSTATUS_T)
10862 || note
->descsz
== sizeof (pxstatus_t
)
10868 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10870 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10872 #if defined (HAVE_PSTATUS32_T)
10873 else if (note
->descsz
== sizeof (pstatus32_t
))
10875 /* 64-bit host, 32-bit corefile */
10878 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10880 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10883 /* Could grab some more details from the "representative"
10884 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10885 NT_LWPSTATUS note, presumably. */
10889 #endif /* defined (HAVE_PSTATUS_T) */
10891 #if defined (HAVE_LWPSTATUS_T)
10893 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10895 lwpstatus_t lwpstat
;
10901 if (note
->descsz
!= sizeof (lwpstat
)
10902 #if defined (HAVE_LWPXSTATUS_T)
10903 && note
->descsz
!= sizeof (lwpxstatus_t
)
10908 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10910 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10911 /* Do not overwrite the core signal if it has already been set by
10913 if (elf_tdata (abfd
)->core
->signal
== 0)
10914 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10916 /* Make a ".reg/999" section. */
10918 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10919 len
= strlen (buf
) + 1;
10920 name
= bfd_alloc (abfd
, len
);
10923 memcpy (name
, buf
, len
);
10925 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10929 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10930 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10931 sect
->filepos
= note
->descpos
10932 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10935 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10936 sect
->size
= sizeof (lwpstat
.pr_reg
);
10937 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10940 sect
->alignment_power
= 2;
10942 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10945 /* Make a ".reg2/999" section */
10947 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10948 len
= strlen (buf
) + 1;
10949 name
= bfd_alloc (abfd
, len
);
10952 memcpy (name
, buf
, len
);
10954 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10958 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10959 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10960 sect
->filepos
= note
->descpos
10961 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10964 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10965 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10966 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10969 sect
->alignment_power
= 2;
10971 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10973 #endif /* defined (HAVE_LWPSTATUS_T) */
10975 /* These constants, and the structure offsets used below, are defined by
10976 Cygwin's core_dump.h */
10977 #define NOTE_INFO_PROCESS 1
10978 #define NOTE_INFO_THREAD 2
10979 #define NOTE_INFO_MODULE 3
10980 #define NOTE_INFO_MODULE64 4
10983 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10988 unsigned int name_size
;
10991 int is_active_thread
;
10994 if (note
->descsz
< 4)
10997 if (! startswith (note
->namedata
, "win32"))
11000 type
= bfd_get_32 (abfd
, note
->descdata
);
11004 const char *type_name
;
11005 unsigned long min_size
;
11008 { "NOTE_INFO_PROCESS", 12 },
11009 { "NOTE_INFO_THREAD", 12 },
11010 { "NOTE_INFO_MODULE", 12 },
11011 { "NOTE_INFO_MODULE64", 16 },
11014 if (type
== 0 || type
> (sizeof(size_check
)/sizeof(size_check
[0])))
11017 if (note
->descsz
< size_check
[type
- 1].min_size
)
11019 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11021 abfd
, size_check
[type
- 1].type_name
, note
->descsz
);
11027 case NOTE_INFO_PROCESS
:
11028 /* FIXME: need to add ->core->command. */
11029 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 4);
11030 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11033 case NOTE_INFO_THREAD
:
11034 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11036 /* thread_info.tid */
11037 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 4));
11039 len
= strlen (buf
) + 1;
11040 name
= (char *) bfd_alloc (abfd
, len
);
11044 memcpy (name
, buf
, len
);
11046 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11050 /* sizeof (thread_info.thread_context) */
11051 sect
->size
= note
->descsz
- 12;
11052 /* offsetof (thread_info.thread_context) */
11053 sect
->filepos
= note
->descpos
+ 12;
11054 sect
->alignment_power
= 2;
11056 /* thread_info.is_active_thread */
11057 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11059 if (is_active_thread
)
11060 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
11064 case NOTE_INFO_MODULE
:
11065 case NOTE_INFO_MODULE64
:
11066 /* Make a ".module/xxxxxxxx" section. */
11067 if (type
== NOTE_INFO_MODULE
)
11069 /* module_info.base_address */
11070 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
11071 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
11072 /* module_info.module_name_size */
11073 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11075 else /* NOTE_INFO_MODULE64 */
11077 /* module_info.base_address */
11078 base_addr
= bfd_get_64 (abfd
, note
->descdata
+ 4);
11079 sprintf (buf
, ".module/%016lx", (unsigned long) base_addr
);
11080 /* module_info.module_name_size */
11081 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 12);
11084 len
= strlen (buf
) + 1;
11085 name
= (char *) bfd_alloc (abfd
, len
);
11089 memcpy (name
, buf
, len
);
11091 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11096 if (note
->descsz
< 12 + name_size
)
11098 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11099 " is too small to contain a name of size %u"),
11100 abfd
, note
->descsz
, name_size
);
11104 sect
->size
= note
->descsz
;
11105 sect
->filepos
= note
->descpos
;
11106 sect
->alignment_power
= 2;
11117 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11119 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11121 switch (note
->type
)
11127 if (bed
->elf_backend_grok_prstatus
)
11128 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
11130 #if defined (HAVE_PRSTATUS_T)
11131 return elfcore_grok_prstatus (abfd
, note
);
11136 #if defined (HAVE_PSTATUS_T)
11138 return elfcore_grok_pstatus (abfd
, note
);
11141 #if defined (HAVE_LWPSTATUS_T)
11143 return elfcore_grok_lwpstatus (abfd
, note
);
11146 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
11147 return elfcore_grok_prfpreg (abfd
, note
);
11149 case NT_WIN32PSTATUS
:
11150 return elfcore_grok_win32pstatus (abfd
, note
);
11152 case NT_PRXFPREG
: /* Linux SSE extension */
11153 if (note
->namesz
== 6
11154 && strcmp (note
->namedata
, "LINUX") == 0)
11155 return elfcore_grok_prxfpreg (abfd
, note
);
11159 case NT_X86_XSTATE
: /* Linux XSAVE extension */
11160 if (note
->namesz
== 6
11161 && strcmp (note
->namedata
, "LINUX") == 0)
11162 return elfcore_grok_xstatereg (abfd
, note
);
11167 if (note
->namesz
== 6
11168 && strcmp (note
->namedata
, "LINUX") == 0)
11169 return elfcore_grok_ppc_vmx (abfd
, note
);
11174 if (note
->namesz
== 6
11175 && strcmp (note
->namedata
, "LINUX") == 0)
11176 return elfcore_grok_ppc_vsx (abfd
, note
);
11181 if (note
->namesz
== 6
11182 && strcmp (note
->namedata
, "LINUX") == 0)
11183 return elfcore_grok_ppc_tar (abfd
, note
);
11188 if (note
->namesz
== 6
11189 && strcmp (note
->namedata
, "LINUX") == 0)
11190 return elfcore_grok_ppc_ppr (abfd
, note
);
11195 if (note
->namesz
== 6
11196 && strcmp (note
->namedata
, "LINUX") == 0)
11197 return elfcore_grok_ppc_dscr (abfd
, note
);
11202 if (note
->namesz
== 6
11203 && strcmp (note
->namedata
, "LINUX") == 0)
11204 return elfcore_grok_ppc_ebb (abfd
, note
);
11209 if (note
->namesz
== 6
11210 && strcmp (note
->namedata
, "LINUX") == 0)
11211 return elfcore_grok_ppc_pmu (abfd
, note
);
11215 case NT_PPC_TM_CGPR
:
11216 if (note
->namesz
== 6
11217 && strcmp (note
->namedata
, "LINUX") == 0)
11218 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
11222 case NT_PPC_TM_CFPR
:
11223 if (note
->namesz
== 6
11224 && strcmp (note
->namedata
, "LINUX") == 0)
11225 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
11229 case NT_PPC_TM_CVMX
:
11230 if (note
->namesz
== 6
11231 && strcmp (note
->namedata
, "LINUX") == 0)
11232 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
11236 case NT_PPC_TM_CVSX
:
11237 if (note
->namesz
== 6
11238 && strcmp (note
->namedata
, "LINUX") == 0)
11239 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
11243 case NT_PPC_TM_SPR
:
11244 if (note
->namesz
== 6
11245 && strcmp (note
->namedata
, "LINUX") == 0)
11246 return elfcore_grok_ppc_tm_spr (abfd
, note
);
11250 case NT_PPC_TM_CTAR
:
11251 if (note
->namesz
== 6
11252 && strcmp (note
->namedata
, "LINUX") == 0)
11253 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
11257 case NT_PPC_TM_CPPR
:
11258 if (note
->namesz
== 6
11259 && strcmp (note
->namedata
, "LINUX") == 0)
11260 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
11264 case NT_PPC_TM_CDSCR
:
11265 if (note
->namesz
== 6
11266 && strcmp (note
->namedata
, "LINUX") == 0)
11267 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
11271 case NT_S390_HIGH_GPRS
:
11272 if (note
->namesz
== 6
11273 && strcmp (note
->namedata
, "LINUX") == 0)
11274 return elfcore_grok_s390_high_gprs (abfd
, note
);
11278 case NT_S390_TIMER
:
11279 if (note
->namesz
== 6
11280 && strcmp (note
->namedata
, "LINUX") == 0)
11281 return elfcore_grok_s390_timer (abfd
, note
);
11285 case NT_S390_TODCMP
:
11286 if (note
->namesz
== 6
11287 && strcmp (note
->namedata
, "LINUX") == 0)
11288 return elfcore_grok_s390_todcmp (abfd
, note
);
11292 case NT_S390_TODPREG
:
11293 if (note
->namesz
== 6
11294 && strcmp (note
->namedata
, "LINUX") == 0)
11295 return elfcore_grok_s390_todpreg (abfd
, note
);
11300 if (note
->namesz
== 6
11301 && strcmp (note
->namedata
, "LINUX") == 0)
11302 return elfcore_grok_s390_ctrs (abfd
, note
);
11306 case NT_S390_PREFIX
:
11307 if (note
->namesz
== 6
11308 && strcmp (note
->namedata
, "LINUX") == 0)
11309 return elfcore_grok_s390_prefix (abfd
, note
);
11313 case NT_S390_LAST_BREAK
:
11314 if (note
->namesz
== 6
11315 && strcmp (note
->namedata
, "LINUX") == 0)
11316 return elfcore_grok_s390_last_break (abfd
, note
);
11320 case NT_S390_SYSTEM_CALL
:
11321 if (note
->namesz
== 6
11322 && strcmp (note
->namedata
, "LINUX") == 0)
11323 return elfcore_grok_s390_system_call (abfd
, note
);
11328 if (note
->namesz
== 6
11329 && strcmp (note
->namedata
, "LINUX") == 0)
11330 return elfcore_grok_s390_tdb (abfd
, note
);
11334 case NT_S390_VXRS_LOW
:
11335 if (note
->namesz
== 6
11336 && strcmp (note
->namedata
, "LINUX") == 0)
11337 return elfcore_grok_s390_vxrs_low (abfd
, note
);
11341 case NT_S390_VXRS_HIGH
:
11342 if (note
->namesz
== 6
11343 && strcmp (note
->namedata
, "LINUX") == 0)
11344 return elfcore_grok_s390_vxrs_high (abfd
, note
);
11348 case NT_S390_GS_CB
:
11349 if (note
->namesz
== 6
11350 && strcmp (note
->namedata
, "LINUX") == 0)
11351 return elfcore_grok_s390_gs_cb (abfd
, note
);
11355 case NT_S390_GS_BC
:
11356 if (note
->namesz
== 6
11357 && strcmp (note
->namedata
, "LINUX") == 0)
11358 return elfcore_grok_s390_gs_bc (abfd
, note
);
11363 if (note
->namesz
== 6
11364 && strcmp (note
->namedata
, "LINUX") == 0)
11365 return elfcore_grok_arc_v2 (abfd
, note
);
11370 if (note
->namesz
== 6
11371 && strcmp (note
->namedata
, "LINUX") == 0)
11372 return elfcore_grok_arm_vfp (abfd
, note
);
11377 if (note
->namesz
== 6
11378 && strcmp (note
->namedata
, "LINUX") == 0)
11379 return elfcore_grok_aarch_tls (abfd
, note
);
11383 case NT_ARM_HW_BREAK
:
11384 if (note
->namesz
== 6
11385 && strcmp (note
->namedata
, "LINUX") == 0)
11386 return elfcore_grok_aarch_hw_break (abfd
, note
);
11390 case NT_ARM_HW_WATCH
:
11391 if (note
->namesz
== 6
11392 && strcmp (note
->namedata
, "LINUX") == 0)
11393 return elfcore_grok_aarch_hw_watch (abfd
, note
);
11398 if (note
->namesz
== 6
11399 && strcmp (note
->namedata
, "LINUX") == 0)
11400 return elfcore_grok_aarch_sve (abfd
, note
);
11404 case NT_ARM_PAC_MASK
:
11405 if (note
->namesz
== 6
11406 && strcmp (note
->namedata
, "LINUX") == 0)
11407 return elfcore_grok_aarch_pauth (abfd
, note
);
11411 case NT_ARM_TAGGED_ADDR_CTRL
:
11412 if (note
->namesz
== 6
11413 && strcmp (note
->namedata
, "LINUX") == 0)
11414 return elfcore_grok_aarch_mte (abfd
, note
);
11419 if (note
->namesz
== 6
11420 && strcmp (note
->namedata
, "LINUX") == 0)
11421 return elfcore_grok_aarch_ssve (abfd
, note
);
11426 if (note
->namesz
== 6
11427 && strcmp (note
->namedata
, "LINUX") == 0)
11428 return elfcore_grok_aarch_za (abfd
, note
);
11433 if (note
->namesz
== 6
11434 && strcmp (note
->namedata
, "LINUX") == 0)
11435 return elfcore_grok_aarch_zt (abfd
, note
);
11440 if (note
->namesz
== 4
11441 && strcmp (note
->namedata
, "GDB") == 0)
11442 return elfcore_grok_gdb_tdesc (abfd
, note
);
11447 if (note
->namesz
== 4
11448 && strcmp (note
->namedata
, "GDB") == 0)
11449 return elfcore_grok_riscv_csr (abfd
, note
);
11453 case NT_LARCH_CPUCFG
:
11454 if (note
->namesz
== 6
11455 && strcmp (note
->namedata
, "LINUX") == 0)
11456 return elfcore_grok_loongarch_cpucfg (abfd
, note
);
11461 if (note
->namesz
== 6
11462 && strcmp (note
->namedata
, "LINUX") == 0)
11463 return elfcore_grok_loongarch_lbt (abfd
, note
);
11468 if (note
->namesz
== 6
11469 && strcmp (note
->namedata
, "LINUX") == 0)
11470 return elfcore_grok_loongarch_lsx (abfd
, note
);
11474 case NT_LARCH_LASX
:
11475 if (note
->namesz
== 6
11476 && strcmp (note
->namedata
, "LINUX") == 0)
11477 return elfcore_grok_loongarch_lasx (abfd
, note
);
11483 if (bed
->elf_backend_grok_psinfo
)
11484 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
11486 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11487 return elfcore_grok_psinfo (abfd
, note
);
11493 return elfcore_make_auxv_note_section (abfd
, note
, 0);
11496 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
11500 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
11507 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
11509 struct bfd_build_id
* build_id
;
11511 if (note
->descsz
== 0)
11514 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
11515 if (build_id
== NULL
)
11518 build_id
->size
= note
->descsz
;
11519 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
11520 abfd
->build_id
= build_id
;
11526 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11528 switch (note
->type
)
11533 case NT_GNU_PROPERTY_TYPE_0
:
11534 return _bfd_elf_parse_gnu_properties (abfd
, note
);
11536 case NT_GNU_BUILD_ID
:
11537 return elfobj_grok_gnu_build_id (abfd
, note
);
11542 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
11544 struct sdt_note
*cur
=
11545 (struct sdt_note
*) bfd_alloc (abfd
,
11546 sizeof (struct sdt_note
) + note
->descsz
);
11548 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
11549 cur
->size
= (bfd_size_type
) note
->descsz
;
11550 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
11552 elf_tdata (abfd
)->sdt_note_head
= cur
;
11558 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11560 switch (note
->type
)
11563 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
11571 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11575 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11578 if (note
->descsz
< 108)
11583 if (note
->descsz
< 120)
11591 /* Check for version 1 in pr_version. */
11592 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11597 /* Skip over pr_psinfosz. */
11598 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11602 offset
+= 4; /* Padding before pr_psinfosz. */
11606 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11607 elf_tdata (abfd
)->core
->program
11608 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
11611 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11612 elf_tdata (abfd
)->core
->command
11613 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
11616 /* Padding before pr_pid. */
11619 /* The pr_pid field was added in version "1a". */
11620 if (note
->descsz
< offset
+ 4)
11623 elf_tdata (abfd
)->core
->pid
11624 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11630 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
11636 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11637 Also compute minimum size of this note. */
11638 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11642 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
11646 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
11647 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
11654 if (note
->descsz
< min_size
)
11657 /* Check for version 1 in pr_version. */
11658 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11661 /* Extract size of pr_reg from pr_gregsetsz. */
11662 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11663 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11665 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11670 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11674 /* Skip over pr_osreldate. */
11677 /* Read signal from pr_cursig. */
11678 if (elf_tdata (abfd
)->core
->signal
== 0)
11679 elf_tdata (abfd
)->core
->signal
11680 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11683 /* Read TID from pr_pid. */
11684 elf_tdata (abfd
)->core
->lwpid
11685 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11688 /* Padding before pr_reg. */
11689 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
11692 /* Make sure that there is enough data remaining in the note. */
11693 if ((note
->descsz
- offset
) < size
)
11696 /* Make a ".reg/999" section and a ".reg" section. */
11697 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
11698 size
, note
->descpos
+ offset
);
11702 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11704 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11706 switch (note
->type
)
11709 if (bed
->elf_backend_grok_freebsd_prstatus
)
11710 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
11712 return elfcore_grok_freebsd_prstatus (abfd
, note
);
11715 return elfcore_grok_prfpreg (abfd
, note
);
11718 return elfcore_grok_freebsd_psinfo (abfd
, note
);
11720 case NT_FREEBSD_THRMISC
:
11721 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
11723 case NT_FREEBSD_PROCSTAT_PROC
:
11724 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
11727 case NT_FREEBSD_PROCSTAT_FILES
:
11728 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
11731 case NT_FREEBSD_PROCSTAT_VMMAP
:
11732 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
11735 case NT_FREEBSD_PROCSTAT_AUXV
:
11736 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11738 case NT_FREEBSD_X86_SEGBASES
:
11739 return elfcore_make_note_pseudosection (abfd
, ".reg-x86-segbases", note
);
11741 case NT_X86_XSTATE
:
11742 return elfcore_grok_xstatereg (abfd
, note
);
11744 case NT_FREEBSD_PTLWPINFO
:
11745 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
11749 return elfcore_grok_aarch_tls (abfd
, note
);
11752 return elfcore_grok_arm_vfp (abfd
, note
);
11760 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
11764 cp
= strchr (note
->namedata
, '@');
11767 *lwpidp
= atoi(cp
+ 1);
11774 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11776 if (note
->descsz
<= 0x7c + 31)
11779 /* Signal number at offset 0x08. */
11780 elf_tdata (abfd
)->core
->signal
11781 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11783 /* Process ID at offset 0x50. */
11784 elf_tdata (abfd
)->core
->pid
11785 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
11787 /* Command name at 0x7c (max 32 bytes, including nul). */
11788 elf_tdata (abfd
)->core
->command
11789 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
11791 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
11796 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11800 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
11801 elf_tdata (abfd
)->core
->lwpid
= lwp
;
11803 switch (note
->type
)
11805 case NT_NETBSDCORE_PROCINFO
:
11806 /* NetBSD-specific core "procinfo". Note that we expect to
11807 find this note before any of the others, which is fine,
11808 since the kernel writes this note out first when it
11809 creates a core file. */
11810 return elfcore_grok_netbsd_procinfo (abfd
, note
);
11811 case NT_NETBSDCORE_AUXV
:
11812 /* NetBSD-specific Elf Auxiliary Vector data. */
11813 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11814 case NT_NETBSDCORE_LWPSTATUS
:
11815 return elfcore_make_note_pseudosection (abfd
,
11816 ".note.netbsdcore.lwpstatus",
11822 /* As of March 2020 there are no other machine-independent notes
11823 defined for NetBSD core files. If the note type is less
11824 than the start of the machine-dependent note types, we don't
11827 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
11831 switch (bfd_get_arch (abfd
))
11833 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11834 PT_GETFPREGS == mach+2. */
11836 case bfd_arch_aarch64
:
11837 case bfd_arch_alpha
:
11838 case bfd_arch_sparc
:
11839 switch (note
->type
)
11841 case NT_NETBSDCORE_FIRSTMACH
+0:
11842 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11844 case NT_NETBSDCORE_FIRSTMACH
+2:
11845 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11851 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11852 There's also old PT___GETREGS40 == mach + 1 for old reg
11853 structure which lacks GBR. */
11856 switch (note
->type
)
11858 case NT_NETBSDCORE_FIRSTMACH
+3:
11859 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11861 case NT_NETBSDCORE_FIRSTMACH
+5:
11862 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11868 /* On all other arch's, PT_GETREGS == mach+1 and
11869 PT_GETFPREGS == mach+3. */
11872 switch (note
->type
)
11874 case NT_NETBSDCORE_FIRSTMACH
+1:
11875 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11877 case NT_NETBSDCORE_FIRSTMACH
+3:
11878 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11888 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11890 if (note
->descsz
<= 0x48 + 31)
11893 /* Signal number at offset 0x08. */
11894 elf_tdata (abfd
)->core
->signal
11895 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11897 /* Process ID at offset 0x20. */
11898 elf_tdata (abfd
)->core
->pid
11899 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
11901 /* Command name at 0x48 (max 32 bytes, including nul). */
11902 elf_tdata (abfd
)->core
->command
11903 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
11908 /* Processes Solaris's process status note.
11909 sig_off ~ offsetof(prstatus_t, pr_cursig)
11910 pid_off ~ offsetof(prstatus_t, pr_pid)
11911 lwpid_off ~ offsetof(prstatus_t, pr_who)
11912 gregset_size ~ sizeof(gregset_t)
11913 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11916 elfcore_grok_solaris_prstatus (bfd
*abfd
, Elf_Internal_Note
* note
, int sig_off
,
11917 int pid_off
, int lwpid_off
, size_t gregset_size
,
11918 size_t gregset_offset
)
11920 asection
*sect
= NULL
;
11921 elf_tdata (abfd
)->core
->signal
11922 = bfd_get_16 (abfd
, note
->descdata
+ sig_off
);
11923 elf_tdata (abfd
)->core
->pid
11924 = bfd_get_32 (abfd
, note
->descdata
+ pid_off
);
11925 elf_tdata (abfd
)->core
->lwpid
11926 = bfd_get_32 (abfd
, note
->descdata
+ lwpid_off
);
11928 sect
= bfd_get_section_by_name (abfd
, ".reg");
11930 sect
->size
= gregset_size
;
11932 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11933 note
->descpos
+ gregset_offset
);
11936 /* Gets program and arguments from a core.
11937 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11938 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11941 elfcore_grok_solaris_info(bfd
*abfd
, Elf_Internal_Note
* note
,
11942 int prog_off
, int comm_off
)
11944 elf_tdata (abfd
)->core
->program
11945 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ prog_off
, 16);
11946 elf_tdata (abfd
)->core
->command
11947 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ comm_off
, 80);
11952 /* Processes Solaris's LWP status note.
11953 gregset_size ~ sizeof(gregset_t)
11954 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11955 fpregset_size ~ sizeof(fpregset_t)
11956 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11959 elfcore_grok_solaris_lwpstatus (bfd
*abfd
, Elf_Internal_Note
* note
,
11960 size_t gregset_size
, int gregset_off
,
11961 size_t fpregset_size
, int fpregset_off
)
11963 asection
*sect
= NULL
;
11964 char reg2_section_name
[16] = { 0 };
11966 (void) snprintf (reg2_section_name
, 16, "%s/%i", ".reg2",
11967 elf_tdata (abfd
)->core
->lwpid
);
11969 /* offsetof(lwpstatus_t, pr_lwpid) */
11970 elf_tdata (abfd
)->core
->lwpid
11971 = bfd_get_32 (abfd
, note
->descdata
+ 4);
11972 /* offsetof(lwpstatus_t, pr_cursig) */
11973 elf_tdata (abfd
)->core
->signal
11974 = bfd_get_16 (abfd
, note
->descdata
+ 12);
11976 sect
= bfd_get_section_by_name (abfd
, ".reg");
11978 sect
->size
= gregset_size
;
11979 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11980 note
->descpos
+ gregset_off
))
11983 sect
= bfd_get_section_by_name (abfd
, reg2_section_name
);
11986 sect
->size
= fpregset_size
;
11987 sect
->filepos
= note
->descpos
+ fpregset_off
;
11988 sect
->alignment_power
= 2;
11990 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg2", fpregset_size
,
11991 note
->descpos
+ fpregset_off
))
11998 elfcore_grok_solaris_note_impl (bfd
*abfd
, Elf_Internal_Note
*note
)
12003 /* core files are identified as 32- or 64-bit, SPARC or x86,
12004 by the size of the descsz which matches the sizeof()
12005 the type appropriate for that note type (e.g., prstatus_t for
12006 SOLARIS_NT_PRSTATUS) for the corresponding architecture
12007 on Solaris. The core file bitness may differ from the bitness of
12008 gdb itself, so fixed values are used instead of sizeof().
12009 Appropriate fixed offsets are also used to obtain data from
12012 switch ((int) note
->type
)
12014 case SOLARIS_NT_PRSTATUS
:
12015 switch (note
->descsz
)
12017 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12018 return elfcore_grok_solaris_prstatus(abfd
, note
,
12019 136, 216, 308, 152, 356);
12020 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12021 return elfcore_grok_solaris_prstatus(abfd
, note
,
12022 264, 360, 520, 304, 600);
12023 case 432: /* sizeof(prstatus_t) Intel 32-bit */
12024 return elfcore_grok_solaris_prstatus(abfd
, note
,
12025 136, 216, 308, 76, 356);
12026 case 824: /* sizeof(prstatus_t) Intel 64-bit */
12027 return elfcore_grok_solaris_prstatus(abfd
, note
,
12028 264, 360, 520, 224, 600);
12033 case SOLARIS_NT_PSINFO
:
12034 case SOLARIS_NT_PRPSINFO
:
12035 switch (note
->descsz
)
12037 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12038 return elfcore_grok_solaris_info(abfd
, note
, 84, 100);
12039 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12040 return elfcore_grok_solaris_info(abfd
, note
, 120, 136);
12041 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12042 return elfcore_grok_solaris_info(abfd
, note
, 88, 104);
12043 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12044 return elfcore_grok_solaris_info(abfd
, note
, 136, 152);
12049 case SOLARIS_NT_LWPSTATUS
:
12050 switch (note
->descsz
)
12052 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12053 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12054 152, 344, 400, 496);
12055 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12056 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12057 304, 544, 544, 848);
12058 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12059 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12060 76, 344, 380, 420);
12061 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12062 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12063 224, 544, 528, 768);
12068 case SOLARIS_NT_LWPSINFO
:
12069 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12070 if (note
->descsz
== 128 || note
->descsz
== 152)
12071 elf_tdata (abfd
)->core
->lwpid
=
12072 bfd_get_32 (abfd
, note
->descdata
+ 4);
12082 /* For name starting with "CORE" this may be either a Solaris
12083 core file or a gdb-generated core file. Do Solaris-specific
12084 processing on selected note types first with
12085 elfcore_grok_solaris_note(), then process the note
12086 in elfcore_grok_note(). */
12089 elfcore_grok_solaris_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12091 if (!elfcore_grok_solaris_note_impl (abfd
, note
))
12094 return elfcore_grok_note (abfd
, note
);
12098 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12100 if (note
->type
== NT_OPENBSD_PROCINFO
)
12101 return elfcore_grok_openbsd_procinfo (abfd
, note
);
12103 if (note
->type
== NT_OPENBSD_REGS
)
12104 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
12106 if (note
->type
== NT_OPENBSD_FPREGS
)
12107 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
12109 if (note
->type
== NT_OPENBSD_XFPREGS
)
12110 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
12112 if (note
->type
== NT_OPENBSD_AUXV
)
12113 return elfcore_make_auxv_note_section (abfd
, note
, 0);
12115 if (note
->type
== NT_OPENBSD_WCOOKIE
)
12117 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
12122 sect
->size
= note
->descsz
;
12123 sect
->filepos
= note
->descpos
;
12124 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
12133 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
12135 void *ddata
= note
->descdata
;
12142 if (note
->descsz
< 16)
12145 /* nto_procfs_status 'pid' field is at offset 0. */
12146 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
12148 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12149 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
12151 /* nto_procfs_status 'flags' field is at offset 8. */
12152 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
12154 /* nto_procfs_status 'what' field is at offset 14. */
12155 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
12157 elf_tdata (abfd
)->core
->signal
= sig
;
12158 elf_tdata (abfd
)->core
->lwpid
= *tid
;
12161 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12162 do not come from signals so we make sure we set the current
12163 thread just in case. */
12164 if (flags
& 0x00000080)
12165 elf_tdata (abfd
)->core
->lwpid
= *tid
;
12167 /* Make a ".qnx_core_status/%d" section. */
12168 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
12170 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
12173 strcpy (name
, buf
);
12175 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12179 sect
->size
= note
->descsz
;
12180 sect
->filepos
= note
->descpos
;
12181 sect
->alignment_power
= 2;
12183 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
12187 elfcore_grok_nto_regs (bfd
*abfd
,
12188 Elf_Internal_Note
*note
,
12196 /* Make a "(base)/%d" section. */
12197 sprintf (buf
, "%s/%ld", base
, tid
);
12199 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
12202 strcpy (name
, buf
);
12204 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12208 sect
->size
= note
->descsz
;
12209 sect
->filepos
= note
->descpos
;
12210 sect
->alignment_power
= 2;
12212 /* This is the current thread. */
12213 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
12214 return elfcore_maybe_make_sect (abfd
, base
, sect
);
12220 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12222 /* Every GREG section has a STATUS section before it. Store the
12223 tid from the previous call to pass down to the next gregs
12225 static long tid
= 1;
12227 switch (note
->type
)
12229 case QNT_CORE_INFO
:
12230 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
12231 case QNT_CORE_STATUS
:
12232 return elfcore_grok_nto_status (abfd
, note
, &tid
);
12233 case QNT_CORE_GREG
:
12234 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
12235 case QNT_CORE_FPREG
:
12236 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
12243 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12249 /* Use note name as section name. */
12250 len
= note
->namesz
;
12251 name
= (char *) bfd_alloc (abfd
, len
);
12254 memcpy (name
, note
->namedata
, len
);
12255 name
[len
- 1] = '\0';
12257 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12261 sect
->size
= note
->descsz
;
12262 sect
->filepos
= note
->descpos
;
12263 sect
->alignment_power
= 1;
12268 /* Function: elfcore_write_note
12271 buffer to hold note, and current size of buffer
12275 size of data for note
12277 Writes note to end of buffer. ELF64 notes are written exactly as
12278 for ELF32, despite the current (as of 2006) ELF gabi specifying
12279 that they ought to have 8-byte namesz and descsz field, and have
12280 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12283 Pointer to realloc'd buffer, *BUFSIZ updated. */
12286 elfcore_write_note (bfd
*abfd
,
12294 Elf_External_Note
*xnp
;
12301 namesz
= strlen (name
) + 1;
12303 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
12305 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
12308 dest
= buf
+ *bufsiz
;
12309 *bufsiz
+= newspace
;
12310 xnp
= (Elf_External_Note
*) dest
;
12311 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
12312 H_PUT_32 (abfd
, size
, xnp
->descsz
);
12313 H_PUT_32 (abfd
, type
, xnp
->type
);
12317 memcpy (dest
, name
, namesz
);
12325 memcpy (dest
, input
, size
);
12335 /* gcc-8 warns (*) on all the strncpy calls in this function about
12336 possible string truncation. The "truncation" is not a bug. We
12337 have an external representation of structs with fields that are not
12338 necessarily NULL terminated and corresponding internal
12339 representation fields that are one larger so that they can always
12340 be NULL terminated.
12341 gcc versions between 4.2 and 4.6 do not allow pragma control of
12342 diagnostics inside functions, giving a hard error if you try to use
12343 the finer control available with later versions.
12344 gcc prior to 4.2 warns about diagnostic push and pop.
12345 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12346 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12347 (*) Depending on your system header files! */
12348 #if GCC_VERSION >= 8000
12349 # pragma GCC diagnostic push
12350 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12353 elfcore_write_prpsinfo (bfd
*abfd
,
12357 const char *psargs
)
12359 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12361 if (bed
->elf_backend_write_core_note
!= NULL
)
12364 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
12365 NT_PRPSINFO
, fname
, psargs
);
12370 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12371 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12372 if (bed
->s
->elfclass
== ELFCLASS32
)
12374 # if defined (HAVE_PSINFO32_T)
12376 int note_type
= NT_PSINFO
;
12379 int note_type
= NT_PRPSINFO
;
12382 memset (&data
, 0, sizeof (data
));
12383 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
12384 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
12385 return elfcore_write_note (abfd
, buf
, bufsiz
,
12386 "CORE", note_type
, &data
, sizeof (data
));
12391 # if defined (HAVE_PSINFO_T)
12393 int note_type
= NT_PSINFO
;
12396 int note_type
= NT_PRPSINFO
;
12399 memset (&data
, 0, sizeof (data
));
12400 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
12401 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
12402 return elfcore_write_note (abfd
, buf
, bufsiz
,
12403 "CORE", note_type
, &data
, sizeof (data
));
12405 #endif /* PSINFO_T or PRPSINFO_T */
12410 #if GCC_VERSION >= 8000
12411 # pragma GCC diagnostic pop
12415 elfcore_write_linux_prpsinfo32
12416 (bfd
*abfd
, char *buf
, int *bufsiz
,
12417 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
12419 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
12421 struct elf_external_linux_prpsinfo32_ugid16 data
;
12423 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
12424 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
12425 &data
, sizeof (data
));
12429 struct elf_external_linux_prpsinfo32_ugid32 data
;
12431 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
12432 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
12433 &data
, sizeof (data
));
12438 elfcore_write_linux_prpsinfo64
12439 (bfd
*abfd
, char *buf
, int *bufsiz
,
12440 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
12442 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
12444 struct elf_external_linux_prpsinfo64_ugid16 data
;
12446 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
12447 return elfcore_write_note (abfd
, buf
, bufsiz
,
12448 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
12452 struct elf_external_linux_prpsinfo64_ugid32 data
;
12454 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
12455 return elfcore_write_note (abfd
, buf
, bufsiz
,
12456 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
12461 elfcore_write_prstatus (bfd
*abfd
,
12468 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12470 if (bed
->elf_backend_write_core_note
!= NULL
)
12473 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
12475 pid
, cursig
, gregs
);
12480 #if defined (HAVE_PRSTATUS_T)
12481 #if defined (HAVE_PRSTATUS32_T)
12482 if (bed
->s
->elfclass
== ELFCLASS32
)
12484 prstatus32_t prstat
;
12486 memset (&prstat
, 0, sizeof (prstat
));
12487 prstat
.pr_pid
= pid
;
12488 prstat
.pr_cursig
= cursig
;
12489 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
12490 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
12491 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
12498 memset (&prstat
, 0, sizeof (prstat
));
12499 prstat
.pr_pid
= pid
;
12500 prstat
.pr_cursig
= cursig
;
12501 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
12502 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
12503 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
12505 #endif /* HAVE_PRSTATUS_T */
12511 #if defined (HAVE_LWPSTATUS_T)
12513 elfcore_write_lwpstatus (bfd
*abfd
,
12520 lwpstatus_t lwpstat
;
12521 const char *note_name
= "CORE";
12523 memset (&lwpstat
, 0, sizeof (lwpstat
));
12524 lwpstat
.pr_lwpid
= pid
>> 16;
12525 lwpstat
.pr_cursig
= cursig
;
12526 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12527 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
12528 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12529 #if !defined(gregs)
12530 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
12531 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
12533 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
12534 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
12537 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12538 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
12540 #endif /* HAVE_LWPSTATUS_T */
12542 #if defined (HAVE_PSTATUS_T)
12544 elfcore_write_pstatus (bfd
*abfd
,
12548 int cursig ATTRIBUTE_UNUSED
,
12549 const void *gregs ATTRIBUTE_UNUSED
)
12551 const char *note_name
= "CORE";
12552 #if defined (HAVE_PSTATUS32_T)
12553 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12555 if (bed
->s
->elfclass
== ELFCLASS32
)
12559 memset (&pstat
, 0, sizeof (pstat
));
12560 pstat
.pr_pid
= pid
& 0xffff;
12561 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12562 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12570 memset (&pstat
, 0, sizeof (pstat
));
12571 pstat
.pr_pid
= pid
& 0xffff;
12572 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12573 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12577 #endif /* HAVE_PSTATUS_T */
12580 elfcore_write_prfpreg (bfd
*abfd
,
12583 const void *fpregs
,
12586 const char *note_name
= "CORE";
12587 return elfcore_write_note (abfd
, buf
, bufsiz
,
12588 note_name
, NT_FPREGSET
, fpregs
, size
);
12592 elfcore_write_prxfpreg (bfd
*abfd
,
12595 const void *xfpregs
,
12598 char *note_name
= "LINUX";
12599 return elfcore_write_note (abfd
, buf
, bufsiz
,
12600 note_name
, NT_PRXFPREG
, xfpregs
, size
);
12604 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
12605 const void *xfpregs
, int size
)
12608 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
12609 note_name
= "FreeBSD";
12611 note_name
= "LINUX";
12612 return elfcore_write_note (abfd
, buf
, bufsiz
,
12613 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
12617 elfcore_write_x86_segbases (bfd
*abfd
, char *buf
, int *bufsiz
,
12618 const void *regs
, int size
)
12620 char *note_name
= "FreeBSD";
12621 return elfcore_write_note (abfd
, buf
, bufsiz
,
12622 note_name
, NT_FREEBSD_X86_SEGBASES
, regs
, size
);
12626 elfcore_write_ppc_vmx (bfd
*abfd
,
12629 const void *ppc_vmx
,
12632 char *note_name
= "LINUX";
12633 return elfcore_write_note (abfd
, buf
, bufsiz
,
12634 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
12638 elfcore_write_ppc_vsx (bfd
*abfd
,
12641 const void *ppc_vsx
,
12644 char *note_name
= "LINUX";
12645 return elfcore_write_note (abfd
, buf
, bufsiz
,
12646 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
12650 elfcore_write_ppc_tar (bfd
*abfd
,
12653 const void *ppc_tar
,
12656 char *note_name
= "LINUX";
12657 return elfcore_write_note (abfd
, buf
, bufsiz
,
12658 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
12662 elfcore_write_ppc_ppr (bfd
*abfd
,
12665 const void *ppc_ppr
,
12668 char *note_name
= "LINUX";
12669 return elfcore_write_note (abfd
, buf
, bufsiz
,
12670 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
12674 elfcore_write_ppc_dscr (bfd
*abfd
,
12677 const void *ppc_dscr
,
12680 char *note_name
= "LINUX";
12681 return elfcore_write_note (abfd
, buf
, bufsiz
,
12682 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
12686 elfcore_write_ppc_ebb (bfd
*abfd
,
12689 const void *ppc_ebb
,
12692 char *note_name
= "LINUX";
12693 return elfcore_write_note (abfd
, buf
, bufsiz
,
12694 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
12698 elfcore_write_ppc_pmu (bfd
*abfd
,
12701 const void *ppc_pmu
,
12704 char *note_name
= "LINUX";
12705 return elfcore_write_note (abfd
, buf
, bufsiz
,
12706 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
12710 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
12713 const void *ppc_tm_cgpr
,
12716 char *note_name
= "LINUX";
12717 return elfcore_write_note (abfd
, buf
, bufsiz
,
12718 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
12722 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
12725 const void *ppc_tm_cfpr
,
12728 char *note_name
= "LINUX";
12729 return elfcore_write_note (abfd
, buf
, bufsiz
,
12730 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
12734 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
12737 const void *ppc_tm_cvmx
,
12740 char *note_name
= "LINUX";
12741 return elfcore_write_note (abfd
, buf
, bufsiz
,
12742 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
12746 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
12749 const void *ppc_tm_cvsx
,
12752 char *note_name
= "LINUX";
12753 return elfcore_write_note (abfd
, buf
, bufsiz
,
12754 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
12758 elfcore_write_ppc_tm_spr (bfd
*abfd
,
12761 const void *ppc_tm_spr
,
12764 char *note_name
= "LINUX";
12765 return elfcore_write_note (abfd
, buf
, bufsiz
,
12766 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
12770 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
12773 const void *ppc_tm_ctar
,
12776 char *note_name
= "LINUX";
12777 return elfcore_write_note (abfd
, buf
, bufsiz
,
12778 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
12782 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
12785 const void *ppc_tm_cppr
,
12788 char *note_name
= "LINUX";
12789 return elfcore_write_note (abfd
, buf
, bufsiz
,
12790 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
12794 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
12797 const void *ppc_tm_cdscr
,
12800 char *note_name
= "LINUX";
12801 return elfcore_write_note (abfd
, buf
, bufsiz
,
12802 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
12806 elfcore_write_s390_high_gprs (bfd
*abfd
,
12809 const void *s390_high_gprs
,
12812 char *note_name
= "LINUX";
12813 return elfcore_write_note (abfd
, buf
, bufsiz
,
12814 note_name
, NT_S390_HIGH_GPRS
,
12815 s390_high_gprs
, size
);
12819 elfcore_write_s390_timer (bfd
*abfd
,
12822 const void *s390_timer
,
12825 char *note_name
= "LINUX";
12826 return elfcore_write_note (abfd
, buf
, bufsiz
,
12827 note_name
, NT_S390_TIMER
, s390_timer
, size
);
12831 elfcore_write_s390_todcmp (bfd
*abfd
,
12834 const void *s390_todcmp
,
12837 char *note_name
= "LINUX";
12838 return elfcore_write_note (abfd
, buf
, bufsiz
,
12839 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
12843 elfcore_write_s390_todpreg (bfd
*abfd
,
12846 const void *s390_todpreg
,
12849 char *note_name
= "LINUX";
12850 return elfcore_write_note (abfd
, buf
, bufsiz
,
12851 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
12855 elfcore_write_s390_ctrs (bfd
*abfd
,
12858 const void *s390_ctrs
,
12861 char *note_name
= "LINUX";
12862 return elfcore_write_note (abfd
, buf
, bufsiz
,
12863 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
12867 elfcore_write_s390_prefix (bfd
*abfd
,
12870 const void *s390_prefix
,
12873 char *note_name
= "LINUX";
12874 return elfcore_write_note (abfd
, buf
, bufsiz
,
12875 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
12879 elfcore_write_s390_last_break (bfd
*abfd
,
12882 const void *s390_last_break
,
12885 char *note_name
= "LINUX";
12886 return elfcore_write_note (abfd
, buf
, bufsiz
,
12887 note_name
, NT_S390_LAST_BREAK
,
12888 s390_last_break
, size
);
12892 elfcore_write_s390_system_call (bfd
*abfd
,
12895 const void *s390_system_call
,
12898 char *note_name
= "LINUX";
12899 return elfcore_write_note (abfd
, buf
, bufsiz
,
12900 note_name
, NT_S390_SYSTEM_CALL
,
12901 s390_system_call
, size
);
12905 elfcore_write_s390_tdb (bfd
*abfd
,
12908 const void *s390_tdb
,
12911 char *note_name
= "LINUX";
12912 return elfcore_write_note (abfd
, buf
, bufsiz
,
12913 note_name
, NT_S390_TDB
, s390_tdb
, size
);
12917 elfcore_write_s390_vxrs_low (bfd
*abfd
,
12920 const void *s390_vxrs_low
,
12923 char *note_name
= "LINUX";
12924 return elfcore_write_note (abfd
, buf
, bufsiz
,
12925 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
12929 elfcore_write_s390_vxrs_high (bfd
*abfd
,
12932 const void *s390_vxrs_high
,
12935 char *note_name
= "LINUX";
12936 return elfcore_write_note (abfd
, buf
, bufsiz
,
12937 note_name
, NT_S390_VXRS_HIGH
,
12938 s390_vxrs_high
, size
);
12942 elfcore_write_s390_gs_cb (bfd
*abfd
,
12945 const void *s390_gs_cb
,
12948 char *note_name
= "LINUX";
12949 return elfcore_write_note (abfd
, buf
, bufsiz
,
12950 note_name
, NT_S390_GS_CB
,
12955 elfcore_write_s390_gs_bc (bfd
*abfd
,
12958 const void *s390_gs_bc
,
12961 char *note_name
= "LINUX";
12962 return elfcore_write_note (abfd
, buf
, bufsiz
,
12963 note_name
, NT_S390_GS_BC
,
12968 elfcore_write_arm_vfp (bfd
*abfd
,
12971 const void *arm_vfp
,
12974 char *note_name
= "LINUX";
12975 return elfcore_write_note (abfd
, buf
, bufsiz
,
12976 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
12980 elfcore_write_aarch_tls (bfd
*abfd
,
12983 const void *aarch_tls
,
12986 char *note_name
= "LINUX";
12987 return elfcore_write_note (abfd
, buf
, bufsiz
,
12988 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
12992 elfcore_write_aarch_hw_break (bfd
*abfd
,
12995 const void *aarch_hw_break
,
12998 char *note_name
= "LINUX";
12999 return elfcore_write_note (abfd
, buf
, bufsiz
,
13000 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
13004 elfcore_write_aarch_hw_watch (bfd
*abfd
,
13007 const void *aarch_hw_watch
,
13010 char *note_name
= "LINUX";
13011 return elfcore_write_note (abfd
, buf
, bufsiz
,
13012 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
13016 elfcore_write_aarch_sve (bfd
*abfd
,
13019 const void *aarch_sve
,
13022 char *note_name
= "LINUX";
13023 return elfcore_write_note (abfd
, buf
, bufsiz
,
13024 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
13028 elfcore_write_aarch_pauth (bfd
*abfd
,
13031 const void *aarch_pauth
,
13034 char *note_name
= "LINUX";
13035 return elfcore_write_note (abfd
, buf
, bufsiz
,
13036 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
13040 elfcore_write_aarch_mte (bfd
*abfd
,
13043 const void *aarch_mte
,
13046 char *note_name
= "LINUX";
13047 return elfcore_write_note (abfd
, buf
, bufsiz
,
13048 note_name
, NT_ARM_TAGGED_ADDR_CTRL
,
13054 elfcore_write_aarch_ssve (bfd
*abfd
,
13057 const void *aarch_ssve
,
13060 char *note_name
= "LINUX";
13061 return elfcore_write_note (abfd
, buf
, bufsiz
,
13062 note_name
, NT_ARM_SSVE
,
13068 elfcore_write_aarch_za (bfd
*abfd
,
13071 const void *aarch_za
,
13074 char *note_name
= "LINUX";
13075 return elfcore_write_note (abfd
, buf
, bufsiz
,
13076 note_name
, NT_ARM_ZA
,
13081 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13082 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13083 written into. Return a pointer to the new start of the note buffer, to
13084 replace BUF which may no longer be valid. */
13087 elfcore_write_aarch_zt (bfd
*abfd
,
13090 const void *aarch_zt
,
13093 char *note_name
= "LINUX";
13094 return elfcore_write_note (abfd
, buf
, bufsiz
,
13095 note_name
, NT_ARM_ZT
,
13101 elfcore_write_arc_v2 (bfd
*abfd
,
13104 const void *arc_v2
,
13107 char *note_name
= "LINUX";
13108 return elfcore_write_note (abfd
, buf
, bufsiz
,
13109 note_name
, NT_ARC_V2
, arc_v2
, size
);
13113 elfcore_write_loongarch_cpucfg (bfd
*abfd
,
13116 const void *loongarch_cpucfg
,
13119 char *note_name
= "LINUX";
13120 return elfcore_write_note (abfd
, buf
, bufsiz
,
13121 note_name
, NT_LARCH_CPUCFG
,
13122 loongarch_cpucfg
, size
);
13126 elfcore_write_loongarch_lbt (bfd
*abfd
,
13129 const void *loongarch_lbt
,
13132 char *note_name
= "LINUX";
13133 return elfcore_write_note (abfd
, buf
, bufsiz
,
13134 note_name
, NT_LARCH_LBT
, loongarch_lbt
, size
);
13138 elfcore_write_loongarch_lsx (bfd
*abfd
,
13141 const void *loongarch_lsx
,
13144 char *note_name
= "LINUX";
13145 return elfcore_write_note (abfd
, buf
, bufsiz
,
13146 note_name
, NT_LARCH_LSX
, loongarch_lsx
, size
);
13150 elfcore_write_loongarch_lasx (bfd
*abfd
,
13153 const void *loongarch_lasx
,
13156 char *note_name
= "LINUX";
13157 return elfcore_write_note (abfd
, buf
, bufsiz
,
13158 note_name
, NT_LARCH_LASX
, loongarch_lasx
, size
);
13161 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13162 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13163 written into. Return a pointer to the new start of the note buffer, to
13164 replace BUF which may no longer be valid. */
13167 elfcore_write_riscv_csr (bfd
*abfd
,
13173 const char *note_name
= "GDB";
13174 return elfcore_write_note (abfd
, buf
, bufsiz
,
13175 note_name
, NT_RISCV_CSR
, csrs
, size
);
13178 /* Write the target description (a string) pointed to by TDESC, length
13179 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13180 note is being written into. Return a pointer to the new start of the
13181 note buffer, to replace BUF which may no longer be valid. */
13184 elfcore_write_gdb_tdesc (bfd
*abfd
,
13190 const char *note_name
= "GDB";
13191 return elfcore_write_note (abfd
, buf
, bufsiz
,
13192 note_name
, NT_GDB_TDESC
, tdesc
, size
);
13196 elfcore_write_register_note (bfd
*abfd
,
13199 const char *section
,
13203 if (strcmp (section
, ".reg2") == 0)
13204 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
13205 if (strcmp (section
, ".reg-xfp") == 0)
13206 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
13207 if (strcmp (section
, ".reg-xstate") == 0)
13208 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
13209 if (strcmp (section
, ".reg-x86-segbases") == 0)
13210 return elfcore_write_x86_segbases (abfd
, buf
, bufsiz
, data
, size
);
13211 if (strcmp (section
, ".reg-ppc-vmx") == 0)
13212 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
13213 if (strcmp (section
, ".reg-ppc-vsx") == 0)
13214 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
13215 if (strcmp (section
, ".reg-ppc-tar") == 0)
13216 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
13217 if (strcmp (section
, ".reg-ppc-ppr") == 0)
13218 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
13219 if (strcmp (section
, ".reg-ppc-dscr") == 0)
13220 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
13221 if (strcmp (section
, ".reg-ppc-ebb") == 0)
13222 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
13223 if (strcmp (section
, ".reg-ppc-pmu") == 0)
13224 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
13225 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
13226 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
13227 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
13228 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
13229 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
13230 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
13231 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
13232 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
13233 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
13234 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
13235 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
13236 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
13237 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
13238 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
13239 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
13240 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
13241 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
13242 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
13243 if (strcmp (section
, ".reg-s390-timer") == 0)
13244 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
13245 if (strcmp (section
, ".reg-s390-todcmp") == 0)
13246 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
13247 if (strcmp (section
, ".reg-s390-todpreg") == 0)
13248 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
13249 if (strcmp (section
, ".reg-s390-ctrs") == 0)
13250 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
13251 if (strcmp (section
, ".reg-s390-prefix") == 0)
13252 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
13253 if (strcmp (section
, ".reg-s390-last-break") == 0)
13254 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
13255 if (strcmp (section
, ".reg-s390-system-call") == 0)
13256 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
13257 if (strcmp (section
, ".reg-s390-tdb") == 0)
13258 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
13259 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
13260 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
13261 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
13262 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
13263 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
13264 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
13265 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
13266 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
13267 if (strcmp (section
, ".reg-arm-vfp") == 0)
13268 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
13269 if (strcmp (section
, ".reg-aarch-tls") == 0)
13270 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
13271 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
13272 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
13273 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
13274 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
13275 if (strcmp (section
, ".reg-aarch-sve") == 0)
13276 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
13277 if (strcmp (section
, ".reg-aarch-pauth") == 0)
13278 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
13279 if (strcmp (section
, ".reg-aarch-mte") == 0)
13280 return elfcore_write_aarch_mte (abfd
, buf
, bufsiz
, data
, size
);
13281 if (strcmp (section
, ".reg-aarch-ssve") == 0)
13282 return elfcore_write_aarch_ssve (abfd
, buf
, bufsiz
, data
, size
);
13283 if (strcmp (section
, ".reg-aarch-za") == 0)
13284 return elfcore_write_aarch_za (abfd
, buf
, bufsiz
, data
, size
);
13285 if (strcmp (section
, ".reg-aarch-zt") == 0)
13286 return elfcore_write_aarch_zt (abfd
, buf
, bufsiz
, data
, size
);
13287 if (strcmp (section
, ".reg-arc-v2") == 0)
13288 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
13289 if (strcmp (section
, ".gdb-tdesc") == 0)
13290 return elfcore_write_gdb_tdesc (abfd
, buf
, bufsiz
, data
, size
);
13291 if (strcmp (section
, ".reg-riscv-csr") == 0)
13292 return elfcore_write_riscv_csr (abfd
, buf
, bufsiz
, data
, size
);
13293 if (strcmp (section
, ".reg-loongarch-cpucfg") == 0)
13294 return elfcore_write_loongarch_cpucfg (abfd
, buf
, bufsiz
, data
, size
);
13295 if (strcmp (section
, ".reg-loongarch-lbt") == 0)
13296 return elfcore_write_loongarch_lbt (abfd
, buf
, bufsiz
, data
, size
);
13297 if (strcmp (section
, ".reg-loongarch-lsx") == 0)
13298 return elfcore_write_loongarch_lsx (abfd
, buf
, bufsiz
, data
, size
);
13299 if (strcmp (section
, ".reg-loongarch-lasx") == 0)
13300 return elfcore_write_loongarch_lasx (abfd
, buf
, bufsiz
, data
, size
);
13305 elfcore_write_file_note (bfd
*obfd
, char *note_data
, int *note_size
,
13306 const void *buf
, int bufsiz
)
13308 return elfcore_write_note (obfd
, note_data
, note_size
,
13309 "CORE", NT_FILE
, buf
, bufsiz
);
13313 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
13318 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13319 gABI specifies that PT_NOTE alignment should be aligned to 4
13320 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13321 align is less than 4, we use 4 byte alignment. */
13324 if (align
!= 4 && align
!= 8)
13328 while (p
< buf
+ size
)
13330 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
13331 Elf_Internal_Note in
;
13333 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
13336 in
.type
= H_GET_32 (abfd
, xnp
->type
);
13338 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
13339 in
.namedata
= xnp
->name
;
13340 if (in
.namesz
> buf
- in
.namedata
+ size
)
13343 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
13344 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
13345 in
.descpos
= offset
+ (in
.descdata
- buf
);
13347 && (in
.descdata
>= buf
+ size
13348 || in
.descsz
> buf
- in
.descdata
+ size
))
13351 switch (bfd_get_format (abfd
))
13358 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13361 const char * string
;
13363 bool (*func
) (bfd
*, Elf_Internal_Note
*);
13367 GROKER_ELEMENT ("", elfcore_grok_note
),
13368 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
13369 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
13370 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note
),
13371 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
13372 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
13373 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
),
13374 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note
)
13376 #undef GROKER_ELEMENT
13379 for (i
= ARRAY_SIZE (grokers
); i
--;)
13381 if (in
.namesz
>= grokers
[i
].len
13382 && strncmp (in
.namedata
, grokers
[i
].string
,
13383 grokers
[i
].len
) == 0)
13385 if (! grokers
[i
].func (abfd
, & in
))
13394 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
13396 if (! elfobj_grok_gnu_note (abfd
, &in
))
13399 else if (in
.namesz
== sizeof "stapsdt"
13400 && strcmp (in
.namedata
, "stapsdt") == 0)
13402 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
13408 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
13415 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
13420 if (size
== 0 || (size
+ 1) == 0)
13423 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
13426 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
13430 /* PR 17512: file: ec08f814
13431 0-termintate the buffer so that string searches will not overflow. */
13434 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
13444 /* Providing external access to the ELF program header table. */
13446 /* Return an upper bound on the number of bytes required to store a
13447 copy of ABFD's program header table entries. Return -1 if an error
13448 occurs; bfd_get_error will return an appropriate code. */
13451 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
13453 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
13455 bfd_set_error (bfd_error_wrong_format
);
13459 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
13462 /* Copy ABFD's program header table entries to *PHDRS. The entries
13463 will be stored as an array of Elf_Internal_Phdr structures, as
13464 defined in include/elf/internal.h. To find out how large the
13465 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13467 Return the number of program header table entries read, or -1 if an
13468 error occurs; bfd_get_error will return an appropriate code. */
13471 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
13475 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
13477 bfd_set_error (bfd_error_wrong_format
);
13481 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
13482 if (num_phdrs
!= 0)
13483 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
13484 num_phdrs
* sizeof (Elf_Internal_Phdr
));
13489 enum elf_reloc_type_class
13490 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13491 const asection
*rel_sec ATTRIBUTE_UNUSED
,
13492 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
13494 return reloc_class_normal
;
13497 /* For RELA architectures, return the relocation value for a
13498 relocation against a local symbol. */
13501 _bfd_elf_rela_local_sym (bfd
*abfd
,
13502 Elf_Internal_Sym
*sym
,
13504 Elf_Internal_Rela
*rel
)
13506 asection
*sec
= *psec
;
13507 bfd_vma relocation
;
13509 relocation
= (sec
->output_section
->vma
13510 + sec
->output_offset
13512 if ((sec
->flags
& SEC_MERGE
)
13513 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
13514 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
13517 _bfd_merged_section_offset (abfd
, psec
,
13518 elf_section_data (sec
)->sec_info
,
13519 sym
->st_value
+ rel
->r_addend
);
13522 /* If we have changed the section, and our original section is
13523 marked with SEC_EXCLUDE, it means that the original
13524 SEC_MERGE section has been completely subsumed in some
13525 other SEC_MERGE section. In this case, we need to leave
13526 some info around for --emit-relocs. */
13527 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
13528 sec
->kept_section
= *psec
;
13531 rel
->r_addend
-= relocation
;
13532 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
13538 _bfd_elf_rel_local_sym (bfd
*abfd
,
13539 Elf_Internal_Sym
*sym
,
13543 asection
*sec
= *psec
;
13545 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
13546 return sym
->st_value
+ addend
;
13548 return _bfd_merged_section_offset (abfd
, psec
,
13549 elf_section_data (sec
)->sec_info
,
13550 sym
->st_value
+ addend
);
13553 /* Adjust an address within a section. Given OFFSET within SEC, return
13554 the new offset within the section, based upon changes made to the
13555 section. Returns -1 if the offset is now invalid.
13556 The offset (in abnd out) is in target sized bytes, however big a
13560 _bfd_elf_section_offset (bfd
*abfd
,
13561 struct bfd_link_info
*info
,
13565 switch (sec
->sec_info_type
)
13567 case SEC_INFO_TYPE_STABS
:
13568 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
13570 case SEC_INFO_TYPE_EH_FRAME
:
13571 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
13574 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
13576 /* Reverse the offset. */
13577 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13578 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
13580 /* address_size and sec->size are in octets. Convert
13581 to bytes before subtracting the original offset. */
13582 offset
= ((sec
->size
- address_size
)
13583 / bfd_octets_per_byte (abfd
, sec
) - offset
);
13590 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
13591 long symcount ATTRIBUTE_UNUSED
,
13592 asymbol
**syms ATTRIBUTE_UNUSED
,
13597 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13600 const char *relplt_name
;
13601 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
13605 Elf_Internal_Shdr
*hdr
;
13611 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
13614 if (dynsymcount
<= 0)
13617 if (!bed
->plt_sym_val
)
13620 relplt_name
= bed
->relplt_name
;
13621 if (relplt_name
== NULL
)
13622 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
13623 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
13624 if (relplt
== NULL
)
13627 hdr
= &elf_section_data (relplt
)->this_hdr
;
13628 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
13629 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
13632 plt
= bfd_get_section_by_name (abfd
, ".plt");
13636 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
13637 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, true))
13640 count
= NUM_SHDR_ENTRIES (hdr
);
13641 size
= count
* sizeof (asymbol
);
13642 p
= relplt
->relocation
;
13643 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13645 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
13646 if (p
->addend
!= 0)
13649 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
13651 size
+= sizeof ("+0x") - 1 + 8;
13656 s
= *ret
= (asymbol
*) bfd_malloc (size
);
13660 names
= (char *) (s
+ count
);
13661 p
= relplt
->relocation
;
13663 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13668 addr
= bed
->plt_sym_val (i
, plt
, p
);
13669 if (addr
== (bfd_vma
) -1)
13672 *s
= **p
->sym_ptr_ptr
;
13673 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13674 we are defining a symbol, ensure one of them is set. */
13675 if ((s
->flags
& BSF_LOCAL
) == 0)
13676 s
->flags
|= BSF_GLOBAL
;
13677 s
->flags
|= BSF_SYNTHETIC
;
13679 s
->value
= addr
- plt
->vma
;
13682 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
13683 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
13685 if (p
->addend
!= 0)
13689 memcpy (names
, "+0x", sizeof ("+0x") - 1);
13690 names
+= sizeof ("+0x") - 1;
13691 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
13692 for (a
= buf
; *a
== '0'; ++a
)
13695 memcpy (names
, a
, len
);
13698 memcpy (names
, "@plt", sizeof ("@plt"));
13699 names
+= sizeof ("@plt");
13706 /* It is only used by x86-64 so far.
13707 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13708 but current usage would allow all of _bfd_std_section to be zero. */
13709 static const asymbol lcomm_sym
13710 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
13711 asection _bfd_elf_large_com_section
13712 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
13713 "LARGE_COMMON", 0, SEC_IS_COMMON
);
13716 _bfd_elf_final_write_processing (bfd
*abfd
)
13718 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
13720 i_ehdrp
= elf_elfheader (abfd
);
13722 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13723 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
13725 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13726 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13727 or STB_GNU_UNIQUE binding. */
13728 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
13730 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13731 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
13732 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
13733 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
13735 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
13736 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13737 "and FreeBSD targets"));
13738 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
13739 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13740 "only by GNU and FreeBSD targets"));
13741 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
13742 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13743 "only by GNU and FreeBSD targets"));
13744 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
13745 _bfd_error_handler (_("GNU_RETAIN section is supported "
13746 "only by GNU and FreeBSD targets"));
13747 bfd_set_error (bfd_error_sorry
);
13755 /* Return TRUE for ELF symbol types that represent functions.
13756 This is the default version of this function, which is sufficient for
13757 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13760 _bfd_elf_is_function_type (unsigned int type
)
13762 return (type
== STT_FUNC
13763 || type
== STT_GNU_IFUNC
);
13766 /* If the ELF symbol SYM might be a function in SEC, return the
13767 function size and set *CODE_OFF to the function's entry point,
13768 otherwise return zero. */
13771 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
13774 bfd_size_type size
;
13775 elf_symbol_type
* elf_sym
= (elf_symbol_type
*) sym
;
13777 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
13778 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
13779 || sym
->section
!= sec
)
13782 size
= (sym
->flags
& BSF_SYNTHETIC
) ? 0 : elf_sym
->internal_elf_sym
.st_size
;
13784 /* In theory we should check that the symbol's type satisfies
13785 _bfd_elf_is_function_type(), but there are some function-like
13786 symbols which would fail this test. (eg _start). Instead
13787 we check for hidden, local, notype symbols with zero size.
13788 This type of symbol is generated by the annobin plugin for gcc
13789 and clang, and should not be considered to be a function symbol. */
13791 && ((sym
->flags
& (BSF_SYNTHETIC
| BSF_LOCAL
)) == BSF_LOCAL
)
13792 && ELF_ST_TYPE (elf_sym
->internal_elf_sym
.st_info
) == STT_NOTYPE
13793 && ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
) == STV_HIDDEN
)
13796 *code_off
= sym
->value
;
13797 /* Do not return 0 for the function's size. */
13798 return size
? size
: 1;
13801 /* Set to non-zero to enable some debug messages. */
13802 #define DEBUG_SECONDARY_RELOCS 0
13804 /* An internal-to-the-bfd-library only section type
13805 used to indicate a cached secondary reloc section. */
13806 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13808 /* Create a BFD section to hold a secondary reloc section. */
13811 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
13812 Elf_Internal_Shdr
*hdr
,
13814 unsigned int shindex
)
13816 /* We only support RELA secondary relocs. */
13817 if (hdr
->sh_type
!= SHT_RELA
)
13820 #if DEBUG_SECONDARY_RELOCS
13821 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
13823 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
13824 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
13827 /* Read in any secondary relocs associated with SEC. */
13830 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
13832 asymbol
** symbols
,
13835 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
13837 bool result
= true;
13838 bfd_vma (*r_sym
) (bfd_vma
);
13839 ufile_ptr filesize
;
13841 #if BFD_DEFAULT_TARGET_SIZE > 32
13842 if (bfd_arch_bits_per_address (abfd
) != 32)
13843 r_sym
= elf64_r_sym
;
13846 r_sym
= elf32_r_sym
;
13848 if (!elf_section_data (sec
)->has_secondary_relocs
)
13851 /* Discover if there are any secondary reloc sections
13852 associated with SEC. */
13853 filesize
= bfd_get_file_size (abfd
);
13854 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
13856 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
13858 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
13859 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
13860 && (hdr
->sh_entsize
== ebd
->s
->sizeof_rel
13861 || hdr
->sh_entsize
== ebd
->s
->sizeof_rela
))
13863 bfd_byte
* native_relocs
;
13864 bfd_byte
* native_reloc
;
13865 arelent
* internal_relocs
;
13866 arelent
* internal_reloc
;
13868 unsigned int entsize
;
13869 unsigned int symcount
;
13870 bfd_size_type reloc_count
;
13873 if (ebd
->elf_info_to_howto
== NULL
)
13876 #if DEBUG_SECONDARY_RELOCS
13877 fprintf (stderr
, "read secondary relocs for %s from %s\n",
13878 sec
->name
, relsec
->name
);
13880 entsize
= hdr
->sh_entsize
;
13883 && ((ufile_ptr
) hdr
->sh_offset
> filesize
13884 || hdr
->sh_size
> filesize
- hdr
->sh_offset
))
13886 bfd_set_error (bfd_error_file_truncated
);
13891 native_relocs
= bfd_malloc (hdr
->sh_size
);
13892 if (native_relocs
== NULL
)
13898 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
13899 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
13901 free (native_relocs
);
13902 bfd_set_error (bfd_error_file_too_big
);
13907 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
13908 if (internal_relocs
== NULL
)
13910 free (native_relocs
);
13915 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
13916 || bfd_read (native_relocs
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
13918 free (native_relocs
);
13919 /* The internal_relocs will be freed when
13920 the memory for the bfd is released. */
13926 symcount
= bfd_get_dynamic_symcount (abfd
);
13928 symcount
= bfd_get_symcount (abfd
);
13930 for (i
= 0, internal_reloc
= internal_relocs
,
13931 native_reloc
= native_relocs
;
13933 i
++, internal_reloc
++, native_reloc
+= entsize
)
13936 Elf_Internal_Rela rela
;
13938 if (entsize
== ebd
->s
->sizeof_rel
)
13939 ebd
->s
->swap_reloc_in (abfd
, native_reloc
, & rela
);
13940 else /* entsize == ebd->s->sizeof_rela */
13941 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
13943 /* The address of an ELF reloc is section relative for an object
13944 file, and absolute for an executable file or shared library.
13945 The address of a normal BFD reloc is always section relative,
13946 and the address of a dynamic reloc is absolute.. */
13947 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
13948 internal_reloc
->address
= rela
.r_offset
;
13950 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
13952 if (r_sym (rela
.r_info
) == STN_UNDEF
)
13954 /* FIXME: This and the error case below mean that we
13955 have a symbol on relocs that is not elf_symbol_type. */
13956 internal_reloc
->sym_ptr_ptr
=
13957 bfd_abs_section_ptr
->symbol_ptr_ptr
;
13959 else if (r_sym (rela
.r_info
) > symcount
)
13962 /* xgettext:c-format */
13963 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13964 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
13965 bfd_set_error (bfd_error_bad_value
);
13966 internal_reloc
->sym_ptr_ptr
=
13967 bfd_abs_section_ptr
->symbol_ptr_ptr
;
13974 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
13975 internal_reloc
->sym_ptr_ptr
= ps
;
13976 /* Make sure that this symbol is not removed by strip. */
13977 (*ps
)->flags
|= BSF_KEEP
;
13980 internal_reloc
->addend
= rela
.r_addend
;
13982 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
13983 if (! res
|| internal_reloc
->howto
== NULL
)
13985 #if DEBUG_SECONDARY_RELOCS
13987 "there is no howto associated with reloc %lx\n",
13994 free (native_relocs
);
13995 /* Store the internal relocs. */
13996 elf_section_data (relsec
)->sec_info
= internal_relocs
;
14003 /* Set the ELF section header fields of an output secondary reloc section. */
14006 _bfd_elf_copy_special_section_fields (const bfd
*ibfd ATTRIBUTE_UNUSED
,
14007 bfd
*obfd ATTRIBUTE_UNUSED
,
14008 const Elf_Internal_Shdr
*isection
,
14009 Elf_Internal_Shdr
*osection
)
14013 struct bfd_elf_section_data
* esd
;
14015 if (isection
== NULL
)
14018 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
14021 isec
= isection
->bfd_section
;
14025 osec
= osection
->bfd_section
;
14029 esd
= elf_section_data (osec
);
14030 BFD_ASSERT (esd
->sec_info
== NULL
);
14031 esd
->sec_info
= elf_section_data (isec
)->sec_info
;
14032 osection
->sh_type
= SHT_RELA
;
14033 osection
->sh_link
= elf_onesymtab (obfd
);
14034 if (osection
->sh_link
== 0)
14036 /* There is no symbol table - we are hosed... */
14038 /* xgettext:c-format */
14039 (_("%pB(%pA): link section cannot be set"
14040 " because the output file does not have a symbol table"),
14042 bfd_set_error (bfd_error_bad_value
);
14046 /* Find the output section that corresponds to the isection's
14048 if (isection
->sh_info
== 0
14049 || isection
->sh_info
>= elf_numsections (ibfd
))
14052 /* xgettext:c-format */
14053 (_("%pB(%pA): info section index is invalid"),
14055 bfd_set_error (bfd_error_bad_value
);
14059 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
14061 if (isection
== NULL
14062 || isection
->bfd_section
== NULL
14063 || isection
->bfd_section
->output_section
== NULL
)
14066 /* xgettext:c-format */
14067 (_("%pB(%pA): info section index cannot be set"
14068 " because the section is not in the output"),
14070 bfd_set_error (bfd_error_bad_value
);
14074 esd
= elf_section_data (isection
->bfd_section
->output_section
);
14075 BFD_ASSERT (esd
!= NULL
);
14076 osection
->sh_info
= esd
->this_idx
;
14077 esd
->has_secondary_relocs
= true;
14078 #if DEBUG_SECONDARY_RELOCS
14079 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
14080 osec
->name
, osection
->sh_link
, osection
->sh_info
);
14081 fprintf (stderr
, "mark section %s as having secondary relocs\n",
14082 bfd_section_name (isection
->bfd_section
->output_section
));
14088 /* Write out a secondary reloc section.
14090 FIXME: Currently this function can result in a serious performance penalty
14091 for files with secondary relocs and lots of sections. The proper way to
14092 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14093 relocs together and then to have this function just walk that chain. */
14096 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
14098 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
14099 bfd_vma addr_offset
;
14101 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
14102 bool result
= true;
14107 #if BFD_DEFAULT_TARGET_SIZE > 32
14108 if (bfd_arch_bits_per_address (abfd
) != 32)
14109 r_info
= elf64_r_info
;
14112 r_info
= elf32_r_info
;
14114 /* The address of an ELF reloc is section relative for an object
14115 file, and absolute for an executable file or shared library.
14116 The address of a BFD reloc is always section relative. */
14118 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
14119 addr_offset
= sec
->vma
;
14121 /* Discover if there are any secondary reloc sections
14122 associated with SEC. */
14123 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
14125 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
14126 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
14128 if (hdr
->sh_type
== SHT_RELA
14129 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
14131 asymbol
* last_sym
;
14133 size_t reloc_count
;
14135 bfd_size_type entsize
;
14136 arelent
* src_irel
;
14137 bfd_byte
* dst_rela
;
14139 if (hdr
->contents
!= NULL
)
14142 /* xgettext:c-format */
14143 (_("%pB(%pA): error: secondary reloc section processed twice"),
14145 bfd_set_error (bfd_error_bad_value
);
14150 entsize
= hdr
->sh_entsize
;
14154 /* xgettext:c-format */
14155 (_("%pB(%pA): error: secondary reloc section"
14156 " has zero sized entries"),
14158 bfd_set_error (bfd_error_bad_value
);
14162 else if (entsize
!= ebd
->s
->sizeof_rel
14163 && entsize
!= ebd
->s
->sizeof_rela
)
14166 /* xgettext:c-format */
14167 (_("%pB(%pA): error: secondary reloc section"
14168 " has non-standard sized entries"),
14170 bfd_set_error (bfd_error_bad_value
);
14175 reloc_count
= hdr
->sh_size
/ entsize
;
14176 hdr
->sh_size
= entsize
* reloc_count
;
14177 if (reloc_count
== 0)
14180 /* xgettext:c-format */
14181 (_("%pB(%pA): error: secondary reloc section is empty!"),
14183 bfd_set_error (bfd_error_bad_value
);
14188 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
14189 if (hdr
->contents
== NULL
)
14192 #if DEBUG_SECONDARY_RELOCS
14193 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
14194 reloc_count
, sec
->name
, relsec
->name
);
14198 dst_rela
= hdr
->contents
;
14199 src_irel
= (arelent
*) esd
->sec_info
;
14200 if (src_irel
== NULL
)
14203 /* xgettext:c-format */
14204 (_("%pB(%pA): error: internal relocs missing"
14205 " for secondary reloc section"),
14207 bfd_set_error (bfd_error_bad_value
);
14212 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= entsize
)
14214 Elf_Internal_Rela src_rela
;
14219 ptr
= src_irel
+ idx
;
14223 /* xgettext:c-format */
14224 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14225 abfd
, relsec
, idx
);
14226 bfd_set_error (bfd_error_bad_value
);
14231 if (ptr
->sym_ptr_ptr
== NULL
)
14233 /* FIXME: Is this an error ? */
14238 sym
= *ptr
->sym_ptr_ptr
;
14240 if (sym
== last_sym
)
14244 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
14248 /* xgettext:c-format */
14249 (_("%pB(%pA): error: secondary reloc %zu"
14250 " references a missing symbol"),
14251 abfd
, relsec
, idx
);
14252 bfd_set_error (bfd_error_bad_value
);
14261 if (sym
->the_bfd
!= NULL
14262 && sym
->the_bfd
->xvec
!= abfd
->xvec
14263 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
14266 /* xgettext:c-format */
14267 (_("%pB(%pA): error: secondary reloc %zu"
14268 " references a deleted symbol"),
14269 abfd
, relsec
, idx
);
14270 bfd_set_error (bfd_error_bad_value
);
14276 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
14277 if (ptr
->howto
== NULL
)
14280 /* xgettext:c-format */
14281 (_("%pB(%pA): error: secondary reloc %zu"
14282 " is of an unknown type"),
14283 abfd
, relsec
, idx
);
14284 bfd_set_error (bfd_error_bad_value
);
14286 src_rela
.r_info
= r_info (0, 0);
14289 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
14290 src_rela
.r_addend
= ptr
->addend
;
14292 if (entsize
== ebd
->s
->sizeof_rel
)
14293 ebd
->s
->swap_reloc_out (abfd
, &src_rela
, dst_rela
);
14294 else /* entsize == ebd->s->sizeof_rela */
14295 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);