1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2019 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 bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bfd_boolean
prep_headers (bfd
*);
55 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
56 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
,
58 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 file_ptr offset
, size_t align
);
61 /* Swap version information in and out. The version information is
62 currently size independent. If that ever changes, this code will
63 need to move into elfcode.h. */
65 /* Swap in a Verdef structure. */
68 _bfd_elf_swap_verdef_in (bfd
*abfd
,
69 const Elf_External_Verdef
*src
,
70 Elf_Internal_Verdef
*dst
)
72 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
73 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
74 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
75 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
76 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
77 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
78 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
81 /* Swap out a Verdef structure. */
84 _bfd_elf_swap_verdef_out (bfd
*abfd
,
85 const Elf_Internal_Verdef
*src
,
86 Elf_External_Verdef
*dst
)
88 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
89 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
90 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
91 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
92 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
93 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
94 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
97 /* Swap in a Verdaux structure. */
100 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
101 const Elf_External_Verdaux
*src
,
102 Elf_Internal_Verdaux
*dst
)
104 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
105 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
108 /* Swap out a Verdaux structure. */
111 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
112 const Elf_Internal_Verdaux
*src
,
113 Elf_External_Verdaux
*dst
)
115 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
116 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
119 /* Swap in a Verneed structure. */
122 _bfd_elf_swap_verneed_in (bfd
*abfd
,
123 const Elf_External_Verneed
*src
,
124 Elf_Internal_Verneed
*dst
)
126 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
127 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
128 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
129 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
130 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
133 /* Swap out a Verneed structure. */
136 _bfd_elf_swap_verneed_out (bfd
*abfd
,
137 const Elf_Internal_Verneed
*src
,
138 Elf_External_Verneed
*dst
)
140 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
141 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
142 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
143 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
144 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
147 /* Swap in a Vernaux structure. */
150 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
151 const Elf_External_Vernaux
*src
,
152 Elf_Internal_Vernaux
*dst
)
154 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
155 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
156 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
157 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
158 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
161 /* Swap out a Vernaux structure. */
164 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
165 const Elf_Internal_Vernaux
*src
,
166 Elf_External_Vernaux
*dst
)
168 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
169 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
170 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
171 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
172 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
175 /* Swap in a Versym structure. */
178 _bfd_elf_swap_versym_in (bfd
*abfd
,
179 const Elf_External_Versym
*src
,
180 Elf_Internal_Versym
*dst
)
182 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
185 /* Swap out a Versym structure. */
188 _bfd_elf_swap_versym_out (bfd
*abfd
,
189 const Elf_Internal_Versym
*src
,
190 Elf_External_Versym
*dst
)
192 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
195 /* Standard ELF hash function. Do not change this function; you will
196 cause invalid hash tables to be generated. */
199 bfd_elf_hash (const char *namearg
)
201 const unsigned char *name
= (const unsigned char *) namearg
;
206 while ((ch
= *name
++) != '\0')
209 if ((g
= (h
& 0xf0000000)) != 0)
212 /* The ELF ABI says `h &= ~g', but this is equivalent in
213 this case and on some machines one insn instead of two. */
217 return h
& 0xffffffff;
220 /* DT_GNU_HASH hash function. Do not change this function; you will
221 cause invalid hash tables to be generated. */
224 bfd_elf_gnu_hash (const char *namearg
)
226 const unsigned char *name
= (const unsigned char *) namearg
;
227 unsigned long h
= 5381;
230 while ((ch
= *name
++) != '\0')
231 h
= (h
<< 5) + h
+ ch
;
232 return h
& 0xffffffff;
235 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
236 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
238 bfd_elf_allocate_object (bfd
*abfd
,
240 enum elf_target_id object_id
)
242 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
243 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
244 if (abfd
->tdata
.any
== NULL
)
247 elf_object_id (abfd
) = object_id
;
248 if (abfd
->direction
!= read_direction
)
250 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
253 elf_tdata (abfd
)->o
= o
;
254 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
261 bfd_elf_make_object (bfd
*abfd
)
263 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
264 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
269 bfd_elf_mkcorefile (bfd
*abfd
)
271 /* I think this can be done just like an object file. */
272 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
274 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
275 return elf_tdata (abfd
)->core
!= NULL
;
279 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
281 Elf_Internal_Shdr
**i_shdrp
;
282 bfd_byte
*shstrtab
= NULL
;
284 bfd_size_type shstrtabsize
;
286 i_shdrp
= elf_elfsections (abfd
);
288 || shindex
>= elf_numsections (abfd
)
289 || i_shdrp
[shindex
] == 0)
292 shstrtab
= i_shdrp
[shindex
]->contents
;
293 if (shstrtab
== NULL
)
295 /* No cached one, attempt to read, and cache what we read. */
296 offset
= i_shdrp
[shindex
]->sh_offset
;
297 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
299 /* Allocate and clear an extra byte at the end, to prevent crashes
300 in case the string table is not terminated. */
301 if (shstrtabsize
+ 1 <= 1
302 || shstrtabsize
> bfd_get_file_size (abfd
)
303 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
304 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
306 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
308 if (bfd_get_error () != bfd_error_system_call
)
309 bfd_set_error (bfd_error_file_truncated
);
310 bfd_release (abfd
, shstrtab
);
312 /* Once we've failed to read it, make sure we don't keep
313 trying. Otherwise, we'll keep allocating space for
314 the string table over and over. */
315 i_shdrp
[shindex
]->sh_size
= 0;
318 shstrtab
[shstrtabsize
] = '\0';
319 i_shdrp
[shindex
]->contents
= shstrtab
;
321 return (char *) shstrtab
;
325 bfd_elf_string_from_elf_section (bfd
*abfd
,
326 unsigned int shindex
,
327 unsigned int strindex
)
329 Elf_Internal_Shdr
*hdr
;
334 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
337 hdr
= elf_elfsections (abfd
)[shindex
];
339 if (hdr
->contents
== NULL
)
341 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
343 /* PR 17512: file: f057ec89. */
344 /* xgettext:c-format */
345 _bfd_error_handler (_("%pB: attempt to load strings from"
346 " a non-string section (number %d)"),
351 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
356 /* PR 24273: The string section's contents may have already
357 been loaded elsewhere, eg because a corrupt file has the
358 string section index in the ELF header pointing at a group
359 section. So be paranoid, and test that the last byte of
360 the section is zero. */
361 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
365 if (strindex
>= hdr
->sh_size
)
367 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
369 /* xgettext:c-format */
370 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
371 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
372 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
374 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
378 return ((char *) hdr
->contents
) + strindex
;
381 /* Read and convert symbols to internal format.
382 SYMCOUNT specifies the number of symbols to read, starting from
383 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
384 are non-NULL, they are used to store the internal symbols, external
385 symbols, and symbol section index extensions, respectively.
386 Returns a pointer to the internal symbol buffer (malloced if necessary)
387 or NULL if there were no symbols or some kind of problem. */
390 bfd_elf_get_elf_syms (bfd
*ibfd
,
391 Elf_Internal_Shdr
*symtab_hdr
,
394 Elf_Internal_Sym
*intsym_buf
,
396 Elf_External_Sym_Shndx
*extshndx_buf
)
398 Elf_Internal_Shdr
*shndx_hdr
;
400 const bfd_byte
*esym
;
401 Elf_External_Sym_Shndx
*alloc_extshndx
;
402 Elf_External_Sym_Shndx
*shndx
;
403 Elf_Internal_Sym
*alloc_intsym
;
404 Elf_Internal_Sym
*isym
;
405 Elf_Internal_Sym
*isymend
;
406 const struct elf_backend_data
*bed
;
411 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
417 /* Normal syms might have section extension entries. */
419 if (elf_symtab_shndx_list (ibfd
) != NULL
)
421 elf_section_list
* entry
;
422 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
424 /* Find an index section that is linked to this symtab section. */
425 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
428 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
431 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
433 shndx_hdr
= & entry
->hdr
;
438 if (shndx_hdr
== NULL
)
440 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
441 /* Not really accurate, but this was how the old code used to work. */
442 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
443 /* Otherwise we do nothing. The assumption is that
444 the index table will not be needed. */
448 /* Read the symbols. */
450 alloc_extshndx
= NULL
;
452 bed
= get_elf_backend_data (ibfd
);
453 extsym_size
= bed
->s
->sizeof_sym
;
454 amt
= (bfd_size_type
) symcount
* extsym_size
;
455 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
456 if (extsym_buf
== NULL
)
458 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
459 extsym_buf
= alloc_ext
;
461 if (extsym_buf
== NULL
462 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
463 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
469 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
473 amt
= (bfd_size_type
) symcount
* sizeof (Elf_External_Sym_Shndx
);
474 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
475 if (extshndx_buf
== NULL
)
477 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
478 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
479 extshndx_buf
= alloc_extshndx
;
481 if (extshndx_buf
== NULL
482 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
483 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
490 if (intsym_buf
== NULL
)
492 alloc_intsym
= (Elf_Internal_Sym
*)
493 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
494 intsym_buf
= alloc_intsym
;
495 if (intsym_buf
== NULL
)
499 /* Convert the symbols to internal form. */
500 isymend
= intsym_buf
+ symcount
;
501 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
502 shndx
= extshndx_buf
;
504 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
505 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
507 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
508 /* xgettext:c-format */
509 _bfd_error_handler (_("%pB symbol number %lu references"
510 " nonexistent SHT_SYMTAB_SHNDX section"),
511 ibfd
, (unsigned long) symoffset
);
512 if (alloc_intsym
!= NULL
)
519 if (alloc_ext
!= NULL
)
521 if (alloc_extshndx
!= NULL
)
522 free (alloc_extshndx
);
527 /* Look up a symbol name. */
529 bfd_elf_sym_name (bfd
*abfd
,
530 Elf_Internal_Shdr
*symtab_hdr
,
531 Elf_Internal_Sym
*isym
,
535 unsigned int iname
= isym
->st_name
;
536 unsigned int shindex
= symtab_hdr
->sh_link
;
538 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
539 /* Check for a bogus st_shndx to avoid crashing. */
540 && isym
->st_shndx
< elf_numsections (abfd
))
542 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
543 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
546 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
549 else if (sym_sec
&& *name
== '\0')
550 name
= bfd_section_name (abfd
, sym_sec
);
555 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
556 sections. The first element is the flags, the rest are section
559 typedef union elf_internal_group
{
560 Elf_Internal_Shdr
*shdr
;
562 } Elf_Internal_Group
;
564 /* Return the name of the group signature symbol. Why isn't the
565 signature just a string? */
568 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
570 Elf_Internal_Shdr
*hdr
;
571 unsigned char esym
[sizeof (Elf64_External_Sym
)];
572 Elf_External_Sym_Shndx eshndx
;
573 Elf_Internal_Sym isym
;
575 /* First we need to ensure the symbol table is available. Make sure
576 that it is a symbol table section. */
577 if (ghdr
->sh_link
>= elf_numsections (abfd
))
579 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
580 if (hdr
->sh_type
!= SHT_SYMTAB
581 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
584 /* Go read the symbol. */
585 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
586 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
587 &isym
, esym
, &eshndx
) == NULL
)
590 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
593 /* Set next_in_group list pointer, and group name for NEWSECT. */
596 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
598 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
600 /* If num_group is zero, read in all SHT_GROUP sections. The count
601 is set to -1 if there are no SHT_GROUP sections. */
604 unsigned int i
, shnum
;
606 /* First count the number of groups. If we have a SHT_GROUP
607 section with just a flag word (ie. sh_size is 4), ignore it. */
608 shnum
= elf_numsections (abfd
);
611 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
612 ( (shdr)->sh_type == SHT_GROUP \
613 && (shdr)->sh_size >= minsize \
614 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
615 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
617 for (i
= 0; i
< shnum
; i
++)
619 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
621 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
627 num_group
= (unsigned) -1;
628 elf_tdata (abfd
)->num_group
= num_group
;
629 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
633 /* We keep a list of elf section headers for group sections,
634 so we can find them quickly. */
637 elf_tdata (abfd
)->num_group
= num_group
;
638 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
639 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
640 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
642 memset (elf_tdata (abfd
)->group_sect_ptr
, 0,
643 num_group
* sizeof (Elf_Internal_Shdr
*));
646 for (i
= 0; i
< shnum
; i
++)
648 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
650 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
653 Elf_Internal_Group
*dest
;
655 /* Make sure the group section has a BFD section
657 if (!bfd_section_from_shdr (abfd
, i
))
660 /* Add to list of sections. */
661 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
664 /* Read the raw contents. */
665 BFD_ASSERT (sizeof (*dest
) >= 4);
666 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
667 shdr
->contents
= (unsigned char *)
668 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
669 /* PR binutils/4110: Handle corrupt group headers. */
670 if (shdr
->contents
== NULL
)
673 /* xgettext:c-format */
674 (_("%pB: corrupt size field in group section"
675 " header: %#" PRIx64
),
676 abfd
, (uint64_t) shdr
->sh_size
);
677 bfd_set_error (bfd_error_bad_value
);
682 memset (shdr
->contents
, 0, amt
);
684 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
685 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
689 /* xgettext:c-format */
690 (_("%pB: invalid size field in group section"
691 " header: %#" PRIx64
""),
692 abfd
, (uint64_t) shdr
->sh_size
);
693 bfd_set_error (bfd_error_bad_value
);
695 /* PR 17510: If the group contents are even
696 partially corrupt, do not allow any of the
697 contents to be used. */
698 memset (shdr
->contents
, 0, amt
);
702 /* Translate raw contents, a flag word followed by an
703 array of elf section indices all in target byte order,
704 to the flag word followed by an array of elf section
706 src
= shdr
->contents
+ shdr
->sh_size
;
707 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
715 idx
= H_GET_32 (abfd
, src
);
716 if (src
== shdr
->contents
)
719 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
720 shdr
->bfd_section
->flags
721 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
726 dest
->shdr
= elf_elfsections (abfd
)[idx
];
727 /* PR binutils/23199: All sections in a
728 section group should be marked with
729 SHF_GROUP. But some tools generate
730 broken objects without SHF_GROUP. Fix
732 dest
->shdr
->sh_flags
|= SHF_GROUP
;
735 || dest
->shdr
->sh_type
== SHT_GROUP
)
738 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
746 /* PR 17510: Corrupt binaries might contain invalid groups. */
747 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
749 elf_tdata (abfd
)->num_group
= num_group
;
751 /* If all groups are invalid then fail. */
754 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
755 elf_tdata (abfd
)->num_group
= num_group
= -1;
757 (_("%pB: no valid group sections found"), abfd
);
758 bfd_set_error (bfd_error_bad_value
);
764 if (num_group
!= (unsigned) -1)
766 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
769 for (j
= 0; j
< num_group
; j
++)
771 /* Begin search from previous found group. */
772 unsigned i
= (j
+ search_offset
) % num_group
;
774 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
775 Elf_Internal_Group
*idx
;
781 idx
= (Elf_Internal_Group
*) shdr
->contents
;
782 if (idx
== NULL
|| shdr
->sh_size
< 4)
784 /* See PR 21957 for a reproducer. */
785 /* xgettext:c-format */
786 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
787 abfd
, shdr
->bfd_section
);
788 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
789 bfd_set_error (bfd_error_bad_value
);
792 n_elt
= shdr
->sh_size
/ 4;
794 /* Look through this group's sections to see if current
795 section is a member. */
797 if ((++idx
)->shdr
== hdr
)
801 /* We are a member of this group. Go looking through
802 other members to see if any others are linked via
804 idx
= (Elf_Internal_Group
*) shdr
->contents
;
805 n_elt
= shdr
->sh_size
/ 4;
807 if ((++idx
)->shdr
!= NULL
808 && (s
= idx
->shdr
->bfd_section
) != NULL
809 && elf_next_in_group (s
) != NULL
)
813 /* Snarf the group name from other member, and
814 insert current section in circular list. */
815 elf_group_name (newsect
) = elf_group_name (s
);
816 elf_next_in_group (newsect
) = elf_next_in_group (s
);
817 elf_next_in_group (s
) = newsect
;
823 gname
= group_signature (abfd
, shdr
);
826 elf_group_name (newsect
) = gname
;
828 /* Start a circular list with one element. */
829 elf_next_in_group (newsect
) = newsect
;
832 /* If the group section has been created, point to the
834 if (shdr
->bfd_section
!= NULL
)
835 elf_next_in_group (shdr
->bfd_section
) = newsect
;
837 elf_tdata (abfd
)->group_search_offset
= i
;
844 if (elf_group_name (newsect
) == NULL
)
846 /* xgettext:c-format */
847 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
855 _bfd_elf_setup_sections (bfd
*abfd
)
858 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
859 bfd_boolean result
= TRUE
;
862 /* Process SHF_LINK_ORDER. */
863 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
865 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
866 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
868 unsigned int elfsec
= this_hdr
->sh_link
;
869 /* FIXME: The old Intel compiler and old strip/objcopy may
870 not set the sh_link or sh_info fields. Hence we could
871 get the situation where elfsec is 0. */
874 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
875 if (bed
->link_order_error_handler
)
876 bed
->link_order_error_handler
877 /* xgettext:c-format */
878 (_("%pB: warning: sh_link not set for section `%pA'"),
883 asection
*linksec
= NULL
;
885 if (elfsec
< elf_numsections (abfd
))
887 this_hdr
= elf_elfsections (abfd
)[elfsec
];
888 linksec
= this_hdr
->bfd_section
;
892 Some strip/objcopy may leave an incorrect value in
893 sh_link. We don't want to proceed. */
897 /* xgettext:c-format */
898 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
899 s
->owner
, elfsec
, s
);
903 elf_linked_to_section (s
) = linksec
;
906 else if (this_hdr
->sh_type
== SHT_GROUP
907 && elf_next_in_group (s
) == NULL
)
910 /* xgettext:c-format */
911 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
912 abfd
, elf_section_data (s
)->this_idx
);
917 /* Process section groups. */
918 if (num_group
== (unsigned) -1)
921 for (i
= 0; i
< num_group
; i
++)
923 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
924 Elf_Internal_Group
*idx
;
927 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
928 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
931 /* xgettext:c-format */
932 (_("%pB: section group entry number %u is corrupt"),
938 idx
= (Elf_Internal_Group
*) shdr
->contents
;
939 n_elt
= shdr
->sh_size
/ 4;
945 if (idx
->shdr
== NULL
)
947 else if (idx
->shdr
->bfd_section
)
948 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
949 else if (idx
->shdr
->sh_type
!= SHT_RELA
950 && idx
->shdr
->sh_type
!= SHT_REL
)
952 /* There are some unknown sections in the group. */
954 /* xgettext:c-format */
955 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
958 bfd_elf_string_from_elf_section (abfd
,
959 (elf_elfheader (abfd
)
972 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
974 return elf_next_in_group (sec
) != NULL
;
978 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
980 unsigned int len
= strlen (name
);
981 char *new_name
= bfd_alloc (abfd
, len
+ 2);
982 if (new_name
== NULL
)
986 memcpy (new_name
+ 2, name
+ 1, len
);
991 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
993 unsigned int len
= strlen (name
);
994 char *new_name
= bfd_alloc (abfd
, len
);
995 if (new_name
== NULL
)
998 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1002 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
1006 int16_t major_version
;
1007 int16_t minor_version
;
1008 unsigned char slim_object
;
1010 /* Flags is a private field that is not defined publicly. */
1014 /* Make a BFD section from an ELF section. We store a pointer to the
1015 BFD section in the bfd_section field of the header. */
1018 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1019 Elf_Internal_Shdr
*hdr
,
1025 const struct elf_backend_data
*bed
;
1027 if (hdr
->bfd_section
!= NULL
)
1030 newsect
= bfd_make_section_anyway (abfd
, name
);
1031 if (newsect
== NULL
)
1034 hdr
->bfd_section
= newsect
;
1035 elf_section_data (newsect
)->this_hdr
= *hdr
;
1036 elf_section_data (newsect
)->this_idx
= shindex
;
1038 /* Always use the real type/flags. */
1039 elf_section_type (newsect
) = hdr
->sh_type
;
1040 elf_section_flags (newsect
) = hdr
->sh_flags
;
1042 newsect
->filepos
= hdr
->sh_offset
;
1044 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
1045 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
1046 || ! bfd_set_section_alignment (abfd
, newsect
,
1047 bfd_log2 (hdr
->sh_addralign
)))
1050 flags
= SEC_NO_FLAGS
;
1051 if (hdr
->sh_type
!= SHT_NOBITS
)
1052 flags
|= SEC_HAS_CONTENTS
;
1053 if (hdr
->sh_type
== SHT_GROUP
)
1055 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1058 if (hdr
->sh_type
!= SHT_NOBITS
)
1061 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1062 flags
|= SEC_READONLY
;
1063 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1065 else if ((flags
& SEC_LOAD
) != 0)
1067 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1070 newsect
->entsize
= hdr
->sh_entsize
;
1072 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1073 flags
|= SEC_STRINGS
;
1074 if (hdr
->sh_flags
& SHF_GROUP
)
1075 if (!setup_group (abfd
, hdr
, newsect
))
1077 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1078 flags
|= SEC_THREAD_LOCAL
;
1079 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1080 flags
|= SEC_EXCLUDE
;
1082 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1084 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1085 but binutils as of 2019-07-23 did not set the EI_OSABI header
1089 case ELFOSABI_FREEBSD
:
1090 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1091 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1095 if ((flags
& SEC_ALLOC
) == 0)
1097 /* The debugging sections appear to be recognized only by name,
1098 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1099 if (name
[0] == '.')
1104 p
= ".debug", n
= 6;
1105 else if (name
[1] == 'g' && name
[2] == 'n')
1106 p
= ".gnu.linkonce.wi.", n
= 17;
1107 else if (name
[1] == 'g' && name
[2] == 'd')
1108 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1109 else if (name
[1] == 'l')
1111 else if (name
[1] == 's')
1113 else if (name
[1] == 'z')
1114 p
= ".zdebug", n
= 7;
1117 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1118 flags
|= SEC_DEBUGGING
;
1122 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1123 only link a single copy of the section. This is used to support
1124 g++. g++ will emit each template expansion in its own section.
1125 The symbols will be defined as weak, so that multiple definitions
1126 are permitted. The GNU linker extension is to actually discard
1127 all but one of the sections. */
1128 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1129 && elf_next_in_group (newsect
) == NULL
)
1130 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1132 bed
= get_elf_backend_data (abfd
);
1133 if (bed
->elf_backend_section_flags
)
1134 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1137 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1140 /* We do not parse the PT_NOTE segments as we are interested even in the
1141 separate debug info files which may have the segments offsets corrupted.
1142 PT_NOTEs from the core files are currently not parsed using BFD. */
1143 if (hdr
->sh_type
== SHT_NOTE
)
1147 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1150 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1151 hdr
->sh_offset
, hdr
->sh_addralign
);
1155 if ((flags
& SEC_ALLOC
) != 0)
1157 Elf_Internal_Phdr
*phdr
;
1158 unsigned int i
, nload
;
1160 /* Some ELF linkers produce binaries with all the program header
1161 p_paddr fields zero. If we have such a binary with more than
1162 one PT_LOAD header, then leave the section lma equal to vma
1163 so that we don't create sections with overlapping lma. */
1164 phdr
= elf_tdata (abfd
)->phdr
;
1165 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1166 if (phdr
->p_paddr
!= 0)
1168 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1170 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1173 phdr
= elf_tdata (abfd
)->phdr
;
1174 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1176 if (((phdr
->p_type
== PT_LOAD
1177 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1178 || phdr
->p_type
== PT_TLS
)
1179 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1181 if ((flags
& SEC_LOAD
) == 0)
1182 newsect
->lma
= (phdr
->p_paddr
1183 + hdr
->sh_addr
- phdr
->p_vaddr
);
1185 /* We used to use the same adjustment for SEC_LOAD
1186 sections, but that doesn't work if the segment
1187 is packed with code from multiple VMAs.
1188 Instead we calculate the section LMA based on
1189 the segment LMA. It is assumed that the
1190 segment will contain sections with contiguous
1191 LMAs, even if the VMAs are not. */
1192 newsect
->lma
= (phdr
->p_paddr
1193 + hdr
->sh_offset
- phdr
->p_offset
);
1195 /* With contiguous segments, we can't tell from file
1196 offsets whether a section with zero size should
1197 be placed at the end of one segment or the
1198 beginning of the next. Decide based on vaddr. */
1199 if (hdr
->sh_addr
>= phdr
->p_vaddr
1200 && (hdr
->sh_addr
+ hdr
->sh_size
1201 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1207 /* Compress/decompress DWARF debug sections with names: .debug_* and
1208 .zdebug_*, after the section flags is set. */
1209 if ((flags
& SEC_DEBUGGING
)
1210 && ((name
[1] == 'd' && name
[6] == '_')
1211 || (name
[1] == 'z' && name
[7] == '_')))
1213 enum { nothing
, compress
, decompress
} action
= nothing
;
1214 int compression_header_size
;
1215 bfd_size_type uncompressed_size
;
1216 unsigned int uncompressed_align_power
;
1217 bfd_boolean compressed
1218 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1219 &compression_header_size
,
1221 &uncompressed_align_power
);
1224 /* Compressed section. Check if we should decompress. */
1225 if ((abfd
->flags
& BFD_DECOMPRESS
))
1226 action
= decompress
;
1229 /* Compress the uncompressed section or convert from/to .zdebug*
1230 section. Check if we should compress. */
1231 if (action
== nothing
)
1233 if (newsect
->size
!= 0
1234 && (abfd
->flags
& BFD_COMPRESS
)
1235 && compression_header_size
>= 0
1236 && uncompressed_size
> 0
1238 || ((compression_header_size
> 0)
1239 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1245 if (action
== compress
)
1247 if (!bfd_init_section_compress_status (abfd
, newsect
))
1250 /* xgettext:c-format */
1251 (_("%pB: unable to initialize compress status for section %s"),
1258 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1261 /* xgettext:c-format */
1262 (_("%pB: unable to initialize decompress status for section %s"),
1268 if (abfd
->is_linker_input
)
1271 && (action
== decompress
1272 || (action
== compress
1273 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1275 /* Convert section name from .zdebug_* to .debug_* so
1276 that linker will consider this section as a debug
1278 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1279 if (new_name
== NULL
)
1281 bfd_rename_section (abfd
, newsect
, new_name
);
1285 /* For objdump, don't rename the section. For objcopy, delay
1286 section rename to elf_fake_sections. */
1287 newsect
->flags
|= SEC_ELF_RENAME
;
1290 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1292 const char *lto_section_name
= ".gnu.lto_.lto.";
1293 if (strncmp (name
, lto_section_name
, strlen (lto_section_name
)) == 0)
1295 struct lto_section lsection
;
1296 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1297 sizeof (struct lto_section
)))
1298 abfd
->lto_slim_object
= lsection
.slim_object
;
1304 const char *const bfd_elf_section_type_names
[] =
1306 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1307 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1308 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1311 /* ELF relocs are against symbols. If we are producing relocatable
1312 output, and the reloc is against an external symbol, and nothing
1313 has given us any additional addend, the resulting reloc will also
1314 be against the same symbol. In such a case, we don't want to
1315 change anything about the way the reloc is handled, since it will
1316 all be done at final link time. Rather than put special case code
1317 into bfd_perform_relocation, all the reloc types use this howto
1318 function. It just short circuits the reloc if producing
1319 relocatable output against an external symbol. */
1321 bfd_reloc_status_type
1322 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1323 arelent
*reloc_entry
,
1325 void *data ATTRIBUTE_UNUSED
,
1326 asection
*input_section
,
1328 char **error_message ATTRIBUTE_UNUSED
)
1330 if (output_bfd
!= NULL
1331 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1332 && (! reloc_entry
->howto
->partial_inplace
1333 || reloc_entry
->addend
== 0))
1335 reloc_entry
->address
+= input_section
->output_offset
;
1336 return bfd_reloc_ok
;
1339 return bfd_reloc_continue
;
1342 /* Returns TRUE if section A matches section B.
1343 Names, addresses and links may be different, but everything else
1344 should be the same. */
1347 section_match (const Elf_Internal_Shdr
* a
,
1348 const Elf_Internal_Shdr
* b
)
1350 if (a
->sh_type
!= b
->sh_type
1351 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1352 || a
->sh_addralign
!= b
->sh_addralign
1353 || a
->sh_entsize
!= b
->sh_entsize
)
1355 if (a
->sh_type
== SHT_SYMTAB
1356 || a
->sh_type
== SHT_STRTAB
)
1358 return a
->sh_size
== b
->sh_size
;
1361 /* Find a section in OBFD that has the same characteristics
1362 as IHEADER. Return the index of this section or SHN_UNDEF if
1363 none can be found. Check's section HINT first, as this is likely
1364 to be the correct section. */
1367 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1368 const unsigned int hint
)
1370 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1373 BFD_ASSERT (iheader
!= NULL
);
1375 /* See PR 20922 for a reproducer of the NULL test. */
1376 if (hint
< elf_numsections (obfd
)
1377 && oheaders
[hint
] != NULL
1378 && section_match (oheaders
[hint
], iheader
))
1381 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1383 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1385 if (oheader
== NULL
)
1387 if (section_match (oheader
, iheader
))
1388 /* FIXME: Do we care if there is a potential for
1389 multiple matches ? */
1396 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1397 Processor specific section, based upon a matching input section.
1398 Returns TRUE upon success, FALSE otherwise. */
1401 copy_special_section_fields (const bfd
*ibfd
,
1403 const Elf_Internal_Shdr
*iheader
,
1404 Elf_Internal_Shdr
*oheader
,
1405 const unsigned int secnum
)
1407 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1408 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1409 bfd_boolean changed
= FALSE
;
1410 unsigned int sh_link
;
1412 if (oheader
->sh_type
== SHT_NOBITS
)
1414 /* This is a feature for objcopy --only-keep-debug:
1415 When a section's type is changed to NOBITS, we preserve
1416 the sh_link and sh_info fields so that they can be
1417 matched up with the original.
1419 Note: Strictly speaking these assignments are wrong.
1420 The sh_link and sh_info fields should point to the
1421 relevent sections in the output BFD, which may not be in
1422 the same location as they were in the input BFD. But
1423 the whole point of this action is to preserve the
1424 original values of the sh_link and sh_info fields, so
1425 that they can be matched up with the section headers in
1426 the original file. So strictly speaking we may be
1427 creating an invalid ELF file, but it is only for a file
1428 that just contains debug info and only for sections
1429 without any contents. */
1430 if (oheader
->sh_link
== 0)
1431 oheader
->sh_link
= iheader
->sh_link
;
1432 if (oheader
->sh_info
== 0)
1433 oheader
->sh_info
= iheader
->sh_info
;
1437 /* Allow the target a chance to decide how these fields should be set. */
1438 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1439 && bed
->elf_backend_copy_special_section_fields
1440 (ibfd
, obfd
, iheader
, oheader
))
1443 /* We have an iheader which might match oheader, and which has non-zero
1444 sh_info and/or sh_link fields. Attempt to follow those links and find
1445 the section in the output bfd which corresponds to the linked section
1446 in the input bfd. */
1447 if (iheader
->sh_link
!= SHN_UNDEF
)
1449 /* See PR 20931 for a reproducer. */
1450 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1453 /* xgettext:c-format */
1454 (_("%pB: invalid sh_link field (%d) in section number %d"),
1455 ibfd
, iheader
->sh_link
, secnum
);
1459 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1460 if (sh_link
!= SHN_UNDEF
)
1462 oheader
->sh_link
= sh_link
;
1466 /* FIXME: Should we install iheader->sh_link
1467 if we could not find a match ? */
1469 /* xgettext:c-format */
1470 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1473 if (iheader
->sh_info
)
1475 /* The sh_info field can hold arbitrary information, but if the
1476 SHF_LINK_INFO flag is set then it should be interpreted as a
1478 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1480 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1482 if (sh_link
!= SHN_UNDEF
)
1483 oheader
->sh_flags
|= SHF_INFO_LINK
;
1486 /* No idea what it means - just copy it. */
1487 sh_link
= iheader
->sh_info
;
1489 if (sh_link
!= SHN_UNDEF
)
1491 oheader
->sh_info
= sh_link
;
1496 /* xgettext:c-format */
1497 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1503 /* Copy the program header and other data from one object module to
1507 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1509 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1510 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1511 const struct elf_backend_data
*bed
;
1514 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1515 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1518 if (!elf_flags_init (obfd
))
1520 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1521 elf_flags_init (obfd
) = TRUE
;
1524 elf_gp (obfd
) = elf_gp (ibfd
);
1526 /* Also copy the EI_OSABI field. */
1527 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1528 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1530 /* If set, copy the EI_ABIVERSION field. */
1531 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1532 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1533 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1535 /* Copy object attributes. */
1536 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1538 if (iheaders
== NULL
|| oheaders
== NULL
)
1541 bed
= get_elf_backend_data (obfd
);
1543 /* Possibly copy other fields in the section header. */
1544 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1547 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1549 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1550 because of a special case need for generating separate debug info
1551 files. See below for more details. */
1553 || (oheader
->sh_type
!= SHT_NOBITS
1554 && oheader
->sh_type
< SHT_LOOS
))
1557 /* Ignore empty sections, and sections whose
1558 fields have already been initialised. */
1559 if (oheader
->sh_size
== 0
1560 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1563 /* Scan for the matching section in the input bfd.
1564 First we try for a direct mapping between the input and output sections. */
1565 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1567 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1569 if (iheader
== NULL
)
1572 if (oheader
->bfd_section
!= NULL
1573 && iheader
->bfd_section
!= NULL
1574 && iheader
->bfd_section
->output_section
!= NULL
1575 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1577 /* We have found a connection from the input section to the
1578 output section. Attempt to copy the header fields. If
1579 this fails then do not try any further sections - there
1580 should only be a one-to-one mapping between input and output. */
1581 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1582 j
= elf_numsections (ibfd
);
1587 if (j
< elf_numsections (ibfd
))
1590 /* That failed. So try to deduce the corresponding input section.
1591 Unfortunately we cannot compare names as the output string table
1592 is empty, so instead we check size, address and type. */
1593 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1595 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1597 if (iheader
== NULL
)
1600 /* Try matching fields in the input section's header.
1601 Since --only-keep-debug turns all non-debug sections into
1602 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1604 if ((oheader
->sh_type
== SHT_NOBITS
1605 || iheader
->sh_type
== oheader
->sh_type
)
1606 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1607 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1608 && iheader
->sh_addralign
== oheader
->sh_addralign
1609 && iheader
->sh_entsize
== oheader
->sh_entsize
1610 && iheader
->sh_size
== oheader
->sh_size
1611 && iheader
->sh_addr
== oheader
->sh_addr
1612 && (iheader
->sh_info
!= oheader
->sh_info
1613 || iheader
->sh_link
!= oheader
->sh_link
))
1615 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1620 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1622 /* Final attempt. Call the backend copy function
1623 with a NULL input section. */
1624 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1625 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1633 get_segment_type (unsigned int p_type
)
1638 case PT_NULL
: pt
= "NULL"; break;
1639 case PT_LOAD
: pt
= "LOAD"; break;
1640 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1641 case PT_INTERP
: pt
= "INTERP"; break;
1642 case PT_NOTE
: pt
= "NOTE"; break;
1643 case PT_SHLIB
: pt
= "SHLIB"; break;
1644 case PT_PHDR
: pt
= "PHDR"; break;
1645 case PT_TLS
: pt
= "TLS"; break;
1646 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1647 case PT_GNU_STACK
: pt
= "STACK"; break;
1648 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1649 default: pt
= NULL
; break;
1654 /* Print out the program headers. */
1657 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1659 FILE *f
= (FILE *) farg
;
1660 Elf_Internal_Phdr
*p
;
1662 bfd_byte
*dynbuf
= NULL
;
1664 p
= elf_tdata (abfd
)->phdr
;
1669 fprintf (f
, _("\nProgram Header:\n"));
1670 c
= elf_elfheader (abfd
)->e_phnum
;
1671 for (i
= 0; i
< c
; i
++, p
++)
1673 const char *pt
= get_segment_type (p
->p_type
);
1678 sprintf (buf
, "0x%lx", p
->p_type
);
1681 fprintf (f
, "%8s off 0x", pt
);
1682 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1683 fprintf (f
, " vaddr 0x");
1684 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1685 fprintf (f
, " paddr 0x");
1686 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1687 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1688 fprintf (f
, " filesz 0x");
1689 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1690 fprintf (f
, " memsz 0x");
1691 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1692 fprintf (f
, " flags %c%c%c",
1693 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1694 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1695 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1696 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1697 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1702 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1705 unsigned int elfsec
;
1706 unsigned long shlink
;
1707 bfd_byte
*extdyn
, *extdynend
;
1709 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1711 fprintf (f
, _("\nDynamic Section:\n"));
1713 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1716 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1717 if (elfsec
== SHN_BAD
)
1719 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1721 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1722 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1725 /* PR 17512: file: 6f427532. */
1726 if (s
->size
< extdynsize
)
1728 extdynend
= extdyn
+ s
->size
;
1729 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1731 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1733 Elf_Internal_Dyn dyn
;
1734 const char *name
= "";
1736 bfd_boolean stringp
;
1737 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1739 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1741 if (dyn
.d_tag
== DT_NULL
)
1748 if (bed
->elf_backend_get_target_dtag
)
1749 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1751 if (!strcmp (name
, ""))
1753 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1758 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1759 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1760 case DT_PLTGOT
: name
= "PLTGOT"; break;
1761 case DT_HASH
: name
= "HASH"; break;
1762 case DT_STRTAB
: name
= "STRTAB"; break;
1763 case DT_SYMTAB
: name
= "SYMTAB"; break;
1764 case DT_RELA
: name
= "RELA"; break;
1765 case DT_RELASZ
: name
= "RELASZ"; break;
1766 case DT_RELAENT
: name
= "RELAENT"; break;
1767 case DT_STRSZ
: name
= "STRSZ"; break;
1768 case DT_SYMENT
: name
= "SYMENT"; break;
1769 case DT_INIT
: name
= "INIT"; break;
1770 case DT_FINI
: name
= "FINI"; break;
1771 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1772 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1773 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1774 case DT_REL
: name
= "REL"; break;
1775 case DT_RELSZ
: name
= "RELSZ"; break;
1776 case DT_RELENT
: name
= "RELENT"; break;
1777 case DT_PLTREL
: name
= "PLTREL"; break;
1778 case DT_DEBUG
: name
= "DEBUG"; break;
1779 case DT_TEXTREL
: name
= "TEXTREL"; break;
1780 case DT_JMPREL
: name
= "JMPREL"; break;
1781 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1782 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1783 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1784 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1785 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1786 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1787 case DT_FLAGS
: name
= "FLAGS"; break;
1788 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1789 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1790 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1791 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1792 case DT_MOVEENT
: name
= "MOVEENT"; break;
1793 case DT_MOVESZ
: name
= "MOVESZ"; break;
1794 case DT_FEATURE
: name
= "FEATURE"; break;
1795 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1796 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1797 case DT_SYMINENT
: name
= "SYMINENT"; break;
1798 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1799 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1800 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1801 case DT_PLTPAD
: name
= "PLTPAD"; break;
1802 case DT_MOVETAB
: name
= "MOVETAB"; break;
1803 case DT_SYMINFO
: name
= "SYMINFO"; break;
1804 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1805 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1806 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1807 case DT_VERSYM
: name
= "VERSYM"; break;
1808 case DT_VERDEF
: name
= "VERDEF"; break;
1809 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1810 case DT_VERNEED
: name
= "VERNEED"; break;
1811 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1812 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1813 case DT_USED
: name
= "USED"; break;
1814 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1815 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1818 fprintf (f
, " %-20s ", name
);
1822 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1827 unsigned int tagv
= dyn
.d_un
.d_val
;
1829 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1832 fprintf (f
, "%s", string
);
1841 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1842 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1844 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1848 if (elf_dynverdef (abfd
) != 0)
1850 Elf_Internal_Verdef
*t
;
1852 fprintf (f
, _("\nVersion definitions:\n"));
1853 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1855 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1856 t
->vd_flags
, t
->vd_hash
,
1857 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1858 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1860 Elf_Internal_Verdaux
*a
;
1863 for (a
= t
->vd_auxptr
->vda_nextptr
;
1867 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1873 if (elf_dynverref (abfd
) != 0)
1875 Elf_Internal_Verneed
*t
;
1877 fprintf (f
, _("\nVersion References:\n"));
1878 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1880 Elf_Internal_Vernaux
*a
;
1882 fprintf (f
, _(" required from %s:\n"),
1883 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1884 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1885 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1886 a
->vna_flags
, a
->vna_other
,
1887 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1899 /* Get version string. */
1902 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1903 bfd_boolean
*hidden
)
1905 const char *version_string
= NULL
;
1906 if (elf_dynversym (abfd
) != 0
1907 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1909 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1911 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1912 vernum
&= VERSYM_VERSION
;
1915 version_string
= "";
1916 else if (vernum
== 1
1917 && (vernum
> elf_tdata (abfd
)->cverdefs
1918 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1920 version_string
= "Base";
1921 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1923 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1926 Elf_Internal_Verneed
*t
;
1928 version_string
= _("<corrupt>");
1929 for (t
= elf_tdata (abfd
)->verref
;
1933 Elf_Internal_Vernaux
*a
;
1935 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1937 if (a
->vna_other
== vernum
)
1939 version_string
= a
->vna_nodename
;
1946 return version_string
;
1949 /* Display ELF-specific fields of a symbol. */
1952 bfd_elf_print_symbol (bfd
*abfd
,
1955 bfd_print_symbol_type how
)
1957 FILE *file
= (FILE *) filep
;
1960 case bfd_print_symbol_name
:
1961 fprintf (file
, "%s", symbol
->name
);
1963 case bfd_print_symbol_more
:
1964 fprintf (file
, "elf ");
1965 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1966 fprintf (file
, " %x", symbol
->flags
);
1968 case bfd_print_symbol_all
:
1970 const char *section_name
;
1971 const char *name
= NULL
;
1972 const struct elf_backend_data
*bed
;
1973 unsigned char st_other
;
1975 const char *version_string
;
1978 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1980 bed
= get_elf_backend_data (abfd
);
1981 if (bed
->elf_backend_print_symbol_all
)
1982 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1986 name
= symbol
->name
;
1987 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1990 fprintf (file
, " %s\t", section_name
);
1991 /* Print the "other" value for a symbol. For common symbols,
1992 we've already printed the size; now print the alignment.
1993 For other symbols, we have no specified alignment, and
1994 we've printed the address; now print the size. */
1995 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1996 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1998 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1999 bfd_fprintf_vma (abfd
, file
, val
);
2001 /* If we have version information, print it. */
2002 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2008 fprintf (file
, " %-11s", version_string
);
2013 fprintf (file
, " (%s)", version_string
);
2014 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2019 /* If the st_other field is not zero, print it. */
2020 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2025 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2026 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2027 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2029 /* Some other non-defined flags are also present, so print
2031 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2034 fprintf (file
, " %s", name
);
2040 /* ELF .o/exec file reading */
2042 /* Create a new bfd section from an ELF section header. */
2045 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2047 Elf_Internal_Shdr
*hdr
;
2048 Elf_Internal_Ehdr
*ehdr
;
2049 const struct elf_backend_data
*bed
;
2051 bfd_boolean ret
= TRUE
;
2052 static bfd_boolean
* sections_being_created
= NULL
;
2053 static bfd
* sections_being_created_abfd
= NULL
;
2054 static unsigned int nesting
= 0;
2056 if (shindex
>= elf_numsections (abfd
))
2061 /* PR17512: A corrupt ELF binary might contain a recursive group of
2062 sections, with each the string indices pointing to the next in the
2063 loop. Detect this here, by refusing to load a section that we are
2064 already in the process of loading. We only trigger this test if
2065 we have nested at least three sections deep as normal ELF binaries
2066 can expect to recurse at least once.
2068 FIXME: It would be better if this array was attached to the bfd,
2069 rather than being held in a static pointer. */
2071 if (sections_being_created_abfd
!= abfd
)
2072 sections_being_created
= NULL
;
2073 if (sections_being_created
== NULL
)
2075 sections_being_created
= (bfd_boolean
*)
2076 bfd_zalloc2 (abfd
, elf_numsections (abfd
), sizeof (bfd_boolean
));
2077 sections_being_created_abfd
= abfd
;
2079 if (sections_being_created
[shindex
])
2082 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2085 sections_being_created
[shindex
] = TRUE
;
2088 hdr
= elf_elfsections (abfd
)[shindex
];
2089 ehdr
= elf_elfheader (abfd
);
2090 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2095 bed
= get_elf_backend_data (abfd
);
2096 switch (hdr
->sh_type
)
2099 /* Inactive section. Throw it away. */
2102 case SHT_PROGBITS
: /* Normal section with contents. */
2103 case SHT_NOBITS
: /* .bss section. */
2104 case SHT_HASH
: /* .hash section. */
2105 case SHT_NOTE
: /* .note section. */
2106 case SHT_INIT_ARRAY
: /* .init_array section. */
2107 case SHT_FINI_ARRAY
: /* .fini_array section. */
2108 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2109 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2110 case SHT_GNU_HASH
: /* .gnu.hash section. */
2111 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2114 case SHT_DYNAMIC
: /* Dynamic linking information. */
2115 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2118 if (hdr
->sh_link
> elf_numsections (abfd
))
2120 /* PR 10478: Accept Solaris binaries with a sh_link
2121 field set to SHN_BEFORE or SHN_AFTER. */
2122 switch (bfd_get_arch (abfd
))
2125 case bfd_arch_sparc
:
2126 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2127 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2129 /* Otherwise fall through. */
2134 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2136 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2138 Elf_Internal_Shdr
*dynsymhdr
;
2140 /* The shared libraries distributed with hpux11 have a bogus
2141 sh_link field for the ".dynamic" section. Find the
2142 string table for the ".dynsym" section instead. */
2143 if (elf_dynsymtab (abfd
) != 0)
2145 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2146 hdr
->sh_link
= dynsymhdr
->sh_link
;
2150 unsigned int i
, num_sec
;
2152 num_sec
= elf_numsections (abfd
);
2153 for (i
= 1; i
< num_sec
; i
++)
2155 dynsymhdr
= elf_elfsections (abfd
)[i
];
2156 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2158 hdr
->sh_link
= dynsymhdr
->sh_link
;
2166 case SHT_SYMTAB
: /* A symbol table. */
2167 if (elf_onesymtab (abfd
) == shindex
)
2170 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2173 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2175 if (hdr
->sh_size
!= 0)
2177 /* Some assemblers erroneously set sh_info to one with a
2178 zero sh_size. ld sees this as a global symbol count
2179 of (unsigned) -1. Fix it here. */
2184 /* PR 18854: A binary might contain more than one symbol table.
2185 Unusual, but possible. Warn, but continue. */
2186 if (elf_onesymtab (abfd
) != 0)
2189 /* xgettext:c-format */
2190 (_("%pB: warning: multiple symbol tables detected"
2191 " - ignoring the table in section %u"),
2195 elf_onesymtab (abfd
) = shindex
;
2196 elf_symtab_hdr (abfd
) = *hdr
;
2197 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2198 abfd
->flags
|= HAS_SYMS
;
2200 /* Sometimes a shared object will map in the symbol table. If
2201 SHF_ALLOC is set, and this is a shared object, then we also
2202 treat this section as a BFD section. We can not base the
2203 decision purely on SHF_ALLOC, because that flag is sometimes
2204 set in a relocatable object file, which would confuse the
2206 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2207 && (abfd
->flags
& DYNAMIC
) != 0
2208 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2212 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2213 can't read symbols without that section loaded as well. It
2214 is most likely specified by the next section header. */
2216 elf_section_list
* entry
;
2217 unsigned int i
, num_sec
;
2219 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2220 if (entry
->hdr
.sh_link
== shindex
)
2223 num_sec
= elf_numsections (abfd
);
2224 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2226 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2228 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2229 && hdr2
->sh_link
== shindex
)
2234 for (i
= 1; i
< shindex
; i
++)
2236 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2238 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2239 && hdr2
->sh_link
== shindex
)
2244 ret
= bfd_section_from_shdr (abfd
, i
);
2245 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2249 case SHT_DYNSYM
: /* A dynamic symbol table. */
2250 if (elf_dynsymtab (abfd
) == shindex
)
2253 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2256 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2258 if (hdr
->sh_size
!= 0)
2261 /* Some linkers erroneously set sh_info to one with a
2262 zero sh_size. ld sees this as a global symbol count
2263 of (unsigned) -1. Fix it here. */
2268 /* PR 18854: A binary might contain more than one dynamic symbol table.
2269 Unusual, but possible. Warn, but continue. */
2270 if (elf_dynsymtab (abfd
) != 0)
2273 /* xgettext:c-format */
2274 (_("%pB: warning: multiple dynamic symbol tables detected"
2275 " - ignoring the table in section %u"),
2279 elf_dynsymtab (abfd
) = shindex
;
2280 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2281 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2282 abfd
->flags
|= HAS_SYMS
;
2284 /* Besides being a symbol table, we also treat this as a regular
2285 section, so that objcopy can handle it. */
2286 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2289 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2291 elf_section_list
* entry
;
2293 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2294 if (entry
->ndx
== shindex
)
2297 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2300 entry
->ndx
= shindex
;
2302 entry
->next
= elf_symtab_shndx_list (abfd
);
2303 elf_symtab_shndx_list (abfd
) = entry
;
2304 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2308 case SHT_STRTAB
: /* A string table. */
2309 if (hdr
->bfd_section
!= NULL
)
2312 if (ehdr
->e_shstrndx
== shindex
)
2314 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2315 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2319 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2322 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2323 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2327 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2330 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2331 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2332 elf_elfsections (abfd
)[shindex
] = hdr
;
2333 /* We also treat this as a regular section, so that objcopy
2335 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2340 /* If the string table isn't one of the above, then treat it as a
2341 regular section. We need to scan all the headers to be sure,
2342 just in case this strtab section appeared before the above. */
2343 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2345 unsigned int i
, num_sec
;
2347 num_sec
= elf_numsections (abfd
);
2348 for (i
= 1; i
< num_sec
; i
++)
2350 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2351 if (hdr2
->sh_link
== shindex
)
2353 /* Prevent endless recursion on broken objects. */
2356 if (! bfd_section_from_shdr (abfd
, i
))
2358 if (elf_onesymtab (abfd
) == i
)
2360 if (elf_dynsymtab (abfd
) == i
)
2361 goto dynsymtab_strtab
;
2365 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2370 /* *These* do a lot of work -- but build no sections! */
2372 asection
*target_sect
;
2373 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2374 unsigned int num_sec
= elf_numsections (abfd
);
2375 struct bfd_elf_section_data
*esdt
;
2378 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2379 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2382 /* Check for a bogus link to avoid crashing. */
2383 if (hdr
->sh_link
>= num_sec
)
2386 /* xgettext:c-format */
2387 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2388 abfd
, hdr
->sh_link
, name
, shindex
);
2389 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2394 /* For some incomprehensible reason Oracle distributes
2395 libraries for Solaris in which some of the objects have
2396 bogus sh_link fields. It would be nice if we could just
2397 reject them, but, unfortunately, some people need to use
2398 them. We scan through the section headers; if we find only
2399 one suitable symbol table, we clobber the sh_link to point
2400 to it. I hope this doesn't break anything.
2402 Don't do it on executable nor shared library. */
2403 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2404 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2405 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2411 for (scan
= 1; scan
< num_sec
; scan
++)
2413 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2414 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2425 hdr
->sh_link
= found
;
2428 /* Get the symbol table. */
2429 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2430 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2431 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2434 /* If this is an alloc section in an executable or shared
2435 library, or the reloc section does not use the main symbol
2436 table we don't treat it as a reloc section. BFD can't
2437 adequately represent such a section, so at least for now,
2438 we don't try. We just present it as a normal section. We
2439 also can't use it as a reloc section if it points to the
2440 null section, an invalid section, another reloc section, or
2441 its sh_link points to the null section. */
2442 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2443 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2444 || hdr
->sh_link
== SHN_UNDEF
2445 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2446 || hdr
->sh_info
== SHN_UNDEF
2447 || hdr
->sh_info
>= num_sec
2448 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2449 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2451 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2456 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2459 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2460 if (target_sect
== NULL
)
2463 esdt
= elf_section_data (target_sect
);
2464 if (hdr
->sh_type
== SHT_RELA
)
2465 p_hdr
= &esdt
->rela
.hdr
;
2467 p_hdr
= &esdt
->rel
.hdr
;
2469 /* PR 17512: file: 0b4f81b7. */
2472 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2477 elf_elfsections (abfd
)[shindex
] = hdr2
;
2478 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2479 * bed
->s
->int_rels_per_ext_rel
);
2480 target_sect
->flags
|= SEC_RELOC
;
2481 target_sect
->relocation
= NULL
;
2482 target_sect
->rel_filepos
= hdr
->sh_offset
;
2483 /* In the section to which the relocations apply, mark whether
2484 its relocations are of the REL or RELA variety. */
2485 if (hdr
->sh_size
!= 0)
2487 if (hdr
->sh_type
== SHT_RELA
)
2488 target_sect
->use_rela_p
= 1;
2490 abfd
->flags
|= HAS_RELOC
;
2494 case SHT_GNU_verdef
:
2495 elf_dynverdef (abfd
) = shindex
;
2496 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2497 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2500 case SHT_GNU_versym
:
2501 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2504 elf_dynversym (abfd
) = shindex
;
2505 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2506 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2509 case SHT_GNU_verneed
:
2510 elf_dynverref (abfd
) = shindex
;
2511 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2512 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2519 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2522 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2528 /* Possibly an attributes section. */
2529 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2530 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2532 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2534 _bfd_elf_parse_attributes (abfd
, hdr
);
2538 /* Check for any processor-specific section types. */
2539 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2542 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2544 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2545 /* FIXME: How to properly handle allocated section reserved
2546 for applications? */
2548 /* xgettext:c-format */
2549 (_("%pB: unknown type [%#x] section `%s'"),
2550 abfd
, hdr
->sh_type
, name
);
2553 /* Allow sections reserved for applications. */
2554 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2559 else if (hdr
->sh_type
>= SHT_LOPROC
2560 && hdr
->sh_type
<= SHT_HIPROC
)
2561 /* FIXME: We should handle this section. */
2563 /* xgettext:c-format */
2564 (_("%pB: unknown type [%#x] section `%s'"),
2565 abfd
, hdr
->sh_type
, name
);
2566 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2568 /* Unrecognised OS-specific sections. */
2569 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2570 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2571 required to correctly process the section and the file should
2572 be rejected with an error message. */
2574 /* xgettext:c-format */
2575 (_("%pB: unknown type [%#x] section `%s'"),
2576 abfd
, hdr
->sh_type
, name
);
2579 /* Otherwise it should be processed. */
2580 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2585 /* FIXME: We should handle this section. */
2587 /* xgettext:c-format */
2588 (_("%pB: unknown type [%#x] section `%s'"),
2589 abfd
, hdr
->sh_type
, name
);
2597 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2598 sections_being_created
[shindex
] = FALSE
;
2599 if (-- nesting
== 0)
2601 sections_being_created
= NULL
;
2602 sections_being_created_abfd
= abfd
;
2607 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2610 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2612 unsigned long r_symndx
)
2614 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2616 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2618 Elf_Internal_Shdr
*symtab_hdr
;
2619 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2620 Elf_External_Sym_Shndx eshndx
;
2622 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2623 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2624 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2627 if (cache
->abfd
!= abfd
)
2629 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2632 cache
->indx
[ent
] = r_symndx
;
2635 return &cache
->sym
[ent
];
2638 /* Given an ELF section number, retrieve the corresponding BFD
2642 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2644 if (sec_index
>= elf_numsections (abfd
))
2646 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2649 static const struct bfd_elf_special_section special_sections_b
[] =
2651 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2652 { NULL
, 0, 0, 0, 0 }
2655 static const struct bfd_elf_special_section special_sections_c
[] =
2657 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2658 { NULL
, 0, 0, 0, 0 }
2661 static const struct bfd_elf_special_section special_sections_d
[] =
2663 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2664 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2665 /* There are more DWARF sections than these, but they needn't be added here
2666 unless you have to cope with broken compilers that don't emit section
2667 attributes or you want to help the user writing assembler. */
2668 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2669 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2670 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2671 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2672 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2673 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2674 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2675 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2676 { NULL
, 0, 0, 0, 0 }
2679 static const struct bfd_elf_special_section special_sections_f
[] =
2681 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2682 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2683 { NULL
, 0 , 0, 0, 0 }
2686 static const struct bfd_elf_special_section special_sections_g
[] =
2688 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2689 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2690 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2691 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2692 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2693 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2694 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2695 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2696 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2697 { NULL
, 0, 0, 0, 0 }
2700 static const struct bfd_elf_special_section special_sections_h
[] =
2702 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2703 { NULL
, 0, 0, 0, 0 }
2706 static const struct bfd_elf_special_section special_sections_i
[] =
2708 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2709 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2710 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2711 { NULL
, 0, 0, 0, 0 }
2714 static const struct bfd_elf_special_section special_sections_l
[] =
2716 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2717 { NULL
, 0, 0, 0, 0 }
2720 static const struct bfd_elf_special_section special_sections_n
[] =
2722 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2723 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2724 { NULL
, 0, 0, 0, 0 }
2727 static const struct bfd_elf_special_section special_sections_p
[] =
2729 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2730 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2731 { NULL
, 0, 0, 0, 0 }
2734 static const struct bfd_elf_special_section special_sections_r
[] =
2736 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2737 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2738 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2739 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2740 { NULL
, 0, 0, 0, 0 }
2743 static const struct bfd_elf_special_section special_sections_s
[] =
2745 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2746 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2747 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2748 /* See struct bfd_elf_special_section declaration for the semantics of
2749 this special case where .prefix_length != strlen (.prefix). */
2750 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2751 { NULL
, 0, 0, 0, 0 }
2754 static const struct bfd_elf_special_section special_sections_t
[] =
2756 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2757 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2758 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2759 { NULL
, 0, 0, 0, 0 }
2762 static const struct bfd_elf_special_section special_sections_z
[] =
2764 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2765 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2766 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2767 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2768 { NULL
, 0, 0, 0, 0 }
2771 static const struct bfd_elf_special_section
* const special_sections
[] =
2773 special_sections_b
, /* 'b' */
2774 special_sections_c
, /* 'c' */
2775 special_sections_d
, /* 'd' */
2777 special_sections_f
, /* 'f' */
2778 special_sections_g
, /* 'g' */
2779 special_sections_h
, /* 'h' */
2780 special_sections_i
, /* 'i' */
2783 special_sections_l
, /* 'l' */
2785 special_sections_n
, /* 'n' */
2787 special_sections_p
, /* 'p' */
2789 special_sections_r
, /* 'r' */
2790 special_sections_s
, /* 's' */
2791 special_sections_t
, /* 't' */
2797 special_sections_z
/* 'z' */
2800 const struct bfd_elf_special_section
*
2801 _bfd_elf_get_special_section (const char *name
,
2802 const struct bfd_elf_special_section
*spec
,
2808 len
= strlen (name
);
2810 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2813 int prefix_len
= spec
[i
].prefix_length
;
2815 if (len
< prefix_len
)
2817 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2820 suffix_len
= spec
[i
].suffix_length
;
2821 if (suffix_len
<= 0)
2823 if (name
[prefix_len
] != 0)
2825 if (suffix_len
== 0)
2827 if (name
[prefix_len
] != '.'
2828 && (suffix_len
== -2
2829 || (rela
&& spec
[i
].type
== SHT_REL
)))
2835 if (len
< prefix_len
+ suffix_len
)
2837 if (memcmp (name
+ len
- suffix_len
,
2838 spec
[i
].prefix
+ prefix_len
,
2848 const struct bfd_elf_special_section
*
2849 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2852 const struct bfd_elf_special_section
*spec
;
2853 const struct elf_backend_data
*bed
;
2855 /* See if this is one of the special sections. */
2856 if (sec
->name
== NULL
)
2859 bed
= get_elf_backend_data (abfd
);
2860 spec
= bed
->special_sections
;
2863 spec
= _bfd_elf_get_special_section (sec
->name
,
2864 bed
->special_sections
,
2870 if (sec
->name
[0] != '.')
2873 i
= sec
->name
[1] - 'b';
2874 if (i
< 0 || i
> 'z' - 'b')
2877 spec
= special_sections
[i
];
2882 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2886 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2888 struct bfd_elf_section_data
*sdata
;
2889 const struct elf_backend_data
*bed
;
2890 const struct bfd_elf_special_section
*ssect
;
2892 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2895 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2899 sec
->used_by_bfd
= sdata
;
2902 /* Indicate whether or not this section should use RELA relocations. */
2903 bed
= get_elf_backend_data (abfd
);
2904 sec
->use_rela_p
= bed
->default_use_rela_p
;
2906 /* When we read a file, we don't need to set ELF section type and
2907 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2908 anyway. We will set ELF section type and flags for all linker
2909 created sections. If user specifies BFD section flags, we will
2910 set ELF section type and flags based on BFD section flags in
2911 elf_fake_sections. Special handling for .init_array/.fini_array
2912 output sections since they may contain .ctors/.dtors input
2913 sections. We don't want _bfd_elf_init_private_section_data to
2914 copy ELF section type from .ctors/.dtors input sections. */
2915 if (abfd
->direction
!= read_direction
2916 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2918 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2921 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2922 || ssect
->type
== SHT_INIT_ARRAY
2923 || ssect
->type
== SHT_FINI_ARRAY
))
2925 elf_section_type (sec
) = ssect
->type
;
2926 elf_section_flags (sec
) = ssect
->attr
;
2930 return _bfd_generic_new_section_hook (abfd
, sec
);
2933 /* Create a new bfd section from an ELF program header.
2935 Since program segments have no names, we generate a synthetic name
2936 of the form segment<NUM>, where NUM is generally the index in the
2937 program header table. For segments that are split (see below) we
2938 generate the names segment<NUM>a and segment<NUM>b.
2940 Note that some program segments may have a file size that is different than
2941 (less than) the memory size. All this means is that at execution the
2942 system must allocate the amount of memory specified by the memory size,
2943 but only initialize it with the first "file size" bytes read from the
2944 file. This would occur for example, with program segments consisting
2945 of combined data+bss.
2947 To handle the above situation, this routine generates TWO bfd sections
2948 for the single program segment. The first has the length specified by
2949 the file size of the segment, and the second has the length specified
2950 by the difference between the two sizes. In effect, the segment is split
2951 into its initialized and uninitialized parts.
2956 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2957 Elf_Internal_Phdr
*hdr
,
2959 const char *type_name
)
2967 split
= ((hdr
->p_memsz
> 0)
2968 && (hdr
->p_filesz
> 0)
2969 && (hdr
->p_memsz
> hdr
->p_filesz
));
2971 if (hdr
->p_filesz
> 0)
2973 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2974 len
= strlen (namebuf
) + 1;
2975 name
= (char *) bfd_alloc (abfd
, len
);
2978 memcpy (name
, namebuf
, len
);
2979 newsect
= bfd_make_section (abfd
, name
);
2980 if (newsect
== NULL
)
2982 newsect
->vma
= hdr
->p_vaddr
;
2983 newsect
->lma
= hdr
->p_paddr
;
2984 newsect
->size
= hdr
->p_filesz
;
2985 newsect
->filepos
= hdr
->p_offset
;
2986 newsect
->flags
|= SEC_HAS_CONTENTS
;
2987 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2988 if (hdr
->p_type
== PT_LOAD
)
2990 newsect
->flags
|= SEC_ALLOC
;
2991 newsect
->flags
|= SEC_LOAD
;
2992 if (hdr
->p_flags
& PF_X
)
2994 /* FIXME: all we known is that it has execute PERMISSION,
2996 newsect
->flags
|= SEC_CODE
;
2999 if (!(hdr
->p_flags
& PF_W
))
3001 newsect
->flags
|= SEC_READONLY
;
3005 if (hdr
->p_memsz
> hdr
->p_filesz
)
3009 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3010 len
= strlen (namebuf
) + 1;
3011 name
= (char *) bfd_alloc (abfd
, len
);
3014 memcpy (name
, namebuf
, len
);
3015 newsect
= bfd_make_section (abfd
, name
);
3016 if (newsect
== NULL
)
3018 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
3019 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
3020 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3021 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3022 align
= newsect
->vma
& -newsect
->vma
;
3023 if (align
== 0 || align
> hdr
->p_align
)
3024 align
= hdr
->p_align
;
3025 newsect
->alignment_power
= bfd_log2 (align
);
3026 if (hdr
->p_type
== PT_LOAD
)
3028 /* Hack for gdb. Segments that have not been modified do
3029 not have their contents written to a core file, on the
3030 assumption that a debugger can find the contents in the
3031 executable. We flag this case by setting the fake
3032 section size to zero. Note that "real" bss sections will
3033 always have their contents dumped to the core file. */
3034 if (bfd_get_format (abfd
) == bfd_core
)
3036 newsect
->flags
|= SEC_ALLOC
;
3037 if (hdr
->p_flags
& PF_X
)
3038 newsect
->flags
|= SEC_CODE
;
3040 if (!(hdr
->p_flags
& PF_W
))
3041 newsect
->flags
|= SEC_READONLY
;
3048 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3050 const struct elf_backend_data
*bed
;
3052 switch (hdr
->p_type
)
3055 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3058 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
3061 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3064 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3067 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3069 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3075 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3078 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3080 case PT_GNU_EH_FRAME
:
3081 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3085 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3088 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3091 /* Check for any processor-specific program segment types. */
3092 bed
= get_elf_backend_data (abfd
);
3093 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3097 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3101 _bfd_elf_single_rel_hdr (asection
*sec
)
3103 if (elf_section_data (sec
)->rel
.hdr
)
3105 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3106 return elf_section_data (sec
)->rel
.hdr
;
3109 return elf_section_data (sec
)->rela
.hdr
;
3113 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3114 Elf_Internal_Shdr
*rel_hdr
,
3115 const char *sec_name
,
3116 bfd_boolean use_rela_p
)
3118 char *name
= (char *) bfd_alloc (abfd
,
3119 sizeof ".rela" + strlen (sec_name
));
3123 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3125 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3127 if (rel_hdr
->sh_name
== (unsigned int) -1)
3133 /* Allocate and initialize a section-header for a new reloc section,
3134 containing relocations against ASECT. It is stored in RELDATA. If
3135 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3139 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3140 struct bfd_elf_section_reloc_data
*reldata
,
3141 const char *sec_name
,
3142 bfd_boolean use_rela_p
,
3143 bfd_boolean delay_st_name_p
)
3145 Elf_Internal_Shdr
*rel_hdr
;
3146 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3148 BFD_ASSERT (reldata
->hdr
== NULL
);
3149 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3150 reldata
->hdr
= rel_hdr
;
3152 if (delay_st_name_p
)
3153 rel_hdr
->sh_name
= (unsigned int) -1;
3154 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3157 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3158 rel_hdr
->sh_entsize
= (use_rela_p
3159 ? bed
->s
->sizeof_rela
3160 : bed
->s
->sizeof_rel
);
3161 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3162 rel_hdr
->sh_flags
= 0;
3163 rel_hdr
->sh_addr
= 0;
3164 rel_hdr
->sh_size
= 0;
3165 rel_hdr
->sh_offset
= 0;
3170 /* Return the default section type based on the passed in section flags. */
3173 bfd_elf_get_default_section_type (flagword flags
)
3175 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3176 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3178 return SHT_PROGBITS
;
3181 struct fake_section_arg
3183 struct bfd_link_info
*link_info
;
3187 /* Set up an ELF internal section header for a section. */
3190 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3192 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3193 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3194 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3195 Elf_Internal_Shdr
*this_hdr
;
3196 unsigned int sh_type
;
3197 const char *name
= asect
->name
;
3198 bfd_boolean delay_st_name_p
= FALSE
;
3202 /* We already failed; just get out of the bfd_map_over_sections
3207 this_hdr
= &esd
->this_hdr
;
3211 /* ld: compress DWARF debug sections with names: .debug_*. */
3212 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3213 && (asect
->flags
& SEC_DEBUGGING
)
3217 /* Set SEC_ELF_COMPRESS to indicate this section should be
3219 asect
->flags
|= SEC_ELF_COMPRESS
;
3221 /* If this section will be compressed, delay adding section
3222 name to section name section after it is compressed in
3223 _bfd_elf_assign_file_positions_for_non_load. */
3224 delay_st_name_p
= TRUE
;
3227 else if ((asect
->flags
& SEC_ELF_RENAME
))
3229 /* objcopy: rename output DWARF debug section. */
3230 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3232 /* When we decompress or compress with SHF_COMPRESSED,
3233 convert section name from .zdebug_* to .debug_* if
3237 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3238 if (new_name
== NULL
)
3246 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3248 /* PR binutils/18087: Compression does not always make a
3249 section smaller. So only rename the section when
3250 compression has actually taken place. If input section
3251 name is .zdebug_*, we should never compress it again. */
3252 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3253 if (new_name
== NULL
)
3258 BFD_ASSERT (name
[1] != 'z');
3263 if (delay_st_name_p
)
3264 this_hdr
->sh_name
= (unsigned int) -1;
3268 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3270 if (this_hdr
->sh_name
== (unsigned int) -1)
3277 /* Don't clear sh_flags. Assembler may set additional bits. */
3279 if ((asect
->flags
& SEC_ALLOC
) != 0
3280 || asect
->user_set_vma
)
3281 this_hdr
->sh_addr
= asect
->vma
;
3283 this_hdr
->sh_addr
= 0;
3285 this_hdr
->sh_offset
= 0;
3286 this_hdr
->sh_size
= asect
->size
;
3287 this_hdr
->sh_link
= 0;
3288 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3289 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3292 /* xgettext:c-format */
3293 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3294 abfd
, asect
->alignment_power
, asect
);
3298 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3299 /* The sh_entsize and sh_info fields may have been set already by
3300 copy_private_section_data. */
3302 this_hdr
->bfd_section
= asect
;
3303 this_hdr
->contents
= NULL
;
3305 /* If the section type is unspecified, we set it based on
3307 if ((asect
->flags
& SEC_GROUP
) != 0)
3308 sh_type
= SHT_GROUP
;
3310 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3312 if (this_hdr
->sh_type
== SHT_NULL
)
3313 this_hdr
->sh_type
= sh_type
;
3314 else if (this_hdr
->sh_type
== SHT_NOBITS
3315 && sh_type
== SHT_PROGBITS
3316 && (asect
->flags
& SEC_ALLOC
) != 0)
3318 /* Warn if we are changing a NOBITS section to PROGBITS, but
3319 allow the link to proceed. This can happen when users link
3320 non-bss input sections to bss output sections, or emit data
3321 to a bss output section via a linker script. */
3323 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3324 this_hdr
->sh_type
= sh_type
;
3327 switch (this_hdr
->sh_type
)
3338 case SHT_INIT_ARRAY
:
3339 case SHT_FINI_ARRAY
:
3340 case SHT_PREINIT_ARRAY
:
3341 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3345 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3349 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3353 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3357 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3358 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3362 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3363 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3366 case SHT_GNU_versym
:
3367 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3370 case SHT_GNU_verdef
:
3371 this_hdr
->sh_entsize
= 0;
3372 /* objcopy or strip will copy over sh_info, but may not set
3373 cverdefs. The linker will set cverdefs, but sh_info will be
3375 if (this_hdr
->sh_info
== 0)
3376 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3378 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3379 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3382 case SHT_GNU_verneed
:
3383 this_hdr
->sh_entsize
= 0;
3384 /* objcopy or strip will copy over sh_info, but may not set
3385 cverrefs. The linker will set cverrefs, but sh_info will be
3387 if (this_hdr
->sh_info
== 0)
3388 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3390 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3391 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3395 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3399 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3403 if ((asect
->flags
& SEC_ALLOC
) != 0)
3404 this_hdr
->sh_flags
|= SHF_ALLOC
;
3405 if ((asect
->flags
& SEC_READONLY
) == 0)
3406 this_hdr
->sh_flags
|= SHF_WRITE
;
3407 if ((asect
->flags
& SEC_CODE
) != 0)
3408 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3409 if ((asect
->flags
& SEC_MERGE
) != 0)
3411 this_hdr
->sh_flags
|= SHF_MERGE
;
3412 this_hdr
->sh_entsize
= asect
->entsize
;
3414 if ((asect
->flags
& SEC_STRINGS
) != 0)
3415 this_hdr
->sh_flags
|= SHF_STRINGS
;
3416 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3417 this_hdr
->sh_flags
|= SHF_GROUP
;
3418 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3420 this_hdr
->sh_flags
|= SHF_TLS
;
3421 if (asect
->size
== 0
3422 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3424 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3426 this_hdr
->sh_size
= 0;
3429 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3430 if (this_hdr
->sh_size
!= 0)
3431 this_hdr
->sh_type
= SHT_NOBITS
;
3435 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3436 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3438 /* If the section has relocs, set up a section header for the
3439 SHT_REL[A] section. If two relocation sections are required for
3440 this section, it is up to the processor-specific back-end to
3441 create the other. */
3442 if ((asect
->flags
& SEC_RELOC
) != 0)
3444 /* When doing a relocatable link, create both REL and RELA sections if
3447 /* Do the normal setup if we wouldn't create any sections here. */
3448 && esd
->rel
.count
+ esd
->rela
.count
> 0
3449 && (bfd_link_relocatable (arg
->link_info
)
3450 || arg
->link_info
->emitrelocations
))
3452 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3453 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3454 FALSE
, delay_st_name_p
))
3459 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3460 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3461 TRUE
, delay_st_name_p
))
3467 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3469 ? &esd
->rela
: &esd
->rel
),
3479 /* Check for processor-specific section types. */
3480 sh_type
= this_hdr
->sh_type
;
3481 if (bed
->elf_backend_fake_sections
3482 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3488 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3490 /* Don't change the header type from NOBITS if we are being
3491 called for objcopy --only-keep-debug. */
3492 this_hdr
->sh_type
= sh_type
;
3496 /* Fill in the contents of a SHT_GROUP section. Called from
3497 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3498 when ELF targets use the generic linker, ld. Called for ld -r
3499 from bfd_elf_final_link. */
3502 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3504 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3505 asection
*elt
, *first
;
3509 /* Ignore linker created group section. See elfNN_ia64_object_p in
3511 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3516 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3518 unsigned long symindx
= 0;
3520 /* elf_group_id will have been set up by objcopy and the
3522 if (elf_group_id (sec
) != NULL
)
3523 symindx
= elf_group_id (sec
)->udata
.i
;
3527 /* If called from the assembler, swap_out_syms will have set up
3528 elf_section_syms. */
3529 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3530 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3532 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3534 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3536 /* The ELF backend linker sets sh_info to -2 when the group
3537 signature symbol is global, and thus the index can't be
3538 set until all local symbols are output. */
3540 struct bfd_elf_section_data
*sec_data
;
3541 unsigned long symndx
;
3542 unsigned long extsymoff
;
3543 struct elf_link_hash_entry
*h
;
3545 /* The point of this little dance to the first SHF_GROUP section
3546 then back to the SHT_GROUP section is that this gets us to
3547 the SHT_GROUP in the input object. */
3548 igroup
= elf_sec_group (elf_next_in_group (sec
));
3549 sec_data
= elf_section_data (igroup
);
3550 symndx
= sec_data
->this_hdr
.sh_info
;
3552 if (!elf_bad_symtab (igroup
->owner
))
3554 Elf_Internal_Shdr
*symtab_hdr
;
3556 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3557 extsymoff
= symtab_hdr
->sh_info
;
3559 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3560 while (h
->root
.type
== bfd_link_hash_indirect
3561 || h
->root
.type
== bfd_link_hash_warning
)
3562 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3564 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3567 /* The contents won't be allocated for "ld -r" or objcopy. */
3569 if (sec
->contents
== NULL
)
3572 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3574 /* Arrange for the section to be written out. */
3575 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3576 if (sec
->contents
== NULL
)
3583 loc
= sec
->contents
+ sec
->size
;
3585 /* Get the pointer to the first section in the group that gas
3586 squirreled away here. objcopy arranges for this to be set to the
3587 start of the input section group. */
3588 first
= elt
= elf_next_in_group (sec
);
3590 /* First element is a flag word. Rest of section is elf section
3591 indices for all the sections of the group. Write them backwards
3592 just to keep the group in the same order as given in .section
3593 directives, not that it matters. */
3600 s
= s
->output_section
;
3602 && !bfd_is_abs_section (s
))
3604 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3605 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3607 if (elf_sec
->rel
.hdr
!= NULL
3609 || (input_elf_sec
->rel
.hdr
!= NULL
3610 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3612 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3614 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3616 if (elf_sec
->rela
.hdr
!= NULL
3618 || (input_elf_sec
->rela
.hdr
!= NULL
3619 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3621 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3623 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3626 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3628 elt
= elf_next_in_group (elt
);
3634 BFD_ASSERT (loc
== sec
->contents
);
3636 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3639 /* Given NAME, the name of a relocation section stripped of its
3640 .rel/.rela prefix, return the section in ABFD to which the
3641 relocations apply. */
3644 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3646 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3647 section likely apply to .got.plt or .got section. */
3648 if (get_elf_backend_data (abfd
)->want_got_plt
3649 && strcmp (name
, ".plt") == 0)
3654 sec
= bfd_get_section_by_name (abfd
, name
);
3660 return bfd_get_section_by_name (abfd
, name
);
3663 /* Return the section to which RELOC_SEC applies. */
3666 elf_get_reloc_section (asection
*reloc_sec
)
3671 const struct elf_backend_data
*bed
;
3673 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3674 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3677 /* We look up the section the relocs apply to by name. */
3678 name
= reloc_sec
->name
;
3679 if (strncmp (name
, ".rel", 4) != 0)
3682 if (type
== SHT_RELA
&& *name
++ != 'a')
3685 abfd
= reloc_sec
->owner
;
3686 bed
= get_elf_backend_data (abfd
);
3687 return bed
->get_reloc_section (abfd
, name
);
3690 /* Assign all ELF section numbers. The dummy first section is handled here
3691 too. The link/info pointers for the standard section types are filled
3692 in here too, while we're at it. */
3695 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3697 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3699 unsigned int section_number
;
3700 Elf_Internal_Shdr
**i_shdrp
;
3701 struct bfd_elf_section_data
*d
;
3702 bfd_boolean need_symtab
;
3706 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3708 /* SHT_GROUP sections are in relocatable files only. */
3709 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3711 size_t reloc_count
= 0;
3713 /* Put SHT_GROUP sections first. */
3714 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3716 d
= elf_section_data (sec
);
3718 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3720 if (sec
->flags
& SEC_LINKER_CREATED
)
3722 /* Remove the linker created SHT_GROUP sections. */
3723 bfd_section_list_remove (abfd
, sec
);
3724 abfd
->section_count
--;
3727 d
->this_idx
= section_number
++;
3730 /* Count relocations. */
3731 reloc_count
+= sec
->reloc_count
;
3734 /* Clear HAS_RELOC if there are no relocations. */
3735 if (reloc_count
== 0)
3736 abfd
->flags
&= ~HAS_RELOC
;
3739 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3741 d
= elf_section_data (sec
);
3743 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3744 d
->this_idx
= section_number
++;
3745 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3746 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3749 d
->rel
.idx
= section_number
++;
3750 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3751 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3758 d
->rela
.idx
= section_number
++;
3759 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3760 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3766 need_symtab
= (bfd_get_symcount (abfd
) > 0
3767 || (link_info
== NULL
3768 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3772 elf_onesymtab (abfd
) = section_number
++;
3773 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3774 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3776 elf_section_list
*entry
;
3778 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3780 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3781 entry
->ndx
= section_number
++;
3782 elf_symtab_shndx_list (abfd
) = entry
;
3784 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3785 ".symtab_shndx", FALSE
);
3786 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3789 elf_strtab_sec (abfd
) = section_number
++;
3790 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3793 elf_shstrtab_sec (abfd
) = section_number
++;
3794 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3795 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3797 if (section_number
>= SHN_LORESERVE
)
3799 /* xgettext:c-format */
3800 _bfd_error_handler (_("%pB: too many sections: %u"),
3801 abfd
, section_number
);
3805 elf_numsections (abfd
) = section_number
;
3806 elf_elfheader (abfd
)->e_shnum
= section_number
;
3808 /* Set up the list of section header pointers, in agreement with the
3810 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3811 sizeof (Elf_Internal_Shdr
*));
3812 if (i_shdrp
== NULL
)
3815 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3816 sizeof (Elf_Internal_Shdr
));
3817 if (i_shdrp
[0] == NULL
)
3819 bfd_release (abfd
, i_shdrp
);
3823 elf_elfsections (abfd
) = i_shdrp
;
3825 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3828 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3829 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3831 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3832 BFD_ASSERT (entry
!= NULL
);
3833 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3834 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3836 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3837 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3840 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3844 d
= elf_section_data (sec
);
3846 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3847 if (d
->rel
.idx
!= 0)
3848 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3849 if (d
->rela
.idx
!= 0)
3850 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3852 /* Fill in the sh_link and sh_info fields while we're at it. */
3854 /* sh_link of a reloc section is the section index of the symbol
3855 table. sh_info is the section index of the section to which
3856 the relocation entries apply. */
3857 if (d
->rel
.idx
!= 0)
3859 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3860 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3861 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3863 if (d
->rela
.idx
!= 0)
3865 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3866 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3867 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3870 /* We need to set up sh_link for SHF_LINK_ORDER. */
3871 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3873 s
= elf_linked_to_section (sec
);
3876 /* elf_linked_to_section points to the input section. */
3877 if (link_info
!= NULL
)
3879 /* Check discarded linkonce section. */
3880 if (discarded_section (s
))
3884 /* xgettext:c-format */
3885 (_("%pB: sh_link of section `%pA' points to"
3886 " discarded section `%pA' of `%pB'"),
3887 abfd
, d
->this_hdr
.bfd_section
,
3889 /* Point to the kept section if it has the same
3890 size as the discarded one. */
3891 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3894 bfd_set_error (bfd_error_bad_value
);
3900 s
= s
->output_section
;
3901 BFD_ASSERT (s
!= NULL
);
3905 /* Handle objcopy. */
3906 if (s
->output_section
== NULL
)
3909 /* xgettext:c-format */
3910 (_("%pB: sh_link of section `%pA' points to"
3911 " removed section `%pA' of `%pB'"),
3912 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3913 bfd_set_error (bfd_error_bad_value
);
3916 s
= s
->output_section
;
3918 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3923 The Intel C compiler generates SHT_IA_64_UNWIND with
3924 SHF_LINK_ORDER. But it doesn't set the sh_link or
3925 sh_info fields. Hence we could get the situation
3927 const struct elf_backend_data
*bed
3928 = get_elf_backend_data (abfd
);
3929 if (bed
->link_order_error_handler
)
3930 bed
->link_order_error_handler
3931 /* xgettext:c-format */
3932 (_("%pB: warning: sh_link not set for section `%pA'"),
3937 switch (d
->this_hdr
.sh_type
)
3941 /* A reloc section which we are treating as a normal BFD
3942 section. sh_link is the section index of the symbol
3943 table. sh_info is the section index of the section to
3944 which the relocation entries apply. We assume that an
3945 allocated reloc section uses the dynamic symbol table.
3946 FIXME: How can we be sure? */
3947 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3949 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3951 s
= elf_get_reloc_section (sec
);
3954 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3955 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3960 /* We assume that a section named .stab*str is a stabs
3961 string section. We look for a section with the same name
3962 but without the trailing ``str'', and set its sh_link
3963 field to point to this section. */
3964 if (CONST_STRNEQ (sec
->name
, ".stab")
3965 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3970 len
= strlen (sec
->name
);
3971 alc
= (char *) bfd_malloc (len
- 2);
3974 memcpy (alc
, sec
->name
, len
- 3);
3975 alc
[len
- 3] = '\0';
3976 s
= bfd_get_section_by_name (abfd
, alc
);
3980 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3982 /* This is a .stab section. */
3983 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3984 elf_section_data (s
)->this_hdr
.sh_entsize
3985 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3992 case SHT_GNU_verneed
:
3993 case SHT_GNU_verdef
:
3994 /* sh_link is the section header index of the string table
3995 used for the dynamic entries, or the symbol table, or the
3997 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3999 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4002 case SHT_GNU_LIBLIST
:
4003 /* sh_link is the section header index of the prelink library
4004 list used for the dynamic entries, or the symbol table, or
4005 the version strings. */
4006 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4007 ? ".dynstr" : ".gnu.libstr");
4009 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4014 case SHT_GNU_versym
:
4015 /* sh_link is the section header index of the symbol table
4016 this hash table or version table is for. */
4017 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4019 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4023 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4027 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4028 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4029 debug section name from .debug_* to .zdebug_* if needed. */
4035 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4037 /* If the backend has a special mapping, use it. */
4038 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4039 if (bed
->elf_backend_sym_is_global
)
4040 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4042 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4043 || bfd_is_und_section (bfd_get_section (sym
))
4044 || bfd_is_com_section (bfd_get_section (sym
)));
4047 /* Filter global symbols of ABFD to include in the import library. All
4048 SYMCOUNT symbols of ABFD can be examined from their pointers in
4049 SYMS. Pointers of symbols to keep should be stored contiguously at
4050 the beginning of that array.
4052 Returns the number of symbols to keep. */
4055 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4056 asymbol
**syms
, long symcount
)
4058 long src_count
, dst_count
= 0;
4060 for (src_count
= 0; src_count
< symcount
; src_count
++)
4062 asymbol
*sym
= syms
[src_count
];
4063 char *name
= (char *) bfd_asymbol_name (sym
);
4064 struct bfd_link_hash_entry
*h
;
4066 if (!sym_is_global (abfd
, sym
))
4069 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4072 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4074 if (h
->linker_def
|| h
->ldscript_def
)
4077 syms
[dst_count
++] = sym
;
4080 syms
[dst_count
] = NULL
;
4085 /* Don't output section symbols for sections that are not going to be
4086 output, that are duplicates or there is no BFD section. */
4089 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4091 elf_symbol_type
*type_ptr
;
4096 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4099 if (sym
->section
== NULL
)
4102 type_ptr
= elf_symbol_from (abfd
, sym
);
4103 return ((type_ptr
!= NULL
4104 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4105 && bfd_is_abs_section (sym
->section
))
4106 || !(sym
->section
->owner
== abfd
4107 || (sym
->section
->output_section
!= NULL
4108 && sym
->section
->output_section
->owner
== abfd
4109 && sym
->section
->output_offset
== 0)
4110 || bfd_is_abs_section (sym
->section
)));
4113 /* Map symbol from it's internal number to the external number, moving
4114 all local symbols to be at the head of the list. */
4117 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4119 unsigned int symcount
= bfd_get_symcount (abfd
);
4120 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4121 asymbol
**sect_syms
;
4122 unsigned int num_locals
= 0;
4123 unsigned int num_globals
= 0;
4124 unsigned int num_locals2
= 0;
4125 unsigned int num_globals2
= 0;
4126 unsigned int max_index
= 0;
4132 fprintf (stderr
, "elf_map_symbols\n");
4136 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4138 if (max_index
< asect
->index
)
4139 max_index
= asect
->index
;
4143 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
4144 if (sect_syms
== NULL
)
4146 elf_section_syms (abfd
) = sect_syms
;
4147 elf_num_section_syms (abfd
) = max_index
;
4149 /* Init sect_syms entries for any section symbols we have already
4150 decided to output. */
4151 for (idx
= 0; idx
< symcount
; idx
++)
4153 asymbol
*sym
= syms
[idx
];
4155 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4157 && !ignore_section_sym (abfd
, sym
)
4158 && !bfd_is_abs_section (sym
->section
))
4160 asection
*sec
= sym
->section
;
4162 if (sec
->owner
!= abfd
)
4163 sec
= sec
->output_section
;
4165 sect_syms
[sec
->index
] = syms
[idx
];
4169 /* Classify all of the symbols. */
4170 for (idx
= 0; idx
< symcount
; idx
++)
4172 if (sym_is_global (abfd
, syms
[idx
]))
4174 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4178 /* We will be adding a section symbol for each normal BFD section. Most
4179 sections will already have a section symbol in outsymbols, but
4180 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4181 at least in that case. */
4182 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4184 if (sect_syms
[asect
->index
] == NULL
)
4186 if (!sym_is_global (abfd
, asect
->symbol
))
4193 /* Now sort the symbols so the local symbols are first. */
4194 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4195 sizeof (asymbol
*));
4197 if (new_syms
== NULL
)
4200 for (idx
= 0; idx
< symcount
; idx
++)
4202 asymbol
*sym
= syms
[idx
];
4205 if (sym_is_global (abfd
, sym
))
4206 i
= num_locals
+ num_globals2
++;
4207 else if (!ignore_section_sym (abfd
, sym
))
4212 sym
->udata
.i
= i
+ 1;
4214 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4216 if (sect_syms
[asect
->index
] == NULL
)
4218 asymbol
*sym
= asect
->symbol
;
4221 sect_syms
[asect
->index
] = sym
;
4222 if (!sym_is_global (abfd
, sym
))
4225 i
= num_locals
+ num_globals2
++;
4227 sym
->udata
.i
= i
+ 1;
4231 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4233 *pnum_locals
= num_locals
;
4237 /* Align to the maximum file alignment that could be required for any
4238 ELF data structure. */
4240 static inline file_ptr
4241 align_file_position (file_ptr off
, int align
)
4243 return (off
+ align
- 1) & ~(align
- 1);
4246 /* Assign a file position to a section, optionally aligning to the
4247 required section alignment. */
4250 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4254 if (align
&& i_shdrp
->sh_addralign
> 1)
4255 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4256 i_shdrp
->sh_offset
= offset
;
4257 if (i_shdrp
->bfd_section
!= NULL
)
4258 i_shdrp
->bfd_section
->filepos
= offset
;
4259 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4260 offset
+= i_shdrp
->sh_size
;
4264 /* Compute the file positions we are going to put the sections at, and
4265 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4266 is not NULL, this is being called by the ELF backend linker. */
4269 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4270 struct bfd_link_info
*link_info
)
4272 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4273 struct fake_section_arg fsargs
;
4275 struct elf_strtab_hash
*strtab
= NULL
;
4276 Elf_Internal_Shdr
*shstrtab_hdr
;
4277 bfd_boolean need_symtab
;
4279 if (abfd
->output_has_begun
)
4282 /* Do any elf backend specific processing first. */
4283 if (bed
->elf_backend_begin_write_processing
)
4284 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4286 if (! prep_headers (abfd
))
4289 /* Post process the headers if necessary. */
4290 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4292 fsargs
.failed
= FALSE
;
4293 fsargs
.link_info
= link_info
;
4294 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4298 if (!assign_section_numbers (abfd
, link_info
))
4301 /* The backend linker builds symbol table information itself. */
4302 need_symtab
= (link_info
== NULL
4303 && (bfd_get_symcount (abfd
) > 0
4304 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4308 /* Non-zero if doing a relocatable link. */
4309 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4311 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4316 if (link_info
== NULL
)
4318 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4323 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4324 /* sh_name was set in prep_headers. */
4325 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4326 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4327 shstrtab_hdr
->sh_addr
= 0;
4328 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4329 shstrtab_hdr
->sh_entsize
= 0;
4330 shstrtab_hdr
->sh_link
= 0;
4331 shstrtab_hdr
->sh_info
= 0;
4332 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4333 shstrtab_hdr
->sh_addralign
= 1;
4335 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4341 Elf_Internal_Shdr
*hdr
;
4343 off
= elf_next_file_pos (abfd
);
4345 hdr
= & elf_symtab_hdr (abfd
);
4346 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4348 if (elf_symtab_shndx_list (abfd
) != NULL
)
4350 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4351 if (hdr
->sh_size
!= 0)
4352 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4353 /* FIXME: What about other symtab_shndx sections in the list ? */
4356 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4357 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4359 elf_next_file_pos (abfd
) = off
;
4361 /* Now that we know where the .strtab section goes, write it
4363 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4364 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4366 _bfd_elf_strtab_free (strtab
);
4369 abfd
->output_has_begun
= TRUE
;
4374 /* Make an initial estimate of the size of the program header. If we
4375 get the number wrong here, we'll redo section placement. */
4377 static bfd_size_type
4378 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4382 const struct elf_backend_data
*bed
;
4384 /* Assume we will need exactly two PT_LOAD segments: one for text
4385 and one for data. */
4388 s
= bfd_get_section_by_name (abfd
, ".interp");
4389 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4391 /* If we have a loadable interpreter section, we need a
4392 PT_INTERP segment. In this case, assume we also need a
4393 PT_PHDR segment, although that may not be true for all
4398 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4400 /* We need a PT_DYNAMIC segment. */
4404 if (info
!= NULL
&& info
->relro
)
4406 /* We need a PT_GNU_RELRO segment. */
4410 if (elf_eh_frame_hdr (abfd
))
4412 /* We need a PT_GNU_EH_FRAME segment. */
4416 if (elf_stack_flags (abfd
))
4418 /* We need a PT_GNU_STACK segment. */
4422 s
= bfd_get_section_by_name (abfd
,
4423 NOTE_GNU_PROPERTY_SECTION_NAME
);
4424 if (s
!= NULL
&& s
->size
!= 0)
4426 /* We need a PT_GNU_PROPERTY segment. */
4430 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4432 if ((s
->flags
& SEC_LOAD
) != 0
4433 && elf_section_type (s
) == SHT_NOTE
)
4435 unsigned int alignment_power
;
4436 /* We need a PT_NOTE segment. */
4438 /* Try to create just one PT_NOTE segment for all adjacent
4439 loadable SHT_NOTE sections. gABI requires that within a
4440 PT_NOTE segment (and also inside of each SHT_NOTE section)
4441 each note should have the same alignment. So we check
4442 whether the sections are correctly aligned. */
4443 alignment_power
= s
->alignment_power
;
4444 while (s
->next
!= NULL
4445 && s
->next
->alignment_power
== alignment_power
4446 && (s
->next
->flags
& SEC_LOAD
) != 0
4447 && elf_section_type (s
->next
) == SHT_NOTE
)
4452 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4454 if (s
->flags
& SEC_THREAD_LOCAL
)
4456 /* We need a PT_TLS segment. */
4462 bed
= get_elf_backend_data (abfd
);
4464 if ((abfd
->flags
& D_PAGED
) != 0
4465 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4467 /* Add a PT_GNU_MBIND segment for each mbind section. */
4468 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4469 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4470 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4472 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4475 /* xgettext:c-format */
4476 (_("%pB: GNU_MBIND section `%pA' has invalid "
4477 "sh_info field: %d"),
4478 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4481 /* Align mbind section to page size. */
4482 if (s
->alignment_power
< page_align_power
)
4483 s
->alignment_power
= page_align_power
;
4488 /* Let the backend count up any program headers it might need. */
4489 if (bed
->elf_backend_additional_program_headers
)
4493 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4499 return segs
* bed
->s
->sizeof_phdr
;
4502 /* Find the segment that contains the output_section of section. */
4505 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4507 struct elf_segment_map
*m
;
4508 Elf_Internal_Phdr
*p
;
4510 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4516 for (i
= m
->count
- 1; i
>= 0; i
--)
4517 if (m
->sections
[i
] == section
)
4524 /* Create a mapping from a set of sections to a program segment. */
4526 static struct elf_segment_map
*
4527 make_mapping (bfd
*abfd
,
4528 asection
**sections
,
4533 struct elf_segment_map
*m
;
4538 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4539 amt
+= (to
- from
) * sizeof (asection
*);
4540 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4544 m
->p_type
= PT_LOAD
;
4545 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4546 m
->sections
[i
- from
] = *hdrpp
;
4547 m
->count
= to
- from
;
4549 if (from
== 0 && phdr
)
4551 /* Include the headers in the first PT_LOAD segment. */
4552 m
->includes_filehdr
= 1;
4553 m
->includes_phdrs
= 1;
4559 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4562 struct elf_segment_map
*
4563 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4565 struct elf_segment_map
*m
;
4567 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4568 sizeof (struct elf_segment_map
));
4572 m
->p_type
= PT_DYNAMIC
;
4574 m
->sections
[0] = dynsec
;
4579 /* Possibly add or remove segments from the segment map. */
4582 elf_modify_segment_map (bfd
*abfd
,
4583 struct bfd_link_info
*info
,
4584 bfd_boolean remove_empty_load
)
4586 struct elf_segment_map
**m
;
4587 const struct elf_backend_data
*bed
;
4589 /* The placement algorithm assumes that non allocated sections are
4590 not in PT_LOAD segments. We ensure this here by removing such
4591 sections from the segment map. We also remove excluded
4592 sections. Finally, any PT_LOAD segment without sections is
4594 m
= &elf_seg_map (abfd
);
4597 unsigned int i
, new_count
;
4599 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4601 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4602 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4603 || (*m
)->p_type
!= PT_LOAD
))
4605 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4609 (*m
)->count
= new_count
;
4611 if (remove_empty_load
4612 && (*m
)->p_type
== PT_LOAD
4614 && !(*m
)->includes_phdrs
)
4620 bed
= get_elf_backend_data (abfd
);
4621 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4623 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4630 #define IS_TBSS(s) \
4631 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4633 /* Set up a mapping from BFD sections to program segments. */
4636 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4639 struct elf_segment_map
*m
;
4640 asection
**sections
= NULL
;
4641 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4642 bfd_boolean no_user_phdrs
;
4644 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4647 info
->user_phdrs
= !no_user_phdrs
;
4649 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4653 struct elf_segment_map
*mfirst
;
4654 struct elf_segment_map
**pm
;
4657 unsigned int hdr_index
;
4658 bfd_vma maxpagesize
;
4660 bfd_boolean phdr_in_segment
;
4661 bfd_boolean writable
;
4662 bfd_boolean executable
;
4664 asection
*first_tls
= NULL
;
4665 asection
*first_mbind
= NULL
;
4666 asection
*dynsec
, *eh_frame_hdr
;
4668 bfd_vma addr_mask
, wrap_to
= 0;
4669 bfd_size_type phdr_size
;
4671 /* Select the allocated sections, and sort them. */
4673 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4674 sizeof (asection
*));
4675 if (sections
== NULL
)
4678 /* Calculate top address, avoiding undefined behaviour of shift
4679 left operator when shift count is equal to size of type
4681 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4682 addr_mask
= (addr_mask
<< 1) + 1;
4685 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4687 if ((s
->flags
& SEC_ALLOC
) != 0)
4691 /* A wrapping section potentially clashes with header. */
4692 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4693 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4696 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4699 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4701 phdr_size
= elf_program_header_size (abfd
);
4702 if (phdr_size
== (bfd_size_type
) -1)
4703 phdr_size
= get_program_header_size (abfd
, info
);
4704 phdr_size
+= bed
->s
->sizeof_ehdr
;
4705 maxpagesize
= bed
->maxpagesize
;
4706 if (maxpagesize
== 0)
4708 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4710 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4711 >= (phdr_size
& (maxpagesize
- 1))))
4712 /* For compatibility with old scripts that may not be using
4713 SIZEOF_HEADERS, add headers when it looks like space has
4714 been left for them. */
4715 phdr_in_segment
= TRUE
;
4717 /* Build the mapping. */
4721 /* If we have a .interp section, then create a PT_PHDR segment for
4722 the program headers and a PT_INTERP segment for the .interp
4724 s
= bfd_get_section_by_name (abfd
, ".interp");
4725 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4727 amt
= sizeof (struct elf_segment_map
);
4728 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4732 m
->p_type
= PT_PHDR
;
4734 m
->p_flags_valid
= 1;
4735 m
->includes_phdrs
= 1;
4736 phdr_in_segment
= TRUE
;
4740 amt
= sizeof (struct elf_segment_map
);
4741 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4745 m
->p_type
= PT_INTERP
;
4753 /* Look through the sections. We put sections in the same program
4754 segment when the start of the second section can be placed within
4755 a few bytes of the end of the first section. */
4761 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4763 && (dynsec
->flags
& SEC_LOAD
) == 0)
4766 if ((abfd
->flags
& D_PAGED
) == 0)
4767 phdr_in_segment
= FALSE
;
4769 /* Deal with -Ttext or something similar such that the first section
4770 is not adjacent to the program headers. This is an
4771 approximation, since at this point we don't know exactly how many
4772 program headers we will need. */
4773 if (phdr_in_segment
&& count
> 0)
4776 bfd_boolean separate_phdr
= FALSE
;
4778 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4780 && info
->separate_code
4781 && (sections
[0]->flags
& SEC_CODE
) != 0)
4783 /* If data sections should be separate from code and
4784 thus not executable, and the first section is
4785 executable then put the file and program headers in
4786 their own PT_LOAD. */
4787 separate_phdr
= TRUE
;
4788 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4789 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4791 /* The file and program headers are currently on the
4792 same page as the first section. Put them on the
4793 previous page if we can. */
4794 if (phdr_lma
>= maxpagesize
)
4795 phdr_lma
-= maxpagesize
;
4797 separate_phdr
= FALSE
;
4800 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4801 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4802 /* If file and program headers would be placed at the end
4803 of memory then it's probably better to omit them. */
4804 phdr_in_segment
= FALSE
;
4805 else if (phdr_lma
< wrap_to
)
4806 /* If a section wraps around to where we'll be placing
4807 file and program headers, then the headers will be
4809 phdr_in_segment
= FALSE
;
4810 else if (separate_phdr
)
4812 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4815 m
->p_paddr
= phdr_lma
;
4817 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4818 m
->p_paddr_valid
= 1;
4821 phdr_in_segment
= FALSE
;
4825 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4828 bfd_boolean new_segment
;
4832 /* See if this section and the last one will fit in the same
4835 if (last_hdr
== NULL
)
4837 /* If we don't have a segment yet, then we don't need a new
4838 one (we build the last one after this loop). */
4839 new_segment
= FALSE
;
4841 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4843 /* If this section has a different relation between the
4844 virtual address and the load address, then we need a new
4848 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4849 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4851 /* If this section has a load address that makes it overlap
4852 the previous section, then we need a new segment. */
4855 else if ((abfd
->flags
& D_PAGED
) != 0
4856 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4857 == (hdr
->lma
& -maxpagesize
)))
4859 /* If we are demand paged then we can't map two disk
4860 pages onto the same memory page. */
4861 new_segment
= FALSE
;
4863 /* In the next test we have to be careful when last_hdr->lma is close
4864 to the end of the address space. If the aligned address wraps
4865 around to the start of the address space, then there are no more
4866 pages left in memory and it is OK to assume that the current
4867 section can be included in the current segment. */
4868 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4869 + maxpagesize
> last_hdr
->lma
)
4870 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4871 + maxpagesize
<= hdr
->lma
))
4873 /* If putting this section in this segment would force us to
4874 skip a page in the segment, then we need a new segment. */
4877 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4878 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4880 /* We don't want to put a loaded section after a
4881 nonloaded (ie. bss style) section in the same segment
4882 as that will force the non-loaded section to be loaded.
4883 Consider .tbss sections as loaded for this purpose. */
4886 else if ((abfd
->flags
& D_PAGED
) == 0)
4888 /* If the file is not demand paged, which means that we
4889 don't require the sections to be correctly aligned in the
4890 file, then there is no other reason for a new segment. */
4891 new_segment
= FALSE
;
4893 else if (info
!= NULL
4894 && info
->separate_code
4895 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4900 && (hdr
->flags
& SEC_READONLY
) == 0)
4902 /* We don't want to put a writable section in a read only
4908 /* Otherwise, we can use the same segment. */
4909 new_segment
= FALSE
;
4912 /* Allow interested parties a chance to override our decision. */
4913 if (last_hdr
!= NULL
4915 && info
->callbacks
->override_segment_assignment
!= NULL
)
4917 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4923 if ((hdr
->flags
& SEC_READONLY
) == 0)
4925 if ((hdr
->flags
& SEC_CODE
) != 0)
4928 /* .tbss sections effectively have zero size. */
4929 last_size
= !IS_TBSS (hdr
) ? hdr
->size
: 0;
4933 /* We need a new program segment. We must create a new program
4934 header holding all the sections from hdr_index until hdr. */
4936 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4943 if ((hdr
->flags
& SEC_READONLY
) == 0)
4948 if ((hdr
->flags
& SEC_CODE
) == 0)
4954 /* .tbss sections effectively have zero size. */
4955 last_size
= !IS_TBSS (hdr
) ? hdr
->size
: 0;
4957 phdr_in_segment
= FALSE
;
4960 /* Create a final PT_LOAD program segment, but not if it's just
4962 if (last_hdr
!= NULL
4963 && (i
- hdr_index
!= 1
4964 || !IS_TBSS (last_hdr
)))
4966 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4974 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4977 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4984 /* For each batch of consecutive loadable SHT_NOTE sections,
4985 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4986 because if we link together nonloadable .note sections and
4987 loadable .note sections, we will generate two .note sections
4988 in the output file. */
4989 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4991 if ((s
->flags
& SEC_LOAD
) != 0
4992 && elf_section_type (s
) == SHT_NOTE
)
4995 unsigned int alignment_power
= s
->alignment_power
;
4998 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5000 if (s2
->next
->alignment_power
== alignment_power
5001 && (s2
->next
->flags
& SEC_LOAD
) != 0
5002 && elf_section_type (s2
->next
) == SHT_NOTE
5003 && align_power (s2
->lma
+ s2
->size
,
5010 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5011 amt
+= count
* sizeof (asection
*);
5012 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5016 m
->p_type
= PT_NOTE
;
5020 m
->sections
[m
->count
- count
--] = s
;
5021 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5024 m
->sections
[m
->count
- 1] = s
;
5025 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5029 if (s
->flags
& SEC_THREAD_LOCAL
)
5035 if (first_mbind
== NULL
5036 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5040 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5043 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5044 amt
+= tls_count
* sizeof (asection
*);
5045 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5050 m
->count
= tls_count
;
5051 /* Mandated PF_R. */
5053 m
->p_flags_valid
= 1;
5055 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
5057 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5060 (_("%pB: TLS sections are not adjacent:"), abfd
);
5063 while (i
< (unsigned int) tls_count
)
5065 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5067 _bfd_error_handler (_(" TLS: %pA"), s
);
5071 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5074 bfd_set_error (bfd_error_bad_value
);
5086 && (abfd
->flags
& D_PAGED
) != 0
5087 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5088 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5089 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5090 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5092 /* Mandated PF_R. */
5093 unsigned long p_flags
= PF_R
;
5094 if ((s
->flags
& SEC_READONLY
) == 0)
5096 if ((s
->flags
& SEC_CODE
) != 0)
5099 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5100 m
= bfd_zalloc (abfd
, amt
);
5104 m
->p_type
= (PT_GNU_MBIND_LO
5105 + elf_section_data (s
)->this_hdr
.sh_info
);
5107 m
->p_flags_valid
= 1;
5109 m
->p_flags
= p_flags
;
5115 s
= bfd_get_section_by_name (abfd
,
5116 NOTE_GNU_PROPERTY_SECTION_NAME
);
5117 if (s
!= NULL
&& s
->size
!= 0)
5119 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5120 m
= bfd_zalloc (abfd
, amt
);
5124 m
->p_type
= PT_GNU_PROPERTY
;
5126 m
->p_flags_valid
= 1;
5133 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5135 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5136 if (eh_frame_hdr
!= NULL
5137 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5139 amt
= sizeof (struct elf_segment_map
);
5140 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5144 m
->p_type
= PT_GNU_EH_FRAME
;
5146 m
->sections
[0] = eh_frame_hdr
->output_section
;
5152 if (elf_stack_flags (abfd
))
5154 amt
= sizeof (struct elf_segment_map
);
5155 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5159 m
->p_type
= PT_GNU_STACK
;
5160 m
->p_flags
= elf_stack_flags (abfd
);
5161 m
->p_align
= bed
->stack_align
;
5162 m
->p_flags_valid
= 1;
5163 m
->p_align_valid
= m
->p_align
!= 0;
5164 if (info
->stacksize
> 0)
5166 m
->p_size
= info
->stacksize
;
5167 m
->p_size_valid
= 1;
5174 if (info
!= NULL
&& info
->relro
)
5176 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5178 if (m
->p_type
== PT_LOAD
5180 && m
->sections
[0]->vma
>= info
->relro_start
5181 && m
->sections
[0]->vma
< info
->relro_end
)
5184 while (--i
!= (unsigned) -1)
5185 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5186 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5189 if (i
!= (unsigned) -1)
5194 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5197 amt
= sizeof (struct elf_segment_map
);
5198 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5202 m
->p_type
= PT_GNU_RELRO
;
5209 elf_seg_map (abfd
) = mfirst
;
5212 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5215 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5217 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5222 if (sections
!= NULL
)
5227 /* Sort sections by address. */
5230 elf_sort_sections (const void *arg1
, const void *arg2
)
5232 const asection
*sec1
= *(const asection
**) arg1
;
5233 const asection
*sec2
= *(const asection
**) arg2
;
5234 bfd_size_type size1
, size2
;
5236 /* Sort by LMA first, since this is the address used to
5237 place the section into a segment. */
5238 if (sec1
->lma
< sec2
->lma
)
5240 else if (sec1
->lma
> sec2
->lma
)
5243 /* Then sort by VMA. Normally the LMA and the VMA will be
5244 the same, and this will do nothing. */
5245 if (sec1
->vma
< sec2
->vma
)
5247 else if (sec1
->vma
> sec2
->vma
)
5250 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5252 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5258 /* If the indices are the same, do not return 0
5259 here, but continue to try the next comparison. */
5260 if (sec1
->target_index
- sec2
->target_index
!= 0)
5261 return sec1
->target_index
- sec2
->target_index
;
5266 else if (TOEND (sec2
))
5271 /* Sort by size, to put zero sized sections
5272 before others at the same address. */
5274 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5275 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5282 return sec1
->target_index
- sec2
->target_index
;
5285 /* Ian Lance Taylor writes:
5287 We shouldn't be using % with a negative signed number. That's just
5288 not good. We have to make sure either that the number is not
5289 negative, or that the number has an unsigned type. When the types
5290 are all the same size they wind up as unsigned. When file_ptr is a
5291 larger signed type, the arithmetic winds up as signed long long,
5294 What we're trying to say here is something like ``increase OFF by
5295 the least amount that will cause it to be equal to the VMA modulo
5297 /* In other words, something like:
5299 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5300 off_offset = off % bed->maxpagesize;
5301 if (vma_offset < off_offset)
5302 adjustment = vma_offset + bed->maxpagesize - off_offset;
5304 adjustment = vma_offset - off_offset;
5306 which can be collapsed into the expression below. */
5309 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5311 /* PR binutils/16199: Handle an alignment of zero. */
5312 if (maxpagesize
== 0)
5314 return ((vma
- off
) % maxpagesize
);
5318 print_segment_map (const struct elf_segment_map
*m
)
5321 const char *pt
= get_segment_type (m
->p_type
);
5326 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5327 sprintf (buf
, "LOPROC+%7.7x",
5328 (unsigned int) (m
->p_type
- PT_LOPROC
));
5329 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5330 sprintf (buf
, "LOOS+%7.7x",
5331 (unsigned int) (m
->p_type
- PT_LOOS
));
5333 snprintf (buf
, sizeof (buf
), "%8.8x",
5334 (unsigned int) m
->p_type
);
5338 fprintf (stderr
, "%s:", pt
);
5339 for (j
= 0; j
< m
->count
; j
++)
5340 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5346 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5351 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5353 buf
= bfd_zmalloc (len
);
5356 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5361 /* Assign file positions to the sections based on the mapping from
5362 sections to segments. This function also sets up some fields in
5366 assign_file_positions_for_load_sections (bfd
*abfd
,
5367 struct bfd_link_info
*link_info
)
5369 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5370 struct elf_segment_map
*m
;
5371 Elf_Internal_Phdr
*phdrs
;
5372 Elf_Internal_Phdr
*p
;
5374 bfd_size_type maxpagesize
;
5375 unsigned int pt_load_count
= 0;
5378 bfd_vma header_pad
= 0;
5380 if (link_info
== NULL
5381 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5385 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5389 header_pad
= m
->header_size
;
5394 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5395 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5399 /* PR binutils/12467. */
5400 elf_elfheader (abfd
)->e_phoff
= 0;
5401 elf_elfheader (abfd
)->e_phentsize
= 0;
5404 elf_elfheader (abfd
)->e_phnum
= alloc
;
5406 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5407 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5409 BFD_ASSERT (elf_program_header_size (abfd
)
5410 >= alloc
* bed
->s
->sizeof_phdr
);
5414 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5418 /* We're writing the size in elf_program_header_size (abfd),
5419 see assign_file_positions_except_relocs, so make sure we have
5420 that amount allocated, with trailing space cleared.
5421 The variable alloc contains the computed need, while
5422 elf_program_header_size (abfd) contains the size used for the
5424 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5425 where the layout is forced to according to a larger size in the
5426 last iterations for the testcase ld-elf/header. */
5427 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5429 phdrs
= (Elf_Internal_Phdr
*)
5431 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5432 sizeof (Elf_Internal_Phdr
));
5433 elf_tdata (abfd
)->phdr
= phdrs
;
5438 if ((abfd
->flags
& D_PAGED
) != 0)
5439 maxpagesize
= bed
->maxpagesize
;
5441 off
= bed
->s
->sizeof_ehdr
;
5442 off
+= alloc
* bed
->s
->sizeof_phdr
;
5443 if (header_pad
< (bfd_vma
) off
)
5449 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5451 m
= m
->next
, p
++, j
++)
5455 bfd_boolean no_contents
;
5457 /* If elf_segment_map is not from map_sections_to_segments, the
5458 sections may not be correctly ordered. NOTE: sorting should
5459 not be done to the PT_NOTE section of a corefile, which may
5460 contain several pseudo-sections artificially created by bfd.
5461 Sorting these pseudo-sections breaks things badly. */
5463 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5464 && m
->p_type
== PT_NOTE
))
5465 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5468 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5469 number of sections with contents contributing to both p_filesz
5470 and p_memsz, followed by a number of sections with no contents
5471 that just contribute to p_memsz. In this loop, OFF tracks next
5472 available file offset for PT_LOAD and PT_NOTE segments. */
5473 p
->p_type
= m
->p_type
;
5474 p
->p_flags
= m
->p_flags
;
5477 p
->p_vaddr
= m
->p_vaddr_offset
;
5479 p
->p_vaddr
= m
->sections
[0]->vma
+ m
->p_vaddr_offset
;
5481 if (m
->p_paddr_valid
)
5482 p
->p_paddr
= m
->p_paddr
;
5483 else if (m
->count
== 0)
5486 p
->p_paddr
= m
->sections
[0]->lma
+ m
->p_vaddr_offset
;
5488 if (p
->p_type
== PT_LOAD
5489 && (abfd
->flags
& D_PAGED
) != 0)
5491 /* p_align in demand paged PT_LOAD segments effectively stores
5492 the maximum page size. When copying an executable with
5493 objcopy, we set m->p_align from the input file. Use this
5494 value for maxpagesize rather than bed->maxpagesize, which
5495 may be different. Note that we use maxpagesize for PT_TLS
5496 segment alignment later in this function, so we are relying
5497 on at least one PT_LOAD segment appearing before a PT_TLS
5499 if (m
->p_align_valid
)
5500 maxpagesize
= m
->p_align
;
5502 p
->p_align
= maxpagesize
;
5505 else if (m
->p_align_valid
)
5506 p
->p_align
= m
->p_align
;
5507 else if (m
->count
== 0)
5508 p
->p_align
= 1 << bed
->s
->log_file_align
;
5512 no_contents
= FALSE
;
5514 if (p
->p_type
== PT_LOAD
5517 bfd_size_type align
;
5518 unsigned int align_power
= 0;
5520 if (m
->p_align_valid
)
5524 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5526 unsigned int secalign
;
5528 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5529 if (secalign
> align_power
)
5530 align_power
= secalign
;
5532 align
= (bfd_size_type
) 1 << align_power
;
5533 if (align
< maxpagesize
)
5534 align
= maxpagesize
;
5537 for (i
= 0; i
< m
->count
; i
++)
5538 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5539 /* If we aren't making room for this section, then
5540 it must be SHT_NOBITS regardless of what we've
5541 set via struct bfd_elf_special_section. */
5542 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5544 /* Find out whether this segment contains any loadable
5547 for (i
= 0; i
< m
->count
; i
++)
5548 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5550 no_contents
= FALSE
;
5554 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5556 /* Broken hardware and/or kernel require that files do not
5557 map the same page with different permissions on some hppa
5559 if (pt_load_count
> 1
5560 && bed
->no_page_alias
5561 && (off
& (maxpagesize
- 1)) != 0
5562 && (off
& -maxpagesize
) == ((off
+ off_adjust
) & -maxpagesize
))
5563 off_adjust
+= maxpagesize
;
5567 /* We shouldn't need to align the segment on disk since
5568 the segment doesn't need file space, but the gABI
5569 arguably requires the alignment and glibc ld.so
5570 checks it. So to comply with the alignment
5571 requirement but not waste file space, we adjust
5572 p_offset for just this segment. (OFF_ADJUST is
5573 subtracted from OFF later.) This may put p_offset
5574 past the end of file, but that shouldn't matter. */
5579 /* Make sure the .dynamic section is the first section in the
5580 PT_DYNAMIC segment. */
5581 else if (p
->p_type
== PT_DYNAMIC
5583 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5586 (_("%pB: The first section in the PT_DYNAMIC segment"
5587 " is not the .dynamic section"),
5589 bfd_set_error (bfd_error_bad_value
);
5592 /* Set the note section type to SHT_NOTE. */
5593 else if (p
->p_type
== PT_NOTE
)
5594 for (i
= 0; i
< m
->count
; i
++)
5595 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5601 if (m
->includes_filehdr
)
5603 if (!m
->p_flags_valid
)
5605 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5606 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5609 if (p
->p_vaddr
< (bfd_vma
) off
5610 || (!m
->p_paddr_valid
5611 && p
->p_paddr
< (bfd_vma
) off
))
5614 (_("%pB: not enough room for program headers,"
5615 " try linking with -N"),
5617 bfd_set_error (bfd_error_bad_value
);
5622 if (!m
->p_paddr_valid
)
5627 if (m
->includes_phdrs
)
5629 if (!m
->p_flags_valid
)
5632 if (!m
->includes_filehdr
)
5634 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5638 p
->p_vaddr
-= off
- p
->p_offset
;
5639 if (!m
->p_paddr_valid
)
5640 p
->p_paddr
-= off
- p
->p_offset
;
5644 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5645 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5648 p
->p_filesz
+= header_pad
;
5649 p
->p_memsz
+= header_pad
;
5653 if (p
->p_type
== PT_LOAD
5654 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5656 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5662 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5664 p
->p_filesz
+= adjust
;
5665 p
->p_memsz
+= adjust
;
5669 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5670 maps. Set filepos for sections in PT_LOAD segments, and in
5671 core files, for sections in PT_NOTE segments.
5672 assign_file_positions_for_non_load_sections will set filepos
5673 for other sections and update p_filesz for other segments. */
5674 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5677 bfd_size_type align
;
5678 Elf_Internal_Shdr
*this_hdr
;
5681 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5682 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5684 if ((p
->p_type
== PT_LOAD
5685 || p
->p_type
== PT_TLS
)
5686 && (this_hdr
->sh_type
!= SHT_NOBITS
5687 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5688 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5689 || p
->p_type
== PT_TLS
))))
5691 bfd_vma p_start
= p
->p_paddr
;
5692 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5693 bfd_vma s_start
= sec
->lma
;
5694 bfd_vma adjust
= s_start
- p_end
;
5698 || p_end
< p_start
))
5701 /* xgettext:c-format */
5702 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5703 abfd
, sec
, (uint64_t) s_start
, (uint64_t) p_end
);
5707 p
->p_memsz
+= adjust
;
5709 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5711 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5713 /* We have a PROGBITS section following NOBITS ones.
5714 Allocate file space for the NOBITS section(s) and
5716 adjust
= p
->p_memsz
- p
->p_filesz
;
5717 if (!write_zeros (abfd
, off
, adjust
))
5721 p
->p_filesz
+= adjust
;
5725 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5727 /* The section at i == 0 is the one that actually contains
5731 this_hdr
->sh_offset
= sec
->filepos
= off
;
5732 off
+= this_hdr
->sh_size
;
5733 p
->p_filesz
= this_hdr
->sh_size
;
5739 /* The rest are fake sections that shouldn't be written. */
5748 if (p
->p_type
== PT_LOAD
)
5750 this_hdr
->sh_offset
= sec
->filepos
= off
;
5751 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5752 off
+= this_hdr
->sh_size
;
5754 else if (this_hdr
->sh_type
== SHT_NOBITS
5755 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5756 && this_hdr
->sh_offset
== 0)
5758 /* This is a .tbss section that didn't get a PT_LOAD.
5759 (See _bfd_elf_map_sections_to_segments "Create a
5760 final PT_LOAD".) Set sh_offset to the value it
5761 would have if we had created a zero p_filesz and
5762 p_memsz PT_LOAD header for the section. This
5763 also makes the PT_TLS header have the same
5765 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5767 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5770 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5772 p
->p_filesz
+= this_hdr
->sh_size
;
5773 /* A load section without SHF_ALLOC is something like
5774 a note section in a PT_NOTE segment. These take
5775 file space but are not loaded into memory. */
5776 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5777 p
->p_memsz
+= this_hdr
->sh_size
;
5779 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5781 if (p
->p_type
== PT_TLS
)
5782 p
->p_memsz
+= this_hdr
->sh_size
;
5784 /* .tbss is special. It doesn't contribute to p_memsz of
5786 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5787 p
->p_memsz
+= this_hdr
->sh_size
;
5790 if (align
> p
->p_align
5791 && !m
->p_align_valid
5792 && (p
->p_type
!= PT_LOAD
5793 || (abfd
->flags
& D_PAGED
) == 0))
5797 if (!m
->p_flags_valid
)
5800 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5802 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5809 /* Check that all sections are in a PT_LOAD segment.
5810 Don't check funky gdb generated core files. */
5811 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5813 bfd_boolean check_vma
= TRUE
;
5815 for (i
= 1; i
< m
->count
; i
++)
5816 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5817 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5818 ->this_hdr
), p
) != 0
5819 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5820 ->this_hdr
), p
) != 0)
5822 /* Looks like we have overlays packed into the segment. */
5827 for (i
= 0; i
< m
->count
; i
++)
5829 Elf_Internal_Shdr
*this_hdr
;
5832 sec
= m
->sections
[i
];
5833 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5834 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5835 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5838 /* xgettext:c-format */
5839 (_("%pB: section `%pA' can't be allocated in segment %d"),
5841 print_segment_map (m
);
5847 elf_next_file_pos (abfd
) = off
;
5851 /* Determine if a bfd is a debuginfo file. Unfortunately there
5852 is no defined method for detecting such files, so we have to
5853 use heuristics instead. */
5856 is_debuginfo_file (bfd
*abfd
)
5858 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5861 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
5862 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
5863 Elf_Internal_Shdr
**headerp
;
5865 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
5867 Elf_Internal_Shdr
*header
= * headerp
;
5869 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
5870 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
5871 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
5872 && header
->sh_type
!= SHT_NOBITS
5873 && header
->sh_type
!= SHT_NOTE
)
5880 /* Assign file positions for the other sections. */
5883 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5884 struct bfd_link_info
*link_info
)
5886 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5887 Elf_Internal_Shdr
**i_shdrpp
;
5888 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5889 Elf_Internal_Phdr
*phdrs
;
5890 Elf_Internal_Phdr
*p
;
5891 struct elf_segment_map
*m
;
5892 struct elf_segment_map
*hdrs_segment
;
5893 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5894 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5898 i_shdrpp
= elf_elfsections (abfd
);
5899 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5900 off
= elf_next_file_pos (abfd
);
5901 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5903 Elf_Internal_Shdr
*hdr
;
5906 if (hdr
->bfd_section
!= NULL
5907 && (hdr
->bfd_section
->filepos
!= 0
5908 || (hdr
->sh_type
== SHT_NOBITS
5909 && hdr
->contents
== NULL
)))
5910 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5911 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5913 if (hdr
->sh_size
!= 0
5914 /* PR 24717 - debuginfo files are known to be not strictly
5915 compliant with the ELF standard. In particular they often
5916 have .note.gnu.property sections that are outside of any
5917 loadable segment. This is not a problem for such files,
5918 so do not warn about them. */
5919 && ! is_debuginfo_file (abfd
))
5921 /* xgettext:c-format */
5922 (_("%pB: warning: allocated section `%s' not in segment"),
5924 (hdr
->bfd_section
== NULL
5926 : hdr
->bfd_section
->name
));
5927 /* We don't need to page align empty sections. */
5928 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5929 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5932 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5934 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5937 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5938 && hdr
->bfd_section
== NULL
)
5939 || (hdr
->bfd_section
!= NULL
5940 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5941 /* Compress DWARF debug sections. */
5942 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5943 || (elf_symtab_shndx_list (abfd
) != NULL
5944 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5945 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5946 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5947 hdr
->sh_offset
= -1;
5949 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5952 /* Now that we have set the section file positions, we can set up
5953 the file positions for the non PT_LOAD segments. */
5957 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5959 hdrs_segment
= NULL
;
5960 phdrs
= elf_tdata (abfd
)->phdr
;
5961 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5964 if (p
->p_type
!= PT_LOAD
)
5967 if (m
->includes_filehdr
)
5969 filehdr_vaddr
= p
->p_vaddr
;
5970 filehdr_paddr
= p
->p_paddr
;
5972 if (m
->includes_phdrs
)
5974 phdrs_vaddr
= p
->p_vaddr
;
5975 phdrs_paddr
= p
->p_paddr
;
5976 if (m
->includes_filehdr
)
5979 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5980 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5985 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5987 /* There is a segment that contains both the file headers and the
5988 program headers, so provide a symbol __ehdr_start pointing there.
5989 A program can use this to examine itself robustly. */
5991 struct elf_link_hash_entry
*hash
5992 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5993 FALSE
, FALSE
, TRUE
);
5994 /* If the symbol was referenced and not defined, define it. */
5996 && (hash
->root
.type
== bfd_link_hash_new
5997 || hash
->root
.type
== bfd_link_hash_undefined
5998 || hash
->root
.type
== bfd_link_hash_undefweak
5999 || hash
->root
.type
== bfd_link_hash_common
))
6002 if (hdrs_segment
->count
!= 0)
6003 /* The segment contains sections, so use the first one. */
6004 s
= hdrs_segment
->sections
[0];
6006 /* Use the first (i.e. lowest-addressed) section in any segment. */
6007 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6016 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6017 hash
->root
.u
.def
.section
= s
;
6021 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6022 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6025 hash
->root
.type
= bfd_link_hash_defined
;
6026 hash
->def_regular
= 1;
6031 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6033 if (p
->p_type
== PT_GNU_RELRO
)
6038 if (link_info
!= NULL
)
6040 /* During linking the range of the RELRO segment is passed
6041 in link_info. Note that there may be padding between
6042 relro_start and the first RELRO section. */
6043 start
= link_info
->relro_start
;
6044 end
= link_info
->relro_end
;
6046 else if (m
->count
!= 0)
6048 if (!m
->p_size_valid
)
6050 start
= m
->sections
[0]->vma
;
6051 end
= start
+ m
->p_size
;
6062 struct elf_segment_map
*lm
;
6063 const Elf_Internal_Phdr
*lp
;
6066 /* Find a LOAD segment containing a section in the RELRO
6068 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6070 lm
= lm
->next
, lp
++)
6072 if (lp
->p_type
== PT_LOAD
6074 && (lm
->sections
[lm
->count
- 1]->vma
6075 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6076 ? lm
->sections
[lm
->count
- 1]->size
6078 && lm
->sections
[0]->vma
< end
)
6084 /* Find the section starting the RELRO segment. */
6085 for (i
= 0; i
< lm
->count
; i
++)
6087 asection
*s
= lm
->sections
[i
];
6096 p
->p_vaddr
= lm
->sections
[i
]->vma
;
6097 p
->p_paddr
= lm
->sections
[i
]->lma
;
6098 p
->p_offset
= lm
->sections
[i
]->filepos
;
6099 p
->p_memsz
= end
- p
->p_vaddr
;
6100 p
->p_filesz
= p
->p_memsz
;
6102 /* The RELRO segment typically ends a few bytes
6103 into .got.plt but other layouts are possible.
6104 In cases where the end does not match any
6105 loaded section (for instance is in file
6106 padding), trim p_filesz back to correspond to
6107 the end of loaded section contents. */
6108 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6109 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6111 /* Preserve the alignment and flags if they are
6112 valid. The gold linker generates RW/4 for
6113 the PT_GNU_RELRO section. It is better for
6114 objcopy/strip to honor these attributes
6115 otherwise gdb will choke when using separate
6117 if (!m
->p_align_valid
)
6119 if (!m
->p_flags_valid
)
6125 if (link_info
!= NULL
)
6128 memset (p
, 0, sizeof *p
);
6130 else if (p
->p_type
== PT_GNU_STACK
)
6132 if (m
->p_size_valid
)
6133 p
->p_memsz
= m
->p_size
;
6135 else if (m
->count
!= 0)
6139 if (p
->p_type
!= PT_LOAD
6140 && (p
->p_type
!= PT_NOTE
6141 || bfd_get_format (abfd
) != bfd_core
))
6143 /* A user specified segment layout may include a PHDR
6144 segment that overlaps with a LOAD segment... */
6145 if (p
->p_type
== PT_PHDR
)
6151 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6153 /* PR 17512: file: 2195325e. */
6155 (_("%pB: error: non-load segment %d includes file header "
6156 "and/or program header"),
6157 abfd
, (int) (p
- phdrs
));
6162 p
->p_offset
= m
->sections
[0]->filepos
;
6163 for (i
= m
->count
; i
-- != 0;)
6165 asection
*sect
= m
->sections
[i
];
6166 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6167 if (hdr
->sh_type
!= SHT_NOBITS
)
6169 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6176 else if (m
->includes_filehdr
)
6178 p
->p_vaddr
= filehdr_vaddr
;
6179 if (! m
->p_paddr_valid
)
6180 p
->p_paddr
= filehdr_paddr
;
6182 else if (m
->includes_phdrs
)
6184 p
->p_vaddr
= phdrs_vaddr
;
6185 if (! m
->p_paddr_valid
)
6186 p
->p_paddr
= phdrs_paddr
;
6190 elf_next_file_pos (abfd
) = off
;
6195 static elf_section_list
*
6196 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6198 for (;list
!= NULL
; list
= list
->next
)
6204 /* Work out the file positions of all the sections. This is called by
6205 _bfd_elf_compute_section_file_positions. All the section sizes and
6206 VMAs must be known before this is called.
6208 Reloc sections come in two flavours: Those processed specially as
6209 "side-channel" data attached to a section to which they apply, and
6210 those that bfd doesn't process as relocations. The latter sort are
6211 stored in a normal bfd section by bfd_section_from_shdr. We don't
6212 consider the former sort here, unless they form part of the loadable
6213 image. Reloc sections not assigned here will be handled later by
6214 assign_file_positions_for_relocs.
6216 We also don't set the positions of the .symtab and .strtab here. */
6219 assign_file_positions_except_relocs (bfd
*abfd
,
6220 struct bfd_link_info
*link_info
)
6222 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6223 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6224 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6226 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6227 && bfd_get_format (abfd
) != bfd_core
)
6229 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6230 unsigned int num_sec
= elf_numsections (abfd
);
6231 Elf_Internal_Shdr
**hdrpp
;
6235 /* Start after the ELF header. */
6236 off
= i_ehdrp
->e_ehsize
;
6238 /* We are not creating an executable, which means that we are
6239 not creating a program header, and that the actual order of
6240 the sections in the file is unimportant. */
6241 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6243 Elf_Internal_Shdr
*hdr
;
6246 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6247 && hdr
->bfd_section
== NULL
)
6248 || (hdr
->bfd_section
!= NULL
6249 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
6250 /* Compress DWARF debug sections. */
6251 || i
== elf_onesymtab (abfd
)
6252 || (elf_symtab_shndx_list (abfd
) != NULL
6253 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6254 || i
== elf_strtab_sec (abfd
)
6255 || i
== elf_shstrtab_sec (abfd
))
6257 hdr
->sh_offset
= -1;
6260 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6263 elf_next_file_pos (abfd
) = off
;
6269 /* Assign file positions for the loaded sections based on the
6270 assignment of sections to segments. */
6271 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6274 /* And for non-load sections. */
6275 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6278 if (bed
->elf_backend_modify_program_headers
!= NULL
)
6280 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
6284 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6285 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6287 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
6288 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
6289 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6291 /* Find the lowest p_vaddr in PT_LOAD segments. */
6292 bfd_vma p_vaddr
= (bfd_vma
) -1;
6293 for (; segment
< end_segment
; segment
++)
6294 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6295 p_vaddr
= segment
->p_vaddr
;
6297 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6298 segments is non-zero. */
6300 i_ehdrp
->e_type
= ET_EXEC
;
6303 /* Write out the program headers. */
6304 alloc
= elf_elfheader (abfd
)->e_phnum
;
6308 /* PR ld/20815 - Check that the program header segment, if present, will
6309 be loaded into memory. FIXME: The check below is not sufficient as
6310 really all PT_LOAD segments should be checked before issuing an error
6311 message. Plus the PHDR segment does not have to be the first segment
6312 in the program header table. But this version of the check should
6313 catch all real world use cases.
6315 FIXME: We used to have code here to sort the PT_LOAD segments into
6316 ascending order, as per the ELF spec. But this breaks some programs,
6317 including the Linux kernel. But really either the spec should be
6318 changed or the programs updated. */
6320 && tdata
->phdr
[0].p_type
== PT_PHDR
6321 && (bed
->elf_backend_allow_non_load_phdr
== NULL
6322 || !bed
->elf_backend_allow_non_load_phdr (abfd
, tdata
->phdr
,
6324 && tdata
->phdr
[1].p_type
== PT_LOAD
6325 && (tdata
->phdr
[1].p_vaddr
> tdata
->phdr
[0].p_vaddr
6326 || (tdata
->phdr
[1].p_vaddr
+ tdata
->phdr
[1].p_memsz
6327 < tdata
->phdr
[0].p_vaddr
+ tdata
->phdr
[0].p_memsz
)))
6329 /* The fix for this error is usually to edit the linker script being
6330 used and set up the program headers manually. Either that or
6331 leave room for the headers at the start of the SECTIONS. */
6332 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6333 " by LOAD segment"),
6338 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
6339 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6347 prep_headers (bfd
*abfd
)
6349 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6350 struct elf_strtab_hash
*shstrtab
;
6351 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6353 i_ehdrp
= elf_elfheader (abfd
);
6355 shstrtab
= _bfd_elf_strtab_init ();
6356 if (shstrtab
== NULL
)
6359 elf_shstrtab (abfd
) = shstrtab
;
6361 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6362 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6363 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6364 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6366 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6367 i_ehdrp
->e_ident
[EI_DATA
] =
6368 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6369 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6371 if ((abfd
->flags
& DYNAMIC
) != 0)
6372 i_ehdrp
->e_type
= ET_DYN
;
6373 else if ((abfd
->flags
& EXEC_P
) != 0)
6374 i_ehdrp
->e_type
= ET_EXEC
;
6375 else if (bfd_get_format (abfd
) == bfd_core
)
6376 i_ehdrp
->e_type
= ET_CORE
;
6378 i_ehdrp
->e_type
= ET_REL
;
6380 switch (bfd_get_arch (abfd
))
6382 case bfd_arch_unknown
:
6383 i_ehdrp
->e_machine
= EM_NONE
;
6386 /* There used to be a long list of cases here, each one setting
6387 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6388 in the corresponding bfd definition. To avoid duplication,
6389 the switch was removed. Machines that need special handling
6390 can generally do it in elf_backend_final_write_processing(),
6391 unless they need the information earlier than the final write.
6392 Such need can generally be supplied by replacing the tests for
6393 e_machine with the conditions used to determine it. */
6395 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6398 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6399 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6401 /* No program header, for now. */
6402 i_ehdrp
->e_phoff
= 0;
6403 i_ehdrp
->e_phentsize
= 0;
6404 i_ehdrp
->e_phnum
= 0;
6406 /* Each bfd section is section header entry. */
6407 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6408 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6410 /* If we're building an executable, we'll need a program header table. */
6411 if (abfd
->flags
& EXEC_P
)
6412 /* It all happens later. */
6416 i_ehdrp
->e_phentsize
= 0;
6417 i_ehdrp
->e_phoff
= 0;
6420 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6421 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6422 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6423 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6424 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6425 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6426 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6427 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6428 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6434 /* Assign file positions for all the reloc sections which are not part
6435 of the loadable file image, and the file position of section headers. */
6438 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6441 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6442 Elf_Internal_Shdr
*shdrp
;
6443 Elf_Internal_Ehdr
*i_ehdrp
;
6444 const struct elf_backend_data
*bed
;
6446 off
= elf_next_file_pos (abfd
);
6448 shdrpp
= elf_elfsections (abfd
);
6449 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6450 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6453 if (shdrp
->sh_offset
== -1)
6455 asection
*sec
= shdrp
->bfd_section
;
6456 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6457 || shdrp
->sh_type
== SHT_RELA
);
6459 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6463 const char *name
= sec
->name
;
6464 struct bfd_elf_section_data
*d
;
6466 /* Compress DWARF debug sections. */
6467 if (!bfd_compress_section (abfd
, sec
,
6471 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6472 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6474 /* If section is compressed with zlib-gnu, convert
6475 section name from .debug_* to .zdebug_*. */
6477 = convert_debug_to_zdebug (abfd
, name
);
6478 if (new_name
== NULL
)
6482 /* Add section name to section name section. */
6483 if (shdrp
->sh_name
!= (unsigned int) -1)
6486 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6488 d
= elf_section_data (sec
);
6490 /* Add reloc section name to section name section. */
6492 && !_bfd_elf_set_reloc_sh_name (abfd
,
6497 && !_bfd_elf_set_reloc_sh_name (abfd
,
6502 /* Update section size and contents. */
6503 shdrp
->sh_size
= sec
->size
;
6504 shdrp
->contents
= sec
->contents
;
6505 shdrp
->bfd_section
->contents
= NULL
;
6507 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6514 /* Place section name section after DWARF debug sections have been
6516 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6517 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6518 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6519 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6521 /* Place the section headers. */
6522 i_ehdrp
= elf_elfheader (abfd
);
6523 bed
= get_elf_backend_data (abfd
);
6524 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6525 i_ehdrp
->e_shoff
= off
;
6526 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6527 elf_next_file_pos (abfd
) = off
;
6533 _bfd_elf_write_object_contents (bfd
*abfd
)
6535 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6536 Elf_Internal_Shdr
**i_shdrp
;
6538 unsigned int count
, num_sec
;
6539 struct elf_obj_tdata
*t
;
6541 if (! abfd
->output_has_begun
6542 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6544 /* Do not rewrite ELF data when the BFD has been opened for update.
6545 abfd->output_has_begun was set to TRUE on opening, so creation of new
6546 sections, and modification of existing section sizes was restricted.
6547 This means the ELF header, program headers and section headers can't have
6549 If the contents of any sections has been modified, then those changes have
6550 already been written to the BFD. */
6551 else if (abfd
->direction
== both_direction
)
6553 BFD_ASSERT (abfd
->output_has_begun
);
6557 i_shdrp
= elf_elfsections (abfd
);
6560 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6564 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6567 /* After writing the headers, we need to write the sections too... */
6568 num_sec
= elf_numsections (abfd
);
6569 for (count
= 1; count
< num_sec
; count
++)
6571 i_shdrp
[count
]->sh_name
6572 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6573 i_shdrp
[count
]->sh_name
);
6574 if (bed
->elf_backend_section_processing
)
6575 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6577 if (i_shdrp
[count
]->contents
)
6579 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6581 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6582 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6587 /* Write out the section header names. */
6588 t
= elf_tdata (abfd
);
6589 if (elf_shstrtab (abfd
) != NULL
6590 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6591 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6594 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6597 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6600 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6601 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6602 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6608 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6610 /* Hopefully this can be done just like an object file. */
6611 return _bfd_elf_write_object_contents (abfd
);
6614 /* Given a section, search the header to find them. */
6617 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6619 const struct elf_backend_data
*bed
;
6620 unsigned int sec_index
;
6622 if (elf_section_data (asect
) != NULL
6623 && elf_section_data (asect
)->this_idx
!= 0)
6624 return elf_section_data (asect
)->this_idx
;
6626 if (bfd_is_abs_section (asect
))
6627 sec_index
= SHN_ABS
;
6628 else if (bfd_is_com_section (asect
))
6629 sec_index
= SHN_COMMON
;
6630 else if (bfd_is_und_section (asect
))
6631 sec_index
= SHN_UNDEF
;
6633 sec_index
= SHN_BAD
;
6635 bed
= get_elf_backend_data (abfd
);
6636 if (bed
->elf_backend_section_from_bfd_section
)
6638 int retval
= sec_index
;
6640 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6644 if (sec_index
== SHN_BAD
)
6645 bfd_set_error (bfd_error_nonrepresentable_section
);
6650 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6654 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6656 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6658 flagword flags
= asym_ptr
->flags
;
6660 /* When gas creates relocations against local labels, it creates its
6661 own symbol for the section, but does put the symbol into the
6662 symbol chain, so udata is 0. When the linker is generating
6663 relocatable output, this section symbol may be for one of the
6664 input sections rather than the output section. */
6665 if (asym_ptr
->udata
.i
== 0
6666 && (flags
& BSF_SECTION_SYM
)
6667 && asym_ptr
->section
)
6672 sec
= asym_ptr
->section
;
6673 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6674 sec
= sec
->output_section
;
6675 if (sec
->owner
== abfd
6676 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6677 && elf_section_syms (abfd
)[indx
] != NULL
)
6678 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6681 idx
= asym_ptr
->udata
.i
;
6685 /* This case can occur when using --strip-symbol on a symbol
6686 which is used in a relocation entry. */
6688 /* xgettext:c-format */
6689 (_("%pB: symbol `%s' required but not present"),
6690 abfd
, bfd_asymbol_name (asym_ptr
));
6691 bfd_set_error (bfd_error_no_symbols
);
6698 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6699 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6707 /* Rewrite program header information. */
6710 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6712 Elf_Internal_Ehdr
*iehdr
;
6713 struct elf_segment_map
*map
;
6714 struct elf_segment_map
*map_first
;
6715 struct elf_segment_map
**pointer_to_map
;
6716 Elf_Internal_Phdr
*segment
;
6719 unsigned int num_segments
;
6720 bfd_boolean phdr_included
= FALSE
;
6721 bfd_boolean p_paddr_valid
;
6722 bfd_vma maxpagesize
;
6723 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6724 unsigned int phdr_adjust_num
= 0;
6725 const struct elf_backend_data
*bed
;
6727 bed
= get_elf_backend_data (ibfd
);
6728 iehdr
= elf_elfheader (ibfd
);
6731 pointer_to_map
= &map_first
;
6733 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6734 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6736 /* Returns the end address of the segment + 1. */
6737 #define SEGMENT_END(segment, start) \
6738 (start + (segment->p_memsz > segment->p_filesz \
6739 ? segment->p_memsz : segment->p_filesz))
6741 #define SECTION_SIZE(section, segment) \
6742 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6743 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6744 ? section->size : 0)
6746 /* Returns TRUE if the given section is contained within
6747 the given segment. VMA addresses are compared. */
6748 #define IS_CONTAINED_BY_VMA(section, segment) \
6749 (section->vma >= segment->p_vaddr \
6750 && (section->vma + SECTION_SIZE (section, segment) \
6751 <= (SEGMENT_END (segment, segment->p_vaddr))))
6753 /* Returns TRUE if the given section is contained within
6754 the given segment. LMA addresses are compared. */
6755 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6756 (section->lma >= base \
6757 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6758 && (section->lma + SECTION_SIZE (section, segment) \
6759 <= SEGMENT_END (segment, base)))
6761 /* Handle PT_NOTE segment. */
6762 #define IS_NOTE(p, s) \
6763 (p->p_type == PT_NOTE \
6764 && elf_section_type (s) == SHT_NOTE \
6765 && (bfd_vma) s->filepos >= p->p_offset \
6766 && ((bfd_vma) s->filepos + s->size \
6767 <= p->p_offset + p->p_filesz))
6769 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6771 #define IS_COREFILE_NOTE(p, s) \
6773 && bfd_get_format (ibfd) == bfd_core \
6777 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6778 linker, which generates a PT_INTERP section with p_vaddr and
6779 p_memsz set to 0. */
6780 #define IS_SOLARIS_PT_INTERP(p, s) \
6782 && p->p_paddr == 0 \
6783 && p->p_memsz == 0 \
6784 && p->p_filesz > 0 \
6785 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6787 && (bfd_vma) s->filepos >= p->p_offset \
6788 && ((bfd_vma) s->filepos + s->size \
6789 <= p->p_offset + p->p_filesz))
6791 /* Decide if the given section should be included in the given segment.
6792 A section will be included if:
6793 1. It is within the address space of the segment -- we use the LMA
6794 if that is set for the segment and the VMA otherwise,
6795 2. It is an allocated section or a NOTE section in a PT_NOTE
6797 3. There is an output section associated with it,
6798 4. The section has not already been allocated to a previous segment.
6799 5. PT_GNU_STACK segments do not include any sections.
6800 6. PT_TLS segment includes only SHF_TLS sections.
6801 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6802 8. PT_DYNAMIC should not contain empty sections at the beginning
6803 (with the possible exception of .dynamic). */
6804 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6805 ((((segment->p_paddr \
6806 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6807 : IS_CONTAINED_BY_VMA (section, segment)) \
6808 && (section->flags & SEC_ALLOC) != 0) \
6809 || IS_NOTE (segment, section)) \
6810 && segment->p_type != PT_GNU_STACK \
6811 && (segment->p_type != PT_TLS \
6812 || (section->flags & SEC_THREAD_LOCAL)) \
6813 && (segment->p_type == PT_LOAD \
6814 || segment->p_type == PT_TLS \
6815 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6816 && (segment->p_type != PT_DYNAMIC \
6817 || SECTION_SIZE (section, segment) > 0 \
6818 || (segment->p_paddr \
6819 ? segment->p_paddr != section->lma \
6820 : segment->p_vaddr != section->vma) \
6821 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6823 && (segment->p_type != PT_LOAD || !section->segment_mark))
6825 /* If the output section of a section in the input segment is NULL,
6826 it is removed from the corresponding output segment. */
6827 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6828 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6829 && section->output_section != NULL)
6831 /* Returns TRUE iff seg1 starts after the end of seg2. */
6832 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6833 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6835 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6836 their VMA address ranges and their LMA address ranges overlap.
6837 It is possible to have overlapping VMA ranges without overlapping LMA
6838 ranges. RedBoot images for example can have both .data and .bss mapped
6839 to the same VMA range, but with the .data section mapped to a different
6841 #define SEGMENT_OVERLAPS(seg1, seg2) \
6842 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6843 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6844 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6845 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6847 /* Initialise the segment mark field. */
6848 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6849 section
->segment_mark
= FALSE
;
6851 /* The Solaris linker creates program headers in which all the
6852 p_paddr fields are zero. When we try to objcopy or strip such a
6853 file, we get confused. Check for this case, and if we find it
6854 don't set the p_paddr_valid fields. */
6855 p_paddr_valid
= FALSE
;
6856 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6859 if (segment
->p_paddr
!= 0)
6861 p_paddr_valid
= TRUE
;
6865 /* Scan through the segments specified in the program header
6866 of the input BFD. For this first scan we look for overlaps
6867 in the loadable segments. These can be created by weird
6868 parameters to objcopy. Also, fix some solaris weirdness. */
6869 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6874 Elf_Internal_Phdr
*segment2
;
6876 if (segment
->p_type
== PT_INTERP
)
6877 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6878 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6880 /* Mininal change so that the normal section to segment
6881 assignment code will work. */
6882 segment
->p_vaddr
= section
->vma
;
6886 if (segment
->p_type
!= PT_LOAD
)
6888 /* Remove PT_GNU_RELRO segment. */
6889 if (segment
->p_type
== PT_GNU_RELRO
)
6890 segment
->p_type
= PT_NULL
;
6894 /* Determine if this segment overlaps any previous segments. */
6895 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6897 bfd_signed_vma extra_length
;
6899 if (segment2
->p_type
!= PT_LOAD
6900 || !SEGMENT_OVERLAPS (segment
, segment2
))
6903 /* Merge the two segments together. */
6904 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6906 /* Extend SEGMENT2 to include SEGMENT and then delete
6908 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6909 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6911 if (extra_length
> 0)
6913 segment2
->p_memsz
+= extra_length
;
6914 segment2
->p_filesz
+= extra_length
;
6917 segment
->p_type
= PT_NULL
;
6919 /* Since we have deleted P we must restart the outer loop. */
6921 segment
= elf_tdata (ibfd
)->phdr
;
6926 /* Extend SEGMENT to include SEGMENT2 and then delete
6928 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6929 - SEGMENT_END (segment
, segment
->p_vaddr
));
6931 if (extra_length
> 0)
6933 segment
->p_memsz
+= extra_length
;
6934 segment
->p_filesz
+= extra_length
;
6937 segment2
->p_type
= PT_NULL
;
6942 /* The second scan attempts to assign sections to segments. */
6943 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6947 unsigned int section_count
;
6948 asection
**sections
;
6949 asection
*output_section
;
6951 asection
*matching_lma
;
6952 asection
*suggested_lma
;
6955 asection
*first_section
;
6957 if (segment
->p_type
== PT_NULL
)
6960 first_section
= NULL
;
6961 /* Compute how many sections might be placed into this segment. */
6962 for (section
= ibfd
->sections
, section_count
= 0;
6964 section
= section
->next
)
6966 /* Find the first section in the input segment, which may be
6967 removed from the corresponding output segment. */
6968 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6970 if (first_section
== NULL
)
6971 first_section
= section
;
6972 if (section
->output_section
!= NULL
)
6977 /* Allocate a segment map big enough to contain
6978 all of the sections we have selected. */
6979 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
6980 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
6981 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6985 /* Initialise the fields of the segment map. Default to
6986 using the physical address of the segment in the input BFD. */
6988 map
->p_type
= segment
->p_type
;
6989 map
->p_flags
= segment
->p_flags
;
6990 map
->p_flags_valid
= 1;
6992 /* If the first section in the input segment is removed, there is
6993 no need to preserve segment physical address in the corresponding
6995 if (!first_section
|| first_section
->output_section
!= NULL
)
6997 map
->p_paddr
= segment
->p_paddr
;
6998 map
->p_paddr_valid
= p_paddr_valid
;
7001 /* Determine if this segment contains the ELF file header
7002 and if it contains the program headers themselves. */
7003 map
->includes_filehdr
= (segment
->p_offset
== 0
7004 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7005 map
->includes_phdrs
= 0;
7007 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7009 map
->includes_phdrs
=
7010 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7011 && (segment
->p_offset
+ segment
->p_filesz
7012 >= ((bfd_vma
) iehdr
->e_phoff
7013 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7015 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7016 phdr_included
= TRUE
;
7019 if (section_count
== 0)
7021 /* Special segments, such as the PT_PHDR segment, may contain
7022 no sections, but ordinary, loadable segments should contain
7023 something. They are allowed by the ELF spec however, so only
7024 a warning is produced.
7025 There is however the valid use case of embedded systems which
7026 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7027 flash memory with zeros. No warning is shown for that case. */
7028 if (segment
->p_type
== PT_LOAD
7029 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7030 /* xgettext:c-format */
7032 (_("%pB: warning: empty loadable segment detected"
7033 " at vaddr=%#" PRIx64
", is this intentional?"),
7034 ibfd
, (uint64_t) segment
->p_vaddr
);
7036 map
->p_vaddr_offset
= segment
->p_vaddr
;
7038 *pointer_to_map
= map
;
7039 pointer_to_map
= &map
->next
;
7044 /* Now scan the sections in the input BFD again and attempt
7045 to add their corresponding output sections to the segment map.
7046 The problem here is how to handle an output section which has
7047 been moved (ie had its LMA changed). There are four possibilities:
7049 1. None of the sections have been moved.
7050 In this case we can continue to use the segment LMA from the
7053 2. All of the sections have been moved by the same amount.
7054 In this case we can change the segment's LMA to match the LMA
7055 of the first section.
7057 3. Some of the sections have been moved, others have not.
7058 In this case those sections which have not been moved can be
7059 placed in the current segment which will have to have its size,
7060 and possibly its LMA changed, and a new segment or segments will
7061 have to be created to contain the other sections.
7063 4. The sections have been moved, but not by the same amount.
7064 In this case we can change the segment's LMA to match the LMA
7065 of the first section and we will have to create a new segment
7066 or segments to contain the other sections.
7068 In order to save time, we allocate an array to hold the section
7069 pointers that we are interested in. As these sections get assigned
7070 to a segment, they are removed from this array. */
7072 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
7073 if (sections
== NULL
)
7076 /* Step One: Scan for segment vs section LMA conflicts.
7077 Also add the sections to the section array allocated above.
7078 Also add the sections to the current segment. In the common
7079 case, where the sections have not been moved, this means that
7080 we have completely filled the segment, and there is nothing
7083 matching_lma
= NULL
;
7084 suggested_lma
= NULL
;
7086 for (section
= first_section
, j
= 0;
7088 section
= section
->next
)
7090 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
7092 output_section
= section
->output_section
;
7094 sections
[j
++] = section
;
7096 /* The Solaris native linker always sets p_paddr to 0.
7097 We try to catch that case here, and set it to the
7098 correct value. Note - some backends require that
7099 p_paddr be left as zero. */
7101 && segment
->p_vaddr
!= 0
7102 && !bed
->want_p_paddr_set_to_zero
7104 && output_section
->lma
!= 0
7105 && (align_power (segment
->p_vaddr
7106 + (map
->includes_filehdr
7107 ? iehdr
->e_ehsize
: 0)
7108 + (map
->includes_phdrs
7109 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7111 output_section
->alignment_power
)
7112 == output_section
->vma
))
7113 map
->p_paddr
= segment
->p_vaddr
;
7115 /* Match up the physical address of the segment with the
7116 LMA address of the output section. */
7117 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7118 || IS_COREFILE_NOTE (segment
, section
)
7119 || (bed
->want_p_paddr_set_to_zero
7120 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
7122 if (matching_lma
== NULL
7123 || output_section
->lma
< matching_lma
->lma
)
7124 matching_lma
= output_section
;
7126 /* We assume that if the section fits within the segment
7127 then it does not overlap any other section within that
7129 map
->sections
[isec
++] = output_section
;
7131 else if (suggested_lma
== NULL
)
7132 suggested_lma
= output_section
;
7134 if (j
== section_count
)
7139 BFD_ASSERT (j
== section_count
);
7141 /* Step Two: Adjust the physical address of the current segment,
7143 if (isec
== section_count
)
7145 /* All of the sections fitted within the segment as currently
7146 specified. This is the default case. Add the segment to
7147 the list of built segments and carry on to process the next
7148 program header in the input BFD. */
7149 map
->count
= section_count
;
7150 *pointer_to_map
= map
;
7151 pointer_to_map
= &map
->next
;
7154 && !bed
->want_p_paddr_set_to_zero
7155 && matching_lma
->lma
!= map
->p_paddr
7156 && !map
->includes_filehdr
7157 && !map
->includes_phdrs
)
7158 /* There is some padding before the first section in the
7159 segment. So, we must account for that in the output
7161 map
->p_vaddr_offset
= map
->p_paddr
- matching_lma
->lma
;
7168 /* Change the current segment's physical address to match
7169 the LMA of the first section that fitted, or if no
7170 section fitted, the first section. */
7171 if (matching_lma
== NULL
)
7172 matching_lma
= suggested_lma
;
7174 map
->p_paddr
= matching_lma
->lma
;
7176 /* Offset the segment physical address from the lma
7177 to allow for space taken up by elf headers. */
7178 if (map
->includes_phdrs
)
7180 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7182 /* iehdr->e_phnum is just an estimate of the number
7183 of program headers that we will need. Make a note
7184 here of the number we used and the segment we chose
7185 to hold these headers, so that we can adjust the
7186 offset when we know the correct value. */
7187 phdr_adjust_num
= iehdr
->e_phnum
;
7188 phdr_adjust_seg
= map
;
7191 if (map
->includes_filehdr
)
7193 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7194 map
->p_paddr
-= iehdr
->e_ehsize
;
7195 /* We've subtracted off the size of headers from the
7196 first section lma, but there may have been some
7197 alignment padding before that section too. Try to
7198 account for that by adjusting the segment lma down to
7199 the same alignment. */
7200 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7201 align
= segment
->p_align
;
7202 map
->p_paddr
&= -align
;
7206 /* Step Three: Loop over the sections again, this time assigning
7207 those that fit to the current segment and removing them from the
7208 sections array; but making sure not to leave large gaps. Once all
7209 possible sections have been assigned to the current segment it is
7210 added to the list of built segments and if sections still remain
7211 to be assigned, a new segment is constructed before repeating
7217 suggested_lma
= NULL
;
7219 /* Fill the current segment with sections that fit. */
7220 for (j
= 0; j
< section_count
; j
++)
7222 section
= sections
[j
];
7224 if (section
== NULL
)
7227 output_section
= section
->output_section
;
7229 BFD_ASSERT (output_section
!= NULL
);
7231 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7232 || IS_COREFILE_NOTE (segment
, section
))
7234 if (map
->count
== 0)
7236 /* If the first section in a segment does not start at
7237 the beginning of the segment, then something is
7239 if (align_power (map
->p_paddr
7240 + (map
->includes_filehdr
7241 ? iehdr
->e_ehsize
: 0)
7242 + (map
->includes_phdrs
7243 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7245 output_section
->alignment_power
)
7246 != output_section
->lma
)
7253 prev_sec
= map
->sections
[map
->count
- 1];
7255 /* If the gap between the end of the previous section
7256 and the start of this section is more than
7257 maxpagesize then we need to start a new segment. */
7258 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7260 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7261 || (prev_sec
->lma
+ prev_sec
->size
7262 > output_section
->lma
))
7264 if (suggested_lma
== NULL
)
7265 suggested_lma
= output_section
;
7271 map
->sections
[map
->count
++] = output_section
;
7274 if (segment
->p_type
== PT_LOAD
)
7275 section
->segment_mark
= TRUE
;
7277 else if (suggested_lma
== NULL
)
7278 suggested_lma
= output_section
;
7281 /* PR 23932. A corrupt input file may contain sections that cannot
7282 be assigned to any segment - because for example they have a
7283 negative size - or segments that do not contain any sections. */
7284 if (map
->count
== 0)
7286 bfd_set_error (bfd_error_bad_value
);
7291 /* Add the current segment to the list of built segments. */
7292 *pointer_to_map
= map
;
7293 pointer_to_map
= &map
->next
;
7295 if (isec
< section_count
)
7297 /* We still have not allocated all of the sections to
7298 segments. Create a new segment here, initialise it
7299 and carry on looping. */
7300 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7301 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
7302 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7309 /* Initialise the fields of the segment map. Set the physical
7310 physical address to the LMA of the first section that has
7311 not yet been assigned. */
7313 map
->p_type
= segment
->p_type
;
7314 map
->p_flags
= segment
->p_flags
;
7315 map
->p_flags_valid
= 1;
7316 map
->p_paddr
= suggested_lma
->lma
;
7317 map
->p_paddr_valid
= p_paddr_valid
;
7318 map
->includes_filehdr
= 0;
7319 map
->includes_phdrs
= 0;
7322 while (isec
< section_count
);
7327 elf_seg_map (obfd
) = map_first
;
7329 /* If we had to estimate the number of program headers that were
7330 going to be needed, then check our estimate now and adjust
7331 the offset if necessary. */
7332 if (phdr_adjust_seg
!= NULL
)
7336 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7339 if (count
> phdr_adjust_num
)
7340 phdr_adjust_seg
->p_paddr
7341 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7343 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7344 if (map
->p_type
== PT_PHDR
)
7347 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7348 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7355 #undef IS_CONTAINED_BY_VMA
7356 #undef IS_CONTAINED_BY_LMA
7358 #undef IS_COREFILE_NOTE
7359 #undef IS_SOLARIS_PT_INTERP
7360 #undef IS_SECTION_IN_INPUT_SEGMENT
7361 #undef INCLUDE_SECTION_IN_SEGMENT
7362 #undef SEGMENT_AFTER_SEGMENT
7363 #undef SEGMENT_OVERLAPS
7367 /* Copy ELF program header information. */
7370 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7372 Elf_Internal_Ehdr
*iehdr
;
7373 struct elf_segment_map
*map
;
7374 struct elf_segment_map
*map_first
;
7375 struct elf_segment_map
**pointer_to_map
;
7376 Elf_Internal_Phdr
*segment
;
7378 unsigned int num_segments
;
7379 bfd_boolean phdr_included
= FALSE
;
7380 bfd_boolean p_paddr_valid
;
7382 iehdr
= elf_elfheader (ibfd
);
7385 pointer_to_map
= &map_first
;
7387 /* If all the segment p_paddr fields are zero, don't set
7388 map->p_paddr_valid. */
7389 p_paddr_valid
= FALSE
;
7390 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7391 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7394 if (segment
->p_paddr
!= 0)
7396 p_paddr_valid
= TRUE
;
7400 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7405 unsigned int section_count
;
7407 Elf_Internal_Shdr
*this_hdr
;
7408 asection
*first_section
= NULL
;
7409 asection
*lowest_section
;
7410 bfd_boolean no_contents
= TRUE
;
7412 /* Compute how many sections are in this segment. */
7413 for (section
= ibfd
->sections
, section_count
= 0;
7415 section
= section
->next
)
7417 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7418 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7420 if (first_section
== NULL
)
7421 first_section
= section
;
7422 if (elf_section_type (section
) != SHT_NOBITS
)
7423 no_contents
= FALSE
;
7428 /* Allocate a segment map big enough to contain
7429 all of the sections we have selected. */
7430 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7431 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
7432 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7436 /* Initialize the fields of the output segment map with the
7439 map
->p_type
= segment
->p_type
;
7440 map
->p_flags
= segment
->p_flags
;
7441 map
->p_flags_valid
= 1;
7442 map
->p_paddr
= segment
->p_paddr
;
7443 map
->p_paddr_valid
= p_paddr_valid
;
7444 map
->p_align
= segment
->p_align
;
7445 map
->p_align_valid
= 1;
7446 map
->p_vaddr_offset
= 0;
7448 if (map
->p_type
== PT_GNU_RELRO
7449 || map
->p_type
== PT_GNU_STACK
)
7451 /* The PT_GNU_RELRO segment may contain the first a few
7452 bytes in the .got.plt section even if the whole .got.plt
7453 section isn't in the PT_GNU_RELRO segment. We won't
7454 change the size of the PT_GNU_RELRO segment.
7455 Similarly, PT_GNU_STACK size is significant on uclinux
7457 map
->p_size
= segment
->p_memsz
;
7458 map
->p_size_valid
= 1;
7461 /* Determine if this segment contains the ELF file header
7462 and if it contains the program headers themselves. */
7463 map
->includes_filehdr
= (segment
->p_offset
== 0
7464 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7466 map
->includes_phdrs
= 0;
7467 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7469 map
->includes_phdrs
=
7470 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7471 && (segment
->p_offset
+ segment
->p_filesz
7472 >= ((bfd_vma
) iehdr
->e_phoff
7473 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7475 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7476 phdr_included
= TRUE
;
7479 lowest_section
= NULL
;
7480 if (section_count
!= 0)
7482 unsigned int isec
= 0;
7484 for (section
= first_section
;
7486 section
= section
->next
)
7488 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7489 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7491 map
->sections
[isec
++] = section
->output_section
;
7492 if ((section
->flags
& SEC_ALLOC
) != 0)
7496 if (lowest_section
== NULL
7497 || section
->lma
< lowest_section
->lma
)
7498 lowest_section
= section
;
7500 /* Section lmas are set up from PT_LOAD header
7501 p_paddr in _bfd_elf_make_section_from_shdr.
7502 If this header has a p_paddr that disagrees
7503 with the section lma, flag the p_paddr as
7505 if ((section
->flags
& SEC_LOAD
) != 0)
7506 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7508 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7509 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7510 map
->p_paddr_valid
= FALSE
;
7512 if (isec
== section_count
)
7518 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7520 /* Try to keep the space used by the headers plus any
7521 padding fixed. If there are sections with file contents
7522 in this segment then the lowest sh_offset is the best
7523 guess. Otherwise the segment only has file contents for
7524 the headers, and p_filesz is the best guess. */
7526 map
->header_size
= segment
->p_filesz
;
7528 map
->header_size
= lowest_section
->filepos
;
7531 if (section_count
== 0)
7532 map
->p_vaddr_offset
= segment
->p_vaddr
;
7533 else if (!map
->includes_phdrs
7534 && !map
->includes_filehdr
7535 && map
->p_paddr_valid
)
7536 /* Account for padding before the first section. */
7537 map
->p_vaddr_offset
= (segment
->p_paddr
7538 - (lowest_section
? lowest_section
->lma
: 0));
7540 map
->count
= section_count
;
7541 *pointer_to_map
= map
;
7542 pointer_to_map
= &map
->next
;
7545 elf_seg_map (obfd
) = map_first
;
7549 /* Copy private BFD data. This copies or rewrites ELF program header
7553 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7555 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7556 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7559 if (elf_tdata (ibfd
)->phdr
== NULL
)
7562 if (ibfd
->xvec
== obfd
->xvec
)
7564 /* Check to see if any sections in the input BFD
7565 covered by ELF program header have changed. */
7566 Elf_Internal_Phdr
*segment
;
7567 asection
*section
, *osec
;
7568 unsigned int i
, num_segments
;
7569 Elf_Internal_Shdr
*this_hdr
;
7570 const struct elf_backend_data
*bed
;
7572 bed
= get_elf_backend_data (ibfd
);
7574 /* Regenerate the segment map if p_paddr is set to 0. */
7575 if (bed
->want_p_paddr_set_to_zero
)
7578 /* Initialize the segment mark field. */
7579 for (section
= obfd
->sections
; section
!= NULL
;
7580 section
= section
->next
)
7581 section
->segment_mark
= FALSE
;
7583 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7584 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7588 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7589 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7590 which severly confuses things, so always regenerate the segment
7591 map in this case. */
7592 if (segment
->p_paddr
== 0
7593 && segment
->p_memsz
== 0
7594 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7597 for (section
= ibfd
->sections
;
7598 section
!= NULL
; section
= section
->next
)
7600 /* We mark the output section so that we know it comes
7601 from the input BFD. */
7602 osec
= section
->output_section
;
7604 osec
->segment_mark
= TRUE
;
7606 /* Check if this section is covered by the segment. */
7607 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7608 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7610 /* FIXME: Check if its output section is changed or
7611 removed. What else do we need to check? */
7613 || section
->flags
!= osec
->flags
7614 || section
->lma
!= osec
->lma
7615 || section
->vma
!= osec
->vma
7616 || section
->size
!= osec
->size
7617 || section
->rawsize
!= osec
->rawsize
7618 || section
->alignment_power
!= osec
->alignment_power
)
7624 /* Check to see if any output section do not come from the
7626 for (section
= obfd
->sections
; section
!= NULL
;
7627 section
= section
->next
)
7629 if (!section
->segment_mark
)
7632 section
->segment_mark
= FALSE
;
7635 return copy_elf_program_header (ibfd
, obfd
);
7639 if (ibfd
->xvec
== obfd
->xvec
)
7641 /* When rewriting program header, set the output maxpagesize to
7642 the maximum alignment of input PT_LOAD segments. */
7643 Elf_Internal_Phdr
*segment
;
7645 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7646 bfd_vma maxpagesize
= 0;
7648 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7651 if (segment
->p_type
== PT_LOAD
7652 && maxpagesize
< segment
->p_align
)
7654 /* PR 17512: file: f17299af. */
7655 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7656 /* xgettext:c-format */
7657 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7658 PRIx64
" is too large"),
7659 ibfd
, (uint64_t) segment
->p_align
);
7661 maxpagesize
= segment
->p_align
;
7664 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7665 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7668 return rewrite_elf_program_header (ibfd
, obfd
);
7671 /* Initialize private output section information from input section. */
7674 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7678 struct bfd_link_info
*link_info
)
7681 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7682 bfd_boolean final_link
= (link_info
!= NULL
7683 && !bfd_link_relocatable (link_info
));
7685 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7686 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7689 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7691 /* For objcopy and relocatable link, don't copy the output ELF
7692 section type from input if the output BFD section flags have been
7693 set to something different. For a final link allow some flags
7694 that the linker clears to differ. */
7695 if (elf_section_type (osec
) == SHT_NULL
7696 && (osec
->flags
== isec
->flags
7698 && ((osec
->flags
^ isec
->flags
)
7699 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7700 elf_section_type (osec
) = elf_section_type (isec
);
7702 /* FIXME: Is this correct for all OS/PROC specific flags? */
7703 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7704 & (SHF_MASKOS
| SHF_MASKPROC
));
7706 /* Copy sh_info from input for mbind section. */
7707 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7708 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7709 elf_section_data (osec
)->this_hdr
.sh_info
7710 = elf_section_data (isec
)->this_hdr
.sh_info
;
7712 /* Set things up for objcopy and relocatable link. The output
7713 SHT_GROUP section will have its elf_next_in_group pointing back
7714 to the input group members. Ignore linker created group section.
7715 See elfNN_ia64_object_p in elfxx-ia64.c. */
7716 if ((link_info
== NULL
7717 || !link_info
->resolve_section_groups
)
7718 && (elf_sec_group (isec
) == NULL
7719 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7721 if (elf_section_flags (isec
) & SHF_GROUP
)
7722 elf_section_flags (osec
) |= SHF_GROUP
;
7723 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7724 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7727 /* If not decompress, preserve SHF_COMPRESSED. */
7728 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7729 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7732 ihdr
= &elf_section_data (isec
)->this_hdr
;
7734 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7735 don't use the output section of the linked-to section since it
7736 may be NULL at this point. */
7737 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7739 ohdr
= &elf_section_data (osec
)->this_hdr
;
7740 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7741 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7744 osec
->use_rela_p
= isec
->use_rela_p
;
7749 /* Copy private section information. This copies over the entsize
7750 field, and sometimes the info field. */
7753 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7758 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7760 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7761 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7764 ihdr
= &elf_section_data (isec
)->this_hdr
;
7765 ohdr
= &elf_section_data (osec
)->this_hdr
;
7767 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7769 if (ihdr
->sh_type
== SHT_SYMTAB
7770 || ihdr
->sh_type
== SHT_DYNSYM
7771 || ihdr
->sh_type
== SHT_GNU_verneed
7772 || ihdr
->sh_type
== SHT_GNU_verdef
)
7773 ohdr
->sh_info
= ihdr
->sh_info
;
7775 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7779 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7780 necessary if we are removing either the SHT_GROUP section or any of
7781 the group member sections. DISCARDED is the value that a section's
7782 output_section has if the section will be discarded, NULL when this
7783 function is called from objcopy, bfd_abs_section_ptr when called
7787 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7791 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7792 if (elf_section_type (isec
) == SHT_GROUP
)
7794 asection
*first
= elf_next_in_group (isec
);
7795 asection
*s
= first
;
7796 bfd_size_type removed
= 0;
7800 /* If this member section is being output but the
7801 SHT_GROUP section is not, then clear the group info
7802 set up by _bfd_elf_copy_private_section_data. */
7803 if (s
->output_section
!= discarded
7804 && isec
->output_section
== discarded
)
7806 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7807 elf_group_name (s
->output_section
) = NULL
;
7809 /* Conversely, if the member section is not being output
7810 but the SHT_GROUP section is, then adjust its size. */
7811 else if (s
->output_section
== discarded
7812 && isec
->output_section
!= discarded
)
7814 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7816 if (elf_sec
->rel
.hdr
!= NULL
7817 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7819 if (elf_sec
->rela
.hdr
!= NULL
7820 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7823 s
= elf_next_in_group (s
);
7829 if (discarded
!= NULL
)
7831 /* If we've been called for ld -r, then we need to
7832 adjust the input section size. */
7833 if (isec
->rawsize
== 0)
7834 isec
->rawsize
= isec
->size
;
7835 isec
->size
= isec
->rawsize
- removed
;
7836 if (isec
->size
<= 4)
7839 isec
->flags
|= SEC_EXCLUDE
;
7844 /* Adjust the output section size when called from
7846 isec
->output_section
->size
-= removed
;
7847 if (isec
->output_section
->size
<= 4)
7849 isec
->output_section
->size
= 0;
7850 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7859 /* Copy private header information. */
7862 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7864 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7865 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7868 /* Copy over private BFD data if it has not already been copied.
7869 This must be done here, rather than in the copy_private_bfd_data
7870 entry point, because the latter is called after the section
7871 contents have been set, which means that the program headers have
7872 already been worked out. */
7873 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7875 if (! copy_private_bfd_data (ibfd
, obfd
))
7879 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7882 /* Copy private symbol information. If this symbol is in a section
7883 which we did not map into a BFD section, try to map the section
7884 index correctly. We use special macro definitions for the mapped
7885 section indices; these definitions are interpreted by the
7886 swap_out_syms function. */
7888 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7889 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7890 #define MAP_STRTAB (SHN_HIOS + 3)
7891 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7892 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7895 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7900 elf_symbol_type
*isym
, *osym
;
7902 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7903 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7906 isym
= elf_symbol_from (ibfd
, isymarg
);
7907 osym
= elf_symbol_from (obfd
, osymarg
);
7910 && isym
->internal_elf_sym
.st_shndx
!= 0
7912 && bfd_is_abs_section (isym
->symbol
.section
))
7916 shndx
= isym
->internal_elf_sym
.st_shndx
;
7917 if (shndx
== elf_onesymtab (ibfd
))
7918 shndx
= MAP_ONESYMTAB
;
7919 else if (shndx
== elf_dynsymtab (ibfd
))
7920 shndx
= MAP_DYNSYMTAB
;
7921 else if (shndx
== elf_strtab_sec (ibfd
))
7923 else if (shndx
== elf_shstrtab_sec (ibfd
))
7924 shndx
= MAP_SHSTRTAB
;
7925 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7926 shndx
= MAP_SYM_SHNDX
;
7927 osym
->internal_elf_sym
.st_shndx
= shndx
;
7933 /* Swap out the symbols. */
7936 swap_out_syms (bfd
*abfd
,
7937 struct elf_strtab_hash
**sttp
,
7940 const struct elf_backend_data
*bed
;
7943 struct elf_strtab_hash
*stt
;
7944 Elf_Internal_Shdr
*symtab_hdr
;
7945 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7946 Elf_Internal_Shdr
*symstrtab_hdr
;
7947 struct elf_sym_strtab
*symstrtab
;
7948 bfd_byte
*outbound_syms
;
7949 bfd_byte
*outbound_shndx
;
7950 unsigned long outbound_syms_index
;
7951 unsigned long outbound_shndx_index
;
7953 unsigned int num_locals
;
7955 bfd_boolean name_local_sections
;
7957 if (!elf_map_symbols (abfd
, &num_locals
))
7960 /* Dump out the symtabs. */
7961 stt
= _bfd_elf_strtab_init ();
7965 bed
= get_elf_backend_data (abfd
);
7966 symcount
= bfd_get_symcount (abfd
);
7967 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7968 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7969 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7970 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7971 symtab_hdr
->sh_info
= num_locals
+ 1;
7972 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7974 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7975 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7977 /* Allocate buffer to swap out the .strtab section. */
7978 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc2 (symcount
+ 1,
7979 sizeof (*symstrtab
));
7980 if (symstrtab
== NULL
)
7982 _bfd_elf_strtab_free (stt
);
7986 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7987 bed
->s
->sizeof_sym
);
7988 if (outbound_syms
== NULL
)
7991 _bfd_elf_strtab_free (stt
);
7995 symtab_hdr
->contents
= outbound_syms
;
7996 outbound_syms_index
= 0;
7998 outbound_shndx
= NULL
;
7999 outbound_shndx_index
= 0;
8001 if (elf_symtab_shndx_list (abfd
))
8003 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8004 if (symtab_shndx_hdr
->sh_name
!= 0)
8006 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
8007 outbound_shndx
= (bfd_byte
*)
8008 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
8009 if (outbound_shndx
== NULL
)
8012 symtab_shndx_hdr
->contents
= outbound_shndx
;
8013 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8014 symtab_shndx_hdr
->sh_size
= amt
;
8015 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8016 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8018 /* FIXME: What about any other headers in the list ? */
8021 /* Now generate the data (for "contents"). */
8023 /* Fill in zeroth symbol and swap it out. */
8024 Elf_Internal_Sym sym
;
8030 sym
.st_shndx
= SHN_UNDEF
;
8031 sym
.st_target_internal
= 0;
8032 symstrtab
[0].sym
= sym
;
8033 symstrtab
[0].dest_index
= outbound_syms_index
;
8034 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8035 outbound_syms_index
++;
8036 if (outbound_shndx
!= NULL
)
8037 outbound_shndx_index
++;
8041 = (bed
->elf_backend_name_local_section_symbols
8042 && bed
->elf_backend_name_local_section_symbols (abfd
));
8044 syms
= bfd_get_outsymbols (abfd
);
8045 for (idx
= 0; idx
< symcount
;)
8047 Elf_Internal_Sym sym
;
8048 bfd_vma value
= syms
[idx
]->value
;
8049 elf_symbol_type
*type_ptr
;
8050 flagword flags
= syms
[idx
]->flags
;
8053 if (!name_local_sections
8054 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8056 /* Local section symbols have no name. */
8057 sym
.st_name
= (unsigned long) -1;
8061 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8062 to get the final offset for st_name. */
8064 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8066 if (sym
.st_name
== (unsigned long) -1)
8070 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
8072 if ((flags
& BSF_SECTION_SYM
) == 0
8073 && bfd_is_com_section (syms
[idx
]->section
))
8075 /* ELF common symbols put the alignment into the `value' field,
8076 and the size into the `size' field. This is backwards from
8077 how BFD handles it, so reverse it here. */
8078 sym
.st_size
= value
;
8079 if (type_ptr
== NULL
8080 || type_ptr
->internal_elf_sym
.st_value
== 0)
8081 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8083 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8084 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8085 (abfd
, syms
[idx
]->section
);
8089 asection
*sec
= syms
[idx
]->section
;
8092 if (sec
->output_section
)
8094 value
+= sec
->output_offset
;
8095 sec
= sec
->output_section
;
8098 /* Don't add in the section vma for relocatable output. */
8099 if (! relocatable_p
)
8101 sym
.st_value
= value
;
8102 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8104 if (bfd_is_abs_section (sec
)
8106 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8108 /* This symbol is in a real ELF section which we did
8109 not create as a BFD section. Undo the mapping done
8110 by copy_private_symbol_data. */
8111 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8115 shndx
= elf_onesymtab (abfd
);
8118 shndx
= elf_dynsymtab (abfd
);
8121 shndx
= elf_strtab_sec (abfd
);
8124 shndx
= elf_shstrtab_sec (abfd
);
8127 if (elf_symtab_shndx_list (abfd
))
8128 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8137 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8139 if (shndx
== SHN_BAD
)
8143 /* Writing this would be a hell of a lot easier if
8144 we had some decent documentation on bfd, and
8145 knew what to expect of the library, and what to
8146 demand of applications. For example, it
8147 appears that `objcopy' might not set the
8148 section of a symbol to be a section that is
8149 actually in the output file. */
8150 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8152 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8153 if (shndx
== SHN_BAD
)
8155 /* xgettext:c-format */
8157 (_("unable to find equivalent output section"
8158 " for symbol '%s' from section '%s'"),
8159 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8161 bfd_set_error (bfd_error_invalid_operation
);
8167 sym
.st_shndx
= shndx
;
8170 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8172 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8173 type
= STT_GNU_IFUNC
;
8174 else if ((flags
& BSF_FUNCTION
) != 0)
8176 else if ((flags
& BSF_OBJECT
) != 0)
8178 else if ((flags
& BSF_RELC
) != 0)
8180 else if ((flags
& BSF_SRELC
) != 0)
8185 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8188 /* Processor-specific types. */
8189 if (type_ptr
!= NULL
8190 && bed
->elf_backend_get_symbol_type
)
8191 type
= ((*bed
->elf_backend_get_symbol_type
)
8192 (&type_ptr
->internal_elf_sym
, type
));
8194 if (flags
& BSF_SECTION_SYM
)
8196 if (flags
& BSF_GLOBAL
)
8197 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8199 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8201 else if (bfd_is_com_section (syms
[idx
]->section
))
8203 if (type
!= STT_TLS
)
8205 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8206 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8207 ? STT_COMMON
: STT_OBJECT
);
8209 type
= ((flags
& BSF_ELF_COMMON
) != 0
8210 ? STT_COMMON
: STT_OBJECT
);
8212 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8214 else if (bfd_is_und_section (syms
[idx
]->section
))
8215 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8219 else if (flags
& BSF_FILE
)
8220 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8223 int bind
= STB_LOCAL
;
8225 if (flags
& BSF_LOCAL
)
8227 else if (flags
& BSF_GNU_UNIQUE
)
8228 bind
= STB_GNU_UNIQUE
;
8229 else if (flags
& BSF_WEAK
)
8231 else if (flags
& BSF_GLOBAL
)
8234 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8237 if (type_ptr
!= NULL
)
8239 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8240 sym
.st_target_internal
8241 = type_ptr
->internal_elf_sym
.st_target_internal
;
8246 sym
.st_target_internal
= 0;
8250 symstrtab
[idx
].sym
= sym
;
8251 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8252 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8254 outbound_syms_index
++;
8255 if (outbound_shndx
!= NULL
)
8256 outbound_shndx_index
++;
8259 /* Finalize the .strtab section. */
8260 _bfd_elf_strtab_finalize (stt
);
8262 /* Swap out the .strtab section. */
8263 for (idx
= 0; idx
<= symcount
; idx
++)
8265 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8266 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8267 elfsym
->sym
.st_name
= 0;
8269 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8270 elfsym
->sym
.st_name
);
8271 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8273 + (elfsym
->dest_index
8274 * bed
->s
->sizeof_sym
)),
8276 + (elfsym
->destshndx_index
8277 * sizeof (Elf_External_Sym_Shndx
))));
8282 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8283 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8284 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8285 symstrtab_hdr
->sh_addr
= 0;
8286 symstrtab_hdr
->sh_entsize
= 0;
8287 symstrtab_hdr
->sh_link
= 0;
8288 symstrtab_hdr
->sh_info
= 0;
8289 symstrtab_hdr
->sh_addralign
= 1;
8294 /* Return the number of bytes required to hold the symtab vector.
8296 Note that we base it on the count plus 1, since we will null terminate
8297 the vector allocated based on this size. However, the ELF symbol table
8298 always has a dummy entry as symbol #0, so it ends up even. */
8301 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8303 bfd_size_type symcount
;
8305 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8307 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8308 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8310 bfd_set_error (bfd_error_file_too_big
);
8313 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8315 symtab_size
-= sizeof (asymbol
*);
8321 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8323 bfd_size_type symcount
;
8325 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8327 if (elf_dynsymtab (abfd
) == 0)
8329 bfd_set_error (bfd_error_invalid_operation
);
8333 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8334 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8336 bfd_set_error (bfd_error_file_too_big
);
8339 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8341 symtab_size
-= sizeof (asymbol
*);
8347 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8350 #if SIZEOF_LONG == SIZEOF_INT
8351 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8353 bfd_set_error (bfd_error_file_too_big
);
8357 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8360 /* Canonicalize the relocs. */
8363 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8370 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8372 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8375 tblptr
= section
->relocation
;
8376 for (i
= 0; i
< section
->reloc_count
; i
++)
8377 *relptr
++ = tblptr
++;
8381 return section
->reloc_count
;
8385 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8387 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8388 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8391 bfd_get_symcount (abfd
) = symcount
;
8396 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8397 asymbol
**allocation
)
8399 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8400 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8403 bfd_get_dynamic_symcount (abfd
) = symcount
;
8407 /* Return the size required for the dynamic reloc entries. Any loadable
8408 section that was actually installed in the BFD, and has type SHT_REL
8409 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8410 dynamic reloc section. */
8413 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8415 bfd_size_type count
;
8418 if (elf_dynsymtab (abfd
) == 0)
8420 bfd_set_error (bfd_error_invalid_operation
);
8425 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8426 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8427 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8428 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8430 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8431 if (count
> LONG_MAX
/ sizeof (arelent
*))
8433 bfd_set_error (bfd_error_file_too_big
);
8437 return count
* sizeof (arelent
*);
8440 /* Canonicalize the dynamic relocation entries. Note that we return the
8441 dynamic relocations as a single block, although they are actually
8442 associated with particular sections; the interface, which was
8443 designed for SunOS style shared libraries, expects that there is only
8444 one set of dynamic relocs. Any loadable section that was actually
8445 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8446 dynamic symbol table, is considered to be a dynamic reloc section. */
8449 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8453 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8457 if (elf_dynsymtab (abfd
) == 0)
8459 bfd_set_error (bfd_error_invalid_operation
);
8463 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8465 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8467 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8468 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8469 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8474 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8476 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8478 for (i
= 0; i
< count
; i
++)
8489 /* Read in the version information. */
8492 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8494 bfd_byte
*contents
= NULL
;
8495 unsigned int freeidx
= 0;
8497 if (elf_dynverref (abfd
) != 0)
8499 Elf_Internal_Shdr
*hdr
;
8500 Elf_External_Verneed
*everneed
;
8501 Elf_Internal_Verneed
*iverneed
;
8503 bfd_byte
*contents_end
;
8505 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8507 if (hdr
->sh_info
== 0
8508 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8510 error_return_bad_verref
:
8512 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8513 bfd_set_error (bfd_error_bad_value
);
8514 error_return_verref
:
8515 elf_tdata (abfd
)->verref
= NULL
;
8516 elf_tdata (abfd
)->cverrefs
= 0;
8520 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8521 if (filesize
> 0 && filesize
< hdr
->sh_size
)
8523 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8525 bfd_set_error (bfd_error_no_memory
);
8527 /* xgettext:c-format */
8528 (_("error: %pB version reference section is too large (%#" PRIx64
" bytes)"),
8529 abfd
, (uint64_t) hdr
->sh_size
);
8530 goto error_return_verref
;
8532 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8533 if (contents
== NULL
)
8534 goto error_return_verref
;
8536 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8537 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8538 goto error_return_verref
;
8540 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8541 bfd_alloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8543 if (elf_tdata (abfd
)->verref
== NULL
)
8544 goto error_return_verref
;
8546 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8547 == sizeof (Elf_External_Vernaux
));
8548 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8549 everneed
= (Elf_External_Verneed
*) contents
;
8550 iverneed
= elf_tdata (abfd
)->verref
;
8551 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8553 Elf_External_Vernaux
*evernaux
;
8554 Elf_Internal_Vernaux
*ivernaux
;
8557 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8559 iverneed
->vn_bfd
= abfd
;
8561 iverneed
->vn_filename
=
8562 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8564 if (iverneed
->vn_filename
== NULL
)
8565 goto error_return_bad_verref
;
8567 if (iverneed
->vn_cnt
== 0)
8568 iverneed
->vn_auxptr
= NULL
;
8571 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8572 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8573 sizeof (Elf_Internal_Vernaux
));
8574 if (iverneed
->vn_auxptr
== NULL
)
8575 goto error_return_verref
;
8578 if (iverneed
->vn_aux
8579 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8580 goto error_return_bad_verref
;
8582 evernaux
= ((Elf_External_Vernaux
*)
8583 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8584 ivernaux
= iverneed
->vn_auxptr
;
8585 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8587 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8589 ivernaux
->vna_nodename
=
8590 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8591 ivernaux
->vna_name
);
8592 if (ivernaux
->vna_nodename
== NULL
)
8593 goto error_return_bad_verref
;
8595 if (ivernaux
->vna_other
> freeidx
)
8596 freeidx
= ivernaux
->vna_other
;
8598 ivernaux
->vna_nextptr
= NULL
;
8599 if (ivernaux
->vna_next
== 0)
8601 iverneed
->vn_cnt
= j
+ 1;
8604 if (j
+ 1 < iverneed
->vn_cnt
)
8605 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8607 if (ivernaux
->vna_next
8608 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8609 goto error_return_bad_verref
;
8611 evernaux
= ((Elf_External_Vernaux
*)
8612 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8615 iverneed
->vn_nextref
= NULL
;
8616 if (iverneed
->vn_next
== 0)
8618 if (i
+ 1 < hdr
->sh_info
)
8619 iverneed
->vn_nextref
= iverneed
+ 1;
8621 if (iverneed
->vn_next
8622 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8623 goto error_return_bad_verref
;
8625 everneed
= ((Elf_External_Verneed
*)
8626 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8628 elf_tdata (abfd
)->cverrefs
= i
;
8634 if (elf_dynverdef (abfd
) != 0)
8636 Elf_Internal_Shdr
*hdr
;
8637 Elf_External_Verdef
*everdef
;
8638 Elf_Internal_Verdef
*iverdef
;
8639 Elf_Internal_Verdef
*iverdefarr
;
8640 Elf_Internal_Verdef iverdefmem
;
8642 unsigned int maxidx
;
8643 bfd_byte
*contents_end_def
, *contents_end_aux
;
8645 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8647 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8649 error_return_bad_verdef
:
8651 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8652 bfd_set_error (bfd_error_bad_value
);
8653 error_return_verdef
:
8654 elf_tdata (abfd
)->verdef
= NULL
;
8655 elf_tdata (abfd
)->cverdefs
= 0;
8659 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8660 if (contents
== NULL
)
8661 goto error_return_verdef
;
8662 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8663 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8664 goto error_return_verdef
;
8666 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8667 >= sizeof (Elf_External_Verdaux
));
8668 contents_end_def
= contents
+ hdr
->sh_size
8669 - sizeof (Elf_External_Verdef
);
8670 contents_end_aux
= contents
+ hdr
->sh_size
8671 - sizeof (Elf_External_Verdaux
);
8673 /* We know the number of entries in the section but not the maximum
8674 index. Therefore we have to run through all entries and find
8676 everdef
= (Elf_External_Verdef
*) contents
;
8678 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8680 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8682 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8683 goto error_return_bad_verdef
;
8684 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8685 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8687 if (iverdefmem
.vd_next
== 0)
8690 if (iverdefmem
.vd_next
8691 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8692 goto error_return_bad_verdef
;
8694 everdef
= ((Elf_External_Verdef
*)
8695 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8698 if (default_imported_symver
)
8700 if (freeidx
> maxidx
)
8706 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8707 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8708 if (elf_tdata (abfd
)->verdef
== NULL
)
8709 goto error_return_verdef
;
8711 elf_tdata (abfd
)->cverdefs
= maxidx
;
8713 everdef
= (Elf_External_Verdef
*) contents
;
8714 iverdefarr
= elf_tdata (abfd
)->verdef
;
8715 for (i
= 0; i
< hdr
->sh_info
; i
++)
8717 Elf_External_Verdaux
*everdaux
;
8718 Elf_Internal_Verdaux
*iverdaux
;
8721 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8723 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8724 goto error_return_bad_verdef
;
8726 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8727 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8729 iverdef
->vd_bfd
= abfd
;
8731 if (iverdef
->vd_cnt
== 0)
8732 iverdef
->vd_auxptr
= NULL
;
8735 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8736 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8737 sizeof (Elf_Internal_Verdaux
));
8738 if (iverdef
->vd_auxptr
== NULL
)
8739 goto error_return_verdef
;
8743 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8744 goto error_return_bad_verdef
;
8746 everdaux
= ((Elf_External_Verdaux
*)
8747 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8748 iverdaux
= iverdef
->vd_auxptr
;
8749 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8751 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8753 iverdaux
->vda_nodename
=
8754 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8755 iverdaux
->vda_name
);
8756 if (iverdaux
->vda_nodename
== NULL
)
8757 goto error_return_bad_verdef
;
8759 iverdaux
->vda_nextptr
= NULL
;
8760 if (iverdaux
->vda_next
== 0)
8762 iverdef
->vd_cnt
= j
+ 1;
8765 if (j
+ 1 < iverdef
->vd_cnt
)
8766 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8768 if (iverdaux
->vda_next
8769 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8770 goto error_return_bad_verdef
;
8772 everdaux
= ((Elf_External_Verdaux
*)
8773 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8776 iverdef
->vd_nodename
= NULL
;
8777 if (iverdef
->vd_cnt
)
8778 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8780 iverdef
->vd_nextdef
= NULL
;
8781 if (iverdef
->vd_next
== 0)
8783 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8784 iverdef
->vd_nextdef
= iverdef
+ 1;
8786 everdef
= ((Elf_External_Verdef
*)
8787 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8793 else if (default_imported_symver
)
8800 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8801 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8802 if (elf_tdata (abfd
)->verdef
== NULL
)
8805 elf_tdata (abfd
)->cverdefs
= freeidx
;
8808 /* Create a default version based on the soname. */
8809 if (default_imported_symver
)
8811 Elf_Internal_Verdef
*iverdef
;
8812 Elf_Internal_Verdaux
*iverdaux
;
8814 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8816 iverdef
->vd_version
= VER_DEF_CURRENT
;
8817 iverdef
->vd_flags
= 0;
8818 iverdef
->vd_ndx
= freeidx
;
8819 iverdef
->vd_cnt
= 1;
8821 iverdef
->vd_bfd
= abfd
;
8823 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8824 if (iverdef
->vd_nodename
== NULL
)
8825 goto error_return_verdef
;
8826 iverdef
->vd_nextdef
= NULL
;
8827 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8828 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8829 if (iverdef
->vd_auxptr
== NULL
)
8830 goto error_return_verdef
;
8832 iverdaux
= iverdef
->vd_auxptr
;
8833 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8839 if (contents
!= NULL
)
8845 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8847 elf_symbol_type
*newsym
;
8849 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
8852 newsym
->symbol
.the_bfd
= abfd
;
8853 return &newsym
->symbol
;
8857 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8861 bfd_symbol_info (symbol
, ret
);
8864 /* Return whether a symbol name implies a local symbol. Most targets
8865 use this function for the is_local_label_name entry point, but some
8869 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8872 /* Normal local symbols start with ``.L''. */
8873 if (name
[0] == '.' && name
[1] == 'L')
8876 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8877 DWARF debugging symbols starting with ``..''. */
8878 if (name
[0] == '.' && name
[1] == '.')
8881 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8882 emitting DWARF debugging output. I suspect this is actually a
8883 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8884 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8885 underscore to be emitted on some ELF targets). For ease of use,
8886 we treat such symbols as local. */
8887 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8890 /* Treat assembler generated fake symbols, dollar local labels and
8891 forward-backward labels (aka local labels) as locals.
8892 These labels have the form:
8894 L0^A.* (fake symbols)
8896 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8898 Versions which start with .L will have already been matched above,
8899 so we only need to match the rest. */
8900 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8902 bfd_boolean ret
= FALSE
;
8906 for (p
= name
+ 2; (c
= *p
); p
++)
8908 if (c
== 1 || c
== 2)
8910 if (c
== 1 && p
== name
+ 2)
8911 /* A fake symbol. */
8914 /* FIXME: We are being paranoid here and treating symbols like
8915 L0^Bfoo as if there were non-local, on the grounds that the
8916 assembler will never generate them. But can any symbol
8917 containing an ASCII value in the range 1-31 ever be anything
8918 other than some kind of local ? */
8935 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8936 asymbol
*symbol ATTRIBUTE_UNUSED
)
8943 _bfd_elf_set_arch_mach (bfd
*abfd
,
8944 enum bfd_architecture arch
,
8945 unsigned long machine
)
8947 /* If this isn't the right architecture for this backend, and this
8948 isn't the generic backend, fail. */
8949 if (arch
!= get_elf_backend_data (abfd
)->arch
8950 && arch
!= bfd_arch_unknown
8951 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8954 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8957 /* Find the nearest line to a particular section and offset,
8958 for error reporting. */
8961 _bfd_elf_find_nearest_line (bfd
*abfd
,
8965 const char **filename_ptr
,
8966 const char **functionname_ptr
,
8967 unsigned int *line_ptr
,
8968 unsigned int *discriminator_ptr
)
8972 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8973 filename_ptr
, functionname_ptr
,
8974 line_ptr
, discriminator_ptr
,
8975 dwarf_debug_sections
,
8976 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8977 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8978 filename_ptr
, functionname_ptr
,
8981 if (!*functionname_ptr
)
8982 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8983 *filename_ptr
? NULL
: filename_ptr
,
8988 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8989 &found
, filename_ptr
,
8990 functionname_ptr
, line_ptr
,
8991 &elf_tdata (abfd
)->line_info
))
8993 if (found
&& (*functionname_ptr
|| *line_ptr
))
8996 if (symbols
== NULL
)
8999 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9000 filename_ptr
, functionname_ptr
))
9007 /* Find the line for a symbol. */
9010 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9011 const char **filename_ptr
, unsigned int *line_ptr
)
9013 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9014 filename_ptr
, NULL
, line_ptr
, NULL
,
9015 dwarf_debug_sections
,
9016 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9019 /* After a call to bfd_find_nearest_line, successive calls to
9020 bfd_find_inliner_info can be used to get source information about
9021 each level of function inlining that terminated at the address
9022 passed to bfd_find_nearest_line. Currently this is only supported
9023 for DWARF2 with appropriate DWARF3 extensions. */
9026 _bfd_elf_find_inliner_info (bfd
*abfd
,
9027 const char **filename_ptr
,
9028 const char **functionname_ptr
,
9029 unsigned int *line_ptr
)
9032 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9033 functionname_ptr
, line_ptr
,
9034 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9039 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9041 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9042 int ret
= bed
->s
->sizeof_ehdr
;
9044 if (!bfd_link_relocatable (info
))
9046 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9048 if (phdr_size
== (bfd_size_type
) -1)
9050 struct elf_segment_map
*m
;
9053 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9054 phdr_size
+= bed
->s
->sizeof_phdr
;
9057 phdr_size
= get_program_header_size (abfd
, info
);
9060 elf_program_header_size (abfd
) = phdr_size
;
9068 _bfd_elf_set_section_contents (bfd
*abfd
,
9070 const void *location
,
9072 bfd_size_type count
)
9074 Elf_Internal_Shdr
*hdr
;
9077 if (! abfd
->output_has_begun
9078 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9084 hdr
= &elf_section_data (section
)->this_hdr
;
9085 if (hdr
->sh_offset
== (file_ptr
) -1)
9087 /* We must compress this section. Write output to the buffer. */
9088 unsigned char *contents
= hdr
->contents
;
9089 if ((offset
+ count
) > hdr
->sh_size
9090 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
9091 || contents
== NULL
)
9093 memcpy (contents
+ offset
, location
, count
);
9096 pos
= hdr
->sh_offset
+ offset
;
9097 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9098 || bfd_bwrite (location
, count
, abfd
) != count
)
9105 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9106 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9107 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9113 /* Try to convert a non-ELF reloc into an ELF one. */
9116 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9118 /* Check whether we really have an ELF howto. */
9120 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9122 bfd_reloc_code_real_type code
;
9123 reloc_howto_type
*howto
;
9125 /* Alien reloc: Try to determine its type to replace it with an
9126 equivalent ELF reloc. */
9128 if (areloc
->howto
->pc_relative
)
9130 switch (areloc
->howto
->bitsize
)
9133 code
= BFD_RELOC_8_PCREL
;
9136 code
= BFD_RELOC_12_PCREL
;
9139 code
= BFD_RELOC_16_PCREL
;
9142 code
= BFD_RELOC_24_PCREL
;
9145 code
= BFD_RELOC_32_PCREL
;
9148 code
= BFD_RELOC_64_PCREL
;
9154 howto
= bfd_reloc_type_lookup (abfd
, code
);
9156 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9158 if (howto
->pcrel_offset
)
9159 areloc
->addend
+= areloc
->address
;
9161 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9166 switch (areloc
->howto
->bitsize
)
9172 code
= BFD_RELOC_14
;
9175 code
= BFD_RELOC_16
;
9178 code
= BFD_RELOC_26
;
9181 code
= BFD_RELOC_32
;
9184 code
= BFD_RELOC_64
;
9190 howto
= bfd_reloc_type_lookup (abfd
, code
);
9194 areloc
->howto
= howto
;
9202 /* xgettext:c-format */
9203 _bfd_error_handler (_("%pB: %s unsupported"),
9204 abfd
, areloc
->howto
->name
);
9205 bfd_set_error (bfd_error_bad_value
);
9210 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9212 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9213 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9215 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9216 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9217 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9220 return _bfd_generic_close_and_cleanup (abfd
);
9223 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9224 in the relocation's offset. Thus we cannot allow any sort of sanity
9225 range-checking to interfere. There is nothing else to do in processing
9228 bfd_reloc_status_type
9229 _bfd_elf_rel_vtable_reloc_fn
9230 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9231 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9232 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9233 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9235 return bfd_reloc_ok
;
9238 /* Elf core file support. Much of this only works on native
9239 toolchains, since we rely on knowing the
9240 machine-dependent procfs structure in order to pick
9241 out details about the corefile. */
9243 #ifdef HAVE_SYS_PROCFS_H
9244 /* Needed for new procfs interface on sparc-solaris. */
9245 # define _STRUCTURED_PROC 1
9246 # include <sys/procfs.h>
9249 /* Return a PID that identifies a "thread" for threaded cores, or the
9250 PID of the main process for non-threaded cores. */
9253 elfcore_make_pid (bfd
*abfd
)
9257 pid
= elf_tdata (abfd
)->core
->lwpid
;
9259 pid
= elf_tdata (abfd
)->core
->pid
;
9264 /* If there isn't a section called NAME, make one, using
9265 data from SECT. Note, this function will generate a
9266 reference to NAME, so you shouldn't deallocate or
9270 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9274 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9277 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9281 sect2
->size
= sect
->size
;
9282 sect2
->filepos
= sect
->filepos
;
9283 sect2
->alignment_power
= sect
->alignment_power
;
9287 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9288 actually creates up to two pseudosections:
9289 - For the single-threaded case, a section named NAME, unless
9290 such a section already exists.
9291 - For the multi-threaded case, a section named "NAME/PID", where
9292 PID is elfcore_make_pid (abfd).
9293 Both pseudosections have identical contents. */
9295 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9301 char *threaded_name
;
9305 /* Build the section name. */
9307 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9308 len
= strlen (buf
) + 1;
9309 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9310 if (threaded_name
== NULL
)
9312 memcpy (threaded_name
, buf
, len
);
9314 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9319 sect
->filepos
= filepos
;
9320 sect
->alignment_power
= 2;
9322 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9326 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9329 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9335 sect
->size
= note
->descsz
- offs
;
9336 sect
->filepos
= note
->descpos
+ offs
;
9337 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9342 /* prstatus_t exists on:
9344 linux 2.[01] + glibc
9348 #if defined (HAVE_PRSTATUS_T)
9351 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9356 if (note
->descsz
== sizeof (prstatus_t
))
9360 size
= sizeof (prstat
.pr_reg
);
9361 offset
= offsetof (prstatus_t
, pr_reg
);
9362 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9364 /* Do not overwrite the core signal if it
9365 has already been set by another thread. */
9366 if (elf_tdata (abfd
)->core
->signal
== 0)
9367 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9368 if (elf_tdata (abfd
)->core
->pid
== 0)
9369 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9371 /* pr_who exists on:
9374 pr_who doesn't exist on:
9377 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9378 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9380 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9383 #if defined (HAVE_PRSTATUS32_T)
9384 else if (note
->descsz
== sizeof (prstatus32_t
))
9386 /* 64-bit host, 32-bit corefile */
9387 prstatus32_t prstat
;
9389 size
= sizeof (prstat
.pr_reg
);
9390 offset
= offsetof (prstatus32_t
, pr_reg
);
9391 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9393 /* Do not overwrite the core signal if it
9394 has already been set by another thread. */
9395 if (elf_tdata (abfd
)->core
->signal
== 0)
9396 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9397 if (elf_tdata (abfd
)->core
->pid
== 0)
9398 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9400 /* pr_who exists on:
9403 pr_who doesn't exist on:
9406 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9407 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9409 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9412 #endif /* HAVE_PRSTATUS32_T */
9415 /* Fail - we don't know how to handle any other
9416 note size (ie. data object type). */
9420 /* Make a ".reg/999" section and a ".reg" section. */
9421 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9422 size
, note
->descpos
+ offset
);
9424 #endif /* defined (HAVE_PRSTATUS_T) */
9426 /* Create a pseudosection containing the exact contents of NOTE. */
9428 elfcore_make_note_pseudosection (bfd
*abfd
,
9430 Elf_Internal_Note
*note
)
9432 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9433 note
->descsz
, note
->descpos
);
9436 /* There isn't a consistent prfpregset_t across platforms,
9437 but it doesn't matter, because we don't have to pick this
9438 data structure apart. */
9441 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9443 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9446 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9447 type of NT_PRXFPREG. Just include the whole note's contents
9451 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9453 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9456 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9457 with a note type of NT_X86_XSTATE. Just include the whole note's
9458 contents literally. */
9461 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9463 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9467 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9469 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9473 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9475 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9479 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9481 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9485 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9487 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9491 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9493 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9497 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9499 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9503 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9505 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9509 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9511 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9515 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9517 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9521 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9523 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9527 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9529 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9533 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9535 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9539 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9541 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9545 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9547 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9551 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9553 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9557 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9559 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9563 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9565 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9569 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9571 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9575 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9577 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9581 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9583 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9587 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9589 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9593 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9595 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9599 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9601 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9605 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9607 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9611 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9613 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9617 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9619 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9623 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9625 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9629 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9631 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9635 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9637 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9641 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9643 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9647 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9649 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9653 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9655 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9659 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9661 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9665 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9667 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9670 #if defined (HAVE_PRPSINFO_T)
9671 typedef prpsinfo_t elfcore_psinfo_t
;
9672 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9673 typedef prpsinfo32_t elfcore_psinfo32_t
;
9677 #if defined (HAVE_PSINFO_T)
9678 typedef psinfo_t elfcore_psinfo_t
;
9679 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9680 typedef psinfo32_t elfcore_psinfo32_t
;
9684 /* return a malloc'ed copy of a string at START which is at
9685 most MAX bytes long, possibly without a terminating '\0'.
9686 the copy will always have a terminating '\0'. */
9689 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9692 char *end
= (char *) memchr (start
, '\0', max
);
9700 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9704 memcpy (dups
, start
, len
);
9710 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9712 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9714 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9716 elfcore_psinfo_t psinfo
;
9718 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9720 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9721 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9723 elf_tdata (abfd
)->core
->program
9724 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9725 sizeof (psinfo
.pr_fname
));
9727 elf_tdata (abfd
)->core
->command
9728 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9729 sizeof (psinfo
.pr_psargs
));
9731 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9732 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9734 /* 64-bit host, 32-bit corefile */
9735 elfcore_psinfo32_t psinfo
;
9737 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9739 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9740 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9742 elf_tdata (abfd
)->core
->program
9743 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9744 sizeof (psinfo
.pr_fname
));
9746 elf_tdata (abfd
)->core
->command
9747 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9748 sizeof (psinfo
.pr_psargs
));
9754 /* Fail - we don't know how to handle any other
9755 note size (ie. data object type). */
9759 /* Note that for some reason, a spurious space is tacked
9760 onto the end of the args in some (at least one anyway)
9761 implementations, so strip it off if it exists. */
9764 char *command
= elf_tdata (abfd
)->core
->command
;
9765 int n
= strlen (command
);
9767 if (0 < n
&& command
[n
- 1] == ' ')
9768 command
[n
- 1] = '\0';
9773 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9775 #if defined (HAVE_PSTATUS_T)
9777 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9779 if (note
->descsz
== sizeof (pstatus_t
)
9780 #if defined (HAVE_PXSTATUS_T)
9781 || note
->descsz
== sizeof (pxstatus_t
)
9787 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9789 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9791 #if defined (HAVE_PSTATUS32_T)
9792 else if (note
->descsz
== sizeof (pstatus32_t
))
9794 /* 64-bit host, 32-bit corefile */
9797 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9799 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9802 /* Could grab some more details from the "representative"
9803 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9804 NT_LWPSTATUS note, presumably. */
9808 #endif /* defined (HAVE_PSTATUS_T) */
9810 #if defined (HAVE_LWPSTATUS_T)
9812 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9814 lwpstatus_t lwpstat
;
9820 if (note
->descsz
!= sizeof (lwpstat
)
9821 #if defined (HAVE_LWPXSTATUS_T)
9822 && note
->descsz
!= sizeof (lwpxstatus_t
)
9827 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9829 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9830 /* Do not overwrite the core signal if it has already been set by
9832 if (elf_tdata (abfd
)->core
->signal
== 0)
9833 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9835 /* Make a ".reg/999" section. */
9837 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9838 len
= strlen (buf
) + 1;
9839 name
= bfd_alloc (abfd
, len
);
9842 memcpy (name
, buf
, len
);
9844 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9848 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9849 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9850 sect
->filepos
= note
->descpos
9851 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9854 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9855 sect
->size
= sizeof (lwpstat
.pr_reg
);
9856 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9859 sect
->alignment_power
= 2;
9861 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9864 /* Make a ".reg2/999" section */
9866 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9867 len
= strlen (buf
) + 1;
9868 name
= bfd_alloc (abfd
, len
);
9871 memcpy (name
, buf
, len
);
9873 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9877 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9878 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9879 sect
->filepos
= note
->descpos
9880 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9883 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9884 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9885 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9888 sect
->alignment_power
= 2;
9890 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9892 #endif /* defined (HAVE_LWPSTATUS_T) */
9895 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9902 int is_active_thread
;
9905 if (note
->descsz
< 728)
9908 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9911 type
= bfd_get_32 (abfd
, note
->descdata
);
9915 case 1 /* NOTE_INFO_PROCESS */:
9916 /* FIXME: need to add ->core->command. */
9917 /* process_info.pid */
9918 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9919 /* process_info.signal */
9920 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9923 case 2 /* NOTE_INFO_THREAD */:
9924 /* Make a ".reg/999" section. */
9925 /* thread_info.tid */
9926 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9928 len
= strlen (buf
) + 1;
9929 name
= (char *) bfd_alloc (abfd
, len
);
9933 memcpy (name
, buf
, len
);
9935 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9939 /* sizeof (thread_info.thread_context) */
9941 /* offsetof (thread_info.thread_context) */
9942 sect
->filepos
= note
->descpos
+ 12;
9943 sect
->alignment_power
= 2;
9945 /* thread_info.is_active_thread */
9946 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9948 if (is_active_thread
)
9949 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9953 case 3 /* NOTE_INFO_MODULE */:
9954 /* Make a ".module/xxxxxxxx" section. */
9955 /* module_info.base_address */
9956 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9957 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9959 len
= strlen (buf
) + 1;
9960 name
= (char *) bfd_alloc (abfd
, len
);
9964 memcpy (name
, buf
, len
);
9966 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9971 sect
->size
= note
->descsz
;
9972 sect
->filepos
= note
->descpos
;
9973 sect
->alignment_power
= 2;
9984 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9986 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9994 if (bed
->elf_backend_grok_prstatus
)
9995 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9997 #if defined (HAVE_PRSTATUS_T)
9998 return elfcore_grok_prstatus (abfd
, note
);
10003 #if defined (HAVE_PSTATUS_T)
10005 return elfcore_grok_pstatus (abfd
, note
);
10008 #if defined (HAVE_LWPSTATUS_T)
10010 return elfcore_grok_lwpstatus (abfd
, note
);
10013 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10014 return elfcore_grok_prfpreg (abfd
, note
);
10016 case NT_WIN32PSTATUS
:
10017 return elfcore_grok_win32pstatus (abfd
, note
);
10019 case NT_PRXFPREG
: /* Linux SSE extension */
10020 if (note
->namesz
== 6
10021 && strcmp (note
->namedata
, "LINUX") == 0)
10022 return elfcore_grok_prxfpreg (abfd
, note
);
10026 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10027 if (note
->namesz
== 6
10028 && strcmp (note
->namedata
, "LINUX") == 0)
10029 return elfcore_grok_xstatereg (abfd
, note
);
10034 if (note
->namesz
== 6
10035 && strcmp (note
->namedata
, "LINUX") == 0)
10036 return elfcore_grok_ppc_vmx (abfd
, note
);
10041 if (note
->namesz
== 6
10042 && strcmp (note
->namedata
, "LINUX") == 0)
10043 return elfcore_grok_ppc_vsx (abfd
, note
);
10048 if (note
->namesz
== 6
10049 && strcmp (note
->namedata
, "LINUX") == 0)
10050 return elfcore_grok_ppc_tar (abfd
, note
);
10055 if (note
->namesz
== 6
10056 && strcmp (note
->namedata
, "LINUX") == 0)
10057 return elfcore_grok_ppc_ppr (abfd
, note
);
10062 if (note
->namesz
== 6
10063 && strcmp (note
->namedata
, "LINUX") == 0)
10064 return elfcore_grok_ppc_dscr (abfd
, note
);
10069 if (note
->namesz
== 6
10070 && strcmp (note
->namedata
, "LINUX") == 0)
10071 return elfcore_grok_ppc_ebb (abfd
, note
);
10076 if (note
->namesz
== 6
10077 && strcmp (note
->namedata
, "LINUX") == 0)
10078 return elfcore_grok_ppc_pmu (abfd
, note
);
10082 case NT_PPC_TM_CGPR
:
10083 if (note
->namesz
== 6
10084 && strcmp (note
->namedata
, "LINUX") == 0)
10085 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10089 case NT_PPC_TM_CFPR
:
10090 if (note
->namesz
== 6
10091 && strcmp (note
->namedata
, "LINUX") == 0)
10092 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10096 case NT_PPC_TM_CVMX
:
10097 if (note
->namesz
== 6
10098 && strcmp (note
->namedata
, "LINUX") == 0)
10099 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10103 case NT_PPC_TM_CVSX
:
10104 if (note
->namesz
== 6
10105 && strcmp (note
->namedata
, "LINUX") == 0)
10106 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10110 case NT_PPC_TM_SPR
:
10111 if (note
->namesz
== 6
10112 && strcmp (note
->namedata
, "LINUX") == 0)
10113 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10117 case NT_PPC_TM_CTAR
:
10118 if (note
->namesz
== 6
10119 && strcmp (note
->namedata
, "LINUX") == 0)
10120 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10124 case NT_PPC_TM_CPPR
:
10125 if (note
->namesz
== 6
10126 && strcmp (note
->namedata
, "LINUX") == 0)
10127 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10131 case NT_PPC_TM_CDSCR
:
10132 if (note
->namesz
== 6
10133 && strcmp (note
->namedata
, "LINUX") == 0)
10134 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10138 case NT_S390_HIGH_GPRS
:
10139 if (note
->namesz
== 6
10140 && strcmp (note
->namedata
, "LINUX") == 0)
10141 return elfcore_grok_s390_high_gprs (abfd
, note
);
10145 case NT_S390_TIMER
:
10146 if (note
->namesz
== 6
10147 && strcmp (note
->namedata
, "LINUX") == 0)
10148 return elfcore_grok_s390_timer (abfd
, note
);
10152 case NT_S390_TODCMP
:
10153 if (note
->namesz
== 6
10154 && strcmp (note
->namedata
, "LINUX") == 0)
10155 return elfcore_grok_s390_todcmp (abfd
, note
);
10159 case NT_S390_TODPREG
:
10160 if (note
->namesz
== 6
10161 && strcmp (note
->namedata
, "LINUX") == 0)
10162 return elfcore_grok_s390_todpreg (abfd
, note
);
10167 if (note
->namesz
== 6
10168 && strcmp (note
->namedata
, "LINUX") == 0)
10169 return elfcore_grok_s390_ctrs (abfd
, note
);
10173 case NT_S390_PREFIX
:
10174 if (note
->namesz
== 6
10175 && strcmp (note
->namedata
, "LINUX") == 0)
10176 return elfcore_grok_s390_prefix (abfd
, note
);
10180 case NT_S390_LAST_BREAK
:
10181 if (note
->namesz
== 6
10182 && strcmp (note
->namedata
, "LINUX") == 0)
10183 return elfcore_grok_s390_last_break (abfd
, note
);
10187 case NT_S390_SYSTEM_CALL
:
10188 if (note
->namesz
== 6
10189 && strcmp (note
->namedata
, "LINUX") == 0)
10190 return elfcore_grok_s390_system_call (abfd
, note
);
10195 if (note
->namesz
== 6
10196 && strcmp (note
->namedata
, "LINUX") == 0)
10197 return elfcore_grok_s390_tdb (abfd
, note
);
10201 case NT_S390_VXRS_LOW
:
10202 if (note
->namesz
== 6
10203 && strcmp (note
->namedata
, "LINUX") == 0)
10204 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10208 case NT_S390_VXRS_HIGH
:
10209 if (note
->namesz
== 6
10210 && strcmp (note
->namedata
, "LINUX") == 0)
10211 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10215 case NT_S390_GS_CB
:
10216 if (note
->namesz
== 6
10217 && strcmp (note
->namedata
, "LINUX") == 0)
10218 return elfcore_grok_s390_gs_cb (abfd
, note
);
10222 case NT_S390_GS_BC
:
10223 if (note
->namesz
== 6
10224 && strcmp (note
->namedata
, "LINUX") == 0)
10225 return elfcore_grok_s390_gs_bc (abfd
, note
);
10230 if (note
->namesz
== 6
10231 && strcmp (note
->namedata
, "LINUX") == 0)
10232 return elfcore_grok_arm_vfp (abfd
, note
);
10237 if (note
->namesz
== 6
10238 && strcmp (note
->namedata
, "LINUX") == 0)
10239 return elfcore_grok_aarch_tls (abfd
, note
);
10243 case NT_ARM_HW_BREAK
:
10244 if (note
->namesz
== 6
10245 && strcmp (note
->namedata
, "LINUX") == 0)
10246 return elfcore_grok_aarch_hw_break (abfd
, note
);
10250 case NT_ARM_HW_WATCH
:
10251 if (note
->namesz
== 6
10252 && strcmp (note
->namedata
, "LINUX") == 0)
10253 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10258 if (note
->namesz
== 6
10259 && strcmp (note
->namedata
, "LINUX") == 0)
10260 return elfcore_grok_aarch_sve (abfd
, note
);
10264 case NT_ARM_PAC_MASK
:
10265 if (note
->namesz
== 6
10266 && strcmp (note
->namedata
, "LINUX") == 0)
10267 return elfcore_grok_aarch_pauth (abfd
, note
);
10273 if (bed
->elf_backend_grok_psinfo
)
10274 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10276 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10277 return elfcore_grok_psinfo (abfd
, note
);
10283 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10286 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10290 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10297 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10299 struct bfd_build_id
* build_id
;
10301 if (note
->descsz
== 0)
10304 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10305 if (build_id
== NULL
)
10308 build_id
->size
= note
->descsz
;
10309 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10310 abfd
->build_id
= build_id
;
10316 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10318 switch (note
->type
)
10323 case NT_GNU_PROPERTY_TYPE_0
:
10324 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10326 case NT_GNU_BUILD_ID
:
10327 return elfobj_grok_gnu_build_id (abfd
, note
);
10332 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10334 struct sdt_note
*cur
=
10335 (struct sdt_note
*) bfd_alloc (abfd
,
10336 sizeof (struct sdt_note
) + note
->descsz
);
10338 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10339 cur
->size
= (bfd_size_type
) note
->descsz
;
10340 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10342 elf_tdata (abfd
)->sdt_note_head
= cur
;
10348 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10350 switch (note
->type
)
10353 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10361 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10365 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10368 if (note
->descsz
< 108)
10373 if (note
->descsz
< 120)
10381 /* Check for version 1 in pr_version. */
10382 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10387 /* Skip over pr_psinfosz. */
10388 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10392 offset
+= 4; /* Padding before pr_psinfosz. */
10396 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10397 elf_tdata (abfd
)->core
->program
10398 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10401 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10402 elf_tdata (abfd
)->core
->command
10403 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10406 /* Padding before pr_pid. */
10409 /* The pr_pid field was added in version "1a". */
10410 if (note
->descsz
< offset
+ 4)
10413 elf_tdata (abfd
)->core
->pid
10414 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10420 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10426 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10427 Also compute minimum size of this note. */
10428 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10432 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10436 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10437 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10444 if (note
->descsz
< min_size
)
10447 /* Check for version 1 in pr_version. */
10448 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10451 /* Extract size of pr_reg from pr_gregsetsz. */
10452 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10453 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10455 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10460 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10464 /* Skip over pr_osreldate. */
10467 /* Read signal from pr_cursig. */
10468 if (elf_tdata (abfd
)->core
->signal
== 0)
10469 elf_tdata (abfd
)->core
->signal
10470 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10473 /* Read TID from pr_pid. */
10474 elf_tdata (abfd
)->core
->lwpid
10475 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10478 /* Padding before pr_reg. */
10479 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10482 /* Make sure that there is enough data remaining in the note. */
10483 if ((note
->descsz
- offset
) < size
)
10486 /* Make a ".reg/999" section and a ".reg" section. */
10487 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10488 size
, note
->descpos
+ offset
);
10492 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10494 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10496 switch (note
->type
)
10499 if (bed
->elf_backend_grok_freebsd_prstatus
)
10500 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10502 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10505 return elfcore_grok_prfpreg (abfd
, note
);
10508 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10510 case NT_FREEBSD_THRMISC
:
10511 if (note
->namesz
== 8)
10512 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10516 case NT_FREEBSD_PROCSTAT_PROC
:
10517 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10520 case NT_FREEBSD_PROCSTAT_FILES
:
10521 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10524 case NT_FREEBSD_PROCSTAT_VMMAP
:
10525 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10528 case NT_FREEBSD_PROCSTAT_AUXV
:
10529 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10531 case NT_X86_XSTATE
:
10532 if (note
->namesz
== 8)
10533 return elfcore_grok_xstatereg (abfd
, note
);
10537 case NT_FREEBSD_PTLWPINFO
:
10538 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10542 return elfcore_grok_arm_vfp (abfd
, note
);
10550 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10554 cp
= strchr (note
->namedata
, '@');
10557 *lwpidp
= atoi(cp
+ 1);
10564 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10566 if (note
->descsz
<= 0x7c + 31)
10569 /* Signal number at offset 0x08. */
10570 elf_tdata (abfd
)->core
->signal
10571 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10573 /* Process ID at offset 0x50. */
10574 elf_tdata (abfd
)->core
->pid
10575 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10577 /* Command name at 0x7c (max 32 bytes, including nul). */
10578 elf_tdata (abfd
)->core
->command
10579 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10581 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10586 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10590 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10591 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10593 switch (note
->type
)
10595 case NT_NETBSDCORE_PROCINFO
:
10596 /* NetBSD-specific core "procinfo". Note that we expect to
10597 find this note before any of the others, which is fine,
10598 since the kernel writes this note out first when it
10599 creates a core file. */
10600 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10601 #ifdef NT_NETBSDCORE_AUXV
10602 case NT_NETBSDCORE_AUXV
:
10603 /* NetBSD-specific Elf Auxiliary Vector data. */
10604 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10610 /* As of March 2017 there are no other machine-independent notes
10611 defined for NetBSD core files. If the note type is less
10612 than the start of the machine-dependent note types, we don't
10615 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10619 switch (bfd_get_arch (abfd
))
10621 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10622 PT_GETFPREGS == mach+2. */
10624 case bfd_arch_alpha
:
10625 case bfd_arch_sparc
:
10626 switch (note
->type
)
10628 case NT_NETBSDCORE_FIRSTMACH
+0:
10629 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10631 case NT_NETBSDCORE_FIRSTMACH
+2:
10632 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10638 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10639 There's also old PT___GETREGS40 == mach + 1 for old reg
10640 structure which lacks GBR. */
10643 switch (note
->type
)
10645 case NT_NETBSDCORE_FIRSTMACH
+3:
10646 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10648 case NT_NETBSDCORE_FIRSTMACH
+5:
10649 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10655 /* On all other arch's, PT_GETREGS == mach+1 and
10656 PT_GETFPREGS == mach+3. */
10659 switch (note
->type
)
10661 case NT_NETBSDCORE_FIRSTMACH
+1:
10662 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10664 case NT_NETBSDCORE_FIRSTMACH
+3:
10665 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10675 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10677 if (note
->descsz
<= 0x48 + 31)
10680 /* Signal number at offset 0x08. */
10681 elf_tdata (abfd
)->core
->signal
10682 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10684 /* Process ID at offset 0x20. */
10685 elf_tdata (abfd
)->core
->pid
10686 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10688 /* Command name at 0x48 (max 32 bytes, including nul). */
10689 elf_tdata (abfd
)->core
->command
10690 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10696 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10698 if (note
->type
== NT_OPENBSD_PROCINFO
)
10699 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10701 if (note
->type
== NT_OPENBSD_REGS
)
10702 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10704 if (note
->type
== NT_OPENBSD_FPREGS
)
10705 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10707 if (note
->type
== NT_OPENBSD_XFPREGS
)
10708 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10710 if (note
->type
== NT_OPENBSD_AUXV
)
10711 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10713 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10715 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10720 sect
->size
= note
->descsz
;
10721 sect
->filepos
= note
->descpos
;
10722 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10731 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10733 void *ddata
= note
->descdata
;
10740 if (note
->descsz
< 16)
10743 /* nto_procfs_status 'pid' field is at offset 0. */
10744 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10746 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10747 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10749 /* nto_procfs_status 'flags' field is at offset 8. */
10750 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10752 /* nto_procfs_status 'what' field is at offset 14. */
10753 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10755 elf_tdata (abfd
)->core
->signal
= sig
;
10756 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10759 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10760 do not come from signals so we make sure we set the current
10761 thread just in case. */
10762 if (flags
& 0x00000080)
10763 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10765 /* Make a ".qnx_core_status/%d" section. */
10766 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10768 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10771 strcpy (name
, buf
);
10773 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10777 sect
->size
= note
->descsz
;
10778 sect
->filepos
= note
->descpos
;
10779 sect
->alignment_power
= 2;
10781 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10785 elfcore_grok_nto_regs (bfd
*abfd
,
10786 Elf_Internal_Note
*note
,
10794 /* Make a "(base)/%d" section. */
10795 sprintf (buf
, "%s/%ld", base
, tid
);
10797 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10800 strcpy (name
, buf
);
10802 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10806 sect
->size
= note
->descsz
;
10807 sect
->filepos
= note
->descpos
;
10808 sect
->alignment_power
= 2;
10810 /* This is the current thread. */
10811 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10812 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10817 #define BFD_QNT_CORE_INFO 7
10818 #define BFD_QNT_CORE_STATUS 8
10819 #define BFD_QNT_CORE_GREG 9
10820 #define BFD_QNT_CORE_FPREG 10
10823 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10825 /* Every GREG section has a STATUS section before it. Store the
10826 tid from the previous call to pass down to the next gregs
10828 static long tid
= 1;
10830 switch (note
->type
)
10832 case BFD_QNT_CORE_INFO
:
10833 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10834 case BFD_QNT_CORE_STATUS
:
10835 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10836 case BFD_QNT_CORE_GREG
:
10837 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10838 case BFD_QNT_CORE_FPREG
:
10839 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10846 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10852 /* Use note name as section name. */
10853 len
= note
->namesz
;
10854 name
= (char *) bfd_alloc (abfd
, len
);
10857 memcpy (name
, note
->namedata
, len
);
10858 name
[len
- 1] = '\0';
10860 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10864 sect
->size
= note
->descsz
;
10865 sect
->filepos
= note
->descpos
;
10866 sect
->alignment_power
= 1;
10871 /* Function: elfcore_write_note
10874 buffer to hold note, and current size of buffer
10878 size of data for note
10880 Writes note to end of buffer. ELF64 notes are written exactly as
10881 for ELF32, despite the current (as of 2006) ELF gabi specifying
10882 that they ought to have 8-byte namesz and descsz field, and have
10883 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10886 Pointer to realloc'd buffer, *BUFSIZ updated. */
10889 elfcore_write_note (bfd
*abfd
,
10897 Elf_External_Note
*xnp
;
10904 namesz
= strlen (name
) + 1;
10906 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10908 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10911 dest
= buf
+ *bufsiz
;
10912 *bufsiz
+= newspace
;
10913 xnp
= (Elf_External_Note
*) dest
;
10914 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10915 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10916 H_PUT_32 (abfd
, type
, xnp
->type
);
10920 memcpy (dest
, name
, namesz
);
10928 memcpy (dest
, input
, size
);
10938 /* gcc-8 warns (*) on all the strncpy calls in this function about
10939 possible string truncation. The "truncation" is not a bug. We
10940 have an external representation of structs with fields that are not
10941 necessarily NULL terminated and corresponding internal
10942 representation fields that are one larger so that they can always
10943 be NULL terminated.
10944 gcc versions between 4.2 and 4.6 do not allow pragma control of
10945 diagnostics inside functions, giving a hard error if you try to use
10946 the finer control available with later versions.
10947 gcc prior to 4.2 warns about diagnostic push and pop.
10948 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10949 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10950 (*) Depending on your system header files! */
10951 #if GCC_VERSION >= 8000
10952 # pragma GCC diagnostic push
10953 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10956 elfcore_write_prpsinfo (bfd
*abfd
,
10960 const char *psargs
)
10962 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10964 if (bed
->elf_backend_write_core_note
!= NULL
)
10967 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10968 NT_PRPSINFO
, fname
, psargs
);
10973 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10974 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10975 if (bed
->s
->elfclass
== ELFCLASS32
)
10977 # if defined (HAVE_PSINFO32_T)
10979 int note_type
= NT_PSINFO
;
10982 int note_type
= NT_PRPSINFO
;
10985 memset (&data
, 0, sizeof (data
));
10986 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10987 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10988 return elfcore_write_note (abfd
, buf
, bufsiz
,
10989 "CORE", note_type
, &data
, sizeof (data
));
10994 # if defined (HAVE_PSINFO_T)
10996 int note_type
= NT_PSINFO
;
10999 int note_type
= NT_PRPSINFO
;
11002 memset (&data
, 0, sizeof (data
));
11003 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11004 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11005 return elfcore_write_note (abfd
, buf
, bufsiz
,
11006 "CORE", note_type
, &data
, sizeof (data
));
11008 #endif /* PSINFO_T or PRPSINFO_T */
11013 #if GCC_VERSION >= 8000
11014 # pragma GCC diagnostic pop
11018 elfcore_write_linux_prpsinfo32
11019 (bfd
*abfd
, char *buf
, int *bufsiz
,
11020 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11022 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11024 struct elf_external_linux_prpsinfo32_ugid16 data
;
11026 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11027 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11028 &data
, sizeof (data
));
11032 struct elf_external_linux_prpsinfo32_ugid32 data
;
11034 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11035 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11036 &data
, sizeof (data
));
11041 elfcore_write_linux_prpsinfo64
11042 (bfd
*abfd
, char *buf
, int *bufsiz
,
11043 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11045 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11047 struct elf_external_linux_prpsinfo64_ugid16 data
;
11049 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11050 return elfcore_write_note (abfd
, buf
, bufsiz
,
11051 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11055 struct elf_external_linux_prpsinfo64_ugid32 data
;
11057 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11058 return elfcore_write_note (abfd
, buf
, bufsiz
,
11059 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11064 elfcore_write_prstatus (bfd
*abfd
,
11071 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11073 if (bed
->elf_backend_write_core_note
!= NULL
)
11076 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11078 pid
, cursig
, gregs
);
11083 #if defined (HAVE_PRSTATUS_T)
11084 #if defined (HAVE_PRSTATUS32_T)
11085 if (bed
->s
->elfclass
== ELFCLASS32
)
11087 prstatus32_t prstat
;
11089 memset (&prstat
, 0, sizeof (prstat
));
11090 prstat
.pr_pid
= pid
;
11091 prstat
.pr_cursig
= cursig
;
11092 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11093 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11094 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11101 memset (&prstat
, 0, sizeof (prstat
));
11102 prstat
.pr_pid
= pid
;
11103 prstat
.pr_cursig
= cursig
;
11104 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11105 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11106 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11108 #endif /* HAVE_PRSTATUS_T */
11114 #if defined (HAVE_LWPSTATUS_T)
11116 elfcore_write_lwpstatus (bfd
*abfd
,
11123 lwpstatus_t lwpstat
;
11124 const char *note_name
= "CORE";
11126 memset (&lwpstat
, 0, sizeof (lwpstat
));
11127 lwpstat
.pr_lwpid
= pid
>> 16;
11128 lwpstat
.pr_cursig
= cursig
;
11129 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11130 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11131 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11132 #if !defined(gregs)
11133 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11134 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11136 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11137 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11140 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11141 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11143 #endif /* HAVE_LWPSTATUS_T */
11145 #if defined (HAVE_PSTATUS_T)
11147 elfcore_write_pstatus (bfd
*abfd
,
11151 int cursig ATTRIBUTE_UNUSED
,
11152 const void *gregs ATTRIBUTE_UNUSED
)
11154 const char *note_name
= "CORE";
11155 #if defined (HAVE_PSTATUS32_T)
11156 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11158 if (bed
->s
->elfclass
== ELFCLASS32
)
11162 memset (&pstat
, 0, sizeof (pstat
));
11163 pstat
.pr_pid
= pid
& 0xffff;
11164 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11165 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11173 memset (&pstat
, 0, sizeof (pstat
));
11174 pstat
.pr_pid
= pid
& 0xffff;
11175 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11176 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11180 #endif /* HAVE_PSTATUS_T */
11183 elfcore_write_prfpreg (bfd
*abfd
,
11186 const void *fpregs
,
11189 const char *note_name
= "CORE";
11190 return elfcore_write_note (abfd
, buf
, bufsiz
,
11191 note_name
, NT_FPREGSET
, fpregs
, size
);
11195 elfcore_write_prxfpreg (bfd
*abfd
,
11198 const void *xfpregs
,
11201 char *note_name
= "LINUX";
11202 return elfcore_write_note (abfd
, buf
, bufsiz
,
11203 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11207 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11208 const void *xfpregs
, int size
)
11211 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11212 note_name
= "FreeBSD";
11214 note_name
= "LINUX";
11215 return elfcore_write_note (abfd
, buf
, bufsiz
,
11216 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11220 elfcore_write_ppc_vmx (bfd
*abfd
,
11223 const void *ppc_vmx
,
11226 char *note_name
= "LINUX";
11227 return elfcore_write_note (abfd
, buf
, bufsiz
,
11228 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11232 elfcore_write_ppc_vsx (bfd
*abfd
,
11235 const void *ppc_vsx
,
11238 char *note_name
= "LINUX";
11239 return elfcore_write_note (abfd
, buf
, bufsiz
,
11240 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11244 elfcore_write_ppc_tar (bfd
*abfd
,
11247 const void *ppc_tar
,
11250 char *note_name
= "LINUX";
11251 return elfcore_write_note (abfd
, buf
, bufsiz
,
11252 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11256 elfcore_write_ppc_ppr (bfd
*abfd
,
11259 const void *ppc_ppr
,
11262 char *note_name
= "LINUX";
11263 return elfcore_write_note (abfd
, buf
, bufsiz
,
11264 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11268 elfcore_write_ppc_dscr (bfd
*abfd
,
11271 const void *ppc_dscr
,
11274 char *note_name
= "LINUX";
11275 return elfcore_write_note (abfd
, buf
, bufsiz
,
11276 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11280 elfcore_write_ppc_ebb (bfd
*abfd
,
11283 const void *ppc_ebb
,
11286 char *note_name
= "LINUX";
11287 return elfcore_write_note (abfd
, buf
, bufsiz
,
11288 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11292 elfcore_write_ppc_pmu (bfd
*abfd
,
11295 const void *ppc_pmu
,
11298 char *note_name
= "LINUX";
11299 return elfcore_write_note (abfd
, buf
, bufsiz
,
11300 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11304 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11307 const void *ppc_tm_cgpr
,
11310 char *note_name
= "LINUX";
11311 return elfcore_write_note (abfd
, buf
, bufsiz
,
11312 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11316 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11319 const void *ppc_tm_cfpr
,
11322 char *note_name
= "LINUX";
11323 return elfcore_write_note (abfd
, buf
, bufsiz
,
11324 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11328 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11331 const void *ppc_tm_cvmx
,
11334 char *note_name
= "LINUX";
11335 return elfcore_write_note (abfd
, buf
, bufsiz
,
11336 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11340 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11343 const void *ppc_tm_cvsx
,
11346 char *note_name
= "LINUX";
11347 return elfcore_write_note (abfd
, buf
, bufsiz
,
11348 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11352 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11355 const void *ppc_tm_spr
,
11358 char *note_name
= "LINUX";
11359 return elfcore_write_note (abfd
, buf
, bufsiz
,
11360 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11364 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11367 const void *ppc_tm_ctar
,
11370 char *note_name
= "LINUX";
11371 return elfcore_write_note (abfd
, buf
, bufsiz
,
11372 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11376 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11379 const void *ppc_tm_cppr
,
11382 char *note_name
= "LINUX";
11383 return elfcore_write_note (abfd
, buf
, bufsiz
,
11384 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11388 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11391 const void *ppc_tm_cdscr
,
11394 char *note_name
= "LINUX";
11395 return elfcore_write_note (abfd
, buf
, bufsiz
,
11396 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11400 elfcore_write_s390_high_gprs (bfd
*abfd
,
11403 const void *s390_high_gprs
,
11406 char *note_name
= "LINUX";
11407 return elfcore_write_note (abfd
, buf
, bufsiz
,
11408 note_name
, NT_S390_HIGH_GPRS
,
11409 s390_high_gprs
, size
);
11413 elfcore_write_s390_timer (bfd
*abfd
,
11416 const void *s390_timer
,
11419 char *note_name
= "LINUX";
11420 return elfcore_write_note (abfd
, buf
, bufsiz
,
11421 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11425 elfcore_write_s390_todcmp (bfd
*abfd
,
11428 const void *s390_todcmp
,
11431 char *note_name
= "LINUX";
11432 return elfcore_write_note (abfd
, buf
, bufsiz
,
11433 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11437 elfcore_write_s390_todpreg (bfd
*abfd
,
11440 const void *s390_todpreg
,
11443 char *note_name
= "LINUX";
11444 return elfcore_write_note (abfd
, buf
, bufsiz
,
11445 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11449 elfcore_write_s390_ctrs (bfd
*abfd
,
11452 const void *s390_ctrs
,
11455 char *note_name
= "LINUX";
11456 return elfcore_write_note (abfd
, buf
, bufsiz
,
11457 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11461 elfcore_write_s390_prefix (bfd
*abfd
,
11464 const void *s390_prefix
,
11467 char *note_name
= "LINUX";
11468 return elfcore_write_note (abfd
, buf
, bufsiz
,
11469 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11473 elfcore_write_s390_last_break (bfd
*abfd
,
11476 const void *s390_last_break
,
11479 char *note_name
= "LINUX";
11480 return elfcore_write_note (abfd
, buf
, bufsiz
,
11481 note_name
, NT_S390_LAST_BREAK
,
11482 s390_last_break
, size
);
11486 elfcore_write_s390_system_call (bfd
*abfd
,
11489 const void *s390_system_call
,
11492 char *note_name
= "LINUX";
11493 return elfcore_write_note (abfd
, buf
, bufsiz
,
11494 note_name
, NT_S390_SYSTEM_CALL
,
11495 s390_system_call
, size
);
11499 elfcore_write_s390_tdb (bfd
*abfd
,
11502 const void *s390_tdb
,
11505 char *note_name
= "LINUX";
11506 return elfcore_write_note (abfd
, buf
, bufsiz
,
11507 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11511 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11514 const void *s390_vxrs_low
,
11517 char *note_name
= "LINUX";
11518 return elfcore_write_note (abfd
, buf
, bufsiz
,
11519 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11523 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11526 const void *s390_vxrs_high
,
11529 char *note_name
= "LINUX";
11530 return elfcore_write_note (abfd
, buf
, bufsiz
,
11531 note_name
, NT_S390_VXRS_HIGH
,
11532 s390_vxrs_high
, size
);
11536 elfcore_write_s390_gs_cb (bfd
*abfd
,
11539 const void *s390_gs_cb
,
11542 char *note_name
= "LINUX";
11543 return elfcore_write_note (abfd
, buf
, bufsiz
,
11544 note_name
, NT_S390_GS_CB
,
11549 elfcore_write_s390_gs_bc (bfd
*abfd
,
11552 const void *s390_gs_bc
,
11555 char *note_name
= "LINUX";
11556 return elfcore_write_note (abfd
, buf
, bufsiz
,
11557 note_name
, NT_S390_GS_BC
,
11562 elfcore_write_arm_vfp (bfd
*abfd
,
11565 const void *arm_vfp
,
11568 char *note_name
= "LINUX";
11569 return elfcore_write_note (abfd
, buf
, bufsiz
,
11570 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11574 elfcore_write_aarch_tls (bfd
*abfd
,
11577 const void *aarch_tls
,
11580 char *note_name
= "LINUX";
11581 return elfcore_write_note (abfd
, buf
, bufsiz
,
11582 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11586 elfcore_write_aarch_hw_break (bfd
*abfd
,
11589 const void *aarch_hw_break
,
11592 char *note_name
= "LINUX";
11593 return elfcore_write_note (abfd
, buf
, bufsiz
,
11594 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11598 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11601 const void *aarch_hw_watch
,
11604 char *note_name
= "LINUX";
11605 return elfcore_write_note (abfd
, buf
, bufsiz
,
11606 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11610 elfcore_write_aarch_sve (bfd
*abfd
,
11613 const void *aarch_sve
,
11616 char *note_name
= "LINUX";
11617 return elfcore_write_note (abfd
, buf
, bufsiz
,
11618 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11622 elfcore_write_aarch_pauth (bfd
*abfd
,
11625 const void *aarch_pauth
,
11628 char *note_name
= "LINUX";
11629 return elfcore_write_note (abfd
, buf
, bufsiz
,
11630 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11634 elfcore_write_register_note (bfd
*abfd
,
11637 const char *section
,
11641 if (strcmp (section
, ".reg2") == 0)
11642 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11643 if (strcmp (section
, ".reg-xfp") == 0)
11644 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11645 if (strcmp (section
, ".reg-xstate") == 0)
11646 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11647 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11648 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11649 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11650 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11651 if (strcmp (section
, ".reg-ppc-tar") == 0)
11652 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11653 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11654 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11655 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11656 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11657 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11658 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11659 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11660 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11661 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11662 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11663 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11664 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11665 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11666 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11667 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11668 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11669 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11670 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11671 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11672 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11673 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11674 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11675 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11676 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11677 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11678 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11679 if (strcmp (section
, ".reg-s390-timer") == 0)
11680 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11681 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11682 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11683 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11684 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11685 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11686 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11687 if (strcmp (section
, ".reg-s390-prefix") == 0)
11688 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11689 if (strcmp (section
, ".reg-s390-last-break") == 0)
11690 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11691 if (strcmp (section
, ".reg-s390-system-call") == 0)
11692 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11693 if (strcmp (section
, ".reg-s390-tdb") == 0)
11694 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11695 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11696 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11697 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11698 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11699 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11700 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11701 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11702 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11703 if (strcmp (section
, ".reg-arm-vfp") == 0)
11704 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11705 if (strcmp (section
, ".reg-aarch-tls") == 0)
11706 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11707 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11708 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11709 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11710 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11711 if (strcmp (section
, ".reg-aarch-sve") == 0)
11712 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11713 if (strcmp (section
, ".reg-aarch-pauth") == 0)
11714 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
11719 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11724 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11725 gABI specifies that PT_NOTE alignment should be aligned to 4
11726 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11727 align is less than 4, we use 4 byte alignment. */
11730 if (align
!= 4 && align
!= 8)
11734 while (p
< buf
+ size
)
11736 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11737 Elf_Internal_Note in
;
11739 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11742 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11744 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11745 in
.namedata
= xnp
->name
;
11746 if (in
.namesz
> buf
- in
.namedata
+ size
)
11749 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11750 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11751 in
.descpos
= offset
+ (in
.descdata
- buf
);
11753 && (in
.descdata
>= buf
+ size
11754 || in
.descsz
> buf
- in
.descdata
+ size
))
11757 switch (bfd_get_format (abfd
))
11764 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11767 const char * string
;
11769 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11773 GROKER_ELEMENT ("", elfcore_grok_note
),
11774 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11775 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11776 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11777 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11778 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
11780 #undef GROKER_ELEMENT
11783 for (i
= ARRAY_SIZE (grokers
); i
--;)
11785 if (in
.namesz
>= grokers
[i
].len
11786 && strncmp (in
.namedata
, grokers
[i
].string
,
11787 grokers
[i
].len
) == 0)
11789 if (! grokers
[i
].func (abfd
, & in
))
11798 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
11800 if (! elfobj_grok_gnu_note (abfd
, &in
))
11803 else if (in
.namesz
== sizeof "stapsdt"
11804 && strcmp (in
.namedata
, "stapsdt") == 0)
11806 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
11812 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
11819 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
11824 if (size
== 0 || (size
+ 1) == 0)
11827 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
11830 buf
= (char *) bfd_malloc (size
+ 1);
11834 /* PR 17512: file: ec08f814
11835 0-termintate the buffer so that string searches will not overflow. */
11838 if (bfd_bread (buf
, size
, abfd
) != size
11839 || !elf_parse_notes (abfd
, buf
, size
, offset
, align
))
11849 /* Providing external access to the ELF program header table. */
11851 /* Return an upper bound on the number of bytes required to store a
11852 copy of ABFD's program header table entries. Return -1 if an error
11853 occurs; bfd_get_error will return an appropriate code. */
11856 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
11858 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11860 bfd_set_error (bfd_error_wrong_format
);
11864 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
11867 /* Copy ABFD's program header table entries to *PHDRS. The entries
11868 will be stored as an array of Elf_Internal_Phdr structures, as
11869 defined in include/elf/internal.h. To find out how large the
11870 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11872 Return the number of program header table entries read, or -1 if an
11873 error occurs; bfd_get_error will return an appropriate code. */
11876 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
11880 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11882 bfd_set_error (bfd_error_wrong_format
);
11886 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
11887 if (num_phdrs
!= 0)
11888 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
11889 num_phdrs
* sizeof (Elf_Internal_Phdr
));
11894 enum elf_reloc_type_class
11895 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11896 const asection
*rel_sec ATTRIBUTE_UNUSED
,
11897 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
11899 return reloc_class_normal
;
11902 /* For RELA architectures, return the relocation value for a
11903 relocation against a local symbol. */
11906 _bfd_elf_rela_local_sym (bfd
*abfd
,
11907 Elf_Internal_Sym
*sym
,
11909 Elf_Internal_Rela
*rel
)
11911 asection
*sec
= *psec
;
11912 bfd_vma relocation
;
11914 relocation
= (sec
->output_section
->vma
11915 + sec
->output_offset
11917 if ((sec
->flags
& SEC_MERGE
)
11918 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
11919 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11922 _bfd_merged_section_offset (abfd
, psec
,
11923 elf_section_data (sec
)->sec_info
,
11924 sym
->st_value
+ rel
->r_addend
);
11927 /* If we have changed the section, and our original section is
11928 marked with SEC_EXCLUDE, it means that the original
11929 SEC_MERGE section has been completely subsumed in some
11930 other SEC_MERGE section. In this case, we need to leave
11931 some info around for --emit-relocs. */
11932 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
11933 sec
->kept_section
= *psec
;
11936 rel
->r_addend
-= relocation
;
11937 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
11943 _bfd_elf_rel_local_sym (bfd
*abfd
,
11944 Elf_Internal_Sym
*sym
,
11948 asection
*sec
= *psec
;
11950 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
11951 return sym
->st_value
+ addend
;
11953 return _bfd_merged_section_offset (abfd
, psec
,
11954 elf_section_data (sec
)->sec_info
,
11955 sym
->st_value
+ addend
);
11958 /* Adjust an address within a section. Given OFFSET within SEC, return
11959 the new offset within the section, based upon changes made to the
11960 section. Returns -1 if the offset is now invalid.
11961 The offset (in abnd out) is in target sized bytes, however big a
11965 _bfd_elf_section_offset (bfd
*abfd
,
11966 struct bfd_link_info
*info
,
11970 switch (sec
->sec_info_type
)
11972 case SEC_INFO_TYPE_STABS
:
11973 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
11975 case SEC_INFO_TYPE_EH_FRAME
:
11976 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
11979 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
11981 /* Reverse the offset. */
11982 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11983 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
11985 /* address_size and sec->size are in octets. Convert
11986 to bytes before subtracting the original offset. */
11987 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
11993 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11994 reconstruct an ELF file by reading the segments out of remote memory
11995 based on the ELF file header at EHDR_VMA and the ELF program headers it
11996 points to. If not null, *LOADBASEP is filled in with the difference
11997 between the VMAs from which the segments were read, and the VMAs the
11998 file headers (and hence BFD's idea of each section's VMA) put them at.
12000 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12001 remote memory at target address VMA into the local buffer at MYADDR; it
12002 should return zero on success or an `errno' code on failure. TEMPL must
12003 be a BFD for an ELF target with the word size and byte order found in
12004 the remote memory. */
12007 bfd_elf_bfd_from_remote_memory
12010 bfd_size_type size
,
12011 bfd_vma
*loadbasep
,
12012 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12014 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12015 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12019 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12020 long symcount ATTRIBUTE_UNUSED
,
12021 asymbol
**syms ATTRIBUTE_UNUSED
,
12026 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12029 const char *relplt_name
;
12030 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12034 Elf_Internal_Shdr
*hdr
;
12040 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12043 if (dynsymcount
<= 0)
12046 if (!bed
->plt_sym_val
)
12049 relplt_name
= bed
->relplt_name
;
12050 if (relplt_name
== NULL
)
12051 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12052 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12053 if (relplt
== NULL
)
12056 hdr
= &elf_section_data (relplt
)->this_hdr
;
12057 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12058 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12061 plt
= bfd_get_section_by_name (abfd
, ".plt");
12065 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12066 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12069 count
= relplt
->size
/ hdr
->sh_entsize
;
12070 size
= count
* sizeof (asymbol
);
12071 p
= relplt
->relocation
;
12072 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12074 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12075 if (p
->addend
!= 0)
12078 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12080 size
+= sizeof ("+0x") - 1 + 8;
12085 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12089 names
= (char *) (s
+ count
);
12090 p
= relplt
->relocation
;
12092 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12097 addr
= bed
->plt_sym_val (i
, plt
, p
);
12098 if (addr
== (bfd_vma
) -1)
12101 *s
= **p
->sym_ptr_ptr
;
12102 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12103 we are defining a symbol, ensure one of them is set. */
12104 if ((s
->flags
& BSF_LOCAL
) == 0)
12105 s
->flags
|= BSF_GLOBAL
;
12106 s
->flags
|= BSF_SYNTHETIC
;
12108 s
->value
= addr
- plt
->vma
;
12111 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12112 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12114 if (p
->addend
!= 0)
12118 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12119 names
+= sizeof ("+0x") - 1;
12120 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12121 for (a
= buf
; *a
== '0'; ++a
)
12124 memcpy (names
, a
, len
);
12127 memcpy (names
, "@plt", sizeof ("@plt"));
12128 names
+= sizeof ("@plt");
12135 /* It is only used by x86-64 so far.
12136 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12137 but current usage would allow all of _bfd_std_section to be zero. */
12138 static const asymbol lcomm_sym
12139 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12140 asection _bfd_elf_large_com_section
12141 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12142 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12145 _bfd_elf_post_process_headers (bfd
*abfd ATTRIBUTE_UNUSED
,
12146 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
12151 _bfd_elf_final_write_processing (bfd
*abfd
)
12153 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12155 i_ehdrp
= elf_elfheader (abfd
);
12157 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12158 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12160 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12161 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12162 STB_GNU_UNIQUE binding. */
12163 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12165 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12166 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12167 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12168 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12170 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12171 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12172 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12173 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12174 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12175 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12176 bfd_set_error (bfd_error_bad_value
);
12184 /* Return TRUE for ELF symbol types that represent functions.
12185 This is the default version of this function, which is sufficient for
12186 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12189 _bfd_elf_is_function_type (unsigned int type
)
12191 return (type
== STT_FUNC
12192 || type
== STT_GNU_IFUNC
);
12195 /* If the ELF symbol SYM might be a function in SEC, return the
12196 function size and set *CODE_OFF to the function's entry point,
12197 otherwise return zero. */
12200 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12203 bfd_size_type size
;
12205 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12206 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12207 || sym
->section
!= sec
)
12210 *code_off
= sym
->value
;
12212 if (!(sym
->flags
& BSF_SYNTHETIC
))
12213 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;