1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2024 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bool assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bool swap_out_syms (bfd
*, struct elf_strtab_hash
**, int,
55 struct bfd_link_info
*);
56 static bool elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
57 file_ptr offset
, size_t align
);
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
201 for (const unsigned char *name
= (const unsigned char *) namearg
;
204 h
= (h
<< 4) + *name
;
205 h
^= (h
>> 24) & 0xf0;
207 return h
& 0x0fffffff;
210 /* DT_GNU_HASH hash function. Do not change this function; you will
211 cause invalid hash tables to be generated. */
214 bfd_elf_gnu_hash (const char *namearg
)
218 for (const unsigned char *name
= (const unsigned char *) namearg
;
220 h
= (h
<< 5) + h
+ *name
;
224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
227 bfd_elf_allocate_object (bfd
*abfd
,
229 enum elf_target_id object_id
)
231 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
232 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
233 if (abfd
->tdata
.any
== NULL
)
236 elf_object_id (abfd
) = object_id
;
237 if (abfd
->direction
!= read_direction
)
239 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
242 elf_tdata (abfd
)->o
= o
;
243 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
250 bfd_elf_make_object (bfd
*abfd
)
252 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
253 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
258 bfd_elf_mkcorefile (bfd
*abfd
)
260 /* I think this can be done just like an object file. */
261 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
263 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
264 return elf_tdata (abfd
)->core
!= NULL
;
268 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
270 Elf_Internal_Shdr
**i_shdrp
;
271 bfd_byte
*shstrtab
= NULL
;
273 bfd_size_type shstrtabsize
;
275 i_shdrp
= elf_elfsections (abfd
);
277 || shindex
>= elf_numsections (abfd
)
278 || i_shdrp
[shindex
] == 0)
281 shstrtab
= i_shdrp
[shindex
]->contents
;
282 if (shstrtab
== NULL
)
284 /* No cached one, attempt to read, and cache what we read. */
285 offset
= i_shdrp
[shindex
]->sh_offset
;
286 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize
+ 1 <= 1
291 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
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
+ 1, dt_strsz
);
2108 /* Since this is a string table, make sure that it is terminated. */
2109 strbuf
[dt_strsz
] = 0;
2111 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
2112 DT_HASH since it is simpler than DT_GNU_HASH. */
2115 unsigned char nb
[16];
2116 unsigned int hash_ent_size
;
2118 switch (bed
->elf_machine_code
)
2123 if (bed
->s
->elfclass
== ELFCLASS64
)
2134 filepos
= offset_from_vma (phdrs
, phnum
, dt_hash
, sizeof (nb
),
2136 if (filepos
== (file_ptr
) -1
2137 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
2138 || bfd_read (nb
, 2 * hash_ent_size
, abfd
) != 2 * hash_ent_size
)
2141 /* The number of dynamic symbol table entries equals the number
2143 if (hash_ent_size
== 8)
2144 symcount
= bfd_get_64 (abfd
, nb
+ hash_ent_size
);
2146 symcount
= bfd_get_32 (abfd
, nb
+ hash_ent_size
);
2150 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2151 bindings are in hash table. Since in dynamic symbol table,
2152 all symbols with STB_LOCAL binding are placed before symbols
2153 with other bindings and all undefined symbols are placed
2154 before defined ones, the highest symbol index in DT_GNU_HASH
2155 is the highest dynamic symbol table index. */
2156 unsigned char nb
[16];
2157 bfd_vma ngnubuckets
;
2159 size_t i
, ngnuchains
;
2160 bfd_vma maxchain
= 0xffffffff, bitmaskwords
;
2161 bfd_vma buckets_vma
;
2163 filepos
= offset_from_vma (phdrs
, phnum
, dt_gnu_hash
,
2165 if (filepos
== (file_ptr
) -1
2166 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
2167 || bfd_read (nb
, sizeof (nb
), abfd
) != sizeof (nb
))
2170 ngnubuckets
= bfd_get_32 (abfd
, nb
);
2171 gnusymidx
= bfd_get_32 (abfd
, nb
+ 4);
2172 bitmaskwords
= bfd_get_32 (abfd
, nb
+ 8);
2173 buckets_vma
= dt_gnu_hash
+ 16;
2174 if (bed
->s
->elfclass
== ELFCLASS32
)
2175 buckets_vma
+= bitmaskwords
* 4;
2177 buckets_vma
+= bitmaskwords
* 8;
2178 filepos
= offset_from_vma (phdrs
, phnum
, buckets_vma
, 4, NULL
);
2179 if (filepos
== (file_ptr
) -1
2180 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2183 gnubuckets
= get_hash_table_data (abfd
, ngnubuckets
, 4, filesize
);
2184 if (gnubuckets
== NULL
)
2187 for (i
= 0; i
< ngnubuckets
; i
++)
2188 if (gnubuckets
[i
] != 0)
2190 if (gnubuckets
[i
] < gnusymidx
)
2193 if (maxchain
== 0xffffffff || gnubuckets
[i
] > maxchain
)
2194 maxchain
= gnubuckets
[i
];
2197 if (maxchain
== 0xffffffff)
2200 goto empty_gnu_hash
;
2203 maxchain
-= gnusymidx
;
2204 filepos
= offset_from_vma (phdrs
, phnum
,
2206 4 * (ngnubuckets
+ maxchain
)),
2208 if (filepos
== (file_ptr
) -1
2209 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2214 if (bfd_read (nb
, 4, abfd
) != 4)
2220 while ((bfd_get_32 (abfd
, nb
) & 1) == 0);
2222 filepos
= offset_from_vma (phdrs
, phnum
,
2223 (buckets_vma
+ 4 * ngnubuckets
),
2225 if (filepos
== (file_ptr
) -1
2226 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2229 gnuchains
= get_hash_table_data (abfd
, maxchain
, 4, filesize
);
2230 if (gnuchains
== NULL
)
2232 ngnuchains
= maxchain
;
2236 filepos
= offset_from_vma (phdrs
, phnum
,
2238 + 4 * (ngnubuckets
+ maxchain
)),
2240 if (filepos
== (file_ptr
) -1
2241 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2244 mipsxlat
= get_hash_table_data (abfd
, maxchain
, 4, filesize
);
2245 if (mipsxlat
== NULL
)
2250 for (i
= 0; i
< ngnubuckets
; ++i
)
2251 if (gnubuckets
[i
] != 0)
2253 bfd_vma si
= gnubuckets
[i
];
2254 bfd_vma off
= si
- gnusymidx
;
2259 if (mipsxlat
[off
] >= symcount
)
2260 symcount
= mipsxlat
[off
] + 1;
2269 while (off
< ngnuchains
&& (gnuchains
[off
++] & 1) == 0);
2273 /* Swap in dynamic symbol table. */
2274 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
2276 bfd_set_error (bfd_error_file_too_big
);
2280 filepos
= offset_from_vma (phdrs
, phnum
, dt_symtab
, amt
, NULL
);
2281 if (filepos
== (file_ptr
) -1
2282 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2284 esymbuf
= _bfd_malloc_and_read (abfd
, amt
, amt
);
2285 if (esymbuf
== NULL
)
2288 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
2290 bfd_set_error (bfd_error_file_too_big
);
2294 /* Dynamic symbol table must be valid until ABFD is closed. */
2295 isymbuf
= (Elf_Internal_Sym
*) bfd_alloc (abfd
, amt
);
2296 if (isymbuf
== NULL
)
2299 swap_symbol_in
= bed
->s
->swap_symbol_in
;
2301 /* Convert the symbols to internal form. */
2302 isymend
= isymbuf
+ symcount
;
2303 for (esym
= esymbuf
, isym
= isymbuf
;
2305 esym
+= extsym_size
, isym
++)
2306 if (!swap_symbol_in (abfd
, esym
, NULL
, isym
)
2307 || isym
->st_name
>= dt_strsz
)
2309 bfd_set_error (bfd_error_invalid_operation
);
2315 /* Swap in DT_VERSYM. */
2316 if (_bfd_mul_overflow (symcount
, 2, &amt
))
2318 bfd_set_error (bfd_error_file_too_big
);
2322 filepos
= offset_from_vma (phdrs
, phnum
, dt_versym
, amt
, NULL
);
2323 if (filepos
== (file_ptr
) -1
2324 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2327 /* DT_VERSYM info must be valid until ABFD is closed. */
2328 versym
= _bfd_alloc_and_read (abfd
, amt
, amt
);
2332 /* Read in DT_VERDEF. */
2333 filepos
= offset_from_vma (phdrs
, phnum
, dt_verdef
,
2335 if (filepos
== (file_ptr
) -1
2336 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2339 /* DT_VERDEF info must be valid until ABFD is closed. */
2340 verdef
= _bfd_alloc_and_read (abfd
, verdef_size
,
2346 /* Read in DT_VERNEED. */
2347 filepos
= offset_from_vma (phdrs
, phnum
, dt_verneed
,
2349 if (filepos
== (file_ptr
) -1
2350 || bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
2353 /* DT_VERNEED info must be valid until ABFD is closed. */
2354 verneed
= _bfd_alloc_and_read (abfd
, verneed_size
,
2360 elf_tdata (abfd
)->dt_strtab
= strbuf
;
2361 elf_tdata (abfd
)->dt_strsz
= dt_strsz
;
2362 elf_tdata (abfd
)->dt_symtab
= isymbuf
;
2363 elf_tdata (abfd
)->dt_symtab_count
= symcount
;
2364 elf_tdata (abfd
)->dt_versym
= versym
;
2365 elf_tdata (abfd
)->dt_verdef
= verdef
;
2366 elf_tdata (abfd
)->dt_verneed
= verneed
;
2367 elf_tdata (abfd
)->dt_verdef_count
2368 = verdef_size
/ sizeof (Elf_External_Verdef
);
2369 elf_tdata (abfd
)->dt_verneed_count
2370 = verneed_size
/ sizeof (Elf_External_Verneed
);
2375 /* Restore file position for elf_object_p. */
2376 if (bfd_seek (abfd
, saved_filepos
, SEEK_SET
) != 0)
2386 /* Reconstruct section from dynamic symbol. */
2389 _bfd_elf_get_section_from_dynamic_symbol (bfd
*abfd
,
2390 Elf_Internal_Sym
*isym
)
2395 if (!elf_use_dt_symtab_p (abfd
))
2398 flags
= SEC_ALLOC
| SEC_LOAD
;
2399 switch (ELF_ST_TYPE (isym
->st_info
))
2403 sec
= bfd_get_section_by_name (abfd
, ".text");
2405 sec
= bfd_make_section_with_flags (abfd
,
2410 sec
= bfd_com_section_ptr
;
2413 sec
= bfd_get_section_by_name (abfd
, ".data");
2415 sec
= bfd_make_section_with_flags (abfd
,
2420 sec
= bfd_get_section_by_name (abfd
, ".tdata");
2422 sec
= bfd_make_section_with_flags (abfd
,
2426 | SEC_THREAD_LOCAL
));
2429 sec
= bfd_abs_section_ptr
;
2436 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2437 and return symbol version for symbol version itself. */
2440 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
2444 const char *version_string
= NULL
;
2445 if ((elf_dynversym (abfd
) != 0
2446 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
2447 || (elf_tdata (abfd
)->dt_versym
!= NULL
2448 && (elf_tdata (abfd
)->dt_verdef
!= NULL
2449 || elf_tdata (abfd
)->dt_verneed
!= NULL
)))
2451 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
2453 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
2454 vernum
&= VERSYM_VERSION
;
2457 version_string
= "";
2458 else if (vernum
== 1
2459 && (vernum
> elf_tdata (abfd
)->cverdefs
2460 || (elf_tdata (abfd
)->verdef
[0].vd_flags
2462 version_string
= base_p
? "Base" : "";
2463 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
2465 const char *nodename
2466 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
2467 version_string
= "";
2470 || symbol
->name
== NULL
2471 || strcmp (symbol
->name
, nodename
) != 0)
2472 version_string
= nodename
;
2476 Elf_Internal_Verneed
*t
;
2478 version_string
= _("<corrupt>");
2479 for (t
= elf_tdata (abfd
)->verref
;
2483 Elf_Internal_Vernaux
*a
;
2485 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2487 if (a
->vna_other
== vernum
)
2490 version_string
= a
->vna_nodename
;
2497 return version_string
;
2500 /* Display ELF-specific fields of a symbol. */
2503 bfd_elf_print_symbol (bfd
*abfd
,
2506 bfd_print_symbol_type how
)
2508 FILE *file
= (FILE *) filep
;
2511 case bfd_print_symbol_name
:
2512 fprintf (file
, "%s", symbol
->name
);
2514 case bfd_print_symbol_more
:
2515 fprintf (file
, "elf ");
2516 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
2517 fprintf (file
, " %x", symbol
->flags
);
2519 case bfd_print_symbol_all
:
2521 const char *section_name
;
2522 const char *name
= NULL
;
2523 const struct elf_backend_data
*bed
;
2524 unsigned char st_other
;
2526 const char *version_string
;
2529 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2531 bed
= get_elf_backend_data (abfd
);
2532 if (bed
->elf_backend_print_symbol_all
)
2533 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
2537 name
= symbol
->name
;
2538 bfd_print_symbol_vandf (abfd
, file
, symbol
);
2541 fprintf (file
, " %s\t", section_name
);
2542 /* Print the "other" value for a symbol. For common symbols,
2543 we've already printed the size; now print the alignment.
2544 For other symbols, we have no specified alignment, and
2545 we've printed the address; now print the size. */
2546 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
2547 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
2549 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
2550 bfd_fprintf_vma (abfd
, file
, val
);
2552 /* If we have version information, print it. */
2553 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2560 fprintf (file
, " %-11s", version_string
);
2565 fprintf (file
, " (%s)", version_string
);
2566 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2571 /* If the st_other field is not zero, print it. */
2572 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2577 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2578 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2579 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2581 /* Some other non-defined flags are also present, so print
2583 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2586 fprintf (file
, " %s", name
);
2592 /* ELF .o/exec file reading */
2594 /* Create a new bfd section from an ELF section header. */
2597 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2599 Elf_Internal_Shdr
*hdr
;
2600 Elf_Internal_Ehdr
*ehdr
;
2601 const struct elf_backend_data
*bed
;
2605 if (shindex
>= elf_numsections (abfd
))
2608 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2609 sh_link or sh_info. Detect this here, by refusing to load a
2610 section that we are already in the process of loading. */
2611 if (elf_tdata (abfd
)->being_created
[shindex
])
2614 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2617 elf_tdata (abfd
)->being_created
[shindex
] = true;
2619 hdr
= elf_elfsections (abfd
)[shindex
];
2620 ehdr
= elf_elfheader (abfd
);
2621 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2626 bed
= get_elf_backend_data (abfd
);
2627 switch (hdr
->sh_type
)
2630 /* Inactive section. Throw it away. */
2633 case SHT_PROGBITS
: /* Normal section with contents. */
2634 case SHT_NOBITS
: /* .bss section. */
2635 case SHT_HASH
: /* .hash section. */
2636 case SHT_NOTE
: /* .note section. */
2637 case SHT_INIT_ARRAY
: /* .init_array section. */
2638 case SHT_FINI_ARRAY
: /* .fini_array section. */
2639 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2640 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2641 case SHT_GNU_HASH
: /* .gnu.hash section. */
2642 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2645 case SHT_DYNAMIC
: /* Dynamic linking information. */
2646 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2649 if (hdr
->sh_link
> elf_numsections (abfd
))
2651 /* PR 10478: Accept Solaris binaries with a sh_link field
2652 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2653 switch (bfd_get_arch (abfd
))
2656 case bfd_arch_sparc
:
2657 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff)
2658 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff))
2660 /* Otherwise fall through. */
2665 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2667 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2669 Elf_Internal_Shdr
*dynsymhdr
;
2671 /* The shared libraries distributed with hpux11 have a bogus
2672 sh_link field for the ".dynamic" section. Find the
2673 string table for the ".dynsym" section instead. */
2674 if (elf_dynsymtab (abfd
) != 0)
2676 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2677 hdr
->sh_link
= dynsymhdr
->sh_link
;
2681 unsigned int i
, num_sec
;
2683 num_sec
= elf_numsections (abfd
);
2684 for (i
= 1; i
< num_sec
; i
++)
2686 dynsymhdr
= elf_elfsections (abfd
)[i
];
2687 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2689 hdr
->sh_link
= dynsymhdr
->sh_link
;
2697 case SHT_SYMTAB
: /* A symbol table. */
2698 if (elf_onesymtab (abfd
) == shindex
)
2701 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2704 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2706 if (hdr
->sh_size
!= 0)
2708 /* Some assemblers erroneously set sh_info to one with a
2709 zero sh_size. ld sees this as a global symbol count
2710 of (unsigned) -1. Fix it here. */
2715 /* PR 18854: A binary might contain more than one symbol table.
2716 Unusual, but possible. Warn, but continue. */
2717 if (elf_onesymtab (abfd
) != 0)
2720 /* xgettext:c-format */
2721 (_("%pB: warning: multiple symbol tables detected"
2722 " - ignoring the table in section %u"),
2726 elf_onesymtab (abfd
) = shindex
;
2727 elf_symtab_hdr (abfd
) = *hdr
;
2728 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2729 abfd
->flags
|= HAS_SYMS
;
2731 /* Sometimes a shared object will map in the symbol table. If
2732 SHF_ALLOC is set, and this is a shared object, then we also
2733 treat this section as a BFD section. We can not base the
2734 decision purely on SHF_ALLOC, because that flag is sometimes
2735 set in a relocatable object file, which would confuse the
2737 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2738 && (abfd
->flags
& DYNAMIC
) != 0
2739 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2743 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2744 can't read symbols without that section loaded as well. It
2745 is most likely specified by the next section header. */
2747 elf_section_list
* entry
;
2748 unsigned int i
, num_sec
;
2750 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2751 if (entry
->hdr
.sh_link
== shindex
)
2754 num_sec
= elf_numsections (abfd
);
2755 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2757 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2759 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2760 && hdr2
->sh_link
== shindex
)
2765 for (i
= 1; i
< shindex
; i
++)
2767 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2769 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2770 && hdr2
->sh_link
== shindex
)
2775 ret
= bfd_section_from_shdr (abfd
, i
);
2776 /* else FIXME: we have failed to find the symbol table.
2777 Should we issue an error? */
2781 case SHT_DYNSYM
: /* A dynamic symbol table. */
2782 if (elf_dynsymtab (abfd
) == shindex
)
2785 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2788 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2790 if (hdr
->sh_size
!= 0)
2793 /* Some linkers erroneously set sh_info to one with a
2794 zero sh_size. ld sees this as a global symbol count
2795 of (unsigned) -1. Fix it here. */
2800 /* PR 18854: A binary might contain more than one dynamic symbol table.
2801 Unusual, but possible. Warn, but continue. */
2802 if (elf_dynsymtab (abfd
) != 0)
2805 /* xgettext:c-format */
2806 (_("%pB: warning: multiple dynamic symbol tables detected"
2807 " - ignoring the table in section %u"),
2811 elf_dynsymtab (abfd
) = shindex
;
2812 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2813 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2814 abfd
->flags
|= HAS_SYMS
;
2816 /* Besides being a symbol table, we also treat this as a regular
2817 section, so that objcopy can handle it. */
2818 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2821 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2823 elf_section_list
* entry
;
2825 for (entry
= elf_symtab_shndx_list (abfd
); entry
; entry
= entry
->next
)
2826 if (entry
->ndx
== shindex
)
2829 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2832 entry
->ndx
= shindex
;
2834 entry
->next
= elf_symtab_shndx_list (abfd
);
2835 elf_symtab_shndx_list (abfd
) = entry
;
2836 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2840 case SHT_STRTAB
: /* A string table. */
2841 if (hdr
->bfd_section
!= NULL
)
2844 if (ehdr
->e_shstrndx
== shindex
)
2846 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2847 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2851 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2854 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2855 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2859 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2862 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2863 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2864 elf_elfsections (abfd
)[shindex
] = hdr
;
2865 /* We also treat this as a regular section, so that objcopy
2867 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2872 /* If the string table isn't one of the above, then treat it as a
2873 regular section. We need to scan all the headers to be sure,
2874 just in case this strtab section appeared before the above. */
2875 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2877 unsigned int i
, num_sec
;
2879 num_sec
= elf_numsections (abfd
);
2880 for (i
= 1; i
< num_sec
; i
++)
2882 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2883 if (hdr2
->sh_link
== shindex
)
2885 /* Prevent endless recursion on broken objects. */
2888 if (! bfd_section_from_shdr (abfd
, i
))
2890 if (elf_onesymtab (abfd
) == i
)
2892 if (elf_dynsymtab (abfd
) == i
)
2893 goto dynsymtab_strtab
;
2897 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2903 /* *These* do a lot of work -- but build no sections! */
2905 asection
*target_sect
;
2906 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2907 unsigned int num_sec
= elf_numsections (abfd
);
2908 struct bfd_elf_section_data
*esdt
;
2911 if (hdr
->sh_type
== SHT_REL
)
2912 size
= bed
->s
->sizeof_rel
;
2913 else if (hdr
->sh_type
== SHT_RELA
)
2914 size
= bed
->s
->sizeof_rela
;
2916 size
= bed
->s
->arch_size
/ 8;
2917 if (hdr
->sh_entsize
!= size
)
2920 /* Check for a bogus link to avoid crashing. */
2921 if (hdr
->sh_link
>= num_sec
)
2924 /* xgettext:c-format */
2925 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2926 abfd
, hdr
->sh_link
, name
, shindex
);
2927 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2931 /* Get the symbol table. */
2932 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2933 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2934 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2937 /* If this is an alloc section in an executable or shared
2938 library, or the reloc section does not use the main symbol
2939 table we don't treat it as a reloc section. BFD can't
2940 adequately represent such a section, so at least for now,
2941 we don't try. We just present it as a normal section. We
2942 also can't use it as a reloc section if it points to the
2943 null section, an invalid section, another reloc section, or
2944 its sh_link points to the null section. */
2945 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2946 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2947 || (hdr
->sh_flags
& SHF_COMPRESSED
) != 0
2948 || hdr
->sh_type
== SHT_RELR
2949 || hdr
->sh_link
== SHN_UNDEF
2950 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2951 || hdr
->sh_info
== SHN_UNDEF
2952 || hdr
->sh_info
>= num_sec
2953 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2954 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2956 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2960 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2963 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2964 if (target_sect
== NULL
)
2967 esdt
= elf_section_data (target_sect
);
2968 if (hdr
->sh_type
== SHT_RELA
)
2969 p_hdr
= &esdt
->rela
.hdr
;
2971 p_hdr
= &esdt
->rel
.hdr
;
2973 /* PR 17512: file: 0b4f81b7.
2974 Also see PR 24456, for a file which deliberately has two reloc
2978 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2981 /* xgettext:c-format */
2982 (_("%pB: warning: secondary relocation section '%s' "
2983 "for section %pA found - ignoring"),
2984 abfd
, name
, target_sect
);
2987 esdt
->has_secondary_relocs
= true;
2991 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2996 elf_elfsections (abfd
)[shindex
] = hdr2
;
2997 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2998 * bed
->s
->int_rels_per_ext_rel
);
2999 target_sect
->flags
|= SEC_RELOC
;
3000 target_sect
->relocation
= NULL
;
3001 target_sect
->rel_filepos
= hdr
->sh_offset
;
3002 /* In the section to which the relocations apply, mark whether
3003 its relocations are of the REL or RELA variety. */
3004 if (hdr
->sh_size
!= 0)
3006 if (hdr
->sh_type
== SHT_RELA
)
3007 target_sect
->use_rela_p
= 1;
3009 abfd
->flags
|= HAS_RELOC
;
3013 case SHT_GNU_verdef
:
3014 if (hdr
->sh_info
!= 0)
3015 elf_dynverdef (abfd
) = shindex
;
3016 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
3017 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3020 case SHT_GNU_versym
:
3021 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
3024 elf_dynversym (abfd
) = shindex
;
3025 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
3026 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3029 case SHT_GNU_verneed
:
3030 if (hdr
->sh_info
!= 0)
3031 elf_dynverref (abfd
) = shindex
;
3032 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
3033 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3040 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
3043 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3049 /* Possibly an attributes section. */
3050 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
3051 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
3053 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3055 _bfd_elf_parse_attributes (abfd
, hdr
);
3059 /* Check for any processor-specific section types. */
3060 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
3063 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
3065 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3066 /* FIXME: How to properly handle allocated section reserved
3067 for applications? */
3069 /* xgettext:c-format */
3070 (_("%pB: unknown type [%#x] section `%s'"),
3071 abfd
, hdr
->sh_type
, name
);
3074 /* Allow sections reserved for applications. */
3075 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3079 else if (hdr
->sh_type
>= SHT_LOPROC
3080 && hdr
->sh_type
<= SHT_HIPROC
)
3081 /* FIXME: We should handle this section. */
3083 /* xgettext:c-format */
3084 (_("%pB: unknown type [%#x] section `%s'"),
3085 abfd
, hdr
->sh_type
, name
);
3086 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
3088 /* Unrecognised OS-specific sections. */
3089 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
3090 /* SHF_OS_NONCONFORMING indicates that special knowledge is
3091 required to correctly process the section and the file should
3092 be rejected with an error message. */
3094 /* xgettext:c-format */
3095 (_("%pB: unknown type [%#x] section `%s'"),
3096 abfd
, hdr
->sh_type
, name
);
3099 /* Otherwise it should be processed. */
3100 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
3105 /* FIXME: We should handle this section. */
3107 /* xgettext:c-format */
3108 (_("%pB: unknown type [%#x] section `%s'"),
3109 abfd
, hdr
->sh_type
, name
);
3117 elf_tdata (abfd
)->being_created
[shindex
] = false;
3121 /* Return the local symbol specified by ABFD, R_SYMNDX. */
3124 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
3126 unsigned long r_symndx
)
3128 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
3130 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
3132 Elf_Internal_Shdr
*symtab_hdr
;
3133 unsigned char esym
[sizeof (Elf64_External_Sym
)];
3134 Elf_External_Sym_Shndx eshndx
;
3136 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3137 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
3138 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
3141 if (cache
->abfd
!= abfd
)
3143 memset (cache
->indx
, -1, sizeof (cache
->indx
));
3146 cache
->indx
[ent
] = r_symndx
;
3149 return &cache
->sym
[ent
];
3152 /* Given an ELF section number, retrieve the corresponding BFD
3156 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
3158 if (sec_index
>= elf_numsections (abfd
))
3160 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
3163 static const struct bfd_elf_special_section special_sections_b
[] =
3165 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3166 { NULL
, 0, 0, 0, 0 }
3169 static const struct bfd_elf_special_section special_sections_c
[] =
3171 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
3172 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
3173 { NULL
, 0, 0, 0, 0 }
3176 static const struct bfd_elf_special_section special_sections_d
[] =
3178 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3179 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3180 /* There are more DWARF sections than these, but they needn't be added here
3181 unless you have to cope with broken compilers that don't emit section
3182 attributes or you want to help the user writing assembler. */
3183 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
3184 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
3185 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
3186 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
3187 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
3188 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
3189 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
3190 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
3191 { NULL
, 0, 0, 0, 0 }
3194 static const struct bfd_elf_special_section special_sections_f
[] =
3196 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3197 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3198 { NULL
, 0 , 0, 0, 0 }
3201 static const struct bfd_elf_special_section special_sections_g
[] =
3203 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3204 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3205 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3206 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
3207 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3208 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
3209 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
3210 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
3211 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
3212 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
3213 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
3214 { NULL
, 0, 0, 0, 0 }
3217 static const struct bfd_elf_special_section special_sections_h
[] =
3219 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
3220 { NULL
, 0, 0, 0, 0 }
3223 static const struct bfd_elf_special_section special_sections_i
[] =
3225 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3226 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3227 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
3228 { NULL
, 0, 0, 0, 0 }
3231 static const struct bfd_elf_special_section special_sections_l
[] =
3233 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
3234 { NULL
, 0, 0, 0, 0 }
3237 static const struct bfd_elf_special_section special_sections_n
[] =
3239 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3240 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
3241 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
3242 { NULL
, 0, 0, 0, 0 }
3245 static const struct bfd_elf_special_section special_sections_p
[] =
3247 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
3248 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
3249 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
3250 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3251 { NULL
, 0, 0, 0, 0 }
3254 static const struct bfd_elf_special_section special_sections_r
[] =
3256 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
3257 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
3258 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR
, SHF_ALLOC
},
3259 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
3260 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
3261 { NULL
, 0, 0, 0, 0 }
3264 static const struct bfd_elf_special_section special_sections_s
[] =
3266 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
3267 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
3268 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
3269 /* See struct bfd_elf_special_section declaration for the semantics of
3270 this special case where .prefix_length != strlen (.prefix). */
3271 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
3272 { NULL
, 0, 0, 0, 0 }
3275 static const struct bfd_elf_special_section special_sections_t
[] =
3277 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
3278 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
3279 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
3280 { NULL
, 0, 0, 0, 0 }
3283 static const struct bfd_elf_special_section special_sections_z
[] =
3285 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
3286 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
3287 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
3288 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
3289 { NULL
, 0, 0, 0, 0 }
3292 static const struct bfd_elf_special_section
* const special_sections
[] =
3294 special_sections_b
, /* 'b' */
3295 special_sections_c
, /* 'c' */
3296 special_sections_d
, /* 'd' */
3298 special_sections_f
, /* 'f' */
3299 special_sections_g
, /* 'g' */
3300 special_sections_h
, /* 'h' */
3301 special_sections_i
, /* 'i' */
3304 special_sections_l
, /* 'l' */
3306 special_sections_n
, /* 'n' */
3308 special_sections_p
, /* 'p' */
3310 special_sections_r
, /* 'r' */
3311 special_sections_s
, /* 's' */
3312 special_sections_t
, /* 't' */
3318 special_sections_z
/* 'z' */
3321 const struct bfd_elf_special_section
*
3322 _bfd_elf_get_special_section (const char *name
,
3323 const struct bfd_elf_special_section
*spec
,
3329 len
= strlen (name
);
3331 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
3334 int prefix_len
= spec
[i
].prefix_length
;
3336 if (len
< prefix_len
)
3338 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
3341 suffix_len
= spec
[i
].suffix_length
;
3342 if (suffix_len
<= 0)
3344 if (name
[prefix_len
] != 0)
3346 if (suffix_len
== 0)
3348 if (name
[prefix_len
] != '.'
3349 && (suffix_len
== -2
3350 || (rela
&& spec
[i
].type
== SHT_REL
)))
3356 if (len
< prefix_len
+ suffix_len
)
3358 if (memcmp (name
+ len
- suffix_len
,
3359 spec
[i
].prefix
+ prefix_len
,
3369 const struct bfd_elf_special_section
*
3370 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
3373 const struct bfd_elf_special_section
*spec
;
3374 const struct elf_backend_data
*bed
;
3376 /* See if this is one of the special sections. */
3377 if (sec
->name
== NULL
)
3380 bed
= get_elf_backend_data (abfd
);
3381 spec
= bed
->special_sections
;
3384 spec
= _bfd_elf_get_special_section (sec
->name
,
3385 bed
->special_sections
,
3391 if (sec
->name
[0] != '.')
3394 i
= sec
->name
[1] - 'b';
3395 if (i
< 0 || i
> 'z' - 'b')
3398 spec
= special_sections
[i
];
3403 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
3407 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
3409 struct bfd_elf_section_data
*sdata
;
3410 const struct elf_backend_data
*bed
;
3411 const struct bfd_elf_special_section
*ssect
;
3413 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
3416 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
3420 sec
->used_by_bfd
= sdata
;
3423 /* Indicate whether or not this section should use RELA relocations. */
3424 bed
= get_elf_backend_data (abfd
);
3425 sec
->use_rela_p
= bed
->default_use_rela_p
;
3427 /* Set up ELF section type and flags for newly created sections, if
3428 there is an ABI mandated section. */
3429 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
3432 elf_section_type (sec
) = ssect
->type
;
3433 elf_section_flags (sec
) = ssect
->attr
;
3436 return _bfd_generic_new_section_hook (abfd
, sec
);
3439 /* Create a new bfd section from an ELF program header.
3441 Since program segments have no names, we generate a synthetic name
3442 of the form segment<NUM>, where NUM is generally the index in the
3443 program header table. For segments that are split (see below) we
3444 generate the names segment<NUM>a and segment<NUM>b.
3446 Note that some program segments may have a file size that is different than
3447 (less than) the memory size. All this means is that at execution the
3448 system must allocate the amount of memory specified by the memory size,
3449 but only initialize it with the first "file size" bytes read from the
3450 file. This would occur for example, with program segments consisting
3451 of combined data+bss.
3453 To handle the above situation, this routine generates TWO bfd sections
3454 for the single program segment. The first has the length specified by
3455 the file size of the segment, and the second has the length specified
3456 by the difference between the two sizes. In effect, the segment is split
3457 into its initialized and uninitialized parts. */
3460 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
3461 Elf_Internal_Phdr
*hdr
,
3463 const char *type_name
)
3470 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
3472 split
= ((hdr
->p_memsz
> 0)
3473 && (hdr
->p_filesz
> 0)
3474 && (hdr
->p_memsz
> hdr
->p_filesz
));
3476 if (hdr
->p_filesz
> 0)
3478 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
3479 len
= strlen (namebuf
) + 1;
3480 name
= (char *) bfd_alloc (abfd
, len
);
3483 memcpy (name
, namebuf
, len
);
3484 newsect
= bfd_make_section (abfd
, name
);
3485 if (newsect
== NULL
)
3487 newsect
->vma
= hdr
->p_vaddr
/ opb
;
3488 newsect
->lma
= hdr
->p_paddr
/ opb
;
3489 newsect
->size
= hdr
->p_filesz
;
3490 newsect
->filepos
= hdr
->p_offset
;
3491 newsect
->flags
|= SEC_HAS_CONTENTS
;
3492 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
3493 if (hdr
->p_type
== PT_LOAD
)
3495 newsect
->flags
|= SEC_ALLOC
;
3496 newsect
->flags
|= SEC_LOAD
;
3497 if (hdr
->p_flags
& PF_X
)
3499 /* FIXME: all we known is that it has execute PERMISSION,
3501 newsect
->flags
|= SEC_CODE
;
3504 if (!(hdr
->p_flags
& PF_W
))
3506 newsect
->flags
|= SEC_READONLY
;
3510 if (hdr
->p_memsz
> hdr
->p_filesz
)
3514 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3515 len
= strlen (namebuf
) + 1;
3516 name
= (char *) bfd_alloc (abfd
, len
);
3519 memcpy (name
, namebuf
, len
);
3520 newsect
= bfd_make_section (abfd
, name
);
3521 if (newsect
== NULL
)
3523 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3524 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3525 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3526 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3527 align
= newsect
->vma
& -newsect
->vma
;
3528 if (align
== 0 || align
> hdr
->p_align
)
3529 align
= hdr
->p_align
;
3530 newsect
->alignment_power
= bfd_log2 (align
);
3531 if (hdr
->p_type
== PT_LOAD
)
3533 newsect
->flags
|= SEC_ALLOC
;
3534 if (hdr
->p_flags
& PF_X
)
3535 newsect
->flags
|= SEC_CODE
;
3537 if (!(hdr
->p_flags
& PF_W
))
3538 newsect
->flags
|= SEC_READONLY
;
3545 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3547 /* The return value is ignored. Build-ids are considered optional. */
3548 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3549 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3555 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3557 const struct elf_backend_data
*bed
;
3559 switch (hdr
->p_type
)
3562 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3565 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3567 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3568 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3572 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3575 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3578 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3580 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3586 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3589 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3591 case PT_GNU_EH_FRAME
:
3592 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3596 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3599 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3602 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3606 /* Check for any processor-specific program segment types. */
3607 bed
= get_elf_backend_data (abfd
);
3608 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3612 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3616 _bfd_elf_single_rel_hdr (asection
*sec
)
3618 if (elf_section_data (sec
)->rel
.hdr
)
3620 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3621 return elf_section_data (sec
)->rel
.hdr
;
3624 return elf_section_data (sec
)->rela
.hdr
;
3628 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3629 Elf_Internal_Shdr
*rel_hdr
,
3630 const char *sec_name
,
3633 char *name
= (char *) bfd_alloc (abfd
,
3634 sizeof ".rela" + strlen (sec_name
));
3638 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3640 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3642 if (rel_hdr
->sh_name
== (unsigned int) -1)
3648 /* Allocate and initialize a section-header for a new reloc section,
3649 containing relocations against ASECT. It is stored in RELDATA. If
3650 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3654 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3655 struct bfd_elf_section_reloc_data
*reldata
,
3656 const char *sec_name
,
3658 bool delay_st_name_p
)
3660 Elf_Internal_Shdr
*rel_hdr
;
3661 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3663 BFD_ASSERT (reldata
->hdr
== NULL
);
3664 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3665 if (rel_hdr
== NULL
)
3667 reldata
->hdr
= rel_hdr
;
3669 if (delay_st_name_p
)
3670 rel_hdr
->sh_name
= (unsigned int) -1;
3671 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3674 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3675 rel_hdr
->sh_entsize
= (use_rela_p
3676 ? bed
->s
->sizeof_rela
3677 : bed
->s
->sizeof_rel
);
3678 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3679 rel_hdr
->sh_flags
= 0;
3680 rel_hdr
->sh_addr
= 0;
3681 rel_hdr
->sh_size
= 0;
3682 rel_hdr
->sh_offset
= 0;
3687 /* Return the default section type based on the passed in section flags. */
3690 bfd_elf_get_default_section_type (flagword flags
)
3692 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3693 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3695 return SHT_PROGBITS
;
3698 struct fake_section_arg
3700 struct bfd_link_info
*link_info
;
3704 /* Set up an ELF internal section header for a section. */
3707 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3709 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3710 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3711 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3712 Elf_Internal_Shdr
*this_hdr
;
3713 unsigned int sh_type
;
3714 const char *name
= asect
->name
;
3715 bool delay_st_name_p
= false;
3720 /* We already failed; just get out of the bfd_map_over_sections
3725 this_hdr
= &esd
->this_hdr
;
3727 /* ld: compress DWARF debug sections with names: .debug_*. */
3729 && (abfd
->flags
& BFD_COMPRESS
) != 0
3730 && (asect
->flags
& SEC_DEBUGGING
) != 0
3734 /* If this section will be compressed, delay adding section
3735 name to section name section after it is compressed in
3736 _bfd_elf_assign_file_positions_for_non_load. */
3737 delay_st_name_p
= true;
3740 if (delay_st_name_p
)
3741 this_hdr
->sh_name
= (unsigned int) -1;
3745 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3747 if (this_hdr
->sh_name
== (unsigned int) -1)
3754 /* Don't clear sh_flags. Assembler may set additional bits. */
3756 if ((asect
->flags
& SEC_ALLOC
) != 0
3757 || asect
->user_set_vma
)
3758 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3760 this_hdr
->sh_addr
= 0;
3762 this_hdr
->sh_offset
= 0;
3763 this_hdr
->sh_size
= asect
->size
;
3764 this_hdr
->sh_link
= 0;
3765 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3766 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3769 /* xgettext:c-format */
3770 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3771 abfd
, asect
->alignment_power
, asect
);
3775 /* Set sh_addralign to the highest power of two given by alignment
3776 consistent with the section VMA. Linker scripts can force VMA. */
3777 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3778 this_hdr
->sh_addralign
= mask
& -mask
;
3779 /* The sh_entsize and sh_info fields may have been set already by
3780 copy_private_section_data. */
3782 this_hdr
->bfd_section
= asect
;
3783 this_hdr
->contents
= NULL
;
3785 /* If the section type is unspecified, we set it based on
3787 if (asect
->type
!= 0)
3788 sh_type
= asect
->type
;
3789 else if ((asect
->flags
& SEC_GROUP
) != 0)
3790 sh_type
= SHT_GROUP
;
3792 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3794 if (this_hdr
->sh_type
== SHT_NULL
)
3795 this_hdr
->sh_type
= sh_type
;
3796 else if (this_hdr
->sh_type
== SHT_NOBITS
3797 && sh_type
== SHT_PROGBITS
3798 && (asect
->flags
& SEC_ALLOC
) != 0)
3800 /* Warn if we are changing a NOBITS section to PROGBITS, but
3801 allow the link to proceed. This can happen when users link
3802 non-bss input sections to bss output sections, or emit data
3803 to a bss output section via a linker script. */
3805 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3806 this_hdr
->sh_type
= sh_type
;
3809 switch (this_hdr
->sh_type
)
3820 case SHT_INIT_ARRAY
:
3821 case SHT_FINI_ARRAY
:
3822 case SHT_PREINIT_ARRAY
:
3823 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3827 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3831 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3835 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3839 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3840 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3844 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3845 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3848 case SHT_GNU_versym
:
3849 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3852 case SHT_GNU_verdef
:
3853 this_hdr
->sh_entsize
= 0;
3854 /* objcopy or strip will copy over sh_info, but may not set
3855 cverdefs. The linker will set cverdefs, but sh_info will be
3857 if (this_hdr
->sh_info
== 0)
3858 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3860 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3861 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3864 case SHT_GNU_verneed
:
3865 this_hdr
->sh_entsize
= 0;
3866 /* objcopy or strip will copy over sh_info, but may not set
3867 cverrefs. The linker will set cverrefs, but sh_info will be
3869 if (this_hdr
->sh_info
== 0)
3870 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3872 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3873 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3877 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3881 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3885 if ((asect
->flags
& SEC_ALLOC
) != 0)
3886 this_hdr
->sh_flags
|= SHF_ALLOC
;
3887 if ((asect
->flags
& SEC_READONLY
) == 0)
3888 this_hdr
->sh_flags
|= SHF_WRITE
;
3889 if ((asect
->flags
& SEC_CODE
) != 0)
3890 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3891 if ((asect
->flags
& SEC_MERGE
) != 0)
3893 this_hdr
->sh_flags
|= SHF_MERGE
;
3894 this_hdr
->sh_entsize
= asect
->entsize
;
3896 if ((asect
->flags
& SEC_STRINGS
) != 0)
3897 this_hdr
->sh_flags
|= SHF_STRINGS
;
3898 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3899 this_hdr
->sh_flags
|= SHF_GROUP
;
3900 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3902 this_hdr
->sh_flags
|= SHF_TLS
;
3903 if (asect
->size
== 0
3904 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3906 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3908 this_hdr
->sh_size
= 0;
3911 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3912 if (this_hdr
->sh_size
!= 0)
3913 this_hdr
->sh_type
= SHT_NOBITS
;
3917 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3918 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3920 /* If the section has relocs, set up a section header for the
3921 SHT_REL[A] section. If two relocation sections are required for
3922 this section, it is up to the processor-specific back-end to
3923 create the other. */
3924 if ((asect
->flags
& SEC_RELOC
) != 0)
3926 /* When doing a relocatable link, create both REL and RELA sections if
3929 /* Do the normal setup if we wouldn't create any sections here. */
3930 && esd
->rel
.count
+ esd
->rela
.count
> 0
3931 && (bfd_link_relocatable (arg
->link_info
)
3932 || arg
->link_info
->emitrelocations
))
3934 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3935 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3936 false, delay_st_name_p
))
3941 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3942 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3943 true, delay_st_name_p
))
3949 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3951 ? &esd
->rela
: &esd
->rel
),
3961 /* Check for processor-specific section types. */
3962 sh_type
= this_hdr
->sh_type
;
3963 if (bed
->elf_backend_fake_sections
3964 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3970 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3972 /* Don't change the header type from NOBITS if we are being
3973 called for objcopy --only-keep-debug. */
3974 this_hdr
->sh_type
= sh_type
;
3978 /* Fill in the contents of a SHT_GROUP section. Called from
3979 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3980 when ELF targets use the generic linker, ld. Called for ld -r
3981 from bfd_elf_final_link. */
3984 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3986 bool *failedptr
= (bool *) failedptrarg
;
3987 asection
*elt
, *first
;
3991 /* Ignore linker created group section. See elfNN_ia64_object_p in
3993 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3998 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
4000 unsigned long symindx
= 0;
4002 /* elf_group_id will have been set up by objcopy and the
4004 if (elf_group_id (sec
) != NULL
)
4005 symindx
= elf_group_id (sec
)->udata
.i
;
4009 /* If called from the assembler, swap_out_syms will have set up
4011 PR 25699: A corrupt input file could contain bogus group info. */
4012 if (sec
->index
>= elf_num_section_syms (abfd
)
4013 || elf_section_syms (abfd
)[sec
->index
] == NULL
)
4018 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
4020 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
4022 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
4024 /* The ELF backend linker sets sh_info to -2 when the group
4025 signature symbol is global, and thus the index can't be
4026 set until all local symbols are output. */
4028 struct bfd_elf_section_data
*sec_data
;
4029 unsigned long symndx
;
4030 unsigned long extsymoff
;
4031 struct elf_link_hash_entry
*h
;
4033 /* The point of this little dance to the first SHF_GROUP section
4034 then back to the SHT_GROUP section is that this gets us to
4035 the SHT_GROUP in the input object. */
4036 igroup
= elf_sec_group (elf_next_in_group (sec
));
4037 sec_data
= elf_section_data (igroup
);
4038 symndx
= sec_data
->this_hdr
.sh_info
;
4040 if (!elf_bad_symtab (igroup
->owner
))
4042 Elf_Internal_Shdr
*symtab_hdr
;
4044 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
4045 extsymoff
= symtab_hdr
->sh_info
;
4047 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
4048 while (h
->root
.type
== bfd_link_hash_indirect
4049 || h
->root
.type
== bfd_link_hash_warning
)
4050 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4052 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
4055 /* The contents won't be allocated for "ld -r" or objcopy. */
4057 if (sec
->contents
== NULL
)
4060 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
4062 /* Arrange for the section to be written out. */
4063 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
4064 if (sec
->contents
== NULL
)
4071 loc
= sec
->contents
+ sec
->size
;
4073 /* Get the pointer to the first section in the group that gas
4074 squirreled away here. objcopy arranges for this to be set to the
4075 start of the input section group. */
4076 first
= elt
= elf_next_in_group (sec
);
4078 /* First element is a flag word. Rest of section is elf section
4079 indices for all the sections of the group. Write them backwards
4080 just to keep the group in the same order as given in .section
4081 directives, not that it matters. */
4088 s
= s
->output_section
;
4090 && !bfd_is_abs_section (s
))
4092 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
4093 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
4095 if (elf_sec
->rel
.hdr
!= NULL
4097 || (input_elf_sec
->rel
.hdr
!= NULL
4098 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
4100 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
4102 if (loc
== sec
->contents
)
4104 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
4106 if (elf_sec
->rela
.hdr
!= NULL
4108 || (input_elf_sec
->rela
.hdr
!= NULL
4109 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
4111 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
4113 if (loc
== sec
->contents
)
4115 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
4118 if (loc
== sec
->contents
)
4120 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
4122 elt
= elf_next_in_group (elt
);
4127 /* We should always get here with loc == sec->contents + 4, but it is
4128 possible to craft bogus SHT_GROUP sections that will cause segfaults
4129 in objcopy without checking loc here and in the loop above. */
4130 if (loc
== sec
->contents
)
4135 if (loc
!= sec
->contents
)
4138 memset (sec
->contents
+ 4, 0, loc
- sec
->contents
);
4139 loc
= sec
->contents
;
4143 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
4146 /* Given NAME, the name of a relocation section stripped of its
4147 .rel/.rela prefix, return the section in ABFD to which the
4148 relocations apply. */
4151 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
4153 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4154 section likely apply to .got.plt or .got section. */
4155 if (get_elf_backend_data (abfd
)->want_got_plt
4156 && strcmp (name
, ".plt") == 0)
4161 sec
= bfd_get_section_by_name (abfd
, name
);
4167 return bfd_get_section_by_name (abfd
, name
);
4170 /* Return the section to which RELOC_SEC applies. */
4173 elf_get_reloc_section (asection
*reloc_sec
)
4178 const struct elf_backend_data
*bed
;
4180 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
4181 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
4184 /* We look up the section the relocs apply to by name. */
4185 name
= reloc_sec
->name
;
4186 if (!startswith (name
, ".rel"))
4189 if (type
== SHT_RELA
&& *name
++ != 'a')
4192 abfd
= reloc_sec
->owner
;
4193 bed
= get_elf_backend_data (abfd
);
4194 return bed
->get_reloc_section (abfd
, name
);
4197 /* Assign all ELF section numbers. The dummy first section is handled here
4198 too. The link/info pointers for the standard section types are filled
4199 in here too, while we're at it. LINK_INFO will be 0 when arriving
4200 here for gas, objcopy, and when using the generic ELF linker. */
4203 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
4205 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
4207 unsigned int section_number
;
4208 Elf_Internal_Shdr
**i_shdrp
;
4209 struct bfd_elf_section_data
*d
;
4215 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
4217 /* SHT_GROUP sections are in relocatable files only. */
4218 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
4220 size_t reloc_count
= 0;
4222 /* Put SHT_GROUP sections first. */
4223 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4225 d
= elf_section_data (sec
);
4227 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
4229 if (sec
->flags
& SEC_LINKER_CREATED
)
4231 /* Remove the linker created SHT_GROUP sections. */
4232 bfd_section_list_remove (abfd
, sec
);
4233 abfd
->section_count
--;
4236 d
->this_idx
= section_number
++;
4239 /* Count relocations. */
4240 reloc_count
+= sec
->reloc_count
;
4243 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4244 if (reloc_count
== 0)
4245 abfd
->flags
&= ~HAS_RELOC
;
4247 abfd
->flags
|= HAS_RELOC
;
4250 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4252 d
= elf_section_data (sec
);
4254 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
4255 d
->this_idx
= section_number
++;
4256 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
4257 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
4260 d
->rel
.idx
= section_number
++;
4261 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
4262 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
4269 d
->rela
.idx
= section_number
++;
4270 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
4271 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
4277 need_symtab
= (bfd_get_symcount (abfd
) > 0
4278 || (link_info
== NULL
4279 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4283 elf_onesymtab (abfd
) = section_number
++;
4284 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
4285 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
4287 elf_section_list
*entry
;
4289 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
4291 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
4292 entry
->ndx
= section_number
++;
4293 elf_symtab_shndx_list (abfd
) = entry
;
4295 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
4296 ".symtab_shndx", false);
4297 if (entry
->hdr
.sh_name
== (unsigned int) -1)
4300 elf_strtab_sec (abfd
) = section_number
++;
4301 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
4304 elf_shstrtab_sec (abfd
) = section_number
++;
4305 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
4306 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
4308 if (section_number
>= SHN_LORESERVE
)
4310 /* xgettext:c-format */
4311 _bfd_error_handler (_("%pB: too many sections: %u"),
4312 abfd
, section_number
);
4316 elf_numsections (abfd
) = section_number
;
4317 elf_elfheader (abfd
)->e_shnum
= section_number
;
4319 /* Set up the list of section header pointers, in agreement with the
4321 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
4322 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
4323 if (i_shdrp
== NULL
)
4326 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
4327 sizeof (Elf_Internal_Shdr
));
4328 if (i_shdrp
[0] == NULL
)
4330 bfd_release (abfd
, i_shdrp
);
4334 elf_elfsections (abfd
) = i_shdrp
;
4336 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
4339 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
4340 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
4342 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
4343 BFD_ASSERT (entry
!= NULL
);
4344 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
4345 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
4347 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
4348 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
4351 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4355 d
= elf_section_data (sec
);
4357 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
4358 if (d
->rel
.idx
!= 0)
4359 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
4360 if (d
->rela
.idx
!= 0)
4361 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
4363 /* Fill in the sh_link and sh_info fields while we're at it. */
4365 /* sh_link of a reloc section is the section index of the symbol
4366 table. sh_info is the section index of the section to which
4367 the relocation entries apply. */
4368 if (d
->rel
.idx
!= 0)
4370 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
4371 d
->rel
.hdr
->sh_info
= d
->this_idx
;
4372 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
4374 if (d
->rela
.idx
!= 0)
4376 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
4377 d
->rela
.hdr
->sh_info
= d
->this_idx
;
4378 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
4381 /* We need to set up sh_link for SHF_LINK_ORDER. */
4382 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
4384 s
= elf_linked_to_section (sec
);
4385 /* We can now have a NULL linked section pointer.
4386 This happens when the sh_link field is 0, which is done
4387 when a linked to section is discarded but the linking
4388 section has been retained for some reason. */
4391 /* Check discarded linkonce section. */
4392 if (discarded_section (s
))
4396 /* xgettext:c-format */
4397 (_("%pB: sh_link of section `%pA' points to"
4398 " discarded section `%pA' of `%pB'"),
4399 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
4400 /* Point to the kept section if it has the same
4401 size as the discarded one. */
4402 kept
= _bfd_elf_check_kept_section (s
, link_info
);
4405 bfd_set_error (bfd_error_bad_value
);
4410 /* Handle objcopy. */
4411 else if (s
->output_section
== NULL
)
4414 /* xgettext:c-format */
4415 (_("%pB: sh_link of section `%pA' points to"
4416 " removed section `%pA' of `%pB'"),
4417 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
4418 bfd_set_error (bfd_error_bad_value
);
4421 s
= s
->output_section
;
4422 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4426 switch (d
->this_hdr
.sh_type
)
4430 /* sh_link is the section index of the symbol table.
4431 sh_info is the section index of the section to which the
4432 relocation entries apply. */
4433 if (d
->this_hdr
.sh_link
== 0)
4435 /* FIXME maybe: If this is a reloc section which we are
4436 treating as a normal section then we likely should
4437 not be assuming its sh_link is .dynsym or .symtab. */
4438 if ((sec
->flags
& SEC_ALLOC
) != 0)
4440 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4442 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4445 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4448 s
= elf_get_reloc_section (sec
);
4451 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
4452 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
4457 /* We assume that a section named .stab*str is a stabs
4458 string section. We look for a section with the same name
4459 but without the trailing ``str'', and set its sh_link
4460 field to point to this section. */
4461 if (startswith (sec
->name
, ".stab")
4462 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
4467 len
= strlen (sec
->name
);
4468 alc
= (char *) bfd_malloc (len
- 2);
4471 memcpy (alc
, sec
->name
, len
- 3);
4472 alc
[len
- 3] = '\0';
4473 s
= bfd_get_section_by_name (abfd
, alc
);
4477 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
4479 /* This is a .stab section. */
4480 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
4487 case SHT_GNU_verneed
:
4488 case SHT_GNU_verdef
:
4489 /* sh_link is the section header index of the string table
4490 used for the dynamic entries, or the symbol table, or the
4492 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4494 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4497 case SHT_GNU_LIBLIST
:
4498 /* sh_link is the section header index of the prelink library
4499 list used for the dynamic entries, or the symbol table, or
4500 the version strings. */
4501 s
= bfd_get_section_by_name (abfd
, ((sec
->flags
& SEC_ALLOC
)
4502 ? ".dynstr" : ".gnu.libstr"));
4504 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4509 case SHT_GNU_versym
:
4510 /* sh_link is the section header index of the symbol table
4511 this hash table or version table is for. */
4512 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4514 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4518 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4522 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4523 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4524 debug section name from .debug_* to .zdebug_* if needed. */
4530 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4532 /* If the backend has a special mapping, use it. */
4533 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4534 if (bed
->elf_backend_sym_is_global
)
4535 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4537 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4538 || bfd_is_und_section (bfd_asymbol_section (sym
))
4539 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4542 /* Filter global symbols of ABFD to include in the import library. All
4543 SYMCOUNT symbols of ABFD can be examined from their pointers in
4544 SYMS. Pointers of symbols to keep should be stored contiguously at
4545 the beginning of that array.
4547 Returns the number of symbols to keep. */
4550 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4551 asymbol
**syms
, long symcount
)
4553 long src_count
, dst_count
= 0;
4555 for (src_count
= 0; src_count
< symcount
; src_count
++)
4557 asymbol
*sym
= syms
[src_count
];
4558 char *name
= (char *) bfd_asymbol_name (sym
);
4559 struct bfd_link_hash_entry
*h
;
4561 if (!sym_is_global (abfd
, sym
))
4564 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, false);
4567 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4569 if (h
->linker_def
|| h
->ldscript_def
)
4572 syms
[dst_count
++] = sym
;
4575 syms
[dst_count
] = NULL
;
4580 /* Don't output section symbols for sections that are not going to be
4581 output, that are duplicates or there is no BFD section. */
4584 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4586 elf_symbol_type
*type_ptr
;
4591 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4594 /* Ignore the section symbol if it isn't used. */
4595 if ((sym
->flags
& BSF_SECTION_SYM_USED
) == 0)
4598 if (sym
->section
== NULL
)
4601 type_ptr
= elf_symbol_from (sym
);
4602 return ((type_ptr
!= NULL
4603 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4604 && bfd_is_abs_section (sym
->section
))
4605 || !(sym
->section
->owner
== abfd
4606 || (sym
->section
->output_section
!= NULL
4607 && sym
->section
->output_section
->owner
== abfd
4608 && sym
->section
->output_offset
== 0)
4609 || bfd_is_abs_section (sym
->section
)));
4612 /* Map symbol from it's internal number to the external number, moving
4613 all local symbols to be at the head of the list. */
4616 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4618 unsigned int symcount
= bfd_get_symcount (abfd
);
4619 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4620 asymbol
**sect_syms
;
4621 unsigned int num_locals
= 0;
4622 unsigned int num_globals
= 0;
4623 unsigned int num_locals2
= 0;
4624 unsigned int num_globals2
= 0;
4625 unsigned int max_index
= 0;
4632 fprintf (stderr
, "elf_map_symbols\n");
4636 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4638 if (max_index
< asect
->index
)
4639 max_index
= asect
->index
;
4643 amt
= max_index
* sizeof (asymbol
*);
4644 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4645 if (sect_syms
== NULL
)
4647 elf_section_syms (abfd
) = sect_syms
;
4648 elf_num_section_syms (abfd
) = max_index
;
4650 /* Init sect_syms entries for any section symbols we have already
4651 decided to output. */
4652 for (idx
= 0; idx
< symcount
; idx
++)
4654 asymbol
*sym
= syms
[idx
];
4656 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4658 && !ignore_section_sym (abfd
, sym
)
4659 && !bfd_is_abs_section (sym
->section
))
4661 asection
*sec
= sym
->section
;
4663 if (sec
->owner
!= abfd
)
4664 sec
= sec
->output_section
;
4666 sect_syms
[sec
->index
] = syms
[idx
];
4670 /* Classify all of the symbols. */
4671 for (idx
= 0; idx
< symcount
; idx
++)
4673 if (sym_is_global (abfd
, syms
[idx
]))
4675 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4679 /* We will be adding a section symbol for each normal BFD section. Most
4680 sections will already have a section symbol in outsymbols, but
4681 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4682 at least in that case. */
4683 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4685 asymbol
*sym
= asect
->symbol
;
4686 /* Don't include ignored section symbols. */
4687 if (!ignore_section_sym (abfd
, sym
)
4688 && sect_syms
[asect
->index
] == NULL
)
4690 if (!sym_is_global (abfd
, asect
->symbol
))
4697 /* Now sort the symbols so the local symbols are first. */
4698 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4699 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4700 if (new_syms
== NULL
)
4703 for (idx
= 0; idx
< symcount
; idx
++)
4705 asymbol
*sym
= syms
[idx
];
4708 if (sym_is_global (abfd
, sym
))
4709 i
= num_locals
+ num_globals2
++;
4710 /* Don't include ignored section symbols. */
4711 else if (!ignore_section_sym (abfd
, sym
))
4716 sym
->udata
.i
= i
+ 1;
4718 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4720 asymbol
*sym
= asect
->symbol
;
4721 if (!ignore_section_sym (abfd
, sym
)
4722 && sect_syms
[asect
->index
] == NULL
)
4726 sect_syms
[asect
->index
] = sym
;
4727 if (!sym_is_global (abfd
, sym
))
4730 i
= num_locals
+ num_globals2
++;
4732 sym
->udata
.i
= i
+ 1;
4736 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4738 *pnum_locals
= num_locals
;
4742 /* Align to the maximum file alignment that could be required for any
4743 ELF data structure. */
4745 static inline file_ptr
4746 align_file_position (file_ptr off
, int align
)
4748 return (off
+ align
- 1) & ~(align
- 1);
4751 /* Assign a file position to a section, optionally aligning to the
4752 required section alignment. */
4755 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4759 if (align
&& i_shdrp
->sh_addralign
> 1)
4760 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
& -i_shdrp
->sh_addralign
);
4761 i_shdrp
->sh_offset
= offset
;
4762 if (i_shdrp
->bfd_section
!= NULL
)
4763 i_shdrp
->bfd_section
->filepos
= offset
;
4764 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4765 offset
+= i_shdrp
->sh_size
;
4769 /* Compute the file positions we are going to put the sections at, and
4770 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4771 is not NULL, this is being called by the ELF backend linker. */
4774 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4775 struct bfd_link_info
*link_info
)
4777 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4778 struct fake_section_arg fsargs
;
4780 struct elf_strtab_hash
*strtab
= NULL
;
4781 Elf_Internal_Shdr
*shstrtab_hdr
;
4784 if (abfd
->output_has_begun
)
4787 /* Do any elf backend specific processing first. */
4788 if (bed
->elf_backend_begin_write_processing
)
4789 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4791 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4794 fsargs
.failed
= false;
4795 fsargs
.link_info
= link_info
;
4796 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4800 if (!assign_section_numbers (abfd
, link_info
))
4803 /* The backend linker builds symbol table information itself. */
4804 need_symtab
= (link_info
== NULL
4805 && (bfd_get_symcount (abfd
) > 0
4806 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4810 /* Non-zero if doing a relocatable link. */
4811 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4813 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
, link_info
))
4818 if (link_info
== NULL
)
4820 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4822 goto err_free_strtab
;
4825 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4826 /* sh_name was set in init_file_header. */
4827 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4828 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4829 shstrtab_hdr
->sh_addr
= 0;
4830 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4831 shstrtab_hdr
->sh_entsize
= 0;
4832 shstrtab_hdr
->sh_link
= 0;
4833 shstrtab_hdr
->sh_info
= 0;
4834 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4835 shstrtab_hdr
->sh_addralign
= 1;
4837 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4838 goto err_free_strtab
;
4843 Elf_Internal_Shdr
*hdr
;
4845 off
= elf_next_file_pos (abfd
);
4847 hdr
= & elf_symtab_hdr (abfd
);
4848 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4850 if (elf_symtab_shndx_list (abfd
) != NULL
)
4852 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4853 if (hdr
->sh_size
!= 0)
4854 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4855 /* FIXME: What about other symtab_shndx sections in the list ? */
4858 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4859 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4861 elf_next_file_pos (abfd
) = off
;
4863 /* Now that we know where the .strtab section goes, write it
4865 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4866 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4867 goto err_free_strtab
;
4868 _bfd_elf_strtab_free (strtab
);
4871 abfd
->output_has_begun
= true;
4876 _bfd_elf_strtab_free (strtab
);
4880 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4881 function effectively returns whether --eh-frame-hdr is given on the
4882 command line. After size_dynamic_sections the result reflects
4883 whether .eh_frame_hdr will actually be output (sizing isn't done
4884 until ldemul_after_allocation). */
4887 elf_eh_frame_hdr (const struct bfd_link_info
*info
)
4889 if (info
!= NULL
&& is_elf_hash_table (info
->hash
))
4890 return elf_hash_table (info
)->eh_info
.hdr_sec
;
4894 /* Make an initial estimate of the size of the program header. If we
4895 get the number wrong here, we'll redo section placement. */
4897 static bfd_size_type
4898 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4902 const struct elf_backend_data
*bed
;
4904 /* Assume we will need exactly two PT_LOAD segments: one for text
4905 and one for data. */
4908 s
= bfd_get_section_by_name (abfd
, ".interp");
4909 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4911 /* If we have a loadable interpreter section, we need a
4912 PT_INTERP segment. In this case, assume we also need a
4913 PT_PHDR segment, although that may not be true for all
4918 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4920 /* We need a PT_DYNAMIC segment. */
4924 if (info
!= NULL
&& info
->relro
)
4926 /* We need a PT_GNU_RELRO segment. */
4930 if (elf_eh_frame_hdr (info
))
4932 /* We need a PT_GNU_EH_FRAME segment. */
4936 if (elf_stack_flags (abfd
))
4938 /* We need a PT_GNU_STACK segment. */
4942 if (elf_sframe (abfd
))
4944 /* We need a PT_GNU_SFRAME segment. */
4948 s
= bfd_get_section_by_name (abfd
,
4949 NOTE_GNU_PROPERTY_SECTION_NAME
);
4950 if (s
!= NULL
&& s
->size
!= 0)
4952 /* We need a PT_GNU_PROPERTY segment. */
4956 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4958 if ((s
->flags
& SEC_LOAD
) != 0
4959 && elf_section_type (s
) == SHT_NOTE
)
4961 unsigned int alignment_power
;
4962 /* We need a PT_NOTE segment. */
4964 /* Try to create just one PT_NOTE segment for all adjacent
4965 loadable SHT_NOTE sections. gABI requires that within a
4966 PT_NOTE segment (and also inside of each SHT_NOTE section)
4967 each note should have the same alignment. So we check
4968 whether the sections are correctly aligned. */
4969 alignment_power
= s
->alignment_power
;
4970 while (s
->next
!= NULL
4971 && s
->next
->alignment_power
== alignment_power
4972 && (s
->next
->flags
& SEC_LOAD
) != 0
4973 && elf_section_type (s
->next
) == SHT_NOTE
)
4978 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4980 if (s
->flags
& SEC_THREAD_LOCAL
)
4982 /* We need a PT_TLS segment. */
4988 bed
= get_elf_backend_data (abfd
);
4990 if ((abfd
->flags
& D_PAGED
) != 0
4991 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4993 /* Add a PT_GNU_MBIND segment for each mbind section. */
4994 bfd_vma commonpagesize
;
4995 unsigned int page_align_power
;
4998 commonpagesize
= info
->commonpagesize
;
5000 commonpagesize
= bed
->commonpagesize
;
5001 page_align_power
= bfd_log2 (commonpagesize
);
5002 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5003 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
5005 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
5008 /* xgettext:c-format */
5009 (_("%pB: GNU_MBIND section `%pA' has invalid "
5010 "sh_info field: %d"),
5011 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
5014 /* Align mbind section to page size. */
5015 if (s
->alignment_power
< page_align_power
)
5016 s
->alignment_power
= page_align_power
;
5021 /* Let the backend count up any program headers it might need. */
5022 if (bed
->elf_backend_additional_program_headers
)
5026 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
5032 return segs
* bed
->s
->sizeof_phdr
;
5035 /* Find the segment that contains the output_section of section. */
5038 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
5040 struct elf_segment_map
*m
;
5041 Elf_Internal_Phdr
*p
;
5043 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
5049 for (i
= m
->count
- 1; i
>= 0; i
--)
5050 if (m
->sections
[i
] == section
)
5057 /* Create a mapping from a set of sections to a program segment. */
5059 static struct elf_segment_map
*
5060 make_mapping (bfd
*abfd
,
5061 asection
**sections
,
5066 struct elf_segment_map
*m
;
5071 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5072 amt
+= (to
- from
) * sizeof (asection
*);
5073 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5077 m
->p_type
= PT_LOAD
;
5078 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
5079 m
->sections
[i
- from
] = *hdrpp
;
5080 m
->count
= to
- from
;
5082 if (from
== 0 && phdr
)
5084 /* Include the headers in the first PT_LOAD segment. */
5085 m
->includes_filehdr
= 1;
5086 m
->includes_phdrs
= 1;
5092 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
5095 struct elf_segment_map
*
5096 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
5098 struct elf_segment_map
*m
;
5100 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
5101 sizeof (struct elf_segment_map
));
5105 m
->p_type
= PT_DYNAMIC
;
5107 m
->sections
[0] = dynsec
;
5112 /* Possibly add or remove segments from the segment map. */
5115 elf_modify_segment_map (bfd
*abfd
,
5116 struct bfd_link_info
*info
,
5117 bool remove_empty_load
)
5119 struct elf_segment_map
**m
;
5120 const struct elf_backend_data
*bed
;
5122 /* The placement algorithm assumes that non allocated sections are
5123 not in PT_LOAD segments. We ensure this here by removing such
5124 sections from the segment map. We also remove excluded
5125 sections. Finally, any PT_LOAD segment without sections is
5127 m
= &elf_seg_map (abfd
);
5130 unsigned int i
, new_count
;
5132 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
5134 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
5135 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
5136 || (*m
)->p_type
!= PT_LOAD
))
5138 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
5142 (*m
)->count
= new_count
;
5144 if (remove_empty_load
5145 && (*m
)->p_type
== PT_LOAD
5147 && !(*m
)->includes_phdrs
)
5153 bed
= get_elf_backend_data (abfd
);
5154 if (bed
->elf_backend_modify_segment_map
!= NULL
)
5156 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
5163 #define IS_TBSS(s) \
5164 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5166 /* Set up a mapping from BFD sections to program segments. Update
5167 NEED_LAYOUT if the section layout is changed. */
5170 _bfd_elf_map_sections_to_segments (bfd
*abfd
,
5171 struct bfd_link_info
*info
,
5175 struct elf_segment_map
*m
;
5176 asection
**sections
= NULL
;
5177 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5180 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
5184 info
->user_phdrs
= !no_user_phdrs
;
5186 /* Size the relative relocations if DT_RELR is enabled. */
5187 if (info
->enable_dt_relr
5188 && need_layout
!= NULL
5189 && bed
->size_relative_relocs
5190 && !bed
->size_relative_relocs (info
, need_layout
))
5191 info
->callbacks
->einfo
5192 (_("%F%P: failed to size relative relocations\n"));
5195 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
5199 struct elf_segment_map
*mfirst
;
5200 struct elf_segment_map
**pm
;
5203 unsigned int hdr_index
;
5204 bfd_vma maxpagesize
;
5206 bool phdr_in_segment
;
5209 unsigned int tls_count
= 0;
5210 asection
*first_tls
= NULL
;
5211 asection
*first_mbind
= NULL
;
5212 asection
*dynsec
, *eh_frame_hdr
;
5215 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
5216 bfd_size_type phdr_size
; /* Octets/bytes. */
5217 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5219 /* Select the allocated sections, and sort them. */
5221 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
5222 sections
= (asection
**) bfd_malloc (amt
);
5223 if (sections
== NULL
)
5226 /* Calculate top address, avoiding undefined behaviour of shift
5227 left operator when shift count is equal to size of type
5229 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
5230 addr_mask
= (addr_mask
<< 1) + 1;
5233 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5235 if ((s
->flags
& SEC_ALLOC
) != 0)
5237 /* target_index is unused until bfd_elf_final_link
5238 starts output of section symbols. Use it to make
5240 s
->target_index
= i
;
5243 /* A wrapping section potentially clashes with header. */
5244 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
5245 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
5248 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
5251 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
5253 phdr_size
= elf_program_header_size (abfd
);
5254 if (phdr_size
== (bfd_size_type
) -1)
5255 phdr_size
= get_program_header_size (abfd
, info
);
5256 phdr_size
+= bed
->s
->sizeof_ehdr
;
5257 /* phdr_size is compared to LMA values which are in bytes. */
5260 maxpagesize
= info
->maxpagesize
;
5262 maxpagesize
= bed
->maxpagesize
;
5263 if (maxpagesize
== 0)
5265 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
5267 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
5268 >= (phdr_size
& (maxpagesize
- 1))))
5269 /* For compatibility with old scripts that may not be using
5270 SIZEOF_HEADERS, add headers when it looks like space has
5271 been left for them. */
5272 phdr_in_segment
= true;
5274 /* Build the mapping. */
5278 /* If we have a .interp section, then create a PT_PHDR segment for
5279 the program headers and a PT_INTERP segment for the .interp
5281 s
= bfd_get_section_by_name (abfd
, ".interp");
5282 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
5284 amt
= sizeof (struct elf_segment_map
);
5285 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5289 m
->p_type
= PT_PHDR
;
5291 m
->p_flags_valid
= 1;
5292 m
->includes_phdrs
= 1;
5293 phdr_in_segment
= true;
5297 amt
= sizeof (struct elf_segment_map
);
5298 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5302 m
->p_type
= PT_INTERP
;
5310 /* Look through the sections. We put sections in the same program
5311 segment when the start of the second section can be placed within
5312 a few bytes of the end of the first section. */
5318 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
5320 && (dynsec
->flags
& SEC_LOAD
) == 0)
5323 if ((abfd
->flags
& D_PAGED
) == 0)
5324 phdr_in_segment
= false;
5326 /* Deal with -Ttext or something similar such that the first section
5327 is not adjacent to the program headers. This is an
5328 approximation, since at this point we don't know exactly how many
5329 program headers we will need. */
5330 if (phdr_in_segment
&& count
> 0)
5332 bfd_vma phdr_lma
; /* Bytes. */
5333 bool separate_phdr
= false;
5335 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
5337 && info
->separate_code
5338 && (sections
[0]->flags
& SEC_CODE
) != 0)
5340 /* If data sections should be separate from code and
5341 thus not executable, and the first section is
5342 executable then put the file and program headers in
5343 their own PT_LOAD. */
5344 separate_phdr
= true;
5345 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
5346 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
5348 /* The file and program headers are currently on the
5349 same page as the first section. Put them on the
5350 previous page if we can. */
5351 if (phdr_lma
>= maxpagesize
)
5352 phdr_lma
-= maxpagesize
;
5354 separate_phdr
= false;
5357 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
5358 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
5359 /* If file and program headers would be placed at the end
5360 of memory then it's probably better to omit them. */
5361 phdr_in_segment
= false;
5362 else if (phdr_lma
< wrap_to
)
5363 /* If a section wraps around to where we'll be placing
5364 file and program headers, then the headers will be
5366 phdr_in_segment
= false;
5367 else if (separate_phdr
)
5369 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
5372 m
->p_paddr
= phdr_lma
* opb
;
5374 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
5375 m
->p_paddr_valid
= 1;
5378 phdr_in_segment
= false;
5382 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
5389 /* See if this section and the last one will fit in the same
5392 if (last_hdr
== NULL
)
5394 /* If we don't have a segment yet, then we don't need a new
5395 one (we build the last one after this loop). */
5396 new_segment
= false;
5398 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
5400 /* If this section has a different relation between the
5401 virtual address and the load address, then we need a new
5405 else if (hdr
->lma
< last_hdr
->lma
+ last_size
5406 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
5408 /* If this section has a load address that makes it overlap
5409 the previous section, then we need a new segment. */
5412 else if ((abfd
->flags
& D_PAGED
) != 0
5413 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
5414 == (hdr
->lma
& -maxpagesize
)))
5416 /* If we are demand paged then we can't map two disk
5417 pages onto the same memory page. */
5418 new_segment
= false;
5420 /* In the next test we have to be careful when last_hdr->lma is close
5421 to the end of the address space. If the aligned address wraps
5422 around to the start of the address space, then there are no more
5423 pages left in memory and it is OK to assume that the current
5424 section can be included in the current segment. */
5425 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
5426 + maxpagesize
> last_hdr
->lma
)
5427 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
5428 + maxpagesize
<= hdr
->lma
))
5430 /* If putting this section in this segment would force us to
5431 skip a page in the segment, then we need a new segment. */
5434 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
5435 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
5437 /* We don't want to put a loaded section after a
5438 nonloaded (ie. bss style) section in the same segment
5439 as that will force the non-loaded section to be loaded.
5440 Consider .tbss sections as loaded for this purpose. */
5443 else if ((abfd
->flags
& D_PAGED
) == 0)
5445 /* If the file is not demand paged, which means that we
5446 don't require the sections to be correctly aligned in the
5447 file, then there is no other reason for a new segment. */
5448 new_segment
= false;
5450 else if (info
!= NULL
5451 && info
->separate_code
5452 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
5457 && (hdr
->flags
& SEC_READONLY
) == 0)
5459 /* We don't want to put a writable section in a read only
5465 /* Otherwise, we can use the same segment. */
5466 new_segment
= false;
5469 /* Allow interested parties a chance to override our decision. */
5470 if (last_hdr
!= NULL
5472 && info
->callbacks
->override_segment_assignment
!= NULL
)
5474 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
5480 if ((hdr
->flags
& SEC_READONLY
) == 0)
5482 if ((hdr
->flags
& SEC_CODE
) != 0)
5485 /* .tbss sections effectively have zero size. */
5486 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5490 /* We need a new program segment. We must create a new program
5491 header holding all the sections from hdr_index until hdr. */
5493 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5500 if ((hdr
->flags
& SEC_READONLY
) == 0)
5505 if ((hdr
->flags
& SEC_CODE
) == 0)
5511 /* .tbss sections effectively have zero size. */
5512 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
5514 phdr_in_segment
= false;
5517 /* Create a final PT_LOAD program segment, but not if it's just
5519 if (last_hdr
!= NULL
5520 && (i
- hdr_index
!= 1
5521 || !IS_TBSS (last_hdr
)))
5523 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5531 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5534 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5541 /* For each batch of consecutive loadable SHT_NOTE sections,
5542 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5543 because if we link together nonloadable .note sections and
5544 loadable .note sections, we will generate two .note sections
5545 in the output file. */
5546 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5548 if ((s
->flags
& SEC_LOAD
) != 0
5549 && elf_section_type (s
) == SHT_NOTE
)
5552 unsigned int alignment_power
= s
->alignment_power
;
5555 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5557 if (s2
->next
->alignment_power
== alignment_power
5558 && (s2
->next
->flags
& SEC_LOAD
) != 0
5559 && elf_section_type (s2
->next
) == SHT_NOTE
5560 && align_power (s2
->lma
+ s2
->size
/ opb
,
5567 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5568 amt
+= count
* sizeof (asection
*);
5569 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5573 m
->p_type
= PT_NOTE
;
5577 m
->sections
[m
->count
- count
--] = s
;
5578 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5581 m
->sections
[m
->count
- 1] = s
;
5582 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5586 if (s
->flags
& SEC_THREAD_LOCAL
)
5592 if (first_mbind
== NULL
5593 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5597 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5600 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5601 amt
+= tls_count
* sizeof (asection
*);
5602 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5607 m
->count
= tls_count
;
5608 /* Mandated PF_R. */
5610 m
->p_flags_valid
= 1;
5612 for (i
= 0; i
< tls_count
; ++i
)
5614 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5617 (_("%pB: TLS sections are not adjacent:"), abfd
);
5620 while (i
< tls_count
)
5622 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5624 _bfd_error_handler (_(" TLS: %pA"), s
);
5628 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5631 bfd_set_error (bfd_error_bad_value
);
5643 && (abfd
->flags
& D_PAGED
) != 0
5644 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5645 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5646 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5647 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5649 /* Mandated PF_R. */
5650 unsigned long p_flags
= PF_R
;
5651 if ((s
->flags
& SEC_READONLY
) == 0)
5653 if ((s
->flags
& SEC_CODE
) != 0)
5656 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5657 m
= bfd_zalloc (abfd
, amt
);
5661 m
->p_type
= (PT_GNU_MBIND_LO
5662 + elf_section_data (s
)->this_hdr
.sh_info
);
5664 m
->p_flags_valid
= 1;
5666 m
->p_flags
= p_flags
;
5672 s
= bfd_get_section_by_name (abfd
,
5673 NOTE_GNU_PROPERTY_SECTION_NAME
);
5674 if (s
!= NULL
&& s
->size
!= 0)
5676 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5677 m
= bfd_zalloc (abfd
, amt
);
5681 m
->p_type
= PT_GNU_PROPERTY
;
5683 m
->p_flags_valid
= 1;
5690 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5692 eh_frame_hdr
= elf_eh_frame_hdr (info
);
5693 if (eh_frame_hdr
!= NULL
5694 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5696 amt
= sizeof (struct elf_segment_map
);
5697 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5701 m
->p_type
= PT_GNU_EH_FRAME
;
5703 m
->sections
[0] = eh_frame_hdr
->output_section
;
5709 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5711 sframe
= elf_sframe (abfd
);
5713 && (sframe
->output_section
->flags
& SEC_LOAD
) != 0
5714 && sframe
->size
!= 0)
5716 amt
= sizeof (struct elf_segment_map
);
5717 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5721 m
->p_type
= PT_GNU_SFRAME
;
5723 m
->sections
[0] = sframe
->output_section
;
5729 if (elf_stack_flags (abfd
))
5731 amt
= sizeof (struct elf_segment_map
);
5732 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5736 m
->p_type
= PT_GNU_STACK
;
5737 m
->p_flags
= elf_stack_flags (abfd
);
5738 m
->p_align
= bed
->stack_align
;
5739 m
->p_flags_valid
= 1;
5740 m
->p_align_valid
= m
->p_align
!= 0;
5741 if (info
->stacksize
> 0)
5743 m
->p_size
= info
->stacksize
;
5744 m
->p_size_valid
= 1;
5751 if (info
!= NULL
&& info
->relro
)
5753 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5755 if (m
->p_type
== PT_LOAD
5757 && m
->sections
[0]->vma
>= info
->relro_start
5758 && m
->sections
[0]->vma
< info
->relro_end
)
5761 while (--i
!= (unsigned) -1)
5763 if (m
->sections
[i
]->size
> 0
5764 && (m
->sections
[i
]->flags
& SEC_LOAD
) != 0
5765 && (m
->sections
[i
]->flags
& SEC_HAS_CONTENTS
) != 0)
5769 if (i
!= (unsigned) -1)
5774 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5777 amt
= sizeof (struct elf_segment_map
);
5778 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5782 m
->p_type
= PT_GNU_RELRO
;
5789 elf_seg_map (abfd
) = mfirst
;
5792 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
|| info
== NULL
))
5795 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5797 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5806 /* Sort sections by address. */
5809 elf_sort_sections (const void *arg1
, const void *arg2
)
5811 const asection
*sec1
= *(const asection
**) arg1
;
5812 const asection
*sec2
= *(const asection
**) arg2
;
5813 bfd_size_type size1
, size2
;
5815 /* Sort by LMA first, since this is the address used to
5816 place the section into a segment. */
5817 if (sec1
->lma
< sec2
->lma
)
5819 else if (sec1
->lma
> sec2
->lma
)
5822 /* Then sort by VMA. Normally the LMA and the VMA will be
5823 the same, and this will do nothing. */
5824 if (sec1
->vma
< sec2
->vma
)
5826 else if (sec1
->vma
> sec2
->vma
)
5829 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5831 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5839 else if (TOEND (sec2
))
5844 /* Sort by size, to put zero sized sections
5845 before others at the same address. */
5847 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5848 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5855 return sec1
->target_index
- sec2
->target_index
;
5858 /* This qsort comparison functions sorts PT_LOAD segments first and
5859 by p_paddr, for assign_file_positions_for_load_sections. */
5862 elf_sort_segments (const void *arg1
, const void *arg2
)
5864 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5865 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5867 if (m1
->p_type
!= m2
->p_type
)
5869 if (m1
->p_type
== PT_NULL
)
5871 if (m2
->p_type
== PT_NULL
)
5873 return m1
->p_type
< m2
->p_type
? -1 : 1;
5875 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5876 return m1
->includes_filehdr
? -1 : 1;
5877 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5878 return m1
->no_sort_lma
? -1 : 1;
5879 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5881 bfd_vma lma1
, lma2
; /* Octets. */
5883 if (m1
->p_paddr_valid
)
5885 else if (m1
->count
!= 0)
5887 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5889 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5892 if (m2
->p_paddr_valid
)
5894 else if (m2
->count
!= 0)
5896 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5898 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5901 return lma1
< lma2
? -1 : 1;
5903 if (m1
->idx
!= m2
->idx
)
5904 return m1
->idx
< m2
->idx
? -1 : 1;
5908 /* Ian Lance Taylor writes:
5910 We shouldn't be using % with a negative signed number. That's just
5911 not good. We have to make sure either that the number is not
5912 negative, or that the number has an unsigned type. When the types
5913 are all the same size they wind up as unsigned. When file_ptr is a
5914 larger signed type, the arithmetic winds up as signed long long,
5917 What we're trying to say here is something like ``increase OFF by
5918 the least amount that will cause it to be equal to the VMA modulo
5920 /* In other words, something like:
5922 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5923 off_offset = off % bed->maxpagesize;
5924 if (vma_offset < off_offset)
5925 adjustment = vma_offset + bed->maxpagesize - off_offset;
5927 adjustment = vma_offset - off_offset;
5929 which can be collapsed into the expression below. */
5932 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5934 /* PR binutils/16199: Handle an alignment of zero. */
5935 if (maxpagesize
== 0)
5937 return ((vma
- off
) % maxpagesize
);
5941 print_segment_map (const struct elf_segment_map
*m
)
5944 const char *pt
= get_segment_type (m
->p_type
);
5949 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5950 sprintf (buf
, "LOPROC+%7.7x",
5951 (unsigned int) (m
->p_type
- PT_LOPROC
));
5952 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5953 sprintf (buf
, "LOOS+%7.7x",
5954 (unsigned int) (m
->p_type
- PT_LOOS
));
5956 snprintf (buf
, sizeof (buf
), "%8.8x",
5957 (unsigned int) m
->p_type
);
5961 fprintf (stderr
, "%s:", pt
);
5962 for (j
= 0; j
< m
->count
; j
++)
5963 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5968 /* Assign file positions to the sections based on the mapping from
5969 sections to segments. This function also sets up some fields in
5973 assign_file_positions_for_load_sections (bfd
*abfd
,
5974 struct bfd_link_info
*link_info
)
5976 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5977 struct elf_segment_map
*m
;
5978 struct elf_segment_map
*phdr_load_seg
;
5979 Elf_Internal_Phdr
*phdrs
;
5980 Elf_Internal_Phdr
*p
;
5981 file_ptr off
; /* Octets. */
5982 bfd_size_type maxpagesize
;
5983 unsigned int alloc
, actual
;
5985 struct elf_segment_map
**sorted_seg_map
;
5986 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5988 if (link_info
== NULL
5989 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
, NULL
))
5993 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5998 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5999 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
6003 /* PR binutils/12467. */
6004 elf_elfheader (abfd
)->e_phoff
= 0;
6005 elf_elfheader (abfd
)->e_phentsize
= 0;
6008 elf_elfheader (abfd
)->e_phnum
= alloc
;
6010 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
6013 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
6017 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
6018 BFD_ASSERT (elf_program_header_size (abfd
)
6019 == actual
* bed
->s
->sizeof_phdr
);
6020 BFD_ASSERT (actual
>= alloc
);
6025 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
6029 /* We're writing the size in elf_program_header_size (abfd),
6030 see assign_file_positions_except_relocs, so make sure we have
6031 that amount allocated, with trailing space cleared.
6032 The variable alloc contains the computed need, while
6033 elf_program_header_size (abfd) contains the size used for the
6035 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6036 where the layout is forced to according to a larger size in the
6037 last iterations for the testcase ld-elf/header. */
6038 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
6039 + alloc
* sizeof (*sorted_seg_map
)));
6040 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
6041 elf_tdata (abfd
)->phdr
= phdrs
;
6045 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
6047 sorted_seg_map
[j
] = m
;
6048 /* If elf_segment_map is not from map_sections_to_segments, the
6049 sections may not be correctly ordered. NOTE: sorting should
6050 not be done to the PT_NOTE section of a corefile, which may
6051 contain several pseudo-sections artificially created by bfd.
6052 Sorting these pseudo-sections breaks things badly. */
6054 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
6055 && m
->p_type
== PT_NOTE
))
6057 for (i
= 0; i
< m
->count
; i
++)
6058 m
->sections
[i
]->target_index
= i
;
6059 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
6064 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
6068 if ((abfd
->flags
& D_PAGED
) != 0)
6070 if (link_info
!= NULL
)
6071 maxpagesize
= link_info
->maxpagesize
;
6073 maxpagesize
= bed
->maxpagesize
;
6076 /* Sections must map to file offsets past the ELF file header. */
6077 off
= bed
->s
->sizeof_ehdr
;
6078 /* And if one of the PT_LOAD headers doesn't include the program
6079 headers then we'll be mapping program headers in the usual
6080 position after the ELF file header. */
6081 phdr_load_seg
= NULL
;
6082 for (j
= 0; j
< alloc
; j
++)
6084 m
= sorted_seg_map
[j
];
6085 if (m
->p_type
!= PT_LOAD
)
6087 if (m
->includes_phdrs
)
6093 if (phdr_load_seg
== NULL
)
6094 off
+= actual
* bed
->s
->sizeof_phdr
;
6096 for (j
= 0; j
< alloc
; j
++)
6099 bfd_vma off_adjust
; /* Octets. */
6101 bfd_size_type p_align
;
6104 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6105 number of sections with contents contributing to both p_filesz
6106 and p_memsz, followed by a number of sections with no contents
6107 that just contribute to p_memsz. In this loop, OFF tracks next
6108 available file offset for PT_LOAD and PT_NOTE segments. */
6109 m
= sorted_seg_map
[j
];
6111 p
->p_type
= m
->p_type
;
6112 p
->p_flags
= m
->p_flags
;
6113 p_align
= bed
->p_align
;
6117 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
6119 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
6121 if (m
->p_paddr_valid
)
6122 p
->p_paddr
= m
->p_paddr
;
6123 else if (m
->count
== 0)
6126 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
6128 if (p
->p_type
== PT_LOAD
6129 && (abfd
->flags
& D_PAGED
) != 0)
6131 /* p_align in demand paged PT_LOAD segments effectively stores
6132 the maximum page size. When copying an executable with
6133 objcopy, we set m->p_align from the input file. Use this
6134 value for maxpagesize rather than bed->maxpagesize, which
6135 may be different. Note that we use maxpagesize for PT_TLS
6136 segment alignment later in this function, so we are relying
6137 on at least one PT_LOAD segment appearing before a PT_TLS
6139 if (m
->p_align_valid
)
6140 maxpagesize
= m
->p_align
;
6141 else if (p_align
!= 0
6142 && (link_info
== NULL
6143 || !link_info
->maxpagesize_is_set
))
6144 /* Set p_align to the default p_align value while laying
6145 out segments aligning to the maximum page size or the
6146 largest section alignment. The run-time loader can
6147 align segments to the default p_align value or the
6148 maximum page size, depending on system page size. */
6151 p
->p_align
= maxpagesize
;
6153 else if (m
->p_align_valid
)
6154 p
->p_align
= m
->p_align
;
6155 else if (m
->count
== 0)
6156 p
->p_align
= 1 << bed
->s
->log_file_align
;
6158 if (m
== phdr_load_seg
)
6160 if (!m
->includes_filehdr
)
6162 off
+= actual
* bed
->s
->sizeof_phdr
;
6165 no_contents
= false;
6167 if (p
->p_type
== PT_LOAD
6170 bfd_size_type align
; /* Bytes. */
6171 unsigned int align_power
= 0;
6173 if (m
->p_align_valid
)
6177 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
6179 unsigned int secalign
;
6181 secalign
= bfd_section_alignment (*secpp
);
6182 if (secalign
> align_power
)
6183 align_power
= secalign
;
6185 align
= (bfd_size_type
) 1 << align_power
;
6186 if (align
< maxpagesize
)
6188 /* If a section requires alignment higher than the
6189 default p_align value, don't set p_align to the
6190 default p_align value. */
6191 if (align
> p_align
)
6193 align
= maxpagesize
;
6197 /* If a section requires alignment higher than the
6198 maximum page size, set p_align to the section
6205 for (i
= 0; i
< m
->count
; i
++)
6206 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
6207 /* If we aren't making room for this section, then
6208 it must be SHT_NOBITS regardless of what we've
6209 set via struct bfd_elf_special_section. */
6210 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
6212 /* Find out whether this segment contains any loadable
6215 for (i
= 0; i
< m
->count
; i
++)
6216 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
6218 no_contents
= false;
6222 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
6224 /* Broken hardware and/or kernel require that files do not
6225 map the same page with different permissions on some hppa
6228 && (abfd
->flags
& D_PAGED
) != 0
6229 && bed
->no_page_alias
6230 && (off
& (maxpagesize
- 1)) != 0
6231 && ((off
& -maxpagesize
)
6232 == ((off
+ off_adjust
) & -maxpagesize
)))
6233 off_adjust
+= maxpagesize
;
6237 /* We shouldn't need to align the segment on disk since
6238 the segment doesn't need file space, but the gABI
6239 arguably requires the alignment and glibc ld.so
6240 checks it. So to comply with the alignment
6241 requirement but not waste file space, we adjust
6242 p_offset for just this segment. (OFF_ADJUST is
6243 subtracted from OFF later.) This may put p_offset
6244 past the end of file, but that shouldn't matter. */
6249 /* Make sure the .dynamic section is the first section in the
6250 PT_DYNAMIC segment. */
6251 else if (p
->p_type
== PT_DYNAMIC
6253 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
6256 (_("%pB: The first section in the PT_DYNAMIC segment"
6257 " is not the .dynamic section"),
6259 bfd_set_error (bfd_error_bad_value
);
6262 /* Set the note section type to SHT_NOTE. */
6263 else if (p
->p_type
== PT_NOTE
)
6264 for (i
= 0; i
< m
->count
; i
++)
6265 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
6267 if (m
->includes_filehdr
)
6269 if (!m
->p_flags_valid
)
6271 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
6272 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
6273 if (p
->p_type
== PT_LOAD
)
6277 if (p
->p_vaddr
< (bfd_vma
) off
6278 || (!m
->p_paddr_valid
6279 && p
->p_paddr
< (bfd_vma
) off
))
6282 (_("%pB: not enough room for program headers,"
6283 " try linking with -N"),
6285 bfd_set_error (bfd_error_bad_value
);
6289 if (!m
->p_paddr_valid
)
6293 else if (sorted_seg_map
[0]->includes_filehdr
)
6295 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
6296 p
->p_vaddr
= filehdr
->p_vaddr
;
6297 if (!m
->p_paddr_valid
)
6298 p
->p_paddr
= filehdr
->p_paddr
;
6302 if (m
->includes_phdrs
)
6304 if (!m
->p_flags_valid
)
6306 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
6307 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
6308 if (!m
->includes_filehdr
)
6310 if (p
->p_type
== PT_LOAD
)
6312 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
6315 p
->p_vaddr
-= off
- p
->p_offset
;
6316 if (!m
->p_paddr_valid
)
6317 p
->p_paddr
-= off
- p
->p_offset
;
6320 else if (phdr_load_seg
!= NULL
)
6322 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
6323 bfd_vma phdr_off
= 0; /* Octets. */
6324 if (phdr_load_seg
->includes_filehdr
)
6325 phdr_off
= bed
->s
->sizeof_ehdr
;
6326 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
6327 if (!m
->p_paddr_valid
)
6328 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
6329 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
6332 p
->p_offset
= bed
->s
->sizeof_ehdr
;
6336 if (p
->p_type
== PT_LOAD
6337 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
6339 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
6344 /* Put meaningless p_offset for PT_LOAD segments
6345 without file contents somewhere within the first
6346 page, in an attempt to not point past EOF. */
6347 bfd_size_type align
= maxpagesize
;
6348 if (align
< p
->p_align
)
6352 p
->p_offset
= off
% align
;
6357 file_ptr adjust
; /* Octets. */
6359 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
6361 p
->p_filesz
+= adjust
;
6362 p
->p_memsz
+= adjust
;
6366 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6367 maps. Set filepos for sections in PT_LOAD segments, and in
6368 core files, for sections in PT_NOTE segments.
6369 assign_file_positions_for_non_load_sections will set filepos
6370 for other sections and update p_filesz for other segments. */
6371 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
6374 bfd_size_type align
;
6375 Elf_Internal_Shdr
*this_hdr
;
6378 this_hdr
= &elf_section_data (sec
)->this_hdr
;
6379 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
6381 if ((p
->p_type
== PT_LOAD
6382 || p
->p_type
== PT_TLS
)
6383 && (this_hdr
->sh_type
!= SHT_NOBITS
6384 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
6385 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
6386 || p
->p_type
== PT_TLS
))))
6388 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
6389 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
6390 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
6391 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
6395 || p_end
< p_start
))
6398 /* xgettext:c-format */
6399 (_("%pB: section %pA lma %#" PRIx64
6400 " adjusted to %#" PRIx64
),
6401 abfd
, sec
, (uint64_t) s_start
/ opb
,
6402 (uint64_t) p_end
/ opb
);
6404 sec
->lma
= p_end
/ opb
;
6406 p
->p_memsz
+= adjust
;
6408 if (p
->p_type
== PT_LOAD
)
6410 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6413 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
6415 /* We have a PROGBITS section following NOBITS ones.
6416 Allocate file space for the NOBITS section(s).
6417 We don't need to write out the zeros, posix
6418 fseek past the end of data already written
6419 followed by a write at that location is
6420 guaranteed to result in zeros being read
6422 adjust
= p
->p_memsz
- p
->p_filesz
;
6425 /* We only adjust sh_offset in SHT_NOBITS sections
6426 as would seem proper for their address when the
6427 section is first in the segment. sh_offset
6428 doesn't really have any significance for
6429 SHT_NOBITS anyway, apart from a notional position
6430 relative to other sections. Historically we
6431 didn't bother with adjusting sh_offset and some
6432 programs depend on it not being adjusted. See
6433 pr12921 and pr25662. */
6434 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
6437 if (this_hdr
->sh_type
== SHT_NOBITS
)
6438 off_adjust
+= adjust
;
6441 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6442 p
->p_filesz
+= adjust
;
6445 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
6447 /* The section at i == 0 is the one that actually contains
6451 this_hdr
->sh_offset
= sec
->filepos
= off
;
6452 off
+= this_hdr
->sh_size
;
6453 p
->p_filesz
= this_hdr
->sh_size
;
6459 /* The rest are fake sections that shouldn't be written. */
6468 if (p
->p_type
== PT_LOAD
)
6470 this_hdr
->sh_offset
= sec
->filepos
= off
;
6471 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6472 off
+= this_hdr
->sh_size
;
6474 else if (this_hdr
->sh_type
== SHT_NOBITS
6475 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
6476 && this_hdr
->sh_offset
== 0)
6478 /* This is a .tbss section that didn't get a PT_LOAD.
6479 (See _bfd_elf_map_sections_to_segments "Create a
6480 final PT_LOAD".) Set sh_offset to the value it
6481 would have if we had created a zero p_filesz and
6482 p_memsz PT_LOAD header for the section. This
6483 also makes the PT_TLS header have the same
6485 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
6487 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
6490 if (this_hdr
->sh_type
!= SHT_NOBITS
)
6492 p
->p_filesz
+= this_hdr
->sh_size
;
6493 /* A load section without SHF_ALLOC is something like
6494 a note section in a PT_NOTE segment. These take
6495 file space but are not loaded into memory. */
6496 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6497 p
->p_memsz
+= this_hdr
->sh_size
;
6499 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
6501 if (p
->p_type
== PT_TLS
)
6502 p
->p_memsz
+= this_hdr
->sh_size
;
6504 /* .tbss is special. It doesn't contribute to p_memsz of
6506 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
6507 p
->p_memsz
+= this_hdr
->sh_size
;
6510 if (align
> p
->p_align
6511 && !m
->p_align_valid
6512 && (p
->p_type
!= PT_LOAD
6513 || (abfd
->flags
& D_PAGED
) == 0))
6517 if (!m
->p_flags_valid
)
6520 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
6522 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
6529 /* PR ld/20815 - Check that the program header segment, if
6530 present, will be loaded into memory. */
6531 if (p
->p_type
== PT_PHDR
6532 && phdr_load_seg
== NULL
6533 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
6534 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
6536 /* The fix for this error is usually to edit the linker script being
6537 used and set up the program headers manually. Either that or
6538 leave room for the headers at the start of the SECTIONS. */
6539 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6540 " by LOAD segment"),
6542 if (link_info
== NULL
)
6544 /* Arrange for the linker to exit with an error, deleting
6545 the output file unless --noinhibit-exec is given. */
6546 link_info
->callbacks
->info ("%X");
6549 /* Check that all sections are in a PT_LOAD segment.
6550 Don't check funky gdb generated core files. */
6551 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
6553 bool check_vma
= true;
6555 for (i
= 1; i
< m
->count
; i
++)
6556 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
6557 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
6558 ->this_hdr
), p
) != 0
6559 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
6560 ->this_hdr
), p
) != 0)
6562 /* Looks like we have overlays packed into the segment. */
6567 for (i
= 0; i
< m
->count
; i
++)
6569 Elf_Internal_Shdr
*this_hdr
;
6572 sec
= m
->sections
[i
];
6573 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6574 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6575 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6578 /* xgettext:c-format */
6579 (_("%pB: section `%pA' can't be allocated in segment %d"),
6581 print_segment_map (m
);
6586 p
->p_align
= p_align
;
6590 elf_next_file_pos (abfd
) = off
;
6592 if (link_info
!= NULL
6593 && phdr_load_seg
!= NULL
6594 && phdr_load_seg
->includes_filehdr
)
6596 /* There is a segment that contains both the file headers and the
6597 program headers, so provide a symbol __ehdr_start pointing there.
6598 A program can use this to examine itself robustly. */
6600 struct elf_link_hash_entry
*hash
6601 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6602 false, false, true);
6603 /* If the symbol was referenced and not defined, define it. */
6605 && (hash
->root
.type
== bfd_link_hash_new
6606 || hash
->root
.type
== bfd_link_hash_undefined
6607 || hash
->root
.type
== bfd_link_hash_undefweak
6608 || hash
->root
.type
== bfd_link_hash_common
))
6611 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6613 if (phdr_load_seg
->count
!= 0)
6614 /* The segment contains sections, so use the first one. */
6615 s
= phdr_load_seg
->sections
[0];
6617 /* Use the first (i.e. lowest-addressed) section in any segment. */
6618 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6619 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6627 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6628 hash
->root
.u
.def
.section
= s
;
6632 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6633 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6636 hash
->root
.type
= bfd_link_hash_defined
;
6637 hash
->def_regular
= 1;
6645 /* Determine if a bfd is a debuginfo file. Unfortunately there
6646 is no defined method for detecting such files, so we have to
6647 use heuristics instead. */
6650 is_debuginfo_file (bfd
*abfd
)
6652 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6655 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6656 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6657 Elf_Internal_Shdr
**headerp
;
6659 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6661 Elf_Internal_Shdr
*header
= * headerp
;
6663 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6664 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6665 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6666 && header
->sh_type
!= SHT_NOBITS
6667 && header
->sh_type
!= SHT_NOTE
)
6674 /* Assign file positions for other sections, except for compressed debug
6675 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6678 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6679 struct bfd_link_info
*link_info
)
6681 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6682 Elf_Internal_Shdr
**i_shdrpp
;
6683 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6684 Elf_Internal_Phdr
*phdrs
;
6685 Elf_Internal_Phdr
*p
;
6686 struct elf_segment_map
*m
;
6688 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6689 bfd_vma maxpagesize
;
6691 if (link_info
!= NULL
)
6692 maxpagesize
= link_info
->maxpagesize
;
6694 maxpagesize
= bed
->maxpagesize
;
6695 i_shdrpp
= elf_elfsections (abfd
);
6696 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6697 off
= elf_next_file_pos (abfd
);
6698 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6700 Elf_Internal_Shdr
*hdr
;
6704 if (hdr
->bfd_section
!= NULL
6705 && (hdr
->bfd_section
->filepos
!= 0
6706 || (hdr
->sh_type
== SHT_NOBITS
6707 && hdr
->contents
== NULL
)))
6708 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6709 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6711 if (hdr
->sh_size
!= 0
6712 /* PR 24717 - debuginfo files are known to be not strictly
6713 compliant with the ELF standard. In particular they often
6714 have .note.gnu.property sections that are outside of any
6715 loadable segment. This is not a problem for such files,
6716 so do not warn about them. */
6717 && ! is_debuginfo_file (abfd
))
6719 /* xgettext:c-format */
6720 (_("%pB: warning: allocated section `%s' not in segment"),
6722 (hdr
->bfd_section
== NULL
6724 : hdr
->bfd_section
->name
));
6725 /* We don't need to page align empty sections. */
6726 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6727 align
= maxpagesize
;
6729 align
= hdr
->sh_addralign
& -hdr
->sh_addralign
;
6730 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
, align
);
6731 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6734 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6735 && hdr
->bfd_section
== NULL
)
6736 /* We don't know the offset of these sections yet:
6737 their size has not been decided. */
6738 || (abfd
->is_linker_output
6739 && hdr
->bfd_section
!= NULL
6740 && (hdr
->sh_name
== -1u
6741 || bfd_section_is_ctf (hdr
->bfd_section
)))
6742 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6743 || (elf_symtab_shndx_list (abfd
) != NULL
6744 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6745 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6746 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6747 hdr
->sh_offset
= -1;
6749 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6751 elf_next_file_pos (abfd
) = off
;
6753 /* Now that we have set the section file positions, we can set up
6754 the file positions for the non PT_LOAD segments. */
6755 phdrs
= elf_tdata (abfd
)->phdr
;
6756 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6758 if (p
->p_type
== PT_GNU_RELRO
)
6760 bfd_vma start
, end
; /* Bytes. */
6763 if (link_info
!= NULL
)
6765 /* During linking the range of the RELRO segment is passed
6766 in link_info. Note that there may be padding between
6767 relro_start and the first RELRO section. */
6768 start
= link_info
->relro_start
;
6769 end
= link_info
->relro_end
;
6771 else if (m
->count
!= 0)
6773 if (!m
->p_size_valid
)
6775 start
= m
->sections
[0]->vma
;
6776 end
= start
+ m
->p_size
/ opb
;
6787 struct elf_segment_map
*lm
;
6788 const Elf_Internal_Phdr
*lp
;
6791 /* Find a LOAD segment containing a section in the RELRO
6793 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6795 lm
= lm
->next
, lp
++)
6797 if (lp
->p_type
== PT_LOAD
6799 && (lm
->sections
[lm
->count
- 1]->vma
6800 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6801 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6803 && lm
->sections
[0]->vma
< end
)
6809 /* Find the section starting the RELRO segment. */
6810 for (i
= 0; i
< lm
->count
; i
++)
6812 asection
*s
= lm
->sections
[i
];
6821 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6822 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6823 p
->p_offset
= lm
->sections
[i
]->filepos
;
6824 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6825 p
->p_filesz
= p
->p_memsz
;
6827 /* The RELRO segment typically ends a few bytes
6828 into .got.plt but other layouts are possible.
6829 In cases where the end does not match any
6830 loaded section (for instance is in file
6831 padding), trim p_filesz back to correspond to
6832 the end of loaded section contents. */
6833 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6834 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6836 /* Preserve the alignment and flags if they are
6837 valid. The gold linker generates RW/4 for
6838 the PT_GNU_RELRO section. It is better for
6839 objcopy/strip to honor these attributes
6840 otherwise gdb will choke when using separate
6842 if (!m
->p_align_valid
)
6844 if (!m
->p_flags_valid
)
6853 if (link_info
!= NULL
)
6855 (_("%pB: warning: unable to allocate any sections"
6856 " to PT_GNU_RELRO segment"),
6858 memset (p
, 0, sizeof *p
);
6861 else if (p
->p_type
== PT_GNU_STACK
)
6863 if (m
->p_size_valid
)
6864 p
->p_memsz
= m
->p_size
;
6866 else if (m
->count
!= 0)
6870 if (p
->p_type
!= PT_LOAD
6871 && (p
->p_type
!= PT_NOTE
6872 || bfd_get_format (abfd
) != bfd_core
))
6874 /* A user specified segment layout may include a PHDR
6875 segment that overlaps with a LOAD segment... */
6876 if (p
->p_type
== PT_PHDR
)
6882 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6884 /* PR 17512: file: 2195325e. */
6886 (_("%pB: error: non-load segment %d includes file header "
6887 "and/or program header"),
6888 abfd
, (int) (p
- phdrs
));
6893 p
->p_offset
= m
->sections
[0]->filepos
;
6894 for (i
= m
->count
; i
-- != 0;)
6896 asection
*sect
= m
->sections
[i
];
6897 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6898 if (hdr
->sh_type
!= SHT_NOBITS
)
6900 p
->p_filesz
= sect
->filepos
- p
->p_offset
+ hdr
->sh_size
;
6901 /* NB: p_memsz of the loadable PT_NOTE segment
6902 should be the same as p_filesz. */
6903 if (p
->p_type
== PT_NOTE
6904 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
6905 p
->p_memsz
= p
->p_filesz
;
6916 static elf_section_list
*
6917 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6919 for (;list
!= NULL
; list
= list
->next
)
6925 /* Work out the file positions of all the sections. This is called by
6926 _bfd_elf_compute_section_file_positions. All the section sizes and
6927 VMAs must be known before this is called.
6929 Reloc sections come in two flavours: Those processed specially as
6930 "side-channel" data attached to a section to which they apply, and
6931 those that bfd doesn't process as relocations. The latter sort are
6932 stored in a normal bfd section by bfd_section_from_shdr. We don't
6933 consider the former sort here, unless they form part of the loadable
6934 image. Reloc sections not assigned here (and compressed debugging
6935 sections and CTF sections which nothing else in the file can rely
6936 upon) will be handled later by assign_file_positions_for_relocs.
6938 We also don't set the positions of the .symtab and .strtab here. */
6941 assign_file_positions_except_relocs (bfd
*abfd
,
6942 struct bfd_link_info
*link_info
)
6944 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6945 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6946 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6949 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6950 && bfd_get_format (abfd
) != bfd_core
)
6952 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6953 unsigned int num_sec
= elf_numsections (abfd
);
6954 Elf_Internal_Shdr
**hdrpp
;
6958 /* Start after the ELF header. */
6959 off
= i_ehdrp
->e_ehsize
;
6961 /* We are not creating an executable, which means that we are
6962 not creating a program header, and that the actual order of
6963 the sections in the file is unimportant. */
6964 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6966 Elf_Internal_Shdr
*hdr
;
6969 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6970 && hdr
->bfd_section
== NULL
)
6971 /* Do not assign offsets for these sections yet: we don't know
6973 || (abfd
->is_linker_output
6974 && hdr
->bfd_section
!= NULL
6975 && (hdr
->sh_name
== -1u
6976 || bfd_section_is_ctf (hdr
->bfd_section
)))
6977 || i
== elf_onesymtab (abfd
)
6978 || (elf_symtab_shndx_list (abfd
) != NULL
6979 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6980 || i
== elf_strtab_sec (abfd
)
6981 || i
== elf_shstrtab_sec (abfd
))
6983 hdr
->sh_offset
= -1;
6986 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
6989 elf_next_file_pos (abfd
) = off
;
6990 elf_program_header_size (abfd
) = 0;
6994 /* Assign file positions for the loaded sections based on the
6995 assignment of sections to segments. */
6996 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6999 /* And for non-load sections. */
7000 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
7004 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
7007 /* Write out the program headers. */
7008 alloc
= i_ehdrp
->e_phnum
;
7011 if (link_info
!= NULL
&& ! link_info
->no_warn_rwx_segments
)
7013 bool warned_tls
= false;
7014 bool warned_rwx
= false;
7016 /* Memory resident segments with non-zero size and RWX
7017 permissions are a security risk, so we generate a warning
7018 here if we are creating any. */
7021 for (i
= 0; i
< alloc
; i
++)
7023 const Elf_Internal_Phdr
* phdr
= tdata
->phdr
+ i
;
7025 if (phdr
->p_memsz
== 0)
7029 && phdr
->p_type
== PT_TLS
7030 && (phdr
->p_flags
& PF_X
))
7032 if (link_info
->warn_is_error_for_rwx_segments
)
7034 _bfd_error_handler (_("\
7035 error: %pB has a TLS segment with execute permission"),
7040 _bfd_error_handler (_("\
7041 warning: %pB has a TLS segment with execute permission"),
7048 else if (! warned_rwx
7049 && phdr
->p_type
== PT_LOAD
7050 && ((phdr
->p_flags
& (PF_R
| PF_W
| PF_X
))
7051 == (PF_R
| PF_W
| PF_X
)))
7053 if (link_info
->warn_is_error_for_rwx_segments
)
7055 _bfd_error_handler (_("\
7056 error: %pB has a LOAD segment with RWX permissions"),
7061 _bfd_error_handler (_("\
7062 warning: %pB has a LOAD segment with RWX permissions"),
7072 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
7073 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
7081 _bfd_elf_init_file_header (bfd
*abfd
,
7082 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
7084 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
7085 struct elf_strtab_hash
*shstrtab
;
7086 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7088 i_ehdrp
= elf_elfheader (abfd
);
7090 shstrtab
= _bfd_elf_strtab_init ();
7091 if (shstrtab
== NULL
)
7094 elf_shstrtab (abfd
) = shstrtab
;
7096 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
7097 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
7098 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
7099 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
7101 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
7102 i_ehdrp
->e_ident
[EI_DATA
] =
7103 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
7104 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
7106 if ((abfd
->flags
& DYNAMIC
) != 0)
7107 i_ehdrp
->e_type
= ET_DYN
;
7108 else if ((abfd
->flags
& EXEC_P
) != 0)
7109 i_ehdrp
->e_type
= ET_EXEC
;
7110 else if (bfd_get_format (abfd
) == bfd_core
)
7111 i_ehdrp
->e_type
= ET_CORE
;
7113 i_ehdrp
->e_type
= ET_REL
;
7115 switch (bfd_get_arch (abfd
))
7117 case bfd_arch_unknown
:
7118 i_ehdrp
->e_machine
= EM_NONE
;
7121 /* There used to be a long list of cases here, each one setting
7122 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7123 in the corresponding bfd definition. To avoid duplication,
7124 the switch was removed. Machines that need special handling
7125 can generally do it in elf_backend_final_write_processing(),
7126 unless they need the information earlier than the final write.
7127 Such need can generally be supplied by replacing the tests for
7128 e_machine with the conditions used to determine it. */
7130 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
7133 i_ehdrp
->e_version
= bed
->s
->ev_current
;
7134 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
7136 /* No program header, for now. */
7137 i_ehdrp
->e_phoff
= 0;
7138 i_ehdrp
->e_phentsize
= 0;
7139 i_ehdrp
->e_phnum
= 0;
7141 /* Each bfd section is section header entry. */
7142 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
7143 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
7145 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
7146 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
7147 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
7148 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
7149 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
7150 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
7151 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
7152 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
7153 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
7159 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7161 FIXME: We used to have code here to sort the PT_LOAD segments into
7162 ascending order, as per the ELF spec. But this breaks some programs,
7163 including the Linux kernel. But really either the spec should be
7164 changed or the programs updated. */
7167 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
7169 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
7171 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
7172 unsigned int num_segments
= i_ehdrp
->e_phnum
;
7173 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
7174 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
7175 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
7177 /* Find the lowest p_vaddr in PT_LOAD segments. */
7178 bfd_vma p_vaddr
= (bfd_vma
) -1;
7179 for (; segment
< end_segment
; segment
++)
7180 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
7181 p_vaddr
= segment
->p_vaddr
;
7183 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7184 segments is non-zero. */
7186 i_ehdrp
->e_type
= ET_EXEC
;
7191 /* Assign file positions for all the reloc sections which are not part
7192 of the loadable file image, and the file position of section headers. */
7195 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
7198 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
7199 Elf_Internal_Shdr
*shdrp
;
7200 Elf_Internal_Ehdr
*i_ehdrp
;
7201 const struct elf_backend_data
*bed
;
7203 /* Skip non-load sections without section header. */
7204 if ((abfd
->flags
& BFD_NO_SECTION_HEADER
) != 0)
7207 off
= elf_next_file_pos (abfd
);
7209 shdrpp
= elf_elfsections (abfd
);
7210 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
7211 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
7214 if (shdrp
->sh_offset
== -1)
7216 asection
*sec
= shdrp
->bfd_section
;
7218 || shdrp
->sh_type
== SHT_REL
7219 || shdrp
->sh_type
== SHT_RELA
)
7221 else if (bfd_section_is_ctf (sec
))
7223 /* Update section size and contents. */
7224 shdrp
->sh_size
= sec
->size
;
7225 shdrp
->contents
= sec
->contents
;
7227 else if (shdrp
->sh_name
== -1u)
7229 const char *name
= sec
->name
;
7230 struct bfd_elf_section_data
*d
;
7232 /* Compress DWARF debug sections. */
7233 if (!bfd_compress_section (abfd
, sec
, shdrp
->contents
))
7236 if (sec
->compress_status
== COMPRESS_SECTION_DONE
7237 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0
7240 /* If section is compressed with zlib-gnu, convert
7241 section name from .debug_* to .zdebug_*. */
7242 char *new_name
= bfd_debug_name_to_zdebug (abfd
, name
);
7243 if (new_name
== NULL
)
7247 /* Add section name to section name section. */
7249 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
7251 d
= elf_section_data (sec
);
7253 /* Add reloc section name to section name section. */
7255 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rel
.hdr
,
7259 && !_bfd_elf_set_reloc_sh_name (abfd
, d
->rela
.hdr
,
7263 /* Update section size and contents. */
7264 shdrp
->sh_size
= sec
->size
;
7265 shdrp
->contents
= sec
->contents
;
7266 sec
->contents
= NULL
;
7269 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
7273 /* Place section name section after DWARF debug sections have been
7275 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
7276 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
7277 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
7278 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
7280 /* Place the section headers. */
7281 i_ehdrp
= elf_elfheader (abfd
);
7282 bed
= get_elf_backend_data (abfd
);
7283 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
7284 i_ehdrp
->e_shoff
= off
;
7285 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
7286 elf_next_file_pos (abfd
) = off
;
7292 _bfd_elf_write_object_contents (bfd
*abfd
)
7294 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7295 Elf_Internal_Shdr
**i_shdrp
;
7297 unsigned int count
, num_sec
;
7298 struct elf_obj_tdata
*t
;
7300 if (! abfd
->output_has_begun
7301 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7303 /* Do not rewrite ELF data when the BFD has been opened for update.
7304 abfd->output_has_begun was set to TRUE on opening, so creation of
7305 new sections, and modification of existing section sizes was
7306 restricted. This means the ELF header, program headers and
7307 section headers can't have changed. If the contents of any
7308 sections has been modified, then those changes have already been
7309 written to the BFD. */
7310 else if (abfd
->direction
== both_direction
)
7312 BFD_ASSERT (abfd
->output_has_begun
);
7316 i_shdrp
= elf_elfsections (abfd
);
7319 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
7323 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
7326 /* After writing the headers, we need to write the sections too... */
7327 num_sec
= elf_numsections (abfd
);
7328 for (count
= 1; count
< num_sec
; count
++)
7330 /* Don't set the sh_name field without section header. */
7331 if ((abfd
->flags
& BFD_NO_SECTION_HEADER
) == 0)
7332 i_shdrp
[count
]->sh_name
7333 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
7334 i_shdrp
[count
]->sh_name
);
7335 if (bed
->elf_backend_section_processing
)
7336 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
7338 if (i_shdrp
[count
]->contents
)
7340 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
7342 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
7343 || bfd_write (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
7348 /* Write out the section header names. */
7349 t
= elf_tdata (abfd
);
7350 if (elf_shstrtab (abfd
) != NULL
7351 && t
->shstrtab_hdr
.sh_offset
!= -1
7352 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
7353 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
7356 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
7359 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
7362 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7363 if (t
->o
->build_id
.after_write_object_contents
!= NULL
7364 && !(*t
->o
->build_id
.after_write_object_contents
) (abfd
))
7366 if (t
->o
->package_metadata
.after_write_object_contents
!= NULL
7367 && !(*t
->o
->package_metadata
.after_write_object_contents
) (abfd
))
7374 _bfd_elf_write_corefile_contents (bfd
*abfd
)
7376 /* Hopefully this can be done just like an object file. */
7377 return _bfd_elf_write_object_contents (abfd
);
7380 /* Given a section, search the header to find them. */
7383 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
7385 const struct elf_backend_data
*bed
;
7386 unsigned int sec_index
;
7388 if (elf_section_data (asect
) != NULL
7389 && elf_section_data (asect
)->this_idx
!= 0)
7390 return elf_section_data (asect
)->this_idx
;
7392 if (bfd_is_abs_section (asect
))
7393 sec_index
= SHN_ABS
;
7394 else if (bfd_is_com_section (asect
))
7395 sec_index
= SHN_COMMON
;
7396 else if (bfd_is_und_section (asect
))
7397 sec_index
= SHN_UNDEF
;
7399 sec_index
= SHN_BAD
;
7401 bed
= get_elf_backend_data (abfd
);
7402 if (bed
->elf_backend_section_from_bfd_section
)
7404 int retval
= sec_index
;
7406 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
7410 if (sec_index
== SHN_BAD
)
7411 bfd_set_error (bfd_error_nonrepresentable_section
);
7416 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7420 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
7422 asymbol
*asym_ptr
= *asym_ptr_ptr
;
7424 flagword flags
= asym_ptr
->flags
;
7426 /* When gas creates relocations against local labels, it creates its
7427 own symbol for the section, but does put the symbol into the
7428 symbol chain, so udata is 0. When the linker is generating
7429 relocatable output, this section symbol may be for one of the
7430 input sections rather than the output section. */
7431 if (asym_ptr
->udata
.i
== 0
7432 && (flags
& BSF_SECTION_SYM
)
7433 && asym_ptr
->section
)
7437 sec
= asym_ptr
->section
;
7438 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
7439 sec
= sec
->output_section
;
7440 if (sec
->owner
== abfd
7441 && sec
->index
< elf_num_section_syms (abfd
)
7442 && elf_section_syms (abfd
)[sec
->index
] != NULL
)
7443 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
7446 idx
= asym_ptr
->udata
.i
;
7450 /* This case can occur when using --strip-symbol on a symbol
7451 which is used in a relocation entry. */
7453 /* xgettext:c-format */
7454 (_("%pB: symbol `%s' required but not present"),
7455 abfd
, bfd_asymbol_name (asym_ptr
));
7456 bfd_set_error (bfd_error_no_symbols
);
7463 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7464 " flags = 0x%.8x\n",
7465 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
7473 static inline bfd_vma
7474 segment_size (Elf_Internal_Phdr
*segment
)
7476 return (segment
->p_memsz
> segment
->p_filesz
7477 ? segment
->p_memsz
: segment
->p_filesz
);
7481 /* Returns the end address of the segment + 1. */
7482 static inline bfd_vma
7483 segment_end (Elf_Internal_Phdr
*segment
, bfd_vma start
)
7485 return start
+ segment_size (segment
);
7488 static inline bfd_size_type
7489 section_size (asection
*section
, Elf_Internal_Phdr
*segment
)
7491 if ((section
->flags
& SEC_HAS_CONTENTS
) != 0
7492 || (section
->flags
& SEC_THREAD_LOCAL
) == 0
7493 || segment
->p_type
== PT_TLS
)
7494 return section
->size
;
7498 /* Returns TRUE if the given section is contained within the given
7499 segment. LMA addresses are compared against PADDR when
7500 USE_VADDR is false, VMA against VADDR when true. */
7502 is_contained_by (asection
*section
, Elf_Internal_Phdr
*segment
,
7503 bfd_vma paddr
, bfd_vma vaddr
, unsigned int opb
,
7506 bfd_vma seg_addr
= !use_vaddr
? paddr
: vaddr
;
7507 bfd_vma addr
= !use_vaddr
? section
->lma
: section
->vma
;
7509 if (_bfd_mul_overflow (addr
, opb
, &octet
))
7511 /* The third and fourth lines below are testing that the section end
7512 address is within the segment. It's written this way to avoid
7513 overflow. Add seg_addr + section_size to both sides of the
7514 inequality to make it obvious. */
7515 return (octet
>= seg_addr
7516 && segment_size (segment
) >= section_size (section
, segment
)
7517 && (octet
- seg_addr
7518 <= segment_size (segment
) - section_size (section
, segment
)));
7521 /* Handle PT_NOTE segment. */
7523 is_note (asection
*s
, Elf_Internal_Phdr
*p
)
7525 return (p
->p_type
== PT_NOTE
7526 && elf_section_type (s
) == SHT_NOTE
7527 && (ufile_ptr
) s
->filepos
>= p
->p_offset
7528 && p
->p_filesz
>= s
->size
7529 && (ufile_ptr
) s
->filepos
- p
->p_offset
<= p
->p_filesz
- s
->size
);
7532 /* Rewrite program header information. */
7535 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
, bfd_vma maxpagesize
)
7537 Elf_Internal_Ehdr
*iehdr
;
7538 struct elf_segment_map
*map
;
7539 struct elf_segment_map
*map_first
;
7540 struct elf_segment_map
**pointer_to_map
;
7541 Elf_Internal_Phdr
*segment
;
7544 unsigned int num_segments
;
7545 bool phdr_included
= false;
7547 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
7548 unsigned int phdr_adjust_num
= 0;
7549 const struct elf_backend_data
*bed
;
7550 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7552 bed
= get_elf_backend_data (ibfd
);
7553 iehdr
= elf_elfheader (ibfd
);
7556 pointer_to_map
= &map_first
;
7558 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7560 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7561 linker, which generates a PT_INTERP section with p_vaddr and
7562 p_memsz set to 0. */
7563 #define IS_SOLARIS_PT_INTERP(p, s) \
7565 && p->p_paddr == 0 \
7566 && p->p_memsz == 0 \
7567 && p->p_filesz > 0 \
7568 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7570 && (bfd_vma) s->filepos >= p->p_offset \
7571 && ((bfd_vma) s->filepos + s->size \
7572 <= p->p_offset + p->p_filesz))
7574 /* Decide if the given section should be included in the given segment.
7575 A section will be included if:
7576 1. It is within the address space of the segment -- we use the LMA
7577 if that is set for the segment and the VMA otherwise,
7578 2. It is an allocated section or a NOTE section in a PT_NOTE
7580 3. There is an output section associated with it,
7581 4. The section has not already been allocated to a previous segment.
7582 5. PT_GNU_STACK segments do not include any sections.
7583 6. PT_TLS segment includes only SHF_TLS sections.
7584 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7585 8. PT_DYNAMIC should not contain empty sections at the beginning
7586 (with the possible exception of .dynamic). */
7587 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7588 (((is_contained_by (section, segment, segment->p_paddr, \
7589 segment->p_vaddr, opb, \
7590 bed->want_p_paddr_set_to_zero) \
7591 && (section->flags & SEC_ALLOC) != 0) \
7592 || is_note (section, segment)) \
7593 && segment->p_type != PT_GNU_STACK \
7594 && (segment->p_type != PT_TLS \
7595 || (section->flags & SEC_THREAD_LOCAL)) \
7596 && (segment->p_type == PT_LOAD \
7597 || segment->p_type == PT_TLS \
7598 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7599 && (segment->p_type != PT_DYNAMIC \
7600 || section_size (section, segment) > 0 \
7601 || (segment->p_paddr \
7602 ? segment->p_paddr != section->lma * (opb) \
7603 : segment->p_vaddr != section->vma * (opb)) \
7604 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7605 && (segment->p_type != PT_LOAD || !section->segment_mark))
7607 /* If the output section of a section in the input segment is NULL,
7608 it is removed from the corresponding output segment. */
7609 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7610 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7611 && section->output_section != NULL)
7613 /* Returns TRUE iff seg1 starts after the end of seg2. */
7614 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7615 (seg1->field >= segment_end (seg2, seg2->field))
7617 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7618 their VMA address ranges and their LMA address ranges overlap.
7619 It is possible to have overlapping VMA ranges without overlapping LMA
7620 ranges. RedBoot images for example can have both .data and .bss mapped
7621 to the same VMA range, but with the .data section mapped to a different
7623 #define SEGMENT_OVERLAPS(seg1, seg2) \
7624 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7625 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7626 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7627 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7629 /* Initialise the segment mark field, and discard stupid alignment. */
7630 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
7632 asection
*o
= section
->output_section
;
7633 if (o
!= NULL
&& o
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
7634 o
->alignment_power
= 0;
7635 section
->segment_mark
= false;
7638 /* The Solaris linker creates program headers in which all the
7639 p_paddr fields are zero. When we try to objcopy or strip such a
7640 file, we get confused. Check for this case, and if we find it
7641 don't set the p_paddr_valid fields. */
7642 p_paddr_valid
= false;
7643 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7646 if (segment
->p_paddr
!= 0)
7648 p_paddr_valid
= true;
7652 /* Scan through the segments specified in the program header
7653 of the input BFD. For this first scan we look for overlaps
7654 in the loadable segments. These can be created by weird
7655 parameters to objcopy. Also, fix some solaris weirdness. */
7656 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7661 Elf_Internal_Phdr
*segment2
;
7663 if (segment
->p_type
== PT_INTERP
)
7664 for (section
= ibfd
->sections
; section
; section
= section
->next
)
7665 if (IS_SOLARIS_PT_INTERP (segment
, section
))
7667 /* Mininal change so that the normal section to segment
7668 assignment code will work. */
7669 segment
->p_vaddr
= section
->vma
* opb
;
7673 if (segment
->p_type
!= PT_LOAD
)
7675 /* Remove PT_GNU_RELRO segment. */
7676 if (segment
->p_type
== PT_GNU_RELRO
)
7677 segment
->p_type
= PT_NULL
;
7681 /* Determine if this segment overlaps any previous segments. */
7682 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7684 bfd_signed_vma extra_length
;
7686 if (segment2
->p_type
!= PT_LOAD
7687 || !SEGMENT_OVERLAPS (segment
, segment2
))
7690 /* Merge the two segments together. */
7691 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7693 /* Extend SEGMENT2 to include SEGMENT and then delete
7695 extra_length
= (segment_end (segment
, segment
->p_vaddr
)
7696 - segment_end (segment2
, segment2
->p_vaddr
));
7698 if (extra_length
> 0)
7700 segment2
->p_memsz
+= extra_length
;
7701 segment2
->p_filesz
+= extra_length
;
7704 segment
->p_type
= PT_NULL
;
7706 /* Since we have deleted P we must restart the outer loop. */
7708 segment
= elf_tdata (ibfd
)->phdr
;
7713 /* Extend SEGMENT to include SEGMENT2 and then delete
7715 extra_length
= (segment_end (segment2
, segment2
->p_vaddr
)
7716 - segment_end (segment
, segment
->p_vaddr
));
7718 if (extra_length
> 0)
7720 segment
->p_memsz
+= extra_length
;
7721 segment
->p_filesz
+= extra_length
;
7724 segment2
->p_type
= PT_NULL
;
7729 /* The second scan attempts to assign sections to segments. */
7730 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7734 unsigned int section_count
;
7735 asection
**sections
;
7736 asection
*output_section
;
7738 asection
*matching_lma
;
7739 asection
*suggested_lma
;
7742 asection
*first_section
;
7744 if (segment
->p_type
== PT_NULL
)
7747 first_section
= NULL
;
7748 /* Compute how many sections might be placed into this segment. */
7749 for (section
= ibfd
->sections
, section_count
= 0;
7751 section
= section
->next
)
7753 /* Find the first section in the input segment, which may be
7754 removed from the corresponding output segment. */
7755 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7757 if (first_section
== NULL
)
7758 first_section
= section
;
7759 if (section
->output_section
!= NULL
)
7764 /* Allocate a segment map big enough to contain
7765 all of the sections we have selected. */
7766 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7767 amt
+= section_count
* sizeof (asection
*);
7768 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7772 /* Initialise the fields of the segment map. Default to
7773 using the physical address of the segment in the input BFD. */
7775 map
->p_type
= segment
->p_type
;
7776 map
->p_flags
= segment
->p_flags
;
7777 map
->p_flags_valid
= 1;
7779 if (map
->p_type
== PT_LOAD
7780 && (ibfd
->flags
& D_PAGED
) != 0
7782 && segment
->p_align
> 1)
7784 map
->p_align
= segment
->p_align
;
7785 if (segment
->p_align
> maxpagesize
)
7786 map
->p_align
= maxpagesize
;
7787 map
->p_align_valid
= 1;
7790 /* If the first section in the input segment is removed, there is
7791 no need to preserve segment physical address in the corresponding
7793 if (!first_section
|| first_section
->output_section
!= NULL
)
7795 map
->p_paddr
= segment
->p_paddr
;
7796 map
->p_paddr_valid
= p_paddr_valid
;
7799 /* Determine if this segment contains the ELF file header
7800 and if it contains the program headers themselves. */
7801 map
->includes_filehdr
= (segment
->p_offset
== 0
7802 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7803 map
->includes_phdrs
= 0;
7805 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7807 map
->includes_phdrs
=
7808 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7809 && (segment
->p_offset
+ segment
->p_filesz
7810 >= ((bfd_vma
) iehdr
->e_phoff
7811 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7813 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7814 phdr_included
= true;
7817 if (section_count
== 0)
7819 /* Special segments, such as the PT_PHDR segment, may contain
7820 no sections, but ordinary, loadable segments should contain
7821 something. They are allowed by the ELF spec however, so only
7822 a warning is produced.
7823 Don't warn if an empty PT_LOAD contains the program headers.
7824 There is however the valid use case of embedded systems which
7825 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7826 flash memory with zeros. No warning is shown for that case. */
7827 if (segment
->p_type
== PT_LOAD
7828 && !map
->includes_phdrs
7829 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7830 /* xgettext:c-format */
7832 (_("%pB: warning: empty loadable segment detected"
7833 " at vaddr=%#" PRIx64
", is this intentional?"),
7834 ibfd
, (uint64_t) segment
->p_vaddr
);
7836 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7838 *pointer_to_map
= map
;
7839 pointer_to_map
= &map
->next
;
7844 /* Now scan the sections in the input BFD again and attempt
7845 to add their corresponding output sections to the segment map.
7846 The problem here is how to handle an output section which has
7847 been moved (ie had its LMA changed). There are four possibilities:
7849 1. None of the sections have been moved.
7850 In this case we can continue to use the segment LMA from the
7853 2. All of the sections have been moved by the same amount.
7854 In this case we can change the segment's LMA to match the LMA
7855 of the first section.
7857 3. Some of the sections have been moved, others have not.
7858 In this case those sections which have not been moved can be
7859 placed in the current segment which will have to have its size,
7860 and possibly its LMA changed, and a new segment or segments will
7861 have to be created to contain the other sections.
7863 4. The sections have been moved, but not by the same amount.
7864 In this case we can change the segment's LMA to match the LMA
7865 of the first section and we will have to create a new segment
7866 or segments to contain the other sections.
7868 In order to save time, we allocate an array to hold the section
7869 pointers that we are interested in. As these sections get assigned
7870 to a segment, they are removed from this array. */
7872 amt
= section_count
* sizeof (asection
*);
7873 sections
= (asection
**) bfd_malloc (amt
);
7874 if (sections
== NULL
)
7877 /* Step One: Scan for segment vs section LMA conflicts.
7878 Also add the sections to the section array allocated above.
7879 Also add the sections to the current segment. In the common
7880 case, where the sections have not been moved, this means that
7881 we have completely filled the segment, and there is nothing
7884 matching_lma
= NULL
;
7885 suggested_lma
= NULL
;
7887 for (section
= first_section
, j
= 0;
7889 section
= section
->next
)
7891 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7893 output_section
= section
->output_section
;
7895 sections
[j
++] = section
;
7897 /* The Solaris native linker always sets p_paddr to 0.
7898 We try to catch that case here, and set it to the
7899 correct value. Note - some backends require that
7900 p_paddr be left as zero. */
7902 && segment
->p_vaddr
!= 0
7903 && !bed
->want_p_paddr_set_to_zero
7905 && output_section
->lma
!= 0
7906 && (align_power (segment
->p_vaddr
7907 + (map
->includes_filehdr
7908 ? iehdr
->e_ehsize
: 0)
7909 + (map
->includes_phdrs
7910 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7912 output_section
->alignment_power
* opb
)
7913 == (output_section
->vma
* opb
)))
7914 map
->p_paddr
= segment
->p_vaddr
;
7916 /* Match up the physical address of the segment with the
7917 LMA address of the output section. */
7918 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
7920 || is_note (section
, segment
))
7922 if (matching_lma
== NULL
7923 || output_section
->lma
< matching_lma
->lma
)
7924 matching_lma
= output_section
;
7926 /* We assume that if the section fits within the segment
7927 then it does not overlap any other section within that
7929 map
->sections
[isec
++] = output_section
;
7931 else if (suggested_lma
== NULL
)
7932 suggested_lma
= output_section
;
7934 if (j
== section_count
)
7939 BFD_ASSERT (j
== section_count
);
7941 /* Step Two: Adjust the physical address of the current segment,
7943 if (isec
== section_count
)
7945 /* All of the sections fitted within the segment as currently
7946 specified. This is the default case. Add the segment to
7947 the list of built segments and carry on to process the next
7948 program header in the input BFD. */
7949 map
->count
= section_count
;
7950 *pointer_to_map
= map
;
7951 pointer_to_map
= &map
->next
;
7954 && !bed
->want_p_paddr_set_to_zero
)
7956 bfd_vma hdr_size
= 0;
7957 if (map
->includes_filehdr
)
7958 hdr_size
= iehdr
->e_ehsize
;
7959 if (map
->includes_phdrs
)
7960 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7962 /* Account for padding before the first section in the
7964 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7965 - matching_lma
->lma
);
7973 /* Change the current segment's physical address to match
7974 the LMA of the first section that fitted, or if no
7975 section fitted, the first section. */
7976 if (matching_lma
== NULL
)
7977 matching_lma
= suggested_lma
;
7979 map
->p_paddr
= matching_lma
->lma
* opb
;
7981 /* Offset the segment physical address from the lma
7982 to allow for space taken up by elf headers. */
7983 if (map
->includes_phdrs
)
7985 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7987 /* iehdr->e_phnum is just an estimate of the number
7988 of program headers that we will need. Make a note
7989 here of the number we used and the segment we chose
7990 to hold these headers, so that we can adjust the
7991 offset when we know the correct value. */
7992 phdr_adjust_num
= iehdr
->e_phnum
;
7993 phdr_adjust_seg
= map
;
7996 if (map
->includes_filehdr
)
7998 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7999 map
->p_paddr
-= iehdr
->e_ehsize
;
8000 /* We've subtracted off the size of headers from the
8001 first section lma, but there may have been some
8002 alignment padding before that section too. Try to
8003 account for that by adjusting the segment lma down to
8004 the same alignment. */
8005 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
8006 align
= segment
->p_align
;
8007 map
->p_paddr
&= -(align
* opb
);
8011 /* Step Three: Loop over the sections again, this time assigning
8012 those that fit to the current segment and removing them from the
8013 sections array; but making sure not to leave large gaps. Once all
8014 possible sections have been assigned to the current segment it is
8015 added to the list of built segments and if sections still remain
8016 to be assigned, a new segment is constructed before repeating
8022 suggested_lma
= NULL
;
8024 /* Fill the current segment with sections that fit. */
8025 for (j
= 0; j
< section_count
; j
++)
8027 section
= sections
[j
];
8029 if (section
== NULL
)
8032 output_section
= section
->output_section
;
8034 BFD_ASSERT (output_section
!= NULL
);
8036 if (is_contained_by (output_section
, segment
, map
->p_paddr
,
8038 || is_note (section
, segment
))
8040 if (map
->count
== 0)
8042 /* If the first section in a segment does not start at
8043 the beginning of the segment, then something is
8045 if (align_power (map
->p_paddr
8046 + (map
->includes_filehdr
8047 ? iehdr
->e_ehsize
: 0)
8048 + (map
->includes_phdrs
8049 ? iehdr
->e_phnum
* iehdr
->e_phentsize
8051 output_section
->alignment_power
* opb
)
8052 != output_section
->lma
* opb
)
8059 prev_sec
= map
->sections
[map
->count
- 1];
8061 /* If the gap between the end of the previous section
8062 and the start of this section is more than
8063 maxpagesize then we need to start a new segment. */
8064 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
8066 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
8067 || (prev_sec
->lma
+ prev_sec
->size
8068 > output_section
->lma
))
8070 if (suggested_lma
== NULL
)
8071 suggested_lma
= output_section
;
8077 map
->sections
[map
->count
++] = output_section
;
8080 if (segment
->p_type
== PT_LOAD
)
8081 section
->segment_mark
= true;
8083 else if (suggested_lma
== NULL
)
8084 suggested_lma
= output_section
;
8087 /* PR 23932. A corrupt input file may contain sections that cannot
8088 be assigned to any segment - because for example they have a
8089 negative size - or segments that do not contain any sections.
8090 But there are also valid reasons why a segment can be empty.
8091 So allow a count of zero. */
8093 /* Add the current segment to the list of built segments. */
8094 *pointer_to_map
= map
;
8095 pointer_to_map
= &map
->next
;
8097 if (isec
< section_count
)
8099 /* We still have not allocated all of the sections to
8100 segments. Create a new segment here, initialise it
8101 and carry on looping. */
8102 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
8103 amt
+= section_count
* sizeof (asection
*);
8104 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
8111 /* Initialise the fields of the segment map. Set the physical
8112 physical address to the LMA of the first section that has
8113 not yet been assigned. */
8115 map
->p_type
= segment
->p_type
;
8116 map
->p_flags
= segment
->p_flags
;
8117 map
->p_flags_valid
= 1;
8118 map
->p_paddr
= suggested_lma
->lma
* opb
;
8119 map
->p_paddr_valid
= p_paddr_valid
;
8120 map
->includes_filehdr
= 0;
8121 map
->includes_phdrs
= 0;
8126 bfd_set_error (bfd_error_sorry
);
8130 while (isec
< section_count
);
8135 elf_seg_map (obfd
) = map_first
;
8137 /* If we had to estimate the number of program headers that were
8138 going to be needed, then check our estimate now and adjust
8139 the offset if necessary. */
8140 if (phdr_adjust_seg
!= NULL
)
8144 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
8147 if (count
> phdr_adjust_num
)
8148 phdr_adjust_seg
->p_paddr
8149 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
8151 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
8152 if (map
->p_type
== PT_PHDR
)
8155 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
8156 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
8161 #undef IS_SOLARIS_PT_INTERP
8162 #undef IS_SECTION_IN_INPUT_SEGMENT
8163 #undef INCLUDE_SECTION_IN_SEGMENT
8164 #undef SEGMENT_AFTER_SEGMENT
8165 #undef SEGMENT_OVERLAPS
8169 /* Return true if p_align in the ELF program header in ABFD is valid. */
8172 elf_is_p_align_valid (bfd
*abfd
)
8175 Elf_Internal_Phdr
*segment
;
8176 unsigned int num_segments
;
8177 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8178 bfd_size_type maxpagesize
= bed
->maxpagesize
;
8179 bfd_size_type p_align
= bed
->p_align
;
8181 /* Return true if the default p_align value isn't set or the maximum
8182 page size is the same as the minimum page size. */
8183 if (p_align
== 0 || maxpagesize
== bed
->minpagesize
)
8186 /* When the default p_align value is set, p_align may be set to the
8187 default p_align value while segments are aligned to the maximum
8188 page size. In this case, the input p_align will be ignored and
8189 the maximum page size will be used to align the output segments. */
8190 segment
= elf_tdata (abfd
)->phdr
;
8191 num_segments
= elf_elfheader (abfd
)->e_phnum
;
8192 for (i
= 0; i
< num_segments
; i
++, segment
++)
8193 if (segment
->p_type
== PT_LOAD
8194 && (segment
->p_align
!= p_align
8195 || vma_page_aligned_bias (segment
->p_vaddr
,
8203 /* Copy ELF program header information. */
8206 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
8208 Elf_Internal_Ehdr
*iehdr
;
8209 struct elf_segment_map
*map
;
8210 struct elf_segment_map
*map_first
;
8211 struct elf_segment_map
**pointer_to_map
;
8212 Elf_Internal_Phdr
*segment
;
8214 unsigned int num_segments
;
8215 bool phdr_included
= false;
8217 bool p_palign_valid
;
8218 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
8220 iehdr
= elf_elfheader (ibfd
);
8223 pointer_to_map
= &map_first
;
8225 /* If all the segment p_paddr fields are zero, don't set
8226 map->p_paddr_valid. */
8227 p_paddr_valid
= false;
8228 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8229 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8232 if (segment
->p_paddr
!= 0)
8234 p_paddr_valid
= true;
8238 p_palign_valid
= elf_is_p_align_valid (ibfd
);
8240 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8245 unsigned int section_count
;
8247 Elf_Internal_Shdr
*this_hdr
;
8248 asection
*first_section
= NULL
;
8249 asection
*lowest_section
;
8251 /* Compute how many sections are in this segment. */
8252 for (section
= ibfd
->sections
, section_count
= 0;
8254 section
= section
->next
)
8256 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8257 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8259 if (first_section
== NULL
)
8260 first_section
= section
;
8265 /* Allocate a segment map big enough to contain
8266 all of the sections we have selected. */
8267 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
8268 amt
+= section_count
* sizeof (asection
*);
8269 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
8273 /* Initialize the fields of the output segment map with the
8276 map
->p_type
= segment
->p_type
;
8277 map
->p_flags
= segment
->p_flags
;
8278 map
->p_flags_valid
= 1;
8279 map
->p_paddr
= segment
->p_paddr
;
8280 map
->p_paddr_valid
= p_paddr_valid
;
8281 map
->p_align
= segment
->p_align
;
8282 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8283 map
->p_align_valid
= (map
->p_type
== PT_GNU_STACK
8285 map
->p_vaddr_offset
= 0;
8287 if (map
->p_type
== PT_GNU_RELRO
8288 || map
->p_type
== PT_GNU_STACK
)
8290 /* The PT_GNU_RELRO segment may contain the first a few
8291 bytes in the .got.plt section even if the whole .got.plt
8292 section isn't in the PT_GNU_RELRO segment. We won't
8293 change the size of the PT_GNU_RELRO segment.
8294 Similarly, PT_GNU_STACK size is significant on uclinux
8296 map
->p_size
= segment
->p_memsz
;
8297 map
->p_size_valid
= 1;
8300 /* Determine if this segment contains the ELF file header
8301 and if it contains the program headers themselves. */
8302 map
->includes_filehdr
= (segment
->p_offset
== 0
8303 && segment
->p_filesz
>= iehdr
->e_ehsize
);
8305 map
->includes_phdrs
= 0;
8306 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
8308 map
->includes_phdrs
=
8309 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
8310 && (segment
->p_offset
+ segment
->p_filesz
8311 >= ((bfd_vma
) iehdr
->e_phoff
8312 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
8314 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
8315 phdr_included
= true;
8318 lowest_section
= NULL
;
8319 if (section_count
!= 0)
8321 unsigned int isec
= 0;
8323 for (section
= first_section
;
8325 section
= section
->next
)
8327 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8328 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8330 map
->sections
[isec
++] = section
->output_section
;
8331 if ((section
->flags
& SEC_ALLOC
) != 0)
8335 if (lowest_section
== NULL
8336 || section
->lma
< lowest_section
->lma
)
8337 lowest_section
= section
;
8339 /* Section lmas are set up from PT_LOAD header
8340 p_paddr in _bfd_elf_make_section_from_shdr.
8341 If this header has a p_paddr that disagrees
8342 with the section lma, flag the p_paddr as
8344 if ((section
->flags
& SEC_LOAD
) != 0)
8345 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
8347 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
8348 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
8349 map
->p_paddr_valid
= false;
8351 if (isec
== section_count
)
8357 if (section_count
== 0)
8358 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
8359 else if (map
->p_paddr_valid
)
8361 /* Account for padding before the first section in the segment. */
8362 bfd_vma hdr_size
= 0;
8363 if (map
->includes_filehdr
)
8364 hdr_size
= iehdr
->e_ehsize
;
8365 if (map
->includes_phdrs
)
8366 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
8368 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
8369 - (lowest_section
? lowest_section
->lma
: 0));
8372 map
->count
= section_count
;
8373 *pointer_to_map
= map
;
8374 pointer_to_map
= &map
->next
;
8377 elf_seg_map (obfd
) = map_first
;
8381 /* Copy private BFD data. This copies or rewrites ELF program header
8385 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
8387 bfd_vma maxpagesize
;
8389 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8390 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8393 if (elf_tdata (ibfd
)->phdr
== NULL
)
8396 if (ibfd
->xvec
== obfd
->xvec
)
8398 /* Check to see if any sections in the input BFD
8399 covered by ELF program header have changed. */
8400 Elf_Internal_Phdr
*segment
;
8401 asection
*section
, *osec
;
8402 unsigned int i
, num_segments
;
8403 Elf_Internal_Shdr
*this_hdr
;
8404 const struct elf_backend_data
*bed
;
8406 bed
= get_elf_backend_data (ibfd
);
8408 /* Regenerate the segment map if p_paddr is set to 0. */
8409 if (bed
->want_p_paddr_set_to_zero
)
8412 /* Initialize the segment mark field. */
8413 for (section
= obfd
->sections
; section
!= NULL
;
8414 section
= section
->next
)
8415 section
->segment_mark
= false;
8417 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8418 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8422 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8423 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8424 which severly confuses things, so always regenerate the segment
8425 map in this case. */
8426 if (segment
->p_paddr
== 0
8427 && segment
->p_memsz
== 0
8428 && (segment
->p_type
== PT_INTERP
8429 || segment
->p_type
== PT_DYNAMIC
))
8432 for (section
= ibfd
->sections
;
8433 section
!= NULL
; section
= section
->next
)
8435 /* We mark the output section so that we know it comes
8436 from the input BFD. */
8437 osec
= section
->output_section
;
8439 osec
->segment_mark
= true;
8441 /* Check if this section is covered by the segment. */
8442 this_hdr
= &(elf_section_data(section
)->this_hdr
);
8443 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
8445 /* FIXME: Check if its output section is changed or
8446 removed. What else do we need to check? */
8448 || section
->flags
!= osec
->flags
8449 || section
->lma
!= osec
->lma
8450 || section
->vma
!= osec
->vma
8451 || section
->size
!= osec
->size
8452 || section
->rawsize
!= osec
->rawsize
8453 || section
->alignment_power
!= osec
->alignment_power
)
8459 /* Check to see if any output section do not come from the
8461 for (section
= obfd
->sections
; section
!= NULL
;
8462 section
= section
->next
)
8464 if (!section
->segment_mark
)
8467 section
->segment_mark
= false;
8470 return copy_elf_program_header (ibfd
, obfd
);
8475 if (ibfd
->xvec
== obfd
->xvec
)
8477 /* When rewriting program header, set the output maxpagesize to
8478 the maximum alignment of input PT_LOAD segments. */
8479 Elf_Internal_Phdr
*segment
;
8481 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
8483 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
8486 if (segment
->p_type
== PT_LOAD
8487 && maxpagesize
< segment
->p_align
)
8489 /* PR 17512: file: f17299af. */
8490 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
8491 /* xgettext:c-format */
8492 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8493 PRIx64
" is too large"),
8494 ibfd
, (uint64_t) segment
->p_align
);
8496 maxpagesize
= segment
->p_align
;
8499 if (maxpagesize
== 0)
8500 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
8502 return rewrite_elf_program_header (ibfd
, obfd
, maxpagesize
);
8505 /* Initialize private output section information from input section. */
8508 _bfd_elf_init_private_section_data (bfd
*ibfd
,
8512 struct bfd_link_info
*link_info
)
8515 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8516 bool final_link
= (link_info
!= NULL
8517 && !bfd_link_relocatable (link_info
));
8519 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8520 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8523 BFD_ASSERT (elf_section_data (osec
) != NULL
);
8525 /* If this is a known ABI section, ELF section type and flags may
8526 have been set up when OSEC was created. For normal sections we
8527 allow the user to override the type and flags other than
8528 SHF_MASKOS and SHF_MASKPROC. */
8529 if (elf_section_type (osec
) == SHT_PROGBITS
8530 || elf_section_type (osec
) == SHT_NOTE
8531 || elf_section_type (osec
) == SHT_NOBITS
)
8532 elf_section_type (osec
) = SHT_NULL
;
8533 /* For objcopy and relocatable link, copy the ELF section type from
8534 the input file if the BFD section flags are the same. (If they
8535 are different the user may be doing something like
8536 "objcopy --set-section-flags .text=alloc,data".) For a final
8537 link allow some flags that the linker clears to differ. */
8538 if (elf_section_type (osec
) == SHT_NULL
8539 && (osec
->flags
== isec
->flags
8541 && ((osec
->flags
^ isec
->flags
)
8542 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
8543 elf_section_type (osec
) = elf_section_type (isec
);
8545 /* FIXME: Is this correct for all OS/PROC specific flags? */
8546 elf_section_flags (osec
) = (elf_section_flags (isec
)
8547 & (SHF_MASKOS
| SHF_MASKPROC
));
8549 /* Copy sh_info from input for mbind section. */
8550 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
8551 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
8552 elf_section_data (osec
)->this_hdr
.sh_info
8553 = elf_section_data (isec
)->this_hdr
.sh_info
;
8555 /* Set things up for objcopy and relocatable link. The output
8556 SHT_GROUP section will have its elf_next_in_group pointing back
8557 to the input group members. Ignore linker created group section.
8558 See elfNN_ia64_object_p in elfxx-ia64.c. */
8559 if ((link_info
== NULL
8560 || !link_info
->resolve_section_groups
)
8561 && (elf_sec_group (isec
) == NULL
8562 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
8564 if (elf_section_flags (isec
) & SHF_GROUP
)
8565 elf_section_flags (osec
) |= SHF_GROUP
;
8566 elf_next_in_group (osec
) = elf_next_in_group (isec
);
8567 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
8570 /* If not decompress, preserve SHF_COMPRESSED. */
8571 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
8572 elf_section_flags (osec
) |= (elf_section_flags (isec
)
8575 ihdr
= &elf_section_data (isec
)->this_hdr
;
8577 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8578 don't use the output section of the linked-to section since it
8579 may be NULL at this point. */
8580 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
8582 ohdr
= &elf_section_data (osec
)->this_hdr
;
8583 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
8584 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
8587 osec
->use_rela_p
= isec
->use_rela_p
;
8592 /* Copy private section information. This copies over the entsize
8593 field, and sometimes the info field. */
8596 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
8601 Elf_Internal_Shdr
*ihdr
, *ohdr
;
8603 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
8604 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8607 ihdr
= &elf_section_data (isec
)->this_hdr
;
8608 ohdr
= &elf_section_data (osec
)->this_hdr
;
8610 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
8612 if (ihdr
->sh_type
== SHT_SYMTAB
8613 || ihdr
->sh_type
== SHT_DYNSYM
8614 || ihdr
->sh_type
== SHT_GNU_verneed
8615 || ihdr
->sh_type
== SHT_GNU_verdef
)
8616 ohdr
->sh_info
= ihdr
->sh_info
;
8618 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
8622 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8623 necessary if we are removing either the SHT_GROUP section or any of
8624 the group member sections. DISCARDED is the value that a section's
8625 output_section has if the section will be discarded, NULL when this
8626 function is called from objcopy, bfd_abs_section_ptr when called
8630 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
8634 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
8635 if (elf_section_type (isec
) == SHT_GROUP
)
8637 asection
*first
= elf_next_in_group (isec
);
8638 asection
*s
= first
;
8639 bfd_size_type removed
= 0;
8643 /* If this member section is being output but the
8644 SHT_GROUP section is not, then clear the group info
8645 set up by _bfd_elf_copy_private_section_data. */
8646 if (s
->output_section
!= discarded
8647 && isec
->output_section
== discarded
)
8649 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
8650 elf_group_name (s
->output_section
) = NULL
;
8654 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
8655 if (s
->output_section
== discarded
8656 && isec
->output_section
!= discarded
)
8658 /* Conversely, if the member section is not being
8659 output but the SHT_GROUP section is, then adjust
8662 if (elf_sec
->rel
.hdr
!= NULL
8663 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8665 if (elf_sec
->rela
.hdr
!= NULL
8666 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
8671 /* Also adjust for zero-sized relocation member
8673 if (elf_sec
->rel
.hdr
!= NULL
8674 && elf_sec
->rel
.hdr
->sh_size
== 0)
8676 if (elf_sec
->rela
.hdr
!= NULL
8677 && elf_sec
->rela
.hdr
->sh_size
== 0)
8681 s
= elf_next_in_group (s
);
8687 if (discarded
!= NULL
)
8689 /* If we've been called for ld -r, then we need to
8690 adjust the input section size. */
8691 if (isec
->rawsize
== 0)
8692 isec
->rawsize
= isec
->size
;
8693 isec
->size
= isec
->rawsize
- removed
;
8694 if (isec
->size
<= 4)
8697 isec
->flags
|= SEC_EXCLUDE
;
8700 else if (isec
->output_section
!= NULL
)
8702 /* Adjust the output section size when called from
8704 isec
->output_section
->size
-= removed
;
8705 if (isec
->output_section
->size
<= 4)
8707 isec
->output_section
->size
= 0;
8708 isec
->output_section
->flags
|= SEC_EXCLUDE
;
8717 /* Copy private header information. */
8720 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
8722 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8723 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8726 /* Copy over private BFD data if it has not already been copied.
8727 This must be done here, rather than in the copy_private_bfd_data
8728 entry point, because the latter is called after the section
8729 contents have been set, which means that the program headers have
8730 already been worked out. */
8731 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
8733 if (! copy_private_bfd_data (ibfd
, obfd
))
8737 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8740 /* Copy private symbol information. If this symbol is in a section
8741 which we did not map into a BFD section, try to map the section
8742 index correctly. We use special macro definitions for the mapped
8743 section indices; these definitions are interpreted by the
8744 swap_out_syms function. */
8746 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8747 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8748 #define MAP_STRTAB (SHN_HIOS + 3)
8749 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8750 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8753 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8758 elf_symbol_type
*isym
, *osym
;
8760 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8761 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8764 isym
= elf_symbol_from (isymarg
);
8765 osym
= elf_symbol_from (osymarg
);
8768 && isym
->internal_elf_sym
.st_shndx
!= 0
8770 && bfd_is_abs_section (isym
->symbol
.section
))
8774 shndx
= isym
->internal_elf_sym
.st_shndx
;
8775 if (shndx
== elf_onesymtab (ibfd
))
8776 shndx
= MAP_ONESYMTAB
;
8777 else if (shndx
== elf_dynsymtab (ibfd
))
8778 shndx
= MAP_DYNSYMTAB
;
8779 else if (shndx
== elf_strtab_sec (ibfd
))
8781 else if (shndx
== elf_shstrtab_sec (ibfd
))
8782 shndx
= MAP_SHSTRTAB
;
8783 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8784 shndx
= MAP_SYM_SHNDX
;
8785 osym
->internal_elf_sym
.st_shndx
= shndx
;
8791 /* Swap out the symbols. */
8794 swap_out_syms (bfd
*abfd
,
8795 struct elf_strtab_hash
**sttp
,
8797 struct bfd_link_info
*info
)
8799 const struct elf_backend_data
*bed
;
8800 unsigned int symcount
;
8802 struct elf_strtab_hash
*stt
;
8803 Elf_Internal_Shdr
*symtab_hdr
;
8804 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8805 Elf_Internal_Shdr
*symstrtab_hdr
;
8806 struct elf_sym_strtab
*symstrtab
;
8807 bfd_byte
*outbound_syms
;
8808 bfd_byte
*outbound_shndx
;
8809 unsigned long outbound_syms_index
;
8811 unsigned int num_locals
;
8813 bool name_local_sections
;
8815 if (!elf_map_symbols (abfd
, &num_locals
))
8818 /* Dump out the symtabs. */
8819 stt
= _bfd_elf_strtab_init ();
8823 bed
= get_elf_backend_data (abfd
);
8824 symcount
= bfd_get_symcount (abfd
);
8825 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8826 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8827 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8828 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8829 symtab_hdr
->sh_info
= num_locals
+ 1;
8830 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8832 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8833 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8835 /* Allocate buffer to swap out the .strtab section. */
8836 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8837 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8839 bfd_set_error (bfd_error_no_memory
);
8840 _bfd_elf_strtab_free (stt
);
8844 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8845 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8848 bfd_set_error (bfd_error_no_memory
);
8851 _bfd_elf_strtab_free (stt
);
8854 symtab_hdr
->contents
= outbound_syms
;
8855 outbound_syms_index
= 0;
8857 outbound_shndx
= NULL
;
8859 if (elf_symtab_shndx_list (abfd
))
8861 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8862 if (symtab_shndx_hdr
->sh_name
!= 0)
8864 if (_bfd_mul_overflow (symcount
+ 1,
8865 sizeof (Elf_External_Sym_Shndx
), &amt
))
8867 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8868 if (outbound_shndx
== NULL
)
8871 symtab_shndx_hdr
->contents
= outbound_shndx
;
8872 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8873 symtab_shndx_hdr
->sh_size
= amt
;
8874 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8875 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8877 /* FIXME: What about any other headers in the list ? */
8880 /* Now generate the data (for "contents"). */
8882 /* Fill in zeroth symbol and swap it out. */
8883 Elf_Internal_Sym sym
;
8889 sym
.st_shndx
= SHN_UNDEF
;
8890 sym
.st_target_internal
= 0;
8891 symstrtab
[0].sym
= sym
;
8892 symstrtab
[0].dest_index
= outbound_syms_index
;
8893 outbound_syms_index
++;
8897 = (bed
->elf_backend_name_local_section_symbols
8898 && bed
->elf_backend_name_local_section_symbols (abfd
));
8900 syms
= bfd_get_outsymbols (abfd
);
8901 for (idx
= 0; idx
< symcount
;)
8903 Elf_Internal_Sym sym
;
8904 bfd_vma value
= syms
[idx
]->value
;
8905 elf_symbol_type
*type_ptr
;
8906 flagword flags
= syms
[idx
]->flags
;
8909 if (!name_local_sections
8910 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8912 /* Local section symbols have no name. */
8913 sym
.st_name
= (unsigned long) -1;
8917 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8918 to get the final offset for st_name. */
8920 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8922 if (sym
.st_name
== (unsigned long) -1)
8926 type_ptr
= elf_symbol_from (syms
[idx
]);
8928 if ((flags
& BSF_SECTION_SYM
) == 0
8929 && bfd_is_com_section (syms
[idx
]->section
))
8931 /* ELF common symbols put the alignment into the `value' field,
8932 and the size into the `size' field. This is backwards from
8933 how BFD handles it, so reverse it here. */
8934 sym
.st_size
= value
;
8935 if (type_ptr
== NULL
8936 || type_ptr
->internal_elf_sym
.st_value
== 0)
8937 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8939 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8940 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8941 (abfd
, syms
[idx
]->section
);
8945 asection
*sec
= syms
[idx
]->section
;
8948 if (sec
->output_section
)
8950 value
+= sec
->output_offset
;
8951 sec
= sec
->output_section
;
8954 /* Don't add in the section vma for relocatable output. */
8955 if (! relocatable_p
)
8957 sym
.st_value
= value
;
8958 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8960 if (bfd_is_abs_section (sec
)
8962 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8964 /* This symbol is in a real ELF section which we did
8965 not create as a BFD section. Undo the mapping done
8966 by copy_private_symbol_data. */
8967 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8971 shndx
= elf_onesymtab (abfd
);
8974 shndx
= elf_dynsymtab (abfd
);
8977 shndx
= elf_strtab_sec (abfd
);
8980 shndx
= elf_shstrtab_sec (abfd
);
8983 if (elf_symtab_shndx_list (abfd
))
8984 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8991 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8993 if (bed
->symbol_section_index
)
8994 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8995 /* Otherwise just leave the index alone. */
8999 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
9000 _bfd_error_handler (_("%pB: \
9001 Unable to handle section index %x in ELF symbol. Using ABS instead."),
9010 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
9012 if (shndx
== SHN_BAD
)
9016 /* Writing this would be a hell of a lot easier if
9017 we had some decent documentation on bfd, and
9018 knew what to expect of the library, and what to
9019 demand of applications. For example, it
9020 appears that `objcopy' might not set the
9021 section of a symbol to be a section that is
9022 actually in the output file. */
9023 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
9025 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
9026 if (shndx
== SHN_BAD
)
9028 /* xgettext:c-format */
9030 (_("unable to find equivalent output section"
9031 " for symbol '%s' from section '%s'"),
9032 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
9034 bfd_set_error (bfd_error_invalid_operation
);
9040 sym
.st_shndx
= shndx
;
9043 if ((flags
& BSF_THREAD_LOCAL
) != 0)
9045 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
9046 type
= STT_GNU_IFUNC
;
9047 else if ((flags
& BSF_FUNCTION
) != 0)
9049 else if ((flags
& BSF_OBJECT
) != 0)
9051 else if ((flags
& BSF_RELC
) != 0)
9053 else if ((flags
& BSF_SRELC
) != 0)
9058 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
9061 /* Processor-specific types. */
9062 if (type_ptr
!= NULL
9063 && bed
->elf_backend_get_symbol_type
)
9064 type
= ((*bed
->elf_backend_get_symbol_type
)
9065 (&type_ptr
->internal_elf_sym
, type
));
9067 if (flags
& BSF_SECTION_SYM
)
9069 if (flags
& BSF_GLOBAL
)
9070 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
9072 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9074 else if (bfd_is_com_section (syms
[idx
]->section
))
9076 if (type
!= STT_TLS
)
9078 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
9079 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
9080 ? STT_COMMON
: STT_OBJECT
);
9082 type
= ((flags
& BSF_ELF_COMMON
) != 0
9083 ? STT_COMMON
: STT_OBJECT
);
9085 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9087 else if (bfd_is_und_section (syms
[idx
]->section
))
9088 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
9092 else if (flags
& BSF_FILE
)
9093 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9096 int bind
= STB_LOCAL
;
9098 if (flags
& BSF_LOCAL
)
9100 else if (flags
& BSF_GNU_UNIQUE
)
9101 bind
= STB_GNU_UNIQUE
;
9102 else if (flags
& BSF_WEAK
)
9104 else if (flags
& BSF_GLOBAL
)
9107 sym
.st_info
= ELF_ST_INFO (bind
, type
);
9110 if (type_ptr
!= NULL
)
9112 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
9113 sym
.st_target_internal
9114 = type_ptr
->internal_elf_sym
.st_target_internal
;
9119 sym
.st_target_internal
= 0;
9123 symstrtab
[idx
].sym
= sym
;
9124 symstrtab
[idx
].dest_index
= outbound_syms_index
;
9126 outbound_syms_index
++;
9129 /* Finalize the .strtab section. */
9130 _bfd_elf_strtab_finalize (stt
);
9132 /* Swap out the .strtab section. */
9133 for (idx
= 0; idx
<= symcount
; idx
++)
9135 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
9136 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9137 elfsym
->sym
.st_name
= 0;
9139 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
9140 elfsym
->sym
.st_name
);
9141 if (info
&& info
->callbacks
->ctf_new_symbol
)
9142 info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
9145 /* Inform the linker of the addition of this symbol. */
9147 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
9149 + (elfsym
->dest_index
9150 * bed
->s
->sizeof_sym
)),
9151 NPTR_ADD (outbound_shndx
,
9153 * sizeof (Elf_External_Sym_Shndx
))));
9158 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
9159 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9160 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
9161 symstrtab_hdr
->sh_addr
= 0;
9162 symstrtab_hdr
->sh_entsize
= 0;
9163 symstrtab_hdr
->sh_link
= 0;
9164 symstrtab_hdr
->sh_info
= 0;
9165 symstrtab_hdr
->sh_addralign
= 1;
9170 /* Return the number of bytes required to hold the symtab vector.
9172 Note that we base it on the count plus 1, since we will null terminate
9173 the vector allocated based on this size. However, the ELF symbol table
9174 always has a dummy entry as symbol #0, so it ends up even. */
9177 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
9179 bfd_size_type symcount
;
9181 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9183 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
9184 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
9186 bfd_set_error (bfd_error_file_too_big
);
9189 symtab_size
= symcount
* (sizeof (asymbol
*));
9191 symtab_size
= sizeof (asymbol
*);
9192 else if (!bfd_write_p (abfd
))
9194 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9196 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
9198 bfd_set_error (bfd_error_file_truncated
);
9207 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
9209 bfd_size_type symcount
;
9211 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
9213 if (elf_dynsymtab (abfd
) == 0)
9215 /* Check if there is dynamic symbol table. */
9216 symcount
= elf_tdata (abfd
)->dt_symtab_count
;
9218 goto compute_symtab_size
;
9220 bfd_set_error (bfd_error_invalid_operation
);
9224 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
9225 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
9227 bfd_set_error (bfd_error_file_too_big
);
9231 compute_symtab_size
:
9232 symtab_size
= symcount
* (sizeof (asymbol
*));
9234 symtab_size
= sizeof (asymbol
*);
9235 else if (!bfd_write_p (abfd
))
9237 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9239 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
9241 bfd_set_error (bfd_error_file_truncated
);
9250 _bfd_elf_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
9252 if (asect
->reloc_count
!= 0 && !bfd_write_p (abfd
))
9254 /* Sanity check reloc section size. */
9255 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9259 struct bfd_elf_section_data
*d
= elf_section_data (asect
);
9260 bfd_size_type rel_size
= d
->rel
.hdr
? d
->rel
.hdr
->sh_size
: 0;
9261 bfd_size_type rela_size
= d
->rela
.hdr
? d
->rela
.hdr
->sh_size
: 0;
9263 if (rel_size
+ rela_size
> filesize
9264 || rel_size
+ rela_size
< rel_size
)
9266 bfd_set_error (bfd_error_file_truncated
);
9272 #if SIZEOF_LONG == SIZEOF_INT
9273 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
9275 bfd_set_error (bfd_error_file_too_big
);
9279 return (asect
->reloc_count
+ 1L) * sizeof (arelent
*);
9282 /* Canonicalize the relocs. */
9285 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
9292 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9294 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
9297 tblptr
= section
->relocation
;
9298 for (i
= 0; i
< section
->reloc_count
; i
++)
9299 *relptr
++ = tblptr
++;
9303 return section
->reloc_count
;
9307 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
9309 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9310 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, false);
9313 abfd
->symcount
= symcount
;
9318 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
9319 asymbol
**allocation
)
9321 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9322 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, true);
9325 abfd
->dynsymcount
= symcount
;
9329 /* Return the size required for the dynamic reloc entries. Any loadable
9330 section that was actually installed in the BFD, and has type SHT_REL
9331 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9332 dynamic reloc section. */
9335 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
9337 bfd_size_type count
, ext_rel_size
;
9340 if (elf_dynsymtab (abfd
) == 0)
9342 bfd_set_error (bfd_error_invalid_operation
);
9348 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9349 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
9350 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
9351 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
)
9352 && (elf_section_data (s
)->this_hdr
.sh_flags
& SHF_COMPRESSED
) == 0)
9354 ext_rel_size
+= elf_section_data (s
)->this_hdr
.sh_size
;
9355 if (ext_rel_size
< elf_section_data (s
)->this_hdr
.sh_size
)
9357 bfd_set_error (bfd_error_file_truncated
);
9360 count
+= NUM_SHDR_ENTRIES (&elf_section_data (s
)->this_hdr
);
9361 if (count
> LONG_MAX
/ sizeof (arelent
*))
9363 bfd_set_error (bfd_error_file_too_big
);
9367 if (count
> 1 && !bfd_write_p (abfd
))
9369 /* Sanity check reloc section sizes. */
9370 ufile_ptr filesize
= bfd_get_file_size (abfd
);
9371 if (filesize
!= 0 && ext_rel_size
> filesize
)
9373 bfd_set_error (bfd_error_file_truncated
);
9377 return count
* sizeof (arelent
*);
9380 /* Canonicalize the dynamic relocation entries. Note that we return the
9381 dynamic relocations as a single block, although they are actually
9382 associated with particular sections; the interface, which was
9383 designed for SunOS style shared libraries, expects that there is only
9384 one set of dynamic relocs. Any loadable section that was actually
9385 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9386 dynamic symbol table, is considered to be a dynamic reloc section. */
9389 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
9393 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
9397 if (elf_dynsymtab (abfd
) == 0)
9399 bfd_set_error (bfd_error_invalid_operation
);
9403 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9405 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9407 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
9408 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
9409 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
)
9410 && (elf_section_data (s
)->this_hdr
.sh_flags
& SHF_COMPRESSED
) == 0)
9415 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
9417 count
= NUM_SHDR_ENTRIES (&elf_section_data (s
)->this_hdr
);
9419 for (i
= 0; i
< count
; i
++)
9430 /* Read in the version information. */
9433 _bfd_elf_slurp_version_tables (bfd
*abfd
, bool default_imported_symver
)
9435 bfd_byte
*contents
= NULL
;
9436 unsigned int freeidx
= 0;
9439 if (elf_dynverref (abfd
) != 0 || elf_tdata (abfd
)->dt_verneed
!= NULL
)
9441 Elf_Internal_Shdr
*hdr
;
9442 Elf_External_Verneed
*everneed
;
9443 Elf_Internal_Verneed
*iverneed
;
9445 bfd_byte
*contents_end
;
9446 size_t verneed_count
;
9447 size_t verneed_size
;
9449 if (elf_tdata (abfd
)->dt_verneed
!= NULL
)
9452 contents
= elf_tdata (abfd
)->dt_verneed
;
9453 verneed_count
= elf_tdata (abfd
)->dt_verneed_count
;
9454 verneed_size
= verneed_count
* sizeof (Elf_External_Verneed
);
9458 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
9460 if (hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
9462 error_return_bad_verref
:
9464 (_("%pB: .gnu.version_r invalid entry"), abfd
);
9465 bfd_set_error (bfd_error_bad_value
);
9466 error_return_verref
:
9467 elf_tdata (abfd
)->verref
= NULL
;
9468 elf_tdata (abfd
)->cverrefs
= 0;
9472 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9473 goto error_return_verref
;
9474 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
9475 if (contents
== NULL
)
9476 goto error_return_verref
;
9478 verneed_size
= hdr
->sh_size
;
9479 verneed_count
= hdr
->sh_info
;
9482 if (_bfd_mul_overflow (verneed_count
,
9483 sizeof (Elf_Internal_Verneed
), &amt
))
9485 bfd_set_error (bfd_error_file_too_big
);
9486 goto error_return_verref
;
9489 goto error_return_verref
;
9490 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
9491 if (elf_tdata (abfd
)->verref
== NULL
)
9492 goto error_return_verref
;
9494 BFD_ASSERT (sizeof (Elf_External_Verneed
)
9495 == sizeof (Elf_External_Vernaux
));
9496 contents_end
= (contents
+ verneed_size
9497 - sizeof (Elf_External_Verneed
));
9498 everneed
= (Elf_External_Verneed
*) contents
;
9499 iverneed
= elf_tdata (abfd
)->verref
;
9500 for (i
= 0; i
< verneed_count
; i
++, iverneed
++)
9502 Elf_External_Vernaux
*evernaux
;
9503 Elf_Internal_Vernaux
*ivernaux
;
9506 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
9508 iverneed
->vn_bfd
= abfd
;
9510 if (elf_use_dt_symtab_p (abfd
))
9512 if (iverneed
->vn_file
< elf_tdata (abfd
)->dt_strsz
)
9513 iverneed
->vn_filename
9514 = elf_tdata (abfd
)->dt_strtab
+ iverneed
->vn_file
;
9516 iverneed
->vn_filename
= NULL
;
9518 else if (hdr
== NULL
)
9519 goto error_return_bad_verref
;
9521 iverneed
->vn_filename
9522 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9524 if (iverneed
->vn_filename
== NULL
)
9525 goto error_return_bad_verref
;
9527 if (iverneed
->vn_cnt
== 0)
9528 iverneed
->vn_auxptr
= NULL
;
9531 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
9532 sizeof (Elf_Internal_Vernaux
), &amt
))
9534 bfd_set_error (bfd_error_file_too_big
);
9535 goto error_return_verref
;
9537 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
9538 bfd_alloc (abfd
, amt
);
9539 if (iverneed
->vn_auxptr
== NULL
)
9540 goto error_return_verref
;
9543 if (iverneed
->vn_aux
9544 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9545 goto error_return_bad_verref
;
9547 evernaux
= ((Elf_External_Vernaux
*)
9548 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
9549 ivernaux
= iverneed
->vn_auxptr
;
9550 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
9552 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
9554 if (elf_use_dt_symtab_p (abfd
))
9556 if (ivernaux
->vna_name
< elf_tdata (abfd
)->dt_strsz
)
9557 ivernaux
->vna_nodename
9558 = elf_tdata (abfd
)->dt_strtab
+ ivernaux
->vna_name
;
9560 ivernaux
->vna_nodename
= NULL
;
9562 else if (hdr
== NULL
)
9563 goto error_return_bad_verref
;
9565 ivernaux
->vna_nodename
9566 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9567 ivernaux
->vna_name
);
9568 if (ivernaux
->vna_nodename
== NULL
)
9569 goto error_return_bad_verref
;
9571 if (ivernaux
->vna_other
> freeidx
)
9572 freeidx
= ivernaux
->vna_other
;
9574 ivernaux
->vna_nextptr
= NULL
;
9575 if (ivernaux
->vna_next
== 0)
9577 iverneed
->vn_cnt
= j
+ 1;
9580 if (j
+ 1 < iverneed
->vn_cnt
)
9581 ivernaux
->vna_nextptr
= ivernaux
+ 1;
9583 if (ivernaux
->vna_next
9584 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
9585 goto error_return_bad_verref
;
9587 evernaux
= ((Elf_External_Vernaux
*)
9588 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
9591 iverneed
->vn_nextref
= NULL
;
9592 if (iverneed
->vn_next
== 0)
9594 if (hdr
!= NULL
&& (i
+ 1 < hdr
->sh_info
))
9595 iverneed
->vn_nextref
= iverneed
+ 1;
9597 if (iverneed
->vn_next
9598 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
9599 goto error_return_bad_verref
;
9601 everneed
= ((Elf_External_Verneed
*)
9602 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
9604 elf_tdata (abfd
)->cverrefs
= i
;
9606 if (contents
!= elf_tdata (abfd
)->dt_verneed
)
9611 if (elf_dynverdef (abfd
) != 0 || elf_tdata (abfd
)->dt_verdef
!= NULL
)
9613 Elf_Internal_Shdr
*hdr
;
9614 Elf_External_Verdef
*everdef
;
9615 Elf_Internal_Verdef
*iverdef
;
9616 Elf_Internal_Verdef
*iverdefarr
;
9617 Elf_Internal_Verdef iverdefmem
;
9619 unsigned int maxidx
;
9620 bfd_byte
*contents_end_def
, *contents_end_aux
;
9621 size_t verdef_count
;
9624 if (elf_tdata (abfd
)->dt_verdef
!= NULL
)
9627 contents
= elf_tdata (abfd
)->dt_verdef
;
9628 verdef_count
= elf_tdata (abfd
)->dt_verdef_count
;
9629 verdef_size
= verdef_count
* sizeof (Elf_External_Verdef
);
9633 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
9635 if (hdr
->sh_size
< sizeof (Elf_External_Verdef
))
9637 error_return_bad_verdef
:
9639 (_("%pB: .gnu.version_d invalid entry"), abfd
);
9640 bfd_set_error (bfd_error_bad_value
);
9641 error_return_verdef
:
9642 elf_tdata (abfd
)->verdef
= NULL
;
9643 elf_tdata (abfd
)->cverdefs
= 0;
9647 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
9648 goto error_return_verdef
;
9649 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
9650 if (contents
== NULL
)
9651 goto error_return_verdef
;
9653 BFD_ASSERT (sizeof (Elf_External_Verdef
)
9654 >= sizeof (Elf_External_Verdaux
));
9656 verdef_count
= hdr
->sh_info
;
9657 verdef_size
= hdr
->sh_size
;
9660 contents_end_def
= (contents
+ verdef_size
9661 - sizeof (Elf_External_Verdef
));
9662 contents_end_aux
= (contents
+ verdef_size
9663 - sizeof (Elf_External_Verdaux
));
9665 /* We know the number of entries in the section but not the maximum
9666 index. Therefore we have to run through all entries and find
9668 everdef
= (Elf_External_Verdef
*) contents
;
9670 for (i
= 0; i
< verdef_count
; ++i
)
9672 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9674 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
9675 goto error_return_bad_verdef
;
9676 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
9677 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
9679 if (iverdefmem
.vd_next
== 0)
9682 if (iverdefmem
.vd_next
9683 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
9684 goto error_return_bad_verdef
;
9686 everdef
= ((Elf_External_Verdef
*)
9687 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
9690 if (default_imported_symver
)
9692 if (freeidx
> maxidx
)
9697 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9699 bfd_set_error (bfd_error_file_too_big
);
9700 goto error_return_verdef
;
9704 goto error_return_verdef
;
9705 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9706 if (elf_tdata (abfd
)->verdef
== NULL
)
9707 goto error_return_verdef
;
9709 elf_tdata (abfd
)->cverdefs
= maxidx
;
9711 everdef
= (Elf_External_Verdef
*) contents
;
9712 iverdefarr
= elf_tdata (abfd
)->verdef
;
9713 for (i
= 0; i
< verdef_count
; ++i
)
9715 Elf_External_Verdaux
*everdaux
;
9716 Elf_Internal_Verdaux
*iverdaux
;
9719 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
9721 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
9722 goto error_return_bad_verdef
;
9724 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
9725 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
9727 iverdef
->vd_bfd
= abfd
;
9729 if (iverdef
->vd_cnt
== 0)
9730 iverdef
->vd_auxptr
= NULL
;
9733 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
9734 sizeof (Elf_Internal_Verdaux
), &amt
))
9736 bfd_set_error (bfd_error_file_too_big
);
9737 goto error_return_verdef
;
9739 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
9740 bfd_alloc (abfd
, amt
);
9741 if (iverdef
->vd_auxptr
== NULL
)
9742 goto error_return_verdef
;
9746 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
9747 goto error_return_bad_verdef
;
9749 everdaux
= ((Elf_External_Verdaux
*)
9750 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
9751 iverdaux
= iverdef
->vd_auxptr
;
9752 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
9754 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
9756 if (elf_use_dt_symtab_p (abfd
))
9758 if (iverdaux
->vda_name
< elf_tdata (abfd
)->dt_strsz
)
9759 iverdaux
->vda_nodename
9760 = elf_tdata (abfd
)->dt_strtab
+ iverdaux
->vda_name
;
9762 iverdaux
->vda_nodename
= NULL
;
9765 iverdaux
->vda_nodename
9766 = bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
9767 iverdaux
->vda_name
);
9768 if (iverdaux
->vda_nodename
== NULL
)
9769 goto error_return_bad_verdef
;
9771 iverdaux
->vda_nextptr
= NULL
;
9772 if (iverdaux
->vda_next
== 0)
9774 iverdef
->vd_cnt
= j
+ 1;
9777 if (j
+ 1 < iverdef
->vd_cnt
)
9778 iverdaux
->vda_nextptr
= iverdaux
+ 1;
9780 if (iverdaux
->vda_next
9781 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
9782 goto error_return_bad_verdef
;
9784 everdaux
= ((Elf_External_Verdaux
*)
9785 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
9788 iverdef
->vd_nodename
= NULL
;
9789 if (iverdef
->vd_cnt
)
9790 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
9792 iverdef
->vd_nextdef
= NULL
;
9793 if (iverdef
->vd_next
== 0)
9795 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
9796 iverdef
->vd_nextdef
= iverdef
+ 1;
9798 everdef
= ((Elf_External_Verdef
*)
9799 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
9802 if (contents
!= elf_tdata (abfd
)->dt_verdef
)
9806 else if (default_imported_symver
)
9813 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
9815 bfd_set_error (bfd_error_file_too_big
);
9820 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
9821 if (elf_tdata (abfd
)->verdef
== NULL
)
9824 elf_tdata (abfd
)->cverdefs
= freeidx
;
9827 /* Create a default version based on the soname. */
9828 if (default_imported_symver
)
9830 Elf_Internal_Verdef
*iverdef
;
9831 Elf_Internal_Verdaux
*iverdaux
;
9833 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
9835 iverdef
->vd_version
= VER_DEF_CURRENT
;
9836 iverdef
->vd_flags
= 0;
9837 iverdef
->vd_ndx
= freeidx
;
9838 iverdef
->vd_cnt
= 1;
9840 iverdef
->vd_bfd
= abfd
;
9842 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
9843 if (iverdef
->vd_nodename
== NULL
)
9844 goto error_return_verdef
;
9845 iverdef
->vd_nextdef
= NULL
;
9846 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
9847 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
9848 if (iverdef
->vd_auxptr
== NULL
)
9849 goto error_return_verdef
;
9851 iverdaux
= iverdef
->vd_auxptr
;
9852 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9858 if (contents
!= elf_tdata (abfd
)->dt_verneed
9859 && contents
!= elf_tdata (abfd
)->dt_verdef
)
9865 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9867 elf_symbol_type
*newsym
;
9869 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9872 newsym
->symbol
.the_bfd
= abfd
;
9873 return &newsym
->symbol
;
9877 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9881 bfd_symbol_info (symbol
, ret
);
9884 /* Return whether a symbol name implies a local symbol. Most targets
9885 use this function for the is_local_label_name entry point, but some
9889 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9892 /* Normal local symbols start with ``.L''. */
9893 if (name
[0] == '.' && name
[1] == 'L')
9896 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9897 DWARF debugging symbols starting with ``..''. */
9898 if (name
[0] == '.' && name
[1] == '.')
9901 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9902 emitting DWARF debugging output. I suspect this is actually a
9903 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9904 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9905 underscore to be emitted on some ELF targets). For ease of use,
9906 we treat such symbols as local. */
9907 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9910 /* Treat assembler generated fake symbols, dollar local labels and
9911 forward-backward labels (aka local labels) as locals.
9912 These labels have the form:
9914 L0^A.* (fake symbols)
9916 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9918 Versions which start with .L will have already been matched above,
9919 so we only need to match the rest. */
9920 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9926 for (p
= name
+ 2; (c
= *p
); p
++)
9928 if (c
== 1 || c
== 2)
9930 if (c
== 1 && p
== name
+ 2)
9931 /* A fake symbol. */
9934 /* FIXME: We are being paranoid here and treating symbols like
9935 L0^Bfoo as if there were non-local, on the grounds that the
9936 assembler will never generate them. But can any symbol
9937 containing an ASCII value in the range 1-31 ever be anything
9938 other than some kind of local ? */
9955 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9956 asymbol
*symbol ATTRIBUTE_UNUSED
)
9963 _bfd_elf_set_arch_mach (bfd
*abfd
,
9964 enum bfd_architecture arch
,
9965 unsigned long machine
)
9967 /* If this isn't the right architecture for this backend, and this
9968 isn't the generic backend, fail. */
9969 if (arch
!= get_elf_backend_data (abfd
)->arch
9970 && arch
!= bfd_arch_unknown
9971 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9974 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9977 /* Find the nearest line to a particular section and offset,
9978 for error reporting. */
9981 _bfd_elf_find_nearest_line (bfd
*abfd
,
9985 const char **filename_ptr
,
9986 const char **functionname_ptr
,
9987 unsigned int *line_ptr
,
9988 unsigned int *discriminator_ptr
)
9990 return _bfd_elf_find_nearest_line_with_alt (abfd
, NULL
, symbols
, section
,
9991 offset
, filename_ptr
,
9992 functionname_ptr
, line_ptr
,
9996 /* Find the nearest line to a particular section and offset,
9997 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9998 can be optionally specified. */
10001 _bfd_elf_find_nearest_line_with_alt (bfd
*abfd
,
10002 const char *alt_filename
,
10006 const char **filename_ptr
,
10007 const char **functionname_ptr
,
10008 unsigned int *line_ptr
,
10009 unsigned int *discriminator_ptr
)
10013 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd
, alt_filename
, symbols
, NULL
,
10014 section
, offset
, filename_ptr
,
10015 functionname_ptr
, line_ptr
,
10017 dwarf_debug_sections
,
10018 &elf_tdata (abfd
)->dwarf2_find_line_info
))
10021 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
10022 filename_ptr
, functionname_ptr
, line_ptr
))
10024 if (!*functionname_ptr
)
10025 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
10026 *filename_ptr
? NULL
: filename_ptr
,
10031 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
10032 &found
, filename_ptr
,
10033 functionname_ptr
, line_ptr
,
10034 &elf_tdata (abfd
)->line_info
))
10036 if (found
&& (*functionname_ptr
|| *line_ptr
))
10039 if (symbols
== NULL
)
10042 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
10043 filename_ptr
, functionname_ptr
))
10050 /* Find the line for a symbol. */
10053 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
10054 const char **filename_ptr
, unsigned int *line_ptr
)
10056 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
10057 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
10058 filename_ptr
, NULL
, line_ptr
, NULL
,
10059 dwarf_debug_sections
,
10060 &tdata
->dwarf2_find_line_info
);
10063 /* After a call to bfd_find_nearest_line, successive calls to
10064 bfd_find_inliner_info can be used to get source information about
10065 each level of function inlining that terminated at the address
10066 passed to bfd_find_nearest_line. Currently this is only supported
10067 for DWARF2 with appropriate DWARF3 extensions. */
10070 _bfd_elf_find_inliner_info (bfd
*abfd
,
10071 const char **filename_ptr
,
10072 const char **functionname_ptr
,
10073 unsigned int *line_ptr
)
10075 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
10076 return _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
10077 functionname_ptr
, line_ptr
,
10078 &tdata
->dwarf2_find_line_info
);
10082 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
10084 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10085 int ret
= bed
->s
->sizeof_ehdr
;
10087 if (!bfd_link_relocatable (info
))
10089 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
10091 if (phdr_size
== (bfd_size_type
) -1)
10093 struct elf_segment_map
*m
;
10096 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
10097 phdr_size
+= bed
->s
->sizeof_phdr
;
10099 if (phdr_size
== 0)
10100 phdr_size
= get_program_header_size (abfd
, info
);
10103 elf_program_header_size (abfd
) = phdr_size
;
10111 _bfd_elf_set_section_contents (bfd
*abfd
,
10113 const void *location
,
10115 bfd_size_type count
)
10117 Elf_Internal_Shdr
*hdr
;
10119 if (! abfd
->output_has_begun
10120 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
10126 hdr
= &elf_section_data (section
)->this_hdr
;
10127 if (hdr
->sh_offset
== (file_ptr
) -1)
10129 unsigned char *contents
;
10131 if (bfd_section_is_ctf (section
))
10132 /* Nothing to do with this section: the contents are generated
10136 if ((offset
+ count
) > hdr
->sh_size
)
10139 (_("%pB:%pA: error: attempting to write"
10140 " over the end of the section"),
10143 bfd_set_error (bfd_error_invalid_operation
);
10147 contents
= hdr
->contents
;
10148 if (contents
== NULL
)
10151 (_("%pB:%pA: error: attempting to write"
10152 " section into an empty buffer"),
10155 bfd_set_error (bfd_error_invalid_operation
);
10159 memcpy (contents
+ offset
, location
, count
);
10163 return _bfd_generic_set_section_contents (abfd
, section
,
10164 location
, offset
, count
);
10168 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
10169 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
10170 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
10176 /* Try to convert a non-ELF reloc into an ELF one. */
10179 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
10181 /* Check whether we really have an ELF howto. */
10183 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
10185 bfd_reloc_code_real_type code
;
10186 reloc_howto_type
*howto
;
10188 /* Alien reloc: Try to determine its type to replace it with an
10189 equivalent ELF reloc. */
10191 if (areloc
->howto
->pc_relative
)
10193 switch (areloc
->howto
->bitsize
)
10196 code
= BFD_RELOC_8_PCREL
;
10199 code
= BFD_RELOC_12_PCREL
;
10202 code
= BFD_RELOC_16_PCREL
;
10205 code
= BFD_RELOC_24_PCREL
;
10208 code
= BFD_RELOC_32_PCREL
;
10211 code
= BFD_RELOC_64_PCREL
;
10217 howto
= bfd_reloc_type_lookup (abfd
, code
);
10219 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
10221 if (howto
->pcrel_offset
)
10222 areloc
->addend
+= areloc
->address
;
10224 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
10229 switch (areloc
->howto
->bitsize
)
10232 code
= BFD_RELOC_8
;
10235 code
= BFD_RELOC_14
;
10238 code
= BFD_RELOC_16
;
10241 code
= BFD_RELOC_26
;
10244 code
= BFD_RELOC_32
;
10247 code
= BFD_RELOC_64
;
10253 howto
= bfd_reloc_type_lookup (abfd
, code
);
10257 areloc
->howto
= howto
;
10265 /* xgettext:c-format */
10266 _bfd_error_handler (_("%pB: %s unsupported"),
10267 abfd
, areloc
->howto
->name
);
10268 bfd_set_error (bfd_error_sorry
);
10273 _bfd_elf_free_cached_info (bfd
*abfd
)
10275 struct elf_obj_tdata
*tdata
;
10277 if ((bfd_get_format (abfd
) == bfd_object
10278 || bfd_get_format (abfd
) == bfd_core
)
10279 && (tdata
= elf_tdata (abfd
)) != NULL
)
10281 if (tdata
->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
10282 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
10283 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
10284 _bfd_dwarf1_cleanup_debug_info (abfd
, &tdata
->dwarf1_find_line_info
);
10285 _bfd_stab_cleanup (abfd
, &tdata
->line_info
);
10288 return _bfd_generic_bfd_free_cached_info (abfd
);
10291 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10292 in the relocation's offset. Thus we cannot allow any sort of sanity
10293 range-checking to interfere. There is nothing else to do in processing
10296 bfd_reloc_status_type
10297 _bfd_elf_rel_vtable_reloc_fn
10298 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
10299 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
10300 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
10301 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
10303 return bfd_reloc_ok
;
10306 /* Elf core file support. Much of this only works on native
10307 toolchains, since we rely on knowing the
10308 machine-dependent procfs structure in order to pick
10309 out details about the corefile. */
10311 #ifdef HAVE_SYS_PROCFS_H
10312 # include <sys/procfs.h>
10315 /* Return a PID that identifies a "thread" for threaded cores, or the
10316 PID of the main process for non-threaded cores. */
10319 elfcore_make_pid (bfd
*abfd
)
10323 pid
= elf_tdata (abfd
)->core
->lwpid
;
10325 pid
= elf_tdata (abfd
)->core
->pid
;
10330 /* If there isn't a section called NAME, make one, using data from
10331 SECT. Note, this function will generate a reference to NAME, so
10332 you shouldn't deallocate or overwrite it. */
10335 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
10339 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
10342 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
10346 sect2
->size
= sect
->size
;
10347 sect2
->filepos
= sect
->filepos
;
10348 sect2
->alignment_power
= sect
->alignment_power
;
10352 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10353 actually creates up to two pseudosections:
10354 - For the single-threaded case, a section named NAME, unless
10355 such a section already exists.
10356 - For the multi-threaded case, a section named "NAME/PID", where
10357 PID is elfcore_make_pid (abfd).
10358 Both pseudosections have identical contents. */
10360 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
10366 char *threaded_name
;
10370 /* Build the section name. */
10372 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
10373 len
= strlen (buf
) + 1;
10374 threaded_name
= (char *) bfd_alloc (abfd
, len
);
10375 if (threaded_name
== NULL
)
10377 memcpy (threaded_name
, buf
, len
);
10379 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
10384 sect
->filepos
= filepos
;
10385 sect
->alignment_power
= 2;
10387 return elfcore_maybe_make_sect (abfd
, name
, sect
);
10391 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
10394 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10400 sect
->size
= note
->descsz
- offs
;
10401 sect
->filepos
= note
->descpos
+ offs
;
10402 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10407 /* prstatus_t exists on:
10409 linux 2.[01] + glibc
10413 #if defined (HAVE_PRSTATUS_T)
10416 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10421 if (note
->descsz
== sizeof (prstatus_t
))
10425 size
= sizeof (prstat
.pr_reg
);
10426 offset
= offsetof (prstatus_t
, pr_reg
);
10427 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
10429 /* Do not overwrite the core signal if it
10430 has already been set by another thread. */
10431 if (elf_tdata (abfd
)->core
->signal
== 0)
10432 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
10433 if (elf_tdata (abfd
)->core
->pid
== 0)
10434 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
10436 /* pr_who exists on:
10439 pr_who doesn't exist on:
10442 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10443 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
10445 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
10448 #if defined (HAVE_PRSTATUS32_T)
10449 else if (note
->descsz
== sizeof (prstatus32_t
))
10451 /* 64-bit host, 32-bit corefile */
10452 prstatus32_t prstat
;
10454 size
= sizeof (prstat
.pr_reg
);
10455 offset
= offsetof (prstatus32_t
, pr_reg
);
10456 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
10458 /* Do not overwrite the core signal if it
10459 has already been set by another thread. */
10460 if (elf_tdata (abfd
)->core
->signal
== 0)
10461 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
10462 if (elf_tdata (abfd
)->core
->pid
== 0)
10463 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
10465 /* pr_who exists on:
10468 pr_who doesn't exist on:
10471 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10472 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
10474 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
10477 #endif /* HAVE_PRSTATUS32_T */
10480 /* Fail - we don't know how to handle any other
10481 note size (ie. data object type). */
10485 /* Make a ".reg/999" section and a ".reg" section. */
10486 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10487 size
, note
->descpos
+ offset
);
10489 #endif /* defined (HAVE_PRSTATUS_T) */
10491 /* Create a pseudosection containing the exact contents of NOTE. */
10493 elfcore_make_note_pseudosection (bfd
*abfd
,
10495 Elf_Internal_Note
*note
)
10497 return _bfd_elfcore_make_pseudosection (abfd
, name
,
10498 note
->descsz
, note
->descpos
);
10501 /* There isn't a consistent prfpregset_t across platforms,
10502 but it doesn't matter, because we don't have to pick this
10503 data structure apart. */
10506 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10508 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10511 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10512 type of NT_PRXFPREG. Just include the whole note's contents
10516 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10518 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10521 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10522 with a note type of NT_X86_XSTATE. Just include the whole note's
10523 contents literally. */
10526 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
10528 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
10532 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10534 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
10538 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10540 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
10544 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
10546 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
10550 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10552 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
10556 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10558 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
10562 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
10564 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
10568 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
10570 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
10574 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10576 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
10580 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
10582 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
10586 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
10588 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
10592 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10594 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
10598 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
10600 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
10604 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
10606 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
10610 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
10612 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
10616 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
10618 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
10622 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
10624 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
10628 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
10630 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
10634 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
10636 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
10640 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
10642 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
10646 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
10648 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
10652 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
10654 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
10658 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10660 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
10664 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
10666 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
10670 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
10672 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
10676 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
10678 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
10682 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
10684 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
10688 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
10690 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
10694 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
10696 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
10700 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
10702 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
10706 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
10708 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
10712 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
10714 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
10718 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
10720 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
10724 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
10726 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
10730 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
10732 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
10736 elfcore_grok_aarch_mte (bfd
*abfd
, Elf_Internal_Note
*note
)
10738 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-mte",
10743 elfcore_grok_aarch_ssve (bfd
*abfd
, Elf_Internal_Note
*note
)
10745 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-ssve", note
);
10749 elfcore_grok_aarch_za (bfd
*abfd
, Elf_Internal_Note
*note
)
10751 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-za", note
);
10754 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10755 successful, otherwise return FALSE. */
10758 elfcore_grok_aarch_zt (bfd
*abfd
, Elf_Internal_Note
*note
)
10760 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-zt", note
);
10764 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
10766 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
10769 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10770 successful otherwise, return FALSE. */
10773 elfcore_grok_riscv_csr (bfd
*abfd
, Elf_Internal_Note
*note
)
10775 return elfcore_make_note_pseudosection (abfd
, ".reg-riscv-csr", note
);
10778 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10779 successful otherwise, return FALSE. */
10782 elfcore_grok_gdb_tdesc (bfd
*abfd
, Elf_Internal_Note
*note
)
10784 return elfcore_make_note_pseudosection (abfd
, ".gdb-tdesc", note
);
10788 elfcore_grok_loongarch_cpucfg (bfd
*abfd
, Elf_Internal_Note
*note
)
10790 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-cpucfg", note
);
10794 elfcore_grok_loongarch_lbt (bfd
*abfd
, Elf_Internal_Note
*note
)
10796 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lbt", note
);
10800 elfcore_grok_loongarch_lsx (bfd
*abfd
, Elf_Internal_Note
*note
)
10802 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lsx", note
);
10806 elfcore_grok_loongarch_lasx (bfd
*abfd
, Elf_Internal_Note
*note
)
10808 return elfcore_make_note_pseudosection (abfd
, ".reg-loongarch-lasx", note
);
10811 #if defined (HAVE_PRPSINFO_T)
10812 typedef prpsinfo_t elfcore_psinfo_t
;
10813 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10814 typedef prpsinfo32_t elfcore_psinfo32_t
;
10818 #if defined (HAVE_PSINFO_T)
10819 typedef psinfo_t elfcore_psinfo_t
;
10820 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10821 typedef psinfo32_t elfcore_psinfo32_t
;
10825 /* return a malloc'ed copy of a string at START which is at
10826 most MAX bytes long, possibly without a terminating '\0'.
10827 the copy will always have a terminating '\0'. */
10830 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
10833 char *end
= (char *) memchr (start
, '\0', max
);
10841 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
10845 memcpy (dups
, start
, len
);
10851 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10853 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10855 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
10857 elfcore_psinfo_t psinfo
;
10859 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10861 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10862 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10864 elf_tdata (abfd
)->core
->program
10865 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10866 sizeof (psinfo
.pr_fname
));
10868 elf_tdata (abfd
)->core
->command
10869 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10870 sizeof (psinfo
.pr_psargs
));
10872 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10873 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
10875 /* 64-bit host, 32-bit corefile */
10876 elfcore_psinfo32_t psinfo
;
10878 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
10880 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10881 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
10883 elf_tdata (abfd
)->core
->program
10884 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
10885 sizeof (psinfo
.pr_fname
));
10887 elf_tdata (abfd
)->core
->command
10888 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
10889 sizeof (psinfo
.pr_psargs
));
10895 /* Fail - we don't know how to handle any other
10896 note size (ie. data object type). */
10900 /* Note that for some reason, a spurious space is tacked
10901 onto the end of the args in some (at least one anyway)
10902 implementations, so strip it off if it exists. */
10905 char *command
= elf_tdata (abfd
)->core
->command
;
10906 int n
= strlen (command
);
10908 if (0 < n
&& command
[n
- 1] == ' ')
10909 command
[n
- 1] = '\0';
10914 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10916 #if defined (HAVE_PSTATUS_T)
10918 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10920 if (note
->descsz
== sizeof (pstatus_t
)
10921 #if defined (HAVE_PXSTATUS_T)
10922 || note
->descsz
== sizeof (pxstatus_t
)
10928 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10930 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10932 #if defined (HAVE_PSTATUS32_T)
10933 else if (note
->descsz
== sizeof (pstatus32_t
))
10935 /* 64-bit host, 32-bit corefile */
10938 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10940 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10943 /* Could grab some more details from the "representative"
10944 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10945 NT_LWPSTATUS note, presumably. */
10949 #endif /* defined (HAVE_PSTATUS_T) */
10951 #if defined (HAVE_LWPSTATUS_T)
10953 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10955 lwpstatus_t lwpstat
;
10961 if (note
->descsz
!= sizeof (lwpstat
)
10962 #if defined (HAVE_LWPXSTATUS_T)
10963 && note
->descsz
!= sizeof (lwpxstatus_t
)
10968 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10970 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10971 /* Do not overwrite the core signal if it has already been set by
10973 if (elf_tdata (abfd
)->core
->signal
== 0)
10974 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10976 /* Make a ".reg/999" section. */
10978 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10979 len
= strlen (buf
) + 1;
10980 name
= bfd_alloc (abfd
, len
);
10983 memcpy (name
, buf
, len
);
10985 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10989 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10990 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10991 sect
->filepos
= note
->descpos
10992 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10995 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10996 sect
->size
= sizeof (lwpstat
.pr_reg
);
10997 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
11000 sect
->alignment_power
= 2;
11002 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
11005 /* Make a ".reg2/999" section */
11007 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
11008 len
= strlen (buf
) + 1;
11009 name
= bfd_alloc (abfd
, len
);
11012 memcpy (name
, buf
, len
);
11014 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11018 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11019 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
11020 sect
->filepos
= note
->descpos
11021 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
11024 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11025 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
11026 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
11029 sect
->alignment_power
= 2;
11031 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
11033 #endif /* defined (HAVE_LWPSTATUS_T) */
11035 /* These constants, and the structure offsets used below, are defined by
11036 Cygwin's core_dump.h */
11037 #define NOTE_INFO_PROCESS 1
11038 #define NOTE_INFO_THREAD 2
11039 #define NOTE_INFO_MODULE 3
11040 #define NOTE_INFO_MODULE64 4
11043 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
11048 unsigned int name_size
;
11051 int is_active_thread
;
11054 if (note
->descsz
< 4)
11057 if (! startswith (note
->namedata
, "win32"))
11060 type
= bfd_get_32 (abfd
, note
->descdata
);
11064 const char *type_name
;
11065 unsigned long min_size
;
11068 { "NOTE_INFO_PROCESS", 12 },
11069 { "NOTE_INFO_THREAD", 12 },
11070 { "NOTE_INFO_MODULE", 12 },
11071 { "NOTE_INFO_MODULE64", 16 },
11074 if (type
== 0 || type
> (sizeof(size_check
)/sizeof(size_check
[0])))
11077 if (note
->descsz
< size_check
[type
- 1].min_size
)
11079 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11081 abfd
, size_check
[type
- 1].type_name
, note
->descsz
);
11087 case NOTE_INFO_PROCESS
:
11088 /* FIXME: need to add ->core->command. */
11089 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 4);
11090 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11093 case NOTE_INFO_THREAD
:
11094 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11096 /* thread_info.tid */
11097 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 4));
11099 len
= strlen (buf
) + 1;
11100 name
= (char *) bfd_alloc (abfd
, len
);
11104 memcpy (name
, buf
, len
);
11106 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11110 /* sizeof (thread_info.thread_context) */
11111 sect
->size
= note
->descsz
- 12;
11112 /* offsetof (thread_info.thread_context) */
11113 sect
->filepos
= note
->descpos
+ 12;
11114 sect
->alignment_power
= 2;
11116 /* thread_info.is_active_thread */
11117 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11119 if (is_active_thread
)
11120 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
11124 case NOTE_INFO_MODULE
:
11125 case NOTE_INFO_MODULE64
:
11126 /* Make a ".module/xxxxxxxx" section. */
11127 if (type
== NOTE_INFO_MODULE
)
11129 /* module_info.base_address */
11130 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
11131 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
11132 /* module_info.module_name_size */
11133 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
11135 else /* NOTE_INFO_MODULE64 */
11137 /* module_info.base_address */
11138 base_addr
= bfd_get_64 (abfd
, note
->descdata
+ 4);
11139 sprintf (buf
, ".module/%016lx", (unsigned long) base_addr
);
11140 /* module_info.module_name_size */
11141 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 12);
11144 len
= strlen (buf
) + 1;
11145 name
= (char *) bfd_alloc (abfd
, len
);
11149 memcpy (name
, buf
, len
);
11151 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11156 if (note
->descsz
< 12 + name_size
)
11158 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11159 " is too small to contain a name of size %u"),
11160 abfd
, note
->descsz
, name_size
);
11164 sect
->size
= note
->descsz
;
11165 sect
->filepos
= note
->descpos
;
11166 sect
->alignment_power
= 2;
11177 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11179 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11181 switch (note
->type
)
11187 if (bed
->elf_backend_grok_prstatus
)
11188 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
11190 #if defined (HAVE_PRSTATUS_T)
11191 return elfcore_grok_prstatus (abfd
, note
);
11196 #if defined (HAVE_PSTATUS_T)
11198 return elfcore_grok_pstatus (abfd
, note
);
11201 #if defined (HAVE_LWPSTATUS_T)
11203 return elfcore_grok_lwpstatus (abfd
, note
);
11206 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
11207 return elfcore_grok_prfpreg (abfd
, note
);
11209 case NT_WIN32PSTATUS
:
11210 return elfcore_grok_win32pstatus (abfd
, note
);
11212 case NT_PRXFPREG
: /* Linux SSE extension */
11213 if (note
->namesz
== 6
11214 && strcmp (note
->namedata
, "LINUX") == 0)
11215 return elfcore_grok_prxfpreg (abfd
, note
);
11219 case NT_X86_XSTATE
: /* Linux XSAVE extension */
11220 if (note
->namesz
== 6
11221 && strcmp (note
->namedata
, "LINUX") == 0)
11222 return elfcore_grok_xstatereg (abfd
, note
);
11227 if (note
->namesz
== 6
11228 && strcmp (note
->namedata
, "LINUX") == 0)
11229 return elfcore_grok_ppc_vmx (abfd
, note
);
11234 if (note
->namesz
== 6
11235 && strcmp (note
->namedata
, "LINUX") == 0)
11236 return elfcore_grok_ppc_vsx (abfd
, note
);
11241 if (note
->namesz
== 6
11242 && strcmp (note
->namedata
, "LINUX") == 0)
11243 return elfcore_grok_ppc_tar (abfd
, note
);
11248 if (note
->namesz
== 6
11249 && strcmp (note
->namedata
, "LINUX") == 0)
11250 return elfcore_grok_ppc_ppr (abfd
, note
);
11255 if (note
->namesz
== 6
11256 && strcmp (note
->namedata
, "LINUX") == 0)
11257 return elfcore_grok_ppc_dscr (abfd
, note
);
11262 if (note
->namesz
== 6
11263 && strcmp (note
->namedata
, "LINUX") == 0)
11264 return elfcore_grok_ppc_ebb (abfd
, note
);
11269 if (note
->namesz
== 6
11270 && strcmp (note
->namedata
, "LINUX") == 0)
11271 return elfcore_grok_ppc_pmu (abfd
, note
);
11275 case NT_PPC_TM_CGPR
:
11276 if (note
->namesz
== 6
11277 && strcmp (note
->namedata
, "LINUX") == 0)
11278 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
11282 case NT_PPC_TM_CFPR
:
11283 if (note
->namesz
== 6
11284 && strcmp (note
->namedata
, "LINUX") == 0)
11285 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
11289 case NT_PPC_TM_CVMX
:
11290 if (note
->namesz
== 6
11291 && strcmp (note
->namedata
, "LINUX") == 0)
11292 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
11296 case NT_PPC_TM_CVSX
:
11297 if (note
->namesz
== 6
11298 && strcmp (note
->namedata
, "LINUX") == 0)
11299 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
11303 case NT_PPC_TM_SPR
:
11304 if (note
->namesz
== 6
11305 && strcmp (note
->namedata
, "LINUX") == 0)
11306 return elfcore_grok_ppc_tm_spr (abfd
, note
);
11310 case NT_PPC_TM_CTAR
:
11311 if (note
->namesz
== 6
11312 && strcmp (note
->namedata
, "LINUX") == 0)
11313 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
11317 case NT_PPC_TM_CPPR
:
11318 if (note
->namesz
== 6
11319 && strcmp (note
->namedata
, "LINUX") == 0)
11320 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
11324 case NT_PPC_TM_CDSCR
:
11325 if (note
->namesz
== 6
11326 && strcmp (note
->namedata
, "LINUX") == 0)
11327 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
11331 case NT_S390_HIGH_GPRS
:
11332 if (note
->namesz
== 6
11333 && strcmp (note
->namedata
, "LINUX") == 0)
11334 return elfcore_grok_s390_high_gprs (abfd
, note
);
11338 case NT_S390_TIMER
:
11339 if (note
->namesz
== 6
11340 && strcmp (note
->namedata
, "LINUX") == 0)
11341 return elfcore_grok_s390_timer (abfd
, note
);
11345 case NT_S390_TODCMP
:
11346 if (note
->namesz
== 6
11347 && strcmp (note
->namedata
, "LINUX") == 0)
11348 return elfcore_grok_s390_todcmp (abfd
, note
);
11352 case NT_S390_TODPREG
:
11353 if (note
->namesz
== 6
11354 && strcmp (note
->namedata
, "LINUX") == 0)
11355 return elfcore_grok_s390_todpreg (abfd
, note
);
11360 if (note
->namesz
== 6
11361 && strcmp (note
->namedata
, "LINUX") == 0)
11362 return elfcore_grok_s390_ctrs (abfd
, note
);
11366 case NT_S390_PREFIX
:
11367 if (note
->namesz
== 6
11368 && strcmp (note
->namedata
, "LINUX") == 0)
11369 return elfcore_grok_s390_prefix (abfd
, note
);
11373 case NT_S390_LAST_BREAK
:
11374 if (note
->namesz
== 6
11375 && strcmp (note
->namedata
, "LINUX") == 0)
11376 return elfcore_grok_s390_last_break (abfd
, note
);
11380 case NT_S390_SYSTEM_CALL
:
11381 if (note
->namesz
== 6
11382 && strcmp (note
->namedata
, "LINUX") == 0)
11383 return elfcore_grok_s390_system_call (abfd
, note
);
11388 if (note
->namesz
== 6
11389 && strcmp (note
->namedata
, "LINUX") == 0)
11390 return elfcore_grok_s390_tdb (abfd
, note
);
11394 case NT_S390_VXRS_LOW
:
11395 if (note
->namesz
== 6
11396 && strcmp (note
->namedata
, "LINUX") == 0)
11397 return elfcore_grok_s390_vxrs_low (abfd
, note
);
11401 case NT_S390_VXRS_HIGH
:
11402 if (note
->namesz
== 6
11403 && strcmp (note
->namedata
, "LINUX") == 0)
11404 return elfcore_grok_s390_vxrs_high (abfd
, note
);
11408 case NT_S390_GS_CB
:
11409 if (note
->namesz
== 6
11410 && strcmp (note
->namedata
, "LINUX") == 0)
11411 return elfcore_grok_s390_gs_cb (abfd
, note
);
11415 case NT_S390_GS_BC
:
11416 if (note
->namesz
== 6
11417 && strcmp (note
->namedata
, "LINUX") == 0)
11418 return elfcore_grok_s390_gs_bc (abfd
, note
);
11423 if (note
->namesz
== 6
11424 && strcmp (note
->namedata
, "LINUX") == 0)
11425 return elfcore_grok_arc_v2 (abfd
, note
);
11430 if (note
->namesz
== 6
11431 && strcmp (note
->namedata
, "LINUX") == 0)
11432 return elfcore_grok_arm_vfp (abfd
, note
);
11437 if (note
->namesz
== 6
11438 && strcmp (note
->namedata
, "LINUX") == 0)
11439 return elfcore_grok_aarch_tls (abfd
, note
);
11443 case NT_ARM_HW_BREAK
:
11444 if (note
->namesz
== 6
11445 && strcmp (note
->namedata
, "LINUX") == 0)
11446 return elfcore_grok_aarch_hw_break (abfd
, note
);
11450 case NT_ARM_HW_WATCH
:
11451 if (note
->namesz
== 6
11452 && strcmp (note
->namedata
, "LINUX") == 0)
11453 return elfcore_grok_aarch_hw_watch (abfd
, note
);
11458 if (note
->namesz
== 6
11459 && strcmp (note
->namedata
, "LINUX") == 0)
11460 return elfcore_grok_aarch_sve (abfd
, note
);
11464 case NT_ARM_PAC_MASK
:
11465 if (note
->namesz
== 6
11466 && strcmp (note
->namedata
, "LINUX") == 0)
11467 return elfcore_grok_aarch_pauth (abfd
, note
);
11471 case NT_ARM_TAGGED_ADDR_CTRL
:
11472 if (note
->namesz
== 6
11473 && strcmp (note
->namedata
, "LINUX") == 0)
11474 return elfcore_grok_aarch_mte (abfd
, note
);
11479 if (note
->namesz
== 6
11480 && strcmp (note
->namedata
, "LINUX") == 0)
11481 return elfcore_grok_aarch_ssve (abfd
, note
);
11486 if (note
->namesz
== 6
11487 && strcmp (note
->namedata
, "LINUX") == 0)
11488 return elfcore_grok_aarch_za (abfd
, note
);
11493 if (note
->namesz
== 6
11494 && strcmp (note
->namedata
, "LINUX") == 0)
11495 return elfcore_grok_aarch_zt (abfd
, note
);
11500 if (note
->namesz
== 4
11501 && strcmp (note
->namedata
, "GDB") == 0)
11502 return elfcore_grok_gdb_tdesc (abfd
, note
);
11507 if (note
->namesz
== 4
11508 && strcmp (note
->namedata
, "GDB") == 0)
11509 return elfcore_grok_riscv_csr (abfd
, note
);
11513 case NT_LARCH_CPUCFG
:
11514 if (note
->namesz
== 6
11515 && strcmp (note
->namedata
, "LINUX") == 0)
11516 return elfcore_grok_loongarch_cpucfg (abfd
, note
);
11521 if (note
->namesz
== 6
11522 && strcmp (note
->namedata
, "LINUX") == 0)
11523 return elfcore_grok_loongarch_lbt (abfd
, note
);
11528 if (note
->namesz
== 6
11529 && strcmp (note
->namedata
, "LINUX") == 0)
11530 return elfcore_grok_loongarch_lsx (abfd
, note
);
11534 case NT_LARCH_LASX
:
11535 if (note
->namesz
== 6
11536 && strcmp (note
->namedata
, "LINUX") == 0)
11537 return elfcore_grok_loongarch_lasx (abfd
, note
);
11543 if (bed
->elf_backend_grok_psinfo
)
11544 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
11546 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11547 return elfcore_grok_psinfo (abfd
, note
);
11553 return elfcore_make_auxv_note_section (abfd
, note
, 0);
11556 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
11560 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
11567 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
11569 struct bfd_build_id
* build_id
;
11571 if (note
->descsz
== 0)
11574 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
11575 if (build_id
== NULL
)
11578 build_id
->size
= note
->descsz
;
11579 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
11580 abfd
->build_id
= build_id
;
11586 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11588 switch (note
->type
)
11593 case NT_GNU_PROPERTY_TYPE_0
:
11594 return _bfd_elf_parse_gnu_properties (abfd
, note
);
11596 case NT_GNU_BUILD_ID
:
11597 return elfobj_grok_gnu_build_id (abfd
, note
);
11602 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
11604 struct sdt_note
*cur
=
11605 (struct sdt_note
*) bfd_alloc (abfd
,
11606 sizeof (struct sdt_note
) + note
->descsz
);
11608 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
11609 cur
->size
= (bfd_size_type
) note
->descsz
;
11610 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
11612 elf_tdata (abfd
)->sdt_note_head
= cur
;
11618 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11620 switch (note
->type
)
11623 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
11631 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11635 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11638 if (note
->descsz
< 108)
11643 if (note
->descsz
< 120)
11651 /* Check for version 1 in pr_version. */
11652 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11657 /* Skip over pr_psinfosz. */
11658 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11662 offset
+= 4; /* Padding before pr_psinfosz. */
11666 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11667 elf_tdata (abfd
)->core
->program
11668 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
11671 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11672 elf_tdata (abfd
)->core
->command
11673 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
11676 /* Padding before pr_pid. */
11679 /* The pr_pid field was added in version "1a". */
11680 if (note
->descsz
< offset
+ 4)
11683 elf_tdata (abfd
)->core
->pid
11684 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11690 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
11696 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11697 Also compute minimum size of this note. */
11698 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
11702 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
11706 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
11707 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
11714 if (note
->descsz
< min_size
)
11717 /* Check for version 1 in pr_version. */
11718 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
11721 /* Extract size of pr_reg from pr_gregsetsz. */
11722 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11723 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
11725 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11730 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11734 /* Skip over pr_osreldate. */
11737 /* Read signal from pr_cursig. */
11738 if (elf_tdata (abfd
)->core
->signal
== 0)
11739 elf_tdata (abfd
)->core
->signal
11740 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11743 /* Read TID from pr_pid. */
11744 elf_tdata (abfd
)->core
->lwpid
11745 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
11748 /* Padding before pr_reg. */
11749 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
11752 /* Make sure that there is enough data remaining in the note. */
11753 if ((note
->descsz
- offset
) < size
)
11756 /* Make a ".reg/999" section and a ".reg" section. */
11757 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
11758 size
, note
->descpos
+ offset
);
11762 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11764 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11766 switch (note
->type
)
11769 if (bed
->elf_backend_grok_freebsd_prstatus
)
11770 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
11772 return elfcore_grok_freebsd_prstatus (abfd
, note
);
11775 return elfcore_grok_prfpreg (abfd
, note
);
11778 return elfcore_grok_freebsd_psinfo (abfd
, note
);
11780 case NT_FREEBSD_THRMISC
:
11781 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
11783 case NT_FREEBSD_PROCSTAT_PROC
:
11784 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
11787 case NT_FREEBSD_PROCSTAT_FILES
:
11788 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
11791 case NT_FREEBSD_PROCSTAT_VMMAP
:
11792 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
11795 case NT_FREEBSD_PROCSTAT_AUXV
:
11796 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11798 case NT_FREEBSD_X86_SEGBASES
:
11799 return elfcore_make_note_pseudosection (abfd
, ".reg-x86-segbases", note
);
11801 case NT_X86_XSTATE
:
11802 return elfcore_grok_xstatereg (abfd
, note
);
11804 case NT_FREEBSD_PTLWPINFO
:
11805 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
11809 return elfcore_grok_aarch_tls (abfd
, note
);
11812 return elfcore_grok_arm_vfp (abfd
, note
);
11820 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
11824 cp
= strchr (note
->namedata
, '@');
11827 *lwpidp
= atoi(cp
+ 1);
11834 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11836 if (note
->descsz
<= 0x7c + 31)
11839 /* Signal number at offset 0x08. */
11840 elf_tdata (abfd
)->core
->signal
11841 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11843 /* Process ID at offset 0x50. */
11844 elf_tdata (abfd
)->core
->pid
11845 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
11847 /* Command name at 0x7c (max 32 bytes, including nul). */
11848 elf_tdata (abfd
)->core
->command
11849 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
11851 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
11856 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11860 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
11861 elf_tdata (abfd
)->core
->lwpid
= lwp
;
11863 switch (note
->type
)
11865 case NT_NETBSDCORE_PROCINFO
:
11866 /* NetBSD-specific core "procinfo". Note that we expect to
11867 find this note before any of the others, which is fine,
11868 since the kernel writes this note out first when it
11869 creates a core file. */
11870 return elfcore_grok_netbsd_procinfo (abfd
, note
);
11871 case NT_NETBSDCORE_AUXV
:
11872 /* NetBSD-specific Elf Auxiliary Vector data. */
11873 return elfcore_make_auxv_note_section (abfd
, note
, 4);
11874 case NT_NETBSDCORE_LWPSTATUS
:
11875 return elfcore_make_note_pseudosection (abfd
,
11876 ".note.netbsdcore.lwpstatus",
11882 /* As of March 2020 there are no other machine-independent notes
11883 defined for NetBSD core files. If the note type is less
11884 than the start of the machine-dependent note types, we don't
11887 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
11891 switch (bfd_get_arch (abfd
))
11893 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11894 PT_GETFPREGS == mach+2. */
11896 case bfd_arch_aarch64
:
11897 case bfd_arch_alpha
:
11898 case bfd_arch_sparc
:
11899 switch (note
->type
)
11901 case NT_NETBSDCORE_FIRSTMACH
+0:
11902 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11904 case NT_NETBSDCORE_FIRSTMACH
+2:
11905 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11911 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11912 There's also old PT___GETREGS40 == mach + 1 for old reg
11913 structure which lacks GBR. */
11916 switch (note
->type
)
11918 case NT_NETBSDCORE_FIRSTMACH
+3:
11919 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11921 case NT_NETBSDCORE_FIRSTMACH
+5:
11922 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11928 /* On all other arch's, PT_GETREGS == mach+1 and
11929 PT_GETFPREGS == mach+3. */
11932 switch (note
->type
)
11934 case NT_NETBSDCORE_FIRSTMACH
+1:
11935 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
11937 case NT_NETBSDCORE_FIRSTMACH
+3:
11938 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
11948 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
11950 if (note
->descsz
<= 0x48 + 31)
11953 /* Signal number at offset 0x08. */
11954 elf_tdata (abfd
)->core
->signal
11955 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
11957 /* Process ID at offset 0x20. */
11958 elf_tdata (abfd
)->core
->pid
11959 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
11961 /* Command name at 0x48 (max 32 bytes, including nul). */
11962 elf_tdata (abfd
)->core
->command
11963 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
11968 /* Processes Solaris's process status note.
11969 sig_off ~ offsetof(prstatus_t, pr_cursig)
11970 pid_off ~ offsetof(prstatus_t, pr_pid)
11971 lwpid_off ~ offsetof(prstatus_t, pr_who)
11972 gregset_size ~ sizeof(gregset_t)
11973 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11976 elfcore_grok_solaris_prstatus (bfd
*abfd
, Elf_Internal_Note
* note
, int sig_off
,
11977 int pid_off
, int lwpid_off
, size_t gregset_size
,
11978 size_t gregset_offset
)
11980 asection
*sect
= NULL
;
11981 elf_tdata (abfd
)->core
->signal
11982 = bfd_get_16 (abfd
, note
->descdata
+ sig_off
);
11983 elf_tdata (abfd
)->core
->pid
11984 = bfd_get_32 (abfd
, note
->descdata
+ pid_off
);
11985 elf_tdata (abfd
)->core
->lwpid
11986 = bfd_get_32 (abfd
, note
->descdata
+ lwpid_off
);
11988 sect
= bfd_get_section_by_name (abfd
, ".reg");
11990 sect
->size
= gregset_size
;
11992 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
11993 note
->descpos
+ gregset_offset
);
11996 /* Gets program and arguments from a core.
11997 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11998 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
12001 elfcore_grok_solaris_info(bfd
*abfd
, Elf_Internal_Note
* note
,
12002 int prog_off
, int comm_off
)
12004 elf_tdata (abfd
)->core
->program
12005 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ prog_off
, 16);
12006 elf_tdata (abfd
)->core
->command
12007 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ comm_off
, 80);
12012 /* Processes Solaris's LWP status note.
12013 gregset_size ~ sizeof(gregset_t)
12014 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
12015 fpregset_size ~ sizeof(fpregset_t)
12016 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
12019 elfcore_grok_solaris_lwpstatus (bfd
*abfd
, Elf_Internal_Note
* note
,
12020 size_t gregset_size
, int gregset_off
,
12021 size_t fpregset_size
, int fpregset_off
)
12023 asection
*sect
= NULL
;
12024 char reg2_section_name
[16] = { 0 };
12026 (void) snprintf (reg2_section_name
, 16, "%s/%i", ".reg2",
12027 elf_tdata (abfd
)->core
->lwpid
);
12029 /* offsetof(lwpstatus_t, pr_lwpid) */
12030 elf_tdata (abfd
)->core
->lwpid
12031 = bfd_get_32 (abfd
, note
->descdata
+ 4);
12032 /* offsetof(lwpstatus_t, pr_cursig) */
12033 elf_tdata (abfd
)->core
->signal
12034 = bfd_get_16 (abfd
, note
->descdata
+ 12);
12036 sect
= bfd_get_section_by_name (abfd
, ".reg");
12038 sect
->size
= gregset_size
;
12039 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg", gregset_size
,
12040 note
->descpos
+ gregset_off
))
12043 sect
= bfd_get_section_by_name (abfd
, reg2_section_name
);
12046 sect
->size
= fpregset_size
;
12047 sect
->filepos
= note
->descpos
+ fpregset_off
;
12048 sect
->alignment_power
= 2;
12050 else if (!_bfd_elfcore_make_pseudosection (abfd
, ".reg2", fpregset_size
,
12051 note
->descpos
+ fpregset_off
))
12058 elfcore_grok_solaris_note_impl (bfd
*abfd
, Elf_Internal_Note
*note
)
12063 /* core files are identified as 32- or 64-bit, SPARC or x86,
12064 by the size of the descsz which matches the sizeof()
12065 the type appropriate for that note type (e.g., prstatus_t for
12066 SOLARIS_NT_PRSTATUS) for the corresponding architecture
12067 on Solaris. The core file bitness may differ from the bitness of
12068 gdb itself, so fixed values are used instead of sizeof().
12069 Appropriate fixed offsets are also used to obtain data from
12072 switch ((int) note
->type
)
12074 case SOLARIS_NT_PRSTATUS
:
12075 switch (note
->descsz
)
12077 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12078 return elfcore_grok_solaris_prstatus(abfd
, note
,
12079 136, 216, 308, 152, 356);
12080 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12081 return elfcore_grok_solaris_prstatus(abfd
, note
,
12082 264, 360, 520, 304, 600);
12083 case 432: /* sizeof(prstatus_t) Intel 32-bit */
12084 return elfcore_grok_solaris_prstatus(abfd
, note
,
12085 136, 216, 308, 76, 356);
12086 case 824: /* sizeof(prstatus_t) Intel 64-bit */
12087 return elfcore_grok_solaris_prstatus(abfd
, note
,
12088 264, 360, 520, 224, 600);
12093 case SOLARIS_NT_PSINFO
:
12094 case SOLARIS_NT_PRPSINFO
:
12095 switch (note
->descsz
)
12097 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12098 return elfcore_grok_solaris_info(abfd
, note
, 84, 100);
12099 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12100 return elfcore_grok_solaris_info(abfd
, note
, 120, 136);
12101 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12102 return elfcore_grok_solaris_info(abfd
, note
, 88, 104);
12103 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12104 return elfcore_grok_solaris_info(abfd
, note
, 136, 152);
12109 case SOLARIS_NT_LWPSTATUS
:
12110 switch (note
->descsz
)
12112 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12113 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12114 152, 344, 400, 496);
12115 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12116 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12117 304, 544, 544, 848);
12118 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12119 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12120 76, 344, 380, 420);
12121 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12122 return elfcore_grok_solaris_lwpstatus(abfd
, note
,
12123 224, 544, 528, 768);
12128 case SOLARIS_NT_LWPSINFO
:
12129 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12130 if (note
->descsz
== 128 || note
->descsz
== 152)
12131 elf_tdata (abfd
)->core
->lwpid
=
12132 bfd_get_32 (abfd
, note
->descdata
+ 4);
12142 /* For name starting with "CORE" this may be either a Solaris
12143 core file or a gdb-generated core file. Do Solaris-specific
12144 processing on selected note types first with
12145 elfcore_grok_solaris_note(), then process the note
12146 in elfcore_grok_note(). */
12149 elfcore_grok_solaris_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12151 if (!elfcore_grok_solaris_note_impl (abfd
, note
))
12154 return elfcore_grok_note (abfd
, note
);
12158 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12160 if (note
->type
== NT_OPENBSD_PROCINFO
)
12161 return elfcore_grok_openbsd_procinfo (abfd
, note
);
12163 if (note
->type
== NT_OPENBSD_REGS
)
12164 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
12166 if (note
->type
== NT_OPENBSD_FPREGS
)
12167 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
12169 if (note
->type
== NT_OPENBSD_XFPREGS
)
12170 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
12172 if (note
->type
== NT_OPENBSD_AUXV
)
12173 return elfcore_make_auxv_note_section (abfd
, note
, 0);
12175 if (note
->type
== NT_OPENBSD_WCOOKIE
)
12177 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
12182 sect
->size
= note
->descsz
;
12183 sect
->filepos
= note
->descpos
;
12184 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
12193 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
12195 void *ddata
= note
->descdata
;
12202 if (note
->descsz
< 16)
12205 /* nto_procfs_status 'pid' field is at offset 0. */
12206 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
12208 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12209 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
12211 /* nto_procfs_status 'flags' field is at offset 8. */
12212 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
12214 /* nto_procfs_status 'what' field is at offset 14. */
12215 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
12217 elf_tdata (abfd
)->core
->signal
= sig
;
12218 elf_tdata (abfd
)->core
->lwpid
= *tid
;
12221 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12222 do not come from signals so we make sure we set the current
12223 thread just in case. */
12224 if (flags
& 0x00000080)
12225 elf_tdata (abfd
)->core
->lwpid
= *tid
;
12227 /* Make a ".qnx_core_status/%d" section. */
12228 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
12230 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
12233 strcpy (name
, buf
);
12235 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12239 sect
->size
= note
->descsz
;
12240 sect
->filepos
= note
->descpos
;
12241 sect
->alignment_power
= 2;
12243 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
12247 elfcore_grok_nto_regs (bfd
*abfd
,
12248 Elf_Internal_Note
*note
,
12256 /* Make a "(base)/%d" section. */
12257 sprintf (buf
, "%s/%ld", base
, tid
);
12259 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
12262 strcpy (name
, buf
);
12264 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12268 sect
->size
= note
->descsz
;
12269 sect
->filepos
= note
->descpos
;
12270 sect
->alignment_power
= 2;
12272 /* This is the current thread. */
12273 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
12274 return elfcore_maybe_make_sect (abfd
, base
, sect
);
12280 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12282 /* Every GREG section has a STATUS section before it. Store the
12283 tid from the previous call to pass down to the next gregs
12285 static long tid
= 1;
12287 switch (note
->type
)
12289 case QNT_CORE_INFO
:
12290 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
12291 case QNT_CORE_STATUS
:
12292 return elfcore_grok_nto_status (abfd
, note
, &tid
);
12293 case QNT_CORE_GREG
:
12294 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
12295 case QNT_CORE_FPREG
:
12296 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
12303 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
12309 /* Use note name as section name. */
12310 len
= note
->namesz
;
12311 name
= (char *) bfd_alloc (abfd
, len
);
12314 memcpy (name
, note
->namedata
, len
);
12315 name
[len
- 1] = '\0';
12317 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
12321 sect
->size
= note
->descsz
;
12322 sect
->filepos
= note
->descpos
;
12323 sect
->alignment_power
= 1;
12328 /* Function: elfcore_write_note
12331 buffer to hold note, and current size of buffer
12335 size of data for note
12337 Writes note to end of buffer. ELF64 notes are written exactly as
12338 for ELF32, despite the current (as of 2006) ELF gabi specifying
12339 that they ought to have 8-byte namesz and descsz field, and have
12340 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12343 Pointer to realloc'd buffer, *BUFSIZ updated. */
12346 elfcore_write_note (bfd
*abfd
,
12354 Elf_External_Note
*xnp
;
12361 namesz
= strlen (name
) + 1;
12363 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
12365 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
12368 dest
= buf
+ *bufsiz
;
12369 *bufsiz
+= newspace
;
12370 xnp
= (Elf_External_Note
*) dest
;
12371 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
12372 H_PUT_32 (abfd
, size
, xnp
->descsz
);
12373 H_PUT_32 (abfd
, type
, xnp
->type
);
12377 memcpy (dest
, name
, namesz
);
12385 memcpy (dest
, input
, size
);
12395 /* gcc-8 warns (*) on all the strncpy calls in this function about
12396 possible string truncation. The "truncation" is not a bug. We
12397 have an external representation of structs with fields that are not
12398 necessarily NULL terminated and corresponding internal
12399 representation fields that are one larger so that they can always
12400 be NULL terminated.
12401 gcc versions between 4.2 and 4.6 do not allow pragma control of
12402 diagnostics inside functions, giving a hard error if you try to use
12403 the finer control available with later versions.
12404 gcc prior to 4.2 warns about diagnostic push and pop.
12405 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12406 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12407 (*) Depending on your system header files! */
12408 #if GCC_VERSION >= 8000
12409 # pragma GCC diagnostic push
12410 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12413 elfcore_write_prpsinfo (bfd
*abfd
,
12417 const char *psargs
)
12419 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12421 if (bed
->elf_backend_write_core_note
!= NULL
)
12424 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
12425 NT_PRPSINFO
, fname
, psargs
);
12430 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12431 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12432 if (bed
->s
->elfclass
== ELFCLASS32
)
12434 # if defined (HAVE_PSINFO32_T)
12436 int note_type
= NT_PSINFO
;
12439 int note_type
= NT_PRPSINFO
;
12442 memset (&data
, 0, sizeof (data
));
12443 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
12444 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
12445 return elfcore_write_note (abfd
, buf
, bufsiz
,
12446 "CORE", note_type
, &data
, sizeof (data
));
12451 # if defined (HAVE_PSINFO_T)
12453 int note_type
= NT_PSINFO
;
12456 int note_type
= NT_PRPSINFO
;
12459 memset (&data
, 0, sizeof (data
));
12460 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
12461 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
12462 return elfcore_write_note (abfd
, buf
, bufsiz
,
12463 "CORE", note_type
, &data
, sizeof (data
));
12465 #endif /* PSINFO_T or PRPSINFO_T */
12470 #if GCC_VERSION >= 8000
12471 # pragma GCC diagnostic pop
12475 elfcore_write_linux_prpsinfo32
12476 (bfd
*abfd
, char *buf
, int *bufsiz
,
12477 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
12479 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
12481 struct elf_external_linux_prpsinfo32_ugid16 data
;
12483 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
12484 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
12485 &data
, sizeof (data
));
12489 struct elf_external_linux_prpsinfo32_ugid32 data
;
12491 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
12492 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
12493 &data
, sizeof (data
));
12498 elfcore_write_linux_prpsinfo64
12499 (bfd
*abfd
, char *buf
, int *bufsiz
,
12500 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
12502 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
12504 struct elf_external_linux_prpsinfo64_ugid16 data
;
12506 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
12507 return elfcore_write_note (abfd
, buf
, bufsiz
,
12508 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
12512 struct elf_external_linux_prpsinfo64_ugid32 data
;
12514 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
12515 return elfcore_write_note (abfd
, buf
, bufsiz
,
12516 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
12521 elfcore_write_prstatus (bfd
*abfd
,
12528 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12530 if (bed
->elf_backend_write_core_note
!= NULL
)
12533 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
12535 pid
, cursig
, gregs
);
12540 #if defined (HAVE_PRSTATUS_T)
12541 #if defined (HAVE_PRSTATUS32_T)
12542 if (bed
->s
->elfclass
== ELFCLASS32
)
12544 prstatus32_t prstat
;
12546 memset (&prstat
, 0, sizeof (prstat
));
12547 prstat
.pr_pid
= pid
;
12548 prstat
.pr_cursig
= cursig
;
12549 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
12550 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
12551 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
12558 memset (&prstat
, 0, sizeof (prstat
));
12559 prstat
.pr_pid
= pid
;
12560 prstat
.pr_cursig
= cursig
;
12561 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
12562 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
12563 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
12565 #endif /* HAVE_PRSTATUS_T */
12571 #if defined (HAVE_LWPSTATUS_T)
12573 elfcore_write_lwpstatus (bfd
*abfd
,
12580 lwpstatus_t lwpstat
;
12581 const char *note_name
= "CORE";
12583 memset (&lwpstat
, 0, sizeof (lwpstat
));
12584 lwpstat
.pr_lwpid
= pid
>> 16;
12585 lwpstat
.pr_cursig
= cursig
;
12586 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12587 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
12588 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12589 #if !defined(gregs)
12590 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
12591 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
12593 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
12594 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
12597 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12598 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
12600 #endif /* HAVE_LWPSTATUS_T */
12602 #if defined (HAVE_PSTATUS_T)
12604 elfcore_write_pstatus (bfd
*abfd
,
12608 int cursig ATTRIBUTE_UNUSED
,
12609 const void *gregs ATTRIBUTE_UNUSED
)
12611 const char *note_name
= "CORE";
12612 #if defined (HAVE_PSTATUS32_T)
12613 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12615 if (bed
->s
->elfclass
== ELFCLASS32
)
12619 memset (&pstat
, 0, sizeof (pstat
));
12620 pstat
.pr_pid
= pid
& 0xffff;
12621 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12622 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12630 memset (&pstat
, 0, sizeof (pstat
));
12631 pstat
.pr_pid
= pid
& 0xffff;
12632 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
12633 NT_PSTATUS
, &pstat
, sizeof (pstat
));
12637 #endif /* HAVE_PSTATUS_T */
12640 elfcore_write_prfpreg (bfd
*abfd
,
12643 const void *fpregs
,
12646 const char *note_name
= "CORE";
12647 return elfcore_write_note (abfd
, buf
, bufsiz
,
12648 note_name
, NT_FPREGSET
, fpregs
, size
);
12652 elfcore_write_prxfpreg (bfd
*abfd
,
12655 const void *xfpregs
,
12658 char *note_name
= "LINUX";
12659 return elfcore_write_note (abfd
, buf
, bufsiz
,
12660 note_name
, NT_PRXFPREG
, xfpregs
, size
);
12664 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
12665 const void *xfpregs
, int size
)
12668 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
12669 note_name
= "FreeBSD";
12671 note_name
= "LINUX";
12672 return elfcore_write_note (abfd
, buf
, bufsiz
,
12673 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
12677 elfcore_write_x86_segbases (bfd
*abfd
, char *buf
, int *bufsiz
,
12678 const void *regs
, int size
)
12680 char *note_name
= "FreeBSD";
12681 return elfcore_write_note (abfd
, buf
, bufsiz
,
12682 note_name
, NT_FREEBSD_X86_SEGBASES
, regs
, size
);
12686 elfcore_write_ppc_vmx (bfd
*abfd
,
12689 const void *ppc_vmx
,
12692 char *note_name
= "LINUX";
12693 return elfcore_write_note (abfd
, buf
, bufsiz
,
12694 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
12698 elfcore_write_ppc_vsx (bfd
*abfd
,
12701 const void *ppc_vsx
,
12704 char *note_name
= "LINUX";
12705 return elfcore_write_note (abfd
, buf
, bufsiz
,
12706 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
12710 elfcore_write_ppc_tar (bfd
*abfd
,
12713 const void *ppc_tar
,
12716 char *note_name
= "LINUX";
12717 return elfcore_write_note (abfd
, buf
, bufsiz
,
12718 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
12722 elfcore_write_ppc_ppr (bfd
*abfd
,
12725 const void *ppc_ppr
,
12728 char *note_name
= "LINUX";
12729 return elfcore_write_note (abfd
, buf
, bufsiz
,
12730 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
12734 elfcore_write_ppc_dscr (bfd
*abfd
,
12737 const void *ppc_dscr
,
12740 char *note_name
= "LINUX";
12741 return elfcore_write_note (abfd
, buf
, bufsiz
,
12742 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
12746 elfcore_write_ppc_ebb (bfd
*abfd
,
12749 const void *ppc_ebb
,
12752 char *note_name
= "LINUX";
12753 return elfcore_write_note (abfd
, buf
, bufsiz
,
12754 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
12758 elfcore_write_ppc_pmu (bfd
*abfd
,
12761 const void *ppc_pmu
,
12764 char *note_name
= "LINUX";
12765 return elfcore_write_note (abfd
, buf
, bufsiz
,
12766 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
12770 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
12773 const void *ppc_tm_cgpr
,
12776 char *note_name
= "LINUX";
12777 return elfcore_write_note (abfd
, buf
, bufsiz
,
12778 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
12782 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
12785 const void *ppc_tm_cfpr
,
12788 char *note_name
= "LINUX";
12789 return elfcore_write_note (abfd
, buf
, bufsiz
,
12790 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
12794 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
12797 const void *ppc_tm_cvmx
,
12800 char *note_name
= "LINUX";
12801 return elfcore_write_note (abfd
, buf
, bufsiz
,
12802 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
12806 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
12809 const void *ppc_tm_cvsx
,
12812 char *note_name
= "LINUX";
12813 return elfcore_write_note (abfd
, buf
, bufsiz
,
12814 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
12818 elfcore_write_ppc_tm_spr (bfd
*abfd
,
12821 const void *ppc_tm_spr
,
12824 char *note_name
= "LINUX";
12825 return elfcore_write_note (abfd
, buf
, bufsiz
,
12826 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
12830 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
12833 const void *ppc_tm_ctar
,
12836 char *note_name
= "LINUX";
12837 return elfcore_write_note (abfd
, buf
, bufsiz
,
12838 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
12842 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
12845 const void *ppc_tm_cppr
,
12848 char *note_name
= "LINUX";
12849 return elfcore_write_note (abfd
, buf
, bufsiz
,
12850 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
12854 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
12857 const void *ppc_tm_cdscr
,
12860 char *note_name
= "LINUX";
12861 return elfcore_write_note (abfd
, buf
, bufsiz
,
12862 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
12866 elfcore_write_s390_high_gprs (bfd
*abfd
,
12869 const void *s390_high_gprs
,
12872 char *note_name
= "LINUX";
12873 return elfcore_write_note (abfd
, buf
, bufsiz
,
12874 note_name
, NT_S390_HIGH_GPRS
,
12875 s390_high_gprs
, size
);
12879 elfcore_write_s390_timer (bfd
*abfd
,
12882 const void *s390_timer
,
12885 char *note_name
= "LINUX";
12886 return elfcore_write_note (abfd
, buf
, bufsiz
,
12887 note_name
, NT_S390_TIMER
, s390_timer
, size
);
12891 elfcore_write_s390_todcmp (bfd
*abfd
,
12894 const void *s390_todcmp
,
12897 char *note_name
= "LINUX";
12898 return elfcore_write_note (abfd
, buf
, bufsiz
,
12899 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
12903 elfcore_write_s390_todpreg (bfd
*abfd
,
12906 const void *s390_todpreg
,
12909 char *note_name
= "LINUX";
12910 return elfcore_write_note (abfd
, buf
, bufsiz
,
12911 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
12915 elfcore_write_s390_ctrs (bfd
*abfd
,
12918 const void *s390_ctrs
,
12921 char *note_name
= "LINUX";
12922 return elfcore_write_note (abfd
, buf
, bufsiz
,
12923 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
12927 elfcore_write_s390_prefix (bfd
*abfd
,
12930 const void *s390_prefix
,
12933 char *note_name
= "LINUX";
12934 return elfcore_write_note (abfd
, buf
, bufsiz
,
12935 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
12939 elfcore_write_s390_last_break (bfd
*abfd
,
12942 const void *s390_last_break
,
12945 char *note_name
= "LINUX";
12946 return elfcore_write_note (abfd
, buf
, bufsiz
,
12947 note_name
, NT_S390_LAST_BREAK
,
12948 s390_last_break
, size
);
12952 elfcore_write_s390_system_call (bfd
*abfd
,
12955 const void *s390_system_call
,
12958 char *note_name
= "LINUX";
12959 return elfcore_write_note (abfd
, buf
, bufsiz
,
12960 note_name
, NT_S390_SYSTEM_CALL
,
12961 s390_system_call
, size
);
12965 elfcore_write_s390_tdb (bfd
*abfd
,
12968 const void *s390_tdb
,
12971 char *note_name
= "LINUX";
12972 return elfcore_write_note (abfd
, buf
, bufsiz
,
12973 note_name
, NT_S390_TDB
, s390_tdb
, size
);
12977 elfcore_write_s390_vxrs_low (bfd
*abfd
,
12980 const void *s390_vxrs_low
,
12983 char *note_name
= "LINUX";
12984 return elfcore_write_note (abfd
, buf
, bufsiz
,
12985 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
12989 elfcore_write_s390_vxrs_high (bfd
*abfd
,
12992 const void *s390_vxrs_high
,
12995 char *note_name
= "LINUX";
12996 return elfcore_write_note (abfd
, buf
, bufsiz
,
12997 note_name
, NT_S390_VXRS_HIGH
,
12998 s390_vxrs_high
, size
);
13002 elfcore_write_s390_gs_cb (bfd
*abfd
,
13005 const void *s390_gs_cb
,
13008 char *note_name
= "LINUX";
13009 return elfcore_write_note (abfd
, buf
, bufsiz
,
13010 note_name
, NT_S390_GS_CB
,
13015 elfcore_write_s390_gs_bc (bfd
*abfd
,
13018 const void *s390_gs_bc
,
13021 char *note_name
= "LINUX";
13022 return elfcore_write_note (abfd
, buf
, bufsiz
,
13023 note_name
, NT_S390_GS_BC
,
13028 elfcore_write_arm_vfp (bfd
*abfd
,
13031 const void *arm_vfp
,
13034 char *note_name
= "LINUX";
13035 return elfcore_write_note (abfd
, buf
, bufsiz
,
13036 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
13040 elfcore_write_aarch_tls (bfd
*abfd
,
13043 const void *aarch_tls
,
13046 char *note_name
= "LINUX";
13047 return elfcore_write_note (abfd
, buf
, bufsiz
,
13048 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
13052 elfcore_write_aarch_hw_break (bfd
*abfd
,
13055 const void *aarch_hw_break
,
13058 char *note_name
= "LINUX";
13059 return elfcore_write_note (abfd
, buf
, bufsiz
,
13060 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
13064 elfcore_write_aarch_hw_watch (bfd
*abfd
,
13067 const void *aarch_hw_watch
,
13070 char *note_name
= "LINUX";
13071 return elfcore_write_note (abfd
, buf
, bufsiz
,
13072 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
13076 elfcore_write_aarch_sve (bfd
*abfd
,
13079 const void *aarch_sve
,
13082 char *note_name
= "LINUX";
13083 return elfcore_write_note (abfd
, buf
, bufsiz
,
13084 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
13088 elfcore_write_aarch_pauth (bfd
*abfd
,
13091 const void *aarch_pauth
,
13094 char *note_name
= "LINUX";
13095 return elfcore_write_note (abfd
, buf
, bufsiz
,
13096 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
13100 elfcore_write_aarch_mte (bfd
*abfd
,
13103 const void *aarch_mte
,
13106 char *note_name
= "LINUX";
13107 return elfcore_write_note (abfd
, buf
, bufsiz
,
13108 note_name
, NT_ARM_TAGGED_ADDR_CTRL
,
13114 elfcore_write_aarch_ssve (bfd
*abfd
,
13117 const void *aarch_ssve
,
13120 char *note_name
= "LINUX";
13121 return elfcore_write_note (abfd
, buf
, bufsiz
,
13122 note_name
, NT_ARM_SSVE
,
13128 elfcore_write_aarch_za (bfd
*abfd
,
13131 const void *aarch_za
,
13134 char *note_name
= "LINUX";
13135 return elfcore_write_note (abfd
, buf
, bufsiz
,
13136 note_name
, NT_ARM_ZA
,
13141 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13142 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13143 written into. Return a pointer to the new start of the note buffer, to
13144 replace BUF which may no longer be valid. */
13147 elfcore_write_aarch_zt (bfd
*abfd
,
13150 const void *aarch_zt
,
13153 char *note_name
= "LINUX";
13154 return elfcore_write_note (abfd
, buf
, bufsiz
,
13155 note_name
, NT_ARM_ZT
,
13161 elfcore_write_arc_v2 (bfd
*abfd
,
13164 const void *arc_v2
,
13167 char *note_name
= "LINUX";
13168 return elfcore_write_note (abfd
, buf
, bufsiz
,
13169 note_name
, NT_ARC_V2
, arc_v2
, size
);
13173 elfcore_write_loongarch_cpucfg (bfd
*abfd
,
13176 const void *loongarch_cpucfg
,
13179 char *note_name
= "LINUX";
13180 return elfcore_write_note (abfd
, buf
, bufsiz
,
13181 note_name
, NT_LARCH_CPUCFG
,
13182 loongarch_cpucfg
, size
);
13186 elfcore_write_loongarch_lbt (bfd
*abfd
,
13189 const void *loongarch_lbt
,
13192 char *note_name
= "LINUX";
13193 return elfcore_write_note (abfd
, buf
, bufsiz
,
13194 note_name
, NT_LARCH_LBT
, loongarch_lbt
, size
);
13198 elfcore_write_loongarch_lsx (bfd
*abfd
,
13201 const void *loongarch_lsx
,
13204 char *note_name
= "LINUX";
13205 return elfcore_write_note (abfd
, buf
, bufsiz
,
13206 note_name
, NT_LARCH_LSX
, loongarch_lsx
, size
);
13210 elfcore_write_loongarch_lasx (bfd
*abfd
,
13213 const void *loongarch_lasx
,
13216 char *note_name
= "LINUX";
13217 return elfcore_write_note (abfd
, buf
, bufsiz
,
13218 note_name
, NT_LARCH_LASX
, loongarch_lasx
, size
);
13221 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13222 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13223 written into. Return a pointer to the new start of the note buffer, to
13224 replace BUF which may no longer be valid. */
13227 elfcore_write_riscv_csr (bfd
*abfd
,
13233 const char *note_name
= "GDB";
13234 return elfcore_write_note (abfd
, buf
, bufsiz
,
13235 note_name
, NT_RISCV_CSR
, csrs
, size
);
13238 /* Write the target description (a string) pointed to by TDESC, length
13239 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13240 note is being written into. Return a pointer to the new start of the
13241 note buffer, to replace BUF which may no longer be valid. */
13244 elfcore_write_gdb_tdesc (bfd
*abfd
,
13250 const char *note_name
= "GDB";
13251 return elfcore_write_note (abfd
, buf
, bufsiz
,
13252 note_name
, NT_GDB_TDESC
, tdesc
, size
);
13256 elfcore_write_register_note (bfd
*abfd
,
13259 const char *section
,
13263 if (strcmp (section
, ".reg2") == 0)
13264 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
13265 if (strcmp (section
, ".reg-xfp") == 0)
13266 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
13267 if (strcmp (section
, ".reg-xstate") == 0)
13268 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
13269 if (strcmp (section
, ".reg-x86-segbases") == 0)
13270 return elfcore_write_x86_segbases (abfd
, buf
, bufsiz
, data
, size
);
13271 if (strcmp (section
, ".reg-ppc-vmx") == 0)
13272 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
13273 if (strcmp (section
, ".reg-ppc-vsx") == 0)
13274 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
13275 if (strcmp (section
, ".reg-ppc-tar") == 0)
13276 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
13277 if (strcmp (section
, ".reg-ppc-ppr") == 0)
13278 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
13279 if (strcmp (section
, ".reg-ppc-dscr") == 0)
13280 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
13281 if (strcmp (section
, ".reg-ppc-ebb") == 0)
13282 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
13283 if (strcmp (section
, ".reg-ppc-pmu") == 0)
13284 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
13285 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
13286 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
13287 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
13288 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
13289 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
13290 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
13291 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
13292 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
13293 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
13294 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
13295 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
13296 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
13297 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
13298 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
13299 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
13300 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
13301 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
13302 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
13303 if (strcmp (section
, ".reg-s390-timer") == 0)
13304 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
13305 if (strcmp (section
, ".reg-s390-todcmp") == 0)
13306 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
13307 if (strcmp (section
, ".reg-s390-todpreg") == 0)
13308 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
13309 if (strcmp (section
, ".reg-s390-ctrs") == 0)
13310 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
13311 if (strcmp (section
, ".reg-s390-prefix") == 0)
13312 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
13313 if (strcmp (section
, ".reg-s390-last-break") == 0)
13314 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
13315 if (strcmp (section
, ".reg-s390-system-call") == 0)
13316 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
13317 if (strcmp (section
, ".reg-s390-tdb") == 0)
13318 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
13319 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
13320 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
13321 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
13322 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
13323 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
13324 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
13325 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
13326 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
13327 if (strcmp (section
, ".reg-arm-vfp") == 0)
13328 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
13329 if (strcmp (section
, ".reg-aarch-tls") == 0)
13330 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
13331 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
13332 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
13333 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
13334 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
13335 if (strcmp (section
, ".reg-aarch-sve") == 0)
13336 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
13337 if (strcmp (section
, ".reg-aarch-pauth") == 0)
13338 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
13339 if (strcmp (section
, ".reg-aarch-mte") == 0)
13340 return elfcore_write_aarch_mte (abfd
, buf
, bufsiz
, data
, size
);
13341 if (strcmp (section
, ".reg-aarch-ssve") == 0)
13342 return elfcore_write_aarch_ssve (abfd
, buf
, bufsiz
, data
, size
);
13343 if (strcmp (section
, ".reg-aarch-za") == 0)
13344 return elfcore_write_aarch_za (abfd
, buf
, bufsiz
, data
, size
);
13345 if (strcmp (section
, ".reg-aarch-zt") == 0)
13346 return elfcore_write_aarch_zt (abfd
, buf
, bufsiz
, data
, size
);
13347 if (strcmp (section
, ".reg-arc-v2") == 0)
13348 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
13349 if (strcmp (section
, ".gdb-tdesc") == 0)
13350 return elfcore_write_gdb_tdesc (abfd
, buf
, bufsiz
, data
, size
);
13351 if (strcmp (section
, ".reg-riscv-csr") == 0)
13352 return elfcore_write_riscv_csr (abfd
, buf
, bufsiz
, data
, size
);
13353 if (strcmp (section
, ".reg-loongarch-cpucfg") == 0)
13354 return elfcore_write_loongarch_cpucfg (abfd
, buf
, bufsiz
, data
, size
);
13355 if (strcmp (section
, ".reg-loongarch-lbt") == 0)
13356 return elfcore_write_loongarch_lbt (abfd
, buf
, bufsiz
, data
, size
);
13357 if (strcmp (section
, ".reg-loongarch-lsx") == 0)
13358 return elfcore_write_loongarch_lsx (abfd
, buf
, bufsiz
, data
, size
);
13359 if (strcmp (section
, ".reg-loongarch-lasx") == 0)
13360 return elfcore_write_loongarch_lasx (abfd
, buf
, bufsiz
, data
, size
);
13365 elfcore_write_file_note (bfd
*obfd
, char *note_data
, int *note_size
,
13366 const void *buf
, int bufsiz
)
13368 return elfcore_write_note (obfd
, note_data
, note_size
,
13369 "CORE", NT_FILE
, buf
, bufsiz
);
13373 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
13378 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13379 gABI specifies that PT_NOTE alignment should be aligned to 4
13380 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13381 align is less than 4, we use 4 byte alignment. */
13384 if (align
!= 4 && align
!= 8)
13388 while (p
< buf
+ size
)
13390 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
13391 Elf_Internal_Note in
;
13393 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
13396 in
.type
= H_GET_32 (abfd
, xnp
->type
);
13398 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
13399 in
.namedata
= xnp
->name
;
13400 if (in
.namesz
> buf
- in
.namedata
+ size
)
13403 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
13404 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
13405 in
.descpos
= offset
+ (in
.descdata
- buf
);
13407 && (in
.descdata
>= buf
+ size
13408 || in
.descsz
> buf
- in
.descdata
+ size
))
13411 switch (bfd_get_format (abfd
))
13418 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13421 const char * string
;
13423 bool (*func
) (bfd
*, Elf_Internal_Note
*);
13427 GROKER_ELEMENT ("", elfcore_grok_note
),
13428 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
13429 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
13430 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note
),
13431 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
13432 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
13433 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
),
13434 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note
)
13436 #undef GROKER_ELEMENT
13439 for (i
= ARRAY_SIZE (grokers
); i
--;)
13441 if (in
.namesz
>= grokers
[i
].len
13442 && strncmp (in
.namedata
, grokers
[i
].string
,
13443 grokers
[i
].len
) == 0)
13445 if (! grokers
[i
].func (abfd
, & in
))
13454 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
13456 if (! elfobj_grok_gnu_note (abfd
, &in
))
13459 else if (in
.namesz
== sizeof "stapsdt"
13460 && strcmp (in
.namedata
, "stapsdt") == 0)
13462 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
13468 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
13475 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
13480 if (size
== 0 || (size
+ 1) == 0)
13483 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
13486 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
13490 /* PR 17512: file: ec08f814
13491 0-termintate the buffer so that string searches will not overflow. */
13494 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
13504 /* Providing external access to the ELF program header table. */
13506 /* Return an upper bound on the number of bytes required to store a
13507 copy of ABFD's program header table entries. Return -1 if an error
13508 occurs; bfd_get_error will return an appropriate code. */
13511 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
13513 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
13515 bfd_set_error (bfd_error_wrong_format
);
13519 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
13522 /* Copy ABFD's program header table entries to *PHDRS. The entries
13523 will be stored as an array of Elf_Internal_Phdr structures, as
13524 defined in include/elf/internal.h. To find out how large the
13525 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13527 Return the number of program header table entries read, or -1 if an
13528 error occurs; bfd_get_error will return an appropriate code. */
13531 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
13535 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
13537 bfd_set_error (bfd_error_wrong_format
);
13541 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
13542 if (num_phdrs
!= 0)
13543 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
13544 num_phdrs
* sizeof (Elf_Internal_Phdr
));
13549 enum elf_reloc_type_class
13550 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13551 const asection
*rel_sec ATTRIBUTE_UNUSED
,
13552 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
13554 return reloc_class_normal
;
13557 /* For RELA architectures, return the relocation value for a
13558 relocation against a local symbol. */
13561 _bfd_elf_rela_local_sym (bfd
*abfd
,
13562 Elf_Internal_Sym
*sym
,
13564 Elf_Internal_Rela
*rel
)
13566 asection
*sec
= *psec
;
13567 bfd_vma relocation
;
13569 relocation
= (sec
->output_section
->vma
13570 + sec
->output_offset
13572 if ((sec
->flags
& SEC_MERGE
)
13573 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
13574 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
13577 _bfd_merged_section_offset (abfd
, psec
,
13578 elf_section_data (sec
)->sec_info
,
13579 sym
->st_value
+ rel
->r_addend
);
13582 /* If we have changed the section, and our original section is
13583 marked with SEC_EXCLUDE, it means that the original
13584 SEC_MERGE section has been completely subsumed in some
13585 other SEC_MERGE section. In this case, we need to leave
13586 some info around for --emit-relocs. */
13587 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
13588 sec
->kept_section
= *psec
;
13591 rel
->r_addend
-= relocation
;
13592 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
13598 _bfd_elf_rel_local_sym (bfd
*abfd
,
13599 Elf_Internal_Sym
*sym
,
13603 asection
*sec
= *psec
;
13605 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
13606 return sym
->st_value
+ addend
;
13608 return _bfd_merged_section_offset (abfd
, psec
,
13609 elf_section_data (sec
)->sec_info
,
13610 sym
->st_value
+ addend
);
13613 /* Adjust an address within a section. Given OFFSET within SEC, return
13614 the new offset within the section, based upon changes made to the
13615 section. Returns -1 if the offset is now invalid.
13616 The offset (in abnd out) is in target sized bytes, however big a
13620 _bfd_elf_section_offset (bfd
*abfd
,
13621 struct bfd_link_info
*info
,
13625 switch (sec
->sec_info_type
)
13627 case SEC_INFO_TYPE_STABS
:
13628 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
13630 case SEC_INFO_TYPE_EH_FRAME
:
13631 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
13634 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
13636 /* Reverse the offset. */
13637 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13638 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
13640 /* address_size and sec->size are in octets. Convert
13641 to bytes before subtracting the original offset. */
13642 offset
= ((sec
->size
- address_size
)
13643 / bfd_octets_per_byte (abfd
, sec
) - offset
);
13650 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
13651 long symcount ATTRIBUTE_UNUSED
,
13652 asymbol
**syms ATTRIBUTE_UNUSED
,
13657 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13660 const char *relplt_name
;
13661 bool (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bool);
13665 Elf_Internal_Shdr
*hdr
;
13671 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
13674 if (dynsymcount
<= 0)
13677 if (!bed
->plt_sym_val
)
13680 relplt_name
= bed
->relplt_name
;
13681 if (relplt_name
== NULL
)
13682 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
13683 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
13684 if (relplt
== NULL
)
13687 hdr
= &elf_section_data (relplt
)->this_hdr
;
13688 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
13689 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
13692 plt
= bfd_get_section_by_name (abfd
, ".plt");
13696 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
13697 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, true))
13700 count
= NUM_SHDR_ENTRIES (hdr
);
13701 size
= count
* sizeof (asymbol
);
13702 p
= relplt
->relocation
;
13703 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13705 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
13706 if (p
->addend
!= 0)
13709 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
13711 size
+= sizeof ("+0x") - 1 + 8;
13716 s
= *ret
= (asymbol
*) bfd_malloc (size
);
13720 names
= (char *) (s
+ count
);
13721 p
= relplt
->relocation
;
13723 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
13728 addr
= bed
->plt_sym_val (i
, plt
, p
);
13729 if (addr
== (bfd_vma
) -1)
13732 *s
= **p
->sym_ptr_ptr
;
13733 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13734 we are defining a symbol, ensure one of them is set. */
13735 if ((s
->flags
& BSF_LOCAL
) == 0)
13736 s
->flags
|= BSF_GLOBAL
;
13737 s
->flags
|= BSF_SYNTHETIC
;
13739 s
->value
= addr
- plt
->vma
;
13742 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
13743 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
13745 if (p
->addend
!= 0)
13749 memcpy (names
, "+0x", sizeof ("+0x") - 1);
13750 names
+= sizeof ("+0x") - 1;
13751 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
13752 for (a
= buf
; *a
== '0'; ++a
)
13755 memcpy (names
, a
, len
);
13758 memcpy (names
, "@plt", sizeof ("@plt"));
13759 names
+= sizeof ("@plt");
13766 /* It is only used by x86-64 so far.
13767 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13768 but current usage would allow all of _bfd_std_section to be zero. */
13769 static const asymbol lcomm_sym
13770 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
13771 asection _bfd_elf_large_com_section
13772 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
13773 "LARGE_COMMON", 0, SEC_IS_COMMON
);
13776 _bfd_elf_final_write_processing (bfd
*abfd
)
13778 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
13780 i_ehdrp
= elf_elfheader (abfd
);
13782 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13783 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
13785 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13786 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13787 or STB_GNU_UNIQUE binding. */
13788 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
13790 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
13791 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
13792 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
13793 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
13795 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
13796 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13797 "and FreeBSD targets"));
13798 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
13799 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13800 "only by GNU and FreeBSD targets"));
13801 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
13802 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13803 "only by GNU and FreeBSD targets"));
13804 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
13805 _bfd_error_handler (_("GNU_RETAIN section is supported "
13806 "only by GNU and FreeBSD targets"));
13807 bfd_set_error (bfd_error_sorry
);
13815 /* Return TRUE for ELF symbol types that represent functions.
13816 This is the default version of this function, which is sufficient for
13817 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13820 _bfd_elf_is_function_type (unsigned int type
)
13822 return (type
== STT_FUNC
13823 || type
== STT_GNU_IFUNC
);
13826 /* If the ELF symbol SYM might be a function in SEC, return the
13827 function size and set *CODE_OFF to the function's entry point,
13828 otherwise return zero. */
13831 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
13834 bfd_size_type size
;
13835 elf_symbol_type
* elf_sym
= (elf_symbol_type
*) sym
;
13837 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
13838 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
13839 || sym
->section
!= sec
)
13842 size
= (sym
->flags
& BSF_SYNTHETIC
) ? 0 : elf_sym
->internal_elf_sym
.st_size
;
13844 /* In theory we should check that the symbol's type satisfies
13845 _bfd_elf_is_function_type(), but there are some function-like
13846 symbols which would fail this test. (eg _start). Instead
13847 we check for hidden, local, notype symbols with zero size.
13848 This type of symbol is generated by the annobin plugin for gcc
13849 and clang, and should not be considered to be a function symbol. */
13851 && ((sym
->flags
& (BSF_SYNTHETIC
| BSF_LOCAL
)) == BSF_LOCAL
)
13852 && ELF_ST_TYPE (elf_sym
->internal_elf_sym
.st_info
) == STT_NOTYPE
13853 && ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
) == STV_HIDDEN
)
13856 *code_off
= sym
->value
;
13857 /* Do not return 0 for the function's size. */
13858 return size
? size
: 1;
13861 /* Set to non-zero to enable some debug messages. */
13862 #define DEBUG_SECONDARY_RELOCS 0
13864 /* An internal-to-the-bfd-library only section type
13865 used to indicate a cached secondary reloc section. */
13866 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13868 /* Create a BFD section to hold a secondary reloc section. */
13871 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
13872 Elf_Internal_Shdr
*hdr
,
13874 unsigned int shindex
)
13876 /* We only support RELA secondary relocs. */
13877 if (hdr
->sh_type
!= SHT_RELA
)
13880 #if DEBUG_SECONDARY_RELOCS
13881 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
13883 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
13884 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
13887 /* Read in any secondary relocs associated with SEC. */
13890 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
13892 asymbol
** symbols
,
13895 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
13897 bool result
= true;
13898 bfd_vma (*r_sym
) (bfd_vma
);
13899 ufile_ptr filesize
;
13901 #if BFD_DEFAULT_TARGET_SIZE > 32
13902 if (bfd_arch_bits_per_address (abfd
) != 32)
13903 r_sym
= elf64_r_sym
;
13906 r_sym
= elf32_r_sym
;
13908 if (!elf_section_data (sec
)->has_secondary_relocs
)
13911 /* Discover if there are any secondary reloc sections
13912 associated with SEC. */
13913 filesize
= bfd_get_file_size (abfd
);
13914 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
13916 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
13918 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
13919 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
13920 && (hdr
->sh_entsize
== ebd
->s
->sizeof_rel
13921 || hdr
->sh_entsize
== ebd
->s
->sizeof_rela
))
13923 bfd_byte
* native_relocs
;
13924 bfd_byte
* native_reloc
;
13925 arelent
* internal_relocs
;
13926 arelent
* internal_reloc
;
13928 unsigned int entsize
;
13929 unsigned int symcount
;
13930 bfd_size_type reloc_count
;
13933 if (ebd
->elf_info_to_howto
== NULL
)
13936 #if DEBUG_SECONDARY_RELOCS
13937 fprintf (stderr
, "read secondary relocs for %s from %s\n",
13938 sec
->name
, relsec
->name
);
13940 entsize
= hdr
->sh_entsize
;
13943 && ((ufile_ptr
) hdr
->sh_offset
> filesize
13944 || hdr
->sh_size
> filesize
- hdr
->sh_offset
))
13946 bfd_set_error (bfd_error_file_truncated
);
13951 native_relocs
= bfd_malloc (hdr
->sh_size
);
13952 if (native_relocs
== NULL
)
13958 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
13959 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
13961 free (native_relocs
);
13962 bfd_set_error (bfd_error_file_too_big
);
13967 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
13968 if (internal_relocs
== NULL
)
13970 free (native_relocs
);
13975 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
13976 || bfd_read (native_relocs
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
13978 free (native_relocs
);
13979 /* The internal_relocs will be freed when
13980 the memory for the bfd is released. */
13986 symcount
= bfd_get_dynamic_symcount (abfd
);
13988 symcount
= bfd_get_symcount (abfd
);
13990 for (i
= 0, internal_reloc
= internal_relocs
,
13991 native_reloc
= native_relocs
;
13993 i
++, internal_reloc
++, native_reloc
+= entsize
)
13996 Elf_Internal_Rela rela
;
13998 if (entsize
== ebd
->s
->sizeof_rel
)
13999 ebd
->s
->swap_reloc_in (abfd
, native_reloc
, & rela
);
14000 else /* entsize == ebd->s->sizeof_rela */
14001 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
14003 /* The address of an ELF reloc is section relative for an object
14004 file, and absolute for an executable file or shared library.
14005 The address of a normal BFD reloc is always section relative,
14006 and the address of a dynamic reloc is absolute.. */
14007 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
14008 internal_reloc
->address
= rela
.r_offset
;
14010 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
14012 if (r_sym (rela
.r_info
) == STN_UNDEF
)
14014 /* FIXME: This and the error case below mean that we
14015 have a symbol on relocs that is not elf_symbol_type. */
14016 internal_reloc
->sym_ptr_ptr
=
14017 bfd_abs_section_ptr
->symbol_ptr_ptr
;
14019 else if (r_sym (rela
.r_info
) > symcount
)
14022 /* xgettext:c-format */
14023 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
14024 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
14025 bfd_set_error (bfd_error_bad_value
);
14026 internal_reloc
->sym_ptr_ptr
=
14027 bfd_abs_section_ptr
->symbol_ptr_ptr
;
14034 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
14035 internal_reloc
->sym_ptr_ptr
= ps
;
14036 /* Make sure that this symbol is not removed by strip. */
14037 (*ps
)->flags
|= BSF_KEEP
;
14040 internal_reloc
->addend
= rela
.r_addend
;
14042 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
14043 if (! res
|| internal_reloc
->howto
== NULL
)
14045 #if DEBUG_SECONDARY_RELOCS
14047 "there is no howto associated with reloc %lx\n",
14054 free (native_relocs
);
14055 /* Store the internal relocs. */
14056 elf_section_data (relsec
)->sec_info
= internal_relocs
;
14063 /* Set the ELF section header fields of an output secondary reloc section. */
14066 _bfd_elf_copy_special_section_fields (const bfd
*ibfd ATTRIBUTE_UNUSED
,
14067 bfd
*obfd ATTRIBUTE_UNUSED
,
14068 const Elf_Internal_Shdr
*isection
,
14069 Elf_Internal_Shdr
*osection
)
14073 struct bfd_elf_section_data
* esd
;
14075 if (isection
== NULL
)
14078 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
14081 isec
= isection
->bfd_section
;
14085 osec
= osection
->bfd_section
;
14089 esd
= elf_section_data (osec
);
14090 BFD_ASSERT (esd
->sec_info
== NULL
);
14091 esd
->sec_info
= elf_section_data (isec
)->sec_info
;
14092 osection
->sh_type
= SHT_RELA
;
14093 osection
->sh_link
= elf_onesymtab (obfd
);
14094 if (osection
->sh_link
== 0)
14096 /* There is no symbol table - we are hosed... */
14098 /* xgettext:c-format */
14099 (_("%pB(%pA): link section cannot be set"
14100 " because the output file does not have a symbol table"),
14102 bfd_set_error (bfd_error_bad_value
);
14106 /* Find the output section that corresponds to the isection's
14108 if (isection
->sh_info
== 0
14109 || isection
->sh_info
>= elf_numsections (ibfd
))
14112 /* xgettext:c-format */
14113 (_("%pB(%pA): info section index is invalid"),
14115 bfd_set_error (bfd_error_bad_value
);
14119 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
14121 if (isection
== NULL
14122 || isection
->bfd_section
== NULL
14123 || isection
->bfd_section
->output_section
== NULL
)
14126 /* xgettext:c-format */
14127 (_("%pB(%pA): info section index cannot be set"
14128 " because the section is not in the output"),
14130 bfd_set_error (bfd_error_bad_value
);
14134 esd
= elf_section_data (isection
->bfd_section
->output_section
);
14135 BFD_ASSERT (esd
!= NULL
);
14136 osection
->sh_info
= esd
->this_idx
;
14137 esd
->has_secondary_relocs
= true;
14138 #if DEBUG_SECONDARY_RELOCS
14139 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
14140 osec
->name
, osection
->sh_link
, osection
->sh_info
);
14141 fprintf (stderr
, "mark section %s as having secondary relocs\n",
14142 bfd_section_name (isection
->bfd_section
->output_section
));
14148 /* Write out a secondary reloc section.
14150 FIXME: Currently this function can result in a serious performance penalty
14151 for files with secondary relocs and lots of sections. The proper way to
14152 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14153 relocs together and then to have this function just walk that chain. */
14156 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
14158 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
14159 bfd_vma addr_offset
;
14161 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
14162 bool result
= true;
14167 #if BFD_DEFAULT_TARGET_SIZE > 32
14168 if (bfd_arch_bits_per_address (abfd
) != 32)
14169 r_info
= elf64_r_info
;
14172 r_info
= elf32_r_info
;
14174 /* The address of an ELF reloc is section relative for an object
14175 file, and absolute for an executable file or shared library.
14176 The address of a BFD reloc is always section relative. */
14178 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
14179 addr_offset
= sec
->vma
;
14181 /* Discover if there are any secondary reloc sections
14182 associated with SEC. */
14183 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
14185 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
14186 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
14188 if (hdr
->sh_type
== SHT_RELA
14189 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
14191 asymbol
* last_sym
;
14193 size_t reloc_count
;
14195 bfd_size_type entsize
;
14196 arelent
* src_irel
;
14197 bfd_byte
* dst_rela
;
14199 if (hdr
->contents
!= NULL
)
14202 /* xgettext:c-format */
14203 (_("%pB(%pA): error: secondary reloc section processed twice"),
14205 bfd_set_error (bfd_error_bad_value
);
14210 entsize
= hdr
->sh_entsize
;
14214 /* xgettext:c-format */
14215 (_("%pB(%pA): error: secondary reloc section"
14216 " has zero sized entries"),
14218 bfd_set_error (bfd_error_bad_value
);
14222 else if (entsize
!= ebd
->s
->sizeof_rel
14223 && entsize
!= ebd
->s
->sizeof_rela
)
14226 /* xgettext:c-format */
14227 (_("%pB(%pA): error: secondary reloc section"
14228 " has non-standard sized entries"),
14230 bfd_set_error (bfd_error_bad_value
);
14235 reloc_count
= hdr
->sh_size
/ entsize
;
14236 hdr
->sh_size
= entsize
* reloc_count
;
14237 if (reloc_count
== 0)
14240 /* xgettext:c-format */
14241 (_("%pB(%pA): error: secondary reloc section is empty!"),
14243 bfd_set_error (bfd_error_bad_value
);
14248 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
14249 if (hdr
->contents
== NULL
)
14252 #if DEBUG_SECONDARY_RELOCS
14253 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
14254 reloc_count
, sec
->name
, relsec
->name
);
14258 dst_rela
= hdr
->contents
;
14259 src_irel
= (arelent
*) esd
->sec_info
;
14260 if (src_irel
== NULL
)
14263 /* xgettext:c-format */
14264 (_("%pB(%pA): error: internal relocs missing"
14265 " for secondary reloc section"),
14267 bfd_set_error (bfd_error_bad_value
);
14272 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= entsize
)
14274 Elf_Internal_Rela src_rela
;
14279 ptr
= src_irel
+ idx
;
14283 /* xgettext:c-format */
14284 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14285 abfd
, relsec
, idx
);
14286 bfd_set_error (bfd_error_bad_value
);
14291 if (ptr
->sym_ptr_ptr
== NULL
)
14293 /* FIXME: Is this an error ? */
14298 sym
= *ptr
->sym_ptr_ptr
;
14300 if (sym
== last_sym
)
14304 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
14308 /* xgettext:c-format */
14309 (_("%pB(%pA): error: secondary reloc %zu"
14310 " references a missing symbol"),
14311 abfd
, relsec
, idx
);
14312 bfd_set_error (bfd_error_bad_value
);
14321 if (sym
->the_bfd
!= NULL
14322 && sym
->the_bfd
->xvec
!= abfd
->xvec
14323 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
14326 /* xgettext:c-format */
14327 (_("%pB(%pA): error: secondary reloc %zu"
14328 " references a deleted symbol"),
14329 abfd
, relsec
, idx
);
14330 bfd_set_error (bfd_error_bad_value
);
14336 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
14337 if (ptr
->howto
== NULL
)
14340 /* xgettext:c-format */
14341 (_("%pB(%pA): error: secondary reloc %zu"
14342 " is of an unknown type"),
14343 abfd
, relsec
, idx
);
14344 bfd_set_error (bfd_error_bad_value
);
14346 src_rela
.r_info
= r_info (0, 0);
14349 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
14350 src_rela
.r_addend
= ptr
->addend
;
14352 if (entsize
== ebd
->s
->sizeof_rel
)
14353 ebd
->s
->swap_reloc_out (abfd
, &src_rela
, dst_rela
);
14354 else /* entsize == ebd->s->sizeof_rela */
14355 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);