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
);
516 if (alloc_intsym
!= NULL
)
523 if (alloc_ext
!= NULL
)
525 if (alloc_extshndx
!= NULL
)
526 free (alloc_extshndx
);
531 /* Look up a symbol name. */
533 bfd_elf_sym_name (bfd
*abfd
,
534 Elf_Internal_Shdr
*symtab_hdr
,
535 Elf_Internal_Sym
*isym
,
539 unsigned int iname
= isym
->st_name
;
540 unsigned int shindex
= symtab_hdr
->sh_link
;
542 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
543 /* Check for a bogus st_shndx to avoid crashing. */
544 && isym
->st_shndx
< elf_numsections (abfd
))
546 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
547 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
550 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
553 else if (sym_sec
&& *name
== '\0')
554 name
= bfd_section_name (sym_sec
);
559 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
560 sections. The first element is the flags, the rest are section
563 typedef union elf_internal_group
{
564 Elf_Internal_Shdr
*shdr
;
566 } Elf_Internal_Group
;
568 /* Return the name of the group signature symbol. Why isn't the
569 signature just a string? */
572 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
574 Elf_Internal_Shdr
*hdr
;
575 unsigned char esym
[sizeof (Elf64_External_Sym
)];
576 Elf_External_Sym_Shndx eshndx
;
577 Elf_Internal_Sym isym
;
579 /* First we need to ensure the symbol table is available. Make sure
580 that it is a symbol table section. */
581 if (ghdr
->sh_link
>= elf_numsections (abfd
))
583 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
584 if (hdr
->sh_type
!= SHT_SYMTAB
585 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
588 /* Go read the symbol. */
589 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
590 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
591 &isym
, esym
, &eshndx
) == NULL
)
594 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
597 /* Set next_in_group list pointer, and group name for NEWSECT. */
600 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
602 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
604 /* If num_group is zero, read in all SHT_GROUP sections. The count
605 is set to -1 if there are no SHT_GROUP sections. */
608 unsigned int i
, shnum
;
610 /* First count the number of groups. If we have a SHT_GROUP
611 section with just a flag word (ie. sh_size is 4), ignore it. */
612 shnum
= elf_numsections (abfd
);
615 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
616 ( (shdr)->sh_type == SHT_GROUP \
617 && (shdr)->sh_size >= minsize \
618 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
619 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
621 for (i
= 0; i
< shnum
; i
++)
623 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
625 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
631 num_group
= (unsigned) -1;
632 elf_tdata (abfd
)->num_group
= num_group
;
633 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
637 /* We keep a list of elf section headers for group sections,
638 so we can find them quickly. */
641 elf_tdata (abfd
)->num_group
= num_group
;
642 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
643 elf_tdata (abfd
)->group_sect_ptr
644 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
645 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
649 for (i
= 0; i
< shnum
; i
++)
651 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
653 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
656 Elf_Internal_Group
*dest
;
658 /* Make sure the group section has a BFD section
660 if (!bfd_section_from_shdr (abfd
, i
))
663 /* Add to list of sections. */
664 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
667 /* Read the raw contents. */
668 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
669 shdr
->contents
= NULL
;
670 if (_bfd_mul_overflow (shdr
->sh_size
,
671 sizeof (*dest
) / 4, &amt
)
672 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
674 = _bfd_alloc_and_read (abfd
, amt
, shdr
->sh_size
)))
677 /* xgettext:c-format */
678 (_("%pB: invalid size field in group section"
679 " header: %#" PRIx64
""),
680 abfd
, (uint64_t) shdr
->sh_size
);
681 bfd_set_error (bfd_error_bad_value
);
686 /* Translate raw contents, a flag word followed by an
687 array of elf section indices all in target byte order,
688 to the flag word followed by an array of elf section
690 src
= shdr
->contents
+ shdr
->sh_size
;
691 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
699 idx
= H_GET_32 (abfd
, src
);
700 if (src
== shdr
->contents
)
704 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
705 shdr
->bfd_section
->flags
706 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
711 dest
->shdr
= elf_elfsections (abfd
)[idx
];
712 /* PR binutils/23199: All sections in a
713 section group should be marked with
714 SHF_GROUP. But some tools generate
715 broken objects without SHF_GROUP. Fix
717 dest
->shdr
->sh_flags
|= SHF_GROUP
;
720 || dest
->shdr
->sh_type
== SHT_GROUP
)
723 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
731 /* PR 17510: Corrupt binaries might contain invalid groups. */
732 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
734 elf_tdata (abfd
)->num_group
= num_group
;
736 /* If all groups are invalid then fail. */
739 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
740 elf_tdata (abfd
)->num_group
= num_group
= -1;
742 (_("%pB: no valid group sections found"), abfd
);
743 bfd_set_error (bfd_error_bad_value
);
749 if (num_group
!= (unsigned) -1)
751 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
754 for (j
= 0; j
< num_group
; j
++)
756 /* Begin search from previous found group. */
757 unsigned i
= (j
+ search_offset
) % num_group
;
759 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
760 Elf_Internal_Group
*idx
;
766 idx
= (Elf_Internal_Group
*) shdr
->contents
;
767 if (idx
== NULL
|| shdr
->sh_size
< 4)
769 /* See PR 21957 for a reproducer. */
770 /* xgettext:c-format */
771 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
772 abfd
, shdr
->bfd_section
);
773 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
774 bfd_set_error (bfd_error_bad_value
);
777 n_elt
= shdr
->sh_size
/ 4;
779 /* Look through this group's sections to see if current
780 section is a member. */
782 if ((++idx
)->shdr
== hdr
)
786 /* We are a member of this group. Go looking through
787 other members to see if any others are linked via
789 idx
= (Elf_Internal_Group
*) shdr
->contents
;
790 n_elt
= shdr
->sh_size
/ 4;
792 if ((++idx
)->shdr
!= NULL
793 && (s
= idx
->shdr
->bfd_section
) != NULL
794 && elf_next_in_group (s
) != NULL
)
798 /* Snarf the group name from other member, and
799 insert current section in circular list. */
800 elf_group_name (newsect
) = elf_group_name (s
);
801 elf_next_in_group (newsect
) = elf_next_in_group (s
);
802 elf_next_in_group (s
) = newsect
;
808 gname
= group_signature (abfd
, shdr
);
811 elf_group_name (newsect
) = gname
;
813 /* Start a circular list with one element. */
814 elf_next_in_group (newsect
) = newsect
;
817 /* If the group section has been created, point to the
819 if (shdr
->bfd_section
!= NULL
)
820 elf_next_in_group (shdr
->bfd_section
) = newsect
;
822 elf_tdata (abfd
)->group_search_offset
= i
;
829 if (elf_group_name (newsect
) == NULL
)
831 /* xgettext:c-format */
832 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
840 _bfd_elf_setup_sections (bfd
*abfd
)
843 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
844 bfd_boolean result
= TRUE
;
847 /* Process SHF_LINK_ORDER. */
848 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
850 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
851 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
853 unsigned int elfsec
= this_hdr
->sh_link
;
854 /* FIXME: The old Intel compiler and old strip/objcopy may
855 not set the sh_link or sh_info fields. Hence we could
856 get the situation where elfsec is 0. */
859 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
860 bed
->link_order_error_handler
861 /* xgettext:c-format */
862 (_("%pB: warning: sh_link not set for section `%pA'"),
867 asection
*linksec
= NULL
;
869 if (elfsec
< elf_numsections (abfd
))
871 this_hdr
= elf_elfsections (abfd
)[elfsec
];
872 linksec
= this_hdr
->bfd_section
;
876 Some strip/objcopy may leave an incorrect value in
877 sh_link. We don't want to proceed. */
881 /* xgettext:c-format */
882 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
883 s
->owner
, elfsec
, s
);
887 elf_linked_to_section (s
) = linksec
;
890 else if (this_hdr
->sh_type
== SHT_GROUP
891 && elf_next_in_group (s
) == NULL
)
894 /* xgettext:c-format */
895 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
896 abfd
, elf_section_data (s
)->this_idx
);
901 /* Process section groups. */
902 if (num_group
== (unsigned) -1)
905 for (i
= 0; i
< num_group
; i
++)
907 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
908 Elf_Internal_Group
*idx
;
911 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
912 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
915 /* xgettext:c-format */
916 (_("%pB: section group entry number %u is corrupt"),
922 idx
= (Elf_Internal_Group
*) shdr
->contents
;
923 n_elt
= shdr
->sh_size
/ 4;
929 if (idx
->shdr
== NULL
)
931 else if (idx
->shdr
->bfd_section
)
932 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
933 else if (idx
->shdr
->sh_type
!= SHT_RELA
934 && idx
->shdr
->sh_type
!= SHT_REL
)
936 /* There are some unknown sections in the group. */
938 /* xgettext:c-format */
939 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
942 bfd_elf_string_from_elf_section (abfd
,
943 (elf_elfheader (abfd
)
956 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
958 return elf_next_in_group (sec
) != NULL
;
962 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
964 if (elf_sec_group (sec
) != NULL
)
965 return elf_group_name (sec
);
970 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
972 unsigned int len
= strlen (name
);
973 char *new_name
= bfd_alloc (abfd
, len
+ 2);
974 if (new_name
== NULL
)
978 memcpy (new_name
+ 2, name
+ 1, len
);
983 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
985 unsigned int len
= strlen (name
);
986 char *new_name
= bfd_alloc (abfd
, len
);
987 if (new_name
== NULL
)
990 memcpy (new_name
+ 1, name
+ 2, len
- 1);
994 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
998 int16_t major_version
;
999 int16_t minor_version
;
1000 unsigned char slim_object
;
1002 /* Flags is a private field that is not defined publicly. */
1006 /* Make a BFD section from an ELF section. We store a pointer to the
1007 BFD section in the bfd_section field of the header. */
1010 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1011 Elf_Internal_Shdr
*hdr
,
1017 const struct elf_backend_data
*bed
;
1018 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
1020 if (hdr
->bfd_section
!= NULL
)
1023 newsect
= bfd_make_section_anyway (abfd
, name
);
1024 if (newsect
== NULL
)
1027 hdr
->bfd_section
= newsect
;
1028 elf_section_data (newsect
)->this_hdr
= *hdr
;
1029 elf_section_data (newsect
)->this_idx
= shindex
;
1031 /* Always use the real type/flags. */
1032 elf_section_type (newsect
) = hdr
->sh_type
;
1033 elf_section_flags (newsect
) = hdr
->sh_flags
;
1035 newsect
->filepos
= hdr
->sh_offset
;
1037 flags
= SEC_NO_FLAGS
;
1038 if (hdr
->sh_type
!= SHT_NOBITS
)
1039 flags
|= SEC_HAS_CONTENTS
;
1040 if (hdr
->sh_type
== SHT_GROUP
)
1042 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1045 if (hdr
->sh_type
!= SHT_NOBITS
)
1048 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1049 flags
|= SEC_READONLY
;
1050 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1052 else if ((flags
& SEC_LOAD
) != 0)
1054 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1057 newsect
->entsize
= hdr
->sh_entsize
;
1059 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1060 flags
|= SEC_STRINGS
;
1061 if (hdr
->sh_flags
& SHF_GROUP
)
1062 if (!setup_group (abfd
, hdr
, newsect
))
1064 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1065 flags
|= SEC_THREAD_LOCAL
;
1066 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1067 flags
|= SEC_EXCLUDE
;
1069 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1071 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1072 but binutils as of 2019-07-23 did not set the EI_OSABI header
1076 case ELFOSABI_FREEBSD
:
1077 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1078 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1082 if ((flags
& SEC_ALLOC
) == 0)
1084 /* The debugging sections appear to be recognized only by name,
1085 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1086 if (name
[0] == '.')
1088 if (strncmp (name
, ".debug", 6) == 0
1089 || strncmp (name
, ".gnu.linkonce.wi.", 17) == 0
1090 || strncmp (name
, ".zdebug", 7) == 0)
1091 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1092 else if (strncmp (name
, GNU_BUILD_ATTRS_SECTION_NAME
, 21) == 0
1093 || strncmp (name
, ".note.gnu", 9) == 0)
1095 flags
|= SEC_ELF_OCTETS
;
1098 else if (strncmp (name
, ".line", 5) == 0
1099 || strncmp (name
, ".stab", 5) == 0
1100 || strcmp (name
, ".gdb_index") == 0)
1101 flags
|= SEC_DEBUGGING
;
1105 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1106 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1107 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
)))
1110 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1111 only link a single copy of the section. This is used to support
1112 g++. g++ will emit each template expansion in its own section.
1113 The symbols will be defined as weak, so that multiple definitions
1114 are permitted. The GNU linker extension is to actually discard
1115 all but one of the sections. */
1116 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1117 && elf_next_in_group (newsect
) == NULL
)
1118 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1120 if (!bfd_set_section_flags (newsect
, flags
))
1123 bed
= get_elf_backend_data (abfd
);
1124 if (bed
->elf_backend_section_flags
)
1125 if (!bed
->elf_backend_section_flags (hdr
))
1128 /* We do not parse the PT_NOTE segments as we are interested even in the
1129 separate debug info files which may have the segments offsets corrupted.
1130 PT_NOTEs from the core files are currently not parsed using BFD. */
1131 if (hdr
->sh_type
== SHT_NOTE
)
1135 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1138 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1139 hdr
->sh_offset
, hdr
->sh_addralign
);
1143 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1145 Elf_Internal_Phdr
*phdr
;
1146 unsigned int i
, nload
;
1148 /* Some ELF linkers produce binaries with all the program header
1149 p_paddr fields zero. If we have such a binary with more than
1150 one PT_LOAD header, then leave the section lma equal to vma
1151 so that we don't create sections with overlapping lma. */
1152 phdr
= elf_tdata (abfd
)->phdr
;
1153 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1154 if (phdr
->p_paddr
!= 0)
1156 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1158 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1161 phdr
= elf_tdata (abfd
)->phdr
;
1162 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1164 if (((phdr
->p_type
== PT_LOAD
1165 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1166 || phdr
->p_type
== PT_TLS
)
1167 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1169 if ((newsect
->flags
& SEC_LOAD
) == 0)
1170 newsect
->lma
= (phdr
->p_paddr
1171 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1173 /* We used to use the same adjustment for SEC_LOAD
1174 sections, but that doesn't work if the segment
1175 is packed with code from multiple VMAs.
1176 Instead we calculate the section LMA based on
1177 the segment LMA. It is assumed that the
1178 segment will contain sections with contiguous
1179 LMAs, even if the VMAs are not. */
1180 newsect
->lma
= (phdr
->p_paddr
1181 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1183 /* With contiguous segments, we can't tell from file
1184 offsets whether a section with zero size should
1185 be placed at the end of one segment or the
1186 beginning of the next. Decide based on vaddr. */
1187 if (hdr
->sh_addr
>= phdr
->p_vaddr
1188 && (hdr
->sh_addr
+ hdr
->sh_size
1189 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1195 /* Compress/decompress DWARF debug sections with names: .debug_* and
1196 .zdebug_*, after the section flags is set. */
1197 if ((newsect
->flags
& SEC_DEBUGGING
)
1198 && ((name
[1] == 'd' && name
[6] == '_')
1199 || (name
[1] == 'z' && name
[7] == '_')))
1201 enum { nothing
, compress
, decompress
} action
= nothing
;
1202 int compression_header_size
;
1203 bfd_size_type uncompressed_size
;
1204 unsigned int uncompressed_align_power
;
1205 bfd_boolean compressed
1206 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1207 &compression_header_size
,
1209 &uncompressed_align_power
);
1212 /* Compressed section. Check if we should decompress. */
1213 if ((abfd
->flags
& BFD_DECOMPRESS
))
1214 action
= decompress
;
1217 /* Compress the uncompressed section or convert from/to .zdebug*
1218 section. Check if we should compress. */
1219 if (action
== nothing
)
1221 if (newsect
->size
!= 0
1222 && (abfd
->flags
& BFD_COMPRESS
)
1223 && compression_header_size
>= 0
1224 && uncompressed_size
> 0
1226 || ((compression_header_size
> 0)
1227 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1233 if (action
== compress
)
1235 if (!bfd_init_section_compress_status (abfd
, newsect
))
1238 /* xgettext:c-format */
1239 (_("%pB: unable to initialize compress status for section %s"),
1246 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1249 /* xgettext:c-format */
1250 (_("%pB: unable to initialize decompress status for section %s"),
1256 if (abfd
->is_linker_input
)
1259 && (action
== decompress
1260 || (action
== compress
1261 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1263 /* Convert section name from .zdebug_* to .debug_* so
1264 that linker will consider this section as a debug
1266 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1267 if (new_name
== NULL
)
1269 bfd_rename_section (newsect
, new_name
);
1273 /* For objdump, don't rename the section. For objcopy, delay
1274 section rename to elf_fake_sections. */
1275 newsect
->flags
|= SEC_ELF_RENAME
;
1278 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1280 const char *lto_section_name
= ".gnu.lto_.lto.";
1281 if (strncmp (name
, lto_section_name
, strlen (lto_section_name
)) == 0)
1283 struct lto_section lsection
;
1284 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1285 sizeof (struct lto_section
)))
1286 abfd
->lto_slim_object
= lsection
.slim_object
;
1292 const char *const bfd_elf_section_type_names
[] =
1294 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1295 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1296 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1299 /* ELF relocs are against symbols. If we are producing relocatable
1300 output, and the reloc is against an external symbol, and nothing
1301 has given us any additional addend, the resulting reloc will also
1302 be against the same symbol. In such a case, we don't want to
1303 change anything about the way the reloc is handled, since it will
1304 all be done at final link time. Rather than put special case code
1305 into bfd_perform_relocation, all the reloc types use this howto
1306 function. It just short circuits the reloc if producing
1307 relocatable output against an external symbol. */
1309 bfd_reloc_status_type
1310 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1311 arelent
*reloc_entry
,
1313 void *data ATTRIBUTE_UNUSED
,
1314 asection
*input_section
,
1316 char **error_message ATTRIBUTE_UNUSED
)
1318 if (output_bfd
!= NULL
1319 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1320 && (! reloc_entry
->howto
->partial_inplace
1321 || reloc_entry
->addend
== 0))
1323 reloc_entry
->address
+= input_section
->output_offset
;
1324 return bfd_reloc_ok
;
1327 return bfd_reloc_continue
;
1330 /* Returns TRUE if section A matches section B.
1331 Names, addresses and links may be different, but everything else
1332 should be the same. */
1335 section_match (const Elf_Internal_Shdr
* a
,
1336 const Elf_Internal_Shdr
* b
)
1338 if (a
->sh_type
!= b
->sh_type
1339 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1340 || a
->sh_addralign
!= b
->sh_addralign
1341 || a
->sh_entsize
!= b
->sh_entsize
)
1343 if (a
->sh_type
== SHT_SYMTAB
1344 || a
->sh_type
== SHT_STRTAB
)
1346 return a
->sh_size
== b
->sh_size
;
1349 /* Find a section in OBFD that has the same characteristics
1350 as IHEADER. Return the index of this section or SHN_UNDEF if
1351 none can be found. Check's section HINT first, as this is likely
1352 to be the correct section. */
1355 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1356 const unsigned int hint
)
1358 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1361 BFD_ASSERT (iheader
!= NULL
);
1363 /* See PR 20922 for a reproducer of the NULL test. */
1364 if (hint
< elf_numsections (obfd
)
1365 && oheaders
[hint
] != NULL
1366 && section_match (oheaders
[hint
], iheader
))
1369 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1371 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1373 if (oheader
== NULL
)
1375 if (section_match (oheader
, iheader
))
1376 /* FIXME: Do we care if there is a potential for
1377 multiple matches ? */
1384 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1385 Processor specific section, based upon a matching input section.
1386 Returns TRUE upon success, FALSE otherwise. */
1389 copy_special_section_fields (const bfd
*ibfd
,
1391 const Elf_Internal_Shdr
*iheader
,
1392 Elf_Internal_Shdr
*oheader
,
1393 const unsigned int secnum
)
1395 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1396 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1397 bfd_boolean changed
= FALSE
;
1398 unsigned int sh_link
;
1400 if (oheader
->sh_type
== SHT_NOBITS
)
1402 /* This is a feature for objcopy --only-keep-debug:
1403 When a section's type is changed to NOBITS, we preserve
1404 the sh_link and sh_info fields so that they can be
1405 matched up with the original.
1407 Note: Strictly speaking these assignments are wrong.
1408 The sh_link and sh_info fields should point to the
1409 relevent sections in the output BFD, which may not be in
1410 the same location as they were in the input BFD. But
1411 the whole point of this action is to preserve the
1412 original values of the sh_link and sh_info fields, so
1413 that they can be matched up with the section headers in
1414 the original file. So strictly speaking we may be
1415 creating an invalid ELF file, but it is only for a file
1416 that just contains debug info and only for sections
1417 without any contents. */
1418 if (oheader
->sh_link
== 0)
1419 oheader
->sh_link
= iheader
->sh_link
;
1420 if (oheader
->sh_info
== 0)
1421 oheader
->sh_info
= iheader
->sh_info
;
1425 /* Allow the target a chance to decide how these fields should be set. */
1426 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1430 /* We have an iheader which might match oheader, and which has non-zero
1431 sh_info and/or sh_link fields. Attempt to follow those links and find
1432 the section in the output bfd which corresponds to the linked section
1433 in the input bfd. */
1434 if (iheader
->sh_link
!= SHN_UNDEF
)
1436 /* See PR 20931 for a reproducer. */
1437 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1440 /* xgettext:c-format */
1441 (_("%pB: invalid sh_link field (%d) in section number %d"),
1442 ibfd
, iheader
->sh_link
, secnum
);
1446 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1447 if (sh_link
!= SHN_UNDEF
)
1449 oheader
->sh_link
= sh_link
;
1453 /* FIXME: Should we install iheader->sh_link
1454 if we could not find a match ? */
1456 /* xgettext:c-format */
1457 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1460 if (iheader
->sh_info
)
1462 /* The sh_info field can hold arbitrary information, but if the
1463 SHF_LINK_INFO flag is set then it should be interpreted as a
1465 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1467 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1469 if (sh_link
!= SHN_UNDEF
)
1470 oheader
->sh_flags
|= SHF_INFO_LINK
;
1473 /* No idea what it means - just copy it. */
1474 sh_link
= iheader
->sh_info
;
1476 if (sh_link
!= SHN_UNDEF
)
1478 oheader
->sh_info
= sh_link
;
1483 /* xgettext:c-format */
1484 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1490 /* Copy the program header and other data from one object module to
1494 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1496 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1497 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1498 const struct elf_backend_data
*bed
;
1501 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1502 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1505 if (!elf_flags_init (obfd
))
1507 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1508 elf_flags_init (obfd
) = TRUE
;
1511 elf_gp (obfd
) = elf_gp (ibfd
);
1513 /* Also copy the EI_OSABI field. */
1514 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1515 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1517 /* If set, copy the EI_ABIVERSION field. */
1518 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1519 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1520 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1522 /* Copy object attributes. */
1523 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1525 if (iheaders
== NULL
|| oheaders
== NULL
)
1528 bed
= get_elf_backend_data (obfd
);
1530 /* Possibly copy other fields in the section header. */
1531 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1534 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1536 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1537 because of a special case need for generating separate debug info
1538 files. See below for more details. */
1540 || (oheader
->sh_type
!= SHT_NOBITS
1541 && oheader
->sh_type
< SHT_LOOS
))
1544 /* Ignore empty sections, and sections whose
1545 fields have already been initialised. */
1546 if (oheader
->sh_size
== 0
1547 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1550 /* Scan for the matching section in the input bfd.
1551 First we try for a direct mapping between the input and output sections. */
1552 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1554 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1556 if (iheader
== NULL
)
1559 if (oheader
->bfd_section
!= NULL
1560 && iheader
->bfd_section
!= NULL
1561 && iheader
->bfd_section
->output_section
!= NULL
1562 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1564 /* We have found a connection from the input section to the
1565 output section. Attempt to copy the header fields. If
1566 this fails then do not try any further sections - there
1567 should only be a one-to-one mapping between input and output. */
1568 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1569 j
= elf_numsections (ibfd
);
1574 if (j
< elf_numsections (ibfd
))
1577 /* That failed. So try to deduce the corresponding input section.
1578 Unfortunately we cannot compare names as the output string table
1579 is empty, so instead we check size, address and type. */
1580 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1582 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1584 if (iheader
== NULL
)
1587 /* Try matching fields in the input section's header.
1588 Since --only-keep-debug turns all non-debug sections into
1589 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1591 if ((oheader
->sh_type
== SHT_NOBITS
1592 || iheader
->sh_type
== oheader
->sh_type
)
1593 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1594 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1595 && iheader
->sh_addralign
== oheader
->sh_addralign
1596 && iheader
->sh_entsize
== oheader
->sh_entsize
1597 && iheader
->sh_size
== oheader
->sh_size
1598 && iheader
->sh_addr
== oheader
->sh_addr
1599 && (iheader
->sh_info
!= oheader
->sh_info
1600 || iheader
->sh_link
!= oheader
->sh_link
))
1602 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1607 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1609 /* Final attempt. Call the backend copy function
1610 with a NULL input section. */
1611 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1620 get_segment_type (unsigned int p_type
)
1625 case PT_NULL
: pt
= "NULL"; break;
1626 case PT_LOAD
: pt
= "LOAD"; break;
1627 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1628 case PT_INTERP
: pt
= "INTERP"; break;
1629 case PT_NOTE
: pt
= "NOTE"; break;
1630 case PT_SHLIB
: pt
= "SHLIB"; break;
1631 case PT_PHDR
: pt
= "PHDR"; break;
1632 case PT_TLS
: pt
= "TLS"; break;
1633 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1634 case PT_GNU_STACK
: pt
= "STACK"; break;
1635 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1636 default: pt
= NULL
; break;
1641 /* Print out the program headers. */
1644 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1646 FILE *f
= (FILE *) farg
;
1647 Elf_Internal_Phdr
*p
;
1649 bfd_byte
*dynbuf
= NULL
;
1651 p
= elf_tdata (abfd
)->phdr
;
1656 fprintf (f
, _("\nProgram Header:\n"));
1657 c
= elf_elfheader (abfd
)->e_phnum
;
1658 for (i
= 0; i
< c
; i
++, p
++)
1660 const char *pt
= get_segment_type (p
->p_type
);
1665 sprintf (buf
, "0x%lx", p
->p_type
);
1668 fprintf (f
, "%8s off 0x", pt
);
1669 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1670 fprintf (f
, " vaddr 0x");
1671 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1672 fprintf (f
, " paddr 0x");
1673 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1674 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1675 fprintf (f
, " filesz 0x");
1676 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1677 fprintf (f
, " memsz 0x");
1678 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1679 fprintf (f
, " flags %c%c%c",
1680 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1681 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1682 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1683 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1684 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1689 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1692 unsigned int elfsec
;
1693 unsigned long shlink
;
1694 bfd_byte
*extdyn
, *extdynend
;
1696 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1698 fprintf (f
, _("\nDynamic Section:\n"));
1700 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1703 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1704 if (elfsec
== SHN_BAD
)
1706 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1708 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1709 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1712 /* PR 17512: file: 6f427532. */
1713 if (s
->size
< extdynsize
)
1715 extdynend
= extdyn
+ s
->size
;
1716 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1718 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1720 Elf_Internal_Dyn dyn
;
1721 const char *name
= "";
1723 bfd_boolean stringp
;
1724 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1726 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1728 if (dyn
.d_tag
== DT_NULL
)
1735 if (bed
->elf_backend_get_target_dtag
)
1736 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1738 if (!strcmp (name
, ""))
1740 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1745 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1746 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1747 case DT_PLTGOT
: name
= "PLTGOT"; break;
1748 case DT_HASH
: name
= "HASH"; break;
1749 case DT_STRTAB
: name
= "STRTAB"; break;
1750 case DT_SYMTAB
: name
= "SYMTAB"; break;
1751 case DT_RELA
: name
= "RELA"; break;
1752 case DT_RELASZ
: name
= "RELASZ"; break;
1753 case DT_RELAENT
: name
= "RELAENT"; break;
1754 case DT_STRSZ
: name
= "STRSZ"; break;
1755 case DT_SYMENT
: name
= "SYMENT"; break;
1756 case DT_INIT
: name
= "INIT"; break;
1757 case DT_FINI
: name
= "FINI"; break;
1758 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1759 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1760 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1761 case DT_REL
: name
= "REL"; break;
1762 case DT_RELSZ
: name
= "RELSZ"; break;
1763 case DT_RELENT
: name
= "RELENT"; break;
1764 case DT_PLTREL
: name
= "PLTREL"; break;
1765 case DT_DEBUG
: name
= "DEBUG"; break;
1766 case DT_TEXTREL
: name
= "TEXTREL"; break;
1767 case DT_JMPREL
: name
= "JMPREL"; break;
1768 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1769 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1770 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1771 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1772 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1773 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1774 case DT_FLAGS
: name
= "FLAGS"; break;
1775 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1776 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1777 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1778 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1779 case DT_MOVEENT
: name
= "MOVEENT"; break;
1780 case DT_MOVESZ
: name
= "MOVESZ"; break;
1781 case DT_FEATURE
: name
= "FEATURE"; break;
1782 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1783 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1784 case DT_SYMINENT
: name
= "SYMINENT"; break;
1785 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1786 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1787 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1788 case DT_PLTPAD
: name
= "PLTPAD"; break;
1789 case DT_MOVETAB
: name
= "MOVETAB"; break;
1790 case DT_SYMINFO
: name
= "SYMINFO"; break;
1791 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1792 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1793 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1794 case DT_VERSYM
: name
= "VERSYM"; break;
1795 case DT_VERDEF
: name
= "VERDEF"; break;
1796 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1797 case DT_VERNEED
: name
= "VERNEED"; break;
1798 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1799 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1800 case DT_USED
: name
= "USED"; break;
1801 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1802 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1805 fprintf (f
, " %-20s ", name
);
1809 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1814 unsigned int tagv
= dyn
.d_un
.d_val
;
1816 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1819 fprintf (f
, "%s", string
);
1828 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1829 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1831 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1835 if (elf_dynverdef (abfd
) != 0)
1837 Elf_Internal_Verdef
*t
;
1839 fprintf (f
, _("\nVersion definitions:\n"));
1840 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1842 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1843 t
->vd_flags
, t
->vd_hash
,
1844 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1845 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1847 Elf_Internal_Verdaux
*a
;
1850 for (a
= t
->vd_auxptr
->vda_nextptr
;
1854 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1860 if (elf_dynverref (abfd
) != 0)
1862 Elf_Internal_Verneed
*t
;
1864 fprintf (f
, _("\nVersion References:\n"));
1865 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1867 Elf_Internal_Vernaux
*a
;
1869 fprintf (f
, _(" required from %s:\n"),
1870 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1871 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1872 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1873 a
->vna_flags
, a
->vna_other
,
1874 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1886 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1887 and return symbol version for symbol version itself. */
1890 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1892 bfd_boolean
*hidden
)
1894 const char *version_string
= NULL
;
1895 if (elf_dynversym (abfd
) != 0
1896 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1898 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1900 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1901 vernum
&= VERSYM_VERSION
;
1904 version_string
= "";
1905 else if (vernum
== 1
1906 && (vernum
> elf_tdata (abfd
)->cverdefs
1907 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1909 version_string
= base_p
? "Base" : "";
1910 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1912 const char *nodename
1913 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1914 version_string
= "";
1917 || symbol
->name
== NULL
1918 || strcmp (symbol
->name
, nodename
) != 0)
1919 version_string
= nodename
;
1923 Elf_Internal_Verneed
*t
;
1925 version_string
= _("<corrupt>");
1926 for (t
= elf_tdata (abfd
)->verref
;
1930 Elf_Internal_Vernaux
*a
;
1932 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1934 if (a
->vna_other
== vernum
)
1936 version_string
= a
->vna_nodename
;
1943 return version_string
;
1946 /* Display ELF-specific fields of a symbol. */
1949 bfd_elf_print_symbol (bfd
*abfd
,
1952 bfd_print_symbol_type how
)
1954 FILE *file
= (FILE *) filep
;
1957 case bfd_print_symbol_name
:
1958 fprintf (file
, "%s", symbol
->name
);
1960 case bfd_print_symbol_more
:
1961 fprintf (file
, "elf ");
1962 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1963 fprintf (file
, " %x", symbol
->flags
);
1965 case bfd_print_symbol_all
:
1967 const char *section_name
;
1968 const char *name
= NULL
;
1969 const struct elf_backend_data
*bed
;
1970 unsigned char st_other
;
1972 const char *version_string
;
1975 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1977 bed
= get_elf_backend_data (abfd
);
1978 if (bed
->elf_backend_print_symbol_all
)
1979 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1983 name
= symbol
->name
;
1984 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1987 fprintf (file
, " %s\t", section_name
);
1988 /* Print the "other" value for a symbol. For common symbols,
1989 we've already printed the size; now print the alignment.
1990 For other symbols, we have no specified alignment, and
1991 we've printed the address; now print the size. */
1992 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1993 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1995 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1996 bfd_fprintf_vma (abfd
, file
, val
);
1998 /* If we have version information, print it. */
1999 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2006 fprintf (file
, " %-11s", version_string
);
2011 fprintf (file
, " (%s)", version_string
);
2012 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2017 /* If the st_other field is not zero, print it. */
2018 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2023 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2024 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2025 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2027 /* Some other non-defined flags are also present, so print
2029 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2032 fprintf (file
, " %s", name
);
2038 /* ELF .o/exec file reading */
2040 /* Create a new bfd section from an ELF section header. */
2043 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2045 Elf_Internal_Shdr
*hdr
;
2046 Elf_Internal_Ehdr
*ehdr
;
2047 const struct elf_backend_data
*bed
;
2049 bfd_boolean ret
= TRUE
;
2050 static bfd_boolean
* sections_being_created
= NULL
;
2051 static bfd
* sections_being_created_abfd
= NULL
;
2052 static unsigned int nesting
= 0;
2054 if (shindex
>= elf_numsections (abfd
))
2059 /* PR17512: A corrupt ELF binary might contain a recursive group of
2060 sections, with each the string indices pointing to the next in the
2061 loop. Detect this here, by refusing to load a section that we are
2062 already in the process of loading. We only trigger this test if
2063 we have nested at least three sections deep as normal ELF binaries
2064 can expect to recurse at least once.
2066 FIXME: It would be better if this array was attached to the bfd,
2067 rather than being held in a static pointer. */
2069 if (sections_being_created_abfd
!= abfd
)
2070 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_malloc (amt
);
2078 memset (sections_being_created
, FALSE
, amt
);
2079 if (sections_being_created
== NULL
)
2081 sections_being_created_abfd
= abfd
;
2083 if (sections_being_created
[shindex
])
2086 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2089 sections_being_created
[shindex
] = TRUE
;
2092 hdr
= elf_elfsections (abfd
)[shindex
];
2093 ehdr
= elf_elfheader (abfd
);
2094 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2099 bed
= get_elf_backend_data (abfd
);
2100 switch (hdr
->sh_type
)
2103 /* Inactive section. Throw it away. */
2106 case SHT_PROGBITS
: /* Normal section with contents. */
2107 case SHT_NOBITS
: /* .bss section. */
2108 case SHT_HASH
: /* .hash section. */
2109 case SHT_NOTE
: /* .note section. */
2110 case SHT_INIT_ARRAY
: /* .init_array section. */
2111 case SHT_FINI_ARRAY
: /* .fini_array section. */
2112 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2113 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2114 case SHT_GNU_HASH
: /* .gnu.hash section. */
2115 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2118 case SHT_DYNAMIC
: /* Dynamic linking information. */
2119 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2122 if (hdr
->sh_link
> elf_numsections (abfd
))
2124 /* PR 10478: Accept Solaris binaries with a sh_link
2125 field set to SHN_BEFORE or SHN_AFTER. */
2126 switch (bfd_get_arch (abfd
))
2129 case bfd_arch_sparc
:
2130 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2131 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2133 /* Otherwise fall through. */
2138 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2140 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2142 Elf_Internal_Shdr
*dynsymhdr
;
2144 /* The shared libraries distributed with hpux11 have a bogus
2145 sh_link field for the ".dynamic" section. Find the
2146 string table for the ".dynsym" section instead. */
2147 if (elf_dynsymtab (abfd
) != 0)
2149 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2150 hdr
->sh_link
= dynsymhdr
->sh_link
;
2154 unsigned int i
, num_sec
;
2156 num_sec
= elf_numsections (abfd
);
2157 for (i
= 1; i
< num_sec
; i
++)
2159 dynsymhdr
= elf_elfsections (abfd
)[i
];
2160 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2162 hdr
->sh_link
= dynsymhdr
->sh_link
;
2170 case SHT_SYMTAB
: /* A symbol table. */
2171 if (elf_onesymtab (abfd
) == shindex
)
2174 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2177 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2179 if (hdr
->sh_size
!= 0)
2181 /* Some assemblers erroneously set sh_info to one with a
2182 zero sh_size. ld sees this as a global symbol count
2183 of (unsigned) -1. Fix it here. */
2188 /* PR 18854: A binary might contain more than one symbol table.
2189 Unusual, but possible. Warn, but continue. */
2190 if (elf_onesymtab (abfd
) != 0)
2193 /* xgettext:c-format */
2194 (_("%pB: warning: multiple symbol tables detected"
2195 " - ignoring the table in section %u"),
2199 elf_onesymtab (abfd
) = shindex
;
2200 elf_symtab_hdr (abfd
) = *hdr
;
2201 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2202 abfd
->flags
|= HAS_SYMS
;
2204 /* Sometimes a shared object will map in the symbol table. If
2205 SHF_ALLOC is set, and this is a shared object, then we also
2206 treat this section as a BFD section. We can not base the
2207 decision purely on SHF_ALLOC, because that flag is sometimes
2208 set in a relocatable object file, which would confuse the
2210 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2211 && (abfd
->flags
& DYNAMIC
) != 0
2212 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2216 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2217 can't read symbols without that section loaded as well. It
2218 is most likely specified by the next section header. */
2220 elf_section_list
* entry
;
2221 unsigned int i
, num_sec
;
2223 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2224 if (entry
->hdr
.sh_link
== shindex
)
2227 num_sec
= elf_numsections (abfd
);
2228 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2230 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2232 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2233 && hdr2
->sh_link
== shindex
)
2238 for (i
= 1; i
< shindex
; i
++)
2240 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2242 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2243 && hdr2
->sh_link
== shindex
)
2248 ret
= bfd_section_from_shdr (abfd
, i
);
2249 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2253 case SHT_DYNSYM
: /* A dynamic symbol table. */
2254 if (elf_dynsymtab (abfd
) == shindex
)
2257 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2260 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2262 if (hdr
->sh_size
!= 0)
2265 /* Some linkers erroneously set sh_info to one with a
2266 zero sh_size. ld sees this as a global symbol count
2267 of (unsigned) -1. Fix it here. */
2272 /* PR 18854: A binary might contain more than one dynamic symbol table.
2273 Unusual, but possible. Warn, but continue. */
2274 if (elf_dynsymtab (abfd
) != 0)
2277 /* xgettext:c-format */
2278 (_("%pB: warning: multiple dynamic symbol tables detected"
2279 " - ignoring the table in section %u"),
2283 elf_dynsymtab (abfd
) = shindex
;
2284 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2285 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2286 abfd
->flags
|= HAS_SYMS
;
2288 /* Besides being a symbol table, we also treat this as a regular
2289 section, so that objcopy can handle it. */
2290 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2293 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2295 elf_section_list
* entry
;
2297 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2298 if (entry
->ndx
== shindex
)
2301 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2304 entry
->ndx
= shindex
;
2306 entry
->next
= elf_symtab_shndx_list (abfd
);
2307 elf_symtab_shndx_list (abfd
) = entry
;
2308 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2312 case SHT_STRTAB
: /* A string table. */
2313 if (hdr
->bfd_section
!= NULL
)
2316 if (ehdr
->e_shstrndx
== shindex
)
2318 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2319 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2323 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2326 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2327 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2331 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2334 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2335 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2336 elf_elfsections (abfd
)[shindex
] = hdr
;
2337 /* We also treat this as a regular section, so that objcopy
2339 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2344 /* If the string table isn't one of the above, then treat it as a
2345 regular section. We need to scan all the headers to be sure,
2346 just in case this strtab section appeared before the above. */
2347 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2349 unsigned int i
, num_sec
;
2351 num_sec
= elf_numsections (abfd
);
2352 for (i
= 1; i
< num_sec
; i
++)
2354 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2355 if (hdr2
->sh_link
== shindex
)
2357 /* Prevent endless recursion on broken objects. */
2360 if (! bfd_section_from_shdr (abfd
, i
))
2362 if (elf_onesymtab (abfd
) == i
)
2364 if (elf_dynsymtab (abfd
) == i
)
2365 goto dynsymtab_strtab
;
2369 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2374 /* *These* do a lot of work -- but build no sections! */
2376 asection
*target_sect
;
2377 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2378 unsigned int num_sec
= elf_numsections (abfd
);
2379 struct bfd_elf_section_data
*esdt
;
2382 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2383 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2386 /* Check for a bogus link to avoid crashing. */
2387 if (hdr
->sh_link
>= num_sec
)
2390 /* xgettext:c-format */
2391 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2392 abfd
, hdr
->sh_link
, name
, shindex
);
2393 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2398 /* For some incomprehensible reason Oracle distributes
2399 libraries for Solaris in which some of the objects have
2400 bogus sh_link fields. It would be nice if we could just
2401 reject them, but, unfortunately, some people need to use
2402 them. We scan through the section headers; if we find only
2403 one suitable symbol table, we clobber the sh_link to point
2404 to it. I hope this doesn't break anything.
2406 Don't do it on executable nor shared library. */
2407 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2408 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2409 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2415 for (scan
= 1; scan
< num_sec
; scan
++)
2417 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2418 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2429 hdr
->sh_link
= found
;
2432 /* Get the symbol table. */
2433 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2434 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2435 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2438 /* If this is an alloc section in an executable or shared
2439 library, or the reloc section does not use the main symbol
2440 table we don't treat it as a reloc section. BFD can't
2441 adequately represent such a section, so at least for now,
2442 we don't try. We just present it as a normal section. We
2443 also can't use it as a reloc section if it points to the
2444 null section, an invalid section, another reloc section, or
2445 its sh_link points to the null section. */
2446 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2447 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2448 || hdr
->sh_link
== SHN_UNDEF
2449 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2450 || hdr
->sh_info
== SHN_UNDEF
2451 || hdr
->sh_info
>= num_sec
2452 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2453 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2455 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2460 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2463 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2464 if (target_sect
== NULL
)
2467 esdt
= elf_section_data (target_sect
);
2468 if (hdr
->sh_type
== SHT_RELA
)
2469 p_hdr
= &esdt
->rela
.hdr
;
2471 p_hdr
= &esdt
->rel
.hdr
;
2473 /* PR 17512: file: 0b4f81b7.
2474 Also see PR 24456, for a file which deliberately has two reloc
2478 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2481 /* xgettext:c-format */
2482 (_("%pB: warning: secondary relocation section '%s' "
2483 "for section %pA found - ignoring"),
2484 abfd
, name
, target_sect
);
2489 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2494 elf_elfsections (abfd
)[shindex
] = hdr2
;
2495 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2496 * bed
->s
->int_rels_per_ext_rel
);
2497 target_sect
->flags
|= SEC_RELOC
;
2498 target_sect
->relocation
= NULL
;
2499 target_sect
->rel_filepos
= hdr
->sh_offset
;
2500 /* In the section to which the relocations apply, mark whether
2501 its relocations are of the REL or RELA variety. */
2502 if (hdr
->sh_size
!= 0)
2504 if (hdr
->sh_type
== SHT_RELA
)
2505 target_sect
->use_rela_p
= 1;
2507 abfd
->flags
|= HAS_RELOC
;
2511 case SHT_GNU_verdef
:
2512 elf_dynverdef (abfd
) = shindex
;
2513 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2514 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2517 case SHT_GNU_versym
:
2518 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2521 elf_dynversym (abfd
) = shindex
;
2522 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2523 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2526 case SHT_GNU_verneed
:
2527 elf_dynverref (abfd
) = shindex
;
2528 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2529 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2536 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2539 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2545 /* Possibly an attributes section. */
2546 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2547 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2549 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2551 _bfd_elf_parse_attributes (abfd
, hdr
);
2555 /* Check for any processor-specific section types. */
2556 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2559 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2561 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2562 /* FIXME: How to properly handle allocated section reserved
2563 for applications? */
2565 /* xgettext:c-format */
2566 (_("%pB: unknown type [%#x] section `%s'"),
2567 abfd
, hdr
->sh_type
, name
);
2570 /* Allow sections reserved for applications. */
2571 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2576 else if (hdr
->sh_type
>= SHT_LOPROC
2577 && hdr
->sh_type
<= SHT_HIPROC
)
2578 /* FIXME: We should handle this section. */
2580 /* xgettext:c-format */
2581 (_("%pB: unknown type [%#x] section `%s'"),
2582 abfd
, hdr
->sh_type
, name
);
2583 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2585 /* Unrecognised OS-specific sections. */
2586 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2587 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2588 required to correctly process the section and the file should
2589 be rejected with an error message. */
2591 /* xgettext:c-format */
2592 (_("%pB: unknown type [%#x] section `%s'"),
2593 abfd
, hdr
->sh_type
, name
);
2596 /* Otherwise it should be processed. */
2597 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2602 /* FIXME: We should handle this section. */
2604 /* xgettext:c-format */
2605 (_("%pB: unknown type [%#x] section `%s'"),
2606 abfd
, hdr
->sh_type
, name
);
2614 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2615 sections_being_created
[shindex
] = FALSE
;
2616 if (-- nesting
== 0)
2618 free (sections_being_created
);
2619 sections_being_created
= NULL
;
2620 sections_being_created_abfd
= NULL
;
2625 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2628 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2630 unsigned long r_symndx
)
2632 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2634 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2636 Elf_Internal_Shdr
*symtab_hdr
;
2637 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2638 Elf_External_Sym_Shndx eshndx
;
2640 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2641 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2642 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2645 if (cache
->abfd
!= abfd
)
2647 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2650 cache
->indx
[ent
] = r_symndx
;
2653 return &cache
->sym
[ent
];
2656 /* Given an ELF section number, retrieve the corresponding BFD
2660 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2662 if (sec_index
>= elf_numsections (abfd
))
2664 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2667 static const struct bfd_elf_special_section special_sections_b
[] =
2669 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2670 { NULL
, 0, 0, 0, 0 }
2673 static const struct bfd_elf_special_section special_sections_c
[] =
2675 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2676 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2677 { NULL
, 0, 0, 0, 0 }
2680 static const struct bfd_elf_special_section special_sections_d
[] =
2682 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2683 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2684 /* There are more DWARF sections than these, but they needn't be added here
2685 unless you have to cope with broken compilers that don't emit section
2686 attributes or you want to help the user writing assembler. */
2687 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2688 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2689 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2690 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2691 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2692 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2693 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2694 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2695 { NULL
, 0, 0, 0, 0 }
2698 static const struct bfd_elf_special_section special_sections_f
[] =
2700 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2701 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2702 { NULL
, 0 , 0, 0, 0 }
2705 static const struct bfd_elf_special_section special_sections_g
[] =
2707 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2708 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2709 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2710 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2711 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2712 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2713 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2714 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2715 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2716 { NULL
, 0, 0, 0, 0 }
2719 static const struct bfd_elf_special_section special_sections_h
[] =
2721 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2722 { NULL
, 0, 0, 0, 0 }
2725 static const struct bfd_elf_special_section special_sections_i
[] =
2727 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2728 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2729 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2730 { NULL
, 0, 0, 0, 0 }
2733 static const struct bfd_elf_special_section special_sections_l
[] =
2735 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2736 { NULL
, 0, 0, 0, 0 }
2739 static const struct bfd_elf_special_section special_sections_n
[] =
2741 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2742 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2743 { NULL
, 0, 0, 0, 0 }
2746 static const struct bfd_elf_special_section special_sections_p
[] =
2748 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2749 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2750 { NULL
, 0, 0, 0, 0 }
2753 static const struct bfd_elf_special_section special_sections_r
[] =
2755 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2756 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2757 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2758 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2759 { NULL
, 0, 0, 0, 0 }
2762 static const struct bfd_elf_special_section special_sections_s
[] =
2764 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2765 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2766 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2767 /* See struct bfd_elf_special_section declaration for the semantics of
2768 this special case where .prefix_length != strlen (.prefix). */
2769 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2770 { NULL
, 0, 0, 0, 0 }
2773 static const struct bfd_elf_special_section special_sections_t
[] =
2775 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2776 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2777 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2778 { NULL
, 0, 0, 0, 0 }
2781 static const struct bfd_elf_special_section special_sections_z
[] =
2783 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2784 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2785 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2786 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2787 { NULL
, 0, 0, 0, 0 }
2790 static const struct bfd_elf_special_section
* const special_sections
[] =
2792 special_sections_b
, /* 'b' */
2793 special_sections_c
, /* 'c' */
2794 special_sections_d
, /* 'd' */
2796 special_sections_f
, /* 'f' */
2797 special_sections_g
, /* 'g' */
2798 special_sections_h
, /* 'h' */
2799 special_sections_i
, /* 'i' */
2802 special_sections_l
, /* 'l' */
2804 special_sections_n
, /* 'n' */
2806 special_sections_p
, /* 'p' */
2808 special_sections_r
, /* 'r' */
2809 special_sections_s
, /* 's' */
2810 special_sections_t
, /* 't' */
2816 special_sections_z
/* 'z' */
2819 const struct bfd_elf_special_section
*
2820 _bfd_elf_get_special_section (const char *name
,
2821 const struct bfd_elf_special_section
*spec
,
2827 len
= strlen (name
);
2829 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2832 int prefix_len
= spec
[i
].prefix_length
;
2834 if (len
< prefix_len
)
2836 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2839 suffix_len
= spec
[i
].suffix_length
;
2840 if (suffix_len
<= 0)
2842 if (name
[prefix_len
] != 0)
2844 if (suffix_len
== 0)
2846 if (name
[prefix_len
] != '.'
2847 && (suffix_len
== -2
2848 || (rela
&& spec
[i
].type
== SHT_REL
)))
2854 if (len
< prefix_len
+ suffix_len
)
2856 if (memcmp (name
+ len
- suffix_len
,
2857 spec
[i
].prefix
+ prefix_len
,
2867 const struct bfd_elf_special_section
*
2868 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2871 const struct bfd_elf_special_section
*spec
;
2872 const struct elf_backend_data
*bed
;
2874 /* See if this is one of the special sections. */
2875 if (sec
->name
== NULL
)
2878 bed
= get_elf_backend_data (abfd
);
2879 spec
= bed
->special_sections
;
2882 spec
= _bfd_elf_get_special_section (sec
->name
,
2883 bed
->special_sections
,
2889 if (sec
->name
[0] != '.')
2892 i
= sec
->name
[1] - 'b';
2893 if (i
< 0 || i
> 'z' - 'b')
2896 spec
= special_sections
[i
];
2901 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2905 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2907 struct bfd_elf_section_data
*sdata
;
2908 const struct elf_backend_data
*bed
;
2909 const struct bfd_elf_special_section
*ssect
;
2911 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2914 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2918 sec
->used_by_bfd
= sdata
;
2921 /* Indicate whether or not this section should use RELA relocations. */
2922 bed
= get_elf_backend_data (abfd
);
2923 sec
->use_rela_p
= bed
->default_use_rela_p
;
2925 /* Set up ELF section type and flags for newly created sections, if
2926 there is an ABI mandated section. */
2927 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2930 elf_section_type (sec
) = ssect
->type
;
2931 elf_section_flags (sec
) = ssect
->attr
;
2934 return _bfd_generic_new_section_hook (abfd
, sec
);
2937 /* Create a new bfd section from an ELF program header.
2939 Since program segments have no names, we generate a synthetic name
2940 of the form segment<NUM>, where NUM is generally the index in the
2941 program header table. For segments that are split (see below) we
2942 generate the names segment<NUM>a and segment<NUM>b.
2944 Note that some program segments may have a file size that is different than
2945 (less than) the memory size. All this means is that at execution the
2946 system must allocate the amount of memory specified by the memory size,
2947 but only initialize it with the first "file size" bytes read from the
2948 file. This would occur for example, with program segments consisting
2949 of combined data+bss.
2951 To handle the above situation, this routine generates TWO bfd sections
2952 for the single program segment. The first has the length specified by
2953 the file size of the segment, and the second has the length specified
2954 by the difference between the two sizes. In effect, the segment is split
2955 into its initialized and uninitialized parts.
2960 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2961 Elf_Internal_Phdr
*hdr
,
2963 const char *type_name
)
2970 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
2972 split
= ((hdr
->p_memsz
> 0)
2973 && (hdr
->p_filesz
> 0)
2974 && (hdr
->p_memsz
> hdr
->p_filesz
));
2976 if (hdr
->p_filesz
> 0)
2978 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2979 len
= strlen (namebuf
) + 1;
2980 name
= (char *) bfd_alloc (abfd
, len
);
2983 memcpy (name
, namebuf
, len
);
2984 newsect
= bfd_make_section (abfd
, name
);
2985 if (newsect
== NULL
)
2987 newsect
->vma
= hdr
->p_vaddr
/ opb
;
2988 newsect
->lma
= hdr
->p_paddr
/ opb
;
2989 newsect
->size
= hdr
->p_filesz
;
2990 newsect
->filepos
= hdr
->p_offset
;
2991 newsect
->flags
|= SEC_HAS_CONTENTS
;
2992 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2993 if (hdr
->p_type
== PT_LOAD
)
2995 newsect
->flags
|= SEC_ALLOC
;
2996 newsect
->flags
|= SEC_LOAD
;
2997 if (hdr
->p_flags
& PF_X
)
2999 /* FIXME: all we known is that it has execute PERMISSION,
3001 newsect
->flags
|= SEC_CODE
;
3004 if (!(hdr
->p_flags
& PF_W
))
3006 newsect
->flags
|= SEC_READONLY
;
3010 if (hdr
->p_memsz
> hdr
->p_filesz
)
3014 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3015 len
= strlen (namebuf
) + 1;
3016 name
= (char *) bfd_alloc (abfd
, len
);
3019 memcpy (name
, namebuf
, len
);
3020 newsect
= bfd_make_section (abfd
, name
);
3021 if (newsect
== NULL
)
3023 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3024 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3025 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3026 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3027 align
= newsect
->vma
& -newsect
->vma
;
3028 if (align
== 0 || align
> hdr
->p_align
)
3029 align
= hdr
->p_align
;
3030 newsect
->alignment_power
= bfd_log2 (align
);
3031 if (hdr
->p_type
== PT_LOAD
)
3033 /* Hack for gdb. Segments that have not been modified do
3034 not have their contents written to a core file, on the
3035 assumption that a debugger can find the contents in the
3036 executable. We flag this case by setting the fake
3037 section size to zero. Note that "real" bss sections will
3038 always have their contents dumped to the core file. */
3039 if (bfd_get_format (abfd
) == bfd_core
)
3041 newsect
->flags
|= SEC_ALLOC
;
3042 if (hdr
->p_flags
& PF_X
)
3043 newsect
->flags
|= SEC_CODE
;
3045 if (!(hdr
->p_flags
& PF_W
))
3046 newsect
->flags
|= SEC_READONLY
;
3053 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3055 /* The return value is ignored. Build-ids are considered optional. */
3056 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3057 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3063 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3065 const struct elf_backend_data
*bed
;
3067 switch (hdr
->p_type
)
3070 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3073 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3075 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3076 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3080 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3083 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3086 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3088 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3094 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3097 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3099 case PT_GNU_EH_FRAME
:
3100 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3104 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3107 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3110 /* Check for any processor-specific program segment types. */
3111 bed
= get_elf_backend_data (abfd
);
3112 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3116 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3120 _bfd_elf_single_rel_hdr (asection
*sec
)
3122 if (elf_section_data (sec
)->rel
.hdr
)
3124 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3125 return elf_section_data (sec
)->rel
.hdr
;
3128 return elf_section_data (sec
)->rela
.hdr
;
3132 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3133 Elf_Internal_Shdr
*rel_hdr
,
3134 const char *sec_name
,
3135 bfd_boolean use_rela_p
)
3137 char *name
= (char *) bfd_alloc (abfd
,
3138 sizeof ".rela" + strlen (sec_name
));
3142 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3144 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3146 if (rel_hdr
->sh_name
== (unsigned int) -1)
3152 /* Allocate and initialize a section-header for a new reloc section,
3153 containing relocations against ASECT. It is stored in RELDATA. If
3154 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3158 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3159 struct bfd_elf_section_reloc_data
*reldata
,
3160 const char *sec_name
,
3161 bfd_boolean use_rela_p
,
3162 bfd_boolean delay_st_name_p
)
3164 Elf_Internal_Shdr
*rel_hdr
;
3165 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3167 BFD_ASSERT (reldata
->hdr
== NULL
);
3168 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3169 reldata
->hdr
= rel_hdr
;
3171 if (delay_st_name_p
)
3172 rel_hdr
->sh_name
= (unsigned int) -1;
3173 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3176 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3177 rel_hdr
->sh_entsize
= (use_rela_p
3178 ? bed
->s
->sizeof_rela
3179 : bed
->s
->sizeof_rel
);
3180 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3181 rel_hdr
->sh_flags
= 0;
3182 rel_hdr
->sh_addr
= 0;
3183 rel_hdr
->sh_size
= 0;
3184 rel_hdr
->sh_offset
= 0;
3189 /* Return the default section type based on the passed in section flags. */
3192 bfd_elf_get_default_section_type (flagword flags
)
3194 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3195 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3197 return SHT_PROGBITS
;
3200 struct fake_section_arg
3202 struct bfd_link_info
*link_info
;
3206 /* Set up an ELF internal section header for a section. */
3209 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3211 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3212 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3213 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3214 Elf_Internal_Shdr
*this_hdr
;
3215 unsigned int sh_type
;
3216 const char *name
= asect
->name
;
3217 bfd_boolean delay_st_name_p
= FALSE
;
3222 /* We already failed; just get out of the bfd_map_over_sections
3227 this_hdr
= &esd
->this_hdr
;
3231 /* ld: compress DWARF debug sections with names: .debug_*. */
3232 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3233 && (asect
->flags
& SEC_DEBUGGING
)
3237 /* Set SEC_ELF_COMPRESS to indicate this section should be
3239 asect
->flags
|= SEC_ELF_COMPRESS
;
3240 /* If this section will be compressed, delay adding section
3241 name to section name section after it is compressed in
3242 _bfd_elf_assign_file_positions_for_non_load. */
3243 delay_st_name_p
= TRUE
;
3246 else if ((asect
->flags
& SEC_ELF_RENAME
))
3248 /* objcopy: rename output DWARF debug section. */
3249 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3251 /* When we decompress or compress with SHF_COMPRESSED,
3252 convert section name from .zdebug_* to .debug_* if
3256 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3257 if (new_name
== NULL
)
3265 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3267 /* PR binutils/18087: Compression does not always make a
3268 section smaller. So only rename the section when
3269 compression has actually taken place. If input section
3270 name is .zdebug_*, we should never compress it again. */
3271 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3272 if (new_name
== NULL
)
3277 BFD_ASSERT (name
[1] != 'z');
3282 if (delay_st_name_p
)
3283 this_hdr
->sh_name
= (unsigned int) -1;
3287 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3289 if (this_hdr
->sh_name
== (unsigned int) -1)
3296 /* Don't clear sh_flags. Assembler may set additional bits. */
3298 if ((asect
->flags
& SEC_ALLOC
) != 0
3299 || asect
->user_set_vma
)
3300 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3302 this_hdr
->sh_addr
= 0;
3304 this_hdr
->sh_offset
= 0;
3305 this_hdr
->sh_size
= asect
->size
;
3306 this_hdr
->sh_link
= 0;
3307 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3308 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3311 /* xgettext:c-format */
3312 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3313 abfd
, asect
->alignment_power
, asect
);
3317 /* Set sh_addralign to the highest power of two given by alignment
3318 consistent with the section VMA. Linker scripts can force VMA. */
3319 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3320 this_hdr
->sh_addralign
= mask
& -mask
;
3321 /* The sh_entsize and sh_info fields may have been set already by
3322 copy_private_section_data. */
3324 this_hdr
->bfd_section
= asect
;
3325 this_hdr
->contents
= NULL
;
3327 /* If the section type is unspecified, we set it based on
3329 if ((asect
->flags
& SEC_GROUP
) != 0)
3330 sh_type
= SHT_GROUP
;
3332 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3334 if (this_hdr
->sh_type
== SHT_NULL
)
3335 this_hdr
->sh_type
= sh_type
;
3336 else if (this_hdr
->sh_type
== SHT_NOBITS
3337 && sh_type
== SHT_PROGBITS
3338 && (asect
->flags
& SEC_ALLOC
) != 0)
3340 /* Warn if we are changing a NOBITS section to PROGBITS, but
3341 allow the link to proceed. This can happen when users link
3342 non-bss input sections to bss output sections, or emit data
3343 to a bss output section via a linker script. */
3345 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3346 this_hdr
->sh_type
= sh_type
;
3349 switch (this_hdr
->sh_type
)
3360 case SHT_INIT_ARRAY
:
3361 case SHT_FINI_ARRAY
:
3362 case SHT_PREINIT_ARRAY
:
3363 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3367 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3371 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3375 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3379 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3380 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3384 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3385 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3388 case SHT_GNU_versym
:
3389 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3392 case SHT_GNU_verdef
:
3393 this_hdr
->sh_entsize
= 0;
3394 /* objcopy or strip will copy over sh_info, but may not set
3395 cverdefs. The linker will set cverdefs, but sh_info will be
3397 if (this_hdr
->sh_info
== 0)
3398 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3400 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3401 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3404 case SHT_GNU_verneed
:
3405 this_hdr
->sh_entsize
= 0;
3406 /* objcopy or strip will copy over sh_info, but may not set
3407 cverrefs. The linker will set cverrefs, but sh_info will be
3409 if (this_hdr
->sh_info
== 0)
3410 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3412 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3413 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3417 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3421 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3425 if ((asect
->flags
& SEC_ALLOC
) != 0)
3426 this_hdr
->sh_flags
|= SHF_ALLOC
;
3427 if ((asect
->flags
& SEC_READONLY
) == 0)
3428 this_hdr
->sh_flags
|= SHF_WRITE
;
3429 if ((asect
->flags
& SEC_CODE
) != 0)
3430 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3431 if ((asect
->flags
& SEC_MERGE
) != 0)
3433 this_hdr
->sh_flags
|= SHF_MERGE
;
3434 this_hdr
->sh_entsize
= asect
->entsize
;
3436 if ((asect
->flags
& SEC_STRINGS
) != 0)
3437 this_hdr
->sh_flags
|= SHF_STRINGS
;
3438 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3439 this_hdr
->sh_flags
|= SHF_GROUP
;
3440 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3442 this_hdr
->sh_flags
|= SHF_TLS
;
3443 if (asect
->size
== 0
3444 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3446 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3448 this_hdr
->sh_size
= 0;
3451 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3452 if (this_hdr
->sh_size
!= 0)
3453 this_hdr
->sh_type
= SHT_NOBITS
;
3457 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3458 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3460 /* If the section has relocs, set up a section header for the
3461 SHT_REL[A] section. If two relocation sections are required for
3462 this section, it is up to the processor-specific back-end to
3463 create the other. */
3464 if ((asect
->flags
& SEC_RELOC
) != 0)
3466 /* When doing a relocatable link, create both REL and RELA sections if
3469 /* Do the normal setup if we wouldn't create any sections here. */
3470 && esd
->rel
.count
+ esd
->rela
.count
> 0
3471 && (bfd_link_relocatable (arg
->link_info
)
3472 || arg
->link_info
->emitrelocations
))
3474 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3475 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3476 FALSE
, delay_st_name_p
))
3481 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3482 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3483 TRUE
, delay_st_name_p
))
3489 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3491 ? &esd
->rela
: &esd
->rel
),
3501 /* Check for processor-specific section types. */
3502 sh_type
= this_hdr
->sh_type
;
3503 if (bed
->elf_backend_fake_sections
3504 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3510 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3512 /* Don't change the header type from NOBITS if we are being
3513 called for objcopy --only-keep-debug. */
3514 this_hdr
->sh_type
= sh_type
;
3518 /* Fill in the contents of a SHT_GROUP section. Called from
3519 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3520 when ELF targets use the generic linker, ld. Called for ld -r
3521 from bfd_elf_final_link. */
3524 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3526 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3527 asection
*elt
, *first
;
3531 /* Ignore linker created group section. See elfNN_ia64_object_p in
3533 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3538 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3540 unsigned long symindx
= 0;
3542 /* elf_group_id will have been set up by objcopy and the
3544 if (elf_group_id (sec
) != NULL
)
3545 symindx
= elf_group_id (sec
)->udata
.i
;
3549 /* If called from the assembler, swap_out_syms will have set up
3551 PR 25699: A corrupt input file could contain bogus group info. */
3552 if (elf_section_syms (abfd
) == NULL
)
3557 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3559 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3561 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3563 /* The ELF backend linker sets sh_info to -2 when the group
3564 signature symbol is global, and thus the index can't be
3565 set until all local symbols are output. */
3567 struct bfd_elf_section_data
*sec_data
;
3568 unsigned long symndx
;
3569 unsigned long extsymoff
;
3570 struct elf_link_hash_entry
*h
;
3572 /* The point of this little dance to the first SHF_GROUP section
3573 then back to the SHT_GROUP section is that this gets us to
3574 the SHT_GROUP in the input object. */
3575 igroup
= elf_sec_group (elf_next_in_group (sec
));
3576 sec_data
= elf_section_data (igroup
);
3577 symndx
= sec_data
->this_hdr
.sh_info
;
3579 if (!elf_bad_symtab (igroup
->owner
))
3581 Elf_Internal_Shdr
*symtab_hdr
;
3583 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3584 extsymoff
= symtab_hdr
->sh_info
;
3586 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3587 while (h
->root
.type
== bfd_link_hash_indirect
3588 || h
->root
.type
== bfd_link_hash_warning
)
3589 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3591 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3594 /* The contents won't be allocated for "ld -r" or objcopy. */
3596 if (sec
->contents
== NULL
)
3599 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3601 /* Arrange for the section to be written out. */
3602 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3603 if (sec
->contents
== NULL
)
3610 loc
= sec
->contents
+ sec
->size
;
3612 /* Get the pointer to the first section in the group that gas
3613 squirreled away here. objcopy arranges for this to be set to the
3614 start of the input section group. */
3615 first
= elt
= elf_next_in_group (sec
);
3617 /* First element is a flag word. Rest of section is elf section
3618 indices for all the sections of the group. Write them backwards
3619 just to keep the group in the same order as given in .section
3620 directives, not that it matters. */
3627 s
= s
->output_section
;
3629 && !bfd_is_abs_section (s
))
3631 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3632 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3634 if (elf_sec
->rel
.hdr
!= NULL
3636 || (input_elf_sec
->rel
.hdr
!= NULL
3637 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3639 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3641 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3643 if (elf_sec
->rela
.hdr
!= NULL
3645 || (input_elf_sec
->rela
.hdr
!= NULL
3646 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3648 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3650 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3653 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3655 elt
= elf_next_in_group (elt
);
3661 BFD_ASSERT (loc
== sec
->contents
);
3663 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3666 /* Given NAME, the name of a relocation section stripped of its
3667 .rel/.rela prefix, return the section in ABFD to which the
3668 relocations apply. */
3671 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3673 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3674 section likely apply to .got.plt or .got section. */
3675 if (get_elf_backend_data (abfd
)->want_got_plt
3676 && strcmp (name
, ".plt") == 0)
3681 sec
= bfd_get_section_by_name (abfd
, name
);
3687 return bfd_get_section_by_name (abfd
, name
);
3690 /* Return the section to which RELOC_SEC applies. */
3693 elf_get_reloc_section (asection
*reloc_sec
)
3698 const struct elf_backend_data
*bed
;
3700 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3701 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3704 /* We look up the section the relocs apply to by name. */
3705 name
= reloc_sec
->name
;
3706 if (strncmp (name
, ".rel", 4) != 0)
3709 if (type
== SHT_RELA
&& *name
++ != 'a')
3712 abfd
= reloc_sec
->owner
;
3713 bed
= get_elf_backend_data (abfd
);
3714 return bed
->get_reloc_section (abfd
, name
);
3717 /* Assign all ELF section numbers. The dummy first section is handled here
3718 too. The link/info pointers for the standard section types are filled
3719 in here too, while we're at it. */
3722 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3724 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3726 unsigned int section_number
;
3727 Elf_Internal_Shdr
**i_shdrp
;
3728 struct bfd_elf_section_data
*d
;
3729 bfd_boolean need_symtab
;
3734 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3736 /* SHT_GROUP sections are in relocatable files only. */
3737 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3739 size_t reloc_count
= 0;
3741 /* Put SHT_GROUP sections first. */
3742 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3744 d
= elf_section_data (sec
);
3746 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3748 if (sec
->flags
& SEC_LINKER_CREATED
)
3750 /* Remove the linker created SHT_GROUP sections. */
3751 bfd_section_list_remove (abfd
, sec
);
3752 abfd
->section_count
--;
3755 d
->this_idx
= section_number
++;
3758 /* Count relocations. */
3759 reloc_count
+= sec
->reloc_count
;
3762 /* Clear HAS_RELOC if there are no relocations. */
3763 if (reloc_count
== 0)
3764 abfd
->flags
&= ~HAS_RELOC
;
3767 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3769 d
= elf_section_data (sec
);
3771 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3772 d
->this_idx
= section_number
++;
3773 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3774 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3777 d
->rel
.idx
= section_number
++;
3778 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3779 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3786 d
->rela
.idx
= section_number
++;
3787 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3788 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3794 need_symtab
= (bfd_get_symcount (abfd
) > 0
3795 || (link_info
== NULL
3796 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3800 elf_onesymtab (abfd
) = section_number
++;
3801 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3802 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3804 elf_section_list
*entry
;
3806 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3808 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3809 entry
->ndx
= section_number
++;
3810 elf_symtab_shndx_list (abfd
) = entry
;
3812 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3813 ".symtab_shndx", FALSE
);
3814 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3817 elf_strtab_sec (abfd
) = section_number
++;
3818 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3821 elf_shstrtab_sec (abfd
) = section_number
++;
3822 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3823 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3825 if (section_number
>= SHN_LORESERVE
)
3827 /* xgettext:c-format */
3828 _bfd_error_handler (_("%pB: too many sections: %u"),
3829 abfd
, section_number
);
3833 elf_numsections (abfd
) = section_number
;
3834 elf_elfheader (abfd
)->e_shnum
= section_number
;
3836 /* Set up the list of section header pointers, in agreement with the
3838 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3839 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3840 if (i_shdrp
== NULL
)
3843 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3844 sizeof (Elf_Internal_Shdr
));
3845 if (i_shdrp
[0] == NULL
)
3847 bfd_release (abfd
, i_shdrp
);
3851 elf_elfsections (abfd
) = i_shdrp
;
3853 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3856 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3857 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3859 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3860 BFD_ASSERT (entry
!= NULL
);
3861 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3862 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3864 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3865 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3868 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3872 d
= elf_section_data (sec
);
3874 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3875 if (d
->rel
.idx
!= 0)
3876 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3877 if (d
->rela
.idx
!= 0)
3878 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3880 /* Fill in the sh_link and sh_info fields while we're at it. */
3882 /* sh_link of a reloc section is the section index of the symbol
3883 table. sh_info is the section index of the section to which
3884 the relocation entries apply. */
3885 if (d
->rel
.idx
!= 0)
3887 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3888 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3889 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3891 if (d
->rela
.idx
!= 0)
3893 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3894 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3895 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3898 /* We need to set up sh_link for SHF_LINK_ORDER. */
3899 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3901 s
= elf_linked_to_section (sec
);
3904 /* elf_linked_to_section points to the input section. */
3905 if (link_info
!= NULL
)
3907 /* Check discarded linkonce section. */
3908 if (discarded_section (s
))
3912 /* xgettext:c-format */
3913 (_("%pB: sh_link of section `%pA' points to"
3914 " discarded section `%pA' of `%pB'"),
3915 abfd
, d
->this_hdr
.bfd_section
,
3917 /* Point to the kept section if it has the same
3918 size as the discarded one. */
3919 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3922 bfd_set_error (bfd_error_bad_value
);
3928 s
= s
->output_section
;
3929 BFD_ASSERT (s
!= NULL
);
3933 /* Handle objcopy. */
3934 if (s
->output_section
== NULL
)
3937 /* xgettext:c-format */
3938 (_("%pB: sh_link of section `%pA' points to"
3939 " removed section `%pA' of `%pB'"),
3940 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3941 bfd_set_error (bfd_error_bad_value
);
3944 s
= s
->output_section
;
3946 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3951 The Intel C compiler generates SHT_IA_64_UNWIND with
3952 SHF_LINK_ORDER. But it doesn't set the sh_link or
3953 sh_info fields. Hence we could get the situation
3955 const struct elf_backend_data
*bed
3956 = get_elf_backend_data (abfd
);
3957 bed
->link_order_error_handler
3958 /* xgettext:c-format */
3959 (_("%pB: warning: sh_link not set for section `%pA'"),
3964 switch (d
->this_hdr
.sh_type
)
3968 /* A reloc section which we are treating as a normal BFD
3969 section. sh_link is the section index of the symbol
3970 table. sh_info is the section index of the section to
3971 which the relocation entries apply. We assume that an
3972 allocated reloc section uses the dynamic symbol table.
3973 FIXME: How can we be sure? */
3974 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3976 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3978 s
= elf_get_reloc_section (sec
);
3981 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3982 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3987 /* We assume that a section named .stab*str is a stabs
3988 string section. We look for a section with the same name
3989 but without the trailing ``str'', and set its sh_link
3990 field to point to this section. */
3991 if (CONST_STRNEQ (sec
->name
, ".stab")
3992 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3997 len
= strlen (sec
->name
);
3998 alc
= (char *) bfd_malloc (len
- 2);
4001 memcpy (alc
, sec
->name
, len
- 3);
4002 alc
[len
- 3] = '\0';
4003 s
= bfd_get_section_by_name (abfd
, alc
);
4007 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
4009 /* This is a .stab section. */
4010 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
4017 case SHT_GNU_verneed
:
4018 case SHT_GNU_verdef
:
4019 /* sh_link is the section header index of the string table
4020 used for the dynamic entries, or the symbol table, or the
4022 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4024 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4027 case SHT_GNU_LIBLIST
:
4028 /* sh_link is the section header index of the prelink library
4029 list used for the dynamic entries, or the symbol table, or
4030 the version strings. */
4031 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4032 ? ".dynstr" : ".gnu.libstr");
4034 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4039 case SHT_GNU_versym
:
4040 /* sh_link is the section header index of the symbol table
4041 this hash table or version table is for. */
4042 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4044 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4048 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4052 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4053 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4054 debug section name from .debug_* to .zdebug_* if needed. */
4060 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4062 /* If the backend has a special mapping, use it. */
4063 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4064 if (bed
->elf_backend_sym_is_global
)
4065 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4067 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4068 || bfd_is_und_section (bfd_asymbol_section (sym
))
4069 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4072 /* Filter global symbols of ABFD to include in the import library. All
4073 SYMCOUNT symbols of ABFD can be examined from their pointers in
4074 SYMS. Pointers of symbols to keep should be stored contiguously at
4075 the beginning of that array.
4077 Returns the number of symbols to keep. */
4080 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4081 asymbol
**syms
, long symcount
)
4083 long src_count
, dst_count
= 0;
4085 for (src_count
= 0; src_count
< symcount
; src_count
++)
4087 asymbol
*sym
= syms
[src_count
];
4088 char *name
= (char *) bfd_asymbol_name (sym
);
4089 struct bfd_link_hash_entry
*h
;
4091 if (!sym_is_global (abfd
, sym
))
4094 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4097 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4099 if (h
->linker_def
|| h
->ldscript_def
)
4102 syms
[dst_count
++] = sym
;
4105 syms
[dst_count
] = NULL
;
4110 /* Don't output section symbols for sections that are not going to be
4111 output, that are duplicates or there is no BFD section. */
4114 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4116 elf_symbol_type
*type_ptr
;
4121 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4124 if (sym
->section
== NULL
)
4127 type_ptr
= elf_symbol_from (abfd
, sym
);
4128 return ((type_ptr
!= NULL
4129 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4130 && bfd_is_abs_section (sym
->section
))
4131 || !(sym
->section
->owner
== abfd
4132 || (sym
->section
->output_section
!= NULL
4133 && sym
->section
->output_section
->owner
== abfd
4134 && sym
->section
->output_offset
== 0)
4135 || bfd_is_abs_section (sym
->section
)));
4138 /* Map symbol from it's internal number to the external number, moving
4139 all local symbols to be at the head of the list. */
4142 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4144 unsigned int symcount
= bfd_get_symcount (abfd
);
4145 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4146 asymbol
**sect_syms
;
4147 unsigned int num_locals
= 0;
4148 unsigned int num_globals
= 0;
4149 unsigned int num_locals2
= 0;
4150 unsigned int num_globals2
= 0;
4151 unsigned int max_index
= 0;
4158 fprintf (stderr
, "elf_map_symbols\n");
4162 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4164 if (max_index
< asect
->index
)
4165 max_index
= asect
->index
;
4169 amt
= max_index
* sizeof (asymbol
*);
4170 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4171 if (sect_syms
== NULL
)
4173 elf_section_syms (abfd
) = sect_syms
;
4174 elf_num_section_syms (abfd
) = max_index
;
4176 /* Init sect_syms entries for any section symbols we have already
4177 decided to output. */
4178 for (idx
= 0; idx
< symcount
; idx
++)
4180 asymbol
*sym
= syms
[idx
];
4182 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4184 && !ignore_section_sym (abfd
, sym
)
4185 && !bfd_is_abs_section (sym
->section
))
4187 asection
*sec
= sym
->section
;
4189 if (sec
->owner
!= abfd
)
4190 sec
= sec
->output_section
;
4192 sect_syms
[sec
->index
] = syms
[idx
];
4196 /* Classify all of the symbols. */
4197 for (idx
= 0; idx
< symcount
; idx
++)
4199 if (sym_is_global (abfd
, syms
[idx
]))
4201 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4205 /* We will be adding a section symbol for each normal BFD section. Most
4206 sections will already have a section symbol in outsymbols, but
4207 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4208 at least in that case. */
4209 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4211 if (sect_syms
[asect
->index
] == NULL
)
4213 if (!sym_is_global (abfd
, asect
->symbol
))
4220 /* Now sort the symbols so the local symbols are first. */
4221 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4222 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4223 if (new_syms
== NULL
)
4226 for (idx
= 0; idx
< symcount
; idx
++)
4228 asymbol
*sym
= syms
[idx
];
4231 if (sym_is_global (abfd
, sym
))
4232 i
= num_locals
+ num_globals2
++;
4233 else if (!ignore_section_sym (abfd
, sym
))
4238 sym
->udata
.i
= i
+ 1;
4240 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4242 if (sect_syms
[asect
->index
] == NULL
)
4244 asymbol
*sym
= asect
->symbol
;
4247 sect_syms
[asect
->index
] = sym
;
4248 if (!sym_is_global (abfd
, sym
))
4251 i
= num_locals
+ num_globals2
++;
4253 sym
->udata
.i
= i
+ 1;
4257 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4259 *pnum_locals
= num_locals
;
4263 /* Align to the maximum file alignment that could be required for any
4264 ELF data structure. */
4266 static inline file_ptr
4267 align_file_position (file_ptr off
, int align
)
4269 return (off
+ align
- 1) & ~(align
- 1);
4272 /* Assign a file position to a section, optionally aligning to the
4273 required section alignment. */
4276 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4280 if (align
&& i_shdrp
->sh_addralign
> 1)
4281 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4282 i_shdrp
->sh_offset
= offset
;
4283 if (i_shdrp
->bfd_section
!= NULL
)
4284 i_shdrp
->bfd_section
->filepos
= offset
;
4285 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4286 offset
+= i_shdrp
->sh_size
;
4290 /* Compute the file positions we are going to put the sections at, and
4291 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4292 is not NULL, this is being called by the ELF backend linker. */
4295 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4296 struct bfd_link_info
*link_info
)
4298 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4299 struct fake_section_arg fsargs
;
4301 struct elf_strtab_hash
*strtab
= NULL
;
4302 Elf_Internal_Shdr
*shstrtab_hdr
;
4303 bfd_boolean need_symtab
;
4305 if (abfd
->output_has_begun
)
4308 /* Do any elf backend specific processing first. */
4309 if (bed
->elf_backend_begin_write_processing
)
4310 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4312 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4315 fsargs
.failed
= FALSE
;
4316 fsargs
.link_info
= link_info
;
4317 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4321 if (!assign_section_numbers (abfd
, link_info
))
4324 /* The backend linker builds symbol table information itself. */
4325 need_symtab
= (link_info
== NULL
4326 && (bfd_get_symcount (abfd
) > 0
4327 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4331 /* Non-zero if doing a relocatable link. */
4332 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4334 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4339 if (link_info
== NULL
)
4341 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4346 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4347 /* sh_name was set in init_file_header. */
4348 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4349 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4350 shstrtab_hdr
->sh_addr
= 0;
4351 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4352 shstrtab_hdr
->sh_entsize
= 0;
4353 shstrtab_hdr
->sh_link
= 0;
4354 shstrtab_hdr
->sh_info
= 0;
4355 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4356 shstrtab_hdr
->sh_addralign
= 1;
4358 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4364 Elf_Internal_Shdr
*hdr
;
4366 off
= elf_next_file_pos (abfd
);
4368 hdr
= & elf_symtab_hdr (abfd
);
4369 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4371 if (elf_symtab_shndx_list (abfd
) != NULL
)
4373 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4374 if (hdr
->sh_size
!= 0)
4375 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4376 /* FIXME: What about other symtab_shndx sections in the list ? */
4379 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4380 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4382 elf_next_file_pos (abfd
) = off
;
4384 /* Now that we know where the .strtab section goes, write it
4386 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4387 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4389 _bfd_elf_strtab_free (strtab
);
4392 abfd
->output_has_begun
= TRUE
;
4397 /* Make an initial estimate of the size of the program header. If we
4398 get the number wrong here, we'll redo section placement. */
4400 static bfd_size_type
4401 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4405 const struct elf_backend_data
*bed
;
4407 /* Assume we will need exactly two PT_LOAD segments: one for text
4408 and one for data. */
4411 s
= bfd_get_section_by_name (abfd
, ".interp");
4412 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4414 /* If we have a loadable interpreter section, we need a
4415 PT_INTERP segment. In this case, assume we also need a
4416 PT_PHDR segment, although that may not be true for all
4421 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4423 /* We need a PT_DYNAMIC segment. */
4427 if (info
!= NULL
&& info
->relro
)
4429 /* We need a PT_GNU_RELRO segment. */
4433 if (elf_eh_frame_hdr (abfd
))
4435 /* We need a PT_GNU_EH_FRAME segment. */
4439 if (elf_stack_flags (abfd
))
4441 /* We need a PT_GNU_STACK segment. */
4445 s
= bfd_get_section_by_name (abfd
,
4446 NOTE_GNU_PROPERTY_SECTION_NAME
);
4447 if (s
!= NULL
&& s
->size
!= 0)
4449 /* We need a PT_GNU_PROPERTY segment. */
4453 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4455 if ((s
->flags
& SEC_LOAD
) != 0
4456 && elf_section_type (s
) == SHT_NOTE
)
4458 unsigned int alignment_power
;
4459 /* We need a PT_NOTE segment. */
4461 /* Try to create just one PT_NOTE segment for all adjacent
4462 loadable SHT_NOTE sections. gABI requires that within a
4463 PT_NOTE segment (and also inside of each SHT_NOTE section)
4464 each note should have the same alignment. So we check
4465 whether the sections are correctly aligned. */
4466 alignment_power
= s
->alignment_power
;
4467 while (s
->next
!= NULL
4468 && s
->next
->alignment_power
== alignment_power
4469 && (s
->next
->flags
& SEC_LOAD
) != 0
4470 && elf_section_type (s
->next
) == SHT_NOTE
)
4475 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4477 if (s
->flags
& SEC_THREAD_LOCAL
)
4479 /* We need a PT_TLS segment. */
4485 bed
= get_elf_backend_data (abfd
);
4487 if ((abfd
->flags
& D_PAGED
) != 0
4488 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4490 /* Add a PT_GNU_MBIND segment for each mbind section. */
4491 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4492 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4493 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4495 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4498 /* xgettext:c-format */
4499 (_("%pB: GNU_MBIND section `%pA' has invalid "
4500 "sh_info field: %d"),
4501 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4504 /* Align mbind section to page size. */
4505 if (s
->alignment_power
< page_align_power
)
4506 s
->alignment_power
= page_align_power
;
4511 /* Let the backend count up any program headers it might need. */
4512 if (bed
->elf_backend_additional_program_headers
)
4516 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4522 return segs
* bed
->s
->sizeof_phdr
;
4525 /* Find the segment that contains the output_section of section. */
4528 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4530 struct elf_segment_map
*m
;
4531 Elf_Internal_Phdr
*p
;
4533 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4539 for (i
= m
->count
- 1; i
>= 0; i
--)
4540 if (m
->sections
[i
] == section
)
4547 /* Create a mapping from a set of sections to a program segment. */
4549 static struct elf_segment_map
*
4550 make_mapping (bfd
*abfd
,
4551 asection
**sections
,
4556 struct elf_segment_map
*m
;
4561 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4562 amt
+= (to
- from
) * sizeof (asection
*);
4563 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4567 m
->p_type
= PT_LOAD
;
4568 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4569 m
->sections
[i
- from
] = *hdrpp
;
4570 m
->count
= to
- from
;
4572 if (from
== 0 && phdr
)
4574 /* Include the headers in the first PT_LOAD segment. */
4575 m
->includes_filehdr
= 1;
4576 m
->includes_phdrs
= 1;
4582 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4585 struct elf_segment_map
*
4586 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4588 struct elf_segment_map
*m
;
4590 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4591 sizeof (struct elf_segment_map
));
4595 m
->p_type
= PT_DYNAMIC
;
4597 m
->sections
[0] = dynsec
;
4602 /* Possibly add or remove segments from the segment map. */
4605 elf_modify_segment_map (bfd
*abfd
,
4606 struct bfd_link_info
*info
,
4607 bfd_boolean remove_empty_load
)
4609 struct elf_segment_map
**m
;
4610 const struct elf_backend_data
*bed
;
4612 /* The placement algorithm assumes that non allocated sections are
4613 not in PT_LOAD segments. We ensure this here by removing such
4614 sections from the segment map. We also remove excluded
4615 sections. Finally, any PT_LOAD segment without sections is
4617 m
= &elf_seg_map (abfd
);
4620 unsigned int i
, new_count
;
4622 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4624 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4625 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4626 || (*m
)->p_type
!= PT_LOAD
))
4628 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4632 (*m
)->count
= new_count
;
4634 if (remove_empty_load
4635 && (*m
)->p_type
== PT_LOAD
4637 && !(*m
)->includes_phdrs
)
4643 bed
= get_elf_backend_data (abfd
);
4644 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4646 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4653 #define IS_TBSS(s) \
4654 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4656 /* Set up a mapping from BFD sections to program segments. */
4659 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4662 struct elf_segment_map
*m
;
4663 asection
**sections
= NULL
;
4664 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4665 bfd_boolean no_user_phdrs
;
4667 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4670 info
->user_phdrs
= !no_user_phdrs
;
4672 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4676 struct elf_segment_map
*mfirst
;
4677 struct elf_segment_map
**pm
;
4680 unsigned int hdr_index
;
4681 bfd_vma maxpagesize
;
4683 bfd_boolean phdr_in_segment
;
4684 bfd_boolean writable
;
4685 bfd_boolean executable
;
4686 unsigned int tls_count
= 0;
4687 asection
*first_tls
= NULL
;
4688 asection
*first_mbind
= NULL
;
4689 asection
*dynsec
, *eh_frame_hdr
;
4691 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4692 bfd_size_type phdr_size
; /* Octets/bytes. */
4693 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4695 /* Select the allocated sections, and sort them. */
4697 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4698 sections
= (asection
**) bfd_malloc (amt
);
4699 if (sections
== NULL
)
4702 /* Calculate top address, avoiding undefined behaviour of shift
4703 left operator when shift count is equal to size of type
4705 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4706 addr_mask
= (addr_mask
<< 1) + 1;
4709 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4711 if ((s
->flags
& SEC_ALLOC
) != 0)
4713 /* target_index is unused until bfd_elf_final_link
4714 starts output of section symbols. Use it to make
4716 s
->target_index
= i
;
4719 /* A wrapping section potentially clashes with header. */
4720 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4721 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4724 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4727 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4729 phdr_size
= elf_program_header_size (abfd
);
4730 if (phdr_size
== (bfd_size_type
) -1)
4731 phdr_size
= get_program_header_size (abfd
, info
);
4732 phdr_size
+= bed
->s
->sizeof_ehdr
;
4733 /* phdr_size is compared to LMA values which are in bytes. */
4735 maxpagesize
= bed
->maxpagesize
;
4736 if (maxpagesize
== 0)
4738 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4740 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4741 >= (phdr_size
& (maxpagesize
- 1))))
4742 /* For compatibility with old scripts that may not be using
4743 SIZEOF_HEADERS, add headers when it looks like space has
4744 been left for them. */
4745 phdr_in_segment
= TRUE
;
4747 /* Build the mapping. */
4751 /* If we have a .interp section, then create a PT_PHDR segment for
4752 the program headers and a PT_INTERP segment for the .interp
4754 s
= bfd_get_section_by_name (abfd
, ".interp");
4755 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4757 amt
= sizeof (struct elf_segment_map
);
4758 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4762 m
->p_type
= PT_PHDR
;
4764 m
->p_flags_valid
= 1;
4765 m
->includes_phdrs
= 1;
4766 phdr_in_segment
= TRUE
;
4770 amt
= sizeof (struct elf_segment_map
);
4771 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4775 m
->p_type
= PT_INTERP
;
4783 /* Look through the sections. We put sections in the same program
4784 segment when the start of the second section can be placed within
4785 a few bytes of the end of the first section. */
4791 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4793 && (dynsec
->flags
& SEC_LOAD
) == 0)
4796 if ((abfd
->flags
& D_PAGED
) == 0)
4797 phdr_in_segment
= FALSE
;
4799 /* Deal with -Ttext or something similar such that the first section
4800 is not adjacent to the program headers. This is an
4801 approximation, since at this point we don't know exactly how many
4802 program headers we will need. */
4803 if (phdr_in_segment
&& count
> 0)
4805 bfd_vma phdr_lma
; /* Bytes. */
4806 bfd_boolean separate_phdr
= FALSE
;
4808 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4810 && info
->separate_code
4811 && (sections
[0]->flags
& SEC_CODE
) != 0)
4813 /* If data sections should be separate from code and
4814 thus not executable, and the first section is
4815 executable then put the file and program headers in
4816 their own PT_LOAD. */
4817 separate_phdr
= TRUE
;
4818 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4819 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4821 /* The file and program headers are currently on the
4822 same page as the first section. Put them on the
4823 previous page if we can. */
4824 if (phdr_lma
>= maxpagesize
)
4825 phdr_lma
-= maxpagesize
;
4827 separate_phdr
= FALSE
;
4830 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4831 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4832 /* If file and program headers would be placed at the end
4833 of memory then it's probably better to omit them. */
4834 phdr_in_segment
= FALSE
;
4835 else if (phdr_lma
< wrap_to
)
4836 /* If a section wraps around to where we'll be placing
4837 file and program headers, then the headers will be
4839 phdr_in_segment
= FALSE
;
4840 else if (separate_phdr
)
4842 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4845 m
->p_paddr
= phdr_lma
* opb
;
4847 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4848 m
->p_paddr_valid
= 1;
4851 phdr_in_segment
= FALSE
;
4855 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4858 bfd_boolean new_segment
;
4862 /* See if this section and the last one will fit in the same
4865 if (last_hdr
== NULL
)
4867 /* If we don't have a segment yet, then we don't need a new
4868 one (we build the last one after this loop). */
4869 new_segment
= FALSE
;
4871 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4873 /* If this section has a different relation between the
4874 virtual address and the load address, then we need a new
4878 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4879 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4881 /* If this section has a load address that makes it overlap
4882 the previous section, then we need a new segment. */
4885 else if ((abfd
->flags
& D_PAGED
) != 0
4886 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4887 == (hdr
->lma
& -maxpagesize
)))
4889 /* If we are demand paged then we can't map two disk
4890 pages onto the same memory page. */
4891 new_segment
= FALSE
;
4893 /* In the next test we have to be careful when last_hdr->lma is close
4894 to the end of the address space. If the aligned address wraps
4895 around to the start of the address space, then there are no more
4896 pages left in memory and it is OK to assume that the current
4897 section can be included in the current segment. */
4898 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4899 + maxpagesize
> last_hdr
->lma
)
4900 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4901 + maxpagesize
<= hdr
->lma
))
4903 /* If putting this section in this segment would force us to
4904 skip a page in the segment, then we need a new segment. */
4907 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4908 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4910 /* We don't want to put a loaded section after a
4911 nonloaded (ie. bss style) section in the same segment
4912 as that will force the non-loaded section to be loaded.
4913 Consider .tbss sections as loaded for this purpose. */
4916 else if ((abfd
->flags
& D_PAGED
) == 0)
4918 /* If the file is not demand paged, which means that we
4919 don't require the sections to be correctly aligned in the
4920 file, then there is no other reason for a new segment. */
4921 new_segment
= FALSE
;
4923 else if (info
!= NULL
4924 && info
->separate_code
4925 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4930 && (hdr
->flags
& SEC_READONLY
) == 0)
4932 /* We don't want to put a writable section in a read only
4938 /* Otherwise, we can use the same segment. */
4939 new_segment
= FALSE
;
4942 /* Allow interested parties a chance to override our decision. */
4943 if (last_hdr
!= NULL
4945 && info
->callbacks
->override_segment_assignment
!= NULL
)
4947 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4953 if ((hdr
->flags
& SEC_READONLY
) == 0)
4955 if ((hdr
->flags
& SEC_CODE
) != 0)
4958 /* .tbss sections effectively have zero size. */
4959 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4963 /* We need a new program segment. We must create a new program
4964 header holding all the sections from hdr_index until hdr. */
4966 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4973 if ((hdr
->flags
& SEC_READONLY
) == 0)
4978 if ((hdr
->flags
& SEC_CODE
) == 0)
4984 /* .tbss sections effectively have zero size. */
4985 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4987 phdr_in_segment
= FALSE
;
4990 /* Create a final PT_LOAD program segment, but not if it's just
4992 if (last_hdr
!= NULL
4993 && (i
- hdr_index
!= 1
4994 || !IS_TBSS (last_hdr
)))
4996 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
5004 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5007 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5014 /* For each batch of consecutive loadable SHT_NOTE sections,
5015 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5016 because if we link together nonloadable .note sections and
5017 loadable .note sections, we will generate two .note sections
5018 in the output file. */
5019 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5021 if ((s
->flags
& SEC_LOAD
) != 0
5022 && elf_section_type (s
) == SHT_NOTE
)
5025 unsigned int alignment_power
= s
->alignment_power
;
5028 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5030 if (s2
->next
->alignment_power
== alignment_power
5031 && (s2
->next
->flags
& SEC_LOAD
) != 0
5032 && elf_section_type (s2
->next
) == SHT_NOTE
5033 && align_power (s2
->lma
+ s2
->size
/ opb
,
5040 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5041 amt
+= count
* sizeof (asection
*);
5042 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5046 m
->p_type
= PT_NOTE
;
5050 m
->sections
[m
->count
- count
--] = s
;
5051 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5054 m
->sections
[m
->count
- 1] = s
;
5055 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5059 if (s
->flags
& SEC_THREAD_LOCAL
)
5065 if (first_mbind
== NULL
5066 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5070 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5073 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5074 amt
+= tls_count
* sizeof (asection
*);
5075 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5080 m
->count
= tls_count
;
5081 /* Mandated PF_R. */
5083 m
->p_flags_valid
= 1;
5085 for (i
= 0; i
< tls_count
; ++i
)
5087 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5090 (_("%pB: TLS sections are not adjacent:"), abfd
);
5093 while (i
< tls_count
)
5095 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5097 _bfd_error_handler (_(" TLS: %pA"), s
);
5101 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5104 bfd_set_error (bfd_error_bad_value
);
5116 && (abfd
->flags
& D_PAGED
) != 0
5117 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5118 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5119 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5120 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5122 /* Mandated PF_R. */
5123 unsigned long p_flags
= PF_R
;
5124 if ((s
->flags
& SEC_READONLY
) == 0)
5126 if ((s
->flags
& SEC_CODE
) != 0)
5129 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5130 m
= bfd_zalloc (abfd
, amt
);
5134 m
->p_type
= (PT_GNU_MBIND_LO
5135 + elf_section_data (s
)->this_hdr
.sh_info
);
5137 m
->p_flags_valid
= 1;
5139 m
->p_flags
= p_flags
;
5145 s
= bfd_get_section_by_name (abfd
,
5146 NOTE_GNU_PROPERTY_SECTION_NAME
);
5147 if (s
!= NULL
&& s
->size
!= 0)
5149 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5150 m
= bfd_zalloc (abfd
, amt
);
5154 m
->p_type
= PT_GNU_PROPERTY
;
5156 m
->p_flags_valid
= 1;
5163 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5165 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5166 if (eh_frame_hdr
!= NULL
5167 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5169 amt
= sizeof (struct elf_segment_map
);
5170 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5174 m
->p_type
= PT_GNU_EH_FRAME
;
5176 m
->sections
[0] = eh_frame_hdr
->output_section
;
5182 if (elf_stack_flags (abfd
))
5184 amt
= sizeof (struct elf_segment_map
);
5185 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5189 m
->p_type
= PT_GNU_STACK
;
5190 m
->p_flags
= elf_stack_flags (abfd
);
5191 m
->p_align
= bed
->stack_align
;
5192 m
->p_flags_valid
= 1;
5193 m
->p_align_valid
= m
->p_align
!= 0;
5194 if (info
->stacksize
> 0)
5196 m
->p_size
= info
->stacksize
;
5197 m
->p_size_valid
= 1;
5204 if (info
!= NULL
&& info
->relro
)
5206 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5208 if (m
->p_type
== PT_LOAD
5210 && m
->sections
[0]->vma
>= info
->relro_start
5211 && m
->sections
[0]->vma
< info
->relro_end
)
5214 while (--i
!= (unsigned) -1)
5216 if (m
->sections
[i
]->size
> 0
5217 && (m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5218 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5222 if (i
!= (unsigned) -1)
5227 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5230 amt
= sizeof (struct elf_segment_map
);
5231 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5235 m
->p_type
= PT_GNU_RELRO
;
5242 elf_seg_map (abfd
) = mfirst
;
5245 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5248 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5250 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5255 if (sections
!= NULL
)
5260 /* Sort sections by address. */
5263 elf_sort_sections (const void *arg1
, const void *arg2
)
5265 const asection
*sec1
= *(const asection
**) arg1
;
5266 const asection
*sec2
= *(const asection
**) arg2
;
5267 bfd_size_type size1
, size2
;
5269 /* Sort by LMA first, since this is the address used to
5270 place the section into a segment. */
5271 if (sec1
->lma
< sec2
->lma
)
5273 else if (sec1
->lma
> sec2
->lma
)
5276 /* Then sort by VMA. Normally the LMA and the VMA will be
5277 the same, and this will do nothing. */
5278 if (sec1
->vma
< sec2
->vma
)
5280 else if (sec1
->vma
> sec2
->vma
)
5283 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5285 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5292 else if (TOEND (sec2
))
5297 /* Sort by size, to put zero sized sections
5298 before others at the same address. */
5300 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5301 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5308 return sec1
->target_index
- sec2
->target_index
;
5311 /* This qsort comparison functions sorts PT_LOAD segments first and
5312 by p_paddr, for assign_file_positions_for_load_sections. */
5315 elf_sort_segments (const void *arg1
, const void *arg2
)
5317 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5318 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5320 if (m1
->p_type
!= m2
->p_type
)
5322 if (m1
->p_type
== PT_NULL
)
5324 if (m2
->p_type
== PT_NULL
)
5326 return m1
->p_type
< m2
->p_type
? -1 : 1;
5328 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5329 return m1
->includes_filehdr
? -1 : 1;
5330 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5331 return m1
->no_sort_lma
? -1 : 1;
5332 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5334 bfd_vma lma1
, lma2
; /* Octets. */
5336 if (m1
->p_paddr_valid
)
5338 else if (m1
->count
!= 0)
5340 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5342 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5345 if (m2
->p_paddr_valid
)
5347 else if (m2
->count
!= 0)
5349 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5351 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5354 return lma1
< lma2
? -1 : 1;
5356 if (m1
->idx
!= m2
->idx
)
5357 return m1
->idx
< m2
->idx
? -1 : 1;
5361 /* Ian Lance Taylor writes:
5363 We shouldn't be using % with a negative signed number. That's just
5364 not good. We have to make sure either that the number is not
5365 negative, or that the number has an unsigned type. When the types
5366 are all the same size they wind up as unsigned. When file_ptr is a
5367 larger signed type, the arithmetic winds up as signed long long,
5370 What we're trying to say here is something like ``increase OFF by
5371 the least amount that will cause it to be equal to the VMA modulo
5373 /* In other words, something like:
5375 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5376 off_offset = off % bed->maxpagesize;
5377 if (vma_offset < off_offset)
5378 adjustment = vma_offset + bed->maxpagesize - off_offset;
5380 adjustment = vma_offset - off_offset;
5382 which can be collapsed into the expression below. */
5385 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5387 /* PR binutils/16199: Handle an alignment of zero. */
5388 if (maxpagesize
== 0)
5390 return ((vma
- off
) % maxpagesize
);
5394 print_segment_map (const struct elf_segment_map
*m
)
5397 const char *pt
= get_segment_type (m
->p_type
);
5402 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5403 sprintf (buf
, "LOPROC+%7.7x",
5404 (unsigned int) (m
->p_type
- PT_LOPROC
));
5405 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5406 sprintf (buf
, "LOOS+%7.7x",
5407 (unsigned int) (m
->p_type
- PT_LOOS
));
5409 snprintf (buf
, sizeof (buf
), "%8.8x",
5410 (unsigned int) m
->p_type
);
5414 fprintf (stderr
, "%s:", pt
);
5415 for (j
= 0; j
< m
->count
; j
++)
5416 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5422 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5427 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5429 buf
= bfd_zmalloc (len
);
5432 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5437 /* Assign file positions to the sections based on the mapping from
5438 sections to segments. This function also sets up some fields in
5442 assign_file_positions_for_load_sections (bfd
*abfd
,
5443 struct bfd_link_info
*link_info
)
5445 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5446 struct elf_segment_map
*m
;
5447 struct elf_segment_map
*phdr_load_seg
;
5448 Elf_Internal_Phdr
*phdrs
;
5449 Elf_Internal_Phdr
*p
;
5450 file_ptr off
; /* Octets. */
5451 bfd_size_type maxpagesize
;
5452 unsigned int alloc
, actual
;
5454 struct elf_segment_map
**sorted_seg_map
;
5455 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5457 if (link_info
== NULL
5458 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5462 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5467 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5468 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5472 /* PR binutils/12467. */
5473 elf_elfheader (abfd
)->e_phoff
= 0;
5474 elf_elfheader (abfd
)->e_phentsize
= 0;
5477 elf_elfheader (abfd
)->e_phnum
= alloc
;
5479 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5482 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5486 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5487 BFD_ASSERT (elf_program_header_size (abfd
)
5488 == actual
* bed
->s
->sizeof_phdr
);
5489 BFD_ASSERT (actual
>= alloc
);
5494 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5498 /* We're writing the size in elf_program_header_size (abfd),
5499 see assign_file_positions_except_relocs, so make sure we have
5500 that amount allocated, with trailing space cleared.
5501 The variable alloc contains the computed need, while
5502 elf_program_header_size (abfd) contains the size used for the
5504 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5505 where the layout is forced to according to a larger size in the
5506 last iterations for the testcase ld-elf/header. */
5507 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5508 + alloc
* sizeof (*sorted_seg_map
)));
5509 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5510 elf_tdata (abfd
)->phdr
= phdrs
;
5514 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5516 sorted_seg_map
[j
] = m
;
5517 /* If elf_segment_map is not from map_sections_to_segments, the
5518 sections may not be correctly ordered. NOTE: sorting should
5519 not be done to the PT_NOTE section of a corefile, which may
5520 contain several pseudo-sections artificially created by bfd.
5521 Sorting these pseudo-sections breaks things badly. */
5523 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5524 && m
->p_type
== PT_NOTE
))
5526 for (i
= 0; i
< m
->count
; i
++)
5527 m
->sections
[i
]->target_index
= i
;
5528 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5533 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5537 if ((abfd
->flags
& D_PAGED
) != 0)
5538 maxpagesize
= bed
->maxpagesize
;
5540 /* Sections must map to file offsets past the ELF file header. */
5541 off
= bed
->s
->sizeof_ehdr
;
5542 /* And if one of the PT_LOAD headers doesn't include the program
5543 headers then we'll be mapping program headers in the usual
5544 position after the ELF file header. */
5545 phdr_load_seg
= NULL
;
5546 for (j
= 0; j
< alloc
; j
++)
5548 m
= sorted_seg_map
[j
];
5549 if (m
->p_type
!= PT_LOAD
)
5551 if (m
->includes_phdrs
)
5557 if (phdr_load_seg
== NULL
)
5558 off
+= actual
* bed
->s
->sizeof_phdr
;
5560 for (j
= 0; j
< alloc
; j
++)
5563 bfd_vma off_adjust
; /* Octets. */
5564 bfd_boolean no_contents
;
5566 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5567 number of sections with contents contributing to both p_filesz
5568 and p_memsz, followed by a number of sections with no contents
5569 that just contribute to p_memsz. In this loop, OFF tracks next
5570 available file offset for PT_LOAD and PT_NOTE segments. */
5571 m
= sorted_seg_map
[j
];
5573 p
->p_type
= m
->p_type
;
5574 p
->p_flags
= m
->p_flags
;
5577 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5579 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5581 if (m
->p_paddr_valid
)
5582 p
->p_paddr
= m
->p_paddr
;
5583 else if (m
->count
== 0)
5586 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5588 if (p
->p_type
== PT_LOAD
5589 && (abfd
->flags
& D_PAGED
) != 0)
5591 /* p_align in demand paged PT_LOAD segments effectively stores
5592 the maximum page size. When copying an executable with
5593 objcopy, we set m->p_align from the input file. Use this
5594 value for maxpagesize rather than bed->maxpagesize, which
5595 may be different. Note that we use maxpagesize for PT_TLS
5596 segment alignment later in this function, so we are relying
5597 on at least one PT_LOAD segment appearing before a PT_TLS
5599 if (m
->p_align_valid
)
5600 maxpagesize
= m
->p_align
;
5602 p
->p_align
= maxpagesize
;
5604 else if (m
->p_align_valid
)
5605 p
->p_align
= m
->p_align
;
5606 else if (m
->count
== 0)
5607 p
->p_align
= 1 << bed
->s
->log_file_align
;
5609 if (m
== phdr_load_seg
)
5611 if (!m
->includes_filehdr
)
5613 off
+= actual
* bed
->s
->sizeof_phdr
;
5616 no_contents
= FALSE
;
5618 if (p
->p_type
== PT_LOAD
5621 bfd_size_type align
; /* Bytes. */
5622 unsigned int align_power
= 0;
5624 if (m
->p_align_valid
)
5628 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5630 unsigned int secalign
;
5632 secalign
= bfd_section_alignment (*secpp
);
5633 if (secalign
> align_power
)
5634 align_power
= secalign
;
5636 align
= (bfd_size_type
) 1 << align_power
;
5637 if (align
< maxpagesize
)
5638 align
= maxpagesize
;
5641 for (i
= 0; i
< m
->count
; i
++)
5642 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5643 /* If we aren't making room for this section, then
5644 it must be SHT_NOBITS regardless of what we've
5645 set via struct bfd_elf_special_section. */
5646 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5648 /* Find out whether this segment contains any loadable
5651 for (i
= 0; i
< m
->count
; i
++)
5652 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5654 no_contents
= FALSE
;
5658 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5660 /* Broken hardware and/or kernel require that files do not
5661 map the same page with different permissions on some hppa
5664 && (abfd
->flags
& D_PAGED
) != 0
5665 && bed
->no_page_alias
5666 && (off
& (maxpagesize
- 1)) != 0
5667 && ((off
& -maxpagesize
)
5668 == ((off
+ off_adjust
) & -maxpagesize
)))
5669 off_adjust
+= maxpagesize
;
5673 /* We shouldn't need to align the segment on disk since
5674 the segment doesn't need file space, but the gABI
5675 arguably requires the alignment and glibc ld.so
5676 checks it. So to comply with the alignment
5677 requirement but not waste file space, we adjust
5678 p_offset for just this segment. (OFF_ADJUST is
5679 subtracted from OFF later.) This may put p_offset
5680 past the end of file, but that shouldn't matter. */
5685 /* Make sure the .dynamic section is the first section in the
5686 PT_DYNAMIC segment. */
5687 else if (p
->p_type
== PT_DYNAMIC
5689 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5692 (_("%pB: The first section in the PT_DYNAMIC segment"
5693 " is not the .dynamic section"),
5695 bfd_set_error (bfd_error_bad_value
);
5698 /* Set the note section type to SHT_NOTE. */
5699 else if (p
->p_type
== PT_NOTE
)
5700 for (i
= 0; i
< m
->count
; i
++)
5701 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5703 if (m
->includes_filehdr
)
5705 if (!m
->p_flags_valid
)
5707 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5708 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5709 if (p
->p_type
== PT_LOAD
)
5713 if (p
->p_vaddr
< (bfd_vma
) off
5714 || (!m
->p_paddr_valid
5715 && p
->p_paddr
< (bfd_vma
) off
))
5718 (_("%pB: not enough room for program headers,"
5719 " try linking with -N"),
5721 bfd_set_error (bfd_error_bad_value
);
5725 if (!m
->p_paddr_valid
)
5729 else if (sorted_seg_map
[0]->includes_filehdr
)
5731 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5732 p
->p_vaddr
= filehdr
->p_vaddr
;
5733 if (!m
->p_paddr_valid
)
5734 p
->p_paddr
= filehdr
->p_paddr
;
5738 if (m
->includes_phdrs
)
5740 if (!m
->p_flags_valid
)
5742 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5743 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5744 if (!m
->includes_filehdr
)
5746 if (p
->p_type
== PT_LOAD
)
5748 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5751 p
->p_vaddr
-= off
- p
->p_offset
;
5752 if (!m
->p_paddr_valid
)
5753 p
->p_paddr
-= off
- p
->p_offset
;
5756 else if (phdr_load_seg
!= NULL
)
5758 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5759 bfd_vma phdr_off
= 0; /* Octets. */
5760 if (phdr_load_seg
->includes_filehdr
)
5761 phdr_off
= bed
->s
->sizeof_ehdr
;
5762 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5763 if (!m
->p_paddr_valid
)
5764 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5765 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5768 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5772 if (p
->p_type
== PT_LOAD
5773 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5775 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5780 /* Put meaningless p_offset for PT_LOAD segments
5781 without file contents somewhere within the first
5782 page, in an attempt to not point past EOF. */
5783 bfd_size_type align
= maxpagesize
;
5784 if (align
< p
->p_align
)
5788 p
->p_offset
= off
% align
;
5793 file_ptr adjust
; /* Octets. */
5795 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5797 p
->p_filesz
+= adjust
;
5798 p
->p_memsz
+= adjust
;
5802 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5803 maps. Set filepos for sections in PT_LOAD segments, and in
5804 core files, for sections in PT_NOTE segments.
5805 assign_file_positions_for_non_load_sections will set filepos
5806 for other sections and update p_filesz for other segments. */
5807 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5810 bfd_size_type align
;
5811 Elf_Internal_Shdr
*this_hdr
;
5814 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5815 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5817 if ((p
->p_type
== PT_LOAD
5818 || p
->p_type
== PT_TLS
)
5819 && (this_hdr
->sh_type
!= SHT_NOBITS
5820 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5821 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5822 || p
->p_type
== PT_TLS
))))
5824 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5825 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5826 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5827 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5831 || p_end
< p_start
))
5834 /* xgettext:c-format */
5835 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5836 abfd
, sec
, (uint64_t) s_start
/ opb
,
5837 (uint64_t) p_end
/ opb
);
5839 sec
->lma
= p_end
/ opb
;
5841 p
->p_memsz
+= adjust
;
5843 if (p
->p_type
== PT_LOAD
)
5845 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5848 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5850 /* We have a PROGBITS section following NOBITS ones.
5851 Allocate file space for the NOBITS section(s) and
5853 adjust
= p
->p_memsz
- p
->p_filesz
;
5854 if (!write_zeros (abfd
, off
, adjust
))
5858 /* We only adjust sh_offset in SHT_NOBITS sections
5859 as would seem proper for their address when the
5860 section is first in the segment. sh_offset
5861 doesn't really have any significance for
5862 SHT_NOBITS anyway, apart from a notional position
5863 relative to other sections. Historically we
5864 didn't bother with adjusting sh_offset and some
5865 programs depend on it not being adjusted. See
5866 pr12921 and pr25662. */
5867 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
5870 if (this_hdr
->sh_type
== SHT_NOBITS
)
5871 off_adjust
+= adjust
;
5874 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5875 p
->p_filesz
+= adjust
;
5878 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5880 /* The section at i == 0 is the one that actually contains
5884 this_hdr
->sh_offset
= sec
->filepos
= off
;
5885 off
+= this_hdr
->sh_size
;
5886 p
->p_filesz
= this_hdr
->sh_size
;
5892 /* The rest are fake sections that shouldn't be written. */
5901 if (p
->p_type
== PT_LOAD
)
5903 this_hdr
->sh_offset
= sec
->filepos
= off
;
5904 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5905 off
+= this_hdr
->sh_size
;
5907 else if (this_hdr
->sh_type
== SHT_NOBITS
5908 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5909 && this_hdr
->sh_offset
== 0)
5911 /* This is a .tbss section that didn't get a PT_LOAD.
5912 (See _bfd_elf_map_sections_to_segments "Create a
5913 final PT_LOAD".) Set sh_offset to the value it
5914 would have if we had created a zero p_filesz and
5915 p_memsz PT_LOAD header for the section. This
5916 also makes the PT_TLS header have the same
5918 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5920 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5923 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5925 p
->p_filesz
+= this_hdr
->sh_size
;
5926 /* A load section without SHF_ALLOC is something like
5927 a note section in a PT_NOTE segment. These take
5928 file space but are not loaded into memory. */
5929 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5930 p
->p_memsz
+= this_hdr
->sh_size
;
5932 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5934 if (p
->p_type
== PT_TLS
)
5935 p
->p_memsz
+= this_hdr
->sh_size
;
5937 /* .tbss is special. It doesn't contribute to p_memsz of
5939 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5940 p
->p_memsz
+= this_hdr
->sh_size
;
5943 if (align
> p
->p_align
5944 && !m
->p_align_valid
5945 && (p
->p_type
!= PT_LOAD
5946 || (abfd
->flags
& D_PAGED
) == 0))
5950 if (!m
->p_flags_valid
)
5953 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5955 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5962 /* PR ld/20815 - Check that the program header segment, if
5963 present, will be loaded into memory. */
5964 if (p
->p_type
== PT_PHDR
5965 && phdr_load_seg
== NULL
5966 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
5967 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
5969 /* The fix for this error is usually to edit the linker script being
5970 used and set up the program headers manually. Either that or
5971 leave room for the headers at the start of the SECTIONS. */
5972 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5973 " by LOAD segment"),
5975 if (link_info
== NULL
)
5977 /* Arrange for the linker to exit with an error, deleting
5978 the output file unless --noinhibit-exec is given. */
5979 link_info
->callbacks
->info ("%X");
5982 /* Check that all sections are in a PT_LOAD segment.
5983 Don't check funky gdb generated core files. */
5984 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5986 bfd_boolean check_vma
= TRUE
;
5988 for (i
= 1; i
< m
->count
; i
++)
5989 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5990 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5991 ->this_hdr
), p
) != 0
5992 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5993 ->this_hdr
), p
) != 0)
5995 /* Looks like we have overlays packed into the segment. */
6000 for (i
= 0; i
< m
->count
; i
++)
6002 Elf_Internal_Shdr
*this_hdr
;
6005 sec
= m
->sections
[i
];
6006 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6007 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6008 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6011 /* xgettext:c-format */
6012 (_("%pB: section `%pA' can't be allocated in segment %d"),
6014 print_segment_map (m
);
6020 elf_next_file_pos (abfd
) = off
;
6022 if (link_info
!= NULL
6023 && phdr_load_seg
!= NULL
6024 && phdr_load_seg
->includes_filehdr
)
6026 /* There is a segment that contains both the file headers and the
6027 program headers, so provide a symbol __ehdr_start pointing there.
6028 A program can use this to examine itself robustly. */
6030 struct elf_link_hash_entry
*hash
6031 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6032 FALSE
, FALSE
, TRUE
);
6033 /* If the symbol was referenced and not defined, define it. */
6035 && (hash
->root
.type
== bfd_link_hash_new
6036 || hash
->root
.type
== bfd_link_hash_undefined
6037 || hash
->root
.type
== bfd_link_hash_undefweak
6038 || hash
->root
.type
== bfd_link_hash_common
))
6041 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6043 if (phdr_load_seg
->count
!= 0)
6044 /* The segment contains sections, so use the first one. */
6045 s
= phdr_load_seg
->sections
[0];
6047 /* Use the first (i.e. lowest-addressed) section in any segment. */
6048 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6049 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6057 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6058 hash
->root
.u
.def
.section
= s
;
6062 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6063 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6066 hash
->root
.type
= bfd_link_hash_defined
;
6067 hash
->def_regular
= 1;
6075 /* Determine if a bfd is a debuginfo file. Unfortunately there
6076 is no defined method for detecting such files, so we have to
6077 use heuristics instead. */
6080 is_debuginfo_file (bfd
*abfd
)
6082 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6085 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6086 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6087 Elf_Internal_Shdr
**headerp
;
6089 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6091 Elf_Internal_Shdr
*header
= * headerp
;
6093 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6094 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6095 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6096 && header
->sh_type
!= SHT_NOBITS
6097 && header
->sh_type
!= SHT_NOTE
)
6104 /* Assign file positions for the other sections, except for compressed debugging
6105 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6108 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6109 struct bfd_link_info
*link_info
)
6111 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6112 Elf_Internal_Shdr
**i_shdrpp
;
6113 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6114 Elf_Internal_Phdr
*phdrs
;
6115 Elf_Internal_Phdr
*p
;
6116 struct elf_segment_map
*m
;
6118 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6120 i_shdrpp
= elf_elfsections (abfd
);
6121 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6122 off
= elf_next_file_pos (abfd
);
6123 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6125 Elf_Internal_Shdr
*hdr
;
6128 if (hdr
->bfd_section
!= NULL
6129 && (hdr
->bfd_section
->filepos
!= 0
6130 || (hdr
->sh_type
== SHT_NOBITS
6131 && hdr
->contents
== NULL
)))
6132 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6133 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6135 if (hdr
->sh_size
!= 0
6136 /* PR 24717 - debuginfo files are known to be not strictly
6137 compliant with the ELF standard. In particular they often
6138 have .note.gnu.property sections that are outside of any
6139 loadable segment. This is not a problem for such files,
6140 so do not warn about them. */
6141 && ! is_debuginfo_file (abfd
))
6143 /* xgettext:c-format */
6144 (_("%pB: warning: allocated section `%s' not in segment"),
6146 (hdr
->bfd_section
== NULL
6148 : hdr
->bfd_section
->name
));
6149 /* We don't need to page align empty sections. */
6150 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6151 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6154 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6156 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6159 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6160 && hdr
->bfd_section
== NULL
)
6161 /* We don't know the offset of these sections yet: their size has
6162 not been decided. */
6163 || (hdr
->bfd_section
!= NULL
6164 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6165 || (bfd_section_is_ctf (hdr
->bfd_section
)
6166 && abfd
->is_linker_output
)))
6167 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6168 || (elf_symtab_shndx_list (abfd
) != NULL
6169 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6170 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6171 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6172 hdr
->sh_offset
= -1;
6174 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6176 elf_next_file_pos (abfd
) = off
;
6178 /* Now that we have set the section file positions, we can set up
6179 the file positions for the non PT_LOAD segments. */
6180 phdrs
= elf_tdata (abfd
)->phdr
;
6181 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6183 if (p
->p_type
== PT_GNU_RELRO
)
6185 bfd_vma start
, end
; /* Bytes. */
6188 if (link_info
!= NULL
)
6190 /* During linking the range of the RELRO segment is passed
6191 in link_info. Note that there may be padding between
6192 relro_start and the first RELRO section. */
6193 start
= link_info
->relro_start
;
6194 end
= link_info
->relro_end
;
6196 else if (m
->count
!= 0)
6198 if (!m
->p_size_valid
)
6200 start
= m
->sections
[0]->vma
;
6201 end
= start
+ m
->p_size
/ opb
;
6212 struct elf_segment_map
*lm
;
6213 const Elf_Internal_Phdr
*lp
;
6216 /* Find a LOAD segment containing a section in the RELRO
6218 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6220 lm
= lm
->next
, lp
++)
6222 if (lp
->p_type
== PT_LOAD
6224 && (lm
->sections
[lm
->count
- 1]->vma
6225 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6226 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6228 && lm
->sections
[0]->vma
< end
)
6234 /* Find the section starting the RELRO segment. */
6235 for (i
= 0; i
< lm
->count
; i
++)
6237 asection
*s
= lm
->sections
[i
];
6246 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6247 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6248 p
->p_offset
= lm
->sections
[i
]->filepos
;
6249 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6250 p
->p_filesz
= p
->p_memsz
;
6252 /* The RELRO segment typically ends a few bytes
6253 into .got.plt but other layouts are possible.
6254 In cases where the end does not match any
6255 loaded section (for instance is in file
6256 padding), trim p_filesz back to correspond to
6257 the end of loaded section contents. */
6258 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6259 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6261 /* Preserve the alignment and flags if they are
6262 valid. The gold linker generates RW/4 for
6263 the PT_GNU_RELRO section. It is better for
6264 objcopy/strip to honor these attributes
6265 otherwise gdb will choke when using separate
6267 if (!m
->p_align_valid
)
6269 if (!m
->p_flags_valid
)
6275 if (link_info
!= NULL
)
6278 memset (p
, 0, sizeof *p
);
6280 else if (p
->p_type
== PT_GNU_STACK
)
6282 if (m
->p_size_valid
)
6283 p
->p_memsz
= m
->p_size
;
6285 else if (m
->count
!= 0)
6289 if (p
->p_type
!= PT_LOAD
6290 && (p
->p_type
!= PT_NOTE
6291 || bfd_get_format (abfd
) != bfd_core
))
6293 /* A user specified segment layout may include a PHDR
6294 segment that overlaps with a LOAD segment... */
6295 if (p
->p_type
== PT_PHDR
)
6301 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6303 /* PR 17512: file: 2195325e. */
6305 (_("%pB: error: non-load segment %d includes file header "
6306 "and/or program header"),
6307 abfd
, (int) (p
- phdrs
));
6312 p
->p_offset
= m
->sections
[0]->filepos
;
6313 for (i
= m
->count
; i
-- != 0;)
6315 asection
*sect
= m
->sections
[i
];
6316 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6317 if (hdr
->sh_type
!= SHT_NOBITS
)
6319 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6331 static elf_section_list
*
6332 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6334 for (;list
!= NULL
; list
= list
->next
)
6340 /* Work out the file positions of all the sections. This is called by
6341 _bfd_elf_compute_section_file_positions. All the section sizes and
6342 VMAs must be known before this is called.
6344 Reloc sections come in two flavours: Those processed specially as
6345 "side-channel" data attached to a section to which they apply, and those that
6346 bfd doesn't process as relocations. The latter sort are stored in a normal
6347 bfd section by bfd_section_from_shdr. We don't consider the former sort
6348 here, unless they form part of the loadable image. Reloc sections not
6349 assigned here (and compressed debugging sections and CTF sections which
6350 nothing else in the file can rely upon) will be handled later by
6351 assign_file_positions_for_relocs.
6353 We also don't set the positions of the .symtab and .strtab here. */
6356 assign_file_positions_except_relocs (bfd
*abfd
,
6357 struct bfd_link_info
*link_info
)
6359 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6360 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6361 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6364 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6365 && bfd_get_format (abfd
) != bfd_core
)
6367 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6368 unsigned int num_sec
= elf_numsections (abfd
);
6369 Elf_Internal_Shdr
**hdrpp
;
6373 /* Start after the ELF header. */
6374 off
= i_ehdrp
->e_ehsize
;
6376 /* We are not creating an executable, which means that we are
6377 not creating a program header, and that the actual order of
6378 the sections in the file is unimportant. */
6379 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6381 Elf_Internal_Shdr
*hdr
;
6384 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6385 && hdr
->bfd_section
== NULL
)
6386 /* Do not assign offsets for these sections yet: we don't know
6388 || (hdr
->bfd_section
!= NULL
6389 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6390 || (bfd_section_is_ctf (hdr
->bfd_section
)
6391 && abfd
->is_linker_output
)))
6392 || i
== elf_onesymtab (abfd
)
6393 || (elf_symtab_shndx_list (abfd
) != NULL
6394 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6395 || i
== elf_strtab_sec (abfd
)
6396 || i
== elf_shstrtab_sec (abfd
))
6398 hdr
->sh_offset
= -1;
6401 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6404 elf_next_file_pos (abfd
) = off
;
6405 elf_program_header_size (abfd
) = 0;
6409 /* Assign file positions for the loaded sections based on the
6410 assignment of sections to segments. */
6411 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6414 /* And for non-load sections. */
6415 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6419 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6422 /* Write out the program headers. */
6423 alloc
= i_ehdrp
->e_phnum
;
6426 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6427 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6435 _bfd_elf_init_file_header (bfd
*abfd
,
6436 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6438 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6439 struct elf_strtab_hash
*shstrtab
;
6440 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6442 i_ehdrp
= elf_elfheader (abfd
);
6444 shstrtab
= _bfd_elf_strtab_init ();
6445 if (shstrtab
== NULL
)
6448 elf_shstrtab (abfd
) = shstrtab
;
6450 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6451 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6452 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6453 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6455 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6456 i_ehdrp
->e_ident
[EI_DATA
] =
6457 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6458 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6460 if ((abfd
->flags
& DYNAMIC
) != 0)
6461 i_ehdrp
->e_type
= ET_DYN
;
6462 else if ((abfd
->flags
& EXEC_P
) != 0)
6463 i_ehdrp
->e_type
= ET_EXEC
;
6464 else if (bfd_get_format (abfd
) == bfd_core
)
6465 i_ehdrp
->e_type
= ET_CORE
;
6467 i_ehdrp
->e_type
= ET_REL
;
6469 switch (bfd_get_arch (abfd
))
6471 case bfd_arch_unknown
:
6472 i_ehdrp
->e_machine
= EM_NONE
;
6475 /* There used to be a long list of cases here, each one setting
6476 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6477 in the corresponding bfd definition. To avoid duplication,
6478 the switch was removed. Machines that need special handling
6479 can generally do it in elf_backend_final_write_processing(),
6480 unless they need the information earlier than the final write.
6481 Such need can generally be supplied by replacing the tests for
6482 e_machine with the conditions used to determine it. */
6484 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6487 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6488 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6490 /* No program header, for now. */
6491 i_ehdrp
->e_phoff
= 0;
6492 i_ehdrp
->e_phentsize
= 0;
6493 i_ehdrp
->e_phnum
= 0;
6495 /* Each bfd section is section header entry. */
6496 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6497 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6499 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6500 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6501 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6502 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6503 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6504 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6505 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6506 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6507 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6513 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6515 FIXME: We used to have code here to sort the PT_LOAD segments into
6516 ascending order, as per the ELF spec. But this breaks some programs,
6517 including the Linux kernel. But really either the spec should be
6518 changed or the programs updated. */
6521 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6523 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6525 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6526 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6527 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6528 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6529 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6531 /* Find the lowest p_vaddr in PT_LOAD segments. */
6532 bfd_vma p_vaddr
= (bfd_vma
) -1;
6533 for (; segment
< end_segment
; segment
++)
6534 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6535 p_vaddr
= segment
->p_vaddr
;
6537 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6538 segments is non-zero. */
6540 i_ehdrp
->e_type
= ET_EXEC
;
6545 /* Assign file positions for all the reloc sections which are not part
6546 of the loadable file image, and the file position of section headers. */
6549 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6552 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6553 Elf_Internal_Shdr
*shdrp
;
6554 Elf_Internal_Ehdr
*i_ehdrp
;
6555 const struct elf_backend_data
*bed
;
6557 off
= elf_next_file_pos (abfd
);
6559 shdrpp
= elf_elfsections (abfd
);
6560 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6561 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6564 if (shdrp
->sh_offset
== -1)
6566 asection
*sec
= shdrp
->bfd_section
;
6567 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6568 || shdrp
->sh_type
== SHT_RELA
);
6569 bfd_boolean is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6572 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6574 if (!is_rel
&& !is_ctf
)
6576 const char *name
= sec
->name
;
6577 struct bfd_elf_section_data
*d
;
6579 /* Compress DWARF debug sections. */
6580 if (!bfd_compress_section (abfd
, sec
,
6584 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6585 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6587 /* If section is compressed with zlib-gnu, convert
6588 section name from .debug_* to .zdebug_*. */
6590 = convert_debug_to_zdebug (abfd
, name
);
6591 if (new_name
== NULL
)
6595 /* Add section name to section name section. */
6596 if (shdrp
->sh_name
!= (unsigned int) -1)
6599 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6601 d
= elf_section_data (sec
);
6603 /* Add reloc section name to section name section. */
6605 && !_bfd_elf_set_reloc_sh_name (abfd
,
6610 && !_bfd_elf_set_reloc_sh_name (abfd
,
6615 /* Update section size and contents. */
6616 shdrp
->sh_size
= sec
->size
;
6617 shdrp
->contents
= sec
->contents
;
6618 shdrp
->bfd_section
->contents
= NULL
;
6622 /* Update section size and contents. */
6623 shdrp
->sh_size
= sec
->size
;
6624 shdrp
->contents
= sec
->contents
;
6627 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6634 /* Place section name section after DWARF debug sections have been
6636 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6637 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6638 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6639 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6641 /* Place the section headers. */
6642 i_ehdrp
= elf_elfheader (abfd
);
6643 bed
= get_elf_backend_data (abfd
);
6644 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6645 i_ehdrp
->e_shoff
= off
;
6646 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6647 elf_next_file_pos (abfd
) = off
;
6653 _bfd_elf_write_object_contents (bfd
*abfd
)
6655 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6656 Elf_Internal_Shdr
**i_shdrp
;
6658 unsigned int count
, num_sec
;
6659 struct elf_obj_tdata
*t
;
6661 if (! abfd
->output_has_begun
6662 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6664 /* Do not rewrite ELF data when the BFD has been opened for update.
6665 abfd->output_has_begun was set to TRUE on opening, so creation of new
6666 sections, and modification of existing section sizes was restricted.
6667 This means the ELF header, program headers and section headers can't have
6669 If the contents of any sections has been modified, then those changes have
6670 already been written to the BFD. */
6671 else if (abfd
->direction
== both_direction
)
6673 BFD_ASSERT (abfd
->output_has_begun
);
6677 i_shdrp
= elf_elfsections (abfd
);
6680 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6684 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6687 /* After writing the headers, we need to write the sections too... */
6688 num_sec
= elf_numsections (abfd
);
6689 for (count
= 1; count
< num_sec
; count
++)
6691 i_shdrp
[count
]->sh_name
6692 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6693 i_shdrp
[count
]->sh_name
);
6694 if (bed
->elf_backend_section_processing
)
6695 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6697 if (i_shdrp
[count
]->contents
)
6699 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6701 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6702 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6707 /* Write out the section header names. */
6708 t
= elf_tdata (abfd
);
6709 if (elf_shstrtab (abfd
) != NULL
6710 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6711 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6714 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6717 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6720 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6721 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6722 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6728 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6730 /* Hopefully this can be done just like an object file. */
6731 return _bfd_elf_write_object_contents (abfd
);
6734 /* Given a section, search the header to find them. */
6737 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6739 const struct elf_backend_data
*bed
;
6740 unsigned int sec_index
;
6742 if (elf_section_data (asect
) != NULL
6743 && elf_section_data (asect
)->this_idx
!= 0)
6744 return elf_section_data (asect
)->this_idx
;
6746 if (bfd_is_abs_section (asect
))
6747 sec_index
= SHN_ABS
;
6748 else if (bfd_is_com_section (asect
))
6749 sec_index
= SHN_COMMON
;
6750 else if (bfd_is_und_section (asect
))
6751 sec_index
= SHN_UNDEF
;
6753 sec_index
= SHN_BAD
;
6755 bed
= get_elf_backend_data (abfd
);
6756 if (bed
->elf_backend_section_from_bfd_section
)
6758 int retval
= sec_index
;
6760 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6764 if (sec_index
== SHN_BAD
)
6765 bfd_set_error (bfd_error_nonrepresentable_section
);
6770 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6774 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6776 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6778 flagword flags
= asym_ptr
->flags
;
6780 /* When gas creates relocations against local labels, it creates its
6781 own symbol for the section, but does put the symbol into the
6782 symbol chain, so udata is 0. When the linker is generating
6783 relocatable output, this section symbol may be for one of the
6784 input sections rather than the output section. */
6785 if (asym_ptr
->udata
.i
== 0
6786 && (flags
& BSF_SECTION_SYM
)
6787 && asym_ptr
->section
)
6792 sec
= asym_ptr
->section
;
6793 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6794 sec
= sec
->output_section
;
6795 if (sec
->owner
== abfd
6796 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6797 && elf_section_syms (abfd
)[indx
] != NULL
)
6798 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6801 idx
= asym_ptr
->udata
.i
;
6805 /* This case can occur when using --strip-symbol on a symbol
6806 which is used in a relocation entry. */
6808 /* xgettext:c-format */
6809 (_("%pB: symbol `%s' required but not present"),
6810 abfd
, bfd_asymbol_name (asym_ptr
));
6811 bfd_set_error (bfd_error_no_symbols
);
6818 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6819 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6827 /* Rewrite program header information. */
6830 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6832 Elf_Internal_Ehdr
*iehdr
;
6833 struct elf_segment_map
*map
;
6834 struct elf_segment_map
*map_first
;
6835 struct elf_segment_map
**pointer_to_map
;
6836 Elf_Internal_Phdr
*segment
;
6839 unsigned int num_segments
;
6840 bfd_boolean phdr_included
= FALSE
;
6841 bfd_boolean p_paddr_valid
;
6842 bfd_vma maxpagesize
;
6843 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6844 unsigned int phdr_adjust_num
= 0;
6845 const struct elf_backend_data
*bed
;
6846 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
6848 bed
= get_elf_backend_data (ibfd
);
6849 iehdr
= elf_elfheader (ibfd
);
6852 pointer_to_map
= &map_first
;
6854 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6855 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6857 /* Returns the end address of the segment + 1. */
6858 #define SEGMENT_END(segment, start) \
6859 (start + (segment->p_memsz > segment->p_filesz \
6860 ? segment->p_memsz : segment->p_filesz))
6862 #define SECTION_SIZE(section, segment) \
6863 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6864 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6865 ? section->size : 0)
6867 /* Returns TRUE if the given section is contained within
6868 the given segment. VMA addresses are compared. */
6869 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6870 (section->vma * (opb) >= segment->p_vaddr \
6871 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6872 <= (SEGMENT_END (segment, segment->p_vaddr))))
6874 /* Returns TRUE if the given section is contained within
6875 the given segment. LMA addresses are compared. */
6876 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6877 (section->lma * (opb) >= base \
6878 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6879 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6880 <= SEGMENT_END (segment, base)))
6882 /* Handle PT_NOTE segment. */
6883 #define IS_NOTE(p, s) \
6884 (p->p_type == PT_NOTE \
6885 && elf_section_type (s) == SHT_NOTE \
6886 && (bfd_vma) s->filepos >= p->p_offset \
6887 && ((bfd_vma) s->filepos + s->size \
6888 <= p->p_offset + p->p_filesz))
6890 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6892 #define IS_COREFILE_NOTE(p, s) \
6894 && bfd_get_format (ibfd) == bfd_core \
6898 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6899 linker, which generates a PT_INTERP section with p_vaddr and
6900 p_memsz set to 0. */
6901 #define IS_SOLARIS_PT_INTERP(p, s) \
6903 && p->p_paddr == 0 \
6904 && p->p_memsz == 0 \
6905 && p->p_filesz > 0 \
6906 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6908 && (bfd_vma) s->filepos >= p->p_offset \
6909 && ((bfd_vma) s->filepos + s->size \
6910 <= p->p_offset + p->p_filesz))
6912 /* Decide if the given section should be included in the given segment.
6913 A section will be included if:
6914 1. It is within the address space of the segment -- we use the LMA
6915 if that is set for the segment and the VMA otherwise,
6916 2. It is an allocated section or a NOTE section in a PT_NOTE
6918 3. There is an output section associated with it,
6919 4. The section has not already been allocated to a previous segment.
6920 5. PT_GNU_STACK segments do not include any sections.
6921 6. PT_TLS segment includes only SHF_TLS sections.
6922 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6923 8. PT_DYNAMIC should not contain empty sections at the beginning
6924 (with the possible exception of .dynamic). */
6925 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6926 ((((segment->p_paddr \
6927 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6928 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
6929 && (section->flags & SEC_ALLOC) != 0) \
6930 || IS_NOTE (segment, section)) \
6931 && segment->p_type != PT_GNU_STACK \
6932 && (segment->p_type != PT_TLS \
6933 || (section->flags & SEC_THREAD_LOCAL)) \
6934 && (segment->p_type == PT_LOAD \
6935 || segment->p_type == PT_TLS \
6936 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6937 && (segment->p_type != PT_DYNAMIC \
6938 || SECTION_SIZE (section, segment) > 0 \
6939 || (segment->p_paddr \
6940 ? segment->p_paddr != section->lma * (opb) \
6941 : segment->p_vaddr != section->vma * (opb)) \
6942 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6943 && (segment->p_type != PT_LOAD || !section->segment_mark))
6945 /* If the output section of a section in the input segment is NULL,
6946 it is removed from the corresponding output segment. */
6947 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6948 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
6949 && section->output_section != NULL)
6951 /* Returns TRUE iff seg1 starts after the end of seg2. */
6952 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6953 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6955 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6956 their VMA address ranges and their LMA address ranges overlap.
6957 It is possible to have overlapping VMA ranges without overlapping LMA
6958 ranges. RedBoot images for example can have both .data and .bss mapped
6959 to the same VMA range, but with the .data section mapped to a different
6961 #define SEGMENT_OVERLAPS(seg1, seg2) \
6962 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6963 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6964 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6965 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6967 /* Initialise the segment mark field. */
6968 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6969 section
->segment_mark
= FALSE
;
6971 /* The Solaris linker creates program headers in which all the
6972 p_paddr fields are zero. When we try to objcopy or strip such a
6973 file, we get confused. Check for this case, and if we find it
6974 don't set the p_paddr_valid fields. */
6975 p_paddr_valid
= FALSE
;
6976 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6979 if (segment
->p_paddr
!= 0)
6981 p_paddr_valid
= TRUE
;
6985 /* Scan through the segments specified in the program header
6986 of the input BFD. For this first scan we look for overlaps
6987 in the loadable segments. These can be created by weird
6988 parameters to objcopy. Also, fix some solaris weirdness. */
6989 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6994 Elf_Internal_Phdr
*segment2
;
6996 if (segment
->p_type
== PT_INTERP
)
6997 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6998 if (IS_SOLARIS_PT_INTERP (segment
, section
))
7000 /* Mininal change so that the normal section to segment
7001 assignment code will work. */
7002 segment
->p_vaddr
= section
->vma
* opb
;
7006 if (segment
->p_type
!= PT_LOAD
)
7008 /* Remove PT_GNU_RELRO segment. */
7009 if (segment
->p_type
== PT_GNU_RELRO
)
7010 segment
->p_type
= PT_NULL
;
7014 /* Determine if this segment overlaps any previous segments. */
7015 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7017 bfd_signed_vma extra_length
;
7019 if (segment2
->p_type
!= PT_LOAD
7020 || !SEGMENT_OVERLAPS (segment
, segment2
))
7023 /* Merge the two segments together. */
7024 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7026 /* Extend SEGMENT2 to include SEGMENT and then delete
7028 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
7029 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
7031 if (extra_length
> 0)
7033 segment2
->p_memsz
+= extra_length
;
7034 segment2
->p_filesz
+= extra_length
;
7037 segment
->p_type
= PT_NULL
;
7039 /* Since we have deleted P we must restart the outer loop. */
7041 segment
= elf_tdata (ibfd
)->phdr
;
7046 /* Extend SEGMENT to include SEGMENT2 and then delete
7048 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
7049 - SEGMENT_END (segment
, segment
->p_vaddr
));
7051 if (extra_length
> 0)
7053 segment
->p_memsz
+= extra_length
;
7054 segment
->p_filesz
+= extra_length
;
7057 segment2
->p_type
= PT_NULL
;
7062 /* The second scan attempts to assign sections to segments. */
7063 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7067 unsigned int section_count
;
7068 asection
**sections
;
7069 asection
*output_section
;
7071 asection
*matching_lma
;
7072 asection
*suggested_lma
;
7075 asection
*first_section
;
7077 if (segment
->p_type
== PT_NULL
)
7080 first_section
= NULL
;
7081 /* Compute how many sections might be placed into this segment. */
7082 for (section
= ibfd
->sections
, section_count
= 0;
7084 section
= section
->next
)
7086 /* Find the first section in the input segment, which may be
7087 removed from the corresponding output segment. */
7088 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7090 if (first_section
== NULL
)
7091 first_section
= section
;
7092 if (section
->output_section
!= NULL
)
7097 /* Allocate a segment map big enough to contain
7098 all of the sections we have selected. */
7099 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7100 amt
+= section_count
* sizeof (asection
*);
7101 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7105 /* Initialise the fields of the segment map. Default to
7106 using the physical address of the segment in the input BFD. */
7108 map
->p_type
= segment
->p_type
;
7109 map
->p_flags
= segment
->p_flags
;
7110 map
->p_flags_valid
= 1;
7112 /* If the first section in the input segment is removed, there is
7113 no need to preserve segment physical address in the corresponding
7115 if (!first_section
|| first_section
->output_section
!= NULL
)
7117 map
->p_paddr
= segment
->p_paddr
;
7118 map
->p_paddr_valid
= p_paddr_valid
;
7121 /* Determine if this segment contains the ELF file header
7122 and if it contains the program headers themselves. */
7123 map
->includes_filehdr
= (segment
->p_offset
== 0
7124 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7125 map
->includes_phdrs
= 0;
7127 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7129 map
->includes_phdrs
=
7130 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7131 && (segment
->p_offset
+ segment
->p_filesz
7132 >= ((bfd_vma
) iehdr
->e_phoff
7133 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7135 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7136 phdr_included
= TRUE
;
7139 if (section_count
== 0)
7141 /* Special segments, such as the PT_PHDR segment, may contain
7142 no sections, but ordinary, loadable segments should contain
7143 something. They are allowed by the ELF spec however, so only
7144 a warning is produced.
7145 There is however the valid use case of embedded systems which
7146 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7147 flash memory with zeros. No warning is shown for that case. */
7148 if (segment
->p_type
== PT_LOAD
7149 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7150 /* xgettext:c-format */
7152 (_("%pB: warning: empty loadable segment detected"
7153 " at vaddr=%#" PRIx64
", is this intentional?"),
7154 ibfd
, (uint64_t) segment
->p_vaddr
);
7156 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7158 *pointer_to_map
= map
;
7159 pointer_to_map
= &map
->next
;
7164 /* Now scan the sections in the input BFD again and attempt
7165 to add their corresponding output sections to the segment map.
7166 The problem here is how to handle an output section which has
7167 been moved (ie had its LMA changed). There are four possibilities:
7169 1. None of the sections have been moved.
7170 In this case we can continue to use the segment LMA from the
7173 2. All of the sections have been moved by the same amount.
7174 In this case we can change the segment's LMA to match the LMA
7175 of the first section.
7177 3. Some of the sections have been moved, others have not.
7178 In this case those sections which have not been moved can be
7179 placed in the current segment which will have to have its size,
7180 and possibly its LMA changed, and a new segment or segments will
7181 have to be created to contain the other sections.
7183 4. The sections have been moved, but not by the same amount.
7184 In this case we can change the segment's LMA to match the LMA
7185 of the first section and we will have to create a new segment
7186 or segments to contain the other sections.
7188 In order to save time, we allocate an array to hold the section
7189 pointers that we are interested in. As these sections get assigned
7190 to a segment, they are removed from this array. */
7192 amt
= section_count
* sizeof (asection
*);
7193 sections
= (asection
**) bfd_malloc (amt
);
7194 if (sections
== NULL
)
7197 /* Step One: Scan for segment vs section LMA conflicts.
7198 Also add the sections to the section array allocated above.
7199 Also add the sections to the current segment. In the common
7200 case, where the sections have not been moved, this means that
7201 we have completely filled the segment, and there is nothing
7204 matching_lma
= NULL
;
7205 suggested_lma
= NULL
;
7207 for (section
= first_section
, j
= 0;
7209 section
= section
->next
)
7211 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7213 output_section
= section
->output_section
;
7215 sections
[j
++] = section
;
7217 /* The Solaris native linker always sets p_paddr to 0.
7218 We try to catch that case here, and set it to the
7219 correct value. Note - some backends require that
7220 p_paddr be left as zero. */
7222 && segment
->p_vaddr
!= 0
7223 && !bed
->want_p_paddr_set_to_zero
7225 && output_section
->lma
!= 0
7226 && (align_power (segment
->p_vaddr
7227 + (map
->includes_filehdr
7228 ? iehdr
->e_ehsize
: 0)
7229 + (map
->includes_phdrs
7230 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7232 output_section
->alignment_power
* opb
)
7233 == (output_section
->vma
* opb
)))
7234 map
->p_paddr
= segment
->p_vaddr
;
7236 /* Match up the physical address of the segment with the
7237 LMA address of the output section. */
7238 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7240 || IS_COREFILE_NOTE (segment
, section
)
7241 || (bed
->want_p_paddr_set_to_zero
7242 && IS_CONTAINED_BY_VMA (output_section
, segment
, opb
)))
7244 if (matching_lma
== NULL
7245 || output_section
->lma
< matching_lma
->lma
)
7246 matching_lma
= output_section
;
7248 /* We assume that if the section fits within the segment
7249 then it does not overlap any other section within that
7251 map
->sections
[isec
++] = output_section
;
7253 else if (suggested_lma
== NULL
)
7254 suggested_lma
= output_section
;
7256 if (j
== section_count
)
7261 BFD_ASSERT (j
== section_count
);
7263 /* Step Two: Adjust the physical address of the current segment,
7265 if (isec
== section_count
)
7267 /* All of the sections fitted within the segment as currently
7268 specified. This is the default case. Add the segment to
7269 the list of built segments and carry on to process the next
7270 program header in the input BFD. */
7271 map
->count
= section_count
;
7272 *pointer_to_map
= map
;
7273 pointer_to_map
= &map
->next
;
7276 && !bed
->want_p_paddr_set_to_zero
)
7278 bfd_vma hdr_size
= 0;
7279 if (map
->includes_filehdr
)
7280 hdr_size
= iehdr
->e_ehsize
;
7281 if (map
->includes_phdrs
)
7282 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7284 /* Account for padding before the first section in the
7286 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7287 - matching_lma
->lma
);
7295 /* Change the current segment's physical address to match
7296 the LMA of the first section that fitted, or if no
7297 section fitted, the first section. */
7298 if (matching_lma
== NULL
)
7299 matching_lma
= suggested_lma
;
7301 map
->p_paddr
= matching_lma
->lma
* opb
;
7303 /* Offset the segment physical address from the lma
7304 to allow for space taken up by elf headers. */
7305 if (map
->includes_phdrs
)
7307 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7309 /* iehdr->e_phnum is just an estimate of the number
7310 of program headers that we will need. Make a note
7311 here of the number we used and the segment we chose
7312 to hold these headers, so that we can adjust the
7313 offset when we know the correct value. */
7314 phdr_adjust_num
= iehdr
->e_phnum
;
7315 phdr_adjust_seg
= map
;
7318 if (map
->includes_filehdr
)
7320 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7321 map
->p_paddr
-= iehdr
->e_ehsize
;
7322 /* We've subtracted off the size of headers from the
7323 first section lma, but there may have been some
7324 alignment padding before that section too. Try to
7325 account for that by adjusting the segment lma down to
7326 the same alignment. */
7327 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7328 align
= segment
->p_align
;
7329 map
->p_paddr
&= -(align
* opb
);
7333 /* Step Three: Loop over the sections again, this time assigning
7334 those that fit to the current segment and removing them from the
7335 sections array; but making sure not to leave large gaps. Once all
7336 possible sections have been assigned to the current segment it is
7337 added to the list of built segments and if sections still remain
7338 to be assigned, a new segment is constructed before repeating
7344 suggested_lma
= NULL
;
7346 /* Fill the current segment with sections that fit. */
7347 for (j
= 0; j
< section_count
; j
++)
7349 section
= sections
[j
];
7351 if (section
== NULL
)
7354 output_section
= section
->output_section
;
7356 BFD_ASSERT (output_section
!= NULL
);
7358 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7360 || IS_COREFILE_NOTE (segment
, section
))
7362 if (map
->count
== 0)
7364 /* If the first section in a segment does not start at
7365 the beginning of the segment, then something is
7367 if (align_power (map
->p_paddr
7368 + (map
->includes_filehdr
7369 ? iehdr
->e_ehsize
: 0)
7370 + (map
->includes_phdrs
7371 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7373 output_section
->alignment_power
* opb
)
7374 != output_section
->lma
* opb
)
7381 prev_sec
= map
->sections
[map
->count
- 1];
7383 /* If the gap between the end of the previous section
7384 and the start of this section is more than
7385 maxpagesize then we need to start a new segment. */
7386 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7388 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7389 || (prev_sec
->lma
+ prev_sec
->size
7390 > output_section
->lma
))
7392 if (suggested_lma
== NULL
)
7393 suggested_lma
= output_section
;
7399 map
->sections
[map
->count
++] = output_section
;
7402 if (segment
->p_type
== PT_LOAD
)
7403 section
->segment_mark
= TRUE
;
7405 else if (suggested_lma
== NULL
)
7406 suggested_lma
= output_section
;
7409 /* PR 23932. A corrupt input file may contain sections that cannot
7410 be assigned to any segment - because for example they have a
7411 negative size - or segments that do not contain any sections.
7412 But there are also valid reasons why a segment can be empty.
7413 So allow a count of zero. */
7415 /* Add the current segment to the list of built segments. */
7416 *pointer_to_map
= map
;
7417 pointer_to_map
= &map
->next
;
7419 if (isec
< section_count
)
7421 /* We still have not allocated all of the sections to
7422 segments. Create a new segment here, initialise it
7423 and carry on looping. */
7424 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7425 amt
+= section_count
* sizeof (asection
*);
7426 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7433 /* Initialise the fields of the segment map. Set the physical
7434 physical address to the LMA of the first section that has
7435 not yet been assigned. */
7437 map
->p_type
= segment
->p_type
;
7438 map
->p_flags
= segment
->p_flags
;
7439 map
->p_flags_valid
= 1;
7440 map
->p_paddr
= suggested_lma
->lma
* opb
;
7441 map
->p_paddr_valid
= p_paddr_valid
;
7442 map
->includes_filehdr
= 0;
7443 map
->includes_phdrs
= 0;
7448 bfd_set_error (bfd_error_sorry
);
7452 while (isec
< section_count
);
7457 elf_seg_map (obfd
) = map_first
;
7459 /* If we had to estimate the number of program headers that were
7460 going to be needed, then check our estimate now and adjust
7461 the offset if necessary. */
7462 if (phdr_adjust_seg
!= NULL
)
7466 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7469 if (count
> phdr_adjust_num
)
7470 phdr_adjust_seg
->p_paddr
7471 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7473 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7474 if (map
->p_type
== PT_PHDR
)
7477 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7478 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7485 #undef IS_CONTAINED_BY_VMA
7486 #undef IS_CONTAINED_BY_LMA
7488 #undef IS_COREFILE_NOTE
7489 #undef IS_SOLARIS_PT_INTERP
7490 #undef IS_SECTION_IN_INPUT_SEGMENT
7491 #undef INCLUDE_SECTION_IN_SEGMENT
7492 #undef SEGMENT_AFTER_SEGMENT
7493 #undef SEGMENT_OVERLAPS
7497 /* Copy ELF program header information. */
7500 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7502 Elf_Internal_Ehdr
*iehdr
;
7503 struct elf_segment_map
*map
;
7504 struct elf_segment_map
*map_first
;
7505 struct elf_segment_map
**pointer_to_map
;
7506 Elf_Internal_Phdr
*segment
;
7508 unsigned int num_segments
;
7509 bfd_boolean phdr_included
= FALSE
;
7510 bfd_boolean p_paddr_valid
;
7511 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7513 iehdr
= elf_elfheader (ibfd
);
7516 pointer_to_map
= &map_first
;
7518 /* If all the segment p_paddr fields are zero, don't set
7519 map->p_paddr_valid. */
7520 p_paddr_valid
= FALSE
;
7521 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7522 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7525 if (segment
->p_paddr
!= 0)
7527 p_paddr_valid
= TRUE
;
7531 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7536 unsigned int section_count
;
7538 Elf_Internal_Shdr
*this_hdr
;
7539 asection
*first_section
= NULL
;
7540 asection
*lowest_section
;
7542 /* Compute how many sections are in this segment. */
7543 for (section
= ibfd
->sections
, section_count
= 0;
7545 section
= section
->next
)
7547 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7548 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7550 if (first_section
== NULL
)
7551 first_section
= section
;
7556 /* Allocate a segment map big enough to contain
7557 all of the sections we have selected. */
7558 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7559 amt
+= section_count
* sizeof (asection
*);
7560 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7564 /* Initialize the fields of the output segment map with the
7567 map
->p_type
= segment
->p_type
;
7568 map
->p_flags
= segment
->p_flags
;
7569 map
->p_flags_valid
= 1;
7570 map
->p_paddr
= segment
->p_paddr
;
7571 map
->p_paddr_valid
= p_paddr_valid
;
7572 map
->p_align
= segment
->p_align
;
7573 map
->p_align_valid
= 1;
7574 map
->p_vaddr_offset
= 0;
7576 if (map
->p_type
== PT_GNU_RELRO
7577 || map
->p_type
== PT_GNU_STACK
)
7579 /* The PT_GNU_RELRO segment may contain the first a few
7580 bytes in the .got.plt section even if the whole .got.plt
7581 section isn't in the PT_GNU_RELRO segment. We won't
7582 change the size of the PT_GNU_RELRO segment.
7583 Similarly, PT_GNU_STACK size is significant on uclinux
7585 map
->p_size
= segment
->p_memsz
;
7586 map
->p_size_valid
= 1;
7589 /* Determine if this segment contains the ELF file header
7590 and if it contains the program headers themselves. */
7591 map
->includes_filehdr
= (segment
->p_offset
== 0
7592 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7594 map
->includes_phdrs
= 0;
7595 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7597 map
->includes_phdrs
=
7598 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7599 && (segment
->p_offset
+ segment
->p_filesz
7600 >= ((bfd_vma
) iehdr
->e_phoff
7601 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7603 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7604 phdr_included
= TRUE
;
7607 lowest_section
= NULL
;
7608 if (section_count
!= 0)
7610 unsigned int isec
= 0;
7612 for (section
= first_section
;
7614 section
= section
->next
)
7616 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7617 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7619 map
->sections
[isec
++] = section
->output_section
;
7620 if ((section
->flags
& SEC_ALLOC
) != 0)
7624 if (lowest_section
== NULL
7625 || section
->lma
< lowest_section
->lma
)
7626 lowest_section
= section
;
7628 /* Section lmas are set up from PT_LOAD header
7629 p_paddr in _bfd_elf_make_section_from_shdr.
7630 If this header has a p_paddr that disagrees
7631 with the section lma, flag the p_paddr as
7633 if ((section
->flags
& SEC_LOAD
) != 0)
7634 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7636 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7637 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7638 map
->p_paddr_valid
= FALSE
;
7640 if (isec
== section_count
)
7646 if (section_count
== 0)
7647 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7648 else if (map
->p_paddr_valid
)
7650 /* Account for padding before the first section in the segment. */
7651 bfd_vma hdr_size
= 0;
7652 if (map
->includes_filehdr
)
7653 hdr_size
= iehdr
->e_ehsize
;
7654 if (map
->includes_phdrs
)
7655 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7657 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7658 - (lowest_section
? lowest_section
->lma
: 0));
7661 map
->count
= section_count
;
7662 *pointer_to_map
= map
;
7663 pointer_to_map
= &map
->next
;
7666 elf_seg_map (obfd
) = map_first
;
7670 /* Copy private BFD data. This copies or rewrites ELF program header
7674 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7676 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7677 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7680 if (elf_tdata (ibfd
)->phdr
== NULL
)
7683 if (ibfd
->xvec
== obfd
->xvec
)
7685 /* Check to see if any sections in the input BFD
7686 covered by ELF program header have changed. */
7687 Elf_Internal_Phdr
*segment
;
7688 asection
*section
, *osec
;
7689 unsigned int i
, num_segments
;
7690 Elf_Internal_Shdr
*this_hdr
;
7691 const struct elf_backend_data
*bed
;
7693 bed
= get_elf_backend_data (ibfd
);
7695 /* Regenerate the segment map if p_paddr is set to 0. */
7696 if (bed
->want_p_paddr_set_to_zero
)
7699 /* Initialize the segment mark field. */
7700 for (section
= obfd
->sections
; section
!= NULL
;
7701 section
= section
->next
)
7702 section
->segment_mark
= FALSE
;
7704 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7705 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7709 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7710 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7711 which severly confuses things, so always regenerate the segment
7712 map in this case. */
7713 if (segment
->p_paddr
== 0
7714 && segment
->p_memsz
== 0
7715 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7718 for (section
= ibfd
->sections
;
7719 section
!= NULL
; section
= section
->next
)
7721 /* We mark the output section so that we know it comes
7722 from the input BFD. */
7723 osec
= section
->output_section
;
7725 osec
->segment_mark
= TRUE
;
7727 /* Check if this section is covered by the segment. */
7728 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7729 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7731 /* FIXME: Check if its output section is changed or
7732 removed. What else do we need to check? */
7734 || section
->flags
!= osec
->flags
7735 || section
->lma
!= osec
->lma
7736 || section
->vma
!= osec
->vma
7737 || section
->size
!= osec
->size
7738 || section
->rawsize
!= osec
->rawsize
7739 || section
->alignment_power
!= osec
->alignment_power
)
7745 /* Check to see if any output section do not come from the
7747 for (section
= obfd
->sections
; section
!= NULL
;
7748 section
= section
->next
)
7750 if (!section
->segment_mark
)
7753 section
->segment_mark
= FALSE
;
7756 return copy_elf_program_header (ibfd
, obfd
);
7760 if (ibfd
->xvec
== obfd
->xvec
)
7762 /* When rewriting program header, set the output maxpagesize to
7763 the maximum alignment of input PT_LOAD segments. */
7764 Elf_Internal_Phdr
*segment
;
7766 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7767 bfd_vma maxpagesize
= 0;
7769 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7772 if (segment
->p_type
== PT_LOAD
7773 && maxpagesize
< segment
->p_align
)
7775 /* PR 17512: file: f17299af. */
7776 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7777 /* xgettext:c-format */
7778 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7779 PRIx64
" is too large"),
7780 ibfd
, (uint64_t) segment
->p_align
);
7782 maxpagesize
= segment
->p_align
;
7785 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7786 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7789 return rewrite_elf_program_header (ibfd
, obfd
);
7792 /* Initialize private output section information from input section. */
7795 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7799 struct bfd_link_info
*link_info
)
7802 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7803 bfd_boolean final_link
= (link_info
!= NULL
7804 && !bfd_link_relocatable (link_info
));
7806 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7807 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7810 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7812 /* If this is a known ABI section, ELF section type and flags may
7813 have been set up when OSEC was created. For normal sections we
7814 allow the user to override the type and flags other than
7815 SHF_MASKOS and SHF_MASKPROC. */
7816 if (elf_section_type (osec
) == SHT_PROGBITS
7817 || elf_section_type (osec
) == SHT_NOTE
7818 || elf_section_type (osec
) == SHT_NOBITS
)
7819 elf_section_type (osec
) = SHT_NULL
;
7820 /* For objcopy and relocatable link, copy the ELF section type from
7821 the input file if the BFD section flags are the same. (If they
7822 are different the user may be doing something like
7823 "objcopy --set-section-flags .text=alloc,data".) For a final
7824 link allow some flags that the linker clears to differ. */
7825 if (elf_section_type (osec
) == SHT_NULL
7826 && (osec
->flags
== isec
->flags
7828 && ((osec
->flags
^ isec
->flags
)
7829 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7830 elf_section_type (osec
) = elf_section_type (isec
);
7832 /* FIXME: Is this correct for all OS/PROC specific flags? */
7833 elf_section_flags (osec
) = (elf_section_flags (isec
)
7834 & (SHF_MASKOS
| SHF_MASKPROC
));
7836 /* Copy sh_info from input for mbind section. */
7837 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7838 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7839 elf_section_data (osec
)->this_hdr
.sh_info
7840 = elf_section_data (isec
)->this_hdr
.sh_info
;
7842 /* Set things up for objcopy and relocatable link. The output
7843 SHT_GROUP section will have its elf_next_in_group pointing back
7844 to the input group members. Ignore linker created group section.
7845 See elfNN_ia64_object_p in elfxx-ia64.c. */
7846 if ((link_info
== NULL
7847 || !link_info
->resolve_section_groups
)
7848 && (elf_sec_group (isec
) == NULL
7849 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7851 if (elf_section_flags (isec
) & SHF_GROUP
)
7852 elf_section_flags (osec
) |= SHF_GROUP
;
7853 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7854 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7857 /* If not decompress, preserve SHF_COMPRESSED. */
7858 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7859 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7862 ihdr
= &elf_section_data (isec
)->this_hdr
;
7864 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7865 don't use the output section of the linked-to section since it
7866 may be NULL at this point. */
7867 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7869 ohdr
= &elf_section_data (osec
)->this_hdr
;
7870 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7871 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7874 osec
->use_rela_p
= isec
->use_rela_p
;
7879 /* Copy private section information. This copies over the entsize
7880 field, and sometimes the info field. */
7883 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7888 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7890 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7891 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7894 ihdr
= &elf_section_data (isec
)->this_hdr
;
7895 ohdr
= &elf_section_data (osec
)->this_hdr
;
7897 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7899 if (ihdr
->sh_type
== SHT_SYMTAB
7900 || ihdr
->sh_type
== SHT_DYNSYM
7901 || ihdr
->sh_type
== SHT_GNU_verneed
7902 || ihdr
->sh_type
== SHT_GNU_verdef
)
7903 ohdr
->sh_info
= ihdr
->sh_info
;
7905 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7909 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7910 necessary if we are removing either the SHT_GROUP section or any of
7911 the group member sections. DISCARDED is the value that a section's
7912 output_section has if the section will be discarded, NULL when this
7913 function is called from objcopy, bfd_abs_section_ptr when called
7917 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7921 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7922 if (elf_section_type (isec
) == SHT_GROUP
)
7924 asection
*first
= elf_next_in_group (isec
);
7925 asection
*s
= first
;
7926 bfd_size_type removed
= 0;
7930 /* If this member section is being output but the
7931 SHT_GROUP section is not, then clear the group info
7932 set up by _bfd_elf_copy_private_section_data. */
7933 if (s
->output_section
!= discarded
7934 && isec
->output_section
== discarded
)
7936 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7937 elf_group_name (s
->output_section
) = NULL
;
7941 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7942 if (s
->output_section
== discarded
7943 && isec
->output_section
!= discarded
)
7945 /* Conversely, if the member section is not being
7946 output but the SHT_GROUP section is, then adjust
7949 if (elf_sec
->rel
.hdr
!= NULL
7950 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7952 if (elf_sec
->rela
.hdr
!= NULL
7953 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7958 /* Also adjust for zero-sized relocation member
7960 if (elf_sec
->rel
.hdr
!= NULL
7961 && elf_sec
->rel
.hdr
->sh_size
== 0)
7963 if (elf_sec
->rela
.hdr
!= NULL
7964 && elf_sec
->rela
.hdr
->sh_size
== 0)
7968 s
= elf_next_in_group (s
);
7974 if (discarded
!= NULL
)
7976 /* If we've been called for ld -r, then we need to
7977 adjust the input section size. */
7978 if (isec
->rawsize
== 0)
7979 isec
->rawsize
= isec
->size
;
7980 isec
->size
= isec
->rawsize
- removed
;
7981 if (isec
->size
<= 4)
7984 isec
->flags
|= SEC_EXCLUDE
;
7989 /* Adjust the output section size when called from
7991 isec
->output_section
->size
-= removed
;
7992 if (isec
->output_section
->size
<= 4)
7994 isec
->output_section
->size
= 0;
7995 isec
->output_section
->flags
|= SEC_EXCLUDE
;
8004 /* Copy private header information. */
8007 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
8009 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8010 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8013 /* Copy over private BFD data if it has not already been copied.
8014 This must be done here, rather than in the copy_private_bfd_data
8015 entry point, because the latter is called after the section
8016 contents have been set, which means that the program headers have
8017 already been worked out. */
8018 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
8020 if (! copy_private_bfd_data (ibfd
, obfd
))
8024 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8027 /* Copy private symbol information. If this symbol is in a section
8028 which we did not map into a BFD section, try to map the section
8029 index correctly. We use special macro definitions for the mapped
8030 section indices; these definitions are interpreted by the
8031 swap_out_syms function. */
8033 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8034 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8035 #define MAP_STRTAB (SHN_HIOS + 3)
8036 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8037 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8040 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8045 elf_symbol_type
*isym
, *osym
;
8047 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8048 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8051 isym
= elf_symbol_from (ibfd
, isymarg
);
8052 osym
= elf_symbol_from (obfd
, osymarg
);
8055 && isym
->internal_elf_sym
.st_shndx
!= 0
8057 && bfd_is_abs_section (isym
->symbol
.section
))
8061 shndx
= isym
->internal_elf_sym
.st_shndx
;
8062 if (shndx
== elf_onesymtab (ibfd
))
8063 shndx
= MAP_ONESYMTAB
;
8064 else if (shndx
== elf_dynsymtab (ibfd
))
8065 shndx
= MAP_DYNSYMTAB
;
8066 else if (shndx
== elf_strtab_sec (ibfd
))
8068 else if (shndx
== elf_shstrtab_sec (ibfd
))
8069 shndx
= MAP_SHSTRTAB
;
8070 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8071 shndx
= MAP_SYM_SHNDX
;
8072 osym
->internal_elf_sym
.st_shndx
= shndx
;
8078 /* Swap out the symbols. */
8081 swap_out_syms (bfd
*abfd
,
8082 struct elf_strtab_hash
**sttp
,
8085 const struct elf_backend_data
*bed
;
8086 unsigned int symcount
;
8088 struct elf_strtab_hash
*stt
;
8089 Elf_Internal_Shdr
*symtab_hdr
;
8090 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8091 Elf_Internal_Shdr
*symstrtab_hdr
;
8092 struct elf_sym_strtab
*symstrtab
;
8093 bfd_byte
*outbound_syms
;
8094 bfd_byte
*outbound_shndx
;
8095 unsigned long outbound_syms_index
;
8096 unsigned long outbound_shndx_index
;
8098 unsigned int num_locals
;
8100 bfd_boolean name_local_sections
;
8102 if (!elf_map_symbols (abfd
, &num_locals
))
8105 /* Dump out the symtabs. */
8106 stt
= _bfd_elf_strtab_init ();
8110 bed
= get_elf_backend_data (abfd
);
8111 symcount
= bfd_get_symcount (abfd
);
8112 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8113 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8114 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8115 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8116 symtab_hdr
->sh_info
= num_locals
+ 1;
8117 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8119 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8120 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8122 /* Allocate buffer to swap out the .strtab section. */
8123 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8124 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8126 bfd_set_error (bfd_error_no_memory
);
8127 _bfd_elf_strtab_free (stt
);
8131 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8132 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8135 bfd_set_error (bfd_error_no_memory
);
8138 _bfd_elf_strtab_free (stt
);
8141 symtab_hdr
->contents
= outbound_syms
;
8142 outbound_syms_index
= 0;
8144 outbound_shndx
= NULL
;
8145 outbound_shndx_index
= 0;
8147 if (elf_symtab_shndx_list (abfd
))
8149 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8150 if (symtab_shndx_hdr
->sh_name
!= 0)
8152 if (_bfd_mul_overflow (symcount
+ 1,
8153 sizeof (Elf_External_Sym_Shndx
), &amt
))
8155 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8156 if (outbound_shndx
== NULL
)
8159 symtab_shndx_hdr
->contents
= outbound_shndx
;
8160 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8161 symtab_shndx_hdr
->sh_size
= amt
;
8162 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8163 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8165 /* FIXME: What about any other headers in the list ? */
8168 /* Now generate the data (for "contents"). */
8170 /* Fill in zeroth symbol and swap it out. */
8171 Elf_Internal_Sym sym
;
8177 sym
.st_shndx
= SHN_UNDEF
;
8178 sym
.st_target_internal
= 0;
8179 symstrtab
[0].sym
= sym
;
8180 symstrtab
[0].dest_index
= outbound_syms_index
;
8181 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8182 outbound_syms_index
++;
8183 if (outbound_shndx
!= NULL
)
8184 outbound_shndx_index
++;
8188 = (bed
->elf_backend_name_local_section_symbols
8189 && bed
->elf_backend_name_local_section_symbols (abfd
));
8191 syms
= bfd_get_outsymbols (abfd
);
8192 for (idx
= 0; idx
< symcount
;)
8194 Elf_Internal_Sym sym
;
8195 bfd_vma value
= syms
[idx
]->value
;
8196 elf_symbol_type
*type_ptr
;
8197 flagword flags
= syms
[idx
]->flags
;
8200 if (!name_local_sections
8201 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8203 /* Local section symbols have no name. */
8204 sym
.st_name
= (unsigned long) -1;
8208 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8209 to get the final offset for st_name. */
8211 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8213 if (sym
.st_name
== (unsigned long) -1)
8217 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
8219 if ((flags
& BSF_SECTION_SYM
) == 0
8220 && bfd_is_com_section (syms
[idx
]->section
))
8222 /* ELF common symbols put the alignment into the `value' field,
8223 and the size into the `size' field. This is backwards from
8224 how BFD handles it, so reverse it here. */
8225 sym
.st_size
= value
;
8226 if (type_ptr
== NULL
8227 || type_ptr
->internal_elf_sym
.st_value
== 0)
8228 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8230 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8231 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8232 (abfd
, syms
[idx
]->section
);
8236 asection
*sec
= syms
[idx
]->section
;
8239 if (sec
->output_section
)
8241 value
+= sec
->output_offset
;
8242 sec
= sec
->output_section
;
8245 /* Don't add in the section vma for relocatable output. */
8246 if (! relocatable_p
)
8248 sym
.st_value
= value
;
8249 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8251 if (bfd_is_abs_section (sec
)
8253 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8255 /* This symbol is in a real ELF section which we did
8256 not create as a BFD section. Undo the mapping done
8257 by copy_private_symbol_data. */
8258 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8262 shndx
= elf_onesymtab (abfd
);
8265 shndx
= elf_dynsymtab (abfd
);
8268 shndx
= elf_strtab_sec (abfd
);
8271 shndx
= elf_shstrtab_sec (abfd
);
8274 if (elf_symtab_shndx_list (abfd
))
8275 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8282 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8284 if (bed
->symbol_section_index
)
8285 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8286 /* Otherwise just leave the index alone. */
8290 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8291 _bfd_error_handler (_("%pB: \
8292 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8301 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8303 if (shndx
== SHN_BAD
)
8307 /* Writing this would be a hell of a lot easier if
8308 we had some decent documentation on bfd, and
8309 knew what to expect of the library, and what to
8310 demand of applications. For example, it
8311 appears that `objcopy' might not set the
8312 section of a symbol to be a section that is
8313 actually in the output file. */
8314 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8316 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8317 if (shndx
== SHN_BAD
)
8319 /* xgettext:c-format */
8321 (_("unable to find equivalent output section"
8322 " for symbol '%s' from section '%s'"),
8323 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8325 bfd_set_error (bfd_error_invalid_operation
);
8331 sym
.st_shndx
= shndx
;
8334 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8336 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8337 type
= STT_GNU_IFUNC
;
8338 else if ((flags
& BSF_FUNCTION
) != 0)
8340 else if ((flags
& BSF_OBJECT
) != 0)
8342 else if ((flags
& BSF_RELC
) != 0)
8344 else if ((flags
& BSF_SRELC
) != 0)
8349 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8352 /* Processor-specific types. */
8353 if (type_ptr
!= NULL
8354 && bed
->elf_backend_get_symbol_type
)
8355 type
= ((*bed
->elf_backend_get_symbol_type
)
8356 (&type_ptr
->internal_elf_sym
, type
));
8358 if (flags
& BSF_SECTION_SYM
)
8360 if (flags
& BSF_GLOBAL
)
8361 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8363 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8365 else if (bfd_is_com_section (syms
[idx
]->section
))
8367 if (type
!= STT_TLS
)
8369 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8370 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8371 ? STT_COMMON
: STT_OBJECT
);
8373 type
= ((flags
& BSF_ELF_COMMON
) != 0
8374 ? STT_COMMON
: STT_OBJECT
);
8376 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8378 else if (bfd_is_und_section (syms
[idx
]->section
))
8379 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8383 else if (flags
& BSF_FILE
)
8384 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8387 int bind
= STB_LOCAL
;
8389 if (flags
& BSF_LOCAL
)
8391 else if (flags
& BSF_GNU_UNIQUE
)
8392 bind
= STB_GNU_UNIQUE
;
8393 else if (flags
& BSF_WEAK
)
8395 else if (flags
& BSF_GLOBAL
)
8398 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8401 if (type_ptr
!= NULL
)
8403 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8404 sym
.st_target_internal
8405 = type_ptr
->internal_elf_sym
.st_target_internal
;
8410 sym
.st_target_internal
= 0;
8414 symstrtab
[idx
].sym
= sym
;
8415 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8416 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8418 outbound_syms_index
++;
8419 if (outbound_shndx
!= NULL
)
8420 outbound_shndx_index
++;
8423 /* Finalize the .strtab section. */
8424 _bfd_elf_strtab_finalize (stt
);
8426 /* Swap out the .strtab section. */
8427 for (idx
= 0; idx
<= symcount
; idx
++)
8429 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8430 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8431 elfsym
->sym
.st_name
= 0;
8433 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8434 elfsym
->sym
.st_name
);
8435 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8437 + (elfsym
->dest_index
8438 * bed
->s
->sizeof_sym
)),
8440 + (elfsym
->destshndx_index
8441 * sizeof (Elf_External_Sym_Shndx
))));
8446 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8447 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8448 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8449 symstrtab_hdr
->sh_addr
= 0;
8450 symstrtab_hdr
->sh_entsize
= 0;
8451 symstrtab_hdr
->sh_link
= 0;
8452 symstrtab_hdr
->sh_info
= 0;
8453 symstrtab_hdr
->sh_addralign
= 1;
8458 /* Return the number of bytes required to hold the symtab vector.
8460 Note that we base it on the count plus 1, since we will null terminate
8461 the vector allocated based on this size. However, the ELF symbol table
8462 always has a dummy entry as symbol #0, so it ends up even. */
8465 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8467 bfd_size_type symcount
;
8469 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8471 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8472 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8474 bfd_set_error (bfd_error_file_too_big
);
8477 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8479 symtab_size
-= sizeof (asymbol
*);
8485 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8487 bfd_size_type symcount
;
8489 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8491 if (elf_dynsymtab (abfd
) == 0)
8493 bfd_set_error (bfd_error_invalid_operation
);
8497 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8498 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8500 bfd_set_error (bfd_error_file_too_big
);
8503 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8505 symtab_size
-= sizeof (asymbol
*);
8511 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8514 #if SIZEOF_LONG == SIZEOF_INT
8515 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8517 bfd_set_error (bfd_error_file_too_big
);
8521 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8524 /* Canonicalize the relocs. */
8527 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8534 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8536 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8539 tblptr
= section
->relocation
;
8540 for (i
= 0; i
< section
->reloc_count
; i
++)
8541 *relptr
++ = tblptr
++;
8545 return section
->reloc_count
;
8549 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8551 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8552 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8555 abfd
->symcount
= symcount
;
8560 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8561 asymbol
**allocation
)
8563 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8564 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8567 abfd
->dynsymcount
= symcount
;
8571 /* Return the size required for the dynamic reloc entries. Any loadable
8572 section that was actually installed in the BFD, and has type SHT_REL
8573 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8574 dynamic reloc section. */
8577 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8579 bfd_size_type count
;
8582 if (elf_dynsymtab (abfd
) == 0)
8584 bfd_set_error (bfd_error_invalid_operation
);
8589 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8590 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8591 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8592 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8594 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8595 if (count
> LONG_MAX
/ sizeof (arelent
*))
8597 bfd_set_error (bfd_error_file_too_big
);
8601 return count
* sizeof (arelent
*);
8604 /* Canonicalize the dynamic relocation entries. Note that we return the
8605 dynamic relocations as a single block, although they are actually
8606 associated with particular sections; the interface, which was
8607 designed for SunOS style shared libraries, expects that there is only
8608 one set of dynamic relocs. Any loadable section that was actually
8609 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8610 dynamic symbol table, is considered to be a dynamic reloc section. */
8613 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8617 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8621 if (elf_dynsymtab (abfd
) == 0)
8623 bfd_set_error (bfd_error_invalid_operation
);
8627 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8629 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8631 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8632 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8633 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8638 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8640 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8642 for (i
= 0; i
< count
; i
++)
8653 /* Read in the version information. */
8656 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8658 bfd_byte
*contents
= NULL
;
8659 unsigned int freeidx
= 0;
8662 if (elf_dynverref (abfd
) != 0)
8664 Elf_Internal_Shdr
*hdr
;
8665 Elf_External_Verneed
*everneed
;
8666 Elf_Internal_Verneed
*iverneed
;
8668 bfd_byte
*contents_end
;
8670 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8672 if (hdr
->sh_info
== 0
8673 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8675 error_return_bad_verref
:
8677 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8678 bfd_set_error (bfd_error_bad_value
);
8679 error_return_verref
:
8680 elf_tdata (abfd
)->verref
= NULL
;
8681 elf_tdata (abfd
)->cverrefs
= 0;
8685 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8686 goto error_return_verref
;
8687 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8688 if (contents
== NULL
)
8689 goto error_return_verref
;
8691 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8693 bfd_set_error (bfd_error_file_too_big
);
8694 goto error_return_verref
;
8696 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8697 if (elf_tdata (abfd
)->verref
== NULL
)
8698 goto error_return_verref
;
8700 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8701 == sizeof (Elf_External_Vernaux
));
8702 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8703 everneed
= (Elf_External_Verneed
*) contents
;
8704 iverneed
= elf_tdata (abfd
)->verref
;
8705 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8707 Elf_External_Vernaux
*evernaux
;
8708 Elf_Internal_Vernaux
*ivernaux
;
8711 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8713 iverneed
->vn_bfd
= abfd
;
8715 iverneed
->vn_filename
=
8716 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8718 if (iverneed
->vn_filename
== NULL
)
8719 goto error_return_bad_verref
;
8721 if (iverneed
->vn_cnt
== 0)
8722 iverneed
->vn_auxptr
= NULL
;
8725 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8726 sizeof (Elf_Internal_Vernaux
), &amt
))
8728 bfd_set_error (bfd_error_file_too_big
);
8729 goto error_return_verref
;
8731 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8732 bfd_alloc (abfd
, amt
);
8733 if (iverneed
->vn_auxptr
== NULL
)
8734 goto error_return_verref
;
8737 if (iverneed
->vn_aux
8738 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8739 goto error_return_bad_verref
;
8741 evernaux
= ((Elf_External_Vernaux
*)
8742 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8743 ivernaux
= iverneed
->vn_auxptr
;
8744 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8746 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8748 ivernaux
->vna_nodename
=
8749 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8750 ivernaux
->vna_name
);
8751 if (ivernaux
->vna_nodename
== NULL
)
8752 goto error_return_bad_verref
;
8754 if (ivernaux
->vna_other
> freeidx
)
8755 freeidx
= ivernaux
->vna_other
;
8757 ivernaux
->vna_nextptr
= NULL
;
8758 if (ivernaux
->vna_next
== 0)
8760 iverneed
->vn_cnt
= j
+ 1;
8763 if (j
+ 1 < iverneed
->vn_cnt
)
8764 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8766 if (ivernaux
->vna_next
8767 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8768 goto error_return_bad_verref
;
8770 evernaux
= ((Elf_External_Vernaux
*)
8771 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8774 iverneed
->vn_nextref
= NULL
;
8775 if (iverneed
->vn_next
== 0)
8777 if (i
+ 1 < hdr
->sh_info
)
8778 iverneed
->vn_nextref
= iverneed
+ 1;
8780 if (iverneed
->vn_next
8781 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8782 goto error_return_bad_verref
;
8784 everneed
= ((Elf_External_Verneed
*)
8785 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8787 elf_tdata (abfd
)->cverrefs
= i
;
8793 if (elf_dynverdef (abfd
) != 0)
8795 Elf_Internal_Shdr
*hdr
;
8796 Elf_External_Verdef
*everdef
;
8797 Elf_Internal_Verdef
*iverdef
;
8798 Elf_Internal_Verdef
*iverdefarr
;
8799 Elf_Internal_Verdef iverdefmem
;
8801 unsigned int maxidx
;
8802 bfd_byte
*contents_end_def
, *contents_end_aux
;
8804 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8806 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8808 error_return_bad_verdef
:
8810 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8811 bfd_set_error (bfd_error_bad_value
);
8812 error_return_verdef
:
8813 elf_tdata (abfd
)->verdef
= NULL
;
8814 elf_tdata (abfd
)->cverdefs
= 0;
8818 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8819 goto error_return_verdef
;
8820 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8821 if (contents
== NULL
)
8822 goto error_return_verdef
;
8824 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8825 >= sizeof (Elf_External_Verdaux
));
8826 contents_end_def
= contents
+ hdr
->sh_size
8827 - sizeof (Elf_External_Verdef
);
8828 contents_end_aux
= contents
+ hdr
->sh_size
8829 - sizeof (Elf_External_Verdaux
);
8831 /* We know the number of entries in the section but not the maximum
8832 index. Therefore we have to run through all entries and find
8834 everdef
= (Elf_External_Verdef
*) contents
;
8836 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8838 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8840 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8841 goto error_return_bad_verdef
;
8842 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8843 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8845 if (iverdefmem
.vd_next
== 0)
8848 if (iverdefmem
.vd_next
8849 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8850 goto error_return_bad_verdef
;
8852 everdef
= ((Elf_External_Verdef
*)
8853 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8856 if (default_imported_symver
)
8858 if (freeidx
> maxidx
)
8863 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8865 bfd_set_error (bfd_error_file_too_big
);
8866 goto error_return_verdef
;
8868 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8869 if (elf_tdata (abfd
)->verdef
== NULL
)
8870 goto error_return_verdef
;
8872 elf_tdata (abfd
)->cverdefs
= maxidx
;
8874 everdef
= (Elf_External_Verdef
*) contents
;
8875 iverdefarr
= elf_tdata (abfd
)->verdef
;
8876 for (i
= 0; i
< hdr
->sh_info
; i
++)
8878 Elf_External_Verdaux
*everdaux
;
8879 Elf_Internal_Verdaux
*iverdaux
;
8882 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8884 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8885 goto error_return_bad_verdef
;
8887 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8888 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8890 iverdef
->vd_bfd
= abfd
;
8892 if (iverdef
->vd_cnt
== 0)
8893 iverdef
->vd_auxptr
= NULL
;
8896 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
8897 sizeof (Elf_Internal_Verdaux
), &amt
))
8899 bfd_set_error (bfd_error_file_too_big
);
8900 goto error_return_verdef
;
8902 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8903 bfd_alloc (abfd
, amt
);
8904 if (iverdef
->vd_auxptr
== NULL
)
8905 goto error_return_verdef
;
8909 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8910 goto error_return_bad_verdef
;
8912 everdaux
= ((Elf_External_Verdaux
*)
8913 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8914 iverdaux
= iverdef
->vd_auxptr
;
8915 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8917 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8919 iverdaux
->vda_nodename
=
8920 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8921 iverdaux
->vda_name
);
8922 if (iverdaux
->vda_nodename
== NULL
)
8923 goto error_return_bad_verdef
;
8925 iverdaux
->vda_nextptr
= NULL
;
8926 if (iverdaux
->vda_next
== 0)
8928 iverdef
->vd_cnt
= j
+ 1;
8931 if (j
+ 1 < iverdef
->vd_cnt
)
8932 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8934 if (iverdaux
->vda_next
8935 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8936 goto error_return_bad_verdef
;
8938 everdaux
= ((Elf_External_Verdaux
*)
8939 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8942 iverdef
->vd_nodename
= NULL
;
8943 if (iverdef
->vd_cnt
)
8944 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8946 iverdef
->vd_nextdef
= NULL
;
8947 if (iverdef
->vd_next
== 0)
8949 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8950 iverdef
->vd_nextdef
= iverdef
+ 1;
8952 everdef
= ((Elf_External_Verdef
*)
8953 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8959 else if (default_imported_symver
)
8966 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8968 bfd_set_error (bfd_error_file_too_big
);
8971 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8972 if (elf_tdata (abfd
)->verdef
== NULL
)
8975 elf_tdata (abfd
)->cverdefs
= freeidx
;
8978 /* Create a default version based on the soname. */
8979 if (default_imported_symver
)
8981 Elf_Internal_Verdef
*iverdef
;
8982 Elf_Internal_Verdaux
*iverdaux
;
8984 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8986 iverdef
->vd_version
= VER_DEF_CURRENT
;
8987 iverdef
->vd_flags
= 0;
8988 iverdef
->vd_ndx
= freeidx
;
8989 iverdef
->vd_cnt
= 1;
8991 iverdef
->vd_bfd
= abfd
;
8993 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8994 if (iverdef
->vd_nodename
== NULL
)
8995 goto error_return_verdef
;
8996 iverdef
->vd_nextdef
= NULL
;
8997 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8998 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8999 if (iverdef
->vd_auxptr
== NULL
)
9000 goto error_return_verdef
;
9002 iverdaux
= iverdef
->vd_auxptr
;
9003 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9009 if (contents
!= NULL
)
9015 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9017 elf_symbol_type
*newsym
;
9019 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9022 newsym
->symbol
.the_bfd
= abfd
;
9023 return &newsym
->symbol
;
9027 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9031 bfd_symbol_info (symbol
, ret
);
9034 /* Return whether a symbol name implies a local symbol. Most targets
9035 use this function for the is_local_label_name entry point, but some
9039 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9042 /* Normal local symbols start with ``.L''. */
9043 if (name
[0] == '.' && name
[1] == 'L')
9046 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9047 DWARF debugging symbols starting with ``..''. */
9048 if (name
[0] == '.' && name
[1] == '.')
9051 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9052 emitting DWARF debugging output. I suspect this is actually a
9053 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9054 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9055 underscore to be emitted on some ELF targets). For ease of use,
9056 we treat such symbols as local. */
9057 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9060 /* Treat assembler generated fake symbols, dollar local labels and
9061 forward-backward labels (aka local labels) as locals.
9062 These labels have the form:
9064 L0^A.* (fake symbols)
9066 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9068 Versions which start with .L will have already been matched above,
9069 so we only need to match the rest. */
9070 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9072 bfd_boolean ret
= FALSE
;
9076 for (p
= name
+ 2; (c
= *p
); p
++)
9078 if (c
== 1 || c
== 2)
9080 if (c
== 1 && p
== name
+ 2)
9081 /* A fake symbol. */
9084 /* FIXME: We are being paranoid here and treating symbols like
9085 L0^Bfoo as if there were non-local, on the grounds that the
9086 assembler will never generate them. But can any symbol
9087 containing an ASCII value in the range 1-31 ever be anything
9088 other than some kind of local ? */
9105 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9106 asymbol
*symbol ATTRIBUTE_UNUSED
)
9113 _bfd_elf_set_arch_mach (bfd
*abfd
,
9114 enum bfd_architecture arch
,
9115 unsigned long machine
)
9117 /* If this isn't the right architecture for this backend, and this
9118 isn't the generic backend, fail. */
9119 if (arch
!= get_elf_backend_data (abfd
)->arch
9120 && arch
!= bfd_arch_unknown
9121 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9124 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9127 /* Find the nearest line to a particular section and offset,
9128 for error reporting. */
9131 _bfd_elf_find_nearest_line (bfd
*abfd
,
9135 const char **filename_ptr
,
9136 const char **functionname_ptr
,
9137 unsigned int *line_ptr
,
9138 unsigned int *discriminator_ptr
)
9142 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
9143 filename_ptr
, functionname_ptr
,
9144 line_ptr
, discriminator_ptr
,
9145 dwarf_debug_sections
,
9146 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9149 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9150 filename_ptr
, functionname_ptr
, line_ptr
))
9152 if (!*functionname_ptr
)
9153 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9154 *filename_ptr
? NULL
: filename_ptr
,
9159 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9160 &found
, filename_ptr
,
9161 functionname_ptr
, line_ptr
,
9162 &elf_tdata (abfd
)->line_info
))
9164 if (found
&& (*functionname_ptr
|| *line_ptr
))
9167 if (symbols
== NULL
)
9170 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9171 filename_ptr
, functionname_ptr
))
9178 /* Find the line for a symbol. */
9181 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9182 const char **filename_ptr
, unsigned int *line_ptr
)
9184 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9185 filename_ptr
, NULL
, line_ptr
, NULL
,
9186 dwarf_debug_sections
,
9187 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9190 /* After a call to bfd_find_nearest_line, successive calls to
9191 bfd_find_inliner_info can be used to get source information about
9192 each level of function inlining that terminated at the address
9193 passed to bfd_find_nearest_line. Currently this is only supported
9194 for DWARF2 with appropriate DWARF3 extensions. */
9197 _bfd_elf_find_inliner_info (bfd
*abfd
,
9198 const char **filename_ptr
,
9199 const char **functionname_ptr
,
9200 unsigned int *line_ptr
)
9203 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9204 functionname_ptr
, line_ptr
,
9205 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9210 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9212 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9213 int ret
= bed
->s
->sizeof_ehdr
;
9215 if (!bfd_link_relocatable (info
))
9217 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9219 if (phdr_size
== (bfd_size_type
) -1)
9221 struct elf_segment_map
*m
;
9224 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9225 phdr_size
+= bed
->s
->sizeof_phdr
;
9228 phdr_size
= get_program_header_size (abfd
, info
);
9231 elf_program_header_size (abfd
) = phdr_size
;
9239 _bfd_elf_set_section_contents (bfd
*abfd
,
9241 const void *location
,
9243 bfd_size_type count
)
9245 Elf_Internal_Shdr
*hdr
;
9248 if (! abfd
->output_has_begun
9249 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9255 hdr
= &elf_section_data (section
)->this_hdr
;
9256 if (hdr
->sh_offset
== (file_ptr
) -1)
9258 unsigned char *contents
;
9260 if (bfd_section_is_ctf (section
))
9261 /* Nothing to do with this section: the contents are generated
9265 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9268 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9270 bfd_set_error (bfd_error_invalid_operation
);
9274 if ((offset
+ count
) > hdr
->sh_size
)
9277 (_("%pB:%pA: error: attempting to write over the end of the section"),
9280 bfd_set_error (bfd_error_invalid_operation
);
9284 contents
= hdr
->contents
;
9285 if (contents
== NULL
)
9288 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9291 bfd_set_error (bfd_error_invalid_operation
);
9295 memcpy (contents
+ offset
, location
, count
);
9299 pos
= hdr
->sh_offset
+ offset
;
9300 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9301 || bfd_bwrite (location
, count
, abfd
) != count
)
9308 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9309 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9310 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9316 /* Try to convert a non-ELF reloc into an ELF one. */
9319 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9321 /* Check whether we really have an ELF howto. */
9323 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9325 bfd_reloc_code_real_type code
;
9326 reloc_howto_type
*howto
;
9328 /* Alien reloc: Try to determine its type to replace it with an
9329 equivalent ELF reloc. */
9331 if (areloc
->howto
->pc_relative
)
9333 switch (areloc
->howto
->bitsize
)
9336 code
= BFD_RELOC_8_PCREL
;
9339 code
= BFD_RELOC_12_PCREL
;
9342 code
= BFD_RELOC_16_PCREL
;
9345 code
= BFD_RELOC_24_PCREL
;
9348 code
= BFD_RELOC_32_PCREL
;
9351 code
= BFD_RELOC_64_PCREL
;
9357 howto
= bfd_reloc_type_lookup (abfd
, code
);
9359 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9361 if (howto
->pcrel_offset
)
9362 areloc
->addend
+= areloc
->address
;
9364 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9369 switch (areloc
->howto
->bitsize
)
9375 code
= BFD_RELOC_14
;
9378 code
= BFD_RELOC_16
;
9381 code
= BFD_RELOC_26
;
9384 code
= BFD_RELOC_32
;
9387 code
= BFD_RELOC_64
;
9393 howto
= bfd_reloc_type_lookup (abfd
, code
);
9397 areloc
->howto
= howto
;
9405 /* xgettext:c-format */
9406 _bfd_error_handler (_("%pB: %s unsupported"),
9407 abfd
, areloc
->howto
->name
);
9408 bfd_set_error (bfd_error_sorry
);
9413 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9415 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9416 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9418 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9419 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9420 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9423 return _bfd_generic_close_and_cleanup (abfd
);
9426 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9427 in the relocation's offset. Thus we cannot allow any sort of sanity
9428 range-checking to interfere. There is nothing else to do in processing
9431 bfd_reloc_status_type
9432 _bfd_elf_rel_vtable_reloc_fn
9433 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9434 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9435 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9436 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9438 return bfd_reloc_ok
;
9441 /* Elf core file support. Much of this only works on native
9442 toolchains, since we rely on knowing the
9443 machine-dependent procfs structure in order to pick
9444 out details about the corefile. */
9446 #ifdef HAVE_SYS_PROCFS_H
9447 /* Needed for new procfs interface on sparc-solaris. */
9448 # define _STRUCTURED_PROC 1
9449 # include <sys/procfs.h>
9452 /* Return a PID that identifies a "thread" for threaded cores, or the
9453 PID of the main process for non-threaded cores. */
9456 elfcore_make_pid (bfd
*abfd
)
9460 pid
= elf_tdata (abfd
)->core
->lwpid
;
9462 pid
= elf_tdata (abfd
)->core
->pid
;
9467 /* If there isn't a section called NAME, make one, using
9468 data from SECT. Note, this function will generate a
9469 reference to NAME, so you shouldn't deallocate or
9473 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9477 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9480 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9484 sect2
->size
= sect
->size
;
9485 sect2
->filepos
= sect
->filepos
;
9486 sect2
->alignment_power
= sect
->alignment_power
;
9490 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9491 actually creates up to two pseudosections:
9492 - For the single-threaded case, a section named NAME, unless
9493 such a section already exists.
9494 - For the multi-threaded case, a section named "NAME/PID", where
9495 PID is elfcore_make_pid (abfd).
9496 Both pseudosections have identical contents. */
9498 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9504 char *threaded_name
;
9508 /* Build the section name. */
9510 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9511 len
= strlen (buf
) + 1;
9512 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9513 if (threaded_name
== NULL
)
9515 memcpy (threaded_name
, buf
, len
);
9517 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9522 sect
->filepos
= filepos
;
9523 sect
->alignment_power
= 2;
9525 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9529 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9532 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9538 sect
->size
= note
->descsz
- offs
;
9539 sect
->filepos
= note
->descpos
+ offs
;
9540 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9545 /* prstatus_t exists on:
9547 linux 2.[01] + glibc
9551 #if defined (HAVE_PRSTATUS_T)
9554 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9559 if (note
->descsz
== sizeof (prstatus_t
))
9563 size
= sizeof (prstat
.pr_reg
);
9564 offset
= offsetof (prstatus_t
, pr_reg
);
9565 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9567 /* Do not overwrite the core signal if it
9568 has already been set by another thread. */
9569 if (elf_tdata (abfd
)->core
->signal
== 0)
9570 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9571 if (elf_tdata (abfd
)->core
->pid
== 0)
9572 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9574 /* pr_who exists on:
9577 pr_who doesn't exist on:
9580 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9581 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9583 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9586 #if defined (HAVE_PRSTATUS32_T)
9587 else if (note
->descsz
== sizeof (prstatus32_t
))
9589 /* 64-bit host, 32-bit corefile */
9590 prstatus32_t prstat
;
9592 size
= sizeof (prstat
.pr_reg
);
9593 offset
= offsetof (prstatus32_t
, pr_reg
);
9594 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9596 /* Do not overwrite the core signal if it
9597 has already been set by another thread. */
9598 if (elf_tdata (abfd
)->core
->signal
== 0)
9599 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9600 if (elf_tdata (abfd
)->core
->pid
== 0)
9601 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9603 /* pr_who exists on:
9606 pr_who doesn't exist on:
9609 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9610 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9612 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9615 #endif /* HAVE_PRSTATUS32_T */
9618 /* Fail - we don't know how to handle any other
9619 note size (ie. data object type). */
9623 /* Make a ".reg/999" section and a ".reg" section. */
9624 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9625 size
, note
->descpos
+ offset
);
9627 #endif /* defined (HAVE_PRSTATUS_T) */
9629 /* Create a pseudosection containing the exact contents of NOTE. */
9631 elfcore_make_note_pseudosection (bfd
*abfd
,
9633 Elf_Internal_Note
*note
)
9635 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9636 note
->descsz
, note
->descpos
);
9639 /* There isn't a consistent prfpregset_t across platforms,
9640 but it doesn't matter, because we don't have to pick this
9641 data structure apart. */
9644 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9646 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9649 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9650 type of NT_PRXFPREG. Just include the whole note's contents
9654 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9656 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9659 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9660 with a note type of NT_X86_XSTATE. Just include the whole note's
9661 contents literally. */
9664 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9666 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9670 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9672 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9676 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9678 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9682 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9684 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9688 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9690 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9694 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9696 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9700 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9702 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9706 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9708 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9712 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9714 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9718 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9720 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9724 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9726 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9730 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9732 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9736 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9738 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9742 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9744 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9748 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9750 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9754 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9756 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9760 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9762 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9766 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9768 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9772 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9774 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9778 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9780 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9784 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9786 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9790 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9792 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9796 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9798 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9802 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9804 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9808 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9810 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9814 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9816 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9820 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9822 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9826 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9828 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9832 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9834 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9838 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9840 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9844 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9846 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9850 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9852 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9856 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9858 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9862 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9864 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9868 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9870 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9874 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
9876 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
9879 #if defined (HAVE_PRPSINFO_T)
9880 typedef prpsinfo_t elfcore_psinfo_t
;
9881 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9882 typedef prpsinfo32_t elfcore_psinfo32_t
;
9886 #if defined (HAVE_PSINFO_T)
9887 typedef psinfo_t elfcore_psinfo_t
;
9888 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9889 typedef psinfo32_t elfcore_psinfo32_t
;
9893 /* return a malloc'ed copy of a string at START which is at
9894 most MAX bytes long, possibly without a terminating '\0'.
9895 the copy will always have a terminating '\0'. */
9898 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9901 char *end
= (char *) memchr (start
, '\0', max
);
9909 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9913 memcpy (dups
, start
, len
);
9919 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9921 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9923 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9925 elfcore_psinfo_t psinfo
;
9927 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9929 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9930 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9932 elf_tdata (abfd
)->core
->program
9933 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9934 sizeof (psinfo
.pr_fname
));
9936 elf_tdata (abfd
)->core
->command
9937 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9938 sizeof (psinfo
.pr_psargs
));
9940 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9941 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9943 /* 64-bit host, 32-bit corefile */
9944 elfcore_psinfo32_t psinfo
;
9946 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9948 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9949 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9951 elf_tdata (abfd
)->core
->program
9952 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9953 sizeof (psinfo
.pr_fname
));
9955 elf_tdata (abfd
)->core
->command
9956 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9957 sizeof (psinfo
.pr_psargs
));
9963 /* Fail - we don't know how to handle any other
9964 note size (ie. data object type). */
9968 /* Note that for some reason, a spurious space is tacked
9969 onto the end of the args in some (at least one anyway)
9970 implementations, so strip it off if it exists. */
9973 char *command
= elf_tdata (abfd
)->core
->command
;
9974 int n
= strlen (command
);
9976 if (0 < n
&& command
[n
- 1] == ' ')
9977 command
[n
- 1] = '\0';
9982 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9984 #if defined (HAVE_PSTATUS_T)
9986 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9988 if (note
->descsz
== sizeof (pstatus_t
)
9989 #if defined (HAVE_PXSTATUS_T)
9990 || note
->descsz
== sizeof (pxstatus_t
)
9996 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9998 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10000 #if defined (HAVE_PSTATUS32_T)
10001 else if (note
->descsz
== sizeof (pstatus32_t
))
10003 /* 64-bit host, 32-bit corefile */
10006 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10008 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10011 /* Could grab some more details from the "representative"
10012 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10013 NT_LWPSTATUS note, presumably. */
10017 #endif /* defined (HAVE_PSTATUS_T) */
10019 #if defined (HAVE_LWPSTATUS_T)
10021 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10023 lwpstatus_t lwpstat
;
10029 if (note
->descsz
!= sizeof (lwpstat
)
10030 #if defined (HAVE_LWPXSTATUS_T)
10031 && note
->descsz
!= sizeof (lwpxstatus_t
)
10036 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10038 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10039 /* Do not overwrite the core signal if it has already been set by
10041 if (elf_tdata (abfd
)->core
->signal
== 0)
10042 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10044 /* Make a ".reg/999" section. */
10046 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10047 len
= strlen (buf
) + 1;
10048 name
= bfd_alloc (abfd
, len
);
10051 memcpy (name
, buf
, len
);
10053 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10057 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10058 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10059 sect
->filepos
= note
->descpos
10060 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10063 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10064 sect
->size
= sizeof (lwpstat
.pr_reg
);
10065 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10068 sect
->alignment_power
= 2;
10070 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10073 /* Make a ".reg2/999" section */
10075 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10076 len
= strlen (buf
) + 1;
10077 name
= bfd_alloc (abfd
, len
);
10080 memcpy (name
, buf
, len
);
10082 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10086 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10087 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10088 sect
->filepos
= note
->descpos
10089 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10092 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10093 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10094 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10097 sect
->alignment_power
= 2;
10099 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10101 #endif /* defined (HAVE_LWPSTATUS_T) */
10104 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10111 int is_active_thread
;
10114 if (note
->descsz
< 728)
10117 if (! CONST_STRNEQ (note
->namedata
, "win32"))
10120 type
= bfd_get_32 (abfd
, note
->descdata
);
10124 case 1 /* NOTE_INFO_PROCESS */:
10125 /* FIXME: need to add ->core->command. */
10126 /* process_info.pid */
10127 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10128 /* process_info.signal */
10129 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10132 case 2 /* NOTE_INFO_THREAD */:
10133 /* Make a ".reg/999" section. */
10134 /* thread_info.tid */
10135 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
10137 len
= strlen (buf
) + 1;
10138 name
= (char *) bfd_alloc (abfd
, len
);
10142 memcpy (name
, buf
, len
);
10144 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10148 /* sizeof (thread_info.thread_context) */
10150 /* offsetof (thread_info.thread_context) */
10151 sect
->filepos
= note
->descpos
+ 12;
10152 sect
->alignment_power
= 2;
10154 /* thread_info.is_active_thread */
10155 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10157 if (is_active_thread
)
10158 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10162 case 3 /* NOTE_INFO_MODULE */:
10163 /* Make a ".module/xxxxxxxx" section. */
10164 /* module_info.base_address */
10165 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10166 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10168 len
= strlen (buf
) + 1;
10169 name
= (char *) bfd_alloc (abfd
, len
);
10173 memcpy (name
, buf
, len
);
10175 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10180 sect
->size
= note
->descsz
;
10181 sect
->filepos
= note
->descpos
;
10182 sect
->alignment_power
= 2;
10193 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10195 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10197 switch (note
->type
)
10203 if (bed
->elf_backend_grok_prstatus
)
10204 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10206 #if defined (HAVE_PRSTATUS_T)
10207 return elfcore_grok_prstatus (abfd
, note
);
10212 #if defined (HAVE_PSTATUS_T)
10214 return elfcore_grok_pstatus (abfd
, note
);
10217 #if defined (HAVE_LWPSTATUS_T)
10219 return elfcore_grok_lwpstatus (abfd
, note
);
10222 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10223 return elfcore_grok_prfpreg (abfd
, note
);
10225 case NT_WIN32PSTATUS
:
10226 return elfcore_grok_win32pstatus (abfd
, note
);
10228 case NT_PRXFPREG
: /* Linux SSE extension */
10229 if (note
->namesz
== 6
10230 && strcmp (note
->namedata
, "LINUX") == 0)
10231 return elfcore_grok_prxfpreg (abfd
, note
);
10235 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10236 if (note
->namesz
== 6
10237 && strcmp (note
->namedata
, "LINUX") == 0)
10238 return elfcore_grok_xstatereg (abfd
, note
);
10243 if (note
->namesz
== 6
10244 && strcmp (note
->namedata
, "LINUX") == 0)
10245 return elfcore_grok_ppc_vmx (abfd
, note
);
10250 if (note
->namesz
== 6
10251 && strcmp (note
->namedata
, "LINUX") == 0)
10252 return elfcore_grok_ppc_vsx (abfd
, note
);
10257 if (note
->namesz
== 6
10258 && strcmp (note
->namedata
, "LINUX") == 0)
10259 return elfcore_grok_ppc_tar (abfd
, note
);
10264 if (note
->namesz
== 6
10265 && strcmp (note
->namedata
, "LINUX") == 0)
10266 return elfcore_grok_ppc_ppr (abfd
, note
);
10271 if (note
->namesz
== 6
10272 && strcmp (note
->namedata
, "LINUX") == 0)
10273 return elfcore_grok_ppc_dscr (abfd
, note
);
10278 if (note
->namesz
== 6
10279 && strcmp (note
->namedata
, "LINUX") == 0)
10280 return elfcore_grok_ppc_ebb (abfd
, note
);
10285 if (note
->namesz
== 6
10286 && strcmp (note
->namedata
, "LINUX") == 0)
10287 return elfcore_grok_ppc_pmu (abfd
, note
);
10291 case NT_PPC_TM_CGPR
:
10292 if (note
->namesz
== 6
10293 && strcmp (note
->namedata
, "LINUX") == 0)
10294 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10298 case NT_PPC_TM_CFPR
:
10299 if (note
->namesz
== 6
10300 && strcmp (note
->namedata
, "LINUX") == 0)
10301 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10305 case NT_PPC_TM_CVMX
:
10306 if (note
->namesz
== 6
10307 && strcmp (note
->namedata
, "LINUX") == 0)
10308 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10312 case NT_PPC_TM_CVSX
:
10313 if (note
->namesz
== 6
10314 && strcmp (note
->namedata
, "LINUX") == 0)
10315 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10319 case NT_PPC_TM_SPR
:
10320 if (note
->namesz
== 6
10321 && strcmp (note
->namedata
, "LINUX") == 0)
10322 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10326 case NT_PPC_TM_CTAR
:
10327 if (note
->namesz
== 6
10328 && strcmp (note
->namedata
, "LINUX") == 0)
10329 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10333 case NT_PPC_TM_CPPR
:
10334 if (note
->namesz
== 6
10335 && strcmp (note
->namedata
, "LINUX") == 0)
10336 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10340 case NT_PPC_TM_CDSCR
:
10341 if (note
->namesz
== 6
10342 && strcmp (note
->namedata
, "LINUX") == 0)
10343 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10347 case NT_S390_HIGH_GPRS
:
10348 if (note
->namesz
== 6
10349 && strcmp (note
->namedata
, "LINUX") == 0)
10350 return elfcore_grok_s390_high_gprs (abfd
, note
);
10354 case NT_S390_TIMER
:
10355 if (note
->namesz
== 6
10356 && strcmp (note
->namedata
, "LINUX") == 0)
10357 return elfcore_grok_s390_timer (abfd
, note
);
10361 case NT_S390_TODCMP
:
10362 if (note
->namesz
== 6
10363 && strcmp (note
->namedata
, "LINUX") == 0)
10364 return elfcore_grok_s390_todcmp (abfd
, note
);
10368 case NT_S390_TODPREG
:
10369 if (note
->namesz
== 6
10370 && strcmp (note
->namedata
, "LINUX") == 0)
10371 return elfcore_grok_s390_todpreg (abfd
, note
);
10376 if (note
->namesz
== 6
10377 && strcmp (note
->namedata
, "LINUX") == 0)
10378 return elfcore_grok_s390_ctrs (abfd
, note
);
10382 case NT_S390_PREFIX
:
10383 if (note
->namesz
== 6
10384 && strcmp (note
->namedata
, "LINUX") == 0)
10385 return elfcore_grok_s390_prefix (abfd
, note
);
10389 case NT_S390_LAST_BREAK
:
10390 if (note
->namesz
== 6
10391 && strcmp (note
->namedata
, "LINUX") == 0)
10392 return elfcore_grok_s390_last_break (abfd
, note
);
10396 case NT_S390_SYSTEM_CALL
:
10397 if (note
->namesz
== 6
10398 && strcmp (note
->namedata
, "LINUX") == 0)
10399 return elfcore_grok_s390_system_call (abfd
, note
);
10404 if (note
->namesz
== 6
10405 && strcmp (note
->namedata
, "LINUX") == 0)
10406 return elfcore_grok_s390_tdb (abfd
, note
);
10410 case NT_S390_VXRS_LOW
:
10411 if (note
->namesz
== 6
10412 && strcmp (note
->namedata
, "LINUX") == 0)
10413 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10417 case NT_S390_VXRS_HIGH
:
10418 if (note
->namesz
== 6
10419 && strcmp (note
->namedata
, "LINUX") == 0)
10420 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10424 case NT_S390_GS_CB
:
10425 if (note
->namesz
== 6
10426 && strcmp (note
->namedata
, "LINUX") == 0)
10427 return elfcore_grok_s390_gs_cb (abfd
, note
);
10431 case NT_S390_GS_BC
:
10432 if (note
->namesz
== 6
10433 && strcmp (note
->namedata
, "LINUX") == 0)
10434 return elfcore_grok_s390_gs_bc (abfd
, note
);
10439 if (note
->namesz
== 6
10440 && strcmp (note
->namedata
, "LINUX") == 0)
10441 return elfcore_grok_arc_v2 (abfd
, note
);
10446 if (note
->namesz
== 6
10447 && strcmp (note
->namedata
, "LINUX") == 0)
10448 return elfcore_grok_arm_vfp (abfd
, note
);
10453 if (note
->namesz
== 6
10454 && strcmp (note
->namedata
, "LINUX") == 0)
10455 return elfcore_grok_aarch_tls (abfd
, note
);
10459 case NT_ARM_HW_BREAK
:
10460 if (note
->namesz
== 6
10461 && strcmp (note
->namedata
, "LINUX") == 0)
10462 return elfcore_grok_aarch_hw_break (abfd
, note
);
10466 case NT_ARM_HW_WATCH
:
10467 if (note
->namesz
== 6
10468 && strcmp (note
->namedata
, "LINUX") == 0)
10469 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10474 if (note
->namesz
== 6
10475 && strcmp (note
->namedata
, "LINUX") == 0)
10476 return elfcore_grok_aarch_sve (abfd
, note
);
10480 case NT_ARM_PAC_MASK
:
10481 if (note
->namesz
== 6
10482 && strcmp (note
->namedata
, "LINUX") == 0)
10483 return elfcore_grok_aarch_pauth (abfd
, note
);
10489 if (bed
->elf_backend_grok_psinfo
)
10490 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10492 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10493 return elfcore_grok_psinfo (abfd
, note
);
10499 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10502 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10506 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10513 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10515 struct bfd_build_id
* build_id
;
10517 if (note
->descsz
== 0)
10520 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10521 if (build_id
== NULL
)
10524 build_id
->size
= note
->descsz
;
10525 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10526 abfd
->build_id
= build_id
;
10532 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10534 switch (note
->type
)
10539 case NT_GNU_PROPERTY_TYPE_0
:
10540 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10542 case NT_GNU_BUILD_ID
:
10543 return elfobj_grok_gnu_build_id (abfd
, note
);
10548 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10550 struct sdt_note
*cur
=
10551 (struct sdt_note
*) bfd_alloc (abfd
,
10552 sizeof (struct sdt_note
) + note
->descsz
);
10554 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10555 cur
->size
= (bfd_size_type
) note
->descsz
;
10556 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10558 elf_tdata (abfd
)->sdt_note_head
= cur
;
10564 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10566 switch (note
->type
)
10569 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10577 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10581 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10584 if (note
->descsz
< 108)
10589 if (note
->descsz
< 120)
10597 /* Check for version 1 in pr_version. */
10598 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10603 /* Skip over pr_psinfosz. */
10604 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10608 offset
+= 4; /* Padding before pr_psinfosz. */
10612 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10613 elf_tdata (abfd
)->core
->program
10614 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10617 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10618 elf_tdata (abfd
)->core
->command
10619 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10622 /* Padding before pr_pid. */
10625 /* The pr_pid field was added in version "1a". */
10626 if (note
->descsz
< offset
+ 4)
10629 elf_tdata (abfd
)->core
->pid
10630 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10636 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10642 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10643 Also compute minimum size of this note. */
10644 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10648 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10652 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10653 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10660 if (note
->descsz
< min_size
)
10663 /* Check for version 1 in pr_version. */
10664 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10667 /* Extract size of pr_reg from pr_gregsetsz. */
10668 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10669 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10671 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10676 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10680 /* Skip over pr_osreldate. */
10683 /* Read signal from pr_cursig. */
10684 if (elf_tdata (abfd
)->core
->signal
== 0)
10685 elf_tdata (abfd
)->core
->signal
10686 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10689 /* Read TID from pr_pid. */
10690 elf_tdata (abfd
)->core
->lwpid
10691 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10694 /* Padding before pr_reg. */
10695 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10698 /* Make sure that there is enough data remaining in the note. */
10699 if ((note
->descsz
- offset
) < size
)
10702 /* Make a ".reg/999" section and a ".reg" section. */
10703 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10704 size
, note
->descpos
+ offset
);
10708 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10710 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10712 switch (note
->type
)
10715 if (bed
->elf_backend_grok_freebsd_prstatus
)
10716 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10718 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10721 return elfcore_grok_prfpreg (abfd
, note
);
10724 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10726 case NT_FREEBSD_THRMISC
:
10727 if (note
->namesz
== 8)
10728 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10732 case NT_FREEBSD_PROCSTAT_PROC
:
10733 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10736 case NT_FREEBSD_PROCSTAT_FILES
:
10737 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10740 case NT_FREEBSD_PROCSTAT_VMMAP
:
10741 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10744 case NT_FREEBSD_PROCSTAT_AUXV
:
10745 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10747 case NT_X86_XSTATE
:
10748 if (note
->namesz
== 8)
10749 return elfcore_grok_xstatereg (abfd
, note
);
10753 case NT_FREEBSD_PTLWPINFO
:
10754 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10758 return elfcore_grok_arm_vfp (abfd
, note
);
10766 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10770 cp
= strchr (note
->namedata
, '@');
10773 *lwpidp
= atoi(cp
+ 1);
10780 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10782 if (note
->descsz
<= 0x7c + 31)
10785 /* Signal number at offset 0x08. */
10786 elf_tdata (abfd
)->core
->signal
10787 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10789 /* Process ID at offset 0x50. */
10790 elf_tdata (abfd
)->core
->pid
10791 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10793 /* Command name at 0x7c (max 32 bytes, including nul). */
10794 elf_tdata (abfd
)->core
->command
10795 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10797 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10802 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10806 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10807 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10809 switch (note
->type
)
10811 case NT_NETBSDCORE_PROCINFO
:
10812 /* NetBSD-specific core "procinfo". Note that we expect to
10813 find this note before any of the others, which is fine,
10814 since the kernel writes this note out first when it
10815 creates a core file. */
10816 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10817 #ifdef NT_NETBSDCORE_AUXV
10818 case NT_NETBSDCORE_AUXV
:
10819 /* NetBSD-specific Elf Auxiliary Vector data. */
10820 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10822 #ifdef NT_NETBSDCORE_LWPSTATUS
10823 case NT_NETBSDCORE_LWPSTATUS
:
10824 return elfcore_make_note_pseudosection (abfd
,
10825 ".note.netbsdcore.lwpstatus",
10832 /* As of March 2020 there are no other machine-independent notes
10833 defined for NetBSD core files. If the note type is less
10834 than the start of the machine-dependent note types, we don't
10837 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10841 switch (bfd_get_arch (abfd
))
10843 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10844 PT_GETFPREGS == mach+2. */
10846 case bfd_arch_aarch64
:
10847 case bfd_arch_alpha
:
10848 case bfd_arch_sparc
:
10849 switch (note
->type
)
10851 case NT_NETBSDCORE_FIRSTMACH
+0:
10852 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10854 case NT_NETBSDCORE_FIRSTMACH
+2:
10855 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10861 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10862 There's also old PT___GETREGS40 == mach + 1 for old reg
10863 structure which lacks GBR. */
10866 switch (note
->type
)
10868 case NT_NETBSDCORE_FIRSTMACH
+3:
10869 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10871 case NT_NETBSDCORE_FIRSTMACH
+5:
10872 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10878 /* On all other arch's, PT_GETREGS == mach+1 and
10879 PT_GETFPREGS == mach+3. */
10882 switch (note
->type
)
10884 case NT_NETBSDCORE_FIRSTMACH
+1:
10885 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10887 case NT_NETBSDCORE_FIRSTMACH
+3:
10888 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10898 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10900 if (note
->descsz
<= 0x48 + 31)
10903 /* Signal number at offset 0x08. */
10904 elf_tdata (abfd
)->core
->signal
10905 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10907 /* Process ID at offset 0x20. */
10908 elf_tdata (abfd
)->core
->pid
10909 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10911 /* Command name at 0x48 (max 32 bytes, including nul). */
10912 elf_tdata (abfd
)->core
->command
10913 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10919 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10921 if (note
->type
== NT_OPENBSD_PROCINFO
)
10922 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10924 if (note
->type
== NT_OPENBSD_REGS
)
10925 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10927 if (note
->type
== NT_OPENBSD_FPREGS
)
10928 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10930 if (note
->type
== NT_OPENBSD_XFPREGS
)
10931 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10933 if (note
->type
== NT_OPENBSD_AUXV
)
10934 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10936 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10938 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10943 sect
->size
= note
->descsz
;
10944 sect
->filepos
= note
->descpos
;
10945 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10954 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10956 void *ddata
= note
->descdata
;
10963 if (note
->descsz
< 16)
10966 /* nto_procfs_status 'pid' field is at offset 0. */
10967 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10969 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10970 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10972 /* nto_procfs_status 'flags' field is at offset 8. */
10973 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10975 /* nto_procfs_status 'what' field is at offset 14. */
10976 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10978 elf_tdata (abfd
)->core
->signal
= sig
;
10979 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10982 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10983 do not come from signals so we make sure we set the current
10984 thread just in case. */
10985 if (flags
& 0x00000080)
10986 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10988 /* Make a ".qnx_core_status/%d" section. */
10989 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10991 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10994 strcpy (name
, buf
);
10996 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11000 sect
->size
= note
->descsz
;
11001 sect
->filepos
= note
->descpos
;
11002 sect
->alignment_power
= 2;
11004 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
11008 elfcore_grok_nto_regs (bfd
*abfd
,
11009 Elf_Internal_Note
*note
,
11017 /* Make a "(base)/%d" section. */
11018 sprintf (buf
, "%s/%ld", base
, tid
);
11020 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11023 strcpy (name
, buf
);
11025 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11029 sect
->size
= note
->descsz
;
11030 sect
->filepos
= note
->descpos
;
11031 sect
->alignment_power
= 2;
11033 /* This is the current thread. */
11034 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
11035 return elfcore_maybe_make_sect (abfd
, base
, sect
);
11040 #define BFD_QNT_CORE_INFO 7
11041 #define BFD_QNT_CORE_STATUS 8
11042 #define BFD_QNT_CORE_GREG 9
11043 #define BFD_QNT_CORE_FPREG 10
11046 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11048 /* Every GREG section has a STATUS section before it. Store the
11049 tid from the previous call to pass down to the next gregs
11051 static long tid
= 1;
11053 switch (note
->type
)
11055 case BFD_QNT_CORE_INFO
:
11056 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
11057 case BFD_QNT_CORE_STATUS
:
11058 return elfcore_grok_nto_status (abfd
, note
, &tid
);
11059 case BFD_QNT_CORE_GREG
:
11060 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
11061 case BFD_QNT_CORE_FPREG
:
11062 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
11069 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11075 /* Use note name as section name. */
11076 len
= note
->namesz
;
11077 name
= (char *) bfd_alloc (abfd
, len
);
11080 memcpy (name
, note
->namedata
, len
);
11081 name
[len
- 1] = '\0';
11083 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11087 sect
->size
= note
->descsz
;
11088 sect
->filepos
= note
->descpos
;
11089 sect
->alignment_power
= 1;
11094 /* Function: elfcore_write_note
11097 buffer to hold note, and current size of buffer
11101 size of data for note
11103 Writes note to end of buffer. ELF64 notes are written exactly as
11104 for ELF32, despite the current (as of 2006) ELF gabi specifying
11105 that they ought to have 8-byte namesz and descsz field, and have
11106 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11109 Pointer to realloc'd buffer, *BUFSIZ updated. */
11112 elfcore_write_note (bfd
*abfd
,
11120 Elf_External_Note
*xnp
;
11127 namesz
= strlen (name
) + 1;
11129 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11131 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11134 dest
= buf
+ *bufsiz
;
11135 *bufsiz
+= newspace
;
11136 xnp
= (Elf_External_Note
*) dest
;
11137 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11138 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11139 H_PUT_32 (abfd
, type
, xnp
->type
);
11143 memcpy (dest
, name
, namesz
);
11151 memcpy (dest
, input
, size
);
11161 /* gcc-8 warns (*) on all the strncpy calls in this function about
11162 possible string truncation. The "truncation" is not a bug. We
11163 have an external representation of structs with fields that are not
11164 necessarily NULL terminated and corresponding internal
11165 representation fields that are one larger so that they can always
11166 be NULL terminated.
11167 gcc versions between 4.2 and 4.6 do not allow pragma control of
11168 diagnostics inside functions, giving a hard error if you try to use
11169 the finer control available with later versions.
11170 gcc prior to 4.2 warns about diagnostic push and pop.
11171 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11172 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11173 (*) Depending on your system header files! */
11174 #if GCC_VERSION >= 8000
11175 # pragma GCC diagnostic push
11176 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11179 elfcore_write_prpsinfo (bfd
*abfd
,
11183 const char *psargs
)
11185 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11187 if (bed
->elf_backend_write_core_note
!= NULL
)
11190 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11191 NT_PRPSINFO
, fname
, psargs
);
11196 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11197 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11198 if (bed
->s
->elfclass
== ELFCLASS32
)
11200 # if defined (HAVE_PSINFO32_T)
11202 int note_type
= NT_PSINFO
;
11205 int note_type
= NT_PRPSINFO
;
11208 memset (&data
, 0, sizeof (data
));
11209 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11210 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11211 return elfcore_write_note (abfd
, buf
, bufsiz
,
11212 "CORE", note_type
, &data
, sizeof (data
));
11217 # if defined (HAVE_PSINFO_T)
11219 int note_type
= NT_PSINFO
;
11222 int note_type
= NT_PRPSINFO
;
11225 memset (&data
, 0, sizeof (data
));
11226 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11227 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11228 return elfcore_write_note (abfd
, buf
, bufsiz
,
11229 "CORE", note_type
, &data
, sizeof (data
));
11231 #endif /* PSINFO_T or PRPSINFO_T */
11236 #if GCC_VERSION >= 8000
11237 # pragma GCC diagnostic pop
11241 elfcore_write_linux_prpsinfo32
11242 (bfd
*abfd
, char *buf
, int *bufsiz
,
11243 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11245 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11247 struct elf_external_linux_prpsinfo32_ugid16 data
;
11249 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11250 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11251 &data
, sizeof (data
));
11255 struct elf_external_linux_prpsinfo32_ugid32 data
;
11257 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11258 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11259 &data
, sizeof (data
));
11264 elfcore_write_linux_prpsinfo64
11265 (bfd
*abfd
, char *buf
, int *bufsiz
,
11266 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11268 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11270 struct elf_external_linux_prpsinfo64_ugid16 data
;
11272 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11273 return elfcore_write_note (abfd
, buf
, bufsiz
,
11274 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11278 struct elf_external_linux_prpsinfo64_ugid32 data
;
11280 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11281 return elfcore_write_note (abfd
, buf
, bufsiz
,
11282 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11287 elfcore_write_prstatus (bfd
*abfd
,
11294 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11296 if (bed
->elf_backend_write_core_note
!= NULL
)
11299 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11301 pid
, cursig
, gregs
);
11306 #if defined (HAVE_PRSTATUS_T)
11307 #if defined (HAVE_PRSTATUS32_T)
11308 if (bed
->s
->elfclass
== ELFCLASS32
)
11310 prstatus32_t prstat
;
11312 memset (&prstat
, 0, sizeof (prstat
));
11313 prstat
.pr_pid
= pid
;
11314 prstat
.pr_cursig
= cursig
;
11315 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11316 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11317 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11324 memset (&prstat
, 0, sizeof (prstat
));
11325 prstat
.pr_pid
= pid
;
11326 prstat
.pr_cursig
= cursig
;
11327 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11328 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11329 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11331 #endif /* HAVE_PRSTATUS_T */
11337 #if defined (HAVE_LWPSTATUS_T)
11339 elfcore_write_lwpstatus (bfd
*abfd
,
11346 lwpstatus_t lwpstat
;
11347 const char *note_name
= "CORE";
11349 memset (&lwpstat
, 0, sizeof (lwpstat
));
11350 lwpstat
.pr_lwpid
= pid
>> 16;
11351 lwpstat
.pr_cursig
= cursig
;
11352 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11353 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11354 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11355 #if !defined(gregs)
11356 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11357 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11359 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11360 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11363 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11364 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11366 #endif /* HAVE_LWPSTATUS_T */
11368 #if defined (HAVE_PSTATUS_T)
11370 elfcore_write_pstatus (bfd
*abfd
,
11374 int cursig ATTRIBUTE_UNUSED
,
11375 const void *gregs ATTRIBUTE_UNUSED
)
11377 const char *note_name
= "CORE";
11378 #if defined (HAVE_PSTATUS32_T)
11379 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11381 if (bed
->s
->elfclass
== ELFCLASS32
)
11385 memset (&pstat
, 0, sizeof (pstat
));
11386 pstat
.pr_pid
= pid
& 0xffff;
11387 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11388 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11396 memset (&pstat
, 0, sizeof (pstat
));
11397 pstat
.pr_pid
= pid
& 0xffff;
11398 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11399 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11403 #endif /* HAVE_PSTATUS_T */
11406 elfcore_write_prfpreg (bfd
*abfd
,
11409 const void *fpregs
,
11412 const char *note_name
= "CORE";
11413 return elfcore_write_note (abfd
, buf
, bufsiz
,
11414 note_name
, NT_FPREGSET
, fpregs
, size
);
11418 elfcore_write_prxfpreg (bfd
*abfd
,
11421 const void *xfpregs
,
11424 char *note_name
= "LINUX";
11425 return elfcore_write_note (abfd
, buf
, bufsiz
,
11426 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11430 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11431 const void *xfpregs
, int size
)
11434 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11435 note_name
= "FreeBSD";
11437 note_name
= "LINUX";
11438 return elfcore_write_note (abfd
, buf
, bufsiz
,
11439 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11443 elfcore_write_ppc_vmx (bfd
*abfd
,
11446 const void *ppc_vmx
,
11449 char *note_name
= "LINUX";
11450 return elfcore_write_note (abfd
, buf
, bufsiz
,
11451 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11455 elfcore_write_ppc_vsx (bfd
*abfd
,
11458 const void *ppc_vsx
,
11461 char *note_name
= "LINUX";
11462 return elfcore_write_note (abfd
, buf
, bufsiz
,
11463 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11467 elfcore_write_ppc_tar (bfd
*abfd
,
11470 const void *ppc_tar
,
11473 char *note_name
= "LINUX";
11474 return elfcore_write_note (abfd
, buf
, bufsiz
,
11475 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11479 elfcore_write_ppc_ppr (bfd
*abfd
,
11482 const void *ppc_ppr
,
11485 char *note_name
= "LINUX";
11486 return elfcore_write_note (abfd
, buf
, bufsiz
,
11487 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11491 elfcore_write_ppc_dscr (bfd
*abfd
,
11494 const void *ppc_dscr
,
11497 char *note_name
= "LINUX";
11498 return elfcore_write_note (abfd
, buf
, bufsiz
,
11499 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11503 elfcore_write_ppc_ebb (bfd
*abfd
,
11506 const void *ppc_ebb
,
11509 char *note_name
= "LINUX";
11510 return elfcore_write_note (abfd
, buf
, bufsiz
,
11511 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11515 elfcore_write_ppc_pmu (bfd
*abfd
,
11518 const void *ppc_pmu
,
11521 char *note_name
= "LINUX";
11522 return elfcore_write_note (abfd
, buf
, bufsiz
,
11523 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11527 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11530 const void *ppc_tm_cgpr
,
11533 char *note_name
= "LINUX";
11534 return elfcore_write_note (abfd
, buf
, bufsiz
,
11535 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11539 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11542 const void *ppc_tm_cfpr
,
11545 char *note_name
= "LINUX";
11546 return elfcore_write_note (abfd
, buf
, bufsiz
,
11547 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11551 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11554 const void *ppc_tm_cvmx
,
11557 char *note_name
= "LINUX";
11558 return elfcore_write_note (abfd
, buf
, bufsiz
,
11559 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11563 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11566 const void *ppc_tm_cvsx
,
11569 char *note_name
= "LINUX";
11570 return elfcore_write_note (abfd
, buf
, bufsiz
,
11571 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11575 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11578 const void *ppc_tm_spr
,
11581 char *note_name
= "LINUX";
11582 return elfcore_write_note (abfd
, buf
, bufsiz
,
11583 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11587 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11590 const void *ppc_tm_ctar
,
11593 char *note_name
= "LINUX";
11594 return elfcore_write_note (abfd
, buf
, bufsiz
,
11595 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11599 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11602 const void *ppc_tm_cppr
,
11605 char *note_name
= "LINUX";
11606 return elfcore_write_note (abfd
, buf
, bufsiz
,
11607 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11611 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11614 const void *ppc_tm_cdscr
,
11617 char *note_name
= "LINUX";
11618 return elfcore_write_note (abfd
, buf
, bufsiz
,
11619 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11623 elfcore_write_s390_high_gprs (bfd
*abfd
,
11626 const void *s390_high_gprs
,
11629 char *note_name
= "LINUX";
11630 return elfcore_write_note (abfd
, buf
, bufsiz
,
11631 note_name
, NT_S390_HIGH_GPRS
,
11632 s390_high_gprs
, size
);
11636 elfcore_write_s390_timer (bfd
*abfd
,
11639 const void *s390_timer
,
11642 char *note_name
= "LINUX";
11643 return elfcore_write_note (abfd
, buf
, bufsiz
,
11644 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11648 elfcore_write_s390_todcmp (bfd
*abfd
,
11651 const void *s390_todcmp
,
11654 char *note_name
= "LINUX";
11655 return elfcore_write_note (abfd
, buf
, bufsiz
,
11656 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11660 elfcore_write_s390_todpreg (bfd
*abfd
,
11663 const void *s390_todpreg
,
11666 char *note_name
= "LINUX";
11667 return elfcore_write_note (abfd
, buf
, bufsiz
,
11668 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11672 elfcore_write_s390_ctrs (bfd
*abfd
,
11675 const void *s390_ctrs
,
11678 char *note_name
= "LINUX";
11679 return elfcore_write_note (abfd
, buf
, bufsiz
,
11680 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11684 elfcore_write_s390_prefix (bfd
*abfd
,
11687 const void *s390_prefix
,
11690 char *note_name
= "LINUX";
11691 return elfcore_write_note (abfd
, buf
, bufsiz
,
11692 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11696 elfcore_write_s390_last_break (bfd
*abfd
,
11699 const void *s390_last_break
,
11702 char *note_name
= "LINUX";
11703 return elfcore_write_note (abfd
, buf
, bufsiz
,
11704 note_name
, NT_S390_LAST_BREAK
,
11705 s390_last_break
, size
);
11709 elfcore_write_s390_system_call (bfd
*abfd
,
11712 const void *s390_system_call
,
11715 char *note_name
= "LINUX";
11716 return elfcore_write_note (abfd
, buf
, bufsiz
,
11717 note_name
, NT_S390_SYSTEM_CALL
,
11718 s390_system_call
, size
);
11722 elfcore_write_s390_tdb (bfd
*abfd
,
11725 const void *s390_tdb
,
11728 char *note_name
= "LINUX";
11729 return elfcore_write_note (abfd
, buf
, bufsiz
,
11730 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11734 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11737 const void *s390_vxrs_low
,
11740 char *note_name
= "LINUX";
11741 return elfcore_write_note (abfd
, buf
, bufsiz
,
11742 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11746 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11749 const void *s390_vxrs_high
,
11752 char *note_name
= "LINUX";
11753 return elfcore_write_note (abfd
, buf
, bufsiz
,
11754 note_name
, NT_S390_VXRS_HIGH
,
11755 s390_vxrs_high
, size
);
11759 elfcore_write_s390_gs_cb (bfd
*abfd
,
11762 const void *s390_gs_cb
,
11765 char *note_name
= "LINUX";
11766 return elfcore_write_note (abfd
, buf
, bufsiz
,
11767 note_name
, NT_S390_GS_CB
,
11772 elfcore_write_s390_gs_bc (bfd
*abfd
,
11775 const void *s390_gs_bc
,
11778 char *note_name
= "LINUX";
11779 return elfcore_write_note (abfd
, buf
, bufsiz
,
11780 note_name
, NT_S390_GS_BC
,
11785 elfcore_write_arm_vfp (bfd
*abfd
,
11788 const void *arm_vfp
,
11791 char *note_name
= "LINUX";
11792 return elfcore_write_note (abfd
, buf
, bufsiz
,
11793 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11797 elfcore_write_aarch_tls (bfd
*abfd
,
11800 const void *aarch_tls
,
11803 char *note_name
= "LINUX";
11804 return elfcore_write_note (abfd
, buf
, bufsiz
,
11805 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11809 elfcore_write_aarch_hw_break (bfd
*abfd
,
11812 const void *aarch_hw_break
,
11815 char *note_name
= "LINUX";
11816 return elfcore_write_note (abfd
, buf
, bufsiz
,
11817 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11821 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11824 const void *aarch_hw_watch
,
11827 char *note_name
= "LINUX";
11828 return elfcore_write_note (abfd
, buf
, bufsiz
,
11829 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11833 elfcore_write_aarch_sve (bfd
*abfd
,
11836 const void *aarch_sve
,
11839 char *note_name
= "LINUX";
11840 return elfcore_write_note (abfd
, buf
, bufsiz
,
11841 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11845 elfcore_write_aarch_pauth (bfd
*abfd
,
11848 const void *aarch_pauth
,
11851 char *note_name
= "LINUX";
11852 return elfcore_write_note (abfd
, buf
, bufsiz
,
11853 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11857 elfcore_write_arc_v2 (bfd
*abfd
,
11860 const void *arc_v2
,
11863 char *note_name
= "LINUX";
11864 return elfcore_write_note (abfd
, buf
, bufsiz
,
11865 note_name
, NT_ARC_V2
, arc_v2
, size
);
11869 elfcore_write_register_note (bfd
*abfd
,
11872 const char *section
,
11876 if (strcmp (section
, ".reg2") == 0)
11877 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11878 if (strcmp (section
, ".reg-xfp") == 0)
11879 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11880 if (strcmp (section
, ".reg-xstate") == 0)
11881 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11882 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11883 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11884 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11885 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11886 if (strcmp (section
, ".reg-ppc-tar") == 0)
11887 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11888 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11889 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11890 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11891 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11892 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11893 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11894 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11895 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11896 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11897 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11898 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11899 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11900 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11901 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11902 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11903 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11904 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11905 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11906 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11907 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11908 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11909 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11910 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11911 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11912 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11913 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11914 if (strcmp (section
, ".reg-s390-timer") == 0)
11915 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11916 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11917 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11918 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11919 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11920 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11921 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11922 if (strcmp (section
, ".reg-s390-prefix") == 0)
11923 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11924 if (strcmp (section
, ".reg-s390-last-break") == 0)
11925 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11926 if (strcmp (section
, ".reg-s390-system-call") == 0)
11927 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11928 if (strcmp (section
, ".reg-s390-tdb") == 0)
11929 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11930 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11931 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11932 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11933 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11934 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11935 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11936 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11937 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11938 if (strcmp (section
, ".reg-arm-vfp") == 0)
11939 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11940 if (strcmp (section
, ".reg-aarch-tls") == 0)
11941 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11942 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11943 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11944 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11945 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11946 if (strcmp (section
, ".reg-aarch-sve") == 0)
11947 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11948 if (strcmp (section
, ".reg-aarch-pauth") == 0)
11949 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
11950 if (strcmp (section
, ".reg-arc-v2") == 0)
11951 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
11956 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11961 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11962 gABI specifies that PT_NOTE alignment should be aligned to 4
11963 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11964 align is less than 4, we use 4 byte alignment. */
11967 if (align
!= 4 && align
!= 8)
11971 while (p
< buf
+ size
)
11973 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11974 Elf_Internal_Note in
;
11976 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11979 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11981 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11982 in
.namedata
= xnp
->name
;
11983 if (in
.namesz
> buf
- in
.namedata
+ size
)
11986 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11987 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11988 in
.descpos
= offset
+ (in
.descdata
- buf
);
11990 && (in
.descdata
>= buf
+ size
11991 || in
.descsz
> buf
- in
.descdata
+ size
))
11994 switch (bfd_get_format (abfd
))
12001 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12004 const char * string
;
12006 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
12010 GROKER_ELEMENT ("", elfcore_grok_note
),
12011 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
12012 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
12013 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
12014 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
12015 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
12016 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
)
12018 #undef GROKER_ELEMENT
12021 for (i
= ARRAY_SIZE (grokers
); i
--;)
12023 if (in
.namesz
>= grokers
[i
].len
12024 && strncmp (in
.namedata
, grokers
[i
].string
,
12025 grokers
[i
].len
) == 0)
12027 if (! grokers
[i
].func (abfd
, & in
))
12036 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
12038 if (! elfobj_grok_gnu_note (abfd
, &in
))
12041 else if (in
.namesz
== sizeof "stapsdt"
12042 && strcmp (in
.namedata
, "stapsdt") == 0)
12044 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
12050 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
12057 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
12062 if (size
== 0 || (size
+ 1) == 0)
12065 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
12068 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
12072 /* PR 17512: file: ec08f814
12073 0-termintate the buffer so that string searches will not overflow. */
12076 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
12086 /* Providing external access to the ELF program header table. */
12088 /* Return an upper bound on the number of bytes required to store a
12089 copy of ABFD's program header table entries. Return -1 if an error
12090 occurs; bfd_get_error will return an appropriate code. */
12093 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12095 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12097 bfd_set_error (bfd_error_wrong_format
);
12101 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12104 /* Copy ABFD's program header table entries to *PHDRS. The entries
12105 will be stored as an array of Elf_Internal_Phdr structures, as
12106 defined in include/elf/internal.h. To find out how large the
12107 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12109 Return the number of program header table entries read, or -1 if an
12110 error occurs; bfd_get_error will return an appropriate code. */
12113 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12117 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12119 bfd_set_error (bfd_error_wrong_format
);
12123 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12124 if (num_phdrs
!= 0)
12125 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12126 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12131 enum elf_reloc_type_class
12132 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12133 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12134 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12136 return reloc_class_normal
;
12139 /* For RELA architectures, return the relocation value for a
12140 relocation against a local symbol. */
12143 _bfd_elf_rela_local_sym (bfd
*abfd
,
12144 Elf_Internal_Sym
*sym
,
12146 Elf_Internal_Rela
*rel
)
12148 asection
*sec
= *psec
;
12149 bfd_vma relocation
;
12151 relocation
= (sec
->output_section
->vma
12152 + sec
->output_offset
12154 if ((sec
->flags
& SEC_MERGE
)
12155 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12156 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12159 _bfd_merged_section_offset (abfd
, psec
,
12160 elf_section_data (sec
)->sec_info
,
12161 sym
->st_value
+ rel
->r_addend
);
12164 /* If we have changed the section, and our original section is
12165 marked with SEC_EXCLUDE, it means that the original
12166 SEC_MERGE section has been completely subsumed in some
12167 other SEC_MERGE section. In this case, we need to leave
12168 some info around for --emit-relocs. */
12169 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12170 sec
->kept_section
= *psec
;
12173 rel
->r_addend
-= relocation
;
12174 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12180 _bfd_elf_rel_local_sym (bfd
*abfd
,
12181 Elf_Internal_Sym
*sym
,
12185 asection
*sec
= *psec
;
12187 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12188 return sym
->st_value
+ addend
;
12190 return _bfd_merged_section_offset (abfd
, psec
,
12191 elf_section_data (sec
)->sec_info
,
12192 sym
->st_value
+ addend
);
12195 /* Adjust an address within a section. Given OFFSET within SEC, return
12196 the new offset within the section, based upon changes made to the
12197 section. Returns -1 if the offset is now invalid.
12198 The offset (in abnd out) is in target sized bytes, however big a
12202 _bfd_elf_section_offset (bfd
*abfd
,
12203 struct bfd_link_info
*info
,
12207 switch (sec
->sec_info_type
)
12209 case SEC_INFO_TYPE_STABS
:
12210 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12212 case SEC_INFO_TYPE_EH_FRAME
:
12213 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12216 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12218 /* Reverse the offset. */
12219 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12220 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12222 /* address_size and sec->size are in octets. Convert
12223 to bytes before subtracting the original offset. */
12224 offset
= ((sec
->size
- address_size
)
12225 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12231 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12232 reconstruct an ELF file by reading the segments out of remote memory
12233 based on the ELF file header at EHDR_VMA and the ELF program headers it
12234 points to. If not null, *LOADBASEP is filled in with the difference
12235 between the VMAs from which the segments were read, and the VMAs the
12236 file headers (and hence BFD's idea of each section's VMA) put them at.
12238 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12239 remote memory at target address VMA into the local buffer at MYADDR; it
12240 should return zero on success or an `errno' code on failure. TEMPL must
12241 be a BFD for an ELF target with the word size and byte order found in
12242 the remote memory. */
12245 bfd_elf_bfd_from_remote_memory
12248 bfd_size_type size
,
12249 bfd_vma
*loadbasep
,
12250 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12252 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12253 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12257 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12258 long symcount ATTRIBUTE_UNUSED
,
12259 asymbol
**syms ATTRIBUTE_UNUSED
,
12264 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12267 const char *relplt_name
;
12268 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12272 Elf_Internal_Shdr
*hdr
;
12278 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12281 if (dynsymcount
<= 0)
12284 if (!bed
->plt_sym_val
)
12287 relplt_name
= bed
->relplt_name
;
12288 if (relplt_name
== NULL
)
12289 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12290 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12291 if (relplt
== NULL
)
12294 hdr
= &elf_section_data (relplt
)->this_hdr
;
12295 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12296 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12299 plt
= bfd_get_section_by_name (abfd
, ".plt");
12303 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12304 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12307 count
= relplt
->size
/ hdr
->sh_entsize
;
12308 size
= count
* sizeof (asymbol
);
12309 p
= relplt
->relocation
;
12310 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12312 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12313 if (p
->addend
!= 0)
12316 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12318 size
+= sizeof ("+0x") - 1 + 8;
12323 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12327 names
= (char *) (s
+ count
);
12328 p
= relplt
->relocation
;
12330 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12335 addr
= bed
->plt_sym_val (i
, plt
, p
);
12336 if (addr
== (bfd_vma
) -1)
12339 *s
= **p
->sym_ptr_ptr
;
12340 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12341 we are defining a symbol, ensure one of them is set. */
12342 if ((s
->flags
& BSF_LOCAL
) == 0)
12343 s
->flags
|= BSF_GLOBAL
;
12344 s
->flags
|= BSF_SYNTHETIC
;
12346 s
->value
= addr
- plt
->vma
;
12349 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12350 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12352 if (p
->addend
!= 0)
12356 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12357 names
+= sizeof ("+0x") - 1;
12358 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12359 for (a
= buf
; *a
== '0'; ++a
)
12362 memcpy (names
, a
, len
);
12365 memcpy (names
, "@plt", sizeof ("@plt"));
12366 names
+= sizeof ("@plt");
12373 /* It is only used by x86-64 so far.
12374 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12375 but current usage would allow all of _bfd_std_section to be zero. */
12376 static const asymbol lcomm_sym
12377 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12378 asection _bfd_elf_large_com_section
12379 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12380 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12383 _bfd_elf_final_write_processing (bfd
*abfd
)
12385 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12387 i_ehdrp
= elf_elfheader (abfd
);
12389 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12390 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12392 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12393 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12394 STB_GNU_UNIQUE binding. */
12395 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12397 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12398 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12399 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12400 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12402 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12403 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12404 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12405 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12406 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12407 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12408 bfd_set_error (bfd_error_sorry
);
12416 /* Return TRUE for ELF symbol types that represent functions.
12417 This is the default version of this function, which is sufficient for
12418 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12421 _bfd_elf_is_function_type (unsigned int type
)
12423 return (type
== STT_FUNC
12424 || type
== STT_GNU_IFUNC
);
12427 /* If the ELF symbol SYM might be a function in SEC, return the
12428 function size and set *CODE_OFF to the function's entry point,
12429 otherwise return zero. */
12432 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12435 bfd_size_type size
;
12437 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12438 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12439 || sym
->section
!= sec
)
12442 *code_off
= sym
->value
;
12444 if (!(sym
->flags
& BSF_SYNTHETIC
))
12445 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
12451 /* Set to non-zero to enable some debug messages. */
12452 #define DEBUG_SECONDARY_RELOCS 0
12454 /* An internal-to-the-bfd-library only section type
12455 used to indicate a cached secondary reloc section. */
12456 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12458 /* Create a BFD section to hold a secondary reloc section. */
12461 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
12462 Elf_Internal_Shdr
*hdr
,
12464 unsigned int shindex
)
12466 /* We only support RELA secondary relocs. */
12467 if (hdr
->sh_type
!= SHT_RELA
)
12470 #if DEBUG_SECONDARY_RELOCS
12471 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
12473 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
12474 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
12477 /* Read in any secondary relocs associated with SEC. */
12480 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
12482 asymbol
** symbols
)
12484 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12486 bfd_boolean result
= TRUE
;
12487 bfd_vma (*r_sym
) (bfd_vma
);
12489 #if BFD_DEFAULT_TARGET_SIZE > 32
12490 if (bfd_arch_bits_per_address (abfd
) != 32)
12491 r_sym
= elf64_r_sym
;
12494 r_sym
= elf32_r_sym
;
12496 /* Discover if there are any secondary reloc sections
12497 associated with SEC. */
12498 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12500 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
12502 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
12503 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12505 bfd_byte
* native_relocs
;
12506 bfd_byte
* native_reloc
;
12507 arelent
* internal_relocs
;
12508 arelent
* internal_reloc
;
12510 unsigned int entsize
;
12511 unsigned int symcount
;
12512 unsigned int reloc_count
;
12515 if (ebd
->elf_info_to_howto
== NULL
)
12518 #if DEBUG_SECONDARY_RELOCS
12519 fprintf (stderr
, "read secondary relocs for %s from %s\n",
12520 sec
->name
, relsec
->name
);
12522 entsize
= hdr
->sh_entsize
;
12524 native_relocs
= bfd_malloc (hdr
->sh_size
);
12525 if (native_relocs
== NULL
)
12531 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
12532 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
12534 free (native_relocs
);
12535 bfd_set_error (bfd_error_file_too_big
);
12540 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
12541 if (internal_relocs
== NULL
)
12543 free (native_relocs
);
12548 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
12549 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
12552 free (native_relocs
);
12553 /* The internal_relocs will be freed when
12554 the memory for the bfd is released. */
12559 symcount
= bfd_get_symcount (abfd
);
12561 for (i
= 0, internal_reloc
= internal_relocs
,
12562 native_reloc
= native_relocs
;
12564 i
++, internal_reloc
++, native_reloc
+= entsize
)
12567 Elf_Internal_Rela rela
;
12569 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
12571 /* The address of an ELF reloc is section relative for an object
12572 file, and absolute for an executable file or shared library.
12573 The address of a normal BFD reloc is always section relative,
12574 and the address of a dynamic reloc is absolute.. */
12575 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
12576 internal_reloc
->address
= rela
.r_offset
;
12578 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
12580 if (r_sym (rela
.r_info
) == STN_UNDEF
)
12582 /* FIXME: This and the error case below mean that we
12583 have a symbol on relocs that is not elf_symbol_type. */
12584 internal_reloc
->sym_ptr_ptr
=
12585 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12587 else if (r_sym (rela
.r_info
) > symcount
)
12590 /* xgettext:c-format */
12591 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12592 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
12593 bfd_set_error (bfd_error_bad_value
);
12594 internal_reloc
->sym_ptr_ptr
=
12595 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12602 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
12604 internal_reloc
->sym_ptr_ptr
= ps
;
12605 /* Make sure that this symbol is not removed by strip. */
12606 (*ps
)->flags
|= BSF_KEEP
;
12609 internal_reloc
->addend
= rela
.r_addend
;
12611 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
12612 if (! res
|| internal_reloc
->howto
== NULL
)
12614 #if DEBUG_SECONDARY_RELOCS
12615 fprintf (stderr
, "there is no howto associated with reloc %lx\n",
12622 free (native_relocs
);
12623 /* Store the internal relocs. */
12624 elf_section_data (relsec
)->sec_info
= internal_relocs
;
12631 /* Set the ELF section header fields of an output secondary reloc section. */
12634 _bfd_elf_copy_special_section_fields (const bfd
* ibfd ATTRIBUTE_UNUSED
,
12635 bfd
* obfd ATTRIBUTE_UNUSED
,
12636 const Elf_Internal_Shdr
* isection
,
12637 Elf_Internal_Shdr
* osection
)
12642 if (isection
== NULL
)
12645 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
12648 isec
= isection
->bfd_section
;
12652 osec
= osection
->bfd_section
;
12656 BFD_ASSERT (elf_section_data (osec
)->sec_info
== NULL
);
12657 elf_section_data (osec
)->sec_info
= elf_section_data (isec
)->sec_info
;
12658 osection
->sh_type
= SHT_RELA
;
12659 osection
->sh_link
= elf_onesymtab (obfd
);
12660 if (osection
->sh_link
== 0)
12662 /* There is no symbol table - we are hosed... */
12664 /* xgettext:c-format */
12665 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12667 bfd_set_error (bfd_error_bad_value
);
12671 /* Find the output section that corresponds to the isection's sh_info link. */
12672 if (isection
->sh_info
== 0
12673 || isection
->sh_info
>= elf_numsections (ibfd
))
12676 /* xgettext:c-format */
12677 (_("%pB(%pA): info section index is invalid"),
12679 bfd_set_error (bfd_error_bad_value
);
12683 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
12685 if (isection
== NULL
12686 || isection
->bfd_section
== NULL
12687 || isection
->bfd_section
->output_section
== NULL
)
12690 /* xgettext:c-format */
12691 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12693 bfd_set_error (bfd_error_bad_value
);
12697 osection
->sh_info
=
12698 elf_section_data (isection
->bfd_section
->output_section
)->this_idx
;
12700 #if DEBUG_SECONDARY_RELOCS
12701 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
12702 osec
->name
, osection
->sh_link
, osection
->sh_info
);
12708 /* Write out a secondary reloc section. */
12711 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
12713 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12714 bfd_vma addr_offset
;
12716 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
12717 bfd_boolean result
= TRUE
;
12722 #if BFD_DEFAULT_TARGET_SIZE > 32
12723 if (bfd_arch_bits_per_address (abfd
) != 32)
12724 r_info
= elf64_r_info
;
12727 r_info
= elf32_r_info
;
12729 /* The address of an ELF reloc is section relative for an object
12730 file, and absolute for an executable file or shared library.
12731 The address of a BFD reloc is always section relative. */
12733 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
12734 addr_offset
= sec
->vma
;
12736 /* Discover if there are any secondary reloc sections
12737 associated with SEC. */
12738 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12740 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
12741 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
12743 if (hdr
->sh_type
== SHT_RELA
12744 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12746 asymbol
* last_sym
;
12748 unsigned int reloc_count
;
12750 arelent
* src_irel
;
12751 bfd_byte
* dst_rela
;
12753 if (hdr
->contents
!= NULL
)
12756 /* xgettext:c-format */
12757 (_("%pB(%pA): error: secondary reloc section processed twice"),
12759 bfd_set_error (bfd_error_bad_value
);
12764 reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
12765 if (reloc_count
<= 0)
12768 /* xgettext:c-format */
12769 (_("%pB(%pA): error: secondary reloc section is empty!"),
12771 bfd_set_error (bfd_error_bad_value
);
12776 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
12777 if (hdr
->contents
== NULL
)
12780 #if DEBUG_SECONDARY_RELOCS
12781 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
12782 reloc_count
, sec
->name
, relsec
->name
);
12786 dst_rela
= hdr
->contents
;
12787 src_irel
= (arelent
*) esd
->sec_info
;
12788 if (src_irel
== NULL
)
12791 /* xgettext:c-format */
12792 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12794 bfd_set_error (bfd_error_bad_value
);
12799 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= hdr
->sh_entsize
)
12801 Elf_Internal_Rela src_rela
;
12806 ptr
= src_irel
+ idx
;
12810 /* xgettext:c-format */
12811 (_("%pB(%pA): error: reloc table entry %u is empty"),
12812 abfd
, relsec
, idx
);
12813 bfd_set_error (bfd_error_bad_value
);
12818 if (ptr
->sym_ptr_ptr
== NULL
)
12820 /* FIXME: Is this an error ? */
12825 sym
= *ptr
->sym_ptr_ptr
;
12827 if (sym
== last_sym
)
12831 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
12835 /* xgettext:c-format */
12836 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12837 abfd
, relsec
, idx
);
12838 bfd_set_error (bfd_error_bad_value
);
12847 if (sym
->the_bfd
!= NULL
12848 && sym
->the_bfd
->xvec
!= abfd
->xvec
12849 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
12852 /* xgettext:c-format */
12853 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12854 abfd
, relsec
, idx
);
12855 bfd_set_error (bfd_error_bad_value
);
12861 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
12862 if (ptr
->howto
== NULL
)
12865 /* xgettext:c-format */
12866 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12867 abfd
, relsec
, idx
);
12868 bfd_set_error (bfd_error_bad_value
);
12870 src_rela
.r_info
= r_info (0, 0);
12873 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
12874 src_rela
.r_addend
= ptr
->addend
;
12875 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);