1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2020 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
56 file_ptr offset
, size_t align
);
58 /* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
62 /* Swap in a Verdef structure. */
65 _bfd_elf_swap_verdef_in (bfd
*abfd
,
66 const Elf_External_Verdef
*src
,
67 Elf_Internal_Verdef
*dst
)
69 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
70 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
71 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
72 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
73 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
74 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
75 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
78 /* Swap out a Verdef structure. */
81 _bfd_elf_swap_verdef_out (bfd
*abfd
,
82 const Elf_Internal_Verdef
*src
,
83 Elf_External_Verdef
*dst
)
85 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
86 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
87 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
88 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
89 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
90 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
91 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
94 /* Swap in a Verdaux structure. */
97 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
98 const Elf_External_Verdaux
*src
,
99 Elf_Internal_Verdaux
*dst
)
101 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
102 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
105 /* Swap out a Verdaux structure. */
108 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
109 const Elf_Internal_Verdaux
*src
,
110 Elf_External_Verdaux
*dst
)
112 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
113 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
116 /* Swap in a Verneed structure. */
119 _bfd_elf_swap_verneed_in (bfd
*abfd
,
120 const Elf_External_Verneed
*src
,
121 Elf_Internal_Verneed
*dst
)
123 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
124 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
125 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
126 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
127 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
130 /* Swap out a Verneed structure. */
133 _bfd_elf_swap_verneed_out (bfd
*abfd
,
134 const Elf_Internal_Verneed
*src
,
135 Elf_External_Verneed
*dst
)
137 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
138 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
139 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
140 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
141 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
144 /* Swap in a Vernaux structure. */
147 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
148 const Elf_External_Vernaux
*src
,
149 Elf_Internal_Vernaux
*dst
)
151 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
152 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
153 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
154 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
155 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
158 /* Swap out a Vernaux structure. */
161 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
162 const Elf_Internal_Vernaux
*src
,
163 Elf_External_Vernaux
*dst
)
165 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
166 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
167 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
168 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
169 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
172 /* Swap in a Versym structure. */
175 _bfd_elf_swap_versym_in (bfd
*abfd
,
176 const Elf_External_Versym
*src
,
177 Elf_Internal_Versym
*dst
)
179 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
182 /* Swap out a Versym structure. */
185 _bfd_elf_swap_versym_out (bfd
*abfd
,
186 const Elf_Internal_Versym
*src
,
187 Elf_External_Versym
*dst
)
189 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
192 /* Standard ELF hash function. Do not change this function; you will
193 cause invalid hash tables to be generated. */
196 bfd_elf_hash (const char *namearg
)
198 const unsigned char *name
= (const unsigned char *) namearg
;
203 while ((ch
= *name
++) != '\0')
206 if ((g
= (h
& 0xf0000000)) != 0)
209 /* The ELF ABI says `h &= ~g', but this is equivalent in
210 this case and on some machines one insn instead of two. */
214 return h
& 0xffffffff;
217 /* DT_GNU_HASH hash function. Do not change this function; you will
218 cause invalid hash tables to be generated. */
221 bfd_elf_gnu_hash (const char *namearg
)
223 const unsigned char *name
= (const unsigned char *) namearg
;
224 unsigned long h
= 5381;
227 while ((ch
= *name
++) != '\0')
228 h
= (h
<< 5) + h
+ ch
;
229 return h
& 0xffffffff;
232 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
233 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 bfd_elf_allocate_object (bfd
*abfd
,
237 enum elf_target_id object_id
)
239 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
240 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
241 if (abfd
->tdata
.any
== NULL
)
244 elf_object_id (abfd
) = object_id
;
245 if (abfd
->direction
!= read_direction
)
247 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
250 elf_tdata (abfd
)->o
= o
;
251 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
258 bfd_elf_make_object (bfd
*abfd
)
260 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
261 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
266 bfd_elf_mkcorefile (bfd
*abfd
)
268 /* I think this can be done just like an object file. */
269 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
271 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
272 return elf_tdata (abfd
)->core
!= NULL
;
276 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
278 Elf_Internal_Shdr
**i_shdrp
;
279 bfd_byte
*shstrtab
= NULL
;
281 bfd_size_type shstrtabsize
;
283 i_shdrp
= elf_elfsections (abfd
);
285 || shindex
>= elf_numsections (abfd
)
286 || i_shdrp
[shindex
] == 0)
289 shstrtab
= i_shdrp
[shindex
]->contents
;
290 if (shstrtab
== NULL
)
292 /* No cached one, attempt to read, and cache what we read. */
293 offset
= i_shdrp
[shindex
]->sh_offset
;
294 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
296 /* Allocate and clear an extra byte at the end, to prevent crashes
297 in case the string table is not terminated. */
298 if (shstrtabsize
+ 1 <= 1
299 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
300 || (shstrtab
= _bfd_alloc_and_read (abfd
, shstrtabsize
+ 1,
301 shstrtabsize
)) == NULL
)
303 /* Once we've failed to read it, make sure we don't keep
304 trying. Otherwise, we'll keep allocating space for
305 the string table over and over. */
306 i_shdrp
[shindex
]->sh_size
= 0;
309 shstrtab
[shstrtabsize
] = '\0';
310 i_shdrp
[shindex
]->contents
= shstrtab
;
312 return (char *) shstrtab
;
316 bfd_elf_string_from_elf_section (bfd
*abfd
,
317 unsigned int shindex
,
318 unsigned int strindex
)
320 Elf_Internal_Shdr
*hdr
;
325 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
328 hdr
= elf_elfsections (abfd
)[shindex
];
330 if (hdr
->contents
== NULL
)
332 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
334 /* PR 17512: file: f057ec89. */
335 /* xgettext:c-format */
336 _bfd_error_handler (_("%pB: attempt to load strings from"
337 " a non-string section (number %d)"),
342 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
347 /* PR 24273: The string section's contents may have already
348 been loaded elsewhere, eg because a corrupt file has the
349 string section index in the ELF header pointing at a group
350 section. So be paranoid, and test that the last byte of
351 the section is zero. */
352 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
356 if (strindex
>= hdr
->sh_size
)
358 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
360 /* xgettext:c-format */
361 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
362 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
363 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
365 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
369 return ((char *) hdr
->contents
) + strindex
;
372 /* Read and convert symbols to internal format.
373 SYMCOUNT specifies the number of symbols to read, starting from
374 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
375 are non-NULL, they are used to store the internal symbols, external
376 symbols, and symbol section index extensions, respectively.
377 Returns a pointer to the internal symbol buffer (malloced if necessary)
378 or NULL if there were no symbols or some kind of problem. */
381 bfd_elf_get_elf_syms (bfd
*ibfd
,
382 Elf_Internal_Shdr
*symtab_hdr
,
385 Elf_Internal_Sym
*intsym_buf
,
387 Elf_External_Sym_Shndx
*extshndx_buf
)
389 Elf_Internal_Shdr
*shndx_hdr
;
391 const bfd_byte
*esym
;
392 Elf_External_Sym_Shndx
*alloc_extshndx
;
393 Elf_External_Sym_Shndx
*shndx
;
394 Elf_Internal_Sym
*alloc_intsym
;
395 Elf_Internal_Sym
*isym
;
396 Elf_Internal_Sym
*isymend
;
397 const struct elf_backend_data
*bed
;
402 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
408 /* Normal syms might have section extension entries. */
410 if (elf_symtab_shndx_list (ibfd
) != NULL
)
412 elf_section_list
* entry
;
413 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
415 /* Find an index section that is linked to this symtab section. */
416 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
419 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
422 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
424 shndx_hdr
= & entry
->hdr
;
429 if (shndx_hdr
== NULL
)
431 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
432 /* Not really accurate, but this was how the old code used to work. */
433 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
434 /* Otherwise we do nothing. The assumption is that
435 the index table will not be needed. */
439 /* Read the symbols. */
441 alloc_extshndx
= NULL
;
443 bed
= get_elf_backend_data (ibfd
);
444 extsym_size
= bed
->s
->sizeof_sym
;
445 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
447 bfd_set_error (bfd_error_file_too_big
);
451 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
452 if (extsym_buf
== NULL
)
454 alloc_ext
= bfd_malloc (amt
);
455 extsym_buf
= alloc_ext
;
457 if (extsym_buf
== NULL
458 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
459 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
465 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
469 if (_bfd_mul_overflow (symcount
, sizeof (Elf_External_Sym_Shndx
), &amt
))
471 bfd_set_error (bfd_error_file_too_big
);
475 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
476 if (extshndx_buf
== NULL
)
478 alloc_extshndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
479 extshndx_buf
= alloc_extshndx
;
481 if (extshndx_buf
== NULL
482 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
483 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
490 if (intsym_buf
== NULL
)
492 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
494 bfd_set_error (bfd_error_file_too_big
);
497 alloc_intsym
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
498 intsym_buf
= alloc_intsym
;
499 if (intsym_buf
== NULL
)
503 /* Convert the symbols to internal form. */
504 isymend
= intsym_buf
+ symcount
;
505 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
506 shndx
= extshndx_buf
;
508 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
509 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
511 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
512 /* xgettext:c-format */
513 _bfd_error_handler (_("%pB symbol number %lu references"
514 " nonexistent SHT_SYMTAB_SHNDX section"),
515 ibfd
, (unsigned long) symoffset
);
523 free (alloc_extshndx
);
528 /* Look up a symbol name. */
530 bfd_elf_sym_name (bfd
*abfd
,
531 Elf_Internal_Shdr
*symtab_hdr
,
532 Elf_Internal_Sym
*isym
,
536 unsigned int iname
= isym
->st_name
;
537 unsigned int shindex
= symtab_hdr
->sh_link
;
539 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
540 /* Check for a bogus st_shndx to avoid crashing. */
541 && isym
->st_shndx
< elf_numsections (abfd
))
543 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
544 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
547 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
550 else if (sym_sec
&& *name
== '\0')
551 name
= bfd_section_name (sym_sec
);
556 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
557 sections. The first element is the flags, the rest are section
560 typedef union elf_internal_group
{
561 Elf_Internal_Shdr
*shdr
;
563 } Elf_Internal_Group
;
565 /* Return the name of the group signature symbol. Why isn't the
566 signature just a string? */
569 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
571 Elf_Internal_Shdr
*hdr
;
572 unsigned char esym
[sizeof (Elf64_External_Sym
)];
573 Elf_External_Sym_Shndx eshndx
;
574 Elf_Internal_Sym isym
;
576 /* First we need to ensure the symbol table is available. Make sure
577 that it is a symbol table section. */
578 if (ghdr
->sh_link
>= elf_numsections (abfd
))
580 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
581 if (hdr
->sh_type
!= SHT_SYMTAB
582 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
585 /* Go read the symbol. */
586 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
587 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
588 &isym
, esym
, &eshndx
) == NULL
)
591 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
594 /* Set next_in_group list pointer, and group name for NEWSECT. */
597 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
599 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
601 /* If num_group is zero, read in all SHT_GROUP sections. The count
602 is set to -1 if there are no SHT_GROUP sections. */
605 unsigned int i
, shnum
;
607 /* First count the number of groups. If we have a SHT_GROUP
608 section with just a flag word (ie. sh_size is 4), ignore it. */
609 shnum
= elf_numsections (abfd
);
612 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
613 ( (shdr)->sh_type == SHT_GROUP \
614 && (shdr)->sh_size >= minsize \
615 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
616 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
618 for (i
= 0; i
< shnum
; i
++)
620 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
622 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
628 num_group
= (unsigned) -1;
629 elf_tdata (abfd
)->num_group
= num_group
;
630 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
634 /* We keep a list of elf section headers for group sections,
635 so we can find them quickly. */
638 elf_tdata (abfd
)->num_group
= num_group
;
639 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
640 elf_tdata (abfd
)->group_sect_ptr
641 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
642 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
646 for (i
= 0; i
< shnum
; i
++)
648 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
650 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
653 Elf_Internal_Group
*dest
;
655 /* Make sure the group section has a BFD section
657 if (!bfd_section_from_shdr (abfd
, i
))
660 /* Add to list of sections. */
661 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
664 /* Read the raw contents. */
665 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
666 shdr
->contents
= NULL
;
667 if (_bfd_mul_overflow (shdr
->sh_size
,
668 sizeof (*dest
) / 4, &amt
)
669 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
671 = _bfd_alloc_and_read (abfd
, amt
, shdr
->sh_size
)))
674 /* xgettext:c-format */
675 (_("%pB: invalid size field in group section"
676 " header: %#" PRIx64
""),
677 abfd
, (uint64_t) shdr
->sh_size
);
678 bfd_set_error (bfd_error_bad_value
);
683 /* Translate raw contents, a flag word followed by an
684 array of elf section indices all in target byte order,
685 to the flag word followed by an array of elf section
687 src
= shdr
->contents
+ shdr
->sh_size
;
688 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
696 idx
= H_GET_32 (abfd
, src
);
697 if (src
== shdr
->contents
)
701 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
702 shdr
->bfd_section
->flags
703 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
708 dest
->shdr
= elf_elfsections (abfd
)[idx
];
709 /* PR binutils/23199: All sections in a
710 section group should be marked with
711 SHF_GROUP. But some tools generate
712 broken objects without SHF_GROUP. Fix
714 dest
->shdr
->sh_flags
|= SHF_GROUP
;
717 || dest
->shdr
->sh_type
== SHT_GROUP
)
720 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
728 /* PR 17510: Corrupt binaries might contain invalid groups. */
729 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
731 elf_tdata (abfd
)->num_group
= num_group
;
733 /* If all groups are invalid then fail. */
736 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
737 elf_tdata (abfd
)->num_group
= num_group
= -1;
739 (_("%pB: no valid group sections found"), abfd
);
740 bfd_set_error (bfd_error_bad_value
);
746 if (num_group
!= (unsigned) -1)
748 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
751 for (j
= 0; j
< num_group
; j
++)
753 /* Begin search from previous found group. */
754 unsigned i
= (j
+ search_offset
) % num_group
;
756 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
757 Elf_Internal_Group
*idx
;
763 idx
= (Elf_Internal_Group
*) shdr
->contents
;
764 if (idx
== NULL
|| shdr
->sh_size
< 4)
766 /* See PR 21957 for a reproducer. */
767 /* xgettext:c-format */
768 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
769 abfd
, shdr
->bfd_section
);
770 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
771 bfd_set_error (bfd_error_bad_value
);
774 n_elt
= shdr
->sh_size
/ 4;
776 /* Look through this group's sections to see if current
777 section is a member. */
779 if ((++idx
)->shdr
== hdr
)
783 /* We are a member of this group. Go looking through
784 other members to see if any others are linked via
786 idx
= (Elf_Internal_Group
*) shdr
->contents
;
787 n_elt
= shdr
->sh_size
/ 4;
789 if ((++idx
)->shdr
!= NULL
790 && (s
= idx
->shdr
->bfd_section
) != NULL
791 && elf_next_in_group (s
) != NULL
)
795 /* Snarf the group name from other member, and
796 insert current section in circular list. */
797 elf_group_name (newsect
) = elf_group_name (s
);
798 elf_next_in_group (newsect
) = elf_next_in_group (s
);
799 elf_next_in_group (s
) = newsect
;
805 gname
= group_signature (abfd
, shdr
);
808 elf_group_name (newsect
) = gname
;
810 /* Start a circular list with one element. */
811 elf_next_in_group (newsect
) = newsect
;
814 /* If the group section has been created, point to the
816 if (shdr
->bfd_section
!= NULL
)
817 elf_next_in_group (shdr
->bfd_section
) = newsect
;
819 elf_tdata (abfd
)->group_search_offset
= i
;
826 if (elf_group_name (newsect
) == NULL
)
828 /* xgettext:c-format */
829 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
837 _bfd_elf_setup_sections (bfd
*abfd
)
840 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
841 bfd_boolean result
= TRUE
;
844 /* Process SHF_LINK_ORDER. */
845 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
847 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
848 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
850 unsigned int elfsec
= this_hdr
->sh_link
;
851 /* An sh_link value of 0 is now allowed. It indicates that linked
852 to section has already been discarded, but that the current
853 section has been retained for some other reason. This linking
854 section is still a candidate for later garbage collection
858 elf_linked_to_section (s
) = NULL
;
862 asection
*linksec
= NULL
;
864 if (elfsec
< elf_numsections (abfd
))
866 this_hdr
= elf_elfsections (abfd
)[elfsec
];
867 linksec
= this_hdr
->bfd_section
;
871 Some strip/objcopy may leave an incorrect value in
872 sh_link. We don't want to proceed. */
876 /* xgettext:c-format */
877 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
878 s
->owner
, elfsec
, s
);
882 elf_linked_to_section (s
) = linksec
;
885 else if (this_hdr
->sh_type
== SHT_GROUP
886 && elf_next_in_group (s
) == NULL
)
889 /* xgettext:c-format */
890 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
891 abfd
, elf_section_data (s
)->this_idx
);
896 /* Process section groups. */
897 if (num_group
== (unsigned) -1)
900 for (i
= 0; i
< num_group
; i
++)
902 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
903 Elf_Internal_Group
*idx
;
906 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
907 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
910 /* xgettext:c-format */
911 (_("%pB: section group entry number %u is corrupt"),
917 idx
= (Elf_Internal_Group
*) shdr
->contents
;
918 n_elt
= shdr
->sh_size
/ 4;
924 if (idx
->shdr
== NULL
)
926 else if (idx
->shdr
->bfd_section
)
927 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
928 else if (idx
->shdr
->sh_type
!= SHT_RELA
929 && idx
->shdr
->sh_type
!= SHT_REL
)
931 /* There are some unknown sections in the group. */
933 /* xgettext:c-format */
934 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
937 bfd_elf_string_from_elf_section (abfd
,
938 (elf_elfheader (abfd
)
951 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
953 return elf_next_in_group (sec
) != NULL
;
957 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
959 if (elf_sec_group (sec
) != NULL
)
960 return elf_group_name (sec
);
965 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
967 unsigned int len
= strlen (name
);
968 char *new_name
= bfd_alloc (abfd
, len
+ 2);
969 if (new_name
== NULL
)
973 memcpy (new_name
+ 2, name
+ 1, len
);
978 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
980 unsigned int len
= strlen (name
);
981 char *new_name
= bfd_alloc (abfd
, len
);
982 if (new_name
== NULL
)
985 memcpy (new_name
+ 1, name
+ 2, len
- 1);
989 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
993 int16_t major_version
;
994 int16_t minor_version
;
995 unsigned char slim_object
;
997 /* Flags is a private field that is not defined publicly. */
1001 /* Make a BFD section from an ELF section. We store a pointer to the
1002 BFD section in the bfd_section field of the header. */
1005 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1006 Elf_Internal_Shdr
*hdr
,
1012 const struct elf_backend_data
*bed
;
1013 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
1015 if (hdr
->bfd_section
!= NULL
)
1018 newsect
= bfd_make_section_anyway (abfd
, name
);
1019 if (newsect
== NULL
)
1022 hdr
->bfd_section
= newsect
;
1023 elf_section_data (newsect
)->this_hdr
= *hdr
;
1024 elf_section_data (newsect
)->this_idx
= shindex
;
1026 /* Always use the real type/flags. */
1027 elf_section_type (newsect
) = hdr
->sh_type
;
1028 elf_section_flags (newsect
) = hdr
->sh_flags
;
1030 newsect
->filepos
= hdr
->sh_offset
;
1032 flags
= SEC_NO_FLAGS
;
1033 if (hdr
->sh_type
!= SHT_NOBITS
)
1034 flags
|= SEC_HAS_CONTENTS
;
1035 if (hdr
->sh_type
== SHT_GROUP
)
1037 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1040 if (hdr
->sh_type
!= SHT_NOBITS
)
1043 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1044 flags
|= SEC_READONLY
;
1045 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1047 else if ((flags
& SEC_LOAD
) != 0)
1049 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1052 newsect
->entsize
= hdr
->sh_entsize
;
1054 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1055 flags
|= SEC_STRINGS
;
1056 if (hdr
->sh_flags
& SHF_GROUP
)
1057 if (!setup_group (abfd
, hdr
, newsect
))
1059 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1060 flags
|= SEC_THREAD_LOCAL
;
1061 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1062 flags
|= SEC_EXCLUDE
;
1064 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1066 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1067 but binutils as of 2019-07-23 did not set the EI_OSABI header
1070 case ELFOSABI_FREEBSD
:
1071 if ((hdr
->sh_flags
& SHF_GNU_RETAIN
) != 0)
1072 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_retain
;
1075 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1076 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1080 if ((flags
& SEC_ALLOC
) == 0)
1082 /* The debugging sections appear to be recognized only by name,
1083 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1084 if (name
[0] == '.')
1086 if (strncmp (name
, ".debug", 6) == 0
1087 || strncmp (name
, ".gnu.linkonce.wi.", 17) == 0
1088 || strncmp (name
, ".zdebug", 7) == 0)
1089 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1090 else if (strncmp (name
, GNU_BUILD_ATTRS_SECTION_NAME
, 21) == 0
1091 || strncmp (name
, ".note.gnu", 9) == 0)
1093 flags
|= SEC_ELF_OCTETS
;
1096 else if (strncmp (name
, ".line", 5) == 0
1097 || strncmp (name
, ".stab", 5) == 0
1098 || strcmp (name
, ".gdb_index") == 0)
1099 flags
|= SEC_DEBUGGING
;
1103 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1104 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1105 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
)))
1108 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1109 only link a single copy of the section. This is used to support
1110 g++. g++ will emit each template expansion in its own section.
1111 The symbols will be defined as weak, so that multiple definitions
1112 are permitted. The GNU linker extension is to actually discard
1113 all but one of the sections. */
1114 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1115 && elf_next_in_group (newsect
) == NULL
)
1116 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1118 if (!bfd_set_section_flags (newsect
, flags
))
1121 bed
= get_elf_backend_data (abfd
);
1122 if (bed
->elf_backend_section_flags
)
1123 if (!bed
->elf_backend_section_flags (hdr
))
1126 /* We do not parse the PT_NOTE segments as we are interested even in the
1127 separate debug info files which may have the segments offsets corrupted.
1128 PT_NOTEs from the core files are currently not parsed using BFD. */
1129 if (hdr
->sh_type
== SHT_NOTE
)
1133 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1136 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1137 hdr
->sh_offset
, hdr
->sh_addralign
);
1141 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1143 Elf_Internal_Phdr
*phdr
;
1144 unsigned int i
, nload
;
1146 /* Some ELF linkers produce binaries with all the program header
1147 p_paddr fields zero. If we have such a binary with more than
1148 one PT_LOAD header, then leave the section lma equal to vma
1149 so that we don't create sections with overlapping lma. */
1150 phdr
= elf_tdata (abfd
)->phdr
;
1151 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1152 if (phdr
->p_paddr
!= 0)
1154 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1156 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1159 phdr
= elf_tdata (abfd
)->phdr
;
1160 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1162 if (((phdr
->p_type
== PT_LOAD
1163 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1164 || phdr
->p_type
== PT_TLS
)
1165 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1167 if ((newsect
->flags
& SEC_LOAD
) == 0)
1168 newsect
->lma
= (phdr
->p_paddr
1169 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1171 /* We used to use the same adjustment for SEC_LOAD
1172 sections, but that doesn't work if the segment
1173 is packed with code from multiple VMAs.
1174 Instead we calculate the section LMA based on
1175 the segment LMA. It is assumed that the
1176 segment will contain sections with contiguous
1177 LMAs, even if the VMAs are not. */
1178 newsect
->lma
= (phdr
->p_paddr
1179 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1181 /* With contiguous segments, we can't tell from file
1182 offsets whether a section with zero size should
1183 be placed at the end of one segment or the
1184 beginning of the next. Decide based on vaddr. */
1185 if (hdr
->sh_addr
>= phdr
->p_vaddr
1186 && (hdr
->sh_addr
+ hdr
->sh_size
1187 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1193 /* Compress/decompress DWARF debug sections with names: .debug_* and
1194 .zdebug_*, after the section flags is set. */
1195 if ((newsect
->flags
& SEC_DEBUGGING
)
1196 && ((name
[1] == 'd' && name
[6] == '_')
1197 || (name
[1] == 'z' && name
[7] == '_')))
1199 enum { nothing
, compress
, decompress
} action
= nothing
;
1200 int compression_header_size
;
1201 bfd_size_type uncompressed_size
;
1202 unsigned int uncompressed_align_power
;
1203 bfd_boolean compressed
1204 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1205 &compression_header_size
,
1207 &uncompressed_align_power
);
1210 /* Compressed section. Check if we should decompress. */
1211 if ((abfd
->flags
& BFD_DECOMPRESS
))
1212 action
= decompress
;
1215 /* Compress the uncompressed section or convert from/to .zdebug*
1216 section. Check if we should compress. */
1217 if (action
== nothing
)
1219 if (newsect
->size
!= 0
1220 && (abfd
->flags
& BFD_COMPRESS
)
1221 && compression_header_size
>= 0
1222 && uncompressed_size
> 0
1224 || ((compression_header_size
> 0)
1225 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1231 if (action
== compress
)
1233 if (!bfd_init_section_compress_status (abfd
, newsect
))
1236 /* xgettext:c-format */
1237 (_("%pB: unable to initialize compress status for section %s"),
1244 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1247 /* xgettext:c-format */
1248 (_("%pB: unable to initialize decompress status for section %s"),
1254 if (abfd
->is_linker_input
)
1257 && (action
== decompress
1258 || (action
== compress
1259 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1261 /* Convert section name from .zdebug_* to .debug_* so
1262 that linker will consider this section as a debug
1264 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1265 if (new_name
== NULL
)
1267 bfd_rename_section (newsect
, new_name
);
1271 /* For objdump, don't rename the section. For objcopy, delay
1272 section rename to elf_fake_sections. */
1273 newsect
->flags
|= SEC_ELF_RENAME
;
1276 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1278 const char *lto_section_name
= ".gnu.lto_.lto.";
1279 if (strncmp (name
, lto_section_name
, strlen (lto_section_name
)) == 0)
1281 struct lto_section lsection
;
1282 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1283 sizeof (struct lto_section
)))
1284 abfd
->lto_slim_object
= lsection
.slim_object
;
1290 const char *const bfd_elf_section_type_names
[] =
1292 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1293 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1294 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1297 /* ELF relocs are against symbols. If we are producing relocatable
1298 output, and the reloc is against an external symbol, and nothing
1299 has given us any additional addend, the resulting reloc will also
1300 be against the same symbol. In such a case, we don't want to
1301 change anything about the way the reloc is handled, since it will
1302 all be done at final link time. Rather than put special case code
1303 into bfd_perform_relocation, all the reloc types use this howto
1304 function. It just short circuits the reloc if producing
1305 relocatable output against an external symbol. */
1307 bfd_reloc_status_type
1308 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1309 arelent
*reloc_entry
,
1311 void *data ATTRIBUTE_UNUSED
,
1312 asection
*input_section
,
1314 char **error_message ATTRIBUTE_UNUSED
)
1316 if (output_bfd
!= NULL
1317 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1318 && (! reloc_entry
->howto
->partial_inplace
1319 || reloc_entry
->addend
== 0))
1321 reloc_entry
->address
+= input_section
->output_offset
;
1322 return bfd_reloc_ok
;
1325 return bfd_reloc_continue
;
1328 /* Returns TRUE if section A matches section B.
1329 Names, addresses and links may be different, but everything else
1330 should be the same. */
1333 section_match (const Elf_Internal_Shdr
* a
,
1334 const Elf_Internal_Shdr
* b
)
1336 if (a
->sh_type
!= b
->sh_type
1337 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1338 || a
->sh_addralign
!= b
->sh_addralign
1339 || a
->sh_entsize
!= b
->sh_entsize
)
1341 if (a
->sh_type
== SHT_SYMTAB
1342 || a
->sh_type
== SHT_STRTAB
)
1344 return a
->sh_size
== b
->sh_size
;
1347 /* Find a section in OBFD that has the same characteristics
1348 as IHEADER. Return the index of this section or SHN_UNDEF if
1349 none can be found. Check's section HINT first, as this is likely
1350 to be the correct section. */
1353 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1354 const unsigned int hint
)
1356 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1359 BFD_ASSERT (iheader
!= NULL
);
1361 /* See PR 20922 for a reproducer of the NULL test. */
1362 if (hint
< elf_numsections (obfd
)
1363 && oheaders
[hint
] != NULL
1364 && section_match (oheaders
[hint
], iheader
))
1367 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1369 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1371 if (oheader
== NULL
)
1373 if (section_match (oheader
, iheader
))
1374 /* FIXME: Do we care if there is a potential for
1375 multiple matches ? */
1382 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1383 Processor specific section, based upon a matching input section.
1384 Returns TRUE upon success, FALSE otherwise. */
1387 copy_special_section_fields (const bfd
*ibfd
,
1389 const Elf_Internal_Shdr
*iheader
,
1390 Elf_Internal_Shdr
*oheader
,
1391 const unsigned int secnum
)
1393 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1394 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1395 bfd_boolean changed
= FALSE
;
1396 unsigned int sh_link
;
1398 if (oheader
->sh_type
== SHT_NOBITS
)
1400 /* This is a feature for objcopy --only-keep-debug:
1401 When a section's type is changed to NOBITS, we preserve
1402 the sh_link and sh_info fields so that they can be
1403 matched up with the original.
1405 Note: Strictly speaking these assignments are wrong.
1406 The sh_link and sh_info fields should point to the
1407 relevent sections in the output BFD, which may not be in
1408 the same location as they were in the input BFD. But
1409 the whole point of this action is to preserve the
1410 original values of the sh_link and sh_info fields, so
1411 that they can be matched up with the section headers in
1412 the original file. So strictly speaking we may be
1413 creating an invalid ELF file, but it is only for a file
1414 that just contains debug info and only for sections
1415 without any contents. */
1416 if (oheader
->sh_link
== 0)
1417 oheader
->sh_link
= iheader
->sh_link
;
1418 if (oheader
->sh_info
== 0)
1419 oheader
->sh_info
= iheader
->sh_info
;
1423 /* Allow the target a chance to decide how these fields should be set. */
1424 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1428 /* We have an iheader which might match oheader, and which has non-zero
1429 sh_info and/or sh_link fields. Attempt to follow those links and find
1430 the section in the output bfd which corresponds to the linked section
1431 in the input bfd. */
1432 if (iheader
->sh_link
!= SHN_UNDEF
)
1434 /* See PR 20931 for a reproducer. */
1435 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1438 /* xgettext:c-format */
1439 (_("%pB: invalid sh_link field (%d) in section number %d"),
1440 ibfd
, iheader
->sh_link
, secnum
);
1444 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1445 if (sh_link
!= SHN_UNDEF
)
1447 oheader
->sh_link
= sh_link
;
1451 /* FIXME: Should we install iheader->sh_link
1452 if we could not find a match ? */
1454 /* xgettext:c-format */
1455 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1458 if (iheader
->sh_info
)
1460 /* The sh_info field can hold arbitrary information, but if the
1461 SHF_LINK_INFO flag is set then it should be interpreted as a
1463 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1465 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1467 if (sh_link
!= SHN_UNDEF
)
1468 oheader
->sh_flags
|= SHF_INFO_LINK
;
1471 /* No idea what it means - just copy it. */
1472 sh_link
= iheader
->sh_info
;
1474 if (sh_link
!= SHN_UNDEF
)
1476 oheader
->sh_info
= sh_link
;
1481 /* xgettext:c-format */
1482 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1488 /* Copy the program header and other data from one object module to
1492 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1494 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1495 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1496 const struct elf_backend_data
*bed
;
1499 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1500 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1503 if (!elf_flags_init (obfd
))
1505 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1506 elf_flags_init (obfd
) = TRUE
;
1509 elf_gp (obfd
) = elf_gp (ibfd
);
1511 /* Also copy the EI_OSABI field. */
1512 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1513 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1515 /* If set, copy the EI_ABIVERSION field. */
1516 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1517 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1518 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1520 /* Copy object attributes. */
1521 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1523 if (iheaders
== NULL
|| oheaders
== NULL
)
1526 bed
= get_elf_backend_data (obfd
);
1528 /* Possibly copy other fields in the section header. */
1529 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1532 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1534 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1535 because of a special case need for generating separate debug info
1536 files. See below for more details. */
1538 || (oheader
->sh_type
!= SHT_NOBITS
1539 && oheader
->sh_type
< SHT_LOOS
))
1542 /* Ignore empty sections, and sections whose
1543 fields have already been initialised. */
1544 if (oheader
->sh_size
== 0
1545 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1548 /* Scan for the matching section in the input bfd.
1549 First we try for a direct mapping between the input and output sections. */
1550 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1552 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1554 if (iheader
== NULL
)
1557 if (oheader
->bfd_section
!= NULL
1558 && iheader
->bfd_section
!= NULL
1559 && iheader
->bfd_section
->output_section
!= NULL
1560 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1562 /* We have found a connection from the input section to the
1563 output section. Attempt to copy the header fields. If
1564 this fails then do not try any further sections - there
1565 should only be a one-to-one mapping between input and output. */
1566 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1567 j
= elf_numsections (ibfd
);
1572 if (j
< elf_numsections (ibfd
))
1575 /* That failed. So try to deduce the corresponding input section.
1576 Unfortunately we cannot compare names as the output string table
1577 is empty, so instead we check size, address and type. */
1578 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1580 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1582 if (iheader
== NULL
)
1585 /* Try matching fields in the input section's header.
1586 Since --only-keep-debug turns all non-debug sections into
1587 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1589 if ((oheader
->sh_type
== SHT_NOBITS
1590 || iheader
->sh_type
== oheader
->sh_type
)
1591 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1592 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1593 && iheader
->sh_addralign
== oheader
->sh_addralign
1594 && iheader
->sh_entsize
== oheader
->sh_entsize
1595 && iheader
->sh_size
== oheader
->sh_size
1596 && iheader
->sh_addr
== oheader
->sh_addr
1597 && (iheader
->sh_info
!= oheader
->sh_info
1598 || iheader
->sh_link
!= oheader
->sh_link
))
1600 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1605 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1607 /* Final attempt. Call the backend copy function
1608 with a NULL input section. */
1609 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1618 get_segment_type (unsigned int p_type
)
1623 case PT_NULL
: pt
= "NULL"; break;
1624 case PT_LOAD
: pt
= "LOAD"; break;
1625 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1626 case PT_INTERP
: pt
= "INTERP"; break;
1627 case PT_NOTE
: pt
= "NOTE"; break;
1628 case PT_SHLIB
: pt
= "SHLIB"; break;
1629 case PT_PHDR
: pt
= "PHDR"; break;
1630 case PT_TLS
: pt
= "TLS"; break;
1631 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1632 case PT_GNU_STACK
: pt
= "STACK"; break;
1633 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1634 default: pt
= NULL
; break;
1639 /* Print out the program headers. */
1642 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1644 FILE *f
= (FILE *) farg
;
1645 Elf_Internal_Phdr
*p
;
1647 bfd_byte
*dynbuf
= NULL
;
1649 p
= elf_tdata (abfd
)->phdr
;
1654 fprintf (f
, _("\nProgram Header:\n"));
1655 c
= elf_elfheader (abfd
)->e_phnum
;
1656 for (i
= 0; i
< c
; i
++, p
++)
1658 const char *pt
= get_segment_type (p
->p_type
);
1663 sprintf (buf
, "0x%lx", p
->p_type
);
1666 fprintf (f
, "%8s off 0x", pt
);
1667 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1668 fprintf (f
, " vaddr 0x");
1669 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1670 fprintf (f
, " paddr 0x");
1671 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1672 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1673 fprintf (f
, " filesz 0x");
1674 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1675 fprintf (f
, " memsz 0x");
1676 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1677 fprintf (f
, " flags %c%c%c",
1678 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1679 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1680 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1681 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1682 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1687 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1690 unsigned int elfsec
;
1691 unsigned long shlink
;
1692 bfd_byte
*extdyn
, *extdynend
;
1694 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1696 fprintf (f
, _("\nDynamic Section:\n"));
1698 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1701 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1702 if (elfsec
== SHN_BAD
)
1704 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1706 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1707 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1710 /* PR 17512: file: 6f427532. */
1711 if (s
->size
< extdynsize
)
1713 extdynend
= extdyn
+ s
->size
;
1714 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1716 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1718 Elf_Internal_Dyn dyn
;
1719 const char *name
= "";
1721 bfd_boolean stringp
;
1722 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1724 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1726 if (dyn
.d_tag
== DT_NULL
)
1733 if (bed
->elf_backend_get_target_dtag
)
1734 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1736 if (!strcmp (name
, ""))
1738 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1743 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1744 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1745 case DT_PLTGOT
: name
= "PLTGOT"; break;
1746 case DT_HASH
: name
= "HASH"; break;
1747 case DT_STRTAB
: name
= "STRTAB"; break;
1748 case DT_SYMTAB
: name
= "SYMTAB"; break;
1749 case DT_RELA
: name
= "RELA"; break;
1750 case DT_RELASZ
: name
= "RELASZ"; break;
1751 case DT_RELAENT
: name
= "RELAENT"; break;
1752 case DT_STRSZ
: name
= "STRSZ"; break;
1753 case DT_SYMENT
: name
= "SYMENT"; break;
1754 case DT_INIT
: name
= "INIT"; break;
1755 case DT_FINI
: name
= "FINI"; break;
1756 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1757 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1758 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1759 case DT_REL
: name
= "REL"; break;
1760 case DT_RELSZ
: name
= "RELSZ"; break;
1761 case DT_RELENT
: name
= "RELENT"; break;
1762 case DT_PLTREL
: name
= "PLTREL"; break;
1763 case DT_DEBUG
: name
= "DEBUG"; break;
1764 case DT_TEXTREL
: name
= "TEXTREL"; break;
1765 case DT_JMPREL
: name
= "JMPREL"; break;
1766 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1767 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1768 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1769 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1770 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1771 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1772 case DT_FLAGS
: name
= "FLAGS"; break;
1773 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1774 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1775 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1776 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1777 case DT_MOVEENT
: name
= "MOVEENT"; break;
1778 case DT_MOVESZ
: name
= "MOVESZ"; break;
1779 case DT_FEATURE
: name
= "FEATURE"; break;
1780 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1781 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1782 case DT_SYMINENT
: name
= "SYMINENT"; break;
1783 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1784 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1785 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1786 case DT_PLTPAD
: name
= "PLTPAD"; break;
1787 case DT_MOVETAB
: name
= "MOVETAB"; break;
1788 case DT_SYMINFO
: name
= "SYMINFO"; break;
1789 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1790 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1791 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1792 case DT_VERSYM
: name
= "VERSYM"; break;
1793 case DT_VERDEF
: name
= "VERDEF"; break;
1794 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1795 case DT_VERNEED
: name
= "VERNEED"; break;
1796 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1797 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1798 case DT_USED
: name
= "USED"; break;
1799 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1800 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1803 fprintf (f
, " %-20s ", name
);
1807 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1812 unsigned int tagv
= dyn
.d_un
.d_val
;
1814 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1817 fprintf (f
, "%s", string
);
1826 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1827 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1829 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1833 if (elf_dynverdef (abfd
) != 0)
1835 Elf_Internal_Verdef
*t
;
1837 fprintf (f
, _("\nVersion definitions:\n"));
1838 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1840 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1841 t
->vd_flags
, t
->vd_hash
,
1842 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1843 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1845 Elf_Internal_Verdaux
*a
;
1848 for (a
= t
->vd_auxptr
->vda_nextptr
;
1852 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1858 if (elf_dynverref (abfd
) != 0)
1860 Elf_Internal_Verneed
*t
;
1862 fprintf (f
, _("\nVersion References:\n"));
1863 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1865 Elf_Internal_Vernaux
*a
;
1867 fprintf (f
, _(" required from %s:\n"),
1868 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1869 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1870 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1871 a
->vna_flags
, a
->vna_other
,
1872 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1883 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1884 and return symbol version for symbol version itself. */
1887 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1889 bfd_boolean
*hidden
)
1891 const char *version_string
= NULL
;
1892 if (elf_dynversym (abfd
) != 0
1893 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1895 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1897 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1898 vernum
&= VERSYM_VERSION
;
1901 version_string
= "";
1902 else if (vernum
== 1
1903 && (vernum
> elf_tdata (abfd
)->cverdefs
1904 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1906 version_string
= base_p
? "Base" : "";
1907 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1909 const char *nodename
1910 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1911 version_string
= "";
1914 || symbol
->name
== NULL
1915 || strcmp (symbol
->name
, nodename
) != 0)
1916 version_string
= nodename
;
1920 Elf_Internal_Verneed
*t
;
1922 version_string
= _("<corrupt>");
1923 for (t
= elf_tdata (abfd
)->verref
;
1927 Elf_Internal_Vernaux
*a
;
1929 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1931 if (a
->vna_other
== vernum
)
1933 version_string
= a
->vna_nodename
;
1940 return version_string
;
1943 /* Display ELF-specific fields of a symbol. */
1946 bfd_elf_print_symbol (bfd
*abfd
,
1949 bfd_print_symbol_type how
)
1951 FILE *file
= (FILE *) filep
;
1954 case bfd_print_symbol_name
:
1955 fprintf (file
, "%s", symbol
->name
);
1957 case bfd_print_symbol_more
:
1958 fprintf (file
, "elf ");
1959 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1960 fprintf (file
, " %x", symbol
->flags
);
1962 case bfd_print_symbol_all
:
1964 const char *section_name
;
1965 const char *name
= NULL
;
1966 const struct elf_backend_data
*bed
;
1967 unsigned char st_other
;
1969 const char *version_string
;
1972 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1974 bed
= get_elf_backend_data (abfd
);
1975 if (bed
->elf_backend_print_symbol_all
)
1976 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1980 name
= symbol
->name
;
1981 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1984 fprintf (file
, " %s\t", section_name
);
1985 /* Print the "other" value for a symbol. For common symbols,
1986 we've already printed the size; now print the alignment.
1987 For other symbols, we have no specified alignment, and
1988 we've printed the address; now print the size. */
1989 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1990 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1992 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1993 bfd_fprintf_vma (abfd
, file
, val
);
1995 /* If we have version information, print it. */
1996 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2003 fprintf (file
, " %-11s", version_string
);
2008 fprintf (file
, " (%s)", version_string
);
2009 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2014 /* If the st_other field is not zero, print it. */
2015 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2020 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2021 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2022 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2024 /* Some other non-defined flags are also present, so print
2026 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2029 fprintf (file
, " %s", name
);
2035 /* ELF .o/exec file reading */
2037 /* Create a new bfd section from an ELF section header. */
2040 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2042 Elf_Internal_Shdr
*hdr
;
2043 Elf_Internal_Ehdr
*ehdr
;
2044 const struct elf_backend_data
*bed
;
2046 bfd_boolean ret
= TRUE
;
2047 static bfd_boolean
* sections_being_created
= NULL
;
2048 static bfd
* sections_being_created_abfd
= NULL
;
2049 static unsigned int nesting
= 0;
2051 if (shindex
>= elf_numsections (abfd
))
2056 /* PR17512: A corrupt ELF binary might contain a recursive group of
2057 sections, with each the string indices pointing to the next in the
2058 loop. Detect this here, by refusing to load a section that we are
2059 already in the process of loading. We only trigger this test if
2060 we have nested at least three sections deep as normal ELF binaries
2061 can expect to recurse at least once.
2063 FIXME: It would be better if this array was attached to the bfd,
2064 rather than being held in a static pointer. */
2066 if (sections_being_created_abfd
!= abfd
)
2068 free (sections_being_created
);
2069 sections_being_created
= NULL
;
2071 if (sections_being_created
== NULL
)
2073 size_t amt
= elf_numsections (abfd
) * sizeof (bfd_boolean
);
2075 /* PR 26005: Do not use bfd_zalloc here as the memory might
2076 be released before the bfd has been fully scanned. */
2077 sections_being_created
= (bfd_boolean
*) bfd_zmalloc (amt
);
2078 if (sections_being_created
== NULL
)
2080 sections_being_created_abfd
= abfd
;
2082 if (sections_being_created
[shindex
])
2085 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2088 sections_being_created
[shindex
] = TRUE
;
2091 hdr
= elf_elfsections (abfd
)[shindex
];
2092 ehdr
= elf_elfheader (abfd
);
2093 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2098 bed
= get_elf_backend_data (abfd
);
2099 switch (hdr
->sh_type
)
2102 /* Inactive section. Throw it away. */
2105 case SHT_PROGBITS
: /* Normal section with contents. */
2106 case SHT_NOBITS
: /* .bss section. */
2107 case SHT_HASH
: /* .hash section. */
2108 case SHT_NOTE
: /* .note section. */
2109 case SHT_INIT_ARRAY
: /* .init_array section. */
2110 case SHT_FINI_ARRAY
: /* .fini_array section. */
2111 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2112 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2113 case SHT_GNU_HASH
: /* .gnu.hash section. */
2114 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2117 case SHT_DYNAMIC
: /* Dynamic linking information. */
2118 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2121 if (hdr
->sh_link
> elf_numsections (abfd
))
2123 /* PR 10478: Accept Solaris binaries with a sh_link
2124 field set to SHN_BEFORE or SHN_AFTER. */
2125 switch (bfd_get_arch (abfd
))
2128 case bfd_arch_sparc
:
2129 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2130 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2132 /* Otherwise fall through. */
2137 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2139 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2141 Elf_Internal_Shdr
*dynsymhdr
;
2143 /* The shared libraries distributed with hpux11 have a bogus
2144 sh_link field for the ".dynamic" section. Find the
2145 string table for the ".dynsym" section instead. */
2146 if (elf_dynsymtab (abfd
) != 0)
2148 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2149 hdr
->sh_link
= dynsymhdr
->sh_link
;
2153 unsigned int i
, num_sec
;
2155 num_sec
= elf_numsections (abfd
);
2156 for (i
= 1; i
< num_sec
; i
++)
2158 dynsymhdr
= elf_elfsections (abfd
)[i
];
2159 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2161 hdr
->sh_link
= dynsymhdr
->sh_link
;
2169 case SHT_SYMTAB
: /* A symbol table. */
2170 if (elf_onesymtab (abfd
) == shindex
)
2173 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2176 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2178 if (hdr
->sh_size
!= 0)
2180 /* Some assemblers erroneously set sh_info to one with a
2181 zero sh_size. ld sees this as a global symbol count
2182 of (unsigned) -1. Fix it here. */
2187 /* PR 18854: A binary might contain more than one symbol table.
2188 Unusual, but possible. Warn, but continue. */
2189 if (elf_onesymtab (abfd
) != 0)
2192 /* xgettext:c-format */
2193 (_("%pB: warning: multiple symbol tables detected"
2194 " - ignoring the table in section %u"),
2198 elf_onesymtab (abfd
) = shindex
;
2199 elf_symtab_hdr (abfd
) = *hdr
;
2200 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2201 abfd
->flags
|= HAS_SYMS
;
2203 /* Sometimes a shared object will map in the symbol table. If
2204 SHF_ALLOC is set, and this is a shared object, then we also
2205 treat this section as a BFD section. We can not base the
2206 decision purely on SHF_ALLOC, because that flag is sometimes
2207 set in a relocatable object file, which would confuse the
2209 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2210 && (abfd
->flags
& DYNAMIC
) != 0
2211 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2215 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2216 can't read symbols without that section loaded as well. It
2217 is most likely specified by the next section header. */
2219 elf_section_list
* entry
;
2220 unsigned int i
, num_sec
;
2222 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2223 if (entry
->hdr
.sh_link
== shindex
)
2226 num_sec
= elf_numsections (abfd
);
2227 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2229 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2231 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2232 && hdr2
->sh_link
== shindex
)
2237 for (i
= 1; i
< shindex
; i
++)
2239 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2241 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2242 && hdr2
->sh_link
== shindex
)
2247 ret
= bfd_section_from_shdr (abfd
, i
);
2248 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2252 case SHT_DYNSYM
: /* A dynamic symbol table. */
2253 if (elf_dynsymtab (abfd
) == shindex
)
2256 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2259 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2261 if (hdr
->sh_size
!= 0)
2264 /* Some linkers erroneously set sh_info to one with a
2265 zero sh_size. ld sees this as a global symbol count
2266 of (unsigned) -1. Fix it here. */
2271 /* PR 18854: A binary might contain more than one dynamic symbol table.
2272 Unusual, but possible. Warn, but continue. */
2273 if (elf_dynsymtab (abfd
) != 0)
2276 /* xgettext:c-format */
2277 (_("%pB: warning: multiple dynamic symbol tables detected"
2278 " - ignoring the table in section %u"),
2282 elf_dynsymtab (abfd
) = shindex
;
2283 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2284 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2285 abfd
->flags
|= HAS_SYMS
;
2287 /* Besides being a symbol table, we also treat this as a regular
2288 section, so that objcopy can handle it. */
2289 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2292 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2294 elf_section_list
* entry
;
2296 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2297 if (entry
->ndx
== shindex
)
2300 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2303 entry
->ndx
= shindex
;
2305 entry
->next
= elf_symtab_shndx_list (abfd
);
2306 elf_symtab_shndx_list (abfd
) = entry
;
2307 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2311 case SHT_STRTAB
: /* A string table. */
2312 if (hdr
->bfd_section
!= NULL
)
2315 if (ehdr
->e_shstrndx
== shindex
)
2317 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2318 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2322 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2325 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2326 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2330 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2333 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2334 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2335 elf_elfsections (abfd
)[shindex
] = hdr
;
2336 /* We also treat this as a regular section, so that objcopy
2338 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2343 /* If the string table isn't one of the above, then treat it as a
2344 regular section. We need to scan all the headers to be sure,
2345 just in case this strtab section appeared before the above. */
2346 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2348 unsigned int i
, num_sec
;
2350 num_sec
= elf_numsections (abfd
);
2351 for (i
= 1; i
< num_sec
; i
++)
2353 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2354 if (hdr2
->sh_link
== shindex
)
2356 /* Prevent endless recursion on broken objects. */
2359 if (! bfd_section_from_shdr (abfd
, i
))
2361 if (elf_onesymtab (abfd
) == i
)
2363 if (elf_dynsymtab (abfd
) == i
)
2364 goto dynsymtab_strtab
;
2368 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2373 /* *These* do a lot of work -- but build no sections! */
2375 asection
*target_sect
;
2376 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2377 unsigned int num_sec
= elf_numsections (abfd
);
2378 struct bfd_elf_section_data
*esdt
;
2381 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2382 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2385 /* Check for a bogus link to avoid crashing. */
2386 if (hdr
->sh_link
>= num_sec
)
2389 /* xgettext:c-format */
2390 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2391 abfd
, hdr
->sh_link
, name
, shindex
);
2392 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2397 /* For some incomprehensible reason Oracle distributes
2398 libraries for Solaris in which some of the objects have
2399 bogus sh_link fields. It would be nice if we could just
2400 reject them, but, unfortunately, some people need to use
2401 them. We scan through the section headers; if we find only
2402 one suitable symbol table, we clobber the sh_link to point
2403 to it. I hope this doesn't break anything.
2405 Don't do it on executable nor shared library. */
2406 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2407 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2408 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2414 for (scan
= 1; scan
< num_sec
; scan
++)
2416 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2417 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2428 hdr
->sh_link
= found
;
2431 /* Get the symbol table. */
2432 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2433 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2434 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2437 /* If this is an alloc section in an executable or shared
2438 library, or the reloc section does not use the main symbol
2439 table we don't treat it as a reloc section. BFD can't
2440 adequately represent such a section, so at least for now,
2441 we don't try. We just present it as a normal section. We
2442 also can't use it as a reloc section if it points to the
2443 null section, an invalid section, another reloc section, or
2444 its sh_link points to the null section. */
2445 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2446 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2447 || hdr
->sh_link
== SHN_UNDEF
2448 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2449 || hdr
->sh_info
== SHN_UNDEF
2450 || hdr
->sh_info
>= num_sec
2451 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2452 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2454 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2459 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2462 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2463 if (target_sect
== NULL
)
2466 esdt
= elf_section_data (target_sect
);
2467 if (hdr
->sh_type
== SHT_RELA
)
2468 p_hdr
= &esdt
->rela
.hdr
;
2470 p_hdr
= &esdt
->rel
.hdr
;
2472 /* PR 17512: file: 0b4f81b7.
2473 Also see PR 24456, for a file which deliberately has two reloc
2477 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2480 /* xgettext:c-format */
2481 (_("%pB: warning: secondary relocation section '%s' "
2482 "for section %pA found - ignoring"),
2483 abfd
, name
, target_sect
);
2488 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2493 elf_elfsections (abfd
)[shindex
] = hdr2
;
2494 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2495 * bed
->s
->int_rels_per_ext_rel
);
2496 target_sect
->flags
|= SEC_RELOC
;
2497 target_sect
->relocation
= NULL
;
2498 target_sect
->rel_filepos
= hdr
->sh_offset
;
2499 /* In the section to which the relocations apply, mark whether
2500 its relocations are of the REL or RELA variety. */
2501 if (hdr
->sh_size
!= 0)
2503 if (hdr
->sh_type
== SHT_RELA
)
2504 target_sect
->use_rela_p
= 1;
2506 abfd
->flags
|= HAS_RELOC
;
2510 case SHT_GNU_verdef
:
2511 elf_dynverdef (abfd
) = shindex
;
2512 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2513 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2516 case SHT_GNU_versym
:
2517 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2520 elf_dynversym (abfd
) = shindex
;
2521 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2522 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2525 case SHT_GNU_verneed
:
2526 elf_dynverref (abfd
) = shindex
;
2527 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2528 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2535 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2538 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2544 /* Possibly an attributes section. */
2545 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2546 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2548 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2550 _bfd_elf_parse_attributes (abfd
, hdr
);
2554 /* Check for any processor-specific section types. */
2555 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2558 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2560 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2561 /* FIXME: How to properly handle allocated section reserved
2562 for applications? */
2564 /* xgettext:c-format */
2565 (_("%pB: unknown type [%#x] section `%s'"),
2566 abfd
, hdr
->sh_type
, name
);
2569 /* Allow sections reserved for applications. */
2570 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2575 else if (hdr
->sh_type
>= SHT_LOPROC
2576 && hdr
->sh_type
<= SHT_HIPROC
)
2577 /* FIXME: We should handle this section. */
2579 /* xgettext:c-format */
2580 (_("%pB: unknown type [%#x] section `%s'"),
2581 abfd
, hdr
->sh_type
, name
);
2582 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2584 /* Unrecognised OS-specific sections. */
2585 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2586 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2587 required to correctly process the section and the file should
2588 be rejected with an error message. */
2590 /* xgettext:c-format */
2591 (_("%pB: unknown type [%#x] section `%s'"),
2592 abfd
, hdr
->sh_type
, name
);
2595 /* Otherwise it should be processed. */
2596 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2601 /* FIXME: We should handle this section. */
2603 /* xgettext:c-format */
2604 (_("%pB: unknown type [%#x] section `%s'"),
2605 abfd
, hdr
->sh_type
, name
);
2613 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2614 sections_being_created
[shindex
] = FALSE
;
2615 if (-- nesting
== 0)
2617 free (sections_being_created
);
2618 sections_being_created
= NULL
;
2619 sections_being_created_abfd
= NULL
;
2624 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2627 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2629 unsigned long r_symndx
)
2631 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2633 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2635 Elf_Internal_Shdr
*symtab_hdr
;
2636 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2637 Elf_External_Sym_Shndx eshndx
;
2639 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2640 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2641 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2644 if (cache
->abfd
!= abfd
)
2646 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2649 cache
->indx
[ent
] = r_symndx
;
2652 return &cache
->sym
[ent
];
2655 /* Given an ELF section number, retrieve the corresponding BFD
2659 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2661 if (sec_index
>= elf_numsections (abfd
))
2663 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2666 static const struct bfd_elf_special_section special_sections_b
[] =
2668 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2669 { NULL
, 0, 0, 0, 0 }
2672 static const struct bfd_elf_special_section special_sections_c
[] =
2674 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2675 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2676 { NULL
, 0, 0, 0, 0 }
2679 static const struct bfd_elf_special_section special_sections_d
[] =
2681 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2682 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2683 /* There are more DWARF sections than these, but they needn't be added here
2684 unless you have to cope with broken compilers that don't emit section
2685 attributes or you want to help the user writing assembler. */
2686 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2687 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2688 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2689 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2690 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2691 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2692 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2693 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2694 { NULL
, 0, 0, 0, 0 }
2697 static const struct bfd_elf_special_section special_sections_f
[] =
2699 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2700 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2701 { NULL
, 0 , 0, 0, 0 }
2704 static const struct bfd_elf_special_section special_sections_g
[] =
2706 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2707 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2708 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2709 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2710 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2711 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2712 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2713 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2714 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2715 { NULL
, 0, 0, 0, 0 }
2718 static const struct bfd_elf_special_section special_sections_h
[] =
2720 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2721 { NULL
, 0, 0, 0, 0 }
2724 static const struct bfd_elf_special_section special_sections_i
[] =
2726 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2727 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2728 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2729 { NULL
, 0, 0, 0, 0 }
2732 static const struct bfd_elf_special_section special_sections_l
[] =
2734 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2735 { NULL
, 0, 0, 0, 0 }
2738 static const struct bfd_elf_special_section special_sections_n
[] =
2740 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2741 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2742 { NULL
, 0, 0, 0, 0 }
2745 static const struct bfd_elf_special_section special_sections_p
[] =
2747 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2748 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2749 { NULL
, 0, 0, 0, 0 }
2752 static const struct bfd_elf_special_section special_sections_r
[] =
2754 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2755 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2756 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2757 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2758 { NULL
, 0, 0, 0, 0 }
2761 static const struct bfd_elf_special_section special_sections_s
[] =
2763 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2764 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2765 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2766 /* See struct bfd_elf_special_section declaration for the semantics of
2767 this special case where .prefix_length != strlen (.prefix). */
2768 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2769 { NULL
, 0, 0, 0, 0 }
2772 static const struct bfd_elf_special_section special_sections_t
[] =
2774 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2775 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2776 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2777 { NULL
, 0, 0, 0, 0 }
2780 static const struct bfd_elf_special_section special_sections_z
[] =
2782 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2783 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2784 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2785 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2786 { NULL
, 0, 0, 0, 0 }
2789 static const struct bfd_elf_special_section
* const special_sections
[] =
2791 special_sections_b
, /* 'b' */
2792 special_sections_c
, /* 'c' */
2793 special_sections_d
, /* 'd' */
2795 special_sections_f
, /* 'f' */
2796 special_sections_g
, /* 'g' */
2797 special_sections_h
, /* 'h' */
2798 special_sections_i
, /* 'i' */
2801 special_sections_l
, /* 'l' */
2803 special_sections_n
, /* 'n' */
2805 special_sections_p
, /* 'p' */
2807 special_sections_r
, /* 'r' */
2808 special_sections_s
, /* 's' */
2809 special_sections_t
, /* 't' */
2815 special_sections_z
/* 'z' */
2818 const struct bfd_elf_special_section
*
2819 _bfd_elf_get_special_section (const char *name
,
2820 const struct bfd_elf_special_section
*spec
,
2826 len
= strlen (name
);
2828 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2831 int prefix_len
= spec
[i
].prefix_length
;
2833 if (len
< prefix_len
)
2835 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2838 suffix_len
= spec
[i
].suffix_length
;
2839 if (suffix_len
<= 0)
2841 if (name
[prefix_len
] != 0)
2843 if (suffix_len
== 0)
2845 if (name
[prefix_len
] != '.'
2846 && (suffix_len
== -2
2847 || (rela
&& spec
[i
].type
== SHT_REL
)))
2853 if (len
< prefix_len
+ suffix_len
)
2855 if (memcmp (name
+ len
- suffix_len
,
2856 spec
[i
].prefix
+ prefix_len
,
2866 const struct bfd_elf_special_section
*
2867 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2870 const struct bfd_elf_special_section
*spec
;
2871 const struct elf_backend_data
*bed
;
2873 /* See if this is one of the special sections. */
2874 if (sec
->name
== NULL
)
2877 bed
= get_elf_backend_data (abfd
);
2878 spec
= bed
->special_sections
;
2881 spec
= _bfd_elf_get_special_section (sec
->name
,
2882 bed
->special_sections
,
2888 if (sec
->name
[0] != '.')
2891 i
= sec
->name
[1] - 'b';
2892 if (i
< 0 || i
> 'z' - 'b')
2895 spec
= special_sections
[i
];
2900 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2904 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2906 struct bfd_elf_section_data
*sdata
;
2907 const struct elf_backend_data
*bed
;
2908 const struct bfd_elf_special_section
*ssect
;
2910 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2913 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2917 sec
->used_by_bfd
= sdata
;
2920 /* Indicate whether or not this section should use RELA relocations. */
2921 bed
= get_elf_backend_data (abfd
);
2922 sec
->use_rela_p
= bed
->default_use_rela_p
;
2924 /* Set up ELF section type and flags for newly created sections, if
2925 there is an ABI mandated section. */
2926 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2929 elf_section_type (sec
) = ssect
->type
;
2930 elf_section_flags (sec
) = ssect
->attr
;
2933 return _bfd_generic_new_section_hook (abfd
, sec
);
2936 /* Create a new bfd section from an ELF program header.
2938 Since program segments have no names, we generate a synthetic name
2939 of the form segment<NUM>, where NUM is generally the index in the
2940 program header table. For segments that are split (see below) we
2941 generate the names segment<NUM>a and segment<NUM>b.
2943 Note that some program segments may have a file size that is different than
2944 (less than) the memory size. All this means is that at execution the
2945 system must allocate the amount of memory specified by the memory size,
2946 but only initialize it with the first "file size" bytes read from the
2947 file. This would occur for example, with program segments consisting
2948 of combined data+bss.
2950 To handle the above situation, this routine generates TWO bfd sections
2951 for the single program segment. The first has the length specified by
2952 the file size of the segment, and the second has the length specified
2953 by the difference between the two sizes. In effect, the segment is split
2954 into its initialized and uninitialized parts.
2959 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2960 Elf_Internal_Phdr
*hdr
,
2962 const char *type_name
)
2969 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
2971 split
= ((hdr
->p_memsz
> 0)
2972 && (hdr
->p_filesz
> 0)
2973 && (hdr
->p_memsz
> hdr
->p_filesz
));
2975 if (hdr
->p_filesz
> 0)
2977 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2978 len
= strlen (namebuf
) + 1;
2979 name
= (char *) bfd_alloc (abfd
, len
);
2982 memcpy (name
, namebuf
, len
);
2983 newsect
= bfd_make_section (abfd
, name
);
2984 if (newsect
== NULL
)
2986 newsect
->vma
= hdr
->p_vaddr
/ opb
;
2987 newsect
->lma
= hdr
->p_paddr
/ opb
;
2988 newsect
->size
= hdr
->p_filesz
;
2989 newsect
->filepos
= hdr
->p_offset
;
2990 newsect
->flags
|= SEC_HAS_CONTENTS
;
2991 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2992 if (hdr
->p_type
== PT_LOAD
)
2994 newsect
->flags
|= SEC_ALLOC
;
2995 newsect
->flags
|= SEC_LOAD
;
2996 if (hdr
->p_flags
& PF_X
)
2998 /* FIXME: all we known is that it has execute PERMISSION,
3000 newsect
->flags
|= SEC_CODE
;
3003 if (!(hdr
->p_flags
& PF_W
))
3005 newsect
->flags
|= SEC_READONLY
;
3009 if (hdr
->p_memsz
> hdr
->p_filesz
)
3013 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3014 len
= strlen (namebuf
) + 1;
3015 name
= (char *) bfd_alloc (abfd
, len
);
3018 memcpy (name
, namebuf
, len
);
3019 newsect
= bfd_make_section (abfd
, name
);
3020 if (newsect
== NULL
)
3022 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3023 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3024 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3025 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3026 align
= newsect
->vma
& -newsect
->vma
;
3027 if (align
== 0 || align
> hdr
->p_align
)
3028 align
= hdr
->p_align
;
3029 newsect
->alignment_power
= bfd_log2 (align
);
3030 if (hdr
->p_type
== PT_LOAD
)
3032 newsect
->flags
|= SEC_ALLOC
;
3033 if (hdr
->p_flags
& PF_X
)
3034 newsect
->flags
|= SEC_CODE
;
3036 if (!(hdr
->p_flags
& PF_W
))
3037 newsect
->flags
|= SEC_READONLY
;
3044 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3046 /* The return value is ignored. Build-ids are considered optional. */
3047 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3048 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3054 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3056 const struct elf_backend_data
*bed
;
3058 switch (hdr
->p_type
)
3061 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3064 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3066 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3067 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3071 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3074 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3077 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3079 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3085 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3088 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3090 case PT_GNU_EH_FRAME
:
3091 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3095 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3098 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3101 /* Check for any processor-specific program segment types. */
3102 bed
= get_elf_backend_data (abfd
);
3103 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3107 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3111 _bfd_elf_single_rel_hdr (asection
*sec
)
3113 if (elf_section_data (sec
)->rel
.hdr
)
3115 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3116 return elf_section_data (sec
)->rel
.hdr
;
3119 return elf_section_data (sec
)->rela
.hdr
;
3123 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3124 Elf_Internal_Shdr
*rel_hdr
,
3125 const char *sec_name
,
3126 bfd_boolean use_rela_p
)
3128 char *name
= (char *) bfd_alloc (abfd
,
3129 sizeof ".rela" + strlen (sec_name
));
3133 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3135 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3137 if (rel_hdr
->sh_name
== (unsigned int) -1)
3143 /* Allocate and initialize a section-header for a new reloc section,
3144 containing relocations against ASECT. It is stored in RELDATA. If
3145 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3149 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3150 struct bfd_elf_section_reloc_data
*reldata
,
3151 const char *sec_name
,
3152 bfd_boolean use_rela_p
,
3153 bfd_boolean delay_st_name_p
)
3155 Elf_Internal_Shdr
*rel_hdr
;
3156 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3158 BFD_ASSERT (reldata
->hdr
== NULL
);
3159 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3160 reldata
->hdr
= rel_hdr
;
3162 if (delay_st_name_p
)
3163 rel_hdr
->sh_name
= (unsigned int) -1;
3164 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3167 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3168 rel_hdr
->sh_entsize
= (use_rela_p
3169 ? bed
->s
->sizeof_rela
3170 : bed
->s
->sizeof_rel
);
3171 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3172 rel_hdr
->sh_flags
= 0;
3173 rel_hdr
->sh_addr
= 0;
3174 rel_hdr
->sh_size
= 0;
3175 rel_hdr
->sh_offset
= 0;
3180 /* Return the default section type based on the passed in section flags. */
3183 bfd_elf_get_default_section_type (flagword flags
)
3185 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3186 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3188 return SHT_PROGBITS
;
3191 struct fake_section_arg
3193 struct bfd_link_info
*link_info
;
3197 /* Set up an ELF internal section header for a section. */
3200 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3202 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3203 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3204 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3205 Elf_Internal_Shdr
*this_hdr
;
3206 unsigned int sh_type
;
3207 const char *name
= asect
->name
;
3208 bfd_boolean delay_st_name_p
= FALSE
;
3213 /* We already failed; just get out of the bfd_map_over_sections
3218 this_hdr
= &esd
->this_hdr
;
3222 /* ld: compress DWARF debug sections with names: .debug_*. */
3223 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3224 && (asect
->flags
& SEC_DEBUGGING
)
3228 /* Set SEC_ELF_COMPRESS to indicate this section should be
3230 asect
->flags
|= SEC_ELF_COMPRESS
;
3231 /* If this section will be compressed, delay adding section
3232 name to section name section after it is compressed in
3233 _bfd_elf_assign_file_positions_for_non_load. */
3234 delay_st_name_p
= TRUE
;
3237 else if ((asect
->flags
& SEC_ELF_RENAME
))
3239 /* objcopy: rename output DWARF debug section. */
3240 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3242 /* When we decompress or compress with SHF_COMPRESSED,
3243 convert section name from .zdebug_* to .debug_* if
3247 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3248 if (new_name
== NULL
)
3256 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3258 /* PR binutils/18087: Compression does not always make a
3259 section smaller. So only rename the section when
3260 compression has actually taken place. If input section
3261 name is .zdebug_*, we should never compress it again. */
3262 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3263 if (new_name
== NULL
)
3268 BFD_ASSERT (name
[1] != 'z');
3273 if (delay_st_name_p
)
3274 this_hdr
->sh_name
= (unsigned int) -1;
3278 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3280 if (this_hdr
->sh_name
== (unsigned int) -1)
3287 /* Don't clear sh_flags. Assembler may set additional bits. */
3289 if ((asect
->flags
& SEC_ALLOC
) != 0
3290 || asect
->user_set_vma
)
3291 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3293 this_hdr
->sh_addr
= 0;
3295 this_hdr
->sh_offset
= 0;
3296 this_hdr
->sh_size
= asect
->size
;
3297 this_hdr
->sh_link
= 0;
3298 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3299 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3302 /* xgettext:c-format */
3303 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3304 abfd
, asect
->alignment_power
, asect
);
3308 /* Set sh_addralign to the highest power of two given by alignment
3309 consistent with the section VMA. Linker scripts can force VMA. */
3310 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3311 this_hdr
->sh_addralign
= mask
& -mask
;
3312 /* The sh_entsize and sh_info fields may have been set already by
3313 copy_private_section_data. */
3315 this_hdr
->bfd_section
= asect
;
3316 this_hdr
->contents
= NULL
;
3318 /* If the section type is unspecified, we set it based on
3320 if ((asect
->flags
& SEC_GROUP
) != 0)
3321 sh_type
= SHT_GROUP
;
3323 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3325 if (this_hdr
->sh_type
== SHT_NULL
)
3326 this_hdr
->sh_type
= sh_type
;
3327 else if (this_hdr
->sh_type
== SHT_NOBITS
3328 && sh_type
== SHT_PROGBITS
3329 && (asect
->flags
& SEC_ALLOC
) != 0)
3331 /* Warn if we are changing a NOBITS section to PROGBITS, but
3332 allow the link to proceed. This can happen when users link
3333 non-bss input sections to bss output sections, or emit data
3334 to a bss output section via a linker script. */
3336 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3337 this_hdr
->sh_type
= sh_type
;
3340 switch (this_hdr
->sh_type
)
3351 case SHT_INIT_ARRAY
:
3352 case SHT_FINI_ARRAY
:
3353 case SHT_PREINIT_ARRAY
:
3354 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3358 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3362 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3366 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3370 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3371 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3375 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3376 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3379 case SHT_GNU_versym
:
3380 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3383 case SHT_GNU_verdef
:
3384 this_hdr
->sh_entsize
= 0;
3385 /* objcopy or strip will copy over sh_info, but may not set
3386 cverdefs. The linker will set cverdefs, but sh_info will be
3388 if (this_hdr
->sh_info
== 0)
3389 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3391 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3392 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3395 case SHT_GNU_verneed
:
3396 this_hdr
->sh_entsize
= 0;
3397 /* objcopy or strip will copy over sh_info, but may not set
3398 cverrefs. The linker will set cverrefs, but sh_info will be
3400 if (this_hdr
->sh_info
== 0)
3401 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3403 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3404 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3408 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3412 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3416 if ((asect
->flags
& SEC_ALLOC
) != 0)
3417 this_hdr
->sh_flags
|= SHF_ALLOC
;
3418 if ((asect
->flags
& SEC_READONLY
) == 0)
3419 this_hdr
->sh_flags
|= SHF_WRITE
;
3420 if ((asect
->flags
& SEC_CODE
) != 0)
3421 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3422 if ((asect
->flags
& SEC_MERGE
) != 0)
3424 this_hdr
->sh_flags
|= SHF_MERGE
;
3425 this_hdr
->sh_entsize
= asect
->entsize
;
3427 if ((asect
->flags
& SEC_STRINGS
) != 0)
3428 this_hdr
->sh_flags
|= SHF_STRINGS
;
3429 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3430 this_hdr
->sh_flags
|= SHF_GROUP
;
3431 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3433 this_hdr
->sh_flags
|= SHF_TLS
;
3434 if (asect
->size
== 0
3435 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3437 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3439 this_hdr
->sh_size
= 0;
3442 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3443 if (this_hdr
->sh_size
!= 0)
3444 this_hdr
->sh_type
= SHT_NOBITS
;
3448 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3449 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3451 /* If the section has relocs, set up a section header for the
3452 SHT_REL[A] section. If two relocation sections are required for
3453 this section, it is up to the processor-specific back-end to
3454 create the other. */
3455 if ((asect
->flags
& SEC_RELOC
) != 0)
3457 /* When doing a relocatable link, create both REL and RELA sections if
3460 /* Do the normal setup if we wouldn't create any sections here. */
3461 && esd
->rel
.count
+ esd
->rela
.count
> 0
3462 && (bfd_link_relocatable (arg
->link_info
)
3463 || arg
->link_info
->emitrelocations
))
3465 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3466 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3467 FALSE
, delay_st_name_p
))
3472 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3473 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3474 TRUE
, delay_st_name_p
))
3480 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3482 ? &esd
->rela
: &esd
->rel
),
3492 /* Check for processor-specific section types. */
3493 sh_type
= this_hdr
->sh_type
;
3494 if (bed
->elf_backend_fake_sections
3495 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3501 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3503 /* Don't change the header type from NOBITS if we are being
3504 called for objcopy --only-keep-debug. */
3505 this_hdr
->sh_type
= sh_type
;
3509 /* Fill in the contents of a SHT_GROUP section. Called from
3510 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3511 when ELF targets use the generic linker, ld. Called for ld -r
3512 from bfd_elf_final_link. */
3515 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3517 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3518 asection
*elt
, *first
;
3522 /* Ignore linker created group section. See elfNN_ia64_object_p in
3524 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3529 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3531 unsigned long symindx
= 0;
3533 /* elf_group_id will have been set up by objcopy and the
3535 if (elf_group_id (sec
) != NULL
)
3536 symindx
= elf_group_id (sec
)->udata
.i
;
3540 /* If called from the assembler, swap_out_syms will have set up
3542 PR 25699: A corrupt input file could contain bogus group info. */
3543 if (elf_section_syms (abfd
) == NULL
)
3548 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3550 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3552 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3554 /* The ELF backend linker sets sh_info to -2 when the group
3555 signature symbol is global, and thus the index can't be
3556 set until all local symbols are output. */
3558 struct bfd_elf_section_data
*sec_data
;
3559 unsigned long symndx
;
3560 unsigned long extsymoff
;
3561 struct elf_link_hash_entry
*h
;
3563 /* The point of this little dance to the first SHF_GROUP section
3564 then back to the SHT_GROUP section is that this gets us to
3565 the SHT_GROUP in the input object. */
3566 igroup
= elf_sec_group (elf_next_in_group (sec
));
3567 sec_data
= elf_section_data (igroup
);
3568 symndx
= sec_data
->this_hdr
.sh_info
;
3570 if (!elf_bad_symtab (igroup
->owner
))
3572 Elf_Internal_Shdr
*symtab_hdr
;
3574 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3575 extsymoff
= symtab_hdr
->sh_info
;
3577 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3578 while (h
->root
.type
== bfd_link_hash_indirect
3579 || h
->root
.type
== bfd_link_hash_warning
)
3580 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3582 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3585 /* The contents won't be allocated for "ld -r" or objcopy. */
3587 if (sec
->contents
== NULL
)
3590 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3592 /* Arrange for the section to be written out. */
3593 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3594 if (sec
->contents
== NULL
)
3601 loc
= sec
->contents
+ sec
->size
;
3603 /* Get the pointer to the first section in the group that gas
3604 squirreled away here. objcopy arranges for this to be set to the
3605 start of the input section group. */
3606 first
= elt
= elf_next_in_group (sec
);
3608 /* First element is a flag word. Rest of section is elf section
3609 indices for all the sections of the group. Write them backwards
3610 just to keep the group in the same order as given in .section
3611 directives, not that it matters. */
3618 s
= s
->output_section
;
3620 && !bfd_is_abs_section (s
))
3622 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3623 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3625 if (elf_sec
->rel
.hdr
!= NULL
3627 || (input_elf_sec
->rel
.hdr
!= NULL
3628 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3630 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3632 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3634 if (elf_sec
->rela
.hdr
!= NULL
3636 || (input_elf_sec
->rela
.hdr
!= NULL
3637 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3639 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3641 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3644 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3646 elt
= elf_next_in_group (elt
);
3652 BFD_ASSERT (loc
== sec
->contents
);
3654 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3657 /* Given NAME, the name of a relocation section stripped of its
3658 .rel/.rela prefix, return the section in ABFD to which the
3659 relocations apply. */
3662 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3664 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3665 section likely apply to .got.plt or .got section. */
3666 if (get_elf_backend_data (abfd
)->want_got_plt
3667 && strcmp (name
, ".plt") == 0)
3672 sec
= bfd_get_section_by_name (abfd
, name
);
3678 return bfd_get_section_by_name (abfd
, name
);
3681 /* Return the section to which RELOC_SEC applies. */
3684 elf_get_reloc_section (asection
*reloc_sec
)
3689 const struct elf_backend_data
*bed
;
3691 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3692 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3695 /* We look up the section the relocs apply to by name. */
3696 name
= reloc_sec
->name
;
3697 if (strncmp (name
, ".rel", 4) != 0)
3700 if (type
== SHT_RELA
&& *name
++ != 'a')
3703 abfd
= reloc_sec
->owner
;
3704 bed
= get_elf_backend_data (abfd
);
3705 return bed
->get_reloc_section (abfd
, name
);
3708 /* Assign all ELF section numbers. The dummy first section is handled here
3709 too. The link/info pointers for the standard section types are filled
3710 in here too, while we're at it. LINK_INFO will be 0 when arriving
3711 here for objcopy, and when using the generic ELF linker. */
3714 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3716 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3718 unsigned int section_number
;
3719 Elf_Internal_Shdr
**i_shdrp
;
3720 struct bfd_elf_section_data
*d
;
3721 bfd_boolean need_symtab
;
3726 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3728 /* SHT_GROUP sections are in relocatable files only. */
3729 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3731 size_t reloc_count
= 0;
3733 /* Put SHT_GROUP sections first. */
3734 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3736 d
= elf_section_data (sec
);
3738 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3740 if (sec
->flags
& SEC_LINKER_CREATED
)
3742 /* Remove the linker created SHT_GROUP sections. */
3743 bfd_section_list_remove (abfd
, sec
);
3744 abfd
->section_count
--;
3747 d
->this_idx
= section_number
++;
3750 /* Count relocations. */
3751 reloc_count
+= sec
->reloc_count
;
3754 /* Clear HAS_RELOC if there are no relocations. */
3755 if (reloc_count
== 0)
3756 abfd
->flags
&= ~HAS_RELOC
;
3759 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3761 d
= elf_section_data (sec
);
3763 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3764 d
->this_idx
= section_number
++;
3765 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3766 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3769 d
->rel
.idx
= section_number
++;
3770 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3771 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3778 d
->rela
.idx
= section_number
++;
3779 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3780 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3786 need_symtab
= (bfd_get_symcount (abfd
) > 0
3787 || (link_info
== NULL
3788 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3792 elf_onesymtab (abfd
) = section_number
++;
3793 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3794 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3796 elf_section_list
*entry
;
3798 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3800 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3801 entry
->ndx
= section_number
++;
3802 elf_symtab_shndx_list (abfd
) = entry
;
3804 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3805 ".symtab_shndx", FALSE
);
3806 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3809 elf_strtab_sec (abfd
) = section_number
++;
3810 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3813 elf_shstrtab_sec (abfd
) = section_number
++;
3814 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3815 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3817 if (section_number
>= SHN_LORESERVE
)
3819 /* xgettext:c-format */
3820 _bfd_error_handler (_("%pB: too many sections: %u"),
3821 abfd
, section_number
);
3825 elf_numsections (abfd
) = section_number
;
3826 elf_elfheader (abfd
)->e_shnum
= section_number
;
3828 /* Set up the list of section header pointers, in agreement with the
3830 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3831 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3832 if (i_shdrp
== NULL
)
3835 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3836 sizeof (Elf_Internal_Shdr
));
3837 if (i_shdrp
[0] == NULL
)
3839 bfd_release (abfd
, i_shdrp
);
3843 elf_elfsections (abfd
) = i_shdrp
;
3845 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3848 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3849 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3851 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3852 BFD_ASSERT (entry
!= NULL
);
3853 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3854 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3856 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3857 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3860 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3864 d
= elf_section_data (sec
);
3866 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3867 if (d
->rel
.idx
!= 0)
3868 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3869 if (d
->rela
.idx
!= 0)
3870 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3872 /* Fill in the sh_link and sh_info fields while we're at it. */
3874 /* sh_link of a reloc section is the section index of the symbol
3875 table. sh_info is the section index of the section to which
3876 the relocation entries apply. */
3877 if (d
->rel
.idx
!= 0)
3879 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3880 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3881 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3883 if (d
->rela
.idx
!= 0)
3885 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3886 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3887 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3890 /* We need to set up sh_link for SHF_LINK_ORDER. */
3891 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3893 s
= elf_linked_to_section (sec
);
3894 /* We can now have a NULL linked section pointer.
3895 This happens when the sh_link field is 0, which is done
3896 when a linked to section is discarded but the linking
3897 section has been retained for some reason. */
3900 /* Check discarded linkonce section. */
3901 if (discarded_section (s
))
3905 /* xgettext:c-format */
3906 (_("%pB: sh_link of section `%pA' points to"
3907 " discarded section `%pA' of `%pB'"),
3908 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3909 /* Point to the kept section if it has the same
3910 size as the discarded one. */
3911 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3914 bfd_set_error (bfd_error_bad_value
);
3919 /* Handle objcopy. */
3920 else if (s
->output_section
== NULL
)
3923 /* xgettext:c-format */
3924 (_("%pB: sh_link of section `%pA' points to"
3925 " removed section `%pA' of `%pB'"),
3926 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3927 bfd_set_error (bfd_error_bad_value
);
3930 s
= s
->output_section
;
3931 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3935 switch (d
->this_hdr
.sh_type
)
3939 /* A reloc section which we are treating as a normal BFD
3940 section. sh_link is the section index of the symbol
3941 table. sh_info is the section index of the section to
3942 which the relocation entries apply. We assume that an
3943 allocated reloc section uses the dynamic symbol table.
3944 FIXME: How can we be sure? */
3945 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3947 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3949 s
= elf_get_reloc_section (sec
);
3952 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3953 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3958 /* We assume that a section named .stab*str is a stabs
3959 string section. We look for a section with the same name
3960 but without the trailing ``str'', and set its sh_link
3961 field to point to this section. */
3962 if (CONST_STRNEQ (sec
->name
, ".stab")
3963 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3968 len
= strlen (sec
->name
);
3969 alc
= (char *) bfd_malloc (len
- 2);
3972 memcpy (alc
, sec
->name
, len
- 3);
3973 alc
[len
- 3] = '\0';
3974 s
= bfd_get_section_by_name (abfd
, alc
);
3978 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3980 /* This is a .stab section. */
3981 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
3988 case SHT_GNU_verneed
:
3989 case SHT_GNU_verdef
:
3990 /* sh_link is the section header index of the string table
3991 used for the dynamic entries, or the symbol table, or the
3993 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3995 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3998 case SHT_GNU_LIBLIST
:
3999 /* sh_link is the section header index of the prelink library
4000 list used for the dynamic entries, or the symbol table, or
4001 the version strings. */
4002 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4003 ? ".dynstr" : ".gnu.libstr");
4005 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4010 case SHT_GNU_versym
:
4011 /* sh_link is the section header index of the symbol table
4012 this hash table or version table is for. */
4013 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4015 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4019 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4023 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4024 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4025 debug section name from .debug_* to .zdebug_* if needed. */
4031 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4033 /* If the backend has a special mapping, use it. */
4034 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4035 if (bed
->elf_backend_sym_is_global
)
4036 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4038 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4039 || bfd_is_und_section (bfd_asymbol_section (sym
))
4040 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4043 /* Filter global symbols of ABFD to include in the import library. All
4044 SYMCOUNT symbols of ABFD can be examined from their pointers in
4045 SYMS. Pointers of symbols to keep should be stored contiguously at
4046 the beginning of that array.
4048 Returns the number of symbols to keep. */
4051 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4052 asymbol
**syms
, long symcount
)
4054 long src_count
, dst_count
= 0;
4056 for (src_count
= 0; src_count
< symcount
; src_count
++)
4058 asymbol
*sym
= syms
[src_count
];
4059 char *name
= (char *) bfd_asymbol_name (sym
);
4060 struct bfd_link_hash_entry
*h
;
4062 if (!sym_is_global (abfd
, sym
))
4065 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4068 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4070 if (h
->linker_def
|| h
->ldscript_def
)
4073 syms
[dst_count
++] = sym
;
4076 syms
[dst_count
] = NULL
;
4081 /* Don't output section symbols for sections that are not going to be
4082 output, that are duplicates or there is no BFD section. */
4085 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4087 elf_symbol_type
*type_ptr
;
4092 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4095 if (sym
->section
== NULL
)
4098 type_ptr
= elf_symbol_from (sym
);
4099 return ((type_ptr
!= NULL
4100 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4101 && bfd_is_abs_section (sym
->section
))
4102 || !(sym
->section
->owner
== abfd
4103 || (sym
->section
->output_section
!= NULL
4104 && sym
->section
->output_section
->owner
== abfd
4105 && sym
->section
->output_offset
== 0)
4106 || bfd_is_abs_section (sym
->section
)));
4109 /* Map symbol from it's internal number to the external number, moving
4110 all local symbols to be at the head of the list. */
4113 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4115 unsigned int symcount
= bfd_get_symcount (abfd
);
4116 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4117 asymbol
**sect_syms
;
4118 unsigned int num_locals
= 0;
4119 unsigned int num_globals
= 0;
4120 unsigned int num_locals2
= 0;
4121 unsigned int num_globals2
= 0;
4122 unsigned int max_index
= 0;
4129 fprintf (stderr
, "elf_map_symbols\n");
4133 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4135 if (max_index
< asect
->index
)
4136 max_index
= asect
->index
;
4140 amt
= max_index
* sizeof (asymbol
*);
4141 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4142 if (sect_syms
== NULL
)
4144 elf_section_syms (abfd
) = sect_syms
;
4145 elf_num_section_syms (abfd
) = max_index
;
4147 /* Init sect_syms entries for any section symbols we have already
4148 decided to output. */
4149 for (idx
= 0; idx
< symcount
; idx
++)
4151 asymbol
*sym
= syms
[idx
];
4153 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4155 && !ignore_section_sym (abfd
, sym
)
4156 && !bfd_is_abs_section (sym
->section
))
4158 asection
*sec
= sym
->section
;
4160 if (sec
->owner
!= abfd
)
4161 sec
= sec
->output_section
;
4163 sect_syms
[sec
->index
] = syms
[idx
];
4167 /* Classify all of the symbols. */
4168 for (idx
= 0; idx
< symcount
; idx
++)
4170 if (sym_is_global (abfd
, syms
[idx
]))
4172 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4176 /* We will be adding a section symbol for each normal BFD section. Most
4177 sections will already have a section symbol in outsymbols, but
4178 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4179 at least in that case. */
4180 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4182 if (sect_syms
[asect
->index
] == NULL
)
4184 if (!sym_is_global (abfd
, asect
->symbol
))
4191 /* Now sort the symbols so the local symbols are first. */
4192 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4193 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4194 if (new_syms
== NULL
)
4197 for (idx
= 0; idx
< symcount
; idx
++)
4199 asymbol
*sym
= syms
[idx
];
4202 if (sym_is_global (abfd
, sym
))
4203 i
= num_locals
+ num_globals2
++;
4204 else if (!ignore_section_sym (abfd
, sym
))
4209 sym
->udata
.i
= i
+ 1;
4211 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4213 if (sect_syms
[asect
->index
] == NULL
)
4215 asymbol
*sym
= asect
->symbol
;
4218 sect_syms
[asect
->index
] = sym
;
4219 if (!sym_is_global (abfd
, sym
))
4222 i
= num_locals
+ num_globals2
++;
4224 sym
->udata
.i
= i
+ 1;
4228 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4230 *pnum_locals
= num_locals
;
4234 /* Align to the maximum file alignment that could be required for any
4235 ELF data structure. */
4237 static inline file_ptr
4238 align_file_position (file_ptr off
, int align
)
4240 return (off
+ align
- 1) & ~(align
- 1);
4243 /* Assign a file position to a section, optionally aligning to the
4244 required section alignment. */
4247 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4251 if (align
&& i_shdrp
->sh_addralign
> 1)
4252 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4253 i_shdrp
->sh_offset
= offset
;
4254 if (i_shdrp
->bfd_section
!= NULL
)
4255 i_shdrp
->bfd_section
->filepos
= offset
;
4256 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4257 offset
+= i_shdrp
->sh_size
;
4261 /* Compute the file positions we are going to put the sections at, and
4262 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4263 is not NULL, this is being called by the ELF backend linker. */
4266 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4267 struct bfd_link_info
*link_info
)
4269 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4270 struct fake_section_arg fsargs
;
4272 struct elf_strtab_hash
*strtab
= NULL
;
4273 Elf_Internal_Shdr
*shstrtab_hdr
;
4274 bfd_boolean need_symtab
;
4276 if (abfd
->output_has_begun
)
4279 /* Do any elf backend specific processing first. */
4280 if (bed
->elf_backend_begin_write_processing
)
4281 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4283 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4286 fsargs
.failed
= FALSE
;
4287 fsargs
.link_info
= link_info
;
4288 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4292 if (!assign_section_numbers (abfd
, link_info
))
4295 /* The backend linker builds symbol table information itself. */
4296 need_symtab
= (link_info
== NULL
4297 && (bfd_get_symcount (abfd
) > 0
4298 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4302 /* Non-zero if doing a relocatable link. */
4303 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4305 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4310 if (link_info
== NULL
)
4312 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4317 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4318 /* sh_name was set in init_file_header. */
4319 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4320 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4321 shstrtab_hdr
->sh_addr
= 0;
4322 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4323 shstrtab_hdr
->sh_entsize
= 0;
4324 shstrtab_hdr
->sh_link
= 0;
4325 shstrtab_hdr
->sh_info
= 0;
4326 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4327 shstrtab_hdr
->sh_addralign
= 1;
4329 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4335 Elf_Internal_Shdr
*hdr
;
4337 off
= elf_next_file_pos (abfd
);
4339 hdr
= & elf_symtab_hdr (abfd
);
4340 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4342 if (elf_symtab_shndx_list (abfd
) != NULL
)
4344 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4345 if (hdr
->sh_size
!= 0)
4346 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4347 /* FIXME: What about other symtab_shndx sections in the list ? */
4350 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4351 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4353 elf_next_file_pos (abfd
) = off
;
4355 /* Now that we know where the .strtab section goes, write it
4357 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4358 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4360 _bfd_elf_strtab_free (strtab
);
4363 abfd
->output_has_begun
= TRUE
;
4368 /* Make an initial estimate of the size of the program header. If we
4369 get the number wrong here, we'll redo section placement. */
4371 static bfd_size_type
4372 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4376 const struct elf_backend_data
*bed
;
4378 /* Assume we will need exactly two PT_LOAD segments: one for text
4379 and one for data. */
4382 s
= bfd_get_section_by_name (abfd
, ".interp");
4383 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4385 /* If we have a loadable interpreter section, we need a
4386 PT_INTERP segment. In this case, assume we also need a
4387 PT_PHDR segment, although that may not be true for all
4392 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4394 /* We need a PT_DYNAMIC segment. */
4398 if (info
!= NULL
&& info
->relro
)
4400 /* We need a PT_GNU_RELRO segment. */
4404 if (elf_eh_frame_hdr (abfd
))
4406 /* We need a PT_GNU_EH_FRAME segment. */
4410 if (elf_stack_flags (abfd
))
4412 /* We need a PT_GNU_STACK segment. */
4416 s
= bfd_get_section_by_name (abfd
,
4417 NOTE_GNU_PROPERTY_SECTION_NAME
);
4418 if (s
!= NULL
&& s
->size
!= 0)
4420 /* We need a PT_GNU_PROPERTY segment. */
4424 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4426 if ((s
->flags
& SEC_LOAD
) != 0
4427 && elf_section_type (s
) == SHT_NOTE
)
4429 unsigned int alignment_power
;
4430 /* We need a PT_NOTE segment. */
4432 /* Try to create just one PT_NOTE segment for all adjacent
4433 loadable SHT_NOTE sections. gABI requires that within a
4434 PT_NOTE segment (and also inside of each SHT_NOTE section)
4435 each note should have the same alignment. So we check
4436 whether the sections are correctly aligned. */
4437 alignment_power
= s
->alignment_power
;
4438 while (s
->next
!= NULL
4439 && s
->next
->alignment_power
== alignment_power
4440 && (s
->next
->flags
& SEC_LOAD
) != 0
4441 && elf_section_type (s
->next
) == SHT_NOTE
)
4446 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4448 if (s
->flags
& SEC_THREAD_LOCAL
)
4450 /* We need a PT_TLS segment. */
4456 bed
= get_elf_backend_data (abfd
);
4458 if ((abfd
->flags
& D_PAGED
) != 0
4459 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4461 /* Add a PT_GNU_MBIND segment for each mbind section. */
4462 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4463 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4464 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4466 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4469 /* xgettext:c-format */
4470 (_("%pB: GNU_MBIND section `%pA' has invalid "
4471 "sh_info field: %d"),
4472 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4475 /* Align mbind section to page size. */
4476 if (s
->alignment_power
< page_align_power
)
4477 s
->alignment_power
= page_align_power
;
4482 /* Let the backend count up any program headers it might need. */
4483 if (bed
->elf_backend_additional_program_headers
)
4487 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4493 return segs
* bed
->s
->sizeof_phdr
;
4496 /* Find the segment that contains the output_section of section. */
4499 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4501 struct elf_segment_map
*m
;
4502 Elf_Internal_Phdr
*p
;
4504 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4510 for (i
= m
->count
- 1; i
>= 0; i
--)
4511 if (m
->sections
[i
] == section
)
4518 /* Create a mapping from a set of sections to a program segment. */
4520 static struct elf_segment_map
*
4521 make_mapping (bfd
*abfd
,
4522 asection
**sections
,
4527 struct elf_segment_map
*m
;
4532 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4533 amt
+= (to
- from
) * sizeof (asection
*);
4534 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4538 m
->p_type
= PT_LOAD
;
4539 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4540 m
->sections
[i
- from
] = *hdrpp
;
4541 m
->count
= to
- from
;
4543 if (from
== 0 && phdr
)
4545 /* Include the headers in the first PT_LOAD segment. */
4546 m
->includes_filehdr
= 1;
4547 m
->includes_phdrs
= 1;
4553 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4556 struct elf_segment_map
*
4557 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4559 struct elf_segment_map
*m
;
4561 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4562 sizeof (struct elf_segment_map
));
4566 m
->p_type
= PT_DYNAMIC
;
4568 m
->sections
[0] = dynsec
;
4573 /* Possibly add or remove segments from the segment map. */
4576 elf_modify_segment_map (bfd
*abfd
,
4577 struct bfd_link_info
*info
,
4578 bfd_boolean remove_empty_load
)
4580 struct elf_segment_map
**m
;
4581 const struct elf_backend_data
*bed
;
4583 /* The placement algorithm assumes that non allocated sections are
4584 not in PT_LOAD segments. We ensure this here by removing such
4585 sections from the segment map. We also remove excluded
4586 sections. Finally, any PT_LOAD segment without sections is
4588 m
= &elf_seg_map (abfd
);
4591 unsigned int i
, new_count
;
4593 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4595 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4596 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4597 || (*m
)->p_type
!= PT_LOAD
))
4599 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4603 (*m
)->count
= new_count
;
4605 if (remove_empty_load
4606 && (*m
)->p_type
== PT_LOAD
4608 && !(*m
)->includes_phdrs
)
4614 bed
= get_elf_backend_data (abfd
);
4615 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4617 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4624 #define IS_TBSS(s) \
4625 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4627 /* Set up a mapping from BFD sections to program segments. */
4630 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4633 struct elf_segment_map
*m
;
4634 asection
**sections
= NULL
;
4635 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4636 bfd_boolean no_user_phdrs
;
4638 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4641 info
->user_phdrs
= !no_user_phdrs
;
4643 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4647 struct elf_segment_map
*mfirst
;
4648 struct elf_segment_map
**pm
;
4651 unsigned int hdr_index
;
4652 bfd_vma maxpagesize
;
4654 bfd_boolean phdr_in_segment
;
4655 bfd_boolean writable
;
4656 bfd_boolean executable
;
4657 unsigned int tls_count
= 0;
4658 asection
*first_tls
= NULL
;
4659 asection
*first_mbind
= NULL
;
4660 asection
*dynsec
, *eh_frame_hdr
;
4662 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4663 bfd_size_type phdr_size
; /* Octets/bytes. */
4664 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4666 /* Select the allocated sections, and sort them. */
4668 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4669 sections
= (asection
**) bfd_malloc (amt
);
4670 if (sections
== NULL
)
4673 /* Calculate top address, avoiding undefined behaviour of shift
4674 left operator when shift count is equal to size of type
4676 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4677 addr_mask
= (addr_mask
<< 1) + 1;
4680 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4682 if ((s
->flags
& SEC_ALLOC
) != 0)
4684 /* target_index is unused until bfd_elf_final_link
4685 starts output of section symbols. Use it to make
4687 s
->target_index
= i
;
4690 /* A wrapping section potentially clashes with header. */
4691 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4692 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4695 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4698 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4700 phdr_size
= elf_program_header_size (abfd
);
4701 if (phdr_size
== (bfd_size_type
) -1)
4702 phdr_size
= get_program_header_size (abfd
, info
);
4703 phdr_size
+= bed
->s
->sizeof_ehdr
;
4704 /* phdr_size is compared to LMA values which are in bytes. */
4706 maxpagesize
= bed
->maxpagesize
;
4707 if (maxpagesize
== 0)
4709 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4711 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4712 >= (phdr_size
& (maxpagesize
- 1))))
4713 /* For compatibility with old scripts that may not be using
4714 SIZEOF_HEADERS, add headers when it looks like space has
4715 been left for them. */
4716 phdr_in_segment
= TRUE
;
4718 /* Build the mapping. */
4722 /* If we have a .interp section, then create a PT_PHDR segment for
4723 the program headers and a PT_INTERP segment for the .interp
4725 s
= bfd_get_section_by_name (abfd
, ".interp");
4726 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4728 amt
= sizeof (struct elf_segment_map
);
4729 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4733 m
->p_type
= PT_PHDR
;
4735 m
->p_flags_valid
= 1;
4736 m
->includes_phdrs
= 1;
4737 phdr_in_segment
= TRUE
;
4741 amt
= sizeof (struct elf_segment_map
);
4742 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4746 m
->p_type
= PT_INTERP
;
4754 /* Look through the sections. We put sections in the same program
4755 segment when the start of the second section can be placed within
4756 a few bytes of the end of the first section. */
4762 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4764 && (dynsec
->flags
& SEC_LOAD
) == 0)
4767 if ((abfd
->flags
& D_PAGED
) == 0)
4768 phdr_in_segment
= FALSE
;
4770 /* Deal with -Ttext or something similar such that the first section
4771 is not adjacent to the program headers. This is an
4772 approximation, since at this point we don't know exactly how many
4773 program headers we will need. */
4774 if (phdr_in_segment
&& count
> 0)
4776 bfd_vma phdr_lma
; /* Bytes. */
4777 bfd_boolean separate_phdr
= FALSE
;
4779 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4781 && info
->separate_code
4782 && (sections
[0]->flags
& SEC_CODE
) != 0)
4784 /* If data sections should be separate from code and
4785 thus not executable, and the first section is
4786 executable then put the file and program headers in
4787 their own PT_LOAD. */
4788 separate_phdr
= TRUE
;
4789 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4790 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4792 /* The file and program headers are currently on the
4793 same page as the first section. Put them on the
4794 previous page if we can. */
4795 if (phdr_lma
>= maxpagesize
)
4796 phdr_lma
-= maxpagesize
;
4798 separate_phdr
= FALSE
;
4801 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4802 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4803 /* If file and program headers would be placed at the end
4804 of memory then it's probably better to omit them. */
4805 phdr_in_segment
= FALSE
;
4806 else if (phdr_lma
< wrap_to
)
4807 /* If a section wraps around to where we'll be placing
4808 file and program headers, then the headers will be
4810 phdr_in_segment
= FALSE
;
4811 else if (separate_phdr
)
4813 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4816 m
->p_paddr
= phdr_lma
* opb
;
4818 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4819 m
->p_paddr_valid
= 1;
4822 phdr_in_segment
= FALSE
;
4826 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4829 bfd_boolean new_segment
;
4833 /* See if this section and the last one will fit in the same
4836 if (last_hdr
== NULL
)
4838 /* If we don't have a segment yet, then we don't need a new
4839 one (we build the last one after this loop). */
4840 new_segment
= FALSE
;
4842 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4844 /* If this section has a different relation between the
4845 virtual address and the load address, then we need a new
4849 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4850 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4852 /* If this section has a load address that makes it overlap
4853 the previous section, then we need a new segment. */
4856 else if ((abfd
->flags
& D_PAGED
) != 0
4857 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4858 == (hdr
->lma
& -maxpagesize
)))
4860 /* If we are demand paged then we can't map two disk
4861 pages onto the same memory page. */
4862 new_segment
= FALSE
;
4864 /* In the next test we have to be careful when last_hdr->lma is close
4865 to the end of the address space. If the aligned address wraps
4866 around to the start of the address space, then there are no more
4867 pages left in memory and it is OK to assume that the current
4868 section can be included in the current segment. */
4869 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4870 + maxpagesize
> last_hdr
->lma
)
4871 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4872 + maxpagesize
<= hdr
->lma
))
4874 /* If putting this section in this segment would force us to
4875 skip a page in the segment, then we need a new segment. */
4878 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4879 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4881 /* We don't want to put a loaded section after a
4882 nonloaded (ie. bss style) section in the same segment
4883 as that will force the non-loaded section to be loaded.
4884 Consider .tbss sections as loaded for this purpose. */
4887 else if ((abfd
->flags
& D_PAGED
) == 0)
4889 /* If the file is not demand paged, which means that we
4890 don't require the sections to be correctly aligned in the
4891 file, then there is no other reason for a new segment. */
4892 new_segment
= FALSE
;
4894 else if (info
!= NULL
4895 && info
->separate_code
4896 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4901 && (hdr
->flags
& SEC_READONLY
) == 0)
4903 /* We don't want to put a writable section in a read only
4909 /* Otherwise, we can use the same segment. */
4910 new_segment
= FALSE
;
4913 /* Allow interested parties a chance to override our decision. */
4914 if (last_hdr
!= NULL
4916 && info
->callbacks
->override_segment_assignment
!= NULL
)
4918 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4924 if ((hdr
->flags
& SEC_READONLY
) == 0)
4926 if ((hdr
->flags
& SEC_CODE
) != 0)
4929 /* .tbss sections effectively have zero size. */
4930 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4934 /* We need a new program segment. We must create a new program
4935 header holding all the sections from hdr_index until hdr. */
4937 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4944 if ((hdr
->flags
& SEC_READONLY
) == 0)
4949 if ((hdr
->flags
& SEC_CODE
) == 0)
4955 /* .tbss sections effectively have zero size. */
4956 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4958 phdr_in_segment
= FALSE
;
4961 /* Create a final PT_LOAD program segment, but not if it's just
4963 if (last_hdr
!= NULL
4964 && (i
- hdr_index
!= 1
4965 || !IS_TBSS (last_hdr
)))
4967 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4975 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4978 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4985 /* For each batch of consecutive loadable SHT_NOTE sections,
4986 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4987 because if we link together nonloadable .note sections and
4988 loadable .note sections, we will generate two .note sections
4989 in the output file. */
4990 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4992 if ((s
->flags
& SEC_LOAD
) != 0
4993 && elf_section_type (s
) == SHT_NOTE
)
4996 unsigned int alignment_power
= s
->alignment_power
;
4999 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5001 if (s2
->next
->alignment_power
== alignment_power
5002 && (s2
->next
->flags
& SEC_LOAD
) != 0
5003 && elf_section_type (s2
->next
) == SHT_NOTE
5004 && align_power (s2
->lma
+ s2
->size
/ opb
,
5011 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5012 amt
+= count
* sizeof (asection
*);
5013 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5017 m
->p_type
= PT_NOTE
;
5021 m
->sections
[m
->count
- count
--] = s
;
5022 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5025 m
->sections
[m
->count
- 1] = s
;
5026 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5030 if (s
->flags
& SEC_THREAD_LOCAL
)
5036 if (first_mbind
== NULL
5037 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5041 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5044 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5045 amt
+= tls_count
* sizeof (asection
*);
5046 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5051 m
->count
= tls_count
;
5052 /* Mandated PF_R. */
5054 m
->p_flags_valid
= 1;
5056 for (i
= 0; i
< tls_count
; ++i
)
5058 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5061 (_("%pB: TLS sections are not adjacent:"), abfd
);
5064 while (i
< tls_count
)
5066 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5068 _bfd_error_handler (_(" TLS: %pA"), s
);
5072 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5075 bfd_set_error (bfd_error_bad_value
);
5087 && (abfd
->flags
& D_PAGED
) != 0
5088 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5089 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5090 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5091 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5093 /* Mandated PF_R. */
5094 unsigned long p_flags
= PF_R
;
5095 if ((s
->flags
& SEC_READONLY
) == 0)
5097 if ((s
->flags
& SEC_CODE
) != 0)
5100 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5101 m
= bfd_zalloc (abfd
, amt
);
5105 m
->p_type
= (PT_GNU_MBIND_LO
5106 + elf_section_data (s
)->this_hdr
.sh_info
);
5108 m
->p_flags_valid
= 1;
5110 m
->p_flags
= p_flags
;
5116 s
= bfd_get_section_by_name (abfd
,
5117 NOTE_GNU_PROPERTY_SECTION_NAME
);
5118 if (s
!= NULL
&& s
->size
!= 0)
5120 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5121 m
= bfd_zalloc (abfd
, amt
);
5125 m
->p_type
= PT_GNU_PROPERTY
;
5127 m
->p_flags_valid
= 1;
5134 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5136 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5137 if (eh_frame_hdr
!= NULL
5138 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5140 amt
= sizeof (struct elf_segment_map
);
5141 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5145 m
->p_type
= PT_GNU_EH_FRAME
;
5147 m
->sections
[0] = eh_frame_hdr
->output_section
;
5153 if (elf_stack_flags (abfd
))
5155 amt
= sizeof (struct elf_segment_map
);
5156 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5160 m
->p_type
= PT_GNU_STACK
;
5161 m
->p_flags
= elf_stack_flags (abfd
);
5162 m
->p_align
= bed
->stack_align
;
5163 m
->p_flags_valid
= 1;
5164 m
->p_align_valid
= m
->p_align
!= 0;
5165 if (info
->stacksize
> 0)
5167 m
->p_size
= info
->stacksize
;
5168 m
->p_size_valid
= 1;
5175 if (info
!= NULL
&& info
->relro
)
5177 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5179 if (m
->p_type
== PT_LOAD
5181 && m
->sections
[0]->vma
>= info
->relro_start
5182 && m
->sections
[0]->vma
< info
->relro_end
)
5185 while (--i
!= (unsigned) -1)
5187 if (m
->sections
[i
]->size
> 0
5188 && (m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5189 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5193 if (i
!= (unsigned) -1)
5198 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5201 amt
= sizeof (struct elf_segment_map
);
5202 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5206 m
->p_type
= PT_GNU_RELRO
;
5213 elf_seg_map (abfd
) = mfirst
;
5216 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5219 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5221 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5230 /* Sort sections by address. */
5233 elf_sort_sections (const void *arg1
, const void *arg2
)
5235 const asection
*sec1
= *(const asection
**) arg1
;
5236 const asection
*sec2
= *(const asection
**) arg2
;
5237 bfd_size_type size1
, size2
;
5239 /* Sort by LMA first, since this is the address used to
5240 place the section into a segment. */
5241 if (sec1
->lma
< sec2
->lma
)
5243 else if (sec1
->lma
> sec2
->lma
)
5246 /* Then sort by VMA. Normally the LMA and the VMA will be
5247 the same, and this will do nothing. */
5248 if (sec1
->vma
< sec2
->vma
)
5250 else if (sec1
->vma
> sec2
->vma
)
5253 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5255 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5262 else if (TOEND (sec2
))
5267 /* Sort by size, to put zero sized sections
5268 before others at the same address. */
5270 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5271 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5278 return sec1
->target_index
- sec2
->target_index
;
5281 /* This qsort comparison functions sorts PT_LOAD segments first and
5282 by p_paddr, for assign_file_positions_for_load_sections. */
5285 elf_sort_segments (const void *arg1
, const void *arg2
)
5287 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5288 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5290 if (m1
->p_type
!= m2
->p_type
)
5292 if (m1
->p_type
== PT_NULL
)
5294 if (m2
->p_type
== PT_NULL
)
5296 return m1
->p_type
< m2
->p_type
? -1 : 1;
5298 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5299 return m1
->includes_filehdr
? -1 : 1;
5300 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5301 return m1
->no_sort_lma
? -1 : 1;
5302 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5304 bfd_vma lma1
, lma2
; /* Octets. */
5306 if (m1
->p_paddr_valid
)
5308 else if (m1
->count
!= 0)
5310 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5312 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5315 if (m2
->p_paddr_valid
)
5317 else if (m2
->count
!= 0)
5319 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5321 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5324 return lma1
< lma2
? -1 : 1;
5326 if (m1
->idx
!= m2
->idx
)
5327 return m1
->idx
< m2
->idx
? -1 : 1;
5331 /* Ian Lance Taylor writes:
5333 We shouldn't be using % with a negative signed number. That's just
5334 not good. We have to make sure either that the number is not
5335 negative, or that the number has an unsigned type. When the types
5336 are all the same size they wind up as unsigned. When file_ptr is a
5337 larger signed type, the arithmetic winds up as signed long long,
5340 What we're trying to say here is something like ``increase OFF by
5341 the least amount that will cause it to be equal to the VMA modulo
5343 /* In other words, something like:
5345 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5346 off_offset = off % bed->maxpagesize;
5347 if (vma_offset < off_offset)
5348 adjustment = vma_offset + bed->maxpagesize - off_offset;
5350 adjustment = vma_offset - off_offset;
5352 which can be collapsed into the expression below. */
5355 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5357 /* PR binutils/16199: Handle an alignment of zero. */
5358 if (maxpagesize
== 0)
5360 return ((vma
- off
) % maxpagesize
);
5364 print_segment_map (const struct elf_segment_map
*m
)
5367 const char *pt
= get_segment_type (m
->p_type
);
5372 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5373 sprintf (buf
, "LOPROC+%7.7x",
5374 (unsigned int) (m
->p_type
- PT_LOPROC
));
5375 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5376 sprintf (buf
, "LOOS+%7.7x",
5377 (unsigned int) (m
->p_type
- PT_LOOS
));
5379 snprintf (buf
, sizeof (buf
), "%8.8x",
5380 (unsigned int) m
->p_type
);
5384 fprintf (stderr
, "%s:", pt
);
5385 for (j
= 0; j
< m
->count
; j
++)
5386 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5392 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5397 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5399 buf
= bfd_zmalloc (len
);
5402 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5407 /* Assign file positions to the sections based on the mapping from
5408 sections to segments. This function also sets up some fields in
5412 assign_file_positions_for_load_sections (bfd
*abfd
,
5413 struct bfd_link_info
*link_info
)
5415 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5416 struct elf_segment_map
*m
;
5417 struct elf_segment_map
*phdr_load_seg
;
5418 Elf_Internal_Phdr
*phdrs
;
5419 Elf_Internal_Phdr
*p
;
5420 file_ptr off
; /* Octets. */
5421 bfd_size_type maxpagesize
;
5422 unsigned int alloc
, actual
;
5424 struct elf_segment_map
**sorted_seg_map
;
5425 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5427 if (link_info
== NULL
5428 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5432 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5437 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5438 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5442 /* PR binutils/12467. */
5443 elf_elfheader (abfd
)->e_phoff
= 0;
5444 elf_elfheader (abfd
)->e_phentsize
= 0;
5447 elf_elfheader (abfd
)->e_phnum
= alloc
;
5449 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5452 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5456 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5457 BFD_ASSERT (elf_program_header_size (abfd
)
5458 == actual
* bed
->s
->sizeof_phdr
);
5459 BFD_ASSERT (actual
>= alloc
);
5464 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5468 /* We're writing the size in elf_program_header_size (abfd),
5469 see assign_file_positions_except_relocs, so make sure we have
5470 that amount allocated, with trailing space cleared.
5471 The variable alloc contains the computed need, while
5472 elf_program_header_size (abfd) contains the size used for the
5474 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5475 where the layout is forced to according to a larger size in the
5476 last iterations for the testcase ld-elf/header. */
5477 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5478 + alloc
* sizeof (*sorted_seg_map
)));
5479 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5480 elf_tdata (abfd
)->phdr
= phdrs
;
5484 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5486 sorted_seg_map
[j
] = m
;
5487 /* If elf_segment_map is not from map_sections_to_segments, the
5488 sections may not be correctly ordered. NOTE: sorting should
5489 not be done to the PT_NOTE section of a corefile, which may
5490 contain several pseudo-sections artificially created by bfd.
5491 Sorting these pseudo-sections breaks things badly. */
5493 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5494 && m
->p_type
== PT_NOTE
))
5496 for (i
= 0; i
< m
->count
; i
++)
5497 m
->sections
[i
]->target_index
= i
;
5498 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5503 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5507 if ((abfd
->flags
& D_PAGED
) != 0)
5508 maxpagesize
= bed
->maxpagesize
;
5510 /* Sections must map to file offsets past the ELF file header. */
5511 off
= bed
->s
->sizeof_ehdr
;
5512 /* And if one of the PT_LOAD headers doesn't include the program
5513 headers then we'll be mapping program headers in the usual
5514 position after the ELF file header. */
5515 phdr_load_seg
= NULL
;
5516 for (j
= 0; j
< alloc
; j
++)
5518 m
= sorted_seg_map
[j
];
5519 if (m
->p_type
!= PT_LOAD
)
5521 if (m
->includes_phdrs
)
5527 if (phdr_load_seg
== NULL
)
5528 off
+= actual
* bed
->s
->sizeof_phdr
;
5530 for (j
= 0; j
< alloc
; j
++)
5533 bfd_vma off_adjust
; /* Octets. */
5534 bfd_boolean no_contents
;
5536 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5537 number of sections with contents contributing to both p_filesz
5538 and p_memsz, followed by a number of sections with no contents
5539 that just contribute to p_memsz. In this loop, OFF tracks next
5540 available file offset for PT_LOAD and PT_NOTE segments. */
5541 m
= sorted_seg_map
[j
];
5543 p
->p_type
= m
->p_type
;
5544 p
->p_flags
= m
->p_flags
;
5547 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5549 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5551 if (m
->p_paddr_valid
)
5552 p
->p_paddr
= m
->p_paddr
;
5553 else if (m
->count
== 0)
5556 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5558 if (p
->p_type
== PT_LOAD
5559 && (abfd
->flags
& D_PAGED
) != 0)
5561 /* p_align in demand paged PT_LOAD segments effectively stores
5562 the maximum page size. When copying an executable with
5563 objcopy, we set m->p_align from the input file. Use this
5564 value for maxpagesize rather than bed->maxpagesize, which
5565 may be different. Note that we use maxpagesize for PT_TLS
5566 segment alignment later in this function, so we are relying
5567 on at least one PT_LOAD segment appearing before a PT_TLS
5569 if (m
->p_align_valid
)
5570 maxpagesize
= m
->p_align
;
5572 p
->p_align
= maxpagesize
;
5574 else if (m
->p_align_valid
)
5575 p
->p_align
= m
->p_align
;
5576 else if (m
->count
== 0)
5577 p
->p_align
= 1 << bed
->s
->log_file_align
;
5579 if (m
== phdr_load_seg
)
5581 if (!m
->includes_filehdr
)
5583 off
+= actual
* bed
->s
->sizeof_phdr
;
5586 no_contents
= FALSE
;
5588 if (p
->p_type
== PT_LOAD
5591 bfd_size_type align
; /* Bytes. */
5592 unsigned int align_power
= 0;
5594 if (m
->p_align_valid
)
5598 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5600 unsigned int secalign
;
5602 secalign
= bfd_section_alignment (*secpp
);
5603 if (secalign
> align_power
)
5604 align_power
= secalign
;
5606 align
= (bfd_size_type
) 1 << align_power
;
5607 if (align
< maxpagesize
)
5608 align
= maxpagesize
;
5611 for (i
= 0; i
< m
->count
; i
++)
5612 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5613 /* If we aren't making room for this section, then
5614 it must be SHT_NOBITS regardless of what we've
5615 set via struct bfd_elf_special_section. */
5616 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5618 /* Find out whether this segment contains any loadable
5621 for (i
= 0; i
< m
->count
; i
++)
5622 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5624 no_contents
= FALSE
;
5628 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5630 /* Broken hardware and/or kernel require that files do not
5631 map the same page with different permissions on some hppa
5634 && (abfd
->flags
& D_PAGED
) != 0
5635 && bed
->no_page_alias
5636 && (off
& (maxpagesize
- 1)) != 0
5637 && ((off
& -maxpagesize
)
5638 == ((off
+ off_adjust
) & -maxpagesize
)))
5639 off_adjust
+= maxpagesize
;
5643 /* We shouldn't need to align the segment on disk since
5644 the segment doesn't need file space, but the gABI
5645 arguably requires the alignment and glibc ld.so
5646 checks it. So to comply with the alignment
5647 requirement but not waste file space, we adjust
5648 p_offset for just this segment. (OFF_ADJUST is
5649 subtracted from OFF later.) This may put p_offset
5650 past the end of file, but that shouldn't matter. */
5655 /* Make sure the .dynamic section is the first section in the
5656 PT_DYNAMIC segment. */
5657 else if (p
->p_type
== PT_DYNAMIC
5659 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5662 (_("%pB: The first section in the PT_DYNAMIC segment"
5663 " is not the .dynamic section"),
5665 bfd_set_error (bfd_error_bad_value
);
5668 /* Set the note section type to SHT_NOTE. */
5669 else if (p
->p_type
== PT_NOTE
)
5670 for (i
= 0; i
< m
->count
; i
++)
5671 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5673 if (m
->includes_filehdr
)
5675 if (!m
->p_flags_valid
)
5677 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5678 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5679 if (p
->p_type
== PT_LOAD
)
5683 if (p
->p_vaddr
< (bfd_vma
) off
5684 || (!m
->p_paddr_valid
5685 && p
->p_paddr
< (bfd_vma
) off
))
5688 (_("%pB: not enough room for program headers,"
5689 " try linking with -N"),
5691 bfd_set_error (bfd_error_bad_value
);
5695 if (!m
->p_paddr_valid
)
5699 else if (sorted_seg_map
[0]->includes_filehdr
)
5701 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5702 p
->p_vaddr
= filehdr
->p_vaddr
;
5703 if (!m
->p_paddr_valid
)
5704 p
->p_paddr
= filehdr
->p_paddr
;
5708 if (m
->includes_phdrs
)
5710 if (!m
->p_flags_valid
)
5712 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5713 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5714 if (!m
->includes_filehdr
)
5716 if (p
->p_type
== PT_LOAD
)
5718 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5721 p
->p_vaddr
-= off
- p
->p_offset
;
5722 if (!m
->p_paddr_valid
)
5723 p
->p_paddr
-= off
- p
->p_offset
;
5726 else if (phdr_load_seg
!= NULL
)
5728 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5729 bfd_vma phdr_off
= 0; /* Octets. */
5730 if (phdr_load_seg
->includes_filehdr
)
5731 phdr_off
= bed
->s
->sizeof_ehdr
;
5732 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5733 if (!m
->p_paddr_valid
)
5734 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5735 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5738 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5742 if (p
->p_type
== PT_LOAD
5743 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5745 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5750 /* Put meaningless p_offset for PT_LOAD segments
5751 without file contents somewhere within the first
5752 page, in an attempt to not point past EOF. */
5753 bfd_size_type align
= maxpagesize
;
5754 if (align
< p
->p_align
)
5758 p
->p_offset
= off
% align
;
5763 file_ptr adjust
; /* Octets. */
5765 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5767 p
->p_filesz
+= adjust
;
5768 p
->p_memsz
+= adjust
;
5772 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5773 maps. Set filepos for sections in PT_LOAD segments, and in
5774 core files, for sections in PT_NOTE segments.
5775 assign_file_positions_for_non_load_sections will set filepos
5776 for other sections and update p_filesz for other segments. */
5777 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5780 bfd_size_type align
;
5781 Elf_Internal_Shdr
*this_hdr
;
5784 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5785 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5787 if ((p
->p_type
== PT_LOAD
5788 || p
->p_type
== PT_TLS
)
5789 && (this_hdr
->sh_type
!= SHT_NOBITS
5790 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5791 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5792 || p
->p_type
== PT_TLS
))))
5794 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5795 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5796 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5797 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5801 || p_end
< p_start
))
5804 /* xgettext:c-format */
5805 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5806 abfd
, sec
, (uint64_t) s_start
/ opb
,
5807 (uint64_t) p_end
/ opb
);
5809 sec
->lma
= p_end
/ opb
;
5811 p
->p_memsz
+= adjust
;
5813 if (p
->p_type
== PT_LOAD
)
5815 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5818 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5820 /* We have a PROGBITS section following NOBITS ones.
5821 Allocate file space for the NOBITS section(s) and
5823 adjust
= p
->p_memsz
- p
->p_filesz
;
5824 if (!write_zeros (abfd
, off
, adjust
))
5828 /* We only adjust sh_offset in SHT_NOBITS sections
5829 as would seem proper for their address when the
5830 section is first in the segment. sh_offset
5831 doesn't really have any significance for
5832 SHT_NOBITS anyway, apart from a notional position
5833 relative to other sections. Historically we
5834 didn't bother with adjusting sh_offset and some
5835 programs depend on it not being adjusted. See
5836 pr12921 and pr25662. */
5837 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
5840 if (this_hdr
->sh_type
== SHT_NOBITS
)
5841 off_adjust
+= adjust
;
5844 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5845 p
->p_filesz
+= adjust
;
5848 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5850 /* The section at i == 0 is the one that actually contains
5854 this_hdr
->sh_offset
= sec
->filepos
= off
;
5855 off
+= this_hdr
->sh_size
;
5856 p
->p_filesz
= this_hdr
->sh_size
;
5862 /* The rest are fake sections that shouldn't be written. */
5871 if (p
->p_type
== PT_LOAD
)
5873 this_hdr
->sh_offset
= sec
->filepos
= off
;
5874 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5875 off
+= this_hdr
->sh_size
;
5877 else if (this_hdr
->sh_type
== SHT_NOBITS
5878 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5879 && this_hdr
->sh_offset
== 0)
5881 /* This is a .tbss section that didn't get a PT_LOAD.
5882 (See _bfd_elf_map_sections_to_segments "Create a
5883 final PT_LOAD".) Set sh_offset to the value it
5884 would have if we had created a zero p_filesz and
5885 p_memsz PT_LOAD header for the section. This
5886 also makes the PT_TLS header have the same
5888 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5890 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5893 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5895 p
->p_filesz
+= this_hdr
->sh_size
;
5896 /* A load section without SHF_ALLOC is something like
5897 a note section in a PT_NOTE segment. These take
5898 file space but are not loaded into memory. */
5899 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5900 p
->p_memsz
+= this_hdr
->sh_size
;
5902 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5904 if (p
->p_type
== PT_TLS
)
5905 p
->p_memsz
+= this_hdr
->sh_size
;
5907 /* .tbss is special. It doesn't contribute to p_memsz of
5909 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5910 p
->p_memsz
+= this_hdr
->sh_size
;
5913 if (align
> p
->p_align
5914 && !m
->p_align_valid
5915 && (p
->p_type
!= PT_LOAD
5916 || (abfd
->flags
& D_PAGED
) == 0))
5920 if (!m
->p_flags_valid
)
5923 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5925 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5932 /* PR ld/20815 - Check that the program header segment, if
5933 present, will be loaded into memory. */
5934 if (p
->p_type
== PT_PHDR
5935 && phdr_load_seg
== NULL
5936 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
5937 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
5939 /* The fix for this error is usually to edit the linker script being
5940 used and set up the program headers manually. Either that or
5941 leave room for the headers at the start of the SECTIONS. */
5942 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5943 " by LOAD segment"),
5945 if (link_info
== NULL
)
5947 /* Arrange for the linker to exit with an error, deleting
5948 the output file unless --noinhibit-exec is given. */
5949 link_info
->callbacks
->info ("%X");
5952 /* Check that all sections are in a PT_LOAD segment.
5953 Don't check funky gdb generated core files. */
5954 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5956 bfd_boolean check_vma
= TRUE
;
5958 for (i
= 1; i
< m
->count
; i
++)
5959 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5960 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5961 ->this_hdr
), p
) != 0
5962 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5963 ->this_hdr
), p
) != 0)
5965 /* Looks like we have overlays packed into the segment. */
5970 for (i
= 0; i
< m
->count
; i
++)
5972 Elf_Internal_Shdr
*this_hdr
;
5975 sec
= m
->sections
[i
];
5976 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5977 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5978 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5981 /* xgettext:c-format */
5982 (_("%pB: section `%pA' can't be allocated in segment %d"),
5984 print_segment_map (m
);
5990 elf_next_file_pos (abfd
) = off
;
5992 if (link_info
!= NULL
5993 && phdr_load_seg
!= NULL
5994 && phdr_load_seg
->includes_filehdr
)
5996 /* There is a segment that contains both the file headers and the
5997 program headers, so provide a symbol __ehdr_start pointing there.
5998 A program can use this to examine itself robustly. */
6000 struct elf_link_hash_entry
*hash
6001 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6002 FALSE
, FALSE
, TRUE
);
6003 /* If the symbol was referenced and not defined, define it. */
6005 && (hash
->root
.type
== bfd_link_hash_new
6006 || hash
->root
.type
== bfd_link_hash_undefined
6007 || hash
->root
.type
== bfd_link_hash_undefweak
6008 || hash
->root
.type
== bfd_link_hash_common
))
6011 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6013 if (phdr_load_seg
->count
!= 0)
6014 /* The segment contains sections, so use the first one. */
6015 s
= phdr_load_seg
->sections
[0];
6017 /* Use the first (i.e. lowest-addressed) section in any segment. */
6018 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6019 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6027 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6028 hash
->root
.u
.def
.section
= s
;
6032 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6033 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6036 hash
->root
.type
= bfd_link_hash_defined
;
6037 hash
->def_regular
= 1;
6045 /* Determine if a bfd is a debuginfo file. Unfortunately there
6046 is no defined method for detecting such files, so we have to
6047 use heuristics instead. */
6050 is_debuginfo_file (bfd
*abfd
)
6052 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6055 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6056 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6057 Elf_Internal_Shdr
**headerp
;
6059 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6061 Elf_Internal_Shdr
*header
= * headerp
;
6063 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6064 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6065 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6066 && header
->sh_type
!= SHT_NOBITS
6067 && header
->sh_type
!= SHT_NOTE
)
6074 /* Assign file positions for the other sections, except for compressed debugging
6075 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6078 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6079 struct bfd_link_info
*link_info
)
6081 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6082 Elf_Internal_Shdr
**i_shdrpp
;
6083 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6084 Elf_Internal_Phdr
*phdrs
;
6085 Elf_Internal_Phdr
*p
;
6086 struct elf_segment_map
*m
;
6088 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6090 i_shdrpp
= elf_elfsections (abfd
);
6091 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6092 off
= elf_next_file_pos (abfd
);
6093 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6095 Elf_Internal_Shdr
*hdr
;
6098 if (hdr
->bfd_section
!= NULL
6099 && (hdr
->bfd_section
->filepos
!= 0
6100 || (hdr
->sh_type
== SHT_NOBITS
6101 && hdr
->contents
== NULL
)))
6102 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6103 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6105 if (hdr
->sh_size
!= 0
6106 /* PR 24717 - debuginfo files are known to be not strictly
6107 compliant with the ELF standard. In particular they often
6108 have .note.gnu.property sections that are outside of any
6109 loadable segment. This is not a problem for such files,
6110 so do not warn about them. */
6111 && ! is_debuginfo_file (abfd
))
6113 /* xgettext:c-format */
6114 (_("%pB: warning: allocated section `%s' not in segment"),
6116 (hdr
->bfd_section
== NULL
6118 : hdr
->bfd_section
->name
));
6119 /* We don't need to page align empty sections. */
6120 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6121 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6124 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6126 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6129 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6130 && hdr
->bfd_section
== NULL
)
6131 /* We don't know the offset of these sections yet: their size has
6132 not been decided. */
6133 || (hdr
->bfd_section
!= NULL
6134 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6135 || (bfd_section_is_ctf (hdr
->bfd_section
)
6136 && abfd
->is_linker_output
)))
6137 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6138 || (elf_symtab_shndx_list (abfd
) != NULL
6139 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6140 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6141 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6142 hdr
->sh_offset
= -1;
6144 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6146 elf_next_file_pos (abfd
) = off
;
6148 /* Now that we have set the section file positions, we can set up
6149 the file positions for the non PT_LOAD segments. */
6150 phdrs
= elf_tdata (abfd
)->phdr
;
6151 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6153 if (p
->p_type
== PT_GNU_RELRO
)
6155 bfd_vma start
, end
; /* Bytes. */
6158 if (link_info
!= NULL
)
6160 /* During linking the range of the RELRO segment is passed
6161 in link_info. Note that there may be padding between
6162 relro_start and the first RELRO section. */
6163 start
= link_info
->relro_start
;
6164 end
= link_info
->relro_end
;
6166 else if (m
->count
!= 0)
6168 if (!m
->p_size_valid
)
6170 start
= m
->sections
[0]->vma
;
6171 end
= start
+ m
->p_size
/ opb
;
6182 struct elf_segment_map
*lm
;
6183 const Elf_Internal_Phdr
*lp
;
6186 /* Find a LOAD segment containing a section in the RELRO
6188 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6190 lm
= lm
->next
, lp
++)
6192 if (lp
->p_type
== PT_LOAD
6194 && (lm
->sections
[lm
->count
- 1]->vma
6195 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6196 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6198 && lm
->sections
[0]->vma
< end
)
6204 /* Find the section starting the RELRO segment. */
6205 for (i
= 0; i
< lm
->count
; i
++)
6207 asection
*s
= lm
->sections
[i
];
6216 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6217 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6218 p
->p_offset
= lm
->sections
[i
]->filepos
;
6219 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6220 p
->p_filesz
= p
->p_memsz
;
6222 /* The RELRO segment typically ends a few bytes
6223 into .got.plt but other layouts are possible.
6224 In cases where the end does not match any
6225 loaded section (for instance is in file
6226 padding), trim p_filesz back to correspond to
6227 the end of loaded section contents. */
6228 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6229 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6231 /* Preserve the alignment and flags if they are
6232 valid. The gold linker generates RW/4 for
6233 the PT_GNU_RELRO section. It is better for
6234 objcopy/strip to honor these attributes
6235 otherwise gdb will choke when using separate
6237 if (!m
->p_align_valid
)
6239 if (!m
->p_flags_valid
)
6245 if (link_info
!= NULL
)
6248 memset (p
, 0, sizeof *p
);
6250 else if (p
->p_type
== PT_GNU_STACK
)
6252 if (m
->p_size_valid
)
6253 p
->p_memsz
= m
->p_size
;
6255 else if (m
->count
!= 0)
6259 if (p
->p_type
!= PT_LOAD
6260 && (p
->p_type
!= PT_NOTE
6261 || bfd_get_format (abfd
) != bfd_core
))
6263 /* A user specified segment layout may include a PHDR
6264 segment that overlaps with a LOAD segment... */
6265 if (p
->p_type
== PT_PHDR
)
6271 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6273 /* PR 17512: file: 2195325e. */
6275 (_("%pB: error: non-load segment %d includes file header "
6276 "and/or program header"),
6277 abfd
, (int) (p
- phdrs
));
6282 p
->p_offset
= m
->sections
[0]->filepos
;
6283 for (i
= m
->count
; i
-- != 0;)
6285 asection
*sect
= m
->sections
[i
];
6286 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6287 if (hdr
->sh_type
!= SHT_NOBITS
)
6289 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6301 static elf_section_list
*
6302 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6304 for (;list
!= NULL
; list
= list
->next
)
6310 /* Work out the file positions of all the sections. This is called by
6311 _bfd_elf_compute_section_file_positions. All the section sizes and
6312 VMAs must be known before this is called.
6314 Reloc sections come in two flavours: Those processed specially as
6315 "side-channel" data attached to a section to which they apply, and those that
6316 bfd doesn't process as relocations. The latter sort are stored in a normal
6317 bfd section by bfd_section_from_shdr. We don't consider the former sort
6318 here, unless they form part of the loadable image. Reloc sections not
6319 assigned here (and compressed debugging sections and CTF sections which
6320 nothing else in the file can rely upon) will be handled later by
6321 assign_file_positions_for_relocs.
6323 We also don't set the positions of the .symtab and .strtab here. */
6326 assign_file_positions_except_relocs (bfd
*abfd
,
6327 struct bfd_link_info
*link_info
)
6329 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6330 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6331 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6334 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6335 && bfd_get_format (abfd
) != bfd_core
)
6337 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6338 unsigned int num_sec
= elf_numsections (abfd
);
6339 Elf_Internal_Shdr
**hdrpp
;
6343 /* Start after the ELF header. */
6344 off
= i_ehdrp
->e_ehsize
;
6346 /* We are not creating an executable, which means that we are
6347 not creating a program header, and that the actual order of
6348 the sections in the file is unimportant. */
6349 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6351 Elf_Internal_Shdr
*hdr
;
6354 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6355 && hdr
->bfd_section
== NULL
)
6356 /* Do not assign offsets for these sections yet: we don't know
6358 || (hdr
->bfd_section
!= NULL
6359 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6360 || (bfd_section_is_ctf (hdr
->bfd_section
)
6361 && abfd
->is_linker_output
)))
6362 || i
== elf_onesymtab (abfd
)
6363 || (elf_symtab_shndx_list (abfd
) != NULL
6364 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6365 || i
== elf_strtab_sec (abfd
)
6366 || i
== elf_shstrtab_sec (abfd
))
6368 hdr
->sh_offset
= -1;
6371 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6374 elf_next_file_pos (abfd
) = off
;
6375 elf_program_header_size (abfd
) = 0;
6379 /* Assign file positions for the loaded sections based on the
6380 assignment of sections to segments. */
6381 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6384 /* And for non-load sections. */
6385 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6389 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6392 /* Write out the program headers. */
6393 alloc
= i_ehdrp
->e_phnum
;
6396 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6397 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6405 _bfd_elf_init_file_header (bfd
*abfd
,
6406 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6408 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6409 struct elf_strtab_hash
*shstrtab
;
6410 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6412 i_ehdrp
= elf_elfheader (abfd
);
6414 shstrtab
= _bfd_elf_strtab_init ();
6415 if (shstrtab
== NULL
)
6418 elf_shstrtab (abfd
) = shstrtab
;
6420 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6421 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6422 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6423 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6425 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6426 i_ehdrp
->e_ident
[EI_DATA
] =
6427 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6428 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6430 if ((abfd
->flags
& DYNAMIC
) != 0)
6431 i_ehdrp
->e_type
= ET_DYN
;
6432 else if ((abfd
->flags
& EXEC_P
) != 0)
6433 i_ehdrp
->e_type
= ET_EXEC
;
6434 else if (bfd_get_format (abfd
) == bfd_core
)
6435 i_ehdrp
->e_type
= ET_CORE
;
6437 i_ehdrp
->e_type
= ET_REL
;
6439 switch (bfd_get_arch (abfd
))
6441 case bfd_arch_unknown
:
6442 i_ehdrp
->e_machine
= EM_NONE
;
6445 /* There used to be a long list of cases here, each one setting
6446 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6447 in the corresponding bfd definition. To avoid duplication,
6448 the switch was removed. Machines that need special handling
6449 can generally do it in elf_backend_final_write_processing(),
6450 unless they need the information earlier than the final write.
6451 Such need can generally be supplied by replacing the tests for
6452 e_machine with the conditions used to determine it. */
6454 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6457 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6458 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6460 /* No program header, for now. */
6461 i_ehdrp
->e_phoff
= 0;
6462 i_ehdrp
->e_phentsize
= 0;
6463 i_ehdrp
->e_phnum
= 0;
6465 /* Each bfd section is section header entry. */
6466 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6467 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6469 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6470 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6471 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6472 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6473 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6474 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6475 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6476 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6477 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6483 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6485 FIXME: We used to have code here to sort the PT_LOAD segments into
6486 ascending order, as per the ELF spec. But this breaks some programs,
6487 including the Linux kernel. But really either the spec should be
6488 changed or the programs updated. */
6491 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6493 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6495 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6496 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6497 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6498 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6499 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6501 /* Find the lowest p_vaddr in PT_LOAD segments. */
6502 bfd_vma p_vaddr
= (bfd_vma
) -1;
6503 for (; segment
< end_segment
; segment
++)
6504 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6505 p_vaddr
= segment
->p_vaddr
;
6507 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6508 segments is non-zero. */
6510 i_ehdrp
->e_type
= ET_EXEC
;
6515 /* Assign file positions for all the reloc sections which are not part
6516 of the loadable file image, and the file position of section headers. */
6519 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6522 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6523 Elf_Internal_Shdr
*shdrp
;
6524 Elf_Internal_Ehdr
*i_ehdrp
;
6525 const struct elf_backend_data
*bed
;
6527 off
= elf_next_file_pos (abfd
);
6529 shdrpp
= elf_elfsections (abfd
);
6530 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6531 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6534 if (shdrp
->sh_offset
== -1)
6536 asection
*sec
= shdrp
->bfd_section
;
6537 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6538 || shdrp
->sh_type
== SHT_RELA
);
6539 bfd_boolean is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6542 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6544 if (!is_rel
&& !is_ctf
)
6546 const char *name
= sec
->name
;
6547 struct bfd_elf_section_data
*d
;
6549 /* Compress DWARF debug sections. */
6550 if (!bfd_compress_section (abfd
, sec
,
6554 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6555 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6557 /* If section is compressed with zlib-gnu, convert
6558 section name from .debug_* to .zdebug_*. */
6560 = convert_debug_to_zdebug (abfd
, name
);
6561 if (new_name
== NULL
)
6565 /* Add section name to section name section. */
6566 if (shdrp
->sh_name
!= (unsigned int) -1)
6569 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6571 d
= elf_section_data (sec
);
6573 /* Add reloc section name to section name section. */
6575 && !_bfd_elf_set_reloc_sh_name (abfd
,
6580 && !_bfd_elf_set_reloc_sh_name (abfd
,
6585 /* Update section size and contents. */
6586 shdrp
->sh_size
= sec
->size
;
6587 shdrp
->contents
= sec
->contents
;
6588 shdrp
->bfd_section
->contents
= NULL
;
6592 /* Update section size and contents. */
6593 shdrp
->sh_size
= sec
->size
;
6594 shdrp
->contents
= sec
->contents
;
6597 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6604 /* Place section name section after DWARF debug sections have been
6606 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6607 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6608 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6609 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6611 /* Place the section headers. */
6612 i_ehdrp
= elf_elfheader (abfd
);
6613 bed
= get_elf_backend_data (abfd
);
6614 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6615 i_ehdrp
->e_shoff
= off
;
6616 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6617 elf_next_file_pos (abfd
) = off
;
6623 _bfd_elf_write_object_contents (bfd
*abfd
)
6625 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6626 Elf_Internal_Shdr
**i_shdrp
;
6628 unsigned int count
, num_sec
;
6629 struct elf_obj_tdata
*t
;
6631 if (! abfd
->output_has_begun
6632 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6634 /* Do not rewrite ELF data when the BFD has been opened for update.
6635 abfd->output_has_begun was set to TRUE on opening, so creation of new
6636 sections, and modification of existing section sizes was restricted.
6637 This means the ELF header, program headers and section headers can't have
6639 If the contents of any sections has been modified, then those changes have
6640 already been written to the BFD. */
6641 else if (abfd
->direction
== both_direction
)
6643 BFD_ASSERT (abfd
->output_has_begun
);
6647 i_shdrp
= elf_elfsections (abfd
);
6650 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6654 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6657 /* After writing the headers, we need to write the sections too... */
6658 num_sec
= elf_numsections (abfd
);
6659 for (count
= 1; count
< num_sec
; count
++)
6661 i_shdrp
[count
]->sh_name
6662 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6663 i_shdrp
[count
]->sh_name
);
6664 if (bed
->elf_backend_section_processing
)
6665 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6667 if (i_shdrp
[count
]->contents
)
6669 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6671 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6672 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6677 /* Write out the section header names. */
6678 t
= elf_tdata (abfd
);
6679 if (elf_shstrtab (abfd
) != NULL
6680 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6681 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6684 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6687 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6690 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6691 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6692 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6698 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6700 /* Hopefully this can be done just like an object file. */
6701 return _bfd_elf_write_object_contents (abfd
);
6704 /* Given a section, search the header to find them. */
6707 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6709 const struct elf_backend_data
*bed
;
6710 unsigned int sec_index
;
6712 if (elf_section_data (asect
) != NULL
6713 && elf_section_data (asect
)->this_idx
!= 0)
6714 return elf_section_data (asect
)->this_idx
;
6716 if (bfd_is_abs_section (asect
))
6717 sec_index
= SHN_ABS
;
6718 else if (bfd_is_com_section (asect
))
6719 sec_index
= SHN_COMMON
;
6720 else if (bfd_is_und_section (asect
))
6721 sec_index
= SHN_UNDEF
;
6723 sec_index
= SHN_BAD
;
6725 bed
= get_elf_backend_data (abfd
);
6726 if (bed
->elf_backend_section_from_bfd_section
)
6728 int retval
= sec_index
;
6730 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6734 if (sec_index
== SHN_BAD
)
6735 bfd_set_error (bfd_error_nonrepresentable_section
);
6740 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6744 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6746 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6748 flagword flags
= asym_ptr
->flags
;
6750 /* When gas creates relocations against local labels, it creates its
6751 own symbol for the section, but does put the symbol into the
6752 symbol chain, so udata is 0. When the linker is generating
6753 relocatable output, this section symbol may be for one of the
6754 input sections rather than the output section. */
6755 if (asym_ptr
->udata
.i
== 0
6756 && (flags
& BSF_SECTION_SYM
)
6757 && asym_ptr
->section
)
6762 sec
= asym_ptr
->section
;
6763 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6764 sec
= sec
->output_section
;
6765 if (sec
->owner
== abfd
6766 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6767 && elf_section_syms (abfd
)[indx
] != NULL
)
6768 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6771 idx
= asym_ptr
->udata
.i
;
6775 /* This case can occur when using --strip-symbol on a symbol
6776 which is used in a relocation entry. */
6778 /* xgettext:c-format */
6779 (_("%pB: symbol `%s' required but not present"),
6780 abfd
, bfd_asymbol_name (asym_ptr
));
6781 bfd_set_error (bfd_error_no_symbols
);
6788 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6789 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6797 /* Rewrite program header information. */
6800 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6802 Elf_Internal_Ehdr
*iehdr
;
6803 struct elf_segment_map
*map
;
6804 struct elf_segment_map
*map_first
;
6805 struct elf_segment_map
**pointer_to_map
;
6806 Elf_Internal_Phdr
*segment
;
6809 unsigned int num_segments
;
6810 bfd_boolean phdr_included
= FALSE
;
6811 bfd_boolean p_paddr_valid
;
6812 bfd_vma maxpagesize
;
6813 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6814 unsigned int phdr_adjust_num
= 0;
6815 const struct elf_backend_data
*bed
;
6816 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
6818 bed
= get_elf_backend_data (ibfd
);
6819 iehdr
= elf_elfheader (ibfd
);
6822 pointer_to_map
= &map_first
;
6824 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6825 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6827 /* Returns the end address of the segment + 1. */
6828 #define SEGMENT_END(segment, start) \
6829 (start + (segment->p_memsz > segment->p_filesz \
6830 ? segment->p_memsz : segment->p_filesz))
6832 #define SECTION_SIZE(section, segment) \
6833 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6834 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6835 ? section->size : 0)
6837 /* Returns TRUE if the given section is contained within
6838 the given segment. VMA addresses are compared. */
6839 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6840 (section->vma * (opb) >= segment->p_vaddr \
6841 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6842 <= (SEGMENT_END (segment, segment->p_vaddr))))
6844 /* Returns TRUE if the given section is contained within
6845 the given segment. LMA addresses are compared. */
6846 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6847 (section->lma * (opb) >= base \
6848 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6849 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6850 <= SEGMENT_END (segment, base)))
6852 /* Handle PT_NOTE segment. */
6853 #define IS_NOTE(p, s) \
6854 (p->p_type == PT_NOTE \
6855 && elf_section_type (s) == SHT_NOTE \
6856 && (bfd_vma) s->filepos >= p->p_offset \
6857 && ((bfd_vma) s->filepos + s->size \
6858 <= p->p_offset + p->p_filesz))
6860 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6862 #define IS_COREFILE_NOTE(p, s) \
6864 && bfd_get_format (ibfd) == bfd_core \
6868 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6869 linker, which generates a PT_INTERP section with p_vaddr and
6870 p_memsz set to 0. */
6871 #define IS_SOLARIS_PT_INTERP(p, s) \
6873 && p->p_paddr == 0 \
6874 && p->p_memsz == 0 \
6875 && p->p_filesz > 0 \
6876 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6878 && (bfd_vma) s->filepos >= p->p_offset \
6879 && ((bfd_vma) s->filepos + s->size \
6880 <= p->p_offset + p->p_filesz))
6882 /* Decide if the given section should be included in the given segment.
6883 A section will be included if:
6884 1. It is within the address space of the segment -- we use the LMA
6885 if that is set for the segment and the VMA otherwise,
6886 2. It is an allocated section or a NOTE section in a PT_NOTE
6888 3. There is an output section associated with it,
6889 4. The section has not already been allocated to a previous segment.
6890 5. PT_GNU_STACK segments do not include any sections.
6891 6. PT_TLS segment includes only SHF_TLS sections.
6892 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6893 8. PT_DYNAMIC should not contain empty sections at the beginning
6894 (with the possible exception of .dynamic). */
6895 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6896 ((((segment->p_paddr \
6897 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6898 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
6899 && (section->flags & SEC_ALLOC) != 0) \
6900 || IS_NOTE (segment, section)) \
6901 && segment->p_type != PT_GNU_STACK \
6902 && (segment->p_type != PT_TLS \
6903 || (section->flags & SEC_THREAD_LOCAL)) \
6904 && (segment->p_type == PT_LOAD \
6905 || segment->p_type == PT_TLS \
6906 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6907 && (segment->p_type != PT_DYNAMIC \
6908 || SECTION_SIZE (section, segment) > 0 \
6909 || (segment->p_paddr \
6910 ? segment->p_paddr != section->lma * (opb) \
6911 : segment->p_vaddr != section->vma * (opb)) \
6912 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6913 && (segment->p_type != PT_LOAD || !section->segment_mark))
6915 /* If the output section of a section in the input segment is NULL,
6916 it is removed from the corresponding output segment. */
6917 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6918 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
6919 && section->output_section != NULL)
6921 /* Returns TRUE iff seg1 starts after the end of seg2. */
6922 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6923 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6925 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6926 their VMA address ranges and their LMA address ranges overlap.
6927 It is possible to have overlapping VMA ranges without overlapping LMA
6928 ranges. RedBoot images for example can have both .data and .bss mapped
6929 to the same VMA range, but with the .data section mapped to a different
6931 #define SEGMENT_OVERLAPS(seg1, seg2) \
6932 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6933 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6934 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6935 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6937 /* Initialise the segment mark field. */
6938 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6939 section
->segment_mark
= FALSE
;
6941 /* The Solaris linker creates program headers in which all the
6942 p_paddr fields are zero. When we try to objcopy or strip such a
6943 file, we get confused. Check for this case, and if we find it
6944 don't set the p_paddr_valid fields. */
6945 p_paddr_valid
= FALSE
;
6946 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6949 if (segment
->p_paddr
!= 0)
6951 p_paddr_valid
= TRUE
;
6955 /* Scan through the segments specified in the program header
6956 of the input BFD. For this first scan we look for overlaps
6957 in the loadable segments. These can be created by weird
6958 parameters to objcopy. Also, fix some solaris weirdness. */
6959 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6964 Elf_Internal_Phdr
*segment2
;
6966 if (segment
->p_type
== PT_INTERP
)
6967 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6968 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6970 /* Mininal change so that the normal section to segment
6971 assignment code will work. */
6972 segment
->p_vaddr
= section
->vma
* opb
;
6976 if (segment
->p_type
!= PT_LOAD
)
6978 /* Remove PT_GNU_RELRO segment. */
6979 if (segment
->p_type
== PT_GNU_RELRO
)
6980 segment
->p_type
= PT_NULL
;
6984 /* Determine if this segment overlaps any previous segments. */
6985 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6987 bfd_signed_vma extra_length
;
6989 if (segment2
->p_type
!= PT_LOAD
6990 || !SEGMENT_OVERLAPS (segment
, segment2
))
6993 /* Merge the two segments together. */
6994 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6996 /* Extend SEGMENT2 to include SEGMENT and then delete
6998 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6999 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
7001 if (extra_length
> 0)
7003 segment2
->p_memsz
+= extra_length
;
7004 segment2
->p_filesz
+= extra_length
;
7007 segment
->p_type
= PT_NULL
;
7009 /* Since we have deleted P we must restart the outer loop. */
7011 segment
= elf_tdata (ibfd
)->phdr
;
7016 /* Extend SEGMENT to include SEGMENT2 and then delete
7018 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
7019 - SEGMENT_END (segment
, segment
->p_vaddr
));
7021 if (extra_length
> 0)
7023 segment
->p_memsz
+= extra_length
;
7024 segment
->p_filesz
+= extra_length
;
7027 segment2
->p_type
= PT_NULL
;
7032 /* The second scan attempts to assign sections to segments. */
7033 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7037 unsigned int section_count
;
7038 asection
**sections
;
7039 asection
*output_section
;
7041 asection
*matching_lma
;
7042 asection
*suggested_lma
;
7045 asection
*first_section
;
7047 if (segment
->p_type
== PT_NULL
)
7050 first_section
= NULL
;
7051 /* Compute how many sections might be placed into this segment. */
7052 for (section
= ibfd
->sections
, section_count
= 0;
7054 section
= section
->next
)
7056 /* Find the first section in the input segment, which may be
7057 removed from the corresponding output segment. */
7058 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7060 if (first_section
== NULL
)
7061 first_section
= section
;
7062 if (section
->output_section
!= NULL
)
7067 /* Allocate a segment map big enough to contain
7068 all of the sections we have selected. */
7069 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7070 amt
+= section_count
* sizeof (asection
*);
7071 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7075 /* Initialise the fields of the segment map. Default to
7076 using the physical address of the segment in the input BFD. */
7078 map
->p_type
= segment
->p_type
;
7079 map
->p_flags
= segment
->p_flags
;
7080 map
->p_flags_valid
= 1;
7082 /* If the first section in the input segment is removed, there is
7083 no need to preserve segment physical address in the corresponding
7085 if (!first_section
|| first_section
->output_section
!= NULL
)
7087 map
->p_paddr
= segment
->p_paddr
;
7088 map
->p_paddr_valid
= p_paddr_valid
;
7091 /* Determine if this segment contains the ELF file header
7092 and if it contains the program headers themselves. */
7093 map
->includes_filehdr
= (segment
->p_offset
== 0
7094 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7095 map
->includes_phdrs
= 0;
7097 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7099 map
->includes_phdrs
=
7100 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7101 && (segment
->p_offset
+ segment
->p_filesz
7102 >= ((bfd_vma
) iehdr
->e_phoff
7103 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7105 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7106 phdr_included
= TRUE
;
7109 if (section_count
== 0)
7111 /* Special segments, such as the PT_PHDR segment, may contain
7112 no sections, but ordinary, loadable segments should contain
7113 something. They are allowed by the ELF spec however, so only
7114 a warning is produced.
7115 There is however the valid use case of embedded systems which
7116 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7117 flash memory with zeros. No warning is shown for that case. */
7118 if (segment
->p_type
== PT_LOAD
7119 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7120 /* xgettext:c-format */
7122 (_("%pB: warning: empty loadable segment detected"
7123 " at vaddr=%#" PRIx64
", is this intentional?"),
7124 ibfd
, (uint64_t) segment
->p_vaddr
);
7126 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7128 *pointer_to_map
= map
;
7129 pointer_to_map
= &map
->next
;
7134 /* Now scan the sections in the input BFD again and attempt
7135 to add their corresponding output sections to the segment map.
7136 The problem here is how to handle an output section which has
7137 been moved (ie had its LMA changed). There are four possibilities:
7139 1. None of the sections have been moved.
7140 In this case we can continue to use the segment LMA from the
7143 2. All of the sections have been moved by the same amount.
7144 In this case we can change the segment's LMA to match the LMA
7145 of the first section.
7147 3. Some of the sections have been moved, others have not.
7148 In this case those sections which have not been moved can be
7149 placed in the current segment which will have to have its size,
7150 and possibly its LMA changed, and a new segment or segments will
7151 have to be created to contain the other sections.
7153 4. The sections have been moved, but not by the same amount.
7154 In this case we can change the segment's LMA to match the LMA
7155 of the first section and we will have to create a new segment
7156 or segments to contain the other sections.
7158 In order to save time, we allocate an array to hold the section
7159 pointers that we are interested in. As these sections get assigned
7160 to a segment, they are removed from this array. */
7162 amt
= section_count
* sizeof (asection
*);
7163 sections
= (asection
**) bfd_malloc (amt
);
7164 if (sections
== NULL
)
7167 /* Step One: Scan for segment vs section LMA conflicts.
7168 Also add the sections to the section array allocated above.
7169 Also add the sections to the current segment. In the common
7170 case, where the sections have not been moved, this means that
7171 we have completely filled the segment, and there is nothing
7174 matching_lma
= NULL
;
7175 suggested_lma
= NULL
;
7177 for (section
= first_section
, j
= 0;
7179 section
= section
->next
)
7181 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7183 output_section
= section
->output_section
;
7185 sections
[j
++] = section
;
7187 /* The Solaris native linker always sets p_paddr to 0.
7188 We try to catch that case here, and set it to the
7189 correct value. Note - some backends require that
7190 p_paddr be left as zero. */
7192 && segment
->p_vaddr
!= 0
7193 && !bed
->want_p_paddr_set_to_zero
7195 && output_section
->lma
!= 0
7196 && (align_power (segment
->p_vaddr
7197 + (map
->includes_filehdr
7198 ? iehdr
->e_ehsize
: 0)
7199 + (map
->includes_phdrs
7200 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7202 output_section
->alignment_power
* opb
)
7203 == (output_section
->vma
* opb
)))
7204 map
->p_paddr
= segment
->p_vaddr
;
7206 /* Match up the physical address of the segment with the
7207 LMA address of the output section. */
7208 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7210 || IS_COREFILE_NOTE (segment
, section
)
7211 || (bed
->want_p_paddr_set_to_zero
7212 && IS_CONTAINED_BY_VMA (output_section
, segment
, opb
)))
7214 if (matching_lma
== NULL
7215 || output_section
->lma
< matching_lma
->lma
)
7216 matching_lma
= output_section
;
7218 /* We assume that if the section fits within the segment
7219 then it does not overlap any other section within that
7221 map
->sections
[isec
++] = output_section
;
7223 else if (suggested_lma
== NULL
)
7224 suggested_lma
= output_section
;
7226 if (j
== section_count
)
7231 BFD_ASSERT (j
== section_count
);
7233 /* Step Two: Adjust the physical address of the current segment,
7235 if (isec
== section_count
)
7237 /* All of the sections fitted within the segment as currently
7238 specified. This is the default case. Add the segment to
7239 the list of built segments and carry on to process the next
7240 program header in the input BFD. */
7241 map
->count
= section_count
;
7242 *pointer_to_map
= map
;
7243 pointer_to_map
= &map
->next
;
7246 && !bed
->want_p_paddr_set_to_zero
)
7248 bfd_vma hdr_size
= 0;
7249 if (map
->includes_filehdr
)
7250 hdr_size
= iehdr
->e_ehsize
;
7251 if (map
->includes_phdrs
)
7252 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7254 /* Account for padding before the first section in the
7256 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7257 - matching_lma
->lma
);
7265 /* Change the current segment's physical address to match
7266 the LMA of the first section that fitted, or if no
7267 section fitted, the first section. */
7268 if (matching_lma
== NULL
)
7269 matching_lma
= suggested_lma
;
7271 map
->p_paddr
= matching_lma
->lma
* opb
;
7273 /* Offset the segment physical address from the lma
7274 to allow for space taken up by elf headers. */
7275 if (map
->includes_phdrs
)
7277 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7279 /* iehdr->e_phnum is just an estimate of the number
7280 of program headers that we will need. Make a note
7281 here of the number we used and the segment we chose
7282 to hold these headers, so that we can adjust the
7283 offset when we know the correct value. */
7284 phdr_adjust_num
= iehdr
->e_phnum
;
7285 phdr_adjust_seg
= map
;
7288 if (map
->includes_filehdr
)
7290 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7291 map
->p_paddr
-= iehdr
->e_ehsize
;
7292 /* We've subtracted off the size of headers from the
7293 first section lma, but there may have been some
7294 alignment padding before that section too. Try to
7295 account for that by adjusting the segment lma down to
7296 the same alignment. */
7297 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7298 align
= segment
->p_align
;
7299 map
->p_paddr
&= -(align
* opb
);
7303 /* Step Three: Loop over the sections again, this time assigning
7304 those that fit to the current segment and removing them from the
7305 sections array; but making sure not to leave large gaps. Once all
7306 possible sections have been assigned to the current segment it is
7307 added to the list of built segments and if sections still remain
7308 to be assigned, a new segment is constructed before repeating
7314 suggested_lma
= NULL
;
7316 /* Fill the current segment with sections that fit. */
7317 for (j
= 0; j
< section_count
; j
++)
7319 section
= sections
[j
];
7321 if (section
== NULL
)
7324 output_section
= section
->output_section
;
7326 BFD_ASSERT (output_section
!= NULL
);
7328 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7330 || IS_COREFILE_NOTE (segment
, section
))
7332 if (map
->count
== 0)
7334 /* If the first section in a segment does not start at
7335 the beginning of the segment, then something is
7337 if (align_power (map
->p_paddr
7338 + (map
->includes_filehdr
7339 ? iehdr
->e_ehsize
: 0)
7340 + (map
->includes_phdrs
7341 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7343 output_section
->alignment_power
* opb
)
7344 != output_section
->lma
* opb
)
7351 prev_sec
= map
->sections
[map
->count
- 1];
7353 /* If the gap between the end of the previous section
7354 and the start of this section is more than
7355 maxpagesize then we need to start a new segment. */
7356 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7358 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7359 || (prev_sec
->lma
+ prev_sec
->size
7360 > output_section
->lma
))
7362 if (suggested_lma
== NULL
)
7363 suggested_lma
= output_section
;
7369 map
->sections
[map
->count
++] = output_section
;
7372 if (segment
->p_type
== PT_LOAD
)
7373 section
->segment_mark
= TRUE
;
7375 else if (suggested_lma
== NULL
)
7376 suggested_lma
= output_section
;
7379 /* PR 23932. A corrupt input file may contain sections that cannot
7380 be assigned to any segment - because for example they have a
7381 negative size - or segments that do not contain any sections.
7382 But there are also valid reasons why a segment can be empty.
7383 So allow a count of zero. */
7385 /* Add the current segment to the list of built segments. */
7386 *pointer_to_map
= map
;
7387 pointer_to_map
= &map
->next
;
7389 if (isec
< section_count
)
7391 /* We still have not allocated all of the sections to
7392 segments. Create a new segment here, initialise it
7393 and carry on looping. */
7394 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7395 amt
+= section_count
* sizeof (asection
*);
7396 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7403 /* Initialise the fields of the segment map. Set the physical
7404 physical address to the LMA of the first section that has
7405 not yet been assigned. */
7407 map
->p_type
= segment
->p_type
;
7408 map
->p_flags
= segment
->p_flags
;
7409 map
->p_flags_valid
= 1;
7410 map
->p_paddr
= suggested_lma
->lma
* opb
;
7411 map
->p_paddr_valid
= p_paddr_valid
;
7412 map
->includes_filehdr
= 0;
7413 map
->includes_phdrs
= 0;
7418 bfd_set_error (bfd_error_sorry
);
7422 while (isec
< section_count
);
7427 elf_seg_map (obfd
) = map_first
;
7429 /* If we had to estimate the number of program headers that were
7430 going to be needed, then check our estimate now and adjust
7431 the offset if necessary. */
7432 if (phdr_adjust_seg
!= NULL
)
7436 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7439 if (count
> phdr_adjust_num
)
7440 phdr_adjust_seg
->p_paddr
7441 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7443 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7444 if (map
->p_type
== PT_PHDR
)
7447 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7448 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7455 #undef IS_CONTAINED_BY_VMA
7456 #undef IS_CONTAINED_BY_LMA
7458 #undef IS_COREFILE_NOTE
7459 #undef IS_SOLARIS_PT_INTERP
7460 #undef IS_SECTION_IN_INPUT_SEGMENT
7461 #undef INCLUDE_SECTION_IN_SEGMENT
7462 #undef SEGMENT_AFTER_SEGMENT
7463 #undef SEGMENT_OVERLAPS
7467 /* Copy ELF program header information. */
7470 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7472 Elf_Internal_Ehdr
*iehdr
;
7473 struct elf_segment_map
*map
;
7474 struct elf_segment_map
*map_first
;
7475 struct elf_segment_map
**pointer_to_map
;
7476 Elf_Internal_Phdr
*segment
;
7478 unsigned int num_segments
;
7479 bfd_boolean phdr_included
= FALSE
;
7480 bfd_boolean p_paddr_valid
;
7481 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7483 iehdr
= elf_elfheader (ibfd
);
7486 pointer_to_map
= &map_first
;
7488 /* If all the segment p_paddr fields are zero, don't set
7489 map->p_paddr_valid. */
7490 p_paddr_valid
= FALSE
;
7491 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7492 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7495 if (segment
->p_paddr
!= 0)
7497 p_paddr_valid
= TRUE
;
7501 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7506 unsigned int section_count
;
7508 Elf_Internal_Shdr
*this_hdr
;
7509 asection
*first_section
= NULL
;
7510 asection
*lowest_section
;
7512 /* Compute how many sections are in this segment. */
7513 for (section
= ibfd
->sections
, section_count
= 0;
7515 section
= section
->next
)
7517 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7518 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7520 if (first_section
== NULL
)
7521 first_section
= section
;
7526 /* Allocate a segment map big enough to contain
7527 all of the sections we have selected. */
7528 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7529 amt
+= section_count
* sizeof (asection
*);
7530 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7534 /* Initialize the fields of the output segment map with the
7537 map
->p_type
= segment
->p_type
;
7538 map
->p_flags
= segment
->p_flags
;
7539 map
->p_flags_valid
= 1;
7540 map
->p_paddr
= segment
->p_paddr
;
7541 map
->p_paddr_valid
= p_paddr_valid
;
7542 map
->p_align
= segment
->p_align
;
7543 map
->p_align_valid
= 1;
7544 map
->p_vaddr_offset
= 0;
7546 if (map
->p_type
== PT_GNU_RELRO
7547 || map
->p_type
== PT_GNU_STACK
)
7549 /* The PT_GNU_RELRO segment may contain the first a few
7550 bytes in the .got.plt section even if the whole .got.plt
7551 section isn't in the PT_GNU_RELRO segment. We won't
7552 change the size of the PT_GNU_RELRO segment.
7553 Similarly, PT_GNU_STACK size is significant on uclinux
7555 map
->p_size
= segment
->p_memsz
;
7556 map
->p_size_valid
= 1;
7559 /* Determine if this segment contains the ELF file header
7560 and if it contains the program headers themselves. */
7561 map
->includes_filehdr
= (segment
->p_offset
== 0
7562 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7564 map
->includes_phdrs
= 0;
7565 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7567 map
->includes_phdrs
=
7568 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7569 && (segment
->p_offset
+ segment
->p_filesz
7570 >= ((bfd_vma
) iehdr
->e_phoff
7571 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7573 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7574 phdr_included
= TRUE
;
7577 lowest_section
= NULL
;
7578 if (section_count
!= 0)
7580 unsigned int isec
= 0;
7582 for (section
= first_section
;
7584 section
= section
->next
)
7586 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7587 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7589 map
->sections
[isec
++] = section
->output_section
;
7590 if ((section
->flags
& SEC_ALLOC
) != 0)
7594 if (lowest_section
== NULL
7595 || section
->lma
< lowest_section
->lma
)
7596 lowest_section
= section
;
7598 /* Section lmas are set up from PT_LOAD header
7599 p_paddr in _bfd_elf_make_section_from_shdr.
7600 If this header has a p_paddr that disagrees
7601 with the section lma, flag the p_paddr as
7603 if ((section
->flags
& SEC_LOAD
) != 0)
7604 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7606 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7607 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7608 map
->p_paddr_valid
= FALSE
;
7610 if (isec
== section_count
)
7616 if (section_count
== 0)
7617 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7618 else if (map
->p_paddr_valid
)
7620 /* Account for padding before the first section in the segment. */
7621 bfd_vma hdr_size
= 0;
7622 if (map
->includes_filehdr
)
7623 hdr_size
= iehdr
->e_ehsize
;
7624 if (map
->includes_phdrs
)
7625 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7627 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7628 - (lowest_section
? lowest_section
->lma
: 0));
7631 map
->count
= section_count
;
7632 *pointer_to_map
= map
;
7633 pointer_to_map
= &map
->next
;
7636 elf_seg_map (obfd
) = map_first
;
7640 /* Copy private BFD data. This copies or rewrites ELF program header
7644 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7646 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7647 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7650 if (elf_tdata (ibfd
)->phdr
== NULL
)
7653 if (ibfd
->xvec
== obfd
->xvec
)
7655 /* Check to see if any sections in the input BFD
7656 covered by ELF program header have changed. */
7657 Elf_Internal_Phdr
*segment
;
7658 asection
*section
, *osec
;
7659 unsigned int i
, num_segments
;
7660 Elf_Internal_Shdr
*this_hdr
;
7661 const struct elf_backend_data
*bed
;
7663 bed
= get_elf_backend_data (ibfd
);
7665 /* Regenerate the segment map if p_paddr is set to 0. */
7666 if (bed
->want_p_paddr_set_to_zero
)
7669 /* Initialize the segment mark field. */
7670 for (section
= obfd
->sections
; section
!= NULL
;
7671 section
= section
->next
)
7672 section
->segment_mark
= FALSE
;
7674 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7675 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7679 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7680 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7681 which severly confuses things, so always regenerate the segment
7682 map in this case. */
7683 if (segment
->p_paddr
== 0
7684 && segment
->p_memsz
== 0
7685 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7688 for (section
= ibfd
->sections
;
7689 section
!= NULL
; section
= section
->next
)
7691 /* We mark the output section so that we know it comes
7692 from the input BFD. */
7693 osec
= section
->output_section
;
7695 osec
->segment_mark
= TRUE
;
7697 /* Check if this section is covered by the segment. */
7698 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7699 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7701 /* FIXME: Check if its output section is changed or
7702 removed. What else do we need to check? */
7704 || section
->flags
!= osec
->flags
7705 || section
->lma
!= osec
->lma
7706 || section
->vma
!= osec
->vma
7707 || section
->size
!= osec
->size
7708 || section
->rawsize
!= osec
->rawsize
7709 || section
->alignment_power
!= osec
->alignment_power
)
7715 /* Check to see if any output section do not come from the
7717 for (section
= obfd
->sections
; section
!= NULL
;
7718 section
= section
->next
)
7720 if (!section
->segment_mark
)
7723 section
->segment_mark
= FALSE
;
7726 return copy_elf_program_header (ibfd
, obfd
);
7730 if (ibfd
->xvec
== obfd
->xvec
)
7732 /* When rewriting program header, set the output maxpagesize to
7733 the maximum alignment of input PT_LOAD segments. */
7734 Elf_Internal_Phdr
*segment
;
7736 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7737 bfd_vma maxpagesize
= 0;
7739 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7742 if (segment
->p_type
== PT_LOAD
7743 && maxpagesize
< segment
->p_align
)
7745 /* PR 17512: file: f17299af. */
7746 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7747 /* xgettext:c-format */
7748 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7749 PRIx64
" is too large"),
7750 ibfd
, (uint64_t) segment
->p_align
);
7752 maxpagesize
= segment
->p_align
;
7755 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7756 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7759 return rewrite_elf_program_header (ibfd
, obfd
);
7762 /* Initialize private output section information from input section. */
7765 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7769 struct bfd_link_info
*link_info
)
7772 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7773 bfd_boolean final_link
= (link_info
!= NULL
7774 && !bfd_link_relocatable (link_info
));
7776 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7777 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7780 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7782 /* If this is a known ABI section, ELF section type and flags may
7783 have been set up when OSEC was created. For normal sections we
7784 allow the user to override the type and flags other than
7785 SHF_MASKOS and SHF_MASKPROC. */
7786 if (elf_section_type (osec
) == SHT_PROGBITS
7787 || elf_section_type (osec
) == SHT_NOTE
7788 || elf_section_type (osec
) == SHT_NOBITS
)
7789 elf_section_type (osec
) = SHT_NULL
;
7790 /* For objcopy and relocatable link, copy the ELF section type from
7791 the input file if the BFD section flags are the same. (If they
7792 are different the user may be doing something like
7793 "objcopy --set-section-flags .text=alloc,data".) For a final
7794 link allow some flags that the linker clears to differ. */
7795 if (elf_section_type (osec
) == SHT_NULL
7796 && (osec
->flags
== isec
->flags
7798 && ((osec
->flags
^ isec
->flags
)
7799 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7800 elf_section_type (osec
) = elf_section_type (isec
);
7802 /* FIXME: Is this correct for all OS/PROC specific flags? */
7803 elf_section_flags (osec
) = (elf_section_flags (isec
)
7804 & (SHF_MASKOS
| SHF_MASKPROC
));
7806 /* Copy sh_info from input for mbind section. */
7807 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7808 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7809 elf_section_data (osec
)->this_hdr
.sh_info
7810 = elf_section_data (isec
)->this_hdr
.sh_info
;
7812 /* Set things up for objcopy and relocatable link. The output
7813 SHT_GROUP section will have its elf_next_in_group pointing back
7814 to the input group members. Ignore linker created group section.
7815 See elfNN_ia64_object_p in elfxx-ia64.c. */
7816 if ((link_info
== NULL
7817 || !link_info
->resolve_section_groups
)
7818 && (elf_sec_group (isec
) == NULL
7819 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7821 if (elf_section_flags (isec
) & SHF_GROUP
)
7822 elf_section_flags (osec
) |= SHF_GROUP
;
7823 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7824 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7827 /* If not decompress, preserve SHF_COMPRESSED. */
7828 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7829 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7832 ihdr
= &elf_section_data (isec
)->this_hdr
;
7834 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7835 don't use the output section of the linked-to section since it
7836 may be NULL at this point. */
7837 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7839 ohdr
= &elf_section_data (osec
)->this_hdr
;
7840 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7841 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7844 osec
->use_rela_p
= isec
->use_rela_p
;
7849 /* Copy private section information. This copies over the entsize
7850 field, and sometimes the info field. */
7853 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7858 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7860 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7861 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7864 ihdr
= &elf_section_data (isec
)->this_hdr
;
7865 ohdr
= &elf_section_data (osec
)->this_hdr
;
7867 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7869 if (ihdr
->sh_type
== SHT_SYMTAB
7870 || ihdr
->sh_type
== SHT_DYNSYM
7871 || ihdr
->sh_type
== SHT_GNU_verneed
7872 || ihdr
->sh_type
== SHT_GNU_verdef
)
7873 ohdr
->sh_info
= ihdr
->sh_info
;
7875 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7879 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7880 necessary if we are removing either the SHT_GROUP section or any of
7881 the group member sections. DISCARDED is the value that a section's
7882 output_section has if the section will be discarded, NULL when this
7883 function is called from objcopy, bfd_abs_section_ptr when called
7887 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7891 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7892 if (elf_section_type (isec
) == SHT_GROUP
)
7894 asection
*first
= elf_next_in_group (isec
);
7895 asection
*s
= first
;
7896 bfd_size_type removed
= 0;
7900 /* If this member section is being output but the
7901 SHT_GROUP section is not, then clear the group info
7902 set up by _bfd_elf_copy_private_section_data. */
7903 if (s
->output_section
!= discarded
7904 && isec
->output_section
== discarded
)
7906 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7907 elf_group_name (s
->output_section
) = NULL
;
7911 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7912 if (s
->output_section
== discarded
7913 && isec
->output_section
!= discarded
)
7915 /* Conversely, if the member section is not being
7916 output but the SHT_GROUP section is, then adjust
7919 if (elf_sec
->rel
.hdr
!= NULL
7920 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7922 if (elf_sec
->rela
.hdr
!= NULL
7923 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7928 /* Also adjust for zero-sized relocation member
7930 if (elf_sec
->rel
.hdr
!= NULL
7931 && elf_sec
->rel
.hdr
->sh_size
== 0)
7933 if (elf_sec
->rela
.hdr
!= NULL
7934 && elf_sec
->rela
.hdr
->sh_size
== 0)
7938 s
= elf_next_in_group (s
);
7944 if (discarded
!= NULL
)
7946 /* If we've been called for ld -r, then we need to
7947 adjust the input section size. */
7948 if (isec
->rawsize
== 0)
7949 isec
->rawsize
= isec
->size
;
7950 isec
->size
= isec
->rawsize
- removed
;
7951 if (isec
->size
<= 4)
7954 isec
->flags
|= SEC_EXCLUDE
;
7959 /* Adjust the output section size when called from
7961 isec
->output_section
->size
-= removed
;
7962 if (isec
->output_section
->size
<= 4)
7964 isec
->output_section
->size
= 0;
7965 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7974 /* Copy private header information. */
7977 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7979 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7980 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7983 /* Copy over private BFD data if it has not already been copied.
7984 This must be done here, rather than in the copy_private_bfd_data
7985 entry point, because the latter is called after the section
7986 contents have been set, which means that the program headers have
7987 already been worked out. */
7988 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7990 if (! copy_private_bfd_data (ibfd
, obfd
))
7994 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7997 /* Copy private symbol information. If this symbol is in a section
7998 which we did not map into a BFD section, try to map the section
7999 index correctly. We use special macro definitions for the mapped
8000 section indices; these definitions are interpreted by the
8001 swap_out_syms function. */
8003 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8004 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8005 #define MAP_STRTAB (SHN_HIOS + 3)
8006 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8007 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8010 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8015 elf_symbol_type
*isym
, *osym
;
8017 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8018 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8021 isym
= elf_symbol_from (isymarg
);
8022 osym
= elf_symbol_from (osymarg
);
8025 && isym
->internal_elf_sym
.st_shndx
!= 0
8027 && bfd_is_abs_section (isym
->symbol
.section
))
8031 shndx
= isym
->internal_elf_sym
.st_shndx
;
8032 if (shndx
== elf_onesymtab (ibfd
))
8033 shndx
= MAP_ONESYMTAB
;
8034 else if (shndx
== elf_dynsymtab (ibfd
))
8035 shndx
= MAP_DYNSYMTAB
;
8036 else if (shndx
== elf_strtab_sec (ibfd
))
8038 else if (shndx
== elf_shstrtab_sec (ibfd
))
8039 shndx
= MAP_SHSTRTAB
;
8040 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8041 shndx
= MAP_SYM_SHNDX
;
8042 osym
->internal_elf_sym
.st_shndx
= shndx
;
8048 /* Swap out the symbols. */
8051 swap_out_syms (bfd
*abfd
,
8052 struct elf_strtab_hash
**sttp
,
8055 const struct elf_backend_data
*bed
;
8056 unsigned int symcount
;
8058 struct elf_strtab_hash
*stt
;
8059 Elf_Internal_Shdr
*symtab_hdr
;
8060 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8061 Elf_Internal_Shdr
*symstrtab_hdr
;
8062 struct elf_sym_strtab
*symstrtab
;
8063 bfd_byte
*outbound_syms
;
8064 bfd_byte
*outbound_shndx
;
8065 unsigned long outbound_syms_index
;
8066 unsigned long outbound_shndx_index
;
8068 unsigned int num_locals
;
8070 bfd_boolean name_local_sections
;
8072 if (!elf_map_symbols (abfd
, &num_locals
))
8075 /* Dump out the symtabs. */
8076 stt
= _bfd_elf_strtab_init ();
8080 bed
= get_elf_backend_data (abfd
);
8081 symcount
= bfd_get_symcount (abfd
);
8082 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8083 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8084 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8085 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8086 symtab_hdr
->sh_info
= num_locals
+ 1;
8087 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8089 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8090 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8092 /* Allocate buffer to swap out the .strtab section. */
8093 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8094 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8096 bfd_set_error (bfd_error_no_memory
);
8097 _bfd_elf_strtab_free (stt
);
8101 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8102 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8105 bfd_set_error (bfd_error_no_memory
);
8108 _bfd_elf_strtab_free (stt
);
8111 symtab_hdr
->contents
= outbound_syms
;
8112 outbound_syms_index
= 0;
8114 outbound_shndx
= NULL
;
8115 outbound_shndx_index
= 0;
8117 if (elf_symtab_shndx_list (abfd
))
8119 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8120 if (symtab_shndx_hdr
->sh_name
!= 0)
8122 if (_bfd_mul_overflow (symcount
+ 1,
8123 sizeof (Elf_External_Sym_Shndx
), &amt
))
8125 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8126 if (outbound_shndx
== NULL
)
8129 symtab_shndx_hdr
->contents
= outbound_shndx
;
8130 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8131 symtab_shndx_hdr
->sh_size
= amt
;
8132 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8133 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8135 /* FIXME: What about any other headers in the list ? */
8138 /* Now generate the data (for "contents"). */
8140 /* Fill in zeroth symbol and swap it out. */
8141 Elf_Internal_Sym sym
;
8147 sym
.st_shndx
= SHN_UNDEF
;
8148 sym
.st_target_internal
= 0;
8149 symstrtab
[0].sym
= sym
;
8150 symstrtab
[0].dest_index
= outbound_syms_index
;
8151 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8152 outbound_syms_index
++;
8153 if (outbound_shndx
!= NULL
)
8154 outbound_shndx_index
++;
8158 = (bed
->elf_backend_name_local_section_symbols
8159 && bed
->elf_backend_name_local_section_symbols (abfd
));
8161 syms
= bfd_get_outsymbols (abfd
);
8162 for (idx
= 0; idx
< symcount
;)
8164 Elf_Internal_Sym sym
;
8165 bfd_vma value
= syms
[idx
]->value
;
8166 elf_symbol_type
*type_ptr
;
8167 flagword flags
= syms
[idx
]->flags
;
8170 if (!name_local_sections
8171 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8173 /* Local section symbols have no name. */
8174 sym
.st_name
= (unsigned long) -1;
8178 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8179 to get the final offset for st_name. */
8181 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8183 if (sym
.st_name
== (unsigned long) -1)
8187 type_ptr
= elf_symbol_from (syms
[idx
]);
8189 if ((flags
& BSF_SECTION_SYM
) == 0
8190 && bfd_is_com_section (syms
[idx
]->section
))
8192 /* ELF common symbols put the alignment into the `value' field,
8193 and the size into the `size' field. This is backwards from
8194 how BFD handles it, so reverse it here. */
8195 sym
.st_size
= value
;
8196 if (type_ptr
== NULL
8197 || type_ptr
->internal_elf_sym
.st_value
== 0)
8198 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8200 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8201 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8202 (abfd
, syms
[idx
]->section
);
8206 asection
*sec
= syms
[idx
]->section
;
8209 if (sec
->output_section
)
8211 value
+= sec
->output_offset
;
8212 sec
= sec
->output_section
;
8215 /* Don't add in the section vma for relocatable output. */
8216 if (! relocatable_p
)
8218 sym
.st_value
= value
;
8219 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8221 if (bfd_is_abs_section (sec
)
8223 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8225 /* This symbol is in a real ELF section which we did
8226 not create as a BFD section. Undo the mapping done
8227 by copy_private_symbol_data. */
8228 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8232 shndx
= elf_onesymtab (abfd
);
8235 shndx
= elf_dynsymtab (abfd
);
8238 shndx
= elf_strtab_sec (abfd
);
8241 shndx
= elf_shstrtab_sec (abfd
);
8244 if (elf_symtab_shndx_list (abfd
))
8245 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8252 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8254 if (bed
->symbol_section_index
)
8255 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8256 /* Otherwise just leave the index alone. */
8260 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8261 _bfd_error_handler (_("%pB: \
8262 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8271 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8273 if (shndx
== SHN_BAD
)
8277 /* Writing this would be a hell of a lot easier if
8278 we had some decent documentation on bfd, and
8279 knew what to expect of the library, and what to
8280 demand of applications. For example, it
8281 appears that `objcopy' might not set the
8282 section of a symbol to be a section that is
8283 actually in the output file. */
8284 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8286 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8287 if (shndx
== SHN_BAD
)
8289 /* xgettext:c-format */
8291 (_("unable to find equivalent output section"
8292 " for symbol '%s' from section '%s'"),
8293 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8295 bfd_set_error (bfd_error_invalid_operation
);
8301 sym
.st_shndx
= shndx
;
8304 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8306 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8307 type
= STT_GNU_IFUNC
;
8308 else if ((flags
& BSF_FUNCTION
) != 0)
8310 else if ((flags
& BSF_OBJECT
) != 0)
8312 else if ((flags
& BSF_RELC
) != 0)
8314 else if ((flags
& BSF_SRELC
) != 0)
8319 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8322 /* Processor-specific types. */
8323 if (type_ptr
!= NULL
8324 && bed
->elf_backend_get_symbol_type
)
8325 type
= ((*bed
->elf_backend_get_symbol_type
)
8326 (&type_ptr
->internal_elf_sym
, type
));
8328 if (flags
& BSF_SECTION_SYM
)
8330 if (flags
& BSF_GLOBAL
)
8331 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8333 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8335 else if (bfd_is_com_section (syms
[idx
]->section
))
8337 if (type
!= STT_TLS
)
8339 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8340 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8341 ? STT_COMMON
: STT_OBJECT
);
8343 type
= ((flags
& BSF_ELF_COMMON
) != 0
8344 ? STT_COMMON
: STT_OBJECT
);
8346 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8348 else if (bfd_is_und_section (syms
[idx
]->section
))
8349 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8353 else if (flags
& BSF_FILE
)
8354 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8357 int bind
= STB_LOCAL
;
8359 if (flags
& BSF_LOCAL
)
8361 else if (flags
& BSF_GNU_UNIQUE
)
8362 bind
= STB_GNU_UNIQUE
;
8363 else if (flags
& BSF_WEAK
)
8365 else if (flags
& BSF_GLOBAL
)
8368 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8371 if (type_ptr
!= NULL
)
8373 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8374 sym
.st_target_internal
8375 = type_ptr
->internal_elf_sym
.st_target_internal
;
8380 sym
.st_target_internal
= 0;
8384 symstrtab
[idx
].sym
= sym
;
8385 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8386 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8388 outbound_syms_index
++;
8389 if (outbound_shndx
!= NULL
)
8390 outbound_shndx_index
++;
8393 /* Finalize the .strtab section. */
8394 _bfd_elf_strtab_finalize (stt
);
8396 /* Swap out the .strtab section. */
8397 for (idx
= 0; idx
<= symcount
; idx
++)
8399 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8400 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8401 elfsym
->sym
.st_name
= 0;
8403 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8404 elfsym
->sym
.st_name
);
8405 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8407 + (elfsym
->dest_index
8408 * bed
->s
->sizeof_sym
)),
8410 + (elfsym
->destshndx_index
8411 * sizeof (Elf_External_Sym_Shndx
))));
8416 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8417 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8418 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8419 symstrtab_hdr
->sh_addr
= 0;
8420 symstrtab_hdr
->sh_entsize
= 0;
8421 symstrtab_hdr
->sh_link
= 0;
8422 symstrtab_hdr
->sh_info
= 0;
8423 symstrtab_hdr
->sh_addralign
= 1;
8428 /* Return the number of bytes required to hold the symtab vector.
8430 Note that we base it on the count plus 1, since we will null terminate
8431 the vector allocated based on this size. However, the ELF symbol table
8432 always has a dummy entry as symbol #0, so it ends up even. */
8435 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8437 bfd_size_type symcount
;
8439 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8441 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8442 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
8444 bfd_set_error (bfd_error_file_too_big
);
8447 symtab_size
= symcount
* (sizeof (asymbol
*));
8449 symtab_size
= sizeof (asymbol
*);
8450 else if (!bfd_write_p (abfd
))
8452 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8454 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
8456 bfd_set_error (bfd_error_file_truncated
);
8465 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8467 bfd_size_type symcount
;
8469 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8471 if (elf_dynsymtab (abfd
) == 0)
8473 bfd_set_error (bfd_error_invalid_operation
);
8477 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8478 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
8480 bfd_set_error (bfd_error_file_too_big
);
8483 symtab_size
= symcount
* (sizeof (asymbol
*));
8485 symtab_size
= sizeof (asymbol
*);
8486 else if (!bfd_write_p (abfd
))
8488 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8490 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
8492 bfd_set_error (bfd_error_file_truncated
);
8501 _bfd_elf_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
8503 if (asect
->reloc_count
!= 0 && !bfd_write_p (abfd
))
8505 /* Sanity check reloc section size. */
8506 struct bfd_elf_section_data
*d
= elf_section_data (asect
);
8507 Elf_Internal_Shdr
*rel_hdr
= &d
->this_hdr
;
8508 bfd_size_type ext_rel_size
= rel_hdr
->sh_size
;
8509 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8511 if (filesize
!= 0 && ext_rel_size
> filesize
)
8513 bfd_set_error (bfd_error_file_truncated
);
8518 #if SIZEOF_LONG == SIZEOF_INT
8519 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8521 bfd_set_error (bfd_error_file_too_big
);
8525 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8528 /* Canonicalize the relocs. */
8531 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8538 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8540 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8543 tblptr
= section
->relocation
;
8544 for (i
= 0; i
< section
->reloc_count
; i
++)
8545 *relptr
++ = tblptr
++;
8549 return section
->reloc_count
;
8553 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8555 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8556 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8559 abfd
->symcount
= symcount
;
8564 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8565 asymbol
**allocation
)
8567 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8568 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8571 abfd
->dynsymcount
= symcount
;
8575 /* Return the size required for the dynamic reloc entries. Any loadable
8576 section that was actually installed in the BFD, and has type SHT_REL
8577 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8578 dynamic reloc section. */
8581 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8583 bfd_size_type count
, ext_rel_size
;
8586 if (elf_dynsymtab (abfd
) == 0)
8588 bfd_set_error (bfd_error_invalid_operation
);
8594 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8595 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8596 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8597 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8599 ext_rel_size
+= s
->size
;
8600 if (ext_rel_size
< s
->size
)
8602 bfd_set_error (bfd_error_file_truncated
);
8605 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8606 if (count
> LONG_MAX
/ sizeof (arelent
*))
8608 bfd_set_error (bfd_error_file_too_big
);
8612 if (count
> 1 && !bfd_write_p (abfd
))
8614 /* Sanity check reloc section sizes. */
8615 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8616 if (filesize
!= 0 && ext_rel_size
> filesize
)
8618 bfd_set_error (bfd_error_file_truncated
);
8622 return count
* sizeof (arelent
*);
8625 /* Canonicalize the dynamic relocation entries. Note that we return the
8626 dynamic relocations as a single block, although they are actually
8627 associated with particular sections; the interface, which was
8628 designed for SunOS style shared libraries, expects that there is only
8629 one set of dynamic relocs. Any loadable section that was actually
8630 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8631 dynamic symbol table, is considered to be a dynamic reloc section. */
8634 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8638 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8642 if (elf_dynsymtab (abfd
) == 0)
8644 bfd_set_error (bfd_error_invalid_operation
);
8648 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8650 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8652 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8653 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8654 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8659 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8661 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8663 for (i
= 0; i
< count
; i
++)
8674 /* Read in the version information. */
8677 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8679 bfd_byte
*contents
= NULL
;
8680 unsigned int freeidx
= 0;
8683 if (elf_dynverref (abfd
) != 0)
8685 Elf_Internal_Shdr
*hdr
;
8686 Elf_External_Verneed
*everneed
;
8687 Elf_Internal_Verneed
*iverneed
;
8689 bfd_byte
*contents_end
;
8691 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8693 if (hdr
->sh_info
== 0
8694 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8696 error_return_bad_verref
:
8698 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8699 bfd_set_error (bfd_error_bad_value
);
8700 error_return_verref
:
8701 elf_tdata (abfd
)->verref
= NULL
;
8702 elf_tdata (abfd
)->cverrefs
= 0;
8706 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8707 goto error_return_verref
;
8708 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8709 if (contents
== NULL
)
8710 goto error_return_verref
;
8712 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8714 bfd_set_error (bfd_error_file_too_big
);
8715 goto error_return_verref
;
8717 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8718 if (elf_tdata (abfd
)->verref
== NULL
)
8719 goto error_return_verref
;
8721 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8722 == sizeof (Elf_External_Vernaux
));
8723 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8724 everneed
= (Elf_External_Verneed
*) contents
;
8725 iverneed
= elf_tdata (abfd
)->verref
;
8726 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8728 Elf_External_Vernaux
*evernaux
;
8729 Elf_Internal_Vernaux
*ivernaux
;
8732 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8734 iverneed
->vn_bfd
= abfd
;
8736 iverneed
->vn_filename
=
8737 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8739 if (iverneed
->vn_filename
== NULL
)
8740 goto error_return_bad_verref
;
8742 if (iverneed
->vn_cnt
== 0)
8743 iverneed
->vn_auxptr
= NULL
;
8746 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8747 sizeof (Elf_Internal_Vernaux
), &amt
))
8749 bfd_set_error (bfd_error_file_too_big
);
8750 goto error_return_verref
;
8752 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8753 bfd_alloc (abfd
, amt
);
8754 if (iverneed
->vn_auxptr
== NULL
)
8755 goto error_return_verref
;
8758 if (iverneed
->vn_aux
8759 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8760 goto error_return_bad_verref
;
8762 evernaux
= ((Elf_External_Vernaux
*)
8763 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8764 ivernaux
= iverneed
->vn_auxptr
;
8765 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8767 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8769 ivernaux
->vna_nodename
=
8770 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8771 ivernaux
->vna_name
);
8772 if (ivernaux
->vna_nodename
== NULL
)
8773 goto error_return_bad_verref
;
8775 if (ivernaux
->vna_other
> freeidx
)
8776 freeidx
= ivernaux
->vna_other
;
8778 ivernaux
->vna_nextptr
= NULL
;
8779 if (ivernaux
->vna_next
== 0)
8781 iverneed
->vn_cnt
= j
+ 1;
8784 if (j
+ 1 < iverneed
->vn_cnt
)
8785 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8787 if (ivernaux
->vna_next
8788 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8789 goto error_return_bad_verref
;
8791 evernaux
= ((Elf_External_Vernaux
*)
8792 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8795 iverneed
->vn_nextref
= NULL
;
8796 if (iverneed
->vn_next
== 0)
8798 if (i
+ 1 < hdr
->sh_info
)
8799 iverneed
->vn_nextref
= iverneed
+ 1;
8801 if (iverneed
->vn_next
8802 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8803 goto error_return_bad_verref
;
8805 everneed
= ((Elf_External_Verneed
*)
8806 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8808 elf_tdata (abfd
)->cverrefs
= i
;
8814 if (elf_dynverdef (abfd
) != 0)
8816 Elf_Internal_Shdr
*hdr
;
8817 Elf_External_Verdef
*everdef
;
8818 Elf_Internal_Verdef
*iverdef
;
8819 Elf_Internal_Verdef
*iverdefarr
;
8820 Elf_Internal_Verdef iverdefmem
;
8822 unsigned int maxidx
;
8823 bfd_byte
*contents_end_def
, *contents_end_aux
;
8825 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8827 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8829 error_return_bad_verdef
:
8831 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8832 bfd_set_error (bfd_error_bad_value
);
8833 error_return_verdef
:
8834 elf_tdata (abfd
)->verdef
= NULL
;
8835 elf_tdata (abfd
)->cverdefs
= 0;
8839 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8840 goto error_return_verdef
;
8841 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8842 if (contents
== NULL
)
8843 goto error_return_verdef
;
8845 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8846 >= sizeof (Elf_External_Verdaux
));
8847 contents_end_def
= contents
+ hdr
->sh_size
8848 - sizeof (Elf_External_Verdef
);
8849 contents_end_aux
= contents
+ hdr
->sh_size
8850 - sizeof (Elf_External_Verdaux
);
8852 /* We know the number of entries in the section but not the maximum
8853 index. Therefore we have to run through all entries and find
8855 everdef
= (Elf_External_Verdef
*) contents
;
8857 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8859 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8861 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8862 goto error_return_bad_verdef
;
8863 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8864 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8866 if (iverdefmem
.vd_next
== 0)
8869 if (iverdefmem
.vd_next
8870 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8871 goto error_return_bad_verdef
;
8873 everdef
= ((Elf_External_Verdef
*)
8874 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8877 if (default_imported_symver
)
8879 if (freeidx
> maxidx
)
8884 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8886 bfd_set_error (bfd_error_file_too_big
);
8887 goto error_return_verdef
;
8889 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8890 if (elf_tdata (abfd
)->verdef
== NULL
)
8891 goto error_return_verdef
;
8893 elf_tdata (abfd
)->cverdefs
= maxidx
;
8895 everdef
= (Elf_External_Verdef
*) contents
;
8896 iverdefarr
= elf_tdata (abfd
)->verdef
;
8897 for (i
= 0; i
< hdr
->sh_info
; i
++)
8899 Elf_External_Verdaux
*everdaux
;
8900 Elf_Internal_Verdaux
*iverdaux
;
8903 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8905 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8906 goto error_return_bad_verdef
;
8908 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8909 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8911 iverdef
->vd_bfd
= abfd
;
8913 if (iverdef
->vd_cnt
== 0)
8914 iverdef
->vd_auxptr
= NULL
;
8917 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
8918 sizeof (Elf_Internal_Verdaux
), &amt
))
8920 bfd_set_error (bfd_error_file_too_big
);
8921 goto error_return_verdef
;
8923 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8924 bfd_alloc (abfd
, amt
);
8925 if (iverdef
->vd_auxptr
== NULL
)
8926 goto error_return_verdef
;
8930 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8931 goto error_return_bad_verdef
;
8933 everdaux
= ((Elf_External_Verdaux
*)
8934 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8935 iverdaux
= iverdef
->vd_auxptr
;
8936 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8938 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8940 iverdaux
->vda_nodename
=
8941 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8942 iverdaux
->vda_name
);
8943 if (iverdaux
->vda_nodename
== NULL
)
8944 goto error_return_bad_verdef
;
8946 iverdaux
->vda_nextptr
= NULL
;
8947 if (iverdaux
->vda_next
== 0)
8949 iverdef
->vd_cnt
= j
+ 1;
8952 if (j
+ 1 < iverdef
->vd_cnt
)
8953 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8955 if (iverdaux
->vda_next
8956 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8957 goto error_return_bad_verdef
;
8959 everdaux
= ((Elf_External_Verdaux
*)
8960 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8963 iverdef
->vd_nodename
= NULL
;
8964 if (iverdef
->vd_cnt
)
8965 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8967 iverdef
->vd_nextdef
= NULL
;
8968 if (iverdef
->vd_next
== 0)
8970 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8971 iverdef
->vd_nextdef
= iverdef
+ 1;
8973 everdef
= ((Elf_External_Verdef
*)
8974 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8980 else if (default_imported_symver
)
8987 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8989 bfd_set_error (bfd_error_file_too_big
);
8992 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8993 if (elf_tdata (abfd
)->verdef
== NULL
)
8996 elf_tdata (abfd
)->cverdefs
= freeidx
;
8999 /* Create a default version based on the soname. */
9000 if (default_imported_symver
)
9002 Elf_Internal_Verdef
*iverdef
;
9003 Elf_Internal_Verdaux
*iverdaux
;
9005 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
9007 iverdef
->vd_version
= VER_DEF_CURRENT
;
9008 iverdef
->vd_flags
= 0;
9009 iverdef
->vd_ndx
= freeidx
;
9010 iverdef
->vd_cnt
= 1;
9012 iverdef
->vd_bfd
= abfd
;
9014 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
9015 if (iverdef
->vd_nodename
== NULL
)
9016 goto error_return_verdef
;
9017 iverdef
->vd_nextdef
= NULL
;
9018 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
9019 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
9020 if (iverdef
->vd_auxptr
== NULL
)
9021 goto error_return_verdef
;
9023 iverdaux
= iverdef
->vd_auxptr
;
9024 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9035 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9037 elf_symbol_type
*newsym
;
9039 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9042 newsym
->symbol
.the_bfd
= abfd
;
9043 return &newsym
->symbol
;
9047 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9051 bfd_symbol_info (symbol
, ret
);
9054 /* Return whether a symbol name implies a local symbol. Most targets
9055 use this function for the is_local_label_name entry point, but some
9059 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9062 /* Normal local symbols start with ``.L''. */
9063 if (name
[0] == '.' && name
[1] == 'L')
9066 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9067 DWARF debugging symbols starting with ``..''. */
9068 if (name
[0] == '.' && name
[1] == '.')
9071 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9072 emitting DWARF debugging output. I suspect this is actually a
9073 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9074 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9075 underscore to be emitted on some ELF targets). For ease of use,
9076 we treat such symbols as local. */
9077 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9080 /* Treat assembler generated fake symbols, dollar local labels and
9081 forward-backward labels (aka local labels) as locals.
9082 These labels have the form:
9084 L0^A.* (fake symbols)
9086 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9088 Versions which start with .L will have already been matched above,
9089 so we only need to match the rest. */
9090 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9092 bfd_boolean ret
= FALSE
;
9096 for (p
= name
+ 2; (c
= *p
); p
++)
9098 if (c
== 1 || c
== 2)
9100 if (c
== 1 && p
== name
+ 2)
9101 /* A fake symbol. */
9104 /* FIXME: We are being paranoid here and treating symbols like
9105 L0^Bfoo as if there were non-local, on the grounds that the
9106 assembler will never generate them. But can any symbol
9107 containing an ASCII value in the range 1-31 ever be anything
9108 other than some kind of local ? */
9125 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9126 asymbol
*symbol ATTRIBUTE_UNUSED
)
9133 _bfd_elf_set_arch_mach (bfd
*abfd
,
9134 enum bfd_architecture arch
,
9135 unsigned long machine
)
9137 /* If this isn't the right architecture for this backend, and this
9138 isn't the generic backend, fail. */
9139 if (arch
!= get_elf_backend_data (abfd
)->arch
9140 && arch
!= bfd_arch_unknown
9141 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9144 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9147 /* Find the nearest line to a particular section and offset,
9148 for error reporting. */
9151 _bfd_elf_find_nearest_line (bfd
*abfd
,
9155 const char **filename_ptr
,
9156 const char **functionname_ptr
,
9157 unsigned int *line_ptr
,
9158 unsigned int *discriminator_ptr
)
9162 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
9163 filename_ptr
, functionname_ptr
,
9164 line_ptr
, discriminator_ptr
,
9165 dwarf_debug_sections
,
9166 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9169 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9170 filename_ptr
, functionname_ptr
, line_ptr
))
9172 if (!*functionname_ptr
)
9173 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9174 *filename_ptr
? NULL
: filename_ptr
,
9179 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9180 &found
, filename_ptr
,
9181 functionname_ptr
, line_ptr
,
9182 &elf_tdata (abfd
)->line_info
))
9184 if (found
&& (*functionname_ptr
|| *line_ptr
))
9187 if (symbols
== NULL
)
9190 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9191 filename_ptr
, functionname_ptr
))
9198 /* Find the line for a symbol. */
9201 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9202 const char **filename_ptr
, unsigned int *line_ptr
)
9204 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9205 filename_ptr
, NULL
, line_ptr
, NULL
,
9206 dwarf_debug_sections
,
9207 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9210 /* After a call to bfd_find_nearest_line, successive calls to
9211 bfd_find_inliner_info can be used to get source information about
9212 each level of function inlining that terminated at the address
9213 passed to bfd_find_nearest_line. Currently this is only supported
9214 for DWARF2 with appropriate DWARF3 extensions. */
9217 _bfd_elf_find_inliner_info (bfd
*abfd
,
9218 const char **filename_ptr
,
9219 const char **functionname_ptr
,
9220 unsigned int *line_ptr
)
9223 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9224 functionname_ptr
, line_ptr
,
9225 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9230 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9232 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9233 int ret
= bed
->s
->sizeof_ehdr
;
9235 if (!bfd_link_relocatable (info
))
9237 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9239 if (phdr_size
== (bfd_size_type
) -1)
9241 struct elf_segment_map
*m
;
9244 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9245 phdr_size
+= bed
->s
->sizeof_phdr
;
9248 phdr_size
= get_program_header_size (abfd
, info
);
9251 elf_program_header_size (abfd
) = phdr_size
;
9259 _bfd_elf_set_section_contents (bfd
*abfd
,
9261 const void *location
,
9263 bfd_size_type count
)
9265 Elf_Internal_Shdr
*hdr
;
9268 if (! abfd
->output_has_begun
9269 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9275 hdr
= &elf_section_data (section
)->this_hdr
;
9276 if (hdr
->sh_offset
== (file_ptr
) -1)
9278 unsigned char *contents
;
9280 if (bfd_section_is_ctf (section
))
9281 /* Nothing to do with this section: the contents are generated
9285 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9288 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9290 bfd_set_error (bfd_error_invalid_operation
);
9294 if ((offset
+ count
) > hdr
->sh_size
)
9297 (_("%pB:%pA: error: attempting to write over the end of the section"),
9300 bfd_set_error (bfd_error_invalid_operation
);
9304 contents
= hdr
->contents
;
9305 if (contents
== NULL
)
9308 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9311 bfd_set_error (bfd_error_invalid_operation
);
9315 memcpy (contents
+ offset
, location
, count
);
9319 pos
= hdr
->sh_offset
+ offset
;
9320 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9321 || bfd_bwrite (location
, count
, abfd
) != count
)
9328 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9329 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9330 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9336 /* Try to convert a non-ELF reloc into an ELF one. */
9339 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9341 /* Check whether we really have an ELF howto. */
9343 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9345 bfd_reloc_code_real_type code
;
9346 reloc_howto_type
*howto
;
9348 /* Alien reloc: Try to determine its type to replace it with an
9349 equivalent ELF reloc. */
9351 if (areloc
->howto
->pc_relative
)
9353 switch (areloc
->howto
->bitsize
)
9356 code
= BFD_RELOC_8_PCREL
;
9359 code
= BFD_RELOC_12_PCREL
;
9362 code
= BFD_RELOC_16_PCREL
;
9365 code
= BFD_RELOC_24_PCREL
;
9368 code
= BFD_RELOC_32_PCREL
;
9371 code
= BFD_RELOC_64_PCREL
;
9377 howto
= bfd_reloc_type_lookup (abfd
, code
);
9379 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9381 if (howto
->pcrel_offset
)
9382 areloc
->addend
+= areloc
->address
;
9384 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9389 switch (areloc
->howto
->bitsize
)
9395 code
= BFD_RELOC_14
;
9398 code
= BFD_RELOC_16
;
9401 code
= BFD_RELOC_26
;
9404 code
= BFD_RELOC_32
;
9407 code
= BFD_RELOC_64
;
9413 howto
= bfd_reloc_type_lookup (abfd
, code
);
9417 areloc
->howto
= howto
;
9425 /* xgettext:c-format */
9426 _bfd_error_handler (_("%pB: %s unsupported"),
9427 abfd
, areloc
->howto
->name
);
9428 bfd_set_error (bfd_error_sorry
);
9433 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9435 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9437 && (bfd_get_format (abfd
) == bfd_object
9438 || bfd_get_format (abfd
) == bfd_core
))
9440 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9441 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9442 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9445 return _bfd_generic_close_and_cleanup (abfd
);
9448 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9449 in the relocation's offset. Thus we cannot allow any sort of sanity
9450 range-checking to interfere. There is nothing else to do in processing
9453 bfd_reloc_status_type
9454 _bfd_elf_rel_vtable_reloc_fn
9455 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9456 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9457 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9458 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9460 return bfd_reloc_ok
;
9463 /* Elf core file support. Much of this only works on native
9464 toolchains, since we rely on knowing the
9465 machine-dependent procfs structure in order to pick
9466 out details about the corefile. */
9468 #ifdef HAVE_SYS_PROCFS_H
9469 # include <sys/procfs.h>
9472 /* Return a PID that identifies a "thread" for threaded cores, or the
9473 PID of the main process for non-threaded cores. */
9476 elfcore_make_pid (bfd
*abfd
)
9480 pid
= elf_tdata (abfd
)->core
->lwpid
;
9482 pid
= elf_tdata (abfd
)->core
->pid
;
9487 /* If there isn't a section called NAME, make one, using
9488 data from SECT. Note, this function will generate a
9489 reference to NAME, so you shouldn't deallocate or
9493 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9497 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9500 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9504 sect2
->size
= sect
->size
;
9505 sect2
->filepos
= sect
->filepos
;
9506 sect2
->alignment_power
= sect
->alignment_power
;
9510 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9511 actually creates up to two pseudosections:
9512 - For the single-threaded case, a section named NAME, unless
9513 such a section already exists.
9514 - For the multi-threaded case, a section named "NAME/PID", where
9515 PID is elfcore_make_pid (abfd).
9516 Both pseudosections have identical contents. */
9518 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9524 char *threaded_name
;
9528 /* Build the section name. */
9530 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9531 len
= strlen (buf
) + 1;
9532 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9533 if (threaded_name
== NULL
)
9535 memcpy (threaded_name
, buf
, len
);
9537 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9542 sect
->filepos
= filepos
;
9543 sect
->alignment_power
= 2;
9545 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9549 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9552 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9558 sect
->size
= note
->descsz
- offs
;
9559 sect
->filepos
= note
->descpos
+ offs
;
9560 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9565 /* prstatus_t exists on:
9567 linux 2.[01] + glibc
9571 #if defined (HAVE_PRSTATUS_T)
9574 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9579 if (note
->descsz
== sizeof (prstatus_t
))
9583 size
= sizeof (prstat
.pr_reg
);
9584 offset
= offsetof (prstatus_t
, pr_reg
);
9585 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9587 /* Do not overwrite the core signal if it
9588 has already been set by another thread. */
9589 if (elf_tdata (abfd
)->core
->signal
== 0)
9590 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9591 if (elf_tdata (abfd
)->core
->pid
== 0)
9592 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9594 /* pr_who exists on:
9597 pr_who doesn't exist on:
9600 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9601 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9603 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9606 #if defined (HAVE_PRSTATUS32_T)
9607 else if (note
->descsz
== sizeof (prstatus32_t
))
9609 /* 64-bit host, 32-bit corefile */
9610 prstatus32_t prstat
;
9612 size
= sizeof (prstat
.pr_reg
);
9613 offset
= offsetof (prstatus32_t
, pr_reg
);
9614 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9616 /* Do not overwrite the core signal if it
9617 has already been set by another thread. */
9618 if (elf_tdata (abfd
)->core
->signal
== 0)
9619 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9620 if (elf_tdata (abfd
)->core
->pid
== 0)
9621 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9623 /* pr_who exists on:
9626 pr_who doesn't exist on:
9629 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9630 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9632 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9635 #endif /* HAVE_PRSTATUS32_T */
9638 /* Fail - we don't know how to handle any other
9639 note size (ie. data object type). */
9643 /* Make a ".reg/999" section and a ".reg" section. */
9644 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9645 size
, note
->descpos
+ offset
);
9647 #endif /* defined (HAVE_PRSTATUS_T) */
9649 /* Create a pseudosection containing the exact contents of NOTE. */
9651 elfcore_make_note_pseudosection (bfd
*abfd
,
9653 Elf_Internal_Note
*note
)
9655 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9656 note
->descsz
, note
->descpos
);
9659 /* There isn't a consistent prfpregset_t across platforms,
9660 but it doesn't matter, because we don't have to pick this
9661 data structure apart. */
9664 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9666 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9669 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9670 type of NT_PRXFPREG. Just include the whole note's contents
9674 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9676 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9679 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9680 with a note type of NT_X86_XSTATE. Just include the whole note's
9681 contents literally. */
9684 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9686 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9690 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9692 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9696 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9698 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9702 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9704 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9708 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9710 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9714 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9716 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9720 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9722 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9726 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9728 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9732 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9734 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9738 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9740 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9744 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9746 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9750 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9752 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9756 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9758 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9762 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9764 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9768 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9770 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9774 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9776 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9780 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9782 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9786 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9788 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9792 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9794 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9798 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9800 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9804 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9806 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9810 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9812 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9816 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9818 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9822 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9824 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9828 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9830 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9834 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9836 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9840 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9842 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9846 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9848 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9852 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9854 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9858 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9860 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9864 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9866 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9870 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9872 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9876 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9878 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9882 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9884 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9888 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9890 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9894 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
9896 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
9899 #if defined (HAVE_PRPSINFO_T)
9900 typedef prpsinfo_t elfcore_psinfo_t
;
9901 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9902 typedef prpsinfo32_t elfcore_psinfo32_t
;
9906 #if defined (HAVE_PSINFO_T)
9907 typedef psinfo_t elfcore_psinfo_t
;
9908 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9909 typedef psinfo32_t elfcore_psinfo32_t
;
9913 /* return a malloc'ed copy of a string at START which is at
9914 most MAX bytes long, possibly without a terminating '\0'.
9915 the copy will always have a terminating '\0'. */
9918 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9921 char *end
= (char *) memchr (start
, '\0', max
);
9929 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9933 memcpy (dups
, start
, len
);
9939 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9941 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9943 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9945 elfcore_psinfo_t psinfo
;
9947 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9949 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9950 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9952 elf_tdata (abfd
)->core
->program
9953 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9954 sizeof (psinfo
.pr_fname
));
9956 elf_tdata (abfd
)->core
->command
9957 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9958 sizeof (psinfo
.pr_psargs
));
9960 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9961 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9963 /* 64-bit host, 32-bit corefile */
9964 elfcore_psinfo32_t psinfo
;
9966 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9968 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9969 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9971 elf_tdata (abfd
)->core
->program
9972 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9973 sizeof (psinfo
.pr_fname
));
9975 elf_tdata (abfd
)->core
->command
9976 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9977 sizeof (psinfo
.pr_psargs
));
9983 /* Fail - we don't know how to handle any other
9984 note size (ie. data object type). */
9988 /* Note that for some reason, a spurious space is tacked
9989 onto the end of the args in some (at least one anyway)
9990 implementations, so strip it off if it exists. */
9993 char *command
= elf_tdata (abfd
)->core
->command
;
9994 int n
= strlen (command
);
9996 if (0 < n
&& command
[n
- 1] == ' ')
9997 command
[n
- 1] = '\0';
10002 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10004 #if defined (HAVE_PSTATUS_T)
10006 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10008 if (note
->descsz
== sizeof (pstatus_t
)
10009 #if defined (HAVE_PXSTATUS_T)
10010 || note
->descsz
== sizeof (pxstatus_t
)
10016 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10018 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10020 #if defined (HAVE_PSTATUS32_T)
10021 else if (note
->descsz
== sizeof (pstatus32_t
))
10023 /* 64-bit host, 32-bit corefile */
10026 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10028 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10031 /* Could grab some more details from the "representative"
10032 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10033 NT_LWPSTATUS note, presumably. */
10037 #endif /* defined (HAVE_PSTATUS_T) */
10039 #if defined (HAVE_LWPSTATUS_T)
10041 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10043 lwpstatus_t lwpstat
;
10049 if (note
->descsz
!= sizeof (lwpstat
)
10050 #if defined (HAVE_LWPXSTATUS_T)
10051 && note
->descsz
!= sizeof (lwpxstatus_t
)
10056 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10058 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10059 /* Do not overwrite the core signal if it has already been set by
10061 if (elf_tdata (abfd
)->core
->signal
== 0)
10062 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10064 /* Make a ".reg/999" section. */
10066 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10067 len
= strlen (buf
) + 1;
10068 name
= bfd_alloc (abfd
, len
);
10071 memcpy (name
, buf
, len
);
10073 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10077 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10078 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10079 sect
->filepos
= note
->descpos
10080 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10083 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10084 sect
->size
= sizeof (lwpstat
.pr_reg
);
10085 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10088 sect
->alignment_power
= 2;
10090 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10093 /* Make a ".reg2/999" section */
10095 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10096 len
= strlen (buf
) + 1;
10097 name
= bfd_alloc (abfd
, len
);
10100 memcpy (name
, buf
, len
);
10102 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10106 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10107 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10108 sect
->filepos
= note
->descpos
10109 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10112 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10113 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10114 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10117 sect
->alignment_power
= 2;
10119 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10121 #endif /* defined (HAVE_LWPSTATUS_T) */
10123 /* These constants, and the structure offsets used below, are defined by
10124 Cygwin's core_dump.h */
10125 #define NOTE_INFO_PROCESS 1
10126 #define NOTE_INFO_THREAD 2
10127 #define NOTE_INFO_MODULE 3
10128 #define NOTE_INFO_MODULE64 4
10131 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10136 unsigned int name_size
;
10139 int is_active_thread
;
10142 if (note
->descsz
< 4)
10145 if (! CONST_STRNEQ (note
->namedata
, "win32"))
10148 type
= bfd_get_32 (abfd
, note
->descdata
);
10151 const char *type_name
;
10152 unsigned long min_size
;
10155 { "NOTE_INFO_PROCESS", 12 },
10156 { "NOTE_INFO_THREAD", 12 },
10157 { "NOTE_INFO_MODULE", 12 },
10158 { "NOTE_INFO_MODULE64", 16 },
10161 if (type
> (sizeof(size_check
)/sizeof(size_check
[0])))
10164 if (note
->descsz
< size_check
[type
- 1].min_size
)
10166 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10167 abfd
, size_check
[type
- 1].type_name
, note
->descsz
);
10173 case NOTE_INFO_PROCESS
:
10174 /* FIXME: need to add ->core->command. */
10175 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10176 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10179 case NOTE_INFO_THREAD
:
10180 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10182 /* thread_info.tid */
10183 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 4));
10185 len
= strlen (buf
) + 1;
10186 name
= (char *) bfd_alloc (abfd
, len
);
10190 memcpy (name
, buf
, len
);
10192 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10196 /* sizeof (thread_info.thread_context) */
10197 sect
->size
= note
->descsz
- 12;
10198 /* offsetof (thread_info.thread_context) */
10199 sect
->filepos
= note
->descpos
+ 12;
10200 sect
->alignment_power
= 2;
10202 /* thread_info.is_active_thread */
10203 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10205 if (is_active_thread
)
10206 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10210 case NOTE_INFO_MODULE
:
10211 case NOTE_INFO_MODULE64
:
10212 /* Make a ".module/xxxxxxxx" section. */
10213 if (type
== NOTE_INFO_MODULE
)
10215 /* module_info.base_address */
10216 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10217 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10218 /* module_info.module_name_size */
10219 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10221 else /* NOTE_INFO_MODULE64 */
10223 /* module_info.base_address */
10224 base_addr
= bfd_get_64 (abfd
, note
->descdata
+ 4);
10225 sprintf (buf
, ".module/%016lx", (unsigned long) base_addr
);
10226 /* module_info.module_name_size */
10227 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10230 len
= strlen (buf
) + 1;
10231 name
= (char *) bfd_alloc (abfd
, len
);
10235 memcpy (name
, buf
, len
);
10237 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10242 if (note
->descsz
< 12 + name_size
)
10244 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
10245 abfd
, note
->descsz
, name_size
);
10249 sect
->size
= note
->descsz
;
10250 sect
->filepos
= note
->descpos
;
10251 sect
->alignment_power
= 2;
10262 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10264 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10266 switch (note
->type
)
10272 if (bed
->elf_backend_grok_prstatus
)
10273 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10275 #if defined (HAVE_PRSTATUS_T)
10276 return elfcore_grok_prstatus (abfd
, note
);
10281 #if defined (HAVE_PSTATUS_T)
10283 return elfcore_grok_pstatus (abfd
, note
);
10286 #if defined (HAVE_LWPSTATUS_T)
10288 return elfcore_grok_lwpstatus (abfd
, note
);
10291 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10292 return elfcore_grok_prfpreg (abfd
, note
);
10294 case NT_WIN32PSTATUS
:
10295 return elfcore_grok_win32pstatus (abfd
, note
);
10297 case NT_PRXFPREG
: /* Linux SSE extension */
10298 if (note
->namesz
== 6
10299 && strcmp (note
->namedata
, "LINUX") == 0)
10300 return elfcore_grok_prxfpreg (abfd
, note
);
10304 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10305 if (note
->namesz
== 6
10306 && strcmp (note
->namedata
, "LINUX") == 0)
10307 return elfcore_grok_xstatereg (abfd
, note
);
10312 if (note
->namesz
== 6
10313 && strcmp (note
->namedata
, "LINUX") == 0)
10314 return elfcore_grok_ppc_vmx (abfd
, note
);
10319 if (note
->namesz
== 6
10320 && strcmp (note
->namedata
, "LINUX") == 0)
10321 return elfcore_grok_ppc_vsx (abfd
, note
);
10326 if (note
->namesz
== 6
10327 && strcmp (note
->namedata
, "LINUX") == 0)
10328 return elfcore_grok_ppc_tar (abfd
, note
);
10333 if (note
->namesz
== 6
10334 && strcmp (note
->namedata
, "LINUX") == 0)
10335 return elfcore_grok_ppc_ppr (abfd
, note
);
10340 if (note
->namesz
== 6
10341 && strcmp (note
->namedata
, "LINUX") == 0)
10342 return elfcore_grok_ppc_dscr (abfd
, note
);
10347 if (note
->namesz
== 6
10348 && strcmp (note
->namedata
, "LINUX") == 0)
10349 return elfcore_grok_ppc_ebb (abfd
, note
);
10354 if (note
->namesz
== 6
10355 && strcmp (note
->namedata
, "LINUX") == 0)
10356 return elfcore_grok_ppc_pmu (abfd
, note
);
10360 case NT_PPC_TM_CGPR
:
10361 if (note
->namesz
== 6
10362 && strcmp (note
->namedata
, "LINUX") == 0)
10363 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10367 case NT_PPC_TM_CFPR
:
10368 if (note
->namesz
== 6
10369 && strcmp (note
->namedata
, "LINUX") == 0)
10370 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10374 case NT_PPC_TM_CVMX
:
10375 if (note
->namesz
== 6
10376 && strcmp (note
->namedata
, "LINUX") == 0)
10377 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10381 case NT_PPC_TM_CVSX
:
10382 if (note
->namesz
== 6
10383 && strcmp (note
->namedata
, "LINUX") == 0)
10384 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10388 case NT_PPC_TM_SPR
:
10389 if (note
->namesz
== 6
10390 && strcmp (note
->namedata
, "LINUX") == 0)
10391 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10395 case NT_PPC_TM_CTAR
:
10396 if (note
->namesz
== 6
10397 && strcmp (note
->namedata
, "LINUX") == 0)
10398 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10402 case NT_PPC_TM_CPPR
:
10403 if (note
->namesz
== 6
10404 && strcmp (note
->namedata
, "LINUX") == 0)
10405 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10409 case NT_PPC_TM_CDSCR
:
10410 if (note
->namesz
== 6
10411 && strcmp (note
->namedata
, "LINUX") == 0)
10412 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10416 case NT_S390_HIGH_GPRS
:
10417 if (note
->namesz
== 6
10418 && strcmp (note
->namedata
, "LINUX") == 0)
10419 return elfcore_grok_s390_high_gprs (abfd
, note
);
10423 case NT_S390_TIMER
:
10424 if (note
->namesz
== 6
10425 && strcmp (note
->namedata
, "LINUX") == 0)
10426 return elfcore_grok_s390_timer (abfd
, note
);
10430 case NT_S390_TODCMP
:
10431 if (note
->namesz
== 6
10432 && strcmp (note
->namedata
, "LINUX") == 0)
10433 return elfcore_grok_s390_todcmp (abfd
, note
);
10437 case NT_S390_TODPREG
:
10438 if (note
->namesz
== 6
10439 && strcmp (note
->namedata
, "LINUX") == 0)
10440 return elfcore_grok_s390_todpreg (abfd
, note
);
10445 if (note
->namesz
== 6
10446 && strcmp (note
->namedata
, "LINUX") == 0)
10447 return elfcore_grok_s390_ctrs (abfd
, note
);
10451 case NT_S390_PREFIX
:
10452 if (note
->namesz
== 6
10453 && strcmp (note
->namedata
, "LINUX") == 0)
10454 return elfcore_grok_s390_prefix (abfd
, note
);
10458 case NT_S390_LAST_BREAK
:
10459 if (note
->namesz
== 6
10460 && strcmp (note
->namedata
, "LINUX") == 0)
10461 return elfcore_grok_s390_last_break (abfd
, note
);
10465 case NT_S390_SYSTEM_CALL
:
10466 if (note
->namesz
== 6
10467 && strcmp (note
->namedata
, "LINUX") == 0)
10468 return elfcore_grok_s390_system_call (abfd
, note
);
10473 if (note
->namesz
== 6
10474 && strcmp (note
->namedata
, "LINUX") == 0)
10475 return elfcore_grok_s390_tdb (abfd
, note
);
10479 case NT_S390_VXRS_LOW
:
10480 if (note
->namesz
== 6
10481 && strcmp (note
->namedata
, "LINUX") == 0)
10482 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10486 case NT_S390_VXRS_HIGH
:
10487 if (note
->namesz
== 6
10488 && strcmp (note
->namedata
, "LINUX") == 0)
10489 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10493 case NT_S390_GS_CB
:
10494 if (note
->namesz
== 6
10495 && strcmp (note
->namedata
, "LINUX") == 0)
10496 return elfcore_grok_s390_gs_cb (abfd
, note
);
10500 case NT_S390_GS_BC
:
10501 if (note
->namesz
== 6
10502 && strcmp (note
->namedata
, "LINUX") == 0)
10503 return elfcore_grok_s390_gs_bc (abfd
, note
);
10508 if (note
->namesz
== 6
10509 && strcmp (note
->namedata
, "LINUX") == 0)
10510 return elfcore_grok_arc_v2 (abfd
, note
);
10515 if (note
->namesz
== 6
10516 && strcmp (note
->namedata
, "LINUX") == 0)
10517 return elfcore_grok_arm_vfp (abfd
, note
);
10522 if (note
->namesz
== 6
10523 && strcmp (note
->namedata
, "LINUX") == 0)
10524 return elfcore_grok_aarch_tls (abfd
, note
);
10528 case NT_ARM_HW_BREAK
:
10529 if (note
->namesz
== 6
10530 && strcmp (note
->namedata
, "LINUX") == 0)
10531 return elfcore_grok_aarch_hw_break (abfd
, note
);
10535 case NT_ARM_HW_WATCH
:
10536 if (note
->namesz
== 6
10537 && strcmp (note
->namedata
, "LINUX") == 0)
10538 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10543 if (note
->namesz
== 6
10544 && strcmp (note
->namedata
, "LINUX") == 0)
10545 return elfcore_grok_aarch_sve (abfd
, note
);
10549 case NT_ARM_PAC_MASK
:
10550 if (note
->namesz
== 6
10551 && strcmp (note
->namedata
, "LINUX") == 0)
10552 return elfcore_grok_aarch_pauth (abfd
, note
);
10558 if (bed
->elf_backend_grok_psinfo
)
10559 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10561 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10562 return elfcore_grok_psinfo (abfd
, note
);
10568 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10571 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10575 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10582 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10584 struct bfd_build_id
* build_id
;
10586 if (note
->descsz
== 0)
10589 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10590 if (build_id
== NULL
)
10593 build_id
->size
= note
->descsz
;
10594 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10595 abfd
->build_id
= build_id
;
10601 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10603 switch (note
->type
)
10608 case NT_GNU_PROPERTY_TYPE_0
:
10609 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10611 case NT_GNU_BUILD_ID
:
10612 return elfobj_grok_gnu_build_id (abfd
, note
);
10617 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10619 struct sdt_note
*cur
=
10620 (struct sdt_note
*) bfd_alloc (abfd
,
10621 sizeof (struct sdt_note
) + note
->descsz
);
10623 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10624 cur
->size
= (bfd_size_type
) note
->descsz
;
10625 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10627 elf_tdata (abfd
)->sdt_note_head
= cur
;
10633 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10635 switch (note
->type
)
10638 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10646 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10650 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10653 if (note
->descsz
< 108)
10658 if (note
->descsz
< 120)
10666 /* Check for version 1 in pr_version. */
10667 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10672 /* Skip over pr_psinfosz. */
10673 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10677 offset
+= 4; /* Padding before pr_psinfosz. */
10681 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10682 elf_tdata (abfd
)->core
->program
10683 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10686 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10687 elf_tdata (abfd
)->core
->command
10688 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10691 /* Padding before pr_pid. */
10694 /* The pr_pid field was added in version "1a". */
10695 if (note
->descsz
< offset
+ 4)
10698 elf_tdata (abfd
)->core
->pid
10699 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10705 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10711 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10712 Also compute minimum size of this note. */
10713 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10717 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10721 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10722 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10729 if (note
->descsz
< min_size
)
10732 /* Check for version 1 in pr_version. */
10733 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10736 /* Extract size of pr_reg from pr_gregsetsz. */
10737 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10738 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10740 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10745 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10749 /* Skip over pr_osreldate. */
10752 /* Read signal from pr_cursig. */
10753 if (elf_tdata (abfd
)->core
->signal
== 0)
10754 elf_tdata (abfd
)->core
->signal
10755 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10758 /* Read TID from pr_pid. */
10759 elf_tdata (abfd
)->core
->lwpid
10760 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10763 /* Padding before pr_reg. */
10764 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10767 /* Make sure that there is enough data remaining in the note. */
10768 if ((note
->descsz
- offset
) < size
)
10771 /* Make a ".reg/999" section and a ".reg" section. */
10772 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10773 size
, note
->descpos
+ offset
);
10777 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10779 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10781 switch (note
->type
)
10784 if (bed
->elf_backend_grok_freebsd_prstatus
)
10785 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10787 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10790 return elfcore_grok_prfpreg (abfd
, note
);
10793 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10795 case NT_FREEBSD_THRMISC
:
10796 if (note
->namesz
== 8)
10797 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10801 case NT_FREEBSD_PROCSTAT_PROC
:
10802 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10805 case NT_FREEBSD_PROCSTAT_FILES
:
10806 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10809 case NT_FREEBSD_PROCSTAT_VMMAP
:
10810 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10813 case NT_FREEBSD_PROCSTAT_AUXV
:
10814 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10816 case NT_X86_XSTATE
:
10817 if (note
->namesz
== 8)
10818 return elfcore_grok_xstatereg (abfd
, note
);
10822 case NT_FREEBSD_PTLWPINFO
:
10823 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10827 return elfcore_grok_arm_vfp (abfd
, note
);
10835 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10839 cp
= strchr (note
->namedata
, '@');
10842 *lwpidp
= atoi(cp
+ 1);
10849 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10851 if (note
->descsz
<= 0x7c + 31)
10854 /* Signal number at offset 0x08. */
10855 elf_tdata (abfd
)->core
->signal
10856 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10858 /* Process ID at offset 0x50. */
10859 elf_tdata (abfd
)->core
->pid
10860 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10862 /* Command name at 0x7c (max 32 bytes, including nul). */
10863 elf_tdata (abfd
)->core
->command
10864 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10866 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10871 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10875 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10876 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10878 switch (note
->type
)
10880 case NT_NETBSDCORE_PROCINFO
:
10881 /* NetBSD-specific core "procinfo". Note that we expect to
10882 find this note before any of the others, which is fine,
10883 since the kernel writes this note out first when it
10884 creates a core file. */
10885 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10886 #ifdef NT_NETBSDCORE_AUXV
10887 case NT_NETBSDCORE_AUXV
:
10888 /* NetBSD-specific Elf Auxiliary Vector data. */
10889 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10891 #ifdef NT_NETBSDCORE_LWPSTATUS
10892 case NT_NETBSDCORE_LWPSTATUS
:
10893 return elfcore_make_note_pseudosection (abfd
,
10894 ".note.netbsdcore.lwpstatus",
10901 /* As of March 2020 there are no other machine-independent notes
10902 defined for NetBSD core files. If the note type is less
10903 than the start of the machine-dependent note types, we don't
10906 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10910 switch (bfd_get_arch (abfd
))
10912 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10913 PT_GETFPREGS == mach+2. */
10915 case bfd_arch_aarch64
:
10916 case bfd_arch_alpha
:
10917 case bfd_arch_sparc
:
10918 switch (note
->type
)
10920 case NT_NETBSDCORE_FIRSTMACH
+0:
10921 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10923 case NT_NETBSDCORE_FIRSTMACH
+2:
10924 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10930 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10931 There's also old PT___GETREGS40 == mach + 1 for old reg
10932 structure which lacks GBR. */
10935 switch (note
->type
)
10937 case NT_NETBSDCORE_FIRSTMACH
+3:
10938 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10940 case NT_NETBSDCORE_FIRSTMACH
+5:
10941 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10947 /* On all other arch's, PT_GETREGS == mach+1 and
10948 PT_GETFPREGS == mach+3. */
10951 switch (note
->type
)
10953 case NT_NETBSDCORE_FIRSTMACH
+1:
10954 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10956 case NT_NETBSDCORE_FIRSTMACH
+3:
10957 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10967 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10969 if (note
->descsz
<= 0x48 + 31)
10972 /* Signal number at offset 0x08. */
10973 elf_tdata (abfd
)->core
->signal
10974 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10976 /* Process ID at offset 0x20. */
10977 elf_tdata (abfd
)->core
->pid
10978 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10980 /* Command name at 0x48 (max 32 bytes, including nul). */
10981 elf_tdata (abfd
)->core
->command
10982 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10988 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10990 if (note
->type
== NT_OPENBSD_PROCINFO
)
10991 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10993 if (note
->type
== NT_OPENBSD_REGS
)
10994 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10996 if (note
->type
== NT_OPENBSD_FPREGS
)
10997 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10999 if (note
->type
== NT_OPENBSD_XFPREGS
)
11000 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
11002 if (note
->type
== NT_OPENBSD_AUXV
)
11003 return elfcore_make_auxv_note_section (abfd
, note
, 0);
11005 if (note
->type
== NT_OPENBSD_WCOOKIE
)
11007 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
11012 sect
->size
= note
->descsz
;
11013 sect
->filepos
= note
->descpos
;
11014 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
11023 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
11025 void *ddata
= note
->descdata
;
11032 if (note
->descsz
< 16)
11035 /* nto_procfs_status 'pid' field is at offset 0. */
11036 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
11038 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11039 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
11041 /* nto_procfs_status 'flags' field is at offset 8. */
11042 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
11044 /* nto_procfs_status 'what' field is at offset 14. */
11045 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
11047 elf_tdata (abfd
)->core
->signal
= sig
;
11048 elf_tdata (abfd
)->core
->lwpid
= *tid
;
11051 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11052 do not come from signals so we make sure we set the current
11053 thread just in case. */
11054 if (flags
& 0x00000080)
11055 elf_tdata (abfd
)->core
->lwpid
= *tid
;
11057 /* Make a ".qnx_core_status/%d" section. */
11058 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
11060 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11063 strcpy (name
, buf
);
11065 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11069 sect
->size
= note
->descsz
;
11070 sect
->filepos
= note
->descpos
;
11071 sect
->alignment_power
= 2;
11073 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
11077 elfcore_grok_nto_regs (bfd
*abfd
,
11078 Elf_Internal_Note
*note
,
11086 /* Make a "(base)/%d" section. */
11087 sprintf (buf
, "%s/%ld", base
, tid
);
11089 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11092 strcpy (name
, buf
);
11094 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11098 sect
->size
= note
->descsz
;
11099 sect
->filepos
= note
->descpos
;
11100 sect
->alignment_power
= 2;
11102 /* This is the current thread. */
11103 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
11104 return elfcore_maybe_make_sect (abfd
, base
, sect
);
11109 #define BFD_QNT_CORE_INFO 7
11110 #define BFD_QNT_CORE_STATUS 8
11111 #define BFD_QNT_CORE_GREG 9
11112 #define BFD_QNT_CORE_FPREG 10
11115 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11117 /* Every GREG section has a STATUS section before it. Store the
11118 tid from the previous call to pass down to the next gregs
11120 static long tid
= 1;
11122 switch (note
->type
)
11124 case BFD_QNT_CORE_INFO
:
11125 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
11126 case BFD_QNT_CORE_STATUS
:
11127 return elfcore_grok_nto_status (abfd
, note
, &tid
);
11128 case BFD_QNT_CORE_GREG
:
11129 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
11130 case BFD_QNT_CORE_FPREG
:
11131 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
11138 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11144 /* Use note name as section name. */
11145 len
= note
->namesz
;
11146 name
= (char *) bfd_alloc (abfd
, len
);
11149 memcpy (name
, note
->namedata
, len
);
11150 name
[len
- 1] = '\0';
11152 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11156 sect
->size
= note
->descsz
;
11157 sect
->filepos
= note
->descpos
;
11158 sect
->alignment_power
= 1;
11163 /* Function: elfcore_write_note
11166 buffer to hold note, and current size of buffer
11170 size of data for note
11172 Writes note to end of buffer. ELF64 notes are written exactly as
11173 for ELF32, despite the current (as of 2006) ELF gabi specifying
11174 that they ought to have 8-byte namesz and descsz field, and have
11175 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11178 Pointer to realloc'd buffer, *BUFSIZ updated. */
11181 elfcore_write_note (bfd
*abfd
,
11189 Elf_External_Note
*xnp
;
11196 namesz
= strlen (name
) + 1;
11198 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11200 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11203 dest
= buf
+ *bufsiz
;
11204 *bufsiz
+= newspace
;
11205 xnp
= (Elf_External_Note
*) dest
;
11206 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11207 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11208 H_PUT_32 (abfd
, type
, xnp
->type
);
11212 memcpy (dest
, name
, namesz
);
11220 memcpy (dest
, input
, size
);
11230 /* gcc-8 warns (*) on all the strncpy calls in this function about
11231 possible string truncation. The "truncation" is not a bug. We
11232 have an external representation of structs with fields that are not
11233 necessarily NULL terminated and corresponding internal
11234 representation fields that are one larger so that they can always
11235 be NULL terminated.
11236 gcc versions between 4.2 and 4.6 do not allow pragma control of
11237 diagnostics inside functions, giving a hard error if you try to use
11238 the finer control available with later versions.
11239 gcc prior to 4.2 warns about diagnostic push and pop.
11240 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11241 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11242 (*) Depending on your system header files! */
11243 #if GCC_VERSION >= 8000
11244 # pragma GCC diagnostic push
11245 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11248 elfcore_write_prpsinfo (bfd
*abfd
,
11252 const char *psargs
)
11254 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11256 if (bed
->elf_backend_write_core_note
!= NULL
)
11259 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11260 NT_PRPSINFO
, fname
, psargs
);
11265 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11266 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11267 if (bed
->s
->elfclass
== ELFCLASS32
)
11269 # if defined (HAVE_PSINFO32_T)
11271 int note_type
= NT_PSINFO
;
11274 int note_type
= NT_PRPSINFO
;
11277 memset (&data
, 0, sizeof (data
));
11278 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11279 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11280 return elfcore_write_note (abfd
, buf
, bufsiz
,
11281 "CORE", note_type
, &data
, sizeof (data
));
11286 # if defined (HAVE_PSINFO_T)
11288 int note_type
= NT_PSINFO
;
11291 int note_type
= NT_PRPSINFO
;
11294 memset (&data
, 0, sizeof (data
));
11295 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11296 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11297 return elfcore_write_note (abfd
, buf
, bufsiz
,
11298 "CORE", note_type
, &data
, sizeof (data
));
11300 #endif /* PSINFO_T or PRPSINFO_T */
11305 #if GCC_VERSION >= 8000
11306 # pragma GCC diagnostic pop
11310 elfcore_write_linux_prpsinfo32
11311 (bfd
*abfd
, char *buf
, int *bufsiz
,
11312 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11314 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11316 struct elf_external_linux_prpsinfo32_ugid16 data
;
11318 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11319 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11320 &data
, sizeof (data
));
11324 struct elf_external_linux_prpsinfo32_ugid32 data
;
11326 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11327 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11328 &data
, sizeof (data
));
11333 elfcore_write_linux_prpsinfo64
11334 (bfd
*abfd
, char *buf
, int *bufsiz
,
11335 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11337 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11339 struct elf_external_linux_prpsinfo64_ugid16 data
;
11341 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11342 return elfcore_write_note (abfd
, buf
, bufsiz
,
11343 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11347 struct elf_external_linux_prpsinfo64_ugid32 data
;
11349 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11350 return elfcore_write_note (abfd
, buf
, bufsiz
,
11351 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11356 elfcore_write_prstatus (bfd
*abfd
,
11363 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11365 if (bed
->elf_backend_write_core_note
!= NULL
)
11368 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11370 pid
, cursig
, gregs
);
11375 #if defined (HAVE_PRSTATUS_T)
11376 #if defined (HAVE_PRSTATUS32_T)
11377 if (bed
->s
->elfclass
== ELFCLASS32
)
11379 prstatus32_t prstat
;
11381 memset (&prstat
, 0, sizeof (prstat
));
11382 prstat
.pr_pid
= pid
;
11383 prstat
.pr_cursig
= cursig
;
11384 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11385 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11386 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11393 memset (&prstat
, 0, sizeof (prstat
));
11394 prstat
.pr_pid
= pid
;
11395 prstat
.pr_cursig
= cursig
;
11396 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11397 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11398 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11400 #endif /* HAVE_PRSTATUS_T */
11406 #if defined (HAVE_LWPSTATUS_T)
11408 elfcore_write_lwpstatus (bfd
*abfd
,
11415 lwpstatus_t lwpstat
;
11416 const char *note_name
= "CORE";
11418 memset (&lwpstat
, 0, sizeof (lwpstat
));
11419 lwpstat
.pr_lwpid
= pid
>> 16;
11420 lwpstat
.pr_cursig
= cursig
;
11421 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11422 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11423 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11424 #if !defined(gregs)
11425 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11426 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11428 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11429 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11432 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11433 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11435 #endif /* HAVE_LWPSTATUS_T */
11437 #if defined (HAVE_PSTATUS_T)
11439 elfcore_write_pstatus (bfd
*abfd
,
11443 int cursig ATTRIBUTE_UNUSED
,
11444 const void *gregs ATTRIBUTE_UNUSED
)
11446 const char *note_name
= "CORE";
11447 #if defined (HAVE_PSTATUS32_T)
11448 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11450 if (bed
->s
->elfclass
== ELFCLASS32
)
11454 memset (&pstat
, 0, sizeof (pstat
));
11455 pstat
.pr_pid
= pid
& 0xffff;
11456 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11457 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11465 memset (&pstat
, 0, sizeof (pstat
));
11466 pstat
.pr_pid
= pid
& 0xffff;
11467 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11468 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11472 #endif /* HAVE_PSTATUS_T */
11475 elfcore_write_prfpreg (bfd
*abfd
,
11478 const void *fpregs
,
11481 const char *note_name
= "CORE";
11482 return elfcore_write_note (abfd
, buf
, bufsiz
,
11483 note_name
, NT_FPREGSET
, fpregs
, size
);
11487 elfcore_write_prxfpreg (bfd
*abfd
,
11490 const void *xfpregs
,
11493 char *note_name
= "LINUX";
11494 return elfcore_write_note (abfd
, buf
, bufsiz
,
11495 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11499 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11500 const void *xfpregs
, int size
)
11503 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11504 note_name
= "FreeBSD";
11506 note_name
= "LINUX";
11507 return elfcore_write_note (abfd
, buf
, bufsiz
,
11508 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11512 elfcore_write_ppc_vmx (bfd
*abfd
,
11515 const void *ppc_vmx
,
11518 char *note_name
= "LINUX";
11519 return elfcore_write_note (abfd
, buf
, bufsiz
,
11520 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11524 elfcore_write_ppc_vsx (bfd
*abfd
,
11527 const void *ppc_vsx
,
11530 char *note_name
= "LINUX";
11531 return elfcore_write_note (abfd
, buf
, bufsiz
,
11532 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11536 elfcore_write_ppc_tar (bfd
*abfd
,
11539 const void *ppc_tar
,
11542 char *note_name
= "LINUX";
11543 return elfcore_write_note (abfd
, buf
, bufsiz
,
11544 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11548 elfcore_write_ppc_ppr (bfd
*abfd
,
11551 const void *ppc_ppr
,
11554 char *note_name
= "LINUX";
11555 return elfcore_write_note (abfd
, buf
, bufsiz
,
11556 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11560 elfcore_write_ppc_dscr (bfd
*abfd
,
11563 const void *ppc_dscr
,
11566 char *note_name
= "LINUX";
11567 return elfcore_write_note (abfd
, buf
, bufsiz
,
11568 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11572 elfcore_write_ppc_ebb (bfd
*abfd
,
11575 const void *ppc_ebb
,
11578 char *note_name
= "LINUX";
11579 return elfcore_write_note (abfd
, buf
, bufsiz
,
11580 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11584 elfcore_write_ppc_pmu (bfd
*abfd
,
11587 const void *ppc_pmu
,
11590 char *note_name
= "LINUX";
11591 return elfcore_write_note (abfd
, buf
, bufsiz
,
11592 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11596 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11599 const void *ppc_tm_cgpr
,
11602 char *note_name
= "LINUX";
11603 return elfcore_write_note (abfd
, buf
, bufsiz
,
11604 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11608 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11611 const void *ppc_tm_cfpr
,
11614 char *note_name
= "LINUX";
11615 return elfcore_write_note (abfd
, buf
, bufsiz
,
11616 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11620 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11623 const void *ppc_tm_cvmx
,
11626 char *note_name
= "LINUX";
11627 return elfcore_write_note (abfd
, buf
, bufsiz
,
11628 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11632 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11635 const void *ppc_tm_cvsx
,
11638 char *note_name
= "LINUX";
11639 return elfcore_write_note (abfd
, buf
, bufsiz
,
11640 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11644 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11647 const void *ppc_tm_spr
,
11650 char *note_name
= "LINUX";
11651 return elfcore_write_note (abfd
, buf
, bufsiz
,
11652 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11656 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11659 const void *ppc_tm_ctar
,
11662 char *note_name
= "LINUX";
11663 return elfcore_write_note (abfd
, buf
, bufsiz
,
11664 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11668 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11671 const void *ppc_tm_cppr
,
11674 char *note_name
= "LINUX";
11675 return elfcore_write_note (abfd
, buf
, bufsiz
,
11676 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11680 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11683 const void *ppc_tm_cdscr
,
11686 char *note_name
= "LINUX";
11687 return elfcore_write_note (abfd
, buf
, bufsiz
,
11688 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11692 elfcore_write_s390_high_gprs (bfd
*abfd
,
11695 const void *s390_high_gprs
,
11698 char *note_name
= "LINUX";
11699 return elfcore_write_note (abfd
, buf
, bufsiz
,
11700 note_name
, NT_S390_HIGH_GPRS
,
11701 s390_high_gprs
, size
);
11705 elfcore_write_s390_timer (bfd
*abfd
,
11708 const void *s390_timer
,
11711 char *note_name
= "LINUX";
11712 return elfcore_write_note (abfd
, buf
, bufsiz
,
11713 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11717 elfcore_write_s390_todcmp (bfd
*abfd
,
11720 const void *s390_todcmp
,
11723 char *note_name
= "LINUX";
11724 return elfcore_write_note (abfd
, buf
, bufsiz
,
11725 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11729 elfcore_write_s390_todpreg (bfd
*abfd
,
11732 const void *s390_todpreg
,
11735 char *note_name
= "LINUX";
11736 return elfcore_write_note (abfd
, buf
, bufsiz
,
11737 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11741 elfcore_write_s390_ctrs (bfd
*abfd
,
11744 const void *s390_ctrs
,
11747 char *note_name
= "LINUX";
11748 return elfcore_write_note (abfd
, buf
, bufsiz
,
11749 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11753 elfcore_write_s390_prefix (bfd
*abfd
,
11756 const void *s390_prefix
,
11759 char *note_name
= "LINUX";
11760 return elfcore_write_note (abfd
, buf
, bufsiz
,
11761 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11765 elfcore_write_s390_last_break (bfd
*abfd
,
11768 const void *s390_last_break
,
11771 char *note_name
= "LINUX";
11772 return elfcore_write_note (abfd
, buf
, bufsiz
,
11773 note_name
, NT_S390_LAST_BREAK
,
11774 s390_last_break
, size
);
11778 elfcore_write_s390_system_call (bfd
*abfd
,
11781 const void *s390_system_call
,
11784 char *note_name
= "LINUX";
11785 return elfcore_write_note (abfd
, buf
, bufsiz
,
11786 note_name
, NT_S390_SYSTEM_CALL
,
11787 s390_system_call
, size
);
11791 elfcore_write_s390_tdb (bfd
*abfd
,
11794 const void *s390_tdb
,
11797 char *note_name
= "LINUX";
11798 return elfcore_write_note (abfd
, buf
, bufsiz
,
11799 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11803 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11806 const void *s390_vxrs_low
,
11809 char *note_name
= "LINUX";
11810 return elfcore_write_note (abfd
, buf
, bufsiz
,
11811 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11815 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11818 const void *s390_vxrs_high
,
11821 char *note_name
= "LINUX";
11822 return elfcore_write_note (abfd
, buf
, bufsiz
,
11823 note_name
, NT_S390_VXRS_HIGH
,
11824 s390_vxrs_high
, size
);
11828 elfcore_write_s390_gs_cb (bfd
*abfd
,
11831 const void *s390_gs_cb
,
11834 char *note_name
= "LINUX";
11835 return elfcore_write_note (abfd
, buf
, bufsiz
,
11836 note_name
, NT_S390_GS_CB
,
11841 elfcore_write_s390_gs_bc (bfd
*abfd
,
11844 const void *s390_gs_bc
,
11847 char *note_name
= "LINUX";
11848 return elfcore_write_note (abfd
, buf
, bufsiz
,
11849 note_name
, NT_S390_GS_BC
,
11854 elfcore_write_arm_vfp (bfd
*abfd
,
11857 const void *arm_vfp
,
11860 char *note_name
= "LINUX";
11861 return elfcore_write_note (abfd
, buf
, bufsiz
,
11862 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11866 elfcore_write_aarch_tls (bfd
*abfd
,
11869 const void *aarch_tls
,
11872 char *note_name
= "LINUX";
11873 return elfcore_write_note (abfd
, buf
, bufsiz
,
11874 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11878 elfcore_write_aarch_hw_break (bfd
*abfd
,
11881 const void *aarch_hw_break
,
11884 char *note_name
= "LINUX";
11885 return elfcore_write_note (abfd
, buf
, bufsiz
,
11886 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11890 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11893 const void *aarch_hw_watch
,
11896 char *note_name
= "LINUX";
11897 return elfcore_write_note (abfd
, buf
, bufsiz
,
11898 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11902 elfcore_write_aarch_sve (bfd
*abfd
,
11905 const void *aarch_sve
,
11908 char *note_name
= "LINUX";
11909 return elfcore_write_note (abfd
, buf
, bufsiz
,
11910 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11914 elfcore_write_aarch_pauth (bfd
*abfd
,
11917 const void *aarch_pauth
,
11920 char *note_name
= "LINUX";
11921 return elfcore_write_note (abfd
, buf
, bufsiz
,
11922 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11926 elfcore_write_arc_v2 (bfd
*abfd
,
11929 const void *arc_v2
,
11932 char *note_name
= "LINUX";
11933 return elfcore_write_note (abfd
, buf
, bufsiz
,
11934 note_name
, NT_ARC_V2
, arc_v2
, size
);
11938 elfcore_write_register_note (bfd
*abfd
,
11941 const char *section
,
11945 if (strcmp (section
, ".reg2") == 0)
11946 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11947 if (strcmp (section
, ".reg-xfp") == 0)
11948 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11949 if (strcmp (section
, ".reg-xstate") == 0)
11950 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11951 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11952 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11953 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11954 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11955 if (strcmp (section
, ".reg-ppc-tar") == 0)
11956 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11957 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11958 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11959 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11960 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11961 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11962 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11963 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11964 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11965 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11966 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11967 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11968 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11969 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11970 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11971 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11972 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11973 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11974 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11975 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11976 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11977 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11978 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11979 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11980 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11981 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11982 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11983 if (strcmp (section
, ".reg-s390-timer") == 0)
11984 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11985 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11986 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11987 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11988 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11989 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11990 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11991 if (strcmp (section
, ".reg-s390-prefix") == 0)
11992 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11993 if (strcmp (section
, ".reg-s390-last-break") == 0)
11994 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11995 if (strcmp (section
, ".reg-s390-system-call") == 0)
11996 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11997 if (strcmp (section
, ".reg-s390-tdb") == 0)
11998 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11999 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
12000 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
12001 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
12002 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
12003 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
12004 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
12005 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
12006 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
12007 if (strcmp (section
, ".reg-arm-vfp") == 0)
12008 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
12009 if (strcmp (section
, ".reg-aarch-tls") == 0)
12010 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
12011 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
12012 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
12013 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
12014 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
12015 if (strcmp (section
, ".reg-aarch-sve") == 0)
12016 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
12017 if (strcmp (section
, ".reg-aarch-pauth") == 0)
12018 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
12019 if (strcmp (section
, ".reg-arc-v2") == 0)
12020 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
12025 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
12030 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12031 gABI specifies that PT_NOTE alignment should be aligned to 4
12032 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12033 align is less than 4, we use 4 byte alignment. */
12036 if (align
!= 4 && align
!= 8)
12040 while (p
< buf
+ size
)
12042 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
12043 Elf_Internal_Note in
;
12045 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
12048 in
.type
= H_GET_32 (abfd
, xnp
->type
);
12050 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
12051 in
.namedata
= xnp
->name
;
12052 if (in
.namesz
> buf
- in
.namedata
+ size
)
12055 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
12056 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
12057 in
.descpos
= offset
+ (in
.descdata
- buf
);
12059 && (in
.descdata
>= buf
+ size
12060 || in
.descsz
> buf
- in
.descdata
+ size
))
12063 switch (bfd_get_format (abfd
))
12070 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12073 const char * string
;
12075 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
12079 GROKER_ELEMENT ("", elfcore_grok_note
),
12080 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
12081 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
12082 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
12083 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
12084 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
12085 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
)
12087 #undef GROKER_ELEMENT
12090 for (i
= ARRAY_SIZE (grokers
); i
--;)
12092 if (in
.namesz
>= grokers
[i
].len
12093 && strncmp (in
.namedata
, grokers
[i
].string
,
12094 grokers
[i
].len
) == 0)
12096 if (! grokers
[i
].func (abfd
, & in
))
12105 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
12107 if (! elfobj_grok_gnu_note (abfd
, &in
))
12110 else if (in
.namesz
== sizeof "stapsdt"
12111 && strcmp (in
.namedata
, "stapsdt") == 0)
12113 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
12119 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
12126 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
12131 if (size
== 0 || (size
+ 1) == 0)
12134 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
12137 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
12141 /* PR 17512: file: ec08f814
12142 0-termintate the buffer so that string searches will not overflow. */
12145 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
12155 /* Providing external access to the ELF program header table. */
12157 /* Return an upper bound on the number of bytes required to store a
12158 copy of ABFD's program header table entries. Return -1 if an error
12159 occurs; bfd_get_error will return an appropriate code. */
12162 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12164 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12166 bfd_set_error (bfd_error_wrong_format
);
12170 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12173 /* Copy ABFD's program header table entries to *PHDRS. The entries
12174 will be stored as an array of Elf_Internal_Phdr structures, as
12175 defined in include/elf/internal.h. To find out how large the
12176 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12178 Return the number of program header table entries read, or -1 if an
12179 error occurs; bfd_get_error will return an appropriate code. */
12182 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12186 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12188 bfd_set_error (bfd_error_wrong_format
);
12192 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12193 if (num_phdrs
!= 0)
12194 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12195 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12200 enum elf_reloc_type_class
12201 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12202 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12203 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12205 return reloc_class_normal
;
12208 /* For RELA architectures, return the relocation value for a
12209 relocation against a local symbol. */
12212 _bfd_elf_rela_local_sym (bfd
*abfd
,
12213 Elf_Internal_Sym
*sym
,
12215 Elf_Internal_Rela
*rel
)
12217 asection
*sec
= *psec
;
12218 bfd_vma relocation
;
12220 relocation
= (sec
->output_section
->vma
12221 + sec
->output_offset
12223 if ((sec
->flags
& SEC_MERGE
)
12224 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12225 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12228 _bfd_merged_section_offset (abfd
, psec
,
12229 elf_section_data (sec
)->sec_info
,
12230 sym
->st_value
+ rel
->r_addend
);
12233 /* If we have changed the section, and our original section is
12234 marked with SEC_EXCLUDE, it means that the original
12235 SEC_MERGE section has been completely subsumed in some
12236 other SEC_MERGE section. In this case, we need to leave
12237 some info around for --emit-relocs. */
12238 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12239 sec
->kept_section
= *psec
;
12242 rel
->r_addend
-= relocation
;
12243 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12249 _bfd_elf_rel_local_sym (bfd
*abfd
,
12250 Elf_Internal_Sym
*sym
,
12254 asection
*sec
= *psec
;
12256 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12257 return sym
->st_value
+ addend
;
12259 return _bfd_merged_section_offset (abfd
, psec
,
12260 elf_section_data (sec
)->sec_info
,
12261 sym
->st_value
+ addend
);
12264 /* Adjust an address within a section. Given OFFSET within SEC, return
12265 the new offset within the section, based upon changes made to the
12266 section. Returns -1 if the offset is now invalid.
12267 The offset (in abnd out) is in target sized bytes, however big a
12271 _bfd_elf_section_offset (bfd
*abfd
,
12272 struct bfd_link_info
*info
,
12276 switch (sec
->sec_info_type
)
12278 case SEC_INFO_TYPE_STABS
:
12279 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12281 case SEC_INFO_TYPE_EH_FRAME
:
12282 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12285 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12287 /* Reverse the offset. */
12288 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12289 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12291 /* address_size and sec->size are in octets. Convert
12292 to bytes before subtracting the original offset. */
12293 offset
= ((sec
->size
- address_size
)
12294 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12300 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12301 reconstruct an ELF file by reading the segments out of remote memory
12302 based on the ELF file header at EHDR_VMA and the ELF program headers it
12303 points to. If not null, *LOADBASEP is filled in with the difference
12304 between the VMAs from which the segments were read, and the VMAs the
12305 file headers (and hence BFD's idea of each section's VMA) put them at.
12307 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12308 remote memory at target address VMA into the local buffer at MYADDR; it
12309 should return zero on success or an `errno' code on failure. TEMPL must
12310 be a BFD for an ELF target with the word size and byte order found in
12311 the remote memory. */
12314 bfd_elf_bfd_from_remote_memory
12317 bfd_size_type size
,
12318 bfd_vma
*loadbasep
,
12319 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12321 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12322 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12326 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12327 long symcount ATTRIBUTE_UNUSED
,
12328 asymbol
**syms ATTRIBUTE_UNUSED
,
12333 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12336 const char *relplt_name
;
12337 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12341 Elf_Internal_Shdr
*hdr
;
12347 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12350 if (dynsymcount
<= 0)
12353 if (!bed
->plt_sym_val
)
12356 relplt_name
= bed
->relplt_name
;
12357 if (relplt_name
== NULL
)
12358 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12359 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12360 if (relplt
== NULL
)
12363 hdr
= &elf_section_data (relplt
)->this_hdr
;
12364 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12365 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12368 plt
= bfd_get_section_by_name (abfd
, ".plt");
12372 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12373 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12376 count
= relplt
->size
/ hdr
->sh_entsize
;
12377 size
= count
* sizeof (asymbol
);
12378 p
= relplt
->relocation
;
12379 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12381 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12382 if (p
->addend
!= 0)
12385 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12387 size
+= sizeof ("+0x") - 1 + 8;
12392 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12396 names
= (char *) (s
+ count
);
12397 p
= relplt
->relocation
;
12399 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12404 addr
= bed
->plt_sym_val (i
, plt
, p
);
12405 if (addr
== (bfd_vma
) -1)
12408 *s
= **p
->sym_ptr_ptr
;
12409 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12410 we are defining a symbol, ensure one of them is set. */
12411 if ((s
->flags
& BSF_LOCAL
) == 0)
12412 s
->flags
|= BSF_GLOBAL
;
12413 s
->flags
|= BSF_SYNTHETIC
;
12415 s
->value
= addr
- plt
->vma
;
12418 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12419 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12421 if (p
->addend
!= 0)
12425 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12426 names
+= sizeof ("+0x") - 1;
12427 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12428 for (a
= buf
; *a
== '0'; ++a
)
12431 memcpy (names
, a
, len
);
12434 memcpy (names
, "@plt", sizeof ("@plt"));
12435 names
+= sizeof ("@plt");
12442 /* It is only used by x86-64 so far.
12443 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12444 but current usage would allow all of _bfd_std_section to be zero. */
12445 static const asymbol lcomm_sym
12446 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12447 asection _bfd_elf_large_com_section
12448 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12449 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12452 _bfd_elf_final_write_processing (bfd
*abfd
)
12454 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12456 i_ehdrp
= elf_elfheader (abfd
);
12458 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12459 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12461 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12462 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
12463 or STB_GNU_UNIQUE binding. */
12464 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12466 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12467 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12468 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12469 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12471 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12472 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
12473 "and FreeBSD targets"));
12474 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12475 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
12476 "only by GNU and FreeBSD targets"));
12477 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12478 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
12479 "only by GNU and FreeBSD targets"));
12480 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
12481 _bfd_error_handler (_("GNU_RETAIN section is supported "
12482 "only by GNU and FreeBSD targets"));
12483 bfd_set_error (bfd_error_sorry
);
12491 /* Return TRUE for ELF symbol types that represent functions.
12492 This is the default version of this function, which is sufficient for
12493 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12496 _bfd_elf_is_function_type (unsigned int type
)
12498 return (type
== STT_FUNC
12499 || type
== STT_GNU_IFUNC
);
12502 /* If the ELF symbol SYM might be a function in SEC, return the
12503 function size and set *CODE_OFF to the function's entry point,
12504 otherwise return zero. */
12507 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12510 bfd_size_type size
;
12512 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12513 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12514 || sym
->section
!= sec
)
12517 *code_off
= sym
->value
;
12519 if (!(sym
->flags
& BSF_SYNTHETIC
))
12520 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
12526 /* Set to non-zero to enable some debug messages. */
12527 #define DEBUG_SECONDARY_RELOCS 0
12529 /* An internal-to-the-bfd-library only section type
12530 used to indicate a cached secondary reloc section. */
12531 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12533 /* Create a BFD section to hold a secondary reloc section. */
12536 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
12537 Elf_Internal_Shdr
*hdr
,
12539 unsigned int shindex
)
12541 /* We only support RELA secondary relocs. */
12542 if (hdr
->sh_type
!= SHT_RELA
)
12545 #if DEBUG_SECONDARY_RELOCS
12546 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
12548 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
12549 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
12552 /* Read in any secondary relocs associated with SEC. */
12555 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
12557 asymbol
** symbols
)
12559 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12561 bfd_boolean result
= TRUE
;
12562 bfd_vma (*r_sym
) (bfd_vma
);
12564 #if BFD_DEFAULT_TARGET_SIZE > 32
12565 if (bfd_arch_bits_per_address (abfd
) != 32)
12566 r_sym
= elf64_r_sym
;
12569 r_sym
= elf32_r_sym
;
12571 /* Discover if there are any secondary reloc sections
12572 associated with SEC. */
12573 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12575 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
12577 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
12578 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
12579 && (hdr
->sh_entsize
== ebd
->s
->sizeof_rel
12580 || hdr
->sh_entsize
== ebd
->s
->sizeof_rela
))
12582 bfd_byte
* native_relocs
;
12583 bfd_byte
* native_reloc
;
12584 arelent
* internal_relocs
;
12585 arelent
* internal_reloc
;
12587 unsigned int entsize
;
12588 unsigned int symcount
;
12589 unsigned int reloc_count
;
12592 if (ebd
->elf_info_to_howto
== NULL
)
12595 #if DEBUG_SECONDARY_RELOCS
12596 fprintf (stderr
, "read secondary relocs for %s from %s\n",
12597 sec
->name
, relsec
->name
);
12599 entsize
= hdr
->sh_entsize
;
12601 native_relocs
= bfd_malloc (hdr
->sh_size
);
12602 if (native_relocs
== NULL
)
12608 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
12609 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
12611 free (native_relocs
);
12612 bfd_set_error (bfd_error_file_too_big
);
12617 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
12618 if (internal_relocs
== NULL
)
12620 free (native_relocs
);
12625 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
12626 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
12629 free (native_relocs
);
12630 /* The internal_relocs will be freed when
12631 the memory for the bfd is released. */
12636 symcount
= bfd_get_symcount (abfd
);
12638 for (i
= 0, internal_reloc
= internal_relocs
,
12639 native_reloc
= native_relocs
;
12641 i
++, internal_reloc
++, native_reloc
+= entsize
)
12644 Elf_Internal_Rela rela
;
12646 if (entsize
== ebd
->s
->sizeof_rel
)
12647 ebd
->s
->swap_reloc_in (abfd
, native_reloc
, & rela
);
12648 else /* entsize == ebd->s->sizeof_rela */
12649 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
12651 /* The address of an ELF reloc is section relative for an object
12652 file, and absolute for an executable file or shared library.
12653 The address of a normal BFD reloc is always section relative,
12654 and the address of a dynamic reloc is absolute.. */
12655 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
12656 internal_reloc
->address
= rela
.r_offset
;
12658 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
12660 if (r_sym (rela
.r_info
) == STN_UNDEF
)
12662 /* FIXME: This and the error case below mean that we
12663 have a symbol on relocs that is not elf_symbol_type. */
12664 internal_reloc
->sym_ptr_ptr
=
12665 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12667 else if (r_sym (rela
.r_info
) > symcount
)
12670 /* xgettext:c-format */
12671 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12672 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
12673 bfd_set_error (bfd_error_bad_value
);
12674 internal_reloc
->sym_ptr_ptr
=
12675 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12682 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
12684 internal_reloc
->sym_ptr_ptr
= ps
;
12685 /* Make sure that this symbol is not removed by strip. */
12686 (*ps
)->flags
|= BSF_KEEP
;
12689 internal_reloc
->addend
= rela
.r_addend
;
12691 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
12692 if (! res
|| internal_reloc
->howto
== NULL
)
12694 #if DEBUG_SECONDARY_RELOCS
12695 fprintf (stderr
, "there is no howto associated with reloc %lx\n",
12702 free (native_relocs
);
12703 /* Store the internal relocs. */
12704 elf_section_data (relsec
)->sec_info
= internal_relocs
;
12711 /* Set the ELF section header fields of an output secondary reloc section. */
12714 _bfd_elf_copy_special_section_fields (const bfd
* ibfd ATTRIBUTE_UNUSED
,
12715 bfd
* obfd ATTRIBUTE_UNUSED
,
12716 const Elf_Internal_Shdr
* isection
,
12717 Elf_Internal_Shdr
* osection
)
12721 struct bfd_elf_section_data
* esd
;
12723 if (isection
== NULL
)
12726 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
12729 isec
= isection
->bfd_section
;
12733 osec
= osection
->bfd_section
;
12737 esd
= elf_section_data (osec
);
12738 BFD_ASSERT (esd
->sec_info
== NULL
);
12739 esd
->sec_info
= elf_section_data (isec
)->sec_info
;
12740 osection
->sh_type
= SHT_RELA
;
12741 osection
->sh_link
= elf_onesymtab (obfd
);
12742 if (osection
->sh_link
== 0)
12744 /* There is no symbol table - we are hosed... */
12746 /* xgettext:c-format */
12747 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12749 bfd_set_error (bfd_error_bad_value
);
12753 /* Find the output section that corresponds to the isection's sh_info link. */
12754 if (isection
->sh_info
== 0
12755 || isection
->sh_info
>= elf_numsections (ibfd
))
12758 /* xgettext:c-format */
12759 (_("%pB(%pA): info section index is invalid"),
12761 bfd_set_error (bfd_error_bad_value
);
12765 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
12767 if (isection
== NULL
12768 || isection
->bfd_section
== NULL
12769 || isection
->bfd_section
->output_section
== NULL
)
12772 /* xgettext:c-format */
12773 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12775 bfd_set_error (bfd_error_bad_value
);
12779 esd
= elf_section_data (isection
->bfd_section
->output_section
);
12780 BFD_ASSERT (esd
!= NULL
);
12781 osection
->sh_info
= esd
->this_idx
;
12782 esd
->has_secondary_relocs
= TRUE
;
12783 #if DEBUG_SECONDARY_RELOCS
12784 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
12785 osec
->name
, osection
->sh_link
, osection
->sh_info
);
12786 fprintf (stderr
, "mark section %s as having secondary relocs\n",
12787 bfd_section_name (isection
->bfd_section
->output_section
));
12793 /* Write out a secondary reloc section.
12795 FIXME: Currently this function can result in a serious performance penalty
12796 for files with secondary relocs and lots of sections. The proper way to
12797 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
12798 relocs together and then to have this function just walk that chain. */
12801 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
12803 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12804 bfd_vma addr_offset
;
12806 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
12807 bfd_boolean result
= TRUE
;
12812 #if BFD_DEFAULT_TARGET_SIZE > 32
12813 if (bfd_arch_bits_per_address (abfd
) != 32)
12814 r_info
= elf64_r_info
;
12817 r_info
= elf32_r_info
;
12819 /* The address of an ELF reloc is section relative for an object
12820 file, and absolute for an executable file or shared library.
12821 The address of a BFD reloc is always section relative. */
12823 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
12824 addr_offset
= sec
->vma
;
12826 /* Discover if there are any secondary reloc sections
12827 associated with SEC. */
12828 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12830 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
12831 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
12833 if (hdr
->sh_type
== SHT_RELA
12834 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12836 asymbol
* last_sym
;
12838 unsigned int reloc_count
;
12840 unsigned int entsize
;
12841 arelent
* src_irel
;
12842 bfd_byte
* dst_rela
;
12844 if (hdr
->contents
!= NULL
)
12847 /* xgettext:c-format */
12848 (_("%pB(%pA): error: secondary reloc section processed twice"),
12850 bfd_set_error (bfd_error_bad_value
);
12855 entsize
= hdr
->sh_entsize
;
12859 /* xgettext:c-format */
12860 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
12862 bfd_set_error (bfd_error_bad_value
);
12866 else if (entsize
!= ebd
->s
->sizeof_rel
12867 && entsize
!= ebd
->s
->sizeof_rela
)
12870 /* xgettext:c-format */
12871 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
12873 bfd_set_error (bfd_error_bad_value
);
12878 reloc_count
= hdr
->sh_size
/ entsize
;
12879 if (reloc_count
<= 0)
12882 /* xgettext:c-format */
12883 (_("%pB(%pA): error: secondary reloc section is empty!"),
12885 bfd_set_error (bfd_error_bad_value
);
12890 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
12891 if (hdr
->contents
== NULL
)
12894 #if DEBUG_SECONDARY_RELOCS
12895 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
12896 reloc_count
, sec
->name
, relsec
->name
);
12900 dst_rela
= hdr
->contents
;
12901 src_irel
= (arelent
*) esd
->sec_info
;
12902 if (src_irel
== NULL
)
12905 /* xgettext:c-format */
12906 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12908 bfd_set_error (bfd_error_bad_value
);
12913 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= entsize
)
12915 Elf_Internal_Rela src_rela
;
12920 ptr
= src_irel
+ idx
;
12924 /* xgettext:c-format */
12925 (_("%pB(%pA): error: reloc table entry %u is empty"),
12926 abfd
, relsec
, idx
);
12927 bfd_set_error (bfd_error_bad_value
);
12932 if (ptr
->sym_ptr_ptr
== NULL
)
12934 /* FIXME: Is this an error ? */
12939 sym
= *ptr
->sym_ptr_ptr
;
12941 if (sym
== last_sym
)
12945 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
12949 /* xgettext:c-format */
12950 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12951 abfd
, relsec
, idx
);
12952 bfd_set_error (bfd_error_bad_value
);
12961 if (sym
->the_bfd
!= NULL
12962 && sym
->the_bfd
->xvec
!= abfd
->xvec
12963 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
12966 /* xgettext:c-format */
12967 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12968 abfd
, relsec
, idx
);
12969 bfd_set_error (bfd_error_bad_value
);
12975 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
12976 if (ptr
->howto
== NULL
)
12979 /* xgettext:c-format */
12980 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12981 abfd
, relsec
, idx
);
12982 bfd_set_error (bfd_error_bad_value
);
12984 src_rela
.r_info
= r_info (0, 0);
12987 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
12988 src_rela
.r_addend
= ptr
->addend
;
12990 if (entsize
== ebd
->s
->sizeof_rel
)
12991 ebd
->s
->swap_reloc_out (abfd
, &src_rela
, dst_rela
);
12992 else /* entsize == ebd->s->sizeof_rela */
12993 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);