1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2020 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
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
56 file_ptr offset
, size_t align
);
58 /* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
62 /* Swap in a Verdef structure. */
65 _bfd_elf_swap_verdef_in (bfd
*abfd
,
66 const Elf_External_Verdef
*src
,
67 Elf_Internal_Verdef
*dst
)
69 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
70 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
71 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
72 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
73 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
74 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
75 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
78 /* Swap out a Verdef structure. */
81 _bfd_elf_swap_verdef_out (bfd
*abfd
,
82 const Elf_Internal_Verdef
*src
,
83 Elf_External_Verdef
*dst
)
85 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
86 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
87 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
88 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
89 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
90 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
91 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
94 /* Swap in a Verdaux structure. */
97 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
98 const Elf_External_Verdaux
*src
,
99 Elf_Internal_Verdaux
*dst
)
101 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
102 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
105 /* Swap out a Verdaux structure. */
108 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
109 const Elf_Internal_Verdaux
*src
,
110 Elf_External_Verdaux
*dst
)
112 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
113 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
116 /* Swap in a Verneed structure. */
119 _bfd_elf_swap_verneed_in (bfd
*abfd
,
120 const Elf_External_Verneed
*src
,
121 Elf_Internal_Verneed
*dst
)
123 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
124 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
125 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
126 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
127 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
130 /* Swap out a Verneed structure. */
133 _bfd_elf_swap_verneed_out (bfd
*abfd
,
134 const Elf_Internal_Verneed
*src
,
135 Elf_External_Verneed
*dst
)
137 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
138 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
139 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
140 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
141 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
144 /* Swap in a Vernaux structure. */
147 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
148 const Elf_External_Vernaux
*src
,
149 Elf_Internal_Vernaux
*dst
)
151 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
152 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
153 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
154 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
155 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
158 /* Swap out a Vernaux structure. */
161 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
162 const Elf_Internal_Vernaux
*src
,
163 Elf_External_Vernaux
*dst
)
165 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
166 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
167 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
168 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
169 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
172 /* Swap in a Versym structure. */
175 _bfd_elf_swap_versym_in (bfd
*abfd
,
176 const Elf_External_Versym
*src
,
177 Elf_Internal_Versym
*dst
)
179 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
182 /* Swap out a Versym structure. */
185 _bfd_elf_swap_versym_out (bfd
*abfd
,
186 const Elf_Internal_Versym
*src
,
187 Elf_External_Versym
*dst
)
189 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
192 /* Standard ELF hash function. Do not change this function; you will
193 cause invalid hash tables to be generated. */
196 bfd_elf_hash (const char *namearg
)
198 const unsigned char *name
= (const unsigned char *) namearg
;
203 while ((ch
= *name
++) != '\0')
206 if ((g
= (h
& 0xf0000000)) != 0)
209 /* The ELF ABI says `h &= ~g', but this is equivalent in
210 this case and on some machines one insn instead of two. */
214 return h
& 0xffffffff;
217 /* DT_GNU_HASH hash function. Do not change this function; you will
218 cause invalid hash tables to be generated. */
221 bfd_elf_gnu_hash (const char *namearg
)
223 const unsigned char *name
= (const unsigned char *) namearg
;
224 unsigned long h
= 5381;
227 while ((ch
= *name
++) != '\0')
228 h
= (h
<< 5) + h
+ ch
;
229 return h
& 0xffffffff;
232 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
233 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 bfd_elf_allocate_object (bfd
*abfd
,
237 enum elf_target_id object_id
)
239 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
240 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
241 if (abfd
->tdata
.any
== NULL
)
244 elf_object_id (abfd
) = object_id
;
245 if (abfd
->direction
!= read_direction
)
247 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
250 elf_tdata (abfd
)->o
= o
;
251 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
258 bfd_elf_make_object (bfd
*abfd
)
260 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
261 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
266 bfd_elf_mkcorefile (bfd
*abfd
)
268 /* I think this can be done just like an object file. */
269 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
271 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
272 return elf_tdata (abfd
)->core
!= NULL
;
276 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
278 Elf_Internal_Shdr
**i_shdrp
;
279 bfd_byte
*shstrtab
= NULL
;
281 bfd_size_type shstrtabsize
;
283 i_shdrp
= elf_elfsections (abfd
);
285 || shindex
>= elf_numsections (abfd
)
286 || i_shdrp
[shindex
] == 0)
289 shstrtab
= i_shdrp
[shindex
]->contents
;
290 if (shstrtab
== NULL
)
292 /* No cached one, attempt to read, and cache what we read. */
293 offset
= i_shdrp
[shindex
]->sh_offset
;
294 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
296 /* Allocate and clear an extra byte at the end, to prevent crashes
297 in case the string table is not terminated. */
298 if (shstrtabsize
+ 1 <= 1
299 || shstrtabsize
> bfd_get_file_size (abfd
)
300 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
301 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
303 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
305 if (bfd_get_error () != bfd_error_system_call
)
306 bfd_set_error (bfd_error_file_truncated
);
307 bfd_release (abfd
, shstrtab
);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp
[shindex
]->sh_size
= 0;
315 shstrtab
[shstrtabsize
] = '\0';
316 i_shdrp
[shindex
]->contents
= shstrtab
;
318 return (char *) shstrtab
;
322 bfd_elf_string_from_elf_section (bfd
*abfd
,
323 unsigned int shindex
,
324 unsigned int strindex
)
326 Elf_Internal_Shdr
*hdr
;
331 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
)
338 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
340 /* PR 17512: file: f057ec89. */
341 /* xgettext:c-format */
342 _bfd_error_handler (_("%pB: attempt to load strings from"
343 " a non-string section (number %d)"),
348 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
353 /* PR 24273: The string section's contents may have already
354 been loaded elsewhere, eg because a corrupt file has the
355 string section index in the ELF header pointing at a group
356 section. So be paranoid, and test that the last byte of
357 the section is zero. */
358 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
362 if (strindex
>= hdr
->sh_size
)
364 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
366 /* xgettext:c-format */
367 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
368 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
369 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
371 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
375 return ((char *) hdr
->contents
) + strindex
;
378 /* Read and convert symbols to internal format.
379 SYMCOUNT specifies the number of symbols to read, starting from
380 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
381 are non-NULL, they are used to store the internal symbols, external
382 symbols, and symbol section index extensions, respectively.
383 Returns a pointer to the internal symbol buffer (malloced if necessary)
384 or NULL if there were no symbols or some kind of problem. */
387 bfd_elf_get_elf_syms (bfd
*ibfd
,
388 Elf_Internal_Shdr
*symtab_hdr
,
391 Elf_Internal_Sym
*intsym_buf
,
393 Elf_External_Sym_Shndx
*extshndx_buf
)
395 Elf_Internal_Shdr
*shndx_hdr
;
397 const bfd_byte
*esym
;
398 Elf_External_Sym_Shndx
*alloc_extshndx
;
399 Elf_External_Sym_Shndx
*shndx
;
400 Elf_Internal_Sym
*alloc_intsym
;
401 Elf_Internal_Sym
*isym
;
402 Elf_Internal_Sym
*isymend
;
403 const struct elf_backend_data
*bed
;
408 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
414 /* Normal syms might have section extension entries. */
416 if (elf_symtab_shndx_list (ibfd
) != NULL
)
418 elf_section_list
* entry
;
419 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
421 /* Find an index section that is linked to this symtab section. */
422 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
425 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
428 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
430 shndx_hdr
= & entry
->hdr
;
435 if (shndx_hdr
== NULL
)
437 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
438 /* Not really accurate, but this was how the old code used to work. */
439 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
440 /* Otherwise we do nothing. The assumption is that
441 the index table will not be needed. */
445 /* Read the symbols. */
447 alloc_extshndx
= NULL
;
449 bed
= get_elf_backend_data (ibfd
);
450 extsym_size
= bed
->s
->sizeof_sym
;
451 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
453 bfd_set_error (bfd_error_file_too_big
);
457 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
458 if (extsym_buf
== NULL
)
460 alloc_ext
= bfd_malloc (amt
);
461 extsym_buf
= alloc_ext
;
463 if (extsym_buf
== NULL
464 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
465 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
471 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
475 if (_bfd_mul_overflow (symcount
, sizeof (Elf_External_Sym_Shndx
), &amt
))
477 bfd_set_error (bfd_error_file_too_big
);
481 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
482 if (extshndx_buf
== NULL
)
484 alloc_extshndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
485 extshndx_buf
= alloc_extshndx
;
487 if (extshndx_buf
== NULL
488 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
489 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
496 if (intsym_buf
== NULL
)
498 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
500 bfd_set_error (bfd_error_file_too_big
);
503 alloc_intsym
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
504 intsym_buf
= alloc_intsym
;
505 if (intsym_buf
== NULL
)
509 /* Convert the symbols to internal form. */
510 isymend
= intsym_buf
+ symcount
;
511 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
512 shndx
= extshndx_buf
;
514 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
515 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
517 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
518 /* xgettext:c-format */
519 _bfd_error_handler (_("%pB symbol number %lu references"
520 " nonexistent SHT_SYMTAB_SHNDX section"),
521 ibfd
, (unsigned long) symoffset
);
522 if (alloc_intsym
!= NULL
)
529 if (alloc_ext
!= NULL
)
531 if (alloc_extshndx
!= NULL
)
532 free (alloc_extshndx
);
537 /* Look up a symbol name. */
539 bfd_elf_sym_name (bfd
*abfd
,
540 Elf_Internal_Shdr
*symtab_hdr
,
541 Elf_Internal_Sym
*isym
,
545 unsigned int iname
= isym
->st_name
;
546 unsigned int shindex
= symtab_hdr
->sh_link
;
548 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
549 /* Check for a bogus st_shndx to avoid crashing. */
550 && isym
->st_shndx
< elf_numsections (abfd
))
552 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
553 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
556 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
559 else if (sym_sec
&& *name
== '\0')
560 name
= bfd_section_name (sym_sec
);
565 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
566 sections. The first element is the flags, the rest are section
569 typedef union elf_internal_group
{
570 Elf_Internal_Shdr
*shdr
;
572 } Elf_Internal_Group
;
574 /* Return the name of the group signature symbol. Why isn't the
575 signature just a string? */
578 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
580 Elf_Internal_Shdr
*hdr
;
581 unsigned char esym
[sizeof (Elf64_External_Sym
)];
582 Elf_External_Sym_Shndx eshndx
;
583 Elf_Internal_Sym isym
;
585 /* First we need to ensure the symbol table is available. Make sure
586 that it is a symbol table section. */
587 if (ghdr
->sh_link
>= elf_numsections (abfd
))
589 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
590 if (hdr
->sh_type
!= SHT_SYMTAB
591 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
594 /* Go read the symbol. */
595 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
596 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
597 &isym
, esym
, &eshndx
) == NULL
)
600 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
603 /* Set next_in_group list pointer, and group name for NEWSECT. */
606 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
608 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
610 /* If num_group is zero, read in all SHT_GROUP sections. The count
611 is set to -1 if there are no SHT_GROUP sections. */
614 unsigned int i
, shnum
;
616 /* First count the number of groups. If we have a SHT_GROUP
617 section with just a flag word (ie. sh_size is 4), ignore it. */
618 shnum
= elf_numsections (abfd
);
621 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
622 ( (shdr)->sh_type == SHT_GROUP \
623 && (shdr)->sh_size >= minsize \
624 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
625 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
627 for (i
= 0; i
< shnum
; i
++)
629 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
631 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
637 num_group
= (unsigned) -1;
638 elf_tdata (abfd
)->num_group
= num_group
;
639 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
643 /* We keep a list of elf section headers for group sections,
644 so we can find them quickly. */
647 elf_tdata (abfd
)->num_group
= num_group
;
648 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
649 elf_tdata (abfd
)->group_sect_ptr
650 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
651 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
655 for (i
= 0; i
< shnum
; i
++)
657 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
659 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
662 Elf_Internal_Group
*dest
;
664 /* Make sure the group section has a BFD section
666 if (!bfd_section_from_shdr (abfd
, i
))
669 /* Add to list of sections. */
670 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
673 /* Read the raw contents. */
674 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
675 shdr
->contents
= NULL
;
676 if (_bfd_mul_overflow (shdr
->sh_size
,
677 sizeof (*dest
) / 4, &amt
)
678 || (shdr
->contents
= bfd_alloc (abfd
, amt
)) == NULL
679 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
680 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
684 /* xgettext:c-format */
685 (_("%pB: invalid size field in group section"
686 " header: %#" PRIx64
""),
687 abfd
, (uint64_t) shdr
->sh_size
);
688 bfd_set_error (bfd_error_bad_value
);
690 /* PR 17510: If the group contents are even
691 partially corrupt, do not allow any of the
692 contents to be used. */
693 if (shdr
->contents
!= NULL
)
695 bfd_release (abfd
, shdr
->contents
);
696 shdr
->contents
= NULL
;
701 /* Translate raw contents, a flag word followed by an
702 array of elf section indices all in target byte order,
703 to the flag word followed by an array of elf section
705 src
= shdr
->contents
+ shdr
->sh_size
;
706 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
714 idx
= H_GET_32 (abfd
, src
);
715 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 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
980 if (elf_sec_group (sec
) != NULL
)
981 return elf_group_name (sec
);
986 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
988 unsigned int len
= strlen (name
);
989 char *new_name
= bfd_alloc (abfd
, len
+ 2);
990 if (new_name
== NULL
)
994 memcpy (new_name
+ 2, name
+ 1, len
);
999 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
1001 unsigned int len
= strlen (name
);
1002 char *new_name
= bfd_alloc (abfd
, len
);
1003 if (new_name
== NULL
)
1006 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1010 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
1014 int16_t major_version
;
1015 int16_t minor_version
;
1016 unsigned char slim_object
;
1018 /* Flags is a private field that is not defined publicly. */
1022 /* Make a BFD section from an ELF section. We store a pointer to the
1023 BFD section in the bfd_section field of the header. */
1026 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1027 Elf_Internal_Shdr
*hdr
,
1033 const struct elf_backend_data
*bed
;
1035 if (hdr
->bfd_section
!= NULL
)
1038 newsect
= bfd_make_section_anyway (abfd
, name
);
1039 if (newsect
== NULL
)
1042 hdr
->bfd_section
= newsect
;
1043 elf_section_data (newsect
)->this_hdr
= *hdr
;
1044 elf_section_data (newsect
)->this_idx
= shindex
;
1046 /* Always use the real type/flags. */
1047 elf_section_type (newsect
) = hdr
->sh_type
;
1048 elf_section_flags (newsect
) = hdr
->sh_flags
;
1050 newsect
->filepos
= hdr
->sh_offset
;
1052 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
)
1053 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1054 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
)))
1057 flags
= SEC_NO_FLAGS
;
1058 if (hdr
->sh_type
!= SHT_NOBITS
)
1059 flags
|= SEC_HAS_CONTENTS
;
1060 if (hdr
->sh_type
== SHT_GROUP
)
1062 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1065 if (hdr
->sh_type
!= SHT_NOBITS
)
1068 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1069 flags
|= SEC_READONLY
;
1070 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1072 else if ((flags
& SEC_LOAD
) != 0)
1074 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1077 newsect
->entsize
= hdr
->sh_entsize
;
1079 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1080 flags
|= SEC_STRINGS
;
1081 if (hdr
->sh_flags
& SHF_GROUP
)
1082 if (!setup_group (abfd
, hdr
, newsect
))
1084 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1085 flags
|= SEC_THREAD_LOCAL
;
1086 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1087 flags
|= SEC_EXCLUDE
;
1089 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1091 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1092 but binutils as of 2019-07-23 did not set the EI_OSABI header
1096 case ELFOSABI_FREEBSD
:
1097 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1098 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1102 if ((flags
& SEC_ALLOC
) == 0)
1104 /* The debugging sections appear to be recognized only by name,
1105 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1106 if (name
[0] == '.')
1108 if (strncmp (name
, ".debug", 6) == 0
1109 || strncmp (name
, ".gnu.linkonce.wi.", 17) == 0
1110 || strncmp (name
, ".zdebug", 7) == 0)
1111 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1112 else if (strncmp (name
, GNU_BUILD_ATTRS_SECTION_NAME
, 21) == 0
1113 || strncmp (name
, ".note.gnu", 9) == 0)
1114 flags
|= SEC_ELF_OCTETS
;
1115 else if (strncmp (name
, ".line", 5) == 0
1116 || strncmp (name
, ".stab", 5) == 0
1117 || strcmp (name
, ".gdb_index") == 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 (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 (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 size_t amt
= elf_numsections (abfd
) * sizeof (bfd_boolean
);
2076 sections_being_created
= (bfd_boolean
*) bfd_zalloc (abfd
, amt
);
2077 if (sections_being_created
== NULL
)
2079 sections_being_created_abfd
= abfd
;
2081 if (sections_being_created
[shindex
])
2084 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2087 sections_being_created
[shindex
] = TRUE
;
2090 hdr
= elf_elfsections (abfd
)[shindex
];
2091 ehdr
= elf_elfheader (abfd
);
2092 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2097 bed
= get_elf_backend_data (abfd
);
2098 switch (hdr
->sh_type
)
2101 /* Inactive section. Throw it away. */
2104 case SHT_PROGBITS
: /* Normal section with contents. */
2105 case SHT_NOBITS
: /* .bss section. */
2106 case SHT_HASH
: /* .hash section. */
2107 case SHT_NOTE
: /* .note section. */
2108 case SHT_INIT_ARRAY
: /* .init_array section. */
2109 case SHT_FINI_ARRAY
: /* .fini_array section. */
2110 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2111 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2112 case SHT_GNU_HASH
: /* .gnu.hash section. */
2113 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2116 case SHT_DYNAMIC
: /* Dynamic linking information. */
2117 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2120 if (hdr
->sh_link
> elf_numsections (abfd
))
2122 /* PR 10478: Accept Solaris binaries with a sh_link
2123 field set to SHN_BEFORE or SHN_AFTER. */
2124 switch (bfd_get_arch (abfd
))
2127 case bfd_arch_sparc
:
2128 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2129 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2131 /* Otherwise fall through. */
2136 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2138 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2140 Elf_Internal_Shdr
*dynsymhdr
;
2142 /* The shared libraries distributed with hpux11 have a bogus
2143 sh_link field for the ".dynamic" section. Find the
2144 string table for the ".dynsym" section instead. */
2145 if (elf_dynsymtab (abfd
) != 0)
2147 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2148 hdr
->sh_link
= dynsymhdr
->sh_link
;
2152 unsigned int i
, num_sec
;
2154 num_sec
= elf_numsections (abfd
);
2155 for (i
= 1; i
< num_sec
; i
++)
2157 dynsymhdr
= elf_elfsections (abfd
)[i
];
2158 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2160 hdr
->sh_link
= dynsymhdr
->sh_link
;
2168 case SHT_SYMTAB
: /* A symbol table. */
2169 if (elf_onesymtab (abfd
) == shindex
)
2172 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2175 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2177 if (hdr
->sh_size
!= 0)
2179 /* Some assemblers erroneously set sh_info to one with a
2180 zero sh_size. ld sees this as a global symbol count
2181 of (unsigned) -1. Fix it here. */
2186 /* PR 18854: A binary might contain more than one symbol table.
2187 Unusual, but possible. Warn, but continue. */
2188 if (elf_onesymtab (abfd
) != 0)
2191 /* xgettext:c-format */
2192 (_("%pB: warning: multiple symbol tables detected"
2193 " - ignoring the table in section %u"),
2197 elf_onesymtab (abfd
) = shindex
;
2198 elf_symtab_hdr (abfd
) = *hdr
;
2199 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2200 abfd
->flags
|= HAS_SYMS
;
2202 /* Sometimes a shared object will map in the symbol table. If
2203 SHF_ALLOC is set, and this is a shared object, then we also
2204 treat this section as a BFD section. We can not base the
2205 decision purely on SHF_ALLOC, because that flag is sometimes
2206 set in a relocatable object file, which would confuse the
2208 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2209 && (abfd
->flags
& DYNAMIC
) != 0
2210 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2214 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2215 can't read symbols without that section loaded as well. It
2216 is most likely specified by the next section header. */
2218 elf_section_list
* entry
;
2219 unsigned int i
, num_sec
;
2221 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2222 if (entry
->hdr
.sh_link
== shindex
)
2225 num_sec
= elf_numsections (abfd
);
2226 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2228 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2230 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2231 && hdr2
->sh_link
== shindex
)
2236 for (i
= 1; i
< shindex
; i
++)
2238 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2240 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2241 && hdr2
->sh_link
== shindex
)
2246 ret
= bfd_section_from_shdr (abfd
, i
);
2247 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2251 case SHT_DYNSYM
: /* A dynamic symbol table. */
2252 if (elf_dynsymtab (abfd
) == shindex
)
2255 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2258 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2260 if (hdr
->sh_size
!= 0)
2263 /* Some linkers erroneously set sh_info to one with a
2264 zero sh_size. ld sees this as a global symbol count
2265 of (unsigned) -1. Fix it here. */
2270 /* PR 18854: A binary might contain more than one dynamic symbol table.
2271 Unusual, but possible. Warn, but continue. */
2272 if (elf_dynsymtab (abfd
) != 0)
2275 /* xgettext:c-format */
2276 (_("%pB: warning: multiple dynamic symbol tables detected"
2277 " - ignoring the table in section %u"),
2281 elf_dynsymtab (abfd
) = shindex
;
2282 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2283 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2284 abfd
->flags
|= HAS_SYMS
;
2286 /* Besides being a symbol table, we also treat this as a regular
2287 section, so that objcopy can handle it. */
2288 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2291 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2293 elf_section_list
* entry
;
2295 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2296 if (entry
->ndx
== shindex
)
2299 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2302 entry
->ndx
= shindex
;
2304 entry
->next
= elf_symtab_shndx_list (abfd
);
2305 elf_symtab_shndx_list (abfd
) = entry
;
2306 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2310 case SHT_STRTAB
: /* A string table. */
2311 if (hdr
->bfd_section
!= NULL
)
2314 if (ehdr
->e_shstrndx
== shindex
)
2316 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2317 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2321 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2324 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2325 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2329 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2332 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2333 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2334 elf_elfsections (abfd
)[shindex
] = hdr
;
2335 /* We also treat this as a regular section, so that objcopy
2337 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2342 /* If the string table isn't one of the above, then treat it as a
2343 regular section. We need to scan all the headers to be sure,
2344 just in case this strtab section appeared before the above. */
2345 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2347 unsigned int i
, num_sec
;
2349 num_sec
= elf_numsections (abfd
);
2350 for (i
= 1; i
< num_sec
; i
++)
2352 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2353 if (hdr2
->sh_link
== shindex
)
2355 /* Prevent endless recursion on broken objects. */
2358 if (! bfd_section_from_shdr (abfd
, i
))
2360 if (elf_onesymtab (abfd
) == i
)
2362 if (elf_dynsymtab (abfd
) == i
)
2363 goto dynsymtab_strtab
;
2367 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2372 /* *These* do a lot of work -- but build no sections! */
2374 asection
*target_sect
;
2375 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2376 unsigned int num_sec
= elf_numsections (abfd
);
2377 struct bfd_elf_section_data
*esdt
;
2380 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2381 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2384 /* Check for a bogus link to avoid crashing. */
2385 if (hdr
->sh_link
>= num_sec
)
2388 /* xgettext:c-format */
2389 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2390 abfd
, hdr
->sh_link
, name
, shindex
);
2391 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2396 /* For some incomprehensible reason Oracle distributes
2397 libraries for Solaris in which some of the objects have
2398 bogus sh_link fields. It would be nice if we could just
2399 reject them, but, unfortunately, some people need to use
2400 them. We scan through the section headers; if we find only
2401 one suitable symbol table, we clobber the sh_link to point
2402 to it. I hope this doesn't break anything.
2404 Don't do it on executable nor shared library. */
2405 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2406 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2407 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2413 for (scan
= 1; scan
< num_sec
; scan
++)
2415 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2416 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2427 hdr
->sh_link
= found
;
2430 /* Get the symbol table. */
2431 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2432 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2433 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2436 /* If this is an alloc section in an executable or shared
2437 library, or the reloc section does not use the main symbol
2438 table we don't treat it as a reloc section. BFD can't
2439 adequately represent such a section, so at least for now,
2440 we don't try. We just present it as a normal section. We
2441 also can't use it as a reloc section if it points to the
2442 null section, an invalid section, another reloc section, or
2443 its sh_link points to the null section. */
2444 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2445 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2446 || hdr
->sh_link
== SHN_UNDEF
2447 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2448 || hdr
->sh_info
== SHN_UNDEF
2449 || hdr
->sh_info
>= num_sec
2450 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2451 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2453 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2458 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2461 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2462 if (target_sect
== NULL
)
2465 esdt
= elf_section_data (target_sect
);
2466 if (hdr
->sh_type
== SHT_RELA
)
2467 p_hdr
= &esdt
->rela
.hdr
;
2469 p_hdr
= &esdt
->rel
.hdr
;
2471 /* PR 17512: file: 0b4f81b7.
2472 Also see PR 24456, for a file which deliberately has two reloc
2477 /* xgettext:c-format */
2478 (_("%pB: warning: multiple relocation sections for section %pA \
2479 found - ignoring all but the first"),
2483 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2488 elf_elfsections (abfd
)[shindex
] = hdr2
;
2489 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2490 * bed
->s
->int_rels_per_ext_rel
);
2491 target_sect
->flags
|= SEC_RELOC
;
2492 target_sect
->relocation
= NULL
;
2493 target_sect
->rel_filepos
= hdr
->sh_offset
;
2494 /* In the section to which the relocations apply, mark whether
2495 its relocations are of the REL or RELA variety. */
2496 if (hdr
->sh_size
!= 0)
2498 if (hdr
->sh_type
== SHT_RELA
)
2499 target_sect
->use_rela_p
= 1;
2501 abfd
->flags
|= HAS_RELOC
;
2505 case SHT_GNU_verdef
:
2506 elf_dynverdef (abfd
) = shindex
;
2507 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2508 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2511 case SHT_GNU_versym
:
2512 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2515 elf_dynversym (abfd
) = shindex
;
2516 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2517 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2520 case SHT_GNU_verneed
:
2521 elf_dynverref (abfd
) = shindex
;
2522 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2523 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2530 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2533 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2539 /* Possibly an attributes section. */
2540 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2541 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2543 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2545 _bfd_elf_parse_attributes (abfd
, hdr
);
2549 /* Check for any processor-specific section types. */
2550 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2553 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2555 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2556 /* FIXME: How to properly handle allocated section reserved
2557 for applications? */
2559 /* xgettext:c-format */
2560 (_("%pB: unknown type [%#x] section `%s'"),
2561 abfd
, hdr
->sh_type
, name
);
2564 /* Allow sections reserved for applications. */
2565 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2570 else if (hdr
->sh_type
>= SHT_LOPROC
2571 && hdr
->sh_type
<= SHT_HIPROC
)
2572 /* FIXME: We should handle this section. */
2574 /* xgettext:c-format */
2575 (_("%pB: unknown type [%#x] section `%s'"),
2576 abfd
, hdr
->sh_type
, name
);
2577 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2579 /* Unrecognised OS-specific sections. */
2580 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2581 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2582 required to correctly process the section and the file should
2583 be rejected with an error message. */
2585 /* xgettext:c-format */
2586 (_("%pB: unknown type [%#x] section `%s'"),
2587 abfd
, hdr
->sh_type
, name
);
2590 /* Otherwise it should be processed. */
2591 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2596 /* FIXME: We should handle this section. */
2598 /* xgettext:c-format */
2599 (_("%pB: unknown type [%#x] section `%s'"),
2600 abfd
, hdr
->sh_type
, name
);
2608 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2609 sections_being_created
[shindex
] = FALSE
;
2610 if (-- nesting
== 0)
2612 sections_being_created
= NULL
;
2613 sections_being_created_abfd
= abfd
;
2618 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2621 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2623 unsigned long r_symndx
)
2625 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2627 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2629 Elf_Internal_Shdr
*symtab_hdr
;
2630 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2631 Elf_External_Sym_Shndx eshndx
;
2633 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2634 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2635 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2638 if (cache
->abfd
!= abfd
)
2640 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2643 cache
->indx
[ent
] = r_symndx
;
2646 return &cache
->sym
[ent
];
2649 /* Given an ELF section number, retrieve the corresponding BFD
2653 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2655 if (sec_index
>= elf_numsections (abfd
))
2657 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2660 static const struct bfd_elf_special_section special_sections_b
[] =
2662 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2663 { NULL
, 0, 0, 0, 0 }
2666 static const struct bfd_elf_special_section special_sections_c
[] =
2668 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2669 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2670 { NULL
, 0, 0, 0, 0 }
2673 static const struct bfd_elf_special_section special_sections_d
[] =
2675 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2676 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2677 /* There are more DWARF sections than these, but they needn't be added here
2678 unless you have to cope with broken compilers that don't emit section
2679 attributes or you want to help the user writing assembler. */
2680 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2681 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2682 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2683 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2684 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2685 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2686 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2687 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2688 { NULL
, 0, 0, 0, 0 }
2691 static const struct bfd_elf_special_section special_sections_f
[] =
2693 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2694 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2695 { NULL
, 0 , 0, 0, 0 }
2698 static const struct bfd_elf_special_section special_sections_g
[] =
2700 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2701 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2702 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2703 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2704 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2705 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2706 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2707 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2708 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2709 { NULL
, 0, 0, 0, 0 }
2712 static const struct bfd_elf_special_section special_sections_h
[] =
2714 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2715 { NULL
, 0, 0, 0, 0 }
2718 static const struct bfd_elf_special_section special_sections_i
[] =
2720 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2721 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2722 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2723 { NULL
, 0, 0, 0, 0 }
2726 static const struct bfd_elf_special_section special_sections_l
[] =
2728 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2729 { NULL
, 0, 0, 0, 0 }
2732 static const struct bfd_elf_special_section special_sections_n
[] =
2734 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2735 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2736 { NULL
, 0, 0, 0, 0 }
2739 static const struct bfd_elf_special_section special_sections_p
[] =
2741 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2742 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2743 { NULL
, 0, 0, 0, 0 }
2746 static const struct bfd_elf_special_section special_sections_r
[] =
2748 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2749 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2750 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2751 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2752 { NULL
, 0, 0, 0, 0 }
2755 static const struct bfd_elf_special_section special_sections_s
[] =
2757 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2758 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2759 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2760 /* See struct bfd_elf_special_section declaration for the semantics of
2761 this special case where .prefix_length != strlen (.prefix). */
2762 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2763 { NULL
, 0, 0, 0, 0 }
2766 static const struct bfd_elf_special_section special_sections_t
[] =
2768 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2769 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2770 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2771 { NULL
, 0, 0, 0, 0 }
2774 static const struct bfd_elf_special_section special_sections_z
[] =
2776 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2777 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2778 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2779 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2780 { NULL
, 0, 0, 0, 0 }
2783 static const struct bfd_elf_special_section
* const special_sections
[] =
2785 special_sections_b
, /* 'b' */
2786 special_sections_c
, /* 'c' */
2787 special_sections_d
, /* 'd' */
2789 special_sections_f
, /* 'f' */
2790 special_sections_g
, /* 'g' */
2791 special_sections_h
, /* 'h' */
2792 special_sections_i
, /* 'i' */
2795 special_sections_l
, /* 'l' */
2797 special_sections_n
, /* 'n' */
2799 special_sections_p
, /* 'p' */
2801 special_sections_r
, /* 'r' */
2802 special_sections_s
, /* 's' */
2803 special_sections_t
, /* 't' */
2809 special_sections_z
/* 'z' */
2812 const struct bfd_elf_special_section
*
2813 _bfd_elf_get_special_section (const char *name
,
2814 const struct bfd_elf_special_section
*spec
,
2820 len
= strlen (name
);
2822 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2825 int prefix_len
= spec
[i
].prefix_length
;
2827 if (len
< prefix_len
)
2829 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2832 suffix_len
= spec
[i
].suffix_length
;
2833 if (suffix_len
<= 0)
2835 if (name
[prefix_len
] != 0)
2837 if (suffix_len
== 0)
2839 if (name
[prefix_len
] != '.'
2840 && (suffix_len
== -2
2841 || (rela
&& spec
[i
].type
== SHT_REL
)))
2847 if (len
< prefix_len
+ suffix_len
)
2849 if (memcmp (name
+ len
- suffix_len
,
2850 spec
[i
].prefix
+ prefix_len
,
2860 const struct bfd_elf_special_section
*
2861 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2864 const struct bfd_elf_special_section
*spec
;
2865 const struct elf_backend_data
*bed
;
2867 /* See if this is one of the special sections. */
2868 if (sec
->name
== NULL
)
2871 bed
= get_elf_backend_data (abfd
);
2872 spec
= bed
->special_sections
;
2875 spec
= _bfd_elf_get_special_section (sec
->name
,
2876 bed
->special_sections
,
2882 if (sec
->name
[0] != '.')
2885 i
= sec
->name
[1] - 'b';
2886 if (i
< 0 || i
> 'z' - 'b')
2889 spec
= special_sections
[i
];
2894 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2898 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2900 struct bfd_elf_section_data
*sdata
;
2901 const struct elf_backend_data
*bed
;
2902 const struct bfd_elf_special_section
*ssect
;
2904 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2907 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2911 sec
->used_by_bfd
= sdata
;
2914 /* Indicate whether or not this section should use RELA relocations. */
2915 bed
= get_elf_backend_data (abfd
);
2916 sec
->use_rela_p
= bed
->default_use_rela_p
;
2918 /* When we read a file, we don't need to set ELF section type and
2919 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2920 anyway. We will set ELF section type and flags for all linker
2921 created sections. If user specifies BFD section flags, we will
2922 set ELF section type and flags based on BFD section flags in
2923 elf_fake_sections. Special handling for .init_array/.fini_array
2924 output sections since they may contain .ctors/.dtors input
2925 sections. We don't want _bfd_elf_init_private_section_data to
2926 copy ELF section type from .ctors/.dtors input sections. */
2927 if (abfd
->direction
!= read_direction
2928 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2930 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2933 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2934 || ssect
->type
== SHT_INIT_ARRAY
2935 || ssect
->type
== SHT_FINI_ARRAY
))
2937 elf_section_type (sec
) = ssect
->type
;
2938 elf_section_flags (sec
) = ssect
->attr
;
2942 return _bfd_generic_new_section_hook (abfd
, sec
);
2945 /* Create a new bfd section from an ELF program header.
2947 Since program segments have no names, we generate a synthetic name
2948 of the form segment<NUM>, where NUM is generally the index in the
2949 program header table. For segments that are split (see below) we
2950 generate the names segment<NUM>a and segment<NUM>b.
2952 Note that some program segments may have a file size that is different than
2953 (less than) the memory size. All this means is that at execution the
2954 system must allocate the amount of memory specified by the memory size,
2955 but only initialize it with the first "file size" bytes read from the
2956 file. This would occur for example, with program segments consisting
2957 of combined data+bss.
2959 To handle the above situation, this routine generates TWO bfd sections
2960 for the single program segment. The first has the length specified by
2961 the file size of the segment, and the second has the length specified
2962 by the difference between the two sizes. In effect, the segment is split
2963 into its initialized and uninitialized parts.
2968 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2969 Elf_Internal_Phdr
*hdr
,
2971 const char *type_name
)
2979 split
= ((hdr
->p_memsz
> 0)
2980 && (hdr
->p_filesz
> 0)
2981 && (hdr
->p_memsz
> hdr
->p_filesz
));
2983 if (hdr
->p_filesz
> 0)
2985 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2986 len
= strlen (namebuf
) + 1;
2987 name
= (char *) bfd_alloc (abfd
, len
);
2990 memcpy (name
, namebuf
, len
);
2991 newsect
= bfd_make_section (abfd
, name
);
2992 if (newsect
== NULL
)
2994 newsect
->vma
= hdr
->p_vaddr
;
2995 newsect
->lma
= hdr
->p_paddr
;
2996 newsect
->size
= hdr
->p_filesz
;
2997 newsect
->filepos
= hdr
->p_offset
;
2998 newsect
->flags
|= SEC_HAS_CONTENTS
;
2999 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
3000 if (hdr
->p_type
== PT_LOAD
)
3002 newsect
->flags
|= SEC_ALLOC
;
3003 newsect
->flags
|= SEC_LOAD
;
3004 if (hdr
->p_flags
& PF_X
)
3006 /* FIXME: all we known is that it has execute PERMISSION,
3008 newsect
->flags
|= SEC_CODE
;
3011 if (!(hdr
->p_flags
& PF_W
))
3013 newsect
->flags
|= SEC_READONLY
;
3017 if (hdr
->p_memsz
> hdr
->p_filesz
)
3021 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3022 len
= strlen (namebuf
) + 1;
3023 name
= (char *) bfd_alloc (abfd
, len
);
3026 memcpy (name
, namebuf
, len
);
3027 newsect
= bfd_make_section (abfd
, name
);
3028 if (newsect
== NULL
)
3030 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
3031 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
3032 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3033 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3034 align
= newsect
->vma
& -newsect
->vma
;
3035 if (align
== 0 || align
> hdr
->p_align
)
3036 align
= hdr
->p_align
;
3037 newsect
->alignment_power
= bfd_log2 (align
);
3038 if (hdr
->p_type
== PT_LOAD
)
3040 /* Hack for gdb. Segments that have not been modified do
3041 not have their contents written to a core file, on the
3042 assumption that a debugger can find the contents in the
3043 executable. We flag this case by setting the fake
3044 section size to zero. Note that "real" bss sections will
3045 always have their contents dumped to the core file. */
3046 if (bfd_get_format (abfd
) == bfd_core
)
3048 newsect
->flags
|= SEC_ALLOC
;
3049 if (hdr
->p_flags
& PF_X
)
3050 newsect
->flags
|= SEC_CODE
;
3052 if (!(hdr
->p_flags
& PF_W
))
3053 newsect
->flags
|= SEC_READONLY
;
3060 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3062 /* The return value is ignored. Build-ids are considered optional. */
3063 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3064 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3070 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3072 const struct elf_backend_data
*bed
;
3074 switch (hdr
->p_type
)
3077 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3080 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3082 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3083 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3087 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3090 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3093 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3095 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3101 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3104 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3106 case PT_GNU_EH_FRAME
:
3107 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3111 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3114 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3117 /* Check for any processor-specific program segment types. */
3118 bed
= get_elf_backend_data (abfd
);
3119 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3123 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3127 _bfd_elf_single_rel_hdr (asection
*sec
)
3129 if (elf_section_data (sec
)->rel
.hdr
)
3131 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3132 return elf_section_data (sec
)->rel
.hdr
;
3135 return elf_section_data (sec
)->rela
.hdr
;
3139 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3140 Elf_Internal_Shdr
*rel_hdr
,
3141 const char *sec_name
,
3142 bfd_boolean use_rela_p
)
3144 char *name
= (char *) bfd_alloc (abfd
,
3145 sizeof ".rela" + strlen (sec_name
));
3149 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3151 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3153 if (rel_hdr
->sh_name
== (unsigned int) -1)
3159 /* Allocate and initialize a section-header for a new reloc section,
3160 containing relocations against ASECT. It is stored in RELDATA. If
3161 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3165 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3166 struct bfd_elf_section_reloc_data
*reldata
,
3167 const char *sec_name
,
3168 bfd_boolean use_rela_p
,
3169 bfd_boolean delay_st_name_p
)
3171 Elf_Internal_Shdr
*rel_hdr
;
3172 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3174 BFD_ASSERT (reldata
->hdr
== NULL
);
3175 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3176 reldata
->hdr
= rel_hdr
;
3178 if (delay_st_name_p
)
3179 rel_hdr
->sh_name
= (unsigned int) -1;
3180 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3183 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3184 rel_hdr
->sh_entsize
= (use_rela_p
3185 ? bed
->s
->sizeof_rela
3186 : bed
->s
->sizeof_rel
);
3187 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3188 rel_hdr
->sh_flags
= 0;
3189 rel_hdr
->sh_addr
= 0;
3190 rel_hdr
->sh_size
= 0;
3191 rel_hdr
->sh_offset
= 0;
3196 /* Return the default section type based on the passed in section flags. */
3199 bfd_elf_get_default_section_type (flagword flags
)
3201 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3202 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3204 return SHT_PROGBITS
;
3207 struct fake_section_arg
3209 struct bfd_link_info
*link_info
;
3213 /* Set up an ELF internal section header for a section. */
3216 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3218 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3219 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3220 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3221 Elf_Internal_Shdr
*this_hdr
;
3222 unsigned int sh_type
;
3223 const char *name
= asect
->name
;
3224 bfd_boolean delay_st_name_p
= FALSE
;
3228 /* We already failed; just get out of the bfd_map_over_sections
3233 this_hdr
= &esd
->this_hdr
;
3237 /* ld: compress DWARF debug sections with names: .debug_*. */
3238 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3239 && (asect
->flags
& SEC_DEBUGGING
)
3243 /* Set SEC_ELF_COMPRESS to indicate this section should be
3245 asect
->flags
|= SEC_ELF_COMPRESS
;
3247 /* If this section will be compressed, delay adding section
3248 name to section name section after it is compressed in
3249 _bfd_elf_assign_file_positions_for_non_load. */
3250 delay_st_name_p
= TRUE
;
3253 else if ((asect
->flags
& SEC_ELF_RENAME
))
3255 /* objcopy: rename output DWARF debug section. */
3256 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3258 /* When we decompress or compress with SHF_COMPRESSED,
3259 convert section name from .zdebug_* to .debug_* if
3263 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3264 if (new_name
== NULL
)
3272 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3274 /* PR binutils/18087: Compression does not always make a
3275 section smaller. So only rename the section when
3276 compression has actually taken place. If input section
3277 name is .zdebug_*, we should never compress it again. */
3278 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3279 if (new_name
== NULL
)
3284 BFD_ASSERT (name
[1] != 'z');
3289 if (delay_st_name_p
)
3290 this_hdr
->sh_name
= (unsigned int) -1;
3294 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3296 if (this_hdr
->sh_name
== (unsigned int) -1)
3303 /* Don't clear sh_flags. Assembler may set additional bits. */
3305 if ((asect
->flags
& SEC_ALLOC
) != 0
3306 || asect
->user_set_vma
)
3307 this_hdr
->sh_addr
= asect
->vma
;
3309 this_hdr
->sh_addr
= 0;
3311 this_hdr
->sh_offset
= 0;
3312 this_hdr
->sh_size
= asect
->size
;
3313 this_hdr
->sh_link
= 0;
3314 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3315 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3318 /* xgettext:c-format */
3319 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3320 abfd
, asect
->alignment_power
, asect
);
3324 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3325 /* The sh_entsize and sh_info fields may have been set already by
3326 copy_private_section_data. */
3328 this_hdr
->bfd_section
= asect
;
3329 this_hdr
->contents
= NULL
;
3331 /* If the section type is unspecified, we set it based on
3333 if ((asect
->flags
& SEC_GROUP
) != 0)
3334 sh_type
= SHT_GROUP
;
3336 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3338 if (this_hdr
->sh_type
== SHT_NULL
)
3339 this_hdr
->sh_type
= sh_type
;
3340 else if (this_hdr
->sh_type
== SHT_NOBITS
3341 && sh_type
== SHT_PROGBITS
3342 && (asect
->flags
& SEC_ALLOC
) != 0)
3344 /* Warn if we are changing a NOBITS section to PROGBITS, but
3345 allow the link to proceed. This can happen when users link
3346 non-bss input sections to bss output sections, or emit data
3347 to a bss output section via a linker script. */
3349 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3350 this_hdr
->sh_type
= sh_type
;
3353 switch (this_hdr
->sh_type
)
3364 case SHT_INIT_ARRAY
:
3365 case SHT_FINI_ARRAY
:
3366 case SHT_PREINIT_ARRAY
:
3367 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3371 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3375 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3379 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3383 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3384 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3388 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3389 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3392 case SHT_GNU_versym
:
3393 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3396 case SHT_GNU_verdef
:
3397 this_hdr
->sh_entsize
= 0;
3398 /* objcopy or strip will copy over sh_info, but may not set
3399 cverdefs. The linker will set cverdefs, but sh_info will be
3401 if (this_hdr
->sh_info
== 0)
3402 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3404 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3405 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3408 case SHT_GNU_verneed
:
3409 this_hdr
->sh_entsize
= 0;
3410 /* objcopy or strip will copy over sh_info, but may not set
3411 cverrefs. The linker will set cverrefs, but sh_info will be
3413 if (this_hdr
->sh_info
== 0)
3414 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3416 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3417 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3421 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3425 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3429 if ((asect
->flags
& SEC_ALLOC
) != 0)
3430 this_hdr
->sh_flags
|= SHF_ALLOC
;
3431 if ((asect
->flags
& SEC_READONLY
) == 0)
3432 this_hdr
->sh_flags
|= SHF_WRITE
;
3433 if ((asect
->flags
& SEC_CODE
) != 0)
3434 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3435 if ((asect
->flags
& SEC_MERGE
) != 0)
3437 this_hdr
->sh_flags
|= SHF_MERGE
;
3438 this_hdr
->sh_entsize
= asect
->entsize
;
3440 if ((asect
->flags
& SEC_STRINGS
) != 0)
3441 this_hdr
->sh_flags
|= SHF_STRINGS
;
3442 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3443 this_hdr
->sh_flags
|= SHF_GROUP
;
3444 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3446 this_hdr
->sh_flags
|= SHF_TLS
;
3447 if (asect
->size
== 0
3448 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3450 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3452 this_hdr
->sh_size
= 0;
3455 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3456 if (this_hdr
->sh_size
!= 0)
3457 this_hdr
->sh_type
= SHT_NOBITS
;
3461 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3462 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3464 /* If the section has relocs, set up a section header for the
3465 SHT_REL[A] section. If two relocation sections are required for
3466 this section, it is up to the processor-specific back-end to
3467 create the other. */
3468 if ((asect
->flags
& SEC_RELOC
) != 0)
3470 /* When doing a relocatable link, create both REL and RELA sections if
3473 /* Do the normal setup if we wouldn't create any sections here. */
3474 && esd
->rel
.count
+ esd
->rela
.count
> 0
3475 && (bfd_link_relocatable (arg
->link_info
)
3476 || arg
->link_info
->emitrelocations
))
3478 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3479 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3480 FALSE
, delay_st_name_p
))
3485 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3486 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3487 TRUE
, delay_st_name_p
))
3493 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3495 ? &esd
->rela
: &esd
->rel
),
3505 /* Check for processor-specific section types. */
3506 sh_type
= this_hdr
->sh_type
;
3507 if (bed
->elf_backend_fake_sections
3508 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3514 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3516 /* Don't change the header type from NOBITS if we are being
3517 called for objcopy --only-keep-debug. */
3518 this_hdr
->sh_type
= sh_type
;
3522 /* Fill in the contents of a SHT_GROUP section. Called from
3523 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3524 when ELF targets use the generic linker, ld. Called for ld -r
3525 from bfd_elf_final_link. */
3528 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3530 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3531 asection
*elt
, *first
;
3535 /* Ignore linker created group section. See elfNN_ia64_object_p in
3537 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3542 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3544 unsigned long symindx
= 0;
3546 /* elf_group_id will have been set up by objcopy and the
3548 if (elf_group_id (sec
) != NULL
)
3549 symindx
= elf_group_id (sec
)->udata
.i
;
3553 /* If called from the assembler, swap_out_syms will have set up
3554 elf_section_syms. */
3555 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3556 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3558 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3560 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3562 /* The ELF backend linker sets sh_info to -2 when the group
3563 signature symbol is global, and thus the index can't be
3564 set until all local symbols are output. */
3566 struct bfd_elf_section_data
*sec_data
;
3567 unsigned long symndx
;
3568 unsigned long extsymoff
;
3569 struct elf_link_hash_entry
*h
;
3571 /* The point of this little dance to the first SHF_GROUP section
3572 then back to the SHT_GROUP section is that this gets us to
3573 the SHT_GROUP in the input object. */
3574 igroup
= elf_sec_group (elf_next_in_group (sec
));
3575 sec_data
= elf_section_data (igroup
);
3576 symndx
= sec_data
->this_hdr
.sh_info
;
3578 if (!elf_bad_symtab (igroup
->owner
))
3580 Elf_Internal_Shdr
*symtab_hdr
;
3582 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3583 extsymoff
= symtab_hdr
->sh_info
;
3585 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3586 while (h
->root
.type
== bfd_link_hash_indirect
3587 || h
->root
.type
== bfd_link_hash_warning
)
3588 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3590 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3593 /* The contents won't be allocated for "ld -r" or objcopy. */
3595 if (sec
->contents
== NULL
)
3598 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3600 /* Arrange for the section to be written out. */
3601 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3602 if (sec
->contents
== NULL
)
3609 loc
= sec
->contents
+ sec
->size
;
3611 /* Get the pointer to the first section in the group that gas
3612 squirreled away here. objcopy arranges for this to be set to the
3613 start of the input section group. */
3614 first
= elt
= elf_next_in_group (sec
);
3616 /* First element is a flag word. Rest of section is elf section
3617 indices for all the sections of the group. Write them backwards
3618 just to keep the group in the same order as given in .section
3619 directives, not that it matters. */
3626 s
= s
->output_section
;
3628 && !bfd_is_abs_section (s
))
3630 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3631 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3633 if (elf_sec
->rel
.hdr
!= NULL
3635 || (input_elf_sec
->rel
.hdr
!= NULL
3636 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3638 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3640 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3642 if (elf_sec
->rela
.hdr
!= NULL
3644 || (input_elf_sec
->rela
.hdr
!= NULL
3645 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3647 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3649 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3652 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3654 elt
= elf_next_in_group (elt
);
3660 BFD_ASSERT (loc
== sec
->contents
);
3662 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3665 /* Given NAME, the name of a relocation section stripped of its
3666 .rel/.rela prefix, return the section in ABFD to which the
3667 relocations apply. */
3670 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3672 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3673 section likely apply to .got.plt or .got section. */
3674 if (get_elf_backend_data (abfd
)->want_got_plt
3675 && strcmp (name
, ".plt") == 0)
3680 sec
= bfd_get_section_by_name (abfd
, name
);
3686 return bfd_get_section_by_name (abfd
, name
);
3689 /* Return the section to which RELOC_SEC applies. */
3692 elf_get_reloc_section (asection
*reloc_sec
)
3697 const struct elf_backend_data
*bed
;
3699 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3700 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3703 /* We look up the section the relocs apply to by name. */
3704 name
= reloc_sec
->name
;
3705 if (strncmp (name
, ".rel", 4) != 0)
3708 if (type
== SHT_RELA
&& *name
++ != 'a')
3711 abfd
= reloc_sec
->owner
;
3712 bed
= get_elf_backend_data (abfd
);
3713 return bed
->get_reloc_section (abfd
, name
);
3716 /* Assign all ELF section numbers. The dummy first section is handled here
3717 too. The link/info pointers for the standard section types are filled
3718 in here too, while we're at it. */
3721 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3723 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3725 unsigned int section_number
;
3726 Elf_Internal_Shdr
**i_shdrp
;
3727 struct bfd_elf_section_data
*d
;
3728 bfd_boolean need_symtab
;
3733 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3735 /* SHT_GROUP sections are in relocatable files only. */
3736 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3738 size_t reloc_count
= 0;
3740 /* Put SHT_GROUP sections first. */
3741 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3743 d
= elf_section_data (sec
);
3745 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3747 if (sec
->flags
& SEC_LINKER_CREATED
)
3749 /* Remove the linker created SHT_GROUP sections. */
3750 bfd_section_list_remove (abfd
, sec
);
3751 abfd
->section_count
--;
3754 d
->this_idx
= section_number
++;
3757 /* Count relocations. */
3758 reloc_count
+= sec
->reloc_count
;
3761 /* Clear HAS_RELOC if there are no relocations. */
3762 if (reloc_count
== 0)
3763 abfd
->flags
&= ~HAS_RELOC
;
3766 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3768 d
= elf_section_data (sec
);
3770 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3771 d
->this_idx
= section_number
++;
3772 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3773 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3776 d
->rel
.idx
= section_number
++;
3777 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3778 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3785 d
->rela
.idx
= section_number
++;
3786 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3787 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3793 need_symtab
= (bfd_get_symcount (abfd
) > 0
3794 || (link_info
== NULL
3795 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3799 elf_onesymtab (abfd
) = section_number
++;
3800 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3801 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3803 elf_section_list
*entry
;
3805 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3807 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3808 entry
->ndx
= section_number
++;
3809 elf_symtab_shndx_list (abfd
) = entry
;
3811 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3812 ".symtab_shndx", FALSE
);
3813 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3816 elf_strtab_sec (abfd
) = section_number
++;
3817 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3820 elf_shstrtab_sec (abfd
) = section_number
++;
3821 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3822 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3824 if (section_number
>= SHN_LORESERVE
)
3826 /* xgettext:c-format */
3827 _bfd_error_handler (_("%pB: too many sections: %u"),
3828 abfd
, section_number
);
3832 elf_numsections (abfd
) = section_number
;
3833 elf_elfheader (abfd
)->e_shnum
= section_number
;
3835 /* Set up the list of section header pointers, in agreement with the
3837 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3838 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3839 if (i_shdrp
== NULL
)
3842 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3843 sizeof (Elf_Internal_Shdr
));
3844 if (i_shdrp
[0] == NULL
)
3846 bfd_release (abfd
, i_shdrp
);
3850 elf_elfsections (abfd
) = i_shdrp
;
3852 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3855 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3856 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3858 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3859 BFD_ASSERT (entry
!= NULL
);
3860 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3861 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3863 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3864 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3867 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3871 d
= elf_section_data (sec
);
3873 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3874 if (d
->rel
.idx
!= 0)
3875 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3876 if (d
->rela
.idx
!= 0)
3877 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3879 /* Fill in the sh_link and sh_info fields while we're at it. */
3881 /* sh_link of a reloc section is the section index of the symbol
3882 table. sh_info is the section index of the section to which
3883 the relocation entries apply. */
3884 if (d
->rel
.idx
!= 0)
3886 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3887 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3888 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3890 if (d
->rela
.idx
!= 0)
3892 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3893 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3894 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3897 /* We need to set up sh_link for SHF_LINK_ORDER. */
3898 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3900 s
= elf_linked_to_section (sec
);
3903 /* elf_linked_to_section points to the input section. */
3904 if (link_info
!= NULL
)
3906 /* Check discarded linkonce section. */
3907 if (discarded_section (s
))
3911 /* xgettext:c-format */
3912 (_("%pB: sh_link of section `%pA' points to"
3913 " discarded section `%pA' of `%pB'"),
3914 abfd
, d
->this_hdr
.bfd_section
,
3916 /* Point to the kept section if it has the same
3917 size as the discarded one. */
3918 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3921 bfd_set_error (bfd_error_bad_value
);
3927 s
= s
->output_section
;
3928 BFD_ASSERT (s
!= NULL
);
3932 /* Handle objcopy. */
3933 if (s
->output_section
== NULL
)
3936 /* xgettext:c-format */
3937 (_("%pB: sh_link of section `%pA' points to"
3938 " removed section `%pA' of `%pB'"),
3939 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3940 bfd_set_error (bfd_error_bad_value
);
3943 s
= s
->output_section
;
3945 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3950 The Intel C compiler generates SHT_IA_64_UNWIND with
3951 SHF_LINK_ORDER. But it doesn't set the sh_link or
3952 sh_info fields. Hence we could get the situation
3954 const struct elf_backend_data
*bed
3955 = get_elf_backend_data (abfd
);
3956 if (bed
->link_order_error_handler
)
3957 bed
->link_order_error_handler
3958 /* xgettext:c-format */
3959 (_("%pB: warning: sh_link not set for section `%pA'"),
3964 switch (d
->this_hdr
.sh_type
)
3968 /* A reloc section which we are treating as a normal BFD
3969 section. sh_link is the section index of the symbol
3970 table. sh_info is the section index of the section to
3971 which the relocation entries apply. We assume that an
3972 allocated reloc section uses the dynamic symbol table.
3973 FIXME: How can we be sure? */
3974 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3976 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3978 s
= elf_get_reloc_section (sec
);
3981 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3982 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3987 /* We assume that a section named .stab*str is a stabs
3988 string section. We look for a section with the same name
3989 but without the trailing ``str'', and set its sh_link
3990 field to point to this section. */
3991 if (CONST_STRNEQ (sec
->name
, ".stab")
3992 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3997 len
= strlen (sec
->name
);
3998 alc
= (char *) bfd_malloc (len
- 2);
4001 memcpy (alc
, sec
->name
, len
- 3);
4002 alc
[len
- 3] = '\0';
4003 s
= bfd_get_section_by_name (abfd
, alc
);
4007 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
4009 /* This is a .stab section. */
4010 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
4011 elf_section_data (s
)->this_hdr
.sh_entsize
4012 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
4019 case SHT_GNU_verneed
:
4020 case SHT_GNU_verdef
:
4021 /* sh_link is the section header index of the string table
4022 used for the dynamic entries, or the symbol table, or the
4024 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4026 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4029 case SHT_GNU_LIBLIST
:
4030 /* sh_link is the section header index of the prelink library
4031 list used for the dynamic entries, or the symbol table, or
4032 the version strings. */
4033 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4034 ? ".dynstr" : ".gnu.libstr");
4036 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4041 case SHT_GNU_versym
:
4042 /* sh_link is the section header index of the symbol table
4043 this hash table or version table is for. */
4044 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4046 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4050 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4054 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4055 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4056 debug section name from .debug_* to .zdebug_* if needed. */
4062 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4064 /* If the backend has a special mapping, use it. */
4065 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4066 if (bed
->elf_backend_sym_is_global
)
4067 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4069 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4070 || bfd_is_und_section (bfd_asymbol_section (sym
))
4071 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4074 /* Filter global symbols of ABFD to include in the import library. All
4075 SYMCOUNT symbols of ABFD can be examined from their pointers in
4076 SYMS. Pointers of symbols to keep should be stored contiguously at
4077 the beginning of that array.
4079 Returns the number of symbols to keep. */
4082 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4083 asymbol
**syms
, long symcount
)
4085 long src_count
, dst_count
= 0;
4087 for (src_count
= 0; src_count
< symcount
; src_count
++)
4089 asymbol
*sym
= syms
[src_count
];
4090 char *name
= (char *) bfd_asymbol_name (sym
);
4091 struct bfd_link_hash_entry
*h
;
4093 if (!sym_is_global (abfd
, sym
))
4096 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4099 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4101 if (h
->linker_def
|| h
->ldscript_def
)
4104 syms
[dst_count
++] = sym
;
4107 syms
[dst_count
] = NULL
;
4112 /* Don't output section symbols for sections that are not going to be
4113 output, that are duplicates or there is no BFD section. */
4116 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4118 elf_symbol_type
*type_ptr
;
4123 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4126 if (sym
->section
== NULL
)
4129 type_ptr
= elf_symbol_from (abfd
, sym
);
4130 return ((type_ptr
!= NULL
4131 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4132 && bfd_is_abs_section (sym
->section
))
4133 || !(sym
->section
->owner
== abfd
4134 || (sym
->section
->output_section
!= NULL
4135 && sym
->section
->output_section
->owner
== abfd
4136 && sym
->section
->output_offset
== 0)
4137 || bfd_is_abs_section (sym
->section
)));
4140 /* Map symbol from it's internal number to the external number, moving
4141 all local symbols to be at the head of the list. */
4144 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4146 unsigned int symcount
= bfd_get_symcount (abfd
);
4147 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4148 asymbol
**sect_syms
;
4149 unsigned int num_locals
= 0;
4150 unsigned int num_globals
= 0;
4151 unsigned int num_locals2
= 0;
4152 unsigned int num_globals2
= 0;
4153 unsigned int max_index
= 0;
4160 fprintf (stderr
, "elf_map_symbols\n");
4164 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4166 if (max_index
< asect
->index
)
4167 max_index
= asect
->index
;
4171 amt
= max_index
* sizeof (asymbol
*);
4172 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4173 if (sect_syms
== NULL
)
4175 elf_section_syms (abfd
) = sect_syms
;
4176 elf_num_section_syms (abfd
) = max_index
;
4178 /* Init sect_syms entries for any section symbols we have already
4179 decided to output. */
4180 for (idx
= 0; idx
< symcount
; idx
++)
4182 asymbol
*sym
= syms
[idx
];
4184 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4186 && !ignore_section_sym (abfd
, sym
)
4187 && !bfd_is_abs_section (sym
->section
))
4189 asection
*sec
= sym
->section
;
4191 if (sec
->owner
!= abfd
)
4192 sec
= sec
->output_section
;
4194 sect_syms
[sec
->index
] = syms
[idx
];
4198 /* Classify all of the symbols. */
4199 for (idx
= 0; idx
< symcount
; idx
++)
4201 if (sym_is_global (abfd
, syms
[idx
]))
4203 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4207 /* We will be adding a section symbol for each normal BFD section. Most
4208 sections will already have a section symbol in outsymbols, but
4209 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4210 at least in that case. */
4211 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4213 if (sect_syms
[asect
->index
] == NULL
)
4215 if (!sym_is_global (abfd
, asect
->symbol
))
4222 /* Now sort the symbols so the local symbols are first. */
4223 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4224 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4225 if (new_syms
== NULL
)
4228 for (idx
= 0; idx
< symcount
; idx
++)
4230 asymbol
*sym
= syms
[idx
];
4233 if (sym_is_global (abfd
, sym
))
4234 i
= num_locals
+ num_globals2
++;
4235 else if (!ignore_section_sym (abfd
, sym
))
4240 sym
->udata
.i
= i
+ 1;
4242 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4244 if (sect_syms
[asect
->index
] == NULL
)
4246 asymbol
*sym
= asect
->symbol
;
4249 sect_syms
[asect
->index
] = sym
;
4250 if (!sym_is_global (abfd
, sym
))
4253 i
= num_locals
+ num_globals2
++;
4255 sym
->udata
.i
= i
+ 1;
4259 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4261 *pnum_locals
= num_locals
;
4265 /* Align to the maximum file alignment that could be required for any
4266 ELF data structure. */
4268 static inline file_ptr
4269 align_file_position (file_ptr off
, int align
)
4271 return (off
+ align
- 1) & ~(align
- 1);
4274 /* Assign a file position to a section, optionally aligning to the
4275 required section alignment. */
4278 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4282 if (align
&& i_shdrp
->sh_addralign
> 1)
4283 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4284 i_shdrp
->sh_offset
= offset
;
4285 if (i_shdrp
->bfd_section
!= NULL
)
4286 i_shdrp
->bfd_section
->filepos
= offset
;
4287 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4288 offset
+= i_shdrp
->sh_size
;
4292 /* Compute the file positions we are going to put the sections at, and
4293 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4294 is not NULL, this is being called by the ELF backend linker. */
4297 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4298 struct bfd_link_info
*link_info
)
4300 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4301 struct fake_section_arg fsargs
;
4303 struct elf_strtab_hash
*strtab
= NULL
;
4304 Elf_Internal_Shdr
*shstrtab_hdr
;
4305 bfd_boolean need_symtab
;
4307 if (abfd
->output_has_begun
)
4310 /* Do any elf backend specific processing first. */
4311 if (bed
->elf_backend_begin_write_processing
)
4312 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4314 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4317 fsargs
.failed
= FALSE
;
4318 fsargs
.link_info
= link_info
;
4319 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4323 if (!assign_section_numbers (abfd
, link_info
))
4326 /* The backend linker builds symbol table information itself. */
4327 need_symtab
= (link_info
== NULL
4328 && (bfd_get_symcount (abfd
) > 0
4329 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4333 /* Non-zero if doing a relocatable link. */
4334 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4336 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4341 if (link_info
== NULL
)
4343 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4348 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4349 /* sh_name was set in init_file_header. */
4350 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4351 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4352 shstrtab_hdr
->sh_addr
= 0;
4353 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4354 shstrtab_hdr
->sh_entsize
= 0;
4355 shstrtab_hdr
->sh_link
= 0;
4356 shstrtab_hdr
->sh_info
= 0;
4357 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4358 shstrtab_hdr
->sh_addralign
= 1;
4360 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4366 Elf_Internal_Shdr
*hdr
;
4368 off
= elf_next_file_pos (abfd
);
4370 hdr
= & elf_symtab_hdr (abfd
);
4371 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4373 if (elf_symtab_shndx_list (abfd
) != NULL
)
4375 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4376 if (hdr
->sh_size
!= 0)
4377 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4378 /* FIXME: What about other symtab_shndx sections in the list ? */
4381 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4382 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4384 elf_next_file_pos (abfd
) = off
;
4386 /* Now that we know where the .strtab section goes, write it
4388 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4389 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4391 _bfd_elf_strtab_free (strtab
);
4394 abfd
->output_has_begun
= TRUE
;
4399 /* Make an initial estimate of the size of the program header. If we
4400 get the number wrong here, we'll redo section placement. */
4402 static bfd_size_type
4403 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4407 const struct elf_backend_data
*bed
;
4409 /* Assume we will need exactly two PT_LOAD segments: one for text
4410 and one for data. */
4413 s
= bfd_get_section_by_name (abfd
, ".interp");
4414 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4416 /* If we have a loadable interpreter section, we need a
4417 PT_INTERP segment. In this case, assume we also need a
4418 PT_PHDR segment, although that may not be true for all
4423 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4425 /* We need a PT_DYNAMIC segment. */
4429 if (info
!= NULL
&& info
->relro
)
4431 /* We need a PT_GNU_RELRO segment. */
4435 if (elf_eh_frame_hdr (abfd
))
4437 /* We need a PT_GNU_EH_FRAME segment. */
4441 if (elf_stack_flags (abfd
))
4443 /* We need a PT_GNU_STACK segment. */
4447 s
= bfd_get_section_by_name (abfd
,
4448 NOTE_GNU_PROPERTY_SECTION_NAME
);
4449 if (s
!= NULL
&& s
->size
!= 0)
4451 /* We need a PT_GNU_PROPERTY segment. */
4455 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4457 if ((s
->flags
& SEC_LOAD
) != 0
4458 && elf_section_type (s
) == SHT_NOTE
)
4460 unsigned int alignment_power
;
4461 /* We need a PT_NOTE segment. */
4463 /* Try to create just one PT_NOTE segment for all adjacent
4464 loadable SHT_NOTE sections. gABI requires that within a
4465 PT_NOTE segment (and also inside of each SHT_NOTE section)
4466 each note should have the same alignment. So we check
4467 whether the sections are correctly aligned. */
4468 alignment_power
= s
->alignment_power
;
4469 while (s
->next
!= NULL
4470 && s
->next
->alignment_power
== alignment_power
4471 && (s
->next
->flags
& SEC_LOAD
) != 0
4472 && elf_section_type (s
->next
) == SHT_NOTE
)
4477 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4479 if (s
->flags
& SEC_THREAD_LOCAL
)
4481 /* We need a PT_TLS segment. */
4487 bed
= get_elf_backend_data (abfd
);
4489 if ((abfd
->flags
& D_PAGED
) != 0
4490 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4492 /* Add a PT_GNU_MBIND segment for each mbind section. */
4493 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4494 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4495 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4497 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4500 /* xgettext:c-format */
4501 (_("%pB: GNU_MBIND section `%pA' has invalid "
4502 "sh_info field: %d"),
4503 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4506 /* Align mbind section to page size. */
4507 if (s
->alignment_power
< page_align_power
)
4508 s
->alignment_power
= page_align_power
;
4513 /* Let the backend count up any program headers it might need. */
4514 if (bed
->elf_backend_additional_program_headers
)
4518 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4524 return segs
* bed
->s
->sizeof_phdr
;
4527 /* Find the segment that contains the output_section of section. */
4530 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4532 struct elf_segment_map
*m
;
4533 Elf_Internal_Phdr
*p
;
4535 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4541 for (i
= m
->count
- 1; i
>= 0; i
--)
4542 if (m
->sections
[i
] == section
)
4549 /* Create a mapping from a set of sections to a program segment. */
4551 static struct elf_segment_map
*
4552 make_mapping (bfd
*abfd
,
4553 asection
**sections
,
4558 struct elf_segment_map
*m
;
4563 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4564 amt
+= (to
- from
) * sizeof (asection
*);
4565 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4569 m
->p_type
= PT_LOAD
;
4570 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4571 m
->sections
[i
- from
] = *hdrpp
;
4572 m
->count
= to
- from
;
4574 if (from
== 0 && phdr
)
4576 /* Include the headers in the first PT_LOAD segment. */
4577 m
->includes_filehdr
= 1;
4578 m
->includes_phdrs
= 1;
4584 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4587 struct elf_segment_map
*
4588 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4590 struct elf_segment_map
*m
;
4592 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4593 sizeof (struct elf_segment_map
));
4597 m
->p_type
= PT_DYNAMIC
;
4599 m
->sections
[0] = dynsec
;
4604 /* Possibly add or remove segments from the segment map. */
4607 elf_modify_segment_map (bfd
*abfd
,
4608 struct bfd_link_info
*info
,
4609 bfd_boolean remove_empty_load
)
4611 struct elf_segment_map
**m
;
4612 const struct elf_backend_data
*bed
;
4614 /* The placement algorithm assumes that non allocated sections are
4615 not in PT_LOAD segments. We ensure this here by removing such
4616 sections from the segment map. We also remove excluded
4617 sections. Finally, any PT_LOAD segment without sections is
4619 m
= &elf_seg_map (abfd
);
4622 unsigned int i
, new_count
;
4624 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4626 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4627 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4628 || (*m
)->p_type
!= PT_LOAD
))
4630 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4634 (*m
)->count
= new_count
;
4636 if (remove_empty_load
4637 && (*m
)->p_type
== PT_LOAD
4639 && !(*m
)->includes_phdrs
)
4645 bed
= get_elf_backend_data (abfd
);
4646 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4648 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4655 #define IS_TBSS(s) \
4656 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4658 /* Set up a mapping from BFD sections to program segments. */
4661 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4664 struct elf_segment_map
*m
;
4665 asection
**sections
= NULL
;
4666 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4667 bfd_boolean no_user_phdrs
;
4669 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4672 info
->user_phdrs
= !no_user_phdrs
;
4674 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4678 struct elf_segment_map
*mfirst
;
4679 struct elf_segment_map
**pm
;
4682 unsigned int hdr_index
;
4683 bfd_vma maxpagesize
;
4685 bfd_boolean phdr_in_segment
;
4686 bfd_boolean writable
;
4687 bfd_boolean executable
;
4688 unsigned int tls_count
= 0;
4689 asection
*first_tls
= NULL
;
4690 asection
*first_mbind
= NULL
;
4691 asection
*dynsec
, *eh_frame_hdr
;
4693 bfd_vma addr_mask
, wrap_to
= 0;
4694 bfd_size_type phdr_size
;
4696 /* Select the allocated sections, and sort them. */
4698 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4699 sections
= (asection
**) bfd_malloc (amt
);
4700 if (sections
== NULL
)
4703 /* Calculate top address, avoiding undefined behaviour of shift
4704 left operator when shift count is equal to size of type
4706 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4707 addr_mask
= (addr_mask
<< 1) + 1;
4710 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4712 if ((s
->flags
& SEC_ALLOC
) != 0)
4714 /* target_index is unused until bfd_elf_final_link
4715 starts output of section symbols. Use it to make
4717 s
->target_index
= i
;
4720 /* A wrapping section potentially clashes with header. */
4721 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4722 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4725 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4728 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4730 phdr_size
= elf_program_header_size (abfd
);
4731 if (phdr_size
== (bfd_size_type
) -1)
4732 phdr_size
= get_program_header_size (abfd
, info
);
4733 phdr_size
+= bed
->s
->sizeof_ehdr
;
4734 maxpagesize
= bed
->maxpagesize
;
4735 if (maxpagesize
== 0)
4737 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4739 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4740 >= (phdr_size
& (maxpagesize
- 1))))
4741 /* For compatibility with old scripts that may not be using
4742 SIZEOF_HEADERS, add headers when it looks like space has
4743 been left for them. */
4744 phdr_in_segment
= TRUE
;
4746 /* Build the mapping. */
4750 /* If we have a .interp section, then create a PT_PHDR segment for
4751 the program headers and a PT_INTERP segment for the .interp
4753 s
= bfd_get_section_by_name (abfd
, ".interp");
4754 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4756 amt
= sizeof (struct elf_segment_map
);
4757 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4761 m
->p_type
= PT_PHDR
;
4763 m
->p_flags_valid
= 1;
4764 m
->includes_phdrs
= 1;
4765 phdr_in_segment
= TRUE
;
4769 amt
= sizeof (struct elf_segment_map
);
4770 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4774 m
->p_type
= PT_INTERP
;
4782 /* Look through the sections. We put sections in the same program
4783 segment when the start of the second section can be placed within
4784 a few bytes of the end of the first section. */
4790 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4792 && (dynsec
->flags
& SEC_LOAD
) == 0)
4795 if ((abfd
->flags
& D_PAGED
) == 0)
4796 phdr_in_segment
= FALSE
;
4798 /* Deal with -Ttext or something similar such that the first section
4799 is not adjacent to the program headers. This is an
4800 approximation, since at this point we don't know exactly how many
4801 program headers we will need. */
4802 if (phdr_in_segment
&& count
> 0)
4805 bfd_boolean separate_phdr
= FALSE
;
4807 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4809 && info
->separate_code
4810 && (sections
[0]->flags
& SEC_CODE
) != 0)
4812 /* If data sections should be separate from code and
4813 thus not executable, and the first section is
4814 executable then put the file and program headers in
4815 their own PT_LOAD. */
4816 separate_phdr
= TRUE
;
4817 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4818 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4820 /* The file and program headers are currently on the
4821 same page as the first section. Put them on the
4822 previous page if we can. */
4823 if (phdr_lma
>= maxpagesize
)
4824 phdr_lma
-= maxpagesize
;
4826 separate_phdr
= FALSE
;
4829 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4830 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4831 /* If file and program headers would be placed at the end
4832 of memory then it's probably better to omit them. */
4833 phdr_in_segment
= FALSE
;
4834 else if (phdr_lma
< wrap_to
)
4835 /* If a section wraps around to where we'll be placing
4836 file and program headers, then the headers will be
4838 phdr_in_segment
= FALSE
;
4839 else if (separate_phdr
)
4841 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4844 m
->p_paddr
= phdr_lma
;
4846 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4847 m
->p_paddr_valid
= 1;
4850 phdr_in_segment
= FALSE
;
4854 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4857 bfd_boolean new_segment
;
4861 /* See if this section and the last one will fit in the same
4864 if (last_hdr
== NULL
)
4866 /* If we don't have a segment yet, then we don't need a new
4867 one (we build the last one after this loop). */
4868 new_segment
= FALSE
;
4870 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4872 /* If this section has a different relation between the
4873 virtual address and the load address, then we need a new
4877 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4878 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4880 /* If this section has a load address that makes it overlap
4881 the previous section, then we need a new segment. */
4884 else if ((abfd
->flags
& D_PAGED
) != 0
4885 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4886 == (hdr
->lma
& -maxpagesize
)))
4888 /* If we are demand paged then we can't map two disk
4889 pages onto the same memory page. */
4890 new_segment
= FALSE
;
4892 /* In the next test we have to be careful when last_hdr->lma is close
4893 to the end of the address space. If the aligned address wraps
4894 around to the start of the address space, then there are no more
4895 pages left in memory and it is OK to assume that the current
4896 section can be included in the current segment. */
4897 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4898 + maxpagesize
> last_hdr
->lma
)
4899 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4900 + maxpagesize
<= hdr
->lma
))
4902 /* If putting this section in this segment would force us to
4903 skip a page in the segment, then we need a new segment. */
4906 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4907 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4909 /* We don't want to put a loaded section after a
4910 nonloaded (ie. bss style) section in the same segment
4911 as that will force the non-loaded section to be loaded.
4912 Consider .tbss sections as loaded for this purpose. */
4915 else if ((abfd
->flags
& D_PAGED
) == 0)
4917 /* If the file is not demand paged, which means that we
4918 don't require the sections to be correctly aligned in the
4919 file, then there is no other reason for a new segment. */
4920 new_segment
= FALSE
;
4922 else if (info
!= NULL
4923 && info
->separate_code
4924 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4929 && (hdr
->flags
& SEC_READONLY
) == 0)
4931 /* We don't want to put a writable section in a read only
4937 /* Otherwise, we can use the same segment. */
4938 new_segment
= FALSE
;
4941 /* Allow interested parties a chance to override our decision. */
4942 if (last_hdr
!= NULL
4944 && info
->callbacks
->override_segment_assignment
!= NULL
)
4946 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4952 if ((hdr
->flags
& SEC_READONLY
) == 0)
4954 if ((hdr
->flags
& SEC_CODE
) != 0)
4957 /* .tbss sections effectively have zero size. */
4958 last_size
= !IS_TBSS (hdr
) ? hdr
->size
: 0;
4962 /* We need a new program segment. We must create a new program
4963 header holding all the sections from hdr_index until hdr. */
4965 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4972 if ((hdr
->flags
& SEC_READONLY
) == 0)
4977 if ((hdr
->flags
& SEC_CODE
) == 0)
4983 /* .tbss sections effectively have zero size. */
4984 last_size
= !IS_TBSS (hdr
) ? hdr
->size
: 0;
4986 phdr_in_segment
= FALSE
;
4989 /* Create a final PT_LOAD program segment, but not if it's just
4991 if (last_hdr
!= NULL
4992 && (i
- hdr_index
!= 1
4993 || !IS_TBSS (last_hdr
)))
4995 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5003 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5006 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5013 /* For each batch of consecutive loadable SHT_NOTE sections,
5014 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5015 because if we link together nonloadable .note sections and
5016 loadable .note sections, we will generate two .note sections
5017 in the output file. */
5018 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5020 if ((s
->flags
& SEC_LOAD
) != 0
5021 && elf_section_type (s
) == SHT_NOTE
)
5024 unsigned int alignment_power
= s
->alignment_power
;
5027 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5029 if (s2
->next
->alignment_power
== alignment_power
5030 && (s2
->next
->flags
& SEC_LOAD
) != 0
5031 && elf_section_type (s2
->next
) == SHT_NOTE
5032 && align_power (s2
->lma
+ s2
->size
,
5039 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5040 amt
+= count
* sizeof (asection
*);
5041 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5045 m
->p_type
= PT_NOTE
;
5049 m
->sections
[m
->count
- count
--] = s
;
5050 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5053 m
->sections
[m
->count
- 1] = s
;
5054 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5058 if (s
->flags
& SEC_THREAD_LOCAL
)
5064 if (first_mbind
== NULL
5065 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5069 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5072 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5073 amt
+= tls_count
* sizeof (asection
*);
5074 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5079 m
->count
= tls_count
;
5080 /* Mandated PF_R. */
5082 m
->p_flags_valid
= 1;
5084 for (i
= 0; i
< tls_count
; ++i
)
5086 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5089 (_("%pB: TLS sections are not adjacent:"), abfd
);
5092 while (i
< tls_count
)
5094 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5096 _bfd_error_handler (_(" TLS: %pA"), s
);
5100 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5103 bfd_set_error (bfd_error_bad_value
);
5115 && (abfd
->flags
& D_PAGED
) != 0
5116 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5117 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5118 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5119 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5121 /* Mandated PF_R. */
5122 unsigned long p_flags
= PF_R
;
5123 if ((s
->flags
& SEC_READONLY
) == 0)
5125 if ((s
->flags
& SEC_CODE
) != 0)
5128 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5129 m
= bfd_zalloc (abfd
, amt
);
5133 m
->p_type
= (PT_GNU_MBIND_LO
5134 + elf_section_data (s
)->this_hdr
.sh_info
);
5136 m
->p_flags_valid
= 1;
5138 m
->p_flags
= p_flags
;
5144 s
= bfd_get_section_by_name (abfd
,
5145 NOTE_GNU_PROPERTY_SECTION_NAME
);
5146 if (s
!= NULL
&& s
->size
!= 0)
5148 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5149 m
= bfd_zalloc (abfd
, amt
);
5153 m
->p_type
= PT_GNU_PROPERTY
;
5155 m
->p_flags_valid
= 1;
5162 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5164 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5165 if (eh_frame_hdr
!= NULL
5166 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5168 amt
= sizeof (struct elf_segment_map
);
5169 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5173 m
->p_type
= PT_GNU_EH_FRAME
;
5175 m
->sections
[0] = eh_frame_hdr
->output_section
;
5181 if (elf_stack_flags (abfd
))
5183 amt
= sizeof (struct elf_segment_map
);
5184 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5188 m
->p_type
= PT_GNU_STACK
;
5189 m
->p_flags
= elf_stack_flags (abfd
);
5190 m
->p_align
= bed
->stack_align
;
5191 m
->p_flags_valid
= 1;
5192 m
->p_align_valid
= m
->p_align
!= 0;
5193 if (info
->stacksize
> 0)
5195 m
->p_size
= info
->stacksize
;
5196 m
->p_size_valid
= 1;
5203 if (info
!= NULL
&& info
->relro
)
5205 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5207 if (m
->p_type
== PT_LOAD
5209 && m
->sections
[0]->vma
>= info
->relro_start
5210 && m
->sections
[0]->vma
< info
->relro_end
)
5213 while (--i
!= (unsigned) -1)
5214 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5215 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5218 if (i
!= (unsigned) -1)
5223 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5226 amt
= sizeof (struct elf_segment_map
);
5227 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5231 m
->p_type
= PT_GNU_RELRO
;
5238 elf_seg_map (abfd
) = mfirst
;
5241 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5244 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5246 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5251 if (sections
!= NULL
)
5256 /* Sort sections by address. */
5259 elf_sort_sections (const void *arg1
, const void *arg2
)
5261 const asection
*sec1
= *(const asection
**) arg1
;
5262 const asection
*sec2
= *(const asection
**) arg2
;
5263 bfd_size_type size1
, size2
;
5265 /* Sort by LMA first, since this is the address used to
5266 place the section into a segment. */
5267 if (sec1
->lma
< sec2
->lma
)
5269 else if (sec1
->lma
> sec2
->lma
)
5272 /* Then sort by VMA. Normally the LMA and the VMA will be
5273 the same, and this will do nothing. */
5274 if (sec1
->vma
< sec2
->vma
)
5276 else if (sec1
->vma
> sec2
->vma
)
5279 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5281 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5288 else if (TOEND (sec2
))
5293 /* Sort by size, to put zero sized sections
5294 before others at the same address. */
5296 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5297 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5304 return sec1
->target_index
- sec2
->target_index
;
5307 /* This qsort comparison functions sorts PT_LOAD segments first and
5308 by p_paddr, for assign_file_positions_for_load_sections. */
5311 elf_sort_segments (const void *arg1
, const void *arg2
)
5313 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5314 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5316 if (m1
->p_type
!= m2
->p_type
)
5318 if (m1
->p_type
== PT_NULL
)
5320 if (m2
->p_type
== PT_NULL
)
5322 return m1
->p_type
< m2
->p_type
? -1 : 1;
5324 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5325 return m1
->includes_filehdr
? -1 : 1;
5326 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5327 return m1
->no_sort_lma
? -1 : 1;
5328 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5332 if (m1
->p_paddr_valid
)
5334 else if (m1
->count
!= 0)
5335 lma1
= m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
;
5337 if (m2
->p_paddr_valid
)
5339 else if (m2
->count
!= 0)
5340 lma2
= m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
;
5342 return lma1
< lma2
? -1 : 1;
5344 if (m1
->idx
!= m2
->idx
)
5345 return m1
->idx
< m2
->idx
? -1 : 1;
5349 /* Ian Lance Taylor writes:
5351 We shouldn't be using % with a negative signed number. That's just
5352 not good. We have to make sure either that the number is not
5353 negative, or that the number has an unsigned type. When the types
5354 are all the same size they wind up as unsigned. When file_ptr is a
5355 larger signed type, the arithmetic winds up as signed long long,
5358 What we're trying to say here is something like ``increase OFF by
5359 the least amount that will cause it to be equal to the VMA modulo
5361 /* In other words, something like:
5363 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5364 off_offset = off % bed->maxpagesize;
5365 if (vma_offset < off_offset)
5366 adjustment = vma_offset + bed->maxpagesize - off_offset;
5368 adjustment = vma_offset - off_offset;
5370 which can be collapsed into the expression below. */
5373 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5375 /* PR binutils/16199: Handle an alignment of zero. */
5376 if (maxpagesize
== 0)
5378 return ((vma
- off
) % maxpagesize
);
5382 print_segment_map (const struct elf_segment_map
*m
)
5385 const char *pt
= get_segment_type (m
->p_type
);
5390 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5391 sprintf (buf
, "LOPROC+%7.7x",
5392 (unsigned int) (m
->p_type
- PT_LOPROC
));
5393 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5394 sprintf (buf
, "LOOS+%7.7x",
5395 (unsigned int) (m
->p_type
- PT_LOOS
));
5397 snprintf (buf
, sizeof (buf
), "%8.8x",
5398 (unsigned int) m
->p_type
);
5402 fprintf (stderr
, "%s:", pt
);
5403 for (j
= 0; j
< m
->count
; j
++)
5404 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5410 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5415 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5417 buf
= bfd_zmalloc (len
);
5420 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5425 /* Assign file positions to the sections based on the mapping from
5426 sections to segments. This function also sets up some fields in
5430 assign_file_positions_for_load_sections (bfd
*abfd
,
5431 struct bfd_link_info
*link_info
)
5433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5434 struct elf_segment_map
*m
;
5435 struct elf_segment_map
*phdr_load_seg
;
5436 Elf_Internal_Phdr
*phdrs
;
5437 Elf_Internal_Phdr
*p
;
5439 bfd_size_type maxpagesize
;
5440 unsigned int alloc
, actual
;
5442 struct elf_segment_map
**sorted_seg_map
;
5444 if (link_info
== NULL
5445 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5449 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5454 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5455 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5459 /* PR binutils/12467. */
5460 elf_elfheader (abfd
)->e_phoff
= 0;
5461 elf_elfheader (abfd
)->e_phentsize
= 0;
5464 elf_elfheader (abfd
)->e_phnum
= alloc
;
5466 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5469 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5473 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5474 BFD_ASSERT (elf_program_header_size (abfd
)
5475 == actual
* bed
->s
->sizeof_phdr
);
5476 BFD_ASSERT (actual
>= alloc
);
5481 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5485 /* We're writing the size in elf_program_header_size (abfd),
5486 see assign_file_positions_except_relocs, so make sure we have
5487 that amount allocated, with trailing space cleared.
5488 The variable alloc contains the computed need, while
5489 elf_program_header_size (abfd) contains the size used for the
5491 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5492 where the layout is forced to according to a larger size in the
5493 last iterations for the testcase ld-elf/header. */
5494 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5495 + alloc
* sizeof (*sorted_seg_map
)));
5496 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5497 elf_tdata (abfd
)->phdr
= phdrs
;
5501 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5503 sorted_seg_map
[j
] = m
;
5504 /* If elf_segment_map is not from map_sections_to_segments, the
5505 sections may not be correctly ordered. NOTE: sorting should
5506 not be done to the PT_NOTE section of a corefile, which may
5507 contain several pseudo-sections artificially created by bfd.
5508 Sorting these pseudo-sections breaks things badly. */
5510 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5511 && m
->p_type
== PT_NOTE
))
5513 for (i
= 0; i
< m
->count
; i
++)
5514 m
->sections
[i
]->target_index
= i
;
5515 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5520 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5524 if ((abfd
->flags
& D_PAGED
) != 0)
5525 maxpagesize
= bed
->maxpagesize
;
5527 /* Sections must map to file offsets past the ELF file header. */
5528 off
= bed
->s
->sizeof_ehdr
;
5529 /* And if one of the PT_LOAD headers doesn't include the program
5530 headers then we'll be mapping program headers in the usual
5531 position after the ELF file header. */
5532 phdr_load_seg
= NULL
;
5533 for (j
= 0; j
< alloc
; j
++)
5535 m
= sorted_seg_map
[j
];
5536 if (m
->p_type
!= PT_LOAD
)
5538 if (m
->includes_phdrs
)
5544 if (phdr_load_seg
== NULL
)
5545 off
+= actual
* bed
->s
->sizeof_phdr
;
5547 for (j
= 0; j
< alloc
; j
++)
5551 bfd_boolean no_contents
;
5553 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5554 number of sections with contents contributing to both p_filesz
5555 and p_memsz, followed by a number of sections with no contents
5556 that just contribute to p_memsz. In this loop, OFF tracks next
5557 available file offset for PT_LOAD and PT_NOTE segments. */
5558 m
= sorted_seg_map
[j
];
5560 p
->p_type
= m
->p_type
;
5561 p
->p_flags
= m
->p_flags
;
5564 p
->p_vaddr
= m
->p_vaddr_offset
;
5566 p
->p_vaddr
= m
->sections
[0]->vma
+ m
->p_vaddr_offset
;
5568 if (m
->p_paddr_valid
)
5569 p
->p_paddr
= m
->p_paddr
;
5570 else if (m
->count
== 0)
5573 p
->p_paddr
= m
->sections
[0]->lma
+ m
->p_vaddr_offset
;
5575 if (p
->p_type
== PT_LOAD
5576 && (abfd
->flags
& D_PAGED
) != 0)
5578 /* p_align in demand paged PT_LOAD segments effectively stores
5579 the maximum page size. When copying an executable with
5580 objcopy, we set m->p_align from the input file. Use this
5581 value for maxpagesize rather than bed->maxpagesize, which
5582 may be different. Note that we use maxpagesize for PT_TLS
5583 segment alignment later in this function, so we are relying
5584 on at least one PT_LOAD segment appearing before a PT_TLS
5586 if (m
->p_align_valid
)
5587 maxpagesize
= m
->p_align
;
5589 p
->p_align
= maxpagesize
;
5591 else if (m
->p_align_valid
)
5592 p
->p_align
= m
->p_align
;
5593 else if (m
->count
== 0)
5594 p
->p_align
= 1 << bed
->s
->log_file_align
;
5596 if (m
== phdr_load_seg
)
5598 if (!m
->includes_filehdr
)
5600 off
+= actual
* bed
->s
->sizeof_phdr
;
5603 no_contents
= FALSE
;
5605 if (p
->p_type
== PT_LOAD
5608 bfd_size_type align
;
5609 unsigned int align_power
= 0;
5611 if (m
->p_align_valid
)
5615 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5617 unsigned int secalign
;
5619 secalign
= bfd_section_alignment (*secpp
);
5620 if (secalign
> align_power
)
5621 align_power
= secalign
;
5623 align
= (bfd_size_type
) 1 << align_power
;
5624 if (align
< maxpagesize
)
5625 align
= maxpagesize
;
5628 for (i
= 0; i
< m
->count
; i
++)
5629 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5630 /* If we aren't making room for this section, then
5631 it must be SHT_NOBITS regardless of what we've
5632 set via struct bfd_elf_special_section. */
5633 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5635 /* Find out whether this segment contains any loadable
5638 for (i
= 0; i
< m
->count
; i
++)
5639 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5641 no_contents
= FALSE
;
5645 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5647 /* Broken hardware and/or kernel require that files do not
5648 map the same page with different permissions on some hppa
5651 && (abfd
->flags
& D_PAGED
) != 0
5652 && bed
->no_page_alias
5653 && (off
& (maxpagesize
- 1)) != 0
5654 && (off
& -maxpagesize
) == ((off
+ off_adjust
) & -maxpagesize
))
5655 off_adjust
+= maxpagesize
;
5659 /* We shouldn't need to align the segment on disk since
5660 the segment doesn't need file space, but the gABI
5661 arguably requires the alignment and glibc ld.so
5662 checks it. So to comply with the alignment
5663 requirement but not waste file space, we adjust
5664 p_offset for just this segment. (OFF_ADJUST is
5665 subtracted from OFF later.) This may put p_offset
5666 past the end of file, but that shouldn't matter. */
5671 /* Make sure the .dynamic section is the first section in the
5672 PT_DYNAMIC segment. */
5673 else if (p
->p_type
== PT_DYNAMIC
5675 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5678 (_("%pB: The first section in the PT_DYNAMIC segment"
5679 " is not the .dynamic section"),
5681 bfd_set_error (bfd_error_bad_value
);
5684 /* Set the note section type to SHT_NOTE. */
5685 else if (p
->p_type
== PT_NOTE
)
5686 for (i
= 0; i
< m
->count
; i
++)
5687 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5689 if (m
->includes_filehdr
)
5691 if (!m
->p_flags_valid
)
5693 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5694 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5695 if (p
->p_type
== PT_LOAD
)
5699 if (p
->p_vaddr
< (bfd_vma
) off
5700 || (!m
->p_paddr_valid
5701 && p
->p_paddr
< (bfd_vma
) off
))
5704 (_("%pB: not enough room for program headers,"
5705 " try linking with -N"),
5707 bfd_set_error (bfd_error_bad_value
);
5711 if (!m
->p_paddr_valid
)
5715 else if (sorted_seg_map
[0]->includes_filehdr
)
5717 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5718 p
->p_vaddr
= filehdr
->p_vaddr
;
5719 if (!m
->p_paddr_valid
)
5720 p
->p_paddr
= filehdr
->p_paddr
;
5724 if (m
->includes_phdrs
)
5726 if (!m
->p_flags_valid
)
5728 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5729 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5730 if (!m
->includes_filehdr
)
5732 if (p
->p_type
== PT_LOAD
)
5734 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5737 p
->p_vaddr
-= off
- p
->p_offset
;
5738 if (!m
->p_paddr_valid
)
5739 p
->p_paddr
-= off
- p
->p_offset
;
5742 else if (phdr_load_seg
!= NULL
)
5744 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5745 bfd_vma phdr_off
= 0;
5746 if (phdr_load_seg
->includes_filehdr
)
5747 phdr_off
= bed
->s
->sizeof_ehdr
;
5748 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5749 if (!m
->p_paddr_valid
)
5750 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5751 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5754 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5758 if (p
->p_type
== PT_LOAD
5759 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5761 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5766 /* Put meaningless p_offset for PT_LOAD segments
5767 without file contents somewhere within the first
5768 page, in an attempt to not point past EOF. */
5769 bfd_size_type align
= maxpagesize
;
5770 if (align
< p
->p_align
)
5774 p
->p_offset
= off
% align
;
5781 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5783 p
->p_filesz
+= adjust
;
5784 p
->p_memsz
+= adjust
;
5788 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5789 maps. Set filepos for sections in PT_LOAD segments, and in
5790 core files, for sections in PT_NOTE segments.
5791 assign_file_positions_for_non_load_sections will set filepos
5792 for other sections and update p_filesz for other segments. */
5793 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5796 bfd_size_type align
;
5797 Elf_Internal_Shdr
*this_hdr
;
5800 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5801 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5803 if ((p
->p_type
== PT_LOAD
5804 || p
->p_type
== PT_TLS
)
5805 && (this_hdr
->sh_type
!= SHT_NOBITS
5806 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5807 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5808 || p
->p_type
== PT_TLS
))))
5810 bfd_vma p_start
= p
->p_paddr
;
5811 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5812 bfd_vma s_start
= sec
->lma
;
5813 bfd_vma adjust
= s_start
- p_end
;
5817 || p_end
< p_start
))
5820 /* xgettext:c-format */
5821 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5822 abfd
, sec
, (uint64_t) s_start
, (uint64_t) p_end
);
5826 p
->p_memsz
+= adjust
;
5828 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5830 if (p
->p_type
== PT_LOAD
)
5832 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5834 /* We have a PROGBITS section following NOBITS ones.
5835 Allocate file space for the NOBITS section(s) and
5837 adjust
= p
->p_memsz
- p
->p_filesz
;
5838 if (!write_zeros (abfd
, off
, adjust
))
5843 p
->p_filesz
+= adjust
;
5847 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5849 /* The section at i == 0 is the one that actually contains
5853 this_hdr
->sh_offset
= sec
->filepos
= off
;
5854 off
+= this_hdr
->sh_size
;
5855 p
->p_filesz
= this_hdr
->sh_size
;
5861 /* The rest are fake sections that shouldn't be written. */
5870 if (p
->p_type
== PT_LOAD
)
5872 this_hdr
->sh_offset
= sec
->filepos
= off
;
5873 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5874 off
+= this_hdr
->sh_size
;
5876 else if (this_hdr
->sh_type
== SHT_NOBITS
5877 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5878 && this_hdr
->sh_offset
== 0)
5880 /* This is a .tbss section that didn't get a PT_LOAD.
5881 (See _bfd_elf_map_sections_to_segments "Create a
5882 final PT_LOAD".) Set sh_offset to the value it
5883 would have if we had created a zero p_filesz and
5884 p_memsz PT_LOAD header for the section. This
5885 also makes the PT_TLS header have the same
5887 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5889 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5892 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5894 p
->p_filesz
+= this_hdr
->sh_size
;
5895 /* A load section without SHF_ALLOC is something like
5896 a note section in a PT_NOTE segment. These take
5897 file space but are not loaded into memory. */
5898 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5899 p
->p_memsz
+= this_hdr
->sh_size
;
5901 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5903 if (p
->p_type
== PT_TLS
)
5904 p
->p_memsz
+= this_hdr
->sh_size
;
5906 /* .tbss is special. It doesn't contribute to p_memsz of
5908 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5909 p
->p_memsz
+= this_hdr
->sh_size
;
5912 if (align
> p
->p_align
5913 && !m
->p_align_valid
5914 && (p
->p_type
!= PT_LOAD
5915 || (abfd
->flags
& D_PAGED
) == 0))
5919 if (!m
->p_flags_valid
)
5922 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5924 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5931 /* PR ld/20815 - Check that the program header segment, if
5932 present, will be loaded into memory. */
5933 if (p
->p_type
== PT_PHDR
5934 && phdr_load_seg
== NULL
5935 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
5936 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
5938 /* The fix for this error is usually to edit the linker script being
5939 used and set up the program headers manually. Either that or
5940 leave room for the headers at the start of the SECTIONS. */
5941 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5942 " by LOAD segment"),
5947 /* Check that all sections are in a PT_LOAD segment.
5948 Don't check funky gdb generated core files. */
5949 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5951 bfd_boolean check_vma
= TRUE
;
5953 for (i
= 1; i
< m
->count
; i
++)
5954 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5955 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5956 ->this_hdr
), p
) != 0
5957 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5958 ->this_hdr
), p
) != 0)
5960 /* Looks like we have overlays packed into the segment. */
5965 for (i
= 0; i
< m
->count
; i
++)
5967 Elf_Internal_Shdr
*this_hdr
;
5970 sec
= m
->sections
[i
];
5971 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5972 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5973 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5976 /* xgettext:c-format */
5977 (_("%pB: section `%pA' can't be allocated in segment %d"),
5979 print_segment_map (m
);
5985 elf_next_file_pos (abfd
) = off
;
5987 if (link_info
!= NULL
5988 && phdr_load_seg
!= NULL
5989 && phdr_load_seg
->includes_filehdr
)
5991 /* There is a segment that contains both the file headers and the
5992 program headers, so provide a symbol __ehdr_start pointing there.
5993 A program can use this to examine itself robustly. */
5995 struct elf_link_hash_entry
*hash
5996 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5997 FALSE
, FALSE
, TRUE
);
5998 /* If the symbol was referenced and not defined, define it. */
6000 && (hash
->root
.type
== bfd_link_hash_new
6001 || hash
->root
.type
== bfd_link_hash_undefined
6002 || hash
->root
.type
== bfd_link_hash_undefweak
6003 || hash
->root
.type
== bfd_link_hash_common
))
6006 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
;
6008 if (phdr_load_seg
->count
!= 0)
6009 /* The segment contains sections, so use the first one. */
6010 s
= phdr_load_seg
->sections
[0];
6012 /* Use the first (i.e. lowest-addressed) section in any segment. */
6013 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6014 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6022 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6023 hash
->root
.u
.def
.section
= s
;
6027 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6028 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6031 hash
->root
.type
= bfd_link_hash_defined
;
6032 hash
->def_regular
= 1;
6040 /* Determine if a bfd is a debuginfo file. Unfortunately there
6041 is no defined method for detecting such files, so we have to
6042 use heuristics instead. */
6045 is_debuginfo_file (bfd
*abfd
)
6047 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6050 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6051 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6052 Elf_Internal_Shdr
**headerp
;
6054 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6056 Elf_Internal_Shdr
*header
= * headerp
;
6058 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6059 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6060 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6061 && header
->sh_type
!= SHT_NOBITS
6062 && header
->sh_type
!= SHT_NOTE
)
6069 /* Assign file positions for the other sections, except for compressed debugging
6070 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6073 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6074 struct bfd_link_info
*link_info
)
6076 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6077 Elf_Internal_Shdr
**i_shdrpp
;
6078 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6079 Elf_Internal_Phdr
*phdrs
;
6080 Elf_Internal_Phdr
*p
;
6081 struct elf_segment_map
*m
;
6084 i_shdrpp
= elf_elfsections (abfd
);
6085 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6086 off
= elf_next_file_pos (abfd
);
6087 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6089 Elf_Internal_Shdr
*hdr
;
6092 if (hdr
->bfd_section
!= NULL
6093 && (hdr
->bfd_section
->filepos
!= 0
6094 || (hdr
->sh_type
== SHT_NOBITS
6095 && hdr
->contents
== NULL
)))
6096 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6097 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6099 if (hdr
->sh_size
!= 0
6100 /* PR 24717 - debuginfo files are known to be not strictly
6101 compliant with the ELF standard. In particular they often
6102 have .note.gnu.property sections that are outside of any
6103 loadable segment. This is not a problem for such files,
6104 so do not warn about them. */
6105 && ! is_debuginfo_file (abfd
))
6107 /* xgettext:c-format */
6108 (_("%pB: warning: allocated section `%s' not in segment"),
6110 (hdr
->bfd_section
== NULL
6112 : hdr
->bfd_section
->name
));
6113 /* We don't need to page align empty sections. */
6114 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6115 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6118 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6120 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6123 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6124 && hdr
->bfd_section
== NULL
)
6125 /* We don't know the offset of these sections yet: their size has
6126 not been decided. */
6127 || (hdr
->bfd_section
!= NULL
6128 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6129 || (bfd_section_is_ctf (hdr
->bfd_section
)
6130 && abfd
->is_linker_output
)))
6131 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6132 || (elf_symtab_shndx_list (abfd
) != NULL
6133 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6134 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6135 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6136 hdr
->sh_offset
= -1;
6138 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6140 elf_next_file_pos (abfd
) = off
;
6142 /* Now that we have set the section file positions, we can set up
6143 the file positions for the non PT_LOAD segments. */
6144 phdrs
= elf_tdata (abfd
)->phdr
;
6145 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6147 if (p
->p_type
== PT_GNU_RELRO
)
6152 if (link_info
!= NULL
)
6154 /* During linking the range of the RELRO segment is passed
6155 in link_info. Note that there may be padding between
6156 relro_start and the first RELRO section. */
6157 start
= link_info
->relro_start
;
6158 end
= link_info
->relro_end
;
6160 else if (m
->count
!= 0)
6162 if (!m
->p_size_valid
)
6164 start
= m
->sections
[0]->vma
;
6165 end
= start
+ m
->p_size
;
6176 struct elf_segment_map
*lm
;
6177 const Elf_Internal_Phdr
*lp
;
6180 /* Find a LOAD segment containing a section in the RELRO
6182 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6184 lm
= lm
->next
, lp
++)
6186 if (lp
->p_type
== PT_LOAD
6188 && (lm
->sections
[lm
->count
- 1]->vma
6189 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6190 ? lm
->sections
[lm
->count
- 1]->size
6192 && lm
->sections
[0]->vma
< end
)
6198 /* Find the section starting the RELRO segment. */
6199 for (i
= 0; i
< lm
->count
; i
++)
6201 asection
*s
= lm
->sections
[i
];
6210 p
->p_vaddr
= lm
->sections
[i
]->vma
;
6211 p
->p_paddr
= lm
->sections
[i
]->lma
;
6212 p
->p_offset
= lm
->sections
[i
]->filepos
;
6213 p
->p_memsz
= end
- p
->p_vaddr
;
6214 p
->p_filesz
= p
->p_memsz
;
6216 /* The RELRO segment typically ends a few bytes
6217 into .got.plt but other layouts are possible.
6218 In cases where the end does not match any
6219 loaded section (for instance is in file
6220 padding), trim p_filesz back to correspond to
6221 the end of loaded section contents. */
6222 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6223 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6225 /* Preserve the alignment and flags if they are
6226 valid. The gold linker generates RW/4 for
6227 the PT_GNU_RELRO section. It is better for
6228 objcopy/strip to honor these attributes
6229 otherwise gdb will choke when using separate
6231 if (!m
->p_align_valid
)
6233 if (!m
->p_flags_valid
)
6239 if (link_info
!= NULL
)
6242 memset (p
, 0, sizeof *p
);
6244 else if (p
->p_type
== PT_GNU_STACK
)
6246 if (m
->p_size_valid
)
6247 p
->p_memsz
= m
->p_size
;
6249 else if (m
->count
!= 0)
6253 if (p
->p_type
!= PT_LOAD
6254 && (p
->p_type
!= PT_NOTE
6255 || bfd_get_format (abfd
) != bfd_core
))
6257 /* A user specified segment layout may include a PHDR
6258 segment that overlaps with a LOAD segment... */
6259 if (p
->p_type
== PT_PHDR
)
6265 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6267 /* PR 17512: file: 2195325e. */
6269 (_("%pB: error: non-load segment %d includes file header "
6270 "and/or program header"),
6271 abfd
, (int) (p
- phdrs
));
6276 p
->p_offset
= m
->sections
[0]->filepos
;
6277 for (i
= m
->count
; i
-- != 0;)
6279 asection
*sect
= m
->sections
[i
];
6280 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6281 if (hdr
->sh_type
!= SHT_NOBITS
)
6283 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6295 static elf_section_list
*
6296 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6298 for (;list
!= NULL
; list
= list
->next
)
6304 /* Work out the file positions of all the sections. This is called by
6305 _bfd_elf_compute_section_file_positions. All the section sizes and
6306 VMAs must be known before this is called.
6308 Reloc sections come in two flavours: Those processed specially as
6309 "side-channel" data attached to a section to which they apply, and those that
6310 bfd doesn't process as relocations. The latter sort are stored in a normal
6311 bfd section by bfd_section_from_shdr. We don't consider the former sort
6312 here, unless they form part of the loadable image. Reloc sections not
6313 assigned here (and compressed debugging sections and CTF sections which
6314 nothing else in the file can rely upon) will be handled later by
6315 assign_file_positions_for_relocs.
6317 We also don't set the positions of the .symtab and .strtab here. */
6320 assign_file_positions_except_relocs (bfd
*abfd
,
6321 struct bfd_link_info
*link_info
)
6323 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6324 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6325 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6328 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6329 && bfd_get_format (abfd
) != bfd_core
)
6331 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6332 unsigned int num_sec
= elf_numsections (abfd
);
6333 Elf_Internal_Shdr
**hdrpp
;
6337 /* Start after the ELF header. */
6338 off
= i_ehdrp
->e_ehsize
;
6340 /* We are not creating an executable, which means that we are
6341 not creating a program header, and that the actual order of
6342 the sections in the file is unimportant. */
6343 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6345 Elf_Internal_Shdr
*hdr
;
6348 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6349 && hdr
->bfd_section
== NULL
)
6350 /* Do not assign offsets for these sections yet: we don't know
6352 || (hdr
->bfd_section
!= NULL
6353 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6354 || (bfd_section_is_ctf (hdr
->bfd_section
)
6355 && abfd
->is_linker_output
)))
6356 || i
== elf_onesymtab (abfd
)
6357 || (elf_symtab_shndx_list (abfd
) != NULL
6358 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6359 || i
== elf_strtab_sec (abfd
)
6360 || i
== elf_shstrtab_sec (abfd
))
6362 hdr
->sh_offset
= -1;
6365 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6368 elf_next_file_pos (abfd
) = off
;
6369 elf_program_header_size (abfd
) = 0;
6373 /* Assign file positions for the loaded sections based on the
6374 assignment of sections to segments. */
6375 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6378 /* And for non-load sections. */
6379 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6383 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6386 /* Write out the program headers. */
6387 alloc
= i_ehdrp
->e_phnum
;
6390 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6391 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6399 _bfd_elf_init_file_header (bfd
*abfd
,
6400 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6402 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6403 struct elf_strtab_hash
*shstrtab
;
6404 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6406 i_ehdrp
= elf_elfheader (abfd
);
6408 shstrtab
= _bfd_elf_strtab_init ();
6409 if (shstrtab
== NULL
)
6412 elf_shstrtab (abfd
) = shstrtab
;
6414 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6415 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6416 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6417 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6419 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6420 i_ehdrp
->e_ident
[EI_DATA
] =
6421 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6422 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6424 if ((abfd
->flags
& DYNAMIC
) != 0)
6425 i_ehdrp
->e_type
= ET_DYN
;
6426 else if ((abfd
->flags
& EXEC_P
) != 0)
6427 i_ehdrp
->e_type
= ET_EXEC
;
6428 else if (bfd_get_format (abfd
) == bfd_core
)
6429 i_ehdrp
->e_type
= ET_CORE
;
6431 i_ehdrp
->e_type
= ET_REL
;
6433 switch (bfd_get_arch (abfd
))
6435 case bfd_arch_unknown
:
6436 i_ehdrp
->e_machine
= EM_NONE
;
6439 /* There used to be a long list of cases here, each one setting
6440 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6441 in the corresponding bfd definition. To avoid duplication,
6442 the switch was removed. Machines that need special handling
6443 can generally do it in elf_backend_final_write_processing(),
6444 unless they need the information earlier than the final write.
6445 Such need can generally be supplied by replacing the tests for
6446 e_machine with the conditions used to determine it. */
6448 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6451 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6452 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6454 /* No program header, for now. */
6455 i_ehdrp
->e_phoff
= 0;
6456 i_ehdrp
->e_phentsize
= 0;
6457 i_ehdrp
->e_phnum
= 0;
6459 /* Each bfd section is section header entry. */
6460 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6461 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6463 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6464 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6465 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6466 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6467 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6468 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6469 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6470 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6471 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6477 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6479 FIXME: We used to have code here to sort the PT_LOAD segments into
6480 ascending order, as per the ELF spec. But this breaks some programs,
6481 including the Linux kernel. But really either the spec should be
6482 changed or the programs updated. */
6485 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6487 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6489 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6490 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6491 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6492 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6493 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6495 /* Find the lowest p_vaddr in PT_LOAD segments. */
6496 bfd_vma p_vaddr
= (bfd_vma
) -1;
6497 for (; segment
< end_segment
; segment
++)
6498 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6499 p_vaddr
= segment
->p_vaddr
;
6501 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6502 segments is non-zero. */
6504 i_ehdrp
->e_type
= ET_EXEC
;
6509 /* Assign file positions for all the reloc sections which are not part
6510 of the loadable file image, and the file position of section headers. */
6513 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6516 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6517 Elf_Internal_Shdr
*shdrp
;
6518 Elf_Internal_Ehdr
*i_ehdrp
;
6519 const struct elf_backend_data
*bed
;
6521 off
= elf_next_file_pos (abfd
);
6523 shdrpp
= elf_elfsections (abfd
);
6524 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6525 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6528 if (shdrp
->sh_offset
== -1)
6530 asection
*sec
= shdrp
->bfd_section
;
6531 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6532 || shdrp
->sh_type
== SHT_RELA
);
6533 bfd_boolean is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6536 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6538 if (!is_rel
&& !is_ctf
)
6540 const char *name
= sec
->name
;
6541 struct bfd_elf_section_data
*d
;
6543 /* Compress DWARF debug sections. */
6544 if (!bfd_compress_section (abfd
, sec
,
6548 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6549 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6551 /* If section is compressed with zlib-gnu, convert
6552 section name from .debug_* to .zdebug_*. */
6554 = convert_debug_to_zdebug (abfd
, name
);
6555 if (new_name
== NULL
)
6559 /* Add section name to section name section. */
6560 if (shdrp
->sh_name
!= (unsigned int) -1)
6563 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6565 d
= elf_section_data (sec
);
6567 /* Add reloc section name to section name section. */
6569 && !_bfd_elf_set_reloc_sh_name (abfd
,
6574 && !_bfd_elf_set_reloc_sh_name (abfd
,
6579 /* Update section size and contents. */
6580 shdrp
->sh_size
= sec
->size
;
6581 shdrp
->contents
= sec
->contents
;
6582 shdrp
->bfd_section
->contents
= NULL
;
6586 /* Update section size and contents. */
6587 shdrp
->sh_size
= sec
->size
;
6588 shdrp
->contents
= sec
->contents
;
6591 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6598 /* Place section name section after DWARF debug sections have been
6600 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6601 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6602 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6603 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6605 /* Place the section headers. */
6606 i_ehdrp
= elf_elfheader (abfd
);
6607 bed
= get_elf_backend_data (abfd
);
6608 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6609 i_ehdrp
->e_shoff
= off
;
6610 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6611 elf_next_file_pos (abfd
) = off
;
6617 _bfd_elf_write_object_contents (bfd
*abfd
)
6619 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6620 Elf_Internal_Shdr
**i_shdrp
;
6622 unsigned int count
, num_sec
;
6623 struct elf_obj_tdata
*t
;
6625 if (! abfd
->output_has_begun
6626 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6628 /* Do not rewrite ELF data when the BFD has been opened for update.
6629 abfd->output_has_begun was set to TRUE on opening, so creation of new
6630 sections, and modification of existing section sizes was restricted.
6631 This means the ELF header, program headers and section headers can't have
6633 If the contents of any sections has been modified, then those changes have
6634 already been written to the BFD. */
6635 else if (abfd
->direction
== both_direction
)
6637 BFD_ASSERT (abfd
->output_has_begun
);
6641 i_shdrp
= elf_elfsections (abfd
);
6644 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6648 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6651 /* After writing the headers, we need to write the sections too... */
6652 num_sec
= elf_numsections (abfd
);
6653 for (count
= 1; count
< num_sec
; count
++)
6655 i_shdrp
[count
]->sh_name
6656 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6657 i_shdrp
[count
]->sh_name
);
6658 if (bed
->elf_backend_section_processing
)
6659 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6661 if (i_shdrp
[count
]->contents
)
6663 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6665 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6666 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6671 /* Write out the section header names. */
6672 t
= elf_tdata (abfd
);
6673 if (elf_shstrtab (abfd
) != NULL
6674 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6675 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6678 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6681 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6684 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6685 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6686 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6692 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6694 /* Hopefully this can be done just like an object file. */
6695 return _bfd_elf_write_object_contents (abfd
);
6698 /* Given a section, search the header to find them. */
6701 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6703 const struct elf_backend_data
*bed
;
6704 unsigned int sec_index
;
6706 if (elf_section_data (asect
) != NULL
6707 && elf_section_data (asect
)->this_idx
!= 0)
6708 return elf_section_data (asect
)->this_idx
;
6710 if (bfd_is_abs_section (asect
))
6711 sec_index
= SHN_ABS
;
6712 else if (bfd_is_com_section (asect
))
6713 sec_index
= SHN_COMMON
;
6714 else if (bfd_is_und_section (asect
))
6715 sec_index
= SHN_UNDEF
;
6717 sec_index
= SHN_BAD
;
6719 bed
= get_elf_backend_data (abfd
);
6720 if (bed
->elf_backend_section_from_bfd_section
)
6722 int retval
= sec_index
;
6724 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6728 if (sec_index
== SHN_BAD
)
6729 bfd_set_error (bfd_error_nonrepresentable_section
);
6734 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6738 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6740 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6742 flagword flags
= asym_ptr
->flags
;
6744 /* When gas creates relocations against local labels, it creates its
6745 own symbol for the section, but does put the symbol into the
6746 symbol chain, so udata is 0. When the linker is generating
6747 relocatable output, this section symbol may be for one of the
6748 input sections rather than the output section. */
6749 if (asym_ptr
->udata
.i
== 0
6750 && (flags
& BSF_SECTION_SYM
)
6751 && asym_ptr
->section
)
6756 sec
= asym_ptr
->section
;
6757 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6758 sec
= sec
->output_section
;
6759 if (sec
->owner
== abfd
6760 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6761 && elf_section_syms (abfd
)[indx
] != NULL
)
6762 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6765 idx
= asym_ptr
->udata
.i
;
6769 /* This case can occur when using --strip-symbol on a symbol
6770 which is used in a relocation entry. */
6772 /* xgettext:c-format */
6773 (_("%pB: symbol `%s' required but not present"),
6774 abfd
, bfd_asymbol_name (asym_ptr
));
6775 bfd_set_error (bfd_error_no_symbols
);
6782 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6783 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6791 /* Rewrite program header information. */
6794 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6796 Elf_Internal_Ehdr
*iehdr
;
6797 struct elf_segment_map
*map
;
6798 struct elf_segment_map
*map_first
;
6799 struct elf_segment_map
**pointer_to_map
;
6800 Elf_Internal_Phdr
*segment
;
6803 unsigned int num_segments
;
6804 bfd_boolean phdr_included
= FALSE
;
6805 bfd_boolean p_paddr_valid
;
6806 bfd_vma maxpagesize
;
6807 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6808 unsigned int phdr_adjust_num
= 0;
6809 const struct elf_backend_data
*bed
;
6811 bed
= get_elf_backend_data (ibfd
);
6812 iehdr
= elf_elfheader (ibfd
);
6815 pointer_to_map
= &map_first
;
6817 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6818 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6820 /* Returns the end address of the segment + 1. */
6821 #define SEGMENT_END(segment, start) \
6822 (start + (segment->p_memsz > segment->p_filesz \
6823 ? segment->p_memsz : segment->p_filesz))
6825 #define SECTION_SIZE(section, segment) \
6826 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6827 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6828 ? section->size : 0)
6830 /* Returns TRUE if the given section is contained within
6831 the given segment. VMA addresses are compared. */
6832 #define IS_CONTAINED_BY_VMA(section, segment) \
6833 (section->vma >= segment->p_vaddr \
6834 && (section->vma + SECTION_SIZE (section, segment) \
6835 <= (SEGMENT_END (segment, segment->p_vaddr))))
6837 /* Returns TRUE if the given section is contained within
6838 the given segment. LMA addresses are compared. */
6839 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6840 (section->lma >= base \
6841 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6842 && (section->lma + SECTION_SIZE (section, segment) \
6843 <= SEGMENT_END (segment, base)))
6845 /* Handle PT_NOTE segment. */
6846 #define IS_NOTE(p, s) \
6847 (p->p_type == PT_NOTE \
6848 && elf_section_type (s) == SHT_NOTE \
6849 && (bfd_vma) s->filepos >= p->p_offset \
6850 && ((bfd_vma) s->filepos + s->size \
6851 <= p->p_offset + p->p_filesz))
6853 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6855 #define IS_COREFILE_NOTE(p, s) \
6857 && bfd_get_format (ibfd) == bfd_core \
6861 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6862 linker, which generates a PT_INTERP section with p_vaddr and
6863 p_memsz set to 0. */
6864 #define IS_SOLARIS_PT_INTERP(p, s) \
6866 && p->p_paddr == 0 \
6867 && p->p_memsz == 0 \
6868 && p->p_filesz > 0 \
6869 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6871 && (bfd_vma) s->filepos >= p->p_offset \
6872 && ((bfd_vma) s->filepos + s->size \
6873 <= p->p_offset + p->p_filesz))
6875 /* Decide if the given section should be included in the given segment.
6876 A section will be included if:
6877 1. It is within the address space of the segment -- we use the LMA
6878 if that is set for the segment and the VMA otherwise,
6879 2. It is an allocated section or a NOTE section in a PT_NOTE
6881 3. There is an output section associated with it,
6882 4. The section has not already been allocated to a previous segment.
6883 5. PT_GNU_STACK segments do not include any sections.
6884 6. PT_TLS segment includes only SHF_TLS sections.
6885 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6886 8. PT_DYNAMIC should not contain empty sections at the beginning
6887 (with the possible exception of .dynamic). */
6888 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6889 ((((segment->p_paddr \
6890 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6891 : IS_CONTAINED_BY_VMA (section, segment)) \
6892 && (section->flags & SEC_ALLOC) != 0) \
6893 || IS_NOTE (segment, section)) \
6894 && segment->p_type != PT_GNU_STACK \
6895 && (segment->p_type != PT_TLS \
6896 || (section->flags & SEC_THREAD_LOCAL)) \
6897 && (segment->p_type == PT_LOAD \
6898 || segment->p_type == PT_TLS \
6899 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6900 && (segment->p_type != PT_DYNAMIC \
6901 || SECTION_SIZE (section, segment) > 0 \
6902 || (segment->p_paddr \
6903 ? segment->p_paddr != section->lma \
6904 : segment->p_vaddr != section->vma) \
6905 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6906 && (segment->p_type != PT_LOAD || !section->segment_mark))
6908 /* If the output section of a section in the input segment is NULL,
6909 it is removed from the corresponding output segment. */
6910 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6911 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6912 && section->output_section != NULL)
6914 /* Returns TRUE iff seg1 starts after the end of seg2. */
6915 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6916 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6918 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6919 their VMA address ranges and their LMA address ranges overlap.
6920 It is possible to have overlapping VMA ranges without overlapping LMA
6921 ranges. RedBoot images for example can have both .data and .bss mapped
6922 to the same VMA range, but with the .data section mapped to a different
6924 #define SEGMENT_OVERLAPS(seg1, seg2) \
6925 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6926 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6927 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6928 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6930 /* Initialise the segment mark field. */
6931 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6932 section
->segment_mark
= FALSE
;
6934 /* The Solaris linker creates program headers in which all the
6935 p_paddr fields are zero. When we try to objcopy or strip such a
6936 file, we get confused. Check for this case, and if we find it
6937 don't set the p_paddr_valid fields. */
6938 p_paddr_valid
= FALSE
;
6939 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6942 if (segment
->p_paddr
!= 0)
6944 p_paddr_valid
= TRUE
;
6948 /* Scan through the segments specified in the program header
6949 of the input BFD. For this first scan we look for overlaps
6950 in the loadable segments. These can be created by weird
6951 parameters to objcopy. Also, fix some solaris weirdness. */
6952 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6957 Elf_Internal_Phdr
*segment2
;
6959 if (segment
->p_type
== PT_INTERP
)
6960 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6961 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6963 /* Mininal change so that the normal section to segment
6964 assignment code will work. */
6965 segment
->p_vaddr
= section
->vma
;
6969 if (segment
->p_type
!= PT_LOAD
)
6971 /* Remove PT_GNU_RELRO segment. */
6972 if (segment
->p_type
== PT_GNU_RELRO
)
6973 segment
->p_type
= PT_NULL
;
6977 /* Determine if this segment overlaps any previous segments. */
6978 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6980 bfd_signed_vma extra_length
;
6982 if (segment2
->p_type
!= PT_LOAD
6983 || !SEGMENT_OVERLAPS (segment
, segment2
))
6986 /* Merge the two segments together. */
6987 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6989 /* Extend SEGMENT2 to include SEGMENT and then delete
6991 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6992 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6994 if (extra_length
> 0)
6996 segment2
->p_memsz
+= extra_length
;
6997 segment2
->p_filesz
+= extra_length
;
7000 segment
->p_type
= PT_NULL
;
7002 /* Since we have deleted P we must restart the outer loop. */
7004 segment
= elf_tdata (ibfd
)->phdr
;
7009 /* Extend SEGMENT to include SEGMENT2 and then delete
7011 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
7012 - SEGMENT_END (segment
, segment
->p_vaddr
));
7014 if (extra_length
> 0)
7016 segment
->p_memsz
+= extra_length
;
7017 segment
->p_filesz
+= extra_length
;
7020 segment2
->p_type
= PT_NULL
;
7025 /* The second scan attempts to assign sections to segments. */
7026 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7030 unsigned int section_count
;
7031 asection
**sections
;
7032 asection
*output_section
;
7034 asection
*matching_lma
;
7035 asection
*suggested_lma
;
7038 asection
*first_section
;
7040 if (segment
->p_type
== PT_NULL
)
7043 first_section
= NULL
;
7044 /* Compute how many sections might be placed into this segment. */
7045 for (section
= ibfd
->sections
, section_count
= 0;
7047 section
= section
->next
)
7049 /* Find the first section in the input segment, which may be
7050 removed from the corresponding output segment. */
7051 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
7053 if (first_section
== NULL
)
7054 first_section
= section
;
7055 if (section
->output_section
!= NULL
)
7060 /* Allocate a segment map big enough to contain
7061 all of the sections we have selected. */
7062 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7063 amt
+= section_count
* sizeof (asection
*);
7064 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7068 /* Initialise the fields of the segment map. Default to
7069 using the physical address of the segment in the input BFD. */
7071 map
->p_type
= segment
->p_type
;
7072 map
->p_flags
= segment
->p_flags
;
7073 map
->p_flags_valid
= 1;
7075 /* If the first section in the input segment is removed, there is
7076 no need to preserve segment physical address in the corresponding
7078 if (!first_section
|| first_section
->output_section
!= NULL
)
7080 map
->p_paddr
= segment
->p_paddr
;
7081 map
->p_paddr_valid
= p_paddr_valid
;
7084 /* Determine if this segment contains the ELF file header
7085 and if it contains the program headers themselves. */
7086 map
->includes_filehdr
= (segment
->p_offset
== 0
7087 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7088 map
->includes_phdrs
= 0;
7090 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7092 map
->includes_phdrs
=
7093 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7094 && (segment
->p_offset
+ segment
->p_filesz
7095 >= ((bfd_vma
) iehdr
->e_phoff
7096 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7098 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7099 phdr_included
= TRUE
;
7102 if (section_count
== 0)
7104 /* Special segments, such as the PT_PHDR segment, may contain
7105 no sections, but ordinary, loadable segments should contain
7106 something. They are allowed by the ELF spec however, so only
7107 a warning is produced.
7108 There is however the valid use case of embedded systems which
7109 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7110 flash memory with zeros. No warning is shown for that case. */
7111 if (segment
->p_type
== PT_LOAD
7112 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7113 /* xgettext:c-format */
7115 (_("%pB: warning: empty loadable segment detected"
7116 " at vaddr=%#" PRIx64
", is this intentional?"),
7117 ibfd
, (uint64_t) segment
->p_vaddr
);
7119 map
->p_vaddr_offset
= segment
->p_vaddr
;
7121 *pointer_to_map
= map
;
7122 pointer_to_map
= &map
->next
;
7127 /* Now scan the sections in the input BFD again and attempt
7128 to add their corresponding output sections to the segment map.
7129 The problem here is how to handle an output section which has
7130 been moved (ie had its LMA changed). There are four possibilities:
7132 1. None of the sections have been moved.
7133 In this case we can continue to use the segment LMA from the
7136 2. All of the sections have been moved by the same amount.
7137 In this case we can change the segment's LMA to match the LMA
7138 of the first section.
7140 3. Some of the sections have been moved, others have not.
7141 In this case those sections which have not been moved can be
7142 placed in the current segment which will have to have its size,
7143 and possibly its LMA changed, and a new segment or segments will
7144 have to be created to contain the other sections.
7146 4. The sections have been moved, but not by the same amount.
7147 In this case we can change the segment's LMA to match the LMA
7148 of the first section and we will have to create a new segment
7149 or segments to contain the other sections.
7151 In order to save time, we allocate an array to hold the section
7152 pointers that we are interested in. As these sections get assigned
7153 to a segment, they are removed from this array. */
7155 amt
= section_count
* sizeof (asection
*);
7156 sections
= (asection
**) bfd_malloc (amt
);
7157 if (sections
== NULL
)
7160 /* Step One: Scan for segment vs section LMA conflicts.
7161 Also add the sections to the section array allocated above.
7162 Also add the sections to the current segment. In the common
7163 case, where the sections have not been moved, this means that
7164 we have completely filled the segment, and there is nothing
7167 matching_lma
= NULL
;
7168 suggested_lma
= NULL
;
7170 for (section
= first_section
, j
= 0;
7172 section
= section
->next
)
7174 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
7176 output_section
= section
->output_section
;
7178 sections
[j
++] = section
;
7180 /* The Solaris native linker always sets p_paddr to 0.
7181 We try to catch that case here, and set it to the
7182 correct value. Note - some backends require that
7183 p_paddr be left as zero. */
7185 && segment
->p_vaddr
!= 0
7186 && !bed
->want_p_paddr_set_to_zero
7188 && output_section
->lma
!= 0
7189 && (align_power (segment
->p_vaddr
7190 + (map
->includes_filehdr
7191 ? iehdr
->e_ehsize
: 0)
7192 + (map
->includes_phdrs
7193 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7195 output_section
->alignment_power
)
7196 == output_section
->vma
))
7197 map
->p_paddr
= segment
->p_vaddr
;
7199 /* Match up the physical address of the segment with the
7200 LMA address of the output section. */
7201 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7202 || IS_COREFILE_NOTE (segment
, section
)
7203 || (bed
->want_p_paddr_set_to_zero
7204 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
7206 if (matching_lma
== NULL
7207 || output_section
->lma
< matching_lma
->lma
)
7208 matching_lma
= output_section
;
7210 /* We assume that if the section fits within the segment
7211 then it does not overlap any other section within that
7213 map
->sections
[isec
++] = output_section
;
7215 else if (suggested_lma
== NULL
)
7216 suggested_lma
= output_section
;
7218 if (j
== section_count
)
7223 BFD_ASSERT (j
== section_count
);
7225 /* Step Two: Adjust the physical address of the current segment,
7227 if (isec
== section_count
)
7229 /* All of the sections fitted within the segment as currently
7230 specified. This is the default case. Add the segment to
7231 the list of built segments and carry on to process the next
7232 program header in the input BFD. */
7233 map
->count
= section_count
;
7234 *pointer_to_map
= map
;
7235 pointer_to_map
= &map
->next
;
7238 && !bed
->want_p_paddr_set_to_zero
)
7240 bfd_vma hdr_size
= 0;
7241 if (map
->includes_filehdr
)
7242 hdr_size
= iehdr
->e_ehsize
;
7243 if (map
->includes_phdrs
)
7244 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7246 /* Account for padding before the first section in the
7248 map
->p_vaddr_offset
= map
->p_paddr
+ hdr_size
- matching_lma
->lma
;
7256 /* Change the current segment's physical address to match
7257 the LMA of the first section that fitted, or if no
7258 section fitted, the first section. */
7259 if (matching_lma
== NULL
)
7260 matching_lma
= suggested_lma
;
7262 map
->p_paddr
= matching_lma
->lma
;
7264 /* Offset the segment physical address from the lma
7265 to allow for space taken up by elf headers. */
7266 if (map
->includes_phdrs
)
7268 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7270 /* iehdr->e_phnum is just an estimate of the number
7271 of program headers that we will need. Make a note
7272 here of the number we used and the segment we chose
7273 to hold these headers, so that we can adjust the
7274 offset when we know the correct value. */
7275 phdr_adjust_num
= iehdr
->e_phnum
;
7276 phdr_adjust_seg
= map
;
7279 if (map
->includes_filehdr
)
7281 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7282 map
->p_paddr
-= iehdr
->e_ehsize
;
7283 /* We've subtracted off the size of headers from the
7284 first section lma, but there may have been some
7285 alignment padding before that section too. Try to
7286 account for that by adjusting the segment lma down to
7287 the same alignment. */
7288 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7289 align
= segment
->p_align
;
7290 map
->p_paddr
&= -align
;
7294 /* Step Three: Loop over the sections again, this time assigning
7295 those that fit to the current segment and removing them from the
7296 sections array; but making sure not to leave large gaps. Once all
7297 possible sections have been assigned to the current segment it is
7298 added to the list of built segments and if sections still remain
7299 to be assigned, a new segment is constructed before repeating
7305 suggested_lma
= NULL
;
7307 /* Fill the current segment with sections that fit. */
7308 for (j
= 0; j
< section_count
; j
++)
7310 section
= sections
[j
];
7312 if (section
== NULL
)
7315 output_section
= section
->output_section
;
7317 BFD_ASSERT (output_section
!= NULL
);
7319 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7320 || IS_COREFILE_NOTE (segment
, section
))
7322 if (map
->count
== 0)
7324 /* If the first section in a segment does not start at
7325 the beginning of the segment, then something is
7327 if (align_power (map
->p_paddr
7328 + (map
->includes_filehdr
7329 ? iehdr
->e_ehsize
: 0)
7330 + (map
->includes_phdrs
7331 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7333 output_section
->alignment_power
)
7334 != output_section
->lma
)
7341 prev_sec
= map
->sections
[map
->count
- 1];
7343 /* If the gap between the end of the previous section
7344 and the start of this section is more than
7345 maxpagesize then we need to start a new segment. */
7346 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7348 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7349 || (prev_sec
->lma
+ prev_sec
->size
7350 > output_section
->lma
))
7352 if (suggested_lma
== NULL
)
7353 suggested_lma
= output_section
;
7359 map
->sections
[map
->count
++] = output_section
;
7362 if (segment
->p_type
== PT_LOAD
)
7363 section
->segment_mark
= TRUE
;
7365 else if (suggested_lma
== NULL
)
7366 suggested_lma
= output_section
;
7369 /* PR 23932. A corrupt input file may contain sections that cannot
7370 be assigned to any segment - because for example they have a
7371 negative size - or segments that do not contain any sections.
7372 But there are also valid reasons why a segment can be empty.
7373 So allow a count of zero. */
7375 /* Add the current segment to the list of built segments. */
7376 *pointer_to_map
= map
;
7377 pointer_to_map
= &map
->next
;
7379 if (isec
< section_count
)
7381 /* We still have not allocated all of the sections to
7382 segments. Create a new segment here, initialise it
7383 and carry on looping. */
7384 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7385 amt
+= section_count
* sizeof (asection
*);
7386 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7393 /* Initialise the fields of the segment map. Set the physical
7394 physical address to the LMA of the first section that has
7395 not yet been assigned. */
7397 map
->p_type
= segment
->p_type
;
7398 map
->p_flags
= segment
->p_flags
;
7399 map
->p_flags_valid
= 1;
7400 map
->p_paddr
= suggested_lma
->lma
;
7401 map
->p_paddr_valid
= p_paddr_valid
;
7402 map
->includes_filehdr
= 0;
7403 map
->includes_phdrs
= 0;
7408 bfd_set_error (bfd_error_sorry
);
7412 while (isec
< section_count
);
7417 elf_seg_map (obfd
) = map_first
;
7419 /* If we had to estimate the number of program headers that were
7420 going to be needed, then check our estimate now and adjust
7421 the offset if necessary. */
7422 if (phdr_adjust_seg
!= NULL
)
7426 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7429 if (count
> phdr_adjust_num
)
7430 phdr_adjust_seg
->p_paddr
7431 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7433 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7434 if (map
->p_type
== PT_PHDR
)
7437 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7438 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7445 #undef IS_CONTAINED_BY_VMA
7446 #undef IS_CONTAINED_BY_LMA
7448 #undef IS_COREFILE_NOTE
7449 #undef IS_SOLARIS_PT_INTERP
7450 #undef IS_SECTION_IN_INPUT_SEGMENT
7451 #undef INCLUDE_SECTION_IN_SEGMENT
7452 #undef SEGMENT_AFTER_SEGMENT
7453 #undef SEGMENT_OVERLAPS
7457 /* Copy ELF program header information. */
7460 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7462 Elf_Internal_Ehdr
*iehdr
;
7463 struct elf_segment_map
*map
;
7464 struct elf_segment_map
*map_first
;
7465 struct elf_segment_map
**pointer_to_map
;
7466 Elf_Internal_Phdr
*segment
;
7468 unsigned int num_segments
;
7469 bfd_boolean phdr_included
= FALSE
;
7470 bfd_boolean p_paddr_valid
;
7472 iehdr
= elf_elfheader (ibfd
);
7475 pointer_to_map
= &map_first
;
7477 /* If all the segment p_paddr fields are zero, don't set
7478 map->p_paddr_valid. */
7479 p_paddr_valid
= FALSE
;
7480 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7481 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7484 if (segment
->p_paddr
!= 0)
7486 p_paddr_valid
= TRUE
;
7490 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7495 unsigned int section_count
;
7497 Elf_Internal_Shdr
*this_hdr
;
7498 asection
*first_section
= NULL
;
7499 asection
*lowest_section
;
7501 /* Compute how many sections are in this segment. */
7502 for (section
= ibfd
->sections
, section_count
= 0;
7504 section
= section
->next
)
7506 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7507 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7509 if (first_section
== NULL
)
7510 first_section
= section
;
7515 /* Allocate a segment map big enough to contain
7516 all of the sections we have selected. */
7517 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7518 amt
+= section_count
* sizeof (asection
*);
7519 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7523 /* Initialize the fields of the output segment map with the
7526 map
->p_type
= segment
->p_type
;
7527 map
->p_flags
= segment
->p_flags
;
7528 map
->p_flags_valid
= 1;
7529 map
->p_paddr
= segment
->p_paddr
;
7530 map
->p_paddr_valid
= p_paddr_valid
;
7531 map
->p_align
= segment
->p_align
;
7532 map
->p_align_valid
= 1;
7533 map
->p_vaddr_offset
= 0;
7535 if (map
->p_type
== PT_GNU_RELRO
7536 || map
->p_type
== PT_GNU_STACK
)
7538 /* The PT_GNU_RELRO segment may contain the first a few
7539 bytes in the .got.plt section even if the whole .got.plt
7540 section isn't in the PT_GNU_RELRO segment. We won't
7541 change the size of the PT_GNU_RELRO segment.
7542 Similarly, PT_GNU_STACK size is significant on uclinux
7544 map
->p_size
= segment
->p_memsz
;
7545 map
->p_size_valid
= 1;
7548 /* Determine if this segment contains the ELF file header
7549 and if it contains the program headers themselves. */
7550 map
->includes_filehdr
= (segment
->p_offset
== 0
7551 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7553 map
->includes_phdrs
= 0;
7554 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7556 map
->includes_phdrs
=
7557 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7558 && (segment
->p_offset
+ segment
->p_filesz
7559 >= ((bfd_vma
) iehdr
->e_phoff
7560 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7562 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7563 phdr_included
= TRUE
;
7566 lowest_section
= NULL
;
7567 if (section_count
!= 0)
7569 unsigned int isec
= 0;
7571 for (section
= first_section
;
7573 section
= section
->next
)
7575 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7576 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7578 map
->sections
[isec
++] = section
->output_section
;
7579 if ((section
->flags
& SEC_ALLOC
) != 0)
7583 if (lowest_section
== NULL
7584 || section
->lma
< lowest_section
->lma
)
7585 lowest_section
= section
;
7587 /* Section lmas are set up from PT_LOAD header
7588 p_paddr in _bfd_elf_make_section_from_shdr.
7589 If this header has a p_paddr that disagrees
7590 with the section lma, flag the p_paddr as
7592 if ((section
->flags
& SEC_LOAD
) != 0)
7593 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7595 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7596 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7597 map
->p_paddr_valid
= FALSE
;
7599 if (isec
== section_count
)
7605 if (section_count
== 0)
7606 map
->p_vaddr_offset
= segment
->p_vaddr
;
7607 else if (map
->p_paddr_valid
)
7609 /* Account for padding before the first section in the segment. */
7610 bfd_vma hdr_size
= 0;
7611 if (map
->includes_filehdr
)
7612 hdr_size
= iehdr
->e_ehsize
;
7613 if (map
->includes_phdrs
)
7614 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7616 map
->p_vaddr_offset
= (map
->p_paddr
+ hdr_size
7617 - (lowest_section
? lowest_section
->lma
: 0));
7620 map
->count
= section_count
;
7621 *pointer_to_map
= map
;
7622 pointer_to_map
= &map
->next
;
7625 elf_seg_map (obfd
) = map_first
;
7629 /* Copy private BFD data. This copies or rewrites ELF program header
7633 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7635 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7636 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7639 if (elf_tdata (ibfd
)->phdr
== NULL
)
7642 if (ibfd
->xvec
== obfd
->xvec
)
7644 /* Check to see if any sections in the input BFD
7645 covered by ELF program header have changed. */
7646 Elf_Internal_Phdr
*segment
;
7647 asection
*section
, *osec
;
7648 unsigned int i
, num_segments
;
7649 Elf_Internal_Shdr
*this_hdr
;
7650 const struct elf_backend_data
*bed
;
7652 bed
= get_elf_backend_data (ibfd
);
7654 /* Regenerate the segment map if p_paddr is set to 0. */
7655 if (bed
->want_p_paddr_set_to_zero
)
7658 /* Initialize the segment mark field. */
7659 for (section
= obfd
->sections
; section
!= NULL
;
7660 section
= section
->next
)
7661 section
->segment_mark
= FALSE
;
7663 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7664 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7668 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7669 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7670 which severly confuses things, so always regenerate the segment
7671 map in this case. */
7672 if (segment
->p_paddr
== 0
7673 && segment
->p_memsz
== 0
7674 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7677 for (section
= ibfd
->sections
;
7678 section
!= NULL
; section
= section
->next
)
7680 /* We mark the output section so that we know it comes
7681 from the input BFD. */
7682 osec
= section
->output_section
;
7684 osec
->segment_mark
= TRUE
;
7686 /* Check if this section is covered by the segment. */
7687 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7688 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7690 /* FIXME: Check if its output section is changed or
7691 removed. What else do we need to check? */
7693 || section
->flags
!= osec
->flags
7694 || section
->lma
!= osec
->lma
7695 || section
->vma
!= osec
->vma
7696 || section
->size
!= osec
->size
7697 || section
->rawsize
!= osec
->rawsize
7698 || section
->alignment_power
!= osec
->alignment_power
)
7704 /* Check to see if any output section do not come from the
7706 for (section
= obfd
->sections
; section
!= NULL
;
7707 section
= section
->next
)
7709 if (!section
->segment_mark
)
7712 section
->segment_mark
= FALSE
;
7715 return copy_elf_program_header (ibfd
, obfd
);
7719 if (ibfd
->xvec
== obfd
->xvec
)
7721 /* When rewriting program header, set the output maxpagesize to
7722 the maximum alignment of input PT_LOAD segments. */
7723 Elf_Internal_Phdr
*segment
;
7725 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7726 bfd_vma maxpagesize
= 0;
7728 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7731 if (segment
->p_type
== PT_LOAD
7732 && maxpagesize
< segment
->p_align
)
7734 /* PR 17512: file: f17299af. */
7735 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7736 /* xgettext:c-format */
7737 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7738 PRIx64
" is too large"),
7739 ibfd
, (uint64_t) segment
->p_align
);
7741 maxpagesize
= segment
->p_align
;
7744 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7745 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7748 return rewrite_elf_program_header (ibfd
, obfd
);
7751 /* Initialize private output section information from input section. */
7754 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7758 struct bfd_link_info
*link_info
)
7761 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7762 bfd_boolean final_link
= (link_info
!= NULL
7763 && !bfd_link_relocatable (link_info
));
7765 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7766 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7769 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7771 /* For objcopy and relocatable link, don't copy the output ELF
7772 section type from input if the output BFD section flags have been
7773 set to something different. For a final link allow some flags
7774 that the linker clears to differ. */
7775 if (elf_section_type (osec
) == SHT_NULL
7776 && (osec
->flags
== isec
->flags
7778 && ((osec
->flags
^ isec
->flags
)
7779 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7780 elf_section_type (osec
) = elf_section_type (isec
);
7782 /* FIXME: Is this correct for all OS/PROC specific flags? */
7783 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7784 & (SHF_MASKOS
| SHF_MASKPROC
));
7786 /* Copy sh_info from input for mbind section. */
7787 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7788 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7789 elf_section_data (osec
)->this_hdr
.sh_info
7790 = elf_section_data (isec
)->this_hdr
.sh_info
;
7792 /* Set things up for objcopy and relocatable link. The output
7793 SHT_GROUP section will have its elf_next_in_group pointing back
7794 to the input group members. Ignore linker created group section.
7795 See elfNN_ia64_object_p in elfxx-ia64.c. */
7796 if ((link_info
== NULL
7797 || !link_info
->resolve_section_groups
)
7798 && (elf_sec_group (isec
) == NULL
7799 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7801 if (elf_section_flags (isec
) & SHF_GROUP
)
7802 elf_section_flags (osec
) |= SHF_GROUP
;
7803 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7804 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7807 /* If not decompress, preserve SHF_COMPRESSED. */
7808 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7809 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7812 ihdr
= &elf_section_data (isec
)->this_hdr
;
7814 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7815 don't use the output section of the linked-to section since it
7816 may be NULL at this point. */
7817 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7819 ohdr
= &elf_section_data (osec
)->this_hdr
;
7820 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7821 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7824 osec
->use_rela_p
= isec
->use_rela_p
;
7829 /* Copy private section information. This copies over the entsize
7830 field, and sometimes the info field. */
7833 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7838 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7840 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7841 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7844 ihdr
= &elf_section_data (isec
)->this_hdr
;
7845 ohdr
= &elf_section_data (osec
)->this_hdr
;
7847 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7849 if (ihdr
->sh_type
== SHT_SYMTAB
7850 || ihdr
->sh_type
== SHT_DYNSYM
7851 || ihdr
->sh_type
== SHT_GNU_verneed
7852 || ihdr
->sh_type
== SHT_GNU_verdef
)
7853 ohdr
->sh_info
= ihdr
->sh_info
;
7855 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7859 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7860 necessary if we are removing either the SHT_GROUP section or any of
7861 the group member sections. DISCARDED is the value that a section's
7862 output_section has if the section will be discarded, NULL when this
7863 function is called from objcopy, bfd_abs_section_ptr when called
7867 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7871 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7872 if (elf_section_type (isec
) == SHT_GROUP
)
7874 asection
*first
= elf_next_in_group (isec
);
7875 asection
*s
= first
;
7876 bfd_size_type removed
= 0;
7880 /* If this member section is being output but the
7881 SHT_GROUP section is not, then clear the group info
7882 set up by _bfd_elf_copy_private_section_data. */
7883 if (s
->output_section
!= discarded
7884 && isec
->output_section
== discarded
)
7886 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7887 elf_group_name (s
->output_section
) = NULL
;
7889 /* Conversely, if the member section is not being output
7890 but the SHT_GROUP section is, then adjust its size. */
7891 else if (s
->output_section
== discarded
7892 && isec
->output_section
!= discarded
)
7894 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7896 if (elf_sec
->rel
.hdr
!= NULL
7897 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7899 if (elf_sec
->rela
.hdr
!= NULL
7900 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7903 s
= elf_next_in_group (s
);
7909 if (discarded
!= NULL
)
7911 /* If we've been called for ld -r, then we need to
7912 adjust the input section size. */
7913 if (isec
->rawsize
== 0)
7914 isec
->rawsize
= isec
->size
;
7915 isec
->size
= isec
->rawsize
- removed
;
7916 if (isec
->size
<= 4)
7919 isec
->flags
|= SEC_EXCLUDE
;
7924 /* Adjust the output section size when called from
7926 isec
->output_section
->size
-= removed
;
7927 if (isec
->output_section
->size
<= 4)
7929 isec
->output_section
->size
= 0;
7930 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7939 /* Copy private header information. */
7942 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7944 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7945 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7948 /* Copy over private BFD data if it has not already been copied.
7949 This must be done here, rather than in the copy_private_bfd_data
7950 entry point, because the latter is called after the section
7951 contents have been set, which means that the program headers have
7952 already been worked out. */
7953 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7955 if (! copy_private_bfd_data (ibfd
, obfd
))
7959 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7962 /* Copy private symbol information. If this symbol is in a section
7963 which we did not map into a BFD section, try to map the section
7964 index correctly. We use special macro definitions for the mapped
7965 section indices; these definitions are interpreted by the
7966 swap_out_syms function. */
7968 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7969 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7970 #define MAP_STRTAB (SHN_HIOS + 3)
7971 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7972 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7975 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7980 elf_symbol_type
*isym
, *osym
;
7982 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7983 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7986 isym
= elf_symbol_from (ibfd
, isymarg
);
7987 osym
= elf_symbol_from (obfd
, osymarg
);
7990 && isym
->internal_elf_sym
.st_shndx
!= 0
7992 && bfd_is_abs_section (isym
->symbol
.section
))
7996 shndx
= isym
->internal_elf_sym
.st_shndx
;
7997 if (shndx
== elf_onesymtab (ibfd
))
7998 shndx
= MAP_ONESYMTAB
;
7999 else if (shndx
== elf_dynsymtab (ibfd
))
8000 shndx
= MAP_DYNSYMTAB
;
8001 else if (shndx
== elf_strtab_sec (ibfd
))
8003 else if (shndx
== elf_shstrtab_sec (ibfd
))
8004 shndx
= MAP_SHSTRTAB
;
8005 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8006 shndx
= MAP_SYM_SHNDX
;
8007 osym
->internal_elf_sym
.st_shndx
= shndx
;
8013 /* Swap out the symbols. */
8016 swap_out_syms (bfd
*abfd
,
8017 struct elf_strtab_hash
**sttp
,
8020 const struct elf_backend_data
*bed
;
8021 unsigned int symcount
;
8023 struct elf_strtab_hash
*stt
;
8024 Elf_Internal_Shdr
*symtab_hdr
;
8025 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8026 Elf_Internal_Shdr
*symstrtab_hdr
;
8027 struct elf_sym_strtab
*symstrtab
;
8028 bfd_byte
*outbound_syms
;
8029 bfd_byte
*outbound_shndx
;
8030 unsigned long outbound_syms_index
;
8031 unsigned long outbound_shndx_index
;
8033 unsigned int num_locals
;
8035 bfd_boolean name_local_sections
;
8037 if (!elf_map_symbols (abfd
, &num_locals
))
8040 /* Dump out the symtabs. */
8041 stt
= _bfd_elf_strtab_init ();
8045 bed
= get_elf_backend_data (abfd
);
8046 symcount
= bfd_get_symcount (abfd
);
8047 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8048 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8049 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8050 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8051 symtab_hdr
->sh_info
= num_locals
+ 1;
8052 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8054 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8055 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8057 /* Allocate buffer to swap out the .strtab section. */
8058 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8059 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8061 bfd_set_error (bfd_error_no_memory
);
8062 _bfd_elf_strtab_free (stt
);
8066 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8067 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8070 bfd_set_error (bfd_error_no_memory
);
8073 _bfd_elf_strtab_free (stt
);
8076 symtab_hdr
->contents
= outbound_syms
;
8077 outbound_syms_index
= 0;
8079 outbound_shndx
= NULL
;
8080 outbound_shndx_index
= 0;
8082 if (elf_symtab_shndx_list (abfd
))
8084 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8085 if (symtab_shndx_hdr
->sh_name
!= 0)
8087 if (_bfd_mul_overflow (symcount
+ 1,
8088 sizeof (Elf_External_Sym_Shndx
), &amt
))
8090 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8091 if (outbound_shndx
== NULL
)
8094 symtab_shndx_hdr
->contents
= outbound_shndx
;
8095 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8096 symtab_shndx_hdr
->sh_size
= amt
;
8097 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8098 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8100 /* FIXME: What about any other headers in the list ? */
8103 /* Now generate the data (for "contents"). */
8105 /* Fill in zeroth symbol and swap it out. */
8106 Elf_Internal_Sym sym
;
8112 sym
.st_shndx
= SHN_UNDEF
;
8113 sym
.st_target_internal
= 0;
8114 symstrtab
[0].sym
= sym
;
8115 symstrtab
[0].dest_index
= outbound_syms_index
;
8116 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8117 outbound_syms_index
++;
8118 if (outbound_shndx
!= NULL
)
8119 outbound_shndx_index
++;
8123 = (bed
->elf_backend_name_local_section_symbols
8124 && bed
->elf_backend_name_local_section_symbols (abfd
));
8126 syms
= bfd_get_outsymbols (abfd
);
8127 for (idx
= 0; idx
< symcount
;)
8129 Elf_Internal_Sym sym
;
8130 bfd_vma value
= syms
[idx
]->value
;
8131 elf_symbol_type
*type_ptr
;
8132 flagword flags
= syms
[idx
]->flags
;
8135 if (!name_local_sections
8136 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8138 /* Local section symbols have no name. */
8139 sym
.st_name
= (unsigned long) -1;
8143 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8144 to get the final offset for st_name. */
8146 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8148 if (sym
.st_name
== (unsigned long) -1)
8152 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
8154 if ((flags
& BSF_SECTION_SYM
) == 0
8155 && bfd_is_com_section (syms
[idx
]->section
))
8157 /* ELF common symbols put the alignment into the `value' field,
8158 and the size into the `size' field. This is backwards from
8159 how BFD handles it, so reverse it here. */
8160 sym
.st_size
= value
;
8161 if (type_ptr
== NULL
8162 || type_ptr
->internal_elf_sym
.st_value
== 0)
8163 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8165 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8166 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8167 (abfd
, syms
[idx
]->section
);
8171 asection
*sec
= syms
[idx
]->section
;
8174 if (sec
->output_section
)
8176 value
+= sec
->output_offset
;
8177 sec
= sec
->output_section
;
8180 /* Don't add in the section vma for relocatable output. */
8181 if (! relocatable_p
)
8183 sym
.st_value
= value
;
8184 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8186 if (bfd_is_abs_section (sec
)
8188 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8190 /* This symbol is in a real ELF section which we did
8191 not create as a BFD section. Undo the mapping done
8192 by copy_private_symbol_data. */
8193 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8197 shndx
= elf_onesymtab (abfd
);
8200 shndx
= elf_dynsymtab (abfd
);
8203 shndx
= elf_strtab_sec (abfd
);
8206 shndx
= elf_shstrtab_sec (abfd
);
8209 if (elf_symtab_shndx_list (abfd
))
8210 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8219 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8221 if (shndx
== SHN_BAD
)
8225 /* Writing this would be a hell of a lot easier if
8226 we had some decent documentation on bfd, and
8227 knew what to expect of the library, and what to
8228 demand of applications. For example, it
8229 appears that `objcopy' might not set the
8230 section of a symbol to be a section that is
8231 actually in the output file. */
8232 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8234 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8235 if (shndx
== SHN_BAD
)
8237 /* xgettext:c-format */
8239 (_("unable to find equivalent output section"
8240 " for symbol '%s' from section '%s'"),
8241 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8243 bfd_set_error (bfd_error_invalid_operation
);
8249 sym
.st_shndx
= shndx
;
8252 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8254 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8255 type
= STT_GNU_IFUNC
;
8256 else if ((flags
& BSF_FUNCTION
) != 0)
8258 else if ((flags
& BSF_OBJECT
) != 0)
8260 else if ((flags
& BSF_RELC
) != 0)
8262 else if ((flags
& BSF_SRELC
) != 0)
8267 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8270 /* Processor-specific types. */
8271 if (type_ptr
!= NULL
8272 && bed
->elf_backend_get_symbol_type
)
8273 type
= ((*bed
->elf_backend_get_symbol_type
)
8274 (&type_ptr
->internal_elf_sym
, type
));
8276 if (flags
& BSF_SECTION_SYM
)
8278 if (flags
& BSF_GLOBAL
)
8279 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8281 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8283 else if (bfd_is_com_section (syms
[idx
]->section
))
8285 if (type
!= STT_TLS
)
8287 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8288 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8289 ? STT_COMMON
: STT_OBJECT
);
8291 type
= ((flags
& BSF_ELF_COMMON
) != 0
8292 ? STT_COMMON
: STT_OBJECT
);
8294 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8296 else if (bfd_is_und_section (syms
[idx
]->section
))
8297 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8301 else if (flags
& BSF_FILE
)
8302 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8305 int bind
= STB_LOCAL
;
8307 if (flags
& BSF_LOCAL
)
8309 else if (flags
& BSF_GNU_UNIQUE
)
8310 bind
= STB_GNU_UNIQUE
;
8311 else if (flags
& BSF_WEAK
)
8313 else if (flags
& BSF_GLOBAL
)
8316 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8319 if (type_ptr
!= NULL
)
8321 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8322 sym
.st_target_internal
8323 = type_ptr
->internal_elf_sym
.st_target_internal
;
8328 sym
.st_target_internal
= 0;
8332 symstrtab
[idx
].sym
= sym
;
8333 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8334 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8336 outbound_syms_index
++;
8337 if (outbound_shndx
!= NULL
)
8338 outbound_shndx_index
++;
8341 /* Finalize the .strtab section. */
8342 _bfd_elf_strtab_finalize (stt
);
8344 /* Swap out the .strtab section. */
8345 for (idx
= 0; idx
<= symcount
; idx
++)
8347 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8348 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8349 elfsym
->sym
.st_name
= 0;
8351 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8352 elfsym
->sym
.st_name
);
8353 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8355 + (elfsym
->dest_index
8356 * bed
->s
->sizeof_sym
)),
8358 + (elfsym
->destshndx_index
8359 * sizeof (Elf_External_Sym_Shndx
))));
8364 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8365 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8366 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8367 symstrtab_hdr
->sh_addr
= 0;
8368 symstrtab_hdr
->sh_entsize
= 0;
8369 symstrtab_hdr
->sh_link
= 0;
8370 symstrtab_hdr
->sh_info
= 0;
8371 symstrtab_hdr
->sh_addralign
= 1;
8376 /* Return the number of bytes required to hold the symtab vector.
8378 Note that we base it on the count plus 1, since we will null terminate
8379 the vector allocated based on this size. However, the ELF symbol table
8380 always has a dummy entry as symbol #0, so it ends up even. */
8383 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8385 bfd_size_type symcount
;
8387 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8389 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8390 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8392 bfd_set_error (bfd_error_file_too_big
);
8395 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8397 symtab_size
-= sizeof (asymbol
*);
8403 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8405 bfd_size_type symcount
;
8407 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8409 if (elf_dynsymtab (abfd
) == 0)
8411 bfd_set_error (bfd_error_invalid_operation
);
8415 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8416 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8418 bfd_set_error (bfd_error_file_too_big
);
8421 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8423 symtab_size
-= sizeof (asymbol
*);
8429 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8432 #if SIZEOF_LONG == SIZEOF_INT
8433 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8435 bfd_set_error (bfd_error_file_too_big
);
8439 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8442 /* Canonicalize the relocs. */
8445 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8452 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8454 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8457 tblptr
= section
->relocation
;
8458 for (i
= 0; i
< section
->reloc_count
; i
++)
8459 *relptr
++ = tblptr
++;
8463 return section
->reloc_count
;
8467 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8469 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8470 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8473 abfd
->symcount
= symcount
;
8478 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8479 asymbol
**allocation
)
8481 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8482 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8485 abfd
->dynsymcount
= symcount
;
8489 /* Return the size required for the dynamic reloc entries. Any loadable
8490 section that was actually installed in the BFD, and has type SHT_REL
8491 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8492 dynamic reloc section. */
8495 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8497 bfd_size_type count
;
8500 if (elf_dynsymtab (abfd
) == 0)
8502 bfd_set_error (bfd_error_invalid_operation
);
8507 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8508 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8509 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8510 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8512 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8513 if (count
> LONG_MAX
/ sizeof (arelent
*))
8515 bfd_set_error (bfd_error_file_too_big
);
8519 return count
* sizeof (arelent
*);
8522 /* Canonicalize the dynamic relocation entries. Note that we return the
8523 dynamic relocations as a single block, although they are actually
8524 associated with particular sections; the interface, which was
8525 designed for SunOS style shared libraries, expects that there is only
8526 one set of dynamic relocs. Any loadable section that was actually
8527 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8528 dynamic symbol table, is considered to be a dynamic reloc section. */
8531 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8535 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8539 if (elf_dynsymtab (abfd
) == 0)
8541 bfd_set_error (bfd_error_invalid_operation
);
8545 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8547 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8549 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8550 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8551 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8556 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8558 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8560 for (i
= 0; i
< count
; i
++)
8571 /* Read in the version information. */
8574 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8576 bfd_byte
*contents
= NULL
;
8577 unsigned int freeidx
= 0;
8580 if (elf_dynverref (abfd
) != 0)
8582 Elf_Internal_Shdr
*hdr
;
8583 Elf_External_Verneed
*everneed
;
8584 Elf_Internal_Verneed
*iverneed
;
8586 bfd_byte
*contents_end
;
8588 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8590 if (hdr
->sh_info
== 0
8591 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8593 error_return_bad_verref
:
8595 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8596 bfd_set_error (bfd_error_bad_value
);
8597 error_return_verref
:
8598 elf_tdata (abfd
)->verref
= NULL
;
8599 elf_tdata (abfd
)->cverrefs
= 0;
8603 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8604 if (filesize
> 0 && filesize
< hdr
->sh_size
)
8606 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8608 bfd_set_error (bfd_error_no_memory
);
8610 /* xgettext:c-format */
8611 (_("error: %pB version reference section is too large (%#" PRIx64
" bytes)"),
8612 abfd
, (uint64_t) hdr
->sh_size
);
8613 goto error_return_verref
;
8615 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8616 if (contents
== NULL
)
8617 goto error_return_verref
;
8619 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8620 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8621 goto error_return_verref
;
8623 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8625 bfd_set_error (bfd_error_file_too_big
);
8626 goto error_return_verref
;
8628 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8629 if (elf_tdata (abfd
)->verref
== NULL
)
8630 goto error_return_verref
;
8632 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8633 == sizeof (Elf_External_Vernaux
));
8634 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8635 everneed
= (Elf_External_Verneed
*) contents
;
8636 iverneed
= elf_tdata (abfd
)->verref
;
8637 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8639 Elf_External_Vernaux
*evernaux
;
8640 Elf_Internal_Vernaux
*ivernaux
;
8643 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8645 iverneed
->vn_bfd
= abfd
;
8647 iverneed
->vn_filename
=
8648 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8650 if (iverneed
->vn_filename
== NULL
)
8651 goto error_return_bad_verref
;
8653 if (iverneed
->vn_cnt
== 0)
8654 iverneed
->vn_auxptr
= NULL
;
8657 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8658 sizeof (Elf_Internal_Vernaux
), &amt
))
8660 bfd_set_error (bfd_error_file_too_big
);
8661 goto error_return_verref
;
8663 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8664 bfd_alloc (abfd
, amt
);
8665 if (iverneed
->vn_auxptr
== NULL
)
8666 goto error_return_verref
;
8669 if (iverneed
->vn_aux
8670 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8671 goto error_return_bad_verref
;
8673 evernaux
= ((Elf_External_Vernaux
*)
8674 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8675 ivernaux
= iverneed
->vn_auxptr
;
8676 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8678 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8680 ivernaux
->vna_nodename
=
8681 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8682 ivernaux
->vna_name
);
8683 if (ivernaux
->vna_nodename
== NULL
)
8684 goto error_return_bad_verref
;
8686 if (ivernaux
->vna_other
> freeidx
)
8687 freeidx
= ivernaux
->vna_other
;
8689 ivernaux
->vna_nextptr
= NULL
;
8690 if (ivernaux
->vna_next
== 0)
8692 iverneed
->vn_cnt
= j
+ 1;
8695 if (j
+ 1 < iverneed
->vn_cnt
)
8696 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8698 if (ivernaux
->vna_next
8699 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8700 goto error_return_bad_verref
;
8702 evernaux
= ((Elf_External_Vernaux
*)
8703 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8706 iverneed
->vn_nextref
= NULL
;
8707 if (iverneed
->vn_next
== 0)
8709 if (i
+ 1 < hdr
->sh_info
)
8710 iverneed
->vn_nextref
= iverneed
+ 1;
8712 if (iverneed
->vn_next
8713 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8714 goto error_return_bad_verref
;
8716 everneed
= ((Elf_External_Verneed
*)
8717 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8719 elf_tdata (abfd
)->cverrefs
= i
;
8725 if (elf_dynverdef (abfd
) != 0)
8727 Elf_Internal_Shdr
*hdr
;
8728 Elf_External_Verdef
*everdef
;
8729 Elf_Internal_Verdef
*iverdef
;
8730 Elf_Internal_Verdef
*iverdefarr
;
8731 Elf_Internal_Verdef iverdefmem
;
8733 unsigned int maxidx
;
8734 bfd_byte
*contents_end_def
, *contents_end_aux
;
8736 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8738 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8740 error_return_bad_verdef
:
8742 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8743 bfd_set_error (bfd_error_bad_value
);
8744 error_return_verdef
:
8745 elf_tdata (abfd
)->verdef
= NULL
;
8746 elf_tdata (abfd
)->cverdefs
= 0;
8750 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8751 if (contents
== NULL
)
8752 goto error_return_verdef
;
8753 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8754 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8755 goto error_return_verdef
;
8757 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8758 >= sizeof (Elf_External_Verdaux
));
8759 contents_end_def
= contents
+ hdr
->sh_size
8760 - sizeof (Elf_External_Verdef
);
8761 contents_end_aux
= contents
+ hdr
->sh_size
8762 - sizeof (Elf_External_Verdaux
);
8764 /* We know the number of entries in the section but not the maximum
8765 index. Therefore we have to run through all entries and find
8767 everdef
= (Elf_External_Verdef
*) contents
;
8769 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8771 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8773 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8774 goto error_return_bad_verdef
;
8775 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8776 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8778 if (iverdefmem
.vd_next
== 0)
8781 if (iverdefmem
.vd_next
8782 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8783 goto error_return_bad_verdef
;
8785 everdef
= ((Elf_External_Verdef
*)
8786 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8789 if (default_imported_symver
)
8791 if (freeidx
> maxidx
)
8796 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8798 bfd_set_error (bfd_error_file_too_big
);
8799 goto error_return_verdef
;
8801 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8802 if (elf_tdata (abfd
)->verdef
== NULL
)
8803 goto error_return_verdef
;
8805 elf_tdata (abfd
)->cverdefs
= maxidx
;
8807 everdef
= (Elf_External_Verdef
*) contents
;
8808 iverdefarr
= elf_tdata (abfd
)->verdef
;
8809 for (i
= 0; i
< hdr
->sh_info
; i
++)
8811 Elf_External_Verdaux
*everdaux
;
8812 Elf_Internal_Verdaux
*iverdaux
;
8815 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8817 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8818 goto error_return_bad_verdef
;
8820 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8821 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8823 iverdef
->vd_bfd
= abfd
;
8825 if (iverdef
->vd_cnt
== 0)
8826 iverdef
->vd_auxptr
= NULL
;
8829 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
8830 sizeof (Elf_Internal_Verdaux
), &amt
))
8832 bfd_set_error (bfd_error_file_too_big
);
8833 goto error_return_verdef
;
8835 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8836 bfd_alloc (abfd
, amt
);
8837 if (iverdef
->vd_auxptr
== NULL
)
8838 goto error_return_verdef
;
8842 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8843 goto error_return_bad_verdef
;
8845 everdaux
= ((Elf_External_Verdaux
*)
8846 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8847 iverdaux
= iverdef
->vd_auxptr
;
8848 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8850 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8852 iverdaux
->vda_nodename
=
8853 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8854 iverdaux
->vda_name
);
8855 if (iverdaux
->vda_nodename
== NULL
)
8856 goto error_return_bad_verdef
;
8858 iverdaux
->vda_nextptr
= NULL
;
8859 if (iverdaux
->vda_next
== 0)
8861 iverdef
->vd_cnt
= j
+ 1;
8864 if (j
+ 1 < iverdef
->vd_cnt
)
8865 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8867 if (iverdaux
->vda_next
8868 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8869 goto error_return_bad_verdef
;
8871 everdaux
= ((Elf_External_Verdaux
*)
8872 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8875 iverdef
->vd_nodename
= NULL
;
8876 if (iverdef
->vd_cnt
)
8877 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8879 iverdef
->vd_nextdef
= NULL
;
8880 if (iverdef
->vd_next
== 0)
8882 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8883 iverdef
->vd_nextdef
= iverdef
+ 1;
8885 everdef
= ((Elf_External_Verdef
*)
8886 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8892 else if (default_imported_symver
)
8899 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8901 bfd_set_error (bfd_error_file_too_big
);
8904 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8905 if (elf_tdata (abfd
)->verdef
== NULL
)
8908 elf_tdata (abfd
)->cverdefs
= freeidx
;
8911 /* Create a default version based on the soname. */
8912 if (default_imported_symver
)
8914 Elf_Internal_Verdef
*iverdef
;
8915 Elf_Internal_Verdaux
*iverdaux
;
8917 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8919 iverdef
->vd_version
= VER_DEF_CURRENT
;
8920 iverdef
->vd_flags
= 0;
8921 iverdef
->vd_ndx
= freeidx
;
8922 iverdef
->vd_cnt
= 1;
8924 iverdef
->vd_bfd
= abfd
;
8926 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8927 if (iverdef
->vd_nodename
== NULL
)
8928 goto error_return_verdef
;
8929 iverdef
->vd_nextdef
= NULL
;
8930 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8931 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8932 if (iverdef
->vd_auxptr
== NULL
)
8933 goto error_return_verdef
;
8935 iverdaux
= iverdef
->vd_auxptr
;
8936 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8942 if (contents
!= NULL
)
8948 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8950 elf_symbol_type
*newsym
;
8952 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
8955 newsym
->symbol
.the_bfd
= abfd
;
8956 return &newsym
->symbol
;
8960 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8964 bfd_symbol_info (symbol
, ret
);
8967 /* Return whether a symbol name implies a local symbol. Most targets
8968 use this function for the is_local_label_name entry point, but some
8972 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8975 /* Normal local symbols start with ``.L''. */
8976 if (name
[0] == '.' && name
[1] == 'L')
8979 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8980 DWARF debugging symbols starting with ``..''. */
8981 if (name
[0] == '.' && name
[1] == '.')
8984 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8985 emitting DWARF debugging output. I suspect this is actually a
8986 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8987 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8988 underscore to be emitted on some ELF targets). For ease of use,
8989 we treat such symbols as local. */
8990 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8993 /* Treat assembler generated fake symbols, dollar local labels and
8994 forward-backward labels (aka local labels) as locals.
8995 These labels have the form:
8997 L0^A.* (fake symbols)
8999 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9001 Versions which start with .L will have already been matched above,
9002 so we only need to match the rest. */
9003 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9005 bfd_boolean ret
= FALSE
;
9009 for (p
= name
+ 2; (c
= *p
); p
++)
9011 if (c
== 1 || c
== 2)
9013 if (c
== 1 && p
== name
+ 2)
9014 /* A fake symbol. */
9017 /* FIXME: We are being paranoid here and treating symbols like
9018 L0^Bfoo as if there were non-local, on the grounds that the
9019 assembler will never generate them. But can any symbol
9020 containing an ASCII value in the range 1-31 ever be anything
9021 other than some kind of local ? */
9038 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9039 asymbol
*symbol ATTRIBUTE_UNUSED
)
9046 _bfd_elf_set_arch_mach (bfd
*abfd
,
9047 enum bfd_architecture arch
,
9048 unsigned long machine
)
9050 /* If this isn't the right architecture for this backend, and this
9051 isn't the generic backend, fail. */
9052 if (arch
!= get_elf_backend_data (abfd
)->arch
9053 && arch
!= bfd_arch_unknown
9054 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9057 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9060 /* Find the nearest line to a particular section and offset,
9061 for error reporting. */
9064 _bfd_elf_find_nearest_line (bfd
*abfd
,
9068 const char **filename_ptr
,
9069 const char **functionname_ptr
,
9070 unsigned int *line_ptr
,
9071 unsigned int *discriminator_ptr
)
9075 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
9076 filename_ptr
, functionname_ptr
,
9077 line_ptr
, discriminator_ptr
,
9078 dwarf_debug_sections
,
9079 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9082 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9083 filename_ptr
, functionname_ptr
, line_ptr
))
9085 if (!*functionname_ptr
)
9086 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9087 *filename_ptr
? NULL
: filename_ptr
,
9092 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9093 &found
, filename_ptr
,
9094 functionname_ptr
, line_ptr
,
9095 &elf_tdata (abfd
)->line_info
))
9097 if (found
&& (*functionname_ptr
|| *line_ptr
))
9100 if (symbols
== NULL
)
9103 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9104 filename_ptr
, functionname_ptr
))
9111 /* Find the line for a symbol. */
9114 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9115 const char **filename_ptr
, unsigned int *line_ptr
)
9117 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9118 filename_ptr
, NULL
, line_ptr
, NULL
,
9119 dwarf_debug_sections
,
9120 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9123 /* After a call to bfd_find_nearest_line, successive calls to
9124 bfd_find_inliner_info can be used to get source information about
9125 each level of function inlining that terminated at the address
9126 passed to bfd_find_nearest_line. Currently this is only supported
9127 for DWARF2 with appropriate DWARF3 extensions. */
9130 _bfd_elf_find_inliner_info (bfd
*abfd
,
9131 const char **filename_ptr
,
9132 const char **functionname_ptr
,
9133 unsigned int *line_ptr
)
9136 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9137 functionname_ptr
, line_ptr
,
9138 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9143 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9145 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9146 int ret
= bed
->s
->sizeof_ehdr
;
9148 if (!bfd_link_relocatable (info
))
9150 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9152 if (phdr_size
== (bfd_size_type
) -1)
9154 struct elf_segment_map
*m
;
9157 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9158 phdr_size
+= bed
->s
->sizeof_phdr
;
9161 phdr_size
= get_program_header_size (abfd
, info
);
9164 elf_program_header_size (abfd
) = phdr_size
;
9172 _bfd_elf_set_section_contents (bfd
*abfd
,
9174 const void *location
,
9176 bfd_size_type count
)
9178 Elf_Internal_Shdr
*hdr
;
9181 if (! abfd
->output_has_begun
9182 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9188 hdr
= &elf_section_data (section
)->this_hdr
;
9189 if (hdr
->sh_offset
== (file_ptr
) -1)
9191 if (bfd_section_is_ctf (section
))
9192 /* Nothing to do with this section: the contents are generated
9196 /* We must compress this section. Write output to the buffer. */
9197 unsigned char *contents
= hdr
->contents
;
9198 if ((offset
+ count
) > hdr
->sh_size
9199 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
9200 || contents
== NULL
)
9202 memcpy (contents
+ offset
, location
, count
);
9205 pos
= hdr
->sh_offset
+ offset
;
9206 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9207 || bfd_bwrite (location
, count
, abfd
) != count
)
9214 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9215 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9216 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9222 /* Try to convert a non-ELF reloc into an ELF one. */
9225 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9227 /* Check whether we really have an ELF howto. */
9229 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9231 bfd_reloc_code_real_type code
;
9232 reloc_howto_type
*howto
;
9234 /* Alien reloc: Try to determine its type to replace it with an
9235 equivalent ELF reloc. */
9237 if (areloc
->howto
->pc_relative
)
9239 switch (areloc
->howto
->bitsize
)
9242 code
= BFD_RELOC_8_PCREL
;
9245 code
= BFD_RELOC_12_PCREL
;
9248 code
= BFD_RELOC_16_PCREL
;
9251 code
= BFD_RELOC_24_PCREL
;
9254 code
= BFD_RELOC_32_PCREL
;
9257 code
= BFD_RELOC_64_PCREL
;
9263 howto
= bfd_reloc_type_lookup (abfd
, code
);
9265 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9267 if (howto
->pcrel_offset
)
9268 areloc
->addend
+= areloc
->address
;
9270 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9275 switch (areloc
->howto
->bitsize
)
9281 code
= BFD_RELOC_14
;
9284 code
= BFD_RELOC_16
;
9287 code
= BFD_RELOC_26
;
9290 code
= BFD_RELOC_32
;
9293 code
= BFD_RELOC_64
;
9299 howto
= bfd_reloc_type_lookup (abfd
, code
);
9303 areloc
->howto
= howto
;
9311 /* xgettext:c-format */
9312 _bfd_error_handler (_("%pB: %s unsupported"),
9313 abfd
, areloc
->howto
->name
);
9314 bfd_set_error (bfd_error_sorry
);
9319 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9321 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9322 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9324 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9325 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9326 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9329 return _bfd_generic_close_and_cleanup (abfd
);
9332 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9333 in the relocation's offset. Thus we cannot allow any sort of sanity
9334 range-checking to interfere. There is nothing else to do in processing
9337 bfd_reloc_status_type
9338 _bfd_elf_rel_vtable_reloc_fn
9339 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9340 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9341 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9342 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9344 return bfd_reloc_ok
;
9347 /* Elf core file support. Much of this only works on native
9348 toolchains, since we rely on knowing the
9349 machine-dependent procfs structure in order to pick
9350 out details about the corefile. */
9352 #ifdef HAVE_SYS_PROCFS_H
9353 /* Needed for new procfs interface on sparc-solaris. */
9354 # define _STRUCTURED_PROC 1
9355 # include <sys/procfs.h>
9358 /* Return a PID that identifies a "thread" for threaded cores, or the
9359 PID of the main process for non-threaded cores. */
9362 elfcore_make_pid (bfd
*abfd
)
9366 pid
= elf_tdata (abfd
)->core
->lwpid
;
9368 pid
= elf_tdata (abfd
)->core
->pid
;
9373 /* If there isn't a section called NAME, make one, using
9374 data from SECT. Note, this function will generate a
9375 reference to NAME, so you shouldn't deallocate or
9379 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9383 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9386 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9390 sect2
->size
= sect
->size
;
9391 sect2
->filepos
= sect
->filepos
;
9392 sect2
->alignment_power
= sect
->alignment_power
;
9396 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9397 actually creates up to two pseudosections:
9398 - For the single-threaded case, a section named NAME, unless
9399 such a section already exists.
9400 - For the multi-threaded case, a section named "NAME/PID", where
9401 PID is elfcore_make_pid (abfd).
9402 Both pseudosections have identical contents. */
9404 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9410 char *threaded_name
;
9414 /* Build the section name. */
9416 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9417 len
= strlen (buf
) + 1;
9418 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9419 if (threaded_name
== NULL
)
9421 memcpy (threaded_name
, buf
, len
);
9423 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9428 sect
->filepos
= filepos
;
9429 sect
->alignment_power
= 2;
9431 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9435 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9438 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9444 sect
->size
= note
->descsz
- offs
;
9445 sect
->filepos
= note
->descpos
+ offs
;
9446 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9451 /* prstatus_t exists on:
9453 linux 2.[01] + glibc
9457 #if defined (HAVE_PRSTATUS_T)
9460 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9465 if (note
->descsz
== sizeof (prstatus_t
))
9469 size
= sizeof (prstat
.pr_reg
);
9470 offset
= offsetof (prstatus_t
, pr_reg
);
9471 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9473 /* Do not overwrite the core signal if it
9474 has already been set by another thread. */
9475 if (elf_tdata (abfd
)->core
->signal
== 0)
9476 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9477 if (elf_tdata (abfd
)->core
->pid
== 0)
9478 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9480 /* pr_who exists on:
9483 pr_who doesn't exist on:
9486 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9487 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9489 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9492 #if defined (HAVE_PRSTATUS32_T)
9493 else if (note
->descsz
== sizeof (prstatus32_t
))
9495 /* 64-bit host, 32-bit corefile */
9496 prstatus32_t prstat
;
9498 size
= sizeof (prstat
.pr_reg
);
9499 offset
= offsetof (prstatus32_t
, pr_reg
);
9500 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9502 /* Do not overwrite the core signal if it
9503 has already been set by another thread. */
9504 if (elf_tdata (abfd
)->core
->signal
== 0)
9505 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9506 if (elf_tdata (abfd
)->core
->pid
== 0)
9507 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9509 /* pr_who exists on:
9512 pr_who doesn't exist on:
9515 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9516 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9518 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9521 #endif /* HAVE_PRSTATUS32_T */
9524 /* Fail - we don't know how to handle any other
9525 note size (ie. data object type). */
9529 /* Make a ".reg/999" section and a ".reg" section. */
9530 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9531 size
, note
->descpos
+ offset
);
9533 #endif /* defined (HAVE_PRSTATUS_T) */
9535 /* Create a pseudosection containing the exact contents of NOTE. */
9537 elfcore_make_note_pseudosection (bfd
*abfd
,
9539 Elf_Internal_Note
*note
)
9541 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9542 note
->descsz
, note
->descpos
);
9545 /* There isn't a consistent prfpregset_t across platforms,
9546 but it doesn't matter, because we don't have to pick this
9547 data structure apart. */
9550 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9552 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9555 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9556 type of NT_PRXFPREG. Just include the whole note's contents
9560 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9562 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9565 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9566 with a note type of NT_X86_XSTATE. Just include the whole note's
9567 contents literally. */
9570 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9572 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9576 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9578 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9582 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9584 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9588 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9590 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9594 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9596 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9600 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9602 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9606 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9608 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9612 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9614 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9618 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9620 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9624 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9626 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9630 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9632 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9636 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9638 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9642 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9644 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9648 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9650 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9654 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9656 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9660 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9662 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9666 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9668 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9672 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9674 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9678 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9680 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9684 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9686 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9690 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9692 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9696 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9698 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9702 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9704 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9708 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9710 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9714 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9716 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9720 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9722 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9726 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9728 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9732 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9734 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9738 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9740 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9744 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9746 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9750 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9752 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9756 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9758 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9762 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9764 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9768 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9770 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9774 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9776 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9779 #if defined (HAVE_PRPSINFO_T)
9780 typedef prpsinfo_t elfcore_psinfo_t
;
9781 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9782 typedef prpsinfo32_t elfcore_psinfo32_t
;
9786 #if defined (HAVE_PSINFO_T)
9787 typedef psinfo_t elfcore_psinfo_t
;
9788 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9789 typedef psinfo32_t elfcore_psinfo32_t
;
9793 /* return a malloc'ed copy of a string at START which is at
9794 most MAX bytes long, possibly without a terminating '\0'.
9795 the copy will always have a terminating '\0'. */
9798 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9801 char *end
= (char *) memchr (start
, '\0', max
);
9809 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9813 memcpy (dups
, start
, len
);
9819 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9821 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9823 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9825 elfcore_psinfo_t psinfo
;
9827 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9829 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9830 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9832 elf_tdata (abfd
)->core
->program
9833 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9834 sizeof (psinfo
.pr_fname
));
9836 elf_tdata (abfd
)->core
->command
9837 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9838 sizeof (psinfo
.pr_psargs
));
9840 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9841 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9843 /* 64-bit host, 32-bit corefile */
9844 elfcore_psinfo32_t psinfo
;
9846 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9848 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9849 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9851 elf_tdata (abfd
)->core
->program
9852 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9853 sizeof (psinfo
.pr_fname
));
9855 elf_tdata (abfd
)->core
->command
9856 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9857 sizeof (psinfo
.pr_psargs
));
9863 /* Fail - we don't know how to handle any other
9864 note size (ie. data object type). */
9868 /* Note that for some reason, a spurious space is tacked
9869 onto the end of the args in some (at least one anyway)
9870 implementations, so strip it off if it exists. */
9873 char *command
= elf_tdata (abfd
)->core
->command
;
9874 int n
= strlen (command
);
9876 if (0 < n
&& command
[n
- 1] == ' ')
9877 command
[n
- 1] = '\0';
9882 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9884 #if defined (HAVE_PSTATUS_T)
9886 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9888 if (note
->descsz
== sizeof (pstatus_t
)
9889 #if defined (HAVE_PXSTATUS_T)
9890 || note
->descsz
== sizeof (pxstatus_t
)
9896 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9898 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9900 #if defined (HAVE_PSTATUS32_T)
9901 else if (note
->descsz
== sizeof (pstatus32_t
))
9903 /* 64-bit host, 32-bit corefile */
9906 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9908 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9911 /* Could grab some more details from the "representative"
9912 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9913 NT_LWPSTATUS note, presumably. */
9917 #endif /* defined (HAVE_PSTATUS_T) */
9919 #if defined (HAVE_LWPSTATUS_T)
9921 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9923 lwpstatus_t lwpstat
;
9929 if (note
->descsz
!= sizeof (lwpstat
)
9930 #if defined (HAVE_LWPXSTATUS_T)
9931 && note
->descsz
!= sizeof (lwpxstatus_t
)
9936 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9938 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9939 /* Do not overwrite the core signal if it has already been set by
9941 if (elf_tdata (abfd
)->core
->signal
== 0)
9942 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9944 /* Make a ".reg/999" section. */
9946 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9947 len
= strlen (buf
) + 1;
9948 name
= bfd_alloc (abfd
, len
);
9951 memcpy (name
, buf
, len
);
9953 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9957 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9958 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9959 sect
->filepos
= note
->descpos
9960 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9963 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9964 sect
->size
= sizeof (lwpstat
.pr_reg
);
9965 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9968 sect
->alignment_power
= 2;
9970 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9973 /* Make a ".reg2/999" section */
9975 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9976 len
= strlen (buf
) + 1;
9977 name
= bfd_alloc (abfd
, len
);
9980 memcpy (name
, buf
, len
);
9982 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9986 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9987 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9988 sect
->filepos
= note
->descpos
9989 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9992 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9993 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9994 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9997 sect
->alignment_power
= 2;
9999 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10001 #endif /* defined (HAVE_LWPSTATUS_T) */
10004 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10011 int is_active_thread
;
10014 if (note
->descsz
< 728)
10017 if (! CONST_STRNEQ (note
->namedata
, "win32"))
10020 type
= bfd_get_32 (abfd
, note
->descdata
);
10024 case 1 /* NOTE_INFO_PROCESS */:
10025 /* FIXME: need to add ->core->command. */
10026 /* process_info.pid */
10027 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10028 /* process_info.signal */
10029 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10032 case 2 /* NOTE_INFO_THREAD */:
10033 /* Make a ".reg/999" section. */
10034 /* thread_info.tid */
10035 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
10037 len
= strlen (buf
) + 1;
10038 name
= (char *) bfd_alloc (abfd
, len
);
10042 memcpy (name
, buf
, len
);
10044 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10048 /* sizeof (thread_info.thread_context) */
10050 /* offsetof (thread_info.thread_context) */
10051 sect
->filepos
= note
->descpos
+ 12;
10052 sect
->alignment_power
= 2;
10054 /* thread_info.is_active_thread */
10055 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10057 if (is_active_thread
)
10058 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10062 case 3 /* NOTE_INFO_MODULE */:
10063 /* Make a ".module/xxxxxxxx" section. */
10064 /* module_info.base_address */
10065 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10066 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10068 len
= strlen (buf
) + 1;
10069 name
= (char *) bfd_alloc (abfd
, len
);
10073 memcpy (name
, buf
, len
);
10075 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10080 sect
->size
= note
->descsz
;
10081 sect
->filepos
= note
->descpos
;
10082 sect
->alignment_power
= 2;
10093 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10095 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10097 switch (note
->type
)
10103 if (bed
->elf_backend_grok_prstatus
)
10104 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10106 #if defined (HAVE_PRSTATUS_T)
10107 return elfcore_grok_prstatus (abfd
, note
);
10112 #if defined (HAVE_PSTATUS_T)
10114 return elfcore_grok_pstatus (abfd
, note
);
10117 #if defined (HAVE_LWPSTATUS_T)
10119 return elfcore_grok_lwpstatus (abfd
, note
);
10122 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10123 return elfcore_grok_prfpreg (abfd
, note
);
10125 case NT_WIN32PSTATUS
:
10126 return elfcore_grok_win32pstatus (abfd
, note
);
10128 case NT_PRXFPREG
: /* Linux SSE extension */
10129 if (note
->namesz
== 6
10130 && strcmp (note
->namedata
, "LINUX") == 0)
10131 return elfcore_grok_prxfpreg (abfd
, note
);
10135 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10136 if (note
->namesz
== 6
10137 && strcmp (note
->namedata
, "LINUX") == 0)
10138 return elfcore_grok_xstatereg (abfd
, note
);
10143 if (note
->namesz
== 6
10144 && strcmp (note
->namedata
, "LINUX") == 0)
10145 return elfcore_grok_ppc_vmx (abfd
, note
);
10150 if (note
->namesz
== 6
10151 && strcmp (note
->namedata
, "LINUX") == 0)
10152 return elfcore_grok_ppc_vsx (abfd
, note
);
10157 if (note
->namesz
== 6
10158 && strcmp (note
->namedata
, "LINUX") == 0)
10159 return elfcore_grok_ppc_tar (abfd
, note
);
10164 if (note
->namesz
== 6
10165 && strcmp (note
->namedata
, "LINUX") == 0)
10166 return elfcore_grok_ppc_ppr (abfd
, note
);
10171 if (note
->namesz
== 6
10172 && strcmp (note
->namedata
, "LINUX") == 0)
10173 return elfcore_grok_ppc_dscr (abfd
, note
);
10178 if (note
->namesz
== 6
10179 && strcmp (note
->namedata
, "LINUX") == 0)
10180 return elfcore_grok_ppc_ebb (abfd
, note
);
10185 if (note
->namesz
== 6
10186 && strcmp (note
->namedata
, "LINUX") == 0)
10187 return elfcore_grok_ppc_pmu (abfd
, note
);
10191 case NT_PPC_TM_CGPR
:
10192 if (note
->namesz
== 6
10193 && strcmp (note
->namedata
, "LINUX") == 0)
10194 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10198 case NT_PPC_TM_CFPR
:
10199 if (note
->namesz
== 6
10200 && strcmp (note
->namedata
, "LINUX") == 0)
10201 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10205 case NT_PPC_TM_CVMX
:
10206 if (note
->namesz
== 6
10207 && strcmp (note
->namedata
, "LINUX") == 0)
10208 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10212 case NT_PPC_TM_CVSX
:
10213 if (note
->namesz
== 6
10214 && strcmp (note
->namedata
, "LINUX") == 0)
10215 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10219 case NT_PPC_TM_SPR
:
10220 if (note
->namesz
== 6
10221 && strcmp (note
->namedata
, "LINUX") == 0)
10222 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10226 case NT_PPC_TM_CTAR
:
10227 if (note
->namesz
== 6
10228 && strcmp (note
->namedata
, "LINUX") == 0)
10229 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10233 case NT_PPC_TM_CPPR
:
10234 if (note
->namesz
== 6
10235 && strcmp (note
->namedata
, "LINUX") == 0)
10236 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10240 case NT_PPC_TM_CDSCR
:
10241 if (note
->namesz
== 6
10242 && strcmp (note
->namedata
, "LINUX") == 0)
10243 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10247 case NT_S390_HIGH_GPRS
:
10248 if (note
->namesz
== 6
10249 && strcmp (note
->namedata
, "LINUX") == 0)
10250 return elfcore_grok_s390_high_gprs (abfd
, note
);
10254 case NT_S390_TIMER
:
10255 if (note
->namesz
== 6
10256 && strcmp (note
->namedata
, "LINUX") == 0)
10257 return elfcore_grok_s390_timer (abfd
, note
);
10261 case NT_S390_TODCMP
:
10262 if (note
->namesz
== 6
10263 && strcmp (note
->namedata
, "LINUX") == 0)
10264 return elfcore_grok_s390_todcmp (abfd
, note
);
10268 case NT_S390_TODPREG
:
10269 if (note
->namesz
== 6
10270 && strcmp (note
->namedata
, "LINUX") == 0)
10271 return elfcore_grok_s390_todpreg (abfd
, note
);
10276 if (note
->namesz
== 6
10277 && strcmp (note
->namedata
, "LINUX") == 0)
10278 return elfcore_grok_s390_ctrs (abfd
, note
);
10282 case NT_S390_PREFIX
:
10283 if (note
->namesz
== 6
10284 && strcmp (note
->namedata
, "LINUX") == 0)
10285 return elfcore_grok_s390_prefix (abfd
, note
);
10289 case NT_S390_LAST_BREAK
:
10290 if (note
->namesz
== 6
10291 && strcmp (note
->namedata
, "LINUX") == 0)
10292 return elfcore_grok_s390_last_break (abfd
, note
);
10296 case NT_S390_SYSTEM_CALL
:
10297 if (note
->namesz
== 6
10298 && strcmp (note
->namedata
, "LINUX") == 0)
10299 return elfcore_grok_s390_system_call (abfd
, note
);
10304 if (note
->namesz
== 6
10305 && strcmp (note
->namedata
, "LINUX") == 0)
10306 return elfcore_grok_s390_tdb (abfd
, note
);
10310 case NT_S390_VXRS_LOW
:
10311 if (note
->namesz
== 6
10312 && strcmp (note
->namedata
, "LINUX") == 0)
10313 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10317 case NT_S390_VXRS_HIGH
:
10318 if (note
->namesz
== 6
10319 && strcmp (note
->namedata
, "LINUX") == 0)
10320 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10324 case NT_S390_GS_CB
:
10325 if (note
->namesz
== 6
10326 && strcmp (note
->namedata
, "LINUX") == 0)
10327 return elfcore_grok_s390_gs_cb (abfd
, note
);
10331 case NT_S390_GS_BC
:
10332 if (note
->namesz
== 6
10333 && strcmp (note
->namedata
, "LINUX") == 0)
10334 return elfcore_grok_s390_gs_bc (abfd
, note
);
10339 if (note
->namesz
== 6
10340 && strcmp (note
->namedata
, "LINUX") == 0)
10341 return elfcore_grok_arm_vfp (abfd
, note
);
10346 if (note
->namesz
== 6
10347 && strcmp (note
->namedata
, "LINUX") == 0)
10348 return elfcore_grok_aarch_tls (abfd
, note
);
10352 case NT_ARM_HW_BREAK
:
10353 if (note
->namesz
== 6
10354 && strcmp (note
->namedata
, "LINUX") == 0)
10355 return elfcore_grok_aarch_hw_break (abfd
, note
);
10359 case NT_ARM_HW_WATCH
:
10360 if (note
->namesz
== 6
10361 && strcmp (note
->namedata
, "LINUX") == 0)
10362 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10367 if (note
->namesz
== 6
10368 && strcmp (note
->namedata
, "LINUX") == 0)
10369 return elfcore_grok_aarch_sve (abfd
, note
);
10373 case NT_ARM_PAC_MASK
:
10374 if (note
->namesz
== 6
10375 && strcmp (note
->namedata
, "LINUX") == 0)
10376 return elfcore_grok_aarch_pauth (abfd
, note
);
10382 if (bed
->elf_backend_grok_psinfo
)
10383 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10385 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10386 return elfcore_grok_psinfo (abfd
, note
);
10392 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10395 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10399 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10406 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10408 struct bfd_build_id
* build_id
;
10410 if (note
->descsz
== 0)
10413 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10414 if (build_id
== NULL
)
10417 build_id
->size
= note
->descsz
;
10418 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10419 abfd
->build_id
= build_id
;
10425 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10427 switch (note
->type
)
10432 case NT_GNU_PROPERTY_TYPE_0
:
10433 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10435 case NT_GNU_BUILD_ID
:
10436 return elfobj_grok_gnu_build_id (abfd
, note
);
10441 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10443 struct sdt_note
*cur
=
10444 (struct sdt_note
*) bfd_alloc (abfd
,
10445 sizeof (struct sdt_note
) + note
->descsz
);
10447 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10448 cur
->size
= (bfd_size_type
) note
->descsz
;
10449 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10451 elf_tdata (abfd
)->sdt_note_head
= cur
;
10457 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10459 switch (note
->type
)
10462 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10470 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10474 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10477 if (note
->descsz
< 108)
10482 if (note
->descsz
< 120)
10490 /* Check for version 1 in pr_version. */
10491 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10496 /* Skip over pr_psinfosz. */
10497 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10501 offset
+= 4; /* Padding before pr_psinfosz. */
10505 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10506 elf_tdata (abfd
)->core
->program
10507 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10510 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10511 elf_tdata (abfd
)->core
->command
10512 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10515 /* Padding before pr_pid. */
10518 /* The pr_pid field was added in version "1a". */
10519 if (note
->descsz
< offset
+ 4)
10522 elf_tdata (abfd
)->core
->pid
10523 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10529 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10535 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10536 Also compute minimum size of this note. */
10537 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10541 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10545 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10546 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10553 if (note
->descsz
< min_size
)
10556 /* Check for version 1 in pr_version. */
10557 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10560 /* Extract size of pr_reg from pr_gregsetsz. */
10561 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10562 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10564 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10569 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10573 /* Skip over pr_osreldate. */
10576 /* Read signal from pr_cursig. */
10577 if (elf_tdata (abfd
)->core
->signal
== 0)
10578 elf_tdata (abfd
)->core
->signal
10579 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10582 /* Read TID from pr_pid. */
10583 elf_tdata (abfd
)->core
->lwpid
10584 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10587 /* Padding before pr_reg. */
10588 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10591 /* Make sure that there is enough data remaining in the note. */
10592 if ((note
->descsz
- offset
) < size
)
10595 /* Make a ".reg/999" section and a ".reg" section. */
10596 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10597 size
, note
->descpos
+ offset
);
10601 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10603 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10605 switch (note
->type
)
10608 if (bed
->elf_backend_grok_freebsd_prstatus
)
10609 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10611 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10614 return elfcore_grok_prfpreg (abfd
, note
);
10617 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10619 case NT_FREEBSD_THRMISC
:
10620 if (note
->namesz
== 8)
10621 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10625 case NT_FREEBSD_PROCSTAT_PROC
:
10626 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10629 case NT_FREEBSD_PROCSTAT_FILES
:
10630 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10633 case NT_FREEBSD_PROCSTAT_VMMAP
:
10634 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10637 case NT_FREEBSD_PROCSTAT_AUXV
:
10638 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10640 case NT_X86_XSTATE
:
10641 if (note
->namesz
== 8)
10642 return elfcore_grok_xstatereg (abfd
, note
);
10646 case NT_FREEBSD_PTLWPINFO
:
10647 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10651 return elfcore_grok_arm_vfp (abfd
, note
);
10659 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10663 cp
= strchr (note
->namedata
, '@');
10666 *lwpidp
= atoi(cp
+ 1);
10673 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10675 if (note
->descsz
<= 0x7c + 31)
10678 /* Signal number at offset 0x08. */
10679 elf_tdata (abfd
)->core
->signal
10680 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10682 /* Process ID at offset 0x50. */
10683 elf_tdata (abfd
)->core
->pid
10684 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10686 /* Command name at 0x7c (max 32 bytes, including nul). */
10687 elf_tdata (abfd
)->core
->command
10688 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10690 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10695 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10699 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10700 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10702 switch (note
->type
)
10704 case NT_NETBSDCORE_PROCINFO
:
10705 /* NetBSD-specific core "procinfo". Note that we expect to
10706 find this note before any of the others, which is fine,
10707 since the kernel writes this note out first when it
10708 creates a core file. */
10709 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10710 #ifdef NT_NETBSDCORE_AUXV
10711 case NT_NETBSDCORE_AUXV
:
10712 /* NetBSD-specific Elf Auxiliary Vector data. */
10713 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10719 /* As of March 2017 there are no other machine-independent notes
10720 defined for NetBSD core files. If the note type is less
10721 than the start of the machine-dependent note types, we don't
10724 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10728 switch (bfd_get_arch (abfd
))
10730 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10731 PT_GETFPREGS == mach+2. */
10733 case bfd_arch_alpha
:
10734 case bfd_arch_sparc
:
10735 switch (note
->type
)
10737 case NT_NETBSDCORE_FIRSTMACH
+0:
10738 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10740 case NT_NETBSDCORE_FIRSTMACH
+2:
10741 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10747 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10748 There's also old PT___GETREGS40 == mach + 1 for old reg
10749 structure which lacks GBR. */
10752 switch (note
->type
)
10754 case NT_NETBSDCORE_FIRSTMACH
+3:
10755 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10757 case NT_NETBSDCORE_FIRSTMACH
+5:
10758 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10764 /* On all other arch's, PT_GETREGS == mach+1 and
10765 PT_GETFPREGS == mach+3. */
10768 switch (note
->type
)
10770 case NT_NETBSDCORE_FIRSTMACH
+1:
10771 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10773 case NT_NETBSDCORE_FIRSTMACH
+3:
10774 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10784 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10786 if (note
->descsz
<= 0x48 + 31)
10789 /* Signal number at offset 0x08. */
10790 elf_tdata (abfd
)->core
->signal
10791 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10793 /* Process ID at offset 0x20. */
10794 elf_tdata (abfd
)->core
->pid
10795 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10797 /* Command name at 0x48 (max 32 bytes, including nul). */
10798 elf_tdata (abfd
)->core
->command
10799 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10805 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10807 if (note
->type
== NT_OPENBSD_PROCINFO
)
10808 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10810 if (note
->type
== NT_OPENBSD_REGS
)
10811 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10813 if (note
->type
== NT_OPENBSD_FPREGS
)
10814 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10816 if (note
->type
== NT_OPENBSD_XFPREGS
)
10817 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10819 if (note
->type
== NT_OPENBSD_AUXV
)
10820 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10822 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10824 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10829 sect
->size
= note
->descsz
;
10830 sect
->filepos
= note
->descpos
;
10831 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10840 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10842 void *ddata
= note
->descdata
;
10849 if (note
->descsz
< 16)
10852 /* nto_procfs_status 'pid' field is at offset 0. */
10853 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10855 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10856 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10858 /* nto_procfs_status 'flags' field is at offset 8. */
10859 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10861 /* nto_procfs_status 'what' field is at offset 14. */
10862 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10864 elf_tdata (abfd
)->core
->signal
= sig
;
10865 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10868 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10869 do not come from signals so we make sure we set the current
10870 thread just in case. */
10871 if (flags
& 0x00000080)
10872 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10874 /* Make a ".qnx_core_status/%d" section. */
10875 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10877 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10880 strcpy (name
, buf
);
10882 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10886 sect
->size
= note
->descsz
;
10887 sect
->filepos
= note
->descpos
;
10888 sect
->alignment_power
= 2;
10890 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10894 elfcore_grok_nto_regs (bfd
*abfd
,
10895 Elf_Internal_Note
*note
,
10903 /* Make a "(base)/%d" section. */
10904 sprintf (buf
, "%s/%ld", base
, tid
);
10906 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10909 strcpy (name
, buf
);
10911 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10915 sect
->size
= note
->descsz
;
10916 sect
->filepos
= note
->descpos
;
10917 sect
->alignment_power
= 2;
10919 /* This is the current thread. */
10920 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10921 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10926 #define BFD_QNT_CORE_INFO 7
10927 #define BFD_QNT_CORE_STATUS 8
10928 #define BFD_QNT_CORE_GREG 9
10929 #define BFD_QNT_CORE_FPREG 10
10932 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10934 /* Every GREG section has a STATUS section before it. Store the
10935 tid from the previous call to pass down to the next gregs
10937 static long tid
= 1;
10939 switch (note
->type
)
10941 case BFD_QNT_CORE_INFO
:
10942 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10943 case BFD_QNT_CORE_STATUS
:
10944 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10945 case BFD_QNT_CORE_GREG
:
10946 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10947 case BFD_QNT_CORE_FPREG
:
10948 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10955 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10961 /* Use note name as section name. */
10962 len
= note
->namesz
;
10963 name
= (char *) bfd_alloc (abfd
, len
);
10966 memcpy (name
, note
->namedata
, len
);
10967 name
[len
- 1] = '\0';
10969 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10973 sect
->size
= note
->descsz
;
10974 sect
->filepos
= note
->descpos
;
10975 sect
->alignment_power
= 1;
10980 /* Function: elfcore_write_note
10983 buffer to hold note, and current size of buffer
10987 size of data for note
10989 Writes note to end of buffer. ELF64 notes are written exactly as
10990 for ELF32, despite the current (as of 2006) ELF gabi specifying
10991 that they ought to have 8-byte namesz and descsz field, and have
10992 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10995 Pointer to realloc'd buffer, *BUFSIZ updated. */
10998 elfcore_write_note (bfd
*abfd
,
11006 Elf_External_Note
*xnp
;
11013 namesz
= strlen (name
) + 1;
11015 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11017 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11020 dest
= buf
+ *bufsiz
;
11021 *bufsiz
+= newspace
;
11022 xnp
= (Elf_External_Note
*) dest
;
11023 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11024 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11025 H_PUT_32 (abfd
, type
, xnp
->type
);
11029 memcpy (dest
, name
, namesz
);
11037 memcpy (dest
, input
, size
);
11047 /* gcc-8 warns (*) on all the strncpy calls in this function about
11048 possible string truncation. The "truncation" is not a bug. We
11049 have an external representation of structs with fields that are not
11050 necessarily NULL terminated and corresponding internal
11051 representation fields that are one larger so that they can always
11052 be NULL terminated.
11053 gcc versions between 4.2 and 4.6 do not allow pragma control of
11054 diagnostics inside functions, giving a hard error if you try to use
11055 the finer control available with later versions.
11056 gcc prior to 4.2 warns about diagnostic push and pop.
11057 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11058 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11059 (*) Depending on your system header files! */
11060 #if GCC_VERSION >= 8000
11061 # pragma GCC diagnostic push
11062 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11065 elfcore_write_prpsinfo (bfd
*abfd
,
11069 const char *psargs
)
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
,
11077 NT_PRPSINFO
, fname
, psargs
);
11082 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11083 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11084 if (bed
->s
->elfclass
== ELFCLASS32
)
11086 # if defined (HAVE_PSINFO32_T)
11088 int note_type
= NT_PSINFO
;
11091 int note_type
= NT_PRPSINFO
;
11094 memset (&data
, 0, sizeof (data
));
11095 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11096 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11097 return elfcore_write_note (abfd
, buf
, bufsiz
,
11098 "CORE", note_type
, &data
, sizeof (data
));
11103 # if defined (HAVE_PSINFO_T)
11105 int note_type
= NT_PSINFO
;
11108 int note_type
= NT_PRPSINFO
;
11111 memset (&data
, 0, sizeof (data
));
11112 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11113 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11114 return elfcore_write_note (abfd
, buf
, bufsiz
,
11115 "CORE", note_type
, &data
, sizeof (data
));
11117 #endif /* PSINFO_T or PRPSINFO_T */
11122 #if GCC_VERSION >= 8000
11123 # pragma GCC diagnostic pop
11127 elfcore_write_linux_prpsinfo32
11128 (bfd
*abfd
, char *buf
, int *bufsiz
,
11129 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11131 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11133 struct elf_external_linux_prpsinfo32_ugid16 data
;
11135 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11136 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11137 &data
, sizeof (data
));
11141 struct elf_external_linux_prpsinfo32_ugid32 data
;
11143 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11144 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11145 &data
, sizeof (data
));
11150 elfcore_write_linux_prpsinfo64
11151 (bfd
*abfd
, char *buf
, int *bufsiz
,
11152 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11154 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11156 struct elf_external_linux_prpsinfo64_ugid16 data
;
11158 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11159 return elfcore_write_note (abfd
, buf
, bufsiz
,
11160 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11164 struct elf_external_linux_prpsinfo64_ugid32 data
;
11166 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11167 return elfcore_write_note (abfd
, buf
, bufsiz
,
11168 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11173 elfcore_write_prstatus (bfd
*abfd
,
11180 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11182 if (bed
->elf_backend_write_core_note
!= NULL
)
11185 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11187 pid
, cursig
, gregs
);
11192 #if defined (HAVE_PRSTATUS_T)
11193 #if defined (HAVE_PRSTATUS32_T)
11194 if (bed
->s
->elfclass
== ELFCLASS32
)
11196 prstatus32_t prstat
;
11198 memset (&prstat
, 0, sizeof (prstat
));
11199 prstat
.pr_pid
= pid
;
11200 prstat
.pr_cursig
= cursig
;
11201 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11202 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11203 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11210 memset (&prstat
, 0, sizeof (prstat
));
11211 prstat
.pr_pid
= pid
;
11212 prstat
.pr_cursig
= cursig
;
11213 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11214 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11215 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11217 #endif /* HAVE_PRSTATUS_T */
11223 #if defined (HAVE_LWPSTATUS_T)
11225 elfcore_write_lwpstatus (bfd
*abfd
,
11232 lwpstatus_t lwpstat
;
11233 const char *note_name
= "CORE";
11235 memset (&lwpstat
, 0, sizeof (lwpstat
));
11236 lwpstat
.pr_lwpid
= pid
>> 16;
11237 lwpstat
.pr_cursig
= cursig
;
11238 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11239 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11240 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11241 #if !defined(gregs)
11242 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11243 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11245 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11246 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11249 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11250 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11252 #endif /* HAVE_LWPSTATUS_T */
11254 #if defined (HAVE_PSTATUS_T)
11256 elfcore_write_pstatus (bfd
*abfd
,
11260 int cursig ATTRIBUTE_UNUSED
,
11261 const void *gregs ATTRIBUTE_UNUSED
)
11263 const char *note_name
= "CORE";
11264 #if defined (HAVE_PSTATUS32_T)
11265 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11267 if (bed
->s
->elfclass
== ELFCLASS32
)
11271 memset (&pstat
, 0, sizeof (pstat
));
11272 pstat
.pr_pid
= pid
& 0xffff;
11273 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11274 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11282 memset (&pstat
, 0, sizeof (pstat
));
11283 pstat
.pr_pid
= pid
& 0xffff;
11284 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11285 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11289 #endif /* HAVE_PSTATUS_T */
11292 elfcore_write_prfpreg (bfd
*abfd
,
11295 const void *fpregs
,
11298 const char *note_name
= "CORE";
11299 return elfcore_write_note (abfd
, buf
, bufsiz
,
11300 note_name
, NT_FPREGSET
, fpregs
, size
);
11304 elfcore_write_prxfpreg (bfd
*abfd
,
11307 const void *xfpregs
,
11310 char *note_name
= "LINUX";
11311 return elfcore_write_note (abfd
, buf
, bufsiz
,
11312 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11316 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11317 const void *xfpregs
, int size
)
11320 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11321 note_name
= "FreeBSD";
11323 note_name
= "LINUX";
11324 return elfcore_write_note (abfd
, buf
, bufsiz
,
11325 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11329 elfcore_write_ppc_vmx (bfd
*abfd
,
11332 const void *ppc_vmx
,
11335 char *note_name
= "LINUX";
11336 return elfcore_write_note (abfd
, buf
, bufsiz
,
11337 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11341 elfcore_write_ppc_vsx (bfd
*abfd
,
11344 const void *ppc_vsx
,
11347 char *note_name
= "LINUX";
11348 return elfcore_write_note (abfd
, buf
, bufsiz
,
11349 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11353 elfcore_write_ppc_tar (bfd
*abfd
,
11356 const void *ppc_tar
,
11359 char *note_name
= "LINUX";
11360 return elfcore_write_note (abfd
, buf
, bufsiz
,
11361 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11365 elfcore_write_ppc_ppr (bfd
*abfd
,
11368 const void *ppc_ppr
,
11371 char *note_name
= "LINUX";
11372 return elfcore_write_note (abfd
, buf
, bufsiz
,
11373 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11377 elfcore_write_ppc_dscr (bfd
*abfd
,
11380 const void *ppc_dscr
,
11383 char *note_name
= "LINUX";
11384 return elfcore_write_note (abfd
, buf
, bufsiz
,
11385 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11389 elfcore_write_ppc_ebb (bfd
*abfd
,
11392 const void *ppc_ebb
,
11395 char *note_name
= "LINUX";
11396 return elfcore_write_note (abfd
, buf
, bufsiz
,
11397 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11401 elfcore_write_ppc_pmu (bfd
*abfd
,
11404 const void *ppc_pmu
,
11407 char *note_name
= "LINUX";
11408 return elfcore_write_note (abfd
, buf
, bufsiz
,
11409 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11413 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11416 const void *ppc_tm_cgpr
,
11419 char *note_name
= "LINUX";
11420 return elfcore_write_note (abfd
, buf
, bufsiz
,
11421 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11425 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11428 const void *ppc_tm_cfpr
,
11431 char *note_name
= "LINUX";
11432 return elfcore_write_note (abfd
, buf
, bufsiz
,
11433 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11437 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11440 const void *ppc_tm_cvmx
,
11443 char *note_name
= "LINUX";
11444 return elfcore_write_note (abfd
, buf
, bufsiz
,
11445 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11449 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11452 const void *ppc_tm_cvsx
,
11455 char *note_name
= "LINUX";
11456 return elfcore_write_note (abfd
, buf
, bufsiz
,
11457 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11461 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11464 const void *ppc_tm_spr
,
11467 char *note_name
= "LINUX";
11468 return elfcore_write_note (abfd
, buf
, bufsiz
,
11469 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11473 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11476 const void *ppc_tm_ctar
,
11479 char *note_name
= "LINUX";
11480 return elfcore_write_note (abfd
, buf
, bufsiz
,
11481 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11485 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11488 const void *ppc_tm_cppr
,
11491 char *note_name
= "LINUX";
11492 return elfcore_write_note (abfd
, buf
, bufsiz
,
11493 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11497 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11500 const void *ppc_tm_cdscr
,
11503 char *note_name
= "LINUX";
11504 return elfcore_write_note (abfd
, buf
, bufsiz
,
11505 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11509 elfcore_write_s390_high_gprs (bfd
*abfd
,
11512 const void *s390_high_gprs
,
11515 char *note_name
= "LINUX";
11516 return elfcore_write_note (abfd
, buf
, bufsiz
,
11517 note_name
, NT_S390_HIGH_GPRS
,
11518 s390_high_gprs
, size
);
11522 elfcore_write_s390_timer (bfd
*abfd
,
11525 const void *s390_timer
,
11528 char *note_name
= "LINUX";
11529 return elfcore_write_note (abfd
, buf
, bufsiz
,
11530 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11534 elfcore_write_s390_todcmp (bfd
*abfd
,
11537 const void *s390_todcmp
,
11540 char *note_name
= "LINUX";
11541 return elfcore_write_note (abfd
, buf
, bufsiz
,
11542 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11546 elfcore_write_s390_todpreg (bfd
*abfd
,
11549 const void *s390_todpreg
,
11552 char *note_name
= "LINUX";
11553 return elfcore_write_note (abfd
, buf
, bufsiz
,
11554 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11558 elfcore_write_s390_ctrs (bfd
*abfd
,
11561 const void *s390_ctrs
,
11564 char *note_name
= "LINUX";
11565 return elfcore_write_note (abfd
, buf
, bufsiz
,
11566 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11570 elfcore_write_s390_prefix (bfd
*abfd
,
11573 const void *s390_prefix
,
11576 char *note_name
= "LINUX";
11577 return elfcore_write_note (abfd
, buf
, bufsiz
,
11578 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11582 elfcore_write_s390_last_break (bfd
*abfd
,
11585 const void *s390_last_break
,
11588 char *note_name
= "LINUX";
11589 return elfcore_write_note (abfd
, buf
, bufsiz
,
11590 note_name
, NT_S390_LAST_BREAK
,
11591 s390_last_break
, size
);
11595 elfcore_write_s390_system_call (bfd
*abfd
,
11598 const void *s390_system_call
,
11601 char *note_name
= "LINUX";
11602 return elfcore_write_note (abfd
, buf
, bufsiz
,
11603 note_name
, NT_S390_SYSTEM_CALL
,
11604 s390_system_call
, size
);
11608 elfcore_write_s390_tdb (bfd
*abfd
,
11611 const void *s390_tdb
,
11614 char *note_name
= "LINUX";
11615 return elfcore_write_note (abfd
, buf
, bufsiz
,
11616 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11620 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11623 const void *s390_vxrs_low
,
11626 char *note_name
= "LINUX";
11627 return elfcore_write_note (abfd
, buf
, bufsiz
,
11628 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11632 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11635 const void *s390_vxrs_high
,
11638 char *note_name
= "LINUX";
11639 return elfcore_write_note (abfd
, buf
, bufsiz
,
11640 note_name
, NT_S390_VXRS_HIGH
,
11641 s390_vxrs_high
, size
);
11645 elfcore_write_s390_gs_cb (bfd
*abfd
,
11648 const void *s390_gs_cb
,
11651 char *note_name
= "LINUX";
11652 return elfcore_write_note (abfd
, buf
, bufsiz
,
11653 note_name
, NT_S390_GS_CB
,
11658 elfcore_write_s390_gs_bc (bfd
*abfd
,
11661 const void *s390_gs_bc
,
11664 char *note_name
= "LINUX";
11665 return elfcore_write_note (abfd
, buf
, bufsiz
,
11666 note_name
, NT_S390_GS_BC
,
11671 elfcore_write_arm_vfp (bfd
*abfd
,
11674 const void *arm_vfp
,
11677 char *note_name
= "LINUX";
11678 return elfcore_write_note (abfd
, buf
, bufsiz
,
11679 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11683 elfcore_write_aarch_tls (bfd
*abfd
,
11686 const void *aarch_tls
,
11689 char *note_name
= "LINUX";
11690 return elfcore_write_note (abfd
, buf
, bufsiz
,
11691 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11695 elfcore_write_aarch_hw_break (bfd
*abfd
,
11698 const void *aarch_hw_break
,
11701 char *note_name
= "LINUX";
11702 return elfcore_write_note (abfd
, buf
, bufsiz
,
11703 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11707 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11710 const void *aarch_hw_watch
,
11713 char *note_name
= "LINUX";
11714 return elfcore_write_note (abfd
, buf
, bufsiz
,
11715 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11719 elfcore_write_aarch_sve (bfd
*abfd
,
11722 const void *aarch_sve
,
11725 char *note_name
= "LINUX";
11726 return elfcore_write_note (abfd
, buf
, bufsiz
,
11727 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11731 elfcore_write_aarch_pauth (bfd
*abfd
,
11734 const void *aarch_pauth
,
11737 char *note_name
= "LINUX";
11738 return elfcore_write_note (abfd
, buf
, bufsiz
,
11739 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11743 elfcore_write_register_note (bfd
*abfd
,
11746 const char *section
,
11750 if (strcmp (section
, ".reg2") == 0)
11751 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11752 if (strcmp (section
, ".reg-xfp") == 0)
11753 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11754 if (strcmp (section
, ".reg-xstate") == 0)
11755 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11756 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11757 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11758 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11759 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11760 if (strcmp (section
, ".reg-ppc-tar") == 0)
11761 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11762 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11763 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11764 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11765 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11766 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11767 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11768 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11769 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11770 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11771 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11772 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11773 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11774 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11775 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11776 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11777 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11778 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11779 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11780 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11781 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11782 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11783 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11784 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11785 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11786 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11787 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11788 if (strcmp (section
, ".reg-s390-timer") == 0)
11789 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11790 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11791 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11792 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11793 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11794 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11795 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11796 if (strcmp (section
, ".reg-s390-prefix") == 0)
11797 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11798 if (strcmp (section
, ".reg-s390-last-break") == 0)
11799 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11800 if (strcmp (section
, ".reg-s390-system-call") == 0)
11801 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11802 if (strcmp (section
, ".reg-s390-tdb") == 0)
11803 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11804 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11805 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11806 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11807 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11808 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11809 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11810 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11811 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11812 if (strcmp (section
, ".reg-arm-vfp") == 0)
11813 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11814 if (strcmp (section
, ".reg-aarch-tls") == 0)
11815 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11816 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11817 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11818 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11819 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11820 if (strcmp (section
, ".reg-aarch-sve") == 0)
11821 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11822 if (strcmp (section
, ".reg-aarch-pauth") == 0)
11823 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
11828 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11833 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11834 gABI specifies that PT_NOTE alignment should be aligned to 4
11835 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11836 align is less than 4, we use 4 byte alignment. */
11839 if (align
!= 4 && align
!= 8)
11843 while (p
< buf
+ size
)
11845 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11846 Elf_Internal_Note in
;
11848 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11851 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11853 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11854 in
.namedata
= xnp
->name
;
11855 if (in
.namesz
> buf
- in
.namedata
+ size
)
11858 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11859 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11860 in
.descpos
= offset
+ (in
.descdata
- buf
);
11862 && (in
.descdata
>= buf
+ size
11863 || in
.descsz
> buf
- in
.descdata
+ size
))
11866 switch (bfd_get_format (abfd
))
11873 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11876 const char * string
;
11878 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11882 GROKER_ELEMENT ("", elfcore_grok_note
),
11883 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11884 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11885 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11886 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11887 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
11888 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
)
11890 #undef GROKER_ELEMENT
11893 for (i
= ARRAY_SIZE (grokers
); i
--;)
11895 if (in
.namesz
>= grokers
[i
].len
11896 && strncmp (in
.namedata
, grokers
[i
].string
,
11897 grokers
[i
].len
) == 0)
11899 if (! grokers
[i
].func (abfd
, & in
))
11908 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
11910 if (! elfobj_grok_gnu_note (abfd
, &in
))
11913 else if (in
.namesz
== sizeof "stapsdt"
11914 && strcmp (in
.namedata
, "stapsdt") == 0)
11916 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
11922 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
11929 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
11934 if (size
== 0 || (size
+ 1) == 0)
11937 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
11940 buf
= (char *) bfd_malloc (size
+ 1);
11944 /* PR 17512: file: ec08f814
11945 0-termintate the buffer so that string searches will not overflow. */
11948 if (bfd_bread (buf
, size
, abfd
) != size
11949 || !elf_parse_notes (abfd
, buf
, size
, offset
, align
))
11959 /* Providing external access to the ELF program header table. */
11961 /* Return an upper bound on the number of bytes required to store a
11962 copy of ABFD's program header table entries. Return -1 if an error
11963 occurs; bfd_get_error will return an appropriate code. */
11966 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
11968 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11970 bfd_set_error (bfd_error_wrong_format
);
11974 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
11977 /* Copy ABFD's program header table entries to *PHDRS. The entries
11978 will be stored as an array of Elf_Internal_Phdr structures, as
11979 defined in include/elf/internal.h. To find out how large the
11980 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11982 Return the number of program header table entries read, or -1 if an
11983 error occurs; bfd_get_error will return an appropriate code. */
11986 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
11990 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11992 bfd_set_error (bfd_error_wrong_format
);
11996 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
11997 if (num_phdrs
!= 0)
11998 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
11999 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12004 enum elf_reloc_type_class
12005 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12006 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12007 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12009 return reloc_class_normal
;
12012 /* For RELA architectures, return the relocation value for a
12013 relocation against a local symbol. */
12016 _bfd_elf_rela_local_sym (bfd
*abfd
,
12017 Elf_Internal_Sym
*sym
,
12019 Elf_Internal_Rela
*rel
)
12021 asection
*sec
= *psec
;
12022 bfd_vma relocation
;
12024 relocation
= (sec
->output_section
->vma
12025 + sec
->output_offset
12027 if ((sec
->flags
& SEC_MERGE
)
12028 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12029 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12032 _bfd_merged_section_offset (abfd
, psec
,
12033 elf_section_data (sec
)->sec_info
,
12034 sym
->st_value
+ rel
->r_addend
);
12037 /* If we have changed the section, and our original section is
12038 marked with SEC_EXCLUDE, it means that the original
12039 SEC_MERGE section has been completely subsumed in some
12040 other SEC_MERGE section. In this case, we need to leave
12041 some info around for --emit-relocs. */
12042 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12043 sec
->kept_section
= *psec
;
12046 rel
->r_addend
-= relocation
;
12047 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12053 _bfd_elf_rel_local_sym (bfd
*abfd
,
12054 Elf_Internal_Sym
*sym
,
12058 asection
*sec
= *psec
;
12060 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12061 return sym
->st_value
+ addend
;
12063 return _bfd_merged_section_offset (abfd
, psec
,
12064 elf_section_data (sec
)->sec_info
,
12065 sym
->st_value
+ addend
);
12068 /* Adjust an address within a section. Given OFFSET within SEC, return
12069 the new offset within the section, based upon changes made to the
12070 section. Returns -1 if the offset is now invalid.
12071 The offset (in abnd out) is in target sized bytes, however big a
12075 _bfd_elf_section_offset (bfd
*abfd
,
12076 struct bfd_link_info
*info
,
12080 switch (sec
->sec_info_type
)
12082 case SEC_INFO_TYPE_STABS
:
12083 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12085 case SEC_INFO_TYPE_EH_FRAME
:
12086 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12089 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12091 /* Reverse the offset. */
12092 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12093 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12095 /* address_size and sec->size are in octets. Convert
12096 to bytes before subtracting the original offset. */
12097 offset
= ((sec
->size
- address_size
)
12098 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12104 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12105 reconstruct an ELF file by reading the segments out of remote memory
12106 based on the ELF file header at EHDR_VMA and the ELF program headers it
12107 points to. If not null, *LOADBASEP is filled in with the difference
12108 between the VMAs from which the segments were read, and the VMAs the
12109 file headers (and hence BFD's idea of each section's VMA) put them at.
12111 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12112 remote memory at target address VMA into the local buffer at MYADDR; it
12113 should return zero on success or an `errno' code on failure. TEMPL must
12114 be a BFD for an ELF target with the word size and byte order found in
12115 the remote memory. */
12118 bfd_elf_bfd_from_remote_memory
12121 bfd_size_type size
,
12122 bfd_vma
*loadbasep
,
12123 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12125 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12126 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12130 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12131 long symcount ATTRIBUTE_UNUSED
,
12132 asymbol
**syms ATTRIBUTE_UNUSED
,
12137 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12140 const char *relplt_name
;
12141 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12145 Elf_Internal_Shdr
*hdr
;
12151 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12154 if (dynsymcount
<= 0)
12157 if (!bed
->plt_sym_val
)
12160 relplt_name
= bed
->relplt_name
;
12161 if (relplt_name
== NULL
)
12162 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12163 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12164 if (relplt
== NULL
)
12167 hdr
= &elf_section_data (relplt
)->this_hdr
;
12168 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12169 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12172 plt
= bfd_get_section_by_name (abfd
, ".plt");
12176 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12177 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12180 count
= relplt
->size
/ hdr
->sh_entsize
;
12181 size
= count
* sizeof (asymbol
);
12182 p
= relplt
->relocation
;
12183 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12185 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12186 if (p
->addend
!= 0)
12189 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12191 size
+= sizeof ("+0x") - 1 + 8;
12196 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12200 names
= (char *) (s
+ count
);
12201 p
= relplt
->relocation
;
12203 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12208 addr
= bed
->plt_sym_val (i
, plt
, p
);
12209 if (addr
== (bfd_vma
) -1)
12212 *s
= **p
->sym_ptr_ptr
;
12213 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12214 we are defining a symbol, ensure one of them is set. */
12215 if ((s
->flags
& BSF_LOCAL
) == 0)
12216 s
->flags
|= BSF_GLOBAL
;
12217 s
->flags
|= BSF_SYNTHETIC
;
12219 s
->value
= addr
- plt
->vma
;
12222 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12223 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12225 if (p
->addend
!= 0)
12229 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12230 names
+= sizeof ("+0x") - 1;
12231 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12232 for (a
= buf
; *a
== '0'; ++a
)
12235 memcpy (names
, a
, len
);
12238 memcpy (names
, "@plt", sizeof ("@plt"));
12239 names
+= sizeof ("@plt");
12246 /* It is only used by x86-64 so far.
12247 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12248 but current usage would allow all of _bfd_std_section to be zero. */
12249 static const asymbol lcomm_sym
12250 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12251 asection _bfd_elf_large_com_section
12252 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12253 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12256 _bfd_elf_final_write_processing (bfd
*abfd
)
12258 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12260 i_ehdrp
= elf_elfheader (abfd
);
12262 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12263 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12265 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12266 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12267 STB_GNU_UNIQUE binding. */
12268 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12270 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12271 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12272 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12273 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12275 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12276 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12277 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12278 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12279 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12280 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12281 bfd_set_error (bfd_error_sorry
);
12289 /* Return TRUE for ELF symbol types that represent functions.
12290 This is the default version of this function, which is sufficient for
12291 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12294 _bfd_elf_is_function_type (unsigned int type
)
12296 return (type
== STT_FUNC
12297 || type
== STT_GNU_IFUNC
);
12300 /* If the ELF symbol SYM might be a function in SEC, return the
12301 function size and set *CODE_OFF to the function's entry point,
12302 otherwise return zero. */
12305 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12308 bfd_size_type size
;
12310 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12311 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12312 || sym
->section
!= sec
)
12315 *code_off
= sym
->value
;
12317 if (!(sym
->flags
& BSF_SYNTHETIC
))
12318 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;