1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2018 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. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-core.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
53 static bfd_boolean
prep_headers (bfd
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
,
57 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
58 file_ptr offset
, size_t align
);
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
67 _bfd_elf_swap_verdef_in (bfd
*abfd
,
68 const Elf_External_Verdef
*src
,
69 Elf_Internal_Verdef
*dst
)
71 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
72 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
73 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
74 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
75 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
76 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
77 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
80 /* Swap out a Verdef structure. */
83 _bfd_elf_swap_verdef_out (bfd
*abfd
,
84 const Elf_Internal_Verdef
*src
,
85 Elf_External_Verdef
*dst
)
87 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
88 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
89 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
90 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
91 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
92 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
93 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
96 /* Swap in a Verdaux structure. */
99 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
100 const Elf_External_Verdaux
*src
,
101 Elf_Internal_Verdaux
*dst
)
103 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
104 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
107 /* Swap out a Verdaux structure. */
110 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
111 const Elf_Internal_Verdaux
*src
,
112 Elf_External_Verdaux
*dst
)
114 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
115 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
118 /* Swap in a Verneed structure. */
121 _bfd_elf_swap_verneed_in (bfd
*abfd
,
122 const Elf_External_Verneed
*src
,
123 Elf_Internal_Verneed
*dst
)
125 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
126 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
127 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
128 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
129 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
132 /* Swap out a Verneed structure. */
135 _bfd_elf_swap_verneed_out (bfd
*abfd
,
136 const Elf_Internal_Verneed
*src
,
137 Elf_External_Verneed
*dst
)
139 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
140 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
141 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
142 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
143 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
146 /* Swap in a Vernaux structure. */
149 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
150 const Elf_External_Vernaux
*src
,
151 Elf_Internal_Vernaux
*dst
)
153 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
154 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
155 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
156 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
157 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
160 /* Swap out a Vernaux structure. */
163 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
164 const Elf_Internal_Vernaux
*src
,
165 Elf_External_Vernaux
*dst
)
167 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
168 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
169 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
170 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
171 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
174 /* Swap in a Versym structure. */
177 _bfd_elf_swap_versym_in (bfd
*abfd
,
178 const Elf_External_Versym
*src
,
179 Elf_Internal_Versym
*dst
)
181 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
184 /* Swap out a Versym structure. */
187 _bfd_elf_swap_versym_out (bfd
*abfd
,
188 const Elf_Internal_Versym
*src
,
189 Elf_External_Versym
*dst
)
191 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
194 /* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
198 bfd_elf_hash (const char *namearg
)
200 const unsigned char *name
= (const unsigned char *) namearg
;
205 while ((ch
= *name
++) != '\0')
208 if ((g
= (h
& 0xf0000000)) != 0)
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
216 return h
& 0xffffffff;
219 /* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
223 bfd_elf_gnu_hash (const char *namearg
)
225 const unsigned char *name
= (const unsigned char *) namearg
;
226 unsigned long h
= 5381;
229 while ((ch
= *name
++) != '\0')
230 h
= (h
<< 5) + h
+ ch
;
231 return h
& 0xffffffff;
234 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
237 bfd_elf_allocate_object (bfd
*abfd
,
239 enum elf_target_id object_id
)
241 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
242 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
243 if (abfd
->tdata
.any
== NULL
)
246 elf_object_id (abfd
) = object_id
;
247 if (abfd
->direction
!= read_direction
)
249 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
252 elf_tdata (abfd
)->o
= o
;
253 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
260 bfd_elf_make_object (bfd
*abfd
)
262 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
263 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
268 bfd_elf_mkcorefile (bfd
*abfd
)
270 /* I think this can be done just like an object file. */
271 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
273 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
274 return elf_tdata (abfd
)->core
!= NULL
;
278 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
280 Elf_Internal_Shdr
**i_shdrp
;
281 bfd_byte
*shstrtab
= NULL
;
283 bfd_size_type shstrtabsize
;
285 i_shdrp
= elf_elfsections (abfd
);
287 || shindex
>= elf_numsections (abfd
)
288 || i_shdrp
[shindex
] == 0)
291 shstrtab
= i_shdrp
[shindex
]->contents
;
292 if (shstrtab
== NULL
)
294 /* No cached one, attempt to read, and cache what we read. */
295 offset
= i_shdrp
[shindex
]->sh_offset
;
296 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
298 /* Allocate and clear an extra byte at the end, to prevent crashes
299 in case the string table is not terminated. */
300 if (shstrtabsize
+ 1 <= 1
301 || shstrtabsize
> bfd_get_file_size (abfd
)
302 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
303 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
305 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
307 if (bfd_get_error () != bfd_error_system_call
)
308 bfd_set_error (bfd_error_file_truncated
);
309 bfd_release (abfd
, shstrtab
);
311 /* Once we've failed to read it, make sure we don't keep
312 trying. Otherwise, we'll keep allocating space for
313 the string table over and over. */
314 i_shdrp
[shindex
]->sh_size
= 0;
317 shstrtab
[shstrtabsize
] = '\0';
318 i_shdrp
[shindex
]->contents
= shstrtab
;
320 return (char *) shstrtab
;
324 bfd_elf_string_from_elf_section (bfd
*abfd
,
325 unsigned int shindex
,
326 unsigned int strindex
)
328 Elf_Internal_Shdr
*hdr
;
333 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
336 hdr
= elf_elfsections (abfd
)[shindex
];
338 if (hdr
->contents
== NULL
)
340 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
342 /* PR 17512: file: f057ec89. */
343 /* xgettext:c-format */
344 _bfd_error_handler (_("%pB: attempt to load strings from"
345 " a non-string section (number %d)"),
350 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
354 if (strindex
>= hdr
->sh_size
)
356 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
358 /* xgettext:c-format */
359 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
360 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
361 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
363 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
367 return ((char *) hdr
->contents
) + strindex
;
370 /* Read and convert symbols to internal format.
371 SYMCOUNT specifies the number of symbols to read, starting from
372 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
373 are non-NULL, they are used to store the internal symbols, external
374 symbols, and symbol section index extensions, respectively.
375 Returns a pointer to the internal symbol buffer (malloced if necessary)
376 or NULL if there were no symbols or some kind of problem. */
379 bfd_elf_get_elf_syms (bfd
*ibfd
,
380 Elf_Internal_Shdr
*symtab_hdr
,
383 Elf_Internal_Sym
*intsym_buf
,
385 Elf_External_Sym_Shndx
*extshndx_buf
)
387 Elf_Internal_Shdr
*shndx_hdr
;
389 const bfd_byte
*esym
;
390 Elf_External_Sym_Shndx
*alloc_extshndx
;
391 Elf_External_Sym_Shndx
*shndx
;
392 Elf_Internal_Sym
*alloc_intsym
;
393 Elf_Internal_Sym
*isym
;
394 Elf_Internal_Sym
*isymend
;
395 const struct elf_backend_data
*bed
;
400 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
406 /* Normal syms might have section extension entries. */
408 if (elf_symtab_shndx_list (ibfd
) != NULL
)
410 elf_section_list
* entry
;
411 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
413 /* Find an index section that is linked to this symtab section. */
414 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
417 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
420 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
422 shndx_hdr
= & entry
->hdr
;
427 if (shndx_hdr
== NULL
)
429 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
430 /* Not really accurate, but this was how the old code used to work. */
431 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
432 /* Otherwise we do nothing. The assumption is that
433 the index table will not be needed. */
437 /* Read the symbols. */
439 alloc_extshndx
= NULL
;
441 bed
= get_elf_backend_data (ibfd
);
442 extsym_size
= bed
->s
->sizeof_sym
;
443 amt
= (bfd_size_type
) symcount
* extsym_size
;
444 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
445 if (extsym_buf
== NULL
)
447 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
448 extsym_buf
= alloc_ext
;
450 if (extsym_buf
== NULL
451 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
452 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
458 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
462 amt
= (bfd_size_type
) symcount
* sizeof (Elf_External_Sym_Shndx
);
463 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
464 if (extshndx_buf
== NULL
)
466 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
467 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
468 extshndx_buf
= alloc_extshndx
;
470 if (extshndx_buf
== NULL
471 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
472 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
479 if (intsym_buf
== NULL
)
481 alloc_intsym
= (Elf_Internal_Sym
*)
482 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
483 intsym_buf
= alloc_intsym
;
484 if (intsym_buf
== NULL
)
488 /* Convert the symbols to internal form. */
489 isymend
= intsym_buf
+ symcount
;
490 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
491 shndx
= extshndx_buf
;
493 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
494 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
496 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
497 /* xgettext:c-format */
498 _bfd_error_handler (_("%pB symbol number %lu references"
499 " nonexistent SHT_SYMTAB_SHNDX section"),
500 ibfd
, (unsigned long) symoffset
);
501 if (alloc_intsym
!= NULL
)
508 if (alloc_ext
!= NULL
)
510 if (alloc_extshndx
!= NULL
)
511 free (alloc_extshndx
);
516 /* Look up a symbol name. */
518 bfd_elf_sym_name (bfd
*abfd
,
519 Elf_Internal_Shdr
*symtab_hdr
,
520 Elf_Internal_Sym
*isym
,
524 unsigned int iname
= isym
->st_name
;
525 unsigned int shindex
= symtab_hdr
->sh_link
;
527 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
528 /* Check for a bogus st_shndx to avoid crashing. */
529 && isym
->st_shndx
< elf_numsections (abfd
))
531 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
532 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
535 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
538 else if (sym_sec
&& *name
== '\0')
539 name
= bfd_section_name (abfd
, sym_sec
);
544 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
545 sections. The first element is the flags, the rest are section
548 typedef union elf_internal_group
{
549 Elf_Internal_Shdr
*shdr
;
551 } Elf_Internal_Group
;
553 /* Return the name of the group signature symbol. Why isn't the
554 signature just a string? */
557 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
559 Elf_Internal_Shdr
*hdr
;
560 unsigned char esym
[sizeof (Elf64_External_Sym
)];
561 Elf_External_Sym_Shndx eshndx
;
562 Elf_Internal_Sym isym
;
564 /* First we need to ensure the symbol table is available. Make sure
565 that it is a symbol table section. */
566 if (ghdr
->sh_link
>= elf_numsections (abfd
))
568 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
569 if (hdr
->sh_type
!= SHT_SYMTAB
570 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
573 /* Go read the symbol. */
574 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
575 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
576 &isym
, esym
, &eshndx
) == NULL
)
579 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
582 /* Set next_in_group list pointer, and group name for NEWSECT. */
585 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
587 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
589 /* If num_group is zero, read in all SHT_GROUP sections. The count
590 is set to -1 if there are no SHT_GROUP sections. */
593 unsigned int i
, shnum
;
595 /* First count the number of groups. If we have a SHT_GROUP
596 section with just a flag word (ie. sh_size is 4), ignore it. */
597 shnum
= elf_numsections (abfd
);
600 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
601 ( (shdr)->sh_type == SHT_GROUP \
602 && (shdr)->sh_size >= minsize \
603 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
604 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
606 for (i
= 0; i
< shnum
; i
++)
608 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
610 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
616 num_group
= (unsigned) -1;
617 elf_tdata (abfd
)->num_group
= num_group
;
618 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
622 /* We keep a list of elf section headers for group sections,
623 so we can find them quickly. */
626 elf_tdata (abfd
)->num_group
= num_group
;
627 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
628 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
629 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
631 memset (elf_tdata (abfd
)->group_sect_ptr
, 0,
632 num_group
* sizeof (Elf_Internal_Shdr
*));
635 for (i
= 0; i
< shnum
; i
++)
637 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
639 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
642 Elf_Internal_Group
*dest
;
644 /* Make sure the group section has a BFD section
646 if (!bfd_section_from_shdr (abfd
, i
))
649 /* Add to list of sections. */
650 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
653 /* Read the raw contents. */
654 BFD_ASSERT (sizeof (*dest
) >= 4);
655 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
656 shdr
->contents
= (unsigned char *)
657 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
658 /* PR binutils/4110: Handle corrupt group headers. */
659 if (shdr
->contents
== NULL
)
662 /* xgettext:c-format */
663 (_("%pB: corrupt size field in group section"
664 " header: %#" PRIx64
),
665 abfd
, (uint64_t) shdr
->sh_size
);
666 bfd_set_error (bfd_error_bad_value
);
671 memset (shdr
->contents
, 0, amt
);
673 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
674 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
678 /* xgettext:c-format */
679 (_("%pB: invalid size field in group section"
680 " header: %#" PRIx64
""),
681 abfd
, (uint64_t) shdr
->sh_size
);
682 bfd_set_error (bfd_error_bad_value
);
684 /* PR 17510: If the group contents are even
685 partially corrupt, do not allow any of the
686 contents to be used. */
687 memset (shdr
->contents
, 0, amt
);
691 /* Translate raw contents, a flag word followed by an
692 array of elf section indices all in target byte order,
693 to the flag word followed by an array of elf section
695 src
= shdr
->contents
+ shdr
->sh_size
;
696 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
704 idx
= H_GET_32 (abfd
, src
);
705 if (src
== shdr
->contents
)
708 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
709 shdr
->bfd_section
->flags
710 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
715 dest
->shdr
= elf_elfsections (abfd
)[idx
];
716 /* PR binutils/23199: All sections in a
717 section group should be marked with
718 SHF_GROUP. But some tools generate
719 broken objects without SHF_GROUP. Fix
721 dest
->shdr
->sh_flags
|= SHF_GROUP
;
724 || dest
->shdr
->sh_type
== SHT_GROUP
)
727 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
735 /* PR 17510: Corrupt binaries might contain invalid groups. */
736 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
738 elf_tdata (abfd
)->num_group
= num_group
;
740 /* If all groups are invalid then fail. */
743 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
744 elf_tdata (abfd
)->num_group
= num_group
= -1;
746 (_("%pB: no valid group sections found"), abfd
);
747 bfd_set_error (bfd_error_bad_value
);
753 if (num_group
!= (unsigned) -1)
755 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
758 for (j
= 0; j
< num_group
; j
++)
760 /* Begin search from previous found group. */
761 unsigned i
= (j
+ search_offset
) % num_group
;
763 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
764 Elf_Internal_Group
*idx
;
770 idx
= (Elf_Internal_Group
*) shdr
->contents
;
771 if (idx
== NULL
|| shdr
->sh_size
< 4)
773 /* See PR 21957 for a reproducer. */
774 /* xgettext:c-format */
775 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
776 abfd
, shdr
->bfd_section
);
777 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
778 bfd_set_error (bfd_error_bad_value
);
781 n_elt
= shdr
->sh_size
/ 4;
783 /* Look through this group's sections to see if current
784 section is a member. */
786 if ((++idx
)->shdr
== hdr
)
790 /* We are a member of this group. Go looking through
791 other members to see if any others are linked via
793 idx
= (Elf_Internal_Group
*) shdr
->contents
;
794 n_elt
= shdr
->sh_size
/ 4;
796 if ((++idx
)->shdr
!= NULL
797 && (s
= idx
->shdr
->bfd_section
) != NULL
798 && elf_next_in_group (s
) != NULL
)
802 /* Snarf the group name from other member, and
803 insert current section in circular list. */
804 elf_group_name (newsect
) = elf_group_name (s
);
805 elf_next_in_group (newsect
) = elf_next_in_group (s
);
806 elf_next_in_group (s
) = newsect
;
812 gname
= group_signature (abfd
, shdr
);
815 elf_group_name (newsect
) = gname
;
817 /* Start a circular list with one element. */
818 elf_next_in_group (newsect
) = newsect
;
821 /* If the group section has been created, point to the
823 if (shdr
->bfd_section
!= NULL
)
824 elf_next_in_group (shdr
->bfd_section
) = newsect
;
826 elf_tdata (abfd
)->group_search_offset
= i
;
833 if (elf_group_name (newsect
) == NULL
)
835 /* xgettext:c-format */
836 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
844 _bfd_elf_setup_sections (bfd
*abfd
)
847 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
848 bfd_boolean result
= TRUE
;
851 /* Process SHF_LINK_ORDER. */
852 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
854 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
855 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
857 unsigned int elfsec
= this_hdr
->sh_link
;
858 /* FIXME: The old Intel compiler and old strip/objcopy may
859 not set the sh_link or sh_info fields. Hence we could
860 get the situation where elfsec is 0. */
863 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
864 if (bed
->link_order_error_handler
)
865 bed
->link_order_error_handler
866 /* xgettext:c-format */
867 (_("%pB: warning: sh_link not set for section `%pA'"),
872 asection
*linksec
= NULL
;
874 if (elfsec
< elf_numsections (abfd
))
876 this_hdr
= elf_elfsections (abfd
)[elfsec
];
877 linksec
= this_hdr
->bfd_section
;
881 Some strip/objcopy may leave an incorrect value in
882 sh_link. We don't want to proceed. */
886 /* xgettext:c-format */
887 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
888 s
->owner
, elfsec
, s
);
892 elf_linked_to_section (s
) = linksec
;
895 else if (this_hdr
->sh_type
== SHT_GROUP
896 && elf_next_in_group (s
) == NULL
)
899 /* xgettext:c-format */
900 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
901 abfd
, elf_section_data (s
)->this_idx
);
906 /* Process section groups. */
907 if (num_group
== (unsigned) -1)
910 for (i
= 0; i
< num_group
; i
++)
912 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
913 Elf_Internal_Group
*idx
;
916 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
917 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
920 /* xgettext:c-format */
921 (_("%pB: section group entry number %u is corrupt"),
927 idx
= (Elf_Internal_Group
*) shdr
->contents
;
928 n_elt
= shdr
->sh_size
/ 4;
934 if (idx
->shdr
== NULL
)
936 else if (idx
->shdr
->bfd_section
)
937 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
938 else if (idx
->shdr
->sh_type
!= SHT_RELA
939 && idx
->shdr
->sh_type
!= SHT_REL
)
941 /* There are some unknown sections in the group. */
943 /* xgettext:c-format */
944 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
947 bfd_elf_string_from_elf_section (abfd
,
948 (elf_elfheader (abfd
)
961 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
963 return elf_next_in_group (sec
) != NULL
;
967 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
969 unsigned int len
= strlen (name
);
970 char *new_name
= bfd_alloc (abfd
, len
+ 2);
971 if (new_name
== NULL
)
975 memcpy (new_name
+ 2, name
+ 1, len
);
980 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
982 unsigned int len
= strlen (name
);
983 char *new_name
= bfd_alloc (abfd
, len
);
984 if (new_name
== NULL
)
987 memcpy (new_name
+ 1, name
+ 2, len
- 1);
991 /* Make a BFD section from an ELF section. We store a pointer to the
992 BFD section in the bfd_section field of the header. */
995 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
996 Elf_Internal_Shdr
*hdr
,
1002 const struct elf_backend_data
*bed
;
1004 if (hdr
->bfd_section
!= NULL
)
1007 newsect
= bfd_make_section_anyway (abfd
, name
);
1008 if (newsect
== NULL
)
1011 hdr
->bfd_section
= newsect
;
1012 elf_section_data (newsect
)->this_hdr
= *hdr
;
1013 elf_section_data (newsect
)->this_idx
= shindex
;
1015 /* Always use the real type/flags. */
1016 elf_section_type (newsect
) = hdr
->sh_type
;
1017 elf_section_flags (newsect
) = hdr
->sh_flags
;
1019 newsect
->filepos
= hdr
->sh_offset
;
1021 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
1022 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
1023 || ! bfd_set_section_alignment (abfd
, newsect
,
1024 bfd_log2 (hdr
->sh_addralign
)))
1027 flags
= SEC_NO_FLAGS
;
1028 if (hdr
->sh_type
!= SHT_NOBITS
)
1029 flags
|= SEC_HAS_CONTENTS
;
1030 if (hdr
->sh_type
== SHT_GROUP
)
1032 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1035 if (hdr
->sh_type
!= SHT_NOBITS
)
1038 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1039 flags
|= SEC_READONLY
;
1040 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1042 else if ((flags
& SEC_LOAD
) != 0)
1044 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1047 newsect
->entsize
= hdr
->sh_entsize
;
1049 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1050 flags
|= SEC_STRINGS
;
1051 if (hdr
->sh_flags
& SHF_GROUP
)
1052 if (!setup_group (abfd
, hdr
, newsect
))
1054 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1055 flags
|= SEC_THREAD_LOCAL
;
1056 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1057 flags
|= SEC_EXCLUDE
;
1059 if ((flags
& SEC_ALLOC
) == 0)
1061 /* The debugging sections appear to be recognized only by name,
1062 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1063 if (name
[0] == '.')
1068 p
= ".debug", n
= 6;
1069 else if (name
[1] == 'g' && name
[2] == 'n')
1070 p
= ".gnu.linkonce.wi.", n
= 17;
1071 else if (name
[1] == 'g' && name
[2] == 'd')
1072 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1073 else if (name
[1] == 'l')
1075 else if (name
[1] == 's')
1077 else if (name
[1] == 'z')
1078 p
= ".zdebug", n
= 7;
1081 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1082 flags
|= SEC_DEBUGGING
;
1086 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1087 only link a single copy of the section. This is used to support
1088 g++. g++ will emit each template expansion in its own section.
1089 The symbols will be defined as weak, so that multiple definitions
1090 are permitted. The GNU linker extension is to actually discard
1091 all but one of the sections. */
1092 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1093 && elf_next_in_group (newsect
) == NULL
)
1094 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1096 bed
= get_elf_backend_data (abfd
);
1097 if (bed
->elf_backend_section_flags
)
1098 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1101 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1104 /* We do not parse the PT_NOTE segments as we are interested even in the
1105 separate debug info files which may have the segments offsets corrupted.
1106 PT_NOTEs from the core files are currently not parsed using BFD. */
1107 if (hdr
->sh_type
== SHT_NOTE
)
1111 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1114 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1115 hdr
->sh_offset
, hdr
->sh_addralign
);
1119 if ((flags
& SEC_ALLOC
) != 0)
1121 Elf_Internal_Phdr
*phdr
;
1122 unsigned int i
, nload
;
1124 /* Some ELF linkers produce binaries with all the program header
1125 p_paddr fields zero. If we have such a binary with more than
1126 one PT_LOAD header, then leave the section lma equal to vma
1127 so that we don't create sections with overlapping lma. */
1128 phdr
= elf_tdata (abfd
)->phdr
;
1129 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1130 if (phdr
->p_paddr
!= 0)
1132 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1134 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1137 phdr
= elf_tdata (abfd
)->phdr
;
1138 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1140 if (((phdr
->p_type
== PT_LOAD
1141 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1142 || phdr
->p_type
== PT_TLS
)
1143 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1145 if ((flags
& SEC_LOAD
) == 0)
1146 newsect
->lma
= (phdr
->p_paddr
1147 + hdr
->sh_addr
- phdr
->p_vaddr
);
1149 /* We used to use the same adjustment for SEC_LOAD
1150 sections, but that doesn't work if the segment
1151 is packed with code from multiple VMAs.
1152 Instead we calculate the section LMA based on
1153 the segment LMA. It is assumed that the
1154 segment will contain sections with contiguous
1155 LMAs, even if the VMAs are not. */
1156 newsect
->lma
= (phdr
->p_paddr
1157 + hdr
->sh_offset
- phdr
->p_offset
);
1159 /* With contiguous segments, we can't tell from file
1160 offsets whether a section with zero size should
1161 be placed at the end of one segment or the
1162 beginning of the next. Decide based on vaddr. */
1163 if (hdr
->sh_addr
>= phdr
->p_vaddr
1164 && (hdr
->sh_addr
+ hdr
->sh_size
1165 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1171 /* Compress/decompress DWARF debug sections with names: .debug_* and
1172 .zdebug_*, after the section flags is set. */
1173 if ((flags
& SEC_DEBUGGING
)
1174 && ((name
[1] == 'd' && name
[6] == '_')
1175 || (name
[1] == 'z' && name
[7] == '_')))
1177 enum { nothing
, compress
, decompress
} action
= nothing
;
1178 int compression_header_size
;
1179 bfd_size_type uncompressed_size
;
1180 unsigned int uncompressed_align_power
;
1181 bfd_boolean compressed
1182 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1183 &compression_header_size
,
1185 &uncompressed_align_power
);
1188 /* Compressed section. Check if we should decompress. */
1189 if ((abfd
->flags
& BFD_DECOMPRESS
))
1190 action
= decompress
;
1193 /* Compress the uncompressed section or convert from/to .zdebug*
1194 section. Check if we should compress. */
1195 if (action
== nothing
)
1197 if (newsect
->size
!= 0
1198 && (abfd
->flags
& BFD_COMPRESS
)
1199 && compression_header_size
>= 0
1200 && uncompressed_size
> 0
1202 || ((compression_header_size
> 0)
1203 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1209 if (action
== compress
)
1211 if (!bfd_init_section_compress_status (abfd
, newsect
))
1214 /* xgettext:c-format */
1215 (_("%pB: unable to initialize compress status for section %s"),
1222 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1225 /* xgettext:c-format */
1226 (_("%pB: unable to initialize decompress status for section %s"),
1232 if (abfd
->is_linker_input
)
1235 && (action
== decompress
1236 || (action
== compress
1237 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1239 /* Convert section name from .zdebug_* to .debug_* so
1240 that linker will consider this section as a debug
1242 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1243 if (new_name
== NULL
)
1245 bfd_rename_section (abfd
, newsect
, new_name
);
1249 /* For objdump, don't rename the section. For objcopy, delay
1250 section rename to elf_fake_sections. */
1251 newsect
->flags
|= SEC_ELF_RENAME
;
1257 const char *const bfd_elf_section_type_names
[] =
1259 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1260 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1261 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1264 /* ELF relocs are against symbols. If we are producing relocatable
1265 output, and the reloc is against an external symbol, and nothing
1266 has given us any additional addend, the resulting reloc will also
1267 be against the same symbol. In such a case, we don't want to
1268 change anything about the way the reloc is handled, since it will
1269 all be done at final link time. Rather than put special case code
1270 into bfd_perform_relocation, all the reloc types use this howto
1271 function. It just short circuits the reloc if producing
1272 relocatable output against an external symbol. */
1274 bfd_reloc_status_type
1275 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1276 arelent
*reloc_entry
,
1278 void *data ATTRIBUTE_UNUSED
,
1279 asection
*input_section
,
1281 char **error_message ATTRIBUTE_UNUSED
)
1283 if (output_bfd
!= NULL
1284 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1285 && (! reloc_entry
->howto
->partial_inplace
1286 || reloc_entry
->addend
== 0))
1288 reloc_entry
->address
+= input_section
->output_offset
;
1289 return bfd_reloc_ok
;
1292 return bfd_reloc_continue
;
1295 /* Returns TRUE if section A matches section B.
1296 Names, addresses and links may be different, but everything else
1297 should be the same. */
1300 section_match (const Elf_Internal_Shdr
* a
,
1301 const Elf_Internal_Shdr
* b
)
1303 if (a
->sh_type
!= b
->sh_type
1304 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1305 || a
->sh_addralign
!= b
->sh_addralign
1306 || a
->sh_entsize
!= b
->sh_entsize
)
1308 if (a
->sh_type
== SHT_SYMTAB
1309 || a
->sh_type
== SHT_STRTAB
)
1311 return a
->sh_size
== b
->sh_size
;
1314 /* Find a section in OBFD that has the same characteristics
1315 as IHEADER. Return the index of this section or SHN_UNDEF if
1316 none can be found. Check's section HINT first, as this is likely
1317 to be the correct section. */
1320 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1321 const unsigned int hint
)
1323 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1326 BFD_ASSERT (iheader
!= NULL
);
1328 /* See PR 20922 for a reproducer of the NULL test. */
1329 if (hint
< elf_numsections (obfd
)
1330 && oheaders
[hint
] != NULL
1331 && section_match (oheaders
[hint
], iheader
))
1334 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1336 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1338 if (oheader
== NULL
)
1340 if (section_match (oheader
, iheader
))
1341 /* FIXME: Do we care if there is a potential for
1342 multiple matches ? */
1349 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1350 Processor specific section, based upon a matching input section.
1351 Returns TRUE upon success, FALSE otherwise. */
1354 copy_special_section_fields (const bfd
*ibfd
,
1356 const Elf_Internal_Shdr
*iheader
,
1357 Elf_Internal_Shdr
*oheader
,
1358 const unsigned int secnum
)
1360 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1361 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1362 bfd_boolean changed
= FALSE
;
1363 unsigned int sh_link
;
1365 if (oheader
->sh_type
== SHT_NOBITS
)
1367 /* This is a feature for objcopy --only-keep-debug:
1368 When a section's type is changed to NOBITS, we preserve
1369 the sh_link and sh_info fields so that they can be
1370 matched up with the original.
1372 Note: Strictly speaking these assignments are wrong.
1373 The sh_link and sh_info fields should point to the
1374 relevent sections in the output BFD, which may not be in
1375 the same location as they were in the input BFD. But
1376 the whole point of this action is to preserve the
1377 original values of the sh_link and sh_info fields, so
1378 that they can be matched up with the section headers in
1379 the original file. So strictly speaking we may be
1380 creating an invalid ELF file, but it is only for a file
1381 that just contains debug info and only for sections
1382 without any contents. */
1383 if (oheader
->sh_link
== 0)
1384 oheader
->sh_link
= iheader
->sh_link
;
1385 if (oheader
->sh_info
== 0)
1386 oheader
->sh_info
= iheader
->sh_info
;
1390 /* Allow the target a chance to decide how these fields should be set. */
1391 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1392 && bed
->elf_backend_copy_special_section_fields
1393 (ibfd
, obfd
, iheader
, oheader
))
1396 /* We have an iheader which might match oheader, and which has non-zero
1397 sh_info and/or sh_link fields. Attempt to follow those links and find
1398 the section in the output bfd which corresponds to the linked section
1399 in the input bfd. */
1400 if (iheader
->sh_link
!= SHN_UNDEF
)
1402 /* See PR 20931 for a reproducer. */
1403 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1406 /* xgettext:c-format */
1407 (_("%pB: invalid sh_link field (%d) in section number %d"),
1408 ibfd
, iheader
->sh_link
, secnum
);
1412 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1413 if (sh_link
!= SHN_UNDEF
)
1415 oheader
->sh_link
= sh_link
;
1419 /* FIXME: Should we install iheader->sh_link
1420 if we could not find a match ? */
1422 /* xgettext:c-format */
1423 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1426 if (iheader
->sh_info
)
1428 /* The sh_info field can hold arbitrary information, but if the
1429 SHF_LINK_INFO flag is set then it should be interpreted as a
1431 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1433 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1435 if (sh_link
!= SHN_UNDEF
)
1436 oheader
->sh_flags
|= SHF_INFO_LINK
;
1439 /* No idea what it means - just copy it. */
1440 sh_link
= iheader
->sh_info
;
1442 if (sh_link
!= SHN_UNDEF
)
1444 oheader
->sh_info
= sh_link
;
1449 /* xgettext:c-format */
1450 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1456 /* Copy the program header and other data from one object module to
1460 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1462 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1463 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1464 const struct elf_backend_data
*bed
;
1467 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1468 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1471 if (!elf_flags_init (obfd
))
1473 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1474 elf_flags_init (obfd
) = TRUE
;
1477 elf_gp (obfd
) = elf_gp (ibfd
);
1479 /* Also copy the EI_OSABI field. */
1480 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1481 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1483 /* If set, copy the EI_ABIVERSION field. */
1484 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1485 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1486 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1488 /* Copy object attributes. */
1489 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1491 if (iheaders
== NULL
|| oheaders
== NULL
)
1494 bed
= get_elf_backend_data (obfd
);
1496 /* Possibly copy other fields in the section header. */
1497 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1500 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1502 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1503 because of a special case need for generating separate debug info
1504 files. See below for more details. */
1506 || (oheader
->sh_type
!= SHT_NOBITS
1507 && oheader
->sh_type
< SHT_LOOS
))
1510 /* Ignore empty sections, and sections whose
1511 fields have already been initialised. */
1512 if (oheader
->sh_size
== 0
1513 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1516 /* Scan for the matching section in the input bfd.
1517 First we try for a direct mapping between the input and output sections. */
1518 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1520 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1522 if (iheader
== NULL
)
1525 if (oheader
->bfd_section
!= NULL
1526 && iheader
->bfd_section
!= NULL
1527 && iheader
->bfd_section
->output_section
!= NULL
1528 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1530 /* We have found a connection from the input section to the
1531 output section. Attempt to copy the header fields. If
1532 this fails then do not try any further sections - there
1533 should only be a one-to-one mapping between input and output. */
1534 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1535 j
= elf_numsections (ibfd
);
1540 if (j
< elf_numsections (ibfd
))
1543 /* That failed. So try to deduce the corresponding input section.
1544 Unfortunately we cannot compare names as the output string table
1545 is empty, so instead we check size, address and type. */
1546 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1548 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1550 if (iheader
== NULL
)
1553 /* Try matching fields in the input section's header.
1554 Since --only-keep-debug turns all non-debug sections into
1555 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1557 if ((oheader
->sh_type
== SHT_NOBITS
1558 || iheader
->sh_type
== oheader
->sh_type
)
1559 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1560 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1561 && iheader
->sh_addralign
== oheader
->sh_addralign
1562 && iheader
->sh_entsize
== oheader
->sh_entsize
1563 && iheader
->sh_size
== oheader
->sh_size
1564 && iheader
->sh_addr
== oheader
->sh_addr
1565 && (iheader
->sh_info
!= oheader
->sh_info
1566 || iheader
->sh_link
!= oheader
->sh_link
))
1568 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1573 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1575 /* Final attempt. Call the backend copy function
1576 with a NULL input section. */
1577 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1578 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1586 get_segment_type (unsigned int p_type
)
1591 case PT_NULL
: pt
= "NULL"; break;
1592 case PT_LOAD
: pt
= "LOAD"; break;
1593 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1594 case PT_INTERP
: pt
= "INTERP"; break;
1595 case PT_NOTE
: pt
= "NOTE"; break;
1596 case PT_SHLIB
: pt
= "SHLIB"; break;
1597 case PT_PHDR
: pt
= "PHDR"; break;
1598 case PT_TLS
: pt
= "TLS"; break;
1599 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1600 case PT_GNU_STACK
: pt
= "STACK"; break;
1601 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1602 default: pt
= NULL
; break;
1607 /* Print out the program headers. */
1610 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1612 FILE *f
= (FILE *) farg
;
1613 Elf_Internal_Phdr
*p
;
1615 bfd_byte
*dynbuf
= NULL
;
1617 p
= elf_tdata (abfd
)->phdr
;
1622 fprintf (f
, _("\nProgram Header:\n"));
1623 c
= elf_elfheader (abfd
)->e_phnum
;
1624 for (i
= 0; i
< c
; i
++, p
++)
1626 const char *pt
= get_segment_type (p
->p_type
);
1631 sprintf (buf
, "0x%lx", p
->p_type
);
1634 fprintf (f
, "%8s off 0x", pt
);
1635 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1636 fprintf (f
, " vaddr 0x");
1637 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1638 fprintf (f
, " paddr 0x");
1639 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1640 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1641 fprintf (f
, " filesz 0x");
1642 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1643 fprintf (f
, " memsz 0x");
1644 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1645 fprintf (f
, " flags %c%c%c",
1646 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1647 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1648 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1649 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1650 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1655 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1658 unsigned int elfsec
;
1659 unsigned long shlink
;
1660 bfd_byte
*extdyn
, *extdynend
;
1662 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1664 fprintf (f
, _("\nDynamic Section:\n"));
1666 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1669 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1670 if (elfsec
== SHN_BAD
)
1672 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1674 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1675 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1678 /* PR 17512: file: 6f427532. */
1679 if (s
->size
< extdynsize
)
1681 extdynend
= extdyn
+ s
->size
;
1682 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1684 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1686 Elf_Internal_Dyn dyn
;
1687 const char *name
= "";
1689 bfd_boolean stringp
;
1690 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1692 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1694 if (dyn
.d_tag
== DT_NULL
)
1701 if (bed
->elf_backend_get_target_dtag
)
1702 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1704 if (!strcmp (name
, ""))
1706 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1711 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1712 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1713 case DT_PLTGOT
: name
= "PLTGOT"; break;
1714 case DT_HASH
: name
= "HASH"; break;
1715 case DT_STRTAB
: name
= "STRTAB"; break;
1716 case DT_SYMTAB
: name
= "SYMTAB"; break;
1717 case DT_RELA
: name
= "RELA"; break;
1718 case DT_RELASZ
: name
= "RELASZ"; break;
1719 case DT_RELAENT
: name
= "RELAENT"; break;
1720 case DT_STRSZ
: name
= "STRSZ"; break;
1721 case DT_SYMENT
: name
= "SYMENT"; break;
1722 case DT_INIT
: name
= "INIT"; break;
1723 case DT_FINI
: name
= "FINI"; break;
1724 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1725 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1726 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1727 case DT_REL
: name
= "REL"; break;
1728 case DT_RELSZ
: name
= "RELSZ"; break;
1729 case DT_RELENT
: name
= "RELENT"; break;
1730 case DT_PLTREL
: name
= "PLTREL"; break;
1731 case DT_DEBUG
: name
= "DEBUG"; break;
1732 case DT_TEXTREL
: name
= "TEXTREL"; break;
1733 case DT_JMPREL
: name
= "JMPREL"; break;
1734 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1735 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1736 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1737 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1738 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1739 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1740 case DT_FLAGS
: name
= "FLAGS"; break;
1741 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1742 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1743 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1744 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1745 case DT_MOVEENT
: name
= "MOVEENT"; break;
1746 case DT_MOVESZ
: name
= "MOVESZ"; break;
1747 case DT_FEATURE
: name
= "FEATURE"; break;
1748 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1749 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1750 case DT_SYMINENT
: name
= "SYMINENT"; break;
1751 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1752 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1753 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1754 case DT_PLTPAD
: name
= "PLTPAD"; break;
1755 case DT_MOVETAB
: name
= "MOVETAB"; break;
1756 case DT_SYMINFO
: name
= "SYMINFO"; break;
1757 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1758 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1759 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1760 case DT_VERSYM
: name
= "VERSYM"; break;
1761 case DT_VERDEF
: name
= "VERDEF"; break;
1762 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1763 case DT_VERNEED
: name
= "VERNEED"; break;
1764 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1765 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1766 case DT_USED
: name
= "USED"; break;
1767 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1768 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1771 fprintf (f
, " %-20s ", name
);
1775 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1780 unsigned int tagv
= dyn
.d_un
.d_val
;
1782 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1785 fprintf (f
, "%s", string
);
1794 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1795 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1797 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1801 if (elf_dynverdef (abfd
) != 0)
1803 Elf_Internal_Verdef
*t
;
1805 fprintf (f
, _("\nVersion definitions:\n"));
1806 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1808 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1809 t
->vd_flags
, t
->vd_hash
,
1810 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1811 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1813 Elf_Internal_Verdaux
*a
;
1816 for (a
= t
->vd_auxptr
->vda_nextptr
;
1820 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1826 if (elf_dynverref (abfd
) != 0)
1828 Elf_Internal_Verneed
*t
;
1830 fprintf (f
, _("\nVersion References:\n"));
1831 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1833 Elf_Internal_Vernaux
*a
;
1835 fprintf (f
, _(" required from %s:\n"),
1836 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1837 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1838 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1839 a
->vna_flags
, a
->vna_other
,
1840 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1852 /* Get version string. */
1855 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1856 bfd_boolean
*hidden
)
1858 const char *version_string
= NULL
;
1859 if (elf_dynversym (abfd
) != 0
1860 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1862 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1864 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1865 vernum
&= VERSYM_VERSION
;
1868 version_string
= "";
1869 else if (vernum
== 1
1870 && (vernum
> elf_tdata (abfd
)->cverdefs
1871 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1873 version_string
= "Base";
1874 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1876 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1879 Elf_Internal_Verneed
*t
;
1881 version_string
= _("<corrupt>");
1882 for (t
= elf_tdata (abfd
)->verref
;
1886 Elf_Internal_Vernaux
*a
;
1888 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1890 if (a
->vna_other
== vernum
)
1892 version_string
= a
->vna_nodename
;
1899 return version_string
;
1902 /* Display ELF-specific fields of a symbol. */
1905 bfd_elf_print_symbol (bfd
*abfd
,
1908 bfd_print_symbol_type how
)
1910 FILE *file
= (FILE *) filep
;
1913 case bfd_print_symbol_name
:
1914 fprintf (file
, "%s", symbol
->name
);
1916 case bfd_print_symbol_more
:
1917 fprintf (file
, "elf ");
1918 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1919 fprintf (file
, " %x", symbol
->flags
);
1921 case bfd_print_symbol_all
:
1923 const char *section_name
;
1924 const char *name
= NULL
;
1925 const struct elf_backend_data
*bed
;
1926 unsigned char st_other
;
1928 const char *version_string
;
1931 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1933 bed
= get_elf_backend_data (abfd
);
1934 if (bed
->elf_backend_print_symbol_all
)
1935 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1939 name
= symbol
->name
;
1940 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1943 fprintf (file
, " %s\t", section_name
);
1944 /* Print the "other" value for a symbol. For common symbols,
1945 we've already printed the size; now print the alignment.
1946 For other symbols, we have no specified alignment, and
1947 we've printed the address; now print the size. */
1948 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1949 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1951 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1952 bfd_fprintf_vma (abfd
, file
, val
);
1954 /* If we have version information, print it. */
1955 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1961 fprintf (file
, " %-11s", version_string
);
1966 fprintf (file
, " (%s)", version_string
);
1967 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1972 /* If the st_other field is not zero, print it. */
1973 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1978 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1979 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1980 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1982 /* Some other non-defined flags are also present, so print
1984 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1987 fprintf (file
, " %s", name
);
1993 /* ELF .o/exec file reading */
1995 /* Create a new bfd section from an ELF section header. */
1998 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2000 Elf_Internal_Shdr
*hdr
;
2001 Elf_Internal_Ehdr
*ehdr
;
2002 const struct elf_backend_data
*bed
;
2004 bfd_boolean ret
= TRUE
;
2005 static bfd_boolean
* sections_being_created
= NULL
;
2006 static bfd
* sections_being_created_abfd
= NULL
;
2007 static unsigned int nesting
= 0;
2009 if (shindex
>= elf_numsections (abfd
))
2014 /* PR17512: A corrupt ELF binary might contain a recursive group of
2015 sections, with each the string indices pointing to the next in the
2016 loop. Detect this here, by refusing to load a section that we are
2017 already in the process of loading. We only trigger this test if
2018 we have nested at least three sections deep as normal ELF binaries
2019 can expect to recurse at least once.
2021 FIXME: It would be better if this array was attached to the bfd,
2022 rather than being held in a static pointer. */
2024 if (sections_being_created_abfd
!= abfd
)
2025 sections_being_created
= NULL
;
2026 if (sections_being_created
== NULL
)
2028 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
2029 sections_being_created
= (bfd_boolean
*)
2030 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
2031 sections_being_created_abfd
= abfd
;
2033 if (sections_being_created
[shindex
])
2036 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2039 sections_being_created
[shindex
] = TRUE
;
2042 hdr
= elf_elfsections (abfd
)[shindex
];
2043 ehdr
= elf_elfheader (abfd
);
2044 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2049 bed
= get_elf_backend_data (abfd
);
2050 switch (hdr
->sh_type
)
2053 /* Inactive section. Throw it away. */
2056 case SHT_PROGBITS
: /* Normal section with contents. */
2057 case SHT_NOBITS
: /* .bss section. */
2058 case SHT_HASH
: /* .hash section. */
2059 case SHT_NOTE
: /* .note section. */
2060 case SHT_INIT_ARRAY
: /* .init_array section. */
2061 case SHT_FINI_ARRAY
: /* .fini_array section. */
2062 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2063 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2064 case SHT_GNU_HASH
: /* .gnu.hash section. */
2065 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2068 case SHT_DYNAMIC
: /* Dynamic linking information. */
2069 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2072 if (hdr
->sh_link
> elf_numsections (abfd
))
2074 /* PR 10478: Accept Solaris binaries with a sh_link
2075 field set to SHN_BEFORE or SHN_AFTER. */
2076 switch (bfd_get_arch (abfd
))
2079 case bfd_arch_sparc
:
2080 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2081 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2083 /* Otherwise fall through. */
2088 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2090 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2092 Elf_Internal_Shdr
*dynsymhdr
;
2094 /* The shared libraries distributed with hpux11 have a bogus
2095 sh_link field for the ".dynamic" section. Find the
2096 string table for the ".dynsym" section instead. */
2097 if (elf_dynsymtab (abfd
) != 0)
2099 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2100 hdr
->sh_link
= dynsymhdr
->sh_link
;
2104 unsigned int i
, num_sec
;
2106 num_sec
= elf_numsections (abfd
);
2107 for (i
= 1; i
< num_sec
; i
++)
2109 dynsymhdr
= elf_elfsections (abfd
)[i
];
2110 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2112 hdr
->sh_link
= dynsymhdr
->sh_link
;
2120 case SHT_SYMTAB
: /* A symbol table. */
2121 if (elf_onesymtab (abfd
) == shindex
)
2124 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2127 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2129 if (hdr
->sh_size
!= 0)
2131 /* Some assemblers erroneously set sh_info to one with a
2132 zero sh_size. ld sees this as a global symbol count
2133 of (unsigned) -1. Fix it here. */
2138 /* PR 18854: A binary might contain more than one symbol table.
2139 Unusual, but possible. Warn, but continue. */
2140 if (elf_onesymtab (abfd
) != 0)
2143 /* xgettext:c-format */
2144 (_("%pB: warning: multiple symbol tables detected"
2145 " - ignoring the table in section %u"),
2149 elf_onesymtab (abfd
) = shindex
;
2150 elf_symtab_hdr (abfd
) = *hdr
;
2151 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2152 abfd
->flags
|= HAS_SYMS
;
2154 /* Sometimes a shared object will map in the symbol table. If
2155 SHF_ALLOC is set, and this is a shared object, then we also
2156 treat this section as a BFD section. We can not base the
2157 decision purely on SHF_ALLOC, because that flag is sometimes
2158 set in a relocatable object file, which would confuse the
2160 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2161 && (abfd
->flags
& DYNAMIC
) != 0
2162 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2166 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2167 can't read symbols without that section loaded as well. It
2168 is most likely specified by the next section header. */
2170 elf_section_list
* entry
;
2171 unsigned int i
, num_sec
;
2173 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2174 if (entry
->hdr
.sh_link
== shindex
)
2177 num_sec
= elf_numsections (abfd
);
2178 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2180 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2182 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2183 && hdr2
->sh_link
== shindex
)
2188 for (i
= 1; i
< shindex
; i
++)
2190 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2192 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2193 && hdr2
->sh_link
== shindex
)
2198 ret
= bfd_section_from_shdr (abfd
, i
);
2199 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2203 case SHT_DYNSYM
: /* A dynamic symbol table. */
2204 if (elf_dynsymtab (abfd
) == shindex
)
2207 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2210 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2212 if (hdr
->sh_size
!= 0)
2215 /* Some linkers erroneously set sh_info to one with a
2216 zero sh_size. ld sees this as a global symbol count
2217 of (unsigned) -1. Fix it here. */
2222 /* PR 18854: A binary might contain more than one dynamic symbol table.
2223 Unusual, but possible. Warn, but continue. */
2224 if (elf_dynsymtab (abfd
) != 0)
2227 /* xgettext:c-format */
2228 (_("%pB: warning: multiple dynamic symbol tables detected"
2229 " - ignoring the table in section %u"),
2233 elf_dynsymtab (abfd
) = shindex
;
2234 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2235 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2236 abfd
->flags
|= HAS_SYMS
;
2238 /* Besides being a symbol table, we also treat this as a regular
2239 section, so that objcopy can handle it. */
2240 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2243 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2245 elf_section_list
* entry
;
2247 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2248 if (entry
->ndx
== shindex
)
2251 entry
= bfd_alloc (abfd
, sizeof * entry
);
2254 entry
->ndx
= shindex
;
2256 entry
->next
= elf_symtab_shndx_list (abfd
);
2257 elf_symtab_shndx_list (abfd
) = entry
;
2258 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2262 case SHT_STRTAB
: /* A string table. */
2263 if (hdr
->bfd_section
!= NULL
)
2266 if (ehdr
->e_shstrndx
== shindex
)
2268 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2269 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2273 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2276 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2277 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2281 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2284 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2285 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2286 elf_elfsections (abfd
)[shindex
] = hdr
;
2287 /* We also treat this as a regular section, so that objcopy
2289 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2294 /* If the string table isn't one of the above, then treat it as a
2295 regular section. We need to scan all the headers to be sure,
2296 just in case this strtab section appeared before the above. */
2297 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2299 unsigned int i
, num_sec
;
2301 num_sec
= elf_numsections (abfd
);
2302 for (i
= 1; i
< num_sec
; i
++)
2304 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2305 if (hdr2
->sh_link
== shindex
)
2307 /* Prevent endless recursion on broken objects. */
2310 if (! bfd_section_from_shdr (abfd
, i
))
2312 if (elf_onesymtab (abfd
) == i
)
2314 if (elf_dynsymtab (abfd
) == i
)
2315 goto dynsymtab_strtab
;
2319 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2324 /* *These* do a lot of work -- but build no sections! */
2326 asection
*target_sect
;
2327 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2328 unsigned int num_sec
= elf_numsections (abfd
);
2329 struct bfd_elf_section_data
*esdt
;
2332 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2333 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2336 /* Check for a bogus link to avoid crashing. */
2337 if (hdr
->sh_link
>= num_sec
)
2340 /* xgettext:c-format */
2341 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2342 abfd
, hdr
->sh_link
, name
, shindex
);
2343 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2348 /* For some incomprehensible reason Oracle distributes
2349 libraries for Solaris in which some of the objects have
2350 bogus sh_link fields. It would be nice if we could just
2351 reject them, but, unfortunately, some people need to use
2352 them. We scan through the section headers; if we find only
2353 one suitable symbol table, we clobber the sh_link to point
2354 to it. I hope this doesn't break anything.
2356 Don't do it on executable nor shared library. */
2357 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2358 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2359 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2365 for (scan
= 1; scan
< num_sec
; scan
++)
2367 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2368 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2379 hdr
->sh_link
= found
;
2382 /* Get the symbol table. */
2383 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2384 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2385 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2388 /* If this is an alloc section in an executable or shared
2389 library, or the reloc section does not use the main symbol
2390 table we don't treat it as a reloc section. BFD can't
2391 adequately represent such a section, so at least for now,
2392 we don't try. We just present it as a normal section. We
2393 also can't use it as a reloc section if it points to the
2394 null section, an invalid section, another reloc section, or
2395 its sh_link points to the null section. */
2396 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2397 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2398 || hdr
->sh_link
== SHN_UNDEF
2399 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2400 || hdr
->sh_info
== SHN_UNDEF
2401 || hdr
->sh_info
>= num_sec
2402 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2403 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2405 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2410 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2413 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2414 if (target_sect
== NULL
)
2417 esdt
= elf_section_data (target_sect
);
2418 if (hdr
->sh_type
== SHT_RELA
)
2419 p_hdr
= &esdt
->rela
.hdr
;
2421 p_hdr
= &esdt
->rel
.hdr
;
2423 /* PR 17512: file: 0b4f81b7. */
2426 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2431 elf_elfsections (abfd
)[shindex
] = hdr2
;
2432 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2433 * bed
->s
->int_rels_per_ext_rel
);
2434 target_sect
->flags
|= SEC_RELOC
;
2435 target_sect
->relocation
= NULL
;
2436 target_sect
->rel_filepos
= hdr
->sh_offset
;
2437 /* In the section to which the relocations apply, mark whether
2438 its relocations are of the REL or RELA variety. */
2439 if (hdr
->sh_size
!= 0)
2441 if (hdr
->sh_type
== SHT_RELA
)
2442 target_sect
->use_rela_p
= 1;
2444 abfd
->flags
|= HAS_RELOC
;
2448 case SHT_GNU_verdef
:
2449 elf_dynverdef (abfd
) = shindex
;
2450 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2451 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2454 case SHT_GNU_versym
:
2455 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2458 elf_dynversym (abfd
) = shindex
;
2459 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2460 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2463 case SHT_GNU_verneed
:
2464 elf_dynverref (abfd
) = shindex
;
2465 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2466 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2473 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2476 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2482 /* Possibly an attributes section. */
2483 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2484 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2486 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2488 _bfd_elf_parse_attributes (abfd
, hdr
);
2492 /* Check for any processor-specific section types. */
2493 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2496 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2498 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2499 /* FIXME: How to properly handle allocated section reserved
2500 for applications? */
2502 /* xgettext:c-format */
2503 (_("%pB: unknown type [%#x] section `%s'"),
2504 abfd
, hdr
->sh_type
, name
);
2507 /* Allow sections reserved for applications. */
2508 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2513 else if (hdr
->sh_type
>= SHT_LOPROC
2514 && hdr
->sh_type
<= SHT_HIPROC
)
2515 /* FIXME: We should handle this section. */
2517 /* xgettext:c-format */
2518 (_("%pB: unknown type [%#x] section `%s'"),
2519 abfd
, hdr
->sh_type
, name
);
2520 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2522 /* Unrecognised OS-specific sections. */
2523 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2524 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2525 required to correctly process the section and the file should
2526 be rejected with an error message. */
2528 /* xgettext:c-format */
2529 (_("%pB: unknown type [%#x] section `%s'"),
2530 abfd
, hdr
->sh_type
, name
);
2533 /* Otherwise it should be processed. */
2534 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2539 /* FIXME: We should handle this section. */
2541 /* xgettext:c-format */
2542 (_("%pB: unknown type [%#x] section `%s'"),
2543 abfd
, hdr
->sh_type
, name
);
2551 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2552 sections_being_created
[shindex
] = FALSE
;
2553 if (-- nesting
== 0)
2555 sections_being_created
= NULL
;
2556 sections_being_created_abfd
= abfd
;
2561 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2564 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2566 unsigned long r_symndx
)
2568 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2570 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2572 Elf_Internal_Shdr
*symtab_hdr
;
2573 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2574 Elf_External_Sym_Shndx eshndx
;
2576 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2577 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2578 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2581 if (cache
->abfd
!= abfd
)
2583 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2586 cache
->indx
[ent
] = r_symndx
;
2589 return &cache
->sym
[ent
];
2592 /* Given an ELF section number, retrieve the corresponding BFD
2596 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2598 if (sec_index
>= elf_numsections (abfd
))
2600 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2603 static const struct bfd_elf_special_section special_sections_b
[] =
2605 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2606 { NULL
, 0, 0, 0, 0 }
2609 static const struct bfd_elf_special_section special_sections_c
[] =
2611 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2612 { NULL
, 0, 0, 0, 0 }
2615 static const struct bfd_elf_special_section special_sections_d
[] =
2617 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2618 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2619 /* There are more DWARF sections than these, but they needn't be added here
2620 unless you have to cope with broken compilers that don't emit section
2621 attributes or you want to help the user writing assembler. */
2622 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2623 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2624 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2625 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2626 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2627 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2628 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2629 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2630 { NULL
, 0, 0, 0, 0 }
2633 static const struct bfd_elf_special_section special_sections_f
[] =
2635 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2636 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2637 { NULL
, 0 , 0, 0, 0 }
2640 static const struct bfd_elf_special_section special_sections_g
[] =
2642 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2643 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2644 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2645 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2646 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2647 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2648 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2649 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2650 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2651 { NULL
, 0, 0, 0, 0 }
2654 static const struct bfd_elf_special_section special_sections_h
[] =
2656 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2657 { NULL
, 0, 0, 0, 0 }
2660 static const struct bfd_elf_special_section special_sections_i
[] =
2662 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2663 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2664 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2665 { NULL
, 0, 0, 0, 0 }
2668 static const struct bfd_elf_special_section special_sections_l
[] =
2670 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2671 { NULL
, 0, 0, 0, 0 }
2674 static const struct bfd_elf_special_section special_sections_n
[] =
2676 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2677 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2678 { NULL
, 0, 0, 0, 0 }
2681 static const struct bfd_elf_special_section special_sections_p
[] =
2683 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2684 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2685 { NULL
, 0, 0, 0, 0 }
2688 static const struct bfd_elf_special_section special_sections_r
[] =
2690 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2691 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2692 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2693 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2694 { NULL
, 0, 0, 0, 0 }
2697 static const struct bfd_elf_special_section special_sections_s
[] =
2699 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2700 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2701 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2702 /* See struct bfd_elf_special_section declaration for the semantics of
2703 this special case where .prefix_length != strlen (.prefix). */
2704 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2705 { NULL
, 0, 0, 0, 0 }
2708 static const struct bfd_elf_special_section special_sections_t
[] =
2710 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2711 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2712 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2713 { NULL
, 0, 0, 0, 0 }
2716 static const struct bfd_elf_special_section special_sections_z
[] =
2718 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2719 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2720 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2721 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2722 { NULL
, 0, 0, 0, 0 }
2725 static const struct bfd_elf_special_section
* const special_sections
[] =
2727 special_sections_b
, /* 'b' */
2728 special_sections_c
, /* 'c' */
2729 special_sections_d
, /* 'd' */
2731 special_sections_f
, /* 'f' */
2732 special_sections_g
, /* 'g' */
2733 special_sections_h
, /* 'h' */
2734 special_sections_i
, /* 'i' */
2737 special_sections_l
, /* 'l' */
2739 special_sections_n
, /* 'n' */
2741 special_sections_p
, /* 'p' */
2743 special_sections_r
, /* 'r' */
2744 special_sections_s
, /* 's' */
2745 special_sections_t
, /* 't' */
2751 special_sections_z
/* 'z' */
2754 const struct bfd_elf_special_section
*
2755 _bfd_elf_get_special_section (const char *name
,
2756 const struct bfd_elf_special_section
*spec
,
2762 len
= strlen (name
);
2764 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2767 int prefix_len
= spec
[i
].prefix_length
;
2769 if (len
< prefix_len
)
2771 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2774 suffix_len
= spec
[i
].suffix_length
;
2775 if (suffix_len
<= 0)
2777 if (name
[prefix_len
] != 0)
2779 if (suffix_len
== 0)
2781 if (name
[prefix_len
] != '.'
2782 && (suffix_len
== -2
2783 || (rela
&& spec
[i
].type
== SHT_REL
)))
2789 if (len
< prefix_len
+ suffix_len
)
2791 if (memcmp (name
+ len
- suffix_len
,
2792 spec
[i
].prefix
+ prefix_len
,
2802 const struct bfd_elf_special_section
*
2803 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2806 const struct bfd_elf_special_section
*spec
;
2807 const struct elf_backend_data
*bed
;
2809 /* See if this is one of the special sections. */
2810 if (sec
->name
== NULL
)
2813 bed
= get_elf_backend_data (abfd
);
2814 spec
= bed
->special_sections
;
2817 spec
= _bfd_elf_get_special_section (sec
->name
,
2818 bed
->special_sections
,
2824 if (sec
->name
[0] != '.')
2827 i
= sec
->name
[1] - 'b';
2828 if (i
< 0 || i
> 'z' - 'b')
2831 spec
= special_sections
[i
];
2836 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2840 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2842 struct bfd_elf_section_data
*sdata
;
2843 const struct elf_backend_data
*bed
;
2844 const struct bfd_elf_special_section
*ssect
;
2846 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2849 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2853 sec
->used_by_bfd
= sdata
;
2856 /* Indicate whether or not this section should use RELA relocations. */
2857 bed
= get_elf_backend_data (abfd
);
2858 sec
->use_rela_p
= bed
->default_use_rela_p
;
2860 /* When we read a file, we don't need to set ELF section type and
2861 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2862 anyway. We will set ELF section type and flags for all linker
2863 created sections. If user specifies BFD section flags, we will
2864 set ELF section type and flags based on BFD section flags in
2865 elf_fake_sections. Special handling for .init_array/.fini_array
2866 output sections since they may contain .ctors/.dtors input
2867 sections. We don't want _bfd_elf_init_private_section_data to
2868 copy ELF section type from .ctors/.dtors input sections. */
2869 if (abfd
->direction
!= read_direction
2870 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2872 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2875 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2876 || ssect
->type
== SHT_INIT_ARRAY
2877 || ssect
->type
== SHT_FINI_ARRAY
))
2879 elf_section_type (sec
) = ssect
->type
;
2880 elf_section_flags (sec
) = ssect
->attr
;
2884 return _bfd_generic_new_section_hook (abfd
, sec
);
2887 /* Create a new bfd section from an ELF program header.
2889 Since program segments have no names, we generate a synthetic name
2890 of the form segment<NUM>, where NUM is generally the index in the
2891 program header table. For segments that are split (see below) we
2892 generate the names segment<NUM>a and segment<NUM>b.
2894 Note that some program segments may have a file size that is different than
2895 (less than) the memory size. All this means is that at execution the
2896 system must allocate the amount of memory specified by the memory size,
2897 but only initialize it with the first "file size" bytes read from the
2898 file. This would occur for example, with program segments consisting
2899 of combined data+bss.
2901 To handle the above situation, this routine generates TWO bfd sections
2902 for the single program segment. The first has the length specified by
2903 the file size of the segment, and the second has the length specified
2904 by the difference between the two sizes. In effect, the segment is split
2905 into its initialized and uninitialized parts.
2910 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2911 Elf_Internal_Phdr
*hdr
,
2913 const char *type_name
)
2921 split
= ((hdr
->p_memsz
> 0)
2922 && (hdr
->p_filesz
> 0)
2923 && (hdr
->p_memsz
> hdr
->p_filesz
));
2925 if (hdr
->p_filesz
> 0)
2927 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2928 len
= strlen (namebuf
) + 1;
2929 name
= (char *) bfd_alloc (abfd
, len
);
2932 memcpy (name
, namebuf
, len
);
2933 newsect
= bfd_make_section (abfd
, name
);
2934 if (newsect
== NULL
)
2936 newsect
->vma
= hdr
->p_vaddr
;
2937 newsect
->lma
= hdr
->p_paddr
;
2938 newsect
->size
= hdr
->p_filesz
;
2939 newsect
->filepos
= hdr
->p_offset
;
2940 newsect
->flags
|= SEC_HAS_CONTENTS
;
2941 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2942 if (hdr
->p_type
== PT_LOAD
)
2944 newsect
->flags
|= SEC_ALLOC
;
2945 newsect
->flags
|= SEC_LOAD
;
2946 if (hdr
->p_flags
& PF_X
)
2948 /* FIXME: all we known is that it has execute PERMISSION,
2950 newsect
->flags
|= SEC_CODE
;
2953 if (!(hdr
->p_flags
& PF_W
))
2955 newsect
->flags
|= SEC_READONLY
;
2959 if (hdr
->p_memsz
> hdr
->p_filesz
)
2963 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2964 len
= strlen (namebuf
) + 1;
2965 name
= (char *) bfd_alloc (abfd
, len
);
2968 memcpy (name
, namebuf
, len
);
2969 newsect
= bfd_make_section (abfd
, name
);
2970 if (newsect
== NULL
)
2972 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2973 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2974 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2975 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2976 align
= newsect
->vma
& -newsect
->vma
;
2977 if (align
== 0 || align
> hdr
->p_align
)
2978 align
= hdr
->p_align
;
2979 newsect
->alignment_power
= bfd_log2 (align
);
2980 if (hdr
->p_type
== PT_LOAD
)
2982 /* Hack for gdb. Segments that have not been modified do
2983 not have their contents written to a core file, on the
2984 assumption that a debugger can find the contents in the
2985 executable. We flag this case by setting the fake
2986 section size to zero. Note that "real" bss sections will
2987 always have their contents dumped to the core file. */
2988 if (bfd_get_format (abfd
) == bfd_core
)
2990 newsect
->flags
|= SEC_ALLOC
;
2991 if (hdr
->p_flags
& PF_X
)
2992 newsect
->flags
|= SEC_CODE
;
2994 if (!(hdr
->p_flags
& PF_W
))
2995 newsect
->flags
|= SEC_READONLY
;
3002 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3004 const struct elf_backend_data
*bed
;
3006 switch (hdr
->p_type
)
3009 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3012 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
3015 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3018 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3021 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3023 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3029 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3032 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3034 case PT_GNU_EH_FRAME
:
3035 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3039 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3042 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3045 /* Check for any processor-specific program segment types. */
3046 bed
= get_elf_backend_data (abfd
);
3047 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3051 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3055 _bfd_elf_single_rel_hdr (asection
*sec
)
3057 if (elf_section_data (sec
)->rel
.hdr
)
3059 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3060 return elf_section_data (sec
)->rel
.hdr
;
3063 return elf_section_data (sec
)->rela
.hdr
;
3067 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3068 Elf_Internal_Shdr
*rel_hdr
,
3069 const char *sec_name
,
3070 bfd_boolean use_rela_p
)
3072 char *name
= (char *) bfd_alloc (abfd
,
3073 sizeof ".rela" + strlen (sec_name
));
3077 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3079 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3081 if (rel_hdr
->sh_name
== (unsigned int) -1)
3087 /* Allocate and initialize a section-header for a new reloc section,
3088 containing relocations against ASECT. It is stored in RELDATA. If
3089 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3093 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3094 struct bfd_elf_section_reloc_data
*reldata
,
3095 const char *sec_name
,
3096 bfd_boolean use_rela_p
,
3097 bfd_boolean delay_st_name_p
)
3099 Elf_Internal_Shdr
*rel_hdr
;
3100 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3102 BFD_ASSERT (reldata
->hdr
== NULL
);
3103 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3104 reldata
->hdr
= rel_hdr
;
3106 if (delay_st_name_p
)
3107 rel_hdr
->sh_name
= (unsigned int) -1;
3108 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3111 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3112 rel_hdr
->sh_entsize
= (use_rela_p
3113 ? bed
->s
->sizeof_rela
3114 : bed
->s
->sizeof_rel
);
3115 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3116 rel_hdr
->sh_flags
= 0;
3117 rel_hdr
->sh_addr
= 0;
3118 rel_hdr
->sh_size
= 0;
3119 rel_hdr
->sh_offset
= 0;
3124 /* Return the default section type based on the passed in section flags. */
3127 bfd_elf_get_default_section_type (flagword flags
)
3129 if ((flags
& SEC_ALLOC
) != 0
3130 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3132 return SHT_PROGBITS
;
3135 struct fake_section_arg
3137 struct bfd_link_info
*link_info
;
3141 /* Set up an ELF internal section header for a section. */
3144 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3146 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3147 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3148 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3149 Elf_Internal_Shdr
*this_hdr
;
3150 unsigned int sh_type
;
3151 const char *name
= asect
->name
;
3152 bfd_boolean delay_st_name_p
= FALSE
;
3156 /* We already failed; just get out of the bfd_map_over_sections
3161 this_hdr
= &esd
->this_hdr
;
3165 /* ld: compress DWARF debug sections with names: .debug_*. */
3166 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3167 && (asect
->flags
& SEC_DEBUGGING
)
3171 /* Set SEC_ELF_COMPRESS to indicate this section should be
3173 asect
->flags
|= SEC_ELF_COMPRESS
;
3175 /* If this section will be compressed, delay adding section
3176 name to section name section after it is compressed in
3177 _bfd_elf_assign_file_positions_for_non_load. */
3178 delay_st_name_p
= TRUE
;
3181 else if ((asect
->flags
& SEC_ELF_RENAME
))
3183 /* objcopy: rename output DWARF debug section. */
3184 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3186 /* When we decompress or compress with SHF_COMPRESSED,
3187 convert section name from .zdebug_* to .debug_* if
3191 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3192 if (new_name
== NULL
)
3200 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3202 /* PR binutils/18087: Compression does not always make a
3203 section smaller. So only rename the section when
3204 compression has actually taken place. If input section
3205 name is .zdebug_*, we should never compress it again. */
3206 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3207 if (new_name
== NULL
)
3212 BFD_ASSERT (name
[1] != 'z');
3217 if (delay_st_name_p
)
3218 this_hdr
->sh_name
= (unsigned int) -1;
3222 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3224 if (this_hdr
->sh_name
== (unsigned int) -1)
3231 /* Don't clear sh_flags. Assembler may set additional bits. */
3233 if ((asect
->flags
& SEC_ALLOC
) != 0
3234 || asect
->user_set_vma
)
3235 this_hdr
->sh_addr
= asect
->vma
;
3237 this_hdr
->sh_addr
= 0;
3239 this_hdr
->sh_offset
= 0;
3240 this_hdr
->sh_size
= asect
->size
;
3241 this_hdr
->sh_link
= 0;
3242 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3243 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3246 /* xgettext:c-format */
3247 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3248 abfd
, asect
->alignment_power
, asect
);
3252 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3253 /* The sh_entsize and sh_info fields may have been set already by
3254 copy_private_section_data. */
3256 this_hdr
->bfd_section
= asect
;
3257 this_hdr
->contents
= NULL
;
3259 /* If the section type is unspecified, we set it based on
3261 if ((asect
->flags
& SEC_GROUP
) != 0)
3262 sh_type
= SHT_GROUP
;
3264 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3266 if (this_hdr
->sh_type
== SHT_NULL
)
3267 this_hdr
->sh_type
= sh_type
;
3268 else if (this_hdr
->sh_type
== SHT_NOBITS
3269 && sh_type
== SHT_PROGBITS
3270 && (asect
->flags
& SEC_ALLOC
) != 0)
3272 /* Warn if we are changing a NOBITS section to PROGBITS, but
3273 allow the link to proceed. This can happen when users link
3274 non-bss input sections to bss output sections, or emit data
3275 to a bss output section via a linker script. */
3277 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3278 this_hdr
->sh_type
= sh_type
;
3281 switch (this_hdr
->sh_type
)
3292 case SHT_INIT_ARRAY
:
3293 case SHT_FINI_ARRAY
:
3294 case SHT_PREINIT_ARRAY
:
3295 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3299 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3303 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3307 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3311 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3312 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3316 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3317 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3320 case SHT_GNU_versym
:
3321 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3324 case SHT_GNU_verdef
:
3325 this_hdr
->sh_entsize
= 0;
3326 /* objcopy or strip will copy over sh_info, but may not set
3327 cverdefs. The linker will set cverdefs, but sh_info will be
3329 if (this_hdr
->sh_info
== 0)
3330 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3332 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3333 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3336 case SHT_GNU_verneed
:
3337 this_hdr
->sh_entsize
= 0;
3338 /* objcopy or strip will copy over sh_info, but may not set
3339 cverrefs. The linker will set cverrefs, but sh_info will be
3341 if (this_hdr
->sh_info
== 0)
3342 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3344 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3345 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3349 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3353 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3357 if ((asect
->flags
& SEC_ALLOC
) != 0)
3358 this_hdr
->sh_flags
|= SHF_ALLOC
;
3359 if ((asect
->flags
& SEC_READONLY
) == 0)
3360 this_hdr
->sh_flags
|= SHF_WRITE
;
3361 if ((asect
->flags
& SEC_CODE
) != 0)
3362 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3363 if ((asect
->flags
& SEC_MERGE
) != 0)
3365 this_hdr
->sh_flags
|= SHF_MERGE
;
3366 this_hdr
->sh_entsize
= asect
->entsize
;
3368 if ((asect
->flags
& SEC_STRINGS
) != 0)
3369 this_hdr
->sh_flags
|= SHF_STRINGS
;
3370 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3371 this_hdr
->sh_flags
|= SHF_GROUP
;
3372 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3374 this_hdr
->sh_flags
|= SHF_TLS
;
3375 if (asect
->size
== 0
3376 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3378 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3380 this_hdr
->sh_size
= 0;
3383 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3384 if (this_hdr
->sh_size
!= 0)
3385 this_hdr
->sh_type
= SHT_NOBITS
;
3389 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3390 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3392 /* If the section has relocs, set up a section header for the
3393 SHT_REL[A] section. If two relocation sections are required for
3394 this section, it is up to the processor-specific back-end to
3395 create the other. */
3396 if ((asect
->flags
& SEC_RELOC
) != 0)
3398 /* When doing a relocatable link, create both REL and RELA sections if
3401 /* Do the normal setup if we wouldn't create any sections here. */
3402 && esd
->rel
.count
+ esd
->rela
.count
> 0
3403 && (bfd_link_relocatable (arg
->link_info
)
3404 || arg
->link_info
->emitrelocations
))
3406 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3407 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3408 FALSE
, delay_st_name_p
))
3413 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3414 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3415 TRUE
, delay_st_name_p
))
3421 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3423 ? &esd
->rela
: &esd
->rel
),
3433 /* Check for processor-specific section types. */
3434 sh_type
= this_hdr
->sh_type
;
3435 if (bed
->elf_backend_fake_sections
3436 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3442 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3444 /* Don't change the header type from NOBITS if we are being
3445 called for objcopy --only-keep-debug. */
3446 this_hdr
->sh_type
= sh_type
;
3450 /* Fill in the contents of a SHT_GROUP section. Called from
3451 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3452 when ELF targets use the generic linker, ld. Called for ld -r
3453 from bfd_elf_final_link. */
3456 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3458 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3459 asection
*elt
, *first
;
3463 /* Ignore linker created group section. See elfNN_ia64_object_p in
3465 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3469 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3471 unsigned long symindx
= 0;
3473 /* elf_group_id will have been set up by objcopy and the
3475 if (elf_group_id (sec
) != NULL
)
3476 symindx
= elf_group_id (sec
)->udata
.i
;
3480 /* If called from the assembler, swap_out_syms will have set up
3481 elf_section_syms. */
3482 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3483 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3485 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3487 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3489 /* The ELF backend linker sets sh_info to -2 when the group
3490 signature symbol is global, and thus the index can't be
3491 set until all local symbols are output. */
3493 struct bfd_elf_section_data
*sec_data
;
3494 unsigned long symndx
;
3495 unsigned long extsymoff
;
3496 struct elf_link_hash_entry
*h
;
3498 /* The point of this little dance to the first SHF_GROUP section
3499 then back to the SHT_GROUP section is that this gets us to
3500 the SHT_GROUP in the input object. */
3501 igroup
= elf_sec_group (elf_next_in_group (sec
));
3502 sec_data
= elf_section_data (igroup
);
3503 symndx
= sec_data
->this_hdr
.sh_info
;
3505 if (!elf_bad_symtab (igroup
->owner
))
3507 Elf_Internal_Shdr
*symtab_hdr
;
3509 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3510 extsymoff
= symtab_hdr
->sh_info
;
3512 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3513 while (h
->root
.type
== bfd_link_hash_indirect
3514 || h
->root
.type
== bfd_link_hash_warning
)
3515 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3517 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3520 /* The contents won't be allocated for "ld -r" or objcopy. */
3522 if (sec
->contents
== NULL
)
3525 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3527 /* Arrange for the section to be written out. */
3528 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3529 if (sec
->contents
== NULL
)
3536 loc
= sec
->contents
+ sec
->size
;
3538 /* Get the pointer to the first section in the group that gas
3539 squirreled away here. objcopy arranges for this to be set to the
3540 start of the input section group. */
3541 first
= elt
= elf_next_in_group (sec
);
3543 /* First element is a flag word. Rest of section is elf section
3544 indices for all the sections of the group. Write them backwards
3545 just to keep the group in the same order as given in .section
3546 directives, not that it matters. */
3553 s
= s
->output_section
;
3555 && !bfd_is_abs_section (s
))
3557 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3558 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3560 if (elf_sec
->rel
.hdr
!= NULL
3562 || (input_elf_sec
->rel
.hdr
!= NULL
3563 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3565 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3567 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3569 if (elf_sec
->rela
.hdr
!= NULL
3571 || (input_elf_sec
->rela
.hdr
!= NULL
3572 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3574 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3576 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3579 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3581 elt
= elf_next_in_group (elt
);
3587 BFD_ASSERT (loc
== sec
->contents
);
3589 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3592 /* Given NAME, the name of a relocation section stripped of its
3593 .rel/.rela prefix, return the section in ABFD to which the
3594 relocations apply. */
3597 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3599 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3600 section likely apply to .got.plt or .got section. */
3601 if (get_elf_backend_data (abfd
)->want_got_plt
3602 && strcmp (name
, ".plt") == 0)
3607 sec
= bfd_get_section_by_name (abfd
, name
);
3613 return bfd_get_section_by_name (abfd
, name
);
3616 /* Return the section to which RELOC_SEC applies. */
3619 elf_get_reloc_section (asection
*reloc_sec
)
3624 const struct elf_backend_data
*bed
;
3626 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3627 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3630 /* We look up the section the relocs apply to by name. */
3631 name
= reloc_sec
->name
;
3632 if (strncmp (name
, ".rel", 4) != 0)
3635 if (type
== SHT_RELA
&& *name
++ != 'a')
3638 abfd
= reloc_sec
->owner
;
3639 bed
= get_elf_backend_data (abfd
);
3640 return bed
->get_reloc_section (abfd
, name
);
3643 /* Assign all ELF section numbers. The dummy first section is handled here
3644 too. The link/info pointers for the standard section types are filled
3645 in here too, while we're at it. */
3648 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3650 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3652 unsigned int section_number
;
3653 Elf_Internal_Shdr
**i_shdrp
;
3654 struct bfd_elf_section_data
*d
;
3655 bfd_boolean need_symtab
;
3659 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3661 /* SHT_GROUP sections are in relocatable files only. */
3662 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3664 size_t reloc_count
= 0;
3666 /* Put SHT_GROUP sections first. */
3667 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3669 d
= elf_section_data (sec
);
3671 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3673 if (sec
->flags
& SEC_LINKER_CREATED
)
3675 /* Remove the linker created SHT_GROUP sections. */
3676 bfd_section_list_remove (abfd
, sec
);
3677 abfd
->section_count
--;
3680 d
->this_idx
= section_number
++;
3683 /* Count relocations. */
3684 reloc_count
+= sec
->reloc_count
;
3687 /* Clear HAS_RELOC if there are no relocations. */
3688 if (reloc_count
== 0)
3689 abfd
->flags
&= ~HAS_RELOC
;
3692 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3694 d
= elf_section_data (sec
);
3696 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3697 d
->this_idx
= section_number
++;
3698 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3699 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3702 d
->rel
.idx
= section_number
++;
3703 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3704 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3711 d
->rela
.idx
= section_number
++;
3712 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3713 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3719 need_symtab
= (bfd_get_symcount (abfd
) > 0
3720 || (link_info
== NULL
3721 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3725 elf_onesymtab (abfd
) = section_number
++;
3726 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3727 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3729 elf_section_list
* entry
;
3731 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3733 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3734 entry
->ndx
= section_number
++;
3735 elf_symtab_shndx_list (abfd
) = entry
;
3737 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3738 ".symtab_shndx", FALSE
);
3739 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3742 elf_strtab_sec (abfd
) = section_number
++;
3743 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3746 elf_shstrtab_sec (abfd
) = section_number
++;
3747 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3748 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3750 if (section_number
>= SHN_LORESERVE
)
3752 /* xgettext:c-format */
3753 _bfd_error_handler (_("%pB: too many sections: %u"),
3754 abfd
, section_number
);
3758 elf_numsections (abfd
) = section_number
;
3759 elf_elfheader (abfd
)->e_shnum
= section_number
;
3761 /* Set up the list of section header pointers, in agreement with the
3763 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3764 sizeof (Elf_Internal_Shdr
*));
3765 if (i_shdrp
== NULL
)
3768 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3769 sizeof (Elf_Internal_Shdr
));
3770 if (i_shdrp
[0] == NULL
)
3772 bfd_release (abfd
, i_shdrp
);
3776 elf_elfsections (abfd
) = i_shdrp
;
3778 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3781 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3782 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3784 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3785 BFD_ASSERT (entry
!= NULL
);
3786 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3787 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3789 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3790 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3793 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3797 d
= elf_section_data (sec
);
3799 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3800 if (d
->rel
.idx
!= 0)
3801 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3802 if (d
->rela
.idx
!= 0)
3803 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3805 /* Fill in the sh_link and sh_info fields while we're at it. */
3807 /* sh_link of a reloc section is the section index of the symbol
3808 table. sh_info is the section index of the section to which
3809 the relocation entries apply. */
3810 if (d
->rel
.idx
!= 0)
3812 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3813 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3814 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3816 if (d
->rela
.idx
!= 0)
3818 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3819 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3820 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3823 /* We need to set up sh_link for SHF_LINK_ORDER. */
3824 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3826 s
= elf_linked_to_section (sec
);
3829 /* elf_linked_to_section points to the input section. */
3830 if (link_info
!= NULL
)
3832 /* Check discarded linkonce section. */
3833 if (discarded_section (s
))
3837 /* xgettext:c-format */
3838 (_("%pB: sh_link of section `%pA' points to"
3839 " discarded section `%pA' of `%pB'"),
3840 abfd
, d
->this_hdr
.bfd_section
,
3842 /* Point to the kept section if it has the same
3843 size as the discarded one. */
3844 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3847 bfd_set_error (bfd_error_bad_value
);
3853 s
= s
->output_section
;
3854 BFD_ASSERT (s
!= NULL
);
3858 /* Handle objcopy. */
3859 if (s
->output_section
== NULL
)
3862 /* xgettext:c-format */
3863 (_("%pB: sh_link of section `%pA' points to"
3864 " removed section `%pA' of `%pB'"),
3865 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3866 bfd_set_error (bfd_error_bad_value
);
3869 s
= s
->output_section
;
3871 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3876 The Intel C compiler generates SHT_IA_64_UNWIND with
3877 SHF_LINK_ORDER. But it doesn't set the sh_link or
3878 sh_info fields. Hence we could get the situation
3880 const struct elf_backend_data
*bed
3881 = get_elf_backend_data (abfd
);
3882 if (bed
->link_order_error_handler
)
3883 bed
->link_order_error_handler
3884 /* xgettext:c-format */
3885 (_("%pB: warning: sh_link not set for section `%pA'"),
3890 switch (d
->this_hdr
.sh_type
)
3894 /* A reloc section which we are treating as a normal BFD
3895 section. sh_link is the section index of the symbol
3896 table. sh_info is the section index of the section to
3897 which the relocation entries apply. We assume that an
3898 allocated reloc section uses the dynamic symbol table.
3899 FIXME: How can we be sure? */
3900 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3902 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3904 s
= elf_get_reloc_section (sec
);
3907 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3908 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3913 /* We assume that a section named .stab*str is a stabs
3914 string section. We look for a section with the same name
3915 but without the trailing ``str'', and set its sh_link
3916 field to point to this section. */
3917 if (CONST_STRNEQ (sec
->name
, ".stab")
3918 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3923 len
= strlen (sec
->name
);
3924 alc
= (char *) bfd_malloc (len
- 2);
3927 memcpy (alc
, sec
->name
, len
- 3);
3928 alc
[len
- 3] = '\0';
3929 s
= bfd_get_section_by_name (abfd
, alc
);
3933 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3935 /* This is a .stab section. */
3936 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3937 elf_section_data (s
)->this_hdr
.sh_entsize
3938 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3945 case SHT_GNU_verneed
:
3946 case SHT_GNU_verdef
:
3947 /* sh_link is the section header index of the string table
3948 used for the dynamic entries, or the symbol table, or the
3950 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3952 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3955 case SHT_GNU_LIBLIST
:
3956 /* sh_link is the section header index of the prelink library
3957 list used for the dynamic entries, or the symbol table, or
3958 the version strings. */
3959 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3960 ? ".dynstr" : ".gnu.libstr");
3962 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3967 case SHT_GNU_versym
:
3968 /* sh_link is the section header index of the symbol table
3969 this hash table or version table is for. */
3970 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3972 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3976 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3980 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3981 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3982 debug section name from .debug_* to .zdebug_* if needed. */
3988 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3990 /* If the backend has a special mapping, use it. */
3991 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3992 if (bed
->elf_backend_sym_is_global
)
3993 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3995 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3996 || bfd_is_und_section (bfd_get_section (sym
))
3997 || bfd_is_com_section (bfd_get_section (sym
)));
4000 /* Filter global symbols of ABFD to include in the import library. All
4001 SYMCOUNT symbols of ABFD can be examined from their pointers in
4002 SYMS. Pointers of symbols to keep should be stored contiguously at
4003 the beginning of that array.
4005 Returns the number of symbols to keep. */
4008 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4009 asymbol
**syms
, long symcount
)
4011 long src_count
, dst_count
= 0;
4013 for (src_count
= 0; src_count
< symcount
; src_count
++)
4015 asymbol
*sym
= syms
[src_count
];
4016 char *name
= (char *) bfd_asymbol_name (sym
);
4017 struct bfd_link_hash_entry
*h
;
4019 if (!sym_is_global (abfd
, sym
))
4022 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4025 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4027 if (h
->linker_def
|| h
->ldscript_def
)
4030 syms
[dst_count
++] = sym
;
4033 syms
[dst_count
] = NULL
;
4038 /* Don't output section symbols for sections that are not going to be
4039 output, that are duplicates or there is no BFD section. */
4042 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4044 elf_symbol_type
*type_ptr
;
4049 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4052 if (sym
->section
== NULL
)
4055 type_ptr
= elf_symbol_from (abfd
, sym
);
4056 return ((type_ptr
!= NULL
4057 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4058 && bfd_is_abs_section (sym
->section
))
4059 || !(sym
->section
->owner
== abfd
4060 || (sym
->section
->output_section
!= NULL
4061 && sym
->section
->output_section
->owner
== abfd
4062 && sym
->section
->output_offset
== 0)
4063 || bfd_is_abs_section (sym
->section
)));
4066 /* Map symbol from it's internal number to the external number, moving
4067 all local symbols to be at the head of the list. */
4070 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4072 unsigned int symcount
= bfd_get_symcount (abfd
);
4073 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4074 asymbol
**sect_syms
;
4075 unsigned int num_locals
= 0;
4076 unsigned int num_globals
= 0;
4077 unsigned int num_locals2
= 0;
4078 unsigned int num_globals2
= 0;
4079 unsigned int max_index
= 0;
4085 fprintf (stderr
, "elf_map_symbols\n");
4089 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4091 if (max_index
< asect
->index
)
4092 max_index
= asect
->index
;
4096 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
4097 if (sect_syms
== NULL
)
4099 elf_section_syms (abfd
) = sect_syms
;
4100 elf_num_section_syms (abfd
) = max_index
;
4102 /* Init sect_syms entries for any section symbols we have already
4103 decided to output. */
4104 for (idx
= 0; idx
< symcount
; idx
++)
4106 asymbol
*sym
= syms
[idx
];
4108 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4110 && !ignore_section_sym (abfd
, sym
)
4111 && !bfd_is_abs_section (sym
->section
))
4113 asection
*sec
= sym
->section
;
4115 if (sec
->owner
!= abfd
)
4116 sec
= sec
->output_section
;
4118 sect_syms
[sec
->index
] = syms
[idx
];
4122 /* Classify all of the symbols. */
4123 for (idx
= 0; idx
< symcount
; idx
++)
4125 if (sym_is_global (abfd
, syms
[idx
]))
4127 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4131 /* We will be adding a section symbol for each normal BFD section. Most
4132 sections will already have a section symbol in outsymbols, but
4133 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4134 at least in that case. */
4135 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4137 if (sect_syms
[asect
->index
] == NULL
)
4139 if (!sym_is_global (abfd
, asect
->symbol
))
4146 /* Now sort the symbols so the local symbols are first. */
4147 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4148 sizeof (asymbol
*));
4150 if (new_syms
== NULL
)
4153 for (idx
= 0; idx
< symcount
; idx
++)
4155 asymbol
*sym
= syms
[idx
];
4158 if (sym_is_global (abfd
, sym
))
4159 i
= num_locals
+ num_globals2
++;
4160 else if (!ignore_section_sym (abfd
, sym
))
4165 sym
->udata
.i
= i
+ 1;
4167 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4169 if (sect_syms
[asect
->index
] == NULL
)
4171 asymbol
*sym
= asect
->symbol
;
4174 sect_syms
[asect
->index
] = sym
;
4175 if (!sym_is_global (abfd
, sym
))
4178 i
= num_locals
+ num_globals2
++;
4180 sym
->udata
.i
= i
+ 1;
4184 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4186 *pnum_locals
= num_locals
;
4190 /* Align to the maximum file alignment that could be required for any
4191 ELF data structure. */
4193 static inline file_ptr
4194 align_file_position (file_ptr off
, int align
)
4196 return (off
+ align
- 1) & ~(align
- 1);
4199 /* Assign a file position to a section, optionally aligning to the
4200 required section alignment. */
4203 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4207 if (align
&& i_shdrp
->sh_addralign
> 1)
4208 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4209 i_shdrp
->sh_offset
= offset
;
4210 if (i_shdrp
->bfd_section
!= NULL
)
4211 i_shdrp
->bfd_section
->filepos
= offset
;
4212 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4213 offset
+= i_shdrp
->sh_size
;
4217 /* Compute the file positions we are going to put the sections at, and
4218 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4219 is not NULL, this is being called by the ELF backend linker. */
4222 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4223 struct bfd_link_info
*link_info
)
4225 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4226 struct fake_section_arg fsargs
;
4228 struct elf_strtab_hash
*strtab
= NULL
;
4229 Elf_Internal_Shdr
*shstrtab_hdr
;
4230 bfd_boolean need_symtab
;
4232 if (abfd
->output_has_begun
)
4235 /* Do any elf backend specific processing first. */
4236 if (bed
->elf_backend_begin_write_processing
)
4237 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4239 if (! prep_headers (abfd
))
4242 /* Post process the headers if necessary. */
4243 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4245 fsargs
.failed
= FALSE
;
4246 fsargs
.link_info
= link_info
;
4247 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4251 if (!assign_section_numbers (abfd
, link_info
))
4254 /* The backend linker builds symbol table information itself. */
4255 need_symtab
= (link_info
== NULL
4256 && (bfd_get_symcount (abfd
) > 0
4257 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4261 /* Non-zero if doing a relocatable link. */
4262 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4264 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4269 if (link_info
== NULL
)
4271 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4276 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4277 /* sh_name was set in prep_headers. */
4278 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4279 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4280 shstrtab_hdr
->sh_addr
= 0;
4281 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4282 shstrtab_hdr
->sh_entsize
= 0;
4283 shstrtab_hdr
->sh_link
= 0;
4284 shstrtab_hdr
->sh_info
= 0;
4285 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4286 shstrtab_hdr
->sh_addralign
= 1;
4288 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4294 Elf_Internal_Shdr
*hdr
;
4296 off
= elf_next_file_pos (abfd
);
4298 hdr
= & elf_symtab_hdr (abfd
);
4299 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4301 if (elf_symtab_shndx_list (abfd
) != NULL
)
4303 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4304 if (hdr
->sh_size
!= 0)
4305 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4306 /* FIXME: What about other symtab_shndx sections in the list ? */
4309 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4310 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4312 elf_next_file_pos (abfd
) = off
;
4314 /* Now that we know where the .strtab section goes, write it
4316 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4317 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4319 _bfd_elf_strtab_free (strtab
);
4322 abfd
->output_has_begun
= TRUE
;
4327 /* Make an initial estimate of the size of the program header. If we
4328 get the number wrong here, we'll redo section placement. */
4330 static bfd_size_type
4331 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4335 const struct elf_backend_data
*bed
;
4337 /* Assume we will need exactly two PT_LOAD segments: one for text
4338 and one for data. */
4341 s
= bfd_get_section_by_name (abfd
, ".interp");
4342 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4344 /* If we have a loadable interpreter section, we need a
4345 PT_INTERP segment. In this case, assume we also need a
4346 PT_PHDR segment, although that may not be true for all
4351 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4353 /* We need a PT_DYNAMIC segment. */
4357 if (info
!= NULL
&& info
->relro
)
4359 /* We need a PT_GNU_RELRO segment. */
4363 if (elf_eh_frame_hdr (abfd
))
4365 /* We need a PT_GNU_EH_FRAME segment. */
4369 if (elf_stack_flags (abfd
))
4371 /* We need a PT_GNU_STACK segment. */
4375 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4377 if ((s
->flags
& SEC_LOAD
) != 0
4378 && elf_section_type (s
) == SHT_NOTE
)
4380 unsigned int alignment_power
;
4381 /* We need a PT_NOTE segment. */
4383 /* Try to create just one PT_NOTE segment for all adjacent
4384 loadable SHT_NOTE sections. gABI requires that within a
4385 PT_NOTE segment (and also inside of each SHT_NOTE section)
4386 each note should have the same alignment. So we check
4387 whether the sections are correctly aligned. */
4388 alignment_power
= s
->alignment_power
;
4389 while (s
->next
!= NULL
4390 && s
->next
->alignment_power
== alignment_power
4391 && (s
->next
->flags
& SEC_LOAD
) != 0
4392 && elf_section_type (s
->next
) == SHT_NOTE
)
4397 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4399 if (s
->flags
& SEC_THREAD_LOCAL
)
4401 /* We need a PT_TLS segment. */
4407 bed
= get_elf_backend_data (abfd
);
4409 if ((abfd
->flags
& D_PAGED
) != 0)
4411 /* Add a PT_GNU_MBIND segment for each mbind section. */
4412 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4413 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4414 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4416 if (elf_section_data (s
)->this_hdr
.sh_info
4420 /* xgettext:c-format */
4421 (_("%pB: GNU_MBIN section `%pA' has invalid sh_info field: %d"),
4422 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4425 /* Align mbind section to page size. */
4426 if (s
->alignment_power
< page_align_power
)
4427 s
->alignment_power
= page_align_power
;
4432 /* Let the backend count up any program headers it might need. */
4433 if (bed
->elf_backend_additional_program_headers
)
4437 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4443 return segs
* bed
->s
->sizeof_phdr
;
4446 /* Find the segment that contains the output_section of section. */
4449 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4451 struct elf_segment_map
*m
;
4452 Elf_Internal_Phdr
*p
;
4454 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4460 for (i
= m
->count
- 1; i
>= 0; i
--)
4461 if (m
->sections
[i
] == section
)
4468 /* Create a mapping from a set of sections to a program segment. */
4470 static struct elf_segment_map
*
4471 make_mapping (bfd
*abfd
,
4472 asection
**sections
,
4477 struct elf_segment_map
*m
;
4482 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4483 amt
+= (to
- from
) * sizeof (asection
*);
4484 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4488 m
->p_type
= PT_LOAD
;
4489 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4490 m
->sections
[i
- from
] = *hdrpp
;
4491 m
->count
= to
- from
;
4493 if (from
== 0 && phdr
)
4495 /* Include the headers in the first PT_LOAD segment. */
4496 m
->includes_filehdr
= 1;
4497 m
->includes_phdrs
= 1;
4503 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4506 struct elf_segment_map
*
4507 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4509 struct elf_segment_map
*m
;
4511 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4512 sizeof (struct elf_segment_map
));
4516 m
->p_type
= PT_DYNAMIC
;
4518 m
->sections
[0] = dynsec
;
4523 /* Possibly add or remove segments from the segment map. */
4526 elf_modify_segment_map (bfd
*abfd
,
4527 struct bfd_link_info
*info
,
4528 bfd_boolean remove_empty_load
)
4530 struct elf_segment_map
**m
;
4531 const struct elf_backend_data
*bed
;
4533 /* The placement algorithm assumes that non allocated sections are
4534 not in PT_LOAD segments. We ensure this here by removing such
4535 sections from the segment map. We also remove excluded
4536 sections. Finally, any PT_LOAD segment without sections is
4538 m
= &elf_seg_map (abfd
);
4541 unsigned int i
, new_count
;
4543 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4545 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4546 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4547 || (*m
)->p_type
!= PT_LOAD
))
4549 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4553 (*m
)->count
= new_count
;
4555 if (remove_empty_load
4556 && (*m
)->p_type
== PT_LOAD
4558 && !(*m
)->includes_phdrs
)
4564 bed
= get_elf_backend_data (abfd
);
4565 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4567 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4574 #define IS_TBSS(s) \
4575 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4577 /* Set up a mapping from BFD sections to program segments. */
4580 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4583 struct elf_segment_map
*m
;
4584 asection
**sections
= NULL
;
4585 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4586 bfd_boolean no_user_phdrs
;
4588 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4591 info
->user_phdrs
= !no_user_phdrs
;
4593 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4597 struct elf_segment_map
*mfirst
;
4598 struct elf_segment_map
**pm
;
4601 unsigned int hdr_index
;
4602 bfd_vma maxpagesize
;
4604 bfd_boolean phdr_in_segment
;
4605 bfd_boolean writable
;
4606 bfd_boolean executable
;
4608 asection
*first_tls
= NULL
;
4609 asection
*first_mbind
= NULL
;
4610 asection
*dynsec
, *eh_frame_hdr
;
4612 bfd_vma addr_mask
, wrap_to
= 0;
4613 bfd_size_type phdr_size
;
4615 /* Select the allocated sections, and sort them. */
4617 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4618 sizeof (asection
*));
4619 if (sections
== NULL
)
4622 /* Calculate top address, avoiding undefined behaviour of shift
4623 left operator when shift count is equal to size of type
4625 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4626 addr_mask
= (addr_mask
<< 1) + 1;
4629 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4631 if ((s
->flags
& SEC_ALLOC
) != 0)
4635 /* A wrapping section potentially clashes with header. */
4636 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4637 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4640 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4643 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4645 phdr_size
= elf_program_header_size (abfd
);
4646 if (phdr_size
== (bfd_size_type
) -1)
4647 phdr_size
= get_program_header_size (abfd
, info
);
4648 phdr_size
+= bed
->s
->sizeof_ehdr
;
4649 maxpagesize
= bed
->maxpagesize
;
4650 if (maxpagesize
== 0)
4652 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4654 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4655 >= (phdr_size
& (maxpagesize
- 1))))
4656 /* For compatibility with old scripts that may not be using
4657 SIZEOF_HEADERS, add headers when it looks like space has
4658 been left for them. */
4659 phdr_in_segment
= TRUE
;
4661 /* Build the mapping. */
4665 /* If we have a .interp section, then create a PT_PHDR segment for
4666 the program headers and a PT_INTERP segment for the .interp
4668 s
= bfd_get_section_by_name (abfd
, ".interp");
4669 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4671 amt
= sizeof (struct elf_segment_map
);
4672 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4676 m
->p_type
= PT_PHDR
;
4678 m
->p_flags_valid
= 1;
4679 m
->includes_phdrs
= 1;
4680 phdr_in_segment
= TRUE
;
4684 amt
= sizeof (struct elf_segment_map
);
4685 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4689 m
->p_type
= PT_INTERP
;
4697 /* Look through the sections. We put sections in the same program
4698 segment when the start of the second section can be placed within
4699 a few bytes of the end of the first section. */
4705 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4707 && (dynsec
->flags
& SEC_LOAD
) == 0)
4710 if ((abfd
->flags
& D_PAGED
) == 0)
4711 phdr_in_segment
= FALSE
;
4713 /* Deal with -Ttext or something similar such that the first section
4714 is not adjacent to the program headers. This is an
4715 approximation, since at this point we don't know exactly how many
4716 program headers we will need. */
4717 if (phdr_in_segment
&& count
> 0)
4720 bfd_boolean separate_phdr
= FALSE
;
4722 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4724 && info
->separate_code
4725 && (sections
[0]->flags
& SEC_CODE
) != 0)
4727 /* If data sections should be separate from code and
4728 thus not executable, and the first section is
4729 executable then put the file and program headers in
4730 their own PT_LOAD. */
4731 separate_phdr
= TRUE
;
4732 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4733 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4735 /* The file and program headers are currently on the
4736 same page as the first section. Put them on the
4737 previous page if we can. */
4738 if (phdr_lma
>= maxpagesize
)
4739 phdr_lma
-= maxpagesize
;
4741 separate_phdr
= FALSE
;
4744 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4745 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4746 /* If file and program headers would be placed at the end
4747 of memory then it's probably better to omit them. */
4748 phdr_in_segment
= FALSE
;
4749 else if (phdr_lma
< wrap_to
)
4750 /* If a section wraps around to where we'll be placing
4751 file and program headers, then the headers will be
4753 phdr_in_segment
= FALSE
;
4754 else if (separate_phdr
)
4756 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4759 m
->p_paddr
= phdr_lma
;
4761 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4762 m
->p_paddr_valid
= 1;
4765 phdr_in_segment
= FALSE
;
4769 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4772 bfd_boolean new_segment
;
4776 /* See if this section and the last one will fit in the same
4779 if (last_hdr
== NULL
)
4781 /* If we don't have a segment yet, then we don't need a new
4782 one (we build the last one after this loop). */
4783 new_segment
= FALSE
;
4785 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4787 /* If this section has a different relation between the
4788 virtual address and the load address, then we need a new
4792 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4793 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4795 /* If this section has a load address that makes it overlap
4796 the previous section, then we need a new segment. */
4799 else if ((abfd
->flags
& D_PAGED
) != 0
4800 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4801 == (hdr
->lma
& -maxpagesize
)))
4803 /* If we are demand paged then we can't map two disk
4804 pages onto the same memory page. */
4805 new_segment
= FALSE
;
4807 /* In the next test we have to be careful when last_hdr->lma is close
4808 to the end of the address space. If the aligned address wraps
4809 around to the start of the address space, then there are no more
4810 pages left in memory and it is OK to assume that the current
4811 section can be included in the current segment. */
4812 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4813 + maxpagesize
> last_hdr
->lma
)
4814 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4815 + maxpagesize
<= hdr
->lma
))
4817 /* If putting this section in this segment would force us to
4818 skip a page in the segment, then we need a new segment. */
4821 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4822 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4824 /* We don't want to put a loaded section after a
4825 nonloaded (ie. bss style) section in the same segment
4826 as that will force the non-loaded section to be loaded.
4827 Consider .tbss sections as loaded for this purpose. */
4830 else if ((abfd
->flags
& D_PAGED
) == 0)
4832 /* If the file is not demand paged, which means that we
4833 don't require the sections to be correctly aligned in the
4834 file, then there is no other reason for a new segment. */
4835 new_segment
= FALSE
;
4837 else if (info
!= NULL
4838 && info
->separate_code
4839 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4844 && (hdr
->flags
& SEC_READONLY
) == 0)
4846 /* We don't want to put a writable section in a read only
4852 /* Otherwise, we can use the same segment. */
4853 new_segment
= FALSE
;
4856 /* Allow interested parties a chance to override our decision. */
4857 if (last_hdr
!= NULL
4859 && info
->callbacks
->override_segment_assignment
!= NULL
)
4861 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4867 if ((hdr
->flags
& SEC_READONLY
) == 0)
4869 if ((hdr
->flags
& SEC_CODE
) != 0)
4872 /* .tbss sections effectively have zero size. */
4873 last_size
= !IS_TBSS (hdr
) ? hdr
->size
: 0;
4877 /* We need a new program segment. We must create a new program
4878 header holding all the sections from hdr_index until hdr. */
4880 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4887 if ((hdr
->flags
& SEC_READONLY
) == 0)
4892 if ((hdr
->flags
& SEC_CODE
) == 0)
4898 /* .tbss sections effectively have zero size. */
4899 last_size
= !IS_TBSS (hdr
) ? hdr
->size
: 0;
4901 phdr_in_segment
= FALSE
;
4904 /* Create a final PT_LOAD program segment, but not if it's just
4906 if (last_hdr
!= NULL
4907 && (i
- hdr_index
!= 1
4908 || !IS_TBSS (last_hdr
)))
4910 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4918 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4921 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4928 /* For each batch of consecutive loadable SHT_NOTE sections,
4929 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4930 because if we link together nonloadable .note sections and
4931 loadable .note sections, we will generate two .note sections
4932 in the output file. */
4933 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4935 if ((s
->flags
& SEC_LOAD
) != 0
4936 && elf_section_type (s
) == SHT_NOTE
)
4939 unsigned int alignment_power
= s
->alignment_power
;
4942 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4944 if (s2
->next
->alignment_power
== alignment_power
4945 && (s2
->next
->flags
& SEC_LOAD
) != 0
4946 && elf_section_type (s2
->next
) == SHT_NOTE
4947 && align_power (s2
->lma
+ s2
->size
,
4954 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4955 amt
+= count
* sizeof (asection
*);
4956 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4960 m
->p_type
= PT_NOTE
;
4964 m
->sections
[m
->count
- count
--] = s
;
4965 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4968 m
->sections
[m
->count
- 1] = s
;
4969 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4973 if (s
->flags
& SEC_THREAD_LOCAL
)
4979 if (first_mbind
== NULL
4980 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
4984 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4987 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4988 amt
+= tls_count
* sizeof (asection
*);
4989 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4994 m
->count
= tls_count
;
4995 /* Mandated PF_R. */
4997 m
->p_flags_valid
= 1;
4999 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
5001 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5004 (_("%pB: TLS sections are not adjacent:"), abfd
);
5007 while (i
< (unsigned int) tls_count
)
5009 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5011 _bfd_error_handler (_(" TLS: %pA"), s
);
5015 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5018 bfd_set_error (bfd_error_bad_value
);
5029 if (first_mbind
&& (abfd
->flags
& D_PAGED
) != 0)
5030 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5031 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5032 && (elf_section_data (s
)->this_hdr
.sh_info
5033 <= PT_GNU_MBIND_NUM
))
5035 /* Mandated PF_R. */
5036 unsigned long p_flags
= PF_R
;
5037 if ((s
->flags
& SEC_READONLY
) == 0)
5039 if ((s
->flags
& SEC_CODE
) != 0)
5042 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5043 m
= bfd_zalloc (abfd
, amt
);
5047 m
->p_type
= (PT_GNU_MBIND_LO
5048 + elf_section_data (s
)->this_hdr
.sh_info
);
5050 m
->p_flags_valid
= 1;
5052 m
->p_flags
= p_flags
;
5058 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5060 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5061 if (eh_frame_hdr
!= NULL
5062 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5064 amt
= sizeof (struct elf_segment_map
);
5065 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5069 m
->p_type
= PT_GNU_EH_FRAME
;
5071 m
->sections
[0] = eh_frame_hdr
->output_section
;
5077 if (elf_stack_flags (abfd
))
5079 amt
= sizeof (struct elf_segment_map
);
5080 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5084 m
->p_type
= PT_GNU_STACK
;
5085 m
->p_flags
= elf_stack_flags (abfd
);
5086 m
->p_align
= bed
->stack_align
;
5087 m
->p_flags_valid
= 1;
5088 m
->p_align_valid
= m
->p_align
!= 0;
5089 if (info
->stacksize
> 0)
5091 m
->p_size
= info
->stacksize
;
5092 m
->p_size_valid
= 1;
5099 if (info
!= NULL
&& info
->relro
)
5101 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5103 if (m
->p_type
== PT_LOAD
5105 && m
->sections
[0]->vma
>= info
->relro_start
5106 && m
->sections
[0]->vma
< info
->relro_end
)
5109 while (--i
!= (unsigned) -1)
5110 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5111 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5114 if (i
!= (unsigned) -1)
5119 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5122 amt
= sizeof (struct elf_segment_map
);
5123 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5127 m
->p_type
= PT_GNU_RELRO
;
5134 elf_seg_map (abfd
) = mfirst
;
5137 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5140 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5142 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5147 if (sections
!= NULL
)
5152 /* Sort sections by address. */
5155 elf_sort_sections (const void *arg1
, const void *arg2
)
5157 const asection
*sec1
= *(const asection
**) arg1
;
5158 const asection
*sec2
= *(const asection
**) arg2
;
5159 bfd_size_type size1
, size2
;
5161 /* Sort by LMA first, since this is the address used to
5162 place the section into a segment. */
5163 if (sec1
->lma
< sec2
->lma
)
5165 else if (sec1
->lma
> sec2
->lma
)
5168 /* Then sort by VMA. Normally the LMA and the VMA will be
5169 the same, and this will do nothing. */
5170 if (sec1
->vma
< sec2
->vma
)
5172 else if (sec1
->vma
> sec2
->vma
)
5175 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5177 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5183 /* If the indices are the same, do not return 0
5184 here, but continue to try the next comparison. */
5185 if (sec1
->target_index
- sec2
->target_index
!= 0)
5186 return sec1
->target_index
- sec2
->target_index
;
5191 else if (TOEND (sec2
))
5196 /* Sort by size, to put zero sized sections
5197 before others at the same address. */
5199 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5200 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5207 return sec1
->target_index
- sec2
->target_index
;
5210 /* Ian Lance Taylor writes:
5212 We shouldn't be using % with a negative signed number. That's just
5213 not good. We have to make sure either that the number is not
5214 negative, or that the number has an unsigned type. When the types
5215 are all the same size they wind up as unsigned. When file_ptr is a
5216 larger signed type, the arithmetic winds up as signed long long,
5219 What we're trying to say here is something like ``increase OFF by
5220 the least amount that will cause it to be equal to the VMA modulo
5222 /* In other words, something like:
5224 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5225 off_offset = off % bed->maxpagesize;
5226 if (vma_offset < off_offset)
5227 adjustment = vma_offset + bed->maxpagesize - off_offset;
5229 adjustment = vma_offset - off_offset;
5231 which can be collapsed into the expression below. */
5234 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5236 /* PR binutils/16199: Handle an alignment of zero. */
5237 if (maxpagesize
== 0)
5239 return ((vma
- off
) % maxpagesize
);
5243 print_segment_map (const struct elf_segment_map
*m
)
5246 const char *pt
= get_segment_type (m
->p_type
);
5251 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5252 sprintf (buf
, "LOPROC+%7.7x",
5253 (unsigned int) (m
->p_type
- PT_LOPROC
));
5254 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5255 sprintf (buf
, "LOOS+%7.7x",
5256 (unsigned int) (m
->p_type
- PT_LOOS
));
5258 snprintf (buf
, sizeof (buf
), "%8.8x",
5259 (unsigned int) m
->p_type
);
5263 fprintf (stderr
, "%s:", pt
);
5264 for (j
= 0; j
< m
->count
; j
++)
5265 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5271 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5276 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5278 buf
= bfd_zmalloc (len
);
5281 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5286 /* Assign file positions to the sections based on the mapping from
5287 sections to segments. This function also sets up some fields in
5291 assign_file_positions_for_load_sections (bfd
*abfd
,
5292 struct bfd_link_info
*link_info
)
5294 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5295 struct elf_segment_map
*m
;
5296 Elf_Internal_Phdr
*phdrs
;
5297 Elf_Internal_Phdr
*p
;
5299 bfd_size_type maxpagesize
;
5300 unsigned int pt_load_count
= 0;
5303 bfd_vma header_pad
= 0;
5305 if (link_info
== NULL
5306 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5310 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5314 header_pad
= m
->header_size
;
5319 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5320 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5324 /* PR binutils/12467. */
5325 elf_elfheader (abfd
)->e_phoff
= 0;
5326 elf_elfheader (abfd
)->e_phentsize
= 0;
5329 elf_elfheader (abfd
)->e_phnum
= alloc
;
5331 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5332 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5334 BFD_ASSERT (elf_program_header_size (abfd
)
5335 >= alloc
* bed
->s
->sizeof_phdr
);
5339 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5343 /* We're writing the size in elf_program_header_size (abfd),
5344 see assign_file_positions_except_relocs, so make sure we have
5345 that amount allocated, with trailing space cleared.
5346 The variable alloc contains the computed need, while
5347 elf_program_header_size (abfd) contains the size used for the
5349 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5350 where the layout is forced to according to a larger size in the
5351 last iterations for the testcase ld-elf/header. */
5352 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5354 phdrs
= (Elf_Internal_Phdr
*)
5356 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5357 sizeof (Elf_Internal_Phdr
));
5358 elf_tdata (abfd
)->phdr
= phdrs
;
5363 if ((abfd
->flags
& D_PAGED
) != 0)
5364 maxpagesize
= bed
->maxpagesize
;
5366 off
= bed
->s
->sizeof_ehdr
;
5367 off
+= alloc
* bed
->s
->sizeof_phdr
;
5368 if (header_pad
< (bfd_vma
) off
)
5374 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5376 m
= m
->next
, p
++, j
++)
5380 bfd_boolean no_contents
;
5382 /* If elf_segment_map is not from map_sections_to_segments, the
5383 sections may not be correctly ordered. NOTE: sorting should
5384 not be done to the PT_NOTE section of a corefile, which may
5385 contain several pseudo-sections artificially created by bfd.
5386 Sorting these pseudo-sections breaks things badly. */
5388 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5389 && m
->p_type
== PT_NOTE
))
5390 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5393 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5394 number of sections with contents contributing to both p_filesz
5395 and p_memsz, followed by a number of sections with no contents
5396 that just contribute to p_memsz. In this loop, OFF tracks next
5397 available file offset for PT_LOAD and PT_NOTE segments. */
5398 p
->p_type
= m
->p_type
;
5399 p
->p_flags
= m
->p_flags
;
5402 p
->p_vaddr
= m
->p_vaddr_offset
;
5404 p
->p_vaddr
= m
->sections
[0]->vma
+ m
->p_vaddr_offset
;
5406 if (m
->p_paddr_valid
)
5407 p
->p_paddr
= m
->p_paddr
;
5408 else if (m
->count
== 0)
5411 p
->p_paddr
= m
->sections
[0]->lma
+ m
->p_vaddr_offset
;
5413 if (p
->p_type
== PT_LOAD
5414 && (abfd
->flags
& D_PAGED
) != 0)
5416 /* p_align in demand paged PT_LOAD segments effectively stores
5417 the maximum page size. When copying an executable with
5418 objcopy, we set m->p_align from the input file. Use this
5419 value for maxpagesize rather than bed->maxpagesize, which
5420 may be different. Note that we use maxpagesize for PT_TLS
5421 segment alignment later in this function, so we are relying
5422 on at least one PT_LOAD segment appearing before a PT_TLS
5424 if (m
->p_align_valid
)
5425 maxpagesize
= m
->p_align
;
5427 p
->p_align
= maxpagesize
;
5430 else if (m
->p_align_valid
)
5431 p
->p_align
= m
->p_align
;
5432 else if (m
->count
== 0)
5433 p
->p_align
= 1 << bed
->s
->log_file_align
;
5437 no_contents
= FALSE
;
5439 if (p
->p_type
== PT_LOAD
5442 bfd_size_type align
;
5443 unsigned int align_power
= 0;
5445 if (m
->p_align_valid
)
5449 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5451 unsigned int secalign
;
5453 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5454 if (secalign
> align_power
)
5455 align_power
= secalign
;
5457 align
= (bfd_size_type
) 1 << align_power
;
5458 if (align
< maxpagesize
)
5459 align
= maxpagesize
;
5462 for (i
= 0; i
< m
->count
; i
++)
5463 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5464 /* If we aren't making room for this section, then
5465 it must be SHT_NOBITS regardless of what we've
5466 set via struct bfd_elf_special_section. */
5467 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5469 /* Find out whether this segment contains any loadable
5472 for (i
= 0; i
< m
->count
; i
++)
5473 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5475 no_contents
= FALSE
;
5479 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5481 /* Broken hardware and/or kernel require that files do not
5482 map the same page with different permissions on some hppa
5484 if (pt_load_count
> 1
5485 && bed
->no_page_alias
5486 && (off
& (maxpagesize
- 1)) != 0
5487 && (off
& -maxpagesize
) == ((off
+ off_adjust
) & -maxpagesize
))
5488 off_adjust
+= maxpagesize
;
5492 /* We shouldn't need to align the segment on disk since
5493 the segment doesn't need file space, but the gABI
5494 arguably requires the alignment and glibc ld.so
5495 checks it. So to comply with the alignment
5496 requirement but not waste file space, we adjust
5497 p_offset for just this segment. (OFF_ADJUST is
5498 subtracted from OFF later.) This may put p_offset
5499 past the end of file, but that shouldn't matter. */
5504 /* Make sure the .dynamic section is the first section in the
5505 PT_DYNAMIC segment. */
5506 else if (p
->p_type
== PT_DYNAMIC
5508 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5511 (_("%pB: The first section in the PT_DYNAMIC segment"
5512 " is not the .dynamic section"),
5514 bfd_set_error (bfd_error_bad_value
);
5517 /* Set the note section type to SHT_NOTE. */
5518 else if (p
->p_type
== PT_NOTE
)
5519 for (i
= 0; i
< m
->count
; i
++)
5520 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5526 if (m
->includes_filehdr
)
5528 if (!m
->p_flags_valid
)
5530 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5531 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5534 if (p
->p_vaddr
< (bfd_vma
) off
5535 || (!m
->p_paddr_valid
5536 && p
->p_paddr
< (bfd_vma
) off
))
5539 (_("%pB: not enough room for program headers,"
5540 " try linking with -N"),
5542 bfd_set_error (bfd_error_bad_value
);
5547 if (!m
->p_paddr_valid
)
5552 if (m
->includes_phdrs
)
5554 if (!m
->p_flags_valid
)
5557 if (!m
->includes_filehdr
)
5559 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5563 p
->p_vaddr
-= off
- p
->p_offset
;
5564 if (!m
->p_paddr_valid
)
5565 p
->p_paddr
-= off
- p
->p_offset
;
5569 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5570 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5573 p
->p_filesz
+= header_pad
;
5574 p
->p_memsz
+= header_pad
;
5578 if (p
->p_type
== PT_LOAD
5579 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5581 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5587 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5589 p
->p_filesz
+= adjust
;
5590 p
->p_memsz
+= adjust
;
5594 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5595 maps. Set filepos for sections in PT_LOAD segments, and in
5596 core files, for sections in PT_NOTE segments.
5597 assign_file_positions_for_non_load_sections will set filepos
5598 for other sections and update p_filesz for other segments. */
5599 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5602 bfd_size_type align
;
5603 Elf_Internal_Shdr
*this_hdr
;
5606 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5607 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5609 if ((p
->p_type
== PT_LOAD
5610 || p
->p_type
== PT_TLS
)
5611 && (this_hdr
->sh_type
!= SHT_NOBITS
5612 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5613 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5614 || p
->p_type
== PT_TLS
))))
5616 bfd_vma p_start
= p
->p_paddr
;
5617 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5618 bfd_vma s_start
= sec
->lma
;
5619 bfd_vma adjust
= s_start
- p_end
;
5623 || p_end
< p_start
))
5626 /* xgettext:c-format */
5627 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5628 abfd
, sec
, (uint64_t) s_start
, (uint64_t) p_end
);
5632 p
->p_memsz
+= adjust
;
5634 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5636 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5638 /* We have a PROGBITS section following NOBITS ones.
5639 Allocate file space for the NOBITS section(s) and
5641 adjust
= p
->p_memsz
- p
->p_filesz
;
5642 if (!write_zeros (abfd
, off
, adjust
))
5646 p
->p_filesz
+= adjust
;
5650 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5652 /* The section at i == 0 is the one that actually contains
5656 this_hdr
->sh_offset
= sec
->filepos
= off
;
5657 off
+= this_hdr
->sh_size
;
5658 p
->p_filesz
= this_hdr
->sh_size
;
5664 /* The rest are fake sections that shouldn't be written. */
5673 if (p
->p_type
== PT_LOAD
)
5675 this_hdr
->sh_offset
= sec
->filepos
= off
;
5676 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5677 off
+= this_hdr
->sh_size
;
5679 else if (this_hdr
->sh_type
== SHT_NOBITS
5680 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5681 && this_hdr
->sh_offset
== 0)
5683 /* This is a .tbss section that didn't get a PT_LOAD.
5684 (See _bfd_elf_map_sections_to_segments "Create a
5685 final PT_LOAD".) Set sh_offset to the value it
5686 would have if we had created a zero p_filesz and
5687 p_memsz PT_LOAD header for the section. This
5688 also makes the PT_TLS header have the same
5690 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5692 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5695 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5697 p
->p_filesz
+= this_hdr
->sh_size
;
5698 /* A load section without SHF_ALLOC is something like
5699 a note section in a PT_NOTE segment. These take
5700 file space but are not loaded into memory. */
5701 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5702 p
->p_memsz
+= this_hdr
->sh_size
;
5704 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5706 if (p
->p_type
== PT_TLS
)
5707 p
->p_memsz
+= this_hdr
->sh_size
;
5709 /* .tbss is special. It doesn't contribute to p_memsz of
5711 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5712 p
->p_memsz
+= this_hdr
->sh_size
;
5715 if (align
> p
->p_align
5716 && !m
->p_align_valid
5717 && (p
->p_type
!= PT_LOAD
5718 || (abfd
->flags
& D_PAGED
) == 0))
5722 if (!m
->p_flags_valid
)
5725 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5727 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5734 /* Check that all sections are in a PT_LOAD segment.
5735 Don't check funky gdb generated core files. */
5736 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5738 bfd_boolean check_vma
= TRUE
;
5740 for (i
= 1; i
< m
->count
; i
++)
5741 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5742 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5743 ->this_hdr
), p
) != 0
5744 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5745 ->this_hdr
), p
) != 0)
5747 /* Looks like we have overlays packed into the segment. */
5752 for (i
= 0; i
< m
->count
; i
++)
5754 Elf_Internal_Shdr
*this_hdr
;
5757 sec
= m
->sections
[i
];
5758 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5759 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5760 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5763 /* xgettext:c-format */
5764 (_("%pB: section `%pA' can't be allocated in segment %d"),
5766 print_segment_map (m
);
5772 elf_next_file_pos (abfd
) = off
;
5776 /* Assign file positions for the other sections. */
5779 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5780 struct bfd_link_info
*link_info
)
5782 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5783 Elf_Internal_Shdr
**i_shdrpp
;
5784 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5785 Elf_Internal_Phdr
*phdrs
;
5786 Elf_Internal_Phdr
*p
;
5787 struct elf_segment_map
*m
;
5788 struct elf_segment_map
*hdrs_segment
;
5789 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5790 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5794 i_shdrpp
= elf_elfsections (abfd
);
5795 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5796 off
= elf_next_file_pos (abfd
);
5797 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5799 Elf_Internal_Shdr
*hdr
;
5802 if (hdr
->bfd_section
!= NULL
5803 && (hdr
->bfd_section
->filepos
!= 0
5804 || (hdr
->sh_type
== SHT_NOBITS
5805 && hdr
->contents
== NULL
)))
5806 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5807 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5809 if (hdr
->sh_size
!= 0)
5811 /* xgettext:c-format */
5812 (_("%pB: warning: allocated section `%s' not in segment"),
5814 (hdr
->bfd_section
== NULL
5816 : hdr
->bfd_section
->name
));
5817 /* We don't need to page align empty sections. */
5818 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5819 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5822 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5824 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5827 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5828 && hdr
->bfd_section
== NULL
)
5829 || (hdr
->bfd_section
!= NULL
5830 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5831 /* Compress DWARF debug sections. */
5832 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5833 || (elf_symtab_shndx_list (abfd
) != NULL
5834 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5835 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5836 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5837 hdr
->sh_offset
= -1;
5839 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5842 /* Now that we have set the section file positions, we can set up
5843 the file positions for the non PT_LOAD segments. */
5847 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5849 hdrs_segment
= NULL
;
5850 phdrs
= elf_tdata (abfd
)->phdr
;
5851 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5854 if (p
->p_type
!= PT_LOAD
)
5857 if (m
->includes_filehdr
)
5859 filehdr_vaddr
= p
->p_vaddr
;
5860 filehdr_paddr
= p
->p_paddr
;
5862 if (m
->includes_phdrs
)
5864 phdrs_vaddr
= p
->p_vaddr
;
5865 phdrs_paddr
= p
->p_paddr
;
5866 if (m
->includes_filehdr
)
5869 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5870 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5875 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5877 /* There is a segment that contains both the file headers and the
5878 program headers, so provide a symbol __ehdr_start pointing there.
5879 A program can use this to examine itself robustly. */
5881 struct elf_link_hash_entry
*hash
5882 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5883 FALSE
, FALSE
, TRUE
);
5884 /* If the symbol was referenced and not defined, define it. */
5886 && (hash
->root
.type
== bfd_link_hash_new
5887 || hash
->root
.type
== bfd_link_hash_undefined
5888 || hash
->root
.type
== bfd_link_hash_undefweak
5889 || hash
->root
.type
== bfd_link_hash_common
))
5892 if (hdrs_segment
->count
!= 0)
5893 /* The segment contains sections, so use the first one. */
5894 s
= hdrs_segment
->sections
[0];
5896 /* Use the first (i.e. lowest-addressed) section in any segment. */
5897 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5906 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5907 hash
->root
.u
.def
.section
= s
;
5911 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5912 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5915 hash
->root
.type
= bfd_link_hash_defined
;
5916 hash
->def_regular
= 1;
5921 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5923 if (p
->p_type
== PT_GNU_RELRO
)
5928 if (link_info
!= NULL
)
5930 /* During linking the range of the RELRO segment is passed
5931 in link_info. Note that there may be padding between
5932 relro_start and the first RELRO section. */
5933 start
= link_info
->relro_start
;
5934 end
= link_info
->relro_end
;
5936 else if (m
->count
!= 0)
5938 if (!m
->p_size_valid
)
5940 start
= m
->sections
[0]->vma
;
5941 end
= start
+ m
->p_size
;
5952 struct elf_segment_map
*lm
;
5953 const Elf_Internal_Phdr
*lp
;
5956 /* Find a LOAD segment containing a section in the RELRO
5958 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5960 lm
= lm
->next
, lp
++)
5962 if (lp
->p_type
== PT_LOAD
5964 && (lm
->sections
[lm
->count
- 1]->vma
5965 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
5966 ? lm
->sections
[lm
->count
- 1]->size
5968 && lm
->sections
[0]->vma
< end
)
5974 /* Find the section starting the RELRO segment. */
5975 for (i
= 0; i
< lm
->count
; i
++)
5977 asection
*s
= lm
->sections
[i
];
5986 p
->p_vaddr
= lm
->sections
[i
]->vma
;
5987 p
->p_paddr
= lm
->sections
[i
]->lma
;
5988 p
->p_offset
= lm
->sections
[i
]->filepos
;
5989 p
->p_memsz
= end
- p
->p_vaddr
;
5990 p
->p_filesz
= p
->p_memsz
;
5992 /* The RELRO segment typically ends a few bytes
5993 into .got.plt but other layouts are possible.
5994 In cases where the end does not match any
5995 loaded section (for instance is in file
5996 padding), trim p_filesz back to correspond to
5997 the end of loaded section contents. */
5998 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
5999 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6001 /* Preserve the alignment and flags if they are
6002 valid. The gold linker generates RW/4 for
6003 the PT_GNU_RELRO section. It is better for
6004 objcopy/strip to honor these attributes
6005 otherwise gdb will choke when using separate
6007 if (!m
->p_align_valid
)
6009 if (!m
->p_flags_valid
)
6015 if (link_info
!= NULL
)
6018 memset (p
, 0, sizeof *p
);
6020 else if (p
->p_type
== PT_GNU_STACK
)
6022 if (m
->p_size_valid
)
6023 p
->p_memsz
= m
->p_size
;
6025 else if (m
->count
!= 0)
6029 if (p
->p_type
!= PT_LOAD
6030 && (p
->p_type
!= PT_NOTE
6031 || bfd_get_format (abfd
) != bfd_core
))
6033 /* A user specified segment layout may include a PHDR
6034 segment that overlaps with a LOAD segment... */
6035 if (p
->p_type
== PT_PHDR
)
6041 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6043 /* PR 17512: file: 2195325e. */
6045 (_("%pB: error: non-load segment %d includes file header "
6046 "and/or program header"),
6047 abfd
, (int) (p
- phdrs
));
6052 p
->p_offset
= m
->sections
[0]->filepos
;
6053 for (i
= m
->count
; i
-- != 0;)
6055 asection
*sect
= m
->sections
[i
];
6056 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6057 if (hdr
->sh_type
!= SHT_NOBITS
)
6059 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6066 else if (m
->includes_filehdr
)
6068 p
->p_vaddr
= filehdr_vaddr
;
6069 if (! m
->p_paddr_valid
)
6070 p
->p_paddr
= filehdr_paddr
;
6072 else if (m
->includes_phdrs
)
6074 p
->p_vaddr
= phdrs_vaddr
;
6075 if (! m
->p_paddr_valid
)
6076 p
->p_paddr
= phdrs_paddr
;
6080 elf_next_file_pos (abfd
) = off
;
6085 static elf_section_list
*
6086 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6088 for (;list
!= NULL
; list
= list
->next
)
6094 /* Work out the file positions of all the sections. This is called by
6095 _bfd_elf_compute_section_file_positions. All the section sizes and
6096 VMAs must be known before this is called.
6098 Reloc sections come in two flavours: Those processed specially as
6099 "side-channel" data attached to a section to which they apply, and
6100 those that bfd doesn't process as relocations. The latter sort are
6101 stored in a normal bfd section by bfd_section_from_shdr. We don't
6102 consider the former sort here, unless they form part of the loadable
6103 image. Reloc sections not assigned here will be handled later by
6104 assign_file_positions_for_relocs.
6106 We also don't set the positions of the .symtab and .strtab here. */
6109 assign_file_positions_except_relocs (bfd
*abfd
,
6110 struct bfd_link_info
*link_info
)
6112 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6113 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6114 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6116 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6117 && bfd_get_format (abfd
) != bfd_core
)
6119 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6120 unsigned int num_sec
= elf_numsections (abfd
);
6121 Elf_Internal_Shdr
**hdrpp
;
6125 /* Start after the ELF header. */
6126 off
= i_ehdrp
->e_ehsize
;
6128 /* We are not creating an executable, which means that we are
6129 not creating a program header, and that the actual order of
6130 the sections in the file is unimportant. */
6131 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6133 Elf_Internal_Shdr
*hdr
;
6136 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6137 && hdr
->bfd_section
== NULL
)
6138 || (hdr
->bfd_section
!= NULL
6139 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
6140 /* Compress DWARF debug sections. */
6141 || i
== elf_onesymtab (abfd
)
6142 || (elf_symtab_shndx_list (abfd
) != NULL
6143 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6144 || i
== elf_strtab_sec (abfd
)
6145 || i
== elf_shstrtab_sec (abfd
))
6147 hdr
->sh_offset
= -1;
6150 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6153 elf_next_file_pos (abfd
) = off
;
6159 /* Assign file positions for the loaded sections based on the
6160 assignment of sections to segments. */
6161 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6164 /* And for non-load sections. */
6165 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6168 if (bed
->elf_backend_modify_program_headers
!= NULL
)
6170 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
6174 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6175 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6177 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
6178 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
6179 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6181 /* Find the lowest p_vaddr in PT_LOAD segments. */
6182 bfd_vma p_vaddr
= (bfd_vma
) -1;
6183 for (; segment
< end_segment
; segment
++)
6184 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6185 p_vaddr
= segment
->p_vaddr
;
6187 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6188 segments is non-zero. */
6190 i_ehdrp
->e_type
= ET_EXEC
;
6193 /* Write out the program headers. */
6194 alloc
= elf_elfheader (abfd
)->e_phnum
;
6198 /* PR ld/20815 - Check that the program header segment, if present, will
6199 be loaded into memory. FIXME: The check below is not sufficient as
6200 really all PT_LOAD segments should be checked before issuing an error
6201 message. Plus the PHDR segment does not have to be the first segment
6202 in the program header table. But this version of the check should
6203 catch all real world use cases.
6205 FIXME: We used to have code here to sort the PT_LOAD segments into
6206 ascending order, as per the ELF spec. But this breaks some programs,
6207 including the Linux kernel. But really either the spec should be
6208 changed or the programs updated. */
6210 && tdata
->phdr
[0].p_type
== PT_PHDR
6211 && (bed
->elf_backend_allow_non_load_phdr
== NULL
6212 || !bed
->elf_backend_allow_non_load_phdr (abfd
, tdata
->phdr
,
6214 && tdata
->phdr
[1].p_type
== PT_LOAD
6215 && (tdata
->phdr
[1].p_vaddr
> tdata
->phdr
[0].p_vaddr
6216 || (tdata
->phdr
[1].p_vaddr
+ tdata
->phdr
[1].p_memsz
6217 < tdata
->phdr
[0].p_vaddr
+ tdata
->phdr
[0].p_memsz
)))
6219 /* The fix for this error is usually to edit the linker script being
6220 used and set up the program headers manually. Either that or
6221 leave room for the headers at the start of the SECTIONS. */
6222 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6223 " by LOAD segment"),
6228 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
6229 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6237 prep_headers (bfd
*abfd
)
6239 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6240 struct elf_strtab_hash
*shstrtab
;
6241 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6243 i_ehdrp
= elf_elfheader (abfd
);
6245 shstrtab
= _bfd_elf_strtab_init ();
6246 if (shstrtab
== NULL
)
6249 elf_shstrtab (abfd
) = shstrtab
;
6251 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6252 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6253 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6254 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6256 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6257 i_ehdrp
->e_ident
[EI_DATA
] =
6258 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6259 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6261 if ((abfd
->flags
& DYNAMIC
) != 0)
6262 i_ehdrp
->e_type
= ET_DYN
;
6263 else if ((abfd
->flags
& EXEC_P
) != 0)
6264 i_ehdrp
->e_type
= ET_EXEC
;
6265 else if (bfd_get_format (abfd
) == bfd_core
)
6266 i_ehdrp
->e_type
= ET_CORE
;
6268 i_ehdrp
->e_type
= ET_REL
;
6270 switch (bfd_get_arch (abfd
))
6272 case bfd_arch_unknown
:
6273 i_ehdrp
->e_machine
= EM_NONE
;
6276 /* There used to be a long list of cases here, each one setting
6277 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6278 in the corresponding bfd definition. To avoid duplication,
6279 the switch was removed. Machines that need special handling
6280 can generally do it in elf_backend_final_write_processing(),
6281 unless they need the information earlier than the final write.
6282 Such need can generally be supplied by replacing the tests for
6283 e_machine with the conditions used to determine it. */
6285 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6288 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6289 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6291 /* No program header, for now. */
6292 i_ehdrp
->e_phoff
= 0;
6293 i_ehdrp
->e_phentsize
= 0;
6294 i_ehdrp
->e_phnum
= 0;
6296 /* Each bfd section is section header entry. */
6297 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6298 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6300 /* If we're building an executable, we'll need a program header table. */
6301 if (abfd
->flags
& EXEC_P
)
6302 /* It all happens later. */
6306 i_ehdrp
->e_phentsize
= 0;
6307 i_ehdrp
->e_phoff
= 0;
6310 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6311 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6312 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6313 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6314 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6315 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6316 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6317 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6318 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6324 /* Assign file positions for all the reloc sections which are not part
6325 of the loadable file image, and the file position of section headers. */
6328 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6331 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6332 Elf_Internal_Shdr
*shdrp
;
6333 Elf_Internal_Ehdr
*i_ehdrp
;
6334 const struct elf_backend_data
*bed
;
6336 off
= elf_next_file_pos (abfd
);
6338 shdrpp
= elf_elfsections (abfd
);
6339 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6340 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6343 if (shdrp
->sh_offset
== -1)
6345 asection
*sec
= shdrp
->bfd_section
;
6346 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6347 || shdrp
->sh_type
== SHT_RELA
);
6349 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6353 const char *name
= sec
->name
;
6354 struct bfd_elf_section_data
*d
;
6356 /* Compress DWARF debug sections. */
6357 if (!bfd_compress_section (abfd
, sec
,
6361 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6362 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6364 /* If section is compressed with zlib-gnu, convert
6365 section name from .debug_* to .zdebug_*. */
6367 = convert_debug_to_zdebug (abfd
, name
);
6368 if (new_name
== NULL
)
6372 /* Add section name to section name section. */
6373 if (shdrp
->sh_name
!= (unsigned int) -1)
6376 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6378 d
= elf_section_data (sec
);
6380 /* Add reloc section name to section name section. */
6382 && !_bfd_elf_set_reloc_sh_name (abfd
,
6387 && !_bfd_elf_set_reloc_sh_name (abfd
,
6392 /* Update section size and contents. */
6393 shdrp
->sh_size
= sec
->size
;
6394 shdrp
->contents
= sec
->contents
;
6395 shdrp
->bfd_section
->contents
= NULL
;
6397 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6404 /* Place section name section after DWARF debug sections have been
6406 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6407 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6408 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6409 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6411 /* Place the section headers. */
6412 i_ehdrp
= elf_elfheader (abfd
);
6413 bed
= get_elf_backend_data (abfd
);
6414 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6415 i_ehdrp
->e_shoff
= off
;
6416 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6417 elf_next_file_pos (abfd
) = off
;
6423 _bfd_elf_write_object_contents (bfd
*abfd
)
6425 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6426 Elf_Internal_Shdr
**i_shdrp
;
6428 unsigned int count
, num_sec
;
6429 struct elf_obj_tdata
*t
;
6431 if (! abfd
->output_has_begun
6432 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6434 /* Do not rewrite ELF data when the BFD has been opened for update.
6435 abfd->output_has_begun was set to TRUE on opening, so creation of new
6436 sections, and modification of existing section sizes was restricted.
6437 This means the ELF header, program headers and section headers can't have
6439 If the contents of any sections has been modified, then those changes have
6440 already been written to the BFD. */
6441 else if (abfd
->direction
== both_direction
)
6443 BFD_ASSERT (abfd
->output_has_begun
);
6447 i_shdrp
= elf_elfsections (abfd
);
6450 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6454 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6457 /* After writing the headers, we need to write the sections too... */
6458 num_sec
= elf_numsections (abfd
);
6459 for (count
= 1; count
< num_sec
; count
++)
6461 i_shdrp
[count
]->sh_name
6462 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6463 i_shdrp
[count
]->sh_name
);
6464 if (bed
->elf_backend_section_processing
)
6465 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6467 if (i_shdrp
[count
]->contents
)
6469 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6471 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6472 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6477 /* Write out the section header names. */
6478 t
= elf_tdata (abfd
);
6479 if (elf_shstrtab (abfd
) != NULL
6480 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6481 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6484 if (bed
->elf_backend_final_write_processing
)
6485 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6487 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6490 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6491 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6492 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6498 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6500 /* Hopefully this can be done just like an object file. */
6501 return _bfd_elf_write_object_contents (abfd
);
6504 /* Given a section, search the header to find them. */
6507 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6509 const struct elf_backend_data
*bed
;
6510 unsigned int sec_index
;
6512 if (elf_section_data (asect
) != NULL
6513 && elf_section_data (asect
)->this_idx
!= 0)
6514 return elf_section_data (asect
)->this_idx
;
6516 if (bfd_is_abs_section (asect
))
6517 sec_index
= SHN_ABS
;
6518 else if (bfd_is_com_section (asect
))
6519 sec_index
= SHN_COMMON
;
6520 else if (bfd_is_und_section (asect
))
6521 sec_index
= SHN_UNDEF
;
6523 sec_index
= SHN_BAD
;
6525 bed
= get_elf_backend_data (abfd
);
6526 if (bed
->elf_backend_section_from_bfd_section
)
6528 int retval
= sec_index
;
6530 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6534 if (sec_index
== SHN_BAD
)
6535 bfd_set_error (bfd_error_nonrepresentable_section
);
6540 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6544 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6546 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6548 flagword flags
= asym_ptr
->flags
;
6550 /* When gas creates relocations against local labels, it creates its
6551 own symbol for the section, but does put the symbol into the
6552 symbol chain, so udata is 0. When the linker is generating
6553 relocatable output, this section symbol may be for one of the
6554 input sections rather than the output section. */
6555 if (asym_ptr
->udata
.i
== 0
6556 && (flags
& BSF_SECTION_SYM
)
6557 && asym_ptr
->section
)
6562 sec
= asym_ptr
->section
;
6563 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6564 sec
= sec
->output_section
;
6565 if (sec
->owner
== abfd
6566 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6567 && elf_section_syms (abfd
)[indx
] != NULL
)
6568 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6571 idx
= asym_ptr
->udata
.i
;
6575 /* This case can occur when using --strip-symbol on a symbol
6576 which is used in a relocation entry. */
6578 /* xgettext:c-format */
6579 (_("%pB: symbol `%s' required but not present"),
6580 abfd
, bfd_asymbol_name (asym_ptr
));
6581 bfd_set_error (bfd_error_no_symbols
);
6588 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6589 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6597 /* Rewrite program header information. */
6600 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6602 Elf_Internal_Ehdr
*iehdr
;
6603 struct elf_segment_map
*map
;
6604 struct elf_segment_map
*map_first
;
6605 struct elf_segment_map
**pointer_to_map
;
6606 Elf_Internal_Phdr
*segment
;
6609 unsigned int num_segments
;
6610 bfd_boolean phdr_included
= FALSE
;
6611 bfd_boolean p_paddr_valid
;
6612 bfd_vma maxpagesize
;
6613 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6614 unsigned int phdr_adjust_num
= 0;
6615 const struct elf_backend_data
*bed
;
6617 bed
= get_elf_backend_data (ibfd
);
6618 iehdr
= elf_elfheader (ibfd
);
6621 pointer_to_map
= &map_first
;
6623 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6624 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6626 /* Returns the end address of the segment + 1. */
6627 #define SEGMENT_END(segment, start) \
6628 (start + (segment->p_memsz > segment->p_filesz \
6629 ? segment->p_memsz : segment->p_filesz))
6631 #define SECTION_SIZE(section, segment) \
6632 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6633 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6634 ? section->size : 0)
6636 /* Returns TRUE if the given section is contained within
6637 the given segment. VMA addresses are compared. */
6638 #define IS_CONTAINED_BY_VMA(section, segment) \
6639 (section->vma >= segment->p_vaddr \
6640 && (section->vma + SECTION_SIZE (section, segment) \
6641 <= (SEGMENT_END (segment, segment->p_vaddr))))
6643 /* Returns TRUE if the given section is contained within
6644 the given segment. LMA addresses are compared. */
6645 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6646 (section->lma >= base \
6647 && (section->lma + SECTION_SIZE (section, segment) \
6648 <= SEGMENT_END (segment, base)))
6650 /* Handle PT_NOTE segment. */
6651 #define IS_NOTE(p, s) \
6652 (p->p_type == PT_NOTE \
6653 && elf_section_type (s) == SHT_NOTE \
6654 && (bfd_vma) s->filepos >= p->p_offset \
6655 && ((bfd_vma) s->filepos + s->size \
6656 <= p->p_offset + p->p_filesz))
6658 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6660 #define IS_COREFILE_NOTE(p, s) \
6662 && bfd_get_format (ibfd) == bfd_core \
6666 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6667 linker, which generates a PT_INTERP section with p_vaddr and
6668 p_memsz set to 0. */
6669 #define IS_SOLARIS_PT_INTERP(p, s) \
6671 && p->p_paddr == 0 \
6672 && p->p_memsz == 0 \
6673 && p->p_filesz > 0 \
6674 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6676 && (bfd_vma) s->filepos >= p->p_offset \
6677 && ((bfd_vma) s->filepos + s->size \
6678 <= p->p_offset + p->p_filesz))
6680 /* Decide if the given section should be included in the given segment.
6681 A section will be included if:
6682 1. It is within the address space of the segment -- we use the LMA
6683 if that is set for the segment and the VMA otherwise,
6684 2. It is an allocated section or a NOTE section in a PT_NOTE
6686 3. There is an output section associated with it,
6687 4. The section has not already been allocated to a previous segment.
6688 5. PT_GNU_STACK segments do not include any sections.
6689 6. PT_TLS segment includes only SHF_TLS sections.
6690 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6691 8. PT_DYNAMIC should not contain empty sections at the beginning
6692 (with the possible exception of .dynamic). */
6693 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6694 ((((segment->p_paddr \
6695 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6696 : IS_CONTAINED_BY_VMA (section, segment)) \
6697 && (section->flags & SEC_ALLOC) != 0) \
6698 || IS_NOTE (segment, section)) \
6699 && segment->p_type != PT_GNU_STACK \
6700 && (segment->p_type != PT_TLS \
6701 || (section->flags & SEC_THREAD_LOCAL)) \
6702 && (segment->p_type == PT_LOAD \
6703 || segment->p_type == PT_TLS \
6704 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6705 && (segment->p_type != PT_DYNAMIC \
6706 || SECTION_SIZE (section, segment) > 0 \
6707 || (segment->p_paddr \
6708 ? segment->p_paddr != section->lma \
6709 : segment->p_vaddr != section->vma) \
6710 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6712 && (segment->p_type != PT_LOAD || !section->segment_mark))
6714 /* If the output section of a section in the input segment is NULL,
6715 it is removed from the corresponding output segment. */
6716 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6717 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6718 && section->output_section != NULL)
6720 /* Returns TRUE iff seg1 starts after the end of seg2. */
6721 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6722 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6724 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6725 their VMA address ranges and their LMA address ranges overlap.
6726 It is possible to have overlapping VMA ranges without overlapping LMA
6727 ranges. RedBoot images for example can have both .data and .bss mapped
6728 to the same VMA range, but with the .data section mapped to a different
6730 #define SEGMENT_OVERLAPS(seg1, seg2) \
6731 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6732 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6733 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6734 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6736 /* Initialise the segment mark field. */
6737 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6738 section
->segment_mark
= FALSE
;
6740 /* The Solaris linker creates program headers in which all the
6741 p_paddr fields are zero. When we try to objcopy or strip such a
6742 file, we get confused. Check for this case, and if we find it
6743 don't set the p_paddr_valid fields. */
6744 p_paddr_valid
= FALSE
;
6745 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6748 if (segment
->p_paddr
!= 0)
6750 p_paddr_valid
= TRUE
;
6754 /* Scan through the segments specified in the program header
6755 of the input BFD. For this first scan we look for overlaps
6756 in the loadable segments. These can be created by weird
6757 parameters to objcopy. Also, fix some solaris weirdness. */
6758 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6763 Elf_Internal_Phdr
*segment2
;
6765 if (segment
->p_type
== PT_INTERP
)
6766 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6767 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6769 /* Mininal change so that the normal section to segment
6770 assignment code will work. */
6771 segment
->p_vaddr
= section
->vma
;
6775 if (segment
->p_type
!= PT_LOAD
)
6777 /* Remove PT_GNU_RELRO segment. */
6778 if (segment
->p_type
== PT_GNU_RELRO
)
6779 segment
->p_type
= PT_NULL
;
6783 /* Determine if this segment overlaps any previous segments. */
6784 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6786 bfd_signed_vma extra_length
;
6788 if (segment2
->p_type
!= PT_LOAD
6789 || !SEGMENT_OVERLAPS (segment
, segment2
))
6792 /* Merge the two segments together. */
6793 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6795 /* Extend SEGMENT2 to include SEGMENT and then delete
6797 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6798 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6800 if (extra_length
> 0)
6802 segment2
->p_memsz
+= extra_length
;
6803 segment2
->p_filesz
+= extra_length
;
6806 segment
->p_type
= PT_NULL
;
6808 /* Since we have deleted P we must restart the outer loop. */
6810 segment
= elf_tdata (ibfd
)->phdr
;
6815 /* Extend SEGMENT to include SEGMENT2 and then delete
6817 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6818 - SEGMENT_END (segment
, segment
->p_vaddr
));
6820 if (extra_length
> 0)
6822 segment
->p_memsz
+= extra_length
;
6823 segment
->p_filesz
+= extra_length
;
6826 segment2
->p_type
= PT_NULL
;
6831 /* The second scan attempts to assign sections to segments. */
6832 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6836 unsigned int section_count
;
6837 asection
**sections
;
6838 asection
*output_section
;
6840 asection
*matching_lma
;
6841 asection
*suggested_lma
;
6844 asection
*first_section
;
6846 if (segment
->p_type
== PT_NULL
)
6849 first_section
= NULL
;
6850 /* Compute how many sections might be placed into this segment. */
6851 for (section
= ibfd
->sections
, section_count
= 0;
6853 section
= section
->next
)
6855 /* Find the first section in the input segment, which may be
6856 removed from the corresponding output segment. */
6857 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6859 if (first_section
== NULL
)
6860 first_section
= section
;
6861 if (section
->output_section
!= NULL
)
6866 /* Allocate a segment map big enough to contain
6867 all of the sections we have selected. */
6868 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
6869 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
6870 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6874 /* Initialise the fields of the segment map. Default to
6875 using the physical address of the segment in the input BFD. */
6877 map
->p_type
= segment
->p_type
;
6878 map
->p_flags
= segment
->p_flags
;
6879 map
->p_flags_valid
= 1;
6881 /* If the first section in the input segment is removed, there is
6882 no need to preserve segment physical address in the corresponding
6884 if (!first_section
|| first_section
->output_section
!= NULL
)
6886 map
->p_paddr
= segment
->p_paddr
;
6887 map
->p_paddr_valid
= p_paddr_valid
;
6890 /* Determine if this segment contains the ELF file header
6891 and if it contains the program headers themselves. */
6892 map
->includes_filehdr
= (segment
->p_offset
== 0
6893 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6894 map
->includes_phdrs
= 0;
6896 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6898 map
->includes_phdrs
=
6899 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6900 && (segment
->p_offset
+ segment
->p_filesz
6901 >= ((bfd_vma
) iehdr
->e_phoff
6902 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6904 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6905 phdr_included
= TRUE
;
6908 if (section_count
== 0)
6910 /* Special segments, such as the PT_PHDR segment, may contain
6911 no sections, but ordinary, loadable segments should contain
6912 something. They are allowed by the ELF spec however, so only
6913 a warning is produced.
6914 There is however the valid use case of embedded systems which
6915 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6916 flash memory with zeros. No warning is shown for that case. */
6917 if (segment
->p_type
== PT_LOAD
6918 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
6919 /* xgettext:c-format */
6921 (_("%pB: warning: empty loadable segment detected"
6922 " at vaddr=%#" PRIx64
", is this intentional?"),
6923 ibfd
, (uint64_t) segment
->p_vaddr
);
6925 map
->p_vaddr_offset
= segment
->p_vaddr
;
6927 *pointer_to_map
= map
;
6928 pointer_to_map
= &map
->next
;
6933 /* Now scan the sections in the input BFD again and attempt
6934 to add their corresponding output sections to the segment map.
6935 The problem here is how to handle an output section which has
6936 been moved (ie had its LMA changed). There are four possibilities:
6938 1. None of the sections have been moved.
6939 In this case we can continue to use the segment LMA from the
6942 2. All of the sections have been moved by the same amount.
6943 In this case we can change the segment's LMA to match the LMA
6944 of the first section.
6946 3. Some of the sections have been moved, others have not.
6947 In this case those sections which have not been moved can be
6948 placed in the current segment which will have to have its size,
6949 and possibly its LMA changed, and a new segment or segments will
6950 have to be created to contain the other sections.
6952 4. The sections have been moved, but not by the same amount.
6953 In this case we can change the segment's LMA to match the LMA
6954 of the first section and we will have to create a new segment
6955 or segments to contain the other sections.
6957 In order to save time, we allocate an array to hold the section
6958 pointers that we are interested in. As these sections get assigned
6959 to a segment, they are removed from this array. */
6961 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6962 if (sections
== NULL
)
6965 /* Step One: Scan for segment vs section LMA conflicts.
6966 Also add the sections to the section array allocated above.
6967 Also add the sections to the current segment. In the common
6968 case, where the sections have not been moved, this means that
6969 we have completely filled the segment, and there is nothing
6972 matching_lma
= NULL
;
6973 suggested_lma
= NULL
;
6975 for (section
= first_section
, j
= 0;
6977 section
= section
->next
)
6979 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6981 output_section
= section
->output_section
;
6983 sections
[j
++] = section
;
6985 /* The Solaris native linker always sets p_paddr to 0.
6986 We try to catch that case here, and set it to the
6987 correct value. Note - some backends require that
6988 p_paddr be left as zero. */
6990 && segment
->p_vaddr
!= 0
6991 && !bed
->want_p_paddr_set_to_zero
6993 && output_section
->lma
!= 0
6994 && (align_power (segment
->p_vaddr
6995 + (map
->includes_filehdr
6996 ? iehdr
->e_ehsize
: 0)
6997 + (map
->includes_phdrs
6998 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7000 output_section
->alignment_power
)
7001 == output_section
->vma
))
7002 map
->p_paddr
= segment
->p_vaddr
;
7004 /* Match up the physical address of the segment with the
7005 LMA address of the output section. */
7006 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7007 || IS_COREFILE_NOTE (segment
, section
)
7008 || (bed
->want_p_paddr_set_to_zero
7009 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
7011 if (matching_lma
== NULL
7012 || output_section
->lma
< matching_lma
->lma
)
7013 matching_lma
= output_section
;
7015 /* We assume that if the section fits within the segment
7016 then it does not overlap any other section within that
7018 map
->sections
[isec
++] = output_section
;
7020 else if (suggested_lma
== NULL
)
7021 suggested_lma
= output_section
;
7023 if (j
== section_count
)
7028 BFD_ASSERT (j
== section_count
);
7030 /* Step Two: Adjust the physical address of the current segment,
7032 if (isec
== section_count
)
7034 /* All of the sections fitted within the segment as currently
7035 specified. This is the default case. Add the segment to
7036 the list of built segments and carry on to process the next
7037 program header in the input BFD. */
7038 map
->count
= section_count
;
7039 *pointer_to_map
= map
;
7040 pointer_to_map
= &map
->next
;
7043 && !bed
->want_p_paddr_set_to_zero
7044 && matching_lma
->lma
!= map
->p_paddr
7045 && !map
->includes_filehdr
7046 && !map
->includes_phdrs
)
7047 /* There is some padding before the first section in the
7048 segment. So, we must account for that in the output
7050 map
->p_vaddr_offset
= map
->p_paddr
- matching_lma
->lma
;
7057 /* Change the current segment's physical address to match
7058 the LMA of the first section that fitted, or if no
7059 section fitted, the first section. */
7060 if (matching_lma
== NULL
)
7061 matching_lma
= suggested_lma
;
7063 map
->p_paddr
= matching_lma
->lma
;
7065 /* Offset the segment physical address from the lma
7066 to allow for space taken up by elf headers. */
7067 if (map
->includes_phdrs
)
7069 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7071 /* iehdr->e_phnum is just an estimate of the number
7072 of program headers that we will need. Make a note
7073 here of the number we used and the segment we chose
7074 to hold these headers, so that we can adjust the
7075 offset when we know the correct value. */
7076 phdr_adjust_num
= iehdr
->e_phnum
;
7077 phdr_adjust_seg
= map
;
7080 if (map
->includes_filehdr
)
7082 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7083 map
->p_paddr
-= iehdr
->e_ehsize
;
7084 /* We've subtracted off the size of headers from the
7085 first section lma, but there may have been some
7086 alignment padding before that section too. Try to
7087 account for that by adjusting the segment lma down to
7088 the same alignment. */
7089 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7090 align
= segment
->p_align
;
7091 map
->p_paddr
&= -align
;
7095 /* Step Three: Loop over the sections again, this time assigning
7096 those that fit to the current segment and removing them from the
7097 sections array; but making sure not to leave large gaps. Once all
7098 possible sections have been assigned to the current segment it is
7099 added to the list of built segments and if sections still remain
7100 to be assigned, a new segment is constructed before repeating
7106 suggested_lma
= NULL
;
7108 /* Fill the current segment with sections that fit. */
7109 for (j
= 0; j
< section_count
; j
++)
7111 section
= sections
[j
];
7113 if (section
== NULL
)
7116 output_section
= section
->output_section
;
7118 BFD_ASSERT (output_section
!= NULL
);
7120 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7121 || IS_COREFILE_NOTE (segment
, section
))
7123 if (map
->count
== 0)
7125 /* If the first section in a segment does not start at
7126 the beginning of the segment, then something is
7128 if (align_power (map
->p_paddr
7129 + (map
->includes_filehdr
7130 ? iehdr
->e_ehsize
: 0)
7131 + (map
->includes_phdrs
7132 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7134 output_section
->alignment_power
)
7135 != output_section
->lma
)
7142 prev_sec
= map
->sections
[map
->count
- 1];
7144 /* If the gap between the end of the previous section
7145 and the start of this section is more than
7146 maxpagesize then we need to start a new segment. */
7147 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7149 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7150 || (prev_sec
->lma
+ prev_sec
->size
7151 > output_section
->lma
))
7153 if (suggested_lma
== NULL
)
7154 suggested_lma
= output_section
;
7160 map
->sections
[map
->count
++] = output_section
;
7163 if (segment
->p_type
== PT_LOAD
)
7164 section
->segment_mark
= TRUE
;
7166 else if (suggested_lma
== NULL
)
7167 suggested_lma
= output_section
;
7170 BFD_ASSERT (map
->count
> 0);
7172 /* Add the current segment to the list of built segments. */
7173 *pointer_to_map
= map
;
7174 pointer_to_map
= &map
->next
;
7176 if (isec
< section_count
)
7178 /* We still have not allocated all of the sections to
7179 segments. Create a new segment here, initialise it
7180 and carry on looping. */
7181 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7182 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
7183 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7190 /* Initialise the fields of the segment map. Set the physical
7191 physical address to the LMA of the first section that has
7192 not yet been assigned. */
7194 map
->p_type
= segment
->p_type
;
7195 map
->p_flags
= segment
->p_flags
;
7196 map
->p_flags_valid
= 1;
7197 map
->p_paddr
= suggested_lma
->lma
;
7198 map
->p_paddr_valid
= p_paddr_valid
;
7199 map
->includes_filehdr
= 0;
7200 map
->includes_phdrs
= 0;
7203 while (isec
< section_count
);
7208 elf_seg_map (obfd
) = map_first
;
7210 /* If we had to estimate the number of program headers that were
7211 going to be needed, then check our estimate now and adjust
7212 the offset if necessary. */
7213 if (phdr_adjust_seg
!= NULL
)
7217 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7220 if (count
> phdr_adjust_num
)
7221 phdr_adjust_seg
->p_paddr
7222 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7224 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7225 if (map
->p_type
== PT_PHDR
)
7228 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7229 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7236 #undef IS_CONTAINED_BY_VMA
7237 #undef IS_CONTAINED_BY_LMA
7239 #undef IS_COREFILE_NOTE
7240 #undef IS_SOLARIS_PT_INTERP
7241 #undef IS_SECTION_IN_INPUT_SEGMENT
7242 #undef INCLUDE_SECTION_IN_SEGMENT
7243 #undef SEGMENT_AFTER_SEGMENT
7244 #undef SEGMENT_OVERLAPS
7248 /* Copy ELF program header information. */
7251 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7253 Elf_Internal_Ehdr
*iehdr
;
7254 struct elf_segment_map
*map
;
7255 struct elf_segment_map
*map_first
;
7256 struct elf_segment_map
**pointer_to_map
;
7257 Elf_Internal_Phdr
*segment
;
7259 unsigned int num_segments
;
7260 bfd_boolean phdr_included
= FALSE
;
7261 bfd_boolean p_paddr_valid
;
7263 iehdr
= elf_elfheader (ibfd
);
7266 pointer_to_map
= &map_first
;
7268 /* If all the segment p_paddr fields are zero, don't set
7269 map->p_paddr_valid. */
7270 p_paddr_valid
= FALSE
;
7271 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7272 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7275 if (segment
->p_paddr
!= 0)
7277 p_paddr_valid
= TRUE
;
7281 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7286 unsigned int section_count
;
7288 Elf_Internal_Shdr
*this_hdr
;
7289 asection
*first_section
= NULL
;
7290 asection
*lowest_section
;
7291 bfd_boolean no_contents
= TRUE
;
7293 /* Compute how many sections are in this segment. */
7294 for (section
= ibfd
->sections
, section_count
= 0;
7296 section
= section
->next
)
7298 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7299 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7301 if (first_section
== NULL
)
7302 first_section
= section
;
7303 if (elf_section_type (section
) != SHT_NOBITS
)
7304 no_contents
= FALSE
;
7309 /* Allocate a segment map big enough to contain
7310 all of the sections we have selected. */
7311 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7312 amt
+= (bfd_size_type
) section_count
* sizeof (asection
*);
7313 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7317 /* Initialize the fields of the output segment map with the
7320 map
->p_type
= segment
->p_type
;
7321 map
->p_flags
= segment
->p_flags
;
7322 map
->p_flags_valid
= 1;
7323 map
->p_paddr
= segment
->p_paddr
;
7324 map
->p_paddr_valid
= p_paddr_valid
;
7325 map
->p_align
= segment
->p_align
;
7326 map
->p_align_valid
= 1;
7327 map
->p_vaddr_offset
= 0;
7329 if (map
->p_type
== PT_GNU_RELRO
7330 || map
->p_type
== PT_GNU_STACK
)
7332 /* The PT_GNU_RELRO segment may contain the first a few
7333 bytes in the .got.plt section even if the whole .got.plt
7334 section isn't in the PT_GNU_RELRO segment. We won't
7335 change the size of the PT_GNU_RELRO segment.
7336 Similarly, PT_GNU_STACK size is significant on uclinux
7338 map
->p_size
= segment
->p_memsz
;
7339 map
->p_size_valid
= 1;
7342 /* Determine if this segment contains the ELF file header
7343 and if it contains the program headers themselves. */
7344 map
->includes_filehdr
= (segment
->p_offset
== 0
7345 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7347 map
->includes_phdrs
= 0;
7348 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7350 map
->includes_phdrs
=
7351 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7352 && (segment
->p_offset
+ segment
->p_filesz
7353 >= ((bfd_vma
) iehdr
->e_phoff
7354 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7356 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7357 phdr_included
= TRUE
;
7360 lowest_section
= NULL
;
7361 if (section_count
!= 0)
7363 unsigned int isec
= 0;
7365 for (section
= first_section
;
7367 section
= section
->next
)
7369 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7370 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7372 map
->sections
[isec
++] = section
->output_section
;
7373 if ((section
->flags
& SEC_ALLOC
) != 0)
7377 if (lowest_section
== NULL
7378 || section
->lma
< lowest_section
->lma
)
7379 lowest_section
= section
;
7381 /* Section lmas are set up from PT_LOAD header
7382 p_paddr in _bfd_elf_make_section_from_shdr.
7383 If this header has a p_paddr that disagrees
7384 with the section lma, flag the p_paddr as
7386 if ((section
->flags
& SEC_LOAD
) != 0)
7387 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7389 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7390 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7391 map
->p_paddr_valid
= FALSE
;
7393 if (isec
== section_count
)
7399 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7401 /* Try to keep the space used by the headers plus any
7402 padding fixed. If there are sections with file contents
7403 in this segment then the lowest sh_offset is the best
7404 guess. Otherwise the segment only has file contents for
7405 the headers, and p_filesz is the best guess. */
7407 map
->header_size
= segment
->p_filesz
;
7409 map
->header_size
= lowest_section
->filepos
;
7412 if (section_count
== 0)
7413 map
->p_vaddr_offset
= segment
->p_vaddr
;
7414 else if (!map
->includes_phdrs
7415 && !map
->includes_filehdr
7416 && map
->p_paddr_valid
)
7417 /* Account for padding before the first section. */
7418 map
->p_vaddr_offset
= (segment
->p_paddr
7419 - (lowest_section
? lowest_section
->lma
: 0));
7421 map
->count
= section_count
;
7422 *pointer_to_map
= map
;
7423 pointer_to_map
= &map
->next
;
7426 elf_seg_map (obfd
) = map_first
;
7430 /* Copy private BFD data. This copies or rewrites ELF program header
7434 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7436 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7437 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7440 if (elf_tdata (ibfd
)->phdr
== NULL
)
7443 if (ibfd
->xvec
== obfd
->xvec
)
7445 /* Check to see if any sections in the input BFD
7446 covered by ELF program header have changed. */
7447 Elf_Internal_Phdr
*segment
;
7448 asection
*section
, *osec
;
7449 unsigned int i
, num_segments
;
7450 Elf_Internal_Shdr
*this_hdr
;
7451 const struct elf_backend_data
*bed
;
7453 bed
= get_elf_backend_data (ibfd
);
7455 /* Regenerate the segment map if p_paddr is set to 0. */
7456 if (bed
->want_p_paddr_set_to_zero
)
7459 /* Initialize the segment mark field. */
7460 for (section
= obfd
->sections
; section
!= NULL
;
7461 section
= section
->next
)
7462 section
->segment_mark
= FALSE
;
7464 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7465 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7469 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7470 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7471 which severly confuses things, so always regenerate the segment
7472 map in this case. */
7473 if (segment
->p_paddr
== 0
7474 && segment
->p_memsz
== 0
7475 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7478 for (section
= ibfd
->sections
;
7479 section
!= NULL
; section
= section
->next
)
7481 /* We mark the output section so that we know it comes
7482 from the input BFD. */
7483 osec
= section
->output_section
;
7485 osec
->segment_mark
= TRUE
;
7487 /* Check if this section is covered by the segment. */
7488 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7489 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7491 /* FIXME: Check if its output section is changed or
7492 removed. What else do we need to check? */
7494 || section
->flags
!= osec
->flags
7495 || section
->lma
!= osec
->lma
7496 || section
->vma
!= osec
->vma
7497 || section
->size
!= osec
->size
7498 || section
->rawsize
!= osec
->rawsize
7499 || section
->alignment_power
!= osec
->alignment_power
)
7505 /* Check to see if any output section do not come from the
7507 for (section
= obfd
->sections
; section
!= NULL
;
7508 section
= section
->next
)
7510 if (!section
->segment_mark
)
7513 section
->segment_mark
= FALSE
;
7516 return copy_elf_program_header (ibfd
, obfd
);
7520 if (ibfd
->xvec
== obfd
->xvec
)
7522 /* When rewriting program header, set the output maxpagesize to
7523 the maximum alignment of input PT_LOAD segments. */
7524 Elf_Internal_Phdr
*segment
;
7526 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7527 bfd_vma maxpagesize
= 0;
7529 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7532 if (segment
->p_type
== PT_LOAD
7533 && maxpagesize
< segment
->p_align
)
7535 /* PR 17512: file: f17299af. */
7536 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7537 /* xgettext:c-format */
7538 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7539 PRIx64
" is too large"),
7540 ibfd
, (uint64_t) segment
->p_align
);
7542 maxpagesize
= segment
->p_align
;
7545 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7546 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7549 return rewrite_elf_program_header (ibfd
, obfd
);
7552 /* Initialize private output section information from input section. */
7555 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7559 struct bfd_link_info
*link_info
)
7562 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7563 bfd_boolean final_link
= (link_info
!= NULL
7564 && !bfd_link_relocatable (link_info
));
7566 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7567 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7570 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7572 /* For objcopy and relocatable link, don't copy the output ELF
7573 section type from input if the output BFD section flags have been
7574 set to something different. For a final link allow some flags
7575 that the linker clears to differ. */
7576 if (elf_section_type (osec
) == SHT_NULL
7577 && (osec
->flags
== isec
->flags
7579 && ((osec
->flags
^ isec
->flags
)
7580 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7581 elf_section_type (osec
) = elf_section_type (isec
);
7583 /* FIXME: Is this correct for all OS/PROC specific flags? */
7584 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7585 & (SHF_MASKOS
| SHF_MASKPROC
));
7587 /* Copy sh_info from input for mbind section. */
7588 if (elf_section_flags (isec
) & SHF_GNU_MBIND
)
7589 elf_section_data (osec
)->this_hdr
.sh_info
7590 = elf_section_data (isec
)->this_hdr
.sh_info
;
7592 /* Set things up for objcopy and relocatable link. The output
7593 SHT_GROUP section will have its elf_next_in_group pointing back
7594 to the input group members. Ignore linker created group section.
7595 See elfNN_ia64_object_p in elfxx-ia64.c. */
7596 if ((link_info
== NULL
7597 || !link_info
->resolve_section_groups
)
7598 && (elf_sec_group (isec
) == NULL
7599 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7601 if (elf_section_flags (isec
) & SHF_GROUP
)
7602 elf_section_flags (osec
) |= SHF_GROUP
;
7603 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7604 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7607 /* If not decompress, preserve SHF_COMPRESSED. */
7608 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7609 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7612 ihdr
= &elf_section_data (isec
)->this_hdr
;
7614 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7615 don't use the output section of the linked-to section since it
7616 may be NULL at this point. */
7617 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7619 ohdr
= &elf_section_data (osec
)->this_hdr
;
7620 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7621 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7624 osec
->use_rela_p
= isec
->use_rela_p
;
7629 /* Copy private section information. This copies over the entsize
7630 field, and sometimes the info field. */
7633 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7638 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7640 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7641 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7644 ihdr
= &elf_section_data (isec
)->this_hdr
;
7645 ohdr
= &elf_section_data (osec
)->this_hdr
;
7647 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7649 if (ihdr
->sh_type
== SHT_SYMTAB
7650 || ihdr
->sh_type
== SHT_DYNSYM
7651 || ihdr
->sh_type
== SHT_GNU_verneed
7652 || ihdr
->sh_type
== SHT_GNU_verdef
)
7653 ohdr
->sh_info
= ihdr
->sh_info
;
7655 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7659 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7660 necessary if we are removing either the SHT_GROUP section or any of
7661 the group member sections. DISCARDED is the value that a section's
7662 output_section has if the section will be discarded, NULL when this
7663 function is called from objcopy, bfd_abs_section_ptr when called
7667 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7671 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7672 if (elf_section_type (isec
) == SHT_GROUP
)
7674 asection
*first
= elf_next_in_group (isec
);
7675 asection
*s
= first
;
7676 bfd_size_type removed
= 0;
7680 /* If this member section is being output but the
7681 SHT_GROUP section is not, then clear the group info
7682 set up by _bfd_elf_copy_private_section_data. */
7683 if (s
->output_section
!= discarded
7684 && isec
->output_section
== discarded
)
7686 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7687 elf_group_name (s
->output_section
) = NULL
;
7689 /* Conversely, if the member section is not being output
7690 but the SHT_GROUP section is, then adjust its size. */
7691 else if (s
->output_section
== discarded
7692 && isec
->output_section
!= discarded
)
7694 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7696 if (elf_sec
->rel
.hdr
!= NULL
7697 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7699 if (elf_sec
->rela
.hdr
!= NULL
7700 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7703 s
= elf_next_in_group (s
);
7709 if (discarded
!= NULL
)
7711 /* If we've been called for ld -r, then we need to
7712 adjust the input section size. */
7713 if (isec
->rawsize
== 0)
7714 isec
->rawsize
= isec
->size
;
7715 isec
->size
= isec
->rawsize
- removed
;
7716 if (isec
->size
<= 4)
7719 isec
->flags
|= SEC_EXCLUDE
;
7724 /* Adjust the output section size when called from
7726 isec
->output_section
->size
-= removed
;
7727 if (isec
->output_section
->size
<= 4)
7729 isec
->output_section
->size
= 0;
7730 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7739 /* Copy private header information. */
7742 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7744 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7745 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7748 /* Copy over private BFD data if it has not already been copied.
7749 This must be done here, rather than in the copy_private_bfd_data
7750 entry point, because the latter is called after the section
7751 contents have been set, which means that the program headers have
7752 already been worked out. */
7753 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7755 if (! copy_private_bfd_data (ibfd
, obfd
))
7759 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7762 /* Copy private symbol information. If this symbol is in a section
7763 which we did not map into a BFD section, try to map the section
7764 index correctly. We use special macro definitions for the mapped
7765 section indices; these definitions are interpreted by the
7766 swap_out_syms function. */
7768 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7769 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7770 #define MAP_STRTAB (SHN_HIOS + 3)
7771 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7772 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7775 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7780 elf_symbol_type
*isym
, *osym
;
7782 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7783 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7786 isym
= elf_symbol_from (ibfd
, isymarg
);
7787 osym
= elf_symbol_from (obfd
, osymarg
);
7790 && isym
->internal_elf_sym
.st_shndx
!= 0
7792 && bfd_is_abs_section (isym
->symbol
.section
))
7796 shndx
= isym
->internal_elf_sym
.st_shndx
;
7797 if (shndx
== elf_onesymtab (ibfd
))
7798 shndx
= MAP_ONESYMTAB
;
7799 else if (shndx
== elf_dynsymtab (ibfd
))
7800 shndx
= MAP_DYNSYMTAB
;
7801 else if (shndx
== elf_strtab_sec (ibfd
))
7803 else if (shndx
== elf_shstrtab_sec (ibfd
))
7804 shndx
= MAP_SHSTRTAB
;
7805 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7806 shndx
= MAP_SYM_SHNDX
;
7807 osym
->internal_elf_sym
.st_shndx
= shndx
;
7813 /* Swap out the symbols. */
7816 swap_out_syms (bfd
*abfd
,
7817 struct elf_strtab_hash
**sttp
,
7820 const struct elf_backend_data
*bed
;
7823 struct elf_strtab_hash
*stt
;
7824 Elf_Internal_Shdr
*symtab_hdr
;
7825 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7826 Elf_Internal_Shdr
*symstrtab_hdr
;
7827 struct elf_sym_strtab
*symstrtab
;
7828 bfd_byte
*outbound_syms
;
7829 bfd_byte
*outbound_shndx
;
7830 unsigned long outbound_syms_index
;
7831 unsigned long outbound_shndx_index
;
7833 unsigned int num_locals
;
7835 bfd_boolean name_local_sections
;
7837 if (!elf_map_symbols (abfd
, &num_locals
))
7840 /* Dump out the symtabs. */
7841 stt
= _bfd_elf_strtab_init ();
7845 bed
= get_elf_backend_data (abfd
);
7846 symcount
= bfd_get_symcount (abfd
);
7847 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7848 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7849 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7850 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7851 symtab_hdr
->sh_info
= num_locals
+ 1;
7852 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7854 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7855 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7857 /* Allocate buffer to swap out the .strtab section. */
7858 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7859 * sizeof (*symstrtab
));
7860 if (symstrtab
== NULL
)
7862 _bfd_elf_strtab_free (stt
);
7866 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7867 bed
->s
->sizeof_sym
);
7868 if (outbound_syms
== NULL
)
7871 _bfd_elf_strtab_free (stt
);
7875 symtab_hdr
->contents
= outbound_syms
;
7876 outbound_syms_index
= 0;
7878 outbound_shndx
= NULL
;
7879 outbound_shndx_index
= 0;
7881 if (elf_symtab_shndx_list (abfd
))
7883 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7884 if (symtab_shndx_hdr
->sh_name
!= 0)
7886 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7887 outbound_shndx
= (bfd_byte
*)
7888 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7889 if (outbound_shndx
== NULL
)
7892 symtab_shndx_hdr
->contents
= outbound_shndx
;
7893 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7894 symtab_shndx_hdr
->sh_size
= amt
;
7895 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7896 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7898 /* FIXME: What about any other headers in the list ? */
7901 /* Now generate the data (for "contents"). */
7903 /* Fill in zeroth symbol and swap it out. */
7904 Elf_Internal_Sym sym
;
7910 sym
.st_shndx
= SHN_UNDEF
;
7911 sym
.st_target_internal
= 0;
7912 symstrtab
[0].sym
= sym
;
7913 symstrtab
[0].dest_index
= outbound_syms_index
;
7914 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7915 outbound_syms_index
++;
7916 if (outbound_shndx
!= NULL
)
7917 outbound_shndx_index
++;
7921 = (bed
->elf_backend_name_local_section_symbols
7922 && bed
->elf_backend_name_local_section_symbols (abfd
));
7924 syms
= bfd_get_outsymbols (abfd
);
7925 for (idx
= 0; idx
< symcount
;)
7927 Elf_Internal_Sym sym
;
7928 bfd_vma value
= syms
[idx
]->value
;
7929 elf_symbol_type
*type_ptr
;
7930 flagword flags
= syms
[idx
]->flags
;
7933 if (!name_local_sections
7934 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7936 /* Local section symbols have no name. */
7937 sym
.st_name
= (unsigned long) -1;
7941 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7942 to get the final offset for st_name. */
7944 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7946 if (sym
.st_name
== (unsigned long) -1)
7950 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7952 if ((flags
& BSF_SECTION_SYM
) == 0
7953 && bfd_is_com_section (syms
[idx
]->section
))
7955 /* ELF common symbols put the alignment into the `value' field,
7956 and the size into the `size' field. This is backwards from
7957 how BFD handles it, so reverse it here. */
7958 sym
.st_size
= value
;
7959 if (type_ptr
== NULL
7960 || type_ptr
->internal_elf_sym
.st_value
== 0)
7961 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7963 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7964 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7965 (abfd
, syms
[idx
]->section
);
7969 asection
*sec
= syms
[idx
]->section
;
7972 if (sec
->output_section
)
7974 value
+= sec
->output_offset
;
7975 sec
= sec
->output_section
;
7978 /* Don't add in the section vma for relocatable output. */
7979 if (! relocatable_p
)
7981 sym
.st_value
= value
;
7982 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7984 if (bfd_is_abs_section (sec
)
7986 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7988 /* This symbol is in a real ELF section which we did
7989 not create as a BFD section. Undo the mapping done
7990 by copy_private_symbol_data. */
7991 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7995 shndx
= elf_onesymtab (abfd
);
7998 shndx
= elf_dynsymtab (abfd
);
8001 shndx
= elf_strtab_sec (abfd
);
8004 shndx
= elf_shstrtab_sec (abfd
);
8007 if (elf_symtab_shndx_list (abfd
))
8008 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8017 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8019 if (shndx
== SHN_BAD
)
8023 /* Writing this would be a hell of a lot easier if
8024 we had some decent documentation on bfd, and
8025 knew what to expect of the library, and what to
8026 demand of applications. For example, it
8027 appears that `objcopy' might not set the
8028 section of a symbol to be a section that is
8029 actually in the output file. */
8030 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8032 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8033 if (shndx
== SHN_BAD
)
8035 /* xgettext:c-format */
8037 (_("unable to find equivalent output section"
8038 " for symbol '%s' from section '%s'"),
8039 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8041 bfd_set_error (bfd_error_invalid_operation
);
8047 sym
.st_shndx
= shndx
;
8050 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8052 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8053 type
= STT_GNU_IFUNC
;
8054 else if ((flags
& BSF_FUNCTION
) != 0)
8056 else if ((flags
& BSF_OBJECT
) != 0)
8058 else if ((flags
& BSF_RELC
) != 0)
8060 else if ((flags
& BSF_SRELC
) != 0)
8065 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8068 /* Processor-specific types. */
8069 if (type_ptr
!= NULL
8070 && bed
->elf_backend_get_symbol_type
)
8071 type
= ((*bed
->elf_backend_get_symbol_type
)
8072 (&type_ptr
->internal_elf_sym
, type
));
8074 if (flags
& BSF_SECTION_SYM
)
8076 if (flags
& BSF_GLOBAL
)
8077 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8079 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8081 else if (bfd_is_com_section (syms
[idx
]->section
))
8083 if (type
!= STT_TLS
)
8085 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8086 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8087 ? STT_COMMON
: STT_OBJECT
);
8089 type
= ((flags
& BSF_ELF_COMMON
) != 0
8090 ? STT_COMMON
: STT_OBJECT
);
8092 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8094 else if (bfd_is_und_section (syms
[idx
]->section
))
8095 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8099 else if (flags
& BSF_FILE
)
8100 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8103 int bind
= STB_LOCAL
;
8105 if (flags
& BSF_LOCAL
)
8107 else if (flags
& BSF_GNU_UNIQUE
)
8108 bind
= STB_GNU_UNIQUE
;
8109 else if (flags
& BSF_WEAK
)
8111 else if (flags
& BSF_GLOBAL
)
8114 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8117 if (type_ptr
!= NULL
)
8119 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8120 sym
.st_target_internal
8121 = type_ptr
->internal_elf_sym
.st_target_internal
;
8126 sym
.st_target_internal
= 0;
8130 symstrtab
[idx
].sym
= sym
;
8131 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8132 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8134 outbound_syms_index
++;
8135 if (outbound_shndx
!= NULL
)
8136 outbound_shndx_index
++;
8139 /* Finalize the .strtab section. */
8140 _bfd_elf_strtab_finalize (stt
);
8142 /* Swap out the .strtab section. */
8143 for (idx
= 0; idx
<= symcount
; idx
++)
8145 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8146 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8147 elfsym
->sym
.st_name
= 0;
8149 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8150 elfsym
->sym
.st_name
);
8151 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8153 + (elfsym
->dest_index
8154 * bed
->s
->sizeof_sym
)),
8156 + (elfsym
->destshndx_index
8157 * sizeof (Elf_External_Sym_Shndx
))));
8162 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8163 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8164 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8165 symstrtab_hdr
->sh_addr
= 0;
8166 symstrtab_hdr
->sh_entsize
= 0;
8167 symstrtab_hdr
->sh_link
= 0;
8168 symstrtab_hdr
->sh_info
= 0;
8169 symstrtab_hdr
->sh_addralign
= 1;
8174 /* Return the number of bytes required to hold the symtab vector.
8176 Note that we base it on the count plus 1, since we will null terminate
8177 the vector allocated based on this size. However, the ELF symbol table
8178 always has a dummy entry as symbol #0, so it ends up even. */
8181 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8185 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8187 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8188 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8190 symtab_size
-= sizeof (asymbol
*);
8196 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8200 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8202 if (elf_dynsymtab (abfd
) == 0)
8204 bfd_set_error (bfd_error_invalid_operation
);
8208 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8209 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8211 symtab_size
-= sizeof (asymbol
*);
8217 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8220 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8223 /* Canonicalize the relocs. */
8226 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8233 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8235 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8238 tblptr
= section
->relocation
;
8239 for (i
= 0; i
< section
->reloc_count
; i
++)
8240 *relptr
++ = tblptr
++;
8244 return section
->reloc_count
;
8248 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8250 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8251 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8254 bfd_get_symcount (abfd
) = symcount
;
8259 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8260 asymbol
**allocation
)
8262 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8263 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8266 bfd_get_dynamic_symcount (abfd
) = symcount
;
8270 /* Return the size required for the dynamic reloc entries. Any loadable
8271 section that was actually installed in the BFD, and has type SHT_REL
8272 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8273 dynamic reloc section. */
8276 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8281 if (elf_dynsymtab (abfd
) == 0)
8283 bfd_set_error (bfd_error_invalid_operation
);
8287 ret
= sizeof (arelent
*);
8288 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8289 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8290 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8291 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8292 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
8293 * sizeof (arelent
*));
8298 /* Canonicalize the dynamic relocation entries. Note that we return the
8299 dynamic relocations as a single block, although they are actually
8300 associated with particular sections; the interface, which was
8301 designed for SunOS style shared libraries, expects that there is only
8302 one set of dynamic relocs. Any loadable section that was actually
8303 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8304 dynamic symbol table, is considered to be a dynamic reloc section. */
8307 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8311 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8315 if (elf_dynsymtab (abfd
) == 0)
8317 bfd_set_error (bfd_error_invalid_operation
);
8321 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8323 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8325 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8326 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8327 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8332 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8334 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8336 for (i
= 0; i
< count
; i
++)
8347 /* Read in the version information. */
8350 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8352 bfd_byte
*contents
= NULL
;
8353 unsigned int freeidx
= 0;
8355 if (elf_dynverref (abfd
) != 0)
8357 Elf_Internal_Shdr
*hdr
;
8358 Elf_External_Verneed
*everneed
;
8359 Elf_Internal_Verneed
*iverneed
;
8361 bfd_byte
*contents_end
;
8363 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8365 if (hdr
->sh_info
== 0
8366 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8368 error_return_bad_verref
:
8370 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8371 bfd_set_error (bfd_error_bad_value
);
8372 error_return_verref
:
8373 elf_tdata (abfd
)->verref
= NULL
;
8374 elf_tdata (abfd
)->cverrefs
= 0;
8378 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8379 if (contents
== NULL
)
8380 goto error_return_verref
;
8382 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8383 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8384 goto error_return_verref
;
8386 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8387 bfd_alloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8389 if (elf_tdata (abfd
)->verref
== NULL
)
8390 goto error_return_verref
;
8392 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8393 == sizeof (Elf_External_Vernaux
));
8394 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8395 everneed
= (Elf_External_Verneed
*) contents
;
8396 iverneed
= elf_tdata (abfd
)->verref
;
8397 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8399 Elf_External_Vernaux
*evernaux
;
8400 Elf_Internal_Vernaux
*ivernaux
;
8403 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8405 iverneed
->vn_bfd
= abfd
;
8407 iverneed
->vn_filename
=
8408 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8410 if (iverneed
->vn_filename
== NULL
)
8411 goto error_return_bad_verref
;
8413 if (iverneed
->vn_cnt
== 0)
8414 iverneed
->vn_auxptr
= NULL
;
8417 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8418 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8419 sizeof (Elf_Internal_Vernaux
));
8420 if (iverneed
->vn_auxptr
== NULL
)
8421 goto error_return_verref
;
8424 if (iverneed
->vn_aux
8425 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8426 goto error_return_bad_verref
;
8428 evernaux
= ((Elf_External_Vernaux
*)
8429 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8430 ivernaux
= iverneed
->vn_auxptr
;
8431 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8433 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8435 ivernaux
->vna_nodename
=
8436 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8437 ivernaux
->vna_name
);
8438 if (ivernaux
->vna_nodename
== NULL
)
8439 goto error_return_bad_verref
;
8441 if (ivernaux
->vna_other
> freeidx
)
8442 freeidx
= ivernaux
->vna_other
;
8444 ivernaux
->vna_nextptr
= NULL
;
8445 if (ivernaux
->vna_next
== 0)
8447 iverneed
->vn_cnt
= j
+ 1;
8450 if (j
+ 1 < iverneed
->vn_cnt
)
8451 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8453 if (ivernaux
->vna_next
8454 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8455 goto error_return_bad_verref
;
8457 evernaux
= ((Elf_External_Vernaux
*)
8458 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8461 iverneed
->vn_nextref
= NULL
;
8462 if (iverneed
->vn_next
== 0)
8464 if (i
+ 1 < hdr
->sh_info
)
8465 iverneed
->vn_nextref
= iverneed
+ 1;
8467 if (iverneed
->vn_next
8468 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8469 goto error_return_bad_verref
;
8471 everneed
= ((Elf_External_Verneed
*)
8472 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8474 elf_tdata (abfd
)->cverrefs
= i
;
8480 if (elf_dynverdef (abfd
) != 0)
8482 Elf_Internal_Shdr
*hdr
;
8483 Elf_External_Verdef
*everdef
;
8484 Elf_Internal_Verdef
*iverdef
;
8485 Elf_Internal_Verdef
*iverdefarr
;
8486 Elf_Internal_Verdef iverdefmem
;
8488 unsigned int maxidx
;
8489 bfd_byte
*contents_end_def
, *contents_end_aux
;
8491 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8493 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8495 error_return_bad_verdef
:
8497 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8498 bfd_set_error (bfd_error_bad_value
);
8499 error_return_verdef
:
8500 elf_tdata (abfd
)->verdef
= NULL
;
8501 elf_tdata (abfd
)->cverdefs
= 0;
8505 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8506 if (contents
== NULL
)
8507 goto error_return_verdef
;
8508 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8509 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8510 goto error_return_verdef
;
8512 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8513 >= sizeof (Elf_External_Verdaux
));
8514 contents_end_def
= contents
+ hdr
->sh_size
8515 - sizeof (Elf_External_Verdef
);
8516 contents_end_aux
= contents
+ hdr
->sh_size
8517 - sizeof (Elf_External_Verdaux
);
8519 /* We know the number of entries in the section but not the maximum
8520 index. Therefore we have to run through all entries and find
8522 everdef
= (Elf_External_Verdef
*) contents
;
8524 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8526 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8528 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8529 goto error_return_bad_verdef
;
8530 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8531 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8533 if (iverdefmem
.vd_next
== 0)
8536 if (iverdefmem
.vd_next
8537 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8538 goto error_return_bad_verdef
;
8540 everdef
= ((Elf_External_Verdef
*)
8541 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8544 if (default_imported_symver
)
8546 if (freeidx
> maxidx
)
8552 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8553 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8554 if (elf_tdata (abfd
)->verdef
== NULL
)
8555 goto error_return_verdef
;
8557 elf_tdata (abfd
)->cverdefs
= maxidx
;
8559 everdef
= (Elf_External_Verdef
*) contents
;
8560 iverdefarr
= elf_tdata (abfd
)->verdef
;
8561 for (i
= 0; i
< hdr
->sh_info
; i
++)
8563 Elf_External_Verdaux
*everdaux
;
8564 Elf_Internal_Verdaux
*iverdaux
;
8567 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8569 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8570 goto error_return_bad_verdef
;
8572 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8573 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8575 iverdef
->vd_bfd
= abfd
;
8577 if (iverdef
->vd_cnt
== 0)
8578 iverdef
->vd_auxptr
= NULL
;
8581 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8582 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8583 sizeof (Elf_Internal_Verdaux
));
8584 if (iverdef
->vd_auxptr
== NULL
)
8585 goto error_return_verdef
;
8589 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8590 goto error_return_bad_verdef
;
8592 everdaux
= ((Elf_External_Verdaux
*)
8593 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8594 iverdaux
= iverdef
->vd_auxptr
;
8595 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8597 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8599 iverdaux
->vda_nodename
=
8600 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8601 iverdaux
->vda_name
);
8602 if (iverdaux
->vda_nodename
== NULL
)
8603 goto error_return_bad_verdef
;
8605 iverdaux
->vda_nextptr
= NULL
;
8606 if (iverdaux
->vda_next
== 0)
8608 iverdef
->vd_cnt
= j
+ 1;
8611 if (j
+ 1 < iverdef
->vd_cnt
)
8612 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8614 if (iverdaux
->vda_next
8615 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8616 goto error_return_bad_verdef
;
8618 everdaux
= ((Elf_External_Verdaux
*)
8619 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8622 iverdef
->vd_nodename
= NULL
;
8623 if (iverdef
->vd_cnt
)
8624 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8626 iverdef
->vd_nextdef
= NULL
;
8627 if (iverdef
->vd_next
== 0)
8629 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8630 iverdef
->vd_nextdef
= iverdef
+ 1;
8632 everdef
= ((Elf_External_Verdef
*)
8633 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8639 else if (default_imported_symver
)
8646 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8647 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8648 if (elf_tdata (abfd
)->verdef
== NULL
)
8651 elf_tdata (abfd
)->cverdefs
= freeidx
;
8654 /* Create a default version based on the soname. */
8655 if (default_imported_symver
)
8657 Elf_Internal_Verdef
*iverdef
;
8658 Elf_Internal_Verdaux
*iverdaux
;
8660 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8662 iverdef
->vd_version
= VER_DEF_CURRENT
;
8663 iverdef
->vd_flags
= 0;
8664 iverdef
->vd_ndx
= freeidx
;
8665 iverdef
->vd_cnt
= 1;
8667 iverdef
->vd_bfd
= abfd
;
8669 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8670 if (iverdef
->vd_nodename
== NULL
)
8671 goto error_return_verdef
;
8672 iverdef
->vd_nextdef
= NULL
;
8673 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8674 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8675 if (iverdef
->vd_auxptr
== NULL
)
8676 goto error_return_verdef
;
8678 iverdaux
= iverdef
->vd_auxptr
;
8679 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8685 if (contents
!= NULL
)
8691 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8693 elf_symbol_type
*newsym
;
8695 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8698 newsym
->symbol
.the_bfd
= abfd
;
8699 return &newsym
->symbol
;
8703 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8707 bfd_symbol_info (symbol
, ret
);
8710 /* Return whether a symbol name implies a local symbol. Most targets
8711 use this function for the is_local_label_name entry point, but some
8715 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8718 /* Normal local symbols start with ``.L''. */
8719 if (name
[0] == '.' && name
[1] == 'L')
8722 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8723 DWARF debugging symbols starting with ``..''. */
8724 if (name
[0] == '.' && name
[1] == '.')
8727 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8728 emitting DWARF debugging output. I suspect this is actually a
8729 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8730 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8731 underscore to be emitted on some ELF targets). For ease of use,
8732 we treat such symbols as local. */
8733 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8736 /* Treat assembler generated fake symbols, dollar local labels and
8737 forward-backward labels (aka local labels) as locals.
8738 These labels have the form:
8740 L0^A.* (fake symbols)
8742 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8744 Versions which start with .L will have already been matched above,
8745 so we only need to match the rest. */
8746 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8748 bfd_boolean ret
= FALSE
;
8752 for (p
= name
+ 2; (c
= *p
); p
++)
8754 if (c
== 1 || c
== 2)
8756 if (c
== 1 && p
== name
+ 2)
8757 /* A fake symbol. */
8760 /* FIXME: We are being paranoid here and treating symbols like
8761 L0^Bfoo as if there were non-local, on the grounds that the
8762 assembler will never generate them. But can any symbol
8763 containing an ASCII value in the range 1-31 ever be anything
8764 other than some kind of local ? */
8781 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8782 asymbol
*symbol ATTRIBUTE_UNUSED
)
8789 _bfd_elf_set_arch_mach (bfd
*abfd
,
8790 enum bfd_architecture arch
,
8791 unsigned long machine
)
8793 /* If this isn't the right architecture for this backend, and this
8794 isn't the generic backend, fail. */
8795 if (arch
!= get_elf_backend_data (abfd
)->arch
8796 && arch
!= bfd_arch_unknown
8797 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8800 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8803 /* Find the nearest line to a particular section and offset,
8804 for error reporting. */
8807 _bfd_elf_find_nearest_line (bfd
*abfd
,
8811 const char **filename_ptr
,
8812 const char **functionname_ptr
,
8813 unsigned int *line_ptr
,
8814 unsigned int *discriminator_ptr
)
8818 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8819 filename_ptr
, functionname_ptr
,
8820 line_ptr
, discriminator_ptr
,
8821 dwarf_debug_sections
, 0,
8822 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8823 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8824 filename_ptr
, functionname_ptr
,
8827 if (!*functionname_ptr
)
8828 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8829 *filename_ptr
? NULL
: filename_ptr
,
8834 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8835 &found
, filename_ptr
,
8836 functionname_ptr
, line_ptr
,
8837 &elf_tdata (abfd
)->line_info
))
8839 if (found
&& (*functionname_ptr
|| *line_ptr
))
8842 if (symbols
== NULL
)
8845 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8846 filename_ptr
, functionname_ptr
))
8853 /* Find the line for a symbol. */
8856 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8857 const char **filename_ptr
, unsigned int *line_ptr
)
8859 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8860 filename_ptr
, NULL
, line_ptr
, NULL
,
8861 dwarf_debug_sections
, 0,
8862 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8865 /* After a call to bfd_find_nearest_line, successive calls to
8866 bfd_find_inliner_info can be used to get source information about
8867 each level of function inlining that terminated at the address
8868 passed to bfd_find_nearest_line. Currently this is only supported
8869 for DWARF2 with appropriate DWARF3 extensions. */
8872 _bfd_elf_find_inliner_info (bfd
*abfd
,
8873 const char **filename_ptr
,
8874 const char **functionname_ptr
,
8875 unsigned int *line_ptr
)
8878 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8879 functionname_ptr
, line_ptr
,
8880 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8885 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8887 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8888 int ret
= bed
->s
->sizeof_ehdr
;
8890 if (!bfd_link_relocatable (info
))
8892 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8894 if (phdr_size
== (bfd_size_type
) -1)
8896 struct elf_segment_map
*m
;
8899 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8900 phdr_size
+= bed
->s
->sizeof_phdr
;
8903 phdr_size
= get_program_header_size (abfd
, info
);
8906 elf_program_header_size (abfd
) = phdr_size
;
8914 _bfd_elf_set_section_contents (bfd
*abfd
,
8916 const void *location
,
8918 bfd_size_type count
)
8920 Elf_Internal_Shdr
*hdr
;
8923 if (! abfd
->output_has_begun
8924 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8930 hdr
= &elf_section_data (section
)->this_hdr
;
8931 if (hdr
->sh_offset
== (file_ptr
) -1)
8933 /* We must compress this section. Write output to the buffer. */
8934 unsigned char *contents
= hdr
->contents
;
8935 if ((offset
+ count
) > hdr
->sh_size
8936 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8937 || contents
== NULL
)
8939 memcpy (contents
+ offset
, location
, count
);
8942 pos
= hdr
->sh_offset
+ offset
;
8943 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8944 || bfd_bwrite (location
, count
, abfd
) != count
)
8951 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8952 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8953 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8959 /* Try to convert a non-ELF reloc into an ELF one. */
8962 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8964 /* Check whether we really have an ELF howto. */
8966 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8968 bfd_reloc_code_real_type code
;
8969 reloc_howto_type
*howto
;
8971 /* Alien reloc: Try to determine its type to replace it with an
8972 equivalent ELF reloc. */
8974 if (areloc
->howto
->pc_relative
)
8976 switch (areloc
->howto
->bitsize
)
8979 code
= BFD_RELOC_8_PCREL
;
8982 code
= BFD_RELOC_12_PCREL
;
8985 code
= BFD_RELOC_16_PCREL
;
8988 code
= BFD_RELOC_24_PCREL
;
8991 code
= BFD_RELOC_32_PCREL
;
8994 code
= BFD_RELOC_64_PCREL
;
9000 howto
= bfd_reloc_type_lookup (abfd
, code
);
9002 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9004 if (howto
->pcrel_offset
)
9005 areloc
->addend
+= areloc
->address
;
9007 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9012 switch (areloc
->howto
->bitsize
)
9018 code
= BFD_RELOC_14
;
9021 code
= BFD_RELOC_16
;
9024 code
= BFD_RELOC_26
;
9027 code
= BFD_RELOC_32
;
9030 code
= BFD_RELOC_64
;
9036 howto
= bfd_reloc_type_lookup (abfd
, code
);
9040 areloc
->howto
= howto
;
9048 /* xgettext:c-format */
9049 _bfd_error_handler (_("%pB: %s unsupported"),
9050 abfd
, areloc
->howto
->name
);
9051 bfd_set_error (bfd_error_bad_value
);
9056 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9058 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9059 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9061 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9062 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9063 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9066 return _bfd_generic_close_and_cleanup (abfd
);
9069 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9070 in the relocation's offset. Thus we cannot allow any sort of sanity
9071 range-checking to interfere. There is nothing else to do in processing
9074 bfd_reloc_status_type
9075 _bfd_elf_rel_vtable_reloc_fn
9076 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9077 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9078 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9079 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9081 return bfd_reloc_ok
;
9084 /* Elf core file support. Much of this only works on native
9085 toolchains, since we rely on knowing the
9086 machine-dependent procfs structure in order to pick
9087 out details about the corefile. */
9089 #ifdef HAVE_SYS_PROCFS_H
9090 /* Needed for new procfs interface on sparc-solaris. */
9091 # define _STRUCTURED_PROC 1
9092 # include <sys/procfs.h>
9095 /* Return a PID that identifies a "thread" for threaded cores, or the
9096 PID of the main process for non-threaded cores. */
9099 elfcore_make_pid (bfd
*abfd
)
9103 pid
= elf_tdata (abfd
)->core
->lwpid
;
9105 pid
= elf_tdata (abfd
)->core
->pid
;
9110 /* If there isn't a section called NAME, make one, using
9111 data from SECT. Note, this function will generate a
9112 reference to NAME, so you shouldn't deallocate or
9116 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9120 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9123 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9127 sect2
->size
= sect
->size
;
9128 sect2
->filepos
= sect
->filepos
;
9129 sect2
->alignment_power
= sect
->alignment_power
;
9133 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9134 actually creates up to two pseudosections:
9135 - For the single-threaded case, a section named NAME, unless
9136 such a section already exists.
9137 - For the multi-threaded case, a section named "NAME/PID", where
9138 PID is elfcore_make_pid (abfd).
9139 Both pseudosections have identical contents. */
9141 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9147 char *threaded_name
;
9151 /* Build the section name. */
9153 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9154 len
= strlen (buf
) + 1;
9155 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9156 if (threaded_name
== NULL
)
9158 memcpy (threaded_name
, buf
, len
);
9160 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9165 sect
->filepos
= filepos
;
9166 sect
->alignment_power
= 2;
9168 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9171 /* prstatus_t exists on:
9173 linux 2.[01] + glibc
9177 #if defined (HAVE_PRSTATUS_T)
9180 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9185 if (note
->descsz
== sizeof (prstatus_t
))
9189 size
= sizeof (prstat
.pr_reg
);
9190 offset
= offsetof (prstatus_t
, pr_reg
);
9191 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9193 /* Do not overwrite the core signal if it
9194 has already been set by another thread. */
9195 if (elf_tdata (abfd
)->core
->signal
== 0)
9196 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9197 if (elf_tdata (abfd
)->core
->pid
== 0)
9198 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9200 /* pr_who exists on:
9203 pr_who doesn't exist on:
9206 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9207 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9209 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9212 #if defined (HAVE_PRSTATUS32_T)
9213 else if (note
->descsz
== sizeof (prstatus32_t
))
9215 /* 64-bit host, 32-bit corefile */
9216 prstatus32_t prstat
;
9218 size
= sizeof (prstat
.pr_reg
);
9219 offset
= offsetof (prstatus32_t
, pr_reg
);
9220 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9222 /* Do not overwrite the core signal if it
9223 has already been set by another thread. */
9224 if (elf_tdata (abfd
)->core
->signal
== 0)
9225 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9226 if (elf_tdata (abfd
)->core
->pid
== 0)
9227 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9229 /* pr_who exists on:
9232 pr_who doesn't exist on:
9235 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9236 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9238 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9241 #endif /* HAVE_PRSTATUS32_T */
9244 /* Fail - we don't know how to handle any other
9245 note size (ie. data object type). */
9249 /* Make a ".reg/999" section and a ".reg" section. */
9250 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9251 size
, note
->descpos
+ offset
);
9253 #endif /* defined (HAVE_PRSTATUS_T) */
9255 /* Create a pseudosection containing the exact contents of NOTE. */
9257 elfcore_make_note_pseudosection (bfd
*abfd
,
9259 Elf_Internal_Note
*note
)
9261 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9262 note
->descsz
, note
->descpos
);
9265 /* There isn't a consistent prfpregset_t across platforms,
9266 but it doesn't matter, because we don't have to pick this
9267 data structure apart. */
9270 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9272 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9275 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9276 type of NT_PRXFPREG. Just include the whole note's contents
9280 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9282 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9285 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9286 with a note type of NT_X86_XSTATE. Just include the whole note's
9287 contents literally. */
9290 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9292 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9296 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9298 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9302 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9304 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9308 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9310 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9314 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9316 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9320 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9322 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9326 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9328 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9332 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9334 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9338 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9340 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9344 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9346 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9350 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9352 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9356 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9358 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9362 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9364 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9368 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9370 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9374 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9376 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9380 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9382 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9386 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9388 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9392 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9394 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9398 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9400 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9404 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9406 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9410 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9412 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9416 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9418 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9422 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9424 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9428 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9430 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9434 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9436 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9440 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9442 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9446 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9448 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9452 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9454 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9458 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9460 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9464 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9466 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9470 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9472 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9476 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9478 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9482 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9484 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9488 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9490 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9493 #if defined (HAVE_PRPSINFO_T)
9494 typedef prpsinfo_t elfcore_psinfo_t
;
9495 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9496 typedef prpsinfo32_t elfcore_psinfo32_t
;
9500 #if defined (HAVE_PSINFO_T)
9501 typedef psinfo_t elfcore_psinfo_t
;
9502 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9503 typedef psinfo32_t elfcore_psinfo32_t
;
9507 /* return a malloc'ed copy of a string at START which is at
9508 most MAX bytes long, possibly without a terminating '\0'.
9509 the copy will always have a terminating '\0'. */
9512 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9515 char *end
= (char *) memchr (start
, '\0', max
);
9523 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9527 memcpy (dups
, start
, len
);
9533 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9535 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9537 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9539 elfcore_psinfo_t psinfo
;
9541 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9543 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9544 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9546 elf_tdata (abfd
)->core
->program
9547 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9548 sizeof (psinfo
.pr_fname
));
9550 elf_tdata (abfd
)->core
->command
9551 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9552 sizeof (psinfo
.pr_psargs
));
9554 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9555 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9557 /* 64-bit host, 32-bit corefile */
9558 elfcore_psinfo32_t psinfo
;
9560 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9562 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9563 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9565 elf_tdata (abfd
)->core
->program
9566 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9567 sizeof (psinfo
.pr_fname
));
9569 elf_tdata (abfd
)->core
->command
9570 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9571 sizeof (psinfo
.pr_psargs
));
9577 /* Fail - we don't know how to handle any other
9578 note size (ie. data object type). */
9582 /* Note that for some reason, a spurious space is tacked
9583 onto the end of the args in some (at least one anyway)
9584 implementations, so strip it off if it exists. */
9587 char *command
= elf_tdata (abfd
)->core
->command
;
9588 int n
= strlen (command
);
9590 if (0 < n
&& command
[n
- 1] == ' ')
9591 command
[n
- 1] = '\0';
9596 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9598 #if defined (HAVE_PSTATUS_T)
9600 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9602 if (note
->descsz
== sizeof (pstatus_t
)
9603 #if defined (HAVE_PXSTATUS_T)
9604 || note
->descsz
== sizeof (pxstatus_t
)
9610 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9612 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9614 #if defined (HAVE_PSTATUS32_T)
9615 else if (note
->descsz
== sizeof (pstatus32_t
))
9617 /* 64-bit host, 32-bit corefile */
9620 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9622 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9625 /* Could grab some more details from the "representative"
9626 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9627 NT_LWPSTATUS note, presumably. */
9631 #endif /* defined (HAVE_PSTATUS_T) */
9633 #if defined (HAVE_LWPSTATUS_T)
9635 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9637 lwpstatus_t lwpstat
;
9643 if (note
->descsz
!= sizeof (lwpstat
)
9644 #if defined (HAVE_LWPXSTATUS_T)
9645 && note
->descsz
!= sizeof (lwpxstatus_t
)
9650 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9652 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9653 /* Do not overwrite the core signal if it has already been set by
9655 if (elf_tdata (abfd
)->core
->signal
== 0)
9656 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9658 /* Make a ".reg/999" section. */
9660 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9661 len
= strlen (buf
) + 1;
9662 name
= bfd_alloc (abfd
, len
);
9665 memcpy (name
, buf
, len
);
9667 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9671 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9672 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9673 sect
->filepos
= note
->descpos
9674 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9677 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9678 sect
->size
= sizeof (lwpstat
.pr_reg
);
9679 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9682 sect
->alignment_power
= 2;
9684 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9687 /* Make a ".reg2/999" section */
9689 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9690 len
= strlen (buf
) + 1;
9691 name
= bfd_alloc (abfd
, len
);
9694 memcpy (name
, buf
, len
);
9696 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9700 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9701 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9702 sect
->filepos
= note
->descpos
9703 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9706 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9707 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9708 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9711 sect
->alignment_power
= 2;
9713 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9715 #endif /* defined (HAVE_LWPSTATUS_T) */
9718 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9725 int is_active_thread
;
9728 if (note
->descsz
< 728)
9731 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9734 type
= bfd_get_32 (abfd
, note
->descdata
);
9738 case 1 /* NOTE_INFO_PROCESS */:
9739 /* FIXME: need to add ->core->command. */
9740 /* process_info.pid */
9741 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9742 /* process_info.signal */
9743 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9746 case 2 /* NOTE_INFO_THREAD */:
9747 /* Make a ".reg/999" section. */
9748 /* thread_info.tid */
9749 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9751 len
= strlen (buf
) + 1;
9752 name
= (char *) bfd_alloc (abfd
, len
);
9756 memcpy (name
, buf
, len
);
9758 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9762 /* sizeof (thread_info.thread_context) */
9764 /* offsetof (thread_info.thread_context) */
9765 sect
->filepos
= note
->descpos
+ 12;
9766 sect
->alignment_power
= 2;
9768 /* thread_info.is_active_thread */
9769 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9771 if (is_active_thread
)
9772 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9776 case 3 /* NOTE_INFO_MODULE */:
9777 /* Make a ".module/xxxxxxxx" section. */
9778 /* module_info.base_address */
9779 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9780 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9782 len
= strlen (buf
) + 1;
9783 name
= (char *) bfd_alloc (abfd
, len
);
9787 memcpy (name
, buf
, len
);
9789 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9794 sect
->size
= note
->descsz
;
9795 sect
->filepos
= note
->descpos
;
9796 sect
->alignment_power
= 2;
9807 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9809 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9817 if (bed
->elf_backend_grok_prstatus
)
9818 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9820 #if defined (HAVE_PRSTATUS_T)
9821 return elfcore_grok_prstatus (abfd
, note
);
9826 #if defined (HAVE_PSTATUS_T)
9828 return elfcore_grok_pstatus (abfd
, note
);
9831 #if defined (HAVE_LWPSTATUS_T)
9833 return elfcore_grok_lwpstatus (abfd
, note
);
9836 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9837 return elfcore_grok_prfpreg (abfd
, note
);
9839 case NT_WIN32PSTATUS
:
9840 return elfcore_grok_win32pstatus (abfd
, note
);
9842 case NT_PRXFPREG
: /* Linux SSE extension */
9843 if (note
->namesz
== 6
9844 && strcmp (note
->namedata
, "LINUX") == 0)
9845 return elfcore_grok_prxfpreg (abfd
, note
);
9849 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9850 if (note
->namesz
== 6
9851 && strcmp (note
->namedata
, "LINUX") == 0)
9852 return elfcore_grok_xstatereg (abfd
, note
);
9857 if (note
->namesz
== 6
9858 && strcmp (note
->namedata
, "LINUX") == 0)
9859 return elfcore_grok_ppc_vmx (abfd
, note
);
9864 if (note
->namesz
== 6
9865 && strcmp (note
->namedata
, "LINUX") == 0)
9866 return elfcore_grok_ppc_vsx (abfd
, note
);
9871 if (note
->namesz
== 6
9872 && strcmp (note
->namedata
, "LINUX") == 0)
9873 return elfcore_grok_ppc_tar (abfd
, note
);
9878 if (note
->namesz
== 6
9879 && strcmp (note
->namedata
, "LINUX") == 0)
9880 return elfcore_grok_ppc_ppr (abfd
, note
);
9885 if (note
->namesz
== 6
9886 && strcmp (note
->namedata
, "LINUX") == 0)
9887 return elfcore_grok_ppc_dscr (abfd
, note
);
9892 if (note
->namesz
== 6
9893 && strcmp (note
->namedata
, "LINUX") == 0)
9894 return elfcore_grok_ppc_ebb (abfd
, note
);
9899 if (note
->namesz
== 6
9900 && strcmp (note
->namedata
, "LINUX") == 0)
9901 return elfcore_grok_ppc_pmu (abfd
, note
);
9905 case NT_PPC_TM_CGPR
:
9906 if (note
->namesz
== 6
9907 && strcmp (note
->namedata
, "LINUX") == 0)
9908 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
9912 case NT_PPC_TM_CFPR
:
9913 if (note
->namesz
== 6
9914 && strcmp (note
->namedata
, "LINUX") == 0)
9915 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
9919 case NT_PPC_TM_CVMX
:
9920 if (note
->namesz
== 6
9921 && strcmp (note
->namedata
, "LINUX") == 0)
9922 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
9926 case NT_PPC_TM_CVSX
:
9927 if (note
->namesz
== 6
9928 && strcmp (note
->namedata
, "LINUX") == 0)
9929 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
9934 if (note
->namesz
== 6
9935 && strcmp (note
->namedata
, "LINUX") == 0)
9936 return elfcore_grok_ppc_tm_spr (abfd
, note
);
9940 case NT_PPC_TM_CTAR
:
9941 if (note
->namesz
== 6
9942 && strcmp (note
->namedata
, "LINUX") == 0)
9943 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
9947 case NT_PPC_TM_CPPR
:
9948 if (note
->namesz
== 6
9949 && strcmp (note
->namedata
, "LINUX") == 0)
9950 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
9954 case NT_PPC_TM_CDSCR
:
9955 if (note
->namesz
== 6
9956 && strcmp (note
->namedata
, "LINUX") == 0)
9957 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
9961 case NT_S390_HIGH_GPRS
:
9962 if (note
->namesz
== 6
9963 && strcmp (note
->namedata
, "LINUX") == 0)
9964 return elfcore_grok_s390_high_gprs (abfd
, note
);
9969 if (note
->namesz
== 6
9970 && strcmp (note
->namedata
, "LINUX") == 0)
9971 return elfcore_grok_s390_timer (abfd
, note
);
9975 case NT_S390_TODCMP
:
9976 if (note
->namesz
== 6
9977 && strcmp (note
->namedata
, "LINUX") == 0)
9978 return elfcore_grok_s390_todcmp (abfd
, note
);
9982 case NT_S390_TODPREG
:
9983 if (note
->namesz
== 6
9984 && strcmp (note
->namedata
, "LINUX") == 0)
9985 return elfcore_grok_s390_todpreg (abfd
, note
);
9990 if (note
->namesz
== 6
9991 && strcmp (note
->namedata
, "LINUX") == 0)
9992 return elfcore_grok_s390_ctrs (abfd
, note
);
9996 case NT_S390_PREFIX
:
9997 if (note
->namesz
== 6
9998 && strcmp (note
->namedata
, "LINUX") == 0)
9999 return elfcore_grok_s390_prefix (abfd
, note
);
10003 case NT_S390_LAST_BREAK
:
10004 if (note
->namesz
== 6
10005 && strcmp (note
->namedata
, "LINUX") == 0)
10006 return elfcore_grok_s390_last_break (abfd
, note
);
10010 case NT_S390_SYSTEM_CALL
:
10011 if (note
->namesz
== 6
10012 && strcmp (note
->namedata
, "LINUX") == 0)
10013 return elfcore_grok_s390_system_call (abfd
, note
);
10018 if (note
->namesz
== 6
10019 && strcmp (note
->namedata
, "LINUX") == 0)
10020 return elfcore_grok_s390_tdb (abfd
, note
);
10024 case NT_S390_VXRS_LOW
:
10025 if (note
->namesz
== 6
10026 && strcmp (note
->namedata
, "LINUX") == 0)
10027 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10031 case NT_S390_VXRS_HIGH
:
10032 if (note
->namesz
== 6
10033 && strcmp (note
->namedata
, "LINUX") == 0)
10034 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10038 case NT_S390_GS_CB
:
10039 if (note
->namesz
== 6
10040 && strcmp (note
->namedata
, "LINUX") == 0)
10041 return elfcore_grok_s390_gs_cb (abfd
, note
);
10045 case NT_S390_GS_BC
:
10046 if (note
->namesz
== 6
10047 && strcmp (note
->namedata
, "LINUX") == 0)
10048 return elfcore_grok_s390_gs_bc (abfd
, note
);
10053 if (note
->namesz
== 6
10054 && strcmp (note
->namedata
, "LINUX") == 0)
10055 return elfcore_grok_arm_vfp (abfd
, note
);
10060 if (note
->namesz
== 6
10061 && strcmp (note
->namedata
, "LINUX") == 0)
10062 return elfcore_grok_aarch_tls (abfd
, note
);
10066 case NT_ARM_HW_BREAK
:
10067 if (note
->namesz
== 6
10068 && strcmp (note
->namedata
, "LINUX") == 0)
10069 return elfcore_grok_aarch_hw_break (abfd
, note
);
10073 case NT_ARM_HW_WATCH
:
10074 if (note
->namesz
== 6
10075 && strcmp (note
->namedata
, "LINUX") == 0)
10076 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10081 if (note
->namesz
== 6
10082 && strcmp (note
->namedata
, "LINUX") == 0)
10083 return elfcore_grok_aarch_sve (abfd
, note
);
10089 if (bed
->elf_backend_grok_psinfo
)
10090 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10092 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10093 return elfcore_grok_psinfo (abfd
, note
);
10100 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10105 sect
->size
= note
->descsz
;
10106 sect
->filepos
= note
->descpos
;
10107 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10113 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10117 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10124 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10126 struct bfd_build_id
* build_id
;
10128 if (note
->descsz
== 0)
10131 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10132 if (build_id
== NULL
)
10135 build_id
->size
= note
->descsz
;
10136 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10137 abfd
->build_id
= build_id
;
10143 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10145 switch (note
->type
)
10150 case NT_GNU_PROPERTY_TYPE_0
:
10151 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10153 case NT_GNU_BUILD_ID
:
10154 return elfobj_grok_gnu_build_id (abfd
, note
);
10159 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10161 struct sdt_note
*cur
=
10162 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
10165 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10166 cur
->size
= (bfd_size_type
) note
->descsz
;
10167 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10169 elf_tdata (abfd
)->sdt_note_head
= cur
;
10175 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10177 switch (note
->type
)
10180 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10188 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10192 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10195 if (note
->descsz
< 108)
10200 if (note
->descsz
< 120)
10208 /* Check for version 1 in pr_version. */
10209 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10214 /* Skip over pr_psinfosz. */
10215 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10219 offset
+= 4; /* Padding before pr_psinfosz. */
10223 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10224 elf_tdata (abfd
)->core
->program
10225 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10228 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10229 elf_tdata (abfd
)->core
->command
10230 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10233 /* Padding before pr_pid. */
10236 /* The pr_pid field was added in version "1a". */
10237 if (note
->descsz
< offset
+ 4)
10240 elf_tdata (abfd
)->core
->pid
10241 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10247 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10253 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10254 Also compute minimum size of this note. */
10255 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10259 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10263 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10264 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10271 if (note
->descsz
< min_size
)
10274 /* Check for version 1 in pr_version. */
10275 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10278 /* Extract size of pr_reg from pr_gregsetsz. */
10279 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10280 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10282 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10287 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10291 /* Skip over pr_osreldate. */
10294 /* Read signal from pr_cursig. */
10295 if (elf_tdata (abfd
)->core
->signal
== 0)
10296 elf_tdata (abfd
)->core
->signal
10297 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10300 /* Read TID from pr_pid. */
10301 elf_tdata (abfd
)->core
->lwpid
10302 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10305 /* Padding before pr_reg. */
10306 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10309 /* Make sure that there is enough data remaining in the note. */
10310 if ((note
->descsz
- offset
) < size
)
10313 /* Make a ".reg/999" section and a ".reg" section. */
10314 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10315 size
, note
->descpos
+ offset
);
10319 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10321 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10323 switch (note
->type
)
10326 if (bed
->elf_backend_grok_freebsd_prstatus
)
10327 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10329 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10332 return elfcore_grok_prfpreg (abfd
, note
);
10335 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10337 case NT_FREEBSD_THRMISC
:
10338 if (note
->namesz
== 8)
10339 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10343 case NT_FREEBSD_PROCSTAT_PROC
:
10344 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10347 case NT_FREEBSD_PROCSTAT_FILES
:
10348 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10351 case NT_FREEBSD_PROCSTAT_VMMAP
:
10352 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10355 case NT_FREEBSD_PROCSTAT_AUXV
:
10357 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10362 sect
->size
= note
->descsz
- 4;
10363 sect
->filepos
= note
->descpos
+ 4;
10364 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10369 case NT_X86_XSTATE
:
10370 if (note
->namesz
== 8)
10371 return elfcore_grok_xstatereg (abfd
, note
);
10375 case NT_FREEBSD_PTLWPINFO
:
10376 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10380 return elfcore_grok_arm_vfp (abfd
, note
);
10388 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10392 cp
= strchr (note
->namedata
, '@');
10395 *lwpidp
= atoi(cp
+ 1);
10402 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10404 if (note
->descsz
<= 0x7c + 31)
10407 /* Signal number at offset 0x08. */
10408 elf_tdata (abfd
)->core
->signal
10409 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10411 /* Process ID at offset 0x50. */
10412 elf_tdata (abfd
)->core
->pid
10413 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10415 /* Command name at 0x7c (max 32 bytes, including nul). */
10416 elf_tdata (abfd
)->core
->command
10417 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10419 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10424 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10428 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10429 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10431 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
10433 /* NetBSD-specific core "procinfo". Note that we expect to
10434 find this note before any of the others, which is fine,
10435 since the kernel writes this note out first when it
10436 creates a core file. */
10438 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10441 /* As of Jan 2002 there are no other machine-independent notes
10442 defined for NetBSD core files. If the note type is less
10443 than the start of the machine-dependent note types, we don't
10446 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10450 switch (bfd_get_arch (abfd
))
10452 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10453 PT_GETFPREGS == mach+2. */
10455 case bfd_arch_alpha
:
10456 case bfd_arch_sparc
:
10457 switch (note
->type
)
10459 case NT_NETBSDCORE_FIRSTMACH
+0:
10460 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10462 case NT_NETBSDCORE_FIRSTMACH
+2:
10463 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10469 /* On all other arch's, PT_GETREGS == mach+1 and
10470 PT_GETFPREGS == mach+3. */
10473 switch (note
->type
)
10475 case NT_NETBSDCORE_FIRSTMACH
+1:
10476 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10478 case NT_NETBSDCORE_FIRSTMACH
+3:
10479 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10489 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10491 if (note
->descsz
<= 0x48 + 31)
10494 /* Signal number at offset 0x08. */
10495 elf_tdata (abfd
)->core
->signal
10496 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10498 /* Process ID at offset 0x20. */
10499 elf_tdata (abfd
)->core
->pid
10500 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10502 /* Command name at 0x48 (max 32 bytes, including nul). */
10503 elf_tdata (abfd
)->core
->command
10504 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10510 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10512 if (note
->type
== NT_OPENBSD_PROCINFO
)
10513 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10515 if (note
->type
== NT_OPENBSD_REGS
)
10516 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10518 if (note
->type
== NT_OPENBSD_FPREGS
)
10519 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10521 if (note
->type
== NT_OPENBSD_XFPREGS
)
10522 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10524 if (note
->type
== NT_OPENBSD_AUXV
)
10526 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10531 sect
->size
= note
->descsz
;
10532 sect
->filepos
= note
->descpos
;
10533 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10538 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10540 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10545 sect
->size
= note
->descsz
;
10546 sect
->filepos
= note
->descpos
;
10547 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10556 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10558 void *ddata
= note
->descdata
;
10565 if (note
->descsz
< 16)
10568 /* nto_procfs_status 'pid' field is at offset 0. */
10569 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10571 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10572 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10574 /* nto_procfs_status 'flags' field is at offset 8. */
10575 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10577 /* nto_procfs_status 'what' field is at offset 14. */
10578 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10580 elf_tdata (abfd
)->core
->signal
= sig
;
10581 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10584 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10585 do not come from signals so we make sure we set the current
10586 thread just in case. */
10587 if (flags
& 0x00000080)
10588 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10590 /* Make a ".qnx_core_status/%d" section. */
10591 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10593 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10596 strcpy (name
, buf
);
10598 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10602 sect
->size
= note
->descsz
;
10603 sect
->filepos
= note
->descpos
;
10604 sect
->alignment_power
= 2;
10606 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10610 elfcore_grok_nto_regs (bfd
*abfd
,
10611 Elf_Internal_Note
*note
,
10619 /* Make a "(base)/%d" section. */
10620 sprintf (buf
, "%s/%ld", base
, tid
);
10622 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10625 strcpy (name
, buf
);
10627 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10631 sect
->size
= note
->descsz
;
10632 sect
->filepos
= note
->descpos
;
10633 sect
->alignment_power
= 2;
10635 /* This is the current thread. */
10636 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10637 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10642 #define BFD_QNT_CORE_INFO 7
10643 #define BFD_QNT_CORE_STATUS 8
10644 #define BFD_QNT_CORE_GREG 9
10645 #define BFD_QNT_CORE_FPREG 10
10648 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10650 /* Every GREG section has a STATUS section before it. Store the
10651 tid from the previous call to pass down to the next gregs
10653 static long tid
= 1;
10655 switch (note
->type
)
10657 case BFD_QNT_CORE_INFO
:
10658 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10659 case BFD_QNT_CORE_STATUS
:
10660 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10661 case BFD_QNT_CORE_GREG
:
10662 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10663 case BFD_QNT_CORE_FPREG
:
10664 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10671 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10677 /* Use note name as section name. */
10678 len
= note
->namesz
;
10679 name
= (char *) bfd_alloc (abfd
, len
);
10682 memcpy (name
, note
->namedata
, len
);
10683 name
[len
- 1] = '\0';
10685 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10689 sect
->size
= note
->descsz
;
10690 sect
->filepos
= note
->descpos
;
10691 sect
->alignment_power
= 1;
10696 /* Function: elfcore_write_note
10699 buffer to hold note, and current size of buffer
10703 size of data for note
10705 Writes note to end of buffer. ELF64 notes are written exactly as
10706 for ELF32, despite the current (as of 2006) ELF gabi specifying
10707 that they ought to have 8-byte namesz and descsz field, and have
10708 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10711 Pointer to realloc'd buffer, *BUFSIZ updated. */
10714 elfcore_write_note (bfd
*abfd
,
10722 Elf_External_Note
*xnp
;
10729 namesz
= strlen (name
) + 1;
10731 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10733 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10736 dest
= buf
+ *bufsiz
;
10737 *bufsiz
+= newspace
;
10738 xnp
= (Elf_External_Note
*) dest
;
10739 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10740 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10741 H_PUT_32 (abfd
, type
, xnp
->type
);
10745 memcpy (dest
, name
, namesz
);
10753 memcpy (dest
, input
, size
);
10763 /* gcc-8 warns (*) on all the strncpy calls in this function about
10764 possible string truncation. The "truncation" is not a bug. We
10765 have an external representation of structs with fields that are not
10766 necessarily NULL terminated and corresponding internal
10767 representation fields that are one larger so that they can always
10768 be NULL terminated.
10769 gcc versions between 4.2 and 4.6 do not allow pragma control of
10770 diagnostics inside functions, giving a hard error if you try to use
10771 the finer control available with later versions.
10772 gcc prior to 4.2 warns about diagnostic push and pop.
10773 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10774 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10775 (*) Depending on your system header files! */
10776 #if GCC_VERSION >= 8000
10777 # pragma GCC diagnostic push
10778 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10781 elfcore_write_prpsinfo (bfd
*abfd
,
10785 const char *psargs
)
10787 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10789 if (bed
->elf_backend_write_core_note
!= NULL
)
10792 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10793 NT_PRPSINFO
, fname
, psargs
);
10798 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10799 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10800 if (bed
->s
->elfclass
== ELFCLASS32
)
10802 # if defined (HAVE_PSINFO32_T)
10804 int note_type
= NT_PSINFO
;
10807 int note_type
= NT_PRPSINFO
;
10810 memset (&data
, 0, sizeof (data
));
10811 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10812 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10813 return elfcore_write_note (abfd
, buf
, bufsiz
,
10814 "CORE", note_type
, &data
, sizeof (data
));
10819 # if defined (HAVE_PSINFO_T)
10821 int note_type
= NT_PSINFO
;
10824 int note_type
= NT_PRPSINFO
;
10827 memset (&data
, 0, sizeof (data
));
10828 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10829 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10830 return elfcore_write_note (abfd
, buf
, bufsiz
,
10831 "CORE", note_type
, &data
, sizeof (data
));
10833 #endif /* PSINFO_T or PRPSINFO_T */
10838 #if GCC_VERSION >= 8000
10839 # pragma GCC diagnostic pop
10843 elfcore_write_linux_prpsinfo32
10844 (bfd
*abfd
, char *buf
, int *bufsiz
,
10845 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10847 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
10849 struct elf_external_linux_prpsinfo32_ugid16 data
;
10851 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
10852 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10853 &data
, sizeof (data
));
10857 struct elf_external_linux_prpsinfo32_ugid32 data
;
10859 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
10860 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10861 &data
, sizeof (data
));
10866 elfcore_write_linux_prpsinfo64
10867 (bfd
*abfd
, char *buf
, int *bufsiz
,
10868 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10870 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
10872 struct elf_external_linux_prpsinfo64_ugid16 data
;
10874 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
10875 return elfcore_write_note (abfd
, buf
, bufsiz
,
10876 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10880 struct elf_external_linux_prpsinfo64_ugid32 data
;
10882 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
10883 return elfcore_write_note (abfd
, buf
, bufsiz
,
10884 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10889 elfcore_write_prstatus (bfd
*abfd
,
10896 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10898 if (bed
->elf_backend_write_core_note
!= NULL
)
10901 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10903 pid
, cursig
, gregs
);
10908 #if defined (HAVE_PRSTATUS_T)
10909 #if defined (HAVE_PRSTATUS32_T)
10910 if (bed
->s
->elfclass
== ELFCLASS32
)
10912 prstatus32_t prstat
;
10914 memset (&prstat
, 0, sizeof (prstat
));
10915 prstat
.pr_pid
= pid
;
10916 prstat
.pr_cursig
= cursig
;
10917 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10918 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10919 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10926 memset (&prstat
, 0, sizeof (prstat
));
10927 prstat
.pr_pid
= pid
;
10928 prstat
.pr_cursig
= cursig
;
10929 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10930 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10931 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10933 #endif /* HAVE_PRSTATUS_T */
10939 #if defined (HAVE_LWPSTATUS_T)
10941 elfcore_write_lwpstatus (bfd
*abfd
,
10948 lwpstatus_t lwpstat
;
10949 const char *note_name
= "CORE";
10951 memset (&lwpstat
, 0, sizeof (lwpstat
));
10952 lwpstat
.pr_lwpid
= pid
>> 16;
10953 lwpstat
.pr_cursig
= cursig
;
10954 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10955 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10956 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10957 #if !defined(gregs)
10958 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10959 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10961 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10962 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10965 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10966 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10968 #endif /* HAVE_LWPSTATUS_T */
10970 #if defined (HAVE_PSTATUS_T)
10972 elfcore_write_pstatus (bfd
*abfd
,
10976 int cursig ATTRIBUTE_UNUSED
,
10977 const void *gregs ATTRIBUTE_UNUSED
)
10979 const char *note_name
= "CORE";
10980 #if defined (HAVE_PSTATUS32_T)
10981 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10983 if (bed
->s
->elfclass
== ELFCLASS32
)
10987 memset (&pstat
, 0, sizeof (pstat
));
10988 pstat
.pr_pid
= pid
& 0xffff;
10989 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10990 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10998 memset (&pstat
, 0, sizeof (pstat
));
10999 pstat
.pr_pid
= pid
& 0xffff;
11000 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11001 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11005 #endif /* HAVE_PSTATUS_T */
11008 elfcore_write_prfpreg (bfd
*abfd
,
11011 const void *fpregs
,
11014 const char *note_name
= "CORE";
11015 return elfcore_write_note (abfd
, buf
, bufsiz
,
11016 note_name
, NT_FPREGSET
, fpregs
, size
);
11020 elfcore_write_prxfpreg (bfd
*abfd
,
11023 const void *xfpregs
,
11026 char *note_name
= "LINUX";
11027 return elfcore_write_note (abfd
, buf
, bufsiz
,
11028 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11032 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11033 const void *xfpregs
, int size
)
11036 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11037 note_name
= "FreeBSD";
11039 note_name
= "LINUX";
11040 return elfcore_write_note (abfd
, buf
, bufsiz
,
11041 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11045 elfcore_write_ppc_vmx (bfd
*abfd
,
11048 const void *ppc_vmx
,
11051 char *note_name
= "LINUX";
11052 return elfcore_write_note (abfd
, buf
, bufsiz
,
11053 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11057 elfcore_write_ppc_vsx (bfd
*abfd
,
11060 const void *ppc_vsx
,
11063 char *note_name
= "LINUX";
11064 return elfcore_write_note (abfd
, buf
, bufsiz
,
11065 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11069 elfcore_write_ppc_tar (bfd
*abfd
,
11072 const void *ppc_tar
,
11075 char *note_name
= "LINUX";
11076 return elfcore_write_note (abfd
, buf
, bufsiz
,
11077 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11081 elfcore_write_ppc_ppr (bfd
*abfd
,
11084 const void *ppc_ppr
,
11087 char *note_name
= "LINUX";
11088 return elfcore_write_note (abfd
, buf
, bufsiz
,
11089 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11093 elfcore_write_ppc_dscr (bfd
*abfd
,
11096 const void *ppc_dscr
,
11099 char *note_name
= "LINUX";
11100 return elfcore_write_note (abfd
, buf
, bufsiz
,
11101 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11105 elfcore_write_ppc_ebb (bfd
*abfd
,
11108 const void *ppc_ebb
,
11111 char *note_name
= "LINUX";
11112 return elfcore_write_note (abfd
, buf
, bufsiz
,
11113 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11117 elfcore_write_ppc_pmu (bfd
*abfd
,
11120 const void *ppc_pmu
,
11123 char *note_name
= "LINUX";
11124 return elfcore_write_note (abfd
, buf
, bufsiz
,
11125 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11129 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11132 const void *ppc_tm_cgpr
,
11135 char *note_name
= "LINUX";
11136 return elfcore_write_note (abfd
, buf
, bufsiz
,
11137 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11141 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11144 const void *ppc_tm_cfpr
,
11147 char *note_name
= "LINUX";
11148 return elfcore_write_note (abfd
, buf
, bufsiz
,
11149 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11153 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11156 const void *ppc_tm_cvmx
,
11159 char *note_name
= "LINUX";
11160 return elfcore_write_note (abfd
, buf
, bufsiz
,
11161 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11165 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11168 const void *ppc_tm_cvsx
,
11171 char *note_name
= "LINUX";
11172 return elfcore_write_note (abfd
, buf
, bufsiz
,
11173 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11177 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11180 const void *ppc_tm_spr
,
11183 char *note_name
= "LINUX";
11184 return elfcore_write_note (abfd
, buf
, bufsiz
,
11185 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11189 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11192 const void *ppc_tm_ctar
,
11195 char *note_name
= "LINUX";
11196 return elfcore_write_note (abfd
, buf
, bufsiz
,
11197 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11201 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11204 const void *ppc_tm_cppr
,
11207 char *note_name
= "LINUX";
11208 return elfcore_write_note (abfd
, buf
, bufsiz
,
11209 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11213 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11216 const void *ppc_tm_cdscr
,
11219 char *note_name
= "LINUX";
11220 return elfcore_write_note (abfd
, buf
, bufsiz
,
11221 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11225 elfcore_write_s390_high_gprs (bfd
*abfd
,
11228 const void *s390_high_gprs
,
11231 char *note_name
= "LINUX";
11232 return elfcore_write_note (abfd
, buf
, bufsiz
,
11233 note_name
, NT_S390_HIGH_GPRS
,
11234 s390_high_gprs
, size
);
11238 elfcore_write_s390_timer (bfd
*abfd
,
11241 const void *s390_timer
,
11244 char *note_name
= "LINUX";
11245 return elfcore_write_note (abfd
, buf
, bufsiz
,
11246 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11250 elfcore_write_s390_todcmp (bfd
*abfd
,
11253 const void *s390_todcmp
,
11256 char *note_name
= "LINUX";
11257 return elfcore_write_note (abfd
, buf
, bufsiz
,
11258 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11262 elfcore_write_s390_todpreg (bfd
*abfd
,
11265 const void *s390_todpreg
,
11268 char *note_name
= "LINUX";
11269 return elfcore_write_note (abfd
, buf
, bufsiz
,
11270 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11274 elfcore_write_s390_ctrs (bfd
*abfd
,
11277 const void *s390_ctrs
,
11280 char *note_name
= "LINUX";
11281 return elfcore_write_note (abfd
, buf
, bufsiz
,
11282 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11286 elfcore_write_s390_prefix (bfd
*abfd
,
11289 const void *s390_prefix
,
11292 char *note_name
= "LINUX";
11293 return elfcore_write_note (abfd
, buf
, bufsiz
,
11294 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11298 elfcore_write_s390_last_break (bfd
*abfd
,
11301 const void *s390_last_break
,
11304 char *note_name
= "LINUX";
11305 return elfcore_write_note (abfd
, buf
, bufsiz
,
11306 note_name
, NT_S390_LAST_BREAK
,
11307 s390_last_break
, size
);
11311 elfcore_write_s390_system_call (bfd
*abfd
,
11314 const void *s390_system_call
,
11317 char *note_name
= "LINUX";
11318 return elfcore_write_note (abfd
, buf
, bufsiz
,
11319 note_name
, NT_S390_SYSTEM_CALL
,
11320 s390_system_call
, size
);
11324 elfcore_write_s390_tdb (bfd
*abfd
,
11327 const void *s390_tdb
,
11330 char *note_name
= "LINUX";
11331 return elfcore_write_note (abfd
, buf
, bufsiz
,
11332 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11336 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11339 const void *s390_vxrs_low
,
11342 char *note_name
= "LINUX";
11343 return elfcore_write_note (abfd
, buf
, bufsiz
,
11344 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11348 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11351 const void *s390_vxrs_high
,
11354 char *note_name
= "LINUX";
11355 return elfcore_write_note (abfd
, buf
, bufsiz
,
11356 note_name
, NT_S390_VXRS_HIGH
,
11357 s390_vxrs_high
, size
);
11361 elfcore_write_s390_gs_cb (bfd
*abfd
,
11364 const void *s390_gs_cb
,
11367 char *note_name
= "LINUX";
11368 return elfcore_write_note (abfd
, buf
, bufsiz
,
11369 note_name
, NT_S390_GS_CB
,
11374 elfcore_write_s390_gs_bc (bfd
*abfd
,
11377 const void *s390_gs_bc
,
11380 char *note_name
= "LINUX";
11381 return elfcore_write_note (abfd
, buf
, bufsiz
,
11382 note_name
, NT_S390_GS_BC
,
11387 elfcore_write_arm_vfp (bfd
*abfd
,
11390 const void *arm_vfp
,
11393 char *note_name
= "LINUX";
11394 return elfcore_write_note (abfd
, buf
, bufsiz
,
11395 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11399 elfcore_write_aarch_tls (bfd
*abfd
,
11402 const void *aarch_tls
,
11405 char *note_name
= "LINUX";
11406 return elfcore_write_note (abfd
, buf
, bufsiz
,
11407 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11411 elfcore_write_aarch_hw_break (bfd
*abfd
,
11414 const void *aarch_hw_break
,
11417 char *note_name
= "LINUX";
11418 return elfcore_write_note (abfd
, buf
, bufsiz
,
11419 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11423 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11426 const void *aarch_hw_watch
,
11429 char *note_name
= "LINUX";
11430 return elfcore_write_note (abfd
, buf
, bufsiz
,
11431 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11435 elfcore_write_aarch_sve (bfd
*abfd
,
11438 const void *aarch_sve
,
11441 char *note_name
= "LINUX";
11442 return elfcore_write_note (abfd
, buf
, bufsiz
,
11443 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11447 elfcore_write_register_note (bfd
*abfd
,
11450 const char *section
,
11454 if (strcmp (section
, ".reg2") == 0)
11455 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11456 if (strcmp (section
, ".reg-xfp") == 0)
11457 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11458 if (strcmp (section
, ".reg-xstate") == 0)
11459 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11460 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11461 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11462 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11463 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11464 if (strcmp (section
, ".reg-ppc-tar") == 0)
11465 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11466 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11467 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11468 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11469 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11470 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11471 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11472 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11473 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11474 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11475 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11476 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11477 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11478 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11479 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11480 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11481 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11482 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11483 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11484 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11485 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11486 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11487 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11488 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11489 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11490 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11491 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11492 if (strcmp (section
, ".reg-s390-timer") == 0)
11493 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11494 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11495 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11496 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11497 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11498 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11499 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11500 if (strcmp (section
, ".reg-s390-prefix") == 0)
11501 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11502 if (strcmp (section
, ".reg-s390-last-break") == 0)
11503 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11504 if (strcmp (section
, ".reg-s390-system-call") == 0)
11505 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11506 if (strcmp (section
, ".reg-s390-tdb") == 0)
11507 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11508 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11509 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11510 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11511 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11512 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11513 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11514 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11515 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11516 if (strcmp (section
, ".reg-arm-vfp") == 0)
11517 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11518 if (strcmp (section
, ".reg-aarch-tls") == 0)
11519 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11520 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11521 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11522 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11523 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11524 if (strcmp (section
, ".reg-aarch-sve") == 0)
11525 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11530 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11535 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11536 gABI specifies that PT_NOTE alignment should be aligned to 4
11537 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11538 align is less than 4, we use 4 byte alignment. */
11541 if (align
!= 4 && align
!= 8)
11545 while (p
< buf
+ size
)
11547 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11548 Elf_Internal_Note in
;
11550 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11553 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11555 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11556 in
.namedata
= xnp
->name
;
11557 if (in
.namesz
> buf
- in
.namedata
+ size
)
11560 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11561 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11562 in
.descpos
= offset
+ (in
.descdata
- buf
);
11564 && (in
.descdata
>= buf
+ size
11565 || in
.descsz
> buf
- in
.descdata
+ size
))
11568 switch (bfd_get_format (abfd
))
11575 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11578 const char * string
;
11580 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11584 GROKER_ELEMENT ("", elfcore_grok_note
),
11585 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11586 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11587 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11588 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11589 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
11591 #undef GROKER_ELEMENT
11594 for (i
= ARRAY_SIZE (grokers
); i
--;)
11596 if (in
.namesz
>= grokers
[i
].len
11597 && strncmp (in
.namedata
, grokers
[i
].string
,
11598 grokers
[i
].len
) == 0)
11600 if (! grokers
[i
].func (abfd
, & in
))
11609 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
11611 if (! elfobj_grok_gnu_note (abfd
, &in
))
11614 else if (in
.namesz
== sizeof "stapsdt"
11615 && strcmp (in
.namedata
, "stapsdt") == 0)
11617 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
11623 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
11630 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
11635 if (size
== 0 || (size
+ 1) == 0)
11638 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
11641 buf
= (char *) bfd_malloc (size
+ 1);
11645 /* PR 17512: file: ec08f814
11646 0-termintate the buffer so that string searches will not overflow. */
11649 if (bfd_bread (buf
, size
, abfd
) != size
11650 || !elf_parse_notes (abfd
, buf
, size
, offset
, align
))
11660 /* Providing external access to the ELF program header table. */
11662 /* Return an upper bound on the number of bytes required to store a
11663 copy of ABFD's program header table entries. Return -1 if an error
11664 occurs; bfd_get_error will return an appropriate code. */
11667 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
11669 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11671 bfd_set_error (bfd_error_wrong_format
);
11675 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
11678 /* Copy ABFD's program header table entries to *PHDRS. The entries
11679 will be stored as an array of Elf_Internal_Phdr structures, as
11680 defined in include/elf/internal.h. To find out how large the
11681 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11683 Return the number of program header table entries read, or -1 if an
11684 error occurs; bfd_get_error will return an appropriate code. */
11687 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
11691 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11693 bfd_set_error (bfd_error_wrong_format
);
11697 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
11698 if (num_phdrs
!= 0)
11699 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
11700 num_phdrs
* sizeof (Elf_Internal_Phdr
));
11705 enum elf_reloc_type_class
11706 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11707 const asection
*rel_sec ATTRIBUTE_UNUSED
,
11708 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
11710 return reloc_class_normal
;
11713 /* For RELA architectures, return the relocation value for a
11714 relocation against a local symbol. */
11717 _bfd_elf_rela_local_sym (bfd
*abfd
,
11718 Elf_Internal_Sym
*sym
,
11720 Elf_Internal_Rela
*rel
)
11722 asection
*sec
= *psec
;
11723 bfd_vma relocation
;
11725 relocation
= (sec
->output_section
->vma
11726 + sec
->output_offset
11728 if ((sec
->flags
& SEC_MERGE
)
11729 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
11730 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11733 _bfd_merged_section_offset (abfd
, psec
,
11734 elf_section_data (sec
)->sec_info
,
11735 sym
->st_value
+ rel
->r_addend
);
11738 /* If we have changed the section, and our original section is
11739 marked with SEC_EXCLUDE, it means that the original
11740 SEC_MERGE section has been completely subsumed in some
11741 other SEC_MERGE section. In this case, we need to leave
11742 some info around for --emit-relocs. */
11743 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
11744 sec
->kept_section
= *psec
;
11747 rel
->r_addend
-= relocation
;
11748 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
11754 _bfd_elf_rel_local_sym (bfd
*abfd
,
11755 Elf_Internal_Sym
*sym
,
11759 asection
*sec
= *psec
;
11761 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
11762 return sym
->st_value
+ addend
;
11764 return _bfd_merged_section_offset (abfd
, psec
,
11765 elf_section_data (sec
)->sec_info
,
11766 sym
->st_value
+ addend
);
11769 /* Adjust an address within a section. Given OFFSET within SEC, return
11770 the new offset within the section, based upon changes made to the
11771 section. Returns -1 if the offset is now invalid.
11772 The offset (in abnd out) is in target sized bytes, however big a
11776 _bfd_elf_section_offset (bfd
*abfd
,
11777 struct bfd_link_info
*info
,
11781 switch (sec
->sec_info_type
)
11783 case SEC_INFO_TYPE_STABS
:
11784 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
11786 case SEC_INFO_TYPE_EH_FRAME
:
11787 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
11790 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
11792 /* Reverse the offset. */
11793 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11794 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
11796 /* address_size and sec->size are in octets. Convert
11797 to bytes before subtracting the original offset. */
11798 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
11804 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11805 reconstruct an ELF file by reading the segments out of remote memory
11806 based on the ELF file header at EHDR_VMA and the ELF program headers it
11807 points to. If not null, *LOADBASEP is filled in with the difference
11808 between the VMAs from which the segments were read, and the VMAs the
11809 file headers (and hence BFD's idea of each section's VMA) put them at.
11811 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11812 remote memory at target address VMA into the local buffer at MYADDR; it
11813 should return zero on success or an `errno' code on failure. TEMPL must
11814 be a BFD for an ELF target with the word size and byte order found in
11815 the remote memory. */
11818 bfd_elf_bfd_from_remote_memory
11821 bfd_size_type size
,
11822 bfd_vma
*loadbasep
,
11823 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
11825 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
11826 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
11830 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
11831 long symcount ATTRIBUTE_UNUSED
,
11832 asymbol
**syms ATTRIBUTE_UNUSED
,
11837 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11840 const char *relplt_name
;
11841 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
11845 Elf_Internal_Shdr
*hdr
;
11851 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
11854 if (dynsymcount
<= 0)
11857 if (!bed
->plt_sym_val
)
11860 relplt_name
= bed
->relplt_name
;
11861 if (relplt_name
== NULL
)
11862 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
11863 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
11864 if (relplt
== NULL
)
11867 hdr
= &elf_section_data (relplt
)->this_hdr
;
11868 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
11869 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
11872 plt
= bfd_get_section_by_name (abfd
, ".plt");
11876 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
11877 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
11880 count
= relplt
->size
/ hdr
->sh_entsize
;
11881 size
= count
* sizeof (asymbol
);
11882 p
= relplt
->relocation
;
11883 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11885 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
11886 if (p
->addend
!= 0)
11889 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
11891 size
+= sizeof ("+0x") - 1 + 8;
11896 s
= *ret
= (asymbol
*) bfd_malloc (size
);
11900 names
= (char *) (s
+ count
);
11901 p
= relplt
->relocation
;
11903 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11908 addr
= bed
->plt_sym_val (i
, plt
, p
);
11909 if (addr
== (bfd_vma
) -1)
11912 *s
= **p
->sym_ptr_ptr
;
11913 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11914 we are defining a symbol, ensure one of them is set. */
11915 if ((s
->flags
& BSF_LOCAL
) == 0)
11916 s
->flags
|= BSF_GLOBAL
;
11917 s
->flags
|= BSF_SYNTHETIC
;
11919 s
->value
= addr
- plt
->vma
;
11922 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
11923 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
11925 if (p
->addend
!= 0)
11929 memcpy (names
, "+0x", sizeof ("+0x") - 1);
11930 names
+= sizeof ("+0x") - 1;
11931 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
11932 for (a
= buf
; *a
== '0'; ++a
)
11935 memcpy (names
, a
, len
);
11938 memcpy (names
, "@plt", sizeof ("@plt"));
11939 names
+= sizeof ("@plt");
11946 /* It is only used by x86-64 so far.
11947 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11948 but current usage would allow all of _bfd_std_section to be zero. */
11949 static const asymbol lcomm_sym
11950 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
11951 asection _bfd_elf_large_com_section
11952 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
11953 "LARGE_COMMON", 0, SEC_IS_COMMON
);
11956 _bfd_elf_post_process_headers (bfd
* abfd
,
11957 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
11959 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
11961 i_ehdrp
= elf_elfheader (abfd
);
11963 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
11965 /* To make things simpler for the loader on Linux systems we set the
11966 osabi field to ELFOSABI_GNU if the binary contains symbols of
11967 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11968 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
11969 && elf_tdata (abfd
)->has_gnu_symbols
)
11970 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
11974 /* Return TRUE for ELF symbol types that represent functions.
11975 This is the default version of this function, which is sufficient for
11976 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11979 _bfd_elf_is_function_type (unsigned int type
)
11981 return (type
== STT_FUNC
11982 || type
== STT_GNU_IFUNC
);
11985 /* If the ELF symbol SYM might be a function in SEC, return the
11986 function size and set *CODE_OFF to the function's entry point,
11987 otherwise return zero. */
11990 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
11993 bfd_size_type size
;
11995 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
11996 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
11997 || sym
->section
!= sec
)
12000 *code_off
= sym
->value
;
12002 if (!(sym
->flags
& BSF_SYNTHETIC
))
12003 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;